overlay.c 35 KB

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