overlay.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. // SPDX-License-Identifier: GPL-2.0
  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.
  7. */
  8. #define pr_fmt(fmt) "OF: overlay: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/string.h>
  15. #include <linux/ctype.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/libfdt.h>
  19. #include <linux/err.h>
  20. #include <linux/idr.h>
  21. #include "of_private.h"
  22. /**
  23. * struct target - info about current target node as recursing through overlay
  24. * @np: node where current level of overlay will be applied
  25. * @in_livetree: @np is a node in the live devicetree
  26. *
  27. * Used in the algorithm to create the portion of a changeset that describes
  28. * an overlay fragment, which is a devicetree subtree. Initially @np is a node
  29. * in the live devicetree where the overlay subtree is targeted to be grafted
  30. * into. When recursing to the next level of the overlay subtree, the target
  31. * also recurses to the next level of the live devicetree, as long as overlay
  32. * subtree node also exists in the live devicetree. When a node in the overlay
  33. * subtree does not exist at the same level in the live devicetree, target->np
  34. * points to a newly allocated node, and all subsequent targets in the subtree
  35. * will be newly allocated nodes.
  36. */
  37. struct target {
  38. struct device_node *np;
  39. bool in_livetree;
  40. };
  41. /**
  42. * struct fragment - info about fragment nodes in overlay expanded device tree
  43. * @overlay: pointer to the __overlay__ node
  44. * @target: target of the overlay operation
  45. */
  46. struct fragment {
  47. struct device_node *overlay;
  48. struct device_node *target;
  49. };
  50. /**
  51. * struct overlay_changeset
  52. * @id: changeset identifier
  53. * @ovcs_list: list on which we are located
  54. * @new_fdt: Memory allocated to hold unflattened aligned FDT
  55. * @overlay_mem: the memory chunk that contains @overlay_root
  56. * @overlay_root: expanded device tree that contains the fragment nodes
  57. * @notify_state: most recent notify action used on overlay
  58. * @count: count of fragment structures
  59. * @fragments: fragment nodes in the overlay expanded device tree
  60. * @symbols_fragment: last element of @fragments[] is the __symbols__ node
  61. * @cset: changeset to apply fragments to live device tree
  62. */
  63. struct overlay_changeset {
  64. int id;
  65. struct list_head ovcs_list;
  66. const void *new_fdt;
  67. const void *overlay_mem;
  68. struct device_node *overlay_root;
  69. enum of_overlay_notify_action notify_state;
  70. int count;
  71. struct fragment *fragments;
  72. bool symbols_fragment;
  73. struct of_changeset cset;
  74. };
  75. /* flags are sticky - once set, do not reset */
  76. static int devicetree_state_flags;
  77. #define DTSF_APPLY_FAIL 0x01
  78. #define DTSF_REVERT_FAIL 0x02
  79. /*
  80. * If a changeset apply or revert encounters an error, an attempt will
  81. * be made to undo partial changes, but may fail. If the undo fails
  82. * we do not know the state of the devicetree.
  83. */
  84. static int devicetree_corrupt(void)
  85. {
  86. return devicetree_state_flags &
  87. (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
  88. }
  89. static int build_changeset_next_level(struct overlay_changeset *ovcs,
  90. struct target *target, const struct device_node *overlay_node);
  91. /*
  92. * of_resolve_phandles() finds the largest phandle in the live tree.
  93. * of_overlay_apply() may add a larger phandle to the live tree.
  94. * Do not allow race between two overlays being applied simultaneously:
  95. * mutex_lock(&of_overlay_phandle_mutex)
  96. * of_resolve_phandles()
  97. * of_overlay_apply()
  98. * mutex_unlock(&of_overlay_phandle_mutex)
  99. */
  100. static DEFINE_MUTEX(of_overlay_phandle_mutex);
  101. void of_overlay_mutex_lock(void)
  102. {
  103. mutex_lock(&of_overlay_phandle_mutex);
  104. }
  105. void of_overlay_mutex_unlock(void)
  106. {
  107. mutex_unlock(&of_overlay_phandle_mutex);
  108. }
  109. static LIST_HEAD(ovcs_list);
  110. static DEFINE_IDR(ovcs_idr);
  111. static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
  112. /**
  113. * of_overlay_notifier_register() - Register notifier for overlay operations
  114. * @nb: Notifier block to register
  115. *
  116. * Register for notification on overlay operations on device tree nodes. The
  117. * reported actions definied by @of_reconfig_change. The notifier callback
  118. * furthermore receives a pointer to the affected device tree node.
  119. *
  120. * Note that a notifier callback is not supposed to store pointers to a device
  121. * tree node or its content beyond @OF_OVERLAY_POST_REMOVE corresponding to the
  122. * respective node it received.
  123. */
  124. int of_overlay_notifier_register(struct notifier_block *nb)
  125. {
  126. return blocking_notifier_chain_register(&overlay_notify_chain, nb);
  127. }
  128. EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
  129. /**
  130. * of_overlay_notifier_unregister() - Unregister notifier for overlay operations
  131. * @nb: Notifier block to unregister
  132. */
  133. int of_overlay_notifier_unregister(struct notifier_block *nb)
  134. {
  135. return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
  136. }
  137. EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
  138. static int overlay_notify(struct overlay_changeset *ovcs,
  139. enum of_overlay_notify_action action)
  140. {
  141. struct of_overlay_notify_data nd;
  142. int i, ret;
  143. ovcs->notify_state = action;
  144. for (i = 0; i < ovcs->count; i++) {
  145. struct fragment *fragment = &ovcs->fragments[i];
  146. nd.target = fragment->target;
  147. nd.overlay = fragment->overlay;
  148. ret = blocking_notifier_call_chain(&overlay_notify_chain,
  149. action, &nd);
  150. if (notifier_to_errno(ret)) {
  151. ret = notifier_to_errno(ret);
  152. pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
  153. of_overlay_action_name(action), ret, nd.target);
  154. return ret;
  155. }
  156. }
  157. return 0;
  158. }
  159. /*
  160. * The values of properties in the "/__symbols__" node are paths in
  161. * the ovcs->overlay_root. When duplicating the properties, the paths
  162. * need to be adjusted to be the correct path for the live device tree.
  163. *
  164. * The paths refer to a node in the subtree of a fragment node's "__overlay__"
  165. * node, for example "/fragment@0/__overlay__/symbol_path_tail",
  166. * where symbol_path_tail can be a single node or it may be a multi-node path.
  167. *
  168. * The duplicated property value will be modified by replacing the
  169. * "/fragment_name/__overlay/" portion of the value with the target
  170. * path from the fragment node.
  171. */
  172. static struct property *dup_and_fixup_symbol_prop(
  173. struct overlay_changeset *ovcs, const struct property *prop)
  174. {
  175. struct fragment *fragment;
  176. struct property *new_prop;
  177. struct device_node *fragment_node;
  178. struct device_node *overlay_node;
  179. const char *path;
  180. const char *path_tail;
  181. const char *target_path;
  182. int k;
  183. int overlay_name_len;
  184. int path_len;
  185. int path_tail_len;
  186. int target_path_len;
  187. if (!prop->value)
  188. return NULL;
  189. if (strnlen(prop->value, prop->length) >= prop->length)
  190. return NULL;
  191. path = prop->value;
  192. path_len = strlen(path);
  193. if (path_len < 1)
  194. return NULL;
  195. fragment_node = __of_find_node_by_path(ovcs->overlay_root, path + 1);
  196. overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
  197. of_node_put(fragment_node);
  198. of_node_put(overlay_node);
  199. for (k = 0; k < ovcs->count; k++) {
  200. fragment = &ovcs->fragments[k];
  201. if (fragment->overlay == overlay_node)
  202. break;
  203. }
  204. if (k >= ovcs->count)
  205. return NULL;
  206. overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
  207. if (overlay_name_len > path_len)
  208. return NULL;
  209. path_tail = path + overlay_name_len;
  210. path_tail_len = strlen(path_tail);
  211. target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
  212. if (!target_path)
  213. return NULL;
  214. target_path_len = strlen(target_path);
  215. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  216. if (!new_prop)
  217. goto err_free_target_path;
  218. new_prop->name = kstrdup(prop->name, GFP_KERNEL);
  219. new_prop->length = target_path_len + path_tail_len + 1;
  220. new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
  221. if (!new_prop->name || !new_prop->value)
  222. goto err_free_new_prop;
  223. strcpy(new_prop->value, target_path);
  224. strcpy(new_prop->value + target_path_len, path_tail);
  225. of_property_set_flag(new_prop, OF_DYNAMIC);
  226. kfree(target_path);
  227. return new_prop;
  228. err_free_new_prop:
  229. __of_prop_free(new_prop);
  230. err_free_target_path:
  231. kfree(target_path);
  232. return NULL;
  233. }
  234. /**
  235. * add_changeset_property() - add @overlay_prop to overlay changeset
  236. * @ovcs: overlay changeset
  237. * @target: where @overlay_prop will be placed
  238. * @overlay_prop: property to add or update, from overlay tree
  239. * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
  240. *
  241. * If @overlay_prop does not already exist in live devicetree, add changeset
  242. * entry to add @overlay_prop in @target, else add changeset entry to update
  243. * value of @overlay_prop.
  244. *
  245. * @target may be either in the live devicetree or in a new subtree that
  246. * is contained in the changeset.
  247. *
  248. * Some special properties are not added or updated (no error returned):
  249. * "name", "phandle", "linux,phandle".
  250. *
  251. * Properties "#address-cells" and "#size-cells" are not updated if they
  252. * are already in the live tree, but if present in the live tree, the values
  253. * in the overlay must match the values in the live tree.
  254. *
  255. * Update of property in symbols node is not allowed.
  256. *
  257. * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  258. * invalid @overlay.
  259. */
  260. static int add_changeset_property(struct overlay_changeset *ovcs,
  261. struct target *target, struct property *overlay_prop,
  262. bool is_symbols_prop)
  263. {
  264. struct property *new_prop = NULL, *prop;
  265. int ret = 0;
  266. if (target->in_livetree)
  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"))
  270. return 0;
  271. if (target->in_livetree)
  272. prop = of_find_property(target->np, overlay_prop->name, NULL);
  273. else
  274. prop = NULL;
  275. if (prop) {
  276. if (!of_prop_cmp(prop->name, "#address-cells")) {
  277. if (!of_prop_val_eq(prop, overlay_prop)) {
  278. pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",
  279. target->np);
  280. ret = -EINVAL;
  281. }
  282. return ret;
  283. } else if (!of_prop_cmp(prop->name, "#size-cells")) {
  284. if (!of_prop_val_eq(prop, overlay_prop)) {
  285. pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",
  286. target->np);
  287. ret = -EINVAL;
  288. }
  289. return ret;
  290. }
  291. }
  292. if (is_symbols_prop) {
  293. if (prop)
  294. return -EINVAL;
  295. new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
  296. } else {
  297. new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
  298. }
  299. if (!new_prop)
  300. return -ENOMEM;
  301. if (!prop) {
  302. if (!target->in_livetree) {
  303. new_prop->next = target->np->deadprops;
  304. target->np->deadprops = new_prop;
  305. }
  306. ret = of_changeset_add_property(&ovcs->cset, target->np,
  307. new_prop);
  308. } else {
  309. ret = of_changeset_update_property(&ovcs->cset, target->np,
  310. new_prop);
  311. }
  312. if (!of_node_check_flag(target->np, OF_OVERLAY))
  313. pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
  314. target->np, new_prop->name);
  315. if (ret)
  316. __of_prop_free(new_prop);
  317. return ret;
  318. }
  319. /**
  320. * add_changeset_node() - add @node (and children) to overlay changeset
  321. * @ovcs: overlay changeset
  322. * @target: where @node will be placed in live tree or changeset
  323. * @node: node from within overlay device tree fragment
  324. *
  325. * If @node does not already exist in @target, add changeset entry
  326. * to add @node in @target.
  327. *
  328. * If @node already exists in @target, and the existing node has
  329. * a phandle, the overlay node is not allowed to have a phandle.
  330. *
  331. * If @node has child nodes, add the children recursively via
  332. * build_changeset_next_level().
  333. *
  334. * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
  335. * not contain the full path in node->full_name. Thus an overlay
  336. * created from an FDT also will not contain the full path in
  337. * node->full_name. However, a live devicetree created from Open
  338. * Firmware may have the full path in node->full_name.
  339. *
  340. * add_changeset_node() follows the FDT convention and does not include
  341. * the full path in node->full_name. Even though it expects the overlay
  342. * to not contain the full path, it uses kbasename() to remove the
  343. * full path should it exist. It also uses kbasename() in comparisons
  344. * to nodes in the live devicetree so that it can apply an overlay to
  345. * a live devicetree created from Open Firmware.
  346. *
  347. * NOTE_2: Multiple mods of created nodes not supported.
  348. *
  349. * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  350. * invalid @overlay.
  351. */
  352. static int add_changeset_node(struct overlay_changeset *ovcs,
  353. struct target *target, struct device_node *node)
  354. {
  355. const char *node_kbasename;
  356. const __be32 *phandle;
  357. struct device_node *tchild;
  358. struct target target_child;
  359. int ret = 0, size;
  360. node_kbasename = kbasename(node->full_name);
  361. for_each_child_of_node(target->np, tchild)
  362. if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
  363. break;
  364. if (!tchild) {
  365. tchild = __of_node_dup(NULL, node_kbasename);
  366. if (!tchild)
  367. return -ENOMEM;
  368. tchild->parent = target->np;
  369. tchild->name = __of_get_property(node, "name", NULL);
  370. if (!tchild->name)
  371. tchild->name = "<NULL>";
  372. /* ignore obsolete "linux,phandle" */
  373. phandle = __of_get_property(node, "phandle", &size);
  374. if (phandle && (size == 4))
  375. tchild->phandle = be32_to_cpup(phandle);
  376. of_node_set_flag(tchild, OF_OVERLAY);
  377. ret = of_changeset_attach_node(&ovcs->cset, tchild);
  378. if (ret)
  379. return ret;
  380. target_child.np = tchild;
  381. target_child.in_livetree = false;
  382. ret = build_changeset_next_level(ovcs, &target_child, node);
  383. of_node_put(tchild);
  384. return ret;
  385. }
  386. if (node->phandle && tchild->phandle) {
  387. ret = -EINVAL;
  388. } else {
  389. target_child.np = tchild;
  390. target_child.in_livetree = target->in_livetree;
  391. ret = build_changeset_next_level(ovcs, &target_child, node);
  392. }
  393. of_node_put(tchild);
  394. return ret;
  395. }
  396. /**
  397. * build_changeset_next_level() - add level of overlay changeset
  398. * @ovcs: overlay changeset
  399. * @target: where to place @overlay_node in live tree
  400. * @overlay_node: node from within an overlay device tree fragment
  401. *
  402. * Add the properties (if any) and nodes (if any) from @overlay_node to the
  403. * @ovcs->cset changeset. If an added node has child nodes, they will
  404. * be added recursively.
  405. *
  406. * Do not allow symbols node to have any children.
  407. *
  408. * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  409. * invalid @overlay_node.
  410. */
  411. static int build_changeset_next_level(struct overlay_changeset *ovcs,
  412. struct target *target, const struct device_node *overlay_node)
  413. {
  414. struct property *prop;
  415. int ret;
  416. for_each_property_of_node(overlay_node, prop) {
  417. ret = add_changeset_property(ovcs, target, prop, 0);
  418. if (ret) {
  419. pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
  420. target->np, prop->name, ret);
  421. return ret;
  422. }
  423. }
  424. for_each_child_of_node_scoped(overlay_node, child) {
  425. ret = add_changeset_node(ovcs, target, child);
  426. if (ret) {
  427. pr_debug("Failed to apply node @%pOF/%pOFn, err=%d\n",
  428. target->np, child, ret);
  429. return ret;
  430. }
  431. }
  432. return 0;
  433. }
  434. /*
  435. * Add the properties from __overlay__ node to the @ovcs->cset changeset.
  436. */
  437. static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
  438. struct target *target,
  439. const struct device_node *overlay_symbols_node)
  440. {
  441. struct property *prop;
  442. int ret;
  443. for_each_property_of_node(overlay_symbols_node, prop) {
  444. ret = add_changeset_property(ovcs, target, prop, 1);
  445. if (ret) {
  446. pr_debug("Failed to apply symbols prop @%pOF/%s, err=%d\n",
  447. target->np, prop->name, ret);
  448. return ret;
  449. }
  450. }
  451. return 0;
  452. }
  453. static int find_dup_cset_node_entry(struct overlay_changeset *ovcs,
  454. struct of_changeset_entry *ce_1)
  455. {
  456. struct of_changeset_entry *ce_2;
  457. char *fn_1, *fn_2;
  458. int node_path_match;
  459. if (ce_1->action != OF_RECONFIG_ATTACH_NODE &&
  460. ce_1->action != OF_RECONFIG_DETACH_NODE)
  461. return 0;
  462. ce_2 = ce_1;
  463. list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
  464. if ((ce_2->action != OF_RECONFIG_ATTACH_NODE &&
  465. ce_2->action != OF_RECONFIG_DETACH_NODE) ||
  466. of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
  467. continue;
  468. fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
  469. fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
  470. node_path_match = !fn_1 || !fn_2 || !strcmp(fn_1, fn_2);
  471. kfree(fn_1);
  472. kfree(fn_2);
  473. if (node_path_match) {
  474. pr_err("ERROR: multiple fragments add and/or delete node %pOF\n",
  475. ce_1->np);
  476. return -EINVAL;
  477. }
  478. }
  479. return 0;
  480. }
  481. static int find_dup_cset_prop(struct overlay_changeset *ovcs,
  482. struct of_changeset_entry *ce_1)
  483. {
  484. struct of_changeset_entry *ce_2;
  485. char *fn_1, *fn_2;
  486. int node_path_match;
  487. if (ce_1->action != OF_RECONFIG_ADD_PROPERTY &&
  488. ce_1->action != OF_RECONFIG_REMOVE_PROPERTY &&
  489. ce_1->action != OF_RECONFIG_UPDATE_PROPERTY)
  490. return 0;
  491. ce_2 = ce_1;
  492. list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
  493. if ((ce_2->action != OF_RECONFIG_ADD_PROPERTY &&
  494. ce_2->action != OF_RECONFIG_REMOVE_PROPERTY &&
  495. ce_2->action != OF_RECONFIG_UPDATE_PROPERTY) ||
  496. of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
  497. continue;
  498. fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
  499. fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
  500. node_path_match = !fn_1 || !fn_2 || !strcmp(fn_1, fn_2);
  501. kfree(fn_1);
  502. kfree(fn_2);
  503. if (node_path_match &&
  504. !of_prop_cmp(ce_1->prop->name, ce_2->prop->name)) {
  505. pr_err("ERROR: multiple fragments add, update, and/or delete property %pOF/%s\n",
  506. ce_1->np, ce_1->prop->name);
  507. return -EINVAL;
  508. }
  509. }
  510. return 0;
  511. }
  512. /**
  513. * changeset_dup_entry_check() - check for duplicate entries
  514. * @ovcs: Overlay changeset
  515. *
  516. * Check changeset @ovcs->cset for multiple {add or delete} node entries for
  517. * the same node or duplicate {add, delete, or update} properties entries
  518. * for the same property.
  519. *
  520. * Return: 0 on success, or -EINVAL if duplicate changeset entry found.
  521. */
  522. static int changeset_dup_entry_check(struct overlay_changeset *ovcs)
  523. {
  524. struct of_changeset_entry *ce_1;
  525. int dup_entry = 0;
  526. list_for_each_entry(ce_1, &ovcs->cset.entries, node) {
  527. dup_entry |= find_dup_cset_node_entry(ovcs, ce_1);
  528. dup_entry |= find_dup_cset_prop(ovcs, ce_1);
  529. }
  530. return dup_entry ? -EINVAL : 0;
  531. }
  532. /**
  533. * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
  534. * @ovcs: Overlay changeset
  535. *
  536. * Create changeset @ovcs->cset to contain the nodes and properties of the
  537. * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
  538. * any portions of the changeset that were successfully created will remain
  539. * in @ovcs->cset.
  540. *
  541. * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  542. * invalid overlay in @ovcs->fragments[].
  543. */
  544. static int build_changeset(struct overlay_changeset *ovcs)
  545. {
  546. struct fragment *fragment;
  547. struct target target;
  548. int fragments_count, i, ret;
  549. /*
  550. * if there is a symbols fragment in ovcs->fragments[i] it is
  551. * the final element in the array
  552. */
  553. if (ovcs->symbols_fragment)
  554. fragments_count = ovcs->count - 1;
  555. else
  556. fragments_count = ovcs->count;
  557. for (i = 0; i < fragments_count; i++) {
  558. fragment = &ovcs->fragments[i];
  559. target.np = fragment->target;
  560. target.in_livetree = true;
  561. ret = build_changeset_next_level(ovcs, &target,
  562. fragment->overlay);
  563. if (ret) {
  564. pr_debug("fragment apply failed '%pOF'\n",
  565. fragment->target);
  566. return ret;
  567. }
  568. }
  569. if (ovcs->symbols_fragment) {
  570. fragment = &ovcs->fragments[ovcs->count - 1];
  571. target.np = fragment->target;
  572. target.in_livetree = true;
  573. ret = build_changeset_symbols_node(ovcs, &target,
  574. fragment->overlay);
  575. if (ret) {
  576. pr_debug("symbols fragment apply failed '%pOF'\n",
  577. fragment->target);
  578. return ret;
  579. }
  580. }
  581. return changeset_dup_entry_check(ovcs);
  582. }
  583. /*
  584. * Find the target node using a number of different strategies
  585. * in order of preference:
  586. *
  587. * 1) "target" property containing the phandle of the target
  588. * 2) "target-path" property containing the path of the target
  589. */
  590. static struct device_node *find_target(struct device_node *info_node,
  591. struct device_node *target_base)
  592. {
  593. struct device_node *node;
  594. char *target_path;
  595. const char *path;
  596. u32 val;
  597. int ret;
  598. ret = of_property_read_u32(info_node, "target", &val);
  599. if (!ret) {
  600. node = of_find_node_by_phandle(val);
  601. if (!node)
  602. pr_err("find target, node: %pOF, phandle 0x%x not found\n",
  603. info_node, val);
  604. return node;
  605. }
  606. ret = of_property_read_string(info_node, "target-path", &path);
  607. if (!ret) {
  608. if (target_base) {
  609. target_path = kasprintf(GFP_KERNEL, "%pOF%s", target_base, path);
  610. if (!target_path)
  611. return NULL;
  612. node = of_find_node_by_path(target_path);
  613. if (!node) {
  614. pr_err("find target, node: %pOF, path '%s' not found\n",
  615. info_node, target_path);
  616. }
  617. kfree(target_path);
  618. } else {
  619. node = of_find_node_by_path(path);
  620. if (!node) {
  621. pr_err("find target, node: %pOF, path '%s' not found\n",
  622. info_node, path);
  623. }
  624. }
  625. return node;
  626. }
  627. pr_err("find target, node: %pOF, no target property\n", info_node);
  628. return NULL;
  629. }
  630. /**
  631. * init_overlay_changeset() - initialize overlay changeset from overlay tree
  632. * @ovcs: Overlay changeset to build
  633. * @target_base: Point to the target node to apply overlay
  634. *
  635. * Initialize @ovcs. Populate @ovcs->fragments with node information from
  636. * the top level of @overlay_root. The relevant top level nodes are the
  637. * fragment nodes and the __symbols__ node. Any other top level node will
  638. * be ignored. Populate other @ovcs fields.
  639. *
  640. * Return: 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
  641. * detected in @overlay_root. On error return, the caller of
  642. * init_overlay_changeset() must call free_overlay_changeset().
  643. */
  644. static int init_overlay_changeset(struct overlay_changeset *ovcs,
  645. struct device_node *target_base)
  646. {
  647. struct device_node *node, *overlay_node;
  648. struct fragment *fragment;
  649. struct fragment *fragments;
  650. int cnt, ret;
  651. /*
  652. * None of the resources allocated by this function will be freed in
  653. * the error paths. Instead the caller of this function is required
  654. * to call free_overlay_changeset() (which will free the resources)
  655. * if error return.
  656. */
  657. /*
  658. * Warn for some issues. Can not return -EINVAL for these until
  659. * of_unittest_apply_overlay() is fixed to pass these checks.
  660. */
  661. if (!of_node_check_flag(ovcs->overlay_root, OF_DYNAMIC))
  662. pr_debug("%s() ovcs->overlay_root is not dynamic\n", __func__);
  663. if (!of_node_check_flag(ovcs->overlay_root, OF_DETACHED))
  664. pr_debug("%s() ovcs->overlay_root is not detached\n", __func__);
  665. if (!of_node_is_root(ovcs->overlay_root))
  666. pr_debug("%s() ovcs->overlay_root is not root\n", __func__);
  667. cnt = 0;
  668. /* fragment nodes */
  669. for_each_child_of_node(ovcs->overlay_root, node) {
  670. overlay_node = of_get_child_by_name(node, "__overlay__");
  671. if (overlay_node) {
  672. cnt++;
  673. of_node_put(overlay_node);
  674. }
  675. }
  676. node = of_get_child_by_name(ovcs->overlay_root, "__symbols__");
  677. if (node) {
  678. cnt++;
  679. of_node_put(node);
  680. }
  681. fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
  682. if (!fragments) {
  683. ret = -ENOMEM;
  684. goto err_out;
  685. }
  686. ovcs->fragments = fragments;
  687. cnt = 0;
  688. for_each_child_of_node(ovcs->overlay_root, node) {
  689. overlay_node = of_get_child_by_name(node, "__overlay__");
  690. if (!overlay_node)
  691. continue;
  692. fragment = &fragments[cnt];
  693. fragment->overlay = overlay_node;
  694. fragment->target = find_target(node, target_base);
  695. if (!fragment->target) {
  696. of_node_put(fragment->overlay);
  697. ret = -EINVAL;
  698. of_node_put(node);
  699. goto err_out;
  700. }
  701. cnt++;
  702. }
  703. /*
  704. * if there is a symbols fragment in ovcs->fragments[i] it is
  705. * the final element in the array
  706. */
  707. node = of_get_child_by_name(ovcs->overlay_root, "__symbols__");
  708. if (node) {
  709. ovcs->symbols_fragment = 1;
  710. fragment = &fragments[cnt];
  711. fragment->overlay = node;
  712. fragment->target = of_find_node_by_path("/__symbols__");
  713. if (!fragment->target) {
  714. pr_err("symbols in overlay, but not in live tree\n");
  715. ret = -EINVAL;
  716. of_node_put(node);
  717. goto err_out;
  718. }
  719. cnt++;
  720. }
  721. if (!cnt) {
  722. pr_err("no fragments or symbols in overlay\n");
  723. ret = -EINVAL;
  724. goto err_out;
  725. }
  726. ovcs->count = cnt;
  727. return 0;
  728. err_out:
  729. pr_err("%s() failed, ret = %d\n", __func__, ret);
  730. return ret;
  731. }
  732. static void free_overlay_changeset(struct overlay_changeset *ovcs)
  733. {
  734. int i;
  735. if (ovcs->cset.entries.next)
  736. of_changeset_destroy(&ovcs->cset);
  737. if (ovcs->id) {
  738. idr_remove(&ovcs_idr, ovcs->id);
  739. list_del(&ovcs->ovcs_list);
  740. ovcs->id = 0;
  741. }
  742. for (i = 0; i < ovcs->count; i++) {
  743. of_node_put(ovcs->fragments[i].target);
  744. of_node_put(ovcs->fragments[i].overlay);
  745. }
  746. kfree(ovcs->fragments);
  747. /*
  748. * There should be no live pointers into ovcs->overlay_mem and
  749. * ovcs->new_fdt due to the policy that overlay notifiers are not
  750. * allowed to retain pointers into the overlay devicetree other
  751. * than during the window from OF_OVERLAY_PRE_APPLY overlay
  752. * notifiers until the OF_OVERLAY_POST_REMOVE overlay notifiers.
  753. *
  754. * A memory leak will occur here if within the window.
  755. */
  756. if (ovcs->notify_state == OF_OVERLAY_INIT ||
  757. ovcs->notify_state == OF_OVERLAY_POST_REMOVE) {
  758. kfree(ovcs->overlay_mem);
  759. kfree(ovcs->new_fdt);
  760. }
  761. kfree(ovcs);
  762. }
  763. /*
  764. * internal documentation
  765. *
  766. * of_overlay_apply() - Create and apply an overlay changeset
  767. * @ovcs: overlay changeset
  768. * @base: point to the target node to apply overlay
  769. *
  770. * Creates and applies an overlay changeset.
  771. *
  772. * If an error is returned by an overlay changeset pre-apply notifier
  773. * then no further overlay changeset pre-apply notifier will be called.
  774. *
  775. * If an error is returned by an overlay changeset post-apply notifier
  776. * then no further overlay changeset post-apply notifier will be called.
  777. *
  778. * If more than one notifier returns an error, then the last notifier
  779. * error to occur is returned.
  780. *
  781. * If an error occurred while applying the overlay changeset, then an
  782. * attempt is made to revert any changes that were made to the
  783. * device tree. If there were any errors during the revert attempt
  784. * then the state of the device tree can not be determined, and any
  785. * following attempt to apply or remove an overlay changeset will be
  786. * refused.
  787. *
  788. * Returns 0 on success, or a negative error number. On error return,
  789. * the caller of of_overlay_apply() must call free_overlay_changeset().
  790. */
  791. static int of_overlay_apply(struct overlay_changeset *ovcs,
  792. struct device_node *base)
  793. {
  794. int ret = 0, ret_revert, ret_tmp;
  795. ret = of_resolve_phandles(ovcs->overlay_root);
  796. if (ret)
  797. goto out;
  798. ret = init_overlay_changeset(ovcs, base);
  799. if (ret)
  800. goto out;
  801. ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
  802. if (ret)
  803. goto out;
  804. ret = build_changeset(ovcs);
  805. if (ret)
  806. goto out;
  807. ret_revert = 0;
  808. ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
  809. if (ret) {
  810. if (ret_revert) {
  811. pr_debug("overlay changeset revert error %d\n",
  812. ret_revert);
  813. devicetree_state_flags |= DTSF_APPLY_FAIL;
  814. }
  815. goto out;
  816. }
  817. ret = __of_changeset_apply_notify(&ovcs->cset);
  818. if (ret)
  819. pr_err("overlay apply changeset entry notify error %d\n", ret);
  820. /* notify failure is not fatal, continue */
  821. ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
  822. if (ret_tmp)
  823. if (!ret)
  824. ret = ret_tmp;
  825. out:
  826. pr_debug("%s() err=%d\n", __func__, ret);
  827. return ret;
  828. }
  829. /**
  830. * of_overlay_fdt_apply() - Create and apply an overlay changeset
  831. * @overlay_fdt: pointer to overlay FDT
  832. * @overlay_fdt_size: number of bytes in @overlay_fdt
  833. * @ret_ovcs_id: pointer for returning created changeset id
  834. * @base: pointer for the target node to apply overlay
  835. *
  836. * Creates and applies an overlay changeset.
  837. *
  838. * See of_overlay_apply() for important behavior information.
  839. *
  840. * Return: 0 on success, or a negative error number. *@ret_ovcs_id is set to
  841. * the value of overlay changeset id, which can be passed to of_overlay_remove()
  842. * to remove the overlay.
  843. *
  844. * On error return, the changeset may be partially applied. This is especially
  845. * likely if an OF_OVERLAY_POST_APPLY notifier returns an error. In this case
  846. * the caller should call of_overlay_remove() with the value in *@ret_ovcs_id.
  847. */
  848. int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
  849. int *ret_ovcs_id, struct device_node *base)
  850. {
  851. void *new_fdt;
  852. void *new_fdt_align;
  853. void *overlay_mem;
  854. int ret;
  855. u32 size;
  856. struct overlay_changeset *ovcs;
  857. *ret_ovcs_id = 0;
  858. if (devicetree_corrupt()) {
  859. pr_err("devicetree state suspect, refuse to apply overlay\n");
  860. return -EBUSY;
  861. }
  862. if (overlay_fdt_size < sizeof(struct fdt_header) ||
  863. fdt_check_header(overlay_fdt)) {
  864. pr_err("Invalid overlay_fdt header\n");
  865. return -EINVAL;
  866. }
  867. size = fdt_totalsize(overlay_fdt);
  868. if (overlay_fdt_size < size)
  869. return -EINVAL;
  870. ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
  871. if (!ovcs)
  872. return -ENOMEM;
  873. of_overlay_mutex_lock();
  874. mutex_lock(&of_mutex);
  875. /*
  876. * ovcs->notify_state must be set to OF_OVERLAY_INIT before allocating
  877. * ovcs resources, implicitly set by kzalloc() of ovcs
  878. */
  879. ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
  880. if (ovcs->id <= 0) {
  881. ret = ovcs->id;
  882. goto err_free_ovcs;
  883. }
  884. INIT_LIST_HEAD(&ovcs->ovcs_list);
  885. list_add_tail(&ovcs->ovcs_list, &ovcs_list);
  886. of_changeset_init(&ovcs->cset);
  887. /*
  888. * Must create permanent copy of FDT because of_fdt_unflatten_tree()
  889. * will create pointers to the passed in FDT in the unflattened tree.
  890. */
  891. new_fdt = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
  892. if (!new_fdt) {
  893. ret = -ENOMEM;
  894. goto err_free_ovcs;
  895. }
  896. ovcs->new_fdt = new_fdt;
  897. new_fdt_align = PTR_ALIGN(new_fdt, FDT_ALIGN_SIZE);
  898. memcpy(new_fdt_align, overlay_fdt, size);
  899. overlay_mem = of_fdt_unflatten_tree(new_fdt_align, NULL,
  900. &ovcs->overlay_root);
  901. if (!overlay_mem) {
  902. pr_err("unable to unflatten overlay_fdt\n");
  903. ret = -EINVAL;
  904. goto err_free_ovcs;
  905. }
  906. ovcs->overlay_mem = overlay_mem;
  907. ret = of_overlay_apply(ovcs, base);
  908. /*
  909. * If of_overlay_apply() error, calling free_overlay_changeset() may
  910. * result in a memory leak if the apply partly succeeded, so do NOT
  911. * goto err_free_ovcs. Instead, the caller of of_overlay_fdt_apply()
  912. * can call of_overlay_remove();
  913. */
  914. *ret_ovcs_id = ovcs->id;
  915. goto out_unlock;
  916. err_free_ovcs:
  917. free_overlay_changeset(ovcs);
  918. out_unlock:
  919. mutex_unlock(&of_mutex);
  920. of_overlay_mutex_unlock();
  921. return ret;
  922. }
  923. EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
  924. /*
  925. * Find @np in @tree.
  926. *
  927. * Returns 1 if @np is @tree or is contained in @tree, else 0
  928. */
  929. static int find_node(struct device_node *tree, struct device_node *np)
  930. {
  931. if (tree == np)
  932. return 1;
  933. for_each_child_of_node_scoped(tree, child) {
  934. if (find_node(child, np))
  935. return 1;
  936. }
  937. return 0;
  938. }
  939. /*
  940. * Is @remove_ce_node a child of, a parent of, or the same as any
  941. * node in an overlay changeset more topmost than @remove_ovcs?
  942. *
  943. * Returns 1 if found, else 0
  944. */
  945. static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
  946. struct device_node *remove_ce_node)
  947. {
  948. struct overlay_changeset *ovcs;
  949. struct of_changeset_entry *ce;
  950. list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
  951. if (ovcs == remove_ovcs)
  952. break;
  953. list_for_each_entry(ce, &ovcs->cset.entries, node) {
  954. if (find_node(ce->np, remove_ce_node)) {
  955. pr_err("%s: #%d overlaps with #%d @%pOF\n",
  956. __func__, remove_ovcs->id, ovcs->id,
  957. remove_ce_node);
  958. return 1;
  959. }
  960. if (find_node(remove_ce_node, ce->np)) {
  961. pr_err("%s: #%d overlaps with #%d @%pOF\n",
  962. __func__, remove_ovcs->id, ovcs->id,
  963. remove_ce_node);
  964. return 1;
  965. }
  966. }
  967. }
  968. return 0;
  969. }
  970. /*
  971. * We can safely remove the overlay only if it's the top-most one.
  972. * Newly applied overlays are inserted at the tail of the overlay list,
  973. * so a top most overlay is the one that is closest to the tail.
  974. *
  975. * The topmost check is done by exploiting this property. For each
  976. * affected device node in the log list we check if this overlay is
  977. * the one closest to the tail. If another overlay has affected this
  978. * device node and is closest to the tail, then removal is not permitted.
  979. */
  980. static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
  981. {
  982. struct of_changeset_entry *remove_ce;
  983. list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
  984. if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
  985. pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
  986. return 0;
  987. }
  988. }
  989. return 1;
  990. }
  991. /**
  992. * of_overlay_remove() - Revert and free an overlay changeset
  993. * @ovcs_id: Pointer to overlay changeset id
  994. *
  995. * Removes an overlay if it is permissible. @ovcs_id was previously returned
  996. * by of_overlay_fdt_apply().
  997. *
  998. * If an error occurred while attempting to revert the overlay changeset,
  999. * then an attempt is made to re-apply any changeset entry that was
  1000. * reverted. If an error occurs on re-apply then the state of the device
  1001. * tree can not be determined, and any following attempt to apply or remove
  1002. * an overlay changeset will be refused.
  1003. *
  1004. * A non-zero return value will not revert the changeset if error is from:
  1005. * - parameter checks
  1006. * - overlay changeset pre-remove notifier
  1007. * - overlay changeset entry revert
  1008. *
  1009. * If an error is returned by an overlay changeset pre-remove notifier
  1010. * then no further overlay changeset pre-remove notifier will be called.
  1011. *
  1012. * If more than one notifier returns an error, then the last notifier
  1013. * error to occur is returned.
  1014. *
  1015. * A non-zero return value will revert the changeset if error is from:
  1016. * - overlay changeset entry notifier
  1017. * - overlay changeset post-remove notifier
  1018. *
  1019. * If an error is returned by an overlay changeset post-remove notifier
  1020. * then no further overlay changeset post-remove notifier will be called.
  1021. *
  1022. * Return: 0 on success, or a negative error number. *@ovcs_id is set to
  1023. * zero after reverting the changeset, even if a subsequent error occurs.
  1024. */
  1025. int of_overlay_remove(int *ovcs_id)
  1026. {
  1027. struct overlay_changeset *ovcs;
  1028. int ret, ret_apply, ret_tmp;
  1029. if (devicetree_corrupt()) {
  1030. pr_err("suspect devicetree state, refuse to remove overlay\n");
  1031. ret = -EBUSY;
  1032. goto out;
  1033. }
  1034. mutex_lock(&of_mutex);
  1035. ovcs = idr_find(&ovcs_idr, *ovcs_id);
  1036. if (!ovcs) {
  1037. ret = -ENODEV;
  1038. pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
  1039. goto err_unlock;
  1040. }
  1041. if (!overlay_removal_is_ok(ovcs)) {
  1042. ret = -EBUSY;
  1043. goto err_unlock;
  1044. }
  1045. ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
  1046. if (ret)
  1047. goto err_unlock;
  1048. ret_apply = 0;
  1049. ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
  1050. if (ret) {
  1051. if (ret_apply)
  1052. devicetree_state_flags |= DTSF_REVERT_FAIL;
  1053. goto err_unlock;
  1054. }
  1055. ret = __of_changeset_revert_notify(&ovcs->cset);
  1056. if (ret)
  1057. pr_err("overlay remove changeset entry notify error %d\n", ret);
  1058. /* notify failure is not fatal, continue */
  1059. *ovcs_id = 0;
  1060. /*
  1061. * Note that the overlay memory will be kfree()ed by
  1062. * free_overlay_changeset() even if the notifier for
  1063. * OF_OVERLAY_POST_REMOVE returns an error.
  1064. */
  1065. ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
  1066. if (ret_tmp)
  1067. if (!ret)
  1068. ret = ret_tmp;
  1069. free_overlay_changeset(ovcs);
  1070. err_unlock:
  1071. /*
  1072. * If jumped over free_overlay_changeset(), then did not kfree()
  1073. * overlay related memory. This is a memory leak unless a subsequent
  1074. * of_overlay_remove() of this overlay is successful.
  1075. */
  1076. mutex_unlock(&of_mutex);
  1077. out:
  1078. pr_debug("%s() err=%d\n", __func__, ret);
  1079. return ret;
  1080. }
  1081. EXPORT_SYMBOL_GPL(of_overlay_remove);
  1082. /**
  1083. * of_overlay_remove_all() - Reverts and frees all overlay changesets
  1084. *
  1085. * Removes all overlays from the system in the correct order.
  1086. *
  1087. * Return: 0 on success, or a negative error number
  1088. */
  1089. int of_overlay_remove_all(void)
  1090. {
  1091. struct overlay_changeset *ovcs, *ovcs_n;
  1092. int ret;
  1093. /* the tail of list is guaranteed to be safe to remove */
  1094. list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
  1095. ret = of_overlay_remove(&ovcs->id);
  1096. if (ret)
  1097. return ret;
  1098. }
  1099. return 0;
  1100. }
  1101. EXPORT_SYMBOL_GPL(of_overlay_remove_all);