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 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
52 FDT_CHECK_HEADER(fdt);
53 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
54 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
58 int fdt_num_mem_rsv(const void *fdt)
62 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
67 static int _nextprop(const void *fdt, int offset)
73 tag = fdt_next_tag(fdt, offset, &nextoffset);
78 return -FDT_ERR_BADSTRUCTURE;
86 } while (tag == FDT_NOP);
88 return -FDT_ERR_NOTFOUND;
91 int fdt_subnode_offset_namelen(const void *fdt, int offset,
92 const char *name, int namelen)
96 FDT_CHECK_HEADER(fdt);
99 (offset >= 0) && (depth >= 0);
100 offset = fdt_next_node(fdt, offset, &depth))
102 && _fdt_nodename_eq(fdt, offset, name, namelen))
106 return -FDT_ERR_NOTFOUND;
107 return offset; /* error */
110 int fdt_subnode_offset(const void *fdt, int parentoffset,
113 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
117 * Find the next of path seperator, note we need to search for both '/' and ':'
118 * and then take the first one so that we do the rigth thing for e.g.
119 * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
120 * first searching for either ':' or '/' does not work.
122 static const char *fdt_path_next_seperator(const char *path)
124 const char *sep1 = strchr(path, '/');
125 const char *sep2 = strchr(path, ':');
128 return (sep1 < sep2) ? sep1 : sep2;
135 int fdt_path_offset(const void *fdt, const char *path)
137 const char *end = path + strlen(path);
138 const char *p = path;
141 FDT_CHECK_HEADER(fdt);
143 /* see if we have an alias */
145 const char *q = fdt_path_next_seperator(path);
150 p = fdt_get_alias_namelen(fdt, p, q - p);
152 return -FDT_ERR_BADPATH;
153 offset = fdt_path_offset(fdt, p);
163 if (*p == '\0' || *p == ':')
165 q = fdt_path_next_seperator(p);
169 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
179 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
181 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
184 if (((err = fdt_check_header(fdt)) != 0)
185 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
189 *len = strlen(nh->name);
199 int fdt_first_property_offset(const void *fdt, int nodeoffset)
203 if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
206 return _nextprop(fdt, offset);
209 int fdt_next_property_offset(const void *fdt, int offset)
211 if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
214 return _nextprop(fdt, offset);
217 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
222 const struct fdt_property *prop;
224 if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
230 prop = _fdt_offset_ptr(fdt, offset);
233 *lenp = fdt32_to_cpu(prop->len);
238 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
241 int namelen, int *lenp)
243 for (offset = fdt_first_property_offset(fdt, offset);
245 (offset = fdt_next_property_offset(fdt, offset))) {
246 const struct fdt_property *prop;
248 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
249 offset = -FDT_ERR_INTERNAL;
252 if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
262 const struct fdt_property *fdt_get_property(const void *fdt,
264 const char *name, int *lenp)
266 return fdt_get_property_namelen(fdt, nodeoffset, name,
270 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
271 const char *name, int namelen, int *lenp)
273 const struct fdt_property *prop;
275 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
282 const void *fdt_getprop_by_offset(const void *fdt, int offset,
283 const char **namep, int *lenp)
285 const struct fdt_property *prop;
287 prop = fdt_get_property_by_offset(fdt, offset, lenp);
291 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
295 const void *fdt_getprop(const void *fdt, int nodeoffset,
296 const char *name, int *lenp)
298 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
301 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
306 /* FIXME: This is a bit sub-optimal, since we potentially scan
307 * over all the properties twice. */
308 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
309 if (!php || (len != sizeof(*php))) {
310 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
311 if (!php || (len != sizeof(*php)))
315 return fdt32_to_cpu(*php);
318 const char *fdt_get_alias_namelen(const void *fdt,
319 const char *name, int namelen)
323 aliasoffset = fdt_path_offset(fdt, "/aliases");
327 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
330 const char *fdt_get_alias(const void *fdt, const char *name)
332 return fdt_get_alias_namelen(fdt, name, strlen(name));
335 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
337 int pdepth = 0, p = 0;
338 int offset, depth, namelen;
341 FDT_CHECK_HEADER(fdt);
344 return -FDT_ERR_NOSPACE;
346 for (offset = 0, depth = 0;
347 (offset >= 0) && (offset <= nodeoffset);
348 offset = fdt_next_node(fdt, offset, &depth)) {
349 while (pdepth > depth) {
352 } while (buf[p-1] != '/');
356 if (pdepth >= depth) {
357 name = fdt_get_name(fdt, offset, &namelen);
360 if ((p + namelen + 1) <= buflen) {
361 memcpy(buf + p, name, namelen);
368 if (offset == nodeoffset) {
369 if (pdepth < (depth + 1))
370 return -FDT_ERR_NOSPACE;
372 if (p > 1) /* special case so that root path is "/", not "" */
379 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
380 return -FDT_ERR_BADOFFSET;
381 else if (offset == -FDT_ERR_BADOFFSET)
382 return -FDT_ERR_BADSTRUCTURE;
384 return offset; /* error from fdt_next_node() */
387 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
388 int supernodedepth, int *nodedepth)
391 int supernodeoffset = -FDT_ERR_INTERNAL;
393 FDT_CHECK_HEADER(fdt);
395 if (supernodedepth < 0)
396 return -FDT_ERR_NOTFOUND;
398 for (offset = 0, depth = 0;
399 (offset >= 0) && (offset <= nodeoffset);
400 offset = fdt_next_node(fdt, offset, &depth)) {
401 if (depth == supernodedepth)
402 supernodeoffset = offset;
404 if (offset == nodeoffset) {
408 if (supernodedepth > depth)
409 return -FDT_ERR_NOTFOUND;
411 return supernodeoffset;
415 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
416 return -FDT_ERR_BADOFFSET;
417 else if (offset == -FDT_ERR_BADOFFSET)
418 return -FDT_ERR_BADSTRUCTURE;
420 return offset; /* error from fdt_next_node() */
423 int fdt_node_depth(const void *fdt, int nodeoffset)
428 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
430 return (err < 0) ? err : -FDT_ERR_INTERNAL;
434 int fdt_parent_offset(const void *fdt, int nodeoffset)
436 int nodedepth = fdt_node_depth(fdt, nodeoffset);
440 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
441 nodedepth - 1, NULL);
444 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
445 const char *propname,
446 const void *propval, int proplen)
452 FDT_CHECK_HEADER(fdt);
454 /* FIXME: The algorithm here is pretty horrible: we scan each
455 * property of a node in fdt_getprop(), then if that didn't
456 * find what we want, we scan over them again making our way
457 * to the next node. Still it's the easiest to implement
458 * approach; performance can come later. */
459 for (offset = fdt_next_node(fdt, startoffset, NULL);
461 offset = fdt_next_node(fdt, offset, NULL)) {
462 val = fdt_getprop(fdt, offset, propname, &len);
463 if (val && (len == proplen)
464 && (memcmp(val, propval, len) == 0))
468 return offset; /* error from fdt_next_node() */
471 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
475 if ((phandle == 0) || (phandle == -1))
476 return -FDT_ERR_BADPHANDLE;
478 FDT_CHECK_HEADER(fdt);
480 /* FIXME: The algorithm here is pretty horrible: we
481 * potentially scan each property of a node in
482 * fdt_get_phandle(), then if that didn't find what
483 * we want, we scan over them again making our way to the next
484 * node. Still it's the easiest to implement approach;
485 * performance can come later. */
486 for (offset = fdt_next_node(fdt, -1, NULL);
488 offset = fdt_next_node(fdt, offset, NULL)) {
489 if (fdt_get_phandle(fdt, offset) == phandle)
493 return offset; /* error from fdt_next_node() */
496 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
498 int len = strlen(str);
501 while (listlen >= len) {
502 if (memcmp(str, strlist, len+1) == 0)
504 p = memchr(strlist, '\0', listlen);
506 return 0; /* malformed strlist.. */
507 listlen -= (p-strlist) + 1;
513 int fdt_count_strings(const void *fdt, int node, const char *property)
515 int length, i, count = 0;
518 list = fdt_getprop(fdt, node, property, &length);
522 for (i = 0; i < length; i++) {
523 int len = strlen(list);
533 int fdt_find_string(const void *fdt, int node, const char *property,
536 const char *list, *end;
539 list = fdt_getprop(fdt, node, property, &len);
544 len = strlen(string);
547 int l = strlen(list);
549 if (l == len && memcmp(list, string, len) == 0)
556 return -FDT_ERR_NOTFOUND;
559 int fdt_get_string_index(const void *fdt, int node, const char *property,
560 int index, const char **output)
565 list = fdt_getprop(fdt, node, property, &length);
567 for (i = 0; i < length; i++) {
568 int len = strlen(list);
580 return FDT_ERR_NOTFOUND;
583 int fdt_get_string(const void *fdt, int node, const char *property,
586 return fdt_get_string_index(fdt, node, property, 0, output);
589 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
590 const char *compatible)
595 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
598 if (fdt_stringlist_contains(prop, len, compatible))
604 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
605 const char *compatible)
609 FDT_CHECK_HEADER(fdt);
611 /* FIXME: The algorithm here is pretty horrible: we scan each
612 * property of a node in fdt_node_check_compatible(), then if
613 * that didn't find what we want, we scan over them again
614 * making our way to the next node. Still it's the easiest to
615 * implement approach; performance can come later. */
616 for (offset = fdt_next_node(fdt, startoffset, NULL);
618 offset = fdt_next_node(fdt, offset, NULL)) {
619 err = fdt_node_check_compatible(fdt, offset, compatible);
620 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
626 return offset; /* error from fdt_next_node() */