1#include "libfdt_env.h"
2
3#include <fdt.h>
4#include <libfdt.h>
5
6#include "libfdt_internal.h"
7
8/**
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 */
22static 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
31	if ((len != sizeof(*val)) || (*val == (uint32_t)-1))
32		return (uint32_t)-1;
33
34	return fdt32_to_cpu(*val);
35}
36
37/**
38 * overlay_get_target - retrieves the offset of a fragment's target
39 * @fdt: Base device tree blob
40 * @fdto: Device tree overlay blob
41 * @fragment: node offset of the fragment in the overlay
42 *
43 * overlay_get_target() retrieves the target offset in the base
44 * 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 */
51static int overlay_get_target(const void *fdt, const void *fdto,
52			      int fragment)
53{
54	uint32_t phandle;
55	const char *path;
56	int path_len;
57
58	/* Try first to do a phandle based lookup */
59	phandle = overlay_get_target_phandle(fdto, fragment);
60	if (phandle == (uint32_t)-1)
61		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 */
67	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	}
81
82	return fdt_path_offset(fdt, path);
83}
84
85/**
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 */
99static 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))
111		return -FDT_ERR_BADPHANDLE;
112
113	adj_val = fdt32_to_cpu(*val);
114	if ((adj_val + delta) < adj_val)
115		return -FDT_ERR_NOPHANDLES;
116
117	adj_val += delta;
118	if (adj_val == (uint32_t)-1)
119		return -FDT_ERR_NOPHANDLES;
120
121	return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
122}
123
124/**
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 */
139static int overlay_adjust_node_phandles(void *fdto, int node,
140					uint32_t delta)
141{
142	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
149	ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
150	if (ret && ret != -FDT_ERR_NOTFOUND)
151		return ret;
152
153	fdt_for_each_subnode(child, fdto, node) {
154		ret = overlay_adjust_node_phandles(fdto, child, delta);
155		if (ret)
156			return ret;
157	}
158
159	return 0;
160}
161
162/**
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 */
176static 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
184/**
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 */
203static 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) {
213		const uint32_t *fixup_val;
214		const char *tree_val;
215		const char *name;
216		int fixup_len;
217		int tree_len;
218		int i;
219
220		fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
221						  &name, &fixup_len);
222		if (!fixup_val)
223			return fixup_len;
224
225		if (fixup_len % sizeof(uint32_t))
226			return -FDT_ERR_BADOVERLAY;
227
228		tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
229		if (!tree_val) {
230			if (tree_len == -FDT_ERR_NOTFOUND)
231				return -FDT_ERR_BADOVERLAY;
232
233			return tree_len;
234		}
235
236		for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
237			uint32_t adj_val, poffset;
238
239			poffset = fdt32_to_cpu(fixup_val[i]);
240
241			/*
242			 * phandles to fixup can be unaligned.
243			 *
244			 * Use a memcpy for the architectures that do
245			 * not support unaligned accesses.
246			 */
247			memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
248
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),
257								  poffset,
258								  &adj_val,
259								  sizeof(adj_val));
260			if (ret == -FDT_ERR_NOSPACE)
261				return -FDT_ERR_BADOVERLAY;
262
263			if (ret)
264				return ret;
265		}
266	}
267
268	fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
269		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);
275		if (ret == -FDT_ERR_NOTFOUND)
276			return -FDT_ERR_BADOVERLAY;
277		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
291/**
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)
309{
310	int fixups;
311
312	fixups = fdt_path_offset(fdto, "/__local_fixups__");
313	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	 */
324	return overlay_update_local_node_references(fdto, 0, fixups,
325						    delta);
326}
327
328/**
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
337 * @poffset: Offset within the overlay property where the phandle is stored
338 * @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 */
351static 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,
355				     int poffset, const char *label)
356{
357	const char *symbol_path;
358	uint32_t phandle;
359	int symbol_off, fixup_off;
360	int prop_len;
361
362	if (symbols_off < 0)
363		return symbols_off;
364
365	symbol_path = fdt_getprop(fdt, symbols_off, label,
366				  &prop_len);
367	if (!symbol_path)
368		return prop_len;
369
370	symbol_off = fdt_path_offset(fdt, symbol_path);
371	if (symbol_off < 0)
372		return symbol_off;
373
374	phandle = fdt_get_phandle(fdt, symbol_off);
375	if (!phandle)
376		return -FDT_ERR_NOTFOUND;
377
378	fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
379	if (fixup_off == -FDT_ERR_NOTFOUND)
380		return -FDT_ERR_BADOVERLAY;
381	if (fixup_off < 0)
382		return fixup_off;
383
384	phandle = cpu_to_fdt32(phandle);
385	return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
386						   name, name_len, poffset,
387						   &phandle, sizeof(phandle));
388};
389
390/**
391 * overlay_fixup_phandle - Set an overlay phandle to the base one
392 * @fdt: Base Device Tree blob
393 * @fdto: Device tree overlay blob
394 * @symbols_off: Node offset of the symbols node in the base device tree
395 * @property: Property offset in the overlay holding the list of fixups
396 *
397 * overlay_fixup_phandle() resolves all the overlay phandles pointed
398 * to in a __fixups__ property, and updates them to match the phandles
399 * in use in the base device tree.
400 *
401 * This is part of the device tree overlay application process, when
402 * you want all the phandles in the overlay to point to the actual
403 * base dt nodes.
404 *
405 * returns:
406 *      0 on success
407 *      Negative error code on failure
408 */
409static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
410				 int property)
411{
412	const char *value;
413	const char *label;
414	int len;
415
416	value = fdt_getprop_by_offset(fdto, property,
417				      &label, &len);
418	if (!value) {
419		if (len == -FDT_ERR_NOTFOUND)
420			return -FDT_ERR_INTERNAL;
421
422		return len;
423	}
424
425	do {
426		const char *path, *name, *fixup_end;
427		const char *fixup_str = value;
428		uint32_t path_len, name_len;
429		uint32_t fixup_len;
430		char *sep, *endptr;
431		int poffset, ret;
432
433		fixup_end = memchr(value, '\0', len);
434		if (!fixup_end)
435			return -FDT_ERR_BADOVERLAY;
436		fixup_len = fixup_end - fixup_str;
437
438		len -= fixup_len + 1;
439		value += fixup_len + 1;
440
441		path = fixup_str;
442		sep = memchr(fixup_str, ':', fixup_len);
443		if (!sep || *sep != ':')
444			return -FDT_ERR_BADOVERLAY;
445
446		path_len = sep - path;
447		if (path_len == (fixup_len - 1))
448			return -FDT_ERR_BADOVERLAY;
449
450		fixup_len -= path_len + 1;
451		name = sep + 1;
452		sep = memchr(name, ':', fixup_len);
453		if (!sep || *sep != ':')
454			return -FDT_ERR_BADOVERLAY;
455
456		name_len = sep - name;
457		if (!name_len)
458			return -FDT_ERR_BADOVERLAY;
459
460		poffset = strtoul(sep + 1, &endptr, 10);
461		if ((*endptr != '\0') || (endptr <= (sep + 1)))
462			return -FDT_ERR_BADOVERLAY;
463
464		ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
465						path, path_len, name, name_len,
466						poffset, label);
467		if (ret)
468			return ret;
469	} while (len > 0);
470
471	return 0;
472}
473
474/**
475 * overlay_fixup_phandles - Resolve the overlay phandles to the base
476 *                          device tree
477 * @fdt: Base Device Tree blob
478 * @fdto: Device tree overlay blob
479 *
480 * overlay_fixup_phandles() resolves all the overlay phandles pointing
481 * to nodes in the base device tree.
482 *
483 * This is one of the steps of the device tree overlay application
484 * process, when you want all the phandles in the overlay to point to
485 * the actual base dt nodes.
486 *
487 * returns:
488 *      0 on success
489 *      Negative error code on failure
490 */
491static int overlay_fixup_phandles(void *fdt, void *fdto)
492{
493	int fixups_off, symbols_off;
494	int property;
495
496	/* We can have overlays without any fixups */
497	fixups_off = fdt_path_offset(fdto, "/__fixups__");
498	if (fixups_off == -FDT_ERR_NOTFOUND)
499		return 0; /* nothing to do */
500	if (fixups_off < 0)
501		return fixups_off;
502
503	/* And base DTs without symbols */
504	symbols_off = fdt_path_offset(fdt, "/__symbols__");
505	if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
506		return symbols_off;
507
508	fdt_for_each_property_offset(property, fdto, fixups_off) {
509		int ret;
510
511		ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
512		if (ret)
513			return ret;
514	}
515
516	return 0;
517}
518
519/**
520 * overlay_apply_node - Merges a node into the base device tree
521 * @fdt: Base Device Tree blob
522 * @target: Node offset in the base device tree to apply the fragment to
523 * @fdto: Device tree overlay blob
524 * @node: Node offset in the overlay holding the changes to merge
525 *
526 * overlay_apply_node() merges a node into a target base device tree
527 * node pointed.
528 *
529 * This is part of the final step in the device tree overlay
530 * application process, when all the phandles have been adjusted and
531 * resolved and you just have to merge overlay into the base device
532 * tree.
533 *
534 * returns:
535 *      0 on success
536 *      Negative error code on failure
537 */
538static int overlay_apply_node(void *fdt, int target,
539			      void *fdto, int node)
540{
541	int property;
542	int subnode;
543
544	fdt_for_each_property_offset(property, fdto, node) {
545		const char *name;
546		const void *prop;
547		int prop_len;
548		int ret;
549
550		prop = fdt_getprop_by_offset(fdto, property, &name,
551					     &prop_len);
552		if (prop_len == -FDT_ERR_NOTFOUND)
553			return -FDT_ERR_INTERNAL;
554		if (prop_len < 0)
555			return prop_len;
556
557		ret = fdt_setprop(fdt, target, name, prop, prop_len);
558		if (ret)
559			return ret;
560	}
561
562	fdt_for_each_subnode(subnode, fdto, node) {
563		const char *name = fdt_get_name(fdto, subnode, NULL);
564		int nnode;
565		int ret;
566
567		nnode = fdt_add_subnode(fdt, target, name);
568		if (nnode == -FDT_ERR_EXISTS) {
569			nnode = fdt_subnode_offset(fdt, target, name);
570			if (nnode == -FDT_ERR_NOTFOUND)
571				return -FDT_ERR_INTERNAL;
572		}
573
574		if (nnode < 0)
575			return nnode;
576
577		ret = overlay_apply_node(fdt, nnode, fdto, subnode);
578		if (ret)
579			return ret;
580	}
581
582	return 0;
583}
584
585/**
586 * overlay_merge - Merge an overlay into its base device tree
587 * @fdt: Base Device Tree blob
588 * @fdto: Device tree overlay blob
589 *
590 * overlay_merge() merges an overlay into its base device tree.
591 *
592 * This is the final step in the device tree overlay application
593 * process, when all the phandles have been adjusted and resolved and
594 * you just have to merge overlay into the base device tree.
595 *
596 * returns:
597 *      0 on success
598 *      Negative error code on failure
599 */
600static int overlay_merge(void *fdt, void *fdto)
601{
602	int fragment;
603
604	fdt_for_each_subnode(fragment, fdto, 0) {
605		int overlay;
606		int target;
607		int ret;
608
609		/*
610		 * Each fragments will have an __overlay__ node. If
611		 * they don't, it's not supposed to be merged
612		 */
613		overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
614		if (overlay == -FDT_ERR_NOTFOUND)
615			continue;
616
617		if (overlay < 0)
618			return overlay;
619
620		target = overlay_get_target(fdt, fdto, fragment);
621		if (target < 0)
622			return target;
623
624		ret = overlay_apply_node(fdt, target, fdto, overlay);
625		if (ret)
626			return ret;
627	}
628
629	return 0;
630}
631
632int fdt_overlay_apply(void *fdt, void *fdto)
633{
634	uint32_t delta = fdt_get_max_phandle(fdt);
635	int ret;
636
637	FDT_CHECK_HEADER(fdt);
638	FDT_CHECK_HEADER(fdto);
639
640	ret = overlay_adjust_local_phandles(fdto, delta);
641	if (ret)
642		goto err;
643
644	ret = overlay_update_local_references(fdto, delta);
645	if (ret)
646		goto err;
647
648	ret = overlay_fixup_phandles(fdt, fdto);
649	if (ret)
650		goto err;
651
652	ret = overlay_merge(fdt, fdto);
653	if (ret)
654		goto err;
655
656	/*
657	 * The overlay has been damaged, erase its magic.
658	 */
659	fdt_set_magic(fdto, ~0);
660
661	return 0;
662
663err:
664	/*
665	 * The overlay might have been damaged, erase its magic.
666	 */
667	fdt_set_magic(fdto, ~0);
668
669	/*
670	 * The base device tree might have been damaged, erase its
671	 * magic.
672	 */
673	fdt_set_magic(fdt, ~0);
674
675	return ret;
676}
677