resolver.c 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Functions for dealing with DT resolution
 *
 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
 * Copyright (C) 2012 Texas Instruments Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 */

12 13
#define pr_fmt(fmt)	"OF: resolver: " fmt

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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/slab.h>

/* illegal phandle value (set when unresolved) */
#define OF_PHANDLE_ILLEGAL	0xdeadbeef

/**
 * Find a node with the give full name by recursively following any of
 * the child node links.
 */
static struct device_node *__of_find_node_by_full_name(struct device_node *node,
		const char *full_name)
{
	struct device_node *child, *found;

	if (node == NULL)
		return NULL;

	if (of_node_cmp(node->full_name, full_name) == 0)
40
		return of_node_get(node);
41 42 43

	for_each_child_of_node(node, child) {
		found = __of_find_node_by_full_name(child, full_name);
44 45
		if (found != NULL) {
			of_node_put(child);
46
			return found;
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
	}

	return NULL;
}

/*
 * Find live tree's maximum phandle value.
 */
static phandle of_get_tree_max_phandle(void)
{
	struct device_node *node;
	phandle phandle;
	unsigned long flags;

	raw_spin_lock_irqsave(&devtree_lock, flags);
	phandle = 0;
	for_each_of_allnodes(node) {
		if (node->phandle != OF_PHANDLE_ILLEGAL &&
				node->phandle > phandle)
			phandle = node->phandle;
	}
	raw_spin_unlock_irqrestore(&devtree_lock, flags);

	return phandle;
}

/*
 * Adjust a subtree's phandle values by a given delta.
 */
static void __of_adjust_tree_phandles(struct device_node *node,
		int phandle_delta)
{
	struct device_node *child;
	struct property *prop;
	phandle phandle;

	if (node->phandle != 0 && node->phandle != OF_PHANDLE_ILLEGAL)
		node->phandle += phandle_delta;

	for_each_property_of_node(node, prop) {

		if (of_prop_cmp(prop->name, "phandle") != 0 &&
		    of_prop_cmp(prop->name, "linux,phandle") != 0)
			continue;

		if (prop->length < 4)
			continue;

		phandle = be32_to_cpup(prop->value);
97
		if (phandle == OF_PHANDLE_ILLEGAL)
98 99 100 101 102 103 104 105 106
			continue;

		*(uint32_t *)prop->value = cpu_to_be32(node->phandle);
	}

	for_each_child_of_node(node, child)
		__of_adjust_tree_phandles(child, phandle_delta);
}

107 108
static int __of_adjust_phandle_ref(struct device_node *node,
		struct property *rprop, int value)
109 110 111 112 113 114 115 116 117
{
	phandle phandle;
	struct device_node *refnode;
	struct property *sprop;
	char *propval, *propcur, *propend, *nodestr, *propstr, *s;
	int offset, propcurlen;
	int err = 0;

	propval = kmalloc(rprop->length, GFP_KERNEL);
118
	if (!propval)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		return -ENOMEM;
	memcpy(propval, rprop->value, rprop->length);

	propend = propval + rprop->length;
	for (propcur = propval; propcur < propend; propcur += propcurlen + 1) {
		propcurlen = strlen(propcur);

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

		propstr = s;
		s = strchr(s, ':');
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}

		*s++ = '\0';
		err = kstrtoint(s, 10, &offset);
143
		if (err != 0)
144 145 146
			goto err_fail;

		refnode = __of_find_node_by_full_name(node, nodestr);
147
		if (!refnode)
148 149 150 151 152 153
			continue;

		for_each_property_of_node(refnode, sprop) {
			if (of_prop_cmp(sprop->name, propstr) == 0)
				break;
		}
154
		of_node_put(refnode);
155 156 157 158 159 160

		if (!sprop) {
			err = -ENOENT;
			goto err_fail;
		}

161
		phandle = value;
162 163 164 165 166 167 168 169
		*(__be32 *)(sprop->value + offset) = cpu_to_be32(phandle);
	}

err_fail:
	kfree(propval);
	return err;
}

170 171 172 173 174 175 176 177 178 179
/* compare nodes taking into account that 'name' strips out the @ part */
static int __of_node_name_cmp(const struct device_node *dn1,
		const struct device_node *dn2)
{
	const char *n1 = strrchr(dn1->full_name, '/') ? : "/";
	const char *n2 = strrchr(dn2->full_name, '/') ? : "/";

	return of_node_cmp(n1, n2);
}

180 181
/*
 * Adjust the local phandle references by the given phandle delta.
182 183 184 185
 * Assumes the existances of a __local_fixups__ node at the root.
 * Assumes that __of_verify_tree_phandle_references has been called.
 * Does not take any devtree locks so make sure you call this on a tree
 * which is at the detached state.
186 187
 */
static int __of_adjust_tree_phandle_references(struct device_node *node,
188
		struct device_node *target, int phandle_delta)
189
{
190 191 192 193 194
	struct device_node *child, *childtarget;
	struct property *rprop, *sprop;
	int err, i, count;
	unsigned int off;
	phandle phandle;
195

196
	if (node == NULL)
197 198
		return 0;

199 200
	for_each_property_of_node(node, rprop) {

201
		/* skip properties added automatically */
202 203 204
		if (of_prop_cmp(rprop->name, "name") == 0 ||
		    of_prop_cmp(rprop->name, "phandle") == 0 ||
		    of_prop_cmp(rprop->name, "linux,phandle") == 0)
205 206
			continue;

207
		if ((rprop->length % 4) != 0 || rprop->length == 0)
208 209 210 211 212 213 214 215
			return -EINVAL;
		count = rprop->length / sizeof(__be32);

		for_each_property_of_node(target, sprop) {
			if (of_prop_cmp(sprop->name, rprop->name) == 0)
				break;
		}

216
		if (sprop == NULL)
217 218 219 220
			return -EINVAL;

		for (i = 0; i < count; i++) {
			off = be32_to_cpu(((__be32 *)rprop->value)[i]);
221
			if (off >= sprop->length || (off + 4) > sprop->length)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
				return -EINVAL;

			if (phandle_delta) {
				phandle = be32_to_cpu(*(__be32 *)(sprop->value + off));
				phandle += phandle_delta;
				*(__be32 *)(sprop->value + off) = cpu_to_be32(phandle);
			}
		}
	}

	for_each_child_of_node(node, child) {

		for_each_child_of_node(target, childtarget)
			if (__of_node_name_cmp(child, childtarget) == 0)
				break;

238
		if (!childtarget)
239 240 241 242 243
			return -EINVAL;

		err = __of_adjust_tree_phandle_references(child, childtarget,
				phandle_delta);
		if (err != 0)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
			return err;
	}

	return 0;
}

/**
 * of_resolve	- Resolve the given node against the live tree.
 *
 * @resolve:	Node to resolve
 *
 * Perform dynamic Device Tree resolution against the live tree
 * to the given node to resolve. This depends on the live tree
 * having a __symbols__ node, and the resolve node the __fixups__ &
 * __local_fixups__ nodes (if needed).
 * The result of the operation is a resolve node that it's contents
 * are fit to be inserted or operate upon the live tree.
 * Returns 0 on success or a negative error value on error.
 */
int of_resolve_phandles(struct device_node *resolve)
{
265
	struct device_node *child, *childroot, *refnode;
266 267 268 269 270 271
	struct device_node *root_sym, *resolve_sym, *resolve_fix;
	struct property *rprop;
	const char *refpath;
	phandle phandle, phandle_delta;
	int err;

272 273 274 275 276
	if (!resolve)
		pr_err("%s: null node\n", __func__);
	if (resolve && !of_node_check_flag(resolve, OF_DETACHED))
		pr_err("%s: node %s not detached\n", __func__,
			 resolve->full_name);
277 278 279 280 281
	if (!resolve || !of_node_check_flag(resolve, OF_DETACHED))
		return -EINVAL;

	phandle_delta = of_get_tree_max_phandle() + 1;
	__of_adjust_tree_phandles(resolve, phandle_delta);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

	childroot = NULL;
	for_each_child_of_node(resolve, childroot)
		if (of_node_cmp(childroot->name, "__local_fixups__") == 0)
			break;

	if (childroot != NULL) {
		err = __of_adjust_tree_phandle_references(childroot,
				resolve, 0);
		if (err != 0)
			return err;

		BUG_ON(__of_adjust_tree_phandle_references(childroot,
				resolve, phandle_delta));
	}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

	root_sym = NULL;
	resolve_sym = NULL;
	resolve_fix = NULL;

	root_sym = of_find_node_by_path("/__symbols__");

	for_each_child_of_node(resolve, child) {

		if (!resolve_sym &&
				of_node_cmp(child->name, "__symbols__") == 0)
			resolve_sym = child;

		if (!resolve_fix &&
				of_node_cmp(child->name, "__fixups__") == 0)
			resolve_fix = child;

		if (resolve_sym && resolve_fix)
			break;
	}

	if (!resolve_fix) {
319
		err = 0;
320 321 322 323
		goto out;
	}

	if (!root_sym) {
324
		pr_err("%s: no symbols in root of device tree.\n", __func__);
325 326 327 328 329 330 331 332 333 334 335 336
		err = -EINVAL;
		goto out;
	}

	for_each_property_of_node(resolve_fix, rprop) {

		/* skip properties added automatically */
		if (of_prop_cmp(rprop->name, "name") == 0)
			continue;

		err = of_property_read_string(root_sym,
				rprop->name, &refpath);
337
		if (err != 0)
338 339 340 341 342 343 344 345 346 347 348
			goto out;

		refnode = of_find_node_by_path(refpath);
		if (!refnode) {
			err = -ENOENT;
			goto out;
		}

		phandle = refnode->phandle;
		of_node_put(refnode);

349
		err = __of_adjust_phandle_ref(resolve, rprop, phandle);
350 351 352 353 354 355 356 357 358 359
		if (err)
			break;
	}

out:
	of_node_put(root_sym);

	return err;
}
EXPORT_SYMBOL_GPL(of_resolve_phandles);