overlay.c 32 KB

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