overlay.c 35 KB

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