1*243ce5d5SMadhukar Pappireddy /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */ 2630b011fSAntonio Nino Diaz #ifndef LIBFDT_INTERNAL_H 3630b011fSAntonio Nino Diaz #define LIBFDT_INTERNAL_H 4630b011fSAntonio Nino Diaz /* 5630b011fSAntonio Nino Diaz * libfdt - Flat Device Tree manipulation 6630b011fSAntonio Nino Diaz * Copyright (C) 2006 David Gibson, IBM Corporation. 7630b011fSAntonio Nino Diaz */ 8630b011fSAntonio Nino Diaz #include <fdt.h> 9630b011fSAntonio Nino Diaz 10630b011fSAntonio Nino Diaz #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 11630b011fSAntonio Nino Diaz #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) 12630b011fSAntonio Nino Diaz 13*243ce5d5SMadhukar Pappireddy int32_t fdt_ro_probe_(const void *fdt); 14*243ce5d5SMadhukar Pappireddy #define FDT_RO_PROBE(fdt) \ 15630b011fSAntonio Nino Diaz { \ 16*243ce5d5SMadhukar Pappireddy int32_t totalsize_; \ 17*243ce5d5SMadhukar Pappireddy if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ 18*243ce5d5SMadhukar Pappireddy return totalsize_; \ 19630b011fSAntonio Nino Diaz } 20630b011fSAntonio Nino Diaz 21630b011fSAntonio Nino Diaz int fdt_check_node_offset_(const void *fdt, int offset); 22630b011fSAntonio Nino Diaz int fdt_check_prop_offset_(const void *fdt, int offset); 23630b011fSAntonio Nino Diaz const char *fdt_find_string_(const char *strtab, int tabsize, const char *s); 24630b011fSAntonio Nino Diaz int fdt_node_end_offset_(void *fdt, int nodeoffset); 25630b011fSAntonio Nino Diaz 26630b011fSAntonio Nino Diaz static inline const void *fdt_offset_ptr_(const void *fdt, int offset) 27630b011fSAntonio Nino Diaz { 28630b011fSAntonio Nino Diaz return (const char *)fdt + fdt_off_dt_struct(fdt) + offset; 29630b011fSAntonio Nino Diaz } 30630b011fSAntonio Nino Diaz 31630b011fSAntonio Nino Diaz static inline void *fdt_offset_ptr_w_(void *fdt, int offset) 32630b011fSAntonio Nino Diaz { 33630b011fSAntonio Nino Diaz return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset); 34630b011fSAntonio Nino Diaz } 35630b011fSAntonio Nino Diaz 36630b011fSAntonio Nino Diaz static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n) 37630b011fSAntonio Nino Diaz { 38630b011fSAntonio Nino Diaz const struct fdt_reserve_entry *rsv_table = 39630b011fSAntonio Nino Diaz (const struct fdt_reserve_entry *) 40630b011fSAntonio Nino Diaz ((const char *)fdt + fdt_off_mem_rsvmap(fdt)); 41630b011fSAntonio Nino Diaz 42630b011fSAntonio Nino Diaz return rsv_table + n; 43630b011fSAntonio Nino Diaz } 44630b011fSAntonio Nino Diaz static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n) 45630b011fSAntonio Nino Diaz { 46630b011fSAntonio Nino Diaz return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n); 47630b011fSAntonio Nino Diaz } 48630b011fSAntonio Nino Diaz 49630b011fSAntonio Nino Diaz #define FDT_SW_MAGIC (~FDT_MAGIC) 50630b011fSAntonio Nino Diaz 51*243ce5d5SMadhukar Pappireddy /**********************************************************************/ 52*243ce5d5SMadhukar Pappireddy /* Checking controls */ 53*243ce5d5SMadhukar Pappireddy /**********************************************************************/ 54*243ce5d5SMadhukar Pappireddy 55*243ce5d5SMadhukar Pappireddy #ifndef FDT_ASSUME_MASK 56*243ce5d5SMadhukar Pappireddy #define FDT_ASSUME_MASK 0 57*243ce5d5SMadhukar Pappireddy #endif 58*243ce5d5SMadhukar Pappireddy 59*243ce5d5SMadhukar Pappireddy /* 60*243ce5d5SMadhukar Pappireddy * Defines assumptions which can be enabled. Each of these can be enabled 61*243ce5d5SMadhukar Pappireddy * individually. For maximum safety, don't enable any assumptions! 62*243ce5d5SMadhukar Pappireddy * 63*243ce5d5SMadhukar Pappireddy * For minimal code size and no safety, use ASSUME_PERFECT at your own risk. 64*243ce5d5SMadhukar Pappireddy * You should have another method of validating the device tree, such as a 65*243ce5d5SMadhukar Pappireddy * signature or hash check before using libfdt. 66*243ce5d5SMadhukar Pappireddy * 67*243ce5d5SMadhukar Pappireddy * For situations where security is not a concern it may be safe to enable 68*243ce5d5SMadhukar Pappireddy * ASSUME_SANE. 69*243ce5d5SMadhukar Pappireddy */ 70*243ce5d5SMadhukar Pappireddy enum { 71*243ce5d5SMadhukar Pappireddy /* 72*243ce5d5SMadhukar Pappireddy * This does essentially no checks. Only the latest device-tree 73*243ce5d5SMadhukar Pappireddy * version is correctly handled. Inconsistencies or errors in the device 74*243ce5d5SMadhukar Pappireddy * tree may cause undefined behaviour or crashes. Invalid parameters 75*243ce5d5SMadhukar Pappireddy * passed to libfdt may do the same. 76*243ce5d5SMadhukar Pappireddy * 77*243ce5d5SMadhukar Pappireddy * If an error occurs when modifying the tree it may leave the tree in 78*243ce5d5SMadhukar Pappireddy * an intermediate (but valid) state. As an example, adding a property 79*243ce5d5SMadhukar Pappireddy * where there is insufficient space may result in the property name 80*243ce5d5SMadhukar Pappireddy * being added to the string table even though the property itself is 81*243ce5d5SMadhukar Pappireddy * not added to the struct section. 82*243ce5d5SMadhukar Pappireddy * 83*243ce5d5SMadhukar Pappireddy * Only use this if you have a fully validated device tree with 84*243ce5d5SMadhukar Pappireddy * the latest supported version and wish to minimise code size. 85*243ce5d5SMadhukar Pappireddy */ 86*243ce5d5SMadhukar Pappireddy ASSUME_PERFECT = 0xff, 87*243ce5d5SMadhukar Pappireddy 88*243ce5d5SMadhukar Pappireddy /* 89*243ce5d5SMadhukar Pappireddy * This assumes that the device tree is sane. i.e. header metadata 90*243ce5d5SMadhukar Pappireddy * and basic hierarchy are correct. 91*243ce5d5SMadhukar Pappireddy * 92*243ce5d5SMadhukar Pappireddy * With this assumption enabled, normal device trees produced by libfdt 93*243ce5d5SMadhukar Pappireddy * and the compiler should be handled safely. Malicious device trees and 94*243ce5d5SMadhukar Pappireddy * complete garbage may cause libfdt to behave badly or crash. Truncated 95*243ce5d5SMadhukar Pappireddy * device trees (e.g. those only partially loaded) can also cause 96*243ce5d5SMadhukar Pappireddy * problems. 97*243ce5d5SMadhukar Pappireddy * 98*243ce5d5SMadhukar Pappireddy * Note: Only checks that relate exclusively to the device tree itself 99*243ce5d5SMadhukar Pappireddy * (not the parameters passed to libfdt) are disabled by this 100*243ce5d5SMadhukar Pappireddy * assumption. This includes checking headers, tags and the like. 101*243ce5d5SMadhukar Pappireddy */ 102*243ce5d5SMadhukar Pappireddy ASSUME_VALID_DTB = 1 << 0, 103*243ce5d5SMadhukar Pappireddy 104*243ce5d5SMadhukar Pappireddy /* 105*243ce5d5SMadhukar Pappireddy * This builds on ASSUME_VALID_DTB and further assumes that libfdt 106*243ce5d5SMadhukar Pappireddy * functions are called with valid parameters, i.e. not trigger 107*243ce5d5SMadhukar Pappireddy * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any 108*243ce5d5SMadhukar Pappireddy * extensive checking of parameters and the device tree, making various 109*243ce5d5SMadhukar Pappireddy * assumptions about correctness. 110*243ce5d5SMadhukar Pappireddy * 111*243ce5d5SMadhukar Pappireddy * It doesn't make sense to enable this assumption unless 112*243ce5d5SMadhukar Pappireddy * ASSUME_VALID_DTB is also enabled. 113*243ce5d5SMadhukar Pappireddy */ 114*243ce5d5SMadhukar Pappireddy ASSUME_VALID_INPUT = 1 << 1, 115*243ce5d5SMadhukar Pappireddy 116*243ce5d5SMadhukar Pappireddy /* 117*243ce5d5SMadhukar Pappireddy * This disables checks for device-tree version and removes all code 118*243ce5d5SMadhukar Pappireddy * which handles older versions. 119*243ce5d5SMadhukar Pappireddy * 120*243ce5d5SMadhukar Pappireddy * Only enable this if you know you have a device tree with the latest 121*243ce5d5SMadhukar Pappireddy * version. 122*243ce5d5SMadhukar Pappireddy */ 123*243ce5d5SMadhukar Pappireddy ASSUME_LATEST = 1 << 2, 124*243ce5d5SMadhukar Pappireddy 125*243ce5d5SMadhukar Pappireddy /* 126*243ce5d5SMadhukar Pappireddy * This assumes that it is OK for a failed addition to the device tree, 127*243ce5d5SMadhukar Pappireddy * due to lack of space or some other problem, to skip any rollback 128*243ce5d5SMadhukar Pappireddy * steps (such as dropping the property name from the string table). 129*243ce5d5SMadhukar Pappireddy * This is safe to enable in most circumstances, even though it may 130*243ce5d5SMadhukar Pappireddy * leave the tree in a sub-optimal state. 131*243ce5d5SMadhukar Pappireddy */ 132*243ce5d5SMadhukar Pappireddy ASSUME_NO_ROLLBACK = 1 << 3, 133*243ce5d5SMadhukar Pappireddy 134*243ce5d5SMadhukar Pappireddy /* 135*243ce5d5SMadhukar Pappireddy * This assumes that the device tree components appear in a 'convenient' 136*243ce5d5SMadhukar Pappireddy * order, i.e. the memory reservation block first, then the structure 137*243ce5d5SMadhukar Pappireddy * block and finally the string block. 138*243ce5d5SMadhukar Pappireddy * 139*243ce5d5SMadhukar Pappireddy * This order is not specified by the device-tree specification, 140*243ce5d5SMadhukar Pappireddy * but is expected by libfdt. The device-tree compiler always created 141*243ce5d5SMadhukar Pappireddy * device trees with this order. 142*243ce5d5SMadhukar Pappireddy * 143*243ce5d5SMadhukar Pappireddy * This assumption disables a check in fdt_open_into() and removes the 144*243ce5d5SMadhukar Pappireddy * ability to fix the problem there. This is safe if you know that the 145*243ce5d5SMadhukar Pappireddy * device tree is correctly ordered. See fdt_blocks_misordered_(). 146*243ce5d5SMadhukar Pappireddy */ 147*243ce5d5SMadhukar Pappireddy ASSUME_LIBFDT_ORDER = 1 << 4, 148*243ce5d5SMadhukar Pappireddy 149*243ce5d5SMadhukar Pappireddy /* 150*243ce5d5SMadhukar Pappireddy * This assumes that libfdt itself does not have any internal bugs. It 151*243ce5d5SMadhukar Pappireddy * drops certain checks that should never be needed unless libfdt has an 152*243ce5d5SMadhukar Pappireddy * undiscovered bug. 153*243ce5d5SMadhukar Pappireddy * 154*243ce5d5SMadhukar Pappireddy * This can generally be considered safe to enable. 155*243ce5d5SMadhukar Pappireddy */ 156*243ce5d5SMadhukar Pappireddy ASSUME_LIBFDT_FLAWLESS = 1 << 5, 157*243ce5d5SMadhukar Pappireddy }; 158*243ce5d5SMadhukar Pappireddy 159*243ce5d5SMadhukar Pappireddy /** 160*243ce5d5SMadhukar Pappireddy * can_assume_() - check if a particular assumption is enabled 161*243ce5d5SMadhukar Pappireddy * 162*243ce5d5SMadhukar Pappireddy * @mask: Mask to check (ASSUME_...) 163*243ce5d5SMadhukar Pappireddy * @return true if that assumption is enabled, else false 164*243ce5d5SMadhukar Pappireddy */ 165*243ce5d5SMadhukar Pappireddy static inline bool can_assume_(int mask) 166*243ce5d5SMadhukar Pappireddy { 167*243ce5d5SMadhukar Pappireddy return FDT_ASSUME_MASK & mask; 168*243ce5d5SMadhukar Pappireddy } 169*243ce5d5SMadhukar Pappireddy 170*243ce5d5SMadhukar Pappireddy /** helper macros for checking assumptions */ 171*243ce5d5SMadhukar Pappireddy #define can_assume(_assume) can_assume_(ASSUME_ ## _assume) 172*243ce5d5SMadhukar Pappireddy 173630b011fSAntonio Nino Diaz #endif /* LIBFDT_INTERNAL_H */ 174