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