2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
8 * a) This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
25 * b) Redistribution and use in source and binary forms, with or
26 * without modification, are permitted provided that the following
29 * 1. Redistributions of source code must retain the above
30 * copyright notice, this list of conditions and the following
32 * 2. Redistributions in binary form must reproduce the above
33 * copyright notice, this list of conditions and the following
34 * disclaimer in the documentation and/or other materials
35 * provided with the distribution.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include "libfdt_env.h"
56 #include "libfdt_internal.h"
58 static int fdt_nodename_eq_(const void *fdt
, int offset
,
59 const char *s
, int len
)
62 const char *p
= fdt_get_name(fdt
, offset
, &olen
);
68 if (memcmp(p
, s
, len
) != 0)
73 else if (!memchr(s
, '@', len
) && (p
[len
] == '@'))
79 const char *fdt_get_string(const void *fdt
, int stroffset
, int *lenp
)
81 uint32_t absoffset
= stroffset
+ fdt_off_dt_strings(fdt
);
86 err
= fdt_ro_probe_(fdt
);
90 err
= -FDT_ERR_BADOFFSET
;
91 if (absoffset
>= fdt_totalsize(fdt
))
93 len
= fdt_totalsize(fdt
) - absoffset
;
95 if (fdt_magic(fdt
) == FDT_MAGIC
) {
98 if (fdt_version(fdt
) >= 17) {
99 if (stroffset
>= fdt_size_dt_strings(fdt
))
101 if ((fdt_size_dt_strings(fdt
) - stroffset
) < len
)
102 len
= fdt_size_dt_strings(fdt
) - stroffset
;
104 } else if (fdt_magic(fdt
) == FDT_SW_MAGIC
) {
106 || (stroffset
< -fdt_size_dt_strings(fdt
)))
108 if ((-stroffset
) < len
)
111 err
= -FDT_ERR_INTERNAL
;
115 s
= (const char *)fdt
+ absoffset
;
116 n
= memchr(s
, '\0', len
);
118 /* missing terminating NULL */
119 err
= -FDT_ERR_TRUNCATED
;
133 const char *fdt_string(const void *fdt
, int stroffset
)
135 return fdt_get_string(fdt
, stroffset
, NULL
);
138 static int fdt_string_eq_(const void *fdt
, int stroffset
,
139 const char *s
, int len
)
142 const char *p
= fdt_get_string(fdt
, stroffset
, &slen
);
144 return p
&& (slen
== len
) && (memcmp(p
, s
, len
) == 0);
147 uint32_t fdt_get_max_phandle(const void *fdt
)
149 uint32_t max_phandle
= 0;
152 for (offset
= fdt_next_node(fdt
, -1, NULL
);;
153 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
156 if (offset
== -FDT_ERR_NOTFOUND
)
162 phandle
= fdt_get_phandle(fdt
, offset
);
163 if (phandle
== (uint32_t)-1)
166 if (phandle
> max_phandle
)
167 max_phandle
= phandle
;
173 static const struct fdt_reserve_entry
*fdt_mem_rsv(const void *fdt
, int n
)
175 int offset
= n
* sizeof(struct fdt_reserve_entry
);
176 int absoffset
= fdt_off_mem_rsvmap(fdt
) + offset
;
178 if (absoffset
< fdt_off_mem_rsvmap(fdt
))
180 if (absoffset
> fdt_totalsize(fdt
) - sizeof(struct fdt_reserve_entry
))
182 return fdt_mem_rsv_(fdt
, n
);
185 int fdt_get_mem_rsv(const void *fdt
, int n
, uint64_t *address
, uint64_t *size
)
187 const struct fdt_reserve_entry
*re
;
190 re
= fdt_mem_rsv(fdt
, n
);
192 return -FDT_ERR_BADOFFSET
;
194 *address
= fdt64_ld(&re
->address
);
195 *size
= fdt64_ld(&re
->size
);
199 int fdt_num_mem_rsv(const void *fdt
)
202 const struct fdt_reserve_entry
*re
;
204 for (i
= 0; (re
= fdt_mem_rsv(fdt
, i
)) != NULL
; i
++) {
205 if (fdt64_ld(&re
->size
) == 0)
208 return -FDT_ERR_TRUNCATED
;
211 static int nextprop_(const void *fdt
, int offset
)
217 tag
= fdt_next_tag(fdt
, offset
, &nextoffset
);
222 return -FDT_ERR_BADSTRUCTURE
;
230 } while (tag
== FDT_NOP
);
232 return -FDT_ERR_NOTFOUND
;
235 int fdt_subnode_offset_namelen(const void *fdt
, int offset
,
236 const char *name
, int namelen
)
243 (offset
>= 0) && (depth
>= 0);
244 offset
= fdt_next_node(fdt
, offset
, &depth
))
246 && fdt_nodename_eq_(fdt
, offset
, name
, namelen
))
250 return -FDT_ERR_NOTFOUND
;
251 return offset
; /* error */
254 int fdt_subnode_offset(const void *fdt
, int parentoffset
,
257 return fdt_subnode_offset_namelen(fdt
, parentoffset
, name
, strlen(name
));
260 int fdt_path_offset_namelen(const void *fdt
, const char *path
, int namelen
)
262 const char *end
= path
+ namelen
;
263 const char *p
= path
;
268 /* see if we have an alias */
270 const char *q
= memchr(path
, '/', end
- p
);
275 p
= fdt_get_alias_namelen(fdt
, p
, q
- p
);
277 return -FDT_ERR_BADPATH
;
278 offset
= fdt_path_offset(fdt
, p
);
291 q
= memchr(p
, '/', end
- p
);
295 offset
= fdt_subnode_offset_namelen(fdt
, offset
, p
, q
-p
);
305 int fdt_path_offset(const void *fdt
, const char *path
)
307 return fdt_path_offset_namelen(fdt
, path
, strlen(path
));
310 const char *fdt_get_name(const void *fdt
, int nodeoffset
, int *len
)
312 const struct fdt_node_header
*nh
= fdt_offset_ptr_(fdt
, nodeoffset
);
316 if (((err
= fdt_ro_probe_(fdt
)) != 0)
317 || ((err
= fdt_check_node_offset_(fdt
, nodeoffset
)) < 0))
322 if (fdt_version(fdt
) < 0x10) {
324 * For old FDT versions, match the naming conventions of V16:
325 * give only the leaf name (after all /). The actual tree
326 * contents are loosely checked.
329 leaf
= strrchr(nameptr
, '/');
331 err
= -FDT_ERR_BADSTRUCTURE
;
338 *len
= strlen(nameptr
);
348 int fdt_first_property_offset(const void *fdt
, int nodeoffset
)
352 if ((offset
= fdt_check_node_offset_(fdt
, nodeoffset
)) < 0)
355 return nextprop_(fdt
, offset
);
358 int fdt_next_property_offset(const void *fdt
, int offset
)
360 if ((offset
= fdt_check_prop_offset_(fdt
, offset
)) < 0)
363 return nextprop_(fdt
, offset
);
366 static const struct fdt_property
*fdt_get_property_by_offset_(const void *fdt
,
371 const struct fdt_property
*prop
;
373 if ((err
= fdt_check_prop_offset_(fdt
, offset
)) < 0) {
379 prop
= fdt_offset_ptr_(fdt
, offset
);
382 *lenp
= fdt32_ld(&prop
->len
);
387 const struct fdt_property
*fdt_get_property_by_offset(const void *fdt
,
391 /* Prior to version 16, properties may need realignment
392 * and this API does not work. fdt_getprop_*() will, however. */
394 if (fdt_version(fdt
) < 0x10) {
396 *lenp
= -FDT_ERR_BADVERSION
;
400 return fdt_get_property_by_offset_(fdt
, offset
, lenp
);
403 static const struct fdt_property
*fdt_get_property_namelen_(const void *fdt
,
410 for (offset
= fdt_first_property_offset(fdt
, offset
);
412 (offset
= fdt_next_property_offset(fdt
, offset
))) {
413 const struct fdt_property
*prop
;
415 if (!(prop
= fdt_get_property_by_offset_(fdt
, offset
, lenp
))) {
416 offset
= -FDT_ERR_INTERNAL
;
419 if (fdt_string_eq_(fdt
, fdt32_ld(&prop
->nameoff
),
433 const struct fdt_property
*fdt_get_property_namelen(const void *fdt
,
436 int namelen
, int *lenp
)
438 /* Prior to version 16, properties may need realignment
439 * and this API does not work. fdt_getprop_*() will, however. */
440 if (fdt_version(fdt
) < 0x10) {
442 *lenp
= -FDT_ERR_BADVERSION
;
446 return fdt_get_property_namelen_(fdt
, offset
, name
, namelen
, lenp
,
451 const struct fdt_property
*fdt_get_property(const void *fdt
,
453 const char *name
, int *lenp
)
455 return fdt_get_property_namelen(fdt
, nodeoffset
, name
,
459 const void *fdt_getprop_namelen(const void *fdt
, int nodeoffset
,
460 const char *name
, int namelen
, int *lenp
)
463 const struct fdt_property
*prop
;
465 prop
= fdt_get_property_namelen_(fdt
, nodeoffset
, name
, namelen
, lenp
,
470 /* Handle realignment */
471 if (fdt_version(fdt
) < 0x10 && (poffset
+ sizeof(*prop
)) % 8 &&
472 fdt32_ld(&prop
->len
) >= 8)
473 return prop
->data
+ 4;
477 const void *fdt_getprop_by_offset(const void *fdt
, int offset
,
478 const char **namep
, int *lenp
)
480 const struct fdt_property
*prop
;
482 prop
= fdt_get_property_by_offset_(fdt
, offset
, lenp
);
488 name
= fdt_get_string(fdt
, fdt32_ld(&prop
->nameoff
),
498 /* Handle realignment */
499 if (fdt_version(fdt
) < 0x10 && (offset
+ sizeof(*prop
)) % 8 &&
500 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 const char *fdt_get_alias_namelen(const void *fdt
,
529 const char *name
, int namelen
)
533 aliasoffset
= fdt_path_offset(fdt
, "/aliases");
537 return fdt_getprop_namelen(fdt
, aliasoffset
, name
, namelen
, NULL
);
540 const char *fdt_get_alias(const void *fdt
, const char *name
)
542 return fdt_get_alias_namelen(fdt
, name
, strlen(name
));
545 int fdt_get_path(const void *fdt
, int nodeoffset
, char *buf
, int buflen
)
547 int pdepth
= 0, p
= 0;
548 int offset
, depth
, namelen
;
554 return -FDT_ERR_NOSPACE
;
556 for (offset
= 0, depth
= 0;
557 (offset
>= 0) && (offset
<= nodeoffset
);
558 offset
= fdt_next_node(fdt
, offset
, &depth
)) {
559 while (pdepth
> depth
) {
562 } while (buf
[p
-1] != '/');
566 if (pdepth
>= depth
) {
567 name
= fdt_get_name(fdt
, offset
, &namelen
);
570 if ((p
+ namelen
+ 1) <= buflen
) {
571 memcpy(buf
+ p
, name
, namelen
);
578 if (offset
== nodeoffset
) {
579 if (pdepth
< (depth
+ 1))
580 return -FDT_ERR_NOSPACE
;
582 if (p
> 1) /* special case so that root path is "/", not "" */
589 if ((offset
== -FDT_ERR_NOTFOUND
) || (offset
>= 0))
590 return -FDT_ERR_BADOFFSET
;
591 else if (offset
== -FDT_ERR_BADOFFSET
)
592 return -FDT_ERR_BADSTRUCTURE
;
594 return offset
; /* error from fdt_next_node() */
597 int fdt_supernode_atdepth_offset(const void *fdt
, int nodeoffset
,
598 int supernodedepth
, int *nodedepth
)
601 int supernodeoffset
= -FDT_ERR_INTERNAL
;
605 if (supernodedepth
< 0)
606 return -FDT_ERR_NOTFOUND
;
608 for (offset
= 0, depth
= 0;
609 (offset
>= 0) && (offset
<= nodeoffset
);
610 offset
= fdt_next_node(fdt
, offset
, &depth
)) {
611 if (depth
== supernodedepth
)
612 supernodeoffset
= offset
;
614 if (offset
== nodeoffset
) {
618 if (supernodedepth
> depth
)
619 return -FDT_ERR_NOTFOUND
;
621 return supernodeoffset
;
625 if ((offset
== -FDT_ERR_NOTFOUND
) || (offset
>= 0))
626 return -FDT_ERR_BADOFFSET
;
627 else if (offset
== -FDT_ERR_BADOFFSET
)
628 return -FDT_ERR_BADSTRUCTURE
;
630 return offset
; /* error from fdt_next_node() */
633 int fdt_node_depth(const void *fdt
, int nodeoffset
)
638 err
= fdt_supernode_atdepth_offset(fdt
, nodeoffset
, 0, &nodedepth
);
640 return (err
< 0) ? err
: -FDT_ERR_INTERNAL
;
644 int fdt_parent_offset(const void *fdt
, int nodeoffset
)
646 int nodedepth
= fdt_node_depth(fdt
, nodeoffset
);
650 return fdt_supernode_atdepth_offset(fdt
, nodeoffset
,
651 nodedepth
- 1, NULL
);
654 int fdt_node_offset_by_prop_value(const void *fdt
, int startoffset
,
655 const char *propname
,
656 const void *propval
, int proplen
)
664 /* FIXME: The algorithm here is pretty horrible: we scan each
665 * property of a node in fdt_getprop(), then if that didn't
666 * find what we want, we scan over them again making our way
667 * to the next node. Still it's the easiest to implement
668 * approach; performance can come later. */
669 for (offset
= fdt_next_node(fdt
, startoffset
, NULL
);
671 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
672 val
= fdt_getprop(fdt
, offset
, propname
, &len
);
673 if (val
&& (len
== proplen
)
674 && (memcmp(val
, propval
, len
) == 0))
678 return offset
; /* error from fdt_next_node() */
681 int fdt_node_offset_by_phandle(const void *fdt
, uint32_t phandle
)
685 if ((phandle
== 0) || (phandle
== -1))
686 return -FDT_ERR_BADPHANDLE
;
690 /* FIXME: The algorithm here is pretty horrible: we
691 * potentially scan each property of a node in
692 * fdt_get_phandle(), then if that didn't find what
693 * we want, we scan over them again making our way to the next
694 * node. Still it's the easiest to implement approach;
695 * performance can come later. */
696 for (offset
= fdt_next_node(fdt
, -1, NULL
);
698 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
699 if (fdt_get_phandle(fdt
, offset
) == phandle
)
703 return offset
; /* error from fdt_next_node() */
706 int fdt_stringlist_contains(const char *strlist
, int listlen
, const char *str
)
708 int len
= strlen(str
);
711 while (listlen
>= len
) {
712 if (memcmp(str
, strlist
, len
+1) == 0)
714 p
= memchr(strlist
, '\0', listlen
);
716 return 0; /* malformed strlist.. */
717 listlen
-= (p
-strlist
) + 1;
723 int fdt_stringlist_count(const void *fdt
, int nodeoffset
, const char *property
)
725 const char *list
, *end
;
726 int length
, count
= 0;
728 list
= fdt_getprop(fdt
, nodeoffset
, property
, &length
);
735 length
= strnlen(list
, end
- list
) + 1;
737 /* Abort if the last string isn't properly NUL-terminated. */
738 if (list
+ length
> end
)
739 return -FDT_ERR_BADVALUE
;
748 int fdt_stringlist_search(const void *fdt
, int nodeoffset
, const char *property
,
751 int length
, len
, idx
= 0;
752 const char *list
, *end
;
754 list
= fdt_getprop(fdt
, nodeoffset
, property
, &length
);
758 len
= strlen(string
) + 1;
762 length
= strnlen(list
, end
- list
) + 1;
764 /* Abort if the last string isn't properly NUL-terminated. */
765 if (list
+ length
> end
)
766 return -FDT_ERR_BADVALUE
;
768 if (length
== len
&& memcmp(list
, string
, length
) == 0)
775 return -FDT_ERR_NOTFOUND
;
778 const char *fdt_stringlist_get(const void *fdt
, int nodeoffset
,
779 const char *property
, int idx
,
782 const char *list
, *end
;
785 list
= fdt_getprop(fdt
, nodeoffset
, property
, &length
);
796 length
= strnlen(list
, end
- list
) + 1;
798 /* Abort if the last string isn't properly NUL-terminated. */
799 if (list
+ length
> end
) {
801 *lenp
= -FDT_ERR_BADVALUE
;
818 *lenp
= -FDT_ERR_NOTFOUND
;
823 int fdt_node_check_compatible(const void *fdt
, int nodeoffset
,
824 const char *compatible
)
829 prop
= fdt_getprop(fdt
, nodeoffset
, "compatible", &len
);
833 return !fdt_stringlist_contains(prop
, len
, compatible
);
836 int fdt_node_offset_by_compatible(const void *fdt
, int startoffset
,
837 const char *compatible
)
843 /* FIXME: The algorithm here is pretty horrible: we scan each
844 * property of a node in fdt_node_check_compatible(), then if
845 * that didn't find what we want, we scan over them again
846 * making our way to the next node. Still it's the easiest to
847 * implement approach; performance can come later. */
848 for (offset
= fdt_next_node(fdt
, startoffset
, NULL
);
850 offset
= fdt_next_node(fdt
, offset
, NULL
)) {
851 err
= fdt_node_check_compatible(fdt
, offset
, compatible
);
852 if ((err
< 0) && (err
!= -FDT_ERR_NOTFOUND
))
858 return offset
; /* error from fdt_next_node() */
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
;