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_string(const void *fdt, int stroffset)
81 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
84 static int fdt_string_eq_(const void *fdt, int stroffset,
85 const char *s, int len)
87 const char *p = fdt_string(fdt, stroffset);
89 return (strlen(p) == len) && (memcmp(p, s, len) == 0);
92 uint32_t fdt_get_max_phandle(const void *fdt)
94 uint32_t max_phandle = 0;
97 for (offset = fdt_next_node(fdt, -1, NULL);;
98 offset = fdt_next_node(fdt, offset, NULL)) {
101 if (offset == -FDT_ERR_NOTFOUND)
107 phandle = fdt_get_phandle(fdt, offset);
108 if (phandle == (uint32_t)-1)
111 if (phandle > max_phandle)
112 max_phandle = phandle;
118 int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
126 offset = fdt_next_node(fdt, offset, NULL);
128 if (offset == -FDT_ERR_NOTFOUND)
134 value = fdt_get_phandle(fdt, offset);
140 if (max == FDT_MAX_PHANDLE)
141 return -FDT_ERR_NOPHANDLES;
149 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
151 FDT_CHECK_HEADER(fdt);
152 *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address);
153 *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size);
157 int fdt_num_mem_rsv(const void *fdt)
161 while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0)
166 static int nextprop_(const void *fdt, int offset)
172 tag = fdt_next_tag(fdt, offset, &nextoffset);
177 return -FDT_ERR_BADSTRUCTURE;
185 } while (tag == FDT_NOP);
187 return -FDT_ERR_NOTFOUND;
190 int fdt_subnode_offset_namelen(const void *fdt, int offset,
191 const char *name, int namelen)
195 FDT_CHECK_HEADER(fdt);
198 (offset >= 0) && (depth >= 0);
199 offset = fdt_next_node(fdt, offset, &depth))
201 && fdt_nodename_eq_(fdt, offset, name, namelen))
205 return -FDT_ERR_NOTFOUND;
206 return offset; /* error */
209 int fdt_subnode_offset(const void *fdt, int parentoffset,
212 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
215 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
217 const char *end = path + namelen;
218 const char *p = path;
221 FDT_CHECK_HEADER(fdt);
223 /* see if we have an alias */
225 const char *q = memchr(path, '/', end - p);
230 p = fdt_get_alias_namelen(fdt, p, q - p);
232 return -FDT_ERR_BADPATH;
233 offset = fdt_path_offset(fdt, p);
246 q = memchr(p, '/', end - p);
250 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
260 int fdt_path_offset(const void *fdt, const char *path)
262 return fdt_path_offset_namelen(fdt, path, strlen(path));
265 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
267 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
271 if (((err = fdt_check_header(fdt)) != 0)
272 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
277 if (fdt_version(fdt) < 0x10) {
279 * For old FDT versions, match the naming conventions of V16:
280 * give only the leaf name (after all /). The actual tree
281 * contents are loosely checked.
284 leaf = strrchr(nameptr, '/');
286 err = -FDT_ERR_BADSTRUCTURE;
293 *len = strlen(nameptr);
303 int fdt_first_property_offset(const void *fdt, int nodeoffset)
307 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
310 return nextprop_(fdt, offset);
313 int fdt_next_property_offset(const void *fdt, int offset)
315 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
318 return nextprop_(fdt, offset);
321 static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
326 const struct fdt_property *prop;
328 if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
334 prop = fdt_offset_ptr_(fdt, offset);
337 *lenp = fdt32_to_cpu(prop->len);
342 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
346 /* Prior to version 16, properties may need realignment
347 * and this API does not work. fdt_getprop_*() will, however. */
349 if (fdt_version(fdt) < 0x10) {
351 *lenp = -FDT_ERR_BADVERSION;
355 return fdt_get_property_by_offset_(fdt, offset, lenp);
358 static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
365 for (offset = fdt_first_property_offset(fdt, offset);
367 (offset = fdt_next_property_offset(fdt, offset))) {
368 const struct fdt_property *prop;
370 if (!(prop = fdt_get_property_by_offset_(fdt, offset, lenp))) {
371 offset = -FDT_ERR_INTERNAL;
374 if (fdt_string_eq_(fdt, fdt32_to_cpu(prop->nameoff),
388 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
391 int namelen, int *lenp)
393 /* Prior to version 16, properties may need realignment
394 * and this API does not work. fdt_getprop_*() will, however. */
395 if (fdt_version(fdt) < 0x10) {
397 *lenp = -FDT_ERR_BADVERSION;
401 return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
406 const struct fdt_property *fdt_get_property(const void *fdt,
408 const char *name, int *lenp)
410 return fdt_get_property_namelen(fdt, nodeoffset, name,
414 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
415 const char *name, int namelen, int *lenp)
418 const struct fdt_property *prop;
420 prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
425 /* Handle realignment */
426 if (fdt_version(fdt) < 0x10 && (poffset + sizeof(*prop)) % 8 &&
427 fdt32_to_cpu(prop->len) >= 8)
428 return prop->data + 4;
432 const void *fdt_getprop_by_offset(const void *fdt, int offset,
433 const char **namep, int *lenp)
435 const struct fdt_property *prop;
437 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
441 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
443 /* Handle realignment */
444 if (fdt_version(fdt) < 0x10 && (offset + sizeof(*prop)) % 8 &&
445 fdt32_to_cpu(prop->len) >= 8)
446 return prop->data + 4;
450 const void *fdt_getprop(const void *fdt, int nodeoffset,
451 const char *name, int *lenp)
453 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
456 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
461 /* FIXME: This is a bit sub-optimal, since we potentially scan
462 * over all the properties twice. */
463 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
464 if (!php || (len != sizeof(*php))) {
465 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
466 if (!php || (len != sizeof(*php)))
470 return fdt32_to_cpu(*php);
473 const char *fdt_get_alias_namelen(const void *fdt,
474 const char *name, int namelen)
478 aliasoffset = fdt_path_offset(fdt, "/aliases");
482 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
485 const char *fdt_get_alias(const void *fdt, const char *name)
487 return fdt_get_alias_namelen(fdt, name, strlen(name));
490 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
492 int pdepth = 0, p = 0;
493 int offset, depth, namelen;
496 FDT_CHECK_HEADER(fdt);
499 return -FDT_ERR_NOSPACE;
501 for (offset = 0, depth = 0;
502 (offset >= 0) && (offset <= nodeoffset);
503 offset = fdt_next_node(fdt, offset, &depth)) {
504 while (pdepth > depth) {
507 } while (buf[p-1] != '/');
511 if (pdepth >= depth) {
512 name = fdt_get_name(fdt, offset, &namelen);
515 if ((p + namelen + 1) <= buflen) {
516 memcpy(buf + p, name, namelen);
523 if (offset == nodeoffset) {
524 if (pdepth < (depth + 1))
525 return -FDT_ERR_NOSPACE;
527 if (p > 1) /* special case so that root path is "/", not "" */
534 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
535 return -FDT_ERR_BADOFFSET;
536 else if (offset == -FDT_ERR_BADOFFSET)
537 return -FDT_ERR_BADSTRUCTURE;
539 return offset; /* error from fdt_next_node() */
542 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
543 int supernodedepth, int *nodedepth)
546 int supernodeoffset = -FDT_ERR_INTERNAL;
548 FDT_CHECK_HEADER(fdt);
550 if (supernodedepth < 0)
551 return -FDT_ERR_NOTFOUND;
553 for (offset = 0, depth = 0;
554 (offset >= 0) && (offset <= nodeoffset);
555 offset = fdt_next_node(fdt, offset, &depth)) {
556 if (depth == supernodedepth)
557 supernodeoffset = offset;
559 if (offset == nodeoffset) {
563 if (supernodedepth > depth)
564 return -FDT_ERR_NOTFOUND;
566 return supernodeoffset;
570 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
571 return -FDT_ERR_BADOFFSET;
572 else if (offset == -FDT_ERR_BADOFFSET)
573 return -FDT_ERR_BADSTRUCTURE;
575 return offset; /* error from fdt_next_node() */
578 int fdt_node_depth(const void *fdt, int nodeoffset)
583 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
585 return (err < 0) ? err : -FDT_ERR_INTERNAL;
589 int fdt_parent_offset(const void *fdt, int nodeoffset)
591 int nodedepth = fdt_node_depth(fdt, nodeoffset);
595 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
596 nodedepth - 1, NULL);
599 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
600 const char *propname,
601 const void *propval, int proplen)
607 FDT_CHECK_HEADER(fdt);
609 /* FIXME: The algorithm here is pretty horrible: we scan each
610 * property of a node in fdt_getprop(), then if that didn't
611 * find what we want, we scan over them again making our way
612 * to the next node. Still it's the easiest to implement
613 * approach; performance can come later. */
614 for (offset = fdt_next_node(fdt, startoffset, NULL);
616 offset = fdt_next_node(fdt, offset, NULL)) {
617 val = fdt_getprop(fdt, offset, propname, &len);
618 if (val && (len == proplen)
619 && (memcmp(val, propval, len) == 0))
623 return offset; /* error from fdt_next_node() */
626 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
630 if ((phandle == 0) || (phandle == -1))
631 return -FDT_ERR_BADPHANDLE;
633 FDT_CHECK_HEADER(fdt);
635 /* FIXME: The algorithm here is pretty horrible: we
636 * potentially scan each property of a node in
637 * fdt_get_phandle(), then if that didn't find what
638 * we want, we scan over them again making our way to the next
639 * node. Still it's the easiest to implement approach;
640 * performance can come later. */
641 for (offset = fdt_next_node(fdt, -1, NULL);
643 offset = fdt_next_node(fdt, offset, NULL)) {
644 if (fdt_get_phandle(fdt, offset) == phandle)
648 return offset; /* error from fdt_next_node() */
651 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
653 int len = strlen(str);
656 while (listlen >= len) {
657 if (memcmp(str, strlist, len+1) == 0)
659 p = memchr(strlist, '\0', listlen);
661 return 0; /* malformed strlist.. */
662 listlen -= (p-strlist) + 1;
668 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
670 const char *list, *end;
671 int length, count = 0;
673 list = fdt_getprop(fdt, nodeoffset, property, &length);
680 length = strnlen(list, end - list) + 1;
682 /* Abort if the last string isn't properly NUL-terminated. */
683 if (list + length > end)
684 return -FDT_ERR_BADVALUE;
693 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
696 int length, len, idx = 0;
697 const char *list, *end;
699 list = fdt_getprop(fdt, nodeoffset, property, &length);
703 len = strlen(string) + 1;
707 length = strnlen(list, end - list) + 1;
709 /* Abort if the last string isn't properly NUL-terminated. */
710 if (list + length > end)
711 return -FDT_ERR_BADVALUE;
713 if (length == len && memcmp(list, string, length) == 0)
720 return -FDT_ERR_NOTFOUND;
723 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
724 const char *property, int idx,
727 const char *list, *end;
730 list = fdt_getprop(fdt, nodeoffset, property, &length);
741 length = strnlen(list, end - list) + 1;
743 /* Abort if the last string isn't properly NUL-terminated. */
744 if (list + length > end) {
746 *lenp = -FDT_ERR_BADVALUE;
763 *lenp = -FDT_ERR_NOTFOUND;
768 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
769 const char *compatible)
774 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
778 return !fdt_stringlist_contains(prop, len, compatible);
781 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
782 const char *compatible)
786 FDT_CHECK_HEADER(fdt);
788 /* FIXME: The algorithm here is pretty horrible: we scan each
789 * property of a node in fdt_node_check_compatible(), then if
790 * that didn't find what we want, we scan over them again
791 * making our way to the next node. Still it's the easiest to
792 * implement approach; performance can come later. */
793 for (offset = fdt_next_node(fdt, startoffset, NULL);
795 offset = fdt_next_node(fdt, offset, NULL)) {
796 err = fdt_node_check_compatible(fdt, offset, compatible);
797 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
803 return offset; /* error from fdt_next_node() */