]>
Commit | Line | Data |
---|---|---|
35748177 GVB |
1 | /* |
2 | * libfdt - Flat Device Tree manipulation | |
3 | * Copyright (C) 2006 David Gibson, IBM Corporation. | |
35084760 | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
35748177 | 5 | */ |
6feed2a5 | 6 | #include <libfdt_env.h> |
35748177 | 7 | |
8cf30809 | 8 | #ifndef USE_HOSTCC |
35748177 GVB |
9 | #include <fdt.h> |
10 | #include <libfdt.h> | |
8cf30809 BS |
11 | #else |
12 | #include "fdt_host.h" | |
13 | #endif | |
35748177 GVB |
14 | |
15 | #include "libfdt_internal.h" | |
16 | ||
fc7758ee DG |
17 | static int _fdt_nodename_eq(const void *fdt, int offset, |
18 | const char *s, int len) | |
35748177 | 19 | { |
ae0b5908 | 20 | const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1); |
35748177 | 21 | |
6feed2a5 | 22 | if (!p) |
35748177 GVB |
23 | /* short match */ |
24 | return 0; | |
25 | ||
26 | if (memcmp(p, s, len) != 0) | |
27 | return 0; | |
28 | ||
8d04f02f KG |
29 | if (p[len] == '\0') |
30 | return 1; | |
31 | else if (!memchr(s, '@', len) && (p[len] == '@')) | |
32 | return 1; | |
33 | else | |
35748177 | 34 | return 0; |
35748177 GVB |
35 | } |
36 | ||
8d04f02f | 37 | const char *fdt_string(const void *fdt, int stroffset) |
9675ee72 | 38 | { |
c6683026 | 39 | return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
8d04f02f | 40 | } |
9675ee72 | 41 | |
0219399a DG |
42 | static 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 | ||
af67b252 | 47 | return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0); |
0219399a DG |
48 | } |
49 | ||
57c7809a MR |
50 | uint32_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) | |
9c07b987 | 63 | return (uint32_t)-1; |
57c7809a MR |
64 | |
65 | phandle = fdt_get_phandle(fdt, offset); | |
66 | if (phandle == (uint32_t)-1) | |
9c07b987 | 67 | continue; |
57c7809a MR |
68 | |
69 | if (phandle > max_phandle) | |
70 | max_phandle = phandle; | |
71 | } | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
8d04f02f KG |
76 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) |
77 | { | |
fc7758ee | 78 | FDT_CHECK_HEADER(fdt); |
8d04f02f KG |
79 | *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address); |
80 | *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size); | |
9675ee72 GVB |
81 | return 0; |
82 | } | |
83 | ||
8d04f02f | 84 | int fdt_num_mem_rsv(const void *fdt) |
35748177 | 85 | { |
8d04f02f KG |
86 | int i = 0; |
87 | ||
88 | while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0) | |
89 | i++; | |
90 | return i; | |
35748177 GVB |
91 | } |
92 | ||
d1c63148 DG |
93 | static int _nextprop(const void *fdt, int offset) |
94 | { | |
95 | uint32_t tag; | |
96 | int nextoffset; | |
97 | ||
98 | do { | |
99 | tag = fdt_next_tag(fdt, offset, &nextoffset); | |
100 | ||
101 | switch (tag) { | |
102 | case FDT_END: | |
103 | if (nextoffset >= 0) | |
104 | return -FDT_ERR_BADSTRUCTURE; | |
105 | else | |
106 | return nextoffset; | |
107 | ||
108 | case FDT_PROP: | |
109 | return offset; | |
110 | } | |
111 | offset = nextoffset; | |
112 | } while (tag == FDT_NOP); | |
113 | ||
114 | return -FDT_ERR_NOTFOUND; | |
115 | } | |
116 | ||
ae0b5908 | 117 | int fdt_subnode_offset_namelen(const void *fdt, int offset, |
35748177 GVB |
118 | const char *name, int namelen) |
119 | { | |
2c0b843e | 120 | int depth; |
35748177 | 121 | |
fc7758ee | 122 | FDT_CHECK_HEADER(fdt); |
35748177 | 123 | |
2c0b843e DG |
124 | for (depth = 0; |
125 | (offset >= 0) && (depth >= 0); | |
126 | offset = fdt_next_node(fdt, offset, &depth)) | |
127 | if ((depth == 1) | |
128 | && _fdt_nodename_eq(fdt, offset, name, namelen)) | |
ae0b5908 | 129 | return offset; |
35748177 | 130 | |
2c0b843e | 131 | if (depth < 0) |
4bc7deee | 132 | return -FDT_ERR_NOTFOUND; |
2c0b843e | 133 | return offset; /* error */ |
35748177 GVB |
134 | } |
135 | ||
136 | int fdt_subnode_offset(const void *fdt, int parentoffset, | |
137 | const char *name) | |
138 | { | |
139 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); | |
140 | } | |
141 | ||
77d7fff8 | 142 | /* |
6f5f92c6 | 143 | * Find the next of path separator, note we need to search for both '/' and ':' |
6feed2a5 | 144 | * and then take the first one so that we do the right thing for e.g. |
77d7fff8 HG |
145 | * "foo/bar:option" and "bar:option/otheroption", both of which happen, so |
146 | * first searching for either ':' or '/' does not work. | |
147 | */ | |
8e968571 | 148 | static const char *fdt_path_next_separator(const char *path, int len) |
77d7fff8 | 149 | { |
8e968571 MR |
150 | const void *sep1 = memchr(path, '/', len); |
151 | const void *sep2 = memchr(path, ':', len); | |
77d7fff8 HG |
152 | |
153 | if (sep1 && sep2) | |
154 | return (sep1 < sep2) ? sep1 : sep2; | |
155 | else if (sep1) | |
156 | return sep1; | |
157 | else | |
158 | return sep2; | |
159 | } | |
160 | ||
8e968571 | 161 | int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) |
35748177 | 162 | { |
8e968571 | 163 | const char *end = path + namelen; |
35748177 GVB |
164 | const char *p = path; |
165 | int offset = 0; | |
166 | ||
fc7758ee | 167 | FDT_CHECK_HEADER(fdt); |
35748177 | 168 | |
feeca3f5 KG |
169 | /* see if we have an alias */ |
170 | if (*path != '/') { | |
8e968571 | 171 | const char *q = fdt_path_next_separator(path, namelen); |
feeca3f5 | 172 | |
feeca3f5 KG |
173 | if (!q) |
174 | q = end; | |
175 | ||
9a6cf73a | 176 | p = fdt_get_alias_namelen(fdt, p, q - p); |
feeca3f5 KG |
177 | if (!p) |
178 | return -FDT_ERR_BADPATH; | |
179 | offset = fdt_path_offset(fdt, p); | |
180 | ||
181 | p = q; | |
182 | } | |
35748177 | 183 | |
8e968571 | 184 | while (*p && (p < end)) { |
35748177 GVB |
185 | const char *q; |
186 | ||
187 | while (*p == '/') | |
188 | p++; | |
8e968571 | 189 | |
77d7fff8 | 190 | if (*p == '\0' || *p == ':') |
8d04f02f | 191 | return offset; |
8e968571 MR |
192 | |
193 | q = fdt_path_next_separator(p, end - p); | |
6feed2a5 | 194 | if (!q) |
35748177 GVB |
195 | q = end; |
196 | ||
197 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); | |
198 | if (offset < 0) | |
199 | return offset; | |
200 | ||
201 | p = q; | |
202 | } | |
203 | ||
aea03c4e | 204 | return offset; |
35748177 GVB |
205 | } |
206 | ||
42b7600d SG |
207 | int fdt_path_offset(const void *fdt, const char *path) |
208 | { | |
209 | return fdt_path_offset_namelen(fdt, path, strlen(path)); | |
210 | } | |
211 | ||
8d04f02f KG |
212 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) |
213 | { | |
2f08bfa9 | 214 | const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset); |
8d04f02f KG |
215 | int err; |
216 | ||
2f08bfa9 DG |
217 | if (((err = fdt_check_header(fdt)) != 0) |
218 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) | |
219 | goto fail; | |
8d04f02f KG |
220 | |
221 | if (len) | |
222 | *len = strlen(nh->name); | |
223 | ||
224 | return nh->name; | |
225 | ||
226 | fail: | |
227 | if (len) | |
228 | *len = err; | |
229 | return NULL; | |
230 | } | |
231 | ||
d1c63148 DG |
232 | int fdt_first_property_offset(const void *fdt, int nodeoffset) |
233 | { | |
234 | int offset; | |
235 | ||
236 | if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0) | |
237 | return offset; | |
238 | ||
239 | return _nextprop(fdt, offset); | |
240 | } | |
241 | ||
242 | int fdt_next_property_offset(const void *fdt, int offset) | |
243 | { | |
244 | if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0) | |
245 | return offset; | |
246 | ||
247 | return _nextprop(fdt, offset); | |
248 | } | |
249 | ||
250 | const struct fdt_property *fdt_get_property_by_offset(const void *fdt, | |
251 | int offset, | |
252 | int *lenp) | |
35748177 | 253 | { |
35748177 | 254 | int err; |
d1c63148 | 255 | const struct fdt_property *prop; |
35748177 | 256 | |
d1c63148 DG |
257 | if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) { |
258 | if (lenp) | |
259 | *lenp = err; | |
260 | return NULL; | |
261 | } | |
35748177 | 262 | |
d1c63148 | 263 | prop = _fdt_offset_ptr(fdt, offset); |
35748177 | 264 | |
d1c63148 DG |
265 | if (lenp) |
266 | *lenp = fdt32_to_cpu(prop->len); | |
35748177 | 267 | |
d1c63148 DG |
268 | return prop; |
269 | } | |
270 | ||
271 | const struct fdt_property *fdt_get_property_namelen(const void *fdt, | |
272 | int offset, | |
273 | const char *name, | |
274 | int namelen, int *lenp) | |
275 | { | |
276 | for (offset = fdt_first_property_offset(fdt, offset); | |
277 | (offset >= 0); | |
278 | (offset = fdt_next_property_offset(fdt, offset))) { | |
279 | const struct fdt_property *prop; | |
280 | ||
281 | if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) { | |
282 | offset = -FDT_ERR_INTERNAL; | |
35748177 | 283 | break; |
35748177 | 284 | } |
d1c63148 DG |
285 | if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff), |
286 | name, namelen)) | |
287 | return prop; | |
288 | } | |
35748177 | 289 | |
35748177 | 290 | if (lenp) |
d1c63148 | 291 | *lenp = offset; |
35748177 GVB |
292 | return NULL; |
293 | } | |
294 | ||
0219399a DG |
295 | const struct fdt_property *fdt_get_property(const void *fdt, |
296 | int nodeoffset, | |
297 | const char *name, int *lenp) | |
298 | { | |
299 | return fdt_get_property_namelen(fdt, nodeoffset, name, | |
300 | strlen(name), lenp); | |
301 | } | |
302 | ||
303 | const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, | |
304 | const char *name, int namelen, int *lenp) | |
35748177 GVB |
305 | { |
306 | const struct fdt_property *prop; | |
307 | ||
0219399a | 308 | prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp); |
6feed2a5 | 309 | if (!prop) |
35748177 GVB |
310 | return NULL; |
311 | ||
8d04f02f | 312 | return prop->data; |
3af0d587 GVB |
313 | } |
314 | ||
d1c63148 DG |
315 | const void *fdt_getprop_by_offset(const void *fdt, int offset, |
316 | const char **namep, int *lenp) | |
317 | { | |
318 | const struct fdt_property *prop; | |
319 | ||
320 | prop = fdt_get_property_by_offset(fdt, offset, lenp); | |
321 | if (!prop) | |
322 | return NULL; | |
323 | if (namep) | |
324 | *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); | |
325 | return prop->data; | |
326 | } | |
327 | ||
0219399a DG |
328 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
329 | const char *name, int *lenp) | |
330 | { | |
331 | return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); | |
332 | } | |
333 | ||
8d04f02f KG |
334 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
335 | { | |
b2ba62a1 | 336 | const fdt32_t *php; |
8d04f02f | 337 | int len; |
3af0d587 | 338 | |
05a22ba0 DG |
339 | /* FIXME: This is a bit sub-optimal, since we potentially scan |
340 | * over all the properties twice. */ | |
341 | php = fdt_getprop(fdt, nodeoffset, "phandle", &len); | |
342 | if (!php || (len != sizeof(*php))) { | |
343 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); | |
344 | if (!php || (len != sizeof(*php))) | |
345 | return 0; | |
346 | } | |
8d04f02f KG |
347 | |
348 | return fdt32_to_cpu(*php); | |
349 | } | |
350 | ||
9a6cf73a DG |
351 | const char *fdt_get_alias_namelen(const void *fdt, |
352 | const char *name, int namelen) | |
353 | { | |
354 | int aliasoffset; | |
355 | ||
356 | aliasoffset = fdt_path_offset(fdt, "/aliases"); | |
357 | if (aliasoffset < 0) | |
358 | return NULL; | |
359 | ||
360 | return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL); | |
361 | } | |
362 | ||
363 | const char *fdt_get_alias(const void *fdt, const char *name) | |
364 | { | |
365 | return fdt_get_alias_namelen(fdt, name, strlen(name)); | |
366 | } | |
367 | ||
8d04f02f | 368 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
3af0d587 | 369 | { |
ae0b5908 DG |
370 | int pdepth = 0, p = 0; |
371 | int offset, depth, namelen; | |
8d04f02f KG |
372 | const char *name; |
373 | ||
fc7758ee | 374 | FDT_CHECK_HEADER(fdt); |
8d04f02f | 375 | |
8d04f02f KG |
376 | if (buflen < 2) |
377 | return -FDT_ERR_NOSPACE; | |
8d04f02f | 378 | |
ae0b5908 DG |
379 | for (offset = 0, depth = 0; |
380 | (offset >= 0) && (offset <= nodeoffset); | |
381 | offset = fdt_next_node(fdt, offset, &depth)) { | |
ae0b5908 DG |
382 | while (pdepth > depth) { |
383 | do { | |
384 | p--; | |
385 | } while (buf[p-1] != '/'); | |
386 | pdepth--; | |
387 | } | |
388 | ||
bbdbc7cb DG |
389 | if (pdepth >= depth) { |
390 | name = fdt_get_name(fdt, offset, &namelen); | |
391 | if (!name) | |
392 | return namelen; | |
393 | if ((p + namelen + 1) <= buflen) { | |
394 | memcpy(buf + p, name, namelen); | |
395 | p += namelen; | |
396 | buf[p++] = '/'; | |
397 | pdepth++; | |
398 | } | |
ae0b5908 | 399 | } |
8d04f02f | 400 | |
ae0b5908 DG |
401 | if (offset == nodeoffset) { |
402 | if (pdepth < (depth + 1)) | |
403 | return -FDT_ERR_NOSPACE; | |
8d04f02f | 404 | |
ae0b5908 DG |
405 | if (p > 1) /* special case so that root path is "/", not "" */ |
406 | p--; | |
407 | buf[p] = '\0'; | |
bbdbc7cb | 408 | return 0; |
3af0d587 | 409 | } |
3af0d587 GVB |
410 | } |
411 | ||
ae0b5908 DG |
412 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
413 | return -FDT_ERR_BADOFFSET; | |
414 | else if (offset == -FDT_ERR_BADOFFSET) | |
415 | return -FDT_ERR_BADSTRUCTURE; | |
3af0d587 | 416 | |
ae0b5908 | 417 | return offset; /* error from fdt_next_node() */ |
35748177 | 418 | } |
3f9f08cf | 419 | |
8d04f02f KG |
420 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
421 | int supernodedepth, int *nodedepth) | |
422 | { | |
ae0b5908 | 423 | int offset, depth; |
8d04f02f KG |
424 | int supernodeoffset = -FDT_ERR_INTERNAL; |
425 | ||
fc7758ee | 426 | FDT_CHECK_HEADER(fdt); |
8d04f02f KG |
427 | |
428 | if (supernodedepth < 0) | |
429 | return -FDT_ERR_NOTFOUND; | |
430 | ||
ae0b5908 DG |
431 | for (offset = 0, depth = 0; |
432 | (offset >= 0) && (offset <= nodeoffset); | |
433 | offset = fdt_next_node(fdt, offset, &depth)) { | |
434 | if (depth == supernodedepth) | |
435 | supernodeoffset = offset; | |
8d04f02f | 436 | |
ae0b5908 DG |
437 | if (offset == nodeoffset) { |
438 | if (nodedepth) | |
439 | *nodedepth = depth; | |
8d04f02f | 440 | |
ae0b5908 DG |
441 | if (supernodedepth > depth) |
442 | return -FDT_ERR_NOTFOUND; | |
443 | else | |
444 | return supernodeoffset; | |
8d04f02f | 445 | } |
ae0b5908 | 446 | } |
8d04f02f | 447 | |
ae0b5908 DG |
448 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
449 | return -FDT_ERR_BADOFFSET; | |
450 | else if (offset == -FDT_ERR_BADOFFSET) | |
451 | return -FDT_ERR_BADSTRUCTURE; | |
8d04f02f | 452 | |
ae0b5908 | 453 | return offset; /* error from fdt_next_node() */ |
8d04f02f KG |
454 | } |
455 | ||
456 | int fdt_node_depth(const void *fdt, int nodeoffset) | |
3f9f08cf | 457 | { |
8d04f02f KG |
458 | int nodedepth; |
459 | int err; | |
460 | ||
461 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); | |
462 | if (err) | |
463 | return (err < 0) ? err : -FDT_ERR_INTERNAL; | |
464 | return nodedepth; | |
465 | } | |
466 | ||
467 | int fdt_parent_offset(const void *fdt, int nodeoffset) | |
468 | { | |
469 | int nodedepth = fdt_node_depth(fdt, nodeoffset); | |
470 | ||
471 | if (nodedepth < 0) | |
472 | return nodedepth; | |
473 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, | |
474 | nodedepth - 1, NULL); | |
475 | } | |
476 | ||
477 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, | |
478 | const char *propname, | |
479 | const void *propval, int proplen) | |
480 | { | |
ae0b5908 | 481 | int offset; |
8d04f02f KG |
482 | const void *val; |
483 | int len; | |
484 | ||
fc7758ee | 485 | FDT_CHECK_HEADER(fdt); |
8d04f02f | 486 | |
8d04f02f KG |
487 | /* FIXME: The algorithm here is pretty horrible: we scan each |
488 | * property of a node in fdt_getprop(), then if that didn't | |
489 | * find what we want, we scan over them again making our way | |
490 | * to the next node. Still it's the easiest to implement | |
491 | * approach; performance can come later. */ | |
ae0b5908 DG |
492 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
493 | offset >= 0; | |
494 | offset = fdt_next_node(fdt, offset, NULL)) { | |
495 | val = fdt_getprop(fdt, offset, propname, &len); | |
496 | if (val && (len == proplen) | |
497 | && (memcmp(val, propval, len) == 0)) | |
498 | return offset; | |
499 | } | |
8d04f02f | 500 | |
ae0b5908 | 501 | return offset; /* error from fdt_next_node() */ |
8d04f02f KG |
502 | } |
503 | ||
504 | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) | |
505 | { | |
05a22ba0 DG |
506 | int offset; |
507 | ||
8d04f02f KG |
508 | if ((phandle == 0) || (phandle == -1)) |
509 | return -FDT_ERR_BADPHANDLE; | |
05a22ba0 DG |
510 | |
511 | FDT_CHECK_HEADER(fdt); | |
512 | ||
513 | /* FIXME: The algorithm here is pretty horrible: we | |
514 | * potentially scan each property of a node in | |
515 | * fdt_get_phandle(), then if that didn't find what | |
516 | * we want, we scan over them again making our way to the next | |
517 | * node. Still it's the easiest to implement approach; | |
518 | * performance can come later. */ | |
519 | for (offset = fdt_next_node(fdt, -1, NULL); | |
520 | offset >= 0; | |
521 | offset = fdt_next_node(fdt, offset, NULL)) { | |
522 | if (fdt_get_phandle(fdt, offset) == phandle) | |
523 | return offset; | |
524 | } | |
525 | ||
526 | return offset; /* error from fdt_next_node() */ | |
8d04f02f KG |
527 | } |
528 | ||
e853b324 | 529 | int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) |
8d04f02f KG |
530 | { |
531 | int len = strlen(str); | |
ef4e8ce1 | 532 | const char *p; |
8d04f02f KG |
533 | |
534 | while (listlen >= len) { | |
535 | if (memcmp(str, strlist, len+1) == 0) | |
536 | return 1; | |
537 | p = memchr(strlist, '\0', listlen); | |
538 | if (!p) | |
539 | return 0; /* malformed strlist.. */ | |
540 | listlen -= (p-strlist) + 1; | |
541 | strlist = p + 1; | |
3f9f08cf GVB |
542 | } |
543 | return 0; | |
544 | } | |
545 | ||
b02e4044 | 546 | int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property) |
bc4147ab | 547 | { |
b02e4044 SG |
548 | const char *list, *end; |
549 | int length, count = 0; | |
bc4147ab | 550 | |
b02e4044 | 551 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
bc4147ab | 552 | if (!list) |
7c9786d6 | 553 | return length; |
b02e4044 SG |
554 | |
555 | end = list + length; | |
556 | ||
557 | while (list < end) { | |
558 | length = strnlen(list, end - list) + 1; | |
bc4147ab | 559 | |
b02e4044 SG |
560 | /* Abort if the last string isn't properly NUL-terminated. */ |
561 | if (list + length > end) | |
562 | return -FDT_ERR_BADVALUE; | |
bc4147ab | 563 | |
b02e4044 | 564 | list += length; |
bc4147ab TR |
565 | count++; |
566 | } | |
567 | ||
568 | return count; | |
569 | } | |
570 | ||
b02e4044 SG |
571 | int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property, |
572 | const char *string) | |
fc503c17 | 573 | { |
b02e4044 | 574 | int length, len, idx = 0; |
fc503c17 | 575 | const char *list, *end; |
fc503c17 | 576 | |
b02e4044 | 577 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
fc503c17 | 578 | if (!list) |
01ae56cf | 579 | return length; |
fc503c17 | 580 | |
b02e4044 SG |
581 | len = strlen(string) + 1; |
582 | end = list + length; | |
fc503c17 TR |
583 | |
584 | while (list < end) { | |
b02e4044 SG |
585 | length = strnlen(list, end - list) + 1; |
586 | ||
587 | /* Abort if the last string isn't properly NUL-terminated. */ | |
588 | if (list + length > end) | |
589 | return -FDT_ERR_BADVALUE; | |
fc503c17 | 590 | |
b02e4044 SG |
591 | if (length == len && memcmp(list, string, length) == 0) |
592 | return idx; | |
fc503c17 | 593 | |
b02e4044 SG |
594 | list += length; |
595 | idx++; | |
fc503c17 TR |
596 | } |
597 | ||
598 | return -FDT_ERR_NOTFOUND; | |
599 | } | |
600 | ||
b02e4044 SG |
601 | const char *fdt_stringlist_get(const void *fdt, int nodeoffset, |
602 | const char *property, int idx, | |
603 | int *lenp) | |
5094eb40 | 604 | { |
b02e4044 SG |
605 | const char *list, *end; |
606 | int length; | |
5094eb40 | 607 | |
b02e4044 SG |
608 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
609 | if (!list) { | |
610 | if (lenp) | |
611 | *lenp = length; | |
5094eb40 | 612 | |
b02e4044 SG |
613 | return NULL; |
614 | } | |
5094eb40 | 615 | |
b02e4044 SG |
616 | end = list + length; |
617 | ||
618 | while (list < end) { | |
619 | length = strnlen(list, end - list) + 1; | |
620 | ||
621 | /* Abort if the last string isn't properly NUL-terminated. */ | |
622 | if (list + length > end) { | |
623 | if (lenp) | |
624 | *lenp = -FDT_ERR_BADVALUE; | |
625 | ||
626 | return NULL; | |
627 | } | |
628 | ||
629 | if (idx == 0) { | |
630 | if (lenp) | |
631 | *lenp = length - 1; | |
632 | ||
633 | return list; | |
5094eb40 TR |
634 | } |
635 | ||
b02e4044 SG |
636 | list += length; |
637 | idx--; | |
5094eb40 TR |
638 | } |
639 | ||
b02e4044 SG |
640 | if (lenp) |
641 | *lenp = -FDT_ERR_NOTFOUND; | |
5094eb40 | 642 | |
b02e4044 | 643 | return NULL; |
5094eb40 TR |
644 | } |
645 | ||
8d04f02f KG |
646 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
647 | const char *compatible) | |
3f9f08cf | 648 | { |
8d04f02f KG |
649 | const void *prop; |
650 | int len; | |
3f9f08cf | 651 | |
8d04f02f KG |
652 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
653 | if (!prop) | |
654 | return len; | |
9c07b987 SG |
655 | |
656 | return !fdt_stringlist_contains(prop, len, compatible); | |
8d04f02f | 657 | } |
3f9f08cf | 658 | |
8d04f02f KG |
659 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
660 | const char *compatible) | |
661 | { | |
11abe45c | 662 | int offset, err; |
8d04f02f | 663 | |
fc7758ee | 664 | FDT_CHECK_HEADER(fdt); |
8d04f02f | 665 | |
8d04f02f KG |
666 | /* FIXME: The algorithm here is pretty horrible: we scan each |
667 | * property of a node in fdt_node_check_compatible(), then if | |
668 | * that didn't find what we want, we scan over them again | |
669 | * making our way to the next node. Still it's the easiest to | |
670 | * implement approach; performance can come later. */ | |
ae0b5908 DG |
671 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
672 | offset >= 0; | |
673 | offset = fdt_next_node(fdt, offset, NULL)) { | |
674 | err = fdt_node_check_compatible(fdt, offset, compatible); | |
675 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) | |
676 | return err; | |
677 | else if (err == 0) | |
678 | return offset; | |
679 | } | |
8d04f02f | 680 | |
ae0b5908 | 681 | return offset; /* error from fdt_next_node() */ |
3f9f08cf | 682 | } |