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)
61 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
67 if (memcmp(p, s, len) != 0)
72 else if (!memchr(s, '@', len) && (p[len] == '@'))
78 const char *fdt_string(const void *fdt, int stroffset)
80 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
83 static int _fdt_string_eq(const void *fdt, int stroffset,
84 const char *s, int len)
86 const char *p = fdt_string(fdt, stroffset);
88 return (strlen(p) == len) && (memcmp(p, s, len) == 0);
91 uint32_t fdt_get_max_phandle(const void *fdt)
93 uint32_t max_phandle = 0;
96 for (offset = fdt_next_node(fdt, -1, NULL);;
97 offset = fdt_next_node(fdt, offset, NULL)) {
100 if (offset == -FDT_ERR_NOTFOUND)
106 phandle = fdt_get_phandle(fdt, offset);
107 if (phandle == (uint32_t)-1)
110 if (phandle > max_phandle)
111 max_phandle = phandle;
117 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
119 FDT_CHECK_HEADER(fdt);
120 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
121 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
125 int fdt_num_mem_rsv(const void *fdt)
129 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
134 static int _nextprop(const void *fdt, int offset)
140 tag = fdt_next_tag(fdt, offset, &nextoffset);
145 return -FDT_ERR_BADSTRUCTURE;
153 } while (tag == FDT_NOP);
155 return -FDT_ERR_NOTFOUND;
158 int fdt_subnode_offset_namelen(const void *fdt, int offset,
159 const char *name, int namelen)
163 FDT_CHECK_HEADER(fdt);
166 (offset >= 0) && (depth >= 0);
167 offset = fdt_next_node(fdt, offset, &depth))
169 && _fdt_nodename_eq(fdt, offset, name, namelen))
173 return -FDT_ERR_NOTFOUND;
174 return offset; /* error */
177 int fdt_subnode_offset(const void *fdt, int parentoffset,
180 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
183 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
185 const char *end = path + namelen;
186 const char *p = path;
189 FDT_CHECK_HEADER(fdt);
191 /* see if we have an alias */
193 const char *q = memchr(path, '/', end - p);
198 p = fdt_get_alias_namelen(fdt, p, q - p);
200 return -FDT_ERR_BADPATH;
201 offset = fdt_path_offset(fdt, p);
214 q = memchr(p, '/', end - p);
218 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
228 int fdt_path_offset(const void *fdt, const char *path)
230 return fdt_path_offset_namelen(fdt, path, strlen(path));
233 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
235 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
238 if (((err = fdt_check_header(fdt)) != 0)
239 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
243 *len = strlen(nh->name);
253 int fdt_first_property_offset(const void *fdt, int nodeoffset)
257 if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
260 return _nextprop(fdt, offset);
263 int fdt_next_property_offset(const void *fdt, int offset)
265 if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
268 return _nextprop(fdt, offset);
271 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
276 const struct fdt_property *prop;
278 if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
284 prop = _fdt_offset_ptr(fdt, offset);
287 *lenp = fdt32_to_cpu(prop->len);
292 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
295 int namelen, int *lenp)
297 for (offset = fdt_first_property_offset(fdt, offset);
299 (offset = fdt_next_property_offset(fdt, offset))) {
300 const struct fdt_property *prop;
302 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
303 offset = -FDT_ERR_INTERNAL;
306 if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
316 const struct fdt_property *fdt_get_property(const void *fdt,
318 const char *name, int *lenp)
320 return fdt_get_property_namelen(fdt, nodeoffset, name,
324 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
325 const char *name, int namelen, int *lenp)
327 const struct fdt_property *prop;
329 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
336 const void *fdt_getprop_by_offset(const void *fdt, int offset,
337 const char **namep, int *lenp)
339 const struct fdt_property *prop;
341 prop = fdt_get_property_by_offset(fdt, offset, lenp);
345 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
349 const void *fdt_getprop(const void *fdt, int nodeoffset,
350 const char *name, int *lenp)
352 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
355 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
360 /* FIXME: This is a bit sub-optimal, since we potentially scan
361 * over all the properties twice. */
362 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
363 if (!php || (len != sizeof(*php))) {
364 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
365 if (!php || (len != sizeof(*php)))
369 return fdt32_to_cpu(*php);
372 const char *fdt_get_alias_namelen(const void *fdt,
373 const char *name, int namelen)
377 aliasoffset = fdt_path_offset(fdt, "/aliases");
381 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
384 const char *fdt_get_alias(const void *fdt, const char *name)
386 return fdt_get_alias_namelen(fdt, name, strlen(name));
389 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
391 int pdepth = 0, p = 0;
392 int offset, depth, namelen;
395 FDT_CHECK_HEADER(fdt);
398 return -FDT_ERR_NOSPACE;
400 for (offset = 0, depth = 0;
401 (offset >= 0) && (offset <= nodeoffset);
402 offset = fdt_next_node(fdt, offset, &depth)) {
403 while (pdepth > depth) {
406 } while (buf[p-1] != '/');
410 if (pdepth >= depth) {
411 name = fdt_get_name(fdt, offset, &namelen);
414 if ((p + namelen + 1) <= buflen) {
415 memcpy(buf + p, name, namelen);
422 if (offset == nodeoffset) {
423 if (pdepth < (depth + 1))
424 return -FDT_ERR_NOSPACE;
426 if (p > 1) /* special case so that root path is "/", not "" */
433 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
434 return -FDT_ERR_BADOFFSET;
435 else if (offset == -FDT_ERR_BADOFFSET)
436 return -FDT_ERR_BADSTRUCTURE;
438 return offset; /* error from fdt_next_node() */
441 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
442 int supernodedepth, int *nodedepth)
445 int supernodeoffset = -FDT_ERR_INTERNAL;
447 FDT_CHECK_HEADER(fdt);
449 if (supernodedepth < 0)
450 return -FDT_ERR_NOTFOUND;
452 for (offset = 0, depth = 0;
453 (offset >= 0) && (offset <= nodeoffset);
454 offset = fdt_next_node(fdt, offset, &depth)) {
455 if (depth == supernodedepth)
456 supernodeoffset = offset;
458 if (offset == nodeoffset) {
462 if (supernodedepth > depth)
463 return -FDT_ERR_NOTFOUND;
465 return supernodeoffset;
469 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
470 return -FDT_ERR_BADOFFSET;
471 else if (offset == -FDT_ERR_BADOFFSET)
472 return -FDT_ERR_BADSTRUCTURE;
474 return offset; /* error from fdt_next_node() */
477 int fdt_node_depth(const void *fdt, int nodeoffset)
482 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
484 return (err < 0) ? err : -FDT_ERR_INTERNAL;
488 int fdt_parent_offset(const void *fdt, int nodeoffset)
490 int nodedepth = fdt_node_depth(fdt, nodeoffset);
494 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
495 nodedepth - 1, NULL);
498 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
499 const char *propname,
500 const void *propval, int proplen)
506 FDT_CHECK_HEADER(fdt);
508 /* FIXME: The algorithm here is pretty horrible: we scan each
509 * property of a node in fdt_getprop(), then if that didn't
510 * find what we want, we scan over them again making our way
511 * to the next node. Still it's the easiest to implement
512 * approach; performance can come later. */
513 for (offset = fdt_next_node(fdt, startoffset, NULL);
515 offset = fdt_next_node(fdt, offset, NULL)) {
516 val = fdt_getprop(fdt, offset, propname, &len);
517 if (val && (len == proplen)
518 && (memcmp(val, propval, len) == 0))
522 return offset; /* error from fdt_next_node() */
525 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
529 if ((phandle == 0) || (phandle == -1))
530 return -FDT_ERR_BADPHANDLE;
532 FDT_CHECK_HEADER(fdt);
534 /* FIXME: The algorithm here is pretty horrible: we
535 * potentially scan each property of a node in
536 * fdt_get_phandle(), then if that didn't find what
537 * we want, we scan over them again making our way to the next
538 * node. Still it's the easiest to implement approach;
539 * performance can come later. */
540 for (offset = fdt_next_node(fdt, -1, NULL);
542 offset = fdt_next_node(fdt, offset, NULL)) {
543 if (fdt_get_phandle(fdt, offset) == phandle)
547 return offset; /* error from fdt_next_node() */
550 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
552 int len = strlen(str);
555 while (listlen >= len) {
556 if (memcmp(str, strlist, len+1) == 0)
558 p = memchr(strlist, '\0', listlen);
560 return 0; /* malformed strlist.. */
561 listlen -= (p-strlist) + 1;
567 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
569 const char *list, *end;
570 int length, count = 0;
572 list = fdt_getprop(fdt, nodeoffset, property, &length);
579 length = strnlen(list, end - list) + 1;
581 /* Abort if the last string isn't properly NUL-terminated. */
582 if (list + length > end)
583 return -FDT_ERR_BADVALUE;
592 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
595 int length, len, idx = 0;
596 const char *list, *end;
598 list = fdt_getprop(fdt, nodeoffset, property, &length);
602 len = strlen(string) + 1;
606 length = strnlen(list, end - list) + 1;
608 /* Abort if the last string isn't properly NUL-terminated. */
609 if (list + length > end)
610 return -FDT_ERR_BADVALUE;
612 if (length == len && memcmp(list, string, length) == 0)
619 return -FDT_ERR_NOTFOUND;
622 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
623 const char *property, int idx,
626 const char *list, *end;
629 list = fdt_getprop(fdt, nodeoffset, property, &length);
640 length = strnlen(list, end - list) + 1;
642 /* Abort if the last string isn't properly NUL-terminated. */
643 if (list + length > end) {
645 *lenp = -FDT_ERR_BADVALUE;
662 *lenp = -FDT_ERR_NOTFOUND;
667 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
668 const char *compatible)
673 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
677 return !fdt_stringlist_contains(prop, len, compatible);
680 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
681 const char *compatible)
685 FDT_CHECK_HEADER(fdt);
687 /* FIXME: The algorithm here is pretty horrible: we scan each
688 * property of a node in fdt_node_check_compatible(), then if
689 * that didn't find what we want, we scan over them again
690 * making our way to the next node. Still it's the easiest to
691 * implement approach; performance can come later. */
692 for (offset = fdt_next_node(fdt, startoffset, NULL);
694 offset = fdt_next_node(fdt, offset, NULL)) {
695 err = fdt_node_check_compatible(fdt, offset, compatible);
696 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
702 return offset; /* error from fdt_next_node() */