blob: 693de9aa5ad8032375b22749a25bc236dc15d950 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04005 */
Masahiro Yamada75f82d02018-03-05 01:20:11 +09006#include <linux/libfdt_env.h>
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04007
Bartlomiej Sieka61250862008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04009#include <fdt.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Bartlomiej Sieka61250862008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040014
15#include "libfdt_internal.h"
16
David Gibson9457d9d2008-07-09 14:10:24 +100017static int _fdt_nodename_eq(const void *fdt, int offset,
18 const char *s, int len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040019{
David Gibsond9d793c2008-02-12 11:58:31 +110020 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040021
Robert P. J. Day55019c52016-05-23 05:40:55 -040022 if (!p)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040023 /* short match */
24 return 0;
25
26 if (memcmp(p, s, len) != 0)
27 return 0;
28
Kumar Galac8ab7052007-10-24 11:04:22 -050029 if (p[len] == '\0')
30 return 1;
31 else if (!memchr(s, '@', len) && (p[len] == '@'))
32 return 1;
33 else
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040034 return 0;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040035}
36
Kumar Galac8ab7052007-10-24 11:04:22 -050037const char *fdt_string(const void *fdt, int stroffset)
Gerald Van Baren18c75c62007-05-17 23:54:36 -040038{
David Gibson870fa472008-07-07 10:14:15 +100039 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
Kumar Galac8ab7052007-10-24 11:04:22 -050040}
Gerald Van Baren18c75c62007-05-17 23:54:36 -040041
David Gibsonc5b130e2008-08-06 14:50:49 +100042static int _fdt_string_eq(const void *fdt, int stroffset,
43 const char *s, int len)
44{
45 const char *p = fdt_string(fdt, stroffset);
46
Jon Nalleyfc473002014-02-26 11:32:21 -050047 return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0);
David Gibsonc5b130e2008-08-06 14:50:49 +100048}
49
Maxime Ripard2125ca52016-07-05 10:26:39 +020050uint32_t fdt_get_max_phandle(const void *fdt)
51{
52 uint32_t max_phandle = 0;
53 int offset;
54
55 for (offset = fdt_next_node(fdt, -1, NULL);;
56 offset = fdt_next_node(fdt, offset, NULL)) {
57 uint32_t phandle;
58
59 if (offset == -FDT_ERR_NOTFOUND)
60 return max_phandle;
61
62 if (offset < 0)
Simon Glassdfdcd6f2016-10-02 17:59:27 -060063 return (uint32_t)-1;
Maxime Ripard2125ca52016-07-05 10:26:39 +020064
65 phandle = fdt_get_phandle(fdt, offset);
66 if (phandle == (uint32_t)-1)
Simon Glassdfdcd6f2016-10-02 17:59:27 -060067 continue;
Maxime Ripard2125ca52016-07-05 10:26:39 +020068
69 if (phandle > max_phandle)
70 max_phandle = phandle;
71 }
72
73 return 0;
74}
75
Thierry Reding0431be02019-03-21 19:09:58 +010076int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
77{
78 uint32_t max = 0;
79 int offset = -1;
80
81 while (true) {
82 uint32_t value;
83
84 offset = fdt_next_node(fdt, offset, NULL);
85 if (offset < 0) {
86 if (offset == -FDT_ERR_NOTFOUND)
87 break;
88
89 return offset;
90 }
91
92 value = fdt_get_phandle(fdt, offset);
93
94 if (value > max)
95 max = value;
96 }
97
98 if (max == FDT_MAX_PHANDLE)
99 return -FDT_ERR_NOPHANDLES;
100
101 if (phandle)
102 *phandle = max + 1;
103
104 return 0;
105}
106
Kumar Galac8ab7052007-10-24 11:04:22 -0500107int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
108{
David Gibson9457d9d2008-07-09 14:10:24 +1000109 FDT_CHECK_HEADER(fdt);
Rob Herringcd5f70d2018-05-19 14:13:53 +0200110 *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address);
111 *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size);
Gerald Van Baren18c75c62007-05-17 23:54:36 -0400112 return 0;
113}
114
Kumar Galac8ab7052007-10-24 11:04:22 -0500115int fdt_num_mem_rsv(const void *fdt)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400116{
Kumar Galac8ab7052007-10-24 11:04:22 -0500117 int i = 0;
118
Rob Herringcd5f70d2018-05-19 14:13:53 +0200119 while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0)
Kumar Galac8ab7052007-10-24 11:04:22 -0500120 i++;
121 return i;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400122}
123
David Gibson7b64da12010-03-09 17:39:14 +1100124static int _nextprop(const void *fdt, int offset)
125{
126 uint32_t tag;
127 int nextoffset;
128
129 do {
130 tag = fdt_next_tag(fdt, offset, &nextoffset);
131
132 switch (tag) {
133 case FDT_END:
134 if (nextoffset >= 0)
135 return -FDT_ERR_BADSTRUCTURE;
136 else
137 return nextoffset;
138
139 case FDT_PROP:
140 return offset;
141 }
142 offset = nextoffset;
143 } while (tag == FDT_NOP);
144
145 return -FDT_ERR_NOTFOUND;
146}
147
David Gibsond9d793c2008-02-12 11:58:31 +1100148int fdt_subnode_offset_namelen(const void *fdt, int offset,
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400149 const char *name, int namelen)
150{
David Gibson954914a2009-02-06 14:01:56 +1100151 int depth;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400152
David Gibson9457d9d2008-07-09 14:10:24 +1000153 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400154
David Gibson954914a2009-02-06 14:01:56 +1100155 for (depth = 0;
156 (offset >= 0) && (depth >= 0);
157 offset = fdt_next_node(fdt, offset, &depth))
158 if ((depth == 1)
159 && _fdt_nodename_eq(fdt, offset, name, namelen))
David Gibsond9d793c2008-02-12 11:58:31 +1100160 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400161
David Gibson954914a2009-02-06 14:01:56 +1100162 if (depth < 0)
David Gibson1c0320e2008-10-29 23:27:45 -0500163 return -FDT_ERR_NOTFOUND;
David Gibson954914a2009-02-06 14:01:56 +1100164 return offset; /* error */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400165}
166
167int fdt_subnode_offset(const void *fdt, int parentoffset,
168 const char *name)
169{
170 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
171}
172
Hans de Goedeab89fee2015-04-20 11:13:37 +0200173/*
Maxime Riparda4a4fb52016-07-05 10:26:40 +0200174 * Find the next of path separator, note we need to search for both '/' and ':'
Robert P. J. Day55019c52016-05-23 05:40:55 -0400175 * and then take the first one so that we do the right thing for e.g.
Hans de Goedeab89fee2015-04-20 11:13:37 +0200176 * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
177 * first searching for either ':' or '/' does not work.
178 */
Maxime Ripard87390e82016-07-05 10:26:41 +0200179static const char *fdt_path_next_separator(const char *path, int len)
Hans de Goedeab89fee2015-04-20 11:13:37 +0200180{
Maxime Ripard87390e82016-07-05 10:26:41 +0200181 const void *sep1 = memchr(path, '/', len);
182 const void *sep2 = memchr(path, ':', len);
Hans de Goedeab89fee2015-04-20 11:13:37 +0200183
184 if (sep1 && sep2)
185 return (sep1 < sep2) ? sep1 : sep2;
186 else if (sep1)
187 return sep1;
188 else
189 return sep2;
190}
191
Maxime Ripard87390e82016-07-05 10:26:41 +0200192int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400193{
Maxime Ripard87390e82016-07-05 10:26:41 +0200194 const char *end = path + namelen;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400195 const char *p = path;
196 int offset = 0;
197
David Gibson9457d9d2008-07-09 14:10:24 +1000198 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400199
Kumar Galaeb45e032008-08-14 08:28:19 -0500200 /* see if we have an alias */
201 if (*path != '/') {
Maxime Ripard87390e82016-07-05 10:26:41 +0200202 const char *q = fdt_path_next_separator(path, namelen);
Kumar Galaeb45e032008-08-14 08:28:19 -0500203
Kumar Galaeb45e032008-08-14 08:28:19 -0500204 if (!q)
205 q = end;
206
David Gibsond6656ea2008-08-20 16:55:14 +1000207 p = fdt_get_alias_namelen(fdt, p, q - p);
Kumar Galaeb45e032008-08-14 08:28:19 -0500208 if (!p)
209 return -FDT_ERR_BADPATH;
210 offset = fdt_path_offset(fdt, p);
211
212 p = q;
213 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400214
Maxime Ripard87390e82016-07-05 10:26:41 +0200215 while (*p && (p < end)) {
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400216 const char *q;
217
218 while (*p == '/')
219 p++;
Maxime Ripard87390e82016-07-05 10:26:41 +0200220
Hans de Goedeab89fee2015-04-20 11:13:37 +0200221 if (*p == '\0' || *p == ':')
Kumar Galac8ab7052007-10-24 11:04:22 -0500222 return offset;
Maxime Ripard87390e82016-07-05 10:26:41 +0200223
224 q = fdt_path_next_separator(p, end - p);
Robert P. J. Day55019c52016-05-23 05:40:55 -0400225 if (!q)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400226 q = end;
227
228 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
229 if (offset < 0)
230 return offset;
231
232 p = q;
233 }
234
Gerald Van Baren32480892007-03-31 14:30:53 -0400235 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400236}
237
Simon Glassa2235e42016-10-02 17:59:30 -0600238int fdt_path_offset(const void *fdt, const char *path)
239{
240 return fdt_path_offset_namelen(fdt, path, strlen(path));
241}
242
Kumar Galac8ab7052007-10-24 11:04:22 -0500243const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400244{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200245 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
Kumar Galac8ab7052007-10-24 11:04:22 -0500246 int err;
247
David Gibson148a49b2008-05-20 17:19:11 +1000248 if (((err = fdt_check_header(fdt)) != 0)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200249 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
David Gibson148a49b2008-05-20 17:19:11 +1000250 goto fail;
Kumar Galac8ab7052007-10-24 11:04:22 -0500251
252 if (len)
253 *len = strlen(nh->name);
254
255 return nh->name;
256
257 fail:
258 if (len)
259 *len = err;
260 return NULL;
261}
262
David Gibson7b64da12010-03-09 17:39:14 +1100263int fdt_first_property_offset(const void *fdt, int nodeoffset)
Kumar Galac8ab7052007-10-24 11:04:22 -0500264{
David Gibson7b64da12010-03-09 17:39:14 +1100265 int offset;
266
Rob Herringcd5f70d2018-05-19 14:13:53 +0200267 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
David Gibson7b64da12010-03-09 17:39:14 +1100268 return offset;
269
270 return _nextprop(fdt, offset);
271}
272
273int fdt_next_property_offset(const void *fdt, int offset)
274{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200275 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
David Gibson7b64da12010-03-09 17:39:14 +1100276 return offset;
277
278 return _nextprop(fdt, offset);
279}
280
281const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
282 int offset,
283 int *lenp)
284{
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400285 int err;
David Gibson7b64da12010-03-09 17:39:14 +1100286 const struct fdt_property *prop;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400287
Rob Herringcd5f70d2018-05-19 14:13:53 +0200288 if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
David Gibson7b64da12010-03-09 17:39:14 +1100289 if (lenp)
290 *lenp = err;
291 return NULL;
292 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400293
Rob Herringcd5f70d2018-05-19 14:13:53 +0200294 prop = fdt_offset_ptr_(fdt, offset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400295
David Gibson7b64da12010-03-09 17:39:14 +1100296 if (lenp)
297 *lenp = fdt32_to_cpu(prop->len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400298
David Gibson7b64da12010-03-09 17:39:14 +1100299 return prop;
300}
301
302const struct fdt_property *fdt_get_property_namelen(const void *fdt,
303 int offset,
304 const char *name,
305 int namelen, int *lenp)
306{
307 for (offset = fdt_first_property_offset(fdt, offset);
308 (offset >= 0);
309 (offset = fdt_next_property_offset(fdt, offset))) {
310 const struct fdt_property *prop;
Kumar Galac8ab7052007-10-24 11:04:22 -0500311
David Gibson7b64da12010-03-09 17:39:14 +1100312 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
313 offset = -FDT_ERR_INTERNAL;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400314 break;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400315 }
David Gibson7b64da12010-03-09 17:39:14 +1100316 if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
317 name, namelen))
318 return prop;
319 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400320
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400321 if (lenp)
David Gibson7b64da12010-03-09 17:39:14 +1100322 *lenp = offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400323 return NULL;
324}
325
David Gibsonc5b130e2008-08-06 14:50:49 +1000326const struct fdt_property *fdt_get_property(const void *fdt,
327 int nodeoffset,
328 const char *name, int *lenp)
329{
330 return fdt_get_property_namelen(fdt, nodeoffset, name,
331 strlen(name), lenp);
332}
333
334const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
335 const char *name, int namelen, int *lenp)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400336{
337 const struct fdt_property *prop;
338
David Gibsonc5b130e2008-08-06 14:50:49 +1000339 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
Robert P. J. Day55019c52016-05-23 05:40:55 -0400340 if (!prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400341 return NULL;
342
David Gibson7b64da12010-03-09 17:39:14 +1100343 return prop->data;
344}
345
346const void *fdt_getprop_by_offset(const void *fdt, int offset,
347 const char **namep, int *lenp)
348{
349 const struct fdt_property *prop;
350
351 prop = fdt_get_property_by_offset(fdt, offset, lenp);
352 if (!prop)
353 return NULL;
354 if (namep)
355 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
Kumar Galac8ab7052007-10-24 11:04:22 -0500356 return prop->data;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400357}
358
David Gibsonc5b130e2008-08-06 14:50:49 +1000359const void *fdt_getprop(const void *fdt, int nodeoffset,
360 const char *name, int *lenp)
361{
362 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
363}
364
Kumar Galac8ab7052007-10-24 11:04:22 -0500365uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
366{
Kim Phillips38c38152013-01-16 13:59:50 +0000367 const fdt32_t *php;
Kumar Galac8ab7052007-10-24 11:04:22 -0500368 int len;
369
David Gibson3e31de92009-11-26 15:37:13 +1100370 /* 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)))
376 return 0;
377 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500378
379 return fdt32_to_cpu(*php);
380}
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400381
David Gibsond6656ea2008-08-20 16:55:14 +1000382const char *fdt_get_alias_namelen(const void *fdt,
383 const char *name, int namelen)
384{
385 int aliasoffset;
386
387 aliasoffset = fdt_path_offset(fdt, "/aliases");
388 if (aliasoffset < 0)
389 return NULL;
390
391 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
392}
393
394const char *fdt_get_alias(const void *fdt, const char *name)
395{
396 return fdt_get_alias_namelen(fdt, name, strlen(name));
397}
398
Kumar Galac8ab7052007-10-24 11:04:22 -0500399int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400400{
David Gibsond9d793c2008-02-12 11:58:31 +1100401 int pdepth = 0, p = 0;
402 int offset, depth, namelen;
Kumar Galac8ab7052007-10-24 11:04:22 -0500403 const char *name;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400404
David Gibson9457d9d2008-07-09 14:10:24 +1000405 FDT_CHECK_HEADER(fdt);
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400406
Kumar Galac8ab7052007-10-24 11:04:22 -0500407 if (buflen < 2)
408 return -FDT_ERR_NOSPACE;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400409
David Gibsond9d793c2008-02-12 11:58:31 +1100410 for (offset = 0, depth = 0;
411 (offset >= 0) && (offset <= nodeoffset);
412 offset = fdt_next_node(fdt, offset, &depth)) {
David Gibsond9d793c2008-02-12 11:58:31 +1100413 while (pdepth > depth) {
414 do {
415 p--;
416 } while (buf[p-1] != '/');
417 pdepth--;
418 }
419
David Gibson8d2810d2008-08-29 14:19:13 +1000420 if (pdepth >= depth) {
421 name = fdt_get_name(fdt, offset, &namelen);
422 if (!name)
423 return namelen;
424 if ((p + namelen + 1) <= buflen) {
425 memcpy(buf + p, name, namelen);
426 p += namelen;
427 buf[p++] = '/';
428 pdepth++;
429 }
David Gibsond9d793c2008-02-12 11:58:31 +1100430 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500431
David Gibsond9d793c2008-02-12 11:58:31 +1100432 if (offset == nodeoffset) {
433 if (pdepth < (depth + 1))
434 return -FDT_ERR_NOSPACE;
Kumar Galac8ab7052007-10-24 11:04:22 -0500435
David Gibsond9d793c2008-02-12 11:58:31 +1100436 if (p > 1) /* special case so that root path is "/", not "" */
437 p--;
438 buf[p] = '\0';
David Gibson8d2810d2008-08-29 14:19:13 +1000439 return 0;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400440 }
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400441 }
442
David Gibsond9d793c2008-02-12 11:58:31 +1100443 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
444 return -FDT_ERR_BADOFFSET;
445 else if (offset == -FDT_ERR_BADOFFSET)
446 return -FDT_ERR_BADSTRUCTURE;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400447
David Gibsond9d793c2008-02-12 11:58:31 +1100448 return offset; /* error from fdt_next_node() */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400449}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400450
Kumar Galac8ab7052007-10-24 11:04:22 -0500451int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
452 int supernodedepth, int *nodedepth)
Gerald Van Barenc077a132007-04-14 22:46:41 -0400453{
David Gibsond9d793c2008-02-12 11:58:31 +1100454 int offset, depth;
Kumar Galac8ab7052007-10-24 11:04:22 -0500455 int supernodeoffset = -FDT_ERR_INTERNAL;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400456
David Gibson9457d9d2008-07-09 14:10:24 +1000457 FDT_CHECK_HEADER(fdt);
Gerald Van Barenc077a132007-04-14 22:46:41 -0400458
Kumar Galac8ab7052007-10-24 11:04:22 -0500459 if (supernodedepth < 0)
460 return -FDT_ERR_NOTFOUND;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400461
David Gibsond9d793c2008-02-12 11:58:31 +1100462 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;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400467
David Gibsond9d793c2008-02-12 11:58:31 +1100468 if (offset == nodeoffset) {
469 if (nodedepth)
470 *nodedepth = depth;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400471
David Gibsond9d793c2008-02-12 11:58:31 +1100472 if (supernodedepth > depth)
473 return -FDT_ERR_NOTFOUND;
474 else
475 return supernodeoffset;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400476 }
David Gibsond9d793c2008-02-12 11:58:31 +1100477 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500478
David Gibsond9d793c2008-02-12 11:58:31 +1100479 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
480 return -FDT_ERR_BADOFFSET;
481 else if (offset == -FDT_ERR_BADOFFSET)
482 return -FDT_ERR_BADSTRUCTURE;
Kumar Galac8ab7052007-10-24 11:04:22 -0500483
David Gibsond9d793c2008-02-12 11:58:31 +1100484 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500485}
486
487int fdt_node_depth(const void *fdt, int nodeoffset)
488{
489 int nodedepth;
490 int err;
491
492 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
493 if (err)
494 return (err < 0) ? err : -FDT_ERR_INTERNAL;
495 return nodedepth;
496}
497
498int fdt_parent_offset(const void *fdt, int nodeoffset)
499{
500 int nodedepth = fdt_node_depth(fdt, nodeoffset);
501
502 if (nodedepth < 0)
503 return nodedepth;
504 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
505 nodedepth - 1, NULL);
506}
507
508int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
509 const char *propname,
510 const void *propval, int proplen)
511{
David Gibsond9d793c2008-02-12 11:58:31 +1100512 int offset;
Kumar Galac8ab7052007-10-24 11:04:22 -0500513 const void *val;
514 int len;
515
David Gibson9457d9d2008-07-09 14:10:24 +1000516 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500517
Kumar Galac8ab7052007-10-24 11:04:22 -0500518 /* 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. */
David Gibsond9d793c2008-02-12 11:58:31 +1100523 for (offset = fdt_next_node(fdt, startoffset, NULL);
524 offset >= 0;
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))
529 return offset;
530 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500531
David Gibsond9d793c2008-02-12 11:58:31 +1100532 return offset; /* error from fdt_next_node() */
Gerald Van Barenc077a132007-04-14 22:46:41 -0400533}
534
Kumar Galac8ab7052007-10-24 11:04:22 -0500535int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
Gerald Van Barenc077a132007-04-14 22:46:41 -0400536{
David Gibson3e31de92009-11-26 15:37:13 +1100537 int offset;
538
Kumar Galac8ab7052007-10-24 11:04:22 -0500539 if ((phandle == 0) || (phandle == -1))
540 return -FDT_ERR_BADPHANDLE;
David Gibson3e31de92009-11-26 15:37:13 +1100541
542 FDT_CHECK_HEADER(fdt);
543
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);
551 offset >= 0;
552 offset = fdt_next_node(fdt, offset, NULL)) {
553 if (fdt_get_phandle(fdt, offset) == phandle)
554 return offset;
555 }
556
557 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500558}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400559
Simon Glass73b47562013-01-21 12:59:18 -0800560int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
Kumar Galac8ab7052007-10-24 11:04:22 -0500561{
562 int len = strlen(str);
David Gibsonb4141b82008-07-07 10:10:48 +1000563 const char *p;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400564
Kumar Galac8ab7052007-10-24 11:04:22 -0500565 while (listlen >= len) {
566 if (memcmp(str, strlist, len+1) == 0)
567 return 1;
568 p = memchr(strlist, '\0', listlen);
569 if (!p)
570 return 0; /* malformed strlist.. */
571 listlen -= (p-strlist) + 1;
572 strlist = p + 1;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400573 }
574 return 0;
575}
Kumar Galac8ab7052007-10-24 11:04:22 -0500576
Simon Glassb0ea7402016-10-02 17:59:28 -0600577int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
Thierry Reding92ba09e2014-08-26 17:33:50 +0200578{
Simon Glassb0ea7402016-10-02 17:59:28 -0600579 const char *list, *end;
580 int length, count = 0;
Thierry Reding92ba09e2014-08-26 17:33:50 +0200581
Simon Glassb0ea7402016-10-02 17:59:28 -0600582 list = fdt_getprop(fdt, nodeoffset, property, &length);
Thierry Reding92ba09e2014-08-26 17:33:50 +0200583 if (!list)
Masahiro Yamada0a378262016-10-17 20:22:33 +0900584 return length;
Thierry Reding92ba09e2014-08-26 17:33:50 +0200585
Simon Glassb0ea7402016-10-02 17:59:28 -0600586 end = list + length;
587
588 while (list < end) {
589 length = strnlen(list, end - list) + 1;
Thierry Reding92ba09e2014-08-26 17:33:50 +0200590
Simon Glassb0ea7402016-10-02 17:59:28 -0600591 /* Abort if the last string isn't properly NUL-terminated. */
592 if (list + length > end)
593 return -FDT_ERR_BADVALUE;
594
595 list += length;
Thierry Reding92ba09e2014-08-26 17:33:50 +0200596 count++;
597 }
598
599 return count;
600}
601
Simon Glassb0ea7402016-10-02 17:59:28 -0600602int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
603 const char *string)
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200604{
Simon Glassb0ea7402016-10-02 17:59:28 -0600605 int length, len, idx = 0;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200606 const char *list, *end;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200607
Simon Glassb0ea7402016-10-02 17:59:28 -0600608 list = fdt_getprop(fdt, nodeoffset, property, &length);
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200609 if (!list)
Masahiro Yamada48fd0f12016-10-17 20:22:34 +0900610 return length;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200611
Simon Glassb0ea7402016-10-02 17:59:28 -0600612 len = strlen(string) + 1;
613 end = list + length;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200614
615 while (list < end) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600616 length = strnlen(list, end - list) + 1;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200617
Simon Glassb0ea7402016-10-02 17:59:28 -0600618 /* Abort if the last string isn't properly NUL-terminated. */
619 if (list + length > end)
620 return -FDT_ERR_BADVALUE;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200621
Simon Glassb0ea7402016-10-02 17:59:28 -0600622 if (length == len && memcmp(list, string, length) == 0)
623 return idx;
624
625 list += length;
626 idx++;
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200627 }
628
629 return -FDT_ERR_NOTFOUND;
630}
631
Simon Glassb0ea7402016-10-02 17:59:28 -0600632const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
633 const char *property, int idx,
634 int *lenp)
Thierry Reding5504b802014-08-26 17:33:52 +0200635{
Simon Glassb0ea7402016-10-02 17:59:28 -0600636 const char *list, *end;
637 int length;
638
639 list = fdt_getprop(fdt, nodeoffset, property, &length);
640 if (!list) {
641 if (lenp)
642 *lenp = length;
Thierry Reding5504b802014-08-26 17:33:52 +0200643
Simon Glassb0ea7402016-10-02 17:59:28 -0600644 return NULL;
645 }
Thierry Reding5504b802014-08-26 17:33:52 +0200646
Simon Glassb0ea7402016-10-02 17:59:28 -0600647 end = list + length;
Thierry Reding5504b802014-08-26 17:33:52 +0200648
Simon Glassb0ea7402016-10-02 17:59:28 -0600649 while (list < end) {
650 length = strnlen(list, end - list) + 1;
651
652 /* Abort if the last string isn't properly NUL-terminated. */
653 if (list + length > end) {
654 if (lenp)
655 *lenp = -FDT_ERR_BADVALUE;
656
657 return NULL;
658 }
659
660 if (idx == 0) {
661 if (lenp)
662 *lenp = length - 1;
663
664 return list;
Thierry Reding5504b802014-08-26 17:33:52 +0200665 }
666
Simon Glassb0ea7402016-10-02 17:59:28 -0600667 list += length;
668 idx--;
Thierry Reding5504b802014-08-26 17:33:52 +0200669 }
670
Simon Glassb0ea7402016-10-02 17:59:28 -0600671 if (lenp)
672 *lenp = -FDT_ERR_NOTFOUND;
Thierry Reding5504b802014-08-26 17:33:52 +0200673
Simon Glassb0ea7402016-10-02 17:59:28 -0600674 return NULL;
Thierry Reding5504b802014-08-26 17:33:52 +0200675}
676
Kumar Galac8ab7052007-10-24 11:04:22 -0500677int fdt_node_check_compatible(const void *fdt, int nodeoffset,
678 const char *compatible)
679{
680 const void *prop;
681 int len;
682
683 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
684 if (!prop)
685 return len;
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600686
687 return !fdt_stringlist_contains(prop, len, compatible);
Kumar Galac8ab7052007-10-24 11:04:22 -0500688}
689
690int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
691 const char *compatible)
692{
David Gibsone8f3f7f2008-02-18 18:09:04 +1100693 int offset, err;
Kumar Galac8ab7052007-10-24 11:04:22 -0500694
David Gibson9457d9d2008-07-09 14:10:24 +1000695 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500696
Kumar Galac8ab7052007-10-24 11:04:22 -0500697 /* 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. */
David Gibsond9d793c2008-02-12 11:58:31 +1100702 for (offset = fdt_next_node(fdt, startoffset, NULL);
703 offset >= 0;
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))
707 return err;
708 else if (err == 0)
709 return offset;
710 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500711
David Gibsond9d793c2008-02-12 11:58:31 +1100712 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500713}