fdt_overlay.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. #include "libfdt_env.h"
  2. #include <fdt.h>
  3. #include <libfdt.h>
  4. #include "libfdt_internal.h"
  5. /**
  6. * overlay_get_target_phandle - retrieves the target phandle of a fragment
  7. * @fdto: pointer to the device tree overlay blob
  8. * @fragment: node offset of the fragment in the overlay
  9. *
  10. * overlay_get_target_phandle() retrieves the target phandle of an
  11. * overlay fragment when that fragment uses a phandle (target
  12. * property) instead of a path (target-path property).
  13. *
  14. * returns:
  15. * the phandle pointed by the target property
  16. * 0, if the phandle was not found
  17. * -1, if the phandle was malformed
  18. */
  19. static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
  20. {
  21. const fdt32_t *val;
  22. int len;
  23. val = fdt_getprop(fdto, fragment, "target", &len);
  24. if (!val)
  25. return 0;
  26. if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
  27. return (uint32_t)-1;
  28. return fdt32_to_cpu(*val);
  29. }
  30. /**
  31. * overlay_get_target - retrieves the offset of a fragment's target
  32. * @fdt: Base device tree blob
  33. * @fdto: Device tree overlay blob
  34. * @fragment: node offset of the fragment in the overlay
  35. * @pathp: pointer which receives the path of the target (or NULL)
  36. *
  37. * overlay_get_target() retrieves the target offset in the base
  38. * device tree of a fragment, no matter how the actual targetting is
  39. * done (through a phandle or a path)
  40. *
  41. * returns:
  42. * the targetted node offset in the base device tree
  43. * Negative error code on error
  44. */
  45. static int overlay_get_target(const void *fdt, const void *fdto,
  46. int fragment, char const **pathp)
  47. {
  48. uint32_t phandle;
  49. const char *path = NULL;
  50. int path_len = 0, ret;
  51. /* Try first to do a phandle based lookup */
  52. phandle = overlay_get_target_phandle(fdto, fragment);
  53. if (phandle == (uint32_t)-1)
  54. return -FDT_ERR_BADPHANDLE;
  55. /* no phandle, try path */
  56. if (!phandle) {
  57. /* And then a path based lookup */
  58. path = fdt_getprop(fdto, fragment, "target-path", &path_len);
  59. if (path)
  60. ret = fdt_path_offset(fdt, path);
  61. else
  62. ret = path_len;
  63. } else
  64. ret = fdt_node_offset_by_phandle(fdt, phandle);
  65. /*
  66. * If we haven't found either a target or a
  67. * target-path property in a node that contains a
  68. * __overlay__ subnode (we wouldn't be called
  69. * otherwise), consider it a improperly written
  70. * overlay
  71. */
  72. if (ret < 0 && path_len == -FDT_ERR_NOTFOUND)
  73. ret = -FDT_ERR_BADOVERLAY;
  74. /* return on error */
  75. if (ret < 0)
  76. return ret;
  77. /* return pointer to path (if available) */
  78. if (pathp)
  79. *pathp = path ? path : NULL;
  80. return ret;
  81. }
  82. /**
  83. * overlay_phandle_add_offset - Increases a phandle by an offset
  84. * @fdt: Base device tree blob
  85. * @node: Device tree overlay blob
  86. * @name: Name of the property to modify (phandle or linux,phandle)
  87. * @delta: offset to apply
  88. *
  89. * overlay_phandle_add_offset() increments a node phandle by a given
  90. * offset.
  91. *
  92. * returns:
  93. * 0 on success.
  94. * Negative error code on error
  95. */
  96. static int overlay_phandle_add_offset(void *fdt, int node,
  97. const char *name, uint32_t delta)
  98. {
  99. const fdt32_t *val;
  100. uint32_t adj_val;
  101. int len;
  102. val = fdt_getprop(fdt, node, name, &len);
  103. if (!val)
  104. return len;
  105. if (len != sizeof(*val))
  106. return -FDT_ERR_BADPHANDLE;
  107. adj_val = fdt32_to_cpu(*val);
  108. if ((adj_val + delta) < adj_val)
  109. return -FDT_ERR_NOPHANDLES;
  110. adj_val += delta;
  111. if (adj_val == (uint32_t)-1)
  112. return -FDT_ERR_NOPHANDLES;
  113. return fdt_setprop_inplace_u32(fdt, node, name, adj_val);
  114. }
  115. /**
  116. * overlay_adjust_node_phandles - Offsets the phandles of a node
  117. * @fdto: Device tree overlay blob
  118. * @node: Offset of the node we want to adjust
  119. * @delta: Offset to shift the phandles of
  120. *
  121. * overlay_adjust_node_phandles() adds a constant to all the phandles
  122. * of a given node. This is mainly use as part of the overlay
  123. * application process, when we want to update all the overlay
  124. * phandles to not conflict with the overlays of the base device tree.
  125. *
  126. * returns:
  127. * 0 on success
  128. * Negative error code on failure
  129. */
  130. static int overlay_adjust_node_phandles(void *fdto, int node,
  131. uint32_t delta)
  132. {
  133. int child;
  134. int ret;
  135. ret = overlay_phandle_add_offset(fdto, node, "phandle", delta);
  136. if (ret && ret != -FDT_ERR_NOTFOUND)
  137. return ret;
  138. ret = overlay_phandle_add_offset(fdto, node, "linux,phandle", delta);
  139. if (ret && ret != -FDT_ERR_NOTFOUND)
  140. return ret;
  141. fdt_for_each_subnode(child, fdto, node) {
  142. ret = overlay_adjust_node_phandles(fdto, child, delta);
  143. if (ret)
  144. return ret;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * overlay_adjust_local_phandles - Adjust the phandles of a whole overlay
  150. * @fdto: Device tree overlay blob
  151. * @delta: Offset to shift the phandles of
  152. *
  153. * overlay_adjust_local_phandles() adds a constant to all the
  154. * phandles of an overlay. This is mainly use as part of the overlay
  155. * application process, when we want to update all the overlay
  156. * phandles to not conflict with the overlays of the base device tree.
  157. *
  158. * returns:
  159. * 0 on success
  160. * Negative error code on failure
  161. */
  162. static int overlay_adjust_local_phandles(void *fdto, uint32_t delta)
  163. {
  164. /*
  165. * Start adjusting the phandles from the overlay root
  166. */
  167. return overlay_adjust_node_phandles(fdto, 0, delta);
  168. }
  169. /**
  170. * overlay_update_local_node_references - Adjust the overlay references
  171. * @fdto: Device tree overlay blob
  172. * @tree_node: Node offset of the node to operate on
  173. * @fixup_node: Node offset of the matching local fixups node
  174. * @delta: Offset to shift the phandles of
  175. *
  176. * overlay_update_local_nodes_references() update the phandles
  177. * pointing to a node within the device tree overlay by adding a
  178. * constant delta.
  179. *
  180. * This is mainly used as part of a device tree application process,
  181. * where you want the device tree overlays phandles to not conflict
  182. * with the ones from the base device tree before merging them.
  183. *
  184. * returns:
  185. * 0 on success
  186. * Negative error code on failure
  187. */
  188. static int overlay_update_local_node_references(void *fdto,
  189. int tree_node,
  190. int fixup_node,
  191. uint32_t delta)
  192. {
  193. int fixup_prop;
  194. int fixup_child;
  195. int ret;
  196. fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
  197. const fdt32_t *fixup_val;
  198. const char *tree_val;
  199. const char *name;
  200. int fixup_len;
  201. int tree_len;
  202. int i;
  203. fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
  204. &name, &fixup_len);
  205. if (!fixup_val)
  206. return fixup_len;
  207. if (fixup_len % sizeof(uint32_t))
  208. return -FDT_ERR_BADOVERLAY;
  209. tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
  210. if (!tree_val) {
  211. if (tree_len == -FDT_ERR_NOTFOUND)
  212. return -FDT_ERR_BADOVERLAY;
  213. return tree_len;
  214. }
  215. for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
  216. fdt32_t adj_val;
  217. uint32_t poffset;
  218. poffset = fdt32_to_cpu(fixup_val[i]);
  219. /*
  220. * phandles to fixup can be unaligned.
  221. *
  222. * Use a memcpy for the architectures that do
  223. * not support unaligned accesses.
  224. */
  225. memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
  226. adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
  227. ret = fdt_setprop_inplace_namelen_partial(fdto,
  228. tree_node,
  229. name,
  230. strlen(name),
  231. poffset,
  232. &adj_val,
  233. sizeof(adj_val));
  234. if (ret == -FDT_ERR_NOSPACE)
  235. return -FDT_ERR_BADOVERLAY;
  236. if (ret)
  237. return ret;
  238. }
  239. }
  240. fdt_for_each_subnode(fixup_child, fdto, fixup_node) {
  241. const char *fixup_child_name = fdt_get_name(fdto, fixup_child,
  242. NULL);
  243. int tree_child;
  244. tree_child = fdt_subnode_offset(fdto, tree_node,
  245. fixup_child_name);
  246. if (tree_child == -FDT_ERR_NOTFOUND)
  247. return -FDT_ERR_BADOVERLAY;
  248. if (tree_child < 0)
  249. return tree_child;
  250. ret = overlay_update_local_node_references(fdto,
  251. tree_child,
  252. fixup_child,
  253. delta);
  254. if (ret)
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * overlay_update_local_references - Adjust the overlay references
  261. * @fdto: Device tree overlay blob
  262. * @delta: Offset to shift the phandles of
  263. *
  264. * overlay_update_local_references() update all the phandles pointing
  265. * to a node within the device tree overlay by adding a constant
  266. * delta to not conflict with the base overlay.
  267. *
  268. * This is mainly used as part of a device tree application process,
  269. * where you want the device tree overlays phandles to not conflict
  270. * with the ones from the base device tree before merging them.
  271. *
  272. * returns:
  273. * 0 on success
  274. * Negative error code on failure
  275. */
  276. static int overlay_update_local_references(void *fdto, uint32_t delta)
  277. {
  278. int fixups;
  279. fixups = fdt_path_offset(fdto, "/__local_fixups__");
  280. if (fixups < 0) {
  281. /* There's no local phandles to adjust, bail out */
  282. if (fixups == -FDT_ERR_NOTFOUND)
  283. return 0;
  284. return fixups;
  285. }
  286. /*
  287. * Update our local references from the root of the tree
  288. */
  289. return overlay_update_local_node_references(fdto, 0, fixups,
  290. delta);
  291. }
  292. /**
  293. * overlay_fixup_one_phandle - Set an overlay phandle to the base one
  294. * @fdt: Base Device Tree blob
  295. * @fdto: Device tree overlay blob
  296. * @symbols_off: Node offset of the symbols node in the base device tree
  297. * @path: Path to a node holding a phandle in the overlay
  298. * @path_len: number of path characters to consider
  299. * @name: Name of the property holding the phandle reference in the overlay
  300. * @name_len: number of name characters to consider
  301. * @poffset: Offset within the overlay property where the phandle is stored
  302. * @label: Label of the node referenced by the phandle
  303. *
  304. * overlay_fixup_one_phandle() resolves an overlay phandle pointing to
  305. * a node in the base device tree.
  306. *
  307. * This is part of the device tree overlay application process, when
  308. * you want all the phandles in the overlay to point to the actual
  309. * base dt nodes.
  310. *
  311. * returns:
  312. * 0 on success
  313. * Negative error code on failure
  314. */
  315. static int overlay_fixup_one_phandle(void *fdt, void *fdto,
  316. int symbols_off,
  317. const char *path, uint32_t path_len,
  318. const char *name, uint32_t name_len,
  319. int poffset, const char *label)
  320. {
  321. const char *symbol_path;
  322. uint32_t phandle;
  323. fdt32_t phandle_prop;
  324. int symbol_off, fixup_off;
  325. int prop_len;
  326. if (symbols_off < 0)
  327. return symbols_off;
  328. symbol_path = fdt_getprop(fdt, symbols_off, label,
  329. &prop_len);
  330. if (!symbol_path)
  331. return prop_len;
  332. symbol_off = fdt_path_offset(fdt, symbol_path);
  333. if (symbol_off < 0)
  334. return symbol_off;
  335. phandle = fdt_get_phandle(fdt, symbol_off);
  336. if (!phandle)
  337. return -FDT_ERR_NOTFOUND;
  338. fixup_off = fdt_path_offset_namelen(fdto, path, path_len);
  339. if (fixup_off == -FDT_ERR_NOTFOUND)
  340. return -FDT_ERR_BADOVERLAY;
  341. if (fixup_off < 0)
  342. return fixup_off;
  343. phandle_prop = cpu_to_fdt32(phandle);
  344. return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
  345. name, name_len, poffset,
  346. &phandle_prop,
  347. sizeof(phandle_prop));
  348. };
  349. /**
  350. * overlay_fixup_phandle - Set an overlay phandle to the base one
  351. * @fdt: Base Device Tree blob
  352. * @fdto: Device tree overlay blob
  353. * @symbols_off: Node offset of the symbols node in the base device tree
  354. * @property: Property offset in the overlay holding the list of fixups
  355. *
  356. * overlay_fixup_phandle() resolves all the overlay phandles pointed
  357. * to in a __fixups__ property, and updates them to match the phandles
  358. * in use in the base device tree.
  359. *
  360. * This is part of the device tree overlay application process, when
  361. * you want all the phandles in the overlay to point to the actual
  362. * base dt nodes.
  363. *
  364. * returns:
  365. * 0 on success
  366. * Negative error code on failure
  367. */
  368. static int overlay_fixup_phandle(void *fdt, void *fdto, int symbols_off,
  369. int property)
  370. {
  371. const char *value;
  372. const char *label;
  373. int len;
  374. value = fdt_getprop_by_offset(fdto, property,
  375. &label, &len);
  376. if (!value) {
  377. if (len == -FDT_ERR_NOTFOUND)
  378. return -FDT_ERR_INTERNAL;
  379. return len;
  380. }
  381. do {
  382. const char *path, *name, *fixup_end;
  383. const char *fixup_str = value;
  384. uint32_t path_len, name_len;
  385. uint32_t fixup_len;
  386. char *sep, *endptr;
  387. int poffset, ret;
  388. fixup_end = memchr(value, '\0', len);
  389. if (!fixup_end)
  390. return -FDT_ERR_BADOVERLAY;
  391. fixup_len = fixup_end - fixup_str;
  392. len -= fixup_len + 1;
  393. value += fixup_len + 1;
  394. path = fixup_str;
  395. sep = memchr(fixup_str, ':', fixup_len);
  396. if (!sep || *sep != ':')
  397. return -FDT_ERR_BADOVERLAY;
  398. path_len = sep - path;
  399. if (path_len == (fixup_len - 1))
  400. return -FDT_ERR_BADOVERLAY;
  401. fixup_len -= path_len + 1;
  402. name = sep + 1;
  403. sep = memchr(name, ':', fixup_len);
  404. if (!sep || *sep != ':')
  405. return -FDT_ERR_BADOVERLAY;
  406. name_len = sep - name;
  407. if (!name_len)
  408. return -FDT_ERR_BADOVERLAY;
  409. poffset = strtoul(sep + 1, &endptr, 10);
  410. if ((*endptr != '\0') || (endptr <= (sep + 1)))
  411. return -FDT_ERR_BADOVERLAY;
  412. ret = overlay_fixup_one_phandle(fdt, fdto, symbols_off,
  413. path, path_len, name, name_len,
  414. poffset, label);
  415. if (ret)
  416. return ret;
  417. } while (len > 0);
  418. return 0;
  419. }
  420. /**
  421. * overlay_fixup_phandles - Resolve the overlay phandles to the base
  422. * device tree
  423. * @fdt: Base Device Tree blob
  424. * @fdto: Device tree overlay blob
  425. *
  426. * overlay_fixup_phandles() resolves all the overlay phandles pointing
  427. * to nodes in the base device tree.
  428. *
  429. * This is one of the steps of the device tree overlay application
  430. * process, when you want all the phandles in the overlay to point to
  431. * the actual base dt nodes.
  432. *
  433. * returns:
  434. * 0 on success
  435. * Negative error code on failure
  436. */
  437. static int overlay_fixup_phandles(void *fdt, void *fdto)
  438. {
  439. int fixups_off, symbols_off;
  440. int property;
  441. /* We can have overlays without any fixups */
  442. fixups_off = fdt_path_offset(fdto, "/__fixups__");
  443. if (fixups_off == -FDT_ERR_NOTFOUND)
  444. return 0; /* nothing to do */
  445. if (fixups_off < 0)
  446. return fixups_off;
  447. /* And base DTs without symbols */
  448. symbols_off = fdt_path_offset(fdt, "/__symbols__");
  449. if ((symbols_off < 0 && (symbols_off != -FDT_ERR_NOTFOUND)))
  450. return symbols_off;
  451. fdt_for_each_property_offset(property, fdto, fixups_off) {
  452. int ret;
  453. ret = overlay_fixup_phandle(fdt, fdto, symbols_off, property);
  454. if (ret)
  455. return ret;
  456. }
  457. return 0;
  458. }
  459. /**
  460. * overlay_apply_node - Merges a node into the base device tree
  461. * @fdt: Base Device Tree blob
  462. * @target: Node offset in the base device tree to apply the fragment to
  463. * @fdto: Device tree overlay blob
  464. * @node: Node offset in the overlay holding the changes to merge
  465. *
  466. * overlay_apply_node() merges a node into a target base device tree
  467. * node pointed.
  468. *
  469. * This is part of the final step in the device tree overlay
  470. * application process, when all the phandles have been adjusted and
  471. * resolved and you just have to merge overlay into the base device
  472. * tree.
  473. *
  474. * returns:
  475. * 0 on success
  476. * Negative error code on failure
  477. */
  478. static int overlay_apply_node(void *fdt, int target,
  479. void *fdto, int node)
  480. {
  481. int property;
  482. int subnode;
  483. fdt_for_each_property_offset(property, fdto, node) {
  484. const char *name;
  485. const void *prop;
  486. int prop_len;
  487. int ret;
  488. prop = fdt_getprop_by_offset(fdto, property, &name,
  489. &prop_len);
  490. if (prop_len == -FDT_ERR_NOTFOUND)
  491. return -FDT_ERR_INTERNAL;
  492. if (prop_len < 0)
  493. return prop_len;
  494. ret = fdt_setprop(fdt, target, name, prop, prop_len);
  495. if (ret)
  496. return ret;
  497. }
  498. fdt_for_each_subnode(subnode, fdto, node) {
  499. const char *name = fdt_get_name(fdto, subnode, NULL);
  500. int nnode;
  501. int ret;
  502. nnode = fdt_add_subnode(fdt, target, name);
  503. if (nnode == -FDT_ERR_EXISTS) {
  504. nnode = fdt_subnode_offset(fdt, target, name);
  505. if (nnode == -FDT_ERR_NOTFOUND)
  506. return -FDT_ERR_INTERNAL;
  507. }
  508. if (nnode < 0)
  509. return nnode;
  510. ret = overlay_apply_node(fdt, nnode, fdto, subnode);
  511. if (ret)
  512. return ret;
  513. }
  514. return 0;
  515. }
  516. /**
  517. * overlay_merge - Merge an overlay into its base device tree
  518. * @fdt: Base Device Tree blob
  519. * @fdto: Device tree overlay blob
  520. *
  521. * overlay_merge() merges an overlay into its base device tree.
  522. *
  523. * This is the next to last step in the device tree overlay application
  524. * process, when all the phandles have been adjusted and resolved and
  525. * you just have to merge overlay into the base device tree.
  526. *
  527. * returns:
  528. * 0 on success
  529. * Negative error code on failure
  530. */
  531. static int overlay_merge(void *fdt, void *fdto)
  532. {
  533. int fragment;
  534. fdt_for_each_subnode(fragment, fdto, 0) {
  535. int overlay;
  536. int target;
  537. int ret;
  538. /*
  539. * Each fragments will have an __overlay__ node. If
  540. * they don't, it's not supposed to be merged
  541. */
  542. overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
  543. if (overlay == -FDT_ERR_NOTFOUND)
  544. continue;
  545. if (overlay < 0)
  546. return overlay;
  547. target = overlay_get_target(fdt, fdto, fragment, NULL);
  548. if (target < 0)
  549. return target;
  550. ret = overlay_apply_node(fdt, target, fdto, overlay);
  551. if (ret)
  552. return ret;
  553. }
  554. return 0;
  555. }
  556. static int get_path_len(const void *fdt, int nodeoffset)
  557. {
  558. int len = 0, namelen;
  559. const char *name;
  560. FDT_CHECK_HEADER(fdt);
  561. for (;;) {
  562. name = fdt_get_name(fdt, nodeoffset, &namelen);
  563. if (!name)
  564. return namelen;
  565. /* root? we're done */
  566. if (namelen == 0)
  567. break;
  568. nodeoffset = fdt_parent_offset(fdt, nodeoffset);
  569. if (nodeoffset < 0)
  570. return nodeoffset;
  571. len += namelen + 1;
  572. }
  573. /* in case of root pretend it's "/" */
  574. if (len == 0)
  575. len++;
  576. return len;
  577. }
  578. /**
  579. * overlay_symbol_update - Update the symbols of base tree after a merge
  580. * @fdt: Base Device Tree blob
  581. * @fdto: Device tree overlay blob
  582. *
  583. * overlay_symbol_update() updates the symbols of the base tree with the
  584. * symbols of the applied overlay
  585. *
  586. * This is the last step in the device tree overlay application
  587. * process, allowing the reference of overlay symbols by subsequent
  588. * overlay operations.
  589. *
  590. * returns:
  591. * 0 on success
  592. * Negative error code on failure
  593. */
  594. static int overlay_symbol_update(void *fdt, void *fdto)
  595. {
  596. int root_sym, ov_sym, prop, path_len, fragment, target;
  597. int len, frag_name_len, ret, rel_path_len;
  598. const char *s, *e;
  599. const char *path;
  600. const char *name;
  601. const char *frag_name;
  602. const char *rel_path;
  603. const char *target_path;
  604. char *buf;
  605. void *p;
  606. ov_sym = fdt_subnode_offset(fdto, 0, "__symbols__");
  607. /* if no overlay symbols exist no problem */
  608. if (ov_sym < 0)
  609. return 0;
  610. root_sym = fdt_subnode_offset(fdt, 0, "__symbols__");
  611. /* it no root symbols exist we should create them */
  612. if (root_sym == -FDT_ERR_NOTFOUND)
  613. root_sym = fdt_add_subnode(fdt, 0, "__symbols__");
  614. /* any error is fatal now */
  615. if (root_sym < 0)
  616. return root_sym;
  617. /* iterate over each overlay symbol */
  618. fdt_for_each_property_offset(prop, fdto, ov_sym) {
  619. path = fdt_getprop_by_offset(fdto, prop, &name, &path_len);
  620. if (!path)
  621. return path_len;
  622. /* verify it's a string property (terminated by a single \0) */
  623. if (path_len < 1 || memchr(path, '\0', path_len) != &path[path_len - 1])
  624. return -FDT_ERR_BADVALUE;
  625. /* keep end marker to avoid strlen() */
  626. e = path + path_len;
  627. /* format: /<fragment-name>/__overlay__/<relative-subnode-path> */
  628. if (*path != '/')
  629. return -FDT_ERR_BADVALUE;
  630. /* get fragment name first */
  631. s = strchr(path + 1, '/');
  632. if (!s)
  633. return -FDT_ERR_BADOVERLAY;
  634. frag_name = path + 1;
  635. frag_name_len = s - path - 1;
  636. /* verify format; safe since "s" lies in \0 terminated prop */
  637. len = sizeof("/__overlay__/") - 1;
  638. if ((e - s) < len || memcmp(s, "/__overlay__/", len))
  639. return -FDT_ERR_BADOVERLAY;
  640. rel_path = s + len;
  641. rel_path_len = e - rel_path;
  642. /* find the fragment index in which the symbol lies */
  643. ret = fdt_subnode_offset_namelen(fdto, 0, frag_name,
  644. frag_name_len);
  645. /* not found? */
  646. if (ret < 0)
  647. return -FDT_ERR_BADOVERLAY;
  648. fragment = ret;
  649. /* an __overlay__ subnode must exist */
  650. ret = fdt_subnode_offset(fdto, fragment, "__overlay__");
  651. if (ret < 0)
  652. return -FDT_ERR_BADOVERLAY;
  653. /* get the target of the fragment */
  654. ret = overlay_get_target(fdt, fdto, fragment, &target_path);
  655. if (ret < 0)
  656. return ret;
  657. target = ret;
  658. /* if we have a target path use */
  659. if (!target_path) {
  660. ret = get_path_len(fdt, target);
  661. if (ret < 0)
  662. return ret;
  663. len = ret;
  664. } else {
  665. len = strlen(target_path);
  666. }
  667. ret = fdt_setprop_placeholder(fdt, root_sym, name,
  668. len + (len > 1) + rel_path_len + 1, &p);
  669. if (ret < 0)
  670. return ret;
  671. if (!target_path) {
  672. /* again in case setprop_placeholder changed it */
  673. ret = overlay_get_target(fdt, fdto, fragment, &target_path);
  674. if (ret < 0)
  675. return ret;
  676. target = ret;
  677. }
  678. buf = p;
  679. if (len > 1) { /* target is not root */
  680. if (!target_path) {
  681. ret = fdt_get_path(fdt, target, buf, len + 1);
  682. if (ret < 0)
  683. return ret;
  684. } else
  685. memcpy(buf, target_path, len + 1);
  686. } else
  687. len--;
  688. buf[len] = '/';
  689. memcpy(buf + len + 1, rel_path, rel_path_len);
  690. buf[len + 1 + rel_path_len] = '\0';
  691. }
  692. return 0;
  693. }
  694. int fdt_overlay_apply(void *fdt, void *fdto)
  695. {
  696. uint32_t delta = fdt_get_max_phandle(fdt);
  697. int ret;
  698. FDT_CHECK_HEADER(fdt);
  699. FDT_CHECK_HEADER(fdto);
  700. ret = overlay_adjust_local_phandles(fdto, delta);
  701. if (ret)
  702. goto err;
  703. ret = overlay_update_local_references(fdto, delta);
  704. if (ret)
  705. goto err;
  706. ret = overlay_fixup_phandles(fdt, fdto);
  707. if (ret)
  708. goto err;
  709. ret = overlay_merge(fdt, fdto);
  710. if (ret)
  711. goto err;
  712. ret = overlay_symbol_update(fdt, fdto);
  713. if (ret)
  714. goto err;
  715. /*
  716. * The overlay has been damaged, erase its magic.
  717. */
  718. fdt_set_magic(fdto, ~0);
  719. return 0;
  720. err:
  721. /*
  722. * The overlay might have been damaged, erase its magic.
  723. */
  724. fdt_set_magic(fdto, ~0);
  725. /*
  726. * The base device tree might have been damaged, erase its
  727. * magic.
  728. */
  729. fdt_set_magic(fdt, ~0);
  730. return ret;
  731. }