1 /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */ 2 #ifndef LIBFDT_INTERNAL_H 3 #define LIBFDT_INTERNAL_H 4 /* 5 * libfdt - Flat Device Tree manipulation 6 * Copyright (C) 2006 David Gibson, IBM Corporation. 7 */ 8 #include <fdt.h> 9 10 #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 11 #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) 12 13 int32_t fdt_ro_probe_(const void *fdt); 14 #define FDT_RO_PROBE(fdt) \ 15 { \ 16 int32_t totalsize_; \ 17 if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ 18 return totalsize_; \ 19 } 20 21 int fdt_check_node_offset_(const void *fdt, int offset); 22 int fdt_check_prop_offset_(const void *fdt, int offset); 23 24 const char *fdt_find_string_len_(const char *strtab, int tabsize, const char *s, 25 int s_len); 26 static inline const char *fdt_find_string_(const char *strtab, int tabsize, 27 const char *s) 28 { 29 return fdt_find_string_len_(strtab, tabsize, s, strlen(s)); 30 } 31 32 int fdt_node_end_offset_(void *fdt, int nodeoffset); 33 34 static inline const void *fdt_offset_ptr_(const void *fdt, int offset) 35 { 36 return (const char *)fdt + fdt_off_dt_struct(fdt) + offset; 37 } 38 39 static inline void *fdt_offset_ptr_w_(void *fdt, int offset) 40 { 41 return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset); 42 } 43 44 static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n) 45 { 46 const struct fdt_reserve_entry *rsv_table = 47 (const struct fdt_reserve_entry *) 48 ((const char *)fdt + fdt_off_mem_rsvmap(fdt)); 49 50 return rsv_table + n; 51 } 52 static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n) 53 { 54 return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n); 55 } 56 57 /* 58 * Internal helpers to access structural elements of the device tree 59 * blob (rather than for example reading integers from within property 60 * values). We assume that we are either given a naturally aligned 61 * address for the platform or if we are not, we are on a platform 62 * where unaligned memory reads will be handled in a graceful manner. 63 * If not the external helpers fdtXX_ld() from libfdt.h can be used 64 * instead. 65 */ 66 static inline uint32_t fdt32_ld_(const fdt32_t *p) 67 { 68 return fdt32_to_cpu(*p); 69 } 70 71 static inline uint64_t fdt64_ld_(const fdt64_t *p) 72 { 73 return fdt64_to_cpu(*p); 74 } 75 76 #define FDT_SW_MAGIC (~FDT_MAGIC) 77 78 /**********************************************************************/ 79 /* Checking controls */ 80 /**********************************************************************/ 81 82 #ifndef FDT_ASSUME_MASK 83 #define FDT_ASSUME_MASK 0 84 #endif 85 86 /* 87 * Defines assumptions which can be enabled. Each of these can be enabled 88 * individually. For maximum safety, don't enable any assumptions! 89 * 90 * For minimal code size and no safety, use ASSUME_PERFECT at your own risk. 91 * You should have another method of validating the device tree, such as a 92 * signature or hash check before using libfdt. 93 * 94 * For situations where security is not a concern it may be safe to enable 95 * ASSUME_SANE. 96 */ 97 enum { 98 /* 99 * This does essentially no checks. Only the latest device-tree 100 * version is correctly handled. Inconsistencies or errors in the device 101 * tree may cause undefined behaviour or crashes. Invalid parameters 102 * passed to libfdt may do the same. 103 * 104 * If an error occurs when modifying the tree it may leave the tree in 105 * an intermediate (but valid) state. As an example, adding a property 106 * where there is insufficient space may result in the property name 107 * being added to the string table even though the property itself is 108 * not added to the struct section. 109 * 110 * Only use this if you have a fully validated device tree with 111 * the latest supported version and wish to minimise code size. 112 */ 113 ASSUME_PERFECT = 0xff, 114 115 /* 116 * This assumes that the device tree is sane. i.e. header metadata 117 * and basic hierarchy are correct. 118 * 119 * With this assumption enabled, normal device trees produced by libfdt 120 * and the compiler should be handled safely. Malicious device trees and 121 * complete garbage may cause libfdt to behave badly or crash. Truncated 122 * device trees (e.g. those only partially loaded) can also cause 123 * problems. 124 * 125 * Note: Only checks that relate exclusively to the device tree itself 126 * (not the parameters passed to libfdt) are disabled by this 127 * assumption. This includes checking headers, tags and the like. 128 */ 129 ASSUME_VALID_DTB = 1 << 0, 130 131 /* 132 * This builds on ASSUME_VALID_DTB and further assumes that libfdt 133 * functions are called with valid parameters, i.e. not trigger 134 * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any 135 * extensive checking of parameters and the device tree, making various 136 * assumptions about correctness. 137 * 138 * It doesn't make sense to enable this assumption unless 139 * ASSUME_VALID_DTB is also enabled. 140 */ 141 ASSUME_VALID_INPUT = 1 << 1, 142 143 /* 144 * This disables checks for device-tree version and removes all code 145 * which handles older versions. 146 * 147 * Only enable this if you know you have a device tree with the latest 148 * version. 149 */ 150 ASSUME_LATEST = 1 << 2, 151 152 /* 153 * This assumes that it is OK for a failed addition to the device tree, 154 * due to lack of space or some other problem, to skip any rollback 155 * steps (such as dropping the property name from the string table). 156 * This is safe to enable in most circumstances, even though it may 157 * leave the tree in a sub-optimal state. 158 */ 159 ASSUME_NO_ROLLBACK = 1 << 3, 160 161 /* 162 * This assumes that the device tree components appear in a 'convenient' 163 * order, i.e. the memory reservation block first, then the structure 164 * block and finally the string block. 165 * 166 * This order is not specified by the device-tree specification, 167 * but is expected by libfdt. The device-tree compiler always created 168 * device trees with this order. 169 * 170 * This assumption disables a check in fdt_open_into() and removes the 171 * ability to fix the problem there. This is safe if you know that the 172 * device tree is correctly ordered. See fdt_blocks_misordered_(). 173 */ 174 ASSUME_LIBFDT_ORDER = 1 << 4, 175 176 /* 177 * This assumes that libfdt itself does not have any internal bugs. It 178 * drops certain checks that should never be needed unless libfdt has an 179 * undiscovered bug. 180 * 181 * This can generally be considered safe to enable. 182 */ 183 ASSUME_LIBFDT_FLAWLESS = 1 << 5, 184 }; 185 186 /** 187 * can_assume_() - check if a particular assumption is enabled 188 * 189 * @mask: Mask to check (ASSUME_...) 190 * @return true if that assumption is enabled, else false 191 */ 192 static inline bool can_assume_(int mask) 193 { 194 return FDT_ASSUME_MASK & mask; 195 } 196 197 /** helper macros for checking assumptions */ 198 #define can_assume(_assume) can_assume_(ASSUME_ ## _assume) 199 200 #endif /* LIBFDT_INTERNAL_H */ 201