blob: d65656aaa8b179f775483a857450227694850563 [file] [log] [blame]
Simon Glassa5e1a582019-10-27 09:47:42 -06001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
Tom Rinibd391732017-09-23 12:52:44 -04002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
Tom Rinibd391732017-09-23 12:52:44 -04005 */
6#include "libfdt_env.h"
7
8#include <fdt.h>
9#include <libfdt.h>
10
11#include "libfdt_internal.h"
12
Rob Herringcd5f70d2018-05-19 14:13:53 +020013static int fdt_nodename_eq_(const void *fdt, int offset,
Tom Rinibd391732017-09-23 12:52:44 -040014 const char *s, int len)
15{
Rob Herringcd5f70d2018-05-19 14:13:53 +020016 int olen;
17 const char *p = fdt_get_name(fdt, offset, &olen);
Tom Rinibd391732017-09-23 12:52:44 -040018
Simon Glassa5e1a582019-10-27 09:47:42 -060019 if (!p || (fdt_chk_extra() && olen < len))
Tom Rinibd391732017-09-23 12:52:44 -040020 /* short match */
21 return 0;
22
23 if (memcmp(p, s, len) != 0)
24 return 0;
25
26 if (p[len] == '\0')
27 return 1;
28 else if (!memchr(s, '@', len) && (p[len] == '@'))
29 return 1;
30 else
31 return 0;
32}
33
Simon Glassa5e1a582019-10-27 09:47:42 -060034const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
Tom Rinibd391732017-09-23 12:52:44 -040035{
Simon Glassa5e1a582019-10-27 09:47:42 -060036 int32_t totalsize;
37 uint32_t absoffset;
38 size_t len;
39 int err;
40 const char *s, *n;
Tom Rinibd391732017-09-23 12:52:44 -040041
Simon Glassa5e1a582019-10-27 09:47:42 -060042 if (!fdt_chk_extra()) {
43 s = (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
Tom Rinibd391732017-09-23 12:52:44 -040044
Simon Glassa5e1a582019-10-27 09:47:42 -060045 if (lenp)
46 *lenp = strlen(s);
47 return s;
48 }
49 totalsize = fdt_ro_probe_(fdt);
50 err = totalsize;
51 if (totalsize < 0)
52 goto fail;
Tom Rinibd391732017-09-23 12:52:44 -040053
Simon Glassa5e1a582019-10-27 09:47:42 -060054 err = -FDT_ERR_BADOFFSET;
55 absoffset = stroffset + fdt_off_dt_strings(fdt);
Andre Przywaradaa7e752020-10-16 15:42:50 +010056 if (absoffset >= (unsigned)totalsize)
Simon Glassa5e1a582019-10-27 09:47:42 -060057 goto fail;
58 len = totalsize - absoffset;
Tom Rinibd391732017-09-23 12:52:44 -040059
Simon Glassa5e1a582019-10-27 09:47:42 -060060 if (fdt_magic(fdt) == FDT_MAGIC) {
61 if (stroffset < 0)
62 goto fail;
63 if (!fdt_chk_version() || fdt_version(fdt) >= 17) {
Andre Przywaradaa7e752020-10-16 15:42:50 +010064 if ((unsigned)stroffset >= fdt_size_dt_strings(fdt))
Simon Glassa5e1a582019-10-27 09:47:42 -060065 goto fail;
66 if ((fdt_size_dt_strings(fdt) - stroffset) < len)
67 len = fdt_size_dt_strings(fdt) - stroffset;
68 }
69 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
Andre Przywaradaa7e752020-10-16 15:42:50 +010070 unsigned int sw_stroffset = -stroffset;
71
72 if ((stroffset >= 0) ||
73 (sw_stroffset > fdt_size_dt_strings(fdt)))
Simon Glassa5e1a582019-10-27 09:47:42 -060074 goto fail;
Andre Przywaradaa7e752020-10-16 15:42:50 +010075 if ((sw_stroffset) < len)
76 len = sw_stroffset;
Simon Glassa5e1a582019-10-27 09:47:42 -060077 } else {
78 err = -FDT_ERR_INTERNAL;
79 goto fail;
80 }
Tom Rinibd391732017-09-23 12:52:44 -040081
Simon Glassa5e1a582019-10-27 09:47:42 -060082 s = (const char *)fdt + absoffset;
83 n = memchr(s, '\0', len);
84 if (!n) {
85 /* missing terminating NULL */
86 err = -FDT_ERR_TRUNCATED;
87 goto fail;
88 }
Tom Rinibd391732017-09-23 12:52:44 -040089
Simon Glassa5e1a582019-10-27 09:47:42 -060090 if (lenp)
91 *lenp = n - s;
92 return s;
Tom Rinibd391732017-09-23 12:52:44 -040093
Simon Glassa5e1a582019-10-27 09:47:42 -060094fail:
95 if (lenp)
96 *lenp = err;
97 return NULL;
98}
Tom Rinibd391732017-09-23 12:52:44 -040099
Simon Glassa5e1a582019-10-27 09:47:42 -0600100const char *fdt_string(const void *fdt, int stroffset)
101{
102 return fdt_get_string(fdt, stroffset, NULL);
103}
Tom Rinibd391732017-09-23 12:52:44 -0400104
Simon Glassa5e1a582019-10-27 09:47:42 -0600105static int fdt_string_eq_(const void *fdt, int stroffset,
106 const char *s, int len)
107{
108 int slen;
109 const char *p = fdt_get_string(fdt, stroffset, &slen);
110
111 return p && (slen == len) && (memcmp(p, s, len) == 0);
Tom Rinibd391732017-09-23 12:52:44 -0400112}
113
Simon Glassa5e1a582019-10-27 09:47:42 -0600114int fdt_find_max_phandle(const void *fdt, uint32_t *phandle)
Thierry Reding0431be02019-03-21 19:09:58 +0100115{
116 uint32_t max = 0;
117 int offset = -1;
118
119 while (true) {
120 uint32_t value;
121
122 offset = fdt_next_node(fdt, offset, NULL);
123 if (offset < 0) {
124 if (offset == -FDT_ERR_NOTFOUND)
125 break;
126
127 return offset;
128 }
129
130 value = fdt_get_phandle(fdt, offset);
131
132 if (value > max)
133 max = value;
134 }
135
Simon Glassa5e1a582019-10-27 09:47:42 -0600136 if (phandle)
137 *phandle = max;
138
139 return 0;
140}
141
142int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
143{
144 uint32_t max;
145 int err;
146
147 err = fdt_find_max_phandle(fdt, &max);
148 if (err < 0)
149 return err;
150
Thierry Reding0431be02019-03-21 19:09:58 +0100151 if (max == FDT_MAX_PHANDLE)
152 return -FDT_ERR_NOPHANDLES;
153
154 if (phandle)
155 *phandle = max + 1;
156
157 return 0;
158}
159
Simon Glassa5e1a582019-10-27 09:47:42 -0600160static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
161{
Andre Przywaradaa7e752020-10-16 15:42:50 +0100162 unsigned int offset = n * sizeof(struct fdt_reserve_entry);
163 unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
Simon Glassa5e1a582019-10-27 09:47:42 -0600164
165 if (fdt_chk_extra()) {
166 if (absoffset < fdt_off_mem_rsvmap(fdt))
167 return NULL;
168 if (absoffset > fdt_totalsize(fdt) -
169 sizeof(struct fdt_reserve_entry))
170 return NULL;
171 }
172 return fdt_mem_rsv_(fdt, n);
173}
174
Tom Rinibd391732017-09-23 12:52:44 -0400175int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
176{
Simon Glassa5e1a582019-10-27 09:47:42 -0600177 const struct fdt_reserve_entry *re;
178
179 FDT_RO_PROBE(fdt);
180 re = fdt_mem_rsv(fdt, n);
181 if (fdt_chk_extra() && !re)
182 return -FDT_ERR_BADOFFSET;
183
Tom Rinibea42b72020-01-27 12:10:31 -0500184 *address = fdt64_to_cpu(re->address);
185 *size = fdt64_to_cpu(re->size);
Tom Rinibd391732017-09-23 12:52:44 -0400186 return 0;
187}
188
189int fdt_num_mem_rsv(const void *fdt)
190{
Simon Glassa5e1a582019-10-27 09:47:42 -0600191 int i;
192 const struct fdt_reserve_entry *re;
Tom Rinibd391732017-09-23 12:52:44 -0400193
Simon Glassa5e1a582019-10-27 09:47:42 -0600194 for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
Tom Rinibea42b72020-01-27 12:10:31 -0500195 if (fdt64_to_cpu(re->size) == 0)
Simon Glassa5e1a582019-10-27 09:47:42 -0600196 return i;
197 }
198 return -FDT_ERR_TRUNCATED;
Tom Rinibd391732017-09-23 12:52:44 -0400199}
200
Rob Herringcd5f70d2018-05-19 14:13:53 +0200201static int nextprop_(const void *fdt, int offset)
Tom Rinibd391732017-09-23 12:52:44 -0400202{
203 uint32_t tag;
204 int nextoffset;
205
206 do {
207 tag = fdt_next_tag(fdt, offset, &nextoffset);
208
209 switch (tag) {
210 case FDT_END:
211 if (nextoffset >= 0)
212 return -FDT_ERR_BADSTRUCTURE;
213 else
214 return nextoffset;
215
216 case FDT_PROP:
217 return offset;
218 }
219 offset = nextoffset;
220 } while (tag == FDT_NOP);
221
222 return -FDT_ERR_NOTFOUND;
223}
224
225int fdt_subnode_offset_namelen(const void *fdt, int offset,
226 const char *name, int namelen)
227{
228 int depth;
229
Simon Glassa5e1a582019-10-27 09:47:42 -0600230 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400231
232 for (depth = 0;
233 (offset >= 0) && (depth >= 0);
234 offset = fdt_next_node(fdt, offset, &depth))
235 if ((depth == 1)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200236 && fdt_nodename_eq_(fdt, offset, name, namelen))
Tom Rinibd391732017-09-23 12:52:44 -0400237 return offset;
238
239 if (depth < 0)
240 return -FDT_ERR_NOTFOUND;
241 return offset; /* error */
242}
243
244int fdt_subnode_offset(const void *fdt, int parentoffset,
245 const char *name)
246{
247 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
248}
249
250int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
251{
252 const char *end = path + namelen;
253 const char *p = path;
254 int offset = 0;
255
Simon Glassa5e1a582019-10-27 09:47:42 -0600256 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400257
258 /* see if we have an alias */
259 if (*path != '/') {
260 const char *q = memchr(path, '/', end - p);
261
262 if (!q)
263 q = end;
264
265 p = fdt_get_alias_namelen(fdt, p, q - p);
266 if (!p)
267 return -FDT_ERR_BADPATH;
268 offset = fdt_path_offset(fdt, p);
269
270 p = q;
271 }
272
273 while (p < end) {
274 const char *q;
275
276 while (*p == '/') {
277 p++;
278 if (p == end)
279 return offset;
280 }
281 q = memchr(p, '/', end - p);
282 if (! q)
283 q = end;
284
285 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
286 if (offset < 0)
287 return offset;
288
289 p = q;
290 }
291
292 return offset;
293}
294
295int fdt_path_offset(const void *fdt, const char *path)
296{
297 return fdt_path_offset_namelen(fdt, path, strlen(path));
298}
299
300const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
301{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200302 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
303 const char *nameptr;
Tom Rinibd391732017-09-23 12:52:44 -0400304 int err;
305
Simon Glassa5e1a582019-10-27 09:47:42 -0600306 if (fdt_chk_extra() &&
307 (((err = fdt_ro_probe_(fdt)) < 0)
308 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)))
309 goto fail;
Tom Rinibd391732017-09-23 12:52:44 -0400310
Rob Herringcd5f70d2018-05-19 14:13:53 +0200311 nameptr = nh->name;
312
Simon Glassa5e1a582019-10-27 09:47:42 -0600313 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200314 /*
315 * For old FDT versions, match the naming conventions of V16:
316 * give only the leaf name (after all /). The actual tree
317 * contents are loosely checked.
318 */
319 const char *leaf;
320 leaf = strrchr(nameptr, '/');
321 if (leaf == NULL) {
322 err = -FDT_ERR_BADSTRUCTURE;
323 goto fail;
324 }
325 nameptr = leaf+1;
326 }
327
Tom Rinibd391732017-09-23 12:52:44 -0400328 if (len)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200329 *len = strlen(nameptr);
Tom Rinibd391732017-09-23 12:52:44 -0400330
Rob Herringcd5f70d2018-05-19 14:13:53 +0200331 return nameptr;
Tom Rinibd391732017-09-23 12:52:44 -0400332
333 fail:
334 if (len)
335 *len = err;
336 return NULL;
337}
338
339int fdt_first_property_offset(const void *fdt, int nodeoffset)
340{
341 int offset;
342
Rob Herringcd5f70d2018-05-19 14:13:53 +0200343 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
Tom Rinibd391732017-09-23 12:52:44 -0400344 return offset;
345
Rob Herringcd5f70d2018-05-19 14:13:53 +0200346 return nextprop_(fdt, offset);
Tom Rinibd391732017-09-23 12:52:44 -0400347}
348
349int fdt_next_property_offset(const void *fdt, int offset)
350{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200351 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
Tom Rinibd391732017-09-23 12:52:44 -0400352 return offset;
353
Rob Herringcd5f70d2018-05-19 14:13:53 +0200354 return nextprop_(fdt, offset);
Tom Rinibd391732017-09-23 12:52:44 -0400355}
356
Rob Herringcd5f70d2018-05-19 14:13:53 +0200357static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
358 int offset,
359 int *lenp)
Tom Rinibd391732017-09-23 12:52:44 -0400360{
361 int err;
362 const struct fdt_property *prop;
363
Simon Glassa5e1a582019-10-27 09:47:42 -0600364 if (fdt_chk_basic() && (err = fdt_check_prop_offset_(fdt, offset)) < 0) {
Tom Rinibd391732017-09-23 12:52:44 -0400365 if (lenp)
366 *lenp = err;
367 return NULL;
368 }
369
Rob Herringcd5f70d2018-05-19 14:13:53 +0200370 prop = fdt_offset_ptr_(fdt, offset);
Tom Rinibd391732017-09-23 12:52:44 -0400371
372 if (lenp)
Tom Rinibea42b72020-01-27 12:10:31 -0500373 *lenp = fdt32_to_cpu(prop->len);
Tom Rinibd391732017-09-23 12:52:44 -0400374
375 return prop;
376}
377
Rob Herringcd5f70d2018-05-19 14:13:53 +0200378const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
379 int offset,
380 int *lenp)
381{
382 /* Prior to version 16, properties may need realignment
383 * and this API does not work. fdt_getprop_*() will, however. */
384
Simon Glassa5e1a582019-10-27 09:47:42 -0600385 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200386 if (lenp)
387 *lenp = -FDT_ERR_BADVERSION;
388 return NULL;
389 }
390
391 return fdt_get_property_by_offset_(fdt, offset, lenp);
392}
393
394static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
395 int offset,
396 const char *name,
397 int namelen,
398 int *lenp,
399 int *poffset)
Tom Rinibd391732017-09-23 12:52:44 -0400400{
401 for (offset = fdt_first_property_offset(fdt, offset);
402 (offset >= 0);
403 (offset = fdt_next_property_offset(fdt, offset))) {
404 const struct fdt_property *prop;
405
Simon Glassa5e1a582019-10-27 09:47:42 -0600406 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
407 if (fdt_chk_extra() && !prop) {
Tom Rinibd391732017-09-23 12:52:44 -0400408 offset = -FDT_ERR_INTERNAL;
409 break;
410 }
Tom Rinibea42b72020-01-27 12:10:31 -0500411 if (fdt_string_eq_(fdt, fdt32_to_cpu(prop->nameoff),
Rob Herringcd5f70d2018-05-19 14:13:53 +0200412 name, namelen)) {
413 if (poffset)
414 *poffset = offset;
Tom Rinibd391732017-09-23 12:52:44 -0400415 return prop;
Rob Herringcd5f70d2018-05-19 14:13:53 +0200416 }
Tom Rinibd391732017-09-23 12:52:44 -0400417 }
418
419 if (lenp)
420 *lenp = offset;
421 return NULL;
422}
423
Rob Herringcd5f70d2018-05-19 14:13:53 +0200424const struct fdt_property *fdt_get_property_namelen(const void *fdt,
425 int offset,
426 const char *name,
427 int namelen, int *lenp)
428{
429 /* Prior to version 16, properties may need realignment
430 * and this API does not work. fdt_getprop_*() will, however. */
Simon Glassa5e1a582019-10-27 09:47:42 -0600431 if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200432 if (lenp)
433 *lenp = -FDT_ERR_BADVERSION;
434 return NULL;
435 }
436
437 return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
438 NULL);
439}
440
Tom Rinibd391732017-09-23 12:52:44 -0400441const struct fdt_property *fdt_get_property(const void *fdt,
442 int nodeoffset,
443 const char *name, int *lenp)
444{
445 return fdt_get_property_namelen(fdt, nodeoffset, name,
446 strlen(name), lenp);
447}
448
449const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
450 const char *name, int namelen, int *lenp)
451{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200452 int poffset;
Tom Rinibd391732017-09-23 12:52:44 -0400453 const struct fdt_property *prop;
454
Rob Herringcd5f70d2018-05-19 14:13:53 +0200455 prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
456 &poffset);
Tom Rini0eb096d2017-09-23 17:31:59 -0400457 if (!prop)
Tom Rinibd391732017-09-23 12:52:44 -0400458 return NULL;
459
Rob Herringcd5f70d2018-05-19 14:13:53 +0200460 /* Handle realignment */
Simon Glassa5e1a582019-10-27 09:47:42 -0600461 if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
Tom Rinibea42b72020-01-27 12:10:31 -0500462 (poffset + sizeof(*prop)) % 8 && fdt32_to_cpu(prop->len) >= 8)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200463 return prop->data + 4;
Tom Rinibd391732017-09-23 12:52:44 -0400464 return prop->data;
465}
466
467const void *fdt_getprop_by_offset(const void *fdt, int offset,
468 const char **namep, int *lenp)
469{
470 const struct fdt_property *prop;
471
Rob Herringcd5f70d2018-05-19 14:13:53 +0200472 prop = fdt_get_property_by_offset_(fdt, offset, lenp);
Tom Rinibd391732017-09-23 12:52:44 -0400473 if (!prop)
474 return NULL;
Simon Glassa5e1a582019-10-27 09:47:42 -0600475 if (namep) {
476 const char *name;
477 int namelen;
478
479 if (fdt_chk_extra()) {
Tom Rinibea42b72020-01-27 12:10:31 -0500480 name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff),
Simon Glassa5e1a582019-10-27 09:47:42 -0600481 &namelen);
482 if (!name) {
483 if (lenp)
484 *lenp = namelen;
485 return NULL;
486 }
487 *namep = name;
488 } else {
Tom Rinibea42b72020-01-27 12:10:31 -0500489 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
Simon Glassa5e1a582019-10-27 09:47:42 -0600490 }
491 }
Rob Herringcd5f70d2018-05-19 14:13:53 +0200492
493 /* Handle realignment */
Simon Glassa5e1a582019-10-27 09:47:42 -0600494 if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
Tom Rinibea42b72020-01-27 12:10:31 -0500495 (offset + sizeof(*prop)) % 8 && fdt32_to_cpu(prop->len) >= 8)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200496 return prop->data + 4;
Tom Rinibd391732017-09-23 12:52:44 -0400497 return prop->data;
498}
499
500const void *fdt_getprop(const void *fdt, int nodeoffset,
501 const char *name, int *lenp)
502{
503 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
504}
505
506uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
507{
508 const fdt32_t *php;
509 int len;
510
511 /* FIXME: This is a bit sub-optimal, since we potentially scan
512 * over all the properties twice. */
513 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
514 if (!php || (len != sizeof(*php))) {
515 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
516 if (!php || (len != sizeof(*php)))
517 return 0;
518 }
519
Tom Rinibea42b72020-01-27 12:10:31 -0500520 return fdt32_to_cpu(*php);
Tom Rinibd391732017-09-23 12:52:44 -0400521}
522
523const char *fdt_get_alias_namelen(const void *fdt,
524 const char *name, int namelen)
525{
526 int aliasoffset;
527
528 aliasoffset = fdt_path_offset(fdt, "/aliases");
529 if (aliasoffset < 0)
530 return NULL;
531
532 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
533}
534
535const char *fdt_get_alias(const void *fdt, const char *name)
536{
537 return fdt_get_alias_namelen(fdt, name, strlen(name));
538}
539
540int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
541{
542 int pdepth = 0, p = 0;
543 int offset, depth, namelen;
544 const char *name;
545
Simon Glassa5e1a582019-10-27 09:47:42 -0600546 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400547
548 if (buflen < 2)
549 return -FDT_ERR_NOSPACE;
550
551 for (offset = 0, depth = 0;
552 (offset >= 0) && (offset <= nodeoffset);
553 offset = fdt_next_node(fdt, offset, &depth)) {
554 while (pdepth > depth) {
555 do {
556 p--;
557 } while (buf[p-1] != '/');
558 pdepth--;
559 }
560
561 if (pdepth >= depth) {
562 name = fdt_get_name(fdt, offset, &namelen);
563 if (!name)
564 return namelen;
565 if ((p + namelen + 1) <= buflen) {
566 memcpy(buf + p, name, namelen);
567 p += namelen;
568 buf[p++] = '/';
569 pdepth++;
570 }
571 }
572
573 if (offset == nodeoffset) {
574 if (pdepth < (depth + 1))
575 return -FDT_ERR_NOSPACE;
576
577 if (p > 1) /* special case so that root path is "/", not "" */
578 p--;
579 buf[p] = '\0';
580 return 0;
581 }
582 }
583
584 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
585 return -FDT_ERR_BADOFFSET;
586 else if (offset == -FDT_ERR_BADOFFSET)
587 return -FDT_ERR_BADSTRUCTURE;
588
589 return offset; /* error from fdt_next_node() */
590}
591
592int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
593 int supernodedepth, int *nodedepth)
594{
595 int offset, depth;
596 int supernodeoffset = -FDT_ERR_INTERNAL;
597
Simon Glassa5e1a582019-10-27 09:47:42 -0600598 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400599
600 if (supernodedepth < 0)
601 return -FDT_ERR_NOTFOUND;
602
603 for (offset = 0, depth = 0;
604 (offset >= 0) && (offset <= nodeoffset);
605 offset = fdt_next_node(fdt, offset, &depth)) {
606 if (depth == supernodedepth)
607 supernodeoffset = offset;
608
609 if (offset == nodeoffset) {
610 if (nodedepth)
611 *nodedepth = depth;
612
613 if (supernodedepth > depth)
614 return -FDT_ERR_NOTFOUND;
615 else
616 return supernodeoffset;
617 }
618 }
619
Simon Glassa5e1a582019-10-27 09:47:42 -0600620 if (fdt_chk_extra()) {
621 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
622 return -FDT_ERR_BADOFFSET;
623 else if (offset == -FDT_ERR_BADOFFSET)
624 return -FDT_ERR_BADSTRUCTURE;
625 }
Tom Rinibd391732017-09-23 12:52:44 -0400626
627 return offset; /* error from fdt_next_node() */
628}
629
630int fdt_node_depth(const void *fdt, int nodeoffset)
631{
632 int nodedepth;
633 int err;
634
635 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
636 if (err)
Simon Glassa5e1a582019-10-27 09:47:42 -0600637 return (!fdt_chk_extra() || err < 0) ? err : -FDT_ERR_INTERNAL;
Tom Rinibd391732017-09-23 12:52:44 -0400638 return nodedepth;
639}
640
641int fdt_parent_offset(const void *fdt, int nodeoffset)
642{
643 int nodedepth = fdt_node_depth(fdt, nodeoffset);
644
645 if (nodedepth < 0)
646 return nodedepth;
647 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
648 nodedepth - 1, NULL);
649}
650
651int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
652 const char *propname,
653 const void *propval, int proplen)
654{
655 int offset;
656 const void *val;
657 int len;
658
Simon Glassa5e1a582019-10-27 09:47:42 -0600659 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400660
661 /* FIXME: The algorithm here is pretty horrible: we scan each
662 * property of a node in fdt_getprop(), then if that didn't
663 * find what we want, we scan over them again making our way
664 * to the next node. Still it's the easiest to implement
665 * approach; performance can come later. */
666 for (offset = fdt_next_node(fdt, startoffset, NULL);
667 offset >= 0;
668 offset = fdt_next_node(fdt, offset, NULL)) {
669 val = fdt_getprop(fdt, offset, propname, &len);
670 if (val && (len == proplen)
671 && (memcmp(val, propval, len) == 0))
672 return offset;
673 }
674
675 return offset; /* error from fdt_next_node() */
676}
677
678int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
679{
680 int offset;
681
Andre Przywaradaa7e752020-10-16 15:42:50 +0100682 if ((phandle == 0) || (phandle == ~0U))
Tom Rinibd391732017-09-23 12:52:44 -0400683 return -FDT_ERR_BADPHANDLE;
684
Simon Glassa5e1a582019-10-27 09:47:42 -0600685 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400686
687 /* FIXME: The algorithm here is pretty horrible: we
688 * potentially scan each property of a node in
689 * fdt_get_phandle(), then if that didn't find what
690 * we want, we scan over them again making our way to the next
691 * node. Still it's the easiest to implement approach;
692 * performance can come later. */
693 for (offset = fdt_next_node(fdt, -1, NULL);
694 offset >= 0;
695 offset = fdt_next_node(fdt, offset, NULL)) {
696 if (fdt_get_phandle(fdt, offset) == phandle)
697 return offset;
698 }
699
700 return offset; /* error from fdt_next_node() */
701}
702
703int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
704{
705 int len = strlen(str);
706 const char *p;
707
708 while (listlen >= len) {
709 if (memcmp(str, strlist, len+1) == 0)
710 return 1;
711 p = memchr(strlist, '\0', listlen);
712 if (!p)
713 return 0; /* malformed strlist.. */
714 listlen -= (p-strlist) + 1;
715 strlist = p + 1;
716 }
717 return 0;
718}
719
720int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
721{
722 const char *list, *end;
723 int length, count = 0;
724
725 list = fdt_getprop(fdt, nodeoffset, property, &length);
726 if (!list)
727 return length;
728
729 end = list + length;
730
731 while (list < end) {
732 length = strnlen(list, end - list) + 1;
733
734 /* Abort if the last string isn't properly NUL-terminated. */
735 if (list + length > end)
736 return -FDT_ERR_BADVALUE;
737
738 list += length;
739 count++;
740 }
741
742 return count;
743}
744
745int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
746 const char *string)
747{
748 int length, len, idx = 0;
749 const char *list, *end;
750
751 list = fdt_getprop(fdt, nodeoffset, property, &length);
752 if (!list)
753 return length;
754
755 len = strlen(string) + 1;
756 end = list + length;
757
758 while (list < end) {
759 length = strnlen(list, end - list) + 1;
760
761 /* Abort if the last string isn't properly NUL-terminated. */
762 if (list + length > end)
763 return -FDT_ERR_BADVALUE;
764
765 if (length == len && memcmp(list, string, length) == 0)
766 return idx;
767
768 list += length;
769 idx++;
770 }
771
772 return -FDT_ERR_NOTFOUND;
773}
774
775const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
776 const char *property, int idx,
777 int *lenp)
778{
779 const char *list, *end;
780 int length;
781
782 list = fdt_getprop(fdt, nodeoffset, property, &length);
783 if (!list) {
784 if (lenp)
785 *lenp = length;
786
787 return NULL;
788 }
789
790 end = list + length;
791
792 while (list < end) {
793 length = strnlen(list, end - list) + 1;
794
795 /* Abort if the last string isn't properly NUL-terminated. */
796 if (list + length > end) {
797 if (lenp)
798 *lenp = -FDT_ERR_BADVALUE;
799
800 return NULL;
801 }
802
803 if (idx == 0) {
804 if (lenp)
805 *lenp = length - 1;
806
807 return list;
808 }
809
810 list += length;
811 idx--;
812 }
813
814 if (lenp)
815 *lenp = -FDT_ERR_NOTFOUND;
816
817 return NULL;
818}
819
820int fdt_node_check_compatible(const void *fdt, int nodeoffset,
821 const char *compatible)
822{
823 const void *prop;
824 int len;
825
826 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
827 if (!prop)
828 return len;
829
830 return !fdt_stringlist_contains(prop, len, compatible);
831}
832
833int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
834 const char *compatible)
835{
836 int offset, err;
837
Simon Glassa5e1a582019-10-27 09:47:42 -0600838 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400839
840 /* FIXME: The algorithm here is pretty horrible: we scan each
841 * property of a node in fdt_node_check_compatible(), then if
842 * that didn't find what we want, we scan over them again
843 * making our way to the next node. Still it's the easiest to
844 * implement approach; performance can come later. */
845 for (offset = fdt_next_node(fdt, startoffset, NULL);
846 offset >= 0;
847 offset = fdt_next_node(fdt, offset, NULL)) {
848 err = fdt_node_check_compatible(fdt, offset, compatible);
849 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
850 return err;
851 else if (err == 0)
852 return offset;
853 }
854
855 return offset; /* error from fdt_next_node() */
856}
Simon Glassa5e1a582019-10-27 09:47:42 -0600857
858#if !defined(FDT_ASSUME_MASK) || FDT_ASSUME_MASK != 0xff
859int fdt_check_full(const void *fdt, size_t bufsize)
860{
861 int err;
862 int num_memrsv;
863 int offset, nextoffset = 0;
864 uint32_t tag;
865 unsigned depth = 0;
866 const void *prop;
867 const char *propname;
Simon Glass19d2c022021-02-15 17:08:11 -0700868 bool expect_end = false;
Simon Glassa5e1a582019-10-27 09:47:42 -0600869
870 if (bufsize < FDT_V1_SIZE)
871 return -FDT_ERR_TRUNCATED;
872 err = fdt_check_header(fdt);
873 if (err != 0)
874 return err;
875 if (bufsize < fdt_totalsize(fdt))
876 return -FDT_ERR_TRUNCATED;
877
878 num_memrsv = fdt_num_mem_rsv(fdt);
879 if (num_memrsv < 0)
880 return num_memrsv;
881
882 while (1) {
883 offset = nextoffset;
884 tag = fdt_next_tag(fdt, offset, &nextoffset);
885
886 if (nextoffset < 0)
887 return nextoffset;
888
Simon Glass19d2c022021-02-15 17:08:11 -0700889 /* If we see two root nodes, something is wrong */
890 if (expect_end && tag != FDT_END)
891 return -FDT_ERR_BADLAYOUT;
892
Simon Glassa5e1a582019-10-27 09:47:42 -0600893 switch (tag) {
894 case FDT_NOP:
895 break;
896
897 case FDT_END:
898 if (depth != 0)
899 return -FDT_ERR_BADSTRUCTURE;
900 return 0;
901
902 case FDT_BEGIN_NODE:
903 depth++;
904 if (depth > INT_MAX)
905 return -FDT_ERR_BADSTRUCTURE;
Simon Glass19d2c022021-02-15 17:08:11 -0700906
907 /* The root node must have an empty name */
908 if (depth == 1) {
909 const char *name;
910 int len;
911
912 name = fdt_get_name(fdt, offset, &len);
913 if (*name || len)
914 return -FDT_ERR_BADLAYOUT;
915 }
Simon Glassa5e1a582019-10-27 09:47:42 -0600916 break;
917
918 case FDT_END_NODE:
919 if (depth == 0)
920 return -FDT_ERR_BADSTRUCTURE;
921 depth--;
Simon Glass19d2c022021-02-15 17:08:11 -0700922 if (depth == 0)
923 expect_end = true;
Simon Glassa5e1a582019-10-27 09:47:42 -0600924 break;
925
926 case FDT_PROP:
927 prop = fdt_getprop_by_offset(fdt, offset, &propname,
928 &err);
929 if (!prop)
930 return err;
931 break;
932
933 default:
934 return -FDT_ERR_INTERNAL;
935 }
936 }
937}
Philippe Reynesd0108112022-02-09 18:01:24 +0100938#else
939int fdt_check_full(const void __always_unused *fdt,
940 size_t __always_unused bufsize)
941{
942 return 0;
943}
944#endif /* #if !defined(FDT_ASSUME_MASK) || FDT_ASSUME_MASK != 0xff */