1 #include "libfdt_env.h"
6 #include "libfdt_internal.h"
9 * overlay_get_target_phandle - retrieves the target phandle of a fragment
10 * @fdto: pointer to the device tree overlay blob
11 * @fragment: node offset of the fragment in the overlay
13 * overlay_get_target_phandle() retrieves the target phandle of an
14 * overlay fragment when that fragment uses a phandle (target
15 * property) instead of a path (target-path property).
18 * the phandle pointed by the target property
19 * 0, if the phandle was not found
20 * -1, if the phandle was malformed
22 static uint32_t overlay_get_target_phandle(const void *fdto
, int fragment
)
27 val
= fdt_getprop(fdto
, fragment
, "target", &len
);
31 if ((len
!= sizeof(*val
)) || (fdt32_to_cpu(*val
) == (uint32_t)-1))
34 return fdt32_to_cpu(*val
);
38 * overlay_get_target - retrieves the offset of a fragment's target
39 * @fdt: Base device tree blob
40 * @fdto: Device tree overlay blob
41 * @fragment: node offset of the fragment in the overlay
42 * @pathp: pointer which receives the path of the target (or NULL)
44 * overlay_get_target() retrieves the target offset in the base
45 * device tree of a fragment, no matter how the actual targetting is
46 * done (through a phandle or a path)
49 * the targetted node offset in the base device tree
50 * Negative error code on error
52 static int overlay_get_target(const void *fdt
, const void *fdto
,
53 int fragment
, char const **pathp
)
56 const char *path
= NULL
;
57 int path_len
= 0, ret
;
59 /* Try first to do a phandle based lookup */
60 phandle
= overlay_get_target_phandle(fdto
, fragment
);
61 if (phandle
== (uint32_t)-1)
62 return -FDT_ERR_BADPHANDLE
;
64 /* no phandle, try path */
66 /* And then a path based lookup */
67 path
= fdt_getprop(fdto
, fragment
, "target-path", &path_len
);
69 ret
= fdt_path_offset(fdt
, path
);
73 ret
= fdt_node_offset_by_phandle(fdt
, phandle
);
76 * If we haven't found either a target or a
77 * target-path property in a node that contains a
78 * __overlay__ subnode (we wouldn't be called
79 * otherwise), consider it a improperly written
82 if (ret
< 0 && path_len
== -FDT_ERR_NOTFOUND
)
83 ret
= -FDT_ERR_BADOVERLAY
;
89 /* return pointer to path (if available) */
91 *pathp
= path
? path
: NULL
;
97 * overlay_phandle_add_offset - Increases a phandle by an offset
98 * @fdt: Base device tree blob
99 * @node: Device tree overlay blob
100 * @name: Name of the property to modify (phandle or linux,phandle)
101 * @delta: offset to apply
103 * overlay_phandle_add_offset() increments a node phandle by a given
108 * Negative error code on error
110 static int overlay_phandle_add_offset(void *fdt
, int node
,
111 const char *name
, uint32_t delta
)
117 val
= fdt_getprop(fdt
, node
, name
, &len
);
121 if (len
!= sizeof(*val
))
122 return -FDT_ERR_BADPHANDLE
;
124 adj_val
= fdt32_to_cpu(*val
);
125 if ((adj_val
+ delta
) < adj_val
)
126 return -FDT_ERR_NOPHANDLES
;
129 if (adj_val
== (uint32_t)-1)
130 return -FDT_ERR_NOPHANDLES
;
132 return fdt_setprop_inplace_u32(fdt
, node
, name
, adj_val
);
136 * overlay_adjust_node_phandles - Offsets the phandles of a node
137 * @fdto: Device tree overlay blob
138 * @node: Offset of the node we want to adjust
139 * @delta: Offset to shift the phandles of
141 * overlay_adjust_node_phandles() adds a constant to all the phandles
142 * of a given node. This is mainly use as part of the overlay
143 * application process, when we want to update all the overlay
144 * phandles to not conflict with the overlays of the base device tree.
148 * Negative error code on failure
150 static int overlay_adjust_node_phandles(void *fdto
, int node
,
156 ret
= overlay_phandle_add_offset(fdto
, node
, "phandle", delta
);
157 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
)
160 ret
= overlay_phandle_add_offset(fdto
, node
, "linux,phandle", delta
);
161 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
)
164 fdt_for_each_subnode(child
, fdto
, node
) {
165 ret
= overlay_adjust_node_phandles(fdto
, child
, delta
);
174 * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
175 * @fdto: Device tree overlay blob
176 * @delta: Offset to shift the phandles of
178 * overlay_adjust_local_phandles() adds a constant to all the
179 * phandles of an overlay. This is mainly use as part of the overlay
180 * application process, when we want to update all the overlay
181 * phandles to not conflict with the overlays of the base device tree.
185 * Negative error code on failure
187 static int overlay_adjust_local_phandles(void *fdto
, uint32_t delta
)
190 * Start adjusting the phandles from the overlay root
192 return overlay_adjust_node_phandles(fdto
, 0, delta
);
196 * overlay_update_local_node_references - Adjust the overlay references
197 * @fdto: Device tree overlay blob
198 * @tree_node: Node offset of the node to operate on
199 * @fixup_node: Node offset of the matching local fixups node
200 * @delta: Offset to shift the phandles of
202 * overlay_update_local_nodes_references() update the phandles
203 * pointing to a node within the device tree overlay by adding a
206 * This is mainly used as part of a device tree application process,
207 * where you want the device tree overlays phandles to not conflict
208 * with the ones from the base device tree before merging them.
212 * Negative error code on failure
214 static int overlay_update_local_node_references(void *fdto
,
223 fdt_for_each_property_offset(fixup_prop
, fdto
, fixup_node
) {
224 const fdt32_t
*fixup_val
;
225 const char *tree_val
;
231 fixup_val
= fdt_getprop_by_offset(fdto
, fixup_prop
,
236 if (fixup_len
% sizeof(uint32_t))
237 return -FDT_ERR_BADOVERLAY
;
239 tree_val
= fdt_getprop(fdto
, tree_node
, name
, &tree_len
);
241 if (tree_len
== -FDT_ERR_NOTFOUND
)
242 return -FDT_ERR_BADOVERLAY
;
247 for (i
= 0; i
< (fixup_len
/ sizeof(uint32_t)); i
++) {
251 poffset
= fdt32_to_cpu(fixup_val
[i
]);
254 * phandles to fixup can be unaligned.
256 * Use a memcpy for the architectures that do
257 * not support unaligned accesses.
259 memcpy(&adj_val
, tree_val
+ poffset
, sizeof(adj_val
));
261 adj_val
= cpu_to_fdt32(fdt32_to_cpu(adj_val
) + delta
);
263 ret
= fdt_setprop_inplace_namelen_partial(fdto
,
270 if (ret
== -FDT_ERR_NOSPACE
)
271 return -FDT_ERR_BADOVERLAY
;
278 fdt_for_each_subnode(fixup_child
, fdto
, fixup_node
) {
279 const char *fixup_child_name
= fdt_get_name(fdto
, fixup_child
,
283 tree_child
= fdt_subnode_offset(fdto
, tree_node
,
285 if (tree_child
== -FDT_ERR_NOTFOUND
)
286 return -FDT_ERR_BADOVERLAY
;
290 ret
= overlay_update_local_node_references(fdto
,
302 * overlay_update_local_references - Adjust the overlay references
303 * @fdto: Device tree overlay blob
304 * @delta: Offset to shift the phandles of
306 * overlay_update_local_references() update all the phandles pointing
307 * to a node within the device tree overlay by adding a constant
308 * delta to not conflict with the base overlay.
310 * This is mainly used as part of a device tree application process,
311 * where you want the device tree overlays phandles to not conflict
312 * with the ones from the base device tree before merging them.
316 * Negative error code on failure
318 static int overlay_update_local_references(void *fdto
, uint32_t delta
)
322 fixups
= fdt_path_offset(fdto
, "/__local_fixups__");
324 /* There's no local phandles to adjust, bail out */
325 if (fixups
== -FDT_ERR_NOTFOUND
)
332 * Update our local references from the root of the tree
334 return overlay_update_local_node_references(fdto
, 0, fixups
,
339 * overlay_fixup_one_phandle - Set an overlay phandle to the base one
340 * @fdt: Base Device Tree blob
341 * @fdto: Device tree overlay blob
342 * @symbols_off: Node offset of the symbols node in the base device tree
343 * @path: Path to a node holding a phandle in the overlay
344 * @path_len: number of path characters to consider
345 * @name: Name of the property holding the phandle reference in the overlay
346 * @name_len: number of name characters to consider
347 * @poffset: Offset within the overlay property where the phandle is stored
348 * @label: Label of the node referenced by the phandle
350 * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
351 * a node in the base device tree.
353 * This is part of the device tree overlay application process, when
354 * you want all the phandles in the overlay to point to the actual
359 * Negative error code on failure
361 static int overlay_fixup_one_phandle(void *fdt
, void *fdto
,
363 const char *path
, uint32_t path_len
,
364 const char *name
, uint32_t name_len
,
365 int poffset
, const char *label
)
367 const char *symbol_path
;
369 fdt32_t phandle_prop
;
370 int symbol_off
, fixup_off
;
376 symbol_path
= fdt_getprop(fdt
, symbols_off
, label
,
381 symbol_off
= fdt_path_offset(fdt
, symbol_path
);
385 phandle
= fdt_get_phandle(fdt
, symbol_off
);
387 return -FDT_ERR_NOTFOUND
;
389 fixup_off
= fdt_path_offset_namelen(fdto
, path
, path_len
);
390 if (fixup_off
== -FDT_ERR_NOTFOUND
)
391 return -FDT_ERR_BADOVERLAY
;
395 phandle_prop
= cpu_to_fdt32(phandle
);
396 return fdt_setprop_inplace_namelen_partial(fdto
, fixup_off
,
397 name
, name_len
, poffset
,
399 sizeof(phandle_prop
));
403 * overlay_fixup_phandle - Set an overlay phandle to the base one
404 * @fdt: Base Device Tree blob
405 * @fdto: Device tree overlay blob
406 * @symbols_off: Node offset of the symbols node in the base device tree
407 * @property: Property offset in the overlay holding the list of fixups
409 * overlay_fixup_phandle() resolves all the overlay phandles pointed
410 * to in a __fixups__ property, and updates them to match the phandles
411 * in use in the base device tree.
413 * This is part of the device tree overlay application process, when
414 * you want all the phandles in the overlay to point to the actual
419 * Negative error code on failure
421 static int overlay_fixup_phandle(void *fdt
, void *fdto
, int symbols_off
,
428 value
= fdt_getprop_by_offset(fdto
, property
,
431 if (len
== -FDT_ERR_NOTFOUND
)
432 return -FDT_ERR_INTERNAL
;
438 const char *path
, *name
, *fixup_end
;
439 const char *fixup_str
= value
;
440 uint32_t path_len
, name_len
;
445 fixup_end
= memchr(value
, '\0', len
);
447 return -FDT_ERR_BADOVERLAY
;
448 fixup_len
= fixup_end
- fixup_str
;
450 len
-= fixup_len
+ 1;
451 value
+= fixup_len
+ 1;
454 sep
= memchr(fixup_str
, ':', fixup_len
);
455 if (!sep
|| *sep
!= ':')
456 return -FDT_ERR_BADOVERLAY
;
458 path_len
= sep
- path
;
459 if (path_len
== (fixup_len
- 1))
460 return -FDT_ERR_BADOVERLAY
;
462 fixup_len
-= path_len
+ 1;
464 sep
= memchr(name
, ':', fixup_len
);
465 if (!sep
|| *sep
!= ':')
466 return -FDT_ERR_BADOVERLAY
;
468 name_len
= sep
- name
;
470 return -FDT_ERR_BADOVERLAY
;
472 poffset
= strtoul(sep
+ 1, &endptr
, 10);
473 if ((*endptr
!= '\0') || (endptr
<= (sep
+ 1)))
474 return -FDT_ERR_BADOVERLAY
;
476 ret
= overlay_fixup_one_phandle(fdt
, fdto
, symbols_off
,
477 path
, path_len
, name
, name_len
,
487 * overlay_fixup_phandles - Resolve the overlay phandles to the base
489 * @fdt: Base Device Tree blob
490 * @fdto: Device tree overlay blob
492 * overlay_fixup_phandles() resolves all the overlay phandles pointing
493 * to nodes in the base device tree.
495 * This is one of the steps of the device tree overlay application
496 * process, when you want all the phandles in the overlay to point to
497 * the actual base dt nodes.
501 * Negative error code on failure
503 static int overlay_fixup_phandles(void *fdt
, void *fdto
)
505 int fixups_off
, symbols_off
;
508 /* We can have overlays without any fixups */
509 fixups_off
= fdt_path_offset(fdto
, "/__fixups__");
510 if (fixups_off
== -FDT_ERR_NOTFOUND
)
511 return 0; /* nothing to do */
515 /* And base DTs without symbols */
516 symbols_off
= fdt_path_offset(fdt
, "/__symbols__");
517 if ((symbols_off
< 0 && (symbols_off
!= -FDT_ERR_NOTFOUND
)))
520 fdt_for_each_property_offset(property
, fdto
, fixups_off
) {
523 ret
= overlay_fixup_phandle(fdt
, fdto
, symbols_off
, property
);
532 * overlay_apply_node - Merges a node into the base device tree
533 * @fdt: Base Device Tree blob
534 * @target: Node offset in the base device tree to apply the fragment to
535 * @fdto: Device tree overlay blob
536 * @node: Node offset in the overlay holding the changes to merge
538 * overlay_apply_node() merges a node into a target base device tree
541 * This is part of the final step in the device tree overlay
542 * application process, when all the phandles have been adjusted and
543 * resolved and you just have to merge overlay into the base device
548 * Negative error code on failure
550 static int overlay_apply_node(void *fdt
, int target
,
551 void *fdto
, int node
)
556 fdt_for_each_property_offset(property
, fdto
, node
) {
562 prop
= fdt_getprop_by_offset(fdto
, property
, &name
,
564 if (prop_len
== -FDT_ERR_NOTFOUND
)
565 return -FDT_ERR_INTERNAL
;
569 ret
= fdt_setprop(fdt
, target
, name
, prop
, prop_len
);
574 fdt_for_each_subnode(subnode
, fdto
, node
) {
575 const char *name
= fdt_get_name(fdto
, subnode
, NULL
);
579 nnode
= fdt_add_subnode(fdt
, target
, name
);
580 if (nnode
== -FDT_ERR_EXISTS
) {
581 nnode
= fdt_subnode_offset(fdt
, target
, name
);
582 if (nnode
== -FDT_ERR_NOTFOUND
)
583 return -FDT_ERR_INTERNAL
;
589 ret
= overlay_apply_node(fdt
, nnode
, fdto
, subnode
);
598 * overlay_merge - Merge an overlay into its base device tree
599 * @fdt: Base Device Tree blob
600 * @fdto: Device tree overlay blob
602 * overlay_merge() merges an overlay into its base device tree.
604 * This is the next to last step in the device tree overlay application
605 * process, when all the phandles have been adjusted and resolved and
606 * you just have to merge overlay into the base device tree.
610 * Negative error code on failure
612 static int overlay_merge(void *fdt
, void *fdto
)
616 fdt_for_each_subnode(fragment
, fdto
, 0) {
622 * Each fragments will have an __overlay__ node. If
623 * they don't, it's not supposed to be merged
625 overlay
= fdt_subnode_offset(fdto
, fragment
, "__overlay__");
626 if (overlay
== -FDT_ERR_NOTFOUND
)
632 target
= overlay_get_target(fdt
, fdto
, fragment
, NULL
);
636 ret
= overlay_apply_node(fdt
, target
, fdto
, overlay
);
644 static int get_path_len(const void *fdt
, int nodeoffset
)
646 int len
= 0, namelen
;
649 FDT_CHECK_HEADER(fdt
);
652 name
= fdt_get_name(fdt
, nodeoffset
, &namelen
);
656 /* root? we're done */
660 nodeoffset
= fdt_parent_offset(fdt
, nodeoffset
);
666 /* in case of root pretend it's "/" */
673 * overlay_symbol_update - Update the symbols of base tree after a merge
674 * @fdt: Base Device Tree blob
675 * @fdto: Device tree overlay blob
677 * overlay_symbol_update() updates the symbols of the base tree with the
678 * symbols of the applied overlay
680 * This is the last step in the device tree overlay application
681 * process, allowing the reference of overlay symbols by subsequent
682 * overlay operations.
686 * Negative error code on failure
688 static int overlay_symbol_update(void *fdt
, void *fdto
)
690 int root_sym
, ov_sym
, prop
, path_len
, fragment
, target
;
691 int len
, frag_name_len
, ret
, rel_path_len
;
695 const char *frag_name
;
696 const char *rel_path
;
697 const char *target_path
;
701 ov_sym
= fdt_subnode_offset(fdto
, 0, "__symbols__");
703 /* if no overlay symbols exist no problem */
707 root_sym
= fdt_subnode_offset(fdt
, 0, "__symbols__");
709 /* it no root symbols exist we should create them */
710 if (root_sym
== -FDT_ERR_NOTFOUND
)
711 root_sym
= fdt_add_subnode(fdt
, 0, "__symbols__");
713 /* any error is fatal now */
717 /* iterate over each overlay symbol */
718 fdt_for_each_property_offset(prop
, fdto
, ov_sym
) {
719 path
= fdt_getprop_by_offset(fdto
, prop
, &name
, &path_len
);
723 /* verify it's a string property (terminated by a single \0) */
724 if (path_len
< 1 || memchr(path
, '\0', path_len
) != &path
[path_len
- 1])
725 return -FDT_ERR_BADVALUE
;
727 /* keep end marker to avoid strlen() */
730 /* format: /<fragment-name>/__overlay__/<relative-subnode-path> */
733 return -FDT_ERR_BADVALUE
;
735 /* get fragment name first */
736 s
= strchr(path
+ 1, '/');
738 return -FDT_ERR_BADOVERLAY
;
740 frag_name
= path
+ 1;
741 frag_name_len
= s
- path
- 1;
743 /* verify format; safe since "s" lies in \0 terminated prop */
744 len
= sizeof("/__overlay__/") - 1;
745 if ((e
- s
) < len
|| memcmp(s
, "/__overlay__/", len
))
746 return -FDT_ERR_BADOVERLAY
;
749 rel_path_len
= e
- rel_path
;
751 /* find the fragment index in which the symbol lies */
752 ret
= fdt_subnode_offset_namelen(fdto
, 0, frag_name
,
756 return -FDT_ERR_BADOVERLAY
;
759 /* an __overlay__ subnode must exist */
760 ret
= fdt_subnode_offset(fdto
, fragment
, "__overlay__");
762 return -FDT_ERR_BADOVERLAY
;
764 /* get the target of the fragment */
765 ret
= overlay_get_target(fdt
, fdto
, fragment
, &target_path
);
770 /* if we have a target path use */
772 ret
= get_path_len(fdt
, target
);
777 len
= strlen(target_path
);
780 ret
= fdt_setprop_placeholder(fdt
, root_sym
, name
,
781 len
+ (len
> 1) + rel_path_len
+ 1, &p
);
786 /* again in case setprop_placeholder changed it */
787 ret
= overlay_get_target(fdt
, fdto
, fragment
, &target_path
);
794 if (len
> 1) { /* target is not root */
796 ret
= fdt_get_path(fdt
, target
, buf
, len
+ 1);
800 memcpy(buf
, target_path
, len
+ 1);
806 memcpy(buf
+ len
+ 1, rel_path
, rel_path_len
);
807 buf
[len
+ 1 + rel_path_len
] = '\0';
813 int fdt_overlay_apply(void *fdt
, void *fdto
)
815 uint32_t delta
= fdt_get_max_phandle(fdt
);
818 FDT_CHECK_HEADER(fdt
);
819 FDT_CHECK_HEADER(fdto
);
821 ret
= overlay_adjust_local_phandles(fdto
, delta
);
825 ret
= overlay_update_local_references(fdto
, delta
);
829 ret
= overlay_fixup_phandles(fdt
, fdto
);
833 ret
= overlay_merge(fdt
, fdto
);
837 ret
= overlay_symbol_update(fdt
, fdto
);
842 * The overlay has been damaged, erase its magic.
844 fdt_set_magic(fdto
, ~0);
850 * The overlay might have been damaged, erase its magic.
852 fdt_set_magic(fdto
, ~0);
855 * The base device tree might have been damaged, erase its
858 fdt_set_magic(fdt
, ~0);