]>
git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/FdtLib/fdt_ro.c
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"
66 const char *p
= fdt_offset_ptr (fdt
, offset
+ FDT_TAGSIZE
, len
+1);
73 if (memcmp (p
, s
, len
) != 0) {
79 } else if (!memchr (s
, '@', len
) && (p
[len
] == '@')) {
92 return (const char *)fdt
+ fdt_off_dt_strings (fdt
) + stroffset
;
103 const char *p
= fdt_string (fdt
, stroffset
);
105 return (strlen (p
) == len
) && (memcmp (p
, s
, len
) == 0);
109 fdt_get_max_phandle (
113 uint32_t max_phandle
= 0;
116 for (offset
= fdt_next_node (fdt
, -1, NULL
); ;
117 offset
= fdt_next_node (fdt
, offset
, NULL
))
121 if (offset
== -FDT_ERR_NOTFOUND
) {
129 phandle
= fdt_get_phandle (fdt
, offset
);
130 if (phandle
== (uint32_t)-1) {
134 if (phandle
> max_phandle
) {
135 max_phandle
= phandle
;
150 FDT_CHECK_HEADER (fdt
);
151 *address
= fdt64_to_cpu (_fdt_mem_rsv (fdt
, n
)->address
);
152 *size
= fdt64_to_cpu (_fdt_mem_rsv (fdt
, n
)->size
);
163 while (fdt64_to_cpu (_fdt_mem_rsv (fdt
, i
)->size
) != 0) {
180 tag
= fdt_next_tag (fdt
, offset
, &nextoffset
);
184 if (nextoffset
>= 0) {
185 return -FDT_ERR_BADSTRUCTURE
;
195 } while (tag
== FDT_NOP
);
197 return -FDT_ERR_NOTFOUND
;
201 fdt_subnode_offset_namelen (
210 FDT_CHECK_HEADER (fdt
);
213 (offset
>= 0) && (depth
>= 0);
214 offset
= fdt_next_node (fdt
, offset
, &depth
))
217 && _fdt_nodename_eq (fdt
, offset
, name
, namelen
))
224 return -FDT_ERR_NOTFOUND
;
227 return offset
; /* error */
237 return fdt_subnode_offset_namelen (fdt
, parentoffset
, name
, strlen (name
));
241 fdt_path_offset_namelen (
247 const char *end
= path
+ namelen
;
248 const char *p
= path
;
251 FDT_CHECK_HEADER (fdt
);
253 /* see if we have an alias */
255 const char *q
= memchr (path
, '/', end
- p
);
261 p
= fdt_get_alias_namelen (fdt
, p
, q
- p
);
263 return -FDT_ERR_BADPATH
;
266 offset
= fdt_path_offset (fdt
, p
);
281 q
= memchr (p
, '/', end
- p
);
286 offset
= fdt_subnode_offset_namelen (fdt
, offset
, p
, q
-p
);
303 return fdt_path_offset_namelen (fdt
, path
, strlen (path
));
313 const struct fdt_node_header
*nh
= _fdt_offset_ptr (fdt
, nodeoffset
);
316 if ( ((err
= fdt_check_header (fdt
)) != 0)
317 || ((err
= _fdt_check_node_offset (fdt
, nodeoffset
)) < 0))
323 *len
= strlen (nh
->name
);
337 fdt_first_property_offset (
344 if ((offset
= _fdt_check_node_offset (fdt
, nodeoffset
)) < 0) {
348 return _nextprop (fdt
, offset
);
352 fdt_next_property_offset (
357 if ((offset
= _fdt_check_prop_offset (fdt
, offset
)) < 0) {
361 return _nextprop (fdt
, offset
);
364 const struct fdt_property
*
365 fdt_get_property_by_offset (
372 const struct fdt_property
*prop
;
374 if ((err
= _fdt_check_prop_offset (fdt
, offset
)) < 0) {
382 prop
= _fdt_offset_ptr (fdt
, offset
);
385 *lenp
= fdt32_to_cpu (prop
->len
);
391 const struct fdt_property
*
392 fdt_get_property_namelen (
400 for (offset
= fdt_first_property_offset (fdt
, offset
);
402 (offset
= fdt_next_property_offset (fdt
, offset
)))
404 const struct fdt_property
*prop
;
406 if (!(prop
= fdt_get_property_by_offset (fdt
, offset
, lenp
))) {
407 offset
= -FDT_ERR_INTERNAL
;
413 fdt32_to_cpu (prop
->nameoff
),
429 const struct fdt_property
*
437 return fdt_get_property_namelen (
447 fdt_getprop_namelen (
455 const struct fdt_property
*prop
;
457 prop
= fdt_get_property_namelen (fdt
, nodeoffset
, name
, namelen
, lenp
);
466 fdt_getprop_by_offset (
473 const struct fdt_property
*prop
;
475 prop
= fdt_get_property_by_offset (fdt
, offset
, lenp
);
481 *namep
= fdt_string (fdt
, fdt32_to_cpu (prop
->nameoff
));
495 return fdt_getprop_namelen (fdt
, nodeoffset
, name
, strlen (name
), lenp
);
507 /* FIXME: This is a bit sub-optimal, since we potentially scan
508 * over all the properties twice. */
509 php
= fdt_getprop (fdt
, nodeoffset
, "phandle", &len
);
510 if (!php
|| (len
!= sizeof (*php
))) {
511 php
= fdt_getprop (fdt
, nodeoffset
, "linux,phandle", &len
);
512 if (!php
|| (len
!= sizeof (*php
))) {
517 return fdt32_to_cpu (*php
);
521 fdt_get_alias_namelen (
529 aliasoffset
= fdt_path_offset (fdt
, "/aliases");
530 if (aliasoffset
< 0) {
534 return fdt_getprop_namelen (fdt
, aliasoffset
, name
, namelen
, NULL
);
543 return fdt_get_alias_namelen (fdt
, name
, strlen (name
));
554 int pdepth
= 0, p
= 0;
555 int offset
, depth
, namelen
;
558 FDT_CHECK_HEADER (fdt
);
561 return -FDT_ERR_NOSPACE
;
564 for (offset
= 0, depth
= 0;
565 (offset
>= 0) && (offset
<= nodeoffset
);
566 offset
= fdt_next_node (fdt
, offset
, &depth
))
568 while (pdepth
> depth
) {
571 } while (buf
[p
-1] != '/');
576 if (pdepth
>= depth
) {
577 name
= fdt_get_name (fdt
, offset
, &namelen
);
582 if ((p
+ namelen
+ 1) <= buflen
) {
583 memcpy (buf
+ p
, name
, namelen
);
590 if (offset
== nodeoffset
) {
591 if (pdepth
< (depth
+ 1)) {
592 return -FDT_ERR_NOSPACE
;
596 /* special case so that root path is "/", not "" */
605 if ((offset
== -FDT_ERR_NOTFOUND
) || (offset
>= 0)) {
606 return -FDT_ERR_BADOFFSET
;
607 } else if (offset
== -FDT_ERR_BADOFFSET
) {
608 return -FDT_ERR_BADSTRUCTURE
;
611 return offset
; /* error from fdt_next_node() */
615 fdt_supernode_atdepth_offset (
623 int supernodeoffset
= -FDT_ERR_INTERNAL
;
625 FDT_CHECK_HEADER (fdt
);
627 if (supernodedepth
< 0) {
628 return -FDT_ERR_NOTFOUND
;
631 for (offset
= 0, depth
= 0;
632 (offset
>= 0) && (offset
<= nodeoffset
);
633 offset
= fdt_next_node (fdt
, offset
, &depth
))
635 if (depth
== supernodedepth
) {
636 supernodeoffset
= offset
;
639 if (offset
== nodeoffset
) {
644 if (supernodedepth
> depth
) {
645 return -FDT_ERR_NOTFOUND
;
647 return supernodeoffset
;
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() */
670 err
= fdt_supernode_atdepth_offset (fdt
, nodeoffset
, 0, &nodedepth
);
672 return (err
< 0) ? err
: -FDT_ERR_INTERNAL
;
684 int nodedepth
= fdt_node_depth (fdt
, nodeoffset
);
690 return fdt_supernode_atdepth_offset (
699 fdt_node_offset_by_prop_value (
702 const char *propname
,
711 FDT_CHECK_HEADER (fdt
);
713 /* FIXME: The algorithm here is pretty horrible: we scan each
714 * property of a node in fdt_getprop(), then if that didn't
715 * find what we want, we scan over them again making our way
716 * to the next node. Still it's the easiest to implement
717 * approach; performance can come later. */
718 for (offset
= fdt_next_node (fdt
, startoffset
, NULL
);
720 offset
= fdt_next_node (fdt
, offset
, NULL
))
722 val
= fdt_getprop (fdt
, offset
, propname
, &len
);
723 if ( val
&& (len
== proplen
)
724 && (memcmp (val
, propval
, len
) == 0))
730 return offset
; /* error from fdt_next_node() */
734 fdt_node_offset_by_phandle (
741 if ((phandle
== 0) || (phandle
== -1)) {
742 return -FDT_ERR_BADPHANDLE
;
745 FDT_CHECK_HEADER (fdt
);
747 /* FIXME: The algorithm here is pretty horrible: we
748 * potentially scan each property of a node in
749 * fdt_get_phandle(), then if that didn't find what
750 * we want, we scan over them again making our way to the next
751 * node. Still it's the easiest to implement approach;
752 * performance can come later. */
753 for (offset
= fdt_next_node (fdt
, -1, NULL
);
755 offset
= fdt_next_node (fdt
, offset
, NULL
))
757 if (fdt_get_phandle (fdt
, offset
) == phandle
) {
762 return offset
; /* error from fdt_next_node() */
766 fdt_stringlist_contains (
772 int len
= strlen (str
);
775 while (listlen
>= len
) {
776 if (memcmp (str
, strlist
, len
+1) == 0) {
780 p
= memchr (strlist
, '\0', listlen
);
782 return 0; /* malformed strlist.. */
785 listlen
-= (p
-strlist
) + 1;
793 fdt_stringlist_count (
799 const char *list
, *end
;
800 int length
, count
= 0;
802 list
= fdt_getprop (fdt
, nodeoffset
, property
, &length
);
810 length
= strnlen (list
, end
- list
) + 1;
812 /* Abort if the last string isn't properly NUL-terminated. */
813 if (list
+ length
> end
) {
814 return -FDT_ERR_BADVALUE
;
825 fdt_stringlist_search (
828 const char *property
,
832 int length
, len
, idx
= 0;
833 const char *list
, *end
;
835 list
= fdt_getprop (fdt
, nodeoffset
, property
, &length
);
840 len
= strlen (string
) + 1;
844 length
= strnlen (list
, end
- list
) + 1;
846 /* Abort if the last string isn't properly NUL-terminated. */
847 if (list
+ length
> end
) {
848 return -FDT_ERR_BADVALUE
;
851 if ((length
== len
) && (memcmp (list
, string
, length
) == 0)) {
859 return -FDT_ERR_NOTFOUND
;
866 const char *property
,
871 const char *list
, *end
;
874 list
= fdt_getprop (fdt
, nodeoffset
, property
, &length
);
886 length
= strnlen (list
, end
- list
) + 1;
888 /* Abort if the last string isn't properly NUL-terminated. */
889 if (list
+ length
> end
) {
891 *lenp
= -FDT_ERR_BADVALUE
;
910 *lenp
= -FDT_ERR_NOTFOUND
;
917 fdt_node_check_compatible (
920 const char *compatible
926 prop
= fdt_getprop (fdt
, nodeoffset
, "compatible", &len
);
931 return !fdt_stringlist_contains (prop
, len
, compatible
);
935 fdt_node_offset_by_compatible (
938 const char *compatible
943 FDT_CHECK_HEADER (fdt
);
945 /* FIXME: The algorithm here is pretty horrible: we scan each
946 * property of a node in fdt_node_check_compatible(), then if
947 * that didn't find what we want, we scan over them again
948 * making our way to the next node. Still it's the easiest to
949 * implement approach; performance can come later. */
950 for (offset
= fdt_next_node (fdt
, startoffset
, NULL
);
952 offset
= fdt_next_node (fdt
, offset
, NULL
))
954 err
= fdt_node_check_compatible (fdt
, offset
, compatible
);
955 if ((err
< 0) && (err
!= -FDT_ERR_NOTFOUND
)) {
957 } else if (err
== 0) {
962 return offset
; /* error from fdt_next_node() */