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);
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 (can_assume(VALID_INPUT)) {
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 (can_assume(LATEST) || 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 (!can_assume(VALID_INPUT)) {
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 (!can_assume(VALID_INPUT) && !re)
182 return -FDT_ERR_BADOFFSET;
184 *address = fdt64_ld_(&re->address);
185 *size = fdt64_ld_(&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_ld_(&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 if (!can_assume(VALID_INPUT) && namelen <= 0)
259 return -FDT_ERR_BADPATH;
261 /* see if we have an alias */
263 const char *q = memchr(path, '/', end - p);
268 p = fdt_get_alias_namelen(fdt, p, q - p);
270 return -FDT_ERR_BADPATH;
271 offset = fdt_path_offset(fdt, p);
284 q = memchr(p, '/', end - p);
288 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
298 int fdt_path_offset(const void *fdt, const char *path)
300 return fdt_path_offset_namelen(fdt, path, strlen(path));
303 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
305 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
309 if (((err = fdt_ro_probe_(fdt)) < 0)
310 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
315 if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
317 * For old FDT versions, match the naming conventions of V16:
318 * give only the leaf name (after all /). The actual tree
319 * contents are loosely checked.
322 leaf = strrchr(nameptr, '/');
324 err = -FDT_ERR_BADSTRUCTURE;
331 *len = strlen(nameptr);
341 int fdt_first_property_offset(const void *fdt, int nodeoffset)
345 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
348 return nextprop_(fdt, offset);
351 int fdt_next_property_offset(const void *fdt, int offset)
353 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
356 return nextprop_(fdt, offset);
359 static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
364 const struct fdt_property *prop;
366 if (!can_assume(VALID_INPUT) &&
367 (err = fdt_check_prop_offset_(fdt, offset)) < 0) {
373 prop = fdt_offset_ptr_(fdt, offset);
376 *lenp = fdt32_ld_(&prop->len);
381 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
385 /* Prior to version 16, properties may need realignment
386 * and this API does not work. fdt_getprop_*() will, however. */
388 if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
390 *lenp = -FDT_ERR_BADVERSION;
394 return fdt_get_property_by_offset_(fdt, offset, lenp);
397 static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
404 for (offset = fdt_first_property_offset(fdt, offset);
406 (offset = fdt_next_property_offset(fdt, offset))) {
407 const struct fdt_property *prop;
409 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
410 if (!can_assume(LIBFDT_FLAWLESS) && !prop) {
411 offset = -FDT_ERR_INTERNAL;
414 if (fdt_string_eq_(fdt, fdt32_ld_(&prop->nameoff),
428 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
431 int namelen, int *lenp)
433 /* Prior to version 16, properties may need realignment
434 * and this API does not work. fdt_getprop_*() will, however. */
435 if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
437 *lenp = -FDT_ERR_BADVERSION;
441 return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
446 const struct fdt_property *fdt_get_property(const void *fdt,
448 const char *name, int *lenp)
450 return fdt_get_property_namelen(fdt, nodeoffset, name,
454 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
455 const char *name, int namelen, int *lenp)
458 const struct fdt_property *prop;
460 prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
465 /* Handle realignment */
466 if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
467 (poffset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
468 return prop->data + 4;
472 const void *fdt_getprop_by_offset(const void *fdt, int offset,
473 const char **namep, int *lenp)
475 const struct fdt_property *prop;
477 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
484 if (!can_assume(VALID_INPUT)) {
485 name = fdt_get_string(fdt, fdt32_ld_(&prop->nameoff),
494 *namep = fdt_string(fdt, fdt32_ld_(&prop->nameoff));
498 /* Handle realignment */
499 if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
500 (offset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
501 return prop->data + 4;
505 const void *fdt_getprop(const void *fdt, int nodeoffset,
506 const char *name, int *lenp)
508 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
511 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
516 /* FIXME: This is a bit sub-optimal, since we potentially scan
517 * over all the properties twice. */
518 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
519 if (!php || (len != sizeof(*php))) {
520 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
521 if (!php || (len != sizeof(*php)))
525 return fdt32_ld_(php);
528 static const void *fdt_path_getprop_namelen(const void *fdt, const char *path,
529 const char *propname, int propnamelen,
532 int offset = fdt_path_offset(fdt, path);
537 return fdt_getprop_namelen(fdt, offset, propname, propnamelen, lenp);
540 const char *fdt_get_alias_namelen(const void *fdt,
541 const char *name, int namelen)
546 alias = fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, &len);
548 if (!can_assume(VALID_DTB) &&
549 !(alias && len > 0 && alias[len - 1] == '\0' && *alias == '/'))
555 const char *fdt_get_alias(const void *fdt, const char *name)
557 return fdt_get_alias_namelen(fdt, name, strlen(name));
560 const char *fdt_get_symbol_namelen(const void *fdt,
561 const char *name, int namelen)
563 return fdt_path_getprop_namelen(fdt, "/__symbols__", name, namelen, NULL);
566 const char *fdt_get_symbol(const void *fdt, const char *name)
568 return fdt_get_symbol_namelen(fdt, name, strlen(name));
571 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
573 int pdepth = 0, p = 0;
574 int offset, depth, namelen;
580 return -FDT_ERR_NOSPACE;
582 for (offset = 0, depth = 0;
583 (offset >= 0) && (offset <= nodeoffset);
584 offset = fdt_next_node(fdt, offset, &depth)) {
585 while (pdepth > depth) {
588 } while (buf[p-1] != '/');
592 if (pdepth >= depth) {
593 name = fdt_get_name(fdt, offset, &namelen);
596 if ((p + namelen + 1) <= buflen) {
597 memcpy(buf + p, name, namelen);
604 if (offset == nodeoffset) {
605 if (pdepth < (depth + 1))
606 return -FDT_ERR_NOSPACE;
608 if (p > 1) /* special case so that root path is "/", not "" */
615 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
616 return -FDT_ERR_BADOFFSET;
617 else if (offset == -FDT_ERR_BADOFFSET)
618 return -FDT_ERR_BADSTRUCTURE;
620 return offset; /* error from fdt_next_node() */
623 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
624 int supernodedepth, int *nodedepth)
627 int supernodeoffset = -FDT_ERR_INTERNAL;
631 if (supernodedepth < 0)
632 return -FDT_ERR_NOTFOUND;
634 for (offset = 0, depth = 0;
635 (offset >= 0) && (offset <= nodeoffset);
636 offset = fdt_next_node(fdt, offset, &depth)) {
637 if (depth == supernodedepth)
638 supernodeoffset = offset;
640 if (offset == nodeoffset) {
644 if (supernodedepth > depth)
645 return -FDT_ERR_NOTFOUND;
647 return supernodeoffset;
651 if (!can_assume(VALID_INPUT)) {
652 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
653 return -FDT_ERR_BADOFFSET;
654 else if (offset == -FDT_ERR_BADOFFSET)
655 return -FDT_ERR_BADSTRUCTURE;
658 return offset; /* error from fdt_next_node() */
661 int fdt_node_depth(const void *fdt, int nodeoffset)
666 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
668 return (can_assume(LIBFDT_FLAWLESS) || err < 0) ? err :
673 int fdt_parent_offset(const void *fdt, int nodeoffset)
675 int nodedepth = fdt_node_depth(fdt, nodeoffset);
679 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
680 nodedepth - 1, NULL);
683 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
684 const char *propname,
685 const void *propval, int proplen)
693 /* FIXME: The algorithm here is pretty horrible: we scan each
694 * property of a node in fdt_getprop(), then if that didn't
695 * find what we want, we scan over them again making our way
696 * to the next node. Still it's the easiest to implement
697 * approach; performance can come later. */
698 for (offset = fdt_next_node(fdt, startoffset, NULL);
700 offset = fdt_next_node(fdt, offset, NULL)) {
701 val = fdt_getprop(fdt, offset, propname, &len);
702 if (val && (len == proplen)
703 && (memcmp(val, propval, len) == 0))
707 return offset; /* error from fdt_next_node() */
710 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
714 if ((phandle == 0) || (phandle == ~0U))
715 return -FDT_ERR_BADPHANDLE;
719 /* FIXME: The algorithm here is pretty horrible: we
720 * potentially scan each property of a node in
721 * fdt_get_phandle(), then if that didn't find what
722 * we want, we scan over them again making our way to the next
723 * node. Still it's the easiest to implement approach;
724 * performance can come later. */
725 for (offset = fdt_next_node(fdt, -1, NULL);
727 offset = fdt_next_node(fdt, offset, NULL)) {
728 if (fdt_get_phandle(fdt, offset) == phandle)
732 return offset; /* error from fdt_next_node() */
735 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
737 int len = strlen(str);
740 while (listlen >= len) {
741 if (memcmp(str, strlist, len+1) == 0)
743 p = memchr(strlist, '\0', listlen);
745 return 0; /* malformed strlist.. */
746 listlen -= (p-strlist) + 1;
752 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
754 const char *list, *end;
755 int length, count = 0;
757 list = fdt_getprop(fdt, nodeoffset, property, &length);
764 length = strnlen(list, end - list) + 1;
766 /* Abort if the last string isn't properly NUL-terminated. */
767 if (list + length > end)
768 return -FDT_ERR_BADVALUE;
777 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
780 int length, len, idx = 0;
781 const char *list, *end;
783 list = fdt_getprop(fdt, nodeoffset, property, &length);
787 len = strlen(string) + 1;
791 length = strnlen(list, end - list) + 1;
793 /* Abort if the last string isn't properly NUL-terminated. */
794 if (list + length > end)
795 return -FDT_ERR_BADVALUE;
797 if (length == len && memcmp(list, string, length) == 0)
804 return -FDT_ERR_NOTFOUND;
807 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
808 const char *property, int idx,
811 const char *list, *end;
814 list = fdt_getprop(fdt, nodeoffset, property, &length);
825 length = strnlen(list, end - list) + 1;
827 /* Abort if the last string isn't properly NUL-terminated. */
828 if (list + length > end) {
830 *lenp = -FDT_ERR_BADVALUE;
847 *lenp = -FDT_ERR_NOTFOUND;
852 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
853 const char *compatible)
858 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
862 return !fdt_stringlist_contains(prop, len, compatible);
865 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
866 const char *compatible)
872 /* FIXME: The algorithm here is pretty horrible: we scan each
873 * property of a node in fdt_node_check_compatible(), then if
874 * that didn't find what we want, we scan over them again
875 * making our way to the next node. Still it's the easiest to
876 * implement approach; performance can come later. */
877 for (offset = fdt_next_node(fdt, startoffset, NULL);
879 offset = fdt_next_node(fdt, offset, NULL)) {
880 err = fdt_node_check_compatible(fdt, offset, compatible);
881 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
887 return offset; /* error from fdt_next_node() */