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);
43 int fdt_overlay_target_offset(const void *fdt, const void *fdto,
44 int fragment_offset, char const **pathp)
47 const char *path = NULL;
48 int path_len = 0, ret;
50 /* Try first to do a phandle based lookup */
51 phandle = overlay_get_target_phandle(fdto, fragment_offset);
52 if (phandle == (uint32_t)-1)
53 return -FDT_ERR_BADPHANDLE;
55 /* no phandle, try path */
57 /* And then a path based lookup */
58 path = fdt_getprop(fdto, fragment_offset, "target-path", &path_len);
60 ret = fdt_path_offset(fdt, path);
64 ret = fdt_node_offset_by_phandle(fdt, phandle);
67 * If we haven't found either a target or a
68 * target-path property in a node that contains a
69 * __overlay__ subnode (we wouldn't be called
70 * otherwise), consider it a improperly written
73 if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
74 ret = -FDT_ERR_BADOVERLAY;
80 /* return pointer to path (if available) */
82 *pathp = path ? path : NULL;
88 * overlay_phandle_add_offset - Increases a phandle by an offset
89 * @fdt: Base device tree blob
90 * @node: Device tree overlay blob
91 * @name: Name of the property to modify (phandle or linux,phandle)
92 * @delta: offset to apply
94 * overlay_phandle_add_offset() increments a node phandle by a given
99 * Negative error code on error
101 static int overlay_phandle_add_offset(void *fdt, int node,
102 const char *name, uint32_t delta)
108 val = fdt_getprop(fdt, node, name, &len);
112 if (len != sizeof(*val))
113 return -FDT_ERR_BADPHANDLE;
115 adj_val = fdt32_to_cpu(*val);
116 if ((adj_val + delta) < adj_val)
117 return -FDT_ERR_NOPHANDLES;
120 if (adj_val == (uint32_t)-1)
121 return -FDT_ERR_NOPHANDLES;
123 return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
127 * overlay_adjust_node_phandles - Offsets the phandles of a node
128 * @fdto: Device tree overlay blob
129 * @node: Offset of the node we want to adjust
130 * @delta: Offset to shift the phandles of
132 * overlay_adjust_node_phandles() adds a constant to all the phandles
133 * of a given node. This is mainly use as part of the overlay
134 * application process, when we want to update all the overlay
135 * phandles to not conflict with the overlays of the base device tree.
139 * Negative error code on failure
141 static int overlay_adjust_node_phandles(void *fdto, int node,
147 ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
148 if (ret && ret != -FDT_ERR_NOTFOUND)
151 ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
152 if (ret && ret != -FDT_ERR_NOTFOUND)
155 fdt_for_each_subnode(child, fdto, node) {
156 ret = overlay_adjust_node_phandles(fdto, child, delta);
165 * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
166 * @fdto: Device tree overlay blob
167 * @delta: Offset to shift the phandles of
169 * overlay_adjust_local_phandles() adds a constant to all the
170 * phandles of an overlay. This is mainly use as part of the overlay
171 * application process, when we want to update all the overlay
172 * phandles to not conflict with the overlays of the base device tree.
176 * Negative error code on failure
178 static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
181 * Start adjusting the phandles from the overlay root
183 return overlay_adjust_node_phandles(fdto, 0, delta);
187 * overlay_update_local_node_references - Adjust the overlay references
188 * @fdto: Device tree overlay blob
189 * @tree_node: Node offset of the node to operate on
190 * @fixup_node: Node offset of the matching local fixups node
191 * @delta: Offset to shift the phandles of
193 * overlay_update_local_nodes_references() update the phandles
194 * pointing to a node within the device tree overlay by adding a
197 * This is mainly used as part of a device tree application process,
198 * where you want the device tree overlays phandles to not conflict
199 * with the ones from the base device tree before merging them.
203 * Negative error code on failure
205 static int overlay_update_local_node_references(void *fdto,
214 fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
215 const fdt32_t *fixup_val;
216 const char *tree_val;
222 fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
227 if (fixup_len % sizeof(uint32_t))
228 return -FDT_ERR_BADOVERLAY;
229 fixup_len /= sizeof(uint32_t);
231 tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
233 if (tree_len == -FDT_ERR_NOTFOUND)
234 return -FDT_ERR_BADOVERLAY;
239 for (i = 0; i < fixup_len; i++) {
243 poffset = fdt32_to_cpu(fixup_val[i]);
246 * phandles to fixup can be unaligned.
248 * Use a memcpy for the architectures that do
249 * not support unaligned accesses.
251 memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
253 adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
255 ret = fdt_setprop_inplace_namelen_partial(fdto,
262 if (ret == -FDT_ERR_NOSPACE)
263 return -FDT_ERR_BADOVERLAY;
270 fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
271 const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
275 tree_child = fdt_subnode_offset(fdto, tree_node,
277 if (tree_child == -FDT_ERR_NOTFOUND)
278 return -FDT_ERR_BADOVERLAY;
282 ret = overlay_update_local_node_references(fdto,
294 * overlay_update_local_references - Adjust the overlay references
295 * @fdto: Device tree overlay blob
296 * @delta: Offset to shift the phandles of
298 * overlay_update_local_references() update all the phandles pointing
299 * to a node within the device tree overlay by adding a constant
300 * delta to not conflict with the base overlay.
302 * This is mainly used as part of a device tree application process,
303 * where you want the device tree overlays phandles to not conflict
304 * with the ones from the base device tree before merging them.
308 * Negative error code on failure
310 static int overlay_update_local_references(void *fdto, uint32_t delta)
314 fixups = fdt_path_offset(fdto, "/__local_fixups__");
316 /* There's no local phandles to adjust, bail out */
317 if (fixups == -FDT_ERR_NOTFOUND)
324 * Update our local references from the root of the tree
326 return overlay_update_local_node_references(fdto, 0, fixups,
331 * overlay_fixup_one_phandle - Set an overlay phandle to the base one
332 * @fdt: Base Device Tree blob
333 * @fdto: Device tree overlay blob
334 * @symbols_off: Node offset of the symbols node in the base device tree
335 * @path: Path to a node holding a phandle in the overlay
336 * @path_len: number of path characters to consider
337 * @name: Name of the property holding the phandle reference in the overlay
338 * @name_len: number of name characters to consider
339 * @poffset: Offset within the overlay property where the phandle is stored
340 * @label: Label of the node referenced by the phandle
342 * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
343 * a node in the base device tree.
345 * This is part of the device tree overlay application process, when
346 * you want all the phandles in the overlay to point to the actual
351 * Negative error code on failure
353 static int overlay_fixup_one_phandle(void *fdt, void *fdto,
355 const char *path, uint32_t path_len,
356 const char *name, uint32_t name_len,
357 int poffset, const char *label)
359 const char *symbol_path;
361 fdt32_t phandle_prop;
362 int symbol_off, fixup_off;
368 symbol_path = fdt_getprop(fdt, symbols_off, label,
373 symbol_off = fdt_path_offset(fdt, symbol_path);
377 phandle = fdt_get_phandle(fdt, symbol_off);
379 return -FDT_ERR_NOTFOUND;
381 fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
382 if (fixup_off == -FDT_ERR_NOTFOUND)
383 return -FDT_ERR_BADOVERLAY;
387 phandle_prop = cpu_to_fdt32(phandle);
388 return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
389 name, name_len, poffset,
391 sizeof(phandle_prop));
395 * overlay_fixup_phandle - Set an overlay phandle to the base one
396 * @fdt: Base Device Tree blob
397 * @fdto: Device tree overlay blob
398 * @symbols_off: Node offset of the symbols node in the base device tree
399 * @property: Property offset in the overlay holding the list of fixups
401 * overlay_fixup_phandle() resolves all the overlay phandles pointed
402 * to in a __fixups__ property, and updates them to match the phandles
403 * in use in the base device tree.
405 * This is part of the device tree overlay application process, when
406 * you want all the phandles in the overlay to point to the actual
411 * Negative error code on failure
413 static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
420 value = fdt_getprop_by_offset(fdto, property,
423 if (len == -FDT_ERR_NOTFOUND)
424 return -FDT_ERR_INTERNAL;
430 const char *path, *name, *fixup_end;
431 const char *fixup_str = value;
432 uint32_t path_len, name_len;
437 fixup_end = memchr(value, '\0', len);
439 return -FDT_ERR_BADOVERLAY;
440 fixup_len = fixup_end - fixup_str;
442 len -= fixup_len + 1;
443 value += fixup_len + 1;
446 sep = memchr(fixup_str, ':', fixup_len);
447 if (!sep || *sep != ':')
448 return -FDT_ERR_BADOVERLAY;
450 path_len = sep - path;
451 if (path_len == (fixup_len - 1))
452 return -FDT_ERR_BADOVERLAY;
454 fixup_len -= path_len + 1;
456 sep = memchr(name, ':', fixup_len);
457 if (!sep || *sep != ':')
458 return -FDT_ERR_BADOVERLAY;
460 name_len = sep - name;
462 return -FDT_ERR_BADOVERLAY;
464 poffset = strtoul(sep + 1, &endptr, 10);
465 if ((*endptr != '\0') || (endptr <= (sep + 1)))
466 return -FDT_ERR_BADOVERLAY;
468 ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
469 path, path_len, name, name_len,
479 * overlay_fixup_phandles - Resolve the overlay phandles to the base
481 * @fdt: Base Device Tree blob
482 * @fdto: Device tree overlay blob
484 * overlay_fixup_phandles() resolves all the overlay phandles pointing
485 * to nodes in the base device tree.
487 * This is one of the steps of the device tree overlay application
488 * process, when you want all the phandles in the overlay to point to
489 * the actual base dt nodes.
493 * Negative error code on failure
495 static int overlay_fixup_phandles(void *fdt, void *fdto)
497 int fixups_off, symbols_off;
500 /* We can have overlays without any fixups */
501 fixups_off = fdt_path_offset(fdto, "/__fixups__");
502 if (fixups_off == -FDT_ERR_NOTFOUND)
503 return 0; /* nothing to do */
507 /* And base DTs without symbols */
508 symbols_off = fdt_path_offset(fdt, "/__symbols__");
509 if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
512 fdt_for_each_property_offset(property, fdto, fixups_off) {
515 ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
524 * overlay_apply_node - Merges a node into the base device tree
525 * @fdt: Base Device Tree blob
526 * @target: Node offset in the base device tree to apply the fragment to
527 * @fdto: Device tree overlay blob
528 * @node: Node offset in the overlay holding the changes to merge
530 * overlay_apply_node() merges a node into a target base device tree
533 * This is part of the final step in the device tree overlay
534 * application process, when all the phandles have been adjusted and
535 * resolved and you just have to merge overlay into the base device
540 * Negative error code on failure
542 static int overlay_apply_node(void *fdt, int target,
543 void *fdto, int node)
548 fdt_for_each_property_offset(property, fdto, node) {
554 prop = fdt_getprop_by_offset(fdto, property, &name,
556 if (prop_len == -FDT_ERR_NOTFOUND)
557 return -FDT_ERR_INTERNAL;
561 ret = fdt_setprop(fdt, target, name, prop, prop_len);
566 fdt_for_each_subnode(subnode, fdto, node) {
567 const char *name = fdt_get_name(fdto, subnode, NULL);
571 nnode = fdt_add_subnode(fdt, target, name);
572 if (nnode == -FDT_ERR_EXISTS) {
573 nnode = fdt_subnode_offset(fdt, target, name);
574 if (nnode == -FDT_ERR_NOTFOUND)
575 return -FDT_ERR_INTERNAL;
581 ret = overlay_apply_node(fdt, nnode, fdto, subnode);
590 * overlay_merge - Merge an overlay into its base device tree
591 * @fdt: Base Device Tree blob
592 * @fdto: Device tree overlay blob
594 * overlay_merge() merges an overlay into its base device tree.
596 * This is the next to last step in the device tree overlay application
597 * process, when all the phandles have been adjusted and resolved and
598 * you just have to merge overlay into the base device tree.
602 * Negative error code on failure
604 static int overlay_merge(void *fdt, void *fdto)
608 fdt_for_each_subnode(fragment, fdto, 0) {
614 * Each fragments will have an __overlay__ node. If
615 * they don't, it's not supposed to be merged
617 overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
618 if (overlay == -FDT_ERR_NOTFOUND)
624 target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
628 ret = overlay_apply_node(fdt, target, fdto, overlay);
636 static int get_path_len(const void *fdt, int nodeoffset)
638 int len = 0, namelen;
644 name = fdt_get_name(fdt, nodeoffset, &namelen);
648 /* root? we're done */
652 nodeoffset = fdt_parent_offset(fdt, nodeoffset);
658 /* in case of root pretend it's "/" */
665 * overlay_symbol_update - Update the symbols of base tree after a merge
666 * @fdt: Base Device Tree blob
667 * @fdto: Device tree overlay blob
669 * overlay_symbol_update() updates the symbols of the base tree with the
670 * symbols of the applied overlay
672 * This is the last step in the device tree overlay application
673 * process, allowing the reference of overlay symbols by subsequent
674 * overlay operations.
678 * Negative error code on failure
680 static int overlay_symbol_update(void *fdt, void *fdto)
682 int root_sym, ov_sym, prop, path_len, fragment, target;
683 int len, frag_name_len, ret, rel_path_len;
687 const char *frag_name;
688 const char *rel_path;
689 const char *target_path;
693 ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
695 /* if no overlay symbols exist no problem */
699 root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
701 /* it no root symbols exist we should create them */
702 if (root_sym == -FDT_ERR_NOTFOUND)
703 root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
705 /* any error is fatal now */
709 /* iterate over each overlay symbol */
710 fdt_for_each_property_offset(prop, fdto, ov_sym) {
711 path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
715 /* verify it's a string property (terminated by a single \0) */
716 if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
717 return -FDT_ERR_BADVALUE;
719 /* keep end marker to avoid strlen() */
723 return -FDT_ERR_BADVALUE;
725 /* get fragment name first */
726 s = strchr(path + 1, '/');
728 /* Symbol refers to something that won't end
729 * up in the target tree */
733 frag_name = path + 1;
734 frag_name_len = s - path - 1;
736 /* verify format; safe since "s" lies in \0 terminated prop */
737 len = sizeof("/__overlay__/") - 1;
738 if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
739 /* /<fragment-name>/__overlay__/<relative-subnode-path> */
741 rel_path_len = e - rel_path - 1;
742 } else if ((e - s) == len
743 && (memcmp(s, "/__overlay__", len - 1) == 0)) {
744 /* /<fragment-name>/__overlay__ */
748 /* Symbol refers to something that won't end
749 * up in the target tree */
753 /* find the fragment index in which the symbol lies */
754 ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
758 return -FDT_ERR_BADOVERLAY;
761 /* an __overlay__ subnode must exist */
762 ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
764 return -FDT_ERR_BADOVERLAY;
766 /* get the target of the fragment */
767 ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
772 /* if we have a target path use */
774 ret = get_path_len(fdt, target);
779 len = strlen(target_path);
782 ret = fdt_setprop_placeholder(fdt, root_sym, name,
783 len + (len > 1) + rel_path_len + 1, &p);
788 /* again in case setprop_placeholder changed it */
789 ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
796 if (len > 1) { /* target is not root */
798 ret = fdt_get_path(fdt, target, buf, len + 1);
802 memcpy(buf, target_path, len + 1);
808 memcpy(buf + len + 1, rel_path, rel_path_len);
809 buf[len + 1 + rel_path_len] = '\0';
815 int fdt_overlay_apply(void *fdt, void *fdto)
823 ret = fdt_find_max_phandle(fdt, &delta);
827 ret = overlay_adjust_local_phandles(fdto, delta);
831 ret = overlay_update_local_references(fdto, delta);
835 ret = overlay_fixup_phandles(fdt, fdto);
839 ret = overlay_merge(fdt, fdto);
843 ret = overlay_symbol_update(fdt, fdto);
848 * The overlay has been damaged, erase its magic.
850 fdt_set_magic(fdto, ~0);
856 * The overlay might have been damaged, erase its magic.
858 fdt_set_magic(fdto, ~0);
861 * The base device tree might have been damaged, erase its
864 fdt_set_magic(fdt, ~0);