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