1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2016 Free Electrons
5 * Copyright (C) 2016 NextThing Co.
6 */
7 #include "libfdt_env.h"
8
9 #include <fdt.h>
10 #include <libfdt.h>
11
12 #include "libfdt_internal.h"
13
14 /**
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
18 *
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).
22 *
23 * returns:
24 * the phandle pointed by the target property
25 * 0, if the phandle was not found
26 * -1, if the phandle was malformed
27 */
overlay_get_target_phandle(const void * fdto,int fragment)28 static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
29 {
30 const fdt32_t *val;
31 int len;
32
33 val = fdt_getprop(fdto, fragment, "target", &len);
34 if (!val)
35 return 0;
36
37 if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
38 return (uint32_t)-1;
39
40 return fdt32_to_cpu(*val);
41 }
42
fdt_overlay_target_offset(const void * fdt,const void * fdto,int fragment_offset,char const ** pathp)43 int fdt_overlay_target_offset(const void *fdt, const void *fdto,
44 int fragment_offset, char const **pathp)
45 {
46 uint32_t phandle;
47 const char *path = NULL;
48 int path_len = 0, ret;
49
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;
54
55 /* no phandle, try path */
56 if (!phandle) {
57 /* And then a path based lookup */
58 path = fdt_getprop(fdto, fragment_offset, "target-path", &path_len);
59 if (path)
60 ret = fdt_path_offset(fdt, path);
61 else
62 ret = path_len;
63 } else
64 ret = fdt_node_offset_by_phandle(fdt, phandle);
65
66 /*
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
71 * overlay
72 */
73 if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
74 ret = -FDT_ERR_BADOVERLAY;
75
76 /* return on error */
77 if (ret < 0)
78 return ret;
79
80 /* return pointer to path (if available) */
81 if (pathp)
82 *pathp = path ? path : NULL;
83
84 return ret;
85 }
86
87 /**
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
93 *
94 * overlay_phandle_add_offset() increments a node phandle by a given
95 * offset.
96 *
97 * returns:
98 * 0 on success.
99 * Negative error code on error
100 */
overlay_phandle_add_offset(void * fdt,int node,const char * name,uint32_t delta)101 static int overlay_phandle_add_offset(void *fdt, int node,
102 const char *name, uint32_t delta)
103 {
104 fdt32_t *valp, val;
105 int len;
106
107 valp = fdt_getprop_w(fdt, node, name, &len);
108 if (!valp)
109 return len;
110
111 if (len != sizeof(val))
112 return -FDT_ERR_BADPHANDLE;
113
114 val = fdt32_ld(valp);
115 if (val + delta < val || val + delta == (uint32_t)-1)
116 return -FDT_ERR_NOPHANDLES;
117
118 fdt32_st(valp, val + delta);
119 return 0;
120 }
121
122 /**
123 * overlay_adjust_node_phandles - Offsets the phandles of a node
124 * @fdto: Device tree overlay blob
125 * @node: Offset of the node we want to adjust
126 * @delta: Offset to shift the phandles of
127 *
128 * overlay_adjust_node_phandles() adds a constant to all the phandles
129 * of a given node. This is mainly use as part of the overlay
130 * application process, when we want to update all the overlay
131 * phandles to not conflict with the overlays of the base device tree.
132 *
133 * returns:
134 * 0 on success
135 * Negative error code on failure
136 */
overlay_adjust_node_phandles(void * fdto,int node,uint32_t delta)137 static int overlay_adjust_node_phandles(void *fdto, int node,
138 uint32_t delta)
139 {
140 int child;
141 int ret;
142
143 ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
144 if (ret && ret != -FDT_ERR_NOTFOUND)
145 return ret;
146
147 ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
148 if (ret && ret != -FDT_ERR_NOTFOUND)
149 return ret;
150
151 fdt_for_each_subnode(child, fdto, node) {
152 ret = overlay_adjust_node_phandles(fdto, child, delta);
153 if (ret)
154 return ret;
155 }
156
157 return 0;
158 }
159
160 /**
161 * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
162 * @fdto: Device tree overlay blob
163 * @delta: Offset to shift the phandles of
164 *
165 * overlay_adjust_local_phandles() adds a constant to all the
166 * phandles of an overlay. This is mainly use as part of the overlay
167 * application process, when we want to update all the overlay
168 * phandles to not conflict with the overlays of the base device tree.
169 *
170 * returns:
171 * 0 on success
172 * Negative error code on failure
173 */
overlay_adjust_local_phandles(void * fdto,uint32_t delta)174 static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
175 {
176 /*
177 * Start adjusting the phandles from the overlay root
178 */
179 return overlay_adjust_node_phandles(fdto, 0, delta);
180 }
181
182 /**
183 * overlay_update_local_node_references - Adjust the overlay references
184 * @fdto: Device tree overlay blob
185 * @tree_node: Node offset of the node to operate on
186 * @fixup_node: Node offset of the matching local fixups node
187 * @delta: Offset to shift the phandles of
188 *
189 * overlay_update_local_nodes_references() update the phandles
190 * pointing to a node within the device tree overlay by adding a
191 * constant delta.
192 *
193 * This is mainly used as part of a device tree application process,
194 * where you want the device tree overlays phandles to not conflict
195 * with the ones from the base device tree before merging them.
196 *
197 * returns:
198 * 0 on success
199 * Negative error code on failure
200 */
overlay_update_local_node_references(void * fdto,int tree_node,int fixup_node,uint32_t delta)201 static int overlay_update_local_node_references(void *fdto,
202 int tree_node,
203 int fixup_node,
204 uint32_t delta)
205 {
206 int fixup_prop;
207 int fixup_child;
208 int ret;
209
210 fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
211 const fdt32_t *fixup_val;
212 const char *name;
213 char *tree_val;
214 int fixup_len;
215 int tree_len;
216 int i;
217
218 fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
219 &name, &fixup_len);
220 if (!fixup_val)
221 return fixup_len;
222
223 if (fixup_len % sizeof(uint32_t))
224 return -FDT_ERR_BADOVERLAY;
225 fixup_len /= sizeof(uint32_t);
226
227 tree_val = fdt_getprop_w(fdto, tree_node, name, &tree_len);
228 if (!tree_val) {
229 if (tree_len == -FDT_ERR_NOTFOUND)
230 return -FDT_ERR_BADOVERLAY;
231
232 return tree_len;
233 }
234
235 for (i = 0; i < fixup_len; i++) {
236 fdt32_t *refp;
237
238 refp = (fdt32_t *)(tree_val + fdt32_ld_(fixup_val + i));
239
240 /*
241 * phandles to fixup can be unaligned, so use
242 * fdt32_{ld,st}() to read/write them.
243 */
244 fdt32_st(refp, fdt32_ld(refp) + delta);
245 }
246 }
247
248 fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
249 const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
250 NULL);
251 int tree_child;
252
253 tree_child = fdt_subnode_offset(fdto, tree_node,
254 fixup_child_name);
255 if (tree_child == -FDT_ERR_NOTFOUND)
256 return -FDT_ERR_BADOVERLAY;
257 if (tree_child < 0)
258 return tree_child;
259
260 ret = overlay_update_local_node_references(fdto,
261 tree_child,
262 fixup_child,
263 delta);
264 if (ret)
265 return ret;
266 }
267
268 return 0;
269 }
270
271 /**
272 * overlay_update_local_references - Adjust the overlay references
273 * @fdto: Device tree overlay blob
274 * @delta: Offset to shift the phandles of
275 *
276 * overlay_update_local_references() update all the phandles pointing
277 * to a node within the device tree overlay by adding a constant
278 * delta to not conflict with the base overlay.
279 *
280 * This is mainly used as part of a device tree application process,
281 * where you want the device tree overlays phandles to not conflict
282 * with the ones from the base device tree before merging them.
283 *
284 * returns:
285 * 0 on success
286 * Negative error code on failure
287 */
overlay_update_local_references(void * fdto,uint32_t delta)288 static int overlay_update_local_references(void *fdto, uint32_t delta)
289 {
290 int fixups;
291
292 fixups = fdt_path_offset(fdto, "/__local_fixups__");
293 if (fixups < 0) {
294 /* There's no local phandles to adjust, bail out */
295 if (fixups == -FDT_ERR_NOTFOUND)
296 return 0;
297
298 return fixups;
299 }
300
301 /*
302 * Update our local references from the root of the tree
303 */
304 return overlay_update_local_node_references(fdto, 0, fixups,
305 delta);
306 }
307
308 /**
309 * overlay_fixup_one_phandle - Set an overlay phandle to the base one
310 * @fdt: Base Device Tree blob
311 * @fdto: Device tree overlay blob
312 * @symbols_off: Node offset of the symbols node in the base device tree
313 * @path: Path to a node holding a phandle in the overlay
314 * @path_len: number of path characters to consider
315 * @name: Name of the property holding the phandle reference in the overlay
316 * @name_len: number of name characters to consider
317 * @poffset: Offset within the overlay property where the phandle is stored
318 * @label: Label of the node referenced by the phandle
319 *
320 * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
321 * a node in the base device tree.
322 *
323 * This is part of the device tree overlay application process, when
324 * you want all the phandles in the overlay to point to the actual
325 * base dt nodes.
326 *
327 * returns:
328 * 0 on success
329 * Negative error code on failure
330 */
overlay_fixup_one_phandle(void * fdt,void * fdto,int symbols_off,const char * path,uint32_t path_len,const char * name,uint32_t name_len,int poffset,const char * label)331 static int overlay_fixup_one_phandle(void *fdt, void *fdto,
332 int symbols_off,
333 const char *path, uint32_t path_len,
334 const char *name, uint32_t name_len,
335 int poffset, const char *label)
336 {
337 const char *symbol_path;
338 uint32_t phandle;
339 fdt32_t phandle_prop;
340 int symbol_off, fixup_off;
341 int prop_len;
342
343 if (symbols_off < 0)
344 return symbols_off;
345
346 symbol_path = fdt_getprop(fdt, symbols_off, label,
347 &prop_len);
348 if (!symbol_path)
349 return prop_len;
350
351 symbol_off = fdt_path_offset(fdt, symbol_path);
352 if (symbol_off < 0)
353 return symbol_off;
354
355 phandle = fdt_get_phandle(fdt, symbol_off);
356 if (!phandle)
357 return -FDT_ERR_NOTFOUND;
358
359 fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
360 if (fixup_off == -FDT_ERR_NOTFOUND)
361 return -FDT_ERR_BADOVERLAY;
362 if (fixup_off < 0)
363 return fixup_off;
364
365 phandle_prop = cpu_to_fdt32(phandle);
366 return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
367 name, name_len, poffset,
368 &phandle_prop,
369 sizeof(phandle_prop));
370 };
371
372 /**
373 * overlay_fixup_phandle - Set an overlay phandle to the base one
374 * @fdt: Base Device Tree blob
375 * @fdto: Device tree overlay blob
376 * @symbols_off: Node offset of the symbols node in the base device tree
377 * @property: Property offset in the overlay holding the list of fixups
378 *
379 * overlay_fixup_phandle() resolves all the overlay phandles pointed
380 * to in a __fixups__ property, and updates them to match the phandles
381 * in use in the base device tree.
382 *
383 * This is part of the device tree overlay application process, when
384 * you want all the phandles in the overlay to point to the actual
385 * base dt nodes.
386 *
387 * returns:
388 * 0 on success
389 * Negative error code on failure
390 */
overlay_fixup_phandle(void * fdt,void * fdto,int symbols_off,int property)391 static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
392 int property)
393 {
394 const char *value;
395 const char *label;
396 int len;
397
398 value = fdt_getprop_by_offset(fdto, property,
399 &label, &len);
400 if (!value) {
401 if (len == -FDT_ERR_NOTFOUND)
402 return -FDT_ERR_INTERNAL;
403
404 return len;
405 }
406
407 do {
408 const char *path, *name, *fixup_end;
409 const char *fixup_str = value;
410 uint32_t path_len, name_len;
411 uint32_t fixup_len;
412 char *sep, *endptr;
413 int poffset, ret;
414
415 fixup_end = memchr(value, '\0', len);
416 if (!fixup_end)
417 return -FDT_ERR_BADOVERLAY;
418 fixup_len = fixup_end - fixup_str;
419
420 len -= fixup_len + 1;
421 value += fixup_len + 1;
422
423 path = fixup_str;
424 sep = memchr(fixup_str, ':', fixup_len);
425 if (!sep || *sep != ':')
426 return -FDT_ERR_BADOVERLAY;
427
428 path_len = sep - path;
429 if (path_len == (fixup_len - 1))
430 return -FDT_ERR_BADOVERLAY;
431
432 fixup_len -= path_len + 1;
433 name = sep + 1;
434 sep = memchr(name, ':', fixup_len);
435 if (!sep || *sep != ':')
436 return -FDT_ERR_BADOVERLAY;
437
438 name_len = sep - name;
439 if (!name_len)
440 return -FDT_ERR_BADOVERLAY;
441
442 poffset = strtoul(sep + 1, &endptr, 10);
443 if ((*endptr != '\0') || (endptr <= (sep + 1)))
444 return -FDT_ERR_BADOVERLAY;
445
446 ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
447 path, path_len, name, name_len,
448 poffset, label);
449 if (ret)
450 return ret;
451 } while (len > 0);
452
453 return 0;
454 }
455
456 /**
457 * overlay_fixup_phandles - Resolve the overlay phandles to the base
458 * device tree
459 * @fdt: Base Device Tree blob
460 * @fdto: Device tree overlay blob
461 *
462 * overlay_fixup_phandles() resolves all the overlay phandles pointing
463 * to nodes in the base device tree.
464 *
465 * This is one of the steps of the device tree overlay application
466 * process, when you want all the phandles in the overlay to point to
467 * the actual base dt nodes.
468 *
469 * returns:
470 * 0 on success
471 * Negative error code on failure
472 */
overlay_fixup_phandles(void * fdt,void * fdto)473 static int overlay_fixup_phandles(void *fdt, void *fdto)
474 {
475 int fixups_off, symbols_off;
476 int property;
477
478 /* We can have overlays without any fixups */
479 fixups_off = fdt_path_offset(fdto, "/__fixups__");
480 if (fixups_off == -FDT_ERR_NOTFOUND)
481 return 0; /* nothing to do */
482 if (fixups_off < 0)
483 return fixups_off;
484
485 /* And base DTs without symbols */
486 symbols_off = fdt_path_offset(fdt, "/__symbols__");
487 if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
488 return symbols_off;
489
490 fdt_for_each_property_offset(property, fdto, fixups_off) {
491 int ret;
492
493 ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
494 if (ret)
495 return ret;
496 }
497
498 return 0;
499 }
500
501 /**
502 * overlay_adjust_local_conflicting_phandle: Changes a phandle value
503 * @fdto: Device tree overlay
504 * @node: The node the phandle is set for
505 * @fdt_phandle: The new value for the phandle
506 *
507 * returns:
508 * 0 on success
509 * Negative error code on failure
510 */
overlay_adjust_local_conflicting_phandle(void * fdto,int node,uint32_t fdt_phandle)511 static int overlay_adjust_local_conflicting_phandle(void *fdto, int node,
512 uint32_t fdt_phandle)
513 {
514 const fdt32_t *php;
515 int len, ret;
516
517 php = fdt_getprop(fdto, node, "phandle", &len);
518 if (php && len == sizeof(*php)) {
519 ret = fdt_setprop_inplace_u32(fdto, node, "phandle", fdt_phandle);
520 if (ret)
521 return ret;
522 }
523
524 php = fdt_getprop(fdto, node, "linux,phandle", &len);
525 if (php && len == sizeof(*php)) {
526 ret = fdt_setprop_inplace_u32(fdto, node, "linux,phandle", fdt_phandle);
527 if (ret)
528 return ret;
529 }
530
531 return 0;
532 }
533
534 /**
535 * overlay_update_node_conflicting_references - Recursively replace phandle values
536 * @fdto: Device tree overlay blob
537 * @tree_node: Node to recurse into
538 * @fixup_node: Node offset of the matching local fixups node
539 * @fdt_phandle: Value to replace phandles with
540 * @fdto_phandle: Value to be replaced
541 *
542 * Replaces all phandles with value @fdto_phandle by @fdt_phandle.
543 *
544 * returns:
545 * 0 on success
546 * Negative error code on failure
547 */
overlay_update_node_conflicting_references(void * fdto,int tree_node,int fixup_node,uint32_t fdt_phandle,uint32_t fdto_phandle)548 static int overlay_update_node_conflicting_references(void *fdto, int tree_node,
549 int fixup_node,
550 uint32_t fdt_phandle,
551 uint32_t fdto_phandle)
552 {
553 int fixup_prop;
554 int fixup_child;
555 int ret;
556
557 fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
558 const fdt32_t *fixup_val;
559 const char *name;
560 char *tree_val;
561 int fixup_len;
562 int tree_len;
563 int i;
564
565 fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
566 &name, &fixup_len);
567 if (!fixup_val)
568 return fixup_len;
569
570 if (fixup_len % sizeof(uint32_t))
571 return -FDT_ERR_BADOVERLAY;
572 fixup_len /= sizeof(uint32_t);
573
574 tree_val = fdt_getprop_w(fdto, tree_node, name, &tree_len);
575 if (!tree_val) {
576 if (tree_len == -FDT_ERR_NOTFOUND)
577 return -FDT_ERR_BADOVERLAY;
578
579 return tree_len;
580 }
581
582 for (i = 0; i < fixup_len; i++) {
583 fdt32_t *refp;
584 uint32_t valp;
585
586 refp = (fdt32_t *)(tree_val + fdt32_ld_(fixup_val + i));
587 valp = fdt32_ld(refp);
588
589 if (valp == fdto_phandle)
590 fdt32_st(refp, fdt_phandle);
591 }
592 }
593
594 fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
595 const char *fixup_child_name = fdt_get_name(fdto, fixup_child, NULL);
596 int tree_child;
597
598 tree_child = fdt_subnode_offset(fdto, tree_node, fixup_child_name);
599
600 if (tree_child == -FDT_ERR_NOTFOUND)
601 return -FDT_ERR_BADOVERLAY;
602 if (tree_child < 0)
603 return tree_child;
604
605 ret = overlay_update_node_conflicting_references(fdto, tree_child,
606 fixup_child,
607 fdt_phandle,
608 fdto_phandle);
609 if (ret)
610 return ret;
611 }
612
613 return 0;
614 }
615
616 /**
617 * overlay_update_local_conflicting_references - Recursively replace phandle values
618 * @fdto: Device tree overlay blob
619 * @fdt_phandle: Value to replace phandles with
620 * @fdto_phandle: Value to be replaced
621 *
622 * Replaces all phandles with value @fdto_phandle by @fdt_phandle.
623 *
624 * returns:
625 * 0 on success
626 * Negative error code on failure
627 */
overlay_update_local_conflicting_references(void * fdto,uint32_t fdt_phandle,uint32_t fdto_phandle)628 static int overlay_update_local_conflicting_references(void *fdto,
629 uint32_t fdt_phandle,
630 uint32_t fdto_phandle)
631 {
632 int fixups;
633
634 fixups = fdt_path_offset(fdto, "/__local_fixups__");
635 if (fixups == -FDT_ERR_NOTFOUND)
636 return 0;
637 if (fixups < 0)
638 return fixups;
639
640 return overlay_update_node_conflicting_references(fdto, 0, fixups,
641 fdt_phandle,
642 fdto_phandle);
643 }
644
645 /**
646 * overlay_prevent_phandle_overwrite_node - Helper function for overlay_prevent_phandle_overwrite
647 * @fdt: Base Device tree blob
648 * @fdtnode: Node in fdt that is checked for an overwrite
649 * @fdto: Device tree overlay blob
650 * @fdtonode: Node in fdto matching @fdtnode
651 *
652 * returns:
653 * 0 on success
654 * Negative error code on failure
655 */
overlay_prevent_phandle_overwrite_node(void * fdt,int fdtnode,void * fdto,int fdtonode)656 static int overlay_prevent_phandle_overwrite_node(void *fdt, int fdtnode,
657 void *fdto, int fdtonode)
658 {
659 uint32_t fdt_phandle, fdto_phandle;
660 int fdtochild;
661
662 fdt_phandle = fdt_get_phandle(fdt, fdtnode);
663 fdto_phandle = fdt_get_phandle(fdto, fdtonode);
664
665 if (fdt_phandle && fdto_phandle) {
666 int ret;
667
668 ret = overlay_adjust_local_conflicting_phandle(fdto, fdtonode,
669 fdt_phandle);
670 if (ret)
671 return ret;
672
673 ret = overlay_update_local_conflicting_references(fdto,
674 fdt_phandle,
675 fdto_phandle);
676 if (ret)
677 return ret;
678 }
679
680 fdt_for_each_subnode(fdtochild, fdto, fdtonode) {
681 const char *name = fdt_get_name(fdto, fdtochild, NULL);
682 int fdtchild;
683 int ret;
684
685 fdtchild = fdt_subnode_offset(fdt, fdtnode, name);
686 if (fdtchild == -FDT_ERR_NOTFOUND)
687 /*
688 * no further overwrites possible here as this node is
689 * new
690 */
691 continue;
692
693 ret = overlay_prevent_phandle_overwrite_node(fdt, fdtchild,
694 fdto, fdtochild);
695 if (ret)
696 return ret;
697 }
698
699 return 0;
700 }
701
702 /**
703 * overlay_prevent_phandle_overwrite - Fixes overlay phandles to not overwrite base phandles
704 * @fdt: Base Device Tree blob
705 * @fdto: Device tree overlay blob
706 *
707 * Checks recursively if applying fdto overwrites phandle values in the base
708 * dtb. When such a phandle is found, the fdto is changed to use the fdt's
709 * phandle value to not break references in the base.
710 *
711 * returns:
712 * 0 on success
713 * Negative error code on failure
714 */
overlay_prevent_phandle_overwrite(void * fdt,void * fdto)715 static int overlay_prevent_phandle_overwrite(void *fdt, void *fdto)
716 {
717 int fragment;
718
719 fdt_for_each_subnode(fragment, fdto, 0) {
720 int overlay;
721 int target;
722 int ret;
723
724 overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
725 if (overlay == -FDT_ERR_NOTFOUND)
726 continue;
727
728 if (overlay < 0)
729 return overlay;
730
731 target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
732 if (target < 0)
733 return target;
734
735 ret = overlay_prevent_phandle_overwrite_node(fdt, target,
736 fdto, overlay);
737 if (ret)
738 return ret;
739 }
740
741 return 0;
742 }
743
744 /**
745 * overlay_apply_node - Merges a node into the base device tree
746 * @fdt: Base Device Tree blob
747 * @target: Node offset in the base device tree to apply the fragment to
748 * @fdto: Device tree overlay blob
749 * @node: Node offset in the overlay holding the changes to merge
750 *
751 * overlay_apply_node() merges a node into a target base device tree
752 * node pointed.
753 *
754 * This is part of the final step in the device tree overlay
755 * application process, when all the phandles have been adjusted and
756 * resolved and you just have to merge overlay into the base device
757 * tree.
758 *
759 * returns:
760 * 0 on success
761 * Negative error code on failure
762 */
overlay_apply_node(void * fdt,int target,void * fdto,int node)763 static int overlay_apply_node(void *fdt, int target,
764 void *fdto, int node)
765 {
766 int property;
767 int subnode;
768
769 fdt_for_each_property_offset(property, fdto, node) {
770 const char *name;
771 const void *prop;
772 int prop_len;
773 int ret;
774
775 prop = fdt_getprop_by_offset(fdto, property, &name,
776 &prop_len);
777 if (prop_len == -FDT_ERR_NOTFOUND)
778 return -FDT_ERR_INTERNAL;
779 if (prop_len < 0)
780 return prop_len;
781
782 ret = fdt_setprop(fdt, target, name, prop, prop_len);
783 if (ret)
784 return ret;
785 }
786
787 fdt_for_each_subnode(subnode, fdto, node) {
788 const char *name = fdt_get_name(fdto, subnode, NULL);
789 int nnode;
790 int ret;
791
792 nnode = fdt_add_subnode(fdt, target, name);
793 if (nnode == -FDT_ERR_EXISTS) {
794 nnode = fdt_subnode_offset(fdt, target, name);
795 if (nnode == -FDT_ERR_NOTFOUND)
796 return -FDT_ERR_INTERNAL;
797 }
798
799 if (nnode < 0)
800 return nnode;
801
802 ret = overlay_apply_node(fdt, nnode, fdto, subnode);
803 if (ret)
804 return ret;
805 }
806
807 return 0;
808 }
809
810 /**
811 * overlay_merge - Merge an overlay into its base device tree
812 * @fdt: Base Device Tree blob
813 * @fdto: Device tree overlay blob
814 *
815 * overlay_merge() merges an overlay into its base device tree.
816 *
817 * This is the next to last step in the device tree overlay application
818 * process, when all the phandles have been adjusted and resolved and
819 * you just have to merge overlay into the base device tree.
820 *
821 * returns:
822 * 0 on success
823 * Negative error code on failure
824 */
overlay_merge(void * fdt,void * fdto)825 static int overlay_merge(void *fdt, void *fdto)
826 {
827 int fragment;
828
829 fdt_for_each_subnode(fragment, fdto, 0) {
830 int overlay;
831 int target;
832 int ret;
833
834 /*
835 * Each fragments will have an __overlay__ node. If
836 * they don't, it's not supposed to be merged
837 */
838 overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
839 if (overlay == -FDT_ERR_NOTFOUND)
840 continue;
841
842 if (overlay < 0)
843 return overlay;
844
845 target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
846 if (target < 0)
847 return target;
848
849 ret = overlay_apply_node(fdt, target, fdto, overlay);
850 if (ret)
851 return ret;
852 }
853
854 return 0;
855 }
856
get_path_len(const void * fdt,int nodeoffset)857 static int get_path_len(const void *fdt, int nodeoffset)
858 {
859 int len = 0, namelen;
860 const char *name;
861
862 FDT_RO_PROBE(fdt);
863
864 for (;;) {
865 name = fdt_get_name(fdt, nodeoffset, &namelen);
866 if (!name)
867 return namelen;
868
869 /* root? we're done */
870 if (namelen == 0)
871 break;
872
873 nodeoffset = fdt_parent_offset(fdt, nodeoffset);
874 if (nodeoffset < 0)
875 return nodeoffset;
876 len += namelen + 1;
877 }
878
879 /* in case of root pretend it's "/" */
880 if (len == 0)
881 len++;
882 return len;
883 }
884
885 /**
886 * overlay_symbol_update - Update the symbols of base tree after a merge
887 * @fdt: Base Device Tree blob
888 * @fdto: Device tree overlay blob
889 *
890 * overlay_symbol_update() updates the symbols of the base tree with the
891 * symbols of the applied overlay
892 *
893 * This is the last step in the device tree overlay application
894 * process, allowing the reference of overlay symbols by subsequent
895 * overlay operations.
896 *
897 * returns:
898 * 0 on success
899 * Negative error code on failure
900 */
overlay_symbol_update(void * fdt,void * fdto)901 static int overlay_symbol_update(void *fdt, void *fdto)
902 {
903 int root_sym, ov_sym, prop, path_len, fragment, target;
904 int len, frag_name_len, ret, rel_path_len;
905 const char *s, *e;
906 const char *path;
907 const char *name;
908 const char *frag_name;
909 const char *rel_path;
910 const char *target_path;
911 char *buf;
912 void *p;
913
914 ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
915
916 /* if no overlay symbols exist no problem */
917 if (ov_sym < 0)
918 return 0;
919
920 root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
921
922 /* it no root symbols exist we should create them */
923 if (root_sym == -FDT_ERR_NOTFOUND)
924 root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
925
926 /* any error is fatal now */
927 if (root_sym < 0)
928 return root_sym;
929
930 /* iterate over each overlay symbol */
931 fdt_for_each_property_offset(prop, fdto, ov_sym) {
932 path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
933 if (!path)
934 return path_len;
935
936 /* verify it's a string property (terminated by a single \0) */
937 if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
938 return -FDT_ERR_BADVALUE;
939
940 /* keep end marker to avoid strlen() */
941 e = path + path_len;
942
943 if (*path != '/')
944 return -FDT_ERR_BADVALUE;
945
946 /* get fragment name first */
947 s = strchr(path + 1, '/');
948 if (!s) {
949 /* Symbol refers to something that won't end
950 * up in the target tree */
951 continue;
952 }
953
954 frag_name = path + 1;
955 frag_name_len = s - path - 1;
956
957 /* verify format; safe since "s" lies in \0 terminated prop */
958 len = sizeof("/__overlay__/") - 1;
959 if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
960 /* /<fragment-name>/__overlay__/<relative-subnode-path> */
961 rel_path = s + len;
962 rel_path_len = e - rel_path - 1;
963 } else if ((e - s) == len
964 && (memcmp(s, "/__overlay__", len - 1) == 0)) {
965 /* /<fragment-name>/__overlay__ */
966 rel_path = "";
967 rel_path_len = 0;
968 } else {
969 /* Symbol refers to something that won't end
970 * up in the target tree */
971 continue;
972 }
973
974 /* find the fragment index in which the symbol lies */
975 ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
976 frag_name_len);
977 /* not found? */
978 if (ret < 0)
979 return -FDT_ERR_BADOVERLAY;
980 fragment = ret;
981
982 /* an __overlay__ subnode must exist */
983 ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
984 if (ret < 0)
985 return -FDT_ERR_BADOVERLAY;
986
987 /* get the target of the fragment */
988 ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
989 if (ret < 0)
990 return ret;
991 target = ret;
992
993 /* if we have a target path use */
994 if (!target_path) {
995 ret = get_path_len(fdt, target);
996 if (ret < 0)
997 return ret;
998 len = ret;
999 } else {
1000 len = strlen(target_path);
1001 }
1002
1003 ret = fdt_setprop_placeholder(fdt, root_sym, name,
1004 len + (len > 1) + rel_path_len + 1, &p);
1005 if (ret < 0)
1006 return ret;
1007
1008 if (!target_path) {
1009 /* again in case setprop_placeholder changed it */
1010 ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
1011 if (ret < 0)
1012 return ret;
1013 target = ret;
1014 }
1015
1016 buf = p;
1017 if (len > 1) { /* target is not root */
1018 if (!target_path) {
1019 ret = fdt_get_path(fdt, target, buf, len + 1);
1020 if (ret < 0)
1021 return ret;
1022 } else
1023 memcpy(buf, target_path, len + 1);
1024
1025 } else
1026 len--;
1027
1028 buf[len] = '/';
1029 memcpy(buf + len + 1, rel_path, rel_path_len);
1030 buf[len + 1 + rel_path_len] = '\0';
1031 }
1032
1033 return 0;
1034 }
1035
fdt_overlay_apply(void * fdt,void * fdto)1036 int fdt_overlay_apply(void *fdt, void *fdto)
1037 {
1038 uint32_t delta;
1039 int ret;
1040
1041 FDT_RO_PROBE(fdt);
1042 FDT_RO_PROBE(fdto);
1043
1044 ret = fdt_find_max_phandle(fdt, &delta);
1045 if (ret)
1046 goto err;
1047
1048 /* Increase all phandles in the fdto by delta */
1049 ret = overlay_adjust_local_phandles(fdto, delta);
1050 if (ret)
1051 goto err;
1052
1053 /* Adapt the phandle values in fdto to the above increase */
1054 ret = overlay_update_local_references(fdto, delta);
1055 if (ret)
1056 goto err;
1057
1058 /* Update fdto's phandles using symbols from fdt */
1059 ret = overlay_fixup_phandles(fdt, fdto);
1060 if (ret)
1061 goto err;
1062
1063 /* Don't overwrite phandles in fdt */
1064 ret = overlay_prevent_phandle_overwrite(fdt, fdto);
1065 if (ret)
1066 goto err;
1067
1068 ret = overlay_merge(fdt, fdto);
1069 if (ret)
1070 goto err;
1071
1072 ret = overlay_symbol_update(fdt, fdto);
1073 if (ret)
1074 goto err;
1075
1076 /*
1077 * The overlay has been damaged, erase its magic.
1078 */
1079 fdt_set_magic(fdto, ~0);
1080
1081 return 0;
1082
1083 err:
1084 /*
1085 * The overlay might have been damaged, erase its magic.
1086 */
1087 fdt_set_magic(fdto, ~0);
1088
1089 /*
1090 * The base device tree might have been damaged, erase its
1091 * magic.
1092 */
1093 fdt_set_magic(fdt, ~0);
1094
1095 return ret;
1096 }
1097