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