overlay.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. kfree(target_path);
  230. return new_prop;
  231. err_free_new_prop:
  232. kfree(new_prop->name);
  233. kfree(new_prop->value);
  234. kfree(new_prop);
  235. err_free_target_path:
  236. kfree(target_path);
  237. return NULL;
  238. }
  239. /**
  240. * add_changeset_property() - add @overlay_prop to overlay changeset
  241. * @ovcs: overlay changeset
  242. * @target: where @overlay_prop will be placed
  243. * @overlay_prop: property to add or update, from overlay tree
  244. * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
  245. *
  246. * If @overlay_prop does not already exist in live devicetree, add changeset
  247. * entry to add @overlay_prop in @target, else add changeset entry to update
  248. * value of @overlay_prop.
  249. *
  250. * @target may be either in the live devicetree or in a new subtree that
  251. * is contained in the changeset.
  252. *
  253. * Some special properties are not added or updated (no error returned):
  254. * "name", "phandle", "linux,phandle".
  255. *
  256. * Properties "#address-cells" and "#size-cells" are not updated if they
  257. * are already in the live tree, but if present in the live tree, the values
  258. * in the overlay must match the values in the live tree.
  259. *
  260. * Update of property in symbols node is not allowed.
  261. *
  262. * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  263. * invalid @overlay.
  264. */
  265. static int add_changeset_property(struct overlay_changeset *ovcs,
  266. struct target *target, struct property *overlay_prop,
  267. bool is_symbols_prop)
  268. {
  269. struct property *new_prop = NULL, *prop;
  270. int ret = 0;
  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 (prop) {
  281. if (!of_prop_cmp(prop->name, "#address-cells")) {
  282. if (!of_prop_val_eq(prop, overlay_prop)) {
  283. pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",
  284. target->np);
  285. ret = -EINVAL;
  286. }
  287. return ret;
  288. } else if (!of_prop_cmp(prop->name, "#size-cells")) {
  289. if (!of_prop_val_eq(prop, overlay_prop)) {
  290. pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",
  291. target->np);
  292. ret = -EINVAL;
  293. }
  294. return ret;
  295. }
  296. }
  297. if (is_symbols_prop) {
  298. if (prop)
  299. return -EINVAL;
  300. new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
  301. } else {
  302. new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
  303. }
  304. if (!new_prop)
  305. return -ENOMEM;
  306. if (!prop) {
  307. if (!target->in_livetree) {
  308. new_prop->next = target->np->deadprops;
  309. target->np->deadprops = new_prop;
  310. }
  311. ret = of_changeset_add_property(&ovcs->cset, target->np,
  312. new_prop);
  313. } else {
  314. ret = of_changeset_update_property(&ovcs->cset, target->np,
  315. new_prop);
  316. }
  317. if (!of_node_check_flag(target->np, OF_OVERLAY))
  318. pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
  319. target->np, new_prop->name);
  320. if (ret) {
  321. kfree(new_prop->name);
  322. kfree(new_prop->value);
  323. kfree(new_prop);
  324. }
  325. return ret;
  326. }
  327. /**
  328. * add_changeset_node() - add @node (and children) to overlay changeset
  329. * @ovcs: overlay changeset
  330. * @target: where @node will be placed in live tree or changeset
  331. * @node: node from within overlay device tree fragment
  332. *
  333. * If @node does not already exist in @target, add changeset entry
  334. * to add @node in @target.
  335. *
  336. * If @node already exists in @target, and the existing node has
  337. * a phandle, the overlay node is not allowed to have a phandle.
  338. *
  339. * If @node has child nodes, add the children recursively via
  340. * build_changeset_next_level().
  341. *
  342. * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
  343. * not contain the full path in node->full_name. Thus an overlay
  344. * created from an FDT also will not contain the full path in
  345. * node->full_name. However, a live devicetree created from Open
  346. * Firmware may have the full path in node->full_name.
  347. *
  348. * add_changeset_node() follows the FDT convention and does not include
  349. * the full path in node->full_name. Even though it expects the overlay
  350. * to not contain the full path, it uses kbasename() to remove the
  351. * full path should it exist. It also uses kbasename() in comparisons
  352. * to nodes in the live devicetree so that it can apply an overlay to
  353. * a live devicetree created from Open Firmware.
  354. *
  355. * NOTE_2: Multiple mods of created nodes not supported.
  356. * If more than one fragment contains a node that does not already exist
  357. * in the live tree, then for each fragment of_changeset_attach_node()
  358. * will add a changeset entry to add the node. When the changeset is
  359. * applied, __of_attach_node() will attach the node twice (once for
  360. * each fragment). At this point the device tree will be corrupted.
  361. *
  362. * TODO: add integrity check to ensure that multiple fragments do not
  363. * create the same node.
  364. *
  365. * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  366. * invalid @overlay.
  367. */
  368. static int add_changeset_node(struct overlay_changeset *ovcs,
  369. struct target *target, struct device_node *node)
  370. {
  371. const char *node_kbasename;
  372. const __be32 *phandle;
  373. struct device_node *tchild;
  374. struct target target_child;
  375. int ret = 0, size;
  376. node_kbasename = kbasename(node->full_name);
  377. for_each_child_of_node(target->np, tchild)
  378. if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
  379. break;
  380. if (!tchild) {
  381. tchild = __of_node_dup(NULL, node_kbasename);
  382. if (!tchild)
  383. return -ENOMEM;
  384. tchild->parent = target->np;
  385. tchild->name = __of_get_property(node, "name", NULL);
  386. tchild->type = __of_get_property(node, "device_type", NULL);
  387. if (!tchild->name)
  388. tchild->name = "<NULL>";
  389. if (!tchild->type)
  390. tchild->type = "<NULL>";
  391. /* ignore obsolete "linux,phandle" */
  392. phandle = __of_get_property(node, "phandle", &size);
  393. if (phandle && (size == 4))
  394. tchild->phandle = be32_to_cpup(phandle);
  395. of_node_set_flag(tchild, OF_OVERLAY);
  396. ret = of_changeset_attach_node(&ovcs->cset, tchild);
  397. if (ret)
  398. return ret;
  399. target_child.np = tchild;
  400. target_child.in_livetree = false;
  401. ret = build_changeset_next_level(ovcs, &target_child, node);
  402. of_node_put(tchild);
  403. return ret;
  404. }
  405. if (node->phandle && tchild->phandle) {
  406. ret = -EINVAL;
  407. } else {
  408. target_child.np = tchild;
  409. target_child.in_livetree = target->in_livetree;
  410. ret = build_changeset_next_level(ovcs, &target_child, node);
  411. }
  412. of_node_put(tchild);
  413. return ret;
  414. }
  415. /**
  416. * build_changeset_next_level() - add level of overlay changeset
  417. * @ovcs: overlay changeset
  418. * @target: where to place @overlay_node in live tree
  419. * @overlay_node: node from within an overlay device tree fragment
  420. *
  421. * Add the properties (if any) and nodes (if any) from @overlay_node to the
  422. * @ovcs->cset changeset. If an added node has child nodes, they will
  423. * be added recursively.
  424. *
  425. * Do not allow symbols node to have any children.
  426. *
  427. * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  428. * invalid @overlay_node.
  429. */
  430. static int build_changeset_next_level(struct overlay_changeset *ovcs,
  431. struct target *target, const struct device_node *overlay_node)
  432. {
  433. struct device_node *child;
  434. struct property *prop;
  435. int ret;
  436. for_each_property_of_node(overlay_node, prop) {
  437. ret = add_changeset_property(ovcs, target, prop, 0);
  438. if (ret) {
  439. pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
  440. target->np, prop->name, ret);
  441. return ret;
  442. }
  443. }
  444. for_each_child_of_node(overlay_node, child) {
  445. ret = add_changeset_node(ovcs, target, child);
  446. if (ret) {
  447. pr_debug("Failed to apply node @%pOF/%pOFn, err=%d\n",
  448. target->np, child, ret);
  449. of_node_put(child);
  450. return ret;
  451. }
  452. }
  453. return 0;
  454. }
  455. /*
  456. * Add the properties from __overlay__ node to the @ovcs->cset changeset.
  457. */
  458. static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
  459. struct target *target,
  460. const struct device_node *overlay_symbols_node)
  461. {
  462. struct property *prop;
  463. int ret;
  464. for_each_property_of_node(overlay_symbols_node, prop) {
  465. ret = add_changeset_property(ovcs, target, prop, 1);
  466. if (ret) {
  467. pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
  468. target->np, prop->name, ret);
  469. return ret;
  470. }
  471. }
  472. return 0;
  473. }
  474. /**
  475. * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
  476. * @ovcs: Overlay changeset
  477. *
  478. * Create changeset @ovcs->cset to contain the nodes and properties of the
  479. * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
  480. * any portions of the changeset that were successfully created will remain
  481. * in @ovcs->cset.
  482. *
  483. * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
  484. * invalid overlay in @ovcs->fragments[].
  485. */
  486. static int build_changeset(struct overlay_changeset *ovcs)
  487. {
  488. struct fragment *fragment;
  489. struct target target;
  490. int fragments_count, i, ret;
  491. /*
  492. * if there is a symbols fragment in ovcs->fragments[i] it is
  493. * the final element in the array
  494. */
  495. if (ovcs->symbols_fragment)
  496. fragments_count = ovcs->count - 1;
  497. else
  498. fragments_count = ovcs->count;
  499. for (i = 0; i < fragments_count; i++) {
  500. fragment = &ovcs->fragments[i];
  501. target.np = fragment->target;
  502. target.in_livetree = true;
  503. ret = build_changeset_next_level(ovcs, &target,
  504. fragment->overlay);
  505. if (ret) {
  506. pr_debug("apply failed '%pOF'\n", fragment->target);
  507. return ret;
  508. }
  509. }
  510. if (ovcs->symbols_fragment) {
  511. fragment = &ovcs->fragments[ovcs->count - 1];
  512. target.np = fragment->target;
  513. target.in_livetree = true;
  514. ret = build_changeset_symbols_node(ovcs, &target,
  515. fragment->overlay);
  516. if (ret) {
  517. pr_debug("apply failed '%pOF'\n", fragment->target);
  518. return ret;
  519. }
  520. }
  521. return 0;
  522. }
  523. /*
  524. * Find the target node using a number of different strategies
  525. * in order of preference:
  526. *
  527. * 1) "target" property containing the phandle of the target
  528. * 2) "target-path" property containing the path of the target
  529. */
  530. static struct device_node *find_target(struct device_node *info_node)
  531. {
  532. struct device_node *node;
  533. const char *path;
  534. u32 val;
  535. int ret;
  536. ret = of_property_read_u32(info_node, "target", &val);
  537. if (!ret) {
  538. node = of_find_node_by_phandle(val);
  539. if (!node)
  540. pr_err("find target, node: %pOF, phandle 0x%x not found\n",
  541. info_node, val);
  542. return node;
  543. }
  544. ret = of_property_read_string(info_node, "target-path", &path);
  545. if (!ret) {
  546. node = of_find_node_by_path(path);
  547. if (!node)
  548. pr_err("find target, node: %pOF, path '%s' not found\n",
  549. info_node, path);
  550. return node;
  551. }
  552. pr_err("find target, node: %pOF, no target property\n", info_node);
  553. return NULL;
  554. }
  555. /**
  556. * init_overlay_changeset() - initialize overlay changeset from overlay tree
  557. * @ovcs: Overlay changeset to build
  558. * @fdt: the FDT that was unflattened to create @tree
  559. * @tree: Contains all the overlay fragments and overlay fixup nodes
  560. *
  561. * Initialize @ovcs. Populate @ovcs->fragments with node information from
  562. * the top level of @tree. The relevant top level nodes are the fragment
  563. * nodes and the __symbols__ node. Any other top level node will be ignored.
  564. *
  565. * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
  566. * detected in @tree, or -ENOSPC if idr_alloc() error.
  567. */
  568. static int init_overlay_changeset(struct overlay_changeset *ovcs,
  569. const void *fdt, struct device_node *tree)
  570. {
  571. struct device_node *node, *overlay_node;
  572. struct fragment *fragment;
  573. struct fragment *fragments;
  574. int cnt, id, ret;
  575. /*
  576. * Warn for some issues. Can not return -EINVAL for these until
  577. * of_unittest_apply_overlay() is fixed to pass these checks.
  578. */
  579. if (!of_node_check_flag(tree, OF_DYNAMIC))
  580. pr_debug("%s() tree is not dynamic\n", __func__);
  581. if (!of_node_check_flag(tree, OF_DETACHED))
  582. pr_debug("%s() tree is not detached\n", __func__);
  583. if (!of_node_is_root(tree))
  584. pr_debug("%s() tree is not root\n", __func__);
  585. ovcs->overlay_tree = tree;
  586. ovcs->fdt = fdt;
  587. INIT_LIST_HEAD(&ovcs->ovcs_list);
  588. of_changeset_init(&ovcs->cset);
  589. id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
  590. if (id <= 0)
  591. return id;
  592. cnt = 0;
  593. /* fragment nodes */
  594. for_each_child_of_node(tree, node) {
  595. overlay_node = of_get_child_by_name(node, "__overlay__");
  596. if (overlay_node) {
  597. cnt++;
  598. of_node_put(overlay_node);
  599. }
  600. }
  601. node = of_get_child_by_name(tree, "__symbols__");
  602. if (node) {
  603. cnt++;
  604. of_node_put(node);
  605. }
  606. fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
  607. if (!fragments) {
  608. ret = -ENOMEM;
  609. goto err_free_idr;
  610. }
  611. cnt = 0;
  612. for_each_child_of_node(tree, node) {
  613. overlay_node = of_get_child_by_name(node, "__overlay__");
  614. if (!overlay_node)
  615. continue;
  616. fragment = &fragments[cnt];
  617. fragment->overlay = overlay_node;
  618. fragment->target = find_target(node);
  619. if (!fragment->target) {
  620. of_node_put(fragment->overlay);
  621. ret = -EINVAL;
  622. goto err_free_fragments;
  623. }
  624. cnt++;
  625. }
  626. /*
  627. * if there is a symbols fragment in ovcs->fragments[i] it is
  628. * the final element in the array
  629. */
  630. node = of_get_child_by_name(tree, "__symbols__");
  631. if (node) {
  632. ovcs->symbols_fragment = 1;
  633. fragment = &fragments[cnt];
  634. fragment->overlay = node;
  635. fragment->target = of_find_node_by_path("/__symbols__");
  636. if (!fragment->target) {
  637. pr_err("symbols in overlay, but not in live tree\n");
  638. ret = -EINVAL;
  639. goto err_free_fragments;
  640. }
  641. cnt++;
  642. }
  643. if (!cnt) {
  644. pr_err("no fragments or symbols in overlay\n");
  645. ret = -EINVAL;
  646. goto err_free_fragments;
  647. }
  648. ovcs->id = id;
  649. ovcs->count = cnt;
  650. ovcs->fragments = fragments;
  651. return 0;
  652. err_free_fragments:
  653. kfree(fragments);
  654. err_free_idr:
  655. idr_remove(&ovcs_idr, id);
  656. pr_err("%s() failed, ret = %d\n", __func__, ret);
  657. return ret;
  658. }
  659. static void free_overlay_changeset(struct overlay_changeset *ovcs)
  660. {
  661. int i;
  662. if (ovcs->cset.entries.next)
  663. of_changeset_destroy(&ovcs->cset);
  664. if (ovcs->id)
  665. idr_remove(&ovcs_idr, ovcs->id);
  666. for (i = 0; i < ovcs->count; i++) {
  667. of_node_put(ovcs->fragments[i].target);
  668. of_node_put(ovcs->fragments[i].overlay);
  669. }
  670. kfree(ovcs->fragments);
  671. /*
  672. * There should be no live pointers into ovcs->overlay_tree and
  673. * ovcs->fdt due to the policy that overlay notifiers are not allowed
  674. * to retain pointers into the overlay devicetree.
  675. */
  676. kfree(ovcs->overlay_tree);
  677. kfree(ovcs->fdt);
  678. kfree(ovcs);
  679. }
  680. /*
  681. * internal documentation
  682. *
  683. * of_overlay_apply() - Create and apply an overlay changeset
  684. * @fdt: the FDT that was unflattened to create @tree
  685. * @tree: Expanded overlay device tree
  686. * @ovcs_id: Pointer to overlay changeset id
  687. *
  688. * Creates and applies an overlay changeset.
  689. *
  690. * If an error occurs in a pre-apply notifier, then no changes are made
  691. * to the device tree.
  692. *
  693. * A non-zero return value will not have created the changeset if error is from:
  694. * - parameter checks
  695. * - building the changeset
  696. * - overlay changeset pre-apply notifier
  697. *
  698. * If an error is returned by an overlay changeset pre-apply notifier
  699. * then no further overlay changeset pre-apply notifier will be called.
  700. *
  701. * A non-zero return value will have created the changeset if error is from:
  702. * - overlay changeset entry notifier
  703. * - overlay changeset post-apply notifier
  704. *
  705. * If an error is returned by an overlay changeset post-apply notifier
  706. * then no further overlay changeset post-apply notifier will be called.
  707. *
  708. * If more than one notifier returns an error, then the last notifier
  709. * error to occur is returned.
  710. *
  711. * If an error occurred while applying the overlay changeset, then an
  712. * attempt is made to revert any changes that were made to the
  713. * device tree. If there were any errors during the revert attempt
  714. * then the state of the device tree can not be determined, and any
  715. * following attempt to apply or remove an overlay changeset will be
  716. * refused.
  717. *
  718. * Returns 0 on success, or a negative error number. Overlay changeset
  719. * id is returned to *ovcs_id.
  720. */
  721. static int of_overlay_apply(const void *fdt, struct device_node *tree,
  722. int *ovcs_id)
  723. {
  724. struct overlay_changeset *ovcs;
  725. int ret = 0, ret_revert, ret_tmp;
  726. /*
  727. * As of this point, fdt and tree belong to the overlay changeset.
  728. * overlay changeset code is responsible for freeing them.
  729. */
  730. if (devicetree_corrupt()) {
  731. pr_err("devicetree state suspect, refuse to apply overlay\n");
  732. kfree(fdt);
  733. kfree(tree);
  734. ret = -EBUSY;
  735. goto out;
  736. }
  737. ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
  738. if (!ovcs) {
  739. kfree(fdt);
  740. kfree(tree);
  741. ret = -ENOMEM;
  742. goto out;
  743. }
  744. of_overlay_mutex_lock();
  745. mutex_lock(&of_mutex);
  746. ret = of_resolve_phandles(tree);
  747. if (ret)
  748. goto err_free_tree;
  749. ret = init_overlay_changeset(ovcs, fdt, tree);
  750. if (ret)
  751. goto err_free_tree;
  752. /*
  753. * after overlay_notify(), ovcs->overlay_tree related pointers may have
  754. * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
  755. * and can not free fdt, aka ovcs->fdt
  756. */
  757. ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
  758. if (ret) {
  759. pr_err("overlay changeset pre-apply notify error %d\n", ret);
  760. goto err_free_overlay_changeset;
  761. }
  762. ret = build_changeset(ovcs);
  763. if (ret)
  764. goto err_free_overlay_changeset;
  765. ret_revert = 0;
  766. ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
  767. if (ret) {
  768. if (ret_revert) {
  769. pr_debug("overlay changeset revert error %d\n",
  770. ret_revert);
  771. devicetree_state_flags |= DTSF_APPLY_FAIL;
  772. }
  773. goto err_free_overlay_changeset;
  774. }
  775. of_populate_phandle_cache();
  776. ret = __of_changeset_apply_notify(&ovcs->cset);
  777. if (ret)
  778. pr_err("overlay changeset entry notify error %d\n", ret);
  779. /* notify failure is not fatal, continue */
  780. list_add_tail(&ovcs->ovcs_list, &ovcs_list);
  781. *ovcs_id = ovcs->id;
  782. ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
  783. if (ret_tmp) {
  784. pr_err("overlay changeset post-apply notify error %d\n",
  785. ret_tmp);
  786. if (!ret)
  787. ret = ret_tmp;
  788. }
  789. goto out_unlock;
  790. err_free_tree:
  791. kfree(fdt);
  792. kfree(tree);
  793. err_free_overlay_changeset:
  794. free_overlay_changeset(ovcs);
  795. out_unlock:
  796. mutex_unlock(&of_mutex);
  797. of_overlay_mutex_unlock();
  798. out:
  799. pr_debug("%s() err=%d\n", __func__, ret);
  800. return ret;
  801. }
  802. int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
  803. int *ovcs_id)
  804. {
  805. const void *new_fdt;
  806. int ret;
  807. u32 size;
  808. struct device_node *overlay_root;
  809. *ovcs_id = 0;
  810. ret = 0;
  811. if (overlay_fdt_size < sizeof(struct fdt_header) ||
  812. fdt_check_header(overlay_fdt)) {
  813. pr_err("Invalid overlay_fdt header\n");
  814. return -EINVAL;
  815. }
  816. size = fdt_totalsize(overlay_fdt);
  817. if (overlay_fdt_size < size)
  818. return -EINVAL;
  819. /*
  820. * Must create permanent copy of FDT because of_fdt_unflatten_tree()
  821. * will create pointers to the passed in FDT in the unflattened tree.
  822. */
  823. new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
  824. if (!new_fdt)
  825. return -ENOMEM;
  826. of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
  827. if (!overlay_root) {
  828. pr_err("unable to unflatten overlay_fdt\n");
  829. ret = -EINVAL;
  830. goto out_free_new_fdt;
  831. }
  832. ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
  833. if (ret < 0) {
  834. /*
  835. * new_fdt and overlay_root now belong to the overlay
  836. * changeset.
  837. * overlay changeset code is responsible for freeing them.
  838. */
  839. goto out;
  840. }
  841. return 0;
  842. out_free_new_fdt:
  843. kfree(new_fdt);
  844. out:
  845. return ret;
  846. }
  847. EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
  848. /*
  849. * Find @np in @tree.
  850. *
  851. * Returns 1 if @np is @tree or is contained in @tree, else 0
  852. */
  853. static int find_node(struct device_node *tree, struct device_node *np)
  854. {
  855. struct device_node *child;
  856. if (tree == np)
  857. return 1;
  858. for_each_child_of_node(tree, child) {
  859. if (find_node(child, np)) {
  860. of_node_put(child);
  861. return 1;
  862. }
  863. }
  864. return 0;
  865. }
  866. /*
  867. * Is @remove_ce_node a child of, a parent of, or the same as any
  868. * node in an overlay changeset more topmost than @remove_ovcs?
  869. *
  870. * Returns 1 if found, else 0
  871. */
  872. static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
  873. struct device_node *remove_ce_node)
  874. {
  875. struct overlay_changeset *ovcs;
  876. struct of_changeset_entry *ce;
  877. list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
  878. if (ovcs == remove_ovcs)
  879. break;
  880. list_for_each_entry(ce, &ovcs->cset.entries, node) {
  881. if (find_node(ce->np, remove_ce_node)) {
  882. pr_err("%s: #%d overlaps with #%d @%pOF\n",
  883. __func__, remove_ovcs->id, ovcs->id,
  884. remove_ce_node);
  885. return 1;
  886. }
  887. if (find_node(remove_ce_node, ce->np)) {
  888. pr_err("%s: #%d overlaps with #%d @%pOF\n",
  889. __func__, remove_ovcs->id, ovcs->id,
  890. remove_ce_node);
  891. return 1;
  892. }
  893. }
  894. }
  895. return 0;
  896. }
  897. /*
  898. * We can safely remove the overlay only if it's the top-most one.
  899. * Newly applied overlays are inserted at the tail of the overlay list,
  900. * so a top most overlay is the one that is closest to the tail.
  901. *
  902. * The topmost check is done by exploiting this property. For each
  903. * affected device node in the log list we check if this overlay is
  904. * the one closest to the tail. If another overlay has affected this
  905. * device node and is closest to the tail, then removal is not permited.
  906. */
  907. static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
  908. {
  909. struct of_changeset_entry *remove_ce;
  910. list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
  911. if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
  912. pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
  913. return 0;
  914. }
  915. }
  916. return 1;
  917. }
  918. /**
  919. * of_overlay_remove() - Revert and free an overlay changeset
  920. * @ovcs_id: Pointer to overlay changeset id
  921. *
  922. * Removes an overlay if it is permissible. @ovcs_id was previously returned
  923. * by of_overlay_fdt_apply().
  924. *
  925. * If an error occurred while attempting to revert the overlay changeset,
  926. * then an attempt is made to re-apply any changeset entry that was
  927. * reverted. If an error occurs on re-apply then the state of the device
  928. * tree can not be determined, and any following attempt to apply or remove
  929. * an overlay changeset will be refused.
  930. *
  931. * A non-zero return value will not revert the changeset if error is from:
  932. * - parameter checks
  933. * - overlay changeset pre-remove notifier
  934. * - overlay changeset entry revert
  935. *
  936. * If an error is returned by an overlay changeset pre-remove notifier
  937. * then no further overlay changeset pre-remove notifier will be called.
  938. *
  939. * If more than one notifier returns an error, then the last notifier
  940. * error to occur is returned.
  941. *
  942. * A non-zero return value will revert the changeset if error is from:
  943. * - overlay changeset entry notifier
  944. * - overlay changeset post-remove notifier
  945. *
  946. * If an error is returned by an overlay changeset post-remove notifier
  947. * then no further overlay changeset post-remove notifier will be called.
  948. *
  949. * Returns 0 on success, or a negative error number. *ovcs_id is set to
  950. * zero after reverting the changeset, even if a subsequent error occurs.
  951. */
  952. int of_overlay_remove(int *ovcs_id)
  953. {
  954. struct overlay_changeset *ovcs;
  955. int ret, ret_apply, ret_tmp;
  956. ret = 0;
  957. if (devicetree_corrupt()) {
  958. pr_err("suspect devicetree state, refuse to remove overlay\n");
  959. ret = -EBUSY;
  960. goto out;
  961. }
  962. mutex_lock(&of_mutex);
  963. ovcs = idr_find(&ovcs_idr, *ovcs_id);
  964. if (!ovcs) {
  965. ret = -ENODEV;
  966. pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
  967. goto out_unlock;
  968. }
  969. if (!overlay_removal_is_ok(ovcs)) {
  970. ret = -EBUSY;
  971. goto out_unlock;
  972. }
  973. ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
  974. if (ret) {
  975. pr_err("overlay changeset pre-remove notify error %d\n", ret);
  976. goto out_unlock;
  977. }
  978. list_del(&ovcs->ovcs_list);
  979. /*
  980. * Disable phandle cache. Avoids race condition that would arise
  981. * from removing cache entry when the associated node is deleted.
  982. */
  983. of_free_phandle_cache();
  984. ret_apply = 0;
  985. ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
  986. of_populate_phandle_cache();
  987. if (ret) {
  988. if (ret_apply)
  989. devicetree_state_flags |= DTSF_REVERT_FAIL;
  990. goto out_unlock;
  991. }
  992. ret = __of_changeset_revert_notify(&ovcs->cset);
  993. if (ret)
  994. pr_err("overlay changeset entry notify error %d\n", ret);
  995. /* notify failure is not fatal, continue */
  996. *ovcs_id = 0;
  997. ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
  998. if (ret_tmp) {
  999. pr_err("overlay changeset post-remove notify error %d\n",
  1000. ret_tmp);
  1001. if (!ret)
  1002. ret = ret_tmp;
  1003. }
  1004. free_overlay_changeset(ovcs);
  1005. out_unlock:
  1006. mutex_unlock(&of_mutex);
  1007. out:
  1008. pr_debug("%s() err=%d\n", __func__, ret);
  1009. return ret;
  1010. }
  1011. EXPORT_SYMBOL_GPL(of_overlay_remove);
  1012. /**
  1013. * of_overlay_remove_all() - Reverts and frees all overlay changesets
  1014. *
  1015. * Removes all overlays from the system in the correct order.
  1016. *
  1017. * Returns 0 on success, or a negative error number
  1018. */
  1019. int of_overlay_remove_all(void)
  1020. {
  1021. struct overlay_changeset *ovcs, *ovcs_n;
  1022. int ret;
  1023. /* the tail of list is guaranteed to be safe to remove */
  1024. list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
  1025. ret = of_overlay_remove(&ovcs->id);
  1026. if (ret)
  1027. return ret;
  1028. }
  1029. return 0;
  1030. }
  1031. EXPORT_SYMBOL_GPL(of_overlay_remove_all);