overlay.c 32 KB

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