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
)
36 uint32_t absoffset
= stroffset
+ fdt_off_dt_strings(fdt
);
41 err
= fdt_ro_probe_(fdt
);
45 err
= -FDT_ERR_BADOFFSET
;
46 if (absoffset
>= fdt_totalsize(fdt
))
48 len
= fdt_totalsize(fdt
) - absoffset
;
50 if (fdt_magic(fdt
) == FDT_MAGIC
) {
53 if (fdt_version(fdt
) >= 17) {
54 if (stroffset
>= fdt_size_dt_strings(fdt
))
56 if ((fdt_size_dt_strings(fdt
) - stroffset
) < len
)
57 len
= fdt_size_dt_strings(fdt
) - stroffset
;
59 } else if (fdt_magic(fdt
) == FDT_SW_MAGIC
) {
61 || (stroffset
< -fdt_size_dt_strings(fdt
)))
63 if ((-stroffset
) < len
)
66 err
= -FDT_ERR_INTERNAL
;
70 s
= (const char *)fdt
+ absoffset
;
71 n
= memchr(s
, '\0', len
);
73 /* missing terminating NULL */
74 err
= -FDT_ERR_TRUNCATED
;
88 const char *fdt_string(const void *fdt
, int stroffset
)
90 return fdt_get_string(fdt
, stroffset
, NULL
);
93 static int fdt_string_eq_(const void *fdt
, int stroffset
,
94 const char *s
, int len
)
97 const char *p
= fdt_get_string(fdt
, stroffset
, &slen
);
99 return p
&& (slen
== len
) && (memcmp(p
, s
, len
) == 0);
102 int fdt_find_max_phandle(const void *fdt
, uint32_t *phandle
)
110 offset
= fdt_next_node(fdt
, offset
, NULL
);
112 if (offset
== -FDT_ERR_NOTFOUND
)
118 value
= fdt_get_phandle(fdt
, offset
);
130 int fdt_generate_phandle(const void *fdt
, uint32_t *phandle
)
135 err
= fdt_find_max_phandle(fdt
, &max
);
139 if (max
== FDT_MAX_PHANDLE
)
140 return -FDT_ERR_NOPHANDLES
;
148 static const struct fdt_reserve_entry
*fdt_mem_rsv(const void *fdt
, int n
)
150 int offset
= n
* sizeof(struct fdt_reserve_entry
);
151 int absoffset
= fdt_off_mem_rsvmap(fdt
) + offset
;
153 if (absoffset
< fdt_off_mem_rsvmap(fdt
))
155 if (absoffset
> fdt_totalsize(fdt
) - sizeof(struct fdt_reserve_entry
))
157 return fdt_mem_rsv_(fdt
, n
);
160 int fdt_get_mem_rsv(const void *fdt
, int n
, uint64_t *address
, uint64_t *size
)
162 const struct fdt_reserve_entry
*re
;
165 re
= fdt_mem_rsv(fdt
, n
);
167 return -FDT_ERR_BADOFFSET
;
169 *address
= fdt64_ld(&re
->address
);
170 *size
= fdt64_ld(&re
->size
);
174 int fdt_num_mem_rsv(const void *fdt
)
177 const struct fdt_reserve_entry
*re
;
179 for (i
= 0; (re
= fdt_mem_rsv(fdt
, i
)) != NULL
; i
++) {
180 if (fdt64_ld(&re
->size
) == 0)
183 return -FDT_ERR_TRUNCATED
;
186 static int nextprop_(const void *fdt
, int offset
)
192 tag
= fdt_next_tag(fdt
, offset
, &nextoffset
);
197 return -FDT_ERR_BADSTRUCTURE
;
205 } while (tag
== FDT_NOP
);
207 return -FDT_ERR_NOTFOUND
;
210 int fdt_subnode_offset_namelen(const void *fdt
, int offset
,
211 const char *name
, int namelen
)
218 (offset
>= 0) && (depth
>= 0);
219 offset
= fdt_next_node(fdt
, offset
, &depth
))
221 && fdt_nodename_eq_(fdt
, offset
, name
, namelen
))
225 return -FDT_ERR_NOTFOUND
;
226 return offset
; /* error */
229 int fdt_subnode_offset(const void *fdt
, int parentoffset
,
232 return fdt_subnode_offset_namelen(fdt
, parentoffset
, name
, strlen(name
));
235 int fdt_path_offset_namelen(const void *fdt
, const char *path
, int namelen
)
237 const char *end
= path
+ namelen
;
238 const char *p
= path
;
243 /* see if we have an alias */
245 const char *q
= memchr(path
, '/', end
- p
);
250 p
= fdt_get_alias_namelen(fdt
, p
, q
- p
);
252 return -FDT_ERR_BADPATH
;
253 offset
= fdt_path_offset(fdt
, p
);
266 q
= memchr(p
, '/', end
- p
);
270 offset
= fdt_subnode_offset_namelen(fdt
, offset
, p
, q
-p
);
280 int fdt_path_offset(const void *fdt
, const char *path
)
282 return fdt_path_offset_namelen(fdt
, path
, strlen(path
));
285 const char *fdt_get_name(const void *fdt
, int nodeoffset
, int *len
)
287 const struct fdt_node_header
*nh
= fdt_offset_ptr_(fdt
, nodeoffset
);
291 if (((err
= fdt_ro_probe_(fdt
)) != 0)
292 || ((err
= fdt_check_node_offset_(fdt
, nodeoffset
)) < 0))
297 if (fdt_version(fdt
) < 0x10) {
299 * For old FDT versions, match the naming conventions of V16:
300 * give only the leaf name (after all /). The actual tree
301 * contents are loosely checked.
304 leaf
= strrchr(nameptr
, '/');
306 err
= -FDT_ERR_BADSTRUCTURE
;
313 *len
= strlen(nameptr
);
323 int fdt_first_property_offset(const void *fdt
, int nodeoffset
)
327 if ((offset
= fdt_check_node_offset_(fdt
, nodeoffset
)) < 0)
330 return nextprop_(fdt
, offset
);
333 int fdt_next_property_offset(const void *fdt
, int offset
)
335 if ((offset
= fdt_check_prop_offset_(fdt
, offset
)) < 0)
338 return nextprop_(fdt
, offset
);
341 static const struct fdt_property
*fdt_get_property_by_offset_(const void *fdt
,
346 const struct fdt_property
*prop
;
348 if ((err
= fdt_check_prop_offset_(fdt
, offset
)) < 0) {
354 prop
= fdt_offset_ptr_(fdt
, offset
);
357 *lenp
= fdt32_ld(&prop
->len
);
362 const struct fdt_property
*fdt_get_property_by_offset(const void *fdt
,
366 /* Prior to version 16, properties may need realignment
367 * and this API does not work. fdt_getprop_*() will, however. */
369 if (fdt_version(fdt
) < 0x10) {
371 *lenp
= -FDT_ERR_BADVERSION
;
375 return fdt_get_property_by_offset_(fdt
, offset
, lenp
);
378 static const struct fdt_property
*fdt_get_property_namelen_(const void *fdt
,
385 for (offset
= fdt_first_property_offset(fdt
, offset
);
387 (offset
= fdt_next_property_offset(fdt
, offset
))) {
388 const struct fdt_property
*prop
;
390 if (!(prop
= fdt_get_property_by_offset_(fdt
, offset
, lenp
))) {
391 offset
= -FDT_ERR_INTERNAL
;
394 if (fdt_string_eq_(fdt
, fdt32_ld(&prop
->nameoff
),
408 const struct fdt_property
*fdt_get_property_namelen(const void *fdt
,
411 int namelen
, int *lenp
)
413 /* Prior to version 16, properties may need realignment
414 * and this API does not work. fdt_getprop_*() will, however. */
415 if (fdt_version(fdt
) < 0x10) {
417 *lenp
= -FDT_ERR_BADVERSION
;
421 return fdt_get_property_namelen_(fdt
, offset
, name
, namelen
, lenp
,
426 const struct fdt_property
*fdt_get_property(const void *fdt
,
428 const char *name
, int *lenp
)
430 return fdt_get_property_namelen(fdt
, nodeoffset
, name
,
434 const void *fdt_getprop_namelen(const void *fdt
, int nodeoffset
,
435 const char *name
, int namelen
, int *lenp
)
438 const struct fdt_property
*prop
;
440 prop
= fdt_get_property_namelen_(fdt
, nodeoffset
, name
, namelen
, lenp
,
445 /* Handle realignment */
446 if (fdt_version(fdt
) < 0x10 && (poffset
+ sizeof(*prop
)) % 8 &&
447 fdt32_ld(&prop
->len
) >= 8)
448 return prop
->data
+ 4;
452 const void *fdt_getprop_by_offset(const void *fdt
, int offset
,
453 const char **namep
, int *lenp
)
455 const struct fdt_property
*prop
;
457 prop
= fdt_get_property_by_offset_(fdt
, offset
, lenp
);
463 name
= fdt_get_string(fdt
, fdt32_ld(&prop
->nameoff
),
473 /* Handle realignment */
474 if (fdt_version(fdt
) < 0x10 && (offset
+ sizeof(*prop
)) % 8 &&
475 fdt32_ld(&prop
->len
) >= 8)
476 return prop
->data
+ 4;
480 const void *fdt_getprop(const void *fdt
, int nodeoffset
,
481 const char *name
, int *lenp
)
483 return fdt_getprop_namelen(fdt
, nodeoffset
, name
, strlen(name
), lenp
);
486 uint32_t fdt_get_phandle(const void *fdt
, int nodeoffset
)
491 /* FIXME: This is a bit sub-optimal, since we potentially scan
492 * over all the properties twice. */
493 php
= fdt_getprop(fdt
, nodeoffset
, "phandle", &len
);
494 if (!php
|| (len
!= sizeof(*php
))) {
495 php
= fdt_getprop(fdt
, nodeoffset
, "linux,phandle", &len
);
496 if (!php
|| (len
!= sizeof(*php
)))
500 return fdt32_ld(php
);
503 const char *fdt_get_alias_namelen(const void *fdt
,
504 const char *name
, int namelen
)
508 aliasoffset
= fdt_path_offset(fdt
, "/aliases");
512 return fdt_getprop_namelen(fdt
, aliasoffset
, name
, namelen
, NULL
);
515 const char *fdt_get_alias(const void *fdt
, const char *name
)
517 return fdt_get_alias_namelen(fdt
, name
, strlen(name
));
520 int fdt_get_path(const void *fdt
, int nodeoffset
, char *buf
, int buflen
)
522 int pdepth
= 0, p
= 0;
523 int offset
, depth
, namelen
;
529 return -FDT_ERR_NOSPACE
;
531 for (offset
= 0, depth
= 0;
532 (offset
>= 0) && (offset
<= nodeoffset
);
533 offset
= fdt_next_node(fdt
, offset
, &depth
)) {
534 while (pdepth
> depth
) {
537 } while (buf
[p
-1] != '/');
541 if (pdepth
>= depth
) {
542 name
= fdt_get_name(fdt
, offset
, &namelen
);
545 if ((p
+ namelen
+ 1) <= buflen
) {
546 memcpy(buf
+ p
, name
, namelen
);
553 if (offset
== nodeoffset
) {
554 if (pdepth
< (depth
+ 1))
555 return -FDT_ERR_NOSPACE
;
557 if (p
> 1) /* special case so that root path is "/", not "" */
564 if ((offset
== -FDT_ERR_NOTFOUND
) || (offset
>= 0))
565 return -FDT_ERR_BADOFFSET
;
566 else if (offset
== -FDT_ERR_BADOFFSET
)
567 return -FDT_ERR_BADSTRUCTURE
;
569 return offset
; /* error from fdt_next_node() */
572 int fdt_supernode_atdepth_offset(const void *fdt
, int nodeoffset
,
573 int supernodedepth
, int *nodedepth
)
576 int supernodeoffset
= -FDT_ERR_INTERNAL
;
580 if (supernodedepth
< 0)
581 return -FDT_ERR_NOTFOUND
;
583 for (offset
= 0, depth
= 0;
584 (offset
>= 0) && (offset
<= nodeoffset
);
585 offset
= fdt_next_node(fdt
, offset
, &depth
)) {
586 if (depth
== supernodedepth
)
587 supernodeoffset
= offset
;
589 if (offset
== nodeoffset
) {
593 if (supernodedepth
> depth
)
594 return -FDT_ERR_NOTFOUND
;
596 return supernodeoffset
;
600 if ((offset
== -FDT_ERR_NOTFOUND
) || (offset
>= 0))
601 return -FDT_ERR_BADOFFSET
;
602 else if (offset
== -FDT_ERR_BADOFFSET
)
603 return -FDT_ERR_BADSTRUCTURE
;
605 return offset
; /* error from fdt_next_node() */
608 int fdt_node_depth(const void *fdt
, int nodeoffset
)
613 err
= fdt_supernode_atdepth_offset(fdt
, nodeoffset
, 0, &nodedepth
);
615 return (err
< 0) ? err
: -FDT_ERR_INTERNAL
;
619 int fdt_parent_offset(const void *fdt
, int nodeoffset
)
621 int nodedepth
= fdt_node_depth(fdt
, nodeoffset
);
625 return fdt_supernode_atdepth_offset(fdt
, nodeoffset
,
626 nodedepth
- 1, NULL
);
629 int fdt_node_offset_by_prop_value(const void *fdt
, int startoffset
,
630 const char *propname
,
631 const void *propval
, int proplen
)
639 /* FIXME: The algorithm here is pretty horrible: we scan each
640 * property of a node in fdt_getprop(), then if that didn't
641 * find what we want, we scan over them again making our way
642 * to the next node. Still it's the easiest to implement
643 * approach; performance can come later. */
644 for (offset
= fdt_next_node(fdt
, startoffset
, NULL
);
646 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
647 val
= fdt_getprop(fdt
, offset
, propname
, &len
);
648 if (val
&& (len
== proplen
)
649 && (memcmp(val
, propval
, len
) == 0))
653 return offset
; /* error from fdt_next_node() */
656 int fdt_node_offset_by_phandle(const void *fdt
, uint32_t phandle
)
660 if ((phandle
== 0) || (phandle
== -1))
661 return -FDT_ERR_BADPHANDLE
;
665 /* FIXME: The algorithm here is pretty horrible: we
666 * potentially scan each property of a node in
667 * fdt_get_phandle(), then if that didn't find what
668 * we want, we scan over them again making our way to the next
669 * node. Still it's the easiest to implement approach;
670 * performance can come later. */
671 for (offset
= fdt_next_node(fdt
, -1, NULL
);
673 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
674 if (fdt_get_phandle(fdt
, offset
) == phandle
)
678 return offset
; /* error from fdt_next_node() */
681 int fdt_stringlist_contains(const char *strlist
, int listlen
, const char *str
)
683 int len
= strlen(str
);
686 while (listlen
>= len
) {
687 if (memcmp(str
, strlist
, len
+1) == 0)
689 p
= memchr(strlist
, '\0', listlen
);
691 return 0; /* malformed strlist.. */
692 listlen
-= (p
-strlist
) + 1;
698 int fdt_stringlist_count(const void *fdt
, int nodeoffset
, const char *property
)
700 const char *list
, *end
;
701 int length
, count
= 0;
703 list
= fdt_getprop(fdt
, nodeoffset
, property
, &length
);
710 length
= strnlen(list
, end
- list
) + 1;
712 /* Abort if the last string isn't properly NUL-terminated. */
713 if (list
+ length
> end
)
714 return -FDT_ERR_BADVALUE
;
723 int fdt_stringlist_search(const void *fdt
, int nodeoffset
, const char *property
,
726 int length
, len
, idx
= 0;
727 const char *list
, *end
;
729 list
= fdt_getprop(fdt
, nodeoffset
, property
, &length
);
733 len
= strlen(string
) + 1;
737 length
= strnlen(list
, end
- list
) + 1;
739 /* Abort if the last string isn't properly NUL-terminated. */
740 if (list
+ length
> end
)
741 return -FDT_ERR_BADVALUE
;
743 if (length
== len
&& memcmp(list
, string
, length
) == 0)
750 return -FDT_ERR_NOTFOUND
;
753 const char *fdt_stringlist_get(const void *fdt
, int nodeoffset
,
754 const char *property
, int idx
,
757 const char *list
, *end
;
760 list
= fdt_getprop(fdt
, nodeoffset
, property
, &length
);
771 length
= strnlen(list
, end
- list
) + 1;
773 /* Abort if the last string isn't properly NUL-terminated. */
774 if (list
+ length
> end
) {
776 *lenp
= -FDT_ERR_BADVALUE
;
793 *lenp
= -FDT_ERR_NOTFOUND
;
798 int fdt_node_check_compatible(const void *fdt
, int nodeoffset
,
799 const char *compatible
)
804 prop
= fdt_getprop(fdt
, nodeoffset
, "compatible", &len
);
808 return !fdt_stringlist_contains(prop
, len
, compatible
);
811 int fdt_node_offset_by_compatible(const void *fdt
, int startoffset
,
812 const char *compatible
)
818 /* FIXME: The algorithm here is pretty horrible: we scan each
819 * property of a node in fdt_node_check_compatible(), then if
820 * that didn't find what we want, we scan over them again
821 * making our way to the next node. Still it's the easiest to
822 * implement approach; performance can come later. */
823 for (offset
= fdt_next_node(fdt
, startoffset
, NULL
);
825 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
826 err
= fdt_node_check_compatible(fdt
, offset
, compatible
);
827 if ((err
< 0) && (err
!= -FDT_ERR_NOTFOUND
))
833 return offset
; /* error from fdt_next_node() */
836 int fdt_check_full(const void *fdt
, size_t bufsize
)
840 int offset
, nextoffset
= 0;
844 const char *propname
;
846 if (bufsize
< FDT_V1_SIZE
)
847 return -FDT_ERR_TRUNCATED
;
848 err
= fdt_check_header(fdt
);
851 if (bufsize
< fdt_totalsize(fdt
))
852 return -FDT_ERR_TRUNCATED
;
854 num_memrsv
= fdt_num_mem_rsv(fdt
);
860 tag
= fdt_next_tag(fdt
, offset
, &nextoffset
);
871 return -FDT_ERR_BADSTRUCTURE
;
877 return -FDT_ERR_BADSTRUCTURE
;
882 return -FDT_ERR_BADSTRUCTURE
;
887 prop
= fdt_getprop_by_offset(fdt
, offset
, &propname
,
894 return -FDT_ERR_INTERNAL
;