1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2013 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
8 #include <fdt_support.h>
9 #include <linux/libfdt_env.h>
10 #include <fdt_region.h>
14 #include <linux/libfdt.h>
19 #define FDT_MAX_DEPTH 32
21 static int str_in_list(const char *str, char * const list[], int count)
25 for (i = 0; i < count; i++)
26 if (!strcmp(list[i], str))
32 int 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)
37 int stack[FDT_MAX_DEPTH] = { 0 };
45 int base = fdt_off_dt_struct(fdt);
46 bool expect_end = false;
51 const struct fdt_property *prop;
60 tag = fdt_next_tag(fdt, offset, &nextoffset);
63 /* If we see two root nodes, something is wrong */
64 if (expect_end && tag != FDT_END)
65 return -FDT_ERR_BADLAYOUT;
71 prop = fdt_get_property_by_offset(fdt, offset, NULL);
72 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
74 return -FDT_ERR_BADSTRUCTURE;
75 if (str_in_list(str, exc_prop, exc_prop_count))
86 if (depth == FDT_MAX_DEPTH)
87 return -FDT_ERR_BADSTRUCTURE;
88 name = fdt_get_name(fdt, offset, &len);
90 /* The root node must have an empty name */
92 return -FDT_ERR_BADLAYOUT;
93 if (end - path + 2 + len >= path_len)
94 return -FDT_ERR_NOSPACE;
102 if (str_in_list(path, inc, inc_count))
112 /* Depth must never go below -1 */
114 return -FDT_ERR_BADSTRUCTURE;
116 want = stack[depth--];
117 while (end > path && *--end != '/')
129 if (include && start == -1) {
130 /* Should we merge with previous? */
131 if (count && count <= max_regions &&
132 offset == region[count - 1].offset +
133 region[count - 1].size - base)
134 start = region[--count].offset - base;
139 if (!include && start != -1) {
140 if (count < max_regions) {
141 region[count].offset = base + start;
142 region[count].size = stop_at - start;
147 } while (tag != FDT_END);
149 if (nextoffset != fdt_size_dt_struct(fdt))
150 return -FDT_ERR_BADLAYOUT;
152 /* Add a region for the END tag and the string table */
153 if (count < max_regions) {
154 region[count].offset = base + start;
155 region[count].size = nextoffset - start;
157 region[count].size += fdt_size_dt_strings(fdt);
165 * fdt_add_region() - Add a new region to our list
166 * @info: State information
167 * @offset: Start offset of region
168 * @size: Size of region
170 * The region is added if there is space, but in any case we increment the
171 * count. If permitted, and the new region overlaps the last one, we merge
174 static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
176 struct fdt_region *reg;
178 reg = info->region ? &info->region[info->count - 1] : NULL;
179 if (info->can_merge && info->count &&
180 info->count <= info->max_regions &&
181 reg && offset <= reg->offset + reg->size) {
182 reg->size = offset + size - reg->offset;
183 } else if (info->count++ < info->max_regions) {
186 reg->offset = offset;
188 if (!(offset - fdt_off_dt_struct(info->fdt)))
189 info->have_node = true;
198 static int region_list_contains_offset(struct fdt_region_state *info,
199 const void *fdt, int target)
201 struct fdt_region *reg;
204 target += fdt_off_dt_struct(fdt);
205 for (reg = info->region, num = 0; num < info->count; reg++, num++) {
206 if (target >= reg->offset && target < reg->offset + reg->size)
214 * fdt_add_alias_regions() - Add regions covering the aliases that we want
216 * The /aliases node is not automatically included by fdtgrep unless the
217 * command-line arguments cause to be included (or not excluded). However
218 * aliases are special in that we generally want to include those which
219 * reference a node that fdtgrep includes.
221 * In fact we want to include only aliases for those nodes still included in
222 * the fdt, and drop the other aliases since they point to nodes that will not
225 * This function scans the aliases and adds regions for those which we want
228 * @fdt: Device tree to scan
229 * @region: List of regions
230 * @count: Number of regions in the list so far (i.e. starting point for this
232 * @max_regions: Maximum number of regions in @region list
233 * @info: Place to put the region state
234 * @return number of regions after processing, or -FDT_ERR_NOSPACE if we did
235 * not have enough room in the regions table for the regions we wanted to add.
237 int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
238 int max_regions, struct fdt_region_state *info)
240 int base = fdt_off_dt_struct(fdt);
241 int node, node_end, offset;
242 int did_alias_header;
244 node = fdt_subnode_offset(fdt, 0, "aliases");
246 return -FDT_ERR_NOTFOUND;
249 * Find the next node so that we know where the /aliases node ends. We
250 * need special handling if /aliases is the last node.
252 node_end = fdt_next_subnode(fdt, node);
253 if (node_end == -FDT_ERR_NOTFOUND)
254 /* Move back to the FDT_END_NODE tag of '/' */
255 node_end = fdt_size_dt_struct(fdt) - sizeof(fdt32_t) * 2;
256 else if (node_end < 0) /* other error */
258 node_end -= sizeof(fdt32_t); /* Move to FDT_END_NODE tag of /aliases */
260 did_alias_header = 0;
261 info->region = region;
264 info->max_regions = max_regions;
266 for (offset = fdt_first_property_offset(fdt, node);
268 offset = fdt_next_property_offset(fdt, offset)) {
269 const struct fdt_property *prop;
273 prop = fdt_get_property_by_offset(fdt, offset, NULL);
274 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
275 target = fdt_path_offset(fdt, name);
276 if (!region_list_contains_offset(info, fdt, target))
278 next = fdt_next_property_offset(fdt, offset);
282 if (!did_alias_header) {
283 fdt_add_region(info, base + node, 12);
284 did_alias_header = 1;
286 fdt_add_region(info, base + offset, next - offset);
289 /* Add the FDT_END_NODE tag */
290 if (did_alias_header)
291 fdt_add_region(info, base + node_end, sizeof(fdt32_t));
293 return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
297 * fdt_include_supernodes() - Include supernodes required by this node
298 * @info: State information
299 * @depth: Current stack depth
301 * When we decided to include a node or property which is not at the top
302 * level, this function forces the inclusion of higher level nodes. For
303 * example, given this tree:
310 * If we decide to include testing then we need the root node to have a valid
311 * tree. This function adds those regions.
313 static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
315 int base = fdt_off_dt_struct(info->fdt);
320 * Work down the stack looking for supernodes that we didn't include.
321 * The algortihm here is actually pretty simple, since we know that
322 * no previous subnode had to include these nodes, or if it did, we
323 * marked them as included (on the stack) already.
325 for (i = 0; i <= depth; i++) {
326 if (!info->stack[i].included) {
327 start = info->stack[i].offset;
329 /* Add the FDT_BEGIN_NODE tag of this supernode */
330 fdt_next_tag(info->fdt, start, &stop_at);
331 if (fdt_add_region(info, base + start, stop_at - start))
334 /* Remember that this supernode is now included */
335 info->stack[i].included = 1;
339 /* Force (later) generation of the FDT_END_NODE tag */
340 if (!info->stack[i].want)
341 info->stack[i].want = WANT_NODES_ONLY;
348 * Tracks the progress through the device tree. Everything fdt_next_region() is
349 * called it picks up at the same state as last time, looks at info->start and
350 * decides what region to add next
353 FDT_DONE_NOTHING, /* Starting */
354 FDT_DONE_MEM_RSVMAP, /* Completed mem_rsvmap region */
355 FDT_DONE_STRUCT, /* Completed struct region */
356 FDT_DONE_EMPTY, /* Completed checking for empty struct region */
357 FDT_DONE_END, /* Completed the FDT_END tag */
358 FDT_DONE_STRINGS, /* Completed the strings */
359 FDT_DONE_ALL, /* All done */
362 int fdt_first_region(const void *fdt,
363 int (*h_include)(void *priv, const void *fdt, int offset,
364 int type, const char *data, int size),
365 void *priv, struct fdt_region *region,
366 char *path, int path_len, int flags,
367 struct fdt_region_state *info)
369 struct fdt_region_ptrs *p = &info->ptrs;
371 /* Set up our state */
374 info->max_regions = 1;
376 info->have_node = false;
377 p->want = WANT_NOTHING;
382 p->done = FDT_DONE_NOTHING;
384 return fdt_next_region(fdt, h_include, priv, region,
385 path, path_len, flags, info);
388 /***********************************************************************
390 * Theory of operation
392 * Note: in this description 'included' means that a node (or other part
393 * of the tree) should be included in the region list, i.e. it will have
394 * a region which covers its part of the tree.
396 * This function maintains some state from the last time it is called.
397 * It checks the next part of the tree that it is supposed to look at
398 * (p.nextoffset) to see if that should be included or not. When it
399 * finds something to include, it sets info->start to its offset. This
400 * marks the start of the region we want to include.
402 * Once info->start is set to the start (i.e. not -1), we continue
403 * scanning until we find something that we don't want included. This
404 * will be the end of a region. At this point we can close off the
405 * region and add it to the list. So we do so, and reset info->start
408 * One complication here is that we want to merge regions. So when we
409 * come to add another region later, we may in fact merge it with the
410 * previous one if one ends where the other starts.
412 * The function fdt_add_region() will return -1 if it fails to add the
413 * region, because we already have a region ready to be returned, and
414 * the new one cannot be merged in with it. In this case, we must return
415 * the region we found, and wait for another call to this function.
416 * When it comes, we will repeat the processing of the tag and again
417 * try to add a region. This time it will succeed.
419 * The current state of the pointers (stack, offset, etc.) is maintained
420 * in a ptrs member. At the start of every loop iteration we make a copy
421 * of it. The copy is then updated as the tag is processed. Only if we
422 * get to the end of the loop iteration (and successfully call
423 * fdt_add_region() if we need to) can we commit the changes we have
424 * made to these pointers. For example, if we see an FDT_END_NODE tag,
425 * we will decrement the depth value. But if we need to add a region
426 * for this tag (let's say because the previous tag is included and this
427 * FDT_END_NODE tag is not included) then we will only commit the result
428 * if we were able to add the region. That allows us to retry again next
431 * We keep track of a variable called 'want' which tells us what we want
432 * to include when there is no specific information provided by the
433 * h_include function for a particular property. This basically handles
434 * the inclusion of properties which are pulled in by virtue of the node
435 * they are in. So if you include a node, its properties are also
436 * included. In this case 'want' will be WANT_NODES_AND_PROPS. The
437 * FDT_REG_DIRECT_SUBNODES feature also makes use of 'want'. While we
438 * are inside the subnode, 'want' will be set to WANT_NODES_ONLY, so
439 * that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE tags will be
440 * included, and properties will be skipped. If WANT_NOTHING is
441 * selected, then we will just rely on what the h_include() function
444 * Using 'want' we work out 'include', which tells us whether this
445 * current tag should be included or not. As you can imagine, if the
446 * value of 'include' changes, that means we are on a boundary between
447 * nodes to include and nodes to exclude. At this point we either close
448 * off a previous region and add it to the list, or mark the start of a
451 * Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the
452 * string list. Each of these dealt with as a whole (i.e. we create a
453 * region for each if it is to be included). For mem_rsvmap we don't
454 * allow it to merge with the first struct region. For the stringlist,
455 * we don't allow it to merge with the last struct region (which
456 * contains at minimum the FDT_END tag).
458 *********************************************************************/
460 int fdt_next_region(const void *fdt,
461 int (*h_include)(void *priv, const void *fdt, int offset,
462 int type, const char *data, int size),
463 void *priv, struct fdt_region *region,
464 char *path, int path_len, int flags,
465 struct fdt_region_state *info)
467 int base = fdt_off_dt_struct(fdt);
471 info->region = region;
473 if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
474 (flags & FDT_REG_ADD_MEM_RSVMAP)) {
475 /* Add the memory reserve map into its own region */
476 if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
477 fdt_off_dt_struct(fdt) -
478 fdt_off_mem_rsvmap(fdt)))
480 info->can_merge = 0; /* Don't allow merging with this */
481 info->ptrs.done = FDT_DONE_MEM_RSVMAP;
485 * Work through the tags one by one, deciding whether each needs to
486 * be included or not. We set the variable 'include' to indicate our
487 * decision. 'want' is used to track what we want to include - it
488 * allows us to pick up all the properties (and/or subnode tags) of
491 while (info->ptrs.done < FDT_DONE_STRUCT) {
492 const struct fdt_property *prop;
493 struct fdt_region_ptrs p;
503 * Make a copy of our pointers. If we make it to the end of
504 * this block then we will commit them back to info->ptrs.
505 * Otherwise we can try again from the same starting state
506 * next time we are called.
511 * Find the tag, and the offset of the next one. If we need to
512 * stop including tags, then by default we stop *after*
513 * including the current tag
515 offset = p.nextoffset;
516 tag = fdt_next_tag(fdt, offset, &p.nextoffset);
517 stop_at = p.nextoffset;
522 prop = fdt_get_property_by_offset(fdt, offset, NULL);
523 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
524 val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
527 include = p.want >= WANT_NODES_AND_PROPS;
531 * Make sure we include the } for this block.
532 * It might be more correct to have this done
533 * by the call to fdt_include_supernodes() in
534 * the case where it adds the node we are
535 * currently in, but this is equivalent.
537 if ((flags & FDT_REG_SUPERNODES) && val &&
539 p.want = WANT_NODES_ONLY;
542 /* Value grepping is not yet supported */
546 include = p.want >= WANT_NODES_AND_PROPS;
553 if (p.depth == FDT_MAX_DEPTH)
554 return -FDT_ERR_BADSTRUCTURE;
555 name = fdt_get_name(fdt, offset, &len);
556 if (p.end - path + 2 + len >= path_len)
557 return -FDT_ERR_NOSPACE;
559 /* Build the full path of this node */
560 if (p.end != path + 1)
564 info->stack[p.depth].want = p.want;
565 info->stack[p.depth].offset = offset;
568 * If we are not intending to include this node unless
569 * it matches, make sure we stop *before* its tag.
571 if (p.want == WANT_NODES_ONLY ||
572 !(flags & (FDT_REG_DIRECT_SUBNODES |
573 FDT_REG_ALL_SUBNODES))) {
575 p.want = WANT_NOTHING;
577 val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
580 /* Include this if requested */
582 p.want = (flags & FDT_REG_ALL_SUBNODES) ?
583 WANT_ALL_NODES_AND_PROPS :
584 WANT_NODES_AND_PROPS;
587 /* If not requested, decay our 'p.want' value */
589 if (p.want != WANT_ALL_NODES_AND_PROPS)
592 /* Not including this tag, so stop now */
598 * Decide whether to include this tag, and update our
599 * stack with the state for this node
602 info->stack[p.depth].included = include;
608 return -FDT_ERR_BADSTRUCTURE;
611 * If we don't want this node, stop right away, unless
612 * we are including subnodes
614 if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
616 p.want = info->stack[p.depth].want;
618 while (p.end > path && *--p.end != '/')
624 /* We always include the end tag */
626 p.done = FDT_DONE_STRUCT;
630 /* If this tag is to be included, mark it as region start */
631 if (include && info->start == -1) {
632 /* Include any supernodes required by this one */
633 if (flags & FDT_REG_SUPERNODES) {
634 if (fdt_include_supernodes(info, p.depth))
637 info->start = offset;
641 * If this tag is not to be included, finish up the current
644 if (!include && info->start != -1) {
646 info->have_node = true;
647 if (fdt_add_region(info, base + info->start,
648 stop_at - info->start))
654 /* If we have made it this far, we can commit our pointers */
658 if (info->ptrs.done < FDT_DONE_EMPTY) {
660 * Handle a special case where no nodes have been written. Write
661 * the first { so we have at least something, since
662 * FDT_REG_SUPERNODES means that a valid tree is required. A
663 * tree with no nodes is not valid
665 if ((flags & FDT_REG_SUPERNODES) && !info->have_node &&
667 /* Output the FDT_BEGIN_NODE and the empty name */
668 if (fdt_add_region(info, base, 8))
674 /* Add a region for the END tag and a separate one for string table */
675 if (info->ptrs.done < FDT_DONE_END) {
676 if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
677 return -FDT_ERR_BADSTRUCTURE;
679 /* Output the } before the end tag to finish it off */
680 if (info->start == fdt_size_dt_struct(fdt) - 4)
683 if (fdt_add_region(info, base + info->start,
684 info->ptrs.nextoffset - info->start))
688 if (info->ptrs.done < FDT_DONE_STRINGS) {
689 if (flags & FDT_REG_ADD_STRING_TAB) {
691 if (fdt_off_dt_strings(fdt) <
692 base + info->ptrs.nextoffset)
693 return -FDT_ERR_BADLAYOUT;
694 if (fdt_add_region(info, fdt_off_dt_strings(fdt),
695 fdt_size_dt_strings(fdt)))
701 return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;