blob: 4cdee169a5ab634a515094e2965291cfc7443d64 [file] [log] [blame]
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001/*
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 */
Rob Herring606ad422016-06-15 08:32:18 -050011
12#define pr_fmt(fmt) "OF: overlay: " fmt
13
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/string.h>
19#include <linux/ctype.h>
20#include <linux/errno.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020021#include <linux/slab.h>
22#include <linux/err.h>
Mark Brown0d1886d2015-02-17 11:36:58 +090023#include <linux/idr.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020024
25#include "of_private.h"
26
27/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070028 * struct fragment - info about fragment nodes in overlay expanded device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020029 * @target: target of the overlay operation
Frank Rowand0290c4c2017-10-17 16:36:23 -070030 * @overlay: pointer to the __overlay__ node
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020031 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070032struct fragment {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020033 struct device_node *target;
34 struct device_node *overlay;
Frank Rowandd1651b02017-07-19 09:25:22 -070035 bool is_symbols_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020036};
37
38/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070039 * struct overlay_changeset
40 * @ovcs_list: list on which we are located
41 * @count: count of @fragments structures
42 * @fragments: info about fragment nodes in overlay expanded device tree
43 * @cset: changeset to apply fragments to live device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020044 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070045struct overlay_changeset {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020046 int id;
Frank Rowand0290c4c2017-10-17 16:36:23 -070047 struct list_head ovcs_list;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020048 int count;
Frank Rowand0290c4c2017-10-17 16:36:23 -070049 struct fragment *fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020050 struct of_changeset cset;
51};
52
Frank Rowand24789c52017-10-17 16:36:26 -070053/* flags are sticky - once set, do not reset */
54static int devicetree_state_flags;
55#define DTSF_APPLY_FAIL 0x01
56#define DTSF_REVERT_FAIL 0x02
57
58/*
59 * If a changeset apply or revert encounters an error, an attempt will
60 * be made to undo partial changes, but may fail. If the undo fails
61 * we do not know the state of the devicetree.
62 */
63static int devicetree_corrupt(void)
64{
65 return devicetree_state_flags &
66 (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
67}
68
Frank Rowand0290c4c2017-10-17 16:36:23 -070069static int build_changeset_next_level(struct overlay_changeset *ovcs,
70 struct device_node *target_node,
71 const struct device_node *overlay_node,
Frank Rowandd1651b02017-07-19 09:25:22 -070072 bool is_symbols_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020073
Frank Rowand61b4de42017-10-17 16:36:25 -070074static LIST_HEAD(ovcs_list);
75static DEFINE_IDR(ovcs_idr);
76
Frank Rowand0290c4c2017-10-17 16:36:23 -070077static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
Alan Tull39a842e2016-11-01 14:14:22 -050078
79int of_overlay_notifier_register(struct notifier_block *nb)
80{
Frank Rowand0290c4c2017-10-17 16:36:23 -070081 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -050082}
83EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
84
85int of_overlay_notifier_unregister(struct notifier_block *nb)
86{
Frank Rowand0290c4c2017-10-17 16:36:23 -070087 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -050088}
89EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
90
Frank Rowand24789c52017-10-17 16:36:26 -070091static char *of_overlay_action_name[] = {
92 "pre-apply",
93 "post-apply",
94 "pre-remove",
95 "post-remove",
96};
97
Frank Rowand0290c4c2017-10-17 16:36:23 -070098static int overlay_notify(struct overlay_changeset *ovcs,
99 enum of_overlay_notify_action action)
Alan Tull39a842e2016-11-01 14:14:22 -0500100{
101 struct of_overlay_notify_data nd;
102 int i, ret;
103
Frank Rowand0290c4c2017-10-17 16:36:23 -0700104 for (i = 0; i < ovcs->count; i++) {
105 struct fragment *fragment = &ovcs->fragments[i];
Alan Tull39a842e2016-11-01 14:14:22 -0500106
Frank Rowand0290c4c2017-10-17 16:36:23 -0700107 nd.target = fragment->target;
108 nd.overlay = fragment->overlay;
Alan Tull39a842e2016-11-01 14:14:22 -0500109
Frank Rowand0290c4c2017-10-17 16:36:23 -0700110 ret = blocking_notifier_call_chain(&overlay_notify_chain,
Alan Tull39a842e2016-11-01 14:14:22 -0500111 action, &nd);
Frank Rowand24789c52017-10-17 16:36:26 -0700112 if (ret == NOTIFY_STOP)
113 return 0;
114 if (ret) {
115 ret = notifier_to_errno(ret);
116 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
117 of_overlay_action_name[action], ret, nd.target);
118 return ret;
119 }
Alan Tull39a842e2016-11-01 14:14:22 -0500120 }
121
122 return 0;
123}
124
Frank Rowand42b2e942017-10-17 16:36:24 -0700125/*
126 * The properties in the "/__symbols__" node are "symbols".
127 *
128 * The value of properties in the "/__symbols__" node is the path of a
129 * node in the subtree of a fragment node's "__overlay__" node, for
130 * example "/fragment@0/__overlay__/symbol_path_tail". Symbol_path_tail
131 * can be a single node or it may be a multi-node path.
132 *
133 * The duplicated property value will be modified by replacing the
134 * "/fragment_name/__overlay/" portion of the value with the target
135 * path from the fragment node.
136 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700137static struct property *dup_and_fixup_symbol_prop(
138 struct overlay_changeset *ovcs, const struct property *prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200139{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700140 struct fragment *fragment;
Frank Rowandd1651b02017-07-19 09:25:22 -0700141 struct property *new;
142 const char *overlay_name;
Frank Rowand42b2e942017-10-17 16:36:24 -0700143 char *symbol_path_tail;
Frank Rowandd1651b02017-07-19 09:25:22 -0700144 char *symbol_path;
145 const char *target_path;
146 int k;
Frank Rowand42b2e942017-10-17 16:36:24 -0700147 int symbol_path_tail_len;
Frank Rowandd1651b02017-07-19 09:25:22 -0700148 int overlay_name_len;
149 int target_path_len;
150
151 if (!prop->value)
152 return NULL;
153 symbol_path = prop->value;
154
155 new = kzalloc(sizeof(*new), GFP_KERNEL);
156 if (!new)
157 return NULL;
158
Frank Rowand0290c4c2017-10-17 16:36:23 -0700159 for (k = 0; k < ovcs->count; k++) {
160 fragment = &ovcs->fragments[k];
161 overlay_name = fragment->overlay->full_name;
Frank Rowandd1651b02017-07-19 09:25:22 -0700162 overlay_name_len = strlen(overlay_name);
163 if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
164 break;
165 }
166
Frank Rowand0290c4c2017-10-17 16:36:23 -0700167 if (k >= ovcs->count)
Frank Rowandd1651b02017-07-19 09:25:22 -0700168 goto err_free;
169
Frank Rowand0290c4c2017-10-17 16:36:23 -0700170 target_path = fragment->target->full_name;
Frank Rowandd1651b02017-07-19 09:25:22 -0700171 target_path_len = strlen(target_path);
172
Frank Rowand42b2e942017-10-17 16:36:24 -0700173 symbol_path_tail = symbol_path + overlay_name_len;
174 symbol_path_tail_len = strlen(symbol_path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700175
176 new->name = kstrdup(prop->name, GFP_KERNEL);
Frank Rowand42b2e942017-10-17 16:36:24 -0700177 new->length = target_path_len + symbol_path_tail_len + 1;
Frank Rowandd1651b02017-07-19 09:25:22 -0700178 new->value = kzalloc(new->length, GFP_KERNEL);
179
180 if (!new->name || !new->value)
181 goto err_free;
182
183 strcpy(new->value, target_path);
Frank Rowand42b2e942017-10-17 16:36:24 -0700184 strcpy(new->value + target_path_len, symbol_path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700185
Frank Rowandd1651b02017-07-19 09:25:22 -0700186 of_property_set_flag(new, OF_DYNAMIC);
187
188 return new;
189
190 err_free:
191 kfree(new->name);
192 kfree(new->value);
193 kfree(new);
194 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700195}
196
Frank Rowand0290c4c2017-10-17 16:36:23 -0700197/**
198 * add_changeset_property() - add @overlay_prop to overlay changeset
199 * @ovcs: overlay changeset
200 * @target_node: where to place @overlay_prop in live tree
201 * @overlay_prop: property to add or update, from overlay tree
202 * is_symbols_node: 1 if @target_node is "/__symbols__"
203 *
204 * If @overlay_prop does not already exist in @target_node, add changeset entry
205 * to add @overlay_prop in @target_node, else add changeset entry to update
206 * value of @overlay_prop.
207 *
Frank Rowand646afc42017-10-17 16:36:21 -0700208 * Some special properties are not updated (no error returned).
Frank Rowand0290c4c2017-10-17 16:36:23 -0700209 *
Frank Rowand646afc42017-10-17 16:36:21 -0700210 * Update of property in symbols node is not allowed.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700211 *
212 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
213 * invalid @overlay.
Frank Rowand646afc42017-10-17 16:36:21 -0700214 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700215static int add_changeset_property(struct overlay_changeset *ovcs,
216 struct device_node *target_node,
217 struct property *overlay_prop,
Frank Rowandd1651b02017-07-19 09:25:22 -0700218 bool is_symbols_node)
219{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700220 struct property *new_prop = NULL, *prop;
Lixin Wangac0f3e32017-10-16 17:54:32 +0800221 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200222
Frank Rowand0290c4c2017-10-17 16:36:23 -0700223 prop = of_find_property(target_node, overlay_prop->name, NULL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200224
Frank Rowand0290c4c2017-10-17 16:36:23 -0700225 if (!of_prop_cmp(overlay_prop->name, "name") ||
226 !of_prop_cmp(overlay_prop->name, "phandle") ||
227 !of_prop_cmp(overlay_prop->name, "linux,phandle"))
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200228 return 0;
229
Frank Rowandd1651b02017-07-19 09:25:22 -0700230 if (is_symbols_node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700231 if (prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700232 return -EINVAL;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700233 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
Frank Rowandd1651b02017-07-19 09:25:22 -0700234 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700235 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
Frank Rowandd1651b02017-07-19 09:25:22 -0700236 }
237
Frank Rowand0290c4c2017-10-17 16:36:23 -0700238 if (!new_prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200239 return -ENOMEM;
240
Frank Rowand0290c4c2017-10-17 16:36:23 -0700241 if (!prop)
242 ret = of_changeset_add_property(&ovcs->cset, target_node,
243 new_prop);
Frank Rowand646afc42017-10-17 16:36:21 -0700244 else
Frank Rowand0290c4c2017-10-17 16:36:23 -0700245 ret = of_changeset_update_property(&ovcs->cset, target_node,
246 new_prop);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200247
Lixin Wangac0f3e32017-10-16 17:54:32 +0800248 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700249 kfree(new_prop->name);
250 kfree(new_prop->value);
251 kfree(new_prop);
Lixin Wangac0f3e32017-10-16 17:54:32 +0800252 }
253 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200254}
255
Frank Rowand0290c4c2017-10-17 16:36:23 -0700256/**
257 * add_changeset_node() - add @node (and children) to overlay changeset
258 * @ovcs: overlay changeset
259 * @target_node: where to place @node in live tree
260 * @node: node from within overlay device tree fragment
261 *
262 * If @node does not already exist in @target_node, add changeset entry
263 * to add @node in @target_node.
264 *
265 * If @node already exists in @target_node, and the existing node has
266 * a phandle, the overlay node is not allowed to have a phandle.
267 *
268 * If @node has child nodes, add the children recursively via
269 * build_changeset_next_level().
270 *
271 * NOTE: Multiple mods of created nodes not supported.
Frank Rowand24789c52017-10-17 16:36:26 -0700272 * If more than one fragment contains a node that does not already exist
273 * in the live tree, then for each fragment of_changeset_attach_node()
274 * will add a changeset entry to add the node. When the changeset is
275 * applied, __of_attach_node() will attach the node twice (once for
276 * each fragment). At this point the device tree will be corrupted.
277 *
278 * TODO: add integrity check to ensure that multiple fragments do not
279 * create the same node.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700280 *
281 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
282 * invalid @overlay.
283 */
284static int add_changeset_node(struct overlay_changeset *ovcs,
285 struct device_node *target_node, struct device_node *node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200286{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700287 const char *node_kbasename;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300288 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200289 int ret = 0;
290
Frank Rowand0290c4c2017-10-17 16:36:23 -0700291 node_kbasename = kbasename(node->full_name);
292 if (!node_kbasename)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200293 return -ENOMEM;
294
Frank Rowand0290c4c2017-10-17 16:36:23 -0700295 for_each_child_of_node(target_node, tchild)
296 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700297 break;
298
Frank Rowand61b4de42017-10-17 16:36:25 -0700299 if (!tchild) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700300 tchild = __of_node_dup(node, "%pOF/%s",
301 target_node, node_kbasename);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200302 if (!tchild)
303 return -ENOMEM;
304
Frank Rowand0290c4c2017-10-17 16:36:23 -0700305 tchild->parent = target_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200306
Frank Rowand0290c4c2017-10-17 16:36:23 -0700307 ret = of_changeset_attach_node(&ovcs->cset, tchild);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200308 if (ret)
309 return ret;
310
Frank Rowand61b4de42017-10-17 16:36:25 -0700311 return build_changeset_next_level(ovcs, tchild, node, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200312 }
313
Frank Rowand61b4de42017-10-17 16:36:25 -0700314 if (node->phandle)
315 return -EINVAL;
316
317 ret = build_changeset_next_level(ovcs, tchild, node, 0);
318 of_node_put(tchild);
319
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200320 return ret;
321}
322
Frank Rowand0290c4c2017-10-17 16:36:23 -0700323/**
324 * build_changeset_next_level() - add level of overlay changeset
325 * @ovcs: overlay changeset
326 * @target_node: where to place @overlay_node in live tree
327 * @overlay_node: node from within an overlay device tree fragment
328 * @is_symbols_node: @overlay_node is node "/__symbols__"
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200329 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700330 * Add the properties (if any) and nodes (if any) from @overlay_node to the
331 * @ovcs->cset changeset. If an added node has child nodes, they will
332 * be added recursively.
Frank Rowand646afc42017-10-17 16:36:21 -0700333 *
334 * Do not allow symbols node to have any children.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700335 *
336 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
337 * invalid @overlay_node.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200338 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700339static int build_changeset_next_level(struct overlay_changeset *ovcs,
340 struct device_node *target_node,
341 const struct device_node *overlay_node,
Frank Rowandd1651b02017-07-19 09:25:22 -0700342 bool is_symbols_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200343{
344 struct device_node *child;
345 struct property *prop;
346 int ret;
347
Frank Rowand0290c4c2017-10-17 16:36:23 -0700348 for_each_property_of_node(overlay_node, prop) {
349 ret = add_changeset_property(ovcs, target_node, prop,
350 is_symbols_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200351 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700352 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
353 target_node, prop->name, ret);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200354 return ret;
355 }
356 }
357
Frank Rowandd1651b02017-07-19 09:25:22 -0700358 if (is_symbols_node)
359 return 0;
360
Frank Rowand0290c4c2017-10-17 16:36:23 -0700361 for_each_child_of_node(overlay_node, child) {
362 ret = add_changeset_node(ovcs, target_node, child);
Frank Rowandbbed8792017-10-17 16:36:22 -0700363 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700364 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
365 target_node, child->name, ret);
Julia Lawall001cf502015-10-22 11:02:48 +0200366 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200367 return ret;
368 }
369 }
370
371 return 0;
372}
373
374/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700375 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
376 * @ovcs: Overlay changeset
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200377 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700378 * Create changeset @ovcs->cset to contain the nodes and properties of the
379 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
380 * any portions of the changeset that were successfully created will remain
381 * in @ovcs->cset.
382 *
383 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
384 * invalid overlay in @ovcs->fragments[].
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200385 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700386static int build_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200387{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700388 int i, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200389
Frank Rowand0290c4c2017-10-17 16:36:23 -0700390 for (i = 0; i < ovcs->count; i++) {
391 struct fragment *fragment = &ovcs->fragments[i];
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200392
Frank Rowand0290c4c2017-10-17 16:36:23 -0700393 ret = build_changeset_next_level(ovcs, fragment->target,
394 fragment->overlay,
395 fragment->is_symbols_node);
396 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700397 pr_debug("apply failed '%pOF'\n", fragment->target);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700398 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200399 }
400 }
401
402 return 0;
403}
404
405/*
406 * Find the target node using a number of different strategies
Frank Rowand646afc42017-10-17 16:36:21 -0700407 * in order of preference:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200408 *
Frank Rowand646afc42017-10-17 16:36:21 -0700409 * 1) "target" property containing the phandle of the target
410 * 2) "target-path" property containing the path of the target
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200411 */
412static struct device_node *find_target_node(struct device_node *info_node)
413{
414 const char *path;
415 u32 val;
416 int ret;
417
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200418 ret = of_property_read_u32(info_node, "target", &val);
Frank Rowandbbed8792017-10-17 16:36:22 -0700419 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200420 return of_find_node_by_phandle(val);
421
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200422 ret = of_property_read_string(info_node, "target-path", &path);
Frank Rowandbbed8792017-10-17 16:36:22 -0700423 if (!ret)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200424 return of_find_node_by_path(path);
425
Rob Herring606ad422016-06-15 08:32:18 -0500426 pr_err("Failed to find target for node %p (%s)\n",
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200427 info_node, info_node->name);
428
429 return NULL;
430}
431
432/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700433 * init_overlay_changeset() - initialize overlay changeset from overlay tree
434 * @ovcs Overlay changeset to build
435 * @tree: Contains all the overlay fragments and overlay fixup nodes
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200436 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700437 * Initialize @ovcs. Populate @ovcs->fragments with node information from
438 * the top level of @tree. The relevant top level nodes are the fragment
439 * nodes and the __symbols__ node. Any other top level node will be ignored.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200440 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700441 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
Frank Rowand61b4de42017-10-17 16:36:25 -0700442 * detected in @tree, or -ENOSPC if idr_alloc() error.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200443 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700444static int init_overlay_changeset(struct overlay_changeset *ovcs,
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200445 struct device_node *tree)
446{
Frank Rowand61b4de42017-10-17 16:36:25 -0700447 struct device_node *node, *overlay_node;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700448 struct fragment *fragment;
449 struct fragment *fragments;
450 int cnt, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200451
Frank Rowand24789c52017-10-17 16:36:26 -0700452 /*
453 * Warn for some issues. Can not return -EINVAL for these until
454 * of_unittest_apply_overlay() is fixed to pass these checks.
455 */
456 if (!of_node_check_flag(tree, OF_DYNAMIC))
457 pr_debug("%s() tree is not dynamic\n", __func__);
458
459 if (!of_node_check_flag(tree, OF_DETACHED))
460 pr_debug("%s() tree is not detached\n", __func__);
461
462 if (!of_node_is_root(tree))
463 pr_debug("%s() tree is not root\n", __func__);
464
Frank Rowand61b4de42017-10-17 16:36:25 -0700465 INIT_LIST_HEAD(&ovcs->ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200466
Frank Rowand61b4de42017-10-17 16:36:25 -0700467 of_changeset_init(&ovcs->cset);
468
469 ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
470 if (ovcs->id <= 0)
471 return ovcs->id;
472
473 cnt = 0;
474
475 /* fragment nodes */
476 for_each_child_of_node(tree, node) {
477 overlay_node = of_get_child_by_name(node, "__overlay__");
478 if (overlay_node) {
479 cnt++;
480 of_node_put(overlay_node);
481 }
482 }
483
484 node = of_get_child_by_name(tree, "__symbols__");
485 if (node) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700486 cnt++;
Frank Rowand61b4de42017-10-17 16:36:25 -0700487 of_node_put(node);
488 }
Frank Rowandd1651b02017-07-19 09:25:22 -0700489
Frank Rowand0290c4c2017-10-17 16:36:23 -0700490 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
Frank Rowand61b4de42017-10-17 16:36:25 -0700491 if (!fragments) {
492 ret = -ENOMEM;
493 goto err_free_idr;
494 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200495
496 cnt = 0;
497 for_each_child_of_node(tree, node) {
Frank Rowand61b4de42017-10-17 16:36:25 -0700498 fragment = &fragments[cnt];
499 fragment->overlay = of_get_child_by_name(node, "__overlay__");
500 if (fragment->overlay) {
501 fragment->target = find_target_node(node);
502 if (!fragment->target) {
503 of_node_put(fragment->overlay);
504 ret = -EINVAL;
505 goto err_free_fragments;
506 } else {
507 cnt++;
508 }
509 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200510 }
511
Frank Rowandd1651b02017-07-19 09:25:22 -0700512 node = of_get_child_by_name(tree, "__symbols__");
513 if (node) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700514 fragment = &fragments[cnt];
515 fragment->overlay = node;
516 fragment->target = of_find_node_by_path("/__symbols__");
517 fragment->is_symbols_node = 1;
Frank Rowandd1651b02017-07-19 09:25:22 -0700518
Frank Rowand0290c4c2017-10-17 16:36:23 -0700519 if (!fragment->target) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700520 pr_err("no symbols in root of device tree.\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700521 ret = -EINVAL;
522 goto err_free_fragments;
Frank Rowandd1651b02017-07-19 09:25:22 -0700523 }
524
525 cnt++;
526 }
527
Frank Rowandbbed8792017-10-17 16:36:22 -0700528 if (!cnt) {
Frank Rowand61b4de42017-10-17 16:36:25 -0700529 ret = -EINVAL;
530 goto err_free_fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200531 }
532
Frank Rowand0290c4c2017-10-17 16:36:23 -0700533 ovcs->count = cnt;
534 ovcs->fragments = fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200535
536 return 0;
Frank Rowand61b4de42017-10-17 16:36:25 -0700537
Frank Rowand61b4de42017-10-17 16:36:25 -0700538err_free_fragments:
539 kfree(fragments);
540err_free_idr:
541 idr_remove(&ovcs_idr, ovcs->id);
542
Frank Rowand24789c52017-10-17 16:36:26 -0700543 pr_err("%s() failed, ret = %d\n", __func__, ret);
544
Frank Rowand61b4de42017-10-17 16:36:25 -0700545 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200546}
547
Frank Rowand61b4de42017-10-17 16:36:25 -0700548static void free_overlay_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200549{
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200550 int i;
551
Frank Rowand61b4de42017-10-17 16:36:25 -0700552 if (!ovcs->cset.entries.next)
553 return;
554 of_changeset_destroy(&ovcs->cset);
555
556 if (ovcs->id)
557 idr_remove(&ovcs_idr, ovcs->id);
558
559 for (i = 0; i < ovcs->count; i++) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700560 of_node_put(ovcs->fragments[i].target);
561 of_node_put(ovcs->fragments[i].overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200562 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700563 kfree(ovcs->fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200564
Frank Rowand61b4de42017-10-17 16:36:25 -0700565 kfree(ovcs);
566}
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200567
568/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700569 * of_overlay_apply() - Create and apply an overlay changeset
570 * @tree: Expanded overlay device tree
Frank Rowand24789c52017-10-17 16:36:26 -0700571 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200572 *
Frank Rowand24789c52017-10-17 16:36:26 -0700573 * Creates and applies an overlay changeset.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200574 *
Frank Rowand24789c52017-10-17 16:36:26 -0700575 * If an error occurs in a pre-apply notifier, then no changes are made
576 * to the device tree.
577 *
578
579 * A non-zero return value will not have created the changeset if error is from:
580 * - parameter checks
581 * - building the changeset
582 * - overlay changset pre-apply notifier
583 *
584 * If an error is returned by an overlay changeset pre-apply notifier
585 * then no further overlay changeset pre-apply notifier will be called.
586 *
587 * A non-zero return value will have created the changeset if error is from:
588 * - overlay changeset entry notifier
589 * - overlay changset post-apply notifier
590 *
591 * If an error is returned by an overlay changeset post-apply notifier
592 * then no further overlay changeset post-apply notifier will be called.
593 *
594 * If more than one notifier returns an error, then the last notifier
595 * error to occur is returned.
596 *
597 * If an error occurred while applying the overlay changeset, then an
598 * attempt is made to revert any changes that were made to the
599 * device tree. If there were any errors during the revert attempt
600 * then the state of the device tree can not be determined, and any
601 * following attempt to apply or remove an overlay changeset will be
602 * refused.
603 *
604 * Returns 0 on success, or a negative error number. Overlay changeset
605 * id is returned to *ovcs_id.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200606 */
Frank Rowand24789c52017-10-17 16:36:26 -0700607
608int of_overlay_apply(struct device_node *tree, int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200609{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700610 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700611 int ret = 0, ret_revert, ret_tmp;
612
613 *ovcs_id = 0;
614
615 if (devicetree_corrupt()) {
616 pr_err("devicetree state suspect, refuse to apply overlay\n");
617 ret = -EBUSY;
618 goto out;
619 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200620
Frank Rowand0290c4c2017-10-17 16:36:23 -0700621 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
Frank Rowand24789c52017-10-17 16:36:26 -0700622 if (!ovcs) {
623 ret = -ENOMEM;
624 goto out;
625 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200626
627 mutex_lock(&of_mutex);
628
Frank Rowand0290c4c2017-10-17 16:36:23 -0700629 ret = init_overlay_changeset(ovcs, tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700630 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700631 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200632
Frank Rowand0290c4c2017-10-17 16:36:23 -0700633 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
Frank Rowand24789c52017-10-17 16:36:26 -0700634 if (ret) {
635 pr_err("overlay changeset pre-apply notify error %d\n", ret);
Frank Rowand61b4de42017-10-17 16:36:25 -0700636 goto err_free_overlay_changeset;
Alan Tull39a842e2016-11-01 14:14:22 -0500637 }
638
Frank Rowand0290c4c2017-10-17 16:36:23 -0700639 ret = build_changeset(ovcs);
640 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700641 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200642
Frank Rowand24789c52017-10-17 16:36:26 -0700643 ret_revert = 0;
644 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
645 if (ret) {
646 if (ret_revert) {
647 pr_debug("overlay changeset revert error %d\n",
648 ret_revert);
649 devicetree_state_flags |= DTSF_APPLY_FAIL;
650 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700651 goto err_free_overlay_changeset;
Frank Rowand24789c52017-10-17 16:36:26 -0700652 } else {
653 ret = __of_changeset_apply_notify(&ovcs->cset);
654 if (ret)
655 pr_err("overlay changeset entry notify error %d\n",
656 ret);
657 /* fall through */
658 }
Rob Herring606ad422016-06-15 08:32:18 -0500659
Frank Rowand0290c4c2017-10-17 16:36:23 -0700660 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
Frank Rowand24789c52017-10-17 16:36:26 -0700661 *ovcs_id = ovcs->id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200662
Frank Rowand24789c52017-10-17 16:36:26 -0700663 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
664 if (ret_tmp) {
665 pr_err("overlay changeset post-apply notify error %d\n",
666 ret_tmp);
667 if (!ret)
668 ret = ret_tmp;
669 }
Alan Tull39a842e2016-11-01 14:14:22 -0500670
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200671 mutex_unlock(&of_mutex);
672
Frank Rowand24789c52017-10-17 16:36:26 -0700673 goto out;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200674
Frank Rowand61b4de42017-10-17 16:36:25 -0700675err_free_overlay_changeset:
676 free_overlay_changeset(ovcs);
677
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200678 mutex_unlock(&of_mutex);
679
Frank Rowand24789c52017-10-17 16:36:26 -0700680out:
681 pr_debug("%s() err=%d\n", __func__, ret);
682
Frank Rowand0290c4c2017-10-17 16:36:23 -0700683 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200684}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700685EXPORT_SYMBOL_GPL(of_overlay_apply);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200686
Frank Rowand646afc42017-10-17 16:36:21 -0700687/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700688 * Find @np in @tree.
689 *
690 * Returns 1 if @np is @tree or is contained in @tree, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700691 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700692static int find_node(struct device_node *tree, struct device_node *np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200693{
694 struct device_node *child;
695
Frank Rowand0290c4c2017-10-17 16:36:23 -0700696 if (tree == np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200697 return 1;
698
699 for_each_child_of_node(tree, child) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700700 if (find_node(child, np)) {
Julia Lawall001cf502015-10-22 11:02:48 +0200701 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200702 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200703 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200704 }
705
706 return 0;
707}
708
Frank Rowand646afc42017-10-17 16:36:21 -0700709/*
Frank Rowand87f242c2017-10-17 16:36:27 -0700710 * Is @remove_ce_node a child of, a parent of, or the same as any
Frank Rowand0290c4c2017-10-17 16:36:23 -0700711 * node in an overlay changeset more topmost than @remove_ovcs?
712 *
713 * Returns 1 if found, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700714 */
Frank Rowand87f242c2017-10-17 16:36:27 -0700715static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
716 struct device_node *remove_ce_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200717{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700718 struct overlay_changeset *ovcs;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200719 struct of_changeset_entry *ce;
720
Frank Rowand0290c4c2017-10-17 16:36:23 -0700721 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
722 if (ovcs == remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200723 break;
724
Frank Rowand0290c4c2017-10-17 16:36:23 -0700725 list_for_each_entry(ce, &ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700726 if (find_node(ce->np, remove_ce_node)) {
727 pr_err("%s: #%d overlaps with #%d @%pOF\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700728 __func__, remove_ovcs->id, ovcs->id,
Frank Rowand87f242c2017-10-17 16:36:27 -0700729 remove_ce_node);
730 return 1;
731 }
732 if (find_node(remove_ce_node, ce->np)) {
733 pr_err("%s: #%d overlaps with #%d @%pOF\n",
734 __func__, remove_ovcs->id, ovcs->id,
735 remove_ce_node);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700736 return 1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200737 }
738 }
739 }
740
Frank Rowand0290c4c2017-10-17 16:36:23 -0700741 return 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200742}
743
744/*
745 * We can safely remove the overlay only if it's the top-most one.
746 * Newly applied overlays are inserted at the tail of the overlay list,
747 * so a top most overlay is the one that is closest to the tail.
748 *
749 * The topmost check is done by exploiting this property. For each
750 * affected device node in the log list we check if this overlay is
751 * the one closest to the tail. If another overlay has affected this
752 * device node and is closest to the tail, then removal is not permited.
753 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700754static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200755{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700756 struct of_changeset_entry *remove_ce;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200757
Frank Rowand0290c4c2017-10-17 16:36:23 -0700758 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700759 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700760 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200761 return 0;
762 }
763 }
764
765 return 1;
766}
767
768/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700769 * of_overlay_remove() - Revert and free an overlay changeset
Frank Rowand24789c52017-10-17 16:36:26 -0700770 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200771 *
Frank Rowand24789c52017-10-17 16:36:26 -0700772 * Removes an overlay if it is permissible. @ovcs_id was previously returned
Frank Rowand0290c4c2017-10-17 16:36:23 -0700773 * by of_overlay_apply().
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200774 *
Frank Rowand24789c52017-10-17 16:36:26 -0700775 * If an error occurred while attempting to revert the overlay changeset,
776 * then an attempt is made to re-apply any changeset entry that was
777 * reverted. If an error occurs on re-apply then the state of the device
778 * tree can not be determined, and any following attempt to apply or remove
779 * an overlay changeset will be refused.
780 *
781 * A non-zero return value will not revert the changeset if error is from:
782 * - parameter checks
783 * - overlay changset pre-remove notifier
784 * - overlay changeset entry revert
785 *
786 * If an error is returned by an overlay changeset pre-remove notifier
787 * then no further overlay changeset pre-remove notifier will be called.
788 *
789 * If more than one notifier returns an error, then the last notifier
790 * error to occur is returned.
791 *
792 * A non-zero return value will revert the changeset if error is from:
793 * - overlay changeset entry notifier
794 * - overlay changset post-remove notifier
795 *
796 * If an error is returned by an overlay changeset post-remove notifier
797 * then no further overlay changeset post-remove notifier will be called.
798 *
799 * Returns 0 on success, or a negative error number. *ovcs_id is set to
800 * zero after reverting the changeset, even if a subsequent error occurs.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200801 */
Frank Rowand24789c52017-10-17 16:36:26 -0700802int of_overlay_remove(int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200803{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700804 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700805 int ret, ret_apply, ret_tmp;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200806
Frank Rowand24789c52017-10-17 16:36:26 -0700807 ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200808
Frank Rowand24789c52017-10-17 16:36:26 -0700809 if (devicetree_corrupt()) {
810 pr_err("suspect devicetree state, refuse to remove overlay\n");
Frank Rowand0290c4c2017-10-17 16:36:23 -0700811 ret = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200812 goto out;
813 }
814
Frank Rowand24789c52017-10-17 16:36:26 -0700815 mutex_lock(&of_mutex);
816
817 ovcs = idr_find(&ovcs_idr, *ovcs_id);
818 if (!ovcs) {
819 ret = -ENODEV;
820 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
821 goto out_unlock;
822 }
823
824 if (!overlay_removal_is_ok(ovcs)) {
825 ret = -EBUSY;
826 goto out_unlock;
827 }
828
829 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
830 if (ret) {
831 pr_err("overlay changeset pre-remove notify error %d\n", ret);
832 goto out_unlock;
833 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700834
Frank Rowand0290c4c2017-10-17 16:36:23 -0700835 list_del(&ovcs->ovcs_list);
Frank Rowand61b4de42017-10-17 16:36:25 -0700836
Frank Rowand24789c52017-10-17 16:36:26 -0700837 ret_apply = 0;
838 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
839 if (ret) {
840 if (ret_apply)
841 devicetree_state_flags |= DTSF_REVERT_FAIL;
842 goto out_unlock;
843 } else {
844 ret = __of_changeset_revert_notify(&ovcs->cset);
845 if (ret) {
846 pr_err("overlay changeset entry notify error %d\n",
847 ret);
848 /* fall through - changeset was reverted */
849 }
850 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700851
Frank Rowand24789c52017-10-17 16:36:26 -0700852 *ovcs_id = 0;
853
854 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
855 if (ret_tmp) {
856 pr_err("overlay changeset post-remove notify error %d\n",
857 ret_tmp);
858 if (!ret)
859 ret = ret_tmp;
860 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700861
862 free_overlay_changeset(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200863
Frank Rowand24789c52017-10-17 16:36:26 -0700864out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200865 mutex_unlock(&of_mutex);
866
Frank Rowand24789c52017-10-17 16:36:26 -0700867out:
868 pr_debug("%s() err=%d\n", __func__, ret);
869
Frank Rowand0290c4c2017-10-17 16:36:23 -0700870 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200871}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700872EXPORT_SYMBOL_GPL(of_overlay_remove);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200873
874/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700875 * of_overlay_remove_all() - Reverts and frees all overlay changesets
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200876 *
877 * Removes all overlays from the system in the correct order.
878 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +0200879 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200880 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700881int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200882{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700883 struct overlay_changeset *ovcs, *ovcs_n;
Frank Rowand61b4de42017-10-17 16:36:25 -0700884 int ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200885
886 /* the tail of list is guaranteed to be safe to remove */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700887 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
Frank Rowand24789c52017-10-17 16:36:26 -0700888 ret = of_overlay_remove(&ovcs->id);
Frank Rowand61b4de42017-10-17 16:36:25 -0700889 if (ret)
890 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200891 }
892
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200893 return 0;
894}
Frank Rowand0290c4c2017-10-17 16:36:23 -0700895EXPORT_SYMBOL_GPL(of_overlay_remove_all);