blob: 03733e574f71db1df2ebec2c08ec66dff95908d0 [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier3eaedea2013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04005 */
6#include "libfdt_env.h"
7
Bartlomiej Sieka61250862008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04009#include <fdt.h>
10#include <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
22 if (! p)
23 /* 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
Kumar Galac8ab7052007-10-24 11:04:22 -050050int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
51{
David Gibson9457d9d2008-07-09 14:10:24 +100052 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -050053 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
54 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
Gerald Van Baren18c75c62007-05-17 23:54:36 -040055 return 0;
56}
57
Kumar Galac8ab7052007-10-24 11:04:22 -050058int fdt_num_mem_rsv(const void *fdt)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040059{
Kumar Galac8ab7052007-10-24 11:04:22 -050060 int i = 0;
61
62 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
63 i++;
64 return i;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040065}
66
David Gibson7b64da12010-03-09 17:39:14 +110067static int _nextprop(const void *fdt, int offset)
68{
69 uint32_t tag;
70 int nextoffset;
71
72 do {
73 tag = fdt_next_tag(fdt, offset, &nextoffset);
74
75 switch (tag) {
76 case FDT_END:
77 if (nextoffset >= 0)
78 return -FDT_ERR_BADSTRUCTURE;
79 else
80 return nextoffset;
81
82 case FDT_PROP:
83 return offset;
84 }
85 offset = nextoffset;
86 } while (tag == FDT_NOP);
87
88 return -FDT_ERR_NOTFOUND;
89}
90
David Gibsond9d793c2008-02-12 11:58:31 +110091int fdt_subnode_offset_namelen(const void *fdt, int offset,
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040092 const char *name, int namelen)
93{
David Gibson954914a2009-02-06 14:01:56 +110094 int depth;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040095
David Gibson9457d9d2008-07-09 14:10:24 +100096 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040097
David Gibson954914a2009-02-06 14:01:56 +110098 for (depth = 0;
99 (offset >= 0) && (depth >= 0);
100 offset = fdt_next_node(fdt, offset, &depth))
101 if ((depth == 1)
102 && _fdt_nodename_eq(fdt, offset, name, namelen))
David Gibsond9d793c2008-02-12 11:58:31 +1100103 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400104
David Gibson954914a2009-02-06 14:01:56 +1100105 if (depth < 0)
David Gibson1c0320e2008-10-29 23:27:45 -0500106 return -FDT_ERR_NOTFOUND;
David Gibson954914a2009-02-06 14:01:56 +1100107 return offset; /* error */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400108}
109
110int fdt_subnode_offset(const void *fdt, int parentoffset,
111 const char *name)
112{
113 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
114}
115
Kumar Galac8ab7052007-10-24 11:04:22 -0500116int fdt_path_offset(const void *fdt, const char *path)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400117{
118 const char *end = path + strlen(path);
119 const char *p = path;
120 int offset = 0;
121
David Gibson9457d9d2008-07-09 14:10:24 +1000122 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400123
Kumar Galaeb45e032008-08-14 08:28:19 -0500124 /* see if we have an alias */
125 if (*path != '/') {
David Gibsond6656ea2008-08-20 16:55:14 +1000126 const char *q = strchr(path, '/');
Kumar Galaeb45e032008-08-14 08:28:19 -0500127
Kumar Galaeb45e032008-08-14 08:28:19 -0500128 if (!q)
129 q = end;
130
David Gibsond6656ea2008-08-20 16:55:14 +1000131 p = fdt_get_alias_namelen(fdt, p, q - p);
Kumar Galaeb45e032008-08-14 08:28:19 -0500132 if (!p)
133 return -FDT_ERR_BADPATH;
134 offset = fdt_path_offset(fdt, p);
135
136 p = q;
137 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400138
139 while (*p) {
140 const char *q;
141
142 while (*p == '/')
143 p++;
144 if (! *p)
Kumar Galac8ab7052007-10-24 11:04:22 -0500145 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400146 q = strchr(p, '/');
147 if (! q)
148 q = end;
149
150 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
151 if (offset < 0)
152 return offset;
153
154 p = q;
155 }
156
Gerald Van Baren32480892007-03-31 14:30:53 -0400157 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400158}
159
Kumar Galac8ab7052007-10-24 11:04:22 -0500160const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400161{
David Gibson148a49b2008-05-20 17:19:11 +1000162 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
Kumar Galac8ab7052007-10-24 11:04:22 -0500163 int err;
164
David Gibson148a49b2008-05-20 17:19:11 +1000165 if (((err = fdt_check_header(fdt)) != 0)
166 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
167 goto fail;
Kumar Galac8ab7052007-10-24 11:04:22 -0500168
169 if (len)
170 *len = strlen(nh->name);
171
172 return nh->name;
173
174 fail:
175 if (len)
176 *len = err;
177 return NULL;
178}
179
David Gibson7b64da12010-03-09 17:39:14 +1100180int fdt_first_property_offset(const void *fdt, int nodeoffset)
Kumar Galac8ab7052007-10-24 11:04:22 -0500181{
David Gibson7b64da12010-03-09 17:39:14 +1100182 int offset;
183
184 if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
185 return offset;
186
187 return _nextprop(fdt, offset);
188}
189
190int fdt_next_property_offset(const void *fdt, int offset)
191{
192 if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
193 return offset;
194
195 return _nextprop(fdt, offset);
196}
197
198const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
199 int offset,
200 int *lenp)
201{
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400202 int err;
David Gibson7b64da12010-03-09 17:39:14 +1100203 const struct fdt_property *prop;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400204
David Gibson7b64da12010-03-09 17:39:14 +1100205 if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
206 if (lenp)
207 *lenp = err;
208 return NULL;
209 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400210
David Gibson7b64da12010-03-09 17:39:14 +1100211 prop = _fdt_offset_ptr(fdt, offset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400212
David Gibson7b64da12010-03-09 17:39:14 +1100213 if (lenp)
214 *lenp = fdt32_to_cpu(prop->len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400215
David Gibson7b64da12010-03-09 17:39:14 +1100216 return prop;
217}
218
219const struct fdt_property *fdt_get_property_namelen(const void *fdt,
220 int offset,
221 const char *name,
222 int namelen, int *lenp)
223{
224 for (offset = fdt_first_property_offset(fdt, offset);
225 (offset >= 0);
226 (offset = fdt_next_property_offset(fdt, offset))) {
227 const struct fdt_property *prop;
Kumar Galac8ab7052007-10-24 11:04:22 -0500228
David Gibson7b64da12010-03-09 17:39:14 +1100229 if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
230 offset = -FDT_ERR_INTERNAL;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400231 break;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400232 }
David Gibson7b64da12010-03-09 17:39:14 +1100233 if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
234 name, namelen))
235 return prop;
236 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400237
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400238 if (lenp)
David Gibson7b64da12010-03-09 17:39:14 +1100239 *lenp = offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400240 return NULL;
241}
242
David Gibsonc5b130e2008-08-06 14:50:49 +1000243const struct fdt_property *fdt_get_property(const void *fdt,
244 int nodeoffset,
245 const char *name, int *lenp)
246{
247 return fdt_get_property_namelen(fdt, nodeoffset, name,
248 strlen(name), lenp);
249}
250
251const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
252 const char *name, int namelen, int *lenp)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400253{
254 const struct fdt_property *prop;
255
David Gibsonc5b130e2008-08-06 14:50:49 +1000256 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400257 if (! prop)
258 return NULL;
259
David Gibson7b64da12010-03-09 17:39:14 +1100260 return prop->data;
261}
262
263const void *fdt_getprop_by_offset(const void *fdt, int offset,
264 const char **namep, int *lenp)
265{
266 const struct fdt_property *prop;
267
268 prop = fdt_get_property_by_offset(fdt, offset, lenp);
269 if (!prop)
270 return NULL;
271 if (namep)
272 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
Kumar Galac8ab7052007-10-24 11:04:22 -0500273 return prop->data;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400274}
275
David Gibsonc5b130e2008-08-06 14:50:49 +1000276const void *fdt_getprop(const void *fdt, int nodeoffset,
277 const char *name, int *lenp)
278{
279 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
280}
281
Kumar Galac8ab7052007-10-24 11:04:22 -0500282uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
283{
Kim Phillips38c38152013-01-16 13:59:50 +0000284 const fdt32_t *php;
Kumar Galac8ab7052007-10-24 11:04:22 -0500285 int len;
286
David Gibson3e31de92009-11-26 15:37:13 +1100287 /* FIXME: This is a bit sub-optimal, since we potentially scan
288 * over all the properties twice. */
289 php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
290 if (!php || (len != sizeof(*php))) {
291 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
292 if (!php || (len != sizeof(*php)))
293 return 0;
294 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500295
296 return fdt32_to_cpu(*php);
297}
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400298
David Gibsond6656ea2008-08-20 16:55:14 +1000299const char *fdt_get_alias_namelen(const void *fdt,
300 const char *name, int namelen)
301{
302 int aliasoffset;
303
304 aliasoffset = fdt_path_offset(fdt, "/aliases");
305 if (aliasoffset < 0)
306 return NULL;
307
308 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
309}
310
311const char *fdt_get_alias(const void *fdt, const char *name)
312{
313 return fdt_get_alias_namelen(fdt, name, strlen(name));
314}
315
Kumar Galac8ab7052007-10-24 11:04:22 -0500316int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400317{
David Gibsond9d793c2008-02-12 11:58:31 +1100318 int pdepth = 0, p = 0;
319 int offset, depth, namelen;
Kumar Galac8ab7052007-10-24 11:04:22 -0500320 const char *name;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400321
David Gibson9457d9d2008-07-09 14:10:24 +1000322 FDT_CHECK_HEADER(fdt);
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400323
Kumar Galac8ab7052007-10-24 11:04:22 -0500324 if (buflen < 2)
325 return -FDT_ERR_NOSPACE;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400326
David Gibsond9d793c2008-02-12 11:58:31 +1100327 for (offset = 0, depth = 0;
328 (offset >= 0) && (offset <= nodeoffset);
329 offset = fdt_next_node(fdt, offset, &depth)) {
David Gibsond9d793c2008-02-12 11:58:31 +1100330 while (pdepth > depth) {
331 do {
332 p--;
333 } while (buf[p-1] != '/');
334 pdepth--;
335 }
336
David Gibson8d2810d2008-08-29 14:19:13 +1000337 if (pdepth >= depth) {
338 name = fdt_get_name(fdt, offset, &namelen);
339 if (!name)
340 return namelen;
341 if ((p + namelen + 1) <= buflen) {
342 memcpy(buf + p, name, namelen);
343 p += namelen;
344 buf[p++] = '/';
345 pdepth++;
346 }
David Gibsond9d793c2008-02-12 11:58:31 +1100347 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500348
David Gibsond9d793c2008-02-12 11:58:31 +1100349 if (offset == nodeoffset) {
350 if (pdepth < (depth + 1))
351 return -FDT_ERR_NOSPACE;
Kumar Galac8ab7052007-10-24 11:04:22 -0500352
David Gibsond9d793c2008-02-12 11:58:31 +1100353 if (p > 1) /* special case so that root path is "/", not "" */
354 p--;
355 buf[p] = '\0';
David Gibson8d2810d2008-08-29 14:19:13 +1000356 return 0;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400357 }
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400358 }
359
David Gibsond9d793c2008-02-12 11:58:31 +1100360 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
361 return -FDT_ERR_BADOFFSET;
362 else if (offset == -FDT_ERR_BADOFFSET)
363 return -FDT_ERR_BADSTRUCTURE;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400364
David Gibsond9d793c2008-02-12 11:58:31 +1100365 return offset; /* error from fdt_next_node() */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400366}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400367
Kumar Galac8ab7052007-10-24 11:04:22 -0500368int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
369 int supernodedepth, int *nodedepth)
Gerald Van Barenc077a132007-04-14 22:46:41 -0400370{
David Gibsond9d793c2008-02-12 11:58:31 +1100371 int offset, depth;
Kumar Galac8ab7052007-10-24 11:04:22 -0500372 int supernodeoffset = -FDT_ERR_INTERNAL;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400373
David Gibson9457d9d2008-07-09 14:10:24 +1000374 FDT_CHECK_HEADER(fdt);
Gerald Van Barenc077a132007-04-14 22:46:41 -0400375
Kumar Galac8ab7052007-10-24 11:04:22 -0500376 if (supernodedepth < 0)
377 return -FDT_ERR_NOTFOUND;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400378
David Gibsond9d793c2008-02-12 11:58:31 +1100379 for (offset = 0, depth = 0;
380 (offset >= 0) && (offset <= nodeoffset);
381 offset = fdt_next_node(fdt, offset, &depth)) {
382 if (depth == supernodedepth)
383 supernodeoffset = offset;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400384
David Gibsond9d793c2008-02-12 11:58:31 +1100385 if (offset == nodeoffset) {
386 if (nodedepth)
387 *nodedepth = depth;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400388
David Gibsond9d793c2008-02-12 11:58:31 +1100389 if (supernodedepth > depth)
390 return -FDT_ERR_NOTFOUND;
391 else
392 return supernodeoffset;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400393 }
David Gibsond9d793c2008-02-12 11:58:31 +1100394 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500395
David Gibsond9d793c2008-02-12 11:58:31 +1100396 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
397 return -FDT_ERR_BADOFFSET;
398 else if (offset == -FDT_ERR_BADOFFSET)
399 return -FDT_ERR_BADSTRUCTURE;
Kumar Galac8ab7052007-10-24 11:04:22 -0500400
David Gibsond9d793c2008-02-12 11:58:31 +1100401 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500402}
403
404int fdt_node_depth(const void *fdt, int nodeoffset)
405{
406 int nodedepth;
407 int err;
408
409 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
410 if (err)
411 return (err < 0) ? err : -FDT_ERR_INTERNAL;
412 return nodedepth;
413}
414
415int fdt_parent_offset(const void *fdt, int nodeoffset)
416{
417 int nodedepth = fdt_node_depth(fdt, nodeoffset);
418
419 if (nodedepth < 0)
420 return nodedepth;
421 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
422 nodedepth - 1, NULL);
423}
424
425int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
426 const char *propname,
427 const void *propval, int proplen)
428{
David Gibsond9d793c2008-02-12 11:58:31 +1100429 int offset;
Kumar Galac8ab7052007-10-24 11:04:22 -0500430 const void *val;
431 int len;
432
David Gibson9457d9d2008-07-09 14:10:24 +1000433 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500434
Kumar Galac8ab7052007-10-24 11:04:22 -0500435 /* FIXME: The algorithm here is pretty horrible: we scan each
436 * property of a node in fdt_getprop(), then if that didn't
437 * find what we want, we scan over them again making our way
438 * to the next node. Still it's the easiest to implement
439 * approach; performance can come later. */
David Gibsond9d793c2008-02-12 11:58:31 +1100440 for (offset = fdt_next_node(fdt, startoffset, NULL);
441 offset >= 0;
442 offset = fdt_next_node(fdt, offset, NULL)) {
443 val = fdt_getprop(fdt, offset, propname, &len);
444 if (val && (len == proplen)
445 && (memcmp(val, propval, len) == 0))
446 return offset;
447 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500448
David Gibsond9d793c2008-02-12 11:58:31 +1100449 return offset; /* error from fdt_next_node() */
Gerald Van Barenc077a132007-04-14 22:46:41 -0400450}
451
Kumar Galac8ab7052007-10-24 11:04:22 -0500452int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
Gerald Van Barenc077a132007-04-14 22:46:41 -0400453{
David Gibson3e31de92009-11-26 15:37:13 +1100454 int offset;
455
Kumar Galac8ab7052007-10-24 11:04:22 -0500456 if ((phandle == 0) || (phandle == -1))
457 return -FDT_ERR_BADPHANDLE;
David Gibson3e31de92009-11-26 15:37:13 +1100458
459 FDT_CHECK_HEADER(fdt);
460
461 /* FIXME: The algorithm here is pretty horrible: we
462 * potentially scan each property of a node in
463 * fdt_get_phandle(), then if that didn't find what
464 * we want, we scan over them again making our way to the next
465 * node. Still it's the easiest to implement approach;
466 * performance can come later. */
467 for (offset = fdt_next_node(fdt, -1, NULL);
468 offset >= 0;
469 offset = fdt_next_node(fdt, offset, NULL)) {
470 if (fdt_get_phandle(fdt, offset) == phandle)
471 return offset;
472 }
473
474 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500475}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400476
Simon Glass73b47562013-01-21 12:59:18 -0800477int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
Kumar Galac8ab7052007-10-24 11:04:22 -0500478{
479 int len = strlen(str);
David Gibsonb4141b82008-07-07 10:10:48 +1000480 const char *p;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400481
Kumar Galac8ab7052007-10-24 11:04:22 -0500482 while (listlen >= len) {
483 if (memcmp(str, strlist, len+1) == 0)
484 return 1;
485 p = memchr(strlist, '\0', listlen);
486 if (!p)
487 return 0; /* malformed strlist.. */
488 listlen -= (p-strlist) + 1;
489 strlist = p + 1;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400490 }
491 return 0;
492}
Kumar Galac8ab7052007-10-24 11:04:22 -0500493
Thierry Reding92ba09e2014-08-26 17:33:50 +0200494int fdt_count_strings(const void *fdt, int node, const char *property)
495{
496 int length, i, count = 0;
497 const char *list;
498
499 list = fdt_getprop(fdt, node, property, &length);
500 if (!list)
501 return -length;
502
503 for (i = 0; i < length; i++) {
504 int len = strlen(list);
505
506 list += len + 1;
507 i += len;
508 count++;
509 }
510
511 return count;
512}
513
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200514int fdt_find_string(const void *fdt, int node, const char *property,
515 const char *string)
516{
517 const char *list, *end;
518 int len, index = 0;
519
520 list = fdt_getprop(fdt, node, property, &len);
521 if (!list)
522 return len;
523
524 end = list + len;
525 len = strlen(string);
526
527 while (list < end) {
528 int l = strlen(list);
529
530 if (l == len && memcmp(list, string, len) == 0)
531 return index;
532
533 list += l + 1;
534 index++;
535 }
536
537 return -FDT_ERR_NOTFOUND;
538}
539
Thierry Reding5504b802014-08-26 17:33:52 +0200540int fdt_get_string_index(const void *fdt, int node, const char *property,
541 int index, const char **output)
542{
543 const char *list;
544 int length, i;
545
546 list = fdt_getprop(fdt, node, property, &length);
547
548 for (i = 0; i < length; i++) {
549 int len = strlen(list);
550
551 if (index == 0) {
552 *output = list;
553 return 0;
554 }
555
556 list += len + 1;
557 i += len;
558 index--;
559 }
560
561 return FDT_ERR_NOTFOUND;
562}
563
564int fdt_get_string(const void *fdt, int node, const char *property,
565 const char **output)
566{
567 return fdt_get_string_index(fdt, node, property, 0, output);
568}
569
Kumar Galac8ab7052007-10-24 11:04:22 -0500570int fdt_node_check_compatible(const void *fdt, int nodeoffset,
571 const char *compatible)
572{
573 const void *prop;
574 int len;
575
576 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
577 if (!prop)
578 return len;
Simon Glass73b47562013-01-21 12:59:18 -0800579 if (fdt_stringlist_contains(prop, len, compatible))
Kumar Galac8ab7052007-10-24 11:04:22 -0500580 return 0;
581 else
582 return 1;
583}
584
585int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
586 const char *compatible)
587{
David Gibsone8f3f7f2008-02-18 18:09:04 +1100588 int offset, err;
Kumar Galac8ab7052007-10-24 11:04:22 -0500589
David Gibson9457d9d2008-07-09 14:10:24 +1000590 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500591
Kumar Galac8ab7052007-10-24 11:04:22 -0500592 /* FIXME: The algorithm here is pretty horrible: we scan each
593 * property of a node in fdt_node_check_compatible(), then if
594 * that didn't find what we want, we scan over them again
595 * making our way to the next node. Still it's the easiest to
596 * implement approach; performance can come later. */
David Gibsond9d793c2008-02-12 11:58:31 +1100597 for (offset = fdt_next_node(fdt, startoffset, NULL);
598 offset >= 0;
599 offset = fdt_next_node(fdt, offset, NULL)) {
600 err = fdt_node_check_compatible(fdt, offset, compatible);
601 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
602 return err;
603 else if (err == 0)
604 return offset;
605 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500606
David Gibsond9d793c2008-02-12 11:58:31 +1100607 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500608}