overlay.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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/%pOFn, err=%d\n",
  374. target_node, child, 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. of_populate_phandle_cache();
  697. ret = __of_changeset_apply_notify(&ovcs->cset);
  698. if (ret)
  699. pr_err("overlay changeset entry notify error %d\n", ret);
  700. /* notify failure is not fatal, continue */
  701. list_add_tail(&ovcs->ovcs_list, &ovcs_list);
  702. *ovcs_id = ovcs->id;
  703. ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
  704. if (ret_tmp) {
  705. pr_err("overlay changeset post-apply notify error %d\n",
  706. ret_tmp);
  707. if (!ret)
  708. ret = ret_tmp;
  709. }
  710. goto out_unlock;
  711. err_free_tree:
  712. kfree(fdt);
  713. kfree(tree);
  714. err_free_overlay_changeset:
  715. free_overlay_changeset(ovcs);
  716. out_unlock:
  717. mutex_unlock(&of_mutex);
  718. of_overlay_mutex_unlock();
  719. out:
  720. pr_debug("%s() err=%d\n", __func__, ret);
  721. return ret;
  722. }
  723. int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
  724. int *ovcs_id)
  725. {
  726. const void *new_fdt;
  727. int ret;
  728. u32 size;
  729. struct device_node *overlay_root;
  730. *ovcs_id = 0;
  731. ret = 0;
  732. if (overlay_fdt_size < sizeof(struct fdt_header) ||
  733. fdt_check_header(overlay_fdt)) {
  734. pr_err("Invalid overlay_fdt header\n");
  735. return -EINVAL;
  736. }
  737. size = fdt_totalsize(overlay_fdt);
  738. if (overlay_fdt_size < size)
  739. return -EINVAL;
  740. /*
  741. * Must create permanent copy of FDT because of_fdt_unflatten_tree()
  742. * will create pointers to the passed in FDT in the unflattened tree.
  743. */
  744. new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
  745. if (!new_fdt)
  746. return -ENOMEM;
  747. of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
  748. if (!overlay_root) {
  749. pr_err("unable to unflatten overlay_fdt\n");
  750. ret = -EINVAL;
  751. goto out_free_new_fdt;
  752. }
  753. ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
  754. if (ret < 0) {
  755. /*
  756. * new_fdt and overlay_root now belong to the overlay
  757. * changeset.
  758. * overlay changeset code is responsible for freeing them.
  759. */
  760. goto out;
  761. }
  762. return 0;
  763. out_free_new_fdt:
  764. kfree(new_fdt);
  765. out:
  766. return ret;
  767. }
  768. EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
  769. /*
  770. * Find @np in @tree.
  771. *
  772. * Returns 1 if @np is @tree or is contained in @tree, else 0
  773. */
  774. static int find_node(struct device_node *tree, struct device_node *np)
  775. {
  776. struct device_node *child;
  777. if (tree == np)
  778. return 1;
  779. for_each_child_of_node(tree, child) {
  780. if (find_node(child, np)) {
  781. of_node_put(child);
  782. return 1;
  783. }
  784. }
  785. return 0;
  786. }
  787. /*
  788. * Is @remove_ce_node a child of, a parent of, or the same as any
  789. * node in an overlay changeset more topmost than @remove_ovcs?
  790. *
  791. * Returns 1 if found, else 0
  792. */
  793. static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
  794. struct device_node *remove_ce_node)
  795. {
  796. struct overlay_changeset *ovcs;
  797. struct of_changeset_entry *ce;
  798. list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
  799. if (ovcs == remove_ovcs)
  800. break;
  801. list_for_each_entry(ce, &ovcs->cset.entries, node) {
  802. if (find_node(ce->np, remove_ce_node)) {
  803. pr_err("%s: #%d overlaps with #%d @%pOF\n",
  804. __func__, remove_ovcs->id, ovcs->id,
  805. remove_ce_node);
  806. return 1;
  807. }
  808. if (find_node(remove_ce_node, ce->np)) {
  809. pr_err("%s: #%d overlaps with #%d @%pOF\n",
  810. __func__, remove_ovcs->id, ovcs->id,
  811. remove_ce_node);
  812. return 1;
  813. }
  814. }
  815. }
  816. return 0;
  817. }
  818. /*
  819. * We can safely remove the overlay only if it's the top-most one.
  820. * Newly applied overlays are inserted at the tail of the overlay list,
  821. * so a top most overlay is the one that is closest to the tail.
  822. *
  823. * The topmost check is done by exploiting this property. For each
  824. * affected device node in the log list we check if this overlay is
  825. * the one closest to the tail. If another overlay has affected this
  826. * device node and is closest to the tail, then removal is not permited.
  827. */
  828. static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
  829. {
  830. struct of_changeset_entry *remove_ce;
  831. list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
  832. if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
  833. pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
  834. return 0;
  835. }
  836. }
  837. return 1;
  838. }
  839. /**
  840. * of_overlay_remove() - Revert and free an overlay changeset
  841. * @ovcs_id: Pointer to overlay changeset id
  842. *
  843. * Removes an overlay if it is permissible. @ovcs_id was previously returned
  844. * by of_overlay_fdt_apply().
  845. *
  846. * If an error occurred while attempting to revert the overlay changeset,
  847. * then an attempt is made to re-apply any changeset entry that was
  848. * reverted. If an error occurs on re-apply then the state of the device
  849. * tree can not be determined, and any following attempt to apply or remove
  850. * an overlay changeset will be refused.
  851. *
  852. * A non-zero return value will not revert the changeset if error is from:
  853. * - parameter checks
  854. * - overlay changeset pre-remove notifier
  855. * - overlay changeset entry revert
  856. *
  857. * If an error is returned by an overlay changeset pre-remove notifier
  858. * then no further overlay changeset pre-remove notifier will be called.
  859. *
  860. * If more than one notifier returns an error, then the last notifier
  861. * error to occur is returned.
  862. *
  863. * A non-zero return value will revert the changeset if error is from:
  864. * - overlay changeset entry notifier
  865. * - overlay changeset post-remove notifier
  866. *
  867. * If an error is returned by an overlay changeset post-remove notifier
  868. * then no further overlay changeset post-remove notifier will be called.
  869. *
  870. * Returns 0 on success, or a negative error number. *ovcs_id is set to
  871. * zero after reverting the changeset, even if a subsequent error occurs.
  872. */
  873. int of_overlay_remove(int *ovcs_id)
  874. {
  875. struct overlay_changeset *ovcs;
  876. int ret, ret_apply, ret_tmp;
  877. ret = 0;
  878. if (devicetree_corrupt()) {
  879. pr_err("suspect devicetree state, refuse to remove overlay\n");
  880. ret = -EBUSY;
  881. goto out;
  882. }
  883. mutex_lock(&of_mutex);
  884. ovcs = idr_find(&ovcs_idr, *ovcs_id);
  885. if (!ovcs) {
  886. ret = -ENODEV;
  887. pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
  888. goto out_unlock;
  889. }
  890. if (!overlay_removal_is_ok(ovcs)) {
  891. ret = -EBUSY;
  892. goto out_unlock;
  893. }
  894. ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
  895. if (ret) {
  896. pr_err("overlay changeset pre-remove notify error %d\n", ret);
  897. goto out_unlock;
  898. }
  899. list_del(&ovcs->ovcs_list);
  900. /*
  901. * Disable phandle cache. Avoids race condition that would arise
  902. * from removing cache entry when the associated node is deleted.
  903. */
  904. of_free_phandle_cache();
  905. ret_apply = 0;
  906. ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
  907. of_populate_phandle_cache();
  908. if (ret) {
  909. if (ret_apply)
  910. devicetree_state_flags |= DTSF_REVERT_FAIL;
  911. goto out_unlock;
  912. }
  913. ret = __of_changeset_revert_notify(&ovcs->cset);
  914. if (ret)
  915. pr_err("overlay changeset entry notify error %d\n", ret);
  916. /* notify failure is not fatal, continue */
  917. *ovcs_id = 0;
  918. ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
  919. if (ret_tmp) {
  920. pr_err("overlay changeset post-remove notify error %d\n",
  921. ret_tmp);
  922. if (!ret)
  923. ret = ret_tmp;
  924. }
  925. free_overlay_changeset(ovcs);
  926. out_unlock:
  927. mutex_unlock(&of_mutex);
  928. out:
  929. pr_debug("%s() err=%d\n", __func__, ret);
  930. return ret;
  931. }
  932. EXPORT_SYMBOL_GPL(of_overlay_remove);
  933. /**
  934. * of_overlay_remove_all() - Reverts and frees all overlay changesets
  935. *
  936. * Removes all overlays from the system in the correct order.
  937. *
  938. * Returns 0 on success, or a negative error number
  939. */
  940. int of_overlay_remove_all(void)
  941. {
  942. struct overlay_changeset *ovcs, *ovcs_n;
  943. int ret;
  944. /* the tail of list is guaranteed to be safe to remove */
  945. list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
  946. ret = of_overlay_remove(&ovcs->id);
  947. if (ret)
  948. return ret;
  949. }
  950. return 0;
  951. }
  952. EXPORT_SYMBOL_GPL(of_overlay_remove_all);