1 // SPDX-License-Identifier: (GPL-2.0-or-later 1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) 2 /* 2 /* 3 * libfdt - Flat Device Tree manipulation 3 * libfdt - Flat Device Tree manipulation 4 * Copyright (C) 2006 David Gibson, IBM Corpor 4 * Copyright (C) 2006 David Gibson, IBM Corporation. 5 */ 5 */ 6 #include "libfdt_env.h" 6 #include "libfdt_env.h" 7 7 8 #include <fdt.h> 8 #include <fdt.h> 9 #include <libfdt.h> 9 #include <libfdt.h> 10 10 11 #include "libfdt_internal.h" 11 #include "libfdt_internal.h" 12 12 13 /* 13 /* 14 * Minimal sanity check for a read-only tree. 14 * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks 15 * that the given buffer contains what appears 15 * that the given buffer contains what appears to be a flattened 16 * device tree with sane information in its he 16 * device tree with sane information in its header. 17 */ 17 */ 18 int32_t fdt_ro_probe_(const void *fdt) 18 int32_t fdt_ro_probe_(const void *fdt) 19 { 19 { 20 uint32_t totalsize = fdt_totalsize(fdt 20 uint32_t totalsize = fdt_totalsize(fdt); 21 21 22 if (can_assume(VALID_DTB)) << 23 return totalsize; << 24 << 25 /* The device tree must be at an 8-byt << 26 if ((uintptr_t)fdt & 7) << 27 return -FDT_ERR_ALIGNMENT; << 28 << 29 if (fdt_magic(fdt) == FDT_MAGIC) { 22 if (fdt_magic(fdt) == FDT_MAGIC) { 30 /* Complete tree */ 23 /* Complete tree */ 31 if (!can_assume(LATEST)) { !! 24 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) 32 if (fdt_version(fdt) < !! 25 return -FDT_ERR_BADVERSION; 33 return -FDT_ER !! 26 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) 34 if (fdt_last_comp_vers !! 27 return -FDT_ERR_BADVERSION; 35 FDT_LA << 36 return -FDT_ER << 37 } << 38 } else if (fdt_magic(fdt) == FDT_SW_MA 28 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { 39 /* Unfinished sequential-write 29 /* Unfinished sequential-write blob */ 40 if (!can_assume(VALID_INPUT) & !! 30 if (fdt_size_dt_struct(fdt) == 0) 41 return -FDT_ERR_BADSTA 31 return -FDT_ERR_BADSTATE; 42 } else { 32 } else { 43 return -FDT_ERR_BADMAGIC; 33 return -FDT_ERR_BADMAGIC; 44 } 34 } 45 35 46 if (totalsize < INT32_MAX) 36 if (totalsize < INT32_MAX) 47 return totalsize; 37 return totalsize; 48 else 38 else 49 return -FDT_ERR_TRUNCATED; 39 return -FDT_ERR_TRUNCATED; 50 } 40 } 51 41 52 static int check_off_(uint32_t hdrsize, uint32 42 static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off) 53 { 43 { 54 return (off >= hdrsize) && (off <= tot 44 return (off >= hdrsize) && (off <= totalsize); 55 } 45 } 56 46 57 static int check_block_(uint32_t hdrsize, uint 47 static int check_block_(uint32_t hdrsize, uint32_t totalsize, 58 uint32_t base, uint32_ 48 uint32_t base, uint32_t size) 59 { 49 { 60 if (!check_off_(hdrsize, totalsize, ba 50 if (!check_off_(hdrsize, totalsize, base)) 61 return 0; /* block start out o 51 return 0; /* block start out of bounds */ 62 if ((base + size) < base) 52 if ((base + size) < base) 63 return 0; /* overflow */ 53 return 0; /* overflow */ 64 if (!check_off_(hdrsize, totalsize, ba 54 if (!check_off_(hdrsize, totalsize, base + size)) 65 return 0; /* block end out of 55 return 0; /* block end out of bounds */ 66 return 1; 56 return 1; 67 } 57 } 68 58 69 size_t fdt_header_size_(uint32_t version) 59 size_t fdt_header_size_(uint32_t version) 70 { 60 { 71 if (version <= 1) 61 if (version <= 1) 72 return FDT_V1_SIZE; 62 return FDT_V1_SIZE; 73 else if (version <= 2) 63 else if (version <= 2) 74 return FDT_V2_SIZE; 64 return FDT_V2_SIZE; 75 else if (version <= 3) 65 else if (version <= 3) 76 return FDT_V3_SIZE; 66 return FDT_V3_SIZE; 77 else if (version <= 16) 67 else if (version <= 16) 78 return FDT_V16_SIZE; 68 return FDT_V16_SIZE; 79 else 69 else 80 return FDT_V17_SIZE; 70 return FDT_V17_SIZE; 81 } 71 } 82 72 83 size_t fdt_header_size(const void *fdt) << 84 { << 85 return can_assume(LATEST) ? FDT_V17_SI << 86 fdt_header_size_(fdt_version(f << 87 } << 88 << 89 int fdt_check_header(const void *fdt) 73 int fdt_check_header(const void *fdt) 90 { 74 { 91 size_t hdrsize; 75 size_t hdrsize; 92 76 93 /* The device tree must be at an 8-byt << 94 if ((uintptr_t)fdt & 7) << 95 return -FDT_ERR_ALIGNMENT; << 96 << 97 if (fdt_magic(fdt) != FDT_MAGIC) 77 if (fdt_magic(fdt) != FDT_MAGIC) 98 return -FDT_ERR_BADMAGIC; 78 return -FDT_ERR_BADMAGIC; 99 if (!can_assume(LATEST)) { << 100 if ((fdt_version(fdt) < FDT_FI << 101 || (fdt_last_comp_version( << 102 FDT_LAST_SUPPORTED_VER << 103 return -FDT_ERR_BADVER << 104 if (fdt_version(fdt) < fdt_las << 105 return -FDT_ERR_BADVER << 106 } << 107 hdrsize = fdt_header_size(fdt); 79 hdrsize = fdt_header_size(fdt); 108 if (!can_assume(VALID_DTB)) { !! 80 if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) 109 if ((fdt_totalsize(fdt) < hdrs !! 81 || (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)) 110 || (fdt_totalsize(fdt) > I !! 82 return -FDT_ERR_BADVERSION; 111 return -FDT_ERR_TRUNCA !! 83 if (fdt_version(fdt) < fdt_last_comp_version(fdt)) >> 84 return -FDT_ERR_BADVERSION; 112 85 113 /* Bounds check memrsv block * !! 86 if ((fdt_totalsize(fdt) < hdrsize) 114 if (!check_off_(hdrsize, fdt_t !! 87 || (fdt_totalsize(fdt) > INT_MAX)) 115 fdt_off_mem_rs !! 88 return -FDT_ERR_TRUNCATED; 116 return -FDT_ERR_TRUNCA << 117 89 118 /* Bounds check structure bloc !! 90 /* Bounds check memrsv block */ 119 if (!can_assume(LATEST) && fdt !! 91 if (!check_off_(hdrsize, fdt_totalsize(fdt), fdt_off_mem_rsvmap(fdt))) 120 if (!check_off_(hdrsiz !! 92 return -FDT_ERR_TRUNCATED; 121 fdt_of << 122 return -FDT_ER << 123 } else { << 124 if (!check_block_(hdrs << 125 fdt_ << 126 fdt_ << 127 return -FDT_ER << 128 } << 129 93 130 /* Bounds check strings block !! 94 /* Bounds check structure block */ >> 95 if (fdt_version(fdt) < 17) { >> 96 if (!check_off_(hdrsize, fdt_totalsize(fdt), >> 97 fdt_off_dt_struct(fdt))) >> 98 return -FDT_ERR_TRUNCATED; >> 99 } else { 131 if (!check_block_(hdrsize, fdt 100 if (!check_block_(hdrsize, fdt_totalsize(fdt), 132 fdt_off_dt_s !! 101 fdt_off_dt_struct(fdt), 133 fdt_size_dt_ !! 102 fdt_size_dt_struct(fdt))) 134 return -FDT_ERR_TRUNCA 103 return -FDT_ERR_TRUNCATED; 135 } 104 } 136 105 >> 106 /* Bounds check strings block */ >> 107 if (!check_block_(hdrsize, fdt_totalsize(fdt), >> 108 fdt_off_dt_strings(fdt), fdt_size_dt_strings(fdt))) >> 109 return -FDT_ERR_TRUNCATED; >> 110 137 return 0; 111 return 0; 138 } 112 } 139 113 140 const void *fdt_offset_ptr(const void *fdt, in 114 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) 141 { 115 { 142 unsigned int uoffset = offset; !! 116 unsigned absoffset = offset + fdt_off_dt_struct(fdt); 143 unsigned int absoffset = offset + fdt_ << 144 117 145 if (offset < 0) !! 118 if ((absoffset < offset) >> 119 || ((absoffset + len) < absoffset) >> 120 || (absoffset + len) > fdt_totalsize(fdt)) 146 return NULL; 121 return NULL; 147 122 148 if (!can_assume(VALID_INPUT)) !! 123 if (fdt_version(fdt) >= 0x11) 149 if ((absoffset < uoffset) !! 124 if (((offset + len) < offset) 150 || ((absoffset + len) < ab << 151 || (absoffset + len) > fdt << 152 return NULL; << 153 << 154 if (can_assume(LATEST) || fdt_version( << 155 if (((uoffset + len) < uoffset << 156 || ((offset + len) > fdt_s 125 || ((offset + len) > fdt_size_dt_struct(fdt))) 157 return NULL; 126 return NULL; 158 127 159 return fdt_offset_ptr_(fdt, offset); 128 return fdt_offset_ptr_(fdt, offset); 160 } 129 } 161 130 162 uint32_t fdt_next_tag(const void *fdt, int sta 131 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) 163 { 132 { 164 const fdt32_t *tagp, *lenp; 133 const fdt32_t *tagp, *lenp; 165 uint32_t tag, len, sum; !! 134 uint32_t tag; 166 int offset = startoffset; 135 int offset = startoffset; 167 const char *p; 136 const char *p; 168 137 169 *nextoffset = -FDT_ERR_TRUNCATED; 138 *nextoffset = -FDT_ERR_TRUNCATED; 170 tagp = fdt_offset_ptr(fdt, offset, FDT 139 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); 171 if (!can_assume(VALID_DTB) && !tagp) !! 140 if (!tagp) 172 return FDT_END; /* premature e 141 return FDT_END; /* premature end */ 173 tag = fdt32_to_cpu(*tagp); 142 tag = fdt32_to_cpu(*tagp); 174 offset += FDT_TAGSIZE; 143 offset += FDT_TAGSIZE; 175 144 176 *nextoffset = -FDT_ERR_BADSTRUCTURE; 145 *nextoffset = -FDT_ERR_BADSTRUCTURE; 177 switch (tag) { 146 switch (tag) { 178 case FDT_BEGIN_NODE: 147 case FDT_BEGIN_NODE: 179 /* skip name */ 148 /* skip name */ 180 do { 149 do { 181 p = fdt_offset_ptr(fdt 150 p = fdt_offset_ptr(fdt, offset++, 1); 182 } while (p && (*p != '\0')); 151 } while (p && (*p != '\0')); 183 if (!can_assume(VALID_DTB) && !! 152 if (!p) 184 return FDT_END; /* pre 153 return FDT_END; /* premature end */ 185 break; 154 break; 186 155 187 case FDT_PROP: 156 case FDT_PROP: 188 lenp = fdt_offset_ptr(fdt, off 157 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); 189 if (!can_assume(VALID_DTB) && !! 158 if (!lenp) 190 return FDT_END; /* pre 159 return FDT_END; /* premature end */ 191 << 192 len = fdt32_to_cpu(*lenp); << 193 sum = len + offset; << 194 if (!can_assume(VALID_DTB) && << 195 (INT_MAX <= sum || sum < ( << 196 return FDT_END; /* pre << 197 << 198 /* skip-name offset, length an 160 /* skip-name offset, length and value */ 199 offset += sizeof(struct fdt_pr !! 161 offset += sizeof(struct fdt_property) - FDT_TAGSIZE 200 !! 162 + fdt32_to_cpu(*lenp); 201 if (!can_assume(LATEST) && !! 163 if (fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 && 202 fdt_version(fdt) < 0x10 && !! 164 ((offset - fdt32_to_cpu(*lenp)) % 8) != 0) 203 ((offset - len) % 8) != 0) << 204 offset += 4; 165 offset += 4; 205 break; 166 break; 206 167 207 case FDT_END: 168 case FDT_END: 208 case FDT_END_NODE: 169 case FDT_END_NODE: 209 case FDT_NOP: 170 case FDT_NOP: 210 break; 171 break; 211 172 212 default: 173 default: 213 return FDT_END; 174 return FDT_END; 214 } 175 } 215 176 216 if (!fdt_offset_ptr(fdt, startoffset, 177 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) 217 return FDT_END; /* premature e 178 return FDT_END; /* premature end */ 218 179 219 *nextoffset = FDT_TAGALIGN(offset); 180 *nextoffset = FDT_TAGALIGN(offset); 220 return tag; 181 return tag; 221 } 182 } 222 183 223 int fdt_check_node_offset_(const void *fdt, in 184 int fdt_check_node_offset_(const void *fdt, int offset) 224 { 185 { 225 if (!can_assume(VALID_INPUT) !! 186 if ((offset < 0) || (offset % FDT_TAGSIZE) 226 && ((offset < 0) || (offset % FDT_ !! 187 || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)) 227 return -FDT_ERR_BADOFFSET; << 228 << 229 if (fdt_next_tag(fdt, offset, &offset) << 230 return -FDT_ERR_BADOFFSET; 188 return -FDT_ERR_BADOFFSET; 231 189 232 return offset; 190 return offset; 233 } 191 } 234 192 235 int fdt_check_prop_offset_(const void *fdt, in 193 int fdt_check_prop_offset_(const void *fdt, int offset) 236 { 194 { 237 if (!can_assume(VALID_INPUT) !! 195 if ((offset < 0) || (offset % FDT_TAGSIZE) 238 && ((offset < 0) || (offset % FDT_ !! 196 || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)) 239 return -FDT_ERR_BADOFFSET; << 240 << 241 if (fdt_next_tag(fdt, offset, &offset) << 242 return -FDT_ERR_BADOFFSET; 197 return -FDT_ERR_BADOFFSET; 243 198 244 return offset; 199 return offset; 245 } 200 } 246 201 247 int fdt_next_node(const void *fdt, int offset, 202 int fdt_next_node(const void *fdt, int offset, int *depth) 248 { 203 { 249 int nextoffset = 0; 204 int nextoffset = 0; 250 uint32_t tag; 205 uint32_t tag; 251 206 252 if (offset >= 0) 207 if (offset >= 0) 253 if ((nextoffset = fdt_check_no 208 if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0) 254 return nextoffset; 209 return nextoffset; 255 210 256 do { 211 do { 257 offset = nextoffset; 212 offset = nextoffset; 258 tag = fdt_next_tag(fdt, offset 213 tag = fdt_next_tag(fdt, offset, &nextoffset); 259 214 260 switch (tag) { 215 switch (tag) { 261 case FDT_PROP: 216 case FDT_PROP: 262 case FDT_NOP: 217 case FDT_NOP: 263 break; 218 break; 264 219 265 case FDT_BEGIN_NODE: 220 case FDT_BEGIN_NODE: 266 if (depth) 221 if (depth) 267 (*depth)++; 222 (*depth)++; 268 break; 223 break; 269 224 270 case FDT_END_NODE: 225 case FDT_END_NODE: 271 if (depth && ((--(*dep 226 if (depth && ((--(*depth)) < 0)) 272 return nextoff 227 return nextoffset; 273 break; 228 break; 274 229 275 case FDT_END: 230 case FDT_END: 276 if ((nextoffset >= 0) 231 if ((nextoffset >= 0) 277 || ((nextoffset == 232 || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) 278 return -FDT_ER 233 return -FDT_ERR_NOTFOUND; 279 else 234 else 280 return nextoff 235 return nextoffset; 281 } 236 } 282 } while (tag != FDT_BEGIN_NODE); 237 } while (tag != FDT_BEGIN_NODE); 283 238 284 return offset; 239 return offset; 285 } 240 } 286 241 287 int fdt_first_subnode(const void *fdt, int off 242 int fdt_first_subnode(const void *fdt, int offset) 288 { 243 { 289 int depth = 0; 244 int depth = 0; 290 245 291 offset = fdt_next_node(fdt, offset, &d 246 offset = fdt_next_node(fdt, offset, &depth); 292 if (offset < 0 || depth != 1) 247 if (offset < 0 || depth != 1) 293 return -FDT_ERR_NOTFOUND; 248 return -FDT_ERR_NOTFOUND; 294 249 295 return offset; 250 return offset; 296 } 251 } 297 252 298 int fdt_next_subnode(const void *fdt, int offs 253 int fdt_next_subnode(const void *fdt, int offset) 299 { 254 { 300 int depth = 1; 255 int depth = 1; 301 256 302 /* 257 /* 303 * With respect to the parent, the dep 258 * With respect to the parent, the depth of the next subnode will be 304 * the same as the last. 259 * the same as the last. 305 */ 260 */ 306 do { 261 do { 307 offset = fdt_next_node(fdt, of 262 offset = fdt_next_node(fdt, offset, &depth); 308 if (offset < 0 || depth < 1) 263 if (offset < 0 || depth < 1) 309 return -FDT_ERR_NOTFOU 264 return -FDT_ERR_NOTFOUND; 310 } while (depth > 1); 265 } while (depth > 1); 311 266 312 return offset; 267 return offset; 313 } 268 } 314 269 315 const char *fdt_find_string_(const char *strta 270 const char *fdt_find_string_(const char *strtab, int tabsize, const char *s) 316 { 271 { 317 int len = strlen(s) + 1; 272 int len = strlen(s) + 1; 318 const char *last = strtab + tabsize - 273 const char *last = strtab + tabsize - len; 319 const char *p; 274 const char *p; 320 275 321 for (p = strtab; p <= last; p++) 276 for (p = strtab; p <= last; p++) 322 if (memcmp(p, s, len) == 0) 277 if (memcmp(p, s, len) == 0) 323 return p; 278 return p; 324 return NULL; 279 return NULL; 325 } 280 } 326 281 327 int fdt_move(const void *fdt, void *buf, int b 282 int fdt_move(const void *fdt, void *buf, int bufsize) 328 { 283 { 329 if (!can_assume(VALID_INPUT) && bufsiz << 330 return -FDT_ERR_NOSPACE; << 331 << 332 FDT_RO_PROBE(fdt); 284 FDT_RO_PROBE(fdt); 333 285 334 if (fdt_totalsize(fdt) > (unsigned int !! 286 if (fdt_totalsize(fdt) > bufsize) 335 return -FDT_ERR_NOSPACE; 287 return -FDT_ERR_NOSPACE; 336 288 337 memmove(buf, fdt, fdt_totalsize(fdt)); 289 memmove(buf, fdt, fdt_totalsize(fdt)); 338 return 0; 290 return 0; 339 } 291 } 340 292
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.