blob: 667659054a7e6c9cd86aeb57028e38a9149e3e58 [file] [log] [blame]
Masahiro Yamada6dd10522020-04-16 18:30:18 +09001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
2/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2013 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
6 */
7
Simon Glass2dc9c342020-05-10 11:40:01 -06008#include <fdt_support.h>
Masahiro Yamada6dd10522020-04-16 18:30:18 +09009#include <linux/libfdt_env.h>
10#include <fdt_region.h>
11
12#ifndef USE_HOSTCC
13#include <fdt.h>
14#include <linux/libfdt.h>
15#else
16#include "fdt_host.h"
17#endif
18
19#define FDT_MAX_DEPTH 32
20
21static int str_in_list(const char *str, char * const list[], int count)
22{
23 int i;
24
25 for (i = 0; i < count; i++)
26 if (!strcmp(list[i], str))
27 return 1;
28
29 return 0;
30}
31
32int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
33 char * const exc_prop[], int exc_prop_count,
34 struct fdt_region region[], int max_regions,
35 char *path, int path_len, int add_string_tab)
36{
37 int stack[FDT_MAX_DEPTH] = { 0 };
38 char *end;
39 int nextoffset = 0;
40 uint32_t tag;
41 int count = 0;
42 int start = -1;
43 int depth = -1;
44 int want = 0;
45 int base = fdt_off_dt_struct(fdt);
46
47 end = path;
48 *end = '\0';
49 do {
50 const struct fdt_property *prop;
51 const char *name;
52 const char *str;
53 int include = 0;
54 int stop_at = 0;
55 int offset;
56 int len;
57
58 offset = nextoffset;
59 tag = fdt_next_tag(fdt, offset, &nextoffset);
60 stop_at = nextoffset;
61
62 switch (tag) {
63 case FDT_PROP:
64 include = want >= 2;
65 stop_at = offset;
66 prop = fdt_get_property_by_offset(fdt, offset, NULL);
67 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
68 if (str_in_list(str, exc_prop, exc_prop_count))
69 include = 0;
70 break;
71
72 case FDT_NOP:
73 include = want >= 2;
74 stop_at = offset;
75 break;
76
77 case FDT_BEGIN_NODE:
78 depth++;
79 if (depth == FDT_MAX_DEPTH)
80 return -FDT_ERR_BADSTRUCTURE;
81 name = fdt_get_name(fdt, offset, &len);
82 if (end - path + 2 + len >= path_len)
83 return -FDT_ERR_NOSPACE;
84 if (end != path + 1)
85 *end++ = '/';
86 strcpy(end, name);
87 end += len;
88 stack[depth] = want;
89 if (want == 1)
90 stop_at = offset;
91 if (str_in_list(path, inc, inc_count))
92 want = 2;
93 else if (want)
94 want--;
95 else
96 stop_at = offset;
97 include = want;
98 break;
99
100 case FDT_END_NODE:
101 /* Depth must never go below -1 */
102 if (depth < 0)
103 return -FDT_ERR_BADSTRUCTURE;
104 include = want;
105 want = stack[depth--];
106 while (end > path && *--end != '/')
107 ;
108 *end = '\0';
109 break;
110
111 case FDT_END:
112 include = 1;
113 break;
114 }
115
116 if (include && start == -1) {
117 /* Should we merge with previous? */
118 if (count && count <= max_regions &&
119 offset == region[count - 1].offset +
120 region[count - 1].size - base)
121 start = region[--count].offset - base;
122 else
123 start = offset;
124 }
125
126 if (!include && start != -1) {
127 if (count < max_regions) {
128 region[count].offset = base + start;
129 region[count].size = stop_at - start;
130 }
131 count++;
132 start = -1;
133 }
134 } while (tag != FDT_END);
135
136 if (nextoffset != fdt_size_dt_struct(fdt))
137 return -FDT_ERR_BADLAYOUT;
138
139 /* Add a region for the END tag and the string table */
140 if (count < max_regions) {
141 region[count].offset = base + start;
142 region[count].size = nextoffset - start;
143 if (add_string_tab)
144 region[count].size += fdt_size_dt_strings(fdt);
145 }
146 count++;
147
148 return count;
149}
150
151/**
152 * fdt_add_region() - Add a new region to our list
153 * @info: State information
154 * @offset: Start offset of region
155 * @size: Size of region
156 *
157 * The region is added if there is space, but in any case we increment the
158 * count. If permitted, and the new region overlaps the last one, we merge
159 * them.
160 */
161static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
162{
163 struct fdt_region *reg;
164
165 reg = info->region ? &info->region[info->count - 1] : NULL;
166 if (info->can_merge && info->count &&
167 info->count <= info->max_regions &&
168 reg && offset <= reg->offset + reg->size) {
169 reg->size = offset + size - reg->offset;
170 } else if (info->count++ < info->max_regions) {
171 if (reg) {
172 reg++;
173 reg->offset = offset;
174 reg->size = size;
175 }
176 } else {
177 return -1;
178 }
179
180 return 0;
181}
182
183static int region_list_contains_offset(struct fdt_region_state *info,
184 const void *fdt, int target)
185{
186 struct fdt_region *reg;
187 int num;
188
189 target += fdt_off_dt_struct(fdt);
190 for (reg = info->region, num = 0; num < info->count; reg++, num++) {
191 if (target >= reg->offset && target < reg->offset + reg->size)
192 return 1;
193 }
194
195 return 0;
196}
197
198/**
199 * fdt_add_alias_regions() - Add regions covering the aliases that we want
200 *
201 * The /aliases node is not automatically included by fdtgrep unless the
202 * command-line arguments cause to be included (or not excluded). However
203 * aliases are special in that we generally want to include those which
204 * reference a node that fdtgrep includes.
205 *
206 * In fact we want to include only aliases for those nodes still included in
207 * the fdt, and drop the other aliases since they point to nodes that will not
208 * be present.
209 *
210 * This function scans the aliases and adds regions for those which we want
211 * to keep.
212 *
213 * @fdt: Device tree to scan
214 * @region: List of regions
215 * @count: Number of regions in the list so far (i.e. starting point for this
216 * function)
217 * @max_regions: Maximum number of regions in @region list
218 * @info: Place to put the region state
219 * @return number of regions after processing, or -FDT_ERR_NOSPACE if we did
220 * not have enough room in the regions table for the regions we wanted to add.
221 */
222int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
223 int max_regions, struct fdt_region_state *info)
224{
225 int base = fdt_off_dt_struct(fdt);
226 int node, node_end, offset;
227 int did_alias_header;
228
229 node = fdt_subnode_offset(fdt, 0, "aliases");
230 if (node < 0)
231 return -FDT_ERR_NOTFOUND;
232
233 /*
234 * Find the next node so that we know where the /aliases node ends. We
235 * need special handling if /aliases is the last node.
236 */
237 node_end = fdt_next_subnode(fdt, node);
238 if (node_end == -FDT_ERR_NOTFOUND)
239 /* Move back to the FDT_END_NODE tag of '/' */
240 node_end = fdt_size_dt_struct(fdt) - sizeof(fdt32_t) * 2;
241 else if (node_end < 0) /* other error */
242 return node_end;
243 node_end -= sizeof(fdt32_t); /* Move to FDT_END_NODE tag of /aliases */
244
245 did_alias_header = 0;
246 info->region = region;
247 info->count = count;
248 info->can_merge = 0;
249 info->max_regions = max_regions;
250
251 for (offset = fdt_first_property_offset(fdt, node);
252 offset >= 0;
253 offset = fdt_next_property_offset(fdt, offset)) {
254 const struct fdt_property *prop;
255 const char *name;
256 int target, next;
257
258 prop = fdt_get_property_by_offset(fdt, offset, NULL);
259 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
260 target = fdt_path_offset(fdt, name);
261 if (!region_list_contains_offset(info, fdt, target))
262 continue;
263 next = fdt_next_property_offset(fdt, offset);
264 if (next < 0)
265 next = node_end;
266
267 if (!did_alias_header) {
268 fdt_add_region(info, base + node, 12);
269 did_alias_header = 1;
270 }
271 fdt_add_region(info, base + offset, next - offset);
272 }
273
274 /* Add the FDT_END_NODE tag */
275 if (did_alias_header)
276 fdt_add_region(info, base + node_end, sizeof(fdt32_t));
277
278 return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
279}
280
281/**
282 * fdt_include_supernodes() - Include supernodes required by this node
283 * @info: State information
284 * @depth: Current stack depth
285 *
286 * When we decided to include a node or property which is not at the top
287 * level, this function forces the inclusion of higher level nodes. For
288 * example, given this tree:
289 *
290 * / {
291 * testing {
292 * }
293 * }
294 *
295 * If we decide to include testing then we need the root node to have a valid
296 * tree. This function adds those regions.
297 */
298static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
299{
300 int base = fdt_off_dt_struct(info->fdt);
301 int start, stop_at;
302 int i;
303
304 /*
305 * Work down the stack looking for supernodes that we didn't include.
306 * The algortihm here is actually pretty simple, since we know that
307 * no previous subnode had to include these nodes, or if it did, we
308 * marked them as included (on the stack) already.
309 */
310 for (i = 0; i <= depth; i++) {
311 if (!info->stack[i].included) {
312 start = info->stack[i].offset;
313
314 /* Add the FDT_BEGIN_NODE tag of this supernode */
315 fdt_next_tag(info->fdt, start, &stop_at);
316 if (fdt_add_region(info, base + start, stop_at - start))
317 return -1;
318
319 /* Remember that this supernode is now included */
320 info->stack[i].included = 1;
321 info->can_merge = 1;
322 }
323
324 /* Force (later) generation of the FDT_END_NODE tag */
325 if (!info->stack[i].want)
326 info->stack[i].want = WANT_NODES_ONLY;
327 }
328
329 return 0;
330}
331
332enum {
333 FDT_DONE_NOTHING,
334 FDT_DONE_MEM_RSVMAP,
335 FDT_DONE_STRUCT,
336 FDT_DONE_END,
337 FDT_DONE_STRINGS,
338 FDT_DONE_ALL,
339};
340
341int fdt_first_region(const void *fdt,
342 int (*h_include)(void *priv, const void *fdt, int offset,
343 int type, const char *data, int size),
344 void *priv, struct fdt_region *region,
345 char *path, int path_len, int flags,
346 struct fdt_region_state *info)
347{
348 struct fdt_region_ptrs *p = &info->ptrs;
349
350 /* Set up our state */
351 info->fdt = fdt;
352 info->can_merge = 1;
353 info->max_regions = 1;
354 info->start = -1;
355 p->want = WANT_NOTHING;
356 p->end = path;
357 *p->end = '\0';
358 p->nextoffset = 0;
359 p->depth = -1;
360 p->done = FDT_DONE_NOTHING;
361
362 return fdt_next_region(fdt, h_include, priv, region,
363 path, path_len, flags, info);
364}
365
366/***********************************************************************
367 *
368 * Theory of operation
369 *
370 * Note: in this description 'included' means that a node (or other part
371 * of the tree) should be included in the region list, i.e. it will have
372 * a region which covers its part of the tree.
373 *
374 * This function maintains some state from the last time it is called.
375 * It checks the next part of the tree that it is supposed to look at
376 * (p.nextoffset) to see if that should be included or not. When it
377 * finds something to include, it sets info->start to its offset. This
378 * marks the start of the region we want to include.
379 *
380 * Once info->start is set to the start (i.e. not -1), we continue
381 * scanning until we find something that we don't want included. This
382 * will be the end of a region. At this point we can close off the
383 * region and add it to the list. So we do so, and reset info->start
384 * to -1.
385 *
386 * One complication here is that we want to merge regions. So when we
387 * come to add another region later, we may in fact merge it with the
388 * previous one if one ends where the other starts.
389 *
390 * The function fdt_add_region() will return -1 if it fails to add the
391 * region, because we already have a region ready to be returned, and
392 * the new one cannot be merged in with it. In this case, we must return
393 * the region we found, and wait for another call to this function.
394 * When it comes, we will repeat the processing of the tag and again
395 * try to add a region. This time it will succeed.
396 *
397 * The current state of the pointers (stack, offset, etc.) is maintained
398 * in a ptrs member. At the start of every loop iteration we make a copy
399 * of it. The copy is then updated as the tag is processed. Only if we
400 * get to the end of the loop iteration (and successfully call
401 * fdt_add_region() if we need to) can we commit the changes we have
402 * made to these pointers. For example, if we see an FDT_END_NODE tag,
403 * we will decrement the depth value. But if we need to add a region
404 * for this tag (let's say because the previous tag is included and this
405 * FDT_END_NODE tag is not included) then we will only commit the result
406 * if we were able to add the region. That allows us to retry again next
407 * time.
408 *
409 * We keep track of a variable called 'want' which tells us what we want
410 * to include when there is no specific information provided by the
411 * h_include function for a particular property. This basically handles
412 * the inclusion of properties which are pulled in by virtue of the node
413 * they are in. So if you include a node, its properties are also
414 * included. In this case 'want' will be WANT_NODES_AND_PROPS. The
415 * FDT_REG_DIRECT_SUBNODES feature also makes use of 'want'. While we
416 * are inside the subnode, 'want' will be set to WANT_NODES_ONLY, so
417 * that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE tags will be
418 * included, and properties will be skipped. If WANT_NOTHING is
419 * selected, then we will just rely on what the h_include() function
420 * tells us.
421 *
422 * Using 'want' we work out 'include', which tells us whether this
423 * current tag should be included or not. As you can imagine, if the
424 * value of 'include' changes, that means we are on a boundary between
425 * nodes to include and nodes to exclude. At this point we either close
426 * off a previous region and add it to the list, or mark the start of a
427 * new region.
428 *
429 * Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the
430 * string list. Each of these dealt with as a whole (i.e. we create a
431 * region for each if it is to be included). For mem_rsvmap we don't
432 * allow it to merge with the first struct region. For the stringlist,
433 * we don't allow it to merge with the last struct region (which
434 * contains at minimum the FDT_END tag).
435 *
436 *********************************************************************/
437
438int fdt_next_region(const void *fdt,
439 int (*h_include)(void *priv, const void *fdt, int offset,
440 int type, const char *data, int size),
441 void *priv, struct fdt_region *region,
442 char *path, int path_len, int flags,
443 struct fdt_region_state *info)
444{
445 int base = fdt_off_dt_struct(fdt);
446 int last_node = 0;
447 const char *str;
448
449 info->region = region;
450 info->count = 0;
451 if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
452 (flags & FDT_REG_ADD_MEM_RSVMAP)) {
453 /* Add the memory reserve map into its own region */
454 if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
455 fdt_off_dt_struct(fdt) -
456 fdt_off_mem_rsvmap(fdt)))
457 return 0;
458 info->can_merge = 0; /* Don't allow merging with this */
459 info->ptrs.done = FDT_DONE_MEM_RSVMAP;
460 }
461
462 /*
463 * Work through the tags one by one, deciding whether each needs to
464 * be included or not. We set the variable 'include' to indicate our
465 * decision. 'want' is used to track what we want to include - it
466 * allows us to pick up all the properties (and/or subnode tags) of
467 * a node.
468 */
469 while (info->ptrs.done < FDT_DONE_STRUCT) {
470 const struct fdt_property *prop;
471 struct fdt_region_ptrs p;
472 const char *name;
473 int include = 0;
474 int stop_at = 0;
475 uint32_t tag;
476 int offset;
477 int val;
478 int len;
479
480 /*
481 * Make a copy of our pointers. If we make it to the end of
482 * this block then we will commit them back to info->ptrs.
483 * Otherwise we can try again from the same starting state
484 * next time we are called.
485 */
486 p = info->ptrs;
487
488 /*
489 * Find the tag, and the offset of the next one. If we need to
490 * stop including tags, then by default we stop *after*
491 * including the current tag
492 */
493 offset = p.nextoffset;
494 tag = fdt_next_tag(fdt, offset, &p.nextoffset);
495 stop_at = p.nextoffset;
496
497 switch (tag) {
498 case FDT_PROP:
499 stop_at = offset;
500 prop = fdt_get_property_by_offset(fdt, offset, NULL);
501 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
502 val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
503 strlen(str) + 1);
504 if (val == -1) {
505 include = p.want >= WANT_NODES_AND_PROPS;
506 } else {
507 include = val;
508 /*
509 * Make sure we include the } for this block.
510 * It might be more correct to have this done
511 * by the call to fdt_include_supernodes() in
512 * the case where it adds the node we are
513 * currently in, but this is equivalent.
514 */
515 if ((flags & FDT_REG_SUPERNODES) && val &&
516 !p.want)
517 p.want = WANT_NODES_ONLY;
518 }
519
520 /* Value grepping is not yet supported */
521 break;
522
523 case FDT_NOP:
524 include = p.want >= WANT_NODES_AND_PROPS;
525 stop_at = offset;
526 break;
527
528 case FDT_BEGIN_NODE:
529 last_node = offset;
530 p.depth++;
531 if (p.depth == FDT_MAX_DEPTH)
532 return -FDT_ERR_BADSTRUCTURE;
533 name = fdt_get_name(fdt, offset, &len);
534 if (p.end - path + 2 + len >= path_len)
535 return -FDT_ERR_NOSPACE;
536
537 /* Build the full path of this node */
538 if (p.end != path + 1)
539 *p.end++ = '/';
540 strcpy(p.end, name);
541 p.end += len;
542 info->stack[p.depth].want = p.want;
543 info->stack[p.depth].offset = offset;
544
545 /*
546 * If we are not intending to include this node unless
547 * it matches, make sure we stop *before* its tag.
548 */
549 if (p.want == WANT_NODES_ONLY ||
550 !(flags & (FDT_REG_DIRECT_SUBNODES |
551 FDT_REG_ALL_SUBNODES))) {
552 stop_at = offset;
553 p.want = WANT_NOTHING;
554 }
555 val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
556 p.end - path + 1);
557
558 /* Include this if requested */
559 if (val) {
560 p.want = (flags & FDT_REG_ALL_SUBNODES) ?
561 WANT_ALL_NODES_AND_PROPS :
562 WANT_NODES_AND_PROPS;
563 }
564
565 /* If not requested, decay our 'p.want' value */
566 else if (p.want) {
567 if (p.want != WANT_ALL_NODES_AND_PROPS)
568 p.want--;
569
570 /* Not including this tag, so stop now */
571 } else {
572 stop_at = offset;
573 }
574
575 /*
576 * Decide whether to include this tag, and update our
577 * stack with the state for this node
578 */
579 include = p.want;
580 info->stack[p.depth].included = include;
581 break;
582
583 case FDT_END_NODE:
584 include = p.want;
585 if (p.depth < 0)
586 return -FDT_ERR_BADSTRUCTURE;
587
588 /*
589 * If we don't want this node, stop right away, unless
590 * we are including subnodes
591 */
592 if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
593 stop_at = offset;
594 p.want = info->stack[p.depth].want;
595 p.depth--;
596 while (p.end > path && *--p.end != '/')
597 ;
598 *p.end = '\0';
599 break;
600
601 case FDT_END:
602 /* We always include the end tag */
603 include = 1;
604 p.done = FDT_DONE_STRUCT;
605 break;
606 }
607
608 /* If this tag is to be included, mark it as region start */
609 if (include && info->start == -1) {
610 /* Include any supernodes required by this one */
611 if (flags & FDT_REG_SUPERNODES) {
612 if (fdt_include_supernodes(info, p.depth))
613 return 0;
614 }
615 info->start = offset;
616 }
617
618 /*
619 * If this tag is not to be included, finish up the current
620 * region.
621 */
622 if (!include && info->start != -1) {
623 if (fdt_add_region(info, base + info->start,
624 stop_at - info->start))
625 return 0;
626 info->start = -1;
627 info->can_merge = 1;
628 }
629
630 /* If we have made it this far, we can commit our pointers */
631 info->ptrs = p;
632 }
633
634 /* Add a region for the END tag and a separate one for string table */
635 if (info->ptrs.done < FDT_DONE_END) {
636 if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
637 return -FDT_ERR_BADSTRUCTURE;
638
639 if (fdt_add_region(info, base + info->start,
640 info->ptrs.nextoffset - info->start))
641 return 0;
642 info->ptrs.done++;
643 }
644 if (info->ptrs.done < FDT_DONE_STRINGS) {
645 if (flags & FDT_REG_ADD_STRING_TAB) {
646 info->can_merge = 0;
647 if (fdt_off_dt_strings(fdt) <
648 base + info->ptrs.nextoffset)
649 return -FDT_ERR_BADLAYOUT;
650 if (fdt_add_region(info, fdt_off_dt_strings(fdt),
651 fdt_size_dt_strings(fdt)))
652 return 0;
653 }
654 info->ptrs.done++;
655 }
656
657 return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
658}