overlay.c 34 KB

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