ufdt_overlay.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*-
  2. * Copyright (c) 2015 Oleksandr Tymoshenko <[email protected]>
  3. * All rights reserved.
  4. *
  5. * This software was developed by Semihalf under sponsorship from
  6. * the FreeBSD Foundation.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "ufdt_overlay.h"
  30. #include "libufdt.h"
  31. #include "ufdt_node_pool.h"
  32. #include "ufdt_overlay_internal.h"
  33. /*
  34. * The original version of fdt_overlay.c is slow in searching for particular
  35. * nodes and adding subnodes/properties due to the operations on flattened
  36. * device tree (FDT).
  37. *
  38. * Here we introduce `libufdt` which builds a real tree structure (named
  39. * ufdt -- unflattned device tree) from FDT. In the real tree, we can perform
  40. * certain operations (e.g., merge 2 subtrees, search for a node by path) in
  41. * almost optimal time complexity with acceptable additional memory usage.
  42. *
  43. * This file is the improved version of fdt_overlay.c by using the real tree
  44. * structure defined in libufdt.
  45. *
  46. * How the device tree overlay works and some
  47. * special terms (e.g., fixups, local fixups, fragment, etc)
  48. * are described in the document
  49. * external/dtc/Documentation/dt-object-internal.txt.
  50. */
  51. /* BEGIN of operations about phandles in ufdt. */
  52. /*
  53. * Increases u32 value at pos by offset.
  54. */
  55. static void fdt_increase_u32(void *pos, uint32_t offset) {
  56. uint32_t val;
  57. dto_memcpy(&val, pos, sizeof(val));
  58. val = cpu_to_fdt32(fdt32_to_cpu(val) + offset);
  59. dto_memcpy(pos, &val, sizeof(val));
  60. }
  61. /*
  62. * Gets the max phandle of a given ufdt.
  63. */
  64. uint32_t ufdt_get_max_phandle(struct ufdt *tree) {
  65. struct ufdt_static_phandle_table sorted_table = tree->phandle_table;
  66. if (sorted_table.len > 0)
  67. return sorted_table.data[sorted_table.len - 1].phandle;
  68. else
  69. return 0;
  70. }
  71. /*
  72. * Tries to increase the phandle value of a node
  73. * if the phandle exists.
  74. */
  75. static void ufdt_node_try_increase_phandle(struct ufdt_node *node,
  76. uint32_t offset) {
  77. int len = 0;
  78. char *prop_data = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
  79. if (prop_data != NULL && len == sizeof(fdt32_t)) {
  80. fdt_increase_u32(prop_data, offset);
  81. }
  82. prop_data = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
  83. if (prop_data != NULL && len == sizeof(fdt32_t)) {
  84. fdt_increase_u32(prop_data, offset);
  85. }
  86. }
  87. /*
  88. * Increases all phandles by offset in a ufdt
  89. * in O(n) time.
  90. */
  91. void ufdt_try_increase_phandle(struct ufdt *tree, uint32_t offset) {
  92. struct ufdt_static_phandle_table sorted_table = tree->phandle_table;
  93. int i;
  94. for (i = 0; i < sorted_table.len; i++) {
  95. struct ufdt_node *target_node = sorted_table.data[i].node;
  96. ufdt_node_try_increase_phandle(target_node, offset);
  97. }
  98. }
  99. /* END of operations about phandles in ufdt. */
  100. /*
  101. * In the overlay_tree, there are some references (phandle)
  102. * pointing to somewhere in the main_tree.
  103. * Fix-up operations is to resolve the right address
  104. * in the overlay_tree.
  105. */
  106. /* BEGIN of doing fixup in the overlay ufdt. */
  107. /*
  108. * Returns exact memory location specified by fixup in format
  109. * /path/to/node:property:offset.
  110. * A property might contain multiple values and the offset is used to locate a
  111. * reference inside the property.
  112. * e.g.,
  113. * "property"=<1, 2, &ref, 4>, we can use /path/to/node:property:8 to get ref,
  114. * where 8 is sizeof(uint32) + sizeof(unit32).
  115. */
  116. void *ufdt_get_fixup_location(struct ufdt *tree, const char *fixup) {
  117. char *path, *prop_ptr, *offset_ptr, *end_ptr;
  118. int prop_offset, prop_len;
  119. const char *prop_data;
  120. char path_buf[1024];
  121. char *path_mem = NULL;
  122. size_t fixup_len = strlen(fixup) + 1;
  123. if (fixup_len > sizeof(path_buf)) {
  124. path_mem = dto_malloc(fixup_len);
  125. path = path_mem;
  126. } else {
  127. path = path_buf;
  128. }
  129. dto_memcpy(path, fixup, fixup_len);
  130. prop_ptr = dto_strchr(path, ':');
  131. if (prop_ptr == NULL) {
  132. dto_error("Missing property part in '%s'\n", path);
  133. goto fail;
  134. }
  135. *prop_ptr = '\0';
  136. prop_ptr++;
  137. offset_ptr = dto_strchr(prop_ptr, ':');
  138. if (offset_ptr == NULL) {
  139. dto_error("Missing offset part in '%s'\n", path);
  140. goto fail;
  141. }
  142. *offset_ptr = '\0';
  143. offset_ptr++;
  144. prop_offset = dto_strtoul(offset_ptr, &end_ptr, 10 /* base */);
  145. if (*end_ptr != '\0') {
  146. dto_error("'%s' is not valid number\n", offset_ptr);
  147. goto fail;
  148. }
  149. struct ufdt_node *target_node;
  150. target_node = ufdt_get_node_by_path(tree, path);
  151. if (target_node == NULL) {
  152. dto_error("Path '%s' not found\n", path);
  153. goto fail;
  154. }
  155. prop_data =
  156. ufdt_node_get_fdt_prop_data_by_name(target_node, prop_ptr, &prop_len);
  157. if (prop_data == NULL) {
  158. dto_error("Property '%s' not found in '%s' node\n", prop_ptr, path);
  159. goto fail;
  160. }
  161. /*
  162. * Note that prop_offset is the offset inside the property data.
  163. */
  164. if (prop_len < prop_offset + (int)sizeof(uint32_t)) {
  165. dto_error("%s: property length is too small for fixup\n", path);
  166. goto fail;
  167. }
  168. if (path_mem) dto_free(path_mem);
  169. return (char *)prop_data + prop_offset;
  170. fail:
  171. if (path_mem) dto_free(path_mem);
  172. return NULL;
  173. }
  174. /*
  175. * Process one entry in __fixups__ { } node.
  176. * @fixups is property value, array of NUL-terminated strings
  177. * with fixup locations.
  178. * @fixups_len length of the fixups array in bytes.
  179. * @phandle is value for these locations.
  180. */
  181. int ufdt_do_one_fixup(struct ufdt *tree, const char *fixups, int fixups_len,
  182. int phandle) {
  183. void *fixup_pos;
  184. uint32_t val;
  185. val = cpu_to_fdt32(phandle);
  186. while (fixups_len > 0) {
  187. fixup_pos = ufdt_get_fixup_location(tree, fixups);
  188. if (fixup_pos != NULL) {
  189. dto_memcpy(fixup_pos, &val, sizeof(val));
  190. } else {
  191. return -1;
  192. }
  193. fixups_len -= dto_strlen(fixups) + 1;
  194. fixups += dto_strlen(fixups) + 1;
  195. }
  196. return 0;
  197. }
  198. /*
  199. * Handle __fixups__ node in overlay tree.
  200. */
  201. int ufdt_overlay_do_fixups(struct ufdt *main_tree, struct ufdt *overlay_tree) {
  202. int len = 0;
  203. struct ufdt_node *overlay_fixups_node =
  204. ufdt_get_node_by_path(overlay_tree, "/__fixups__");
  205. if (!overlay_fixups_node) {
  206. /* There is no __fixups__. Do nothing. */
  207. return 0;
  208. }
  209. struct ufdt_node *main_symbols_node =
  210. ufdt_get_node_by_path(main_tree, "/__symbols__");
  211. struct ufdt_node **it;
  212. for_each_prop(it, overlay_fixups_node) {
  213. /* Find the first property */
  214. /* Check __symbols__ is exist when we have any property in __fixups__ */
  215. if (!main_symbols_node) {
  216. dto_error("No node __symbols__ in main dtb.\n");
  217. return -1;
  218. }
  219. break;
  220. }
  221. for_each_prop(it, overlay_fixups_node) {
  222. /*
  223. * A property in __fixups__ looks like:
  224. * symbol_name =
  225. * "/path/to/node:prop:offset0\x00/path/to/node:prop:offset1..."
  226. * So we firstly find the node "symbol_name" and obtain its phandle in
  227. * __symbols__ of the main_tree.
  228. */
  229. struct ufdt_node *fixups = *it;
  230. char *symbol_path = ufdt_node_get_fdt_prop_data_by_name(
  231. main_symbols_node, ufdt_node_name(fixups), &len);
  232. if (!symbol_path) {
  233. dto_error("Couldn't find '%s' symbol in main dtb\n",
  234. ufdt_node_name(fixups));
  235. return -1;
  236. }
  237. struct ufdt_node *symbol_node;
  238. symbol_node = ufdt_get_node_by_path(main_tree, symbol_path);
  239. if (!symbol_node) {
  240. dto_error("Couldn't find '%s' path in main dtb\n", symbol_path);
  241. return -1;
  242. }
  243. uint32_t phandle = ufdt_node_get_phandle(symbol_node);
  244. const char *fixups_paths = ufdt_node_get_fdt_prop_data(fixups, &len);
  245. if (ufdt_do_one_fixup(overlay_tree, fixups_paths, len, phandle) < 0) {
  246. dto_error("Failed one fixup in ufdt_do_one_fixup\n");
  247. return -1;
  248. }
  249. }
  250. return 0;
  251. }
  252. /* END of doing fixup in the overlay ufdt. */
  253. /*
  254. * Here is to overlay all fragments in the overlay_tree to the main_tree.
  255. * What is "overlay fragment"? The main purpose is to add some subtrees to the
  256. * main_tree in order to complete the entire device tree.
  257. *
  258. * A fragment consists of two parts: 1. the subtree to be added 2. where it
  259. * should be added.
  260. *
  261. * Overlaying a fragment requires: 1. find the node in the main_tree 2. merge
  262. * the subtree into that node in the main_tree.
  263. */
  264. /* BEGIN of applying fragments. */
  265. /*
  266. * Overlay the overlay_node over target_node.
  267. */
  268. static int ufdt_overlay_node(struct ufdt_node *target_node,
  269. struct ufdt_node *overlay_node,
  270. struct ufdt_node_pool *pool) {
  271. return ufdt_node_merge_into(target_node, overlay_node, pool);
  272. }
  273. enum overlay_result ufdt_overlay_get_target(struct ufdt *tree,
  274. struct ufdt_node *frag_node,
  275. struct ufdt_node **target_node) {
  276. uint32_t target;
  277. const char *target_path;
  278. const void *val;
  279. *target_node = NULL;
  280. val = ufdt_node_get_fdt_prop_data_by_name(frag_node, "target", NULL);
  281. if (val) {
  282. dto_memcpy(&target, val, sizeof(target));
  283. target = fdt32_to_cpu(target);
  284. *target_node = ufdt_get_node_by_phandle(tree, target);
  285. if (*target_node == NULL) {
  286. dto_error("failed to find target %04x\n", target);
  287. return OVERLAY_RESULT_TARGET_INVALID;
  288. }
  289. }
  290. if (*target_node == NULL) {
  291. target_path =
  292. ufdt_node_get_fdt_prop_data_by_name(frag_node, "target-path", NULL);
  293. if (target_path == NULL) {
  294. return OVERLAY_RESULT_MISSING_TARGET;
  295. }
  296. *target_node = ufdt_get_node_by_path(tree, target_path);
  297. if (*target_node == NULL) {
  298. dto_error("failed to find target-path %s\n", target_path);
  299. return OVERLAY_RESULT_TARGET_PATH_INVALID;
  300. }
  301. }
  302. return OVERLAY_RESULT_OK;
  303. }
  304. /*
  305. * Apply one overlay fragment (subtree).
  306. */
  307. static enum overlay_result ufdt_apply_fragment(struct ufdt *tree,
  308. struct ufdt_node *frag_node,
  309. struct ufdt_node_pool *pool) {
  310. struct ufdt_node *target_node = NULL;
  311. struct ufdt_node *overlay_node = NULL;
  312. enum overlay_result result =
  313. ufdt_overlay_get_target(tree, frag_node, &target_node);
  314. if (target_node == NULL) {
  315. return result;
  316. }
  317. overlay_node = ufdt_node_get_node_by_path(frag_node, "__overlay__");
  318. if (overlay_node == NULL) {
  319. dto_error("missing __overlay__ sub-node\n");
  320. return OVERLAY_RESULT_MISSING_OVERLAY;
  321. }
  322. int err = ufdt_overlay_node(target_node, overlay_node, pool);
  323. if (err < 0) {
  324. dto_error("failed to overlay node %s to target %s\n",
  325. ufdt_node_name(overlay_node), ufdt_node_name(target_node));
  326. return OVERLAY_RESULT_MERGE_FAIL;
  327. }
  328. return OVERLAY_RESULT_OK;
  329. }
  330. /*
  331. * Applies all fragments to the main_tree.
  332. */
  333. static int ufdt_overlay_apply_fragments(struct ufdt *main_tree,
  334. struct ufdt *overlay_tree,
  335. struct ufdt_node_pool *pool) {
  336. enum overlay_result err;
  337. struct ufdt_node **it;
  338. /*
  339. * This loop may iterate to subnodes that's not a fragment node.
  340. * In such case, ufdt_apply_fragment would fail with return value = -1.
  341. */
  342. for_each_node(it, overlay_tree->root) {
  343. err = ufdt_apply_fragment(main_tree, *it, pool);
  344. if (err == OVERLAY_RESULT_MERGE_FAIL) {
  345. return -1;
  346. }
  347. }
  348. return 0;
  349. }
  350. /* END of applying fragments. */
  351. /*
  352. * Since the overlay_tree will be "merged" into the main_tree, some
  353. * references (e.g., phandle values that acts as an unique ID) need to be
  354. * updated so it won't lead to collision that different nodes have the same
  355. * phandle value.
  356. *
  357. * Two things need to be done:
  358. *
  359. * 1. ufdt_try_increase_phandle()
  360. * Update phandle (an unique integer ID of a node in the device tree) of each
  361. * node in the overlay_tree. To achieve this, we simply increase each phandle
  362. * values in the overlay_tree by the max phandle value of the main_tree.
  363. *
  364. * 2. ufdt_overlay_do_local_fixups()
  365. * If there are some reference in the overlay_tree that references nodes
  366. * inside the overlay_tree, we have to modify the reference value (address of
  367. * the referenced node: phandle) so that it corresponds to the right node inside
  368. * the overlay_tree. Where the reference exists is kept in __local_fixups__ node
  369. * in the overlay_tree.
  370. */
  371. /* BEGIN of updating local references (phandle values) in the overlay ufdt. */
  372. /*
  373. * local fixups
  374. */
  375. static int ufdt_local_fixup_prop(struct ufdt_node *target_prop_node,
  376. struct ufdt_node *local_fixup_prop_node,
  377. uint32_t phandle_offset) {
  378. /*
  379. * prop_offsets_ptr should be a list of fdt32_t.
  380. * <offset0 offset1 offset2 ...>
  381. */
  382. char *prop_offsets_ptr;
  383. int len = 0;
  384. prop_offsets_ptr = ufdt_node_get_fdt_prop_data(local_fixup_prop_node, &len);
  385. char *prop_data;
  386. int target_length = 0;
  387. prop_data = ufdt_node_get_fdt_prop_data(target_prop_node, &target_length);
  388. if (prop_offsets_ptr == NULL || prop_data == NULL) return -1;
  389. int i;
  390. for (i = 0; i < len; i += sizeof(fdt32_t)) {
  391. int offset = fdt32_to_cpu(*(fdt32_t *)(prop_offsets_ptr + i));
  392. if (offset + sizeof(fdt32_t) > (size_t)target_length) return -1;
  393. fdt_increase_u32((prop_data + offset), phandle_offset);
  394. }
  395. return 0;
  396. }
  397. static int ufdt_local_fixup_node(struct ufdt_node *target_node,
  398. struct ufdt_node *local_fixups_node,
  399. uint32_t phandle_offset) {
  400. if (local_fixups_node == NULL) return 0;
  401. struct ufdt_node **it_local_fixups;
  402. struct ufdt_node *sub_target_node;
  403. for_each_prop(it_local_fixups, local_fixups_node) {
  404. sub_target_node = ufdt_node_get_property_by_name(
  405. target_node, ufdt_node_name(*it_local_fixups));
  406. if (sub_target_node != NULL) {
  407. int err = ufdt_local_fixup_prop(sub_target_node, *it_local_fixups,
  408. phandle_offset);
  409. if (err < 0) return -1;
  410. } else {
  411. return -1;
  412. }
  413. }
  414. for_each_node(it_local_fixups, local_fixups_node) {
  415. sub_target_node = ufdt_node_get_node_by_path(
  416. target_node, ufdt_node_name(*it_local_fixups));
  417. if (sub_target_node != NULL) {
  418. int err = ufdt_local_fixup_node(sub_target_node, *it_local_fixups,
  419. phandle_offset);
  420. if (err < 0) return -1;
  421. } else {
  422. return -1;
  423. }
  424. }
  425. return 0;
  426. }
  427. /*
  428. * Handle __local_fixups__ node in overlay DTB
  429. * The __local_fixups__ format we expect is
  430. * __local_fixups__ {
  431. * path {
  432. * to {
  433. * local_ref1 = <offset>;
  434. * };
  435. * };
  436. * path2 {
  437. * to2 {
  438. * local_ref2 = <offset1 offset2 ...>;
  439. * };
  440. * };
  441. * };
  442. *
  443. * which follows the dtc patch from:
  444. * https://marc.info/?l=devicetree&m=144061468601974&w=4
  445. */
  446. int ufdt_overlay_do_local_fixups(struct ufdt *tree, uint32_t phandle_offset) {
  447. struct ufdt_node *overlay_node = ufdt_get_node_by_path(tree, "/");
  448. struct ufdt_node *local_fixups_node =
  449. ufdt_get_node_by_path(tree, "/__local_fixups__");
  450. int err =
  451. ufdt_local_fixup_node(overlay_node, local_fixups_node, phandle_offset);
  452. if (err < 0) return -1;
  453. return 0;
  454. }
  455. static int ufdt_overlay_local_ref_update(struct ufdt *main_tree,
  456. struct ufdt *overlay_tree) {
  457. uint32_t phandle_offset = 0;
  458. phandle_offset = ufdt_get_max_phandle(main_tree);
  459. if (phandle_offset > 0) {
  460. ufdt_try_increase_phandle(overlay_tree, phandle_offset);
  461. }
  462. int err = ufdt_overlay_do_local_fixups(overlay_tree, phandle_offset);
  463. if (err < 0) {
  464. dto_error("failed to perform local fixups in overlay\n");
  465. return -1;
  466. }
  467. return 0;
  468. }
  469. /* END of updating local references (phandle values) in the overlay ufdt. */
  470. static int _ufdt_overlay_fdtps(struct ufdt *main_tree,
  471. const struct ufdt *overlay_tree) {
  472. for (int i = 0; i < overlay_tree->num_used_fdtps; i++) {
  473. void *fdt = overlay_tree->fdtps[i];
  474. if (ufdt_add_fdt(main_tree, fdt) < 0) {
  475. return -1;
  476. }
  477. }
  478. return 0;
  479. }
  480. static int ufdt_overlay_apply(struct ufdt *main_tree, struct ufdt *overlay_tree,
  481. size_t overlay_length,
  482. struct ufdt_node_pool *pool) {
  483. if (_ufdt_overlay_fdtps(main_tree, overlay_tree) < 0) {
  484. dto_error("failed to add more fdt into main ufdt tree.\n");
  485. return -1;
  486. }
  487. if (overlay_length < sizeof(struct fdt_header)) {
  488. dto_error("Overlay_length %zu smaller than header size %zu\n",
  489. overlay_length, sizeof(struct fdt_header));
  490. return -1;
  491. }
  492. if (ufdt_overlay_local_ref_update(main_tree, overlay_tree) < 0) {
  493. dto_error("failed to perform local fixups in overlay\n");
  494. return -1;
  495. }
  496. if (ufdt_overlay_do_fixups(main_tree, overlay_tree) < 0) {
  497. dto_error("failed to perform fixups in overlay\n");
  498. return -1;
  499. }
  500. if (ufdt_overlay_apply_fragments(main_tree, overlay_tree, pool) < 0) {
  501. dto_error("failed to apply fragments\n");
  502. return -1;
  503. }
  504. return 0;
  505. }
  506. struct fdt_header *ufdt_install_blob(void *blob, size_t blob_size) {
  507. struct fdt_header *pHeader;
  508. int err;
  509. dto_debug("ufdt_install_blob (0x%08jx)\n", (uintmax_t)blob);
  510. if (blob_size < sizeof(struct fdt_header)) {
  511. dto_error("Blob_size %zu smaller than the header size %zu\n", blob_size,
  512. sizeof(struct fdt_header));
  513. return NULL;
  514. }
  515. pHeader = (struct fdt_header *)blob;
  516. err = fdt_check_header(pHeader);
  517. if (err < 0) {
  518. if (err == -FDT_ERR_BADVERSION) {
  519. dto_error("incompatible blob version: %d, should be: %d",
  520. fdt_version(pHeader), FDT_LAST_SUPPORTED_VERSION);
  521. } else {
  522. dto_error("error validating blob: %s", fdt_strerror(err));
  523. }
  524. return NULL;
  525. }
  526. return pHeader;
  527. }
  528. /*
  529. * From Google, based on dt_overlay_apply() logic
  530. * Will dto_malloc a new fdt blob and return it. Will not dto_free parameters.
  531. */
  532. struct fdt_header *ufdt_apply_overlay(struct fdt_header *main_fdt_header,
  533. size_t main_fdt_size,
  534. void *overlay_fdtp,
  535. size_t overlay_size) {
  536. size_t out_fdt_size;
  537. if (main_fdt_header == NULL) {
  538. return NULL;
  539. }
  540. if (overlay_size < 8 || overlay_size != fdt_totalsize(overlay_fdtp)) {
  541. dto_error("Bad overlay size!\n");
  542. return NULL;
  543. }
  544. if (main_fdt_size < 8 || main_fdt_size != fdt_totalsize(main_fdt_header)) {
  545. dto_error("Bad fdt size!\n");
  546. return NULL;
  547. }
  548. out_fdt_size = fdt_totalsize(main_fdt_header) + overlay_size;
  549. /* It's actually more than enough */
  550. struct fdt_header *out_fdt_header = dto_malloc(out_fdt_size);
  551. if (out_fdt_header == NULL) {
  552. dto_error("failed to allocate memory for DTB blob with overlays\n");
  553. return NULL;
  554. }
  555. struct ufdt_node_pool pool;
  556. ufdt_node_pool_construct(&pool);
  557. struct ufdt *main_tree = ufdt_from_fdt(main_fdt_header, main_fdt_size, &pool);
  558. struct ufdt *overlay_tree = ufdt_from_fdt(overlay_fdtp, overlay_size, &pool);
  559. int err = ufdt_overlay_apply(main_tree, overlay_tree, overlay_size, &pool);
  560. if (err < 0) {
  561. goto fail;
  562. }
  563. err = ufdt_to_fdt(main_tree, out_fdt_header, out_fdt_size);
  564. if (err < 0) {
  565. dto_error("Failed to dump the device tree to out_fdt_header\n");
  566. goto fail;
  567. }
  568. ufdt_destruct(overlay_tree, &pool);
  569. ufdt_destruct(main_tree, &pool);
  570. ufdt_node_pool_destruct(&pool);
  571. return out_fdt_header;
  572. fail:
  573. ufdt_destruct(overlay_tree, &pool);
  574. ufdt_destruct(main_tree, &pool);
  575. ufdt_node_pool_destruct(&pool);
  576. dto_free(out_fdt_header);
  577. return NULL;
  578. }