Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Functions for working with device tree overlays
  3 *
  4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
  5 * Copyright (C) 2012 Texas Instruments Inc.
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * version 2 as published by the Free Software Foundation.
 10 */
 11#undef DEBUG
 
 
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/of.h>
 15#include <linux/of_device.h>
 
 16#include <linux/string.h>
 17#include <linux/ctype.h>
 18#include <linux/errno.h>
 19#include <linux/string.h>
 20#include <linux/slab.h>
 
 21#include <linux/err.h>
 22#include <linux/idr.h>
 23
 24#include "of_private.h"
 25
 26/**
 27 * struct of_overlay_info - Holds a single overlay info
 28 * @target:	target of the overlay operation
 29 * @overlay:	pointer to the overlay contents node
 30 *
 31 * Holds a single overlay state, including all the overlay logs &
 32 * records.
 33 */
 34struct of_overlay_info {
 35	struct device_node *target;
 36	struct device_node *overlay;
 37};
 38
 39/**
 40 * struct of_overlay - Holds a complete overlay transaction
 41 * @node:	List on which we are located
 42 * @count:	Count of ovinfo structures
 43 * @ovinfo_tab:	Overlay info table (count sized)
 44 * @cset:	Changeset to be used
 45 *
 46 * Holds a complete overlay transaction
 
 
 47 */
 48struct of_overlay {
 49	int id;
 50	struct list_head node;
 
 
 51	int count;
 52	struct of_overlay_info *ovinfo_tab;
 
 53	struct of_changeset cset;
 54};
 55
 56static int of_overlay_apply_one(struct of_overlay *ov,
 57		struct device_node *target, const struct device_node *overlay);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 58
 59static int of_overlay_apply_single_property(struct of_overlay *ov,
 60		struct device_node *target, struct property *prop)
 
 
 
 
 
 
 
 
 
 
 
 
 
 61{
 62	struct property *propn, *tprop;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 63
 64	/* NOTE: Multiple changes of single properties not supported */
 65	tprop = of_find_property(target, prop->name, NULL);
 66
 67	/* special properties are not meant to be updated (silent NOP) */
 68	if (of_prop_cmp(prop->name, "name") == 0 ||
 69	    of_prop_cmp(prop->name, "phandle") == 0 ||
 70	    of_prop_cmp(prop->name, "linux,phandle") == 0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 71		return 0;
 72
 73	propn = __of_prop_dup(prop, GFP_KERNEL);
 74	if (propn == NULL)
 75		return -ENOMEM;
 
 
 
 
 76
 77	/* not found? add */
 78	if (tprop == NULL)
 79		return of_changeset_add_property(&ov->cset, target, propn);
 80
 81	/* found? update */
 82	return of_changeset_update_property(&ov->cset, target, propn);
 
 
 
 
 
 
 
 
 
 
 
 83}
 84
 85static int of_overlay_apply_single_device_node(struct of_overlay *ov,
 86		struct device_node *target, struct device_node *child)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 87{
 88	const char *cname;
 89	struct device_node *tchild;
 90	int ret = 0;
 91
 92	cname = kbasename(child->full_name);
 93	if (cname == NULL)
 94		return -ENOMEM;
 95
 96	/* NOTE: Multiple mods of created nodes not supported */
 97	tchild = of_get_child_by_name(target, cname);
 98	if (tchild != NULL) {
 99		/* apply overlay recursively */
100		ret = of_overlay_apply_one(ov, tchild, child);
101		of_node_put(tchild);
102	} else {
103		/* create empty tree as a target */
104		tchild = __of_node_dup(child, "%s/%s", target->full_name, cname);
105		if (!tchild)
106			return -ENOMEM;
107
108		/* point to parent */
109		tchild->parent = target;
110
111		ret = of_changeset_attach_node(&ov->cset, tchild);
112		if (ret)
113			return ret;
114
115		ret = of_overlay_apply_one(ov, tchild, child);
116		if (ret)
117			return ret;
118	}
119
 
 
 
 
 
 
120	return ret;
121}
122
123/*
124 * Apply a single overlay node recursively.
 
 
 
 
 
 
 
125 *
126 * Note that the in case of an error the target node is left
127 * in a inconsistent state. Error recovery should be performed
128 * by using the changeset.
 
129 */
130static int of_overlay_apply_one(struct of_overlay *ov,
131		struct device_node *target, const struct device_node *overlay)
 
132{
133	struct device_node *child;
134	struct property *prop;
135	int ret;
136
137	for_each_property_of_node(overlay, prop) {
138		ret = of_overlay_apply_single_property(ov, target, prop);
139		if (ret) {
140			pr_err("%s: Failed to apply prop @%s/%s\n",
141				__func__, target->full_name, prop->name);
142			return ret;
143		}
144	}
145
146	for_each_child_of_node(overlay, child) {
147		ret = of_overlay_apply_single_device_node(ov, target, child);
148		if (ret != 0) {
149			pr_err("%s: Failed to apply single node @%s/%s\n",
150					__func__, target->full_name,
151					child->name);
152			of_node_put(child);
153			return ret;
154		}
155	}
156
157	return 0;
158}
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160/**
161 * of_overlay_apply() - Apply @count overlays pointed at by @ovinfo_tab
162 * @ov:		Overlay to apply
163 *
164 * Applies the overlays given, while handling all error conditions
165 * appropriately. Either the operation succeeds, or if it fails the
166 * live tree is reverted to the state before the attempt.
167 * Returns 0, or an error if the overlay attempt failed.
168 */
169static int of_overlay_apply(struct of_overlay *ov)
170{
171	int i, err;
172
173	/* first we apply the overlays atomically */
174	for (i = 0; i < ov->count; i++) {
175		struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
176
177		err = of_overlay_apply_one(ov, ovinfo->target, ovinfo->overlay);
178		if (err != 0) {
179			pr_err("%s: overlay failed '%s'\n",
180				__func__, ovinfo->target->full_name);
181			return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182		}
183	}
184
185	return 0;
186}
187
188/*
189 * Find the target node using a number of different strategies
190 * in order of preference
191 *
192 * "target" property containing the phandle of the target
193 * "target-path" property containing the path of the target
194 */
195static struct device_node *find_target_node(struct device_node *info_node)
196{
 
197	const char *path;
198	u32 val;
199	int ret;
200
201	/* first try to go by using the target as a phandle */
202	ret = of_property_read_u32(info_node, "target", &val);
203	if (ret == 0)
204		return of_find_node_by_phandle(val);
 
 
 
 
 
205
206	/* now try to locate by path */
207	ret = of_property_read_string(info_node, "target-path", &path);
208	if (ret == 0)
209		return of_find_node_by_path(path);
 
 
 
 
 
210
211	pr_err("%s: Failed to find target for node %p (%s)\n", __func__,
212		info_node, info_node->name);
213
214	return NULL;
215}
216
217/**
218 * of_fill_overlay_info() - Fill an overlay info structure
219 * @ov		Overlay to fill
220 * @info_node:	Device node containing the overlay
221 * @ovinfo:	Pointer to the overlay info structure to fill
222 *
223 * Fills an overlay info structure with the overlay information
224 * from a device node. This device node must have a target property
225 * which contains a phandle of the overlay target node, and an
226 * __overlay__ child node which has the overlay contents.
227 * Both ovinfo->target & ovinfo->overlay have their references taken.
228 *
229 * Returns 0 on success, or a negative error value.
230 */
231static int of_fill_overlay_info(struct of_overlay *ov,
232		struct device_node *info_node, struct of_overlay_info *ovinfo)
233{
234	ovinfo->overlay = of_get_child_by_name(info_node, "__overlay__");
235	if (ovinfo->overlay == NULL)
236		goto err_fail;
237
238	ovinfo->target = find_target_node(info_node);
239	if (ovinfo->target == NULL)
240		goto err_fail;
241
242	return 0;
243
244err_fail:
245	of_node_put(ovinfo->target);
246	of_node_put(ovinfo->overlay);
247
248	memset(ovinfo, 0, sizeof(*ovinfo));
249	return -EINVAL;
250}
251
252/**
253 * of_build_overlay_info() - Build an overlay info array
254 * @ov		Overlay to build
255 * @tree:	Device node containing all the overlays
256 *
257 * Helper function that given a tree containing overlay information,
258 * allocates and builds an overlay info array containing it, ready
259 * for use using of_overlay_apply.
260 *
261 * Returns 0 on success with the @cntp @ovinfop pointers valid,
262 * while on error a negative error value is returned.
263 */
264static int of_build_overlay_info(struct of_overlay *ov,
265		struct device_node *tree)
266{
267	struct device_node *node;
268	struct of_overlay_info *ovinfo;
269	int cnt, err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
271	/* worst case; every child is a node */
272	cnt = 0;
273	for_each_child_of_node(tree, node)
 
 
 
 
 
 
 
 
 
 
 
274		cnt++;
 
 
275
276	ovinfo = kcalloc(cnt, sizeof(*ovinfo), GFP_KERNEL);
277	if (ovinfo == NULL)
278		return -ENOMEM;
 
 
279
280	cnt = 0;
281	for_each_child_of_node(tree, node) {
282		memset(&ovinfo[cnt], 0, sizeof(*ovinfo));
283		err = of_fill_overlay_info(ov, node, &ovinfo[cnt]);
284		if (err == 0)
285			cnt++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286	}
287
288	/* if nothing filled, return error */
289	if (cnt == 0) {
290		kfree(ovinfo);
291		return -ENODEV;
292	}
293
294	ov->count = cnt;
295	ov->ovinfo_tab = ovinfo;
 
296
297	return 0;
 
 
 
 
 
 
 
 
 
298}
299
300/**
301 * of_free_overlay_info() - Free an overlay info array
302 * @ov		Overlay to free the overlay info from
303 * @ovinfo_tab:	Array of overlay_info's to free
304 *
305 * Releases the memory of a previously allocated ovinfo array
306 * by of_build_overlay_info.
307 * Returns 0, or an error if the arguments are bogus.
308 */
309static int of_free_overlay_info(struct of_overlay *ov)
310{
311	struct of_overlay_info *ovinfo;
312	int i;
313
314	/* do it in reverse */
315	for (i = ov->count - 1; i >= 0; i--) {
316		ovinfo = &ov->ovinfo_tab[i];
317
318		of_node_put(ovinfo->target);
319		of_node_put(ovinfo->overlay);
320	}
321	kfree(ov->ovinfo_tab);
322
323	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
324}
325
326static LIST_HEAD(ov_list);
327static DEFINE_IDR(ov_idr);
 
 
 
 
 
 
 
 
 
 
 
328
329/**
330 * of_overlay_create() - Create and apply an overlay
331 * @tree:	Device node containing all the overlays
 
 
 
 
332 *
333 * Creates and applies an overlay while also keeping track
334 * of the overlay in a list. This list can be used to prevent
335 * illegal overlay removals.
336 *
337 * Returns the id of the created overlay, or a negative error number
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338 */
339int of_overlay_create(struct device_node *tree)
340{
341	struct of_overlay *ov;
342	int err, id;
343
344	/* allocate the overlay structure */
345	ov = kzalloc(sizeof(*ov), GFP_KERNEL);
346	if (ov == NULL)
347		return -ENOMEM;
348	ov->id = -1;
349
350	INIT_LIST_HEAD(&ov->node);
 
 
 
 
 
 
 
 
 
 
 
351
352	of_changeset_init(&ov->cset);
 
 
 
 
 
 
353
 
354	mutex_lock(&of_mutex);
355
356	id = idr_alloc(&ov_idr, ov, 0, 0, GFP_KERNEL);
357	if (id < 0) {
358		pr_err("%s: idr_alloc() failed for tree@%s\n",
359				__func__, tree->full_name);
360		err = id;
361		goto err_destroy_trans;
362	}
363	ov->id = id;
364
365	/* build the overlay info structures */
366	err = of_build_overlay_info(ov, tree);
367	if (err) {
368		pr_err("%s: of_build_overlay_info() failed for tree@%s\n",
369				__func__, tree->full_name);
370		goto err_free_idr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371	}
372
373	/* apply the overlay */
374	err = of_overlay_apply(ov);
375	if (err) {
376		pr_err("%s: of_overlay_apply() failed for tree@%s\n",
377				__func__, tree->full_name);
378		goto err_abort_trans;
379	}
380
381	/* apply the changeset */
382	err = __of_changeset_apply(&ov->cset);
383	if (err) {
384		pr_err("%s: __of_changeset_apply() failed for tree@%s\n",
385				__func__, tree->full_name);
386		goto err_revert_overlay;
387	}
388
389	/* add to the tail of the overlay list */
390	list_add_tail(&ov->node, &ov_list);
391
392	mutex_unlock(&of_mutex);
 
 
393
394	return id;
 
395
396err_revert_overlay:
397err_abort_trans:
398	of_free_overlay_info(ov);
399err_free_idr:
400	idr_remove(&ov_idr, ov->id);
401err_destroy_trans:
402	of_changeset_destroy(&ov->cset);
403	kfree(ov);
404	mutex_unlock(&of_mutex);
 
405
406	return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407}
408EXPORT_SYMBOL_GPL(of_overlay_create);
409
410/* check whether the given node, lies under the given tree */
411static int overlay_subtree_check(struct device_node *tree,
412		struct device_node *dn)
 
 
 
413{
414	struct device_node *child;
415
416	/* match? */
417	if (tree == dn)
418		return 1;
419
420	for_each_child_of_node(tree, child) {
421		if (overlay_subtree_check(child, dn)) {
422			of_node_put(child);
423			return 1;
424		}
425	}
426
427	return 0;
428}
429
430/* check whether this overlay is the topmost */
431static int overlay_is_topmost(struct of_overlay *ov, struct device_node *dn)
 
 
 
 
 
 
432{
433	struct of_overlay *ovt;
434	struct of_changeset_entry *ce;
435
436	list_for_each_entry_reverse(ovt, &ov_list, node) {
437		/* if we hit ourselves, we're done */
438		if (ovt == ov)
439			break;
440
441		/* check against each subtree affected by this overlay */
442		list_for_each_entry(ce, &ovt->cset.entries, node) {
443			if (overlay_subtree_check(ce->np, dn)) {
444				pr_err("%s: #%d clashes #%d @%s\n",
445					__func__, ov->id, ovt->id,
446					dn->full_name);
447				return 0;
 
 
 
 
 
448			}
449		}
450	}
451
452	/* overlay is topmost */
453	return 1;
454}
455
456/*
457 * We can safely remove the overlay only if it's the top-most one.
458 * Newly applied overlays are inserted at the tail of the overlay list,
459 * so a top most overlay is the one that is closest to the tail.
460 *
461 * The topmost check is done by exploiting this property. For each
462 * affected device node in the log list we check if this overlay is
463 * the one closest to the tail. If another overlay has affected this
464 * device node and is closest to the tail, then removal is not permited.
465 */
466static int overlay_removal_is_ok(struct of_overlay *ov)
467{
468	struct of_changeset_entry *ce;
469
470	list_for_each_entry(ce, &ov->cset.entries, node) {
471		if (!overlay_is_topmost(ov, ce->np)) {
472			pr_err("%s: overlay #%d is not topmost\n",
473					__func__, ov->id);
474			return 0;
475		}
476	}
477
478	return 1;
479}
480
481/**
482 * of_overlay_destroy() - Removes an overlay
483 * @id:	Overlay id number returned by a previous call to of_overlay_create
484 *
485 * Removes an overlay if it is permissible.
 
486 *
487 * Returns 0 on success, or a negative error number
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488 */
489int of_overlay_destroy(int id)
490{
491	struct of_overlay *ov;
492	int err;
493
494	mutex_lock(&of_mutex);
495
496	ov = idr_find(&ov_idr, id);
497	if (ov == NULL) {
498		err = -ENODEV;
499		pr_err("%s: Could not find overlay #%d\n",
500				__func__, id);
501		goto out;
502	}
503
504	/* check whether the overlay is safe to remove */
505	if (!overlay_removal_is_ok(ov)) {
506		err = -EBUSY;
507		pr_err("%s: removal check failed for overlay #%d\n",
508				__func__, id);
509		goto out;
 
510	}
511
 
 
 
 
512
513	list_del(&ov->node);
514	__of_changeset_revert(&ov->cset);
515	of_free_overlay_info(ov);
516	idr_remove(&ov_idr, id);
517	of_changeset_destroy(&ov->cset);
518	kfree(ov);
519
520	err = 0;
521
522out:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523	mutex_unlock(&of_mutex);
524
525	return err;
 
 
 
526}
527EXPORT_SYMBOL_GPL(of_overlay_destroy);
528
529/**
530 * of_overlay_destroy_all() - Removes all overlays from the system
531 *
532 * Removes all overlays from the system in the correct order.
533 *
534 * Returns 0 on success, or a negative error number
535 */
536int of_overlay_destroy_all(void)
537{
538	struct of_overlay *ov, *ovn;
539
540	mutex_lock(&of_mutex);
541
542	/* the tail of list is guaranteed to be safe to remove */
543	list_for_each_entry_safe_reverse(ov, ovn, &ov_list, node) {
544		list_del(&ov->node);
545		__of_changeset_revert(&ov->cset);
546		of_free_overlay_info(ov);
547		idr_remove(&ov_idr, ov->id);
548		kfree(ov);
549	}
550
551	mutex_unlock(&of_mutex);
552
553	return 0;
554}
555EXPORT_SYMBOL_GPL(of_overlay_destroy_all);
v4.17
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Functions for working with device tree overlays
   4 *
   5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
   6 * Copyright (C) 2012 Texas Instruments Inc.
 
 
 
 
   7 */
   8
   9#define pr_fmt(fmt)	"OF: overlay: " fmt
  10
  11#include <linux/kernel.h>
  12#include <linux/module.h>
  13#include <linux/of.h>
  14#include <linux/of_device.h>
  15#include <linux/of_fdt.h>
  16#include <linux/string.h>
  17#include <linux/ctype.h>
  18#include <linux/errno.h>
 
  19#include <linux/slab.h>
  20#include <linux/libfdt.h>
  21#include <linux/err.h>
  22#include <linux/idr.h>
  23
  24#include "of_private.h"
  25
  26/**
  27 * struct fragment - info about fragment nodes in overlay expanded device tree
  28 * @target:	target of the overlay operation
  29 * @overlay:	pointer to the __overlay__ node
 
 
 
  30 */
  31struct fragment {
  32	struct device_node *target;
  33	struct device_node *overlay;
  34};
  35
  36/**
  37 * struct overlay_changeset
  38 * @id:			changeset identifier
  39 * @ovcs_list:		list on which we are located
  40 * @fdt:		FDT that was unflattened to create @overlay_tree
  41 * @overlay_tree:	expanded device tree that contains the fragment nodes
  42 * @count:		count of fragment structures
  43 * @fragments:		fragment nodes in the overlay expanded device tree
  44 * @symbols_fragment:	last element of @fragments[] is the  __symbols__ node
  45 * @cset:		changeset to apply fragments to live device tree
  46 */
  47struct overlay_changeset {
  48	int id;
  49	struct list_head ovcs_list;
  50	const void *fdt;
  51	struct device_node *overlay_tree;
  52	int count;
  53	struct fragment *fragments;
  54	bool symbols_fragment;
  55	struct of_changeset cset;
  56};
  57
  58/* flags are sticky - once set, do not reset */
  59static int devicetree_state_flags;
  60#define DTSF_APPLY_FAIL		0x01
  61#define DTSF_REVERT_FAIL	0x02
  62
  63/*
  64 * If a changeset apply or revert encounters an error, an attempt will
  65 * be made to undo partial changes, but may fail.  If the undo fails
  66 * we do not know the state of the devicetree.
  67 */
  68static int devicetree_corrupt(void)
  69{
  70	return devicetree_state_flags &
  71		(DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
  72}
  73
  74static int build_changeset_next_level(struct overlay_changeset *ovcs,
  75		struct device_node *target_node,
  76		const struct device_node *overlay_node);
  77
  78/*
  79 * of_resolve_phandles() finds the largest phandle in the live tree.
  80 * of_overlay_apply() may add a larger phandle to the live tree.
  81 * Do not allow race between two overlays being applied simultaneously:
  82 *    mutex_lock(&of_overlay_phandle_mutex)
  83 *    of_resolve_phandles()
  84 *    of_overlay_apply()
  85 *    mutex_unlock(&of_overlay_phandle_mutex)
  86 */
  87static DEFINE_MUTEX(of_overlay_phandle_mutex);
  88
  89void of_overlay_mutex_lock(void)
  90{
  91	mutex_lock(&of_overlay_phandle_mutex);
  92}
  93
  94void of_overlay_mutex_unlock(void)
  95{
  96	mutex_unlock(&of_overlay_phandle_mutex);
  97}
  98
  99
 100static LIST_HEAD(ovcs_list);
 101static DEFINE_IDR(ovcs_idr);
 102
 103static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
 104
 105/**
 106 * of_overlay_notifier_register() - Register notifier for overlay operations
 107 * @nb:		Notifier block to register
 108 *
 109 * Register for notification on overlay operations on device tree nodes. The
 110 * reported actions definied by @of_reconfig_change. The notifier callback
 111 * furthermore receives a pointer to the affected device tree node.
 112 *
 113 * Note that a notifier callback is not supposed to store pointers to a device
 114 * tree node or its content beyond @OF_OVERLAY_POST_REMOVE corresponding to the
 115 * respective node it received.
 116 */
 117int of_overlay_notifier_register(struct notifier_block *nb)
 118{
 119	return blocking_notifier_chain_register(&overlay_notify_chain, nb);
 120}
 121EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
 122
 123/**
 124 * of_overlay_notifier_register() - Unregister notifier for overlay operations
 125 * @nb:		Notifier block to unregister
 126 */
 127int of_overlay_notifier_unregister(struct notifier_block *nb)
 128{
 129	return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
 130}
 131EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
 132
 133static char *of_overlay_action_name[] = {
 134	"pre-apply",
 135	"post-apply",
 136	"pre-remove",
 137	"post-remove",
 138};
 139
 140static int overlay_notify(struct overlay_changeset *ovcs,
 141		enum of_overlay_notify_action action)
 142{
 143	struct of_overlay_notify_data nd;
 144	int i, ret;
 145
 146	for (i = 0; i < ovcs->count; i++) {
 147		struct fragment *fragment = &ovcs->fragments[i];
 148
 149		nd.target = fragment->target;
 150		nd.overlay = fragment->overlay;
 151
 152		ret = blocking_notifier_call_chain(&overlay_notify_chain,
 153						   action, &nd);
 154		if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
 155			return 0;
 156		if (ret) {
 157			ret = notifier_to_errno(ret);
 158			pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
 159			       of_overlay_action_name[action], ret, nd.target);
 160			return ret;
 161		}
 162	}
 163
 164	return 0;
 165}
 166
 167/*
 168 * The values of properties in the "/__symbols__" node are paths in
 169 * the ovcs->overlay_tree.  When duplicating the properties, the paths
 170 * need to be adjusted to be the correct path for the live device tree.
 171 *
 172 * The paths refer to a node in the subtree of a fragment node's "__overlay__"
 173 * node, for example "/fragment@0/__overlay__/symbol_path_tail",
 174 * where symbol_path_tail can be a single node or it may be a multi-node path.
 175 *
 176 * The duplicated property value will be modified by replacing the
 177 * "/fragment_name/__overlay/" portion of the value  with the target
 178 * path from the fragment node.
 179 */
 180static struct property *dup_and_fixup_symbol_prop(
 181		struct overlay_changeset *ovcs, const struct property *prop)
 182{
 183	struct fragment *fragment;
 184	struct property *new_prop;
 185	struct device_node *fragment_node;
 186	struct device_node *overlay_node;
 187	const char *path;
 188	const char *path_tail;
 189	const char *target_path;
 190	int k;
 191	int overlay_name_len;
 192	int path_len;
 193	int path_tail_len;
 194	int target_path_len;
 195
 196	if (!prop->value)
 197		return NULL;
 198	if (strnlen(prop->value, prop->length) >= prop->length)
 199		return NULL;
 200	path = prop->value;
 201	path_len = strlen(path);
 202
 203	if (path_len < 1)
 204		return NULL;
 205	fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
 206	overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
 207	of_node_put(fragment_node);
 208	of_node_put(overlay_node);
 209
 210	for (k = 0; k < ovcs->count; k++) {
 211		fragment = &ovcs->fragments[k];
 212		if (fragment->overlay == overlay_node)
 213			break;
 214	}
 215	if (k >= ovcs->count)
 216		return NULL;
 217
 218	overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
 219
 220	if (overlay_name_len > path_len)
 221		return NULL;
 222	path_tail = path + overlay_name_len;
 223	path_tail_len = strlen(path_tail);
 224
 225	target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
 226	if (!target_path)
 227		return NULL;
 228	target_path_len = strlen(target_path);
 229
 230	new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
 231	if (!new_prop)
 232		goto err_free_target_path;
 233
 234	new_prop->name = kstrdup(prop->name, GFP_KERNEL);
 235	new_prop->length = target_path_len + path_tail_len + 1;
 236	new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
 237	if (!new_prop->name || !new_prop->value)
 238		goto err_free_new_prop;
 239
 240	strcpy(new_prop->value, target_path);
 241	strcpy(new_prop->value + target_path_len, path_tail);
 242
 243	of_property_set_flag(new_prop, OF_DYNAMIC);
 244
 245	return new_prop;
 246
 247err_free_new_prop:
 248	kfree(new_prop->name);
 249	kfree(new_prop->value);
 250	kfree(new_prop);
 251err_free_target_path:
 252	kfree(target_path);
 253
 254	return NULL;
 255}
 256
 257/**
 258 * add_changeset_property() - add @overlay_prop to overlay changeset
 259 * @ovcs:		overlay changeset
 260 * @target_node:	where to place @overlay_prop in live tree
 261 * @overlay_prop:	property to add or update, from overlay tree
 262 * @is_symbols_prop:	1 if @overlay_prop is from node "/__symbols__"
 263 *
 264 * If @overlay_prop does not already exist in @target_node, add changeset entry
 265 * to add @overlay_prop in @target_node, else add changeset entry to update
 266 * value of @overlay_prop.
 267 *
 268 * Some special properties are not updated (no error returned).
 269 *
 270 * Update of property in symbols node is not allowed.
 271 *
 272 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 273 * invalid @overlay.
 274 */
 275static int add_changeset_property(struct overlay_changeset *ovcs,
 276		struct device_node *target_node,
 277		struct property *overlay_prop,
 278		bool is_symbols_prop)
 279{
 280	struct property *new_prop = NULL, *prop;
 281	int ret = 0;
 282
 283	prop = of_find_property(target_node, overlay_prop->name, NULL);
 284
 285	if (!of_prop_cmp(overlay_prop->name, "name") ||
 286	    !of_prop_cmp(overlay_prop->name, "phandle") ||
 287	    !of_prop_cmp(overlay_prop->name, "linux,phandle"))
 288		return 0;
 289
 290	if (is_symbols_prop) {
 291		if (prop)
 292			return -EINVAL;
 293		new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
 294	} else {
 295		new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
 296	}
 297
 298	if (!new_prop)
 299		return -ENOMEM;
 
 300
 301	if (!prop)
 302		ret = of_changeset_add_property(&ovcs->cset, target_node,
 303						new_prop);
 304	else
 305		ret = of_changeset_update_property(&ovcs->cset, target_node,
 306						   new_prop);
 307
 308	if (ret) {
 309		kfree(new_prop->name);
 310		kfree(new_prop->value);
 311		kfree(new_prop);
 312	}
 313	return ret;
 314}
 315
 316/**
 317 * add_changeset_node() - add @node (and children) to overlay changeset
 318 * @ovcs:		overlay changeset
 319 * @target_node:	where to place @node in live tree
 320 * @node:		node from within overlay device tree fragment
 321 *
 322 * If @node does not already exist in @target_node, add changeset entry
 323 * to add @node in @target_node.
 324 *
 325 * If @node already exists in @target_node, and the existing node has
 326 * a phandle, the overlay node is not allowed to have a phandle.
 327 *
 328 * If @node has child nodes, add the children recursively via
 329 * build_changeset_next_level().
 330 *
 331 * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
 332 *       not contain the full path in node->full_name.  Thus an overlay
 333 *       created from an FDT also will not contain the full path in
 334 *       node->full_name.  However, a live devicetree created from Open
 335 *       Firmware may have the full path in node->full_name.
 336 *
 337 *       add_changeset_node() follows the FDT convention and does not include
 338 *       the full path in node->full_name.  Even though it expects the overlay
 339 *       to not contain the full path, it uses kbasename() to remove the
 340 *       full path should it exist.  It also uses kbasename() in comparisons
 341 *       to nodes in the live devicetree so that it can apply an overlay to
 342 *       a live devicetree created from Open Firmware.
 343 *
 344 * NOTE_2: Multiple mods of created nodes not supported.
 345 *       If more than one fragment contains a node that does not already exist
 346 *       in the live tree, then for each fragment of_changeset_attach_node()
 347 *       will add a changeset entry to add the node.  When the changeset is
 348 *       applied, __of_attach_node() will attach the node twice (once for
 349 *       each fragment).  At this point the device tree will be corrupted.
 350 *
 351 *       TODO: add integrity check to ensure that multiple fragments do not
 352 *             create the same node.
 353 *
 354 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 355 * invalid @overlay.
 356 */
 357static int add_changeset_node(struct overlay_changeset *ovcs,
 358		struct device_node *target_node, struct device_node *node)
 359{
 360	const char *node_kbasename;
 361	struct device_node *tchild;
 362	int ret = 0;
 363
 364	node_kbasename = kbasename(node->full_name);
 
 
 365
 366	for_each_child_of_node(target_node, tchild)
 367		if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
 368			break;
 369
 370	if (!tchild) {
 371		tchild = __of_node_dup(node, node_kbasename);
 
 
 
 372		if (!tchild)
 373			return -ENOMEM;
 374
 375		tchild->parent = target_node;
 
 376
 377		ret = of_changeset_attach_node(&ovcs->cset, tchild);
 378		if (ret)
 379			return ret;
 380
 381		return build_changeset_next_level(ovcs, tchild, node);
 
 
 382	}
 383
 384	if (node->phandle && tchild->phandle)
 385		ret = -EINVAL;
 386	else
 387		ret = build_changeset_next_level(ovcs, tchild, node);
 388	of_node_put(tchild);
 389
 390	return ret;
 391}
 392
 393/**
 394 * build_changeset_next_level() - add level of overlay changeset
 395 * @ovcs:		overlay changeset
 396 * @target_node:	where to place @overlay_node in live tree
 397 * @overlay_node:	node from within an overlay device tree fragment
 398 *
 399 * Add the properties (if any) and nodes (if any) from @overlay_node to the
 400 * @ovcs->cset changeset.  If an added node has child nodes, they will
 401 * be added recursively.
 402 *
 403 * Do not allow symbols node to have any children.
 404 *
 405 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 406 * invalid @overlay_node.
 407 */
 408static int build_changeset_next_level(struct overlay_changeset *ovcs,
 409		struct device_node *target_node,
 410		const struct device_node *overlay_node)
 411{
 412	struct device_node *child;
 413	struct property *prop;
 414	int ret;
 415
 416	for_each_property_of_node(overlay_node, prop) {
 417		ret = add_changeset_property(ovcs, target_node, prop, 0);
 418		if (ret) {
 419			pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
 420				 target_node, prop->name, ret);
 421			return ret;
 422		}
 423	}
 424
 425	for_each_child_of_node(overlay_node, child) {
 426		ret = add_changeset_node(ovcs, target_node, child);
 427		if (ret) {
 428			pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
 429				 target_node, child->name, ret);
 
 430			of_node_put(child);
 431			return ret;
 432		}
 433	}
 434
 435	return 0;
 436}
 437
 438/*
 439 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
 440 */
 441static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
 442		struct device_node *target_node,
 443		const struct device_node *overlay_symbols_node)
 444{
 445	struct property *prop;
 446	int ret;
 447
 448	for_each_property_of_node(overlay_symbols_node, prop) {
 449		ret = add_changeset_property(ovcs, target_node, prop, 1);
 450		if (ret) {
 451			pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
 452				 target_node, prop->name, ret);
 453			return ret;
 454		}
 455	}
 456
 457	return 0;
 458}
 459
 460/**
 461 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
 462 * @ovcs:	Overlay changeset
 463 *
 464 * Create changeset @ovcs->cset to contain the nodes and properties of the
 465 * overlay device tree fragments in @ovcs->fragments[].  If an error occurs,
 466 * any portions of the changeset that were successfully created will remain
 467 * in @ovcs->cset.
 468 *
 469 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 470 * invalid overlay in @ovcs->fragments[].
 471 */
 472static int build_changeset(struct overlay_changeset *ovcs)
 473{
 474	struct fragment *fragment;
 475	int fragments_count, i, ret;
 476
 477	/*
 478	 * if there is a symbols fragment in ovcs->fragments[i] it is
 479	 * the final element in the array
 480	 */
 481	if (ovcs->symbols_fragment)
 482		fragments_count = ovcs->count - 1;
 483	else
 484		fragments_count = ovcs->count;
 485
 486	for (i = 0; i < fragments_count; i++) {
 487		fragment = &ovcs->fragments[i];
 488
 489		ret = build_changeset_next_level(ovcs, fragment->target,
 490						 fragment->overlay);
 491		if (ret) {
 492			pr_debug("apply failed '%pOF'\n", fragment->target);
 493			return ret;
 494		}
 495	}
 496
 497	if (ovcs->symbols_fragment) {
 498		fragment = &ovcs->fragments[ovcs->count - 1];
 499		ret = build_changeset_symbols_node(ovcs, fragment->target,
 500						   fragment->overlay);
 501		if (ret) {
 502			pr_debug("apply failed '%pOF'\n", fragment->target);
 503			return ret;
 504		}
 505	}
 506
 507	return 0;
 508}
 509
 510/*
 511 * Find the target node using a number of different strategies
 512 * in order of preference:
 513 *
 514 * 1) "target" property containing the phandle of the target
 515 * 2) "target-path" property containing the path of the target
 516 */
 517static struct device_node *find_target_node(struct device_node *info_node)
 518{
 519	struct device_node *node;
 520	const char *path;
 521	u32 val;
 522	int ret;
 523
 
 524	ret = of_property_read_u32(info_node, "target", &val);
 525	if (!ret) {
 526		node = of_find_node_by_phandle(val);
 527		if (!node)
 528			pr_err("find target, node: %pOF, phandle 0x%x not found\n",
 529			       info_node, val);
 530		return node;
 531	}
 532
 
 533	ret = of_property_read_string(info_node, "target-path", &path);
 534	if (!ret) {
 535		node =  of_find_node_by_path(path);
 536		if (!node)
 537			pr_err("find target, node: %pOF, path '%s' not found\n",
 538			       info_node, path);
 539		return node;
 540	}
 541
 542	pr_err("find target, node: %pOF, no target property\n", info_node);
 
 543
 544	return NULL;
 545}
 546
 547/**
 548 * init_overlay_changeset() - initialize overlay changeset from overlay tree
 549 * @ovcs:	Overlay changeset to build
 550 * @fdt:	the FDT that was unflattened to create @tree
 551 * @tree:	Contains all the overlay fragments and overlay fixup nodes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 552 *
 553 * Initialize @ovcs.  Populate @ovcs->fragments with node information from
 554 * the top level of @tree.  The relevant top level nodes are the fragment
 555 * nodes and the __symbols__ node.  Any other top level node will be ignored.
 556 *
 557 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
 558 * detected in @tree, or -ENOSPC if idr_alloc() error.
 559 */
 560static int init_overlay_changeset(struct overlay_changeset *ovcs,
 561		const void *fdt, struct device_node *tree)
 562{
 563	struct device_node *node, *overlay_node;
 564	struct fragment *fragment;
 565	struct fragment *fragments;
 566	int cnt, id, ret;
 567
 568	/*
 569	 * Warn for some issues.  Can not return -EINVAL for these until
 570	 * of_unittest_apply_overlay() is fixed to pass these checks.
 571	 */
 572	if (!of_node_check_flag(tree, OF_DYNAMIC))
 573		pr_debug("%s() tree is not dynamic\n", __func__);
 574
 575	if (!of_node_check_flag(tree, OF_DETACHED))
 576		pr_debug("%s() tree is not detached\n", __func__);
 577
 578	if (!of_node_is_root(tree))
 579		pr_debug("%s() tree is not root\n", __func__);
 580
 581	ovcs->overlay_tree = tree;
 582	ovcs->fdt = fdt;
 583
 584	INIT_LIST_HEAD(&ovcs->ovcs_list);
 585
 586	of_changeset_init(&ovcs->cset);
 587
 588	id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
 589	if (id <= 0)
 590		return id;
 591
 
 592	cnt = 0;
 593
 594	/* fragment nodes */
 595	for_each_child_of_node(tree, node) {
 596		overlay_node = of_get_child_by_name(node, "__overlay__");
 597		if (overlay_node) {
 598			cnt++;
 599			of_node_put(overlay_node);
 600		}
 601	}
 602
 603	node = of_get_child_by_name(tree, "__symbols__");
 604	if (node) {
 605		cnt++;
 606		of_node_put(node);
 607	}
 608
 609	fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
 610	if (!fragments) {
 611		ret = -ENOMEM;
 612		goto err_free_idr;
 613	}
 614
 615	cnt = 0;
 616	for_each_child_of_node(tree, node) {
 617		overlay_node = of_get_child_by_name(node, "__overlay__");
 618		if (!overlay_node)
 619			continue;
 620
 621		fragment = &fragments[cnt];
 622		fragment->overlay = overlay_node;
 623		fragment->target = find_target_node(node);
 624		if (!fragment->target) {
 625			of_node_put(fragment->overlay);
 626			ret = -EINVAL;
 627			goto err_free_fragments;
 628		}
 629
 630		cnt++;
 631	}
 632
 633	/*
 634	 * if there is a symbols fragment in ovcs->fragments[i] it is
 635	 * the final element in the array
 636	 */
 637	node = of_get_child_by_name(tree, "__symbols__");
 638	if (node) {
 639		ovcs->symbols_fragment = 1;
 640		fragment = &fragments[cnt];
 641		fragment->overlay = node;
 642		fragment->target = of_find_node_by_path("/__symbols__");
 643
 644		if (!fragment->target) {
 645			pr_err("symbols in overlay, but not in live tree\n");
 646			ret = -EINVAL;
 647			goto err_free_fragments;
 648		}
 649
 650		cnt++;
 651	}
 652
 653	if (!cnt) {
 654		pr_err("no fragments or symbols in overlay\n");
 655		ret = -EINVAL;
 656		goto err_free_fragments;
 657	}
 658
 659	ovcs->id = id;
 660	ovcs->count = cnt;
 661	ovcs->fragments = fragments;
 662
 663	return 0;
 664
 665err_free_fragments:
 666	kfree(fragments);
 667err_free_idr:
 668	idr_remove(&ovcs_idr, id);
 669
 670	pr_err("%s() failed, ret = %d\n", __func__, ret);
 671
 672	return ret;
 673}
 674
 675static void free_overlay_changeset(struct overlay_changeset *ovcs)
 
 
 
 
 
 
 
 
 
 676{
 
 677	int i;
 678
 679	if (ovcs->cset.entries.next)
 680		of_changeset_destroy(&ovcs->cset);
 
 681
 682	if (ovcs->id)
 683		idr_remove(&ovcs_idr, ovcs->id);
 
 
 684
 685	for (i = 0; i < ovcs->count; i++) {
 686		of_node_put(ovcs->fragments[i].target);
 687		of_node_put(ovcs->fragments[i].overlay);
 688	}
 689	kfree(ovcs->fragments);
 690	/*
 691	 * There should be no live pointers into ovcs->overlay_tree and
 692	 * ovcs->fdt due to the policy that overlay notifiers are not allowed
 693	 * to retain pointers into the overlay devicetree.
 694	 */
 695	kfree(ovcs->overlay_tree);
 696	kfree(ovcs->fdt);
 697	kfree(ovcs);
 698}
 699
 700/*
 701 * internal documentation
 702 *
 703 * of_overlay_apply() - Create and apply an overlay changeset
 704 * @fdt:	the FDT that was unflattened to create @tree
 705 * @tree:	Expanded overlay device tree
 706 * @ovcs_id:	Pointer to overlay changeset id
 707 *
 708 * Creates and applies an overlay changeset.
 709 *
 710 * If an error occurs in a pre-apply notifier, then no changes are made
 711 * to the device tree.
 712 *
 713
 714 * A non-zero return value will not have created the changeset if error is from:
 715 *   - parameter checks
 716 *   - building the changeset
 717 *   - overlay changeset pre-apply notifier
 718 *
 719 * If an error is returned by an overlay changeset pre-apply notifier
 720 * then no further overlay changeset pre-apply notifier will be called.
 721 *
 722 * A non-zero return value will have created the changeset if error is from:
 723 *   - overlay changeset entry notifier
 724 *   - overlay changeset post-apply notifier
 725 *
 726 * If an error is returned by an overlay changeset post-apply notifier
 727 * then no further overlay changeset post-apply notifier will be called.
 728 *
 729 * If more than one notifier returns an error, then the last notifier
 730 * error to occur is returned.
 731 *
 732 * If an error occurred while applying the overlay changeset, then an
 733 * attempt is made to revert any changes that were made to the
 734 * device tree.  If there were any errors during the revert attempt
 735 * then the state of the device tree can not be determined, and any
 736 * following attempt to apply or remove an overlay changeset will be
 737 * refused.
 738 *
 739 * Returns 0 on success, or a negative error number.  Overlay changeset
 740 * id is returned to *ovcs_id.
 741 */
 
 
 
 
 742
 743static int of_overlay_apply(const void *fdt, struct device_node *tree,
 744		int *ovcs_id)
 745{
 746	struct overlay_changeset *ovcs;
 747	int ret = 0, ret_revert, ret_tmp;
 748
 749	/*
 750	 * As of this point, fdt and tree belong to the overlay changeset.
 751	 * overlay changeset code is responsible for freeing them.
 752	 */
 753
 754	if (devicetree_corrupt()) {
 755		pr_err("devicetree state suspect, refuse to apply overlay\n");
 756		kfree(fdt);
 757		kfree(tree);
 758		ret = -EBUSY;
 759		goto out;
 760	}
 761
 762	ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
 763	if (!ovcs) {
 764		kfree(fdt);
 765		kfree(tree);
 766		ret = -ENOMEM;
 767		goto out;
 768	}
 769
 770	of_overlay_mutex_lock();
 771	mutex_lock(&of_mutex);
 772
 773	ret = of_resolve_phandles(tree);
 774	if (ret)
 775		goto err_free_tree;
 776
 777	ret = init_overlay_changeset(ovcs, fdt, tree);
 778	if (ret)
 779		goto err_free_tree;
 780
 781	/*
 782	 * after overlay_notify(), ovcs->overlay_tree related pointers may have
 783	 * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
 784	 * and can not free fdt, aka ovcs->fdt
 785	 */
 786	ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
 787	if (ret) {
 788		pr_err("overlay changeset pre-apply notify error %d\n", ret);
 789		goto err_free_overlay_changeset;
 790	}
 791
 792	ret = build_changeset(ovcs);
 793	if (ret)
 794		goto err_free_overlay_changeset;
 795
 796	ret_revert = 0;
 797	ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
 798	if (ret) {
 799		if (ret_revert) {
 800			pr_debug("overlay changeset revert error %d\n",
 801				 ret_revert);
 802			devicetree_state_flags |= DTSF_APPLY_FAIL;
 803		}
 804		goto err_free_overlay_changeset;
 805	}
 806
 807	ret = __of_changeset_apply_notify(&ovcs->cset);
 808	if (ret)
 809		pr_err("overlay changeset entry notify error %d\n", ret);
 810	/* notify failure is not fatal, continue */
 811
 812	list_add_tail(&ovcs->ovcs_list, &ovcs_list);
 813	*ovcs_id = ovcs->id;
 814
 815	ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
 816	if (ret_tmp) {
 817		pr_err("overlay changeset post-apply notify error %d\n",
 818		       ret_tmp);
 819		if (!ret)
 820			ret = ret_tmp;
 821	}
 822
 823	goto out_unlock;
 
 824
 825err_free_tree:
 826	kfree(fdt);
 827	kfree(tree);
 828
 829err_free_overlay_changeset:
 830	free_overlay_changeset(ovcs);
 831
 832out_unlock:
 
 
 
 
 
 
 
 833	mutex_unlock(&of_mutex);
 834	of_overlay_mutex_unlock();
 835
 836out:
 837	pr_debug("%s() err=%d\n", __func__, ret);
 838
 839	return ret;
 840}
 841
 842int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
 843			 int *ovcs_id)
 844{
 845	const void *new_fdt;
 846	int ret;
 847	u32 size;
 848	struct device_node *overlay_root;
 849
 850	*ovcs_id = 0;
 851	ret = 0;
 852
 853	if (overlay_fdt_size < sizeof(struct fdt_header) ||
 854	    fdt_check_header(overlay_fdt)) {
 855		pr_err("Invalid overlay_fdt header\n");
 856		return -EINVAL;
 857	}
 858
 859	size = fdt_totalsize(overlay_fdt);
 860	if (overlay_fdt_size < size)
 861		return -EINVAL;
 862
 863	/*
 864	 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
 865	 * will create pointers to the passed in FDT in the unflattened tree.
 866	 */
 867	new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
 868	if (!new_fdt)
 869		return -ENOMEM;
 870
 871	of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
 872	if (!overlay_root) {
 873		pr_err("unable to unflatten overlay_fdt\n");
 874		ret = -EINVAL;
 875		goto out_free_new_fdt;
 876	}
 877
 878	ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
 879	if (ret < 0) {
 880		/*
 881		 * new_fdt and overlay_root now belong to the overlay
 882		 * changeset.
 883		 * overlay changeset code is responsible for freeing them.
 884		 */
 885		goto out;
 886	}
 887
 888	return 0;
 889
 890
 891out_free_new_fdt:
 892	kfree(new_fdt);
 893
 894out:
 895	return ret;
 896}
 897EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
 898
 899/*
 900 * Find @np in @tree.
 901 *
 902 * Returns 1 if @np is @tree or is contained in @tree, else 0
 903 */
 904static int find_node(struct device_node *tree, struct device_node *np)
 905{
 906	struct device_node *child;
 907
 908	if (tree == np)
 
 909		return 1;
 910
 911	for_each_child_of_node(tree, child) {
 912		if (find_node(child, np)) {
 913			of_node_put(child);
 914			return 1;
 915		}
 916	}
 917
 918	return 0;
 919}
 920
 921/*
 922 * Is @remove_ce_node a child of, a parent of, or the same as any
 923 * node in an overlay changeset more topmost than @remove_ovcs?
 924 *
 925 * Returns 1 if found, else 0
 926 */
 927static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
 928		struct device_node *remove_ce_node)
 929{
 930	struct overlay_changeset *ovcs;
 931	struct of_changeset_entry *ce;
 932
 933	list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
 934		if (ovcs == remove_ovcs)
 
 935			break;
 936
 937		list_for_each_entry(ce, &ovcs->cset.entries, node) {
 938			if (find_node(ce->np, remove_ce_node)) {
 939				pr_err("%s: #%d overlaps with #%d @%pOF\n",
 940					__func__, remove_ovcs->id, ovcs->id,
 941					remove_ce_node);
 942				return 1;
 943			}
 944			if (find_node(remove_ce_node, ce->np)) {
 945				pr_err("%s: #%d overlaps with #%d @%pOF\n",
 946					__func__, remove_ovcs->id, ovcs->id,
 947					remove_ce_node);
 948				return 1;
 949			}
 950		}
 951	}
 952
 953	return 0;
 
 954}
 955
 956/*
 957 * We can safely remove the overlay only if it's the top-most one.
 958 * Newly applied overlays are inserted at the tail of the overlay list,
 959 * so a top most overlay is the one that is closest to the tail.
 960 *
 961 * The topmost check is done by exploiting this property. For each
 962 * affected device node in the log list we check if this overlay is
 963 * the one closest to the tail. If another overlay has affected this
 964 * device node and is closest to the tail, then removal is not permited.
 965 */
 966static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
 967{
 968	struct of_changeset_entry *remove_ce;
 969
 970	list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
 971		if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
 972			pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
 
 973			return 0;
 974		}
 975	}
 976
 977	return 1;
 978}
 979
 980/**
 981 * of_overlay_remove() - Revert and free an overlay changeset
 982 * @ovcs_id:	Pointer to overlay changeset id
 983 *
 984 * Removes an overlay if it is permissible.  @ovcs_id was previously returned
 985 * by of_overlay_fdt_apply().
 986 *
 987 * If an error occurred while attempting to revert the overlay changeset,
 988 * then an attempt is made to re-apply any changeset entry that was
 989 * reverted.  If an error occurs on re-apply then the state of the device
 990 * tree can not be determined, and any following attempt to apply or remove
 991 * an overlay changeset will be refused.
 992 *
 993 * A non-zero return value will not revert the changeset if error is from:
 994 *   - parameter checks
 995 *   - overlay changeset pre-remove notifier
 996 *   - overlay changeset entry revert
 997 *
 998 * If an error is returned by an overlay changeset pre-remove notifier
 999 * then no further overlay changeset pre-remove notifier will be called.
1000 *
1001 * If more than one notifier returns an error, then the last notifier
1002 * error to occur is returned.
1003 *
1004 * A non-zero return value will revert the changeset if error is from:
1005 *   - overlay changeset entry notifier
1006 *   - overlay changeset post-remove notifier
1007 *
1008 * If an error is returned by an overlay changeset post-remove notifier
1009 * then no further overlay changeset post-remove notifier will be called.
1010 *
1011 * Returns 0 on success, or a negative error number.  *ovcs_id is set to
1012 * zero after reverting the changeset, even if a subsequent error occurs.
1013 */
1014int of_overlay_remove(int *ovcs_id)
1015{
1016	struct overlay_changeset *ovcs;
1017	int ret, ret_apply, ret_tmp;
1018
1019	ret = 0;
1020
1021	if (devicetree_corrupt()) {
1022		pr_err("suspect devicetree state, refuse to remove overlay\n");
1023		ret = -EBUSY;
 
 
1024		goto out;
1025	}
1026
1027	mutex_lock(&of_mutex);
1028
1029	ovcs = idr_find(&ovcs_idr, *ovcs_id);
1030	if (!ovcs) {
1031		ret = -ENODEV;
1032		pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
1033		goto out_unlock;
1034	}
1035
1036	if (!overlay_removal_is_ok(ovcs)) {
1037		ret = -EBUSY;
1038		goto out_unlock;
1039	}
1040
1041	ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
1042	if (ret) {
1043		pr_err("overlay changeset pre-remove notify error %d\n", ret);
1044		goto out_unlock;
1045	}
 
1046
1047	list_del(&ovcs->ovcs_list);
1048
1049	ret_apply = 0;
1050	ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
1051	if (ret) {
1052		if (ret_apply)
1053			devicetree_state_flags |= DTSF_REVERT_FAIL;
1054		goto out_unlock;
1055	}
1056
1057	ret = __of_changeset_revert_notify(&ovcs->cset);
1058	if (ret)
1059		pr_err("overlay changeset entry notify error %d\n", ret);
1060	/* notify failure is not fatal, continue */
1061
1062	*ovcs_id = 0;
1063
1064	ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
1065	if (ret_tmp) {
1066		pr_err("overlay changeset post-remove notify error %d\n",
1067		       ret_tmp);
1068		if (!ret)
1069			ret = ret_tmp;
1070	}
1071
1072	free_overlay_changeset(ovcs);
1073
1074out_unlock:
1075	mutex_unlock(&of_mutex);
1076
1077out:
1078	pr_debug("%s() err=%d\n", __func__, ret);
1079
1080	return ret;
1081}
1082EXPORT_SYMBOL_GPL(of_overlay_remove);
1083
1084/**
1085 * of_overlay_remove_all() - Reverts and frees all overlay changesets
1086 *
1087 * Removes all overlays from the system in the correct order.
1088 *
1089 * Returns 0 on success, or a negative error number
1090 */
1091int of_overlay_remove_all(void)
1092{
1093	struct overlay_changeset *ovcs, *ovcs_n;
1094	int ret;
 
1095
1096	/* the tail of list is guaranteed to be safe to remove */
1097	list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
1098		ret = of_overlay_remove(&ovcs->id);
1099		if (ret)
1100			return ret;
 
 
1101	}
1102
 
 
1103	return 0;
1104}
1105EXPORT_SYMBOL_GPL(of_overlay_remove_all);