2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
6 #include <libfdt_env.h>
15 #include "libfdt_internal.h"
17 static int _fdt_nodename_eq(const void *fdt, int offset,
18 const char *s, int len)
20 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
26 if (memcmp(p, s, len) != 0)
31 else if (!memchr(s, '@', len) && (p[len] == '@'))
37 const char *fdt_string(const void *fdt, int stroffset)
39 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
42 static int _fdt_string_eq(const void *fdt, int stroffset,
43 const char *s, int len)
45 const char *p = fdt_string(fdt, stroffset);
47 return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0);
50 uint32_t fdt_get_max_phandle(const void *fdt)
52 uint32_t max_phandle = 0;
55 for (offset = fdt_next_node(fdt, -1, NULL);;
56 offset = fdt_next_node(fdt, offset, NULL)) {
59 if (offset == -FDT_ERR_NOTFOUND)
65 phandle = fdt_get_phandle(fdt, offset);
66 if (phandle == (uint32_t)-1)
69 if (phandle > max_phandle)
70 max_phandle = phandle;
76 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
78 FDT_CHECK_HEADER(fdt);
79 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
80 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
84 int fdt_num_mem_rsv(const void *fdt)
88 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
93 static int _nextprop(const void *fdt, int offset)
99 tag = fdt_next_tag(fdt, offset, &nextoffset);
104 return -FDT_ERR_BADSTRUCTURE;
112 } while (tag == FDT_NOP);
114 return -FDT_ERR_NOTFOUND;
117 int fdt_subnode_offset_namelen(const void *fdt, int offset,
118 const char *name, int namelen)
122 FDT_CHECK_HEADER(fdt);
125 (offset >= 0) && (depth >= 0);
126 offset = fdt_next_node(fdt, offset, &depth))
128 && _fdt_nodename_eq(fdt, offset, name, namelen))
132 return -FDT_ERR_NOTFOUND;
133 return offset; /* error */
136 int fdt_subnode_offset(const void *fdt, int parentoffset,
139 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
143 * Find the next of path separator, note we need to search for both '/' and ':'
144 * and then take the first one so that we do the right thing for e.g.
145 * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
146 * first searching for either ':' or '/' does not work.
148 static const char *fdt_path_next_separator(const char *path, int len)
150 const void *sep1 = memchr(path, '/', len);
151 const void *sep2 = memchr(path, ':', len);
154 return (sep1 < sep2) ? sep1 : sep2;
161 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
163 const char *end = path + namelen;
164 const char *p = path;
167 FDT_CHECK_HEADER(fdt);
169 /* see if we have an alias */
171 const char *q = fdt_path_next_separator(path, namelen);
176 p = fdt_get_alias_namelen(fdt, p, q - p);
178 return -FDT_ERR_BADPATH;
179 offset = fdt_path_offset(fdt, p);
184 while (*p && (p < end)) {
190 if (*p == '\0' || *p == ':')
193 q = fdt_path_next_separator(p, end - p);
197 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
207 int fdt_path_offset(const void *fdt, const char *path)
209 return fdt_path_offset_namelen(fdt, path, strlen(path));
212 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
214 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
217 if (((err = fdt_check_header(fdt)) != 0)
218 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
222 *len = strlen(nh->name);
232 int fdt_first_property_offset(const void *fdt, int nodeoffset)
236 if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
239 return _nextprop(fdt, offset);
242 int fdt_next_property_offset(const void *fdt, int offset)
244 if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
247 return _nextprop(fdt, offset);
250 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
255 const struct fdt_property *prop;
257 if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
263 prop = _fdt_offset_ptr(fdt, offset);
266 *lenp = fdt32_to_cpu(prop->len);
271 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
274 int namelen, int *lenp)
276 for (offset = fdt_first_property_offset(fdt, offset);
278 (offset = fdt_next_property_offset(fdt, offset))) {
279 const struct fdt_property *prop;
281 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
282 offset = -FDT_ERR_INTERNAL;
285 if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
295 const struct fdt_property *fdt_get_property(const void *fdt,
297 const char *name, int *lenp)
299 return fdt_get_property_namelen(fdt, nodeoffset, name,
303 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
304 const char *name, int namelen, int *lenp)
306 const struct fdt_property *prop;
308 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
315 const void *fdt_getprop_by_offset(const void *fdt, int offset,
316 const char **namep, int *lenp)
318 const struct fdt_property *prop;
320 prop = fdt_get_property_by_offset(fdt, offset, lenp);
324 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
328 const void *fdt_getprop(const void *fdt, int nodeoffset,
329 const char *name, int *lenp)
331 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
334 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
339 /* FIXME: This is a bit sub-optimal, since we potentially scan
340 * over all the properties twice. */
341 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
342 if (!php || (len != sizeof(*php))) {
343 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
344 if (!php || (len != sizeof(*php)))
348 return fdt32_to_cpu(*php);
351 const char *fdt_get_alias_namelen(const void *fdt,
352 const char *name, int namelen)
356 aliasoffset = fdt_path_offset(fdt, "/aliases");
360 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
363 const char *fdt_get_alias(const void *fdt, const char *name)
365 return fdt_get_alias_namelen(fdt, name, strlen(name));
368 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
370 int pdepth = 0, p = 0;
371 int offset, depth, namelen;
374 FDT_CHECK_HEADER(fdt);
377 return -FDT_ERR_NOSPACE;
379 for (offset = 0, depth = 0;
380 (offset >= 0) && (offset <= nodeoffset);
381 offset = fdt_next_node(fdt, offset, &depth)) {
382 while (pdepth > depth) {
385 } while (buf[p-1] != '/');
389 if (pdepth >= depth) {
390 name = fdt_get_name(fdt, offset, &namelen);
393 if ((p + namelen + 1) <= buflen) {
394 memcpy(buf + p, name, namelen);
401 if (offset == nodeoffset) {
402 if (pdepth < (depth + 1))
403 return -FDT_ERR_NOSPACE;
405 if (p > 1) /* special case so that root path is "/", not "" */
412 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
413 return -FDT_ERR_BADOFFSET;
414 else if (offset == -FDT_ERR_BADOFFSET)
415 return -FDT_ERR_BADSTRUCTURE;
417 return offset; /* error from fdt_next_node() */
420 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
421 int supernodedepth, int *nodedepth)
424 int supernodeoffset = -FDT_ERR_INTERNAL;
426 FDT_CHECK_HEADER(fdt);
428 if (supernodedepth < 0)
429 return -FDT_ERR_NOTFOUND;
431 for (offset = 0, depth = 0;
432 (offset >= 0) && (offset <= nodeoffset);
433 offset = fdt_next_node(fdt, offset, &depth)) {
434 if (depth == supernodedepth)
435 supernodeoffset = offset;
437 if (offset == nodeoffset) {
441 if (supernodedepth > depth)
442 return -FDT_ERR_NOTFOUND;
444 return supernodeoffset;
448 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
449 return -FDT_ERR_BADOFFSET;
450 else if (offset == -FDT_ERR_BADOFFSET)
451 return -FDT_ERR_BADSTRUCTURE;
453 return offset; /* error from fdt_next_node() */
456 int fdt_node_depth(const void *fdt, int nodeoffset)
461 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
463 return (err < 0) ? err : -FDT_ERR_INTERNAL;
467 int fdt_parent_offset(const void *fdt, int nodeoffset)
469 int nodedepth = fdt_node_depth(fdt, nodeoffset);
473 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
474 nodedepth - 1, NULL);
477 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
478 const char *propname,
479 const void *propval, int proplen)
485 FDT_CHECK_HEADER(fdt);
487 /* FIXME: The algorithm here is pretty horrible: we scan each
488 * property of a node in fdt_getprop(), then if that didn't
489 * find what we want, we scan over them again making our way
490 * to the next node. Still it's the easiest to implement
491 * approach; performance can come later. */
492 for (offset = fdt_next_node(fdt, startoffset, NULL);
494 offset = fdt_next_node(fdt, offset, NULL)) {
495 val = fdt_getprop(fdt, offset, propname, &len);
496 if (val && (len == proplen)
497 && (memcmp(val, propval, len) == 0))
501 return offset; /* error from fdt_next_node() */
504 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
508 if ((phandle == 0) || (phandle == -1))
509 return -FDT_ERR_BADPHANDLE;
511 FDT_CHECK_HEADER(fdt);
513 /* FIXME: The algorithm here is pretty horrible: we
514 * potentially scan each property of a node in
515 * fdt_get_phandle(), then if that didn't find what
516 * we want, we scan over them again making our way to the next
517 * node. Still it's the easiest to implement approach;
518 * performance can come later. */
519 for (offset = fdt_next_node(fdt, -1, NULL);
521 offset = fdt_next_node(fdt, offset, NULL)) {
522 if (fdt_get_phandle(fdt, offset) == phandle)
526 return offset; /* error from fdt_next_node() */
529 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
531 int len = strlen(str);
534 while (listlen >= len) {
535 if (memcmp(str, strlist, len+1) == 0)
537 p = memchr(strlist, '\0', listlen);
539 return 0; /* malformed strlist.. */
540 listlen -= (p-strlist) + 1;
546 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
548 const char *list, *end;
549 int length, count = 0;
551 list = fdt_getprop(fdt, nodeoffset, property, &length);
558 length = strnlen(list, end - list) + 1;
560 /* Abort if the last string isn't properly NUL-terminated. */
561 if (list + length > end)
562 return -FDT_ERR_BADVALUE;
571 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
574 int length, len, idx = 0;
575 const char *list, *end;
577 list = fdt_getprop(fdt, nodeoffset, property, &length);
581 len = strlen(string) + 1;
585 length = strnlen(list, end - list) + 1;
587 /* Abort if the last string isn't properly NUL-terminated. */
588 if (list + length > end)
589 return -FDT_ERR_BADVALUE;
591 if (length == len && memcmp(list, string, length) == 0)
598 return -FDT_ERR_NOTFOUND;
601 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
602 const char *property, int idx,
605 const char *list, *end;
608 list = fdt_getprop(fdt, nodeoffset, property, &length);
619 length = strnlen(list, end - list) + 1;
621 /* Abort if the last string isn't properly NUL-terminated. */
622 if (list + length > end) {
624 *lenp = -FDT_ERR_BADVALUE;
641 *lenp = -FDT_ERR_NOTFOUND;
646 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
647 const char *compatible)
652 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
656 return !fdt_stringlist_contains(prop, len, compatible);
659 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
660 const char *compatible)
664 FDT_CHECK_HEADER(fdt);
666 /* FIXME: The algorithm here is pretty horrible: we scan each
667 * property of a node in fdt_node_check_compatible(), then if
668 * that didn't find what we want, we scan over them again
669 * making our way to the next node. Still it's the easiest to
670 * implement approach; performance can come later. */
671 for (offset = fdt_next_node(fdt, startoffset, NULL);
673 offset = fdt_next_node(fdt, offset, NULL)) {
674 err = fdt_node_check_compatible(fdt, offset, compatible);
675 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
681 return offset; /* error from fdt_next_node() */