overlay.c 29 KB

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