1 /*	$NetBSD: fdt_overlay.c,v 1.1.1.2 2019/12/22 12:30:38 skrll Exp $	*/
2 
3 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
4 /*
5  * libfdt - Flat Device Tree manipulation
6  * Copyright (C) 2016 Free Electrons
7  * Copyright (C) 2016 NextThing Co.
8  */
9 #include "libfdt_env.h"
10 
11 #include <fdt.h>
12 #include <libfdt.h>
13 
14 #include "libfdt_internal.h"
15 
16 /**
17  * overlay_get_target_phandle - retrieves the target phandle of a fragment
18  * @fdto: pointer to the device tree overlay blob
19  * @fragment: node offset of the fragment in the overlay
20  *
21  * overlay_get_target_phandle() retrieves the target phandle of an
22  * overlay fragment when that fragment uses a phandle (target
23  * property) instead of a path (target-path property).
24  *
25  * returns:
26  *      the phandle pointed by the target property
27  *      0, if the phandle was not found
28  *	-1, if the phandle was malformed
29  */
overlay_get_target_phandle(const void * fdto,int fragment)30 static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
31 {
32 	const fdt32_t *val;
33 	int len;
34 
35 	val = fdt_getprop(fdto, fragment, "target", &len);
36 	if (!val)
37 		return 0;
38 
39 	if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
40 		return (uint32_t)-1;
41 
42 	return fdt32_to_cpu(*val);
43 }
44 
45 /**
46  * overlay_get_target - retrieves the offset of a fragment's target
47  * @fdt: Base device tree blob
48  * @fdto: Device tree overlay blob
49  * @fragment: node offset of the fragment in the overlay
50  * @pathp: pointer which receives the path of the target (or NULL)
51  *
52  * overlay_get_target() retrieves the target offset in the base
53  * device tree of a fragment, no matter how the actual targeting is
54  * done (through a phandle or a path)
55  *
56  * returns:
57  *      the targeted node offset in the base device tree
58  *      Negative error code on error
59  */
overlay_get_target(const void * fdt,const void * fdto,int fragment,char const ** pathp)60 static int overlay_get_target(const void *fdt, const void *fdto,
61 			      int fragment, char const **pathp)
62 {
63 	uint32_t phandle;
64 	const char *path = NULL;
65 	int path_len = 0, ret;
66 
67 	/* Try first to do a phandle based lookup */
68 	phandle = overlay_get_target_phandle(fdto, fragment);
69 	if (phandle == (uint32_t)-1)
70 		return -FDT_ERR_BADPHANDLE;
71 
72 	/* no phandle, try path */
73 	if (!phandle) {
74 		/* And then a path based lookup */
75 		path = fdt_getprop(fdto, fragment, "target-path", &path_len);
76 		if (path)
77 			ret = fdt_path_offset(fdt, path);
78 		else
79 			ret = path_len;
80 	} else
81 		ret = fdt_node_offset_by_phandle(fdt, phandle);
82 
83 	/*
84 	* If we haven't found either a target or a
85 	* target-path property in a node that contains a
86 	* __overlay__ subnode (we wouldn't be called
87 	* otherwise), consider it a improperly written
88 	* overlay
89 	*/
90 	if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
91 		ret = -FDT_ERR_BADOVERLAY;
92 
93 	/* return on error */
94 	if (ret < 0)
95 		return ret;
96 
97 	/* return pointer to path (if available) */
98 	if (pathp)
99 		*pathp = path ? path : NULL;
100 
101 	return ret;
102 }
103 
104 /**
105  * overlay_phandle_add_offset - Increases a phandle by an offset
106  * @fdt: Base device tree blob
107  * @node: Device tree overlay blob
108  * @name: Name of the property to modify (phandle or linux,phandle)
109  * @delta: offset to apply
110  *
111  * overlay_phandle_add_offset() increments a node phandle by a given
112  * offset.
113  *
114  * returns:
115  *      0 on success.
116  *      Negative error code on error
117  */
overlay_phandle_add_offset(void * fdt,int node,const char * name,uint32_t delta)118 static int overlay_phandle_add_offset(void *fdt, int node,
119 				      const char *name, uint32_t delta)
120 {
121 	const fdt32_t *val;
122 	uint32_t adj_val;
123 	int len;
124 
125 	val = fdt_getprop(fdt, node, name, &len);
126 	if (!val)
127 		return len;
128 
129 	if (len != sizeof(*val))
130 		return -FDT_ERR_BADPHANDLE;
131 
132 	adj_val = fdt32_to_cpu(*val);
133 	if ((adj_val + delta) < adj_val)
134 		return -FDT_ERR_NOPHANDLES;
135 
136 	adj_val += delta;
137 	if (adj_val == (uint32_t)-1)
138 		return -FDT_ERR_NOPHANDLES;
139 
140 	return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
141 }
142 
143 /**
144  * overlay_adjust_node_phandles - Offsets the phandles of a node
145  * @fdto: Device tree overlay blob
146  * @node: Offset of the node we want to adjust
147  * @delta: Offset to shift the phandles of
148  *
149  * overlay_adjust_node_phandles() adds a constant to all the phandles
150  * of a given node. This is mainly use as part of the overlay
151  * application process, when we want to update all the overlay
152  * phandles to not conflict with the overlays of the base device tree.
153  *
154  * returns:
155  *      0 on success
156  *      Negative error code on failure
157  */
overlay_adjust_node_phandles(void * fdto,int node,uint32_t delta)158 static int overlay_adjust_node_phandles(void *fdto, int node,
159 					uint32_t delta)
160 {
161 	int child;
162 	int ret;
163 
164 	ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
165 	if (ret && ret != -FDT_ERR_NOTFOUND)
166 		return ret;
167 
168 	ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
169 	if (ret && ret != -FDT_ERR_NOTFOUND)
170 		return ret;
171 
172 	fdt_for_each_subnode(child, fdto, node) {
173 		ret = overlay_adjust_node_phandles(fdto, child, delta);
174 		if (ret)
175 			return ret;
176 	}
177 
178 	return 0;
179 }
180 
181 /**
182  * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
183  * @fdto: Device tree overlay blob
184  * @delta: Offset to shift the phandles of
185  *
186  * overlay_adjust_local_phandles() adds a constant to all the
187  * phandles of an overlay. This is mainly use as part of the overlay
188  * application process, when we want to update all the overlay
189  * phandles to not conflict with the overlays of the base device tree.
190  *
191  * returns:
192  *      0 on success
193  *      Negative error code on failure
194  */
overlay_adjust_local_phandles(void * fdto,uint32_t delta)195 static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
196 {
197 	/*
198 	 * Start adjusting the phandles from the overlay root
199 	 */
200 	return overlay_adjust_node_phandles(fdto, 0, delta);
201 }
202 
203 /**
204  * overlay_update_local_node_references - Adjust the overlay references
205  * @fdto: Device tree overlay blob
206  * @tree_node: Node offset of the node to operate on
207  * @fixup_node: Node offset of the matching local fixups node
208  * @delta: Offset to shift the phandles of
209  *
210  * overlay_update_local_nodes_references() update the phandles
211  * pointing to a node within the device tree overlay by adding a
212  * constant delta.
213  *
214  * This is mainly used as part of a device tree application process,
215  * where you want the device tree overlays phandles to not conflict
216  * with the ones from the base device tree before merging them.
217  *
218  * returns:
219  *      0 on success
220  *      Negative error code on failure
221  */
overlay_update_local_node_references(void * fdto,int tree_node,int fixup_node,uint32_t delta)222 static int overlay_update_local_node_references(void *fdto,
223 						int tree_node,
224 						int fixup_node,
225 						uint32_t delta)
226 {
227 	int fixup_prop;
228 	int fixup_child;
229 	int ret;
230 
231 	fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
232 		const fdt32_t *fixup_val;
233 		const char *tree_val;
234 		const char *name;
235 		int fixup_len;
236 		int tree_len;
237 		int i;
238 
239 		fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
240 						  &name, &fixup_len);
241 		if (!fixup_val)
242 			return fixup_len;
243 
244 		if (fixup_len % sizeof(uint32_t))
245 			return -FDT_ERR_BADOVERLAY;
246 
247 		tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
248 		if (!tree_val) {
249 			if (tree_len == -FDT_ERR_NOTFOUND)
250 				return -FDT_ERR_BADOVERLAY;
251 
252 			return tree_len;
253 		}
254 
255 		for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
256 			fdt32_t adj_val;
257 			uint32_t poffset;
258 
259 			poffset = fdt32_to_cpu(fixup_val[i]);
260 
261 			/*
262 			 * phandles to fixup can be unaligned.
263 			 *
264 			 * Use a memcpy for the architectures that do
265 			 * not support unaligned accesses.
266 			 */
267 			memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
268 
269 			adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
270 
271 			ret = fdt_setprop_inplace_namelen_partial(fdto,
272 								  tree_node,
273 								  name,
274 								  strlen(name),
275 								  poffset,
276 								  &adj_val,
277 								  sizeof(adj_val));
278 			if (ret == -FDT_ERR_NOSPACE)
279 				return -FDT_ERR_BADOVERLAY;
280 
281 			if (ret)
282 				return ret;
283 		}
284 	}
285 
286 	fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
287 		const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
288 							    NULL);
289 		int tree_child;
290 
291 		tree_child = fdt_subnode_offset(fdto, tree_node,
292 						fixup_child_name);
293 		if (tree_child == -FDT_ERR_NOTFOUND)
294 			return -FDT_ERR_BADOVERLAY;
295 		if (tree_child < 0)
296 			return tree_child;
297 
298 		ret = overlay_update_local_node_references(fdto,
299 							   tree_child,
300 							   fixup_child,
301 							   delta);
302 		if (ret)
303 			return ret;
304 	}
305 
306 	return 0;
307 }
308 
309 /**
310  * overlay_update_local_references - Adjust the overlay references
311  * @fdto: Device tree overlay blob
312  * @delta: Offset to shift the phandles of
313  *
314  * overlay_update_local_references() update all the phandles pointing
315  * to a node within the device tree overlay by adding a constant
316  * delta to not conflict with the base overlay.
317  *
318  * This is mainly used as part of a device tree application process,
319  * where you want the device tree overlays phandles to not conflict
320  * with the ones from the base device tree before merging them.
321  *
322  * returns:
323  *      0 on success
324  *      Negative error code on failure
325  */
overlay_update_local_references(void * fdto,uint32_t delta)326 static int overlay_update_local_references(void *fdto, uint32_t delta)
327 {
328 	int fixups;
329 
330 	fixups = fdt_path_offset(fdto, "/__local_fixups__");
331 	if (fixups < 0) {
332 		/* There's no local phandles to adjust, bail out */
333 		if (fixups == -FDT_ERR_NOTFOUND)
334 			return 0;
335 
336 		return fixups;
337 	}
338 
339 	/*
340 	 * Update our local references from the root of the tree
341 	 */
342 	return overlay_update_local_node_references(fdto, 0, fixups,
343 						    delta);
344 }
345 
346 /**
347  * overlay_fixup_one_phandle - Set an overlay phandle to the base one
348  * @fdt: Base Device Tree blob
349  * @fdto: Device tree overlay blob
350  * @symbols_off: Node offset of the symbols node in the base device tree
351  * @path: Path to a node holding a phandle in the overlay
352  * @path_len: number of path characters to consider
353  * @name: Name of the property holding the phandle reference in the overlay
354  * @name_len: number of name characters to consider
355  * @poffset: Offset within the overlay property where the phandle is stored
356  * @label: Label of the node referenced by the phandle
357  *
358  * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
359  * a node in the base device tree.
360  *
361  * This is part of the device tree overlay application process, when
362  * you want all the phandles in the overlay to point to the actual
363  * base dt nodes.
364  *
365  * returns:
366  *      0 on success
367  *      Negative error code on failure
368  */
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)369 static int overlay_fixup_one_phandle(void *fdt, void *fdto,
370 				     int symbols_off,
371 				     const char *path, uint32_t path_len,
372 				     const char *name, uint32_t name_len,
373 				     int poffset, const char *label)
374 {
375 	const char *symbol_path;
376 	uint32_t phandle;
377 	fdt32_t phandle_prop;
378 	int symbol_off, fixup_off;
379 	int prop_len;
380 
381 	if (symbols_off < 0)
382 		return symbols_off;
383 
384 	symbol_path = fdt_getprop(fdt, symbols_off, label,
385 				  &prop_len);
386 	if (!symbol_path)
387 		return prop_len;
388 
389 	symbol_off = fdt_path_offset(fdt, symbol_path);
390 	if (symbol_off < 0)
391 		return symbol_off;
392 
393 	phandle = fdt_get_phandle(fdt, symbol_off);
394 	if (!phandle)
395 		return -FDT_ERR_NOTFOUND;
396 
397 	fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
398 	if (fixup_off == -FDT_ERR_NOTFOUND)
399 		return -FDT_ERR_BADOVERLAY;
400 	if (fixup_off < 0)
401 		return fixup_off;
402 
403 	phandle_prop = cpu_to_fdt32(phandle);
404 	return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
405 						   name, name_len, poffset,
406 						   &phandle_prop,
407 						   sizeof(phandle_prop));
408 };
409 
410 /**
411  * overlay_fixup_phandle - Set an overlay phandle to the base one
412  * @fdt: Base Device Tree blob
413  * @fdto: Device tree overlay blob
414  * @symbols_off: Node offset of the symbols node in the base device tree
415  * @property: Property offset in the overlay holding the list of fixups
416  *
417  * overlay_fixup_phandle() resolves all the overlay phandles pointed
418  * to in a __fixups__ property, and updates them to match the phandles
419  * in use in the base device tree.
420  *
421  * This is part of the device tree overlay application process, when
422  * you want all the phandles in the overlay to point to the actual
423  * base dt nodes.
424  *
425  * returns:
426  *      0 on success
427  *      Negative error code on failure
428  */
overlay_fixup_phandle(void * fdt,void * fdto,int symbols_off,int property)429 static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
430 				 int property)
431 {
432 	const char *value;
433 	const char *label;
434 	int len;
435 
436 	value = fdt_getprop_by_offset(fdto, property,
437 				      &label, &len);
438 	if (!value) {
439 		if (len == -FDT_ERR_NOTFOUND)
440 			return -FDT_ERR_INTERNAL;
441 
442 		return len;
443 	}
444 
445 	do {
446 		const char *path, *name, *fixup_end;
447 		const char *fixup_str = value;
448 		uint32_t path_len, name_len;
449 		uint32_t fixup_len;
450 		char *sep, *endptr;
451 		int poffset, ret;
452 
453 		fixup_end = memchr(value, '\0', len);
454 		if (!fixup_end)
455 			return -FDT_ERR_BADOVERLAY;
456 		fixup_len = fixup_end - fixup_str;
457 
458 		len -= fixup_len + 1;
459 		value += fixup_len + 1;
460 
461 		path = fixup_str;
462 		sep = memchr(fixup_str, ':', fixup_len);
463 		if (!sep || *sep != ':')
464 			return -FDT_ERR_BADOVERLAY;
465 
466 		path_len = sep - path;
467 		if (path_len == (fixup_len - 1))
468 			return -FDT_ERR_BADOVERLAY;
469 
470 		fixup_len -= path_len + 1;
471 		name = sep + 1;
472 		sep = memchr(name, ':', fixup_len);
473 		if (!sep || *sep != ':')
474 			return -FDT_ERR_BADOVERLAY;
475 
476 		name_len = sep - name;
477 		if (!name_len)
478 			return -FDT_ERR_BADOVERLAY;
479 
480 		poffset = strtoul(sep + 1, &endptr, 10);
481 		if ((*endptr != '\0') || (endptr <= (sep + 1)))
482 			return -FDT_ERR_BADOVERLAY;
483 
484 		ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
485 						path, path_len, name, name_len,
486 						poffset, label);
487 		if (ret)
488 			return ret;
489 	} while (len > 0);
490 
491 	return 0;
492 }
493 
494 /**
495  * overlay_fixup_phandles - Resolve the overlay phandles to the base
496  *                          device tree
497  * @fdt: Base Device Tree blob
498  * @fdto: Device tree overlay blob
499  *
500  * overlay_fixup_phandles() resolves all the overlay phandles pointing
501  * to nodes in the base device tree.
502  *
503  * This is one of the steps of the device tree overlay application
504  * process, when you want all the phandles in the overlay to point to
505  * the actual base dt nodes.
506  *
507  * returns:
508  *      0 on success
509  *      Negative error code on failure
510  */
overlay_fixup_phandles(void * fdt,void * fdto)511 static int overlay_fixup_phandles(void *fdt, void *fdto)
512 {
513 	int fixups_off, symbols_off;
514 	int property;
515 
516 	/* We can have overlays without any fixups */
517 	fixups_off = fdt_path_offset(fdto, "/__fixups__");
518 	if (fixups_off == -FDT_ERR_NOTFOUND)
519 		return 0; /* nothing to do */
520 	if (fixups_off < 0)
521 		return fixups_off;
522 
523 	/* And base DTs without symbols */
524 	symbols_off = fdt_path_offset(fdt, "/__symbols__");
525 	if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
526 		return symbols_off;
527 
528 	fdt_for_each_property_offset(property, fdto, fixups_off) {
529 		int ret;
530 
531 		ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
532 		if (ret)
533 			return ret;
534 	}
535 
536 	return 0;
537 }
538 
539 /**
540  * overlay_apply_node - Merges a node into the base device tree
541  * @fdt: Base Device Tree blob
542  * @target: Node offset in the base device tree to apply the fragment to
543  * @fdto: Device tree overlay blob
544  * @node: Node offset in the overlay holding the changes to merge
545  *
546  * overlay_apply_node() merges a node into a target base device tree
547  * node pointed.
548  *
549  * This is part of the final step in the device tree overlay
550  * application process, when all the phandles have been adjusted and
551  * resolved and you just have to merge overlay into the base device
552  * tree.
553  *
554  * returns:
555  *      0 on success
556  *      Negative error code on failure
557  */
overlay_apply_node(void * fdt,int target,void * fdto,int node)558 static int overlay_apply_node(void *fdt, int target,
559 			      void *fdto, int node)
560 {
561 	int property;
562 	int subnode;
563 
564 	fdt_for_each_property_offset(property, fdto, node) {
565 		const char *name;
566 		const void *prop;
567 		int prop_len;
568 		int ret;
569 
570 		prop = fdt_getprop_by_offset(fdto, property, &name,
571 					     &prop_len);
572 		if (prop_len == -FDT_ERR_NOTFOUND)
573 			return -FDT_ERR_INTERNAL;
574 		if (prop_len < 0)
575 			return prop_len;
576 
577 		ret = fdt_setprop(fdt, target, name, prop, prop_len);
578 		if (ret)
579 			return ret;
580 	}
581 
582 	fdt_for_each_subnode(subnode, fdto, node) {
583 		const char *name = fdt_get_name(fdto, subnode, NULL);
584 		int nnode;
585 		int ret;
586 
587 		nnode = fdt_add_subnode(fdt, target, name);
588 		if (nnode == -FDT_ERR_EXISTS) {
589 			nnode = fdt_subnode_offset(fdt, target, name);
590 			if (nnode == -FDT_ERR_NOTFOUND)
591 				return -FDT_ERR_INTERNAL;
592 		}
593 
594 		if (nnode < 0)
595 			return nnode;
596 
597 		ret = overlay_apply_node(fdt, nnode, fdto, subnode);
598 		if (ret)
599 			return ret;
600 	}
601 
602 	return 0;
603 }
604 
605 /**
606  * overlay_merge - Merge an overlay into its base device tree
607  * @fdt: Base Device Tree blob
608  * @fdto: Device tree overlay blob
609  *
610  * overlay_merge() merges an overlay into its base device tree.
611  *
612  * This is the next to last step in the device tree overlay application
613  * process, when all the phandles have been adjusted and resolved and
614  * you just have to merge overlay into the base device tree.
615  *
616  * returns:
617  *      0 on success
618  *      Negative error code on failure
619  */
overlay_merge(void * fdt,void * fdto)620 static int overlay_merge(void *fdt, void *fdto)
621 {
622 	int fragment;
623 
624 	fdt_for_each_subnode(fragment, fdto, 0) {
625 		int overlay;
626 		int target;
627 		int ret;
628 
629 		/*
630 		 * Each fragments will have an __overlay__ node. If
631 		 * they don't, it's not supposed to be merged
632 		 */
633 		overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
634 		if (overlay == -FDT_ERR_NOTFOUND)
635 			continue;
636 
637 		if (overlay < 0)
638 			return overlay;
639 
640 		target = overlay_get_target(fdt, fdto, fragment, NULL);
641 		if (target < 0)
642 			return target;
643 
644 		ret = overlay_apply_node(fdt, target, fdto, overlay);
645 		if (ret)
646 			return ret;
647 	}
648 
649 	return 0;
650 }
651 
get_path_len(const void * fdt,int nodeoffset)652 static int get_path_len(const void *fdt, int nodeoffset)
653 {
654 	int len = 0, namelen;
655 	const char *name;
656 
657 	FDT_RO_PROBE(fdt);
658 
659 	for (;;) {
660 		name = fdt_get_name(fdt, nodeoffset, &namelen);
661 		if (!name)
662 			return namelen;
663 
664 		/* root? we're done */
665 		if (namelen == 0)
666 			break;
667 
668 		nodeoffset = fdt_parent_offset(fdt, nodeoffset);
669 		if (nodeoffset < 0)
670 			return nodeoffset;
671 		len += namelen + 1;
672 	}
673 
674 	/* in case of root pretend it's "/" */
675 	if (len == 0)
676 		len++;
677 	return len;
678 }
679 
680 /**
681  * overlay_symbol_update - Update the symbols of base tree after a merge
682  * @fdt: Base Device Tree blob
683  * @fdto: Device tree overlay blob
684  *
685  * overlay_symbol_update() updates the symbols of the base tree with the
686  * symbols of the applied overlay
687  *
688  * This is the last step in the device tree overlay application
689  * process, allowing the reference of overlay symbols by subsequent
690  * overlay operations.
691  *
692  * returns:
693  *      0 on success
694  *      Negative error code on failure
695  */
overlay_symbol_update(void * fdt,void * fdto)696 static int overlay_symbol_update(void *fdt, void *fdto)
697 {
698 	int root_sym, ov_sym, prop, path_len, fragment, target;
699 	int len, frag_name_len, ret, rel_path_len;
700 	const char *s, *e;
701 	const char *path;
702 	const char *name;
703 	const char *frag_name;
704 	const char *rel_path;
705 	const char *target_path;
706 	char *buf;
707 	void *p;
708 
709 	ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
710 
711 	/* if no overlay symbols exist no problem */
712 	if (ov_sym < 0)
713 		return 0;
714 
715 	root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
716 
717 	/* it no root symbols exist we should create them */
718 	if (root_sym == -FDT_ERR_NOTFOUND)
719 		root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
720 
721 	/* any error is fatal now */
722 	if (root_sym < 0)
723 		return root_sym;
724 
725 	/* iterate over each overlay symbol */
726 	fdt_for_each_property_offset(prop, fdto, ov_sym) {
727 		path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
728 		if (!path)
729 			return path_len;
730 
731 		/* verify it's a string property (terminated by a single \0) */
732 		if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
733 			return -FDT_ERR_BADVALUE;
734 
735 		/* keep end marker to avoid strlen() */
736 		e = path + path_len;
737 
738 		if (*path != '/')
739 			return -FDT_ERR_BADVALUE;
740 
741 		/* get fragment name first */
742 		s = strchr(path + 1, '/');
743 		if (!s) {
744 			/* Symbol refers to something that won't end
745 			 * up in the target tree */
746 			continue;
747 		}
748 
749 		frag_name = path + 1;
750 		frag_name_len = s - path - 1;
751 
752 		/* verify format; safe since "s" lies in \0 terminated prop */
753 		len = sizeof("/__overlay__/") - 1;
754 		if ((e - s) > len && (memcmp(s, "/__overlay__/", len) == 0)) {
755 			/* /<fragment-name>/__overlay__/<relative-subnode-path> */
756 			rel_path = s + len;
757 			rel_path_len = e - rel_path;
758 		} else if ((e - s) == len
759 			   && (memcmp(s, "/__overlay__", len - 1) == 0)) {
760 			/* /<fragment-name>/__overlay__ */
761 			rel_path = "";
762 			rel_path_len = 0;
763 		} else {
764 			/* Symbol refers to something that won't end
765 			 * up in the target tree */
766 			continue;
767 		}
768 
769 		/* find the fragment index in which the symbol lies */
770 		ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
771 					       frag_name_len);
772 		/* not found? */
773 		if (ret < 0)
774 			return -FDT_ERR_BADOVERLAY;
775 		fragment = ret;
776 
777 		/* an __overlay__ subnode must exist */
778 		ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
779 		if (ret < 0)
780 			return -FDT_ERR_BADOVERLAY;
781 
782 		/* get the target of the fragment */
783 		ret = overlay_get_target(fdt, fdto, fragment, &target_path);
784 		if (ret < 0)
785 			return ret;
786 		target = ret;
787 
788 		/* if we have a target path use */
789 		if (!target_path) {
790 			ret = get_path_len(fdt, target);
791 			if (ret < 0)
792 				return ret;
793 			len = ret;
794 		} else {
795 			len = strlen(target_path);
796 		}
797 
798 		ret = fdt_setprop_placeholder(fdt, root_sym, name,
799 				len + (len > 1) + rel_path_len + 1, &p);
800 		if (ret < 0)
801 			return ret;
802 
803 		if (!target_path) {
804 			/* again in case setprop_placeholder changed it */
805 			ret = overlay_get_target(fdt, fdto, fragment, &target_path);
806 			if (ret < 0)
807 				return ret;
808 			target = ret;
809 		}
810 
811 		buf = p;
812 		if (len > 1) { /* target is not root */
813 			if (!target_path) {
814 				ret = fdt_get_path(fdt, target, buf, len + 1);
815 				if (ret < 0)
816 					return ret;
817 			} else
818 				memcpy(buf, target_path, len + 1);
819 
820 		} else
821 			len--;
822 
823 		buf[len] = '/';
824 		memcpy(buf + len + 1, rel_path, rel_path_len);
825 		buf[len + 1 + rel_path_len] = '\0';
826 	}
827 
828 	return 0;
829 }
830 
fdt_overlay_apply(void * fdt,void * fdto)831 int fdt_overlay_apply(void *fdt, void *fdto)
832 {
833 	uint32_t delta;
834 	int ret;
835 
836 	FDT_RO_PROBE(fdt);
837 	FDT_RO_PROBE(fdto);
838 
839 	ret = fdt_find_max_phandle(fdt, &delta);
840 	if (ret)
841 		goto err;
842 
843 	ret = overlay_adjust_local_phandles(fdto, delta);
844 	if (ret)
845 		goto err;
846 
847 	ret = overlay_update_local_references(fdto, delta);
848 	if (ret)
849 		goto err;
850 
851 	ret = overlay_fixup_phandles(fdt, fdto);
852 	if (ret)
853 		goto err;
854 
855 	ret = overlay_merge(fdt, fdto);
856 	if (ret)
857 		goto err;
858 
859 	ret = overlay_symbol_update(fdt, fdto);
860 	if (ret)
861 		goto err;
862 
863 	/*
864 	 * The overlay has been damaged, erase its magic.
865 	 */
866 	fdt_set_magic(fdto, ~0);
867 
868 	return 0;
869 
870 err:
871 	/*
872 	 * The overlay might have been damaged, erase its magic.
873 	 */
874 	fdt_set_magic(fdto, ~0);
875 
876 	/*
877 	 * The base device tree might have been damaged, erase its
878 	 * magic.
879 	 */
880 	fdt_set_magic(fdt, ~0);
881 
882 	return ret;
883 }
884