blob: b930e05d121574ada05e7cd6ef516c5abc49ed42 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02002/*
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.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02007 */
Rob Herring606ad422016-06-15 08:32:18 -05008
9#define pr_fmt(fmt) "OF: overlay: " fmt
10
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_device.h>
Frank Rowand39a751a2018-02-12 00:19:42 -080015#include <linux/of_fdt.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020016#include <linux/string.h>
17#include <linux/ctype.h>
18#include <linux/errno.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020019#include <linux/slab.h>
Frank Rowand39a751a2018-02-12 00:19:42 -080020#include <linux/libfdt.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020021#include <linux/err.h>
Mark Brown0d1886d2015-02-17 11:36:58 +090022#include <linux/idr.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020023
24#include "of_private.h"
25
26/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070027 * struct fragment - info about fragment nodes in overlay expanded device tree
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020028 * @target: target of the overlay operation
Frank Rowand0290c4c2017-10-17 16:36:23 -070029 * @overlay: pointer to the __overlay__ node
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020030 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070031struct fragment {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020032 struct device_node *target;
33 struct device_node *overlay;
34};
35
36/**
Frank Rowand0290c4c2017-10-17 16:36:23 -070037 * struct overlay_changeset
Frank Rowand39a751a2018-02-12 00:19:42 -080038 * @id: changeset identifier
Frank Rowand3912b792017-10-17 16:36:30 -070039 * @ovcs_list: list on which we are located
Frank Rowand39a751a2018-02-12 00:19:42 -080040 * @fdt: FDT that was unflattened to create @overlay_tree
Frank Rowande0a58f32017-10-17 16:36:31 -070041 * @overlay_tree: expanded device tree that contains the fragment nodes
Frank Rowand3912b792017-10-17 16:36:30 -070042 * @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
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020046 */
Frank Rowand0290c4c2017-10-17 16:36:23 -070047struct overlay_changeset {
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020048 int id;
Frank Rowand0290c4c2017-10-17 16:36:23 -070049 struct list_head ovcs_list;
Frank Rowand39a751a2018-02-12 00:19:42 -080050 const void *fdt;
Frank Rowande0a58f32017-10-17 16:36:31 -070051 struct device_node *overlay_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020052 int count;
Frank Rowand0290c4c2017-10-17 16:36:23 -070053 struct fragment *fragments;
Frank Rowand3912b792017-10-17 16:36:30 -070054 bool symbols_fragment;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020055 struct of_changeset cset;
56};
57
Frank Rowand24789c52017-10-17 16:36:26 -070058/* 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
Frank Rowand0290c4c2017-10-17 16:36:23 -070074static int build_changeset_next_level(struct overlay_changeset *ovcs,
75 struct device_node *target_node,
Frank Rowand3912b792017-10-17 16:36:30 -070076 const struct device_node *overlay_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020077
Frank Rowandf948d6d2017-10-17 16:36:29 -070078/*
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
Frank Rowand61b4de42017-10-17 16:36:25 -0700100static LIST_HEAD(ovcs_list);
101static DEFINE_IDR(ovcs_idr);
102
Frank Rowand0290c4c2017-10-17 16:36:23 -0700103static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
Alan Tull39a842e2016-11-01 14:14:22 -0500104
105int of_overlay_notifier_register(struct notifier_block *nb)
106{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700107 return blocking_notifier_chain_register(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500108}
109EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
110
111int of_overlay_notifier_unregister(struct notifier_block *nb)
112{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700113 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
Alan Tull39a842e2016-11-01 14:14:22 -0500114}
115EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
116
Frank Rowand24789c52017-10-17 16:36:26 -0700117static char *of_overlay_action_name[] = {
118 "pre-apply",
119 "post-apply",
120 "pre-remove",
121 "post-remove",
122};
123
Frank Rowand0290c4c2017-10-17 16:36:23 -0700124static int overlay_notify(struct overlay_changeset *ovcs,
125 enum of_overlay_notify_action action)
Alan Tull39a842e2016-11-01 14:14:22 -0500126{
127 struct of_overlay_notify_data nd;
128 int i, ret;
129
Frank Rowand0290c4c2017-10-17 16:36:23 -0700130 for (i = 0; i < ovcs->count; i++) {
131 struct fragment *fragment = &ovcs->fragments[i];
Alan Tull39a842e2016-11-01 14:14:22 -0500132
Frank Rowand0290c4c2017-10-17 16:36:23 -0700133 nd.target = fragment->target;
134 nd.overlay = fragment->overlay;
Alan Tull39a842e2016-11-01 14:14:22 -0500135
Frank Rowand0290c4c2017-10-17 16:36:23 -0700136 ret = blocking_notifier_call_chain(&overlay_notify_chain,
Alan Tull39a842e2016-11-01 14:14:22 -0500137 action, &nd);
Frank Rowanda1d19bd2017-10-19 14:18:27 -0700138 if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
Frank Rowand24789c52017-10-17 16:36:26 -0700139 return 0;
140 if (ret) {
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);
144 return ret;
145 }
Alan Tull39a842e2016-11-01 14:14:22 -0500146 }
147
148 return 0;
149}
150
Frank Rowand42b2e942017-10-17 16:36:24 -0700151/*
Frank Rowande0a58f32017-10-17 16:36:31 -0700152 * 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.
Frank Rowand42b2e942017-10-17 16:36:24 -0700155 *
Frank Rowande0a58f32017-10-17 16:36:31 -0700156 * 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.
Frank Rowand42b2e942017-10-17 16:36:24 -0700159 *
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.
163 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700164static struct property *dup_and_fixup_symbol_prop(
165 struct overlay_changeset *ovcs, const struct property *prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200166{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700167 struct fragment *fragment;
Frank Rowande0a58f32017-10-17 16:36:31 -0700168 struct property *new_prop;
169 struct device_node *fragment_node;
170 struct device_node *overlay_node;
171 const char *path;
172 const char *path_tail;
Frank Rowandd1651b02017-07-19 09:25:22 -0700173 const char *target_path;
174 int k;
Frank Rowandd1651b02017-07-19 09:25:22 -0700175 int overlay_name_len;
Frank Rowande0a58f32017-10-17 16:36:31 -0700176 int path_len;
177 int path_tail_len;
Frank Rowandd1651b02017-07-19 09:25:22 -0700178 int target_path_len;
179
180 if (!prop->value)
181 return NULL;
Frank Rowande0a58f32017-10-17 16:36:31 -0700182 if (strnlen(prop->value, prop->length) >= prop->length)
Frank Rowandd1651b02017-07-19 09:25:22 -0700183 return NULL;
Frank Rowande0a58f32017-10-17 16:36:31 -0700184 path = prop->value;
185 path_len = strlen(path);
186
187 if (path_len < 1)
188 return NULL;
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);
Frank Rowandd1651b02017-07-19 09:25:22 -0700193
Frank Rowand0290c4c2017-10-17 16:36:23 -0700194 for (k = 0; k < ovcs->count; k++) {
195 fragment = &ovcs->fragments[k];
Frank Rowande0a58f32017-10-17 16:36:31 -0700196 if (fragment->overlay == overlay_node)
Frank Rowandd1651b02017-07-19 09:25:22 -0700197 break;
198 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700199 if (k >= ovcs->count)
Frank Rowande0a58f32017-10-17 16:36:31 -0700200 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700201
Frank Rowande0a58f32017-10-17 16:36:31 -0700202 overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
203
204 if (overlay_name_len > path_len)
205 return NULL;
206 path_tail = path + overlay_name_len;
207 path_tail_len = strlen(path_tail);
208
209 target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
210 if (!target_path)
211 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700212 target_path_len = strlen(target_path);
213
Frank Rowande0a58f32017-10-17 16:36:31 -0700214 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
215 if (!new_prop)
216 goto err_free_target_path;
Frank Rowandd1651b02017-07-19 09:25:22 -0700217
Frank Rowande0a58f32017-10-17 16:36:31 -0700218 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;
Frank Rowandd1651b02017-07-19 09:25:22 -0700223
Frank Rowande0a58f32017-10-17 16:36:31 -0700224 strcpy(new_prop->value, target_path);
225 strcpy(new_prop->value + target_path_len, path_tail);
Frank Rowandd1651b02017-07-19 09:25:22 -0700226
Frank Rowande0a58f32017-10-17 16:36:31 -0700227 of_property_set_flag(new_prop, OF_DYNAMIC);
Frank Rowandd1651b02017-07-19 09:25:22 -0700228
Frank Rowande0a58f32017-10-17 16:36:31 -0700229 return new_prop;
Frank Rowandd1651b02017-07-19 09:25:22 -0700230
Frank Rowande0a58f32017-10-17 16:36:31 -0700231err_free_new_prop:
232 kfree(new_prop->name);
233 kfree(new_prop->value);
234 kfree(new_prop);
235err_free_target_path:
236 kfree(target_path);
Frank Rowandd1651b02017-07-19 09:25:22 -0700237
Frank Rowandd1651b02017-07-19 09:25:22 -0700238 return NULL;
Frank Rowandd1651b02017-07-19 09:25:22 -0700239}
240
Frank Rowand0290c4c2017-10-17 16:36:23 -0700241/**
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
Frank Rowand3912b792017-10-17 16:36:30 -0700246 * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
Frank Rowand0290c4c2017-10-17 16:36:23 -0700247 *
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.
251 *
Frank Rowand646afc42017-10-17 16:36:21 -0700252 * Some special properties are not updated (no error returned).
Frank Rowand0290c4c2017-10-17 16:36:23 -0700253 *
Frank Rowand646afc42017-10-17 16:36:21 -0700254 * Update of property in symbols node is not allowed.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700255 *
256 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
257 * invalid @overlay.
Frank Rowand646afc42017-10-17 16:36:21 -0700258 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700259static int add_changeset_property(struct overlay_changeset *ovcs,
260 struct device_node *target_node,
261 struct property *overlay_prop,
Frank Rowand3912b792017-10-17 16:36:30 -0700262 bool is_symbols_prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700263{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700264 struct property *new_prop = NULL, *prop;
Lixin Wangac0f3e32017-10-16 17:54:32 +0800265 int ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200266
Frank Rowand0290c4c2017-10-17 16:36:23 -0700267 prop = of_find_property(target_node, overlay_prop->name, NULL);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200268
Frank Rowand0290c4c2017-10-17 16:36:23 -0700269 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"))
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200272 return 0;
273
Frank Rowand3912b792017-10-17 16:36:30 -0700274 if (is_symbols_prop) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700275 if (prop)
Frank Rowandd1651b02017-07-19 09:25:22 -0700276 return -EINVAL;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700277 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
Frank Rowandd1651b02017-07-19 09:25:22 -0700278 } else {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700279 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
Frank Rowandd1651b02017-07-19 09:25:22 -0700280 }
281
Frank Rowand0290c4c2017-10-17 16:36:23 -0700282 if (!new_prop)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200283 return -ENOMEM;
284
Frank Rowand0290c4c2017-10-17 16:36:23 -0700285 if (!prop)
286 ret = of_changeset_add_property(&ovcs->cset, target_node,
287 new_prop);
Frank Rowand646afc42017-10-17 16:36:21 -0700288 else
Frank Rowand0290c4c2017-10-17 16:36:23 -0700289 ret = of_changeset_update_property(&ovcs->cset, target_node,
290 new_prop);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200291
Lixin Wangac0f3e32017-10-16 17:54:32 +0800292 if (ret) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700293 kfree(new_prop->name);
294 kfree(new_prop->value);
295 kfree(new_prop);
Lixin Wangac0f3e32017-10-16 17:54:32 +0800296 }
297 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200298}
299
Frank Rowand0290c4c2017-10-17 16:36:23 -0700300/**
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
305 *
306 * If @node does not already exist in @target_node, add changeset entry
307 * to add @node in @target_node.
308 *
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.
311 *
312 * If @node has child nodes, add the children recursively via
313 * build_changeset_next_level().
314 *
315 * NOTE: Multiple mods of created nodes not supported.
Frank Rowand24789c52017-10-17 16:36:26 -0700316 * If more than one fragment contains a node that does not already exist
317 * in the live tree, then for each fragment of_changeset_attach_node()
318 * will add a changeset entry to add the node. When the changeset is
319 * applied, __of_attach_node() will attach the node twice (once for
320 * each fragment). At this point the device tree will be corrupted.
321 *
322 * TODO: add integrity check to ensure that multiple fragments do not
323 * create the same node.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700324 *
325 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
326 * invalid @overlay.
327 */
328static int add_changeset_node(struct overlay_changeset *ovcs,
329 struct device_node *target_node, struct device_node *node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200330{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700331 const char *node_kbasename;
Fabio Estevamd3a89162015-03-03 10:04:45 -0300332 struct device_node *tchild;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200333 int ret = 0;
334
Frank Rowand0290c4c2017-10-17 16:36:23 -0700335 node_kbasename = kbasename(node->full_name);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200336
Frank Rowand0290c4c2017-10-17 16:36:23 -0700337 for_each_child_of_node(target_node, tchild)
338 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
Frank Rowandc1cd1e02017-07-19 09:25:21 -0700339 break;
340
Frank Rowand61b4de42017-10-17 16:36:25 -0700341 if (!tchild) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700342 tchild = __of_node_dup(node, "%pOF/%s",
343 target_node, node_kbasename);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200344 if (!tchild)
345 return -ENOMEM;
346
Frank Rowand0290c4c2017-10-17 16:36:23 -0700347 tchild->parent = target_node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200348
Frank Rowand0290c4c2017-10-17 16:36:23 -0700349 ret = of_changeset_attach_node(&ovcs->cset, tchild);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200350 if (ret)
351 return ret;
352
Frank Rowand3912b792017-10-17 16:36:30 -0700353 return build_changeset_next_level(ovcs, tchild, node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200354 }
355
Frank Rowand6d0f5472017-10-17 16:36:28 -0700356 if (node->phandle && tchild->phandle)
357 ret = -EINVAL;
358 else
Frank Rowand3912b792017-10-17 16:36:30 -0700359 ret = build_changeset_next_level(ovcs, tchild, node);
Frank Rowand61b4de42017-10-17 16:36:25 -0700360 of_node_put(tchild);
361
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200362 return ret;
363}
364
Frank Rowand0290c4c2017-10-17 16:36:23 -0700365/**
366 * build_changeset_next_level() - add level of overlay changeset
367 * @ovcs: overlay changeset
368 * @target_node: where to place @overlay_node in live tree
369 * @overlay_node: node from within an overlay device tree fragment
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200370 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700371 * Add the properties (if any) and nodes (if any) from @overlay_node to the
372 * @ovcs->cset changeset. If an added node has child nodes, they will
373 * be added recursively.
Frank Rowand646afc42017-10-17 16:36:21 -0700374 *
375 * Do not allow symbols node to have any children.
Frank Rowand0290c4c2017-10-17 16:36:23 -0700376 *
377 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
378 * invalid @overlay_node.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200379 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700380static int build_changeset_next_level(struct overlay_changeset *ovcs,
381 struct device_node *target_node,
Frank Rowand3912b792017-10-17 16:36:30 -0700382 const struct device_node *overlay_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200383{
384 struct device_node *child;
385 struct property *prop;
386 int ret;
387
Frank Rowand0290c4c2017-10-17 16:36:23 -0700388 for_each_property_of_node(overlay_node, prop) {
Frank Rowand3912b792017-10-17 16:36:30 -0700389 ret = add_changeset_property(ovcs, target_node, prop, 0);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200390 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700391 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
392 target_node, prop->name, ret);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200393 return ret;
394 }
395 }
396
Frank Rowand0290c4c2017-10-17 16:36:23 -0700397 for_each_child_of_node(overlay_node, child) {
398 ret = add_changeset_node(ovcs, target_node, child);
Frank Rowandbbed8792017-10-17 16:36:22 -0700399 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700400 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
401 target_node, child->name, ret);
Julia Lawall001cf502015-10-22 11:02:48 +0200402 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200403 return ret;
404 }
405 }
406
407 return 0;
408}
409
Frank Rowand3912b792017-10-17 16:36:30 -0700410/*
411 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
412 */
413static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
414 struct device_node *target_node,
415 const struct device_node *overlay_symbols_node)
416{
417 struct property *prop;
418 int ret;
419
420 for_each_property_of_node(overlay_symbols_node, prop) {
421 ret = add_changeset_property(ovcs, target_node, prop, 1);
422 if (ret) {
423 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
424 target_node, prop->name, ret);
425 return ret;
426 }
427 }
428
429 return 0;
430}
431
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200432/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700433 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
434 * @ovcs: Overlay changeset
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200435 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700436 * Create changeset @ovcs->cset to contain the nodes and properties of the
437 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
438 * any portions of the changeset that were successfully created will remain
439 * in @ovcs->cset.
440 *
441 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
442 * invalid overlay in @ovcs->fragments[].
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200443 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700444static int build_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200445{
Frank Rowand3912b792017-10-17 16:36:30 -0700446 struct fragment *fragment;
447 int fragments_count, i, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200448
Frank Rowand3912b792017-10-17 16:36:30 -0700449 /*
450 * if there is a symbols fragment in ovcs->fragments[i] it is
451 * the final element in the array
452 */
453 if (ovcs->symbols_fragment)
454 fragments_count = ovcs->count - 1;
455 else
456 fragments_count = ovcs->count;
457
458 for (i = 0; i < fragments_count; i++) {
459 fragment = &ovcs->fragments[i];
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200460
Frank Rowand0290c4c2017-10-17 16:36:23 -0700461 ret = build_changeset_next_level(ovcs, fragment->target,
Frank Rowand3912b792017-10-17 16:36:30 -0700462 fragment->overlay);
463 if (ret) {
464 pr_debug("apply failed '%pOF'\n", fragment->target);
465 return ret;
466 }
467 }
468
469 if (ovcs->symbols_fragment) {
470 fragment = &ovcs->fragments[ovcs->count - 1];
471 ret = build_changeset_symbols_node(ovcs, fragment->target,
472 fragment->overlay);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700473 if (ret) {
Frank Rowand24789c52017-10-17 16:36:26 -0700474 pr_debug("apply failed '%pOF'\n", fragment->target);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700475 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200476 }
477 }
478
479 return 0;
480}
481
482/*
483 * Find the target node using a number of different strategies
Frank Rowand646afc42017-10-17 16:36:21 -0700484 * in order of preference:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200485 *
Frank Rowand646afc42017-10-17 16:36:21 -0700486 * 1) "target" property containing the phandle of the target
487 * 2) "target-path" property containing the path of the target
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200488 */
489static struct device_node *find_target_node(struct device_node *info_node)
490{
Frank Rowande547c002018-02-12 00:25:04 -0800491 struct device_node *node;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200492 const char *path;
493 u32 val;
494 int ret;
495
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200496 ret = of_property_read_u32(info_node, "target", &val);
Frank Rowande547c002018-02-12 00:25:04 -0800497 if (!ret) {
498 node = of_find_node_by_phandle(val);
499 if (!node)
500 pr_err("find target, node: %pOF, phandle 0x%x not found\n",
501 info_node, val);
502 return node;
503 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200504
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200505 ret = of_property_read_string(info_node, "target-path", &path);
Frank Rowande547c002018-02-12 00:25:04 -0800506 if (!ret) {
507 node = of_find_node_by_path(path);
508 if (!node)
509 pr_err("find target, node: %pOF, path '%s' not found\n",
510 info_node, path);
511 return node;
512 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200513
Frank Rowande547c002018-02-12 00:25:04 -0800514 pr_err("find target, node: %pOF, no target property\n", info_node);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200515
516 return NULL;
517}
518
519/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700520 * init_overlay_changeset() - initialize overlay changeset from overlay tree
Frank Rowand39a751a2018-02-12 00:19:42 -0800521 * @ovcs: Overlay changeset to build
522 * @fdt: the FDT that was unflattened to create @tree
Frank Rowand0290c4c2017-10-17 16:36:23 -0700523 * @tree: Contains all the overlay fragments and overlay fixup nodes
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200524 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700525 * Initialize @ovcs. Populate @ovcs->fragments with node information from
526 * the top level of @tree. The relevant top level nodes are the fragment
527 * nodes and the __symbols__ node. Any other top level node will be ignored.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200528 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700529 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
Frank Rowand61b4de42017-10-17 16:36:25 -0700530 * detected in @tree, or -ENOSPC if idr_alloc() error.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200531 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700532static int init_overlay_changeset(struct overlay_changeset *ovcs,
Frank Rowand39a751a2018-02-12 00:19:42 -0800533 const void *fdt, struct device_node *tree)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200534{
Frank Rowand61b4de42017-10-17 16:36:25 -0700535 struct device_node *node, *overlay_node;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700536 struct fragment *fragment;
537 struct fragment *fragments;
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100538 int cnt, id, ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200539
Frank Rowand24789c52017-10-17 16:36:26 -0700540 /*
541 * Warn for some issues. Can not return -EINVAL for these until
542 * of_unittest_apply_overlay() is fixed to pass these checks.
543 */
544 if (!of_node_check_flag(tree, OF_DYNAMIC))
545 pr_debug("%s() tree is not dynamic\n", __func__);
546
547 if (!of_node_check_flag(tree, OF_DETACHED))
548 pr_debug("%s() tree is not detached\n", __func__);
549
550 if (!of_node_is_root(tree))
551 pr_debug("%s() tree is not root\n", __func__);
552
Frank Rowande0a58f32017-10-17 16:36:31 -0700553 ovcs->overlay_tree = tree;
Frank Rowand39a751a2018-02-12 00:19:42 -0800554 ovcs->fdt = fdt;
Frank Rowande0a58f32017-10-17 16:36:31 -0700555
Frank Rowand61b4de42017-10-17 16:36:25 -0700556 INIT_LIST_HEAD(&ovcs->ovcs_list);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200557
Frank Rowand61b4de42017-10-17 16:36:25 -0700558 of_changeset_init(&ovcs->cset);
559
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100560 id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
561 if (id <= 0)
562 return id;
Frank Rowand61b4de42017-10-17 16:36:25 -0700563
564 cnt = 0;
565
566 /* fragment nodes */
567 for_each_child_of_node(tree, node) {
568 overlay_node = of_get_child_by_name(node, "__overlay__");
569 if (overlay_node) {
570 cnt++;
571 of_node_put(overlay_node);
572 }
573 }
574
575 node = of_get_child_by_name(tree, "__symbols__");
576 if (node) {
Frank Rowandd1651b02017-07-19 09:25:22 -0700577 cnt++;
Frank Rowand61b4de42017-10-17 16:36:25 -0700578 of_node_put(node);
579 }
Frank Rowandd1651b02017-07-19 09:25:22 -0700580
Frank Rowand0290c4c2017-10-17 16:36:23 -0700581 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
Frank Rowand61b4de42017-10-17 16:36:25 -0700582 if (!fragments) {
583 ret = -ENOMEM;
584 goto err_free_idr;
585 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200586
587 cnt = 0;
588 for_each_child_of_node(tree, node) {
Geert Uytterhoeven35e691e2017-12-08 14:13:02 +0100589 overlay_node = of_get_child_by_name(node, "__overlay__");
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100590 if (!overlay_node)
591 continue;
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100592
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100593 fragment = &fragments[cnt];
594 fragment->overlay = overlay_node;
595 fragment->target = find_target_node(node);
596 if (!fragment->target) {
597 of_node_put(fragment->overlay);
598 ret = -EINVAL;
599 goto err_free_fragments;
Frank Rowand61b4de42017-10-17 16:36:25 -0700600 }
Geert Uytterhoeven589b7542017-12-08 14:13:03 +0100601
602 cnt++;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200603 }
604
Frank Rowand3912b792017-10-17 16:36:30 -0700605 /*
606 * if there is a symbols fragment in ovcs->fragments[i] it is
607 * the final element in the array
608 */
Frank Rowandd1651b02017-07-19 09:25:22 -0700609 node = of_get_child_by_name(tree, "__symbols__");
610 if (node) {
Frank Rowand3912b792017-10-17 16:36:30 -0700611 ovcs->symbols_fragment = 1;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700612 fragment = &fragments[cnt];
613 fragment->overlay = node;
614 fragment->target = of_find_node_by_path("/__symbols__");
Frank Rowandd1651b02017-07-19 09:25:22 -0700615
Frank Rowand0290c4c2017-10-17 16:36:23 -0700616 if (!fragment->target) {
Frank Rowand4ee7c0d2017-10-19 14:38:11 -0700617 pr_err("symbols in overlay, but not in live tree\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700618 ret = -EINVAL;
619 goto err_free_fragments;
Frank Rowandd1651b02017-07-19 09:25:22 -0700620 }
621
622 cnt++;
623 }
624
Frank Rowandbbed8792017-10-17 16:36:22 -0700625 if (!cnt) {
Frank Rowand39a751a2018-02-12 00:19:42 -0800626 pr_err("no fragments or symbols in overlay\n");
Frank Rowand61b4de42017-10-17 16:36:25 -0700627 ret = -EINVAL;
628 goto err_free_fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200629 }
630
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100631 ovcs->id = id;
Frank Rowand0290c4c2017-10-17 16:36:23 -0700632 ovcs->count = cnt;
633 ovcs->fragments = fragments;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200634
635 return 0;
Frank Rowand61b4de42017-10-17 16:36:25 -0700636
Frank Rowand61b4de42017-10-17 16:36:25 -0700637err_free_fragments:
638 kfree(fragments);
639err_free_idr:
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100640 idr_remove(&ovcs_idr, id);
Frank Rowand61b4de42017-10-17 16:36:25 -0700641
Frank Rowand24789c52017-10-17 16:36:26 -0700642 pr_err("%s() failed, ret = %d\n", __func__, ret);
643
Frank Rowand61b4de42017-10-17 16:36:25 -0700644 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200645}
646
Frank Rowand61b4de42017-10-17 16:36:25 -0700647static void free_overlay_changeset(struct overlay_changeset *ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200648{
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200649 int i;
650
Geert Uytterhoeven1352f092017-12-05 16:27:02 +0100651 if (ovcs->cset.entries.next)
652 of_changeset_destroy(&ovcs->cset);
Frank Rowand61b4de42017-10-17 16:36:25 -0700653
654 if (ovcs->id)
655 idr_remove(&ovcs_idr, ovcs->id);
656
657 for (i = 0; i < ovcs->count; i++) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700658 of_node_put(ovcs->fragments[i].target);
659 of_node_put(ovcs->fragments[i].overlay);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200660 }
Frank Rowand0290c4c2017-10-17 16:36:23 -0700661 kfree(ovcs->fragments);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200662
Frank Rowand39a751a2018-02-12 00:19:42 -0800663 /*
664 * TODO
665 *
666 * would like to: kfree(ovcs->overlay_tree);
667 * but can not since drivers may have pointers into this data
668 *
669 * would like to: kfree(ovcs->fdt);
670 * but can not since drivers may have pointers into this data
671 */
672
Frank Rowand61b4de42017-10-17 16:36:25 -0700673 kfree(ovcs);
674}
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200675
Frank Rowand39a751a2018-02-12 00:19:42 -0800676/*
677 * internal documentation
678 *
Frank Rowand0290c4c2017-10-17 16:36:23 -0700679 * of_overlay_apply() - Create and apply an overlay changeset
Frank Rowand39a751a2018-02-12 00:19:42 -0800680 * @fdt: the FDT that was unflattened to create @tree
Frank Rowand0290c4c2017-10-17 16:36:23 -0700681 * @tree: Expanded overlay device tree
Frank Rowand24789c52017-10-17 16:36:26 -0700682 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200683 *
Frank Rowand24789c52017-10-17 16:36:26 -0700684 * Creates and applies an overlay changeset.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200685 *
Frank Rowand24789c52017-10-17 16:36:26 -0700686 * If an error occurs in a pre-apply notifier, then no changes are made
687 * to the device tree.
688 *
689
690 * A non-zero return value will not have created the changeset if error is from:
691 * - parameter checks
692 * - building the changeset
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100693 * - overlay changeset pre-apply notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700694 *
695 * If an error is returned by an overlay changeset pre-apply notifier
696 * then no further overlay changeset pre-apply notifier will be called.
697 *
698 * A non-zero return value will have created the changeset if error is from:
699 * - overlay changeset entry notifier
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100700 * - overlay changeset post-apply notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700701 *
702 * If an error is returned by an overlay changeset post-apply notifier
703 * then no further overlay changeset post-apply notifier will be called.
704 *
705 * If more than one notifier returns an error, then the last notifier
706 * error to occur is returned.
707 *
708 * If an error occurred while applying the overlay changeset, then an
709 * attempt is made to revert any changes that were made to the
710 * device tree. If there were any errors during the revert attempt
711 * then the state of the device tree can not be determined, and any
712 * following attempt to apply or remove an overlay changeset will be
713 * refused.
714 *
715 * Returns 0 on success, or a negative error number. Overlay changeset
716 * id is returned to *ovcs_id.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200717 */
Frank Rowand24789c52017-10-17 16:36:26 -0700718
Frank Rowand39a751a2018-02-12 00:19:42 -0800719static int of_overlay_apply(const void *fdt, struct device_node *tree,
720 int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200721{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700722 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700723 int ret = 0, ret_revert, ret_tmp;
724
Frank Rowand39a751a2018-02-12 00:19:42 -0800725 /*
726 * As of this point, fdt and tree belong to the overlay changeset.
727 * overlay changeset code is responsible for freeing them.
728 */
Frank Rowand24789c52017-10-17 16:36:26 -0700729
730 if (devicetree_corrupt()) {
731 pr_err("devicetree state suspect, refuse to apply overlay\n");
Frank Rowand39a751a2018-02-12 00:19:42 -0800732 kfree(fdt);
733 kfree(tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700734 ret = -EBUSY;
735 goto out;
736 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200737
Frank Rowand0290c4c2017-10-17 16:36:23 -0700738 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
Frank Rowand24789c52017-10-17 16:36:26 -0700739 if (!ovcs) {
Frank Rowand39a751a2018-02-12 00:19:42 -0800740 kfree(fdt);
741 kfree(tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700742 ret = -ENOMEM;
743 goto out;
744 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200745
Frank Rowandf948d6d2017-10-17 16:36:29 -0700746 of_overlay_mutex_lock();
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100747 mutex_lock(&of_mutex);
Frank Rowandf948d6d2017-10-17 16:36:29 -0700748
749 ret = of_resolve_phandles(tree);
750 if (ret)
Frank Rowand39a751a2018-02-12 00:19:42 -0800751 goto err_free_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200752
Frank Rowand39a751a2018-02-12 00:19:42 -0800753 ret = init_overlay_changeset(ovcs, fdt, tree);
Frank Rowand24789c52017-10-17 16:36:26 -0700754 if (ret)
Frank Rowand39a751a2018-02-12 00:19:42 -0800755 goto err_free_tree;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200756
Frank Rowand39a751a2018-02-12 00:19:42 -0800757 /*
758 * after overlay_notify(), ovcs->overlay_tree related pointers may have
759 * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
760 * and can not free fdt, aka ovcs->fdt
761 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700762 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
Frank Rowand24789c52017-10-17 16:36:26 -0700763 if (ret) {
764 pr_err("overlay changeset pre-apply notify error %d\n", ret);
Frank Rowand61b4de42017-10-17 16:36:25 -0700765 goto err_free_overlay_changeset;
Alan Tull39a842e2016-11-01 14:14:22 -0500766 }
767
Frank Rowand0290c4c2017-10-17 16:36:23 -0700768 ret = build_changeset(ovcs);
769 if (ret)
Frank Rowand61b4de42017-10-17 16:36:25 -0700770 goto err_free_overlay_changeset;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200771
Frank Rowand24789c52017-10-17 16:36:26 -0700772 ret_revert = 0;
773 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
774 if (ret) {
775 if (ret_revert) {
776 pr_debug("overlay changeset revert error %d\n",
777 ret_revert);
778 devicetree_state_flags |= DTSF_APPLY_FAIL;
779 }
Frank Rowand61b4de42017-10-17 16:36:25 -0700780 goto err_free_overlay_changeset;
Frank Rowand24789c52017-10-17 16:36:26 -0700781 }
Rob Herring606ad422016-06-15 08:32:18 -0500782
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +0100783 ret = __of_changeset_apply_notify(&ovcs->cset);
784 if (ret)
785 pr_err("overlay changeset entry notify error %d\n", ret);
786 /* notify failure is not fatal, continue */
787
Frank Rowand0290c4c2017-10-17 16:36:23 -0700788 list_add_tail(&ovcs->ovcs_list, &ovcs_list);
Frank Rowand24789c52017-10-17 16:36:26 -0700789 *ovcs_id = ovcs->id;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200790
Frank Rowand24789c52017-10-17 16:36:26 -0700791 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
792 if (ret_tmp) {
793 pr_err("overlay changeset post-apply notify error %d\n",
794 ret_tmp);
795 if (!ret)
796 ret = ret_tmp;
797 }
Alan Tull39a842e2016-11-01 14:14:22 -0500798
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100799 goto out_unlock;
Frank Rowandf948d6d2017-10-17 16:36:29 -0700800
Frank Rowand39a751a2018-02-12 00:19:42 -0800801err_free_tree:
802 kfree(fdt);
803 kfree(tree);
804
Frank Rowand61b4de42017-10-17 16:36:25 -0700805err_free_overlay_changeset:
806 free_overlay_changeset(ovcs);
807
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100808out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200809 mutex_unlock(&of_mutex);
Geert Uytterhoeven5e474812017-12-05 16:27:03 +0100810 of_overlay_mutex_unlock();
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200811
Frank Rowand24789c52017-10-17 16:36:26 -0700812out:
813 pr_debug("%s() err=%d\n", __func__, ret);
814
Frank Rowand0290c4c2017-10-17 16:36:23 -0700815 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200816}
Frank Rowand39a751a2018-02-12 00:19:42 -0800817
818int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
819 int *ovcs_id)
820{
821 const void *new_fdt;
822 int ret;
823 u32 size;
824 struct device_node *overlay_root;
825
826 *ovcs_id = 0;
827 ret = 0;
828
829 if (overlay_fdt_size < sizeof(struct fdt_header) ||
830 fdt_check_header(overlay_fdt)) {
831 pr_err("Invalid overlay_fdt header\n");
832 return -EINVAL;
833 }
834
835 size = fdt_totalsize(overlay_fdt);
836 if (overlay_fdt_size < size)
837 return -EINVAL;
838
839 /*
840 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
841 * will create pointers to the passed in FDT in the unflattened tree.
842 */
843 new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
844 if (!new_fdt)
845 return -ENOMEM;
846
847 of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
848 if (!overlay_root) {
849 pr_err("unable to unflatten overlay_fdt\n");
850 ret = -EINVAL;
851 goto out_free_new_fdt;
852 }
853
854 ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
855 if (ret < 0) {
856 /*
857 * new_fdt and overlay_root now belong to the overlay
858 * changeset.
859 * overlay changeset code is responsible for freeing them.
860 */
861 goto out;
862 }
863
864 return 0;
865
866
867out_free_new_fdt:
868 kfree(new_fdt);
869
870out:
871 return ret;
872}
873EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200874
Frank Rowand646afc42017-10-17 16:36:21 -0700875/*
Frank Rowand0290c4c2017-10-17 16:36:23 -0700876 * Find @np in @tree.
877 *
878 * Returns 1 if @np is @tree or is contained in @tree, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700879 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700880static int find_node(struct device_node *tree, struct device_node *np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200881{
882 struct device_node *child;
883
Frank Rowand0290c4c2017-10-17 16:36:23 -0700884 if (tree == np)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200885 return 1;
886
887 for_each_child_of_node(tree, child) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700888 if (find_node(child, np)) {
Julia Lawall001cf502015-10-22 11:02:48 +0200889 of_node_put(child);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200890 return 1;
Julia Lawall001cf502015-10-22 11:02:48 +0200891 }
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200892 }
893
894 return 0;
895}
896
Frank Rowand646afc42017-10-17 16:36:21 -0700897/*
Frank Rowand87f242c2017-10-17 16:36:27 -0700898 * Is @remove_ce_node a child of, a parent of, or the same as any
Frank Rowand0290c4c2017-10-17 16:36:23 -0700899 * node in an overlay changeset more topmost than @remove_ovcs?
900 *
901 * Returns 1 if found, else 0
Frank Rowand646afc42017-10-17 16:36:21 -0700902 */
Frank Rowand87f242c2017-10-17 16:36:27 -0700903static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
904 struct device_node *remove_ce_node)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200905{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700906 struct overlay_changeset *ovcs;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200907 struct of_changeset_entry *ce;
908
Frank Rowand0290c4c2017-10-17 16:36:23 -0700909 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
910 if (ovcs == remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200911 break;
912
Frank Rowand0290c4c2017-10-17 16:36:23 -0700913 list_for_each_entry(ce, &ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700914 if (find_node(ce->np, remove_ce_node)) {
915 pr_err("%s: #%d overlaps with #%d @%pOF\n",
Frank Rowand0290c4c2017-10-17 16:36:23 -0700916 __func__, remove_ovcs->id, ovcs->id,
Frank Rowand87f242c2017-10-17 16:36:27 -0700917 remove_ce_node);
918 return 1;
919 }
920 if (find_node(remove_ce_node, ce->np)) {
921 pr_err("%s: #%d overlaps with #%d @%pOF\n",
922 __func__, remove_ovcs->id, ovcs->id,
923 remove_ce_node);
Frank Rowand0290c4c2017-10-17 16:36:23 -0700924 return 1;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200925 }
926 }
927 }
928
Frank Rowand0290c4c2017-10-17 16:36:23 -0700929 return 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200930}
931
932/*
933 * We can safely remove the overlay only if it's the top-most one.
934 * Newly applied overlays are inserted at the tail of the overlay list,
935 * so a top most overlay is the one that is closest to the tail.
936 *
937 * The topmost check is done by exploiting this property. For each
938 * affected device node in the log list we check if this overlay is
939 * the one closest to the tail. If another overlay has affected this
940 * device node and is closest to the tail, then removal is not permited.
941 */
Frank Rowand0290c4c2017-10-17 16:36:23 -0700942static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200943{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700944 struct of_changeset_entry *remove_ce;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200945
Frank Rowand0290c4c2017-10-17 16:36:23 -0700946 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
Frank Rowand87f242c2017-10-17 16:36:27 -0700947 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
Frank Rowand0290c4c2017-10-17 16:36:23 -0700948 pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200949 return 0;
950 }
951 }
952
953 return 1;
954}
955
956/**
Frank Rowand0290c4c2017-10-17 16:36:23 -0700957 * of_overlay_remove() - Revert and free an overlay changeset
Frank Rowand24789c52017-10-17 16:36:26 -0700958 * @ovcs_id: Pointer to overlay changeset id
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200959 *
Frank Rowand24789c52017-10-17 16:36:26 -0700960 * Removes an overlay if it is permissible. @ovcs_id was previously returned
Frank Rowand0290c4c2017-10-17 16:36:23 -0700961 * by of_overlay_apply().
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200962 *
Frank Rowand24789c52017-10-17 16:36:26 -0700963 * If an error occurred while attempting to revert the overlay changeset,
964 * then an attempt is made to re-apply any changeset entry that was
965 * reverted. If an error occurs on re-apply then the state of the device
966 * tree can not be determined, and any following attempt to apply or remove
967 * an overlay changeset will be refused.
968 *
969 * A non-zero return value will not revert the changeset if error is from:
970 * - parameter checks
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100971 * - overlay changeset pre-remove notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700972 * - overlay changeset entry revert
973 *
974 * If an error is returned by an overlay changeset pre-remove notifier
975 * then no further overlay changeset pre-remove notifier will be called.
976 *
977 * If more than one notifier returns an error, then the last notifier
978 * error to occur is returned.
979 *
980 * A non-zero return value will revert the changeset if error is from:
981 * - overlay changeset entry notifier
Geert Uytterhoevene9d92e42017-11-28 09:25:23 +0100982 * - overlay changeset post-remove notifier
Frank Rowand24789c52017-10-17 16:36:26 -0700983 *
984 * If an error is returned by an overlay changeset post-remove notifier
985 * then no further overlay changeset post-remove notifier will be called.
986 *
987 * Returns 0 on success, or a negative error number. *ovcs_id is set to
988 * zero after reverting the changeset, even if a subsequent error occurs.
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200989 */
Frank Rowand24789c52017-10-17 16:36:26 -0700990int of_overlay_remove(int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200991{
Frank Rowand0290c4c2017-10-17 16:36:23 -0700992 struct overlay_changeset *ovcs;
Frank Rowand24789c52017-10-17 16:36:26 -0700993 int ret, ret_apply, ret_tmp;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200994
Frank Rowand24789c52017-10-17 16:36:26 -0700995 ret = 0;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +0200996
Frank Rowand24789c52017-10-17 16:36:26 -0700997 if (devicetree_corrupt()) {
998 pr_err("suspect devicetree state, refuse to remove overlay\n");
Frank Rowand0290c4c2017-10-17 16:36:23 -0700999 ret = -EBUSY;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001000 goto out;
1001 }
1002
Frank Rowand24789c52017-10-17 16:36:26 -07001003 mutex_lock(&of_mutex);
1004
1005 ovcs = idr_find(&ovcs_idr, *ovcs_id);
1006 if (!ovcs) {
1007 ret = -ENODEV;
1008 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
1009 goto out_unlock;
1010 }
1011
1012 if (!overlay_removal_is_ok(ovcs)) {
1013 ret = -EBUSY;
1014 goto out_unlock;
1015 }
1016
1017 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
1018 if (ret) {
1019 pr_err("overlay changeset pre-remove notify error %d\n", ret);
1020 goto out_unlock;
1021 }
Frank Rowand61b4de42017-10-17 16:36:25 -07001022
Frank Rowand0290c4c2017-10-17 16:36:23 -07001023 list_del(&ovcs->ovcs_list);
Frank Rowand61b4de42017-10-17 16:36:25 -07001024
Frank Rowand24789c52017-10-17 16:36:26 -07001025 ret_apply = 0;
1026 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
1027 if (ret) {
1028 if (ret_apply)
1029 devicetree_state_flags |= DTSF_REVERT_FAIL;
1030 goto out_unlock;
Frank Rowand24789c52017-10-17 16:36:26 -07001031 }
Frank Rowand61b4de42017-10-17 16:36:25 -07001032
Geert Uytterhoeven6de67de2017-11-28 09:26:33 +01001033 ret = __of_changeset_revert_notify(&ovcs->cset);
1034 if (ret)
1035 pr_err("overlay changeset entry notify error %d\n", ret);
1036 /* notify failure is not fatal, continue */
1037
Frank Rowand24789c52017-10-17 16:36:26 -07001038 *ovcs_id = 0;
1039
1040 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
1041 if (ret_tmp) {
1042 pr_err("overlay changeset post-remove notify error %d\n",
1043 ret_tmp);
1044 if (!ret)
1045 ret = ret_tmp;
1046 }
Frank Rowand61b4de42017-10-17 16:36:25 -07001047
1048 free_overlay_changeset(ovcs);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001049
Frank Rowand24789c52017-10-17 16:36:26 -07001050out_unlock:
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001051 mutex_unlock(&of_mutex);
1052
Frank Rowand24789c52017-10-17 16:36:26 -07001053out:
1054 pr_debug("%s() err=%d\n", __func__, ret);
1055
Frank Rowand0290c4c2017-10-17 16:36:23 -07001056 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001057}
Frank Rowand0290c4c2017-10-17 16:36:23 -07001058EXPORT_SYMBOL_GPL(of_overlay_remove);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001059
1060/**
Frank Rowand0290c4c2017-10-17 16:36:23 -07001061 * of_overlay_remove_all() - Reverts and frees all overlay changesets
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001062 *
1063 * Removes all overlays from the system in the correct order.
1064 *
Geert Uytterhoeven94a8bf92015-05-21 14:10:26 +02001065 * Returns 0 on success, or a negative error number
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001066 */
Frank Rowand0290c4c2017-10-17 16:36:23 -07001067int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001068{
Frank Rowand0290c4c2017-10-17 16:36:23 -07001069 struct overlay_changeset *ovcs, *ovcs_n;
Frank Rowand61b4de42017-10-17 16:36:25 -07001070 int ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001071
1072 /* the tail of list is guaranteed to be safe to remove */
Frank Rowand0290c4c2017-10-17 16:36:23 -07001073 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
Frank Rowand24789c52017-10-17 16:36:26 -07001074 ret = of_overlay_remove(&ovcs->id);
Frank Rowand61b4de42017-10-17 16:36:25 -07001075 if (ret)
1076 return ret;
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001077 }
1078
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001079 return 0;
1080}
Frank Rowand0290c4c2017-10-17 16:36:23 -07001081EXPORT_SYMBOL_GPL(of_overlay_remove_all);