blob: bb4140412971716af0dea76e130d0a95b684513f [file] [log] [blame]
Maxime Ripard9e901ce2016-07-05 10:26:44 +02001#include "libfdt_env.h"
2
3#include <fdt.h>
4#include <libfdt.h>
5
6#include "libfdt_internal.h"
7
Maxime Ripard0e31a112016-07-05 10:26:46 +02008/**
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
12 *
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).
16 *
17 * returns:
18 * the phandle pointed by the target property
19 * 0, if the phandle was not found
20 * -1, if the phandle was malformed
21 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +020022static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
23{
24 const uint32_t *val;
25 int len;
26
27 val = fdt_getprop(fdto, fragment, "target", &len);
28 if (!val)
29 return 0;
30
Maxime Ripard0b0490a2016-10-17 22:50:18 +020031 if ((len != sizeof(*val)) || (*val == (uint32_t)-1))
Maxime Ripard0e31a112016-07-05 10:26:46 +020032 return (uint32_t)-1;
Maxime Ripard9e901ce2016-07-05 10:26:44 +020033
34 return fdt32_to_cpu(*val);
35}
36
Maxime Ripard0e31a112016-07-05 10:26:46 +020037/**
Maxime Ripard0b0490a2016-10-17 22:50:18 +020038 * overlay_get_target - retrieves the offset of a fragment's target
Maxime Ripard0e31a112016-07-05 10:26:46 +020039 * @fdt: Base device tree blob
40 * @fdto: Device tree overlay blob
41 * @fragment: node offset of the fragment in the overlay
42 *
Maxime Ripard0b0490a2016-10-17 22:50:18 +020043 * overlay_get_target() retrieves the target offset in the base
Maxime Ripard0e31a112016-07-05 10:26:46 +020044 * device tree of a fragment, no matter how the actual targetting is
45 * done (through a phandle or a path)
46 *
47 * returns:
48 * the targetted node offset in the base device tree
49 * Negative error code on error
50 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +020051static int overlay_get_target(const void *fdt, const void *fdto,
52 int fragment)
53{
54 uint32_t phandle;
55 const char *path;
Maxime Ripard0b0490a2016-10-17 22:50:18 +020056 int path_len;
Maxime Ripard9e901ce2016-07-05 10:26:44 +020057
58 /* Try first to do a phandle based lookup */
59 phandle = overlay_get_target_phandle(fdto, fragment);
Maxime Ripard0e31a112016-07-05 10:26:46 +020060 if (phandle == (uint32_t)-1)
Maxime Ripard9e901ce2016-07-05 10:26:44 +020061 return -FDT_ERR_BADPHANDLE;
62
63 if (phandle)
64 return fdt_node_offset_by_phandle(fdt, phandle);
65
66 /* And then a path based lookup */
Maxime Ripard0b0490a2016-10-17 22:50:18 +020067 path = fdt_getprop(fdto, fragment, "target-path", &path_len);
68 if (!path) {
69 /*
70 * If we haven't found either a target or a
71 * target-path property in a node that contains a
72 * __overlay__ subnode (we wouldn't be called
73 * otherwise), consider it a improperly written
74 * overlay
75 */
76 if (path_len == -FDT_ERR_NOTFOUND)
77 return -FDT_ERR_BADOVERLAY;
78
79 return path_len;
80 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +020081
82 return fdt_path_offset(fdt, path);
83}
84
Maxime Ripard0e31a112016-07-05 10:26:46 +020085/**
86 * overlay_phandle_add_offset - Increases a phandle by an offset
87 * @fdt: Base device tree blob
88 * @node: Device tree overlay blob
89 * @name: Name of the property to modify (phandle or linux,phandle)
90 * @delta: offset to apply
91 *
92 * overlay_phandle_add_offset() increments a node phandle by a given
93 * offset.
94 *
95 * returns:
96 * 0 on success.
97 * Negative error code on error
98 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +020099static int overlay_phandle_add_offset(void *fdt, int node,
100 const char *name, uint32_t delta)
101{
102 const uint32_t *val;
103 uint32_t adj_val;
104 int len;
105
106 val = fdt_getprop(fdt, node, name, &len);
107 if (!val)
108 return len;
109
110 if (len != sizeof(*val))
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200111 return -FDT_ERR_BADPHANDLE;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200112
113 adj_val = fdt32_to_cpu(*val);
114 if ((adj_val + delta) < adj_val)
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200115 return -FDT_ERR_NOPHANDLES;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200116
117 adj_val += delta;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200118 if (adj_val == (uint32_t)-1)
119 return -FDT_ERR_NOPHANDLES;
120
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200121 return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
122}
123
Maxime Ripard0e31a112016-07-05 10:26:46 +0200124/**
125 * overlay_adjust_node_phandles - Offsets the phandles of a node
126 * @fdto: Device tree overlay blob
127 * @node: Offset of the node we want to adjust
128 * @delta: Offset to shift the phandles of
129 *
130 * overlay_adjust_node_phandles() adds a constant to all the phandles
131 * of a given node. This is mainly use as part of the overlay
132 * application process, when we want to update all the overlay
133 * phandles to not conflict with the overlays of the base device tree.
134 *
135 * returns:
136 * 0 on success
137 * Negative error code on failure
138 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200139static int overlay_adjust_node_phandles(void *fdto, int node,
140 uint32_t delta)
141{
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200142 int child;
143 int ret;
144
145 ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
146 if (ret && ret != -FDT_ERR_NOTFOUND)
147 return ret;
148
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200149 ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
150 if (ret && ret != -FDT_ERR_NOTFOUND)
151 return ret;
152
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200153 fdt_for_each_subnode(child, fdto, node) {
154 ret = overlay_adjust_node_phandles(fdto, child, delta);
155 if (ret)
156 return ret;
157 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200158
159 return 0;
160}
161
Maxime Ripard0e31a112016-07-05 10:26:46 +0200162/**
163 * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
164 * @fdto: Device tree overlay blob
165 * @delta: Offset to shift the phandles of
166 *
167 * overlay_adjust_local_phandles() adds a constant to all the
168 * phandles of an overlay. This is mainly use as part of the overlay
169 * application process, when we want to update all the overlay
170 * phandles to not conflict with the overlays of the base device tree.
171 *
172 * returns:
173 * 0 on success
174 * Negative error code on failure
175 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200176static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
177{
178 /*
179 * Start adjusting the phandles from the overlay root
180 */
181 return overlay_adjust_node_phandles(fdto, 0, delta);
182}
183
Maxime Ripard0e31a112016-07-05 10:26:46 +0200184/**
185 * overlay_update_local_node_references - Adjust the overlay references
186 * @fdto: Device tree overlay blob
187 * @tree_node: Node offset of the node to operate on
188 * @fixup_node: Node offset of the matching local fixups node
189 * @delta: Offset to shift the phandles of
190 *
191 * overlay_update_local_nodes_references() update the phandles
192 * pointing to a node within the device tree overlay by adding a
193 * constant delta.
194 *
195 * This is mainly used as part of a device tree application process,
196 * where you want the device tree overlays phandles to not conflict
197 * with the ones from the base device tree before merging them.
198 *
199 * returns:
200 * 0 on success
201 * Negative error code on failure
202 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200203static int overlay_update_local_node_references(void *fdto,
204 int tree_node,
205 int fixup_node,
206 uint32_t delta)
207{
208 int fixup_prop;
209 int fixup_child;
210 int ret;
211
212 fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200213 const uint32_t *fixup_val;
214 const char *tree_val;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200215 const char *name;
216 int fixup_len;
217 int tree_len;
Maxime Ripard0e31a112016-07-05 10:26:46 +0200218 int i;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200219
Maxime Ripard0e31a112016-07-05 10:26:46 +0200220 fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
221 &name, &fixup_len);
222 if (!fixup_val)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200223 return fixup_len;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200224
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200225 if (fixup_len % sizeof(uint32_t))
226 return -FDT_ERR_BADOVERLAY;
227
Maxime Ripard0e31a112016-07-05 10:26:46 +0200228 tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200229 if (!tree_val) {
230 if (tree_len == -FDT_ERR_NOTFOUND)
231 return -FDT_ERR_BADOVERLAY;
232
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200233 return tree_len;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200234 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200235
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200236 for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
237 uint32_t adj_val, poffset;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200238
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200239 poffset = fdt32_to_cpu(fixup_val[i]);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200240
Maxime Ripard0e31a112016-07-05 10:26:46 +0200241 /*
242 * phandles to fixup can be unaligned.
243 *
244 * Use a memcpy for the architectures that do
245 * not support unaligned accesses.
246 */
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200247 memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
Maxime Ripard0e31a112016-07-05 10:26:46 +0200248
249 adj_val = fdt32_to_cpu(adj_val);
250 adj_val += delta;
251 adj_val = cpu_to_fdt32(adj_val);
252
253 ret = fdt_setprop_inplace_namelen_partial(fdto,
254 tree_node,
255 name,
256 strlen(name),
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200257 poffset,
Maxime Ripard0e31a112016-07-05 10:26:46 +0200258 &adj_val,
259 sizeof(adj_val));
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200260 if (ret == -FDT_ERR_NOSPACE)
261 return -FDT_ERR_BADOVERLAY;
262
Maxime Ripard0e31a112016-07-05 10:26:46 +0200263 if (ret)
264 return ret;
265 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200266 }
267
Simon Glass499c29e2016-10-02 17:59:29 -0600268 fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200269 const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
270 NULL);
271 int tree_child;
272
273 tree_child = fdt_subnode_offset(fdto, tree_node,
274 fixup_child_name);
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200275 if (ret == -FDT_ERR_NOTFOUND)
276 return -FDT_ERR_BADOVERLAY;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200277 if (tree_child < 0)
278 return tree_child;
279
280 ret = overlay_update_local_node_references(fdto,
281 tree_child,
282 fixup_child,
283 delta);
284 if (ret)
285 return ret;
286 }
287
288 return 0;
289}
290
Maxime Ripard0e31a112016-07-05 10:26:46 +0200291/**
292 * overlay_update_local_references - Adjust the overlay references
293 * @fdto: Device tree overlay blob
294 * @delta: Offset to shift the phandles of
295 *
296 * overlay_update_local_references() update all the phandles pointing
297 * to a node within the device tree overlay by adding a constant
298 * delta to not conflict with the base overlay.
299 *
300 * This is mainly used as part of a device tree application process,
301 * where you want the device tree overlays phandles to not conflict
302 * with the ones from the base device tree before merging them.
303 *
304 * returns:
305 * 0 on success
306 * Negative error code on failure
307 */
308static int overlay_update_local_references(void *fdto, uint32_t delta)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200309{
310 int fixups;
311
Maxime Ripard0e31a112016-07-05 10:26:46 +0200312 fixups = fdt_path_offset(fdto, "/__local_fixups__");
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200313 if (fixups < 0) {
314 /* There's no local phandles to adjust, bail out */
315 if (fixups == -FDT_ERR_NOTFOUND)
316 return 0;
317
318 return fixups;
319 }
320
321 /*
322 * Update our local references from the root of the tree
323 */
Maxime Ripard0e31a112016-07-05 10:26:46 +0200324 return overlay_update_local_node_references(fdto, 0, fixups,
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200325 delta);
326}
327
Maxime Ripard0e31a112016-07-05 10:26:46 +0200328/**
329 * overlay_fixup_one_phandle - Set an overlay phandle to the base one
330 * @fdt: Base Device Tree blob
331 * @fdto: Device tree overlay blob
332 * @symbols_off: Node offset of the symbols node in the base device tree
333 * @path: Path to a node holding a phandle in the overlay
334 * @path_len: number of path characters to consider
335 * @name: Name of the property holding the phandle reference in the overlay
336 * @name_len: number of name characters to consider
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200337 * @poffset: Offset within the overlay property where the phandle is stored
Maxime Ripard0e31a112016-07-05 10:26:46 +0200338 * @label: Label of the node referenced by the phandle
339 *
340 * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
341 * a node in the base device tree.
342 *
343 * This is part of the device tree overlay application process, when
344 * you want all the phandles in the overlay to point to the actual
345 * base dt nodes.
346 *
347 * returns:
348 * 0 on success
349 * Negative error code on failure
350 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200351static int overlay_fixup_one_phandle(void *fdt, void *fdto,
352 int symbols_off,
353 const char *path, uint32_t path_len,
354 const char *name, uint32_t name_len,
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200355 int poffset, const char *label)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200356{
357 const char *symbol_path;
358 uint32_t phandle;
359 int symbol_off, fixup_off;
360 int prop_len;
361
362 symbol_path = fdt_getprop(fdt, symbols_off, label,
363 &prop_len);
364 if (!symbol_path)
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200365 return prop_len;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200366
367 symbol_off = fdt_path_offset(fdt, symbol_path);
368 if (symbol_off < 0)
369 return symbol_off;
370
371 phandle = fdt_get_phandle(fdt, symbol_off);
372 if (!phandle)
373 return -FDT_ERR_NOTFOUND;
374
375 fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200376 if (fixup_off == -FDT_ERR_NOTFOUND)
377 return -FDT_ERR_BADOVERLAY;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200378 if (fixup_off < 0)
379 return fixup_off;
380
381 phandle = cpu_to_fdt32(phandle);
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200382 return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200383 name, name_len, poffset,
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200384 &phandle, sizeof(phandle));
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200385};
386
Maxime Ripard0e31a112016-07-05 10:26:46 +0200387/**
388 * overlay_fixup_phandle - Set an overlay phandle to the base one
389 * @fdt: Base Device Tree blob
390 * @fdto: Device tree overlay blob
391 * @symbols_off: Node offset of the symbols node in the base device tree
392 * @property: Property offset in the overlay holding the list of fixups
393 *
394 * overlay_fixup_phandle() resolves all the overlay phandles pointed
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200395 * to in a __fixups__ property, and updates them to match the phandles
396 * in use in the base device tree.
Maxime Ripard0e31a112016-07-05 10:26:46 +0200397 *
398 * This is part of the device tree overlay application process, when
399 * you want all the phandles in the overlay to point to the actual
400 * base dt nodes.
401 *
402 * returns:
403 * 0 on success
404 * Negative error code on failure
405 */
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200406static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
407 int property)
408{
409 const char *value;
410 const char *label;
411 int len;
412
413 value = fdt_getprop_by_offset(fdto, property,
414 &label, &len);
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200415 if (!value) {
416 if (len == -FDT_ERR_NOTFOUND)
417 return -FDT_ERR_INTERNAL;
418
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200419 return len;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200420 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200421
422 do {
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200423 const char *path, *name, *fixup_end;
424 const char *fixup_str = value;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200425 uint32_t path_len, name_len;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200426 uint32_t fixup_len;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200427 char *sep, *endptr;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200428 int poffset, ret;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200429
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200430 fixup_end = memchr(value, '\0', len);
431 if (!fixup_end)
432 return -FDT_ERR_BADOVERLAY;
433 fixup_len = fixup_end - fixup_str;
434
435 len -= fixup_len + 1;
436 value += fixup_len + 1;
437
438 path = fixup_str;
439 sep = memchr(fixup_str, ':', fixup_len);
440 if (!sep || *sep != ':')
441 return -FDT_ERR_BADOVERLAY;
442
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200443 path_len = sep - path;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200444 if (path_len == (fixup_len - 1))
445 return -FDT_ERR_BADOVERLAY;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200446
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200447 fixup_len -= path_len + 1;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200448 name = sep + 1;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200449 sep = memchr(name, ':', fixup_len);
450 if (!sep || *sep != ':')
451 return -FDT_ERR_BADOVERLAY;
452
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200453 name_len = sep - name;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200454 if (!name_len)
455 return -FDT_ERR_BADOVERLAY;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200456
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200457 poffset = strtoul(sep + 1, &endptr, 10);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200458 if ((*endptr != '\0') || (endptr <= (sep + 1)))
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200459 return -FDT_ERR_BADOVERLAY;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200460
461 ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
462 path, path_len, name, name_len,
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200463 poffset, label);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200464 if (ret)
465 return ret;
466 } while (len > 0);
467
468 return 0;
469}
470
Maxime Ripard0e31a112016-07-05 10:26:46 +0200471/**
472 * overlay_fixup_phandles - Resolve the overlay phandles to the base
473 * device tree
474 * @fdt: Base Device Tree blob
475 * @fdto: Device tree overlay blob
476 *
477 * overlay_fixup_phandles() resolves all the overlay phandles pointing
478 * to nodes in the base device tree.
479 *
480 * This is one of the steps of the device tree overlay application
481 * process, when you want all the phandles in the overlay to point to
482 * the actual base dt nodes.
483 *
484 * returns:
485 * 0 on success
486 * Negative error code on failure
487 */
488static int overlay_fixup_phandles(void *fdt, void *fdto)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200489{
490 int fixups_off, symbols_off;
491 int property;
492
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200493 /* We can have overlays without any fixups */
Maxime Ripard0e31a112016-07-05 10:26:46 +0200494 fixups_off = fdt_path_offset(fdto, "/__fixups__");
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200495 if ((fixups_off < 0 && (fixups_off != -FDT_ERR_NOTFOUND)))
496 return fixups_off;
497
498 /* And base DTs without symbols */
499 symbols_off = fdt_path_offset(fdt, "/__symbols__");
500 if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
501 return symbols_off;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200502
Maxime Ripard0e31a112016-07-05 10:26:46 +0200503 fdt_for_each_property_offset(property, fdto, fixups_off) {
504 int ret;
505
506 ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
507 if (ret)
508 return ret;
509 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200510
511 return 0;
512}
513
Maxime Ripard0e31a112016-07-05 10:26:46 +0200514/**
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200515 * overlay_apply_node - Merges a node into the base device tree
Maxime Ripard0e31a112016-07-05 10:26:46 +0200516 * @fdt: Base Device Tree blob
517 * @target: Node offset in the base device tree to apply the fragment to
518 * @fdto: Device tree overlay blob
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200519 * @node: Node offset in the overlay holding the changes to merge
Maxime Ripard0e31a112016-07-05 10:26:46 +0200520 *
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200521 * overlay_apply_node() merges a node into a target base device tree
522 * node pointed.
Maxime Ripard0e31a112016-07-05 10:26:46 +0200523 *
524 * This is part of the final step in the device tree overlay
525 * application process, when all the phandles have been adjusted and
526 * resolved and you just have to merge overlay into the base device
527 * tree.
528 *
529 * returns:
530 * 0 on success
531 * Negative error code on failure
532 */
533static int overlay_apply_node(void *fdt, int target,
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200534 void *fdto, int node)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200535{
536 int property;
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200537 int subnode;
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200538
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200539 fdt_for_each_property_offset(property, fdto, node) {
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200540 const char *name;
541 const void *prop;
542 int prop_len;
543 int ret;
544
Maxime Ripard0e31a112016-07-05 10:26:46 +0200545 prop = fdt_getprop_by_offset(fdto, property, &name,
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200546 &prop_len);
547 if (prop_len == -FDT_ERR_NOTFOUND)
548 return -FDT_ERR_INTERNAL;
549 if (prop_len < 0)
550 return prop_len;
551
Maxime Ripard0e31a112016-07-05 10:26:46 +0200552 ret = fdt_setprop(fdt, target, name, prop, prop_len);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200553 if (ret)
554 return ret;
555 }
556
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200557 fdt_for_each_subnode(subnode, fdto, node) {
558 const char *name = fdt_get_name(fdto, subnode, NULL);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200559 int nnode;
560 int ret;
561
Maxime Ripard0e31a112016-07-05 10:26:46 +0200562 nnode = fdt_add_subnode(fdt, target, name);
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200563 if (nnode == -FDT_ERR_EXISTS) {
Maxime Ripard0e31a112016-07-05 10:26:46 +0200564 nnode = fdt_subnode_offset(fdt, target, name);
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200565 if (nnode == -FDT_ERR_NOTFOUND)
566 return -FDT_ERR_INTERNAL;
567 }
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200568
569 if (nnode < 0)
570 return nnode;
571
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200572 ret = overlay_apply_node(fdt, nnode, fdto, subnode);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200573 if (ret)
574 return ret;
575 }
576
577 return 0;
578}
579
Maxime Ripard0e31a112016-07-05 10:26:46 +0200580/**
581 * overlay_merge - Merge an overlay into its base device tree
582 * @fdt: Base Device Tree blob
583 * @fdto: Device tree overlay blob
584 *
585 * overlay_merge() merges an overlay into its base device tree.
586 *
587 * This is the final step in the device tree overlay application
588 * process, when all the phandles have been adjusted and resolved and
589 * you just have to merge overlay into the base device tree.
590 *
591 * returns:
592 * 0 on success
593 * Negative error code on failure
594 */
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200595static int overlay_merge(void *fdt, void *fdto)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200596{
597 int fragment;
598
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200599 fdt_for_each_subnode(fragment, fdto, 0) {
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200600 int overlay;
601 int target;
602 int ret;
603
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200604 /*
605 * Each fragments will have an __overlay__ node. If
606 * they don't, it's not supposed to be merged
607 */
608 overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
609 if (overlay == -FDT_ERR_NOTFOUND)
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200610 continue;
611
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200612 if (overlay < 0)
613 return overlay;
614
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200615 target = overlay_get_target(fdt, fdto, fragment);
616 if (target < 0)
617 return target;
618
619 ret = overlay_apply_node(fdt, target, fdto, overlay);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200620 if (ret)
621 return ret;
622 }
623
624 return 0;
625}
626
627int fdt_overlay_apply(void *fdt, void *fdto)
628{
Maxime Ripard0b0490a2016-10-17 22:50:18 +0200629 uint32_t delta = fdt_get_max_phandle(fdt);
Maxime Ripard9e901ce2016-07-05 10:26:44 +0200630 int ret;
631
632 FDT_CHECK_HEADER(fdt);
633 FDT_CHECK_HEADER(fdto);
634
635 ret = overlay_adjust_local_phandles(fdto, delta);
636 if (ret)
637 goto err;
638
639 ret = overlay_update_local_references(fdto, delta);
640 if (ret)
641 goto err;
642
643 ret = overlay_fixup_phandles(fdt, fdto);
644 if (ret)
645 goto err;
646
647 ret = overlay_merge(fdt, fdto);
648 if (ret)
649 goto err;
650
651 /*
652 * The overlay has been damaged, erase its magic.
653 */
654 fdt_set_magic(fdto, ~0);
655
656 return 0;
657
658err:
659 /*
660 * The overlay might have been damaged, erase its magic.
661 */
662 fdt_set_magic(fdto, ~0);
663
664 /*
665 * The base device tree might have been damaged, erase its
666 * magic.
667 */
668 fdt_set_magic(fdt, ~0);
669
670 return ret;
671}