1 // SPDX-License-Identifier: GPL-2.0
3 * Functions for working with device tree overlays
5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
6 * Copyright (C) 2012 Texas Instruments Inc.
9 #define pr_fmt(fmt) "OF: overlay: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.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>
24 #include "of_private.h"
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
32 struct device_node *target;
33 struct device_node *overlay;
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
47 struct overlay_changeset {
49 struct list_head ovcs_list;
51 struct device_node *overlay_tree;
53 struct fragment *fragments;
54 bool symbols_fragment;
55 struct of_changeset cset;
58 /* flags are sticky - once set, do not reset */
59 static int devicetree_state_flags;
60 #define DTSF_APPLY_FAIL 0x01
61 #define DTSF_REVERT_FAIL 0x02
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.
68 static int devicetree_corrupt(void)
70 return devicetree_state_flags &
71 (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
74 static int build_changeset_next_level(struct overlay_changeset *ovcs,
75 struct device_node *target_node,
76 const struct device_node *overlay_node);
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()
85 * mutex_unlock(&of_overlay_phandle_mutex)
87 static DEFINE_MUTEX(of_overlay_phandle_mutex);
89 void of_overlay_mutex_lock(void)
91 mutex_lock(&of_overlay_phandle_mutex);
94 void of_overlay_mutex_unlock(void)
96 mutex_unlock(&of_overlay_phandle_mutex);
100 static LIST_HEAD(ovcs_list);
101 static DEFINE_IDR(ovcs_idr);
103 static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
105 int of_overlay_notifier_register(struct notifier_block *nb)
107 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
109 EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
111 int of_overlay_notifier_unregister(struct notifier_block *nb)
113 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
115 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
117 static char *of_overlay_action_name[] = {
124 static int overlay_notify(struct overlay_changeset *ovcs,
125 enum of_overlay_notify_action action)
127 struct of_overlay_notify_data nd;
130 for (i = 0; i < ovcs->count; i++) {
131 struct fragment *fragment = &ovcs->fragments[i];
133 nd.target = fragment->target;
134 nd.overlay = fragment->overlay;
136 ret = blocking_notifier_call_chain(&overlay_notify_chain,
138 if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
141 ret = notifier_to_errno(ret);
142 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
143 of_overlay_action_name[action], ret, nd.target);
152 * The values of properties in the "/__symbols__" node are paths in
153 * the ovcs->overlay_tree. When duplicating the properties, the paths
154 * need to be adjusted to be the correct path for the live device tree.
156 * The paths refer to a node in the subtree of a fragment node's "__overlay__"
157 * node, for example "/fragment@0/__overlay__/symbol_path_tail",
158 * where symbol_path_tail can be a single node or it may be a multi-node path.
160 * The duplicated property value will be modified by replacing the
161 * "/fragment_name/__overlay/" portion of the value with the target
162 * path from the fragment node.
164 static struct property *dup_and_fixup_symbol_prop(
165 struct overlay_changeset *ovcs, const struct property *prop)
167 struct fragment *fragment;
168 struct property *new_prop;
169 struct device_node *fragment_node;
170 struct device_node *overlay_node;
172 const char *path_tail;
173 const char *target_path;
175 int overlay_name_len;
182 if (strnlen(prop->value, prop->length) >= prop->length)
185 path_len = strlen(path);
189 fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
190 overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
191 of_node_put(fragment_node);
192 of_node_put(overlay_node);
194 for (k = 0; k < ovcs->count; k++) {
195 fragment = &ovcs->fragments[k];
196 if (fragment->overlay == overlay_node)
199 if (k >= ovcs->count)
202 overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
204 if (overlay_name_len > path_len)
206 path_tail = path + overlay_name_len;
207 path_tail_len = strlen(path_tail);
209 target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
212 target_path_len = strlen(target_path);
214 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
216 goto err_free_target_path;
218 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
219 new_prop->length = target_path_len + path_tail_len + 1;
220 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
221 if (!new_prop->name || !new_prop->value)
222 goto err_free_new_prop;
224 strcpy(new_prop->value, target_path);
225 strcpy(new_prop->value + target_path_len, path_tail);
227 of_property_set_flag(new_prop, OF_DYNAMIC);
232 kfree(new_prop->name);
233 kfree(new_prop->value);
235 err_free_target_path:
242 * add_changeset_property() - add @overlay_prop to overlay changeset
243 * @ovcs: overlay changeset
244 * @target_node: where to place @overlay_prop in live tree
245 * @overlay_prop: property to add or update, from overlay tree
246 * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
248 * If @overlay_prop does not already exist in @target_node, add changeset entry
249 * to add @overlay_prop in @target_node, else add changeset entry to update
250 * value of @overlay_prop.
252 * Some special properties are not updated (no error returned).
254 * Update of property in symbols node is not allowed.
256 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
259 static int add_changeset_property(struct overlay_changeset *ovcs,
260 struct device_node *target_node,
261 struct property *overlay_prop,
262 bool is_symbols_prop)
264 struct property *new_prop = NULL, *prop;
267 prop = of_find_property(target_node, overlay_prop->name, NULL);
269 if (!of_prop_cmp(overlay_prop->name, "name") ||
270 !of_prop_cmp(overlay_prop->name, "phandle") ||
271 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
274 if (is_symbols_prop) {
277 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
279 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
286 ret = of_changeset_add_property(&ovcs->cset, target_node,
289 ret = of_changeset_update_property(&ovcs->cset, target_node,
293 kfree(new_prop->name);
294 kfree(new_prop->value);
301 * add_changeset_node() - add @node (and children) to overlay changeset
302 * @ovcs: overlay changeset
303 * @target_node: where to place @node in live tree
304 * @node: node from within overlay device tree fragment
306 * If @node does not already exist in @target_node, add changeset entry
307 * to add @node in @target_node.
309 * If @node already exists in @target_node, and the existing node has
310 * a phandle, the overlay node is not allowed to have a phandle.
312 * If @node has child nodes, add the children recursively via
313 * build_changeset_next_level().
315 * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
316 * not contain the full path in node->full_name. Thus an overlay
317 * created from an FDT also will not contain the full path in
318 * node->full_name. However, a live devicetree created from Open
319 * Firmware may have the full path in node->full_name.
321 * add_changeset_node() follows the FDT convention and does not include
322 * the full path in node->full_name. Even though it expects the overlay
323 * to not contain the full path, it uses kbasename() to remove the
324 * full path should it exist. It also uses kbasename() in comparisons
325 * to nodes in the live devicetree so that it can apply an overlay to
326 * a live devicetree created from Open Firmware.
328 * NOTE_2: Multiple mods of created nodes not supported.
329 * If more than one fragment contains a node that does not already exist
330 * in the live tree, then for each fragment of_changeset_attach_node()
331 * will add a changeset entry to add the node. When the changeset is
332 * applied, __of_attach_node() will attach the node twice (once for
333 * each fragment). At this point the device tree will be corrupted.
335 * TODO: add integrity check to ensure that multiple fragments do not
336 * create the same node.
338 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
341 static int add_changeset_node(struct overlay_changeset *ovcs,
342 struct device_node *target_node, struct device_node *node)
344 const char *node_kbasename;
345 struct device_node *tchild;
348 node_kbasename = kbasename(node->full_name);
350 for_each_child_of_node(target_node, tchild)
351 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
355 tchild = __of_node_dup(node, node_kbasename);
359 tchild->parent = target_node;
361 ret = of_changeset_attach_node(&ovcs->cset, tchild);
365 return build_changeset_next_level(ovcs, tchild, node);
368 if (node->phandle && tchild->phandle)
371 ret = build_changeset_next_level(ovcs, tchild, node);
378 * build_changeset_next_level() - add level of overlay changeset
379 * @ovcs: overlay changeset
380 * @target_node: where to place @overlay_node in live tree
381 * @overlay_node: node from within an overlay device tree fragment
383 * Add the properties (if any) and nodes (if any) from @overlay_node to the
384 * @ovcs->cset changeset. If an added node has child nodes, they will
385 * be added recursively.
387 * Do not allow symbols node to have any children.
389 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
390 * invalid @overlay_node.
392 static int build_changeset_next_level(struct overlay_changeset *ovcs,
393 struct device_node *target_node,
394 const struct device_node *overlay_node)
396 struct device_node *child;
397 struct property *prop;
400 for_each_property_of_node(overlay_node, prop) {
401 ret = add_changeset_property(ovcs, target_node, prop, 0);
403 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
404 target_node, prop->name, ret);
409 for_each_child_of_node(overlay_node, child) {
410 ret = add_changeset_node(ovcs, target_node, child);
412 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
413 target_node, child->name, ret);
423 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
425 static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
426 struct device_node *target_node,
427 const struct device_node *overlay_symbols_node)
429 struct property *prop;
432 for_each_property_of_node(overlay_symbols_node, prop) {
433 ret = add_changeset_property(ovcs, target_node, prop, 1);
435 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
436 target_node, prop->name, ret);
445 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
446 * @ovcs: Overlay changeset
448 * Create changeset @ovcs->cset to contain the nodes and properties of the
449 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
450 * any portions of the changeset that were successfully created will remain
453 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
454 * invalid overlay in @ovcs->fragments[].
456 static int build_changeset(struct overlay_changeset *ovcs)
458 struct fragment *fragment;
459 int fragments_count, i, ret;
462 * if there is a symbols fragment in ovcs->fragments[i] it is
463 * the final element in the array
465 if (ovcs->symbols_fragment)
466 fragments_count = ovcs->count - 1;
468 fragments_count = ovcs->count;
470 for (i = 0; i < fragments_count; i++) {
471 fragment = &ovcs->fragments[i];
473 ret = build_changeset_next_level(ovcs, fragment->target,
476 pr_debug("apply failed '%pOF'\n", fragment->target);
481 if (ovcs->symbols_fragment) {
482 fragment = &ovcs->fragments[ovcs->count - 1];
483 ret = build_changeset_symbols_node(ovcs, fragment->target,
486 pr_debug("apply failed '%pOF'\n", fragment->target);
495 * Find the target node using a number of different strategies
496 * in order of preference:
498 * 1) "target" property containing the phandle of the target
499 * 2) "target-path" property containing the path of the target
501 static struct device_node *find_target_node(struct device_node *info_node)
503 struct device_node *node;
508 ret = of_property_read_u32(info_node, "target", &val);
510 node = of_find_node_by_phandle(val);
512 pr_err("find target, node: %pOF, phandle 0x%x not found\n",
517 ret = of_property_read_string(info_node, "target-path", &path);
519 node = of_find_node_by_path(path);
521 pr_err("find target, node: %pOF, path '%s' not found\n",
526 pr_err("find target, node: %pOF, no target property\n", info_node);
532 * init_overlay_changeset() - initialize overlay changeset from overlay tree
533 * @ovcs: Overlay changeset to build
534 * @fdt: the FDT that was unflattened to create @tree
535 * @tree: Contains all the overlay fragments and overlay fixup nodes
537 * Initialize @ovcs. Populate @ovcs->fragments with node information from
538 * the top level of @tree. The relevant top level nodes are the fragment
539 * nodes and the __symbols__ node. Any other top level node will be ignored.
541 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
542 * detected in @tree, or -ENOSPC if idr_alloc() error.
544 static int init_overlay_changeset(struct overlay_changeset *ovcs,
545 const void *fdt, struct device_node *tree)
547 struct device_node *node, *overlay_node;
548 struct fragment *fragment;
549 struct fragment *fragments;
553 * Warn for some issues. Can not return -EINVAL for these until
554 * of_unittest_apply_overlay() is fixed to pass these checks.
556 if (!of_node_check_flag(tree, OF_DYNAMIC))
557 pr_debug("%s() tree is not dynamic\n", __func__);
559 if (!of_node_check_flag(tree, OF_DETACHED))
560 pr_debug("%s() tree is not detached\n", __func__);
562 if (!of_node_is_root(tree))
563 pr_debug("%s() tree is not root\n", __func__);
565 ovcs->overlay_tree = tree;
568 INIT_LIST_HEAD(&ovcs->ovcs_list);
570 of_changeset_init(&ovcs->cset);
572 id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
579 for_each_child_of_node(tree, node) {
580 overlay_node = of_get_child_by_name(node, "__overlay__");
583 of_node_put(overlay_node);
587 node = of_get_child_by_name(tree, "__symbols__");
593 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
600 for_each_child_of_node(tree, node) {
601 overlay_node = of_get_child_by_name(node, "__overlay__");
605 fragment = &fragments[cnt];
606 fragment->overlay = overlay_node;
607 fragment->target = find_target_node(node);
608 if (!fragment->target) {
609 of_node_put(fragment->overlay);
611 goto err_free_fragments;
618 * if there is a symbols fragment in ovcs->fragments[i] it is
619 * the final element in the array
621 node = of_get_child_by_name(tree, "__symbols__");
623 ovcs->symbols_fragment = 1;
624 fragment = &fragments[cnt];
625 fragment->overlay = node;
626 fragment->target = of_find_node_by_path("/__symbols__");
628 if (!fragment->target) {
629 pr_err("symbols in overlay, but not in live tree\n");
631 goto err_free_fragments;
638 pr_err("no fragments or symbols in overlay\n");
640 goto err_free_fragments;
645 ovcs->fragments = fragments;
652 idr_remove(&ovcs_idr, id);
654 pr_err("%s() failed, ret = %d\n", __func__, ret);
659 static void free_overlay_changeset(struct overlay_changeset *ovcs)
663 if (ovcs->cset.entries.next)
664 of_changeset_destroy(&ovcs->cset);
667 idr_remove(&ovcs_idr, ovcs->id);
669 for (i = 0; i < ovcs->count; i++) {
670 of_node_put(ovcs->fragments[i].target);
671 of_node_put(ovcs->fragments[i].overlay);
673 kfree(ovcs->fragments);
678 * would like to: kfree(ovcs->overlay_tree);
679 * but can not since drivers may have pointers into this data
681 * would like to: kfree(ovcs->fdt);
682 * but can not since drivers may have pointers into this data
689 * internal documentation
691 * of_overlay_apply() - Create and apply an overlay changeset
692 * @fdt: the FDT that was unflattened to create @tree
693 * @tree: Expanded overlay device tree
694 * @ovcs_id: Pointer to overlay changeset id
696 * Creates and applies an overlay changeset.
698 * If an error occurs in a pre-apply notifier, then no changes are made
699 * to the device tree.
702 * A non-zero return value will not have created the changeset if error is from:
704 * - building the changeset
705 * - overlay changeset pre-apply notifier
707 * If an error is returned by an overlay changeset pre-apply notifier
708 * then no further overlay changeset pre-apply notifier will be called.
710 * A non-zero return value will have created the changeset if error is from:
711 * - overlay changeset entry notifier
712 * - overlay changeset post-apply notifier
714 * If an error is returned by an overlay changeset post-apply notifier
715 * then no further overlay changeset post-apply notifier will be called.
717 * If more than one notifier returns an error, then the last notifier
718 * error to occur is returned.
720 * If an error occurred while applying the overlay changeset, then an
721 * attempt is made to revert any changes that were made to the
722 * device tree. If there were any errors during the revert attempt
723 * then the state of the device tree can not be determined, and any
724 * following attempt to apply or remove an overlay changeset will be
727 * Returns 0 on success, or a negative error number. Overlay changeset
728 * id is returned to *ovcs_id.
731 static int of_overlay_apply(const void *fdt, struct device_node *tree,
734 struct overlay_changeset *ovcs;
735 int ret = 0, ret_revert, ret_tmp;
738 * As of this point, fdt and tree belong to the overlay changeset.
739 * overlay changeset code is responsible for freeing them.
742 if (devicetree_corrupt()) {
743 pr_err("devicetree state suspect, refuse to apply overlay\n");
750 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
758 of_overlay_mutex_lock();
759 mutex_lock(&of_mutex);
761 ret = of_resolve_phandles(tree);
765 ret = init_overlay_changeset(ovcs, fdt, tree);
770 * after overlay_notify(), ovcs->overlay_tree related pointers may have
771 * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
772 * and can not free fdt, aka ovcs->fdt
774 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
776 pr_err("overlay changeset pre-apply notify error %d\n", ret);
777 goto err_free_overlay_changeset;
780 ret = build_changeset(ovcs);
782 goto err_free_overlay_changeset;
785 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
788 pr_debug("overlay changeset revert error %d\n",
790 devicetree_state_flags |= DTSF_APPLY_FAIL;
792 goto err_free_overlay_changeset;
795 ret = __of_changeset_apply_notify(&ovcs->cset);
797 pr_err("overlay changeset entry notify error %d\n", ret);
798 /* notify failure is not fatal, continue */
800 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
803 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
805 pr_err("overlay changeset post-apply notify error %d\n",
817 err_free_overlay_changeset:
818 free_overlay_changeset(ovcs);
821 mutex_unlock(&of_mutex);
822 of_overlay_mutex_unlock();
825 pr_debug("%s() err=%d\n", __func__, ret);
830 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
836 struct device_node *overlay_root;
841 if (overlay_fdt_size < sizeof(struct fdt_header) ||
842 fdt_check_header(overlay_fdt)) {
843 pr_err("Invalid overlay_fdt header\n");
847 size = fdt_totalsize(overlay_fdt);
848 if (overlay_fdt_size < size)
852 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
853 * will create pointers to the passed in FDT in the unflattened tree.
855 new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
859 of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
861 pr_err("unable to unflatten overlay_fdt\n");
863 goto out_free_new_fdt;
866 ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
869 * new_fdt and overlay_root now belong to the overlay
871 * overlay changeset code is responsible for freeing them.
885 EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
890 * Returns 1 if @np is @tree or is contained in @tree, else 0
892 static int find_node(struct device_node *tree, struct device_node *np)
894 struct device_node *child;
899 for_each_child_of_node(tree, child) {
900 if (find_node(child, np)) {
910 * Is @remove_ce_node a child of, a parent of, or the same as any
911 * node in an overlay changeset more topmost than @remove_ovcs?
913 * Returns 1 if found, else 0
915 static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
916 struct device_node *remove_ce_node)
918 struct overlay_changeset *ovcs;
919 struct of_changeset_entry *ce;
921 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
922 if (ovcs == remove_ovcs)
925 list_for_each_entry(ce, &ovcs->cset.entries, node) {
926 if (find_node(ce->np, remove_ce_node)) {
927 pr_err("%s: #%d overlaps with #%d @%pOF\n",
928 __func__, remove_ovcs->id, ovcs->id,
932 if (find_node(remove_ce_node, ce->np)) {
933 pr_err("%s: #%d overlaps with #%d @%pOF\n",
934 __func__, remove_ovcs->id, ovcs->id,
945 * We can safely remove the overlay only if it's the top-most one.
946 * Newly applied overlays are inserted at the tail of the overlay list,
947 * so a top most overlay is the one that is closest to the tail.
949 * The topmost check is done by exploiting this property. For each
950 * affected device node in the log list we check if this overlay is
951 * the one closest to the tail. If another overlay has affected this
952 * device node and is closest to the tail, then removal is not permited.
954 static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
956 struct of_changeset_entry *remove_ce;
958 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
959 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
960 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
969 * of_overlay_remove() - Revert and free an overlay changeset
970 * @ovcs_id: Pointer to overlay changeset id
972 * Removes an overlay if it is permissible. @ovcs_id was previously returned
973 * by of_overlay_fdt_apply().
975 * If an error occurred while attempting to revert the overlay changeset,
976 * then an attempt is made to re-apply any changeset entry that was
977 * reverted. If an error occurs on re-apply then the state of the device
978 * tree can not be determined, and any following attempt to apply or remove
979 * an overlay changeset will be refused.
981 * A non-zero return value will not revert the changeset if error is from:
983 * - overlay changeset pre-remove notifier
984 * - overlay changeset entry revert
986 * If an error is returned by an overlay changeset pre-remove notifier
987 * then no further overlay changeset pre-remove notifier will be called.
989 * If more than one notifier returns an error, then the last notifier
990 * error to occur is returned.
992 * A non-zero return value will revert the changeset if error is from:
993 * - overlay changeset entry notifier
994 * - overlay changeset post-remove notifier
996 * If an error is returned by an overlay changeset post-remove notifier
997 * then no further overlay changeset post-remove notifier will be called.
999 * Returns 0 on success, or a negative error number. *ovcs_id is set to
1000 * zero after reverting the changeset, even if a subsequent error occurs.
1002 int of_overlay_remove(int *ovcs_id)
1004 struct overlay_changeset *ovcs;
1005 int ret, ret_apply, ret_tmp;
1009 if (devicetree_corrupt()) {
1010 pr_err("suspect devicetree state, refuse to remove overlay\n");
1015 mutex_lock(&of_mutex);
1017 ovcs = idr_find(&ovcs_idr, *ovcs_id);
1020 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
1024 if (!overlay_removal_is_ok(ovcs)) {
1029 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
1031 pr_err("overlay changeset pre-remove notify error %d\n", ret);
1035 list_del(&ovcs->ovcs_list);
1038 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
1041 devicetree_state_flags |= DTSF_REVERT_FAIL;
1045 ret = __of_changeset_revert_notify(&ovcs->cset);
1047 pr_err("overlay changeset entry notify error %d\n", ret);
1048 /* notify failure is not fatal, continue */
1052 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
1054 pr_err("overlay changeset post-remove notify error %d\n",
1060 free_overlay_changeset(ovcs);
1063 mutex_unlock(&of_mutex);
1066 pr_debug("%s() err=%d\n", __func__, ret);
1070 EXPORT_SYMBOL_GPL(of_overlay_remove);
1073 * of_overlay_remove_all() - Reverts and frees all overlay changesets
1075 * Removes all overlays from the system in the correct order.
1077 * Returns 0 on success, or a negative error number
1079 int of_overlay_remove_all(void)
1081 struct overlay_changeset *ovcs, *ovcs_n;
1084 /* the tail of list is guaranteed to be safe to remove */
1085 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
1086 ret = of_overlay_remove(&ovcs->id);
1093 EXPORT_SYMBOL_GPL(of_overlay_remove_all);