Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions for working with device tree overlays |
| 3 | * |
| 4 | * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com> |
| 5 | * Copyright (C) 2012 Texas Instruments Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | */ |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 11 | |
| 12 | #define pr_fmt(fmt) "OF: overlay: " fmt |
| 13 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 14 | #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 Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/err.h> |
Mark Brown | 0d1886d | 2015-02-17 11:36:58 +0900 | [diff] [blame] | 23 | #include <linux/idr.h> |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 24 | |
| 25 | #include "of_private.h" |
| 26 | |
| 27 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 28 | * struct fragment - info about fragment nodes in overlay expanded device tree |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 29 | * @target: target of the overlay operation |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 30 | * @overlay: pointer to the __overlay__ node |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 31 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 32 | struct fragment { |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 33 | struct device_node *target; |
| 34 | struct device_node *overlay; |
| 35 | }; |
| 36 | |
| 37 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 38 | * struct overlay_changeset |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 39 | * @ovcs_list: list on which we are located |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 40 | * @overlay_tree: expanded device tree that contains the fragment nodes |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 41 | * @count: count of fragment structures |
| 42 | * @fragments: fragment nodes in the overlay expanded device tree |
| 43 | * @symbols_fragment: last element of @fragments[] is the __symbols__ node |
| 44 | * @cset: changeset to apply fragments to live device tree |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 45 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 46 | struct overlay_changeset { |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 47 | int id; |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 48 | struct list_head ovcs_list; |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 49 | struct device_node *overlay_tree; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 50 | int count; |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 51 | struct fragment *fragments; |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 52 | bool symbols_fragment; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 53 | struct of_changeset cset; |
| 54 | }; |
| 55 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 56 | /* flags are sticky - once set, do not reset */ |
| 57 | static int devicetree_state_flags; |
| 58 | #define DTSF_APPLY_FAIL 0x01 |
| 59 | #define DTSF_REVERT_FAIL 0x02 |
| 60 | |
| 61 | /* |
| 62 | * If a changeset apply or revert encounters an error, an attempt will |
| 63 | * be made to undo partial changes, but may fail. If the undo fails |
| 64 | * we do not know the state of the devicetree. |
| 65 | */ |
| 66 | static int devicetree_corrupt(void) |
| 67 | { |
| 68 | return devicetree_state_flags & |
| 69 | (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL); |
| 70 | } |
| 71 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 72 | static int build_changeset_next_level(struct overlay_changeset *ovcs, |
| 73 | struct device_node *target_node, |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 74 | const struct device_node *overlay_node); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 75 | |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 76 | /* |
| 77 | * of_resolve_phandles() finds the largest phandle in the live tree. |
| 78 | * of_overlay_apply() may add a larger phandle to the live tree. |
| 79 | * Do not allow race between two overlays being applied simultaneously: |
| 80 | * mutex_lock(&of_overlay_phandle_mutex) |
| 81 | * of_resolve_phandles() |
| 82 | * of_overlay_apply() |
| 83 | * mutex_unlock(&of_overlay_phandle_mutex) |
| 84 | */ |
| 85 | static DEFINE_MUTEX(of_overlay_phandle_mutex); |
| 86 | |
| 87 | void of_overlay_mutex_lock(void) |
| 88 | { |
| 89 | mutex_lock(&of_overlay_phandle_mutex); |
| 90 | } |
| 91 | |
| 92 | void of_overlay_mutex_unlock(void) |
| 93 | { |
| 94 | mutex_unlock(&of_overlay_phandle_mutex); |
| 95 | } |
| 96 | |
| 97 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 98 | static LIST_HEAD(ovcs_list); |
| 99 | static DEFINE_IDR(ovcs_idr); |
| 100 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 101 | static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain); |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 102 | |
| 103 | int of_overlay_notifier_register(struct notifier_block *nb) |
| 104 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 105 | return blocking_notifier_chain_register(&overlay_notify_chain, nb); |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 106 | } |
| 107 | EXPORT_SYMBOL_GPL(of_overlay_notifier_register); |
| 108 | |
| 109 | int of_overlay_notifier_unregister(struct notifier_block *nb) |
| 110 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 111 | return blocking_notifier_chain_unregister(&overlay_notify_chain, nb); |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 112 | } |
| 113 | EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister); |
| 114 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 115 | static char *of_overlay_action_name[] = { |
| 116 | "pre-apply", |
| 117 | "post-apply", |
| 118 | "pre-remove", |
| 119 | "post-remove", |
| 120 | }; |
| 121 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 122 | static int overlay_notify(struct overlay_changeset *ovcs, |
| 123 | enum of_overlay_notify_action action) |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 124 | { |
| 125 | struct of_overlay_notify_data nd; |
| 126 | int i, ret; |
| 127 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 128 | for (i = 0; i < ovcs->count; i++) { |
| 129 | struct fragment *fragment = &ovcs->fragments[i]; |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 130 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 131 | nd.target = fragment->target; |
| 132 | nd.overlay = fragment->overlay; |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 133 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 134 | ret = blocking_notifier_call_chain(&overlay_notify_chain, |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 135 | action, &nd); |
Frank Rowand | a1d19bd | 2017-10-19 14:18:27 -0700 | [diff] [blame] | 136 | if (ret == NOTIFY_OK || ret == NOTIFY_STOP) |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | if (ret) { |
| 139 | ret = notifier_to_errno(ret); |
| 140 | pr_err("overlay changeset %s notifier error %d, target: %pOF\n", |
| 141 | of_overlay_action_name[action], ret, nd.target); |
| 142 | return ret; |
| 143 | } |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Frank Rowand | 42b2e94 | 2017-10-17 16:36:24 -0700 | [diff] [blame] | 149 | /* |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 150 | * The values of properties in the "/__symbols__" node are paths in |
| 151 | * the ovcs->overlay_tree. When duplicating the properties, the paths |
| 152 | * need to be adjusted to be the correct path for the live device tree. |
Frank Rowand | 42b2e94 | 2017-10-17 16:36:24 -0700 | [diff] [blame] | 153 | * |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 154 | * The paths refer to a node in the subtree of a fragment node's "__overlay__" |
| 155 | * node, for example "/fragment@0/__overlay__/symbol_path_tail", |
| 156 | * where symbol_path_tail can be a single node or it may be a multi-node path. |
Frank Rowand | 42b2e94 | 2017-10-17 16:36:24 -0700 | [diff] [blame] | 157 | * |
| 158 | * The duplicated property value will be modified by replacing the |
| 159 | * "/fragment_name/__overlay/" portion of the value with the target |
| 160 | * path from the fragment node. |
| 161 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 162 | static struct property *dup_and_fixup_symbol_prop( |
| 163 | struct overlay_changeset *ovcs, const struct property *prop) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 164 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 165 | struct fragment *fragment; |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 166 | struct property *new_prop; |
| 167 | struct device_node *fragment_node; |
| 168 | struct device_node *overlay_node; |
| 169 | const char *path; |
| 170 | const char *path_tail; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 171 | const char *target_path; |
| 172 | int k; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 173 | int overlay_name_len; |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 174 | int path_len; |
| 175 | int path_tail_len; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 176 | int target_path_len; |
| 177 | |
| 178 | if (!prop->value) |
| 179 | return NULL; |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 180 | if (strnlen(prop->value, prop->length) >= prop->length) |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 181 | return NULL; |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 182 | path = prop->value; |
| 183 | path_len = strlen(path); |
| 184 | |
| 185 | if (path_len < 1) |
| 186 | return NULL; |
| 187 | fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1); |
| 188 | overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/"); |
| 189 | of_node_put(fragment_node); |
| 190 | of_node_put(overlay_node); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 191 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 192 | for (k = 0; k < ovcs->count; k++) { |
| 193 | fragment = &ovcs->fragments[k]; |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 194 | if (fragment->overlay == overlay_node) |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 195 | break; |
| 196 | } |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 197 | if (k >= ovcs->count) |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 198 | return NULL; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 199 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 200 | overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay); |
| 201 | |
| 202 | if (overlay_name_len > path_len) |
| 203 | return NULL; |
| 204 | path_tail = path + overlay_name_len; |
| 205 | path_tail_len = strlen(path_tail); |
| 206 | |
| 207 | target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target); |
| 208 | if (!target_path) |
| 209 | return NULL; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 210 | target_path_len = strlen(target_path); |
| 211 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 212 | new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL); |
| 213 | if (!new_prop) |
| 214 | goto err_free_target_path; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 215 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 216 | new_prop->name = kstrdup(prop->name, GFP_KERNEL); |
| 217 | new_prop->length = target_path_len + path_tail_len + 1; |
| 218 | new_prop->value = kzalloc(new_prop->length, GFP_KERNEL); |
| 219 | if (!new_prop->name || !new_prop->value) |
| 220 | goto err_free_new_prop; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 221 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 222 | strcpy(new_prop->value, target_path); |
| 223 | strcpy(new_prop->value + target_path_len, path_tail); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 224 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 225 | of_property_set_flag(new_prop, OF_DYNAMIC); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 226 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 227 | return new_prop; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 228 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 229 | err_free_new_prop: |
| 230 | kfree(new_prop->name); |
| 231 | kfree(new_prop->value); |
| 232 | kfree(new_prop); |
| 233 | err_free_target_path: |
| 234 | kfree(target_path); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 235 | |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 236 | return NULL; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 239 | /** |
| 240 | * add_changeset_property() - add @overlay_prop to overlay changeset |
| 241 | * @ovcs: overlay changeset |
| 242 | * @target_node: where to place @overlay_prop in live tree |
| 243 | * @overlay_prop: property to add or update, from overlay tree |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 244 | * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__" |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 245 | * |
| 246 | * If @overlay_prop does not already exist in @target_node, add changeset entry |
| 247 | * to add @overlay_prop in @target_node, else add changeset entry to update |
| 248 | * value of @overlay_prop. |
| 249 | * |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 250 | * Some special properties are not updated (no error returned). |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 251 | * |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 252 | * Update of property in symbols node is not allowed. |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 253 | * |
| 254 | * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if |
| 255 | * invalid @overlay. |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 256 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 257 | static int add_changeset_property(struct overlay_changeset *ovcs, |
| 258 | struct device_node *target_node, |
| 259 | struct property *overlay_prop, |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 260 | bool is_symbols_prop) |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 261 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 262 | struct property *new_prop = NULL, *prop; |
Lixin Wang | ac0f3e3 | 2017-10-16 17:54:32 +0800 | [diff] [blame] | 263 | int ret = 0; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 264 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 265 | prop = of_find_property(target_node, overlay_prop->name, NULL); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 266 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 267 | if (!of_prop_cmp(overlay_prop->name, "name") || |
| 268 | !of_prop_cmp(overlay_prop->name, "phandle") || |
| 269 | !of_prop_cmp(overlay_prop->name, "linux,phandle")) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 270 | return 0; |
| 271 | |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 272 | if (is_symbols_prop) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 273 | if (prop) |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 274 | return -EINVAL; |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 275 | new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 276 | } else { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 277 | new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 280 | if (!new_prop) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 281 | return -ENOMEM; |
| 282 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 283 | if (!prop) |
| 284 | ret = of_changeset_add_property(&ovcs->cset, target_node, |
| 285 | new_prop); |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 286 | else |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 287 | ret = of_changeset_update_property(&ovcs->cset, target_node, |
| 288 | new_prop); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 289 | |
Lixin Wang | ac0f3e3 | 2017-10-16 17:54:32 +0800 | [diff] [blame] | 290 | if (ret) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 291 | kfree(new_prop->name); |
| 292 | kfree(new_prop->value); |
| 293 | kfree(new_prop); |
Lixin Wang | ac0f3e3 | 2017-10-16 17:54:32 +0800 | [diff] [blame] | 294 | } |
| 295 | return ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 298 | /** |
| 299 | * add_changeset_node() - add @node (and children) to overlay changeset |
| 300 | * @ovcs: overlay changeset |
| 301 | * @target_node: where to place @node in live tree |
| 302 | * @node: node from within overlay device tree fragment |
| 303 | * |
| 304 | * If @node does not already exist in @target_node, add changeset entry |
| 305 | * to add @node in @target_node. |
| 306 | * |
| 307 | * If @node already exists in @target_node, and the existing node has |
| 308 | * a phandle, the overlay node is not allowed to have a phandle. |
| 309 | * |
| 310 | * If @node has child nodes, add the children recursively via |
| 311 | * build_changeset_next_level(). |
| 312 | * |
| 313 | * NOTE: Multiple mods of created nodes not supported. |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 314 | * If more than one fragment contains a node that does not already exist |
| 315 | * in the live tree, then for each fragment of_changeset_attach_node() |
| 316 | * will add a changeset entry to add the node. When the changeset is |
| 317 | * applied, __of_attach_node() will attach the node twice (once for |
| 318 | * each fragment). At this point the device tree will be corrupted. |
| 319 | * |
| 320 | * TODO: add integrity check to ensure that multiple fragments do not |
| 321 | * create the same node. |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 322 | * |
| 323 | * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if |
| 324 | * invalid @overlay. |
| 325 | */ |
| 326 | static int add_changeset_node(struct overlay_changeset *ovcs, |
| 327 | struct device_node *target_node, struct device_node *node) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 328 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 329 | const char *node_kbasename; |
Fabio Estevam | d3a8916 | 2015-03-03 10:04:45 -0300 | [diff] [blame] | 330 | struct device_node *tchild; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 331 | int ret = 0; |
| 332 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 333 | node_kbasename = kbasename(node->full_name); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 334 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 335 | for_each_child_of_node(target_node, tchild) |
| 336 | if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name))) |
Frank Rowand | c1cd1e0 | 2017-07-19 09:25:21 -0700 | [diff] [blame] | 337 | break; |
| 338 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 339 | if (!tchild) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 340 | tchild = __of_node_dup(node, "%pOF/%s", |
| 341 | target_node, node_kbasename); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 342 | if (!tchild) |
| 343 | return -ENOMEM; |
| 344 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 345 | tchild->parent = target_node; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 346 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 347 | ret = of_changeset_attach_node(&ovcs->cset, tchild); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 348 | if (ret) |
| 349 | return ret; |
| 350 | |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 351 | return build_changeset_next_level(ovcs, tchild, node); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Frank Rowand | 6d0f547 | 2017-10-17 16:36:28 -0700 | [diff] [blame] | 354 | if (node->phandle && tchild->phandle) |
| 355 | ret = -EINVAL; |
| 356 | else |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 357 | ret = build_changeset_next_level(ovcs, tchild, node); |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 358 | of_node_put(tchild); |
| 359 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 360 | return ret; |
| 361 | } |
| 362 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 363 | /** |
| 364 | * build_changeset_next_level() - add level of overlay changeset |
| 365 | * @ovcs: overlay changeset |
| 366 | * @target_node: where to place @overlay_node in live tree |
| 367 | * @overlay_node: node from within an overlay device tree fragment |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 368 | * |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 369 | * Add the properties (if any) and nodes (if any) from @overlay_node to the |
| 370 | * @ovcs->cset changeset. If an added node has child nodes, they will |
| 371 | * be added recursively. |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 372 | * |
| 373 | * Do not allow symbols node to have any children. |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 374 | * |
| 375 | * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if |
| 376 | * invalid @overlay_node. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 377 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 378 | static int build_changeset_next_level(struct overlay_changeset *ovcs, |
| 379 | struct device_node *target_node, |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 380 | const struct device_node *overlay_node) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 381 | { |
| 382 | struct device_node *child; |
| 383 | struct property *prop; |
| 384 | int ret; |
| 385 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 386 | for_each_property_of_node(overlay_node, prop) { |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 387 | ret = add_changeset_property(ovcs, target_node, prop, 0); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 388 | if (ret) { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 389 | pr_debug("Failed to apply prop @%pOF/%s, err=%d\n", |
| 390 | target_node, prop->name, ret); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 391 | return ret; |
| 392 | } |
| 393 | } |
| 394 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 395 | for_each_child_of_node(overlay_node, child) { |
| 396 | ret = add_changeset_node(ovcs, target_node, child); |
Frank Rowand | bbed879 | 2017-10-17 16:36:22 -0700 | [diff] [blame] | 397 | if (ret) { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 398 | pr_debug("Failed to apply node @%pOF/%s, err=%d\n", |
| 399 | target_node, child->name, ret); |
Julia Lawall | 001cf50 | 2015-10-22 11:02:48 +0200 | [diff] [blame] | 400 | of_node_put(child); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 408 | /* |
| 409 | * Add the properties from __overlay__ node to the @ovcs->cset changeset. |
| 410 | */ |
| 411 | static int build_changeset_symbols_node(struct overlay_changeset *ovcs, |
| 412 | struct device_node *target_node, |
| 413 | const struct device_node *overlay_symbols_node) |
| 414 | { |
| 415 | struct property *prop; |
| 416 | int ret; |
| 417 | |
| 418 | for_each_property_of_node(overlay_symbols_node, prop) { |
| 419 | ret = add_changeset_property(ovcs, target_node, prop, 1); |
| 420 | if (ret) { |
| 421 | pr_debug("Failed to apply prop @%pOF/%s, err=%d\n", |
| 422 | target_node, prop->name, ret); |
| 423 | return ret; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 430 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 431 | * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments |
| 432 | * @ovcs: Overlay changeset |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 433 | * |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 434 | * Create changeset @ovcs->cset to contain the nodes and properties of the |
| 435 | * overlay device tree fragments in @ovcs->fragments[]. If an error occurs, |
| 436 | * any portions of the changeset that were successfully created will remain |
| 437 | * in @ovcs->cset. |
| 438 | * |
| 439 | * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if |
| 440 | * invalid overlay in @ovcs->fragments[]. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 441 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 442 | static int build_changeset(struct overlay_changeset *ovcs) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 443 | { |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 444 | struct fragment *fragment; |
| 445 | int fragments_count, i, ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 446 | |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 447 | /* |
| 448 | * if there is a symbols fragment in ovcs->fragments[i] it is |
| 449 | * the final element in the array |
| 450 | */ |
| 451 | if (ovcs->symbols_fragment) |
| 452 | fragments_count = ovcs->count - 1; |
| 453 | else |
| 454 | fragments_count = ovcs->count; |
| 455 | |
| 456 | for (i = 0; i < fragments_count; i++) { |
| 457 | fragment = &ovcs->fragments[i]; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 458 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 459 | ret = build_changeset_next_level(ovcs, fragment->target, |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 460 | fragment->overlay); |
| 461 | if (ret) { |
| 462 | pr_debug("apply failed '%pOF'\n", fragment->target); |
| 463 | return ret; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (ovcs->symbols_fragment) { |
| 468 | fragment = &ovcs->fragments[ovcs->count - 1]; |
| 469 | ret = build_changeset_symbols_node(ovcs, fragment->target, |
| 470 | fragment->overlay); |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 471 | if (ret) { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 472 | pr_debug("apply failed '%pOF'\n", fragment->target); |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 473 | return ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Find the target node using a number of different strategies |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 482 | * in order of preference: |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 483 | * |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 484 | * 1) "target" property containing the phandle of the target |
| 485 | * 2) "target-path" property containing the path of the target |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 486 | */ |
| 487 | static struct device_node *find_target_node(struct device_node *info_node) |
| 488 | { |
| 489 | const char *path; |
| 490 | u32 val; |
| 491 | int ret; |
| 492 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 493 | ret = of_property_read_u32(info_node, "target", &val); |
Frank Rowand | bbed879 | 2017-10-17 16:36:22 -0700 | [diff] [blame] | 494 | if (!ret) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 495 | return of_find_node_by_phandle(val); |
| 496 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 497 | ret = of_property_read_string(info_node, "target-path", &path); |
Frank Rowand | bbed879 | 2017-10-17 16:36:22 -0700 | [diff] [blame] | 498 | if (!ret) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 499 | return of_find_node_by_path(path); |
| 500 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 501 | pr_err("Failed to find target for node %p (%s)\n", |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 502 | info_node, info_node->name); |
| 503 | |
| 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 508 | * init_overlay_changeset() - initialize overlay changeset from overlay tree |
| 509 | * @ovcs Overlay changeset to build |
| 510 | * @tree: Contains all the overlay fragments and overlay fixup nodes |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 511 | * |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 512 | * Initialize @ovcs. Populate @ovcs->fragments with node information from |
| 513 | * the top level of @tree. The relevant top level nodes are the fragment |
| 514 | * nodes and the __symbols__ node. Any other top level node will be ignored. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 515 | * |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 516 | * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 517 | * detected in @tree, or -ENOSPC if idr_alloc() error. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 518 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 519 | static int init_overlay_changeset(struct overlay_changeset *ovcs, |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 520 | struct device_node *tree) |
| 521 | { |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 522 | struct device_node *node, *overlay_node; |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 523 | struct fragment *fragment; |
| 524 | struct fragment *fragments; |
| 525 | int cnt, ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 526 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 527 | /* |
| 528 | * Warn for some issues. Can not return -EINVAL for these until |
| 529 | * of_unittest_apply_overlay() is fixed to pass these checks. |
| 530 | */ |
| 531 | if (!of_node_check_flag(tree, OF_DYNAMIC)) |
| 532 | pr_debug("%s() tree is not dynamic\n", __func__); |
| 533 | |
| 534 | if (!of_node_check_flag(tree, OF_DETACHED)) |
| 535 | pr_debug("%s() tree is not detached\n", __func__); |
| 536 | |
| 537 | if (!of_node_is_root(tree)) |
| 538 | pr_debug("%s() tree is not root\n", __func__); |
| 539 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 540 | ovcs->overlay_tree = tree; |
| 541 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 542 | INIT_LIST_HEAD(&ovcs->ovcs_list); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 543 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 544 | of_changeset_init(&ovcs->cset); |
| 545 | |
| 546 | ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL); |
| 547 | if (ovcs->id <= 0) |
| 548 | return ovcs->id; |
| 549 | |
| 550 | cnt = 0; |
| 551 | |
| 552 | /* fragment nodes */ |
| 553 | for_each_child_of_node(tree, node) { |
| 554 | overlay_node = of_get_child_by_name(node, "__overlay__"); |
| 555 | if (overlay_node) { |
| 556 | cnt++; |
| 557 | of_node_put(overlay_node); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | node = of_get_child_by_name(tree, "__symbols__"); |
| 562 | if (node) { |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 563 | cnt++; |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 564 | of_node_put(node); |
| 565 | } |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 566 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 567 | fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL); |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 568 | if (!fragments) { |
| 569 | ret = -ENOMEM; |
| 570 | goto err_free_idr; |
| 571 | } |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 572 | |
| 573 | cnt = 0; |
| 574 | for_each_child_of_node(tree, node) { |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 575 | fragment = &fragments[cnt]; |
| 576 | fragment->overlay = of_get_child_by_name(node, "__overlay__"); |
| 577 | if (fragment->overlay) { |
| 578 | fragment->target = find_target_node(node); |
| 579 | if (!fragment->target) { |
| 580 | of_node_put(fragment->overlay); |
| 581 | ret = -EINVAL; |
| 582 | goto err_free_fragments; |
| 583 | } else { |
| 584 | cnt++; |
| 585 | } |
| 586 | } |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 589 | /* |
| 590 | * if there is a symbols fragment in ovcs->fragments[i] it is |
| 591 | * the final element in the array |
| 592 | */ |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 593 | node = of_get_child_by_name(tree, "__symbols__"); |
| 594 | if (node) { |
Frank Rowand | 3912b79 | 2017-10-17 16:36:30 -0700 | [diff] [blame] | 595 | ovcs->symbols_fragment = 1; |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 596 | fragment = &fragments[cnt]; |
| 597 | fragment->overlay = node; |
| 598 | fragment->target = of_find_node_by_path("/__symbols__"); |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 599 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 600 | if (!fragment->target) { |
Frank Rowand | 4ee7c0d | 2017-10-19 14:38:11 -0700 | [diff] [blame] | 601 | pr_err("symbols in overlay, but not in live tree\n"); |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 602 | ret = -EINVAL; |
| 603 | goto err_free_fragments; |
Frank Rowand | d1651b0 | 2017-07-19 09:25:22 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | cnt++; |
| 607 | } |
| 608 | |
Frank Rowand | bbed879 | 2017-10-17 16:36:22 -0700 | [diff] [blame] | 609 | if (!cnt) { |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 610 | ret = -EINVAL; |
| 611 | goto err_free_fragments; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 614 | ovcs->count = cnt; |
| 615 | ovcs->fragments = fragments; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 616 | |
| 617 | return 0; |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 618 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 619 | err_free_fragments: |
| 620 | kfree(fragments); |
| 621 | err_free_idr: |
| 622 | idr_remove(&ovcs_idr, ovcs->id); |
| 623 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 624 | pr_err("%s() failed, ret = %d\n", __func__, ret); |
| 625 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 626 | return ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 629 | static void free_overlay_changeset(struct overlay_changeset *ovcs) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 630 | { |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 631 | int i; |
| 632 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 633 | if (!ovcs->cset.entries.next) |
| 634 | return; |
| 635 | of_changeset_destroy(&ovcs->cset); |
| 636 | |
| 637 | if (ovcs->id) |
| 638 | idr_remove(&ovcs_idr, ovcs->id); |
| 639 | |
| 640 | for (i = 0; i < ovcs->count; i++) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 641 | of_node_put(ovcs->fragments[i].target); |
| 642 | of_node_put(ovcs->fragments[i].overlay); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 643 | } |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 644 | kfree(ovcs->fragments); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 645 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 646 | kfree(ovcs); |
| 647 | } |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 648 | |
| 649 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 650 | * of_overlay_apply() - Create and apply an overlay changeset |
| 651 | * @tree: Expanded overlay device tree |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 652 | * @ovcs_id: Pointer to overlay changeset id |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 653 | * |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 654 | * Creates and applies an overlay changeset. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 655 | * |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 656 | * If an error occurs in a pre-apply notifier, then no changes are made |
| 657 | * to the device tree. |
| 658 | * |
| 659 | |
| 660 | * A non-zero return value will not have created the changeset if error is from: |
| 661 | * - parameter checks |
| 662 | * - building the changeset |
| 663 | * - overlay changset pre-apply notifier |
| 664 | * |
| 665 | * If an error is returned by an overlay changeset pre-apply notifier |
| 666 | * then no further overlay changeset pre-apply notifier will be called. |
| 667 | * |
| 668 | * A non-zero return value will have created the changeset if error is from: |
| 669 | * - overlay changeset entry notifier |
| 670 | * - overlay changset post-apply notifier |
| 671 | * |
| 672 | * If an error is returned by an overlay changeset post-apply notifier |
| 673 | * then no further overlay changeset post-apply notifier will be called. |
| 674 | * |
| 675 | * If more than one notifier returns an error, then the last notifier |
| 676 | * error to occur is returned. |
| 677 | * |
| 678 | * If an error occurred while applying the overlay changeset, then an |
| 679 | * attempt is made to revert any changes that were made to the |
| 680 | * device tree. If there were any errors during the revert attempt |
| 681 | * then the state of the device tree can not be determined, and any |
| 682 | * following attempt to apply or remove an overlay changeset will be |
| 683 | * refused. |
| 684 | * |
| 685 | * Returns 0 on success, or a negative error number. Overlay changeset |
| 686 | * id is returned to *ovcs_id. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 687 | */ |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 688 | |
| 689 | int of_overlay_apply(struct device_node *tree, int *ovcs_id) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 690 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 691 | struct overlay_changeset *ovcs; |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 692 | int ret = 0, ret_revert, ret_tmp; |
| 693 | |
| 694 | *ovcs_id = 0; |
| 695 | |
| 696 | if (devicetree_corrupt()) { |
| 697 | pr_err("devicetree state suspect, refuse to apply overlay\n"); |
| 698 | ret = -EBUSY; |
| 699 | goto out; |
| 700 | } |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 701 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 702 | ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL); |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 703 | if (!ovcs) { |
| 704 | ret = -ENOMEM; |
| 705 | goto out; |
| 706 | } |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 707 | |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 708 | of_overlay_mutex_lock(); |
| 709 | |
| 710 | ret = of_resolve_phandles(tree); |
| 711 | if (ret) |
| 712 | goto err_overlay_unlock; |
| 713 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 714 | mutex_lock(&of_mutex); |
| 715 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 716 | ret = init_overlay_changeset(ovcs, tree); |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 717 | if (ret) |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 718 | goto err_free_overlay_changeset; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 719 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 720 | ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY); |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 721 | if (ret) { |
| 722 | pr_err("overlay changeset pre-apply notify error %d\n", ret); |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 723 | goto err_free_overlay_changeset; |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 724 | } |
| 725 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 726 | ret = build_changeset(ovcs); |
| 727 | if (ret) |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 728 | goto err_free_overlay_changeset; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 729 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 730 | ret_revert = 0; |
| 731 | ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert); |
| 732 | if (ret) { |
| 733 | if (ret_revert) { |
| 734 | pr_debug("overlay changeset revert error %d\n", |
| 735 | ret_revert); |
| 736 | devicetree_state_flags |= DTSF_APPLY_FAIL; |
| 737 | } |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 738 | goto err_free_overlay_changeset; |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 739 | } else { |
| 740 | ret = __of_changeset_apply_notify(&ovcs->cset); |
| 741 | if (ret) |
| 742 | pr_err("overlay changeset entry notify error %d\n", |
| 743 | ret); |
| 744 | /* fall through */ |
| 745 | } |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 746 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 747 | list_add_tail(&ovcs->ovcs_list, &ovcs_list); |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 748 | *ovcs_id = ovcs->id; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 749 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 750 | ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY); |
| 751 | if (ret_tmp) { |
| 752 | pr_err("overlay changeset post-apply notify error %d\n", |
| 753 | ret_tmp); |
| 754 | if (!ret) |
| 755 | ret = ret_tmp; |
| 756 | } |
Alan Tull | 39a842e | 2016-11-01 14:14:22 -0500 | [diff] [blame] | 757 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 758 | mutex_unlock(&of_mutex); |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 759 | of_overlay_mutex_unlock(); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 760 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 761 | goto out; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 762 | |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 763 | err_overlay_unlock: |
| 764 | of_overlay_mutex_unlock(); |
| 765 | |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 766 | err_free_overlay_changeset: |
| 767 | free_overlay_changeset(ovcs); |
| 768 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 769 | mutex_unlock(&of_mutex); |
| 770 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 771 | out: |
| 772 | pr_debug("%s() err=%d\n", __func__, ret); |
| 773 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 774 | return ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 775 | } |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 776 | EXPORT_SYMBOL_GPL(of_overlay_apply); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 777 | |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 778 | /* |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 779 | * Find @np in @tree. |
| 780 | * |
| 781 | * Returns 1 if @np is @tree or is contained in @tree, else 0 |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 782 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 783 | static int find_node(struct device_node *tree, struct device_node *np) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 784 | { |
| 785 | struct device_node *child; |
| 786 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 787 | if (tree == np) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 788 | return 1; |
| 789 | |
| 790 | for_each_child_of_node(tree, child) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 791 | if (find_node(child, np)) { |
Julia Lawall | 001cf50 | 2015-10-22 11:02:48 +0200 | [diff] [blame] | 792 | of_node_put(child); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 793 | return 1; |
Julia Lawall | 001cf50 | 2015-10-22 11:02:48 +0200 | [diff] [blame] | 794 | } |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 800 | /* |
Frank Rowand | 87f242c | 2017-10-17 16:36:27 -0700 | [diff] [blame] | 801 | * Is @remove_ce_node a child of, a parent of, or the same as any |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 802 | * node in an overlay changeset more topmost than @remove_ovcs? |
| 803 | * |
| 804 | * Returns 1 if found, else 0 |
Frank Rowand | 646afc4 | 2017-10-17 16:36:21 -0700 | [diff] [blame] | 805 | */ |
Frank Rowand | 87f242c | 2017-10-17 16:36:27 -0700 | [diff] [blame] | 806 | static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs, |
| 807 | struct device_node *remove_ce_node) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 808 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 809 | struct overlay_changeset *ovcs; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 810 | struct of_changeset_entry *ce; |
| 811 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 812 | list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) { |
| 813 | if (ovcs == remove_ovcs) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 814 | break; |
| 815 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 816 | list_for_each_entry(ce, &ovcs->cset.entries, node) { |
Frank Rowand | 87f242c | 2017-10-17 16:36:27 -0700 | [diff] [blame] | 817 | if (find_node(ce->np, remove_ce_node)) { |
| 818 | pr_err("%s: #%d overlaps with #%d @%pOF\n", |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 819 | __func__, remove_ovcs->id, ovcs->id, |
Frank Rowand | 87f242c | 2017-10-17 16:36:27 -0700 | [diff] [blame] | 820 | remove_ce_node); |
| 821 | return 1; |
| 822 | } |
| 823 | if (find_node(remove_ce_node, ce->np)) { |
| 824 | pr_err("%s: #%d overlaps with #%d @%pOF\n", |
| 825 | __func__, remove_ovcs->id, ovcs->id, |
| 826 | remove_ce_node); |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 827 | return 1; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 832 | return 0; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* |
| 836 | * We can safely remove the overlay only if it's the top-most one. |
| 837 | * Newly applied overlays are inserted at the tail of the overlay list, |
| 838 | * so a top most overlay is the one that is closest to the tail. |
| 839 | * |
| 840 | * The topmost check is done by exploiting this property. For each |
| 841 | * affected device node in the log list we check if this overlay is |
| 842 | * the one closest to the tail. If another overlay has affected this |
| 843 | * device node and is closest to the tail, then removal is not permited. |
| 844 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 845 | static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 846 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 847 | struct of_changeset_entry *remove_ce; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 848 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 849 | list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) { |
Frank Rowand | 87f242c | 2017-10-17 16:36:27 -0700 | [diff] [blame] | 850 | if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 851 | pr_err("overlay #%d is not topmost\n", remove_ovcs->id); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 852 | return 0; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | return 1; |
| 857 | } |
| 858 | |
| 859 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 860 | * of_overlay_remove() - Revert and free an overlay changeset |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 861 | * @ovcs_id: Pointer to overlay changeset id |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 862 | * |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 863 | * Removes an overlay if it is permissible. @ovcs_id was previously returned |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 864 | * by of_overlay_apply(). |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 865 | * |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 866 | * If an error occurred while attempting to revert the overlay changeset, |
| 867 | * then an attempt is made to re-apply any changeset entry that was |
| 868 | * reverted. If an error occurs on re-apply then the state of the device |
| 869 | * tree can not be determined, and any following attempt to apply or remove |
| 870 | * an overlay changeset will be refused. |
| 871 | * |
| 872 | * A non-zero return value will not revert the changeset if error is from: |
| 873 | * - parameter checks |
| 874 | * - overlay changset pre-remove notifier |
| 875 | * - overlay changeset entry revert |
| 876 | * |
| 877 | * If an error is returned by an overlay changeset pre-remove notifier |
| 878 | * then no further overlay changeset pre-remove notifier will be called. |
| 879 | * |
| 880 | * If more than one notifier returns an error, then the last notifier |
| 881 | * error to occur is returned. |
| 882 | * |
| 883 | * A non-zero return value will revert the changeset if error is from: |
| 884 | * - overlay changeset entry notifier |
| 885 | * - overlay changset post-remove notifier |
| 886 | * |
| 887 | * If an error is returned by an overlay changeset post-remove notifier |
| 888 | * then no further overlay changeset post-remove notifier will be called. |
| 889 | * |
| 890 | * Returns 0 on success, or a negative error number. *ovcs_id is set to |
| 891 | * zero after reverting the changeset, even if a subsequent error occurs. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 892 | */ |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 893 | int of_overlay_remove(int *ovcs_id) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 894 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 895 | struct overlay_changeset *ovcs; |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 896 | int ret, ret_apply, ret_tmp; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 897 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 898 | ret = 0; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 899 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 900 | if (devicetree_corrupt()) { |
| 901 | pr_err("suspect devicetree state, refuse to remove overlay\n"); |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 902 | ret = -EBUSY; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 903 | goto out; |
| 904 | } |
| 905 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 906 | mutex_lock(&of_mutex); |
| 907 | |
| 908 | ovcs = idr_find(&ovcs_idr, *ovcs_id); |
| 909 | if (!ovcs) { |
| 910 | ret = -ENODEV; |
| 911 | pr_err("remove: Could not find overlay #%d\n", *ovcs_id); |
| 912 | goto out_unlock; |
| 913 | } |
| 914 | |
| 915 | if (!overlay_removal_is_ok(ovcs)) { |
| 916 | ret = -EBUSY; |
| 917 | goto out_unlock; |
| 918 | } |
| 919 | |
| 920 | ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE); |
| 921 | if (ret) { |
| 922 | pr_err("overlay changeset pre-remove notify error %d\n", ret); |
| 923 | goto out_unlock; |
| 924 | } |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 925 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 926 | list_del(&ovcs->ovcs_list); |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 927 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 928 | ret_apply = 0; |
| 929 | ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply); |
| 930 | if (ret) { |
| 931 | if (ret_apply) |
| 932 | devicetree_state_flags |= DTSF_REVERT_FAIL; |
| 933 | goto out_unlock; |
| 934 | } else { |
| 935 | ret = __of_changeset_revert_notify(&ovcs->cset); |
| 936 | if (ret) { |
| 937 | pr_err("overlay changeset entry notify error %d\n", |
| 938 | ret); |
| 939 | /* fall through - changeset was reverted */ |
| 940 | } |
| 941 | } |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 942 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 943 | *ovcs_id = 0; |
| 944 | |
| 945 | ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE); |
| 946 | if (ret_tmp) { |
| 947 | pr_err("overlay changeset post-remove notify error %d\n", |
| 948 | ret_tmp); |
| 949 | if (!ret) |
| 950 | ret = ret_tmp; |
| 951 | } |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 952 | |
| 953 | free_overlay_changeset(ovcs); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 954 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 955 | out_unlock: |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 956 | mutex_unlock(&of_mutex); |
| 957 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 958 | out: |
| 959 | pr_debug("%s() err=%d\n", __func__, ret); |
| 960 | |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 961 | return ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 962 | } |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 963 | EXPORT_SYMBOL_GPL(of_overlay_remove); |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 964 | |
| 965 | /** |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 966 | * of_overlay_remove_all() - Reverts and frees all overlay changesets |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 967 | * |
| 968 | * Removes all overlays from the system in the correct order. |
| 969 | * |
Geert Uytterhoeven | 94a8bf9 | 2015-05-21 14:10:26 +0200 | [diff] [blame] | 970 | * Returns 0 on success, or a negative error number |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 971 | */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 972 | int of_overlay_remove_all(void) |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 973 | { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 974 | struct overlay_changeset *ovcs, *ovcs_n; |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 975 | int ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 976 | |
| 977 | /* the tail of list is guaranteed to be safe to remove */ |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 978 | list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 979 | ret = of_overlay_remove(&ovcs->id); |
Frank Rowand | 61b4de4 | 2017-10-17 16:36:25 -0700 | [diff] [blame] | 980 | if (ret) |
| 981 | return ret; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 984 | return 0; |
| 985 | } |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 986 | EXPORT_SYMBOL_GPL(of_overlay_remove_all); |