1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2016 Free Electrons
5 * Copyright (C) 2016 NextThing Co.
7 #include "libfdt_env.h"
12 #include "libfdt_internal.h"
15 * overlay_get_target_phandle - retrieves the target phandle of a fragment
16 * @fdto: pointer to the device tree overlay blob
17 * @fragment: node offset of the fragment in the overlay
19 * overlay_get_target_phandle() retrieves the target phandle of an
20 * overlay fragment when that fragment uses a phandle (target
21 * property) instead of a path (target-path property).
24 * the phandle pointed by the target property
25 * 0, if the phandle was not found
26 * -1, if the phandle was malformed
28 static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
33 val = fdt_getprop(fdto, fragment, "target", &len);
37 if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
40 return fdt32_to_cpu(*val);
44 * overlay_get_target - retrieves the offset of a fragment's target
45 * @fdt: Base device tree blob
46 * @fdto: Device tree overlay blob
47 * @fragment: node offset of the fragment in the overlay
48 * @pathp: pointer which receives the path of the target (or NULL)
50 * overlay_get_target() retrieves the target offset in the base
51 * device tree of a fragment, no matter how the actual targeting is
52 * done (through a phandle or a path)
55 * the targeted node offset in the base device tree
56 * Negative error code on error
58 static int overlay_get_target(const void *fdt, const void *fdto,
59 int fragment, char const **pathp)
62 const char *path = NULL;
63 int path_len = 0, ret;
65 /* Try first to do a phandle based lookup */
66 phandle = overlay_get_target_phandle(fdto, fragment);
67 if (phandle == (uint32_t)-1)
68 return -FDT_ERR_BADPHANDLE;
70 /* no phandle, try path */
72 /* And then a path based lookup */
73 path = fdt_getprop(fdto, fragment, "target-path", &path_len);
75 ret = fdt_path_offset(fdt, path);
79 ret = fdt_node_offset_by_phandle(fdt, phandle);
82 * If we haven't found either a target or a
83 * target-path property in a node that contains a
84 * __overlay__ subnode (we wouldn't be called
85 * otherwise), consider it a improperly written
88 if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
89 ret = -FDT_ERR_BADOVERLAY;
95 /* return pointer to path (if available) */
97 *pathp = path ? path : NULL;
103 * overlay_phandle_add_offset - Increases a phandle by an offset
104 * @fdt: Base device tree blob
105 * @node: Device tree overlay blob
106 * @name: Name of the property to modify (phandle or linux,phandle)
107 * @delta: offset to apply
109 * overlay_phandle_add_offset() increments a node phandle by a given
114 * Negative error code on error
116 static int overlay_phandle_add_offset(void *fdt, int node,
117 const char *name, uint32_t delta)
123 val = fdt_getprop(fdt, node, name, &len);
127 if (len != sizeof(*val))
128 return -FDT_ERR_BADPHANDLE;
130 adj_val = fdt32_to_cpu(*val);
131 if ((adj_val + delta) < adj_val)
132 return -FDT_ERR_NOPHANDLES;
135 if (adj_val == (uint32_t)-1)
136 return -FDT_ERR_NOPHANDLES;
138 return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
142 * overlay_adjust_node_phandles - Offsets the phandles of a node
143 * @fdto: Device tree overlay blob
144 * @node: Offset of the node we want to adjust
145 * @delta: Offset to shift the phandles of
147 * overlay_adjust_node_phandles() adds a constant to all the phandles
148 * of a given node. This is mainly use as part of the overlay
149 * application process, when we want to update all the overlay
150 * phandles to not conflict with the overlays of the base device tree.
154 * Negative error code on failure
156 static int overlay_adjust_node_phandles(void *fdto, int node,
162 ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
163 if (ret && ret != -FDT_ERR_NOTFOUND)
166 ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
167 if (ret && ret != -FDT_ERR_NOTFOUND)
170 fdt_for_each_subnode(child, fdto, node) {
171 ret = overlay_adjust_node_phandles(fdto, child, delta);
180 * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
181 * @fdto: Device tree overlay blob
182 * @delta: Offset to shift the phandles of
184 * overlay_adjust_local_phandles() adds a constant to all the
185 * phandles of an overlay. This is mainly use as part of the overlay
186 * application process, when we want to update all the overlay
187 * phandles to not conflict with the overlays of the base device tree.
191 * Negative error code on failure
193 static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
196 * Start adjusting the phandles from the overlay root
198 return overlay_adjust_node_phandles(fdto, 0, delta);
202 * overlay_update_local_node_references - Adjust the overlay references
203 * @fdto: Device tree overlay blob
204 * @tree_node: Node offset of the node to operate on
205 * @fixup_node: Node offset of the matching local fixups node
206 * @delta: Offset to shift the phandles of
208 * overlay_update_local_nodes_references() update the phandles
209 * pointing to a node within the device tree overlay by adding a
212 * This is mainly used as part of a device tree application process,
213 * where you want the device tree overlays phandles to not conflict
214 * with the ones from the base device tree before merging them.
218 * Negative error code on failure
220 static int overlay_update_local_node_references(void *fdto,
229 fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
230 const fdt32_t *fixup_val;
231 const char *tree_val;
237 fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
242 if (fixup_len % sizeof(uint32_t))
243 return -FDT_ERR_BADOVERLAY;
244 fixup_len /= sizeof(uint32_t);
246 tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
248 if (tree_len == -FDT_ERR_NOTFOUND)
249 return -FDT_ERR_BADOVERLAY;
254 for (i = 0; i < fixup_len; i++) {
258 poffset = fdt32_to_cpu(fixup_val[i]);
261 * phandles to fixup can be unaligned.
263 * Use a memcpy for the architectures that do
264 * not support unaligned accesses.
266 memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
268 adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
270 ret = fdt_setprop_inplace_namelen_partial(fdto,
277 if (ret == -FDT_ERR_NOSPACE)
278 return -FDT_ERR_BADOVERLAY;
285 fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
286 const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
290 tree_child = fdt_subnode_offset(fdto, tree_node,
292 if (tree_child == -FDT_ERR_NOTFOUND)
293 return -FDT_ERR_BADOVERLAY;
297 ret = overlay_update_local_node_references(fdto,
309 * overlay_update_local_references - Adjust the overlay references
310 * @fdto: Device tree overlay blob
311 * @delta: Offset to shift the phandles of
313 * overlay_update_local_references() update all the phandles pointing
314 * to a node within the device tree overlay by adding a constant
315 * delta to not conflict with the base overlay.
317 * This is mainly used as part of a device tree application process,
318 * where you want the device tree overlays phandles to not conflict
319 * with the ones from the base device tree before merging them.
323 * Negative error code on failure
325 static int overlay_update_local_references(void *fdto, uint32_t delta)
329 fixups = fdt_path_offset(fdto, "/__local_fixups__");
331 /* There's no local phandles to adjust, bail out */
332 if (fixups == -FDT_ERR_NOTFOUND)
339 * Update our local references from the root of the tree
341 return overlay_update_local_node_references(fdto, 0, fixups,
346 * overlay_fixup_one_phandle - Set an overlay phandle to the base one
347 * @fdt: Base Device Tree blob
348 * @fdto: Device tree overlay blob
349 * @symbols_off: Node offset of the symbols node in the base device tree
350 * @path: Path to a node holding a phandle in the overlay
351 * @path_len: number of path characters to consider
352 * @name: Name of the property holding the phandle reference in the overlay
353 * @name_len: number of name characters to consider
354 * @poffset: Offset within the overlay property where the phandle is stored
355 * @label: Label of the node referenced by the phandle
357 * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
358 * a node in the base device tree.
360 * This is part of the device tree overlay application process, when
361 * you want all the phandles in the overlay to point to the actual
366 * Negative error code on failure
368 static int overlay_fixup_one_phandle(void *fdt, void *fdto,
370 const char *path, uint32_t path_len,
371 const char *name, uint32_t name_len,
372 int poffset, const char *label)
374 const char *symbol_path;
376 fdt32_t phandle_prop;
377 int symbol_off, fixup_off;
383 symbol_path = fdt_getprop(fdt, symbols_off, label,
388 symbol_off = fdt_path_offset(fdt, symbol_path);
392 phandle = fdt_get_phandle(fdt, symbol_off);
394 return -FDT_ERR_NOTFOUND;
396 fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
397 if (fixup_off == -FDT_ERR_NOTFOUND)
398 return -FDT_ERR_BADOVERLAY;
402 phandle_prop = cpu_to_fdt32(phandle);
403 return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
404 name, name_len, poffset,
406 sizeof(phandle_prop));
410 * overlay_fixup_phandle - Set an overlay phandle to the base one
411 * @fdt: Base Device Tree blob
412 * @fdto: Device tree overlay blob
413 * @symbols_off: Node offset of the symbols node in the base device tree
414 * @property: Property offset in the overlay holding the list of fixups
416 * overlay_fixup_phandle() resolves all the overlay phandles pointed
417 * to in a __fixups__ property, and updates them to match the phandles
418 * in use in the base device tree.
420 * This is part of the device tree overlay application process, when
421 * you want all the phandles in the overlay to point to the actual
426 * Negative error code on failure
428 static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
435 value = fdt_getprop_by_offset(fdto, property,
438 if (len == -FDT_ERR_NOTFOUND)
439 return -FDT_ERR_INTERNAL;
445 const char *path, *name, *fixup_end;
446 const char *fixup_str = value;
447 uint32_t path_len, name_len;
452 fixup_end = memchr(value, '\0', len);
454 return -FDT_ERR_BADOVERLAY;
455 fixup_len = fixup_end - fixup_str;
457 len -= fixup_len + 1;
458 value += fixup_len + 1;
461 sep = memchr(fixup_str, ':', fixup_len);
462 if (!sep || *sep != ':')
463 return -FDT_ERR_BADOVERLAY;
465 path_len = sep - path;
466 if (path_len == (fixup_len - 1))
467 return -FDT_ERR_BADOVERLAY;
469 fixup_len -= path_len + 1;
471 sep = memchr(name, ':', fixup_len);
472 if (!sep || *sep != ':')
473 return -FDT_ERR_BADOVERLAY;
475 name_len = sep - name;
477 return -FDT_ERR_BADOVERLAY;
479 poffset = strtoul(sep + 1, &endptr, 10);
480 if ((*endptr != '\0') || (endptr <= (sep + 1)))
481 return -FDT_ERR_BADOVERLAY;
483 ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
484 path, path_len, name, name_len,
494 * overlay_fixup_phandles - Resolve the overlay phandles to the base
496 * @fdt: Base Device Tree blob
497 * @fdto: Device tree overlay blob
499 * overlay_fixup_phandles() resolves all the overlay phandles pointing
500 * to nodes in the base device tree.
502 * This is one of the steps of the device tree overlay application
503 * process, when you want all the phandles in the overlay to point to
504 * the actual base dt nodes.
508 * Negative error code on failure
510 static int overlay_fixup_phandles(void *fdt, void *fdto)
512 int fixups_off, symbols_off;
515 /* We can have overlays without any fixups */
516 fixups_off = fdt_path_offset(fdto, "/__fixups__");
517 if (fixups_off == -FDT_ERR_NOTFOUND)
518 return 0; /* nothing to do */
522 /* And base DTs without symbols */
523 symbols_off = fdt_path_offset(fdt, "/__symbols__");
524 if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
527 fdt_for_each_property_offset(property, fdto, fixups_off) {
530 ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
539 * overlay_apply_node - Merges a node into the base device tree
540 * @fdt: Base Device Tree blob
541 * @target: Node offset in the base device tree to apply the fragment to
542 * @fdto: Device tree overlay blob
543 * @node: Node offset in the overlay holding the changes to merge
545 * overlay_apply_node() merges a node into a target base device tree
548 * This is part of the final step in the device tree overlay
549 * application process, when all the phandles have been adjusted and
550 * resolved and you just have to merge overlay into the base device
555 * Negative error code on failure
557 static int overlay_apply_node(void *fdt, int target,
558 void *fdto, int node)
563 fdt_for_each_property_offset(property, fdto, node) {
569 prop = fdt_getprop_by_offset(fdto, property, &name,
571 if (prop_len == -FDT_ERR_NOTFOUND)
572 return -FDT_ERR_INTERNAL;
576 ret = fdt_setprop(fdt, target, name, prop, prop_len);
581 fdt_for_each_subnode(subnode, fdto, node) {
582 const char *name = fdt_get_name(fdto, subnode, NULL);
586 nnode = fdt_add_subnode(fdt, target, name);
587 if (nnode == -FDT_ERR_EXISTS) {
588 nnode = fdt_subnode_offset(fdt, target, name);
589 if (nnode == -FDT_ERR_NOTFOUND)
590 return -FDT_ERR_INTERNAL;
596 ret = overlay_apply_node(fdt, nnode, fdto, subnode);
605 * overlay_merge - Merge an overlay into its base device tree
606 * @fdt: Base Device Tree blob
607 * @fdto: Device tree overlay blob
609 * overlay_merge() merges an overlay into its base device tree.
611 * This is the next to last step in the device tree overlay application
612 * process, when all the phandles have been adjusted and resolved and
613 * you just have to merge overlay into the base device tree.
617 * Negative error code on failure
619 static int overlay_merge(void *fdt, void *fdto)
623 fdt_for_each_subnode(fragment, fdto, 0) {
629 * Each fragments will have an __overlay__ node. If
630 * they don't, it's not supposed to be merged
632 overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
633 if (overlay == -FDT_ERR_NOTFOUND)
639 target = overlay_get_target(fdt, fdto, fragment, NULL);
643 ret = overlay_apply_node(fdt, target, fdto, overlay);
651 static int get_path_len(const void *fdt, int nodeoffset)
653 int len = 0, namelen;
659 name = fdt_get_name(fdt, nodeoffset, &namelen);
663 /* root? we're done */
667 nodeoffset = fdt_parent_offset(fdt, nodeoffset);
673 /* in case of root pretend it's "/" */
680 * overlay_symbol_update - Update the symbols of base tree after a merge
681 * @fdt: Base Device Tree blob
682 * @fdto: Device tree overlay blob
684 * overlay_symbol_update() updates the symbols of the base tree with the
685 * symbols of the applied overlay
687 * This is the last step in the device tree overlay application
688 * process, allowing the reference of overlay symbols by subsequent
689 * overlay operations.
693 * Negative error code on failure
695 static int overlay_symbol_update(void *fdt, void *fdto)
697 int root_sym, ov_sym, prop, path_len, fragment, target;
698 int len, frag_name_len, ret, rel_path_len;
702 const char *frag_name;
703 const char *rel_path;
704 const char *target_path;
708 ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
710 /* if no overlay symbols exist no problem */
714 root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
716 /* it no root symbols exist we should create them */
717 if (root_sym == -FDT_ERR_NOTFOUND)
718 root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
720 /* any error is fatal now */
724 /* iterate over each overlay symbol */
725 fdt_for_each_property_offset(prop, fdto, ov_sym) {
726 path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
730 /* verify it's a string property (terminated by a single \0) */
731 if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
732 return -FDT_ERR_BADVALUE;
734 /* keep end marker to avoid strlen() */
738 return -FDT_ERR_BADVALUE;
740 /* get fragment name first */
741 s = strchr(path + 1, '/');
743 /* Symbol refers to something that won't end
744 * up in the target tree */
748 frag_name = path + 1;
749 frag_name_len = s - path - 1;
751 /* verify format; safe since "s" lies in \0 terminated prop */
752 len = sizeof("/__overlay__/") - 1;
753 if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
754 /* /<fragment-name>/__overlay__/<relative-subnode-path> */
756 rel_path_len = e - rel_path - 1;
757 } else if ((e - s) == len
758 && (memcmp(s, "/__overlay__", len - 1) == 0)) {
759 /* /<fragment-name>/__overlay__ */
763 /* Symbol refers to something that won't end
764 * up in the target tree */
768 /* find the fragment index in which the symbol lies */
769 ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
773 return -FDT_ERR_BADOVERLAY;
776 /* an __overlay__ subnode must exist */
777 ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
779 return -FDT_ERR_BADOVERLAY;
781 /* get the target of the fragment */
782 ret = overlay_get_target(fdt, fdto, fragment, &target_path);
787 /* if we have a target path use */
789 ret = get_path_len(fdt, target);
794 len = strlen(target_path);
797 ret = fdt_setprop_placeholder(fdt, root_sym, name,
798 len + (len > 1) + rel_path_len + 1, &p);
803 /* again in case setprop_placeholder changed it */
804 ret = overlay_get_target(fdt, fdto, fragment, &target_path);
811 if (len > 1) { /* target is not root */
813 ret = fdt_get_path(fdt, target, buf, len + 1);
817 memcpy(buf, target_path, len + 1);
823 memcpy(buf + len + 1, rel_path, rel_path_len);
824 buf[len + 1 + rel_path_len] = '\0';
830 int fdt_overlay_apply(void *fdt, void *fdto)
838 ret = fdt_find_max_phandle(fdt, &delta);
842 ret = overlay_adjust_local_phandles(fdto, delta);
846 ret = overlay_update_local_references(fdto, delta);
850 ret = overlay_fixup_phandles(fdt, fdto);
854 ret = overlay_merge(fdt, fdto);
858 ret = overlay_symbol_update(fdt, fdto);
863 * The overlay has been damaged, erase its magic.
865 fdt_set_magic(fdto, ~0);
871 * The overlay might have been damaged, erase its magic.
873 fdt_set_magic(fdto, ~0);
876 * The base device tree might have been damaged, erase its
879 fdt_set_magic(fdt, ~0);