summaryrefslogtreecommitdiffstats
path: root/drivers/of/resolver.c
blob: 9107c1fbb68ca4bd212cf7d244ade38c16ab1010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// SPDX-License-Identifier: GPL-2.0
/*
 * Functions for dealing with DT resolution
 *
 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
 * Copyright (C) 2012 Texas Instruments Inc.
 * Copyright (C) 2019 Pengutronix, Michael Tretter <m.tretter@pengutronix.de>
 */
#define pr_fmt(fmt) "of_resolver: " fmt

#include <common.h>
#include <of.h>
#include <errno.h>

/**
 * Recursively update phandles in overlay by adding delta
 */
static void adjust_overlay_phandles(struct device_node *overlay, int delta)
{
	struct device_node *child;
	struct property *prop;

	if (overlay->phandle != 0)
		overlay->phandle += delta;

	list_for_each_entry(prop, &overlay->properties, list) {
		if (of_prop_cmp(prop->name, "phandle") != 0 &&
		    of_prop_cmp(prop->name, "linux,phandle") != 0)
			continue;
		if (prop->length < 4)
			continue;

		be32_add_cpu(prop->value, delta);
	}

	for_each_child_of_node(overlay, child)
		adjust_overlay_phandles(child, delta);
}

/**
 * Update all unresolved phandles in the overlay using prop_fixup
 *
 * prop_fixup contains a list of tuples of path:property_name:offset, each of
 * which refers to a property that is phandle to a node in the base
 * devicetree.
 */
static int update_usages_of_a_phandle_reference(struct device_node *overlay,
						struct property *prop_fixup,
						phandle phandle)
{
	struct device_node *refnode;
	struct property *prop;
	char *value, *cur, *end, *node_path, *prop_name, *s;
	int offset, len;
	int err = 0;

	pr_debug("resolve references to %s to phandle 0x%x\n",
		 prop_fixup->name, phandle);

	value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
	if (!value)
		return -ENOMEM;

	end = value + prop_fixup->length;
	for (cur = value; cur < end; cur += len + 1) {
		len = strlen(cur);

		node_path = cur;
		s = strchr(cur, ':');
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}
		*s++ = '\0';

		prop_name = s;
		s = strchr(s, ':');
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}
		*s++ = '\0';

		err = kstrtoint(s, 10, &offset);
		if (err)
			goto err_fail;

		refnode = of_find_node_by_path_from(overlay, node_path);
		if (!refnode)
			continue;

		prop = of_find_property(refnode, prop_name, NULL);
		if (!prop) {
			err = -ENOENT;
			goto err_fail;
		}

		if (offset < 0 || offset + sizeof(__be32) > prop->length) {
			err = -EINVAL;
			goto err_fail;
		}

		*(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
	}

err_fail:
	kfree(value);

	if (err)
		pr_debug("failed to resolve references to %s\n",
			 prop_fixup->name);

	return err;
}

/*
 * Adjust the local phandle references by the given phandle delta.
 *
 * Subtree @local_fixups, which is overlay node __local_fixups__,
 * mirrors the fragment node structure at the root of the overlay.
 *
 * For each property in the fragments that contains a phandle reference,
 * @local_fixups has a property of the same name that contains a list
 * of offsets of the phandle reference(s) within the respective property
 * value(s).  The values at these offsets will be fixed up.
 */
static int adjust_local_phandle_references(struct device_node *local_fixups,
		struct device_node *overlay, int phandle_delta)
{
	struct device_node *child, *overlay_child;
	struct property *prop_fix, *prop;
	int err, i, count;
	unsigned int off;

	if (!local_fixups)
		return 0;

	list_for_each_entry(prop_fix, &local_fixups->properties, list) {
		if (!of_prop_cmp(prop_fix->name, "name") ||
		    !of_prop_cmp(prop_fix->name, "phandle") ||
		    !of_prop_cmp(prop_fix->name, "linux,phandle"))
			continue;

		if ((prop_fix->length % sizeof(__be32)) != 0 ||
		    prop_fix->length == 0)
			return -EINVAL;
		count = prop_fix->length / sizeof(__be32);

		prop = of_find_property(overlay, prop_fix->name, NULL);
		if (!prop)
			return -EINVAL;

		for (i = 0; i < count; i++) {
			off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
			if ((off + sizeof(__be32)) > prop->length)
				return -EINVAL;

			be32_add_cpu(prop->value + off, phandle_delta);
		}
	}

	for_each_child_of_node(local_fixups, child) {
		for_each_child_of_node(overlay, overlay_child)
			if (!of_node_cmp(child->name, overlay_child->name))
				break;
		if (!overlay_child)
			return -EINVAL;

		err = adjust_local_phandle_references(child, overlay_child,
				phandle_delta);
		if (err)
			return err;
	}

	return 0;
}

/**
 * of_resolve_phandles - Resolve phandles in overlay based on root
 *
 * Rename phandles in overlay to avoid conflicts with the base devicetree and
 * replace all phandles in the overlay with their renamed versions. Resolve
 * phandles referring to nodes in the base devicetree with the phandle from
 * the base devicetree.
 *
 * Returns a new device_node with resolved phandles which must be deleted by
 * the caller of this function.
 */
struct device_node *of_resolve_phandles(struct device_node *root,
					const struct device_node *overlay)
{
	struct device_node *result;
	struct device_node *local_fixups;
	struct device_node *refnode;
	struct device_node *symbols;
	struct device_node *overlay_fixups;
	struct property *prop;
	const char *refpath;
	phandle delta;
	int err;

	result = of_copy_node(NULL, overlay);
	if (!result)
		return NULL;

	delta = of_get_tree_max_phandle(root) + 1;

	/*
	 * Rename the phandles in the devicetree overlay to prevent conflicts
	 * with the phandles in the base devicetree.
	 */
	adjust_overlay_phandles(result, delta);

	/*
	 * __local_fixups__ contains all locations in the overlay that refer
	 * to a phandle defined in the overlay. We must update the references,
	 * because we just adjusted the definitions.
	 */
	local_fixups = of_find_node_by_name(result, "__local_fixups__");
	err = adjust_local_phandle_references(local_fixups, result, delta);
	if (err) {
		pr_err("failed to fix phandles in overlay\n");
		goto err;
	}

	/*
	 * __fixups__ contains all locations in the overlay that refer to a
	 * phandle that is not defined in the overlay and should be defined in
	 * the base device tree. We must update the references, because they
	 * are otherwise undefined.
	 */
	overlay_fixups = of_find_node_by_name(result, "__fixups__");
	if (!overlay_fixups) {
		pr_debug("overlay does not contain phandles to base devicetree\n");
		goto out;
	}

	symbols = of_find_node_by_path_from(root, "/__symbols__");
	if (!symbols) {
		pr_err("__symbols__ missing from base devicetree\n");
		goto err;
	}

	list_for_each_entry(prop, &overlay_fixups->properties, list) {
		if (!of_prop_cmp(prop->name, "name"))
			continue;

		err = of_property_read_string(symbols, prop->name, &refpath);
		if (err) {
			pr_err("cannot find node %s in base devicetree\n",
			       prop->name);
			goto err;
		}

		refnode = of_find_node_by_path_from(root, refpath);
		if (!refnode) {
			pr_err("cannot find path %s in base devicetree\n",
			       refpath);
			err = -EINVAL;
			goto err;
		}

		err = update_usages_of_a_phandle_reference(result, prop,
							   refnode->phandle);
		if (err) {
			pr_err("failed to update phandles for %s in overlay",
			       prop->name);
			goto err;
		}
	}

out:
	return result;
err:
	of_delete_node(result);

	return NULL;

}