1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 */
6 #include "libfdt_env.h"
7
8 #include <fdt.h>
9 #include <libfdt.h>
10
11 #include "libfdt_internal.h"
12
fdt_nodename_eq_(const void * fdt,int offset,const char * s,int len)13 static int fdt_nodename_eq_(const void *fdt, int offset,
14 const char *s, int len)
15 {
16 int olen;
17 const char *p = fdt_get_name(fdt, offset, &olen);
18
19 if (!p || (fdt_chk_extra() && olen < len))
20 /* short match */
21 return 0;
22
23 if (memcmp(p, s, len) != 0)
24 return 0;
25
26 if (p[len] == '\0')
27 return 1;
28 else if (!memchr(s, '@', len) && (p[len] == '@'))
29 return 1;
30 else
31 return 0;
32 }
33
fdt_get_string(const void * fdt,int stroffset,int * lenp)34 const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
35 {
36 int32_t totalsize;
37 uint32_t absoffset;
38 size_t len;
39 int err;
40 const char *s, *n;
41
42 if (!fdt_chk_extra()) {
43 s = (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
44
45 if (lenp)
46 *lenp = strlen(s);
47 return s;
48 }
49 totalsize = fdt_ro_probe_(fdt);
50 err = totalsize;
51 if (totalsize < 0)
52 goto fail;
53
54 err = -FDT_ERR_BADOFFSET;
55 absoffset = stroffset + fdt_off_dt_strings(fdt);
56 if (absoffset >= (unsigned)totalsize)
57 goto fail;
58 len = totalsize - absoffset;
59
60 if (fdt_magic(fdt) == FDT_MAGIC) {
61 if (stroffset < 0)
62 goto fail;
63 if (!fdt_chk_version() || fdt_version(fdt) >= 17) {
64 if ((unsigned)stroffset >= fdt_size_dt_strings(fdt))
65 goto fail;
66 if ((fdt_size_dt_strings(fdt) - stroffset) < len)
67 len = fdt_size_dt_strings(fdt) - stroffset;
68 }
69 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
70 unsigned int sw_stroffset = -stroffset;
71
72 if ((stroffset >= 0) ||
73 (sw_stroffset > fdt_size_dt_strings(fdt)))
74 goto fail;
75 if ((sw_stroffset) < len)
76 len = sw_stroffset;
77 } else {
78 err = -FDT_ERR_INTERNAL;
79 goto fail;
80 }
81
82 s = (const char *)fdt + absoffset;
83 n = memchr(s, '\0', len);
84 if (!n) {
85 /* missing terminating NULL */
86 err = -FDT_ERR_TRUNCATED;
87 goto fail;
88 }
89
90 if (lenp)
91 *lenp = n - s;
92 return s;
93
94 fail:
95 if (lenp)
96 *lenp = err;
97 return NULL;
98 }
99
fdt_string(const void * fdt,int stroffset)100 const char *fdt_string(const void *fdt, int stroffset)
101 {
102 return fdt_get_string(fdt, stroffset, NULL);
103 }
104
fdt_string_eq_(const void * fdt,int stroffset,const char * s,int len)105 static int fdt_string_eq_(const void *fdt, int stroffset,
106 const char *s, int len)
107 {
108 int slen;
109 const char *p = fdt_get_string(fdt, stroffset, &slen);
110
111 return p && (slen == len) && (memcmp(p, s, len) == 0);
112 }
113
fdt_find_max_phandle(const void * fdt,uint32_t * phandle)114 int fdt_find_max_phandle(const void *fdt, uint32_t *phandle)
115 {
116 uint32_t max = 0;
117 int offset = -1;
118
119 while (true) {
120 uint32_t value;
121
122 offset = fdt_next_node(fdt, offset, NULL);
123 if (offset < 0) {
124 if (offset == -FDT_ERR_NOTFOUND)
125 break;
126
127 return offset;
128 }
129
130 value = fdt_get_phandle(fdt, offset);
131
132 if (value > max)
133 max = value;
134 }
135
136 if (phandle)
137 *phandle = max;
138
139 return 0;
140 }
141
fdt_generate_phandle(const void * fdt,uint32_t * phandle)142 int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
143 {
144 uint32_t max;
145 int err;
146
147 err = fdt_find_max_phandle(fdt, &max);
148 if (err < 0)
149 return err;
150
151 if (max == FDT_MAX_PHANDLE)
152 return -FDT_ERR_NOPHANDLES;
153
154 if (phandle)
155 *phandle = max + 1;
156
157 return 0;
158 }
159
fdt_mem_rsv(const void * fdt,int n)160 static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
161 {
162 unsigned int offset = n * sizeof(struct fdt_reserve_entry);
163 unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
164
165 if (fdt_chk_extra()) {
166 if (absoffset < fdt_off_mem_rsvmap(fdt))
167 return NULL;
168 if (absoffset > fdt_totalsize(fdt) -
169 sizeof(struct fdt_reserve_entry))
170 return NULL;
171 }
172 return fdt_mem_rsv_(fdt, n);
173 }
174
fdt_get_mem_rsv(const void * fdt,int n,uint64_t * address,uint64_t * size)175 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
176 {
177 const struct fdt_reserve_entry *re;
178
179 FDT_RO_PROBE(fdt);
180 re = fdt_mem_rsv(fdt, n);
181 if (fdt_chk_extra() && !re)
182 return -FDT_ERR_BADOFFSET;
183
184 *address = fdt64_to_cpu(re->address);
185 *size = fdt64_to_cpu(re->size);
186 return 0;
187 }
188
fdt_num_mem_rsv(const void * fdt)189 int fdt_num_mem_rsv(const void *fdt)
190 {
191 int i;
192 const struct fdt_reserve_entry *re;
193
194 for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
195 if (fdt64_to_cpu(re->size) == 0)
196 return i;
197 }
198 return -FDT_ERR_TRUNCATED;
199 }
200
nextprop_(const void * fdt,int offset)201 static int nextprop_(const void *fdt, int offset)
202 {
203 uint32_t tag;
204 int nextoffset;
205
206 do {
207 tag = fdt_next_tag(fdt, offset, &nextoffset);
208
209 switch (tag) {
210 case FDT_END:
211 if (nextoffset >= 0)
212 return -FDT_ERR_BADSTRUCTURE;
213 else
214 return nextoffset;
215
216 case FDT_PROP:
217 return offset;
218 }
219 offset = nextoffset;
220 } while (tag == FDT_NOP);
221
222 return -FDT_ERR_NOTFOUND;
223 }
224
fdt_subnode_offset_namelen(const void * fdt,int offset,const char * name,int namelen)225 int fdt_subnode_offset_namelen(const void *fdt, int offset,
226 const char *name, int namelen)
227 {
228 int depth;
229
230 FDT_RO_PROBE(fdt);
231
232 for (depth = 0;
233 (offset >= 0) && (depth >= 0);
234 offset = fdt_next_node(fdt, offset, &depth))
235 if ((depth == 1)
236 && fdt_nodename_eq_(fdt, offset, name, namelen))
237 return offset;
238
239 if (depth < 0)
240 return -FDT_ERR_NOTFOUND;
241 return offset; /* error */
242 }
243
fdt_subnode_offset(const void * fdt,int parentoffset,const char * name)244 int fdt_subnode_offset(const void *fdt, int parentoffset,
245 const char *name)
246 {
247 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
248 }
249
fdt_path_offset_namelen(const void * fdt,const char * path,int namelen)250 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
251 {
252 const char *end = path + namelen;
253 const char *p = path;
254 int offset = 0;
255
256 FDT_RO_PROBE(fdt);
257
258 /* see if we have an alias */
259 if (*path != '/') {
260 const char *q = memchr(path, '/', end - p);
261
262 if (!q)
263 q = end;
264
265 p = fdt_get_alias_namelen(fdt, p, q - p);
266 if (!p)
267 return -FDT_ERR_BADPATH;
268 offset = fdt_path_offset(fdt, p);
269
270 p = q;
271 }
272
273 while (p < end) {
274 const char *q;
275
276 while (*p == '/') {
277 p++;
278 if (p == end)
279 return offset;
280 }
281 q = memchr(p, '/', end - p);
282 if (! q)
283 q = end;
284
285 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
286 if (offset < 0)
287 return offset;
288
289 p = q;
290 }
291
292 return offset;
293 }
294
fdt_path_offset(const void * fdt,const char * path)295 int fdt_path_offset(const void *fdt, const char *path)
296 {
297 return fdt_path_offset_namelen(fdt, path, strlen(path));
298 }
299
fdt_get_name(const void * fdt,int nodeoffset,int * len)300 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
301 {
302 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
303 const char *nameptr;
304 int err;
305
306 if (fdt_chk_extra() &&
307 (((err = fdt_ro_probe_(fdt)) < 0)
308 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)))
309 goto fail;
310
311 nameptr = nh->name;
312
313 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
314 /*
315 * For old FDT versions, match the naming conventions of V16:
316 * give only the leaf name (after all /). The actual tree
317 * contents are loosely checked.
318 */
319 const char *leaf;
320 leaf = strrchr(nameptr, '/');
321 if (leaf == NULL) {
322 err = -FDT_ERR_BADSTRUCTURE;
323 goto fail;
324 }
325 nameptr = leaf+1;
326 }
327
328 if (len)
329 *len = strlen(nameptr);
330
331 return nameptr;
332
333 fail:
334 if (len)
335 *len = err;
336 return NULL;
337 }
338
fdt_first_property_offset(const void * fdt,int nodeoffset)339 int fdt_first_property_offset(const void *fdt, int nodeoffset)
340 {
341 int offset;
342
343 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
344 return offset;
345
346 return nextprop_(fdt, offset);
347 }
348
fdt_next_property_offset(const void * fdt,int offset)349 int fdt_next_property_offset(const void *fdt, int offset)
350 {
351 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
352 return offset;
353
354 return nextprop_(fdt, offset);
355 }
356
fdt_get_property_by_offset_(const void * fdt,int offset,int * lenp)357 static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
358 int offset,
359 int *lenp)
360 {
361 int err;
362 const struct fdt_property *prop;
363
364 if (fdt_chk_basic() && (err = fdt_check_prop_offset_(fdt, offset)) < 0) {
365 if (lenp)
366 *lenp = err;
367 return NULL;
368 }
369
370 prop = fdt_offset_ptr_(fdt, offset);
371
372 if (lenp)
373 *lenp = fdt32_to_cpu(prop->len);
374
375 return prop;
376 }
377
fdt_get_property_by_offset(const void * fdt,int offset,int * lenp)378 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
379 int offset,
380 int *lenp)
381 {
382 /* Prior to version 16, properties may need realignment
383 * and this API does not work. fdt_getprop_*() will, however. */
384
385 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
386 if (lenp)
387 *lenp = -FDT_ERR_BADVERSION;
388 return NULL;
389 }
390
391 return fdt_get_property_by_offset_(fdt, offset, lenp);
392 }
393
fdt_get_property_namelen_(const void * fdt,int offset,const char * name,int namelen,int * lenp,int * poffset)394 static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
395 int offset,
396 const char *name,
397 int namelen,
398 int *lenp,
399 int *poffset)
400 {
401 for (offset = fdt_first_property_offset(fdt, offset);
402 (offset >= 0);
403 (offset = fdt_next_property_offset(fdt, offset))) {
404 const struct fdt_property *prop;
405
406 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
407 if (fdt_chk_extra() && !prop) {
408 offset = -FDT_ERR_INTERNAL;
409 break;
410 }
411 if (fdt_string_eq_(fdt, fdt32_to_cpu(prop->nameoff),
412 name, namelen)) {
413 if (poffset)
414 *poffset = offset;
415 return prop;
416 }
417 }
418
419 if (lenp)
420 *lenp = offset;
421 return NULL;
422 }
423
fdt_get_property_namelen(const void * fdt,int offset,const char * name,int namelen,int * lenp)424 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
425 int offset,
426 const char *name,
427 int namelen, int *lenp)
428 {
429 /* Prior to version 16, properties may need realignment
430 * and this API does not work. fdt_getprop_*() will, however. */
431 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
432 if (lenp)
433 *lenp = -FDT_ERR_BADVERSION;
434 return NULL;
435 }
436
437 return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
438 NULL);
439 }
440
fdt_get_property(const void * fdt,int nodeoffset,const char * name,int * lenp)441 const struct fdt_property *fdt_get_property(const void *fdt,
442 int nodeoffset,
443 const char *name, int *lenp)
444 {
445 return fdt_get_property_namelen(fdt, nodeoffset, name,
446 strlen(name), lenp);
447 }
448
fdt_getprop_namelen(const void * fdt,int nodeoffset,const char * name,int namelen,int * lenp)449 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
450 const char *name, int namelen, int *lenp)
451 {
452 int poffset;
453 const struct fdt_property *prop;
454
455 prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
456 &poffset);
457 if (!prop)
458 return NULL;
459
460 /* Handle realignment */
461 if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
462 (poffset + sizeof(*prop)) % 8 && fdt32_to_cpu(prop->len) >= 8)
463 return prop->data + 4;
464 return prop->data;
465 }
466
fdt_getprop_by_offset(const void * fdt,int offset,const char ** namep,int * lenp)467 const void *fdt_getprop_by_offset(const void *fdt, int offset,
468 const char **namep, int *lenp)
469 {
470 const struct fdt_property *prop;
471
472 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
473 if (!prop)
474 return NULL;
475 if (namep) {
476 const char *name;
477 int namelen;
478
479 if (fdt_chk_extra()) {
480 name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff),
481 &namelen);
482 if (!name) {
483 if (lenp)
484 *lenp = namelen;
485 return NULL;
486 }
487 *namep = name;
488 } else {
489 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
490 }
491 }
492
493 /* Handle realignment */
494 if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
495 (offset + sizeof(*prop)) % 8 && fdt32_to_cpu(prop->len) >= 8)
496 return prop->data + 4;
497 return prop->data;
498 }
499
fdt_getprop(const void * fdt,int nodeoffset,const char * name,int * lenp)500 const void *fdt_getprop(const void *fdt, int nodeoffset,
501 const char *name, int *lenp)
502 {
503 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
504 }
505
fdt_get_phandle(const void * fdt,int nodeoffset)506 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
507 {
508 const fdt32_t *php;
509 int len;
510
511 /* FIXME: This is a bit sub-optimal, since we potentially scan
512 * over all the properties twice. */
513 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
514 if (!php || (len != sizeof(*php))) {
515 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
516 if (!php || (len != sizeof(*php)))
517 return 0;
518 }
519
520 return fdt32_to_cpu(*php);
521 }
522
fdt_get_alias_namelen(const void * fdt,const char * name,int namelen)523 const char *fdt_get_alias_namelen(const void *fdt,
524 const char *name, int namelen)
525 {
526 int aliasoffset;
527
528 aliasoffset = fdt_path_offset(fdt, "/aliases");
529 if (aliasoffset < 0)
530 return NULL;
531
532 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
533 }
534
fdt_get_alias(const void * fdt,const char * name)535 const char *fdt_get_alias(const void *fdt, const char *name)
536 {
537 return fdt_get_alias_namelen(fdt, name, strlen(name));
538 }
539
fdt_get_path(const void * fdt,int nodeoffset,char * buf,int buflen)540 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
541 {
542 int pdepth = 0, p = 0;
543 int offset, depth, namelen;
544 const char *name;
545
546 FDT_RO_PROBE(fdt);
547
548 if (buflen < 2)
549 return -FDT_ERR_NOSPACE;
550
551 for (offset = 0, depth = 0;
552 (offset >= 0) && (offset <= nodeoffset);
553 offset = fdt_next_node(fdt, offset, &depth)) {
554 while (pdepth > depth) {
555 do {
556 p--;
557 } while (buf[p-1] != '/');
558 pdepth--;
559 }
560
561 if (pdepth >= depth) {
562 name = fdt_get_name(fdt, offset, &namelen);
563 if (!name)
564 return namelen;
565 if ((p + namelen + 1) <= buflen) {
566 memcpy(buf + p, name, namelen);
567 p += namelen;
568 buf[p++] = '/';
569 pdepth++;
570 }
571 }
572
573 if (offset == nodeoffset) {
574 if (pdepth < (depth + 1))
575 return -FDT_ERR_NOSPACE;
576
577 if (p > 1) /* special case so that root path is "/", not "" */
578 p--;
579 buf[p] = '\0';
580 return 0;
581 }
582 }
583
584 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
585 return -FDT_ERR_BADOFFSET;
586 else if (offset == -FDT_ERR_BADOFFSET)
587 return -FDT_ERR_BADSTRUCTURE;
588
589 return offset; /* error from fdt_next_node() */
590 }
591
fdt_supernode_atdepth_offset(const void * fdt,int nodeoffset,int supernodedepth,int * nodedepth)592 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
593 int supernodedepth, int *nodedepth)
594 {
595 int offset, depth;
596 int supernodeoffset = -FDT_ERR_INTERNAL;
597
598 FDT_RO_PROBE(fdt);
599
600 if (supernodedepth < 0)
601 return -FDT_ERR_NOTFOUND;
602
603 for (offset = 0, depth = 0;
604 (offset >= 0) && (offset <= nodeoffset);
605 offset = fdt_next_node(fdt, offset, &depth)) {
606 if (depth == supernodedepth)
607 supernodeoffset = offset;
608
609 if (offset == nodeoffset) {
610 if (nodedepth)
611 *nodedepth = depth;
612
613 if (supernodedepth > depth)
614 return -FDT_ERR_NOTFOUND;
615 else
616 return supernodeoffset;
617 }
618 }
619
620 if (fdt_chk_extra()) {
621 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
622 return -FDT_ERR_BADOFFSET;
623 else if (offset == -FDT_ERR_BADOFFSET)
624 return -FDT_ERR_BADSTRUCTURE;
625 }
626
627 return offset; /* error from fdt_next_node() */
628 }
629
fdt_node_depth(const void * fdt,int nodeoffset)630 int fdt_node_depth(const void *fdt, int nodeoffset)
631 {
632 int nodedepth;
633 int err;
634
635 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
636 if (err)
637 return (!fdt_chk_extra() || err < 0) ? err : -FDT_ERR_INTERNAL;
638 return nodedepth;
639 }
640
fdt_parent_offset(const void * fdt,int nodeoffset)641 int fdt_parent_offset(const void *fdt, int nodeoffset)
642 {
643 int nodedepth = fdt_node_depth(fdt, nodeoffset);
644
645 if (nodedepth < 0)
646 return nodedepth;
647 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
648 nodedepth - 1, NULL);
649 }
650
fdt_node_offset_by_prop_value(const void * fdt,int startoffset,const char * propname,const void * propval,int proplen)651 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
652 const char *propname,
653 const void *propval, int proplen)
654 {
655 int offset;
656 const void *val;
657 int len;
658
659 FDT_RO_PROBE(fdt);
660
661 /* FIXME: The algorithm here is pretty horrible: we scan each
662 * property of a node in fdt_getprop(), then if that didn't
663 * find what we want, we scan over them again making our way
664 * to the next node. Still it's the easiest to implement
665 * approach; performance can come later. */
666 for (offset = fdt_next_node(fdt, startoffset, NULL);
667 offset >= 0;
668 offset = fdt_next_node(fdt, offset, NULL)) {
669 val = fdt_getprop(fdt, offset, propname, &len);
670 if (val && (len == proplen)
671 && (memcmp(val, propval, len) == 0))
672 return offset;
673 }
674
675 return offset; /* error from fdt_next_node() */
676 }
677
fdt_node_offset_by_phandle(const void * fdt,uint32_t phandle)678 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
679 {
680 int offset;
681
682 if ((phandle == 0) || (phandle == ~0U))
683 return -FDT_ERR_BADPHANDLE;
684
685 FDT_RO_PROBE(fdt);
686
687 /* FIXME: The algorithm here is pretty horrible: we
688 * potentially scan each property of a node in
689 * fdt_get_phandle(), then if that didn't find what
690 * we want, we scan over them again making our way to the next
691 * node. Still it's the easiest to implement approach;
692 * performance can come later. */
693 for (offset = fdt_next_node(fdt, -1, NULL);
694 offset >= 0;
695 offset = fdt_next_node(fdt, offset, NULL)) {
696 if (fdt_get_phandle(fdt, offset) == phandle)
697 return offset;
698 }
699
700 return offset; /* error from fdt_next_node() */
701 }
702
fdt_stringlist_contains(const char * strlist,int listlen,const char * str)703 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
704 {
705 int len = strlen(str);
706 const char *p;
707
708 while (listlen >= len) {
709 if (memcmp(str, strlist, len+1) == 0)
710 return 1;
711 p = memchr(strlist, '\0', listlen);
712 if (!p)
713 return 0; /* malformed strlist.. */
714 listlen -= (p-strlist) + 1;
715 strlist = p + 1;
716 }
717 return 0;
718 }
719
fdt_stringlist_count(const void * fdt,int nodeoffset,const char * property)720 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
721 {
722 const char *list, *end;
723 int length, count = 0;
724
725 list = fdt_getprop(fdt, nodeoffset, property, &length);
726 if (!list)
727 return length;
728
729 end = list + length;
730
731 while (list < end) {
732 length = strnlen(list, end - list) + 1;
733
734 /* Abort if the last string isn't properly NUL-terminated. */
735 if (list + length > end)
736 return -FDT_ERR_BADVALUE;
737
738 list += length;
739 count++;
740 }
741
742 return count;
743 }
744
fdt_stringlist_search(const void * fdt,int nodeoffset,const char * property,const char * string)745 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
746 const char *string)
747 {
748 int length, len, idx = 0;
749 const char *list, *end;
750
751 list = fdt_getprop(fdt, nodeoffset, property, &length);
752 if (!list)
753 return length;
754
755 len = strlen(string) + 1;
756 end = list + length;
757
758 while (list < end) {
759 length = strnlen(list, end - list) + 1;
760
761 /* Abort if the last string isn't properly NUL-terminated. */
762 if (list + length > end)
763 return -FDT_ERR_BADVALUE;
764
765 if (length == len && memcmp(list, string, length) == 0)
766 return idx;
767
768 list += length;
769 idx++;
770 }
771
772 return -FDT_ERR_NOTFOUND;
773 }
774
fdt_stringlist_get(const void * fdt,int nodeoffset,const char * property,int idx,int * lenp)775 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
776 const char *property, int idx,
777 int *lenp)
778 {
779 const char *list, *end;
780 int length;
781
782 list = fdt_getprop(fdt, nodeoffset, property, &length);
783 if (!list) {
784 if (lenp)
785 *lenp = length;
786
787 return NULL;
788 }
789
790 end = list + length;
791
792 while (list < end) {
793 length = strnlen(list, end - list) + 1;
794
795 /* Abort if the last string isn't properly NUL-terminated. */
796 if (list + length > end) {
797 if (lenp)
798 *lenp = -FDT_ERR_BADVALUE;
799
800 return NULL;
801 }
802
803 if (idx == 0) {
804 if (lenp)
805 *lenp = length - 1;
806
807 return list;
808 }
809
810 list += length;
811 idx--;
812 }
813
814 if (lenp)
815 *lenp = -FDT_ERR_NOTFOUND;
816
817 return NULL;
818 }
819
fdt_node_check_compatible(const void * fdt,int nodeoffset,const char * compatible)820 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
821 const char *compatible)
822 {
823 const void *prop;
824 int len;
825
826 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
827 if (!prop)
828 return len;
829
830 return !fdt_stringlist_contains(prop, len, compatible);
831 }
832
fdt_node_offset_by_compatible(const void * fdt,int startoffset,const char * compatible)833 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
834 const char *compatible)
835 {
836 int offset, err;
837
838 FDT_RO_PROBE(fdt);
839
840 /* FIXME: The algorithm here is pretty horrible: we scan each
841 * property of a node in fdt_node_check_compatible(), then if
842 * that didn't find what we want, we scan over them again
843 * making our way to the next node. Still it's the easiest to
844 * implement approach; performance can come later. */
845 for (offset = fdt_next_node(fdt, startoffset, NULL);
846 offset >= 0;
847 offset = fdt_next_node(fdt, offset, NULL)) {
848 err = fdt_node_check_compatible(fdt, offset, compatible);
849 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
850 return err;
851 else if (err == 0)
852 return offset;
853 }
854
855 return offset; /* error from fdt_next_node() */
856 }
857
858 #if !defined(FDT_ASSUME_MASK) || FDT_ASSUME_MASK != 0xff
fdt_check_full(const void * fdt,size_t bufsize)859 int fdt_check_full(const void *fdt, size_t bufsize)
860 {
861 int err;
862 int num_memrsv;
863 int offset, nextoffset = 0;
864 uint32_t tag;
865 unsigned depth = 0;
866 const void *prop;
867 const char *propname;
868 bool expect_end = false;
869
870 if (bufsize < FDT_V1_SIZE)
871 return -FDT_ERR_TRUNCATED;
872 err = fdt_check_header(fdt);
873 if (err != 0)
874 return err;
875 if (bufsize < fdt_totalsize(fdt))
876 return -FDT_ERR_TRUNCATED;
877
878 num_memrsv = fdt_num_mem_rsv(fdt);
879 if (num_memrsv < 0)
880 return num_memrsv;
881
882 while (1) {
883 offset = nextoffset;
884 tag = fdt_next_tag(fdt, offset, &nextoffset);
885
886 if (nextoffset < 0)
887 return nextoffset;
888
889 /* If we see two root nodes, something is wrong */
890 if (expect_end && tag != FDT_END)
891 return -FDT_ERR_BADLAYOUT;
892
893 switch (tag) {
894 case FDT_NOP:
895 break;
896
897 case FDT_END:
898 if (depth != 0)
899 return -FDT_ERR_BADSTRUCTURE;
900 return 0;
901
902 case FDT_BEGIN_NODE:
903 depth++;
904 if (depth > INT_MAX)
905 return -FDT_ERR_BADSTRUCTURE;
906
907 /* The root node must have an empty name */
908 if (depth == 1) {
909 const char *name;
910 int len;
911
912 name = fdt_get_name(fdt, offset, &len);
913 if (*name || len)
914 return -FDT_ERR_BADLAYOUT;
915 }
916 break;
917
918 case FDT_END_NODE:
919 if (depth == 0)
920 return -FDT_ERR_BADSTRUCTURE;
921 depth--;
922 if (depth == 0)
923 expect_end = true;
924 break;
925
926 case FDT_PROP:
927 prop = fdt_getprop_by_offset(fdt, offset, &propname,
928 &err);
929 if (!prop)
930 return err;
931 break;
932
933 default:
934 return -FDT_ERR_INTERNAL;
935 }
936 }
937 }
938 #else
fdt_check_full(const void __always_unused * fdt,size_t __always_unused bufsize)939 int fdt_check_full(const void __always_unused *fdt,
940 size_t __always_unused bufsize)
941 {
942 return 0;
943 }
944 #endif /* #if !defined(FDT_ASSUME_MASK) || FDT_ASSUME_MASK != 0xff */
945