1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 #include "libfdt_env.h"
11 #include "libfdt_internal.h"
13 static int fdt_nodename_eq_(const void *fdt, int offset,
14 const char *s, int len)
17 const char *p = fdt_get_name(fdt, offset, &olen);
19 if (!p || (fdt_chk_extra() && olen < len))
23 if (memcmp(p, s, len) != 0)
28 else if (!memchr(s, '@', len) && (p[len] == '@'))
34 const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
42 if (!fdt_chk_extra()) {
43 s = (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
49 totalsize = fdt_ro_probe_(fdt);
54 err = -FDT_ERR_BADOFFSET;
55 absoffset = stroffset + fdt_off_dt_strings(fdt);
56 if (absoffset >= (unsigned)totalsize)
58 len = totalsize - absoffset;
60 if (fdt_magic(fdt) == FDT_MAGIC) {
63 if (!fdt_chk_version() || fdt_version(fdt) >= 17) {
64 if ((unsigned)stroffset >= fdt_size_dt_strings(fdt))
66 if ((fdt_size_dt_strings(fdt) - stroffset) < len)
67 len = fdt_size_dt_strings(fdt) - stroffset;
69 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
70 unsigned int sw_stroffset = -stroffset;
72 if ((stroffset >= 0) ||
73 (sw_stroffset > fdt_size_dt_strings(fdt)))
75 if ((sw_stroffset) < len)
78 err = -FDT_ERR_INTERNAL;
82 s = (const char *)fdt + absoffset;
83 n = memchr(s, '\0', len);
85 /* missing terminating NULL */
86 err = -FDT_ERR_TRUNCATED;
100 const char *fdt_string(const void *fdt, int stroffset)
102 return fdt_get_string(fdt, stroffset, NULL);
105 static int fdt_string_eq_(const void *fdt, int stroffset,
106 const char *s, int len)
109 const char *p = fdt_get_string(fdt, stroffset, &slen);
111 return p && (slen == len) && (memcmp(p, s, len) == 0);
114 int fdt_find_max_phandle(const void *fdt, uint32_t *phandle)
122 offset = fdt_next_node(fdt, offset, NULL);
124 if (offset == -FDT_ERR_NOTFOUND)
130 value = fdt_get_phandle(fdt, offset);
142 int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
147 err = fdt_find_max_phandle(fdt, &max);
151 if (max == FDT_MAX_PHANDLE)
152 return -FDT_ERR_NOPHANDLES;
160 static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
162 unsigned int offset = n * sizeof(struct fdt_reserve_entry);
163 unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
165 if (fdt_chk_extra()) {
166 if (absoffset < fdt_off_mem_rsvmap(fdt))
168 if (absoffset > fdt_totalsize(fdt) -
169 sizeof(struct fdt_reserve_entry))
172 return fdt_mem_rsv_(fdt, n);
175 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
177 const struct fdt_reserve_entry *re;
180 re = fdt_mem_rsv(fdt, n);
181 if (fdt_chk_extra() && !re)
182 return -FDT_ERR_BADOFFSET;
184 *address = fdt64_to_cpu(re->address);
185 *size = fdt64_to_cpu(re->size);
189 int fdt_num_mem_rsv(const void *fdt)
192 const struct fdt_reserve_entry *re;
194 for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
195 if (fdt64_to_cpu(re->size) == 0)
198 return -FDT_ERR_TRUNCATED;
201 static int nextprop_(const void *fdt, int offset)
207 tag = fdt_next_tag(fdt, offset, &nextoffset);
212 return -FDT_ERR_BADSTRUCTURE;
220 } while (tag == FDT_NOP);
222 return -FDT_ERR_NOTFOUND;
225 int fdt_subnode_offset_namelen(const void *fdt, int offset,
226 const char *name, int namelen)
233 (offset >= 0) && (depth >= 0);
234 offset = fdt_next_node(fdt, offset, &depth))
236 && fdt_nodename_eq_(fdt, offset, name, namelen))
240 return -FDT_ERR_NOTFOUND;
241 return offset; /* error */
244 int fdt_subnode_offset(const void *fdt, int parentoffset,
247 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
250 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
252 const char *end = path + namelen;
253 const char *p = path;
258 /* see if we have an alias */
260 const char *q = memchr(path, '/', end - p);
265 p = fdt_get_alias_namelen(fdt, p, q - p);
267 return -FDT_ERR_BADPATH;
268 offset = fdt_path_offset(fdt, p);
281 q = memchr(p, '/', end - p);
285 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
295 int fdt_path_offset(const void *fdt, const char *path)
297 return fdt_path_offset_namelen(fdt, path, strlen(path));
300 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
302 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
306 if (fdt_chk_extra() &&
307 (((err = fdt_ro_probe_(fdt)) < 0)
308 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)))
313 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
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.
320 leaf = strrchr(nameptr, '/');
322 err = -FDT_ERR_BADSTRUCTURE;
329 *len = strlen(nameptr);
339 int fdt_first_property_offset(const void *fdt, int nodeoffset)
343 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
346 return nextprop_(fdt, offset);
349 int fdt_next_property_offset(const void *fdt, int offset)
351 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
354 return nextprop_(fdt, offset);
357 static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
362 const struct fdt_property *prop;
364 if (fdt_chk_basic() && (err = fdt_check_prop_offset_(fdt, offset)) < 0) {
370 prop = fdt_offset_ptr_(fdt, offset);
373 *lenp = fdt32_to_cpu(prop->len);
378 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
382 /* Prior to version 16, properties may need realignment
383 * and this API does not work. fdt_getprop_*() will, however. */
385 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
387 *lenp = -FDT_ERR_BADVERSION;
391 return fdt_get_property_by_offset_(fdt, offset, lenp);
394 static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
401 for (offset = fdt_first_property_offset(fdt, offset);
403 (offset = fdt_next_property_offset(fdt, offset))) {
404 const struct fdt_property *prop;
406 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
407 if (fdt_chk_extra() && !prop) {
408 offset = -FDT_ERR_INTERNAL;
411 if (fdt_string_eq_(fdt, fdt32_to_cpu(prop->nameoff),
425 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
428 int namelen, int *lenp)
430 /* Prior to version 16, properties may need realignment
431 * and this API does not work. fdt_getprop_*() will, however. */
432 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
434 *lenp = -FDT_ERR_BADVERSION;
438 return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
443 const struct fdt_property *fdt_get_property(const void *fdt,
445 const char *name, int *lenp)
447 return fdt_get_property_namelen(fdt, nodeoffset, name,
451 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
452 const char *name, int namelen, int *lenp)
455 const struct fdt_property *prop;
457 prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
462 /* Handle realignment */
463 if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
464 (poffset + sizeof(*prop)) % 8 && fdt32_to_cpu(prop->len) >= 8)
465 return prop->data + 4;
469 const void *fdt_getprop_by_offset(const void *fdt, int offset,
470 const char **namep, int *lenp)
472 const struct fdt_property *prop;
474 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
481 if (fdt_chk_extra()) {
482 name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff),
491 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
495 /* Handle realignment */
496 if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
497 (offset + sizeof(*prop)) % 8 && fdt32_to_cpu(prop->len) >= 8)
498 return prop->data + 4;
502 const void *fdt_getprop(const void *fdt, int nodeoffset,
503 const char *name, int *lenp)
505 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
508 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
513 /* FIXME: This is a bit sub-optimal, since we potentially scan
514 * over all the properties twice. */
515 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
516 if (!php || (len != sizeof(*php))) {
517 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
518 if (!php || (len != sizeof(*php)))
522 return fdt32_to_cpu(*php);
525 const char *fdt_get_alias_namelen(const void *fdt,
526 const char *name, int namelen)
530 aliasoffset = fdt_path_offset(fdt, "/aliases");
534 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
537 const char *fdt_get_alias(const void *fdt, const char *name)
539 return fdt_get_alias_namelen(fdt, name, strlen(name));
542 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
544 int pdepth = 0, p = 0;
545 int offset, depth, namelen;
551 return -FDT_ERR_NOSPACE;
553 for (offset = 0, depth = 0;
554 (offset >= 0) && (offset <= nodeoffset);
555 offset = fdt_next_node(fdt, offset, &depth)) {
556 while (pdepth > depth) {
559 } while (buf[p-1] != '/');
563 if (pdepth >= depth) {
564 name = fdt_get_name(fdt, offset, &namelen);
567 if ((p + namelen + 1) <= buflen) {
568 memcpy(buf + p, name, namelen);
575 if (offset == nodeoffset) {
576 if (pdepth < (depth + 1))
577 return -FDT_ERR_NOSPACE;
579 if (p > 1) /* special case so that root path is "/", not "" */
586 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
587 return -FDT_ERR_BADOFFSET;
588 else if (offset == -FDT_ERR_BADOFFSET)
589 return -FDT_ERR_BADSTRUCTURE;
591 return offset; /* error from fdt_next_node() */
594 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
595 int supernodedepth, int *nodedepth)
598 int supernodeoffset = -FDT_ERR_INTERNAL;
602 if (supernodedepth < 0)
603 return -FDT_ERR_NOTFOUND;
605 for (offset = 0, depth = 0;
606 (offset >= 0) && (offset <= nodeoffset);
607 offset = fdt_next_node(fdt, offset, &depth)) {
608 if (depth == supernodedepth)
609 supernodeoffset = offset;
611 if (offset == nodeoffset) {
615 if (supernodedepth > depth)
616 return -FDT_ERR_NOTFOUND;
618 return supernodeoffset;
622 if (fdt_chk_extra()) {
623 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
624 return -FDT_ERR_BADOFFSET;
625 else if (offset == -FDT_ERR_BADOFFSET)
626 return -FDT_ERR_BADSTRUCTURE;
629 return offset; /* error from fdt_next_node() */
632 int fdt_node_depth(const void *fdt, int nodeoffset)
637 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
639 return (!fdt_chk_extra() || err < 0) ? err : -FDT_ERR_INTERNAL;
643 int fdt_parent_offset(const void *fdt, int nodeoffset)
645 int nodedepth = fdt_node_depth(fdt, nodeoffset);
649 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
650 nodedepth - 1, NULL);
653 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
654 const char *propname,
655 const void *propval, int proplen)
663 /* FIXME: The algorithm here is pretty horrible: we scan each
664 * property of a node in fdt_getprop(), then if that didn't
665 * find what we want, we scan over them again making our way
666 * to the next node. Still it's the easiest to implement
667 * approach; performance can come later. */
668 for (offset = fdt_next_node(fdt, startoffset, NULL);
670 offset = fdt_next_node(fdt, offset, NULL)) {
671 val = fdt_getprop(fdt, offset, propname, &len);
672 if (val && (len == proplen)
673 && (memcmp(val, propval, len) == 0))
677 return offset; /* error from fdt_next_node() */
680 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
684 if ((phandle == 0) || (phandle == ~0U))
685 return -FDT_ERR_BADPHANDLE;
689 /* FIXME: The algorithm here is pretty horrible: we
690 * potentially scan each property of a node in
691 * fdt_get_phandle(), then if that didn't find what
692 * we want, we scan over them again making our way to the next
693 * node. Still it's the easiest to implement approach;
694 * performance can come later. */
695 for (offset = fdt_next_node(fdt, -1, NULL);
697 offset = fdt_next_node(fdt, offset, NULL)) {
698 if (fdt_get_phandle(fdt, offset) == phandle)
702 return offset; /* error from fdt_next_node() */
705 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
707 int len = strlen(str);
710 while (listlen >= len) {
711 if (memcmp(str, strlist, len+1) == 0)
713 p = memchr(strlist, '\0', listlen);
715 return 0; /* malformed strlist.. */
716 listlen -= (p-strlist) + 1;
722 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
724 const char *list, *end;
725 int length, count = 0;
727 list = fdt_getprop(fdt, nodeoffset, property, &length);
734 length = strnlen(list, end - list) + 1;
736 /* Abort if the last string isn't properly NUL-terminated. */
737 if (list + length > end)
738 return -FDT_ERR_BADVALUE;
747 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
750 int length, len, idx = 0;
751 const char *list, *end;
753 list = fdt_getprop(fdt, nodeoffset, property, &length);
757 len = strlen(string) + 1;
761 length = strnlen(list, end - list) + 1;
763 /* Abort if the last string isn't properly NUL-terminated. */
764 if (list + length > end)
765 return -FDT_ERR_BADVALUE;
767 if (length == len && memcmp(list, string, length) == 0)
774 return -FDT_ERR_NOTFOUND;
777 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
778 const char *property, int idx,
781 const char *list, *end;
784 list = fdt_getprop(fdt, nodeoffset, property, &length);
795 length = strnlen(list, end - list) + 1;
797 /* Abort if the last string isn't properly NUL-terminated. */
798 if (list + length > end) {
800 *lenp = -FDT_ERR_BADVALUE;
817 *lenp = -FDT_ERR_NOTFOUND;
822 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
823 const char *compatible)
828 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
832 return !fdt_stringlist_contains(prop, len, compatible);
835 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
836 const char *compatible)
842 /* FIXME: The algorithm here is pretty horrible: we scan each
843 * property of a node in fdt_node_check_compatible(), then if
844 * that didn't find what we want, we scan over them again
845 * making our way to the next node. Still it's the easiest to
846 * implement approach; performance can come later. */
847 for (offset = fdt_next_node(fdt, startoffset, NULL);
849 offset = fdt_next_node(fdt, offset, NULL)) {
850 err = fdt_node_check_compatible(fdt, offset, compatible);
851 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
857 return offset; /* error from fdt_next_node() */
860 #if !defined(FDT_ASSUME_MASK) || FDT_ASSUME_MASK != 0xff
861 int fdt_check_full(const void *fdt, size_t bufsize)
865 int offset, nextoffset = 0;
869 const char *propname;
871 if (bufsize < FDT_V1_SIZE)
872 return -FDT_ERR_TRUNCATED;
873 err = fdt_check_header(fdt);
876 if (bufsize < fdt_totalsize(fdt))
877 return -FDT_ERR_TRUNCATED;
879 num_memrsv = fdt_num_mem_rsv(fdt);
885 tag = fdt_next_tag(fdt, offset, &nextoffset);
896 return -FDT_ERR_BADSTRUCTURE;
902 return -FDT_ERR_BADSTRUCTURE;
907 return -FDT_ERR_BADSTRUCTURE;
912 prop = fdt_getprop_by_offset(fdt, offset, &propname,
919 return -FDT_ERR_INTERNAL;