1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 #include <linux/libfdt_env.h>
10 #include <linux/libfdt.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_generate_phandle(const void *fdt, uint32_t *phandle)
84 offset = fdt_next_node(fdt, offset, NULL);
86 if (offset == -FDT_ERR_NOTFOUND)
92 value = fdt_get_phandle(fdt, offset);
98 if (max == FDT_MAX_PHANDLE)
99 return -FDT_ERR_NOPHANDLES;
107 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
109 FDT_CHECK_HEADER(fdt);
110 *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address);
111 *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size);
115 int fdt_num_mem_rsv(const void *fdt)
119 while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0)
124 static int _nextprop(const void *fdt, int offset)
130 tag = fdt_next_tag(fdt, offset, &nextoffset);
135 return -FDT_ERR_BADSTRUCTURE;
143 } while (tag == FDT_NOP);
145 return -FDT_ERR_NOTFOUND;
148 int fdt_subnode_offset_namelen(const void *fdt, int offset,
149 const char *name, int namelen)
153 FDT_CHECK_HEADER(fdt);
156 (offset >= 0) && (depth >= 0);
157 offset = fdt_next_node(fdt, offset, &depth))
159 && _fdt_nodename_eq(fdt, offset, name, namelen))
163 return -FDT_ERR_NOTFOUND;
164 return offset; /* error */
167 int fdt_subnode_offset(const void *fdt, int parentoffset,
170 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
174 * Find the next of path separator, note we need to search for both '/' and ':'
175 * and then take the first one so that we do the right thing for e.g.
176 * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
177 * first searching for either ':' or '/' does not work.
179 static const char *fdt_path_next_separator(const char *path, int len)
181 const void *sep1 = memchr(path, '/', len);
182 const void *sep2 = memchr(path, ':', len);
185 return (sep1 < sep2) ? sep1 : sep2;
192 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
194 const char *end = path + namelen;
195 const char *p = path;
198 FDT_CHECK_HEADER(fdt);
200 /* see if we have an alias */
202 const char *q = fdt_path_next_separator(path, namelen);
207 p = fdt_get_alias_namelen(fdt, p, q - p);
209 return -FDT_ERR_BADPATH;
210 offset = fdt_path_offset(fdt, p);
215 while (*p && (p < end)) {
221 if (*p == '\0' || *p == ':')
224 q = fdt_path_next_separator(p, end - p);
228 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
238 int fdt_path_offset(const void *fdt, const char *path)
240 return fdt_path_offset_namelen(fdt, path, strlen(path));
243 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
245 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
248 if (((err = fdt_check_header(fdt)) != 0)
249 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
253 *len = strlen(nh->name);
263 int fdt_first_property_offset(const void *fdt, int nodeoffset)
267 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
270 return _nextprop(fdt, offset);
273 int fdt_next_property_offset(const void *fdt, int offset)
275 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
278 return _nextprop(fdt, offset);
281 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
286 const struct fdt_property *prop;
288 if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
294 prop = fdt_offset_ptr_(fdt, offset);
297 *lenp = fdt32_to_cpu(prop->len);
302 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
305 int namelen, int *lenp)
307 for (offset = fdt_first_property_offset(fdt, offset);
309 (offset = fdt_next_property_offset(fdt, offset))) {
310 const struct fdt_property *prop;
312 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
313 offset = -FDT_ERR_INTERNAL;
316 if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
326 const struct fdt_property *fdt_get_property(const void *fdt,
328 const char *name, int *lenp)
330 return fdt_get_property_namelen(fdt, nodeoffset, name,
334 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
335 const char *name, int namelen, int *lenp)
337 const struct fdt_property *prop;
339 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
346 const void *fdt_getprop_by_offset(const void *fdt, int offset,
347 const char **namep, int *lenp)
349 const struct fdt_property *prop;
351 prop = fdt_get_property_by_offset(fdt, offset, lenp);
355 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
359 const void *fdt_getprop(const void *fdt, int nodeoffset,
360 const char *name, int *lenp)
362 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
365 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
370 /* FIXME: This is a bit sub-optimal, since we potentially scan
371 * over all the properties twice. */
372 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
373 if (!php || (len != sizeof(*php))) {
374 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
375 if (!php || (len != sizeof(*php)))
379 return fdt32_to_cpu(*php);
382 const char *fdt_get_alias_namelen(const void *fdt,
383 const char *name, int namelen)
387 aliasoffset = fdt_path_offset(fdt, "/aliases");
391 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
394 const char *fdt_get_alias(const void *fdt, const char *name)
396 return fdt_get_alias_namelen(fdt, name, strlen(name));
399 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
401 int pdepth = 0, p = 0;
402 int offset, depth, namelen;
405 FDT_CHECK_HEADER(fdt);
408 return -FDT_ERR_NOSPACE;
410 for (offset = 0, depth = 0;
411 (offset >= 0) && (offset <= nodeoffset);
412 offset = fdt_next_node(fdt, offset, &depth)) {
413 while (pdepth > depth) {
416 } while (buf[p-1] != '/');
420 if (pdepth >= depth) {
421 name = fdt_get_name(fdt, offset, &namelen);
424 if ((p + namelen + 1) <= buflen) {
425 memcpy(buf + p, name, namelen);
432 if (offset == nodeoffset) {
433 if (pdepth < (depth + 1))
434 return -FDT_ERR_NOSPACE;
436 if (p > 1) /* special case so that root path is "/", not "" */
443 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
444 return -FDT_ERR_BADOFFSET;
445 else if (offset == -FDT_ERR_BADOFFSET)
446 return -FDT_ERR_BADSTRUCTURE;
448 return offset; /* error from fdt_next_node() */
451 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
452 int supernodedepth, int *nodedepth)
455 int supernodeoffset = -FDT_ERR_INTERNAL;
457 FDT_CHECK_HEADER(fdt);
459 if (supernodedepth < 0)
460 return -FDT_ERR_NOTFOUND;
462 for (offset = 0, depth = 0;
463 (offset >= 0) && (offset <= nodeoffset);
464 offset = fdt_next_node(fdt, offset, &depth)) {
465 if (depth == supernodedepth)
466 supernodeoffset = offset;
468 if (offset == nodeoffset) {
472 if (supernodedepth > depth)
473 return -FDT_ERR_NOTFOUND;
475 return supernodeoffset;
479 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
480 return -FDT_ERR_BADOFFSET;
481 else if (offset == -FDT_ERR_BADOFFSET)
482 return -FDT_ERR_BADSTRUCTURE;
484 return offset; /* error from fdt_next_node() */
487 int fdt_node_depth(const void *fdt, int nodeoffset)
492 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
494 return (err < 0) ? err : -FDT_ERR_INTERNAL;
498 int fdt_parent_offset(const void *fdt, int nodeoffset)
500 int nodedepth = fdt_node_depth(fdt, nodeoffset);
504 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
505 nodedepth - 1, NULL);
508 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
509 const char *propname,
510 const void *propval, int proplen)
516 FDT_CHECK_HEADER(fdt);
518 /* FIXME: The algorithm here is pretty horrible: we scan each
519 * property of a node in fdt_getprop(), then if that didn't
520 * find what we want, we scan over them again making our way
521 * to the next node. Still it's the easiest to implement
522 * approach; performance can come later. */
523 for (offset = fdt_next_node(fdt, startoffset, NULL);
525 offset = fdt_next_node(fdt, offset, NULL)) {
526 val = fdt_getprop(fdt, offset, propname, &len);
527 if (val && (len == proplen)
528 && (memcmp(val, propval, len) == 0))
532 return offset; /* error from fdt_next_node() */
535 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
539 if ((phandle == 0) || (phandle == -1))
540 return -FDT_ERR_BADPHANDLE;
542 FDT_CHECK_HEADER(fdt);
544 /* FIXME: The algorithm here is pretty horrible: we
545 * potentially scan each property of a node in
546 * fdt_get_phandle(), then if that didn't find what
547 * we want, we scan over them again making our way to the next
548 * node. Still it's the easiest to implement approach;
549 * performance can come later. */
550 for (offset = fdt_next_node(fdt, -1, NULL);
552 offset = fdt_next_node(fdt, offset, NULL)) {
553 if (fdt_get_phandle(fdt, offset) == phandle)
557 return offset; /* error from fdt_next_node() */
560 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
562 int len = strlen(str);
565 while (listlen >= len) {
566 if (memcmp(str, strlist, len+1) == 0)
568 p = memchr(strlist, '\0', listlen);
570 return 0; /* malformed strlist.. */
571 listlen -= (p-strlist) + 1;
577 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
579 const char *list, *end;
580 int length, count = 0;
582 list = fdt_getprop(fdt, nodeoffset, property, &length);
589 length = strnlen(list, end - list) + 1;
591 /* Abort if the last string isn't properly NUL-terminated. */
592 if (list + length > end)
593 return -FDT_ERR_BADVALUE;
602 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
605 int length, len, idx = 0;
606 const char *list, *end;
608 list = fdt_getprop(fdt, nodeoffset, property, &length);
612 len = strlen(string) + 1;
616 length = strnlen(list, end - list) + 1;
618 /* Abort if the last string isn't properly NUL-terminated. */
619 if (list + length > end)
620 return -FDT_ERR_BADVALUE;
622 if (length == len && memcmp(list, string, length) == 0)
629 return -FDT_ERR_NOTFOUND;
632 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
633 const char *property, int idx,
636 const char *list, *end;
639 list = fdt_getprop(fdt, nodeoffset, property, &length);
650 length = strnlen(list, end - list) + 1;
652 /* Abort if the last string isn't properly NUL-terminated. */
653 if (list + length > end) {
655 *lenp = -FDT_ERR_BADVALUE;
672 *lenp = -FDT_ERR_NOTFOUND;
677 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
678 const char *compatible)
683 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
687 return !fdt_stringlist_contains(prop, len, compatible);
690 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
691 const char *compatible)
695 FDT_CHECK_HEADER(fdt);
697 /* FIXME: The algorithm here is pretty horrible: we scan each
698 * property of a node in fdt_node_check_compatible(), then if
699 * that didn't find what we want, we scan over them again
700 * making our way to the next node. Still it's the easiest to
701 * implement approach; performance can come later. */
702 for (offset = fdt_next_node(fdt, startoffset, NULL);
704 offset = fdt_next_node(fdt, offset, NULL)) {
705 err = fdt_node_check_compatible(fdt, offset, compatible);
706 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
712 return offset; /* error from fdt_next_node() */