blob: 693de9aa5ad8032375b22749a25bc236dc15d950 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
Gerald Van Baren35748172007-03-31 12:00:56 -04002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
Gerald Van Baren35748172007-03-31 12:00:56 -04005 */
Masahiro Yamadab08c8c42018-03-05 01:20:11 +09006#include <linux/libfdt_env.h>
Gerald Van Baren35748172007-03-31 12:00:56 -04007
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren35748172007-03-31 12:00:56 -04009#include <fdt.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren35748172007-03-31 12:00:56 -040014
15#include "libfdt_internal.h"
16
David Gibsonfc7758e2008-07-09 14:10:24 +100017static int _fdt_nodename_eq(const void *fdt, int offset,
18 const char *s, int len)
Gerald Van Baren35748172007-03-31 12:00:56 -040019{
David Gibsonae0b5902008-02-12 11:58:31 +110020 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
Gerald Van Baren35748172007-03-31 12:00:56 -040021
Robert P. J. Day6feed2a2016-05-23 05:40:55 -040022 if (!p)
Gerald Van Baren35748172007-03-31 12:00:56 -040023 /* short match */
24 return 0;
25
26 if (memcmp(p, s, len) != 0)
27 return 0;
28
Kumar Gala8d04f022007-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 Baren35748172007-03-31 12:00:56 -040034 return 0;
Gerald Van Baren35748172007-03-31 12:00:56 -040035}
36
Kumar Gala8d04f022007-10-24 11:04:22 -050037const char *fdt_string(const void *fdt, int stroffset)
Gerald Van Baren35748172007-03-31 12:00:56 -040038{
David Gibsonc6683022008-07-07 10:14:15 +100039 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
Gerald Van Baren35748172007-03-31 12:00:56 -040040}
41
David Gibson02193992008-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 Nalleyaf67b252014-02-26 11:32:21 -050047 return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0);
David Gibson02193992008-08-06 14:50:49 +100048}
49
Maxime Ripard57c78092016-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 Glass9c07b982016-10-02 17:59:27 -060063 return (uint32_t)-1;
Maxime Ripard57c78092016-07-05 10:26:39 +020064
65 phandle = fdt_get_phandle(fdt, offset);
66 if (phandle == (uint32_t)-1)
Simon Glass9c07b982016-10-02 17:59:27 -060067 continue;
Maxime Ripard57c78092016-07-05 10:26:39 +020068
69 if (phandle > max_phandle)
70 max_phandle = phandle;
71 }
72
73 return 0;
74}
75
Thierry Redingea1df3e2019-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 Gala8d04f022007-10-24 11:04:22 -0500107int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
108{
David Gibsonfc7758e2008-07-09 14:10:24 +1000109 FDT_CHECK_HEADER(fdt);
Rob Herringdb405d12018-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);
Kumar Gala8d04f022007-10-24 11:04:22 -0500112 return 0;
113}
114
115int fdt_num_mem_rsv(const void *fdt)
116{
117 int i = 0;
118
Rob Herringdb405d12018-05-19 14:13:53 +0200119 while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0)
Kumar Gala8d04f022007-10-24 11:04:22 -0500120 i++;
121 return i;
122}
123
David Gibsond1c63142010-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 Gibsonae0b5902008-02-12 11:58:31 +1100148int fdt_subnode_offset_namelen(const void *fdt, int offset,
Gerald Van Baren35748172007-03-31 12:00:56 -0400149 const char *name, int namelen)
150{
David Gibson2c0b8432009-02-06 14:01:56 +1100151 int depth;
Gerald Van Baren35748172007-03-31 12:00:56 -0400152
David Gibsonfc7758e2008-07-09 14:10:24 +1000153 FDT_CHECK_HEADER(fdt);
Gerald Van Baren35748172007-03-31 12:00:56 -0400154
David Gibson2c0b8432009-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 Gibsonae0b5902008-02-12 11:58:31 +1100160 return offset;
Gerald Van Baren35748172007-03-31 12:00:56 -0400161
David Gibson2c0b8432009-02-06 14:01:56 +1100162 if (depth < 0)
David Gibson4bc7dee2008-10-29 23:27:45 -0500163 return -FDT_ERR_NOTFOUND;
David Gibson2c0b8432009-02-06 14:01:56 +1100164 return offset; /* error */
Gerald Van Baren35748172007-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 Goede77d7fff2015-04-20 11:13:37 +0200173/*
Maxime Ripard6f5f92c2016-07-05 10:26:40 +0200174 * Find the next of path separator, note we need to search for both '/' and ':'
Robert P. J. Day6feed2a2016-05-23 05:40:55 -0400175 * and then take the first one so that we do the right thing for e.g.
Hans de Goede77d7fff2015-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 Ripard8e968572016-07-05 10:26:41 +0200179static const char *fdt_path_next_separator(const char *path, int len)
Hans de Goede77d7fff2015-04-20 11:13:37 +0200180{
Maxime Ripard8e968572016-07-05 10:26:41 +0200181 const void *sep1 = memchr(path, '/', len);
182 const void *sep2 = memchr(path, ':', len);
Hans de Goede77d7fff2015-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 Ripard8e968572016-07-05 10:26:41 +0200192int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
Gerald Van Baren35748172007-03-31 12:00:56 -0400193{
Maxime Ripard8e968572016-07-05 10:26:41 +0200194 const char *end = path + namelen;
Gerald Van Baren35748172007-03-31 12:00:56 -0400195 const char *p = path;
196 int offset = 0;
197
David Gibsonfc7758e2008-07-09 14:10:24 +1000198 FDT_CHECK_HEADER(fdt);
Gerald Van Baren35748172007-03-31 12:00:56 -0400199
Kumar Galafeeca3f2008-08-14 08:28:19 -0500200 /* see if we have an alias */
201 if (*path != '/') {
Maxime Ripard8e968572016-07-05 10:26:41 +0200202 const char *q = fdt_path_next_separator(path, namelen);
Kumar Galafeeca3f2008-08-14 08:28:19 -0500203
Kumar Galafeeca3f2008-08-14 08:28:19 -0500204 if (!q)
205 q = end;
206
David Gibson9a6cf732008-08-20 16:55:14 +1000207 p = fdt_get_alias_namelen(fdt, p, q - p);
Kumar Galafeeca3f2008-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 Baren35748172007-03-31 12:00:56 -0400214
Maxime Ripard8e968572016-07-05 10:26:41 +0200215 while (*p && (p < end)) {
Gerald Van Baren35748172007-03-31 12:00:56 -0400216 const char *q;
217
218 while (*p == '/')
219 p++;
Maxime Ripard8e968572016-07-05 10:26:41 +0200220
Hans de Goede77d7fff2015-04-20 11:13:37 +0200221 if (*p == '\0' || *p == ':')
Kumar Gala8d04f022007-10-24 11:04:22 -0500222 return offset;
Maxime Ripard8e968572016-07-05 10:26:41 +0200223
224 q = fdt_path_next_separator(p, end - p);
Robert P. J. Day6feed2a2016-05-23 05:40:55 -0400225 if (!q)
Gerald Van Baren35748172007-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 Barenaea03c42007-03-31 14:30:53 -0400235 return offset;
Gerald Van Baren35748172007-03-31 12:00:56 -0400236}
237
Simon Glass42b76002016-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 Gala8d04f022007-10-24 11:04:22 -0500243const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
Gerald Van Baren35748172007-03-31 12:00:56 -0400244{
Rob Herringdb405d12018-05-19 14:13:53 +0200245 const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
Kumar Gala8d04f022007-10-24 11:04:22 -0500246 int err;
247
David Gibson2f08bfa2008-05-20 17:19:11 +1000248 if (((err = fdt_check_header(fdt)) != 0)
Rob Herringdb405d12018-05-19 14:13:53 +0200249 || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
David Gibson2f08bfa2008-05-20 17:19:11 +1000250 goto fail;
Kumar Gala8d04f022007-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 Gibsond1c63142010-03-09 17:39:14 +1100263int fdt_first_property_offset(const void *fdt, int nodeoffset)
264{
265 int offset;
266
Rob Herringdb405d12018-05-19 14:13:53 +0200267 if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
David Gibsond1c63142010-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 Herringdb405d12018-05-19 14:13:53 +0200275 if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
David Gibsond1c63142010-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{
285 int err;
286 const struct fdt_property *prop;
287
Rob Herringdb405d12018-05-19 14:13:53 +0200288 if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
David Gibsond1c63142010-03-09 17:39:14 +1100289 if (lenp)
290 *lenp = err;
291 return NULL;
292 }
293
Rob Herringdb405d12018-05-19 14:13:53 +0200294 prop = fdt_offset_ptr_(fdt, offset);
David Gibsond1c63142010-03-09 17:39:14 +1100295
296 if (lenp)
297 *lenp = fdt32_to_cpu(prop->len);
298
299 return prop;
300}
301
David Gibson02193992008-08-06 14:50:49 +1000302const struct fdt_property *fdt_get_property_namelen(const void *fdt,
David Gibsond1c63142010-03-09 17:39:14 +1100303 int offset,
David Gibson02193992008-08-06 14:50:49 +1000304 const char *name,
305 int namelen, int *lenp)
Kumar Gala8d04f022007-10-24 11:04:22 -0500306{
David Gibsond1c63142010-03-09 17:39:14 +1100307 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;
Gerald Van Baren35748172007-03-31 12:00:56 -0400311
David Gibsond1c63142010-03-09 17:39:14 +1100312 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
313 offset = -FDT_ERR_INTERNAL;
Gerald Van Baren35748172007-03-31 12:00:56 -0400314 break;
Gerald Van Baren35748172007-03-31 12:00:56 -0400315 }
David Gibsond1c63142010-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 Baren35748172007-03-31 12:00:56 -0400320
Gerald Van Baren35748172007-03-31 12:00:56 -0400321 if (lenp)
David Gibsond1c63142010-03-09 17:39:14 +1100322 *lenp = offset;
Gerald Van Baren35748172007-03-31 12:00:56 -0400323 return NULL;
324}
325
David Gibson02193992008-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 Baren35748172007-03-31 12:00:56 -0400336{
337 const struct fdt_property *prop;
338
David Gibson02193992008-08-06 14:50:49 +1000339 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
Robert P. J. Day6feed2a2016-05-23 05:40:55 -0400340 if (!prop)
Gerald Van Baren35748172007-03-31 12:00:56 -0400341 return NULL;
342
Kumar Gala8d04f022007-10-24 11:04:22 -0500343 return prop->data;
Gerald Van Baren35748172007-03-31 12:00:56 -0400344}
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400345
David Gibsond1c63142010-03-09 17:39:14 +1100346const 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));
356 return prop->data;
357}
358
David Gibson02193992008-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 Gala8d04f022007-10-24 11:04:22 -0500365uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400366{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +0000367 const fdt32_t *php;
Kumar Gala8d04f022007-10-24 11:04:22 -0500368 int len;
369
David Gibson05a22ba2009-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 Gala8d04f022007-10-24 11:04:22 -0500378
379 return fdt32_to_cpu(*php);
380}
381
David Gibson9a6cf732008-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 Gala8d04f022007-10-24 11:04:22 -0500399int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
400{
David Gibsonae0b5902008-02-12 11:58:31 +1100401 int pdepth = 0, p = 0;
402 int offset, depth, namelen;
Kumar Gala8d04f022007-10-24 11:04:22 -0500403 const char *name;
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400404
David Gibsonfc7758e2008-07-09 14:10:24 +1000405 FDT_CHECK_HEADER(fdt);
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400406
Kumar Gala8d04f022007-10-24 11:04:22 -0500407 if (buflen < 2)
Gerald Van Baren3f9f08c2007-04-14 22:46:41 -0400408 return -FDT_ERR_NOSPACE;
Kumar Gala8d04f022007-10-24 11:04:22 -0500409
David Gibsonae0b5902008-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 Gibsonae0b5902008-02-12 11:58:31 +1100413 while (pdepth > depth) {
414 do {
415 p--;
416 } while (buf[p-1] != '/');
417 pdepth--;
418 }
419
David Gibsonbbdbc7c2008-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 Gibsonae0b5902008-02-12 11:58:31 +1100430 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500431
David Gibsonae0b5902008-02-12 11:58:31 +1100432 if (offset == nodeoffset) {
433 if (pdepth < (depth + 1))
434 return -FDT_ERR_NOSPACE;
435
436 if (p > 1) /* special case so that root path is "/", not "" */
Kumar Gala8d04f022007-10-24 11:04:22 -0500437 p--;
David Gibsonae0b5902008-02-12 11:58:31 +1100438 buf[p] = '\0';
David Gibsonbbdbc7c2008-08-29 14:19:13 +1000439 return 0;
Kumar Gala8d04f022007-10-24 11:04:22 -0500440 }
441 }
442
David Gibsonae0b5902008-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;
Kumar Gala8d04f022007-10-24 11:04:22 -0500447
David Gibsonae0b5902008-02-12 11:58:31 +1100448 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500449}
450
451int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
452 int supernodedepth, int *nodedepth)
453{
David Gibsonae0b5902008-02-12 11:58:31 +1100454 int offset, depth;
Kumar Gala8d04f022007-10-24 11:04:22 -0500455 int supernodeoffset = -FDT_ERR_INTERNAL;
456
David Gibsonfc7758e2008-07-09 14:10:24 +1000457 FDT_CHECK_HEADER(fdt);
Kumar Gala8d04f022007-10-24 11:04:22 -0500458
459 if (supernodedepth < 0)
460 return -FDT_ERR_NOTFOUND;
461
David Gibsonae0b5902008-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;
Kumar Gala8d04f022007-10-24 11:04:22 -0500467
David Gibsonae0b5902008-02-12 11:58:31 +1100468 if (offset == nodeoffset) {
469 if (nodedepth)
470 *nodedepth = depth;
Kumar Gala8d04f022007-10-24 11:04:22 -0500471
David Gibsonae0b5902008-02-12 11:58:31 +1100472 if (supernodedepth > depth)
473 return -FDT_ERR_NOTFOUND;
474 else
475 return supernodeoffset;
Kumar Gala8d04f022007-10-24 11:04:22 -0500476 }
David Gibsonae0b5902008-02-12 11:58:31 +1100477 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500478
David Gibsonae0b5902008-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 Gala8d04f022007-10-24 11:04:22 -0500483
David Gibsonae0b5902008-02-12 11:58:31 +1100484 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-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 Gibsonae0b5902008-02-12 11:58:31 +1100512 int offset;
Kumar Gala8d04f022007-10-24 11:04:22 -0500513 const void *val;
514 int len;
515
David Gibsonfc7758e2008-07-09 14:10:24 +1000516 FDT_CHECK_HEADER(fdt);
Kumar Gala8d04f022007-10-24 11:04:22 -0500517
Kumar Gala8d04f022007-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 Gibsonae0b5902008-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 Gala8d04f022007-10-24 11:04:22 -0500531
David Gibsonae0b5902008-02-12 11:58:31 +1100532 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500533}
534
535int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
536{
David Gibson05a22ba2009-11-26 15:37:13 +1100537 int offset;
538
Kumar Gala8d04f022007-10-24 11:04:22 -0500539 if ((phandle == 0) || (phandle == -1))
540 return -FDT_ERR_BADPHANDLE;
David Gibson05a22ba2009-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 Gala8d04f022007-10-24 11:04:22 -0500558}
559
Simon Glasse853b322013-01-21 12:59:18 -0800560int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
Kumar Gala8d04f022007-10-24 11:04:22 -0500561{
562 int len = strlen(str);
David Gibsonef4e8ce2008-07-07 10:10:48 +1000563 const char *p;
Kumar Gala8d04f022007-10-24 11:04:22 -0500564
565 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 Baren3f9f08c2007-04-14 22:46:41 -0400573 }
574 return 0;
575}
Kumar Gala8d04f022007-10-24 11:04:22 -0500576
Simon Glassb02e4042016-10-02 17:59:28 -0600577int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
Thierry Redingbc4147a2014-08-26 17:33:50 +0200578{
Simon Glassb02e4042016-10-02 17:59:28 -0600579 const char *list, *end;
580 int length, count = 0;
Thierry Redingbc4147a2014-08-26 17:33:50 +0200581
Simon Glassb02e4042016-10-02 17:59:28 -0600582 list = fdt_getprop(fdt, nodeoffset, property, &length);
Thierry Redingbc4147a2014-08-26 17:33:50 +0200583 if (!list)
Masahiro Yamada7c9786d2016-10-17 20:22:33 +0900584 return length;
Thierry Redingbc4147a2014-08-26 17:33:50 +0200585
Simon Glassb02e4042016-10-02 17:59:28 -0600586 end = list + length;
Thierry Redingbc4147a2014-08-26 17:33:50 +0200587
Simon Glassb02e4042016-10-02 17:59:28 -0600588 while (list < end) {
589 length = strnlen(list, end - list) + 1;
590
591 /* 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 Redingbc4147a2014-08-26 17:33:50 +0200596 count++;
597 }
598
599 return count;
600}
601
Simon Glassb02e4042016-10-02 17:59:28 -0600602int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
603 const char *string)
Thierry Redingfc503c12014-08-26 17:33:51 +0200604{
Simon Glassb02e4042016-10-02 17:59:28 -0600605 int length, len, idx = 0;
Thierry Redingfc503c12014-08-26 17:33:51 +0200606 const char *list, *end;
Thierry Redingfc503c12014-08-26 17:33:51 +0200607
Simon Glassb02e4042016-10-02 17:59:28 -0600608 list = fdt_getprop(fdt, nodeoffset, property, &length);
Thierry Redingfc503c12014-08-26 17:33:51 +0200609 if (!list)
Masahiro Yamada01ae56c2016-10-17 20:22:34 +0900610 return length;
Thierry Redingfc503c12014-08-26 17:33:51 +0200611
Simon Glassb02e4042016-10-02 17:59:28 -0600612 len = strlen(string) + 1;
613 end = list + length;
Thierry Redingfc503c12014-08-26 17:33:51 +0200614
615 while (list < end) {
Simon Glassb02e4042016-10-02 17:59:28 -0600616 length = strnlen(list, end - list) + 1;
Thierry Redingfc503c12014-08-26 17:33:51 +0200617
Simon Glassb02e4042016-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 Redingfc503c12014-08-26 17:33:51 +0200621
Simon Glassb02e4042016-10-02 17:59:28 -0600622 if (length == len && memcmp(list, string, length) == 0)
623 return idx;
624
625 list += length;
626 idx++;
Thierry Redingfc503c12014-08-26 17:33:51 +0200627 }
628
629 return -FDT_ERR_NOTFOUND;
630}
631
Simon Glassb02e4042016-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 Reding5094eb42014-08-26 17:33:52 +0200635{
Simon Glassb02e4042016-10-02 17:59:28 -0600636 const char *list, *end;
637 int length;
Thierry Reding5094eb42014-08-26 17:33:52 +0200638
Simon Glassb02e4042016-10-02 17:59:28 -0600639 list = fdt_getprop(fdt, nodeoffset, property, &length);
640 if (!list) {
641 if (lenp)
642 *lenp = length;
Thierry Reding5094eb42014-08-26 17:33:52 +0200643
Simon Glassb02e4042016-10-02 17:59:28 -0600644 return NULL;
645 }
Thierry Reding5094eb42014-08-26 17:33:52 +0200646
Simon Glassb02e4042016-10-02 17:59:28 -0600647 end = list + length;
648
649 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;
Thierry Reding5094eb42014-08-26 17:33:52 +0200658 }
659
Simon Glassb02e4042016-10-02 17:59:28 -0600660 if (idx == 0) {
661 if (lenp)
662 *lenp = length - 1;
663
664 return list;
665 }
666
667 list += length;
668 idx--;
Thierry Reding5094eb42014-08-26 17:33:52 +0200669 }
670
Simon Glassb02e4042016-10-02 17:59:28 -0600671 if (lenp)
672 *lenp = -FDT_ERR_NOTFOUND;
Thierry Reding5094eb42014-08-26 17:33:52 +0200673
Simon Glassb02e4042016-10-02 17:59:28 -0600674 return NULL;
Thierry Reding5094eb42014-08-26 17:33:52 +0200675}
676
Kumar Gala8d04f022007-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 Glass9c07b982016-10-02 17:59:27 -0600686
687 return !fdt_stringlist_contains(prop, len, compatible);
Kumar Gala8d04f022007-10-24 11:04:22 -0500688}
689
690int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
691 const char *compatible)
692{
David Gibson11abe452008-02-18 18:09:04 +1100693 int offset, err;
Kumar Gala8d04f022007-10-24 11:04:22 -0500694
David Gibsonfc7758e2008-07-09 14:10:24 +1000695 FDT_CHECK_HEADER(fdt);
Kumar Gala8d04f022007-10-24 11:04:22 -0500696
Kumar Gala8d04f022007-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 Gibsonae0b5902008-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 Gala8d04f022007-10-24 11:04:22 -0500711
David Gibsonae0b5902008-02-12 11:58:31 +1100712 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500713}