1 #include "libfdt_env.h"
6 #include "libfdt_internal.h"
8 static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
13 val = fdt_getprop(fdto, fragment, "target", &len);
17 if ((*val == 0xffffffff) || (len != sizeof(*val)))
20 return fdt32_to_cpu(*val);
23 static int overlay_get_target(const void *fdt, const void *fdto,
29 /* Try first to do a phandle based lookup */
30 phandle = overlay_get_target_phandle(fdto, fragment);
32 return -FDT_ERR_BADPHANDLE;
35 return fdt_node_offset_by_phandle(fdt, phandle);
37 /* And then a path based lookup */
38 path = fdt_getprop(fdto, fragment, "target-path", NULL);
40 return -FDT_ERR_NOTFOUND;
42 return fdt_path_offset(fdt, path);
45 static int overlay_phandle_add_offset(void *fdt, int node,
46 const char *name, uint32_t delta)
52 val = fdt_getprop(fdt, node, name, &len);
56 if (len != sizeof(*val))
57 return -FDT_ERR_BADSTRUCTURE;
59 adj_val = fdt32_to_cpu(*val);
60 if ((adj_val + delta) < adj_val)
61 return -FDT_ERR_BADPHANDLE;
64 return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
67 static int overlay_adjust_node_phandles(void *fdto, int node,
74 ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
75 if (ret && ret != -FDT_ERR_NOTFOUND)
81 ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
82 if (ret && ret != -FDT_ERR_NOTFOUND)
86 * If neither phandle nor linux,phandle have been found return
92 fdt_for_each_subnode(fdto, child, node)
93 overlay_adjust_node_phandles(fdto, child, delta);
98 static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
101 * Start adjusting the phandles from the overlay root
103 return overlay_adjust_node_phandles(fdto, 0, delta);
106 static int overlay_update_local_node_references(void *fdto,
115 fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
116 const uint32_t *val = NULL;
117 uint32_t adj_val, index;
122 val = fdt_getprop_by_offset(fdto, fixup_prop,
126 index = fdt32_to_cpu(*val);
128 val = fdt_getprop(fdto, tree_node, name, &tree_len);
133 * The index can be unaligned.
135 * Use a memcpy for the architectures that do not
136 * support unaligned accesses.
138 memcpy(&adj_val, (unsigned char *)val + index,
141 adj_val = fdt32_to_cpu(adj_val);
143 adj_val = cpu_to_fdt32(adj_val);
145 ret = fdt_setprop_inplace_namelen_by_index(fdto, tree_node,
153 fdt_for_each_subnode(fdto, fixup_child, fixup_node) {
154 const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
158 tree_child = fdt_subnode_offset(fdto, tree_node,
163 ret = overlay_update_local_node_references(fdto,
174 static int overlay_update_local_references(void *dto, uint32_t delta)
178 fixups = fdt_path_offset(dto, "/__local_fixups__");
180 /* There's no local phandles to adjust, bail out */
181 if (fixups == -FDT_ERR_NOTFOUND)
188 * Update our local references from the root of the tree
190 return overlay_update_local_node_references(dto, 0, fixups,
194 static int overlay_fixup_one_phandle(void *fdt, void *fdto,
196 const char *path, uint32_t path_len,
197 const char *name, uint32_t name_len,
198 int index, const char *label)
200 const char *symbol_path;
202 int symbol_off, fixup_off;
205 symbol_path = fdt_getprop(fdt, symbols_off, label,
208 return -FDT_ERR_NOTFOUND;
210 symbol_off = fdt_path_offset(fdt, symbol_path);
214 phandle = fdt_get_phandle(fdt, symbol_off);
216 return -FDT_ERR_NOTFOUND;
218 fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
222 phandle = cpu_to_fdt32(phandle);
223 return fdt_setprop_inplace_namelen_by_index(fdto, fixup_off,
224 name, name_len, index,
225 &phandle, sizeof(phandle));
228 static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
235 value = fdt_getprop_by_offset(fdto, property,
241 const char *prop_string = value;
242 const char *path, *name;
243 uint32_t prop_len = strlen(value);
244 uint32_t path_len, name_len;
250 sep = memchr(prop_string, ':', prop_len);
252 return -FDT_ERR_BADSTRUCTURE;
253 path_len = sep - path;
256 sep = memchr(name, ':', prop_len);
258 return -FDT_ERR_BADSTRUCTURE;
259 name_len = sep - name;
261 index = strtoul(sep + 1, &endptr, 10);
262 if ((*endptr != '\0') || (endptr <= (sep + 1)))
263 return -FDT_ERR_BADSTRUCTURE;
266 value += prop_len + 1;
268 ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
269 path, path_len, name, name_len,
278 static int overlay_fixup_phandles(void *dt, void *dto)
280 int fixups_off, symbols_off;
283 symbols_off = fdt_path_offset(dt, "/__symbols__");
284 fixups_off = fdt_path_offset(dto, "/__fixups__");
286 fdt_for_each_property_offset(property, dto, fixups_off)
287 overlay_fixup_phandle(dt, dto, symbols_off, property);
292 static int apply_overlay_node(void *dt, int target,
293 void *dto, int fragment)
298 fdt_for_each_property_offset(property, dto, fragment) {
304 prop = fdt_getprop_by_offset(dto, property, &name,
306 if (prop_len == -FDT_ERR_NOTFOUND)
307 return -FDT_ERR_INTERNAL;
311 ret = fdt_setprop(dt, target, name, prop, prop_len);
316 fdt_for_each_subnode(dto, node, fragment) {
317 const char *name = fdt_get_name(dto, node, NULL);
321 nnode = fdt_add_subnode(dt, target, name);
322 if (nnode == -FDT_ERR_EXISTS)
323 nnode = fdt_subnode_offset(dt, target, name);
328 ret = apply_overlay_node(dt, nnode, dto, node);
336 static int overlay_merge(void *dt, void *dto)
340 fdt_for_each_subnode(dto, fragment, 0) {
345 target = overlay_get_target(dt, dto, fragment);
349 overlay = fdt_subnode_offset(dto, fragment, "__overlay__");
353 ret = apply_overlay_node(dt, target, dto, overlay);
361 int fdt_overlay_apply(void *fdt, void *fdto)
363 uint32_t delta = fdt_get_max_phandle(fdt) + 1;
366 FDT_CHECK_HEADER(fdt);
367 FDT_CHECK_HEADER(fdto);
369 ret = overlay_adjust_local_phandles(fdto, delta);
373 ret = overlay_update_local_references(fdto, delta);
377 ret = overlay_fixup_phandles(fdt, fdto);
381 ret = overlay_merge(fdt, fdto);
386 * The overlay has been damaged, erase its magic.
388 fdt_set_magic(fdto, ~0);
394 * The overlay might have been damaged, erase its magic.
396 fdt_set_magic(fdto, ~0);
399 * The base device tree might have been damaged, erase its
402 fdt_set_magic(fdt, ~0);