overlay.c 28 KB

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