1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "libfdt.h"
7 #include "dtb_node.h"
8
9 /* "/aliaes" node */
10 static struct dtb_node *fdt_aliases;
11
12 /**
13 * of_find_property_value_of_size() - find property of given size
14 *
15 * Search for a property in a device node and validate the requested size.
16 *
17 * @np: device node from which the property value is to be read.
18 * @propname: name of the property to be searched.
19 * @len: requested length of property value
20 *
21 * @return the property value on success, -EINVAL if the property does not
22 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
23 * property data isn't large enough.
24 */
dtb_node_find_property_value_of_size(const struct dtb_node * dn,const char * propname,uint32_t len)25 static void *dtb_node_find_property_value_of_size(const struct dtb_node *dn,
26 const char *propname, uint32_t len)
27 {
28 struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
29
30 if (!prop)
31 return DTB_ERR_PTR(-EINVAL);
32 if (!prop->value)
33 return DTB_ERR_PTR(-ENODATA);
34 if (len > prop->size)
35 return DTB_ERR_PTR(-EOVERFLOW);
36 return prop->value;
37 }
38
dtb_node_read_u32(const struct dtb_node * dn,const char * propname,uint32_t * outp)39 int dtb_node_read_u32(const struct dtb_node *dn, const char *propname, uint32_t *outp)
40 {
41 const uint32_t *val;
42
43 debug("%s: %s: \n", __func__, propname);
44 if (!dn)
45 return -EINVAL;
46 val = dtb_node_find_property_value_of_size(dn, propname, sizeof(*outp));
47 if (DTB_IS_ERR(val))
48 {
49 debug("(not found)\n");
50 return DTB_PTR_ERR(val);
51 }
52
53 *outp = fdt32_to_cpu(*val);
54 debug("%#x (%d)\n", *outp, *outp);
55
56 return 0;
57 }
58
dtb_node_read_u32_default(const struct dtb_node * node,const char * propname,uint32_t def)59 uint32_t dtb_node_read_u32_default(const struct dtb_node *node, const char *propname, uint32_t def)
60 {
61 dtb_node_read_u32(node, propname, &def);
62
63 return def;
64 }
65
dtb_node_read_u32_array(const struct dtb_node * dn,const char * propname,uint32_t * out_values,size_t sz)66 int dtb_node_read_u32_array(const struct dtb_node *dn, const char *propname,
67 uint32_t *out_values, size_t sz)
68 {
69 const uint32_t *val;
70
71 debug("%s: %s: ", __func__, propname);
72 val = dtb_node_find_property_value_of_size(dn, propname,
73 sz * sizeof(*out_values));
74
75 if (DTB_IS_ERR(val))
76 return DTB_PTR_ERR(val);
77
78 debug("size %zd, val:%d\n", sz, *val);
79 while (sz--)
80 *out_values++ = fdt32_to_cpu(*val++);
81
82 return 0;
83 }
84
dtb_node_read_u32_index_default(const struct dtb_node * node,const char * propname,int index,uint32_t def)85 uint32_t dtb_node_read_u32_index_default(const struct dtb_node *node, const char *propname, int index,
86 uint32_t def)
87 {
88 RT_ASSERT(dtb_node_valid(node));
89 dtb_node_read_u32_index(node, propname, index, &def);
90
91 return def;
92 }
93
dtb_node_read_s32_default(const struct dtb_node * node,const char * propname,int32_t def)94 int dtb_node_read_s32_default(const struct dtb_node *node, const char *propname, int32_t def)
95 {
96 RT_ASSERT(dtb_node_valid(node));
97 dtb_node_read_u32(node, propname, (uint32_t *)&def);
98
99 return def;
100 }
101
dtb_node_read_u32_index(const struct dtb_node * dn,const char * propname,int index,uint32_t * outp)102 int dtb_node_read_u32_index(const struct dtb_node *dn, const char *propname,
103 int index, uint32_t *outp)
104 {
105 const uint32_t *val;
106
107 debug("%s: %s: ", __func__, propname);
108 if (!dn)
109 return -EINVAL;
110
111 val = dtb_node_find_property_value_of_size(dn, propname,
112 sizeof(*outp) * (index + 1));
113 if (DTB_IS_ERR(val))
114 {
115 debug("(not found)\n");
116 return DTB_PTR_ERR(val);
117 }
118
119 *outp = fdt32_to_cpu(val[index]);
120 debug("%#x (%d)\n", *outp, *outp);
121
122 return 0;
123 }
124
dtb_node_read_u64(const struct dtb_node * dn,const char * propname,uint64_t * outp)125 int dtb_node_read_u64(const struct dtb_node *dn, const char *propname, uint64_t *outp)
126 {
127 const uint64_t *val;
128
129 debug("%s: %s: ", __func__, propname);
130 if (!dn)
131 return -EINVAL;
132 val = dtb_node_find_property_value_of_size(dn, propname, sizeof(*outp));
133 if (DTB_IS_ERR(val))
134 {
135 debug("(not found)\n");
136 return DTB_PTR_ERR(val);
137 }
138
139 *outp = fdt64_to_cpu(*val);
140 debug("%#llx (%lld)\n", (unsigned long long)*outp,
141 (unsigned long long)*outp);
142
143 return 0;
144 }
145
dtb_node_read_u64_default(const struct dtb_node * node,const char * propname,uint64_t def)146 uint64_t dtb_node_read_u64_default(const struct dtb_node *node, const char *propname, uint64_t def)
147 {
148 RT_ASSERT(dtb_node_valid(node));
149 dtb_node_read_u64(node, propname, &def);
150
151 return def;
152 }
153
dtb_node_n_addr_cells(const struct dtb_node * dn)154 int dtb_node_n_addr_cells(const struct dtb_node *dn)
155 {
156 const uint32_t *ip;
157
158 do
159 {
160 if (dn->parent)
161 dn = dn->parent;
162 ip = dtb_node_get_dtb_node_property_value(dn, "#address-cells", NULL);
163 if (ip)
164 return fdt32_to_cpu(*ip);
165 } while (dn->parent);
166
167 /* No #address-cells property for the root node */
168 return DEV_ROOT_NODE_ADDR_CELLS_DEFAULT;
169 }
170
dtb_node_n_size_cells(const struct dtb_node * dn)171 int dtb_node_n_size_cells(const struct dtb_node *dn)
172 {
173 const uint32_t *ip;
174
175 do
176 {
177 if (dn->parent)
178 dn = dn->parent;
179 ip = dtb_node_get_dtb_node_property_value(dn, "#size-cells", NULL);
180 if (ip)
181 return fdt32_to_cpu(*ip);
182 } while (dn->parent);
183
184 /* No #size-cells property for the root node */
185 return DEV_ROOT_NODE_SIZE_CELLS_DEFAULT;
186 }
187
dtb_node_simple_addr_cells(const struct dtb_node * dn)188 int dtb_node_simple_addr_cells(const struct dtb_node *dn)
189 {
190 const uint32_t *ip;
191
192 ip = dtb_node_get_dtb_node_property_value(dn, "#address-cells", NULL);
193 if (ip)
194 return fdt32_to_cpu(*ip);
195
196 /* Return a default of 2 to match fdt_address_cells()*/
197 return 2;
198 }
199
dtb_node_simple_size_cells(const struct dtb_node * dn)200 int dtb_node_simple_size_cells(const struct dtb_node *dn)
201 {
202 const uint32_t *ip;
203
204 ip = dtb_node_get_dtb_node_property_value(dn, "#size-cells", NULL);
205 if (ip)
206 return fdt32_to_cpu(*ip);
207
208 /* Return a default of 2 to match fdt_size_cells()*/
209 return 2;
210 }
211
dtb_node_get_dtb_node_property(const struct dtb_node * dtb_node,const char * property_name,int * property_size)212 struct dtb_property *dtb_node_get_dtb_node_property(const struct dtb_node *dtb_node, const char *property_name, int *property_size)
213 {
214 struct dtb_property *dtb_property = NULL;
215
216 if (dtb_node != NULL && property_name != NULL)
217 {
218 dtb_property = dtb_node->properties;
219
220 while (dtb_property != NULL)
221 {
222 if (!strcmp(dtb_property->name, property_name))
223 {
224 if (property_size != NULL)
225 {
226 *property_size = dtb_property->size;
227 }
228 return dtb_property;
229 }
230 dtb_property = dtb_property->next;
231 }
232 }
233
234 return dtb_property;
235 }
236
237 #define for_each_property_of_node(dn, pp) \
238 for (pp = dn->properties; pp != NULL; pp = pp->next)
239
dtb_node_find_node_opts_by_path(const char * path,const char ** opts)240 struct dtb_node *dtb_node_find_node_opts_by_path(const char *path,
241 const char **opts)
242 {
243 struct dtb_node *np = NULL;
244 struct dtb_property *pp;
245 const char *separator = strchr(path, ':');
246
247 if (opts)
248 *opts = separator ? separator + 1 : NULL;
249
250 if (strcmp(path, "/") == 0)
251 return dtb_node_get(get_dtb_node_head());
252
253 /* The path could begin with an alias */
254 if (*path != '/')
255 {
256 int len;
257 const char *p = separator;
258
259 if (!p)
260 p = strchrnul(path, '/');
261 len = p - path;
262
263 /* of_aliases must not be NULL */
264 if (!fdt_aliases)
265 return NULL;
266
267 for_each_property_of_node(fdt_aliases, pp)
268 {
269 if (strlen(pp->name) == len && !strncmp(pp->name, path,
270 len))
271 {
272 np = dtb_node_find_node_by_path(pp->value);
273 break;
274 }
275 }
276 if (!np)
277 return NULL;
278 path = p;
279 }
280
281 /* Step down the tree matching path components */
282 if (!np)
283 np = dtb_node_get(get_dtb_node_head());
284 while (np && *path == '/')
285 {
286 struct dtb_node *tmp = np;
287
288 path++; /* Increment past '/' delimiter */
289 np = dtb_node_get_dtb_node_by_path(np, path);
290 dtb_node_put(tmp);
291 path = strchrnul(path, '/');
292 if (separator && separator < path)
293 break;
294 }
295
296 return np;
297 }
298
dtb_node_find_compatible_node(struct dtb_node * from,const char * compatible)299 struct dtb_node *dtb_node_find_compatible_node(struct dtb_node *from, const char *compatible)
300 {
301 struct dtb_node *dn;
302
303 for_each_of_allnodes_from(from, dn)
304 {
305 if (dtb_node_get_dtb_node_compatible_match(dn, compatible) &&
306 dtb_node_get(dn))
307 break;
308 }
309 dtb_node_put(from);
310
311 return dn;
312 }
313
dtb_node_get_dtb_node_property_value(const struct dtb_node * dtb_node,const char * property_name,int * property_size)314 void *dtb_node_get_dtb_node_property_value(const struct dtb_node *dtb_node, const char *property_name, int *property_size)
315 {
316 struct dtb_property *dtb_property = dtb_node_get_dtb_node_property(dtb_node, property_name, NULL);
317
318 if (!dtb_property || !dtb_property->value || !dtb_property->size)
319 {
320 return NULL;
321 }
322
323 if (property_size != NULL)
324 {
325 *property_size = dtb_property->size;
326 }
327
328 return dtb_property->value;
329 }
330
dtb_node_find_node_by_prop_value(struct dtb_node * from,const char * propname,const void * propval,int proplen)331 const struct dtb_node *dtb_node_find_node_by_prop_value(struct dtb_node *from,
332 const char *propname,
333 const void *propval, int proplen)
334 {
335 struct dtb_node *np;
336 void *value;
337 for_each_of_allnodes_from(from, np)
338 {
339 value = dtb_node_get_dtb_node_property_value(np, propname, &proplen);
340 if (!memcmp(value, propval, proplen) && dtb_node_get(np))
341 break;
342 }
343 dtb_node_put(from);
344
345 return np;
346 }
347
dtb_node_find_all_nodes(const struct dtb_node * prev)348 struct dtb_node *dtb_node_find_all_nodes(const struct dtb_node *prev)
349 {
350 const struct dtb_node *dn;
351
352 if (!prev)
353 {
354 dn = get_dtb_node_head();
355 }
356 else if (prev->child)
357 {
358 dn = prev->child;
359 }
360 else
361 {
362 /*
363 * Walk back up looking for a sibling, or the end of the
364 * structure
365 */
366 dn = prev;
367 while (dn->parent && !dn->sibling)
368 dn = dn->parent;
369 dn = dn->sibling; /* Might be null at the end of the tree */
370 }
371
372 return (struct dtb_node *)dn;
373 }
374
dtb_node_device_is_available(const struct dtb_node * device)375 rt_bool_t dtb_node_device_is_available(const struct dtb_node *device)
376 {
377 const char *status;
378 int statlen;
379
380 if (!device)
381 return RT_FALSE;
382
383 status = dtb_node_get_dtb_node_property_value(device, "status", &statlen);
384 if (status == NULL)
385 return RT_TRUE;
386
387 if (statlen > 0)
388 {
389 if (!strcmp(status, "okay"))
390 return RT_TRUE;
391 }
392
393 return RT_FALSE;
394 }
395
dtb_node_get_parent(const struct dtb_node * node)396 struct dtb_node *dtb_node_get_parent(const struct dtb_node *node)
397 {
398 const struct dtb_node *dn;
399
400 if (!node)
401 return NULL;
402
403 dn = dtb_node_get(node->parent);
404
405 return (struct dtb_node *)dn;
406 }
407
dtb_node_find_node_by_phandle(phandle handle)408 struct dtb_node *dtb_node_find_node_by_phandle(phandle handle)
409 {
410 struct dtb_node *dn;
411
412 if (!handle)
413 return NULL;
414
415 for_each_of_allnodes(dn) if (dn->handle == handle) break;
416 (void)dtb_node_get(dn);
417
418 return dn;
419 }
420
dtb_node_property_match_string(const struct dtb_node * dn,const char * propname,const char * string)421 int dtb_node_property_match_string(const struct dtb_node *dn, const char *propname,
422 const char *string)
423 {
424 const struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
425 size_t l;
426 int i;
427 const char *p, *end;
428
429 if (!prop)
430 return -EINVAL;
431 if (!prop->value)
432 return -ENODATA;
433
434 p = prop->value;
435 end = p + prop->size;
436
437 for (i = 0; p < end; i++, p += l)
438 {
439 l = strnlen(p, end - p) + 1;
440 if (p + l > end)
441 return -EILSEQ;
442 debug("comparing %s with %s\n", string, p);
443 if (strcmp(string, p) == 0)
444 return i; /* Found it; return index */
445 }
446 return -ENODATA;
447 }
448
449 /**
450 * of_property_read_string_helper() - Utility helper for parsing string properties
451 * @np: device node from which the property value is to be read.
452 * @propname: name of the property to be searched.
453 * @out_strs: output array of string pointers.
454 * @sz: number of array elements to read.
455 * @skip: Number of strings to skip over at beginning of list.
456 *
457 * Don't call this function directly. It is a utility helper for the
458 * of_property_read_string*() family of functions.
459 */
dtb_node_property_read_string_helper(const struct dtb_node * dn,const char * propname,const char ** out_strs,size_t sz,int skip)460 int dtb_node_property_read_string_helper(const struct dtb_node *dn,
461 const char *propname, const char **out_strs,
462 size_t sz, int skip)
463 {
464 const struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
465 int l = 0, i = 0;
466 const char *p, *end;
467
468 if (!prop)
469 return -EINVAL;
470 if (!prop->value)
471 return -ENODATA;
472 p = prop->value;
473 end = p + prop->size;
474
475 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l)
476 {
477 l = strnlen(p, end - p) + 1;
478 if (p + l > end)
479 return -EILSEQ;
480 if (out_strs && i >= skip)
481 *out_strs++ = p;
482 }
483 i -= skip;
484 return i <= 0 ? -ENODATA : i;
485 }
486
__dtb_node_parse_phandle_with_args(const struct dtb_node * dn,const char * list_name,const char * cells_name,int cell_count,int index,struct fdt_phandle_args * out_args)487 static int __dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
488 const char *list_name,
489 const char *cells_name,
490 int cell_count, int index,
491 struct fdt_phandle_args *out_args)
492 {
493 const uint32_t *list, *list_end;
494 int rc = 0, cur_index = 0;
495 uint32_t count = 0;
496 struct dtb_node *node = NULL;
497 phandle phandle;
498 int size;
499
500 /* Retrieve the phandle list property */
501 list = dtb_node_get_dtb_node_property_value(dn, list_name, &size);
502 if (!list)
503 return -ENOENT;
504 list_end = list + size / sizeof(*list);
505
506 /* Loop over the phandles until all the requested entry is found */
507 while (list < list_end)
508 {
509 rc = -EINVAL;
510 count = 0;
511
512 /*
513 * If phandle is 0, then it is an empty entry with no
514 * arguments. Skip forward to the next entry.
515 */
516 phandle = fdt32_to_cpu(*(list++));
517 if (phandle)
518 {
519 /*
520 * Find the provider node and parse the #*-cells
521 * property to determine the argument length.
522 *
523 * This is not needed if the cell count is hard-coded
524 * (i.e. cells_name not set, but cell_count is set),
525 * except when we're going to return the found node
526 * below.
527 */
528 if (cells_name || cur_index == index)
529 {
530 node = dtb_node_find_node_by_phandle(phandle);
531 if (!node)
532 {
533 debug("%s: could not find phandle\n",
534 dn->path);
535 goto err;
536 }
537 }
538
539 if (cells_name)
540 {
541 if (dtb_node_read_u32(node, cells_name, &count))
542 {
543 debug("%s: could not get %s for %s\n",
544 dn->path, cells_name,
545 node->path);
546 goto err;
547 }
548 }
549 else
550 {
551 count = cell_count;
552 }
553
554 /*
555 * Make sure that the arguments actually fit in the
556 * remaining property data length
557 */
558 if (list + count > list_end)
559 {
560 debug("%s: arguments longer than property\n",
561 dn->path);
562 goto err;
563 }
564 }
565
566 /*
567 * All of the error cases above bail out of the loop, so at
568 * this point, the parsing is successful. If the requested
569 * index matches, then fill the out_args structure and return,
570 * or return -ENOENT for an empty entry.
571 */
572 rc = -ENOENT;
573 if (cur_index == index)
574 {
575 if (!phandle)
576 goto err;
577
578 if (out_args)
579 {
580 int i;
581 if (count > FDT_MAX_PHANDLE_ARGS)
582 count = FDT_MAX_PHANDLE_ARGS;
583 out_args->np = node;
584 out_args->args_count = count;
585 for (i = 0; i < count; i++)
586 out_args->args[i] =
587 fdt32_to_cpu(*(list++));
588 }
589 else
590 {
591 dtb_node_put(node);
592 }
593
594 /* Found it! return success */
595 return 0;
596 }
597
598 dtb_node_put(node);
599 node = NULL;
600 list += count;
601 cur_index++;
602 }
603
604 /*
605 * Unlock node before returning result; will be one of:
606 * -ENOENT : index is for empty phandle
607 * -EINVAL : parsing error on data
608 * [1..n] : Number of phandle (count mode; when index = -1)
609 */
610 rc = index < 0 ? cur_index : -ENOENT;
611 err:
612 if (node)
613 dtb_node_put(node);
614 return rc;
615 }
616
dtb_node_parse_phandle(const struct dtb_node * dn,const char * phandle_name,int index)617 struct dtb_node *dtb_node_parse_phandle(const struct dtb_node *dn,
618 const char *phandle_name, int index)
619 {
620 struct fdt_phandle_args args;
621
622 if (index < 0)
623 return NULL;
624
625 if (__dtb_node_parse_phandle_with_args(dn, phandle_name, NULL, 0, index,
626 &args))
627 return NULL;
628
629 return args.np;
630 }
631
dtb_node_parse_phandle_with_args(const struct dtb_node * dn,const char * list_name,const char * cells_name,int index,struct fdt_phandle_args * out_args)632 int dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
633 const char *list_name, const char *cells_name,
634 int index, struct fdt_phandle_args *out_args)
635 {
636 if (index < 0)
637 return -EINVAL;
638
639 return __dtb_node_parse_phandle_with_args(dn, list_name, cells_name, 0,
640 index, out_args);
641 }
642
dtb_node_count_phandle_with_args(const struct dtb_node * dn,const char * list_name,const char * cells_name)643 int dtb_node_count_phandle_with_args(const struct dtb_node *dn,
644 const char *list_name, const char *cells_name)
645 {
646 return __dtb_node_parse_phandle_with_args(dn, list_name, cells_name, 0,
647 -1, NULL);
648 }
649