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