2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2016 Free Electrons
4 * Copyright (C) 2016 NextThing Co.
6 * libfdt is dual licensed: you can use it either under the terms of
7 * the GPL, or the BSD license, at your option.
9 * a) This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
26 * b) Redistribution and use in source and binary forms, with or
27 * without modification, are permitted provided that the following
30 * 1. Redistributions of source code must retain the above
31 * copyright notice, this list of conditions and the following
33 * 2. Redistributions in binary form must reproduce the above
34 * copyright notice, this list of conditions and the following
35 * disclaimer in the documentation and/or other materials
36 * provided with the distribution.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
43 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
49 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 #include "libfdt_env.h"
57 #include "libfdt_internal.h"
60 * overlay_get_target_phandle - retrieves the target phandle of a fragment
61 * @fdto: pointer to the device tree overlay blob
62 * @fragment: node offset of the fragment in the overlay
64 * overlay_get_target_phandle() retrieves the target phandle of an
65 * overlay fragment when that fragment uses a phandle (target
66 * property) instead of a path (target-path property).
69 * the phandle pointed by the target property
70 * 0, if the phandle was not found
71 * -1, if the phandle was malformed
73 static uint32_t overlay_get_target_phandle(const void *fdto
, int fragment
)
78 val
= fdt_getprop(fdto
, fragment
, "target", &len
);
82 if ((len
!= sizeof(*val
)) || (fdt32_to_cpu(*val
) == (uint32_t)-1))
85 return fdt32_to_cpu(*val
);
89 * overlay_get_target - retrieves the offset of a fragment's target
90 * @fdt: Base device tree blob
91 * @fdto: Device tree overlay blob
92 * @fragment: node offset of the fragment in the overlay
93 * @pathp: pointer which receives the path of the target (or NULL)
95 * overlay_get_target() retrieves the target offset in the base
96 * device tree of a fragment, no matter how the actual targetting is
97 * done (through a phandle or a path)
100 * the targetted node offset in the base device tree
101 * Negative error code on error
103 static int overlay_get_target(const void *fdt
, const void *fdto
,
104 int fragment
, char const **pathp
)
107 const char *path
= NULL
;
108 int path_len
= 0, ret
;
110 /* Try first to do a phandle based lookup */
111 phandle
= overlay_get_target_phandle(fdto
, fragment
);
112 if (phandle
== (uint32_t)-1)
113 return -FDT_ERR_BADPHANDLE
;
115 /* no phandle, try path */
117 /* And then a path based lookup */
118 path
= fdt_getprop(fdto
, fragment
, "target-path", &path_len
);
120 ret
= fdt_path_offset(fdt
, path
);
124 ret
= fdt_node_offset_by_phandle(fdt
, phandle
);
127 * If we haven't found either a target or a
128 * target-path property in a node that contains a
129 * __overlay__ subnode (we wouldn't be called
130 * otherwise), consider it a improperly written
133 if (ret
< 0 && path_len
== -FDT_ERR_NOTFOUND
)
134 ret
= -FDT_ERR_BADOVERLAY
;
136 /* return on error */
140 /* return pointer to path (if available) */
142 *pathp
= path
? path
: NULL
;
148 * overlay_phandle_add_offset - Increases a phandle by an offset
149 * @fdt: Base device tree blob
150 * @node: Device tree overlay blob
151 * @name: Name of the property to modify (phandle or linux,phandle)
152 * @delta: offset to apply
154 * overlay_phandle_add_offset() increments a node phandle by a given
159 * Negative error code on error
161 static int overlay_phandle_add_offset(void *fdt
, int node
,
162 const char *name
, uint32_t delta
)
168 val
= fdt_getprop(fdt
, node
, name
, &len
);
172 if (len
!= sizeof(*val
))
173 return -FDT_ERR_BADPHANDLE
;
175 adj_val
= fdt32_to_cpu(*val
);
176 if ((adj_val
+ delta
) < adj_val
)
177 return -FDT_ERR_NOPHANDLES
;
180 if (adj_val
== (uint32_t)-1)
181 return -FDT_ERR_NOPHANDLES
;
183 return fdt_setprop_inplace_u32(fdt
, node
, name
, adj_val
);
187 * overlay_adjust_node_phandles - Offsets the phandles of a node
188 * @fdto: Device tree overlay blob
189 * @node: Offset of the node we want to adjust
190 * @delta: Offset to shift the phandles of
192 * overlay_adjust_node_phandles() adds a constant to all the phandles
193 * of a given node. This is mainly use as part of the overlay
194 * application process, when we want to update all the overlay
195 * phandles to not conflict with the overlays of the base device tree.
199 * Negative error code on failure
201 static int overlay_adjust_node_phandles(void *fdto
, int node
,
207 ret
= overlay_phandle_add_offset(fdto
, node
, "phandle", delta
);
208 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
)
211 ret
= overlay_phandle_add_offset(fdto
, node
, "linux,phandle", delta
);
212 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
)
215 fdt_for_each_subnode(child
, fdto
, node
) {
216 ret
= overlay_adjust_node_phandles(fdto
, child
, delta
);
225 * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
226 * @fdto: Device tree overlay blob
227 * @delta: Offset to shift the phandles of
229 * overlay_adjust_local_phandles() adds a constant to all the
230 * phandles of an overlay. This is mainly use as part of the overlay
231 * application process, when we want to update all the overlay
232 * phandles to not conflict with the overlays of the base device tree.
236 * Negative error code on failure
238 static int overlay_adjust_local_phandles(void *fdto
, uint32_t delta
)
241 * Start adjusting the phandles from the overlay root
243 return overlay_adjust_node_phandles(fdto
, 0, delta
);
247 * overlay_update_local_node_references - Adjust the overlay references
248 * @fdto: Device tree overlay blob
249 * @tree_node: Node offset of the node to operate on
250 * @fixup_node: Node offset of the matching local fixups node
251 * @delta: Offset to shift the phandles of
253 * overlay_update_local_nodes_references() update the phandles
254 * pointing to a node within the device tree overlay by adding a
257 * This is mainly used as part of a device tree application process,
258 * where you want the device tree overlays phandles to not conflict
259 * with the ones from the base device tree before merging them.
263 * Negative error code on failure
265 static int overlay_update_local_node_references(void *fdto
,
274 fdt_for_each_property_offset(fixup_prop
, fdto
, fixup_node
) {
275 const fdt32_t
*fixup_val
;
276 const char *tree_val
;
282 fixup_val
= fdt_getprop_by_offset(fdto
, fixup_prop
,
287 if (fixup_len
% sizeof(uint32_t))
288 return -FDT_ERR_BADOVERLAY
;
290 tree_val
= fdt_getprop(fdto
, tree_node
, name
, &tree_len
);
292 if (tree_len
== -FDT_ERR_NOTFOUND
)
293 return -FDT_ERR_BADOVERLAY
;
298 for (i
= 0; i
< (fixup_len
/ sizeof(uint32_t)); i
++) {
302 poffset
= fdt32_to_cpu(fixup_val
[i
]);
305 * phandles to fixup can be unaligned.
307 * Use a memcpy for the architectures that do
308 * not support unaligned accesses.
310 memcpy(&adj_val
, tree_val
+ poffset
, sizeof(adj_val
));
312 adj_val
= cpu_to_fdt32(fdt32_to_cpu(adj_val
) + delta
);
314 ret
= fdt_setprop_inplace_namelen_partial(fdto
,
321 if (ret
== -FDT_ERR_NOSPACE
)
322 return -FDT_ERR_BADOVERLAY
;
329 fdt_for_each_subnode(fixup_child
, fdto
, fixup_node
) {
330 const char *fixup_child_name
= fdt_get_name(fdto
, fixup_child
,
334 tree_child
= fdt_subnode_offset(fdto
, tree_node
,
336 if (tree_child
== -FDT_ERR_NOTFOUND
)
337 return -FDT_ERR_BADOVERLAY
;
341 ret
= overlay_update_local_node_references(fdto
,
353 * overlay_update_local_references - Adjust the overlay references
354 * @fdto: Device tree overlay blob
355 * @delta: Offset to shift the phandles of
357 * overlay_update_local_references() update all the phandles pointing
358 * to a node within the device tree overlay by adding a constant
359 * delta to not conflict with the base overlay.
361 * This is mainly used as part of a device tree application process,
362 * where you want the device tree overlays phandles to not conflict
363 * with the ones from the base device tree before merging them.
367 * Negative error code on failure
369 static int overlay_update_local_references(void *fdto
, uint32_t delta
)
373 fixups
= fdt_path_offset(fdto
, "/__local_fixups__");
375 /* There's no local phandles to adjust, bail out */
376 if (fixups
== -FDT_ERR_NOTFOUND
)
383 * Update our local references from the root of the tree
385 return overlay_update_local_node_references(fdto
, 0, fixups
,
390 * overlay_fixup_one_phandle - Set an overlay phandle to the base one
391 * @fdt: Base Device Tree blob
392 * @fdto: Device tree overlay blob
393 * @symbols_off: Node offset of the symbols node in the base device tree
394 * @path: Path to a node holding a phandle in the overlay
395 * @path_len: number of path characters to consider
396 * @name: Name of the property holding the phandle reference in the overlay
397 * @name_len: number of name characters to consider
398 * @poffset: Offset within the overlay property where the phandle is stored
399 * @label: Label of the node referenced by the phandle
401 * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
402 * a node in the base device tree.
404 * This is part of the device tree overlay application process, when
405 * you want all the phandles in the overlay to point to the actual
410 * Negative error code on failure
412 static int overlay_fixup_one_phandle(void *fdt
, void *fdto
,
414 const char *path
, uint32_t path_len
,
415 const char *name
, uint32_t name_len
,
416 int poffset
, const char *label
)
418 const char *symbol_path
;
420 fdt32_t phandle_prop
;
421 int symbol_off
, fixup_off
;
427 symbol_path
= fdt_getprop(fdt
, symbols_off
, label
,
432 symbol_off
= fdt_path_offset(fdt
, symbol_path
);
436 phandle
= fdt_get_phandle(fdt
, symbol_off
);
438 return -FDT_ERR_NOTFOUND
;
440 fixup_off
= fdt_path_offset_namelen(fdto
, path
, path_len
);
441 if (fixup_off
== -FDT_ERR_NOTFOUND
)
442 return -FDT_ERR_BADOVERLAY
;
446 phandle_prop
= cpu_to_fdt32(phandle
);
447 return fdt_setprop_inplace_namelen_partial(fdto
, fixup_off
,
448 name
, name_len
, poffset
,
450 sizeof(phandle_prop
));
454 * overlay_fixup_phandle - Set an overlay phandle to the base one
455 * @fdt: Base Device Tree blob
456 * @fdto: Device tree overlay blob
457 * @symbols_off: Node offset of the symbols node in the base device tree
458 * @property: Property offset in the overlay holding the list of fixups
460 * overlay_fixup_phandle() resolves all the overlay phandles pointed
461 * to in a __fixups__ property, and updates them to match the phandles
462 * in use in the base device tree.
464 * This is part of the device tree overlay application process, when
465 * you want all the phandles in the overlay to point to the actual
470 * Negative error code on failure
472 static int overlay_fixup_phandle(void *fdt
, void *fdto
, int symbols_off
,
479 value
= fdt_getprop_by_offset(fdto
, property
,
482 if (len
== -FDT_ERR_NOTFOUND
)
483 return -FDT_ERR_INTERNAL
;
489 const char *path
, *name
, *fixup_end
;
490 const char *fixup_str
= value
;
491 uint32_t path_len
, name_len
;
496 fixup_end
= memchr(value
, '\0', len
);
498 return -FDT_ERR_BADOVERLAY
;
499 fixup_len
= fixup_end
- fixup_str
;
501 len
-= fixup_len
+ 1;
502 value
+= fixup_len
+ 1;
505 sep
= memchr(fixup_str
, ':', fixup_len
);
506 if (!sep
|| *sep
!= ':')
507 return -FDT_ERR_BADOVERLAY
;
509 path_len
= sep
- path
;
510 if (path_len
== (fixup_len
- 1))
511 return -FDT_ERR_BADOVERLAY
;
513 fixup_len
-= path_len
+ 1;
515 sep
= memchr(name
, ':', fixup_len
);
516 if (!sep
|| *sep
!= ':')
517 return -FDT_ERR_BADOVERLAY
;
519 name_len
= sep
- name
;
521 return -FDT_ERR_BADOVERLAY
;
523 poffset
= strtoul(sep
+ 1, &endptr
, 10);
524 if ((*endptr
!= '\0') || (endptr
<= (sep
+ 1)))
525 return -FDT_ERR_BADOVERLAY
;
527 ret
= overlay_fixup_one_phandle(fdt
, fdto
, symbols_off
,
528 path
, path_len
, name
, name_len
,
538 * overlay_fixup_phandles - Resolve the overlay phandles to the base
540 * @fdt: Base Device Tree blob
541 * @fdto: Device tree overlay blob
543 * overlay_fixup_phandles() resolves all the overlay phandles pointing
544 * to nodes in the base device tree.
546 * This is one of the steps of the device tree overlay application
547 * process, when you want all the phandles in the overlay to point to
548 * the actual base dt nodes.
552 * Negative error code on failure
554 static int overlay_fixup_phandles(void *fdt
, void *fdto
)
556 int fixups_off
, symbols_off
;
559 /* We can have overlays without any fixups */
560 fixups_off
= fdt_path_offset(fdto
, "/__fixups__");
561 if (fixups_off
== -FDT_ERR_NOTFOUND
)
562 return 0; /* nothing to do */
566 /* And base DTs without symbols */
567 symbols_off
= fdt_path_offset(fdt
, "/__symbols__");
568 if ((symbols_off
< 0 && (symbols_off
!= -FDT_ERR_NOTFOUND
)))
571 fdt_for_each_property_offset(property
, fdto
, fixups_off
) {
574 ret
= overlay_fixup_phandle(fdt
, fdto
, symbols_off
, property
);
583 * overlay_apply_node - Merges a node into the base device tree
584 * @fdt: Base Device Tree blob
585 * @target: Node offset in the base device tree to apply the fragment to
586 * @fdto: Device tree overlay blob
587 * @node: Node offset in the overlay holding the changes to merge
589 * overlay_apply_node() merges a node into a target base device tree
592 * This is part of the final step in the device tree overlay
593 * application process, when all the phandles have been adjusted and
594 * resolved and you just have to merge overlay into the base device
599 * Negative error code on failure
601 static int overlay_apply_node(void *fdt
, int target
,
602 void *fdto
, int node
)
607 fdt_for_each_property_offset(property
, fdto
, node
) {
613 prop
= fdt_getprop_by_offset(fdto
, property
, &name
,
615 if (prop_len
== -FDT_ERR_NOTFOUND
)
616 return -FDT_ERR_INTERNAL
;
620 ret
= fdt_setprop(fdt
, target
, name
, prop
, prop_len
);
625 fdt_for_each_subnode(subnode
, fdto
, node
) {
626 const char *name
= fdt_get_name(fdto
, subnode
, NULL
);
630 nnode
= fdt_add_subnode(fdt
, target
, name
);
631 if (nnode
== -FDT_ERR_EXISTS
) {
632 nnode
= fdt_subnode_offset(fdt
, target
, name
);
633 if (nnode
== -FDT_ERR_NOTFOUND
)
634 return -FDT_ERR_INTERNAL
;
640 ret
= overlay_apply_node(fdt
, nnode
, fdto
, subnode
);
649 * overlay_merge - Merge an overlay into its base device tree
650 * @fdt: Base Device Tree blob
651 * @fdto: Device tree overlay blob
653 * overlay_merge() merges an overlay into its base device tree.
655 * This is the next to last step in the device tree overlay application
656 * process, when all the phandles have been adjusted and resolved and
657 * you just have to merge overlay into the base device tree.
661 * Negative error code on failure
663 static int overlay_merge(void *fdt
, void *fdto
)
667 fdt_for_each_subnode(fragment
, fdto
, 0) {
673 * Each fragments will have an __overlay__ node. If
674 * they don't, it's not supposed to be merged
676 overlay
= fdt_subnode_offset(fdto
, fragment
, "__overlay__");
677 if (overlay
== -FDT_ERR_NOTFOUND
)
683 target
= overlay_get_target(fdt
, fdto
, fragment
, NULL
);
687 ret
= overlay_apply_node(fdt
, target
, fdto
, overlay
);
695 static int get_path_len(const void *fdt
, int nodeoffset
)
697 int len
= 0, namelen
;
703 name
= fdt_get_name(fdt
, nodeoffset
, &namelen
);
707 /* root? we're done */
711 nodeoffset
= fdt_parent_offset(fdt
, nodeoffset
);
717 /* in case of root pretend it's "/" */
724 * overlay_symbol_update - Update the symbols of base tree after a merge
725 * @fdt: Base Device Tree blob
726 * @fdto: Device tree overlay blob
728 * overlay_symbol_update() updates the symbols of the base tree with the
729 * symbols of the applied overlay
731 * This is the last step in the device tree overlay application
732 * process, allowing the reference of overlay symbols by subsequent
733 * overlay operations.
737 * Negative error code on failure
739 static int overlay_symbol_update(void *fdt
, void *fdto
)
741 int root_sym
, ov_sym
, prop
, path_len
, fragment
, target
;
742 int len
, frag_name_len
, ret
, rel_path_len
;
746 const char *frag_name
;
747 const char *rel_path
;
748 const char *target_path
;
752 ov_sym
= fdt_subnode_offset(fdto
, 0, "__symbols__");
754 /* if no overlay symbols exist no problem */
758 root_sym
= fdt_subnode_offset(fdt
, 0, "__symbols__");
760 /* it no root symbols exist we should create them */
761 if (root_sym
== -FDT_ERR_NOTFOUND
)
762 root_sym
= fdt_add_subnode(fdt
, 0, "__symbols__");
764 /* any error is fatal now */
768 /* iterate over each overlay symbol */
769 fdt_for_each_property_offset(prop
, fdto
, ov_sym
) {
770 path
= fdt_getprop_by_offset(fdto
, prop
, &name
, &path_len
);
774 /* verify it's a string property (terminated by a single \0) */
775 if (path_len
< 1 || memchr(path
, '\0', path_len
) != &path
[path_len
- 1])
776 return -FDT_ERR_BADVALUE
;
778 /* keep end marker to avoid strlen() */
781 /* format: /<fragment-name>/__overlay__/<relative-subnode-path> */
784 return -FDT_ERR_BADVALUE
;
786 /* get fragment name first */
787 s
= strchr(path
+ 1, '/');
789 return -FDT_ERR_BADOVERLAY
;
791 frag_name
= path
+ 1;
792 frag_name_len
= s
- path
- 1;
794 /* verify format; safe since "s" lies in \0 terminated prop */
795 len
= sizeof("/__overlay__/") - 1;
796 if ((e
- s
) < len
|| memcmp(s
, "/__overlay__/", len
))
797 return -FDT_ERR_BADOVERLAY
;
800 rel_path_len
= e
- rel_path
;
802 /* find the fragment index in which the symbol lies */
803 ret
= fdt_subnode_offset_namelen(fdto
, 0, frag_name
,
807 return -FDT_ERR_BADOVERLAY
;
810 /* an __overlay__ subnode must exist */
811 ret
= fdt_subnode_offset(fdto
, fragment
, "__overlay__");
813 return -FDT_ERR_BADOVERLAY
;
815 /* get the target of the fragment */
816 ret
= overlay_get_target(fdt
, fdto
, fragment
, &target_path
);
821 /* if we have a target path use */
823 ret
= get_path_len(fdt
, target
);
828 len
= strlen(target_path
);
831 ret
= fdt_setprop_placeholder(fdt
, root_sym
, name
,
832 len
+ (len
> 1) + rel_path_len
+ 1, &p
);
837 /* again in case setprop_placeholder changed it */
838 ret
= overlay_get_target(fdt
, fdto
, fragment
, &target_path
);
845 if (len
> 1) { /* target is not root */
847 ret
= fdt_get_path(fdt
, target
, buf
, len
+ 1);
851 memcpy(buf
, target_path
, len
+ 1);
857 memcpy(buf
+ len
+ 1, rel_path
, rel_path_len
);
858 buf
[len
+ 1 + rel_path_len
] = '\0';
864 int fdt_overlay_apply(void *fdt
, void *fdto
)
866 uint32_t delta
= fdt_get_max_phandle(fdt
);
872 ret
= overlay_adjust_local_phandles(fdto
, delta
);
876 ret
= overlay_update_local_references(fdto
, delta
);
880 ret
= overlay_fixup_phandles(fdt
, fdto
);
884 ret
= overlay_merge(fdt
, fdto
);
888 ret
= overlay_symbol_update(fdt
, fdto
);
893 * The overlay has been damaged, erase its magic.
895 fdt_set_magic(fdto
, ~0);
901 * The overlay might have been damaged, erase its magic.
903 fdt_set_magic(fdto
, ~0);
906 * The base device tree might have been damaged, erase its
909 fdt_set_magic(fdt
, ~0);