1 | /* |
---|
2 | * libfdt - Flat Device Tree manipulation |
---|
3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
---|
4 | * |
---|
5 | * libfdt is dual licensed: you can use it either under the terms of |
---|
6 | * the GPL, or the BSD license, at your option. |
---|
7 | * |
---|
8 | * a) This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License as |
---|
10 | * published by the Free Software Foundation; either version 2 of the |
---|
11 | * License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This library is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public |
---|
19 | * License along with this library; if not, write to the Free |
---|
20 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
---|
21 | * MA 02110-1301 USA |
---|
22 | * |
---|
23 | * Alternatively, |
---|
24 | * |
---|
25 | * b) Redistribution and use in source and binary forms, with or |
---|
26 | * without modification, are permitted provided that the following |
---|
27 | * conditions are met: |
---|
28 | * |
---|
29 | * 1. Redistributions of source code must retain the above |
---|
30 | * copyright notice, this list of conditions and the following |
---|
31 | * disclaimer. |
---|
32 | * 2. Redistributions in binary form must reproduce the above |
---|
33 | * copyright notice, this list of conditions and the following |
---|
34 | * disclaimer in the documentation and/or other materials |
---|
35 | * provided with the distribution. |
---|
36 | * |
---|
37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
---|
38 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
---|
39 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
---|
40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
41 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
42 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
---|
48 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
---|
49 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
50 | */ |
---|
51 | #include "libfdt_env.h" |
---|
52 | |
---|
53 | #include <fdt.h> |
---|
54 | #include <libfdt.h> |
---|
55 | |
---|
56 | #include "libfdt_internal.h" |
---|
57 | |
---|
58 | static int fdt_nodename_eq_(const void *fdt, int offset, |
---|
59 | const char *s, int len) |
---|
60 | { |
---|
61 | int olen; |
---|
62 | const char *p = fdt_get_name(fdt, offset, &olen); |
---|
63 | |
---|
64 | if (!p || olen < len) |
---|
65 | /* short match */ |
---|
66 | return 0; |
---|
67 | |
---|
68 | if (memcmp(p, s, len) != 0) |
---|
69 | return 0; |
---|
70 | |
---|
71 | if (p[len] == '\0') |
---|
72 | return 1; |
---|
73 | else if (!memchr(s, '@', len) && (p[len] == '@')) |
---|
74 | return 1; |
---|
75 | else |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) |
---|
80 | { |
---|
81 | uint32_t absoffset = stroffset + fdt_off_dt_strings(fdt); |
---|
82 | size_t len; |
---|
83 | int err; |
---|
84 | const char *s, *n; |
---|
85 | |
---|
86 | err = fdt_ro_probe_(fdt); |
---|
87 | if (err != 0) |
---|
88 | goto fail; |
---|
89 | |
---|
90 | err = -FDT_ERR_BADOFFSET; |
---|
91 | if (absoffset >= fdt_totalsize(fdt)) |
---|
92 | goto fail; |
---|
93 | len = fdt_totalsize(fdt) - absoffset; |
---|
94 | |
---|
95 | if (fdt_magic(fdt) == FDT_MAGIC) { |
---|
96 | if (stroffset < 0) |
---|
97 | goto fail; |
---|
98 | if (fdt_version(fdt) >= 17) { |
---|
99 | if (stroffset >= fdt_size_dt_strings(fdt)) |
---|
100 | goto fail; |
---|
101 | if ((fdt_size_dt_strings(fdt) - stroffset) < len) |
---|
102 | len = fdt_size_dt_strings(fdt) - stroffset; |
---|
103 | } |
---|
104 | } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { |
---|
105 | if ((stroffset >= 0) |
---|
106 | || (stroffset < -fdt_size_dt_strings(fdt))) |
---|
107 | goto fail; |
---|
108 | if ((-stroffset) < len) |
---|
109 | len = -stroffset; |
---|
110 | } else { |
---|
111 | err = -FDT_ERR_INTERNAL; |
---|
112 | goto fail; |
---|
113 | } |
---|
114 | |
---|
115 | s = (const char *)fdt + absoffset; |
---|
116 | n = memchr(s, '\0', len); |
---|
117 | if (!n) { |
---|
118 | /* missing terminating NULL */ |
---|
119 | err = -FDT_ERR_TRUNCATED; |
---|
120 | goto fail; |
---|
121 | } |
---|
122 | |
---|
123 | if (lenp) |
---|
124 | *lenp = n - s; |
---|
125 | return s; |
---|
126 | |
---|
127 | fail: |
---|
128 | if (lenp) |
---|
129 | *lenp = err; |
---|
130 | return NULL; |
---|
131 | } |
---|
132 | |
---|
133 | const char *fdt_string(const void *fdt, int stroffset) |
---|
134 | { |
---|
135 | return fdt_get_string(fdt, stroffset, NULL); |
---|
136 | } |
---|
137 | |
---|
138 | static int fdt_string_eq_(const void *fdt, int stroffset, |
---|
139 | const char *s, int len) |
---|
140 | { |
---|
141 | int slen; |
---|
142 | const char *p = fdt_get_string(fdt, stroffset, &slen); |
---|
143 | |
---|
144 | return p && (slen == len) && (memcmp(p, s, len) == 0); |
---|
145 | } |
---|
146 | |
---|
147 | uint32_t fdt_get_max_phandle(const void *fdt) |
---|
148 | { |
---|
149 | uint32_t max_phandle = 0; |
---|
150 | int offset; |
---|
151 | |
---|
152 | for (offset = fdt_next_node(fdt, -1, NULL);; |
---|
153 | offset = fdt_next_node(fdt, offset, NULL)) { |
---|
154 | uint32_t phandle; |
---|
155 | |
---|
156 | if (offset == -FDT_ERR_NOTFOUND) |
---|
157 | return max_phandle; |
---|
158 | |
---|
159 | if (offset < 0) |
---|
160 | return (uint32_t)-1; |
---|
161 | |
---|
162 | phandle = fdt_get_phandle(fdt, offset); |
---|
163 | if (phandle == (uint32_t)-1) |
---|
164 | continue; |
---|
165 | |
---|
166 | if (phandle > max_phandle) |
---|
167 | max_phandle = phandle; |
---|
168 | } |
---|
169 | |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) |
---|
174 | { |
---|
175 | FDT_RO_PROBE(fdt); |
---|
176 | *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address); |
---|
177 | *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size); |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | int fdt_num_mem_rsv(const void *fdt) |
---|
182 | { |
---|
183 | int i = 0; |
---|
184 | |
---|
185 | while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0) |
---|
186 | i++; |
---|
187 | return i; |
---|
188 | } |
---|
189 | |
---|
190 | static int nextprop_(const void *fdt, int offset) |
---|
191 | { |
---|
192 | uint32_t tag; |
---|
193 | int nextoffset; |
---|
194 | |
---|
195 | do { |
---|
196 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
---|
197 | |
---|
198 | switch (tag) { |
---|
199 | case FDT_END: |
---|
200 | if (nextoffset >= 0) |
---|
201 | return -FDT_ERR_BADSTRUCTURE; |
---|
202 | else |
---|
203 | return nextoffset; |
---|
204 | |
---|
205 | case FDT_PROP: |
---|
206 | return offset; |
---|
207 | } |
---|
208 | offset = nextoffset; |
---|
209 | } while (tag == FDT_NOP); |
---|
210 | |
---|
211 | return -FDT_ERR_NOTFOUND; |
---|
212 | } |
---|
213 | |
---|
214 | int fdt_subnode_offset_namelen(const void *fdt, int offset, |
---|
215 | const char *name, int namelen) |
---|
216 | { |
---|
217 | int depth; |
---|
218 | |
---|
219 | FDT_RO_PROBE(fdt); |
---|
220 | |
---|
221 | for (depth = 0; |
---|
222 | (offset >= 0) && (depth >= 0); |
---|
223 | offset = fdt_next_node(fdt, offset, &depth)) |
---|
224 | if ((depth == 1) |
---|
225 | && fdt_nodename_eq_(fdt, offset, name, namelen)) |
---|
226 | return offset; |
---|
227 | |
---|
228 | if (depth < 0) |
---|
229 | return -FDT_ERR_NOTFOUND; |
---|
230 | return offset; /* error */ |
---|
231 | } |
---|
232 | |
---|
233 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
---|
234 | const char *name) |
---|
235 | { |
---|
236 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
---|
237 | } |
---|
238 | |
---|
239 | int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) |
---|
240 | { |
---|
241 | const char *end = path + namelen; |
---|
242 | const char *p = path; |
---|
243 | int offset = 0; |
---|
244 | |
---|
245 | FDT_RO_PROBE(fdt); |
---|
246 | |
---|
247 | /* see if we have an alias */ |
---|
248 | if (*path != '/') { |
---|
249 | const char *q = memchr(path, '/', end - p); |
---|
250 | |
---|
251 | if (!q) |
---|
252 | q = end; |
---|
253 | |
---|
254 | p = fdt_get_alias_namelen(fdt, p, q - p); |
---|
255 | if (!p) |
---|
256 | return -FDT_ERR_BADPATH; |
---|
257 | offset = fdt_path_offset(fdt, p); |
---|
258 | |
---|
259 | p = q; |
---|
260 | } |
---|
261 | |
---|
262 | while (p < end) { |
---|
263 | const char *q; |
---|
264 | |
---|
265 | while (*p == '/') { |
---|
266 | p++; |
---|
267 | if (p == end) |
---|
268 | return offset; |
---|
269 | } |
---|
270 | q = memchr(p, '/', end - p); |
---|
271 | if (! q) |
---|
272 | q = end; |
---|
273 | |
---|
274 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
---|
275 | if (offset < 0) |
---|
276 | return offset; |
---|
277 | |
---|
278 | p = q; |
---|
279 | } |
---|
280 | |
---|
281 | return offset; |
---|
282 | } |
---|
283 | |
---|
284 | int fdt_path_offset(const void *fdt, const char *path) |
---|
285 | { |
---|
286 | return fdt_path_offset_namelen(fdt, path, strlen(path)); |
---|
287 | } |
---|
288 | |
---|
289 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) |
---|
290 | { |
---|
291 | const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset); |
---|
292 | const char *nameptr; |
---|
293 | int err; |
---|
294 | |
---|
295 | if (((err = fdt_ro_probe_(fdt)) != 0) |
---|
296 | || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)) |
---|
297 | goto fail; |
---|
298 | |
---|
299 | nameptr = nh->name; |
---|
300 | |
---|
301 | if (fdt_version(fdt) < 0x10) { |
---|
302 | /* |
---|
303 | * For old FDT versions, match the naming conventions of V16: |
---|
304 | * give only the leaf name (after all /). The actual tree |
---|
305 | * contents are loosely checked. |
---|
306 | */ |
---|
307 | const char *leaf; |
---|
308 | leaf = strrchr(nameptr, '/'); |
---|
309 | if (leaf == NULL) { |
---|
310 | err = -FDT_ERR_BADSTRUCTURE; |
---|
311 | goto fail; |
---|
312 | } |
---|
313 | nameptr = leaf+1; |
---|
314 | } |
---|
315 | |
---|
316 | if (len) |
---|
317 | *len = strlen(nameptr); |
---|
318 | |
---|
319 | return nameptr; |
---|
320 | |
---|
321 | fail: |
---|
322 | if (len) |
---|
323 | *len = err; |
---|
324 | return NULL; |
---|
325 | } |
---|
326 | |
---|
327 | int fdt_first_property_offset(const void *fdt, int nodeoffset) |
---|
328 | { |
---|
329 | int offset; |
---|
330 | |
---|
331 | if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0) |
---|
332 | return offset; |
---|
333 | |
---|
334 | return nextprop_(fdt, offset); |
---|
335 | } |
---|
336 | |
---|
337 | int fdt_next_property_offset(const void *fdt, int offset) |
---|
338 | { |
---|
339 | if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0) |
---|
340 | return offset; |
---|
341 | |
---|
342 | return nextprop_(fdt, offset); |
---|
343 | } |
---|
344 | |
---|
345 | static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt, |
---|
346 | int offset, |
---|
347 | int *lenp) |
---|
348 | { |
---|
349 | int err; |
---|
350 | const struct fdt_property *prop; |
---|
351 | |
---|
352 | if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) { |
---|
353 | if (lenp) |
---|
354 | *lenp = err; |
---|
355 | return NULL; |
---|
356 | } |
---|
357 | |
---|
358 | prop = fdt_offset_ptr_(fdt, offset); |
---|
359 | |
---|
360 | if (lenp) |
---|
361 | *lenp = fdt32_to_cpu(prop->len); |
---|
362 | |
---|
363 | return prop; |
---|
364 | } |
---|
365 | |
---|
366 | const struct fdt_property *fdt_get_property_by_offset(const void *fdt, |
---|
367 | int offset, |
---|
368 | int *lenp) |
---|
369 | { |
---|
370 | /* Prior to version 16, properties may need realignment |
---|
371 | * and this API does not work. fdt_getprop_*() will, however. */ |
---|
372 | |
---|
373 | if (fdt_version(fdt) < 0x10) { |
---|
374 | if (lenp) |
---|
375 | *lenp = -FDT_ERR_BADVERSION; |
---|
376 | return NULL; |
---|
377 | } |
---|
378 | |
---|
379 | return fdt_get_property_by_offset_(fdt, offset, lenp); |
---|
380 | } |
---|
381 | |
---|
382 | static const struct fdt_property *fdt_get_property_namelen_(const void *fdt, |
---|
383 | int offset, |
---|
384 | const char *name, |
---|
385 | int namelen, |
---|
386 | int *lenp, |
---|
387 | int *poffset) |
---|
388 | { |
---|
389 | for (offset = fdt_first_property_offset(fdt, offset); |
---|
390 | (offset >= 0); |
---|
391 | (offset = fdt_next_property_offset(fdt, offset))) { |
---|
392 | const struct fdt_property *prop; |
---|
393 | |
---|
394 | if (!(prop = fdt_get_property_by_offset_(fdt, offset, lenp))) { |
---|
395 | offset = -FDT_ERR_INTERNAL; |
---|
396 | break; |
---|
397 | } |
---|
398 | if (fdt_string_eq_(fdt, fdt32_to_cpu(prop->nameoff), |
---|
399 | name, namelen)) { |
---|
400 | if (poffset) |
---|
401 | *poffset = offset; |
---|
402 | return prop; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | if (lenp) |
---|
407 | *lenp = offset; |
---|
408 | return NULL; |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | const struct fdt_property *fdt_get_property_namelen(const void *fdt, |
---|
413 | int offset, |
---|
414 | const char *name, |
---|
415 | int namelen, int *lenp) |
---|
416 | { |
---|
417 | /* Prior to version 16, properties may need realignment |
---|
418 | * and this API does not work. fdt_getprop_*() will, however. */ |
---|
419 | if (fdt_version(fdt) < 0x10) { |
---|
420 | if (lenp) |
---|
421 | *lenp = -FDT_ERR_BADVERSION; |
---|
422 | return NULL; |
---|
423 | } |
---|
424 | |
---|
425 | return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp, |
---|
426 | NULL); |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | const struct fdt_property *fdt_get_property(const void *fdt, |
---|
431 | int nodeoffset, |
---|
432 | const char *name, int *lenp) |
---|
433 | { |
---|
434 | return fdt_get_property_namelen(fdt, nodeoffset, name, |
---|
435 | strlen(name), lenp); |
---|
436 | } |
---|
437 | |
---|
438 | const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, |
---|
439 | const char *name, int namelen, int *lenp) |
---|
440 | { |
---|
441 | int poffset; |
---|
442 | const struct fdt_property *prop; |
---|
443 | |
---|
444 | prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp, |
---|
445 | &poffset); |
---|
446 | if (!prop) |
---|
447 | return NULL; |
---|
448 | |
---|
449 | /* Handle realignment */ |
---|
450 | if (fdt_version(fdt) < 0x10 && (poffset + sizeof(*prop)) % 8 && |
---|
451 | fdt32_to_cpu(prop->len) >= 8) |
---|
452 | return prop->data + 4; |
---|
453 | return prop->data; |
---|
454 | } |
---|
455 | |
---|
456 | const void *fdt_getprop_by_offset(const void *fdt, int offset, |
---|
457 | const char **namep, int *lenp) |
---|
458 | { |
---|
459 | const struct fdt_property *prop; |
---|
460 | |
---|
461 | prop = fdt_get_property_by_offset_(fdt, offset, lenp); |
---|
462 | if (!prop) |
---|
463 | return NULL; |
---|
464 | if (namep) { |
---|
465 | const char *name; |
---|
466 | int namelen; |
---|
467 | name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff), |
---|
468 | &namelen); |
---|
469 | if (!name) { |
---|
470 | if (lenp) |
---|
471 | *lenp = namelen; |
---|
472 | return NULL; |
---|
473 | } |
---|
474 | *namep = name; |
---|
475 | } |
---|
476 | |
---|
477 | /* Handle realignment */ |
---|
478 | if (fdt_version(fdt) < 0x10 && (offset + sizeof(*prop)) % 8 && |
---|
479 | fdt32_to_cpu(prop->len) >= 8) |
---|
480 | return prop->data + 4; |
---|
481 | return prop->data; |
---|
482 | } |
---|
483 | |
---|
484 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
---|
485 | const char *name, int *lenp) |
---|
486 | { |
---|
487 | return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); |
---|
488 | } |
---|
489 | |
---|
490 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
---|
491 | { |
---|
492 | const fdt32_t *php; |
---|
493 | int len; |
---|
494 | |
---|
495 | /* FIXME: This is a bit sub-optimal, since we potentially scan |
---|
496 | * over all the properties twice. */ |
---|
497 | php = fdt_getprop(fdt, nodeoffset, "phandle", &len); |
---|
498 | if (!php || (len != sizeof(*php))) { |
---|
499 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); |
---|
500 | if (!php || (len != sizeof(*php))) |
---|
501 | return 0; |
---|
502 | } |
---|
503 | |
---|
504 | return fdt32_to_cpu(*php); |
---|
505 | } |
---|
506 | |
---|
507 | const char *fdt_get_alias_namelen(const void *fdt, |
---|
508 | const char *name, int namelen) |
---|
509 | { |
---|
510 | int aliasoffset; |
---|
511 | |
---|
512 | aliasoffset = fdt_path_offset(fdt, "/aliases"); |
---|
513 | if (aliasoffset < 0) |
---|
514 | return NULL; |
---|
515 | |
---|
516 | return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL); |
---|
517 | } |
---|
518 | |
---|
519 | const char *fdt_get_alias(const void *fdt, const char *name) |
---|
520 | { |
---|
521 | return fdt_get_alias_namelen(fdt, name, strlen(name)); |
---|
522 | } |
---|
523 | |
---|
524 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
---|
525 | { |
---|
526 | int pdepth = 0, p = 0; |
---|
527 | int offset, depth, namelen; |
---|
528 | const char *name; |
---|
529 | |
---|
530 | FDT_RO_PROBE(fdt); |
---|
531 | |
---|
532 | if (buflen < 2) |
---|
533 | return -FDT_ERR_NOSPACE; |
---|
534 | |
---|
535 | for (offset = 0, depth = 0; |
---|
536 | (offset >= 0) && (offset <= nodeoffset); |
---|
537 | offset = fdt_next_node(fdt, offset, &depth)) { |
---|
538 | while (pdepth > depth) { |
---|
539 | do { |
---|
540 | p--; |
---|
541 | } while (buf[p-1] != '/'); |
---|
542 | pdepth--; |
---|
543 | } |
---|
544 | |
---|
545 | if (pdepth >= depth) { |
---|
546 | name = fdt_get_name(fdt, offset, &namelen); |
---|
547 | if (!name) |
---|
548 | return namelen; |
---|
549 | if ((p + namelen + 1) <= buflen) { |
---|
550 | memcpy(buf + p, name, namelen); |
---|
551 | p += namelen; |
---|
552 | buf[p++] = '/'; |
---|
553 | pdepth++; |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | if (offset == nodeoffset) { |
---|
558 | if (pdepth < (depth + 1)) |
---|
559 | return -FDT_ERR_NOSPACE; |
---|
560 | |
---|
561 | if (p > 1) /* special case so that root path is "/", not "" */ |
---|
562 | p--; |
---|
563 | buf[p] = '\0'; |
---|
564 | return 0; |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
---|
569 | return -FDT_ERR_BADOFFSET; |
---|
570 | else if (offset == -FDT_ERR_BADOFFSET) |
---|
571 | return -FDT_ERR_BADSTRUCTURE; |
---|
572 | |
---|
573 | return offset; /* error from fdt_next_node() */ |
---|
574 | } |
---|
575 | |
---|
576 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
---|
577 | int supernodedepth, int *nodedepth) |
---|
578 | { |
---|
579 | int offset, depth; |
---|
580 | int supernodeoffset = -FDT_ERR_INTERNAL; |
---|
581 | |
---|
582 | FDT_RO_PROBE(fdt); |
---|
583 | |
---|
584 | if (supernodedepth < 0) |
---|
585 | return -FDT_ERR_NOTFOUND; |
---|
586 | |
---|
587 | for (offset = 0, depth = 0; |
---|
588 | (offset >= 0) && (offset <= nodeoffset); |
---|
589 | offset = fdt_next_node(fdt, offset, &depth)) { |
---|
590 | if (depth == supernodedepth) |
---|
591 | supernodeoffset = offset; |
---|
592 | |
---|
593 | if (offset == nodeoffset) { |
---|
594 | if (nodedepth) |
---|
595 | *nodedepth = depth; |
---|
596 | |
---|
597 | if (supernodedepth > depth) |
---|
598 | return -FDT_ERR_NOTFOUND; |
---|
599 | else |
---|
600 | return supernodeoffset; |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
---|
605 | return -FDT_ERR_BADOFFSET; |
---|
606 | else if (offset == -FDT_ERR_BADOFFSET) |
---|
607 | return -FDT_ERR_BADSTRUCTURE; |
---|
608 | |
---|
609 | return offset; /* error from fdt_next_node() */ |
---|
610 | } |
---|
611 | |
---|
612 | int fdt_node_depth(const void *fdt, int nodeoffset) |
---|
613 | { |
---|
614 | int nodedepth; |
---|
615 | int err; |
---|
616 | |
---|
617 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
---|
618 | if (err) |
---|
619 | return (err < 0) ? err : -FDT_ERR_INTERNAL; |
---|
620 | return nodedepth; |
---|
621 | } |
---|
622 | |
---|
623 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
---|
624 | { |
---|
625 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
---|
626 | |
---|
627 | if (nodedepth < 0) |
---|
628 | return nodedepth; |
---|
629 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
---|
630 | nodedepth - 1, NULL); |
---|
631 | } |
---|
632 | |
---|
633 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
---|
634 | const char *propname, |
---|
635 | const void *propval, int proplen) |
---|
636 | { |
---|
637 | int offset; |
---|
638 | const void *val; |
---|
639 | int len; |
---|
640 | |
---|
641 | FDT_RO_PROBE(fdt); |
---|
642 | |
---|
643 | /* FIXME: The algorithm here is pretty horrible: we scan each |
---|
644 | * property of a node in fdt_getprop(), then if that didn't |
---|
645 | * find what we want, we scan over them again making our way |
---|
646 | * to the next node. Still it's the easiest to implement |
---|
647 | * approach; performance can come later. */ |
---|
648 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
---|
649 | offset >= 0; |
---|
650 | offset = fdt_next_node(fdt, offset, NULL)) { |
---|
651 | val = fdt_getprop(fdt, offset, propname, &len); |
---|
652 | if (val && (len == proplen) |
---|
653 | && (memcmp(val, propval, len) == 0)) |
---|
654 | return offset; |
---|
655 | } |
---|
656 | |
---|
657 | return offset; /* error from fdt_next_node() */ |
---|
658 | } |
---|
659 | |
---|
660 | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) |
---|
661 | { |
---|
662 | int offset; |
---|
663 | |
---|
664 | if ((phandle == 0) || (phandle == -1)) |
---|
665 | return -FDT_ERR_BADPHANDLE; |
---|
666 | |
---|
667 | FDT_RO_PROBE(fdt); |
---|
668 | |
---|
669 | /* FIXME: The algorithm here is pretty horrible: we |
---|
670 | * potentially scan each property of a node in |
---|
671 | * fdt_get_phandle(), then if that didn't find what |
---|
672 | * we want, we scan over them again making our way to the next |
---|
673 | * node. Still it's the easiest to implement approach; |
---|
674 | * performance can come later. */ |
---|
675 | for (offset = fdt_next_node(fdt, -1, NULL); |
---|
676 | offset >= 0; |
---|
677 | offset = fdt_next_node(fdt, offset, NULL)) { |
---|
678 | if (fdt_get_phandle(fdt, offset) == phandle) |
---|
679 | return offset; |
---|
680 | } |
---|
681 | |
---|
682 | return offset; /* error from fdt_next_node() */ |
---|
683 | } |
---|
684 | |
---|
685 | int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) |
---|
686 | { |
---|
687 | int len = strlen(str); |
---|
688 | const char *p; |
---|
689 | |
---|
690 | while (listlen >= len) { |
---|
691 | if (memcmp(str, strlist, len+1) == 0) |
---|
692 | return 1; |
---|
693 | p = memchr(strlist, '\0', listlen); |
---|
694 | if (!p) |
---|
695 | return 0; /* malformed strlist.. */ |
---|
696 | listlen -= (p-strlist) + 1; |
---|
697 | strlist = p + 1; |
---|
698 | } |
---|
699 | return 0; |
---|
700 | } |
---|
701 | |
---|
702 | int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property) |
---|
703 | { |
---|
704 | const char *list, *end; |
---|
705 | int length, count = 0; |
---|
706 | |
---|
707 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
---|
708 | if (!list) |
---|
709 | return length; |
---|
710 | |
---|
711 | end = list + length; |
---|
712 | |
---|
713 | while (list < end) { |
---|
714 | length = strnlen(list, end - list) + 1; |
---|
715 | |
---|
716 | /* Abort if the last string isn't properly NUL-terminated. */ |
---|
717 | if (list + length > end) |
---|
718 | return -FDT_ERR_BADVALUE; |
---|
719 | |
---|
720 | list += length; |
---|
721 | count++; |
---|
722 | } |
---|
723 | |
---|
724 | return count; |
---|
725 | } |
---|
726 | |
---|
727 | int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property, |
---|
728 | const char *string) |
---|
729 | { |
---|
730 | int length, len, idx = 0; |
---|
731 | const char *list, *end; |
---|
732 | |
---|
733 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
---|
734 | if (!list) |
---|
735 | return length; |
---|
736 | |
---|
737 | len = strlen(string) + 1; |
---|
738 | end = list + length; |
---|
739 | |
---|
740 | while (list < end) { |
---|
741 | length = strnlen(list, end - list) + 1; |
---|
742 | |
---|
743 | /* Abort if the last string isn't properly NUL-terminated. */ |
---|
744 | if (list + length > end) |
---|
745 | return -FDT_ERR_BADVALUE; |
---|
746 | |
---|
747 | if (length == len && memcmp(list, string, length) == 0) |
---|
748 | return idx; |
---|
749 | |
---|
750 | list += length; |
---|
751 | idx++; |
---|
752 | } |
---|
753 | |
---|
754 | return -FDT_ERR_NOTFOUND; |
---|
755 | } |
---|
756 | |
---|
757 | const char *fdt_stringlist_get(const void *fdt, int nodeoffset, |
---|
758 | const char *property, int idx, |
---|
759 | int *lenp) |
---|
760 | { |
---|
761 | const char *list, *end; |
---|
762 | int length; |
---|
763 | |
---|
764 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
---|
765 | if (!list) { |
---|
766 | if (lenp) |
---|
767 | *lenp = length; |
---|
768 | |
---|
769 | return NULL; |
---|
770 | } |
---|
771 | |
---|
772 | end = list + length; |
---|
773 | |
---|
774 | while (list < end) { |
---|
775 | length = strnlen(list, end - list) + 1; |
---|
776 | |
---|
777 | /* Abort if the last string isn't properly NUL-terminated. */ |
---|
778 | if (list + length > end) { |
---|
779 | if (lenp) |
---|
780 | *lenp = -FDT_ERR_BADVALUE; |
---|
781 | |
---|
782 | return NULL; |
---|
783 | } |
---|
784 | |
---|
785 | if (idx == 0) { |
---|
786 | if (lenp) |
---|
787 | *lenp = length - 1; |
---|
788 | |
---|
789 | return list; |
---|
790 | } |
---|
791 | |
---|
792 | list += length; |
---|
793 | idx--; |
---|
794 | } |
---|
795 | |
---|
796 | if (lenp) |
---|
797 | *lenp = -FDT_ERR_NOTFOUND; |
---|
798 | |
---|
799 | return NULL; |
---|
800 | } |
---|
801 | |
---|
802 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
---|
803 | const char *compatible) |
---|
804 | { |
---|
805 | const void *prop; |
---|
806 | int len; |
---|
807 | |
---|
808 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
---|
809 | if (!prop) |
---|
810 | return len; |
---|
811 | |
---|
812 | return !fdt_stringlist_contains(prop, len, compatible); |
---|
813 | } |
---|
814 | |
---|
815 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
---|
816 | const char *compatible) |
---|
817 | { |
---|
818 | int offset, err; |
---|
819 | |
---|
820 | FDT_RO_PROBE(fdt); |
---|
821 | |
---|
822 | /* FIXME: The algorithm here is pretty horrible: we scan each |
---|
823 | * property of a node in fdt_node_check_compatible(), then if |
---|
824 | * that didn't find what we want, we scan over them again |
---|
825 | * making our way to the next node. Still it's the easiest to |
---|
826 | * implement approach; performance can come later. */ |
---|
827 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
---|
828 | offset >= 0; |
---|
829 | offset = fdt_next_node(fdt, offset, NULL)) { |
---|
830 | err = fdt_node_check_compatible(fdt, offset, compatible); |
---|
831 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) |
---|
832 | return err; |
---|
833 | else if (err == 0) |
---|
834 | return offset; |
---|
835 | } |
---|
836 | |
---|
837 | return offset; /* error from fdt_next_node() */ |
---|
838 | } |
---|