image-fdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #include <common.h>
  11. #include <fdt_support.h>
  12. #include <fdtdec.h>
  13. #include <env.h>
  14. #include <errno.h>
  15. #include <image.h>
  16. #include <lmb.h>
  17. #include <log.h>
  18. #include <malloc.h>
  19. #include <asm/global_data.h>
  20. #include <linux/libfdt.h>
  21. #include <mapmem.h>
  22. #include <asm/io.h>
  23. #include <dm/ofnode.h>
  24. #include <tee/optee.h>
  25. /* adding a ramdisk needs 0x44 bytes in version 2008.10 */
  26. #define FDT_RAMDISK_OVERHEAD 0x80
  27. DECLARE_GLOBAL_DATA_PTR;
  28. static void fdt_error(const char *msg)
  29. {
  30. puts("ERROR: ");
  31. puts(msg);
  32. puts(" - must RESET the board to recover.\n");
  33. }
  34. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  35. static const struct legacy_img_hdr *image_get_fdt(ulong fdt_addr)
  36. {
  37. const struct legacy_img_hdr *fdt_hdr = map_sysmem(fdt_addr, 0);
  38. image_print_contents(fdt_hdr);
  39. puts(" Verifying Checksum ... ");
  40. if (!image_check_hcrc(fdt_hdr)) {
  41. fdt_error("fdt header checksum invalid");
  42. return NULL;
  43. }
  44. if (!image_check_dcrc(fdt_hdr)) {
  45. fdt_error("fdt checksum invalid");
  46. return NULL;
  47. }
  48. puts("OK\n");
  49. if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
  50. fdt_error("uImage is not a fdt");
  51. return NULL;
  52. }
  53. if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
  54. fdt_error("uImage is compressed");
  55. return NULL;
  56. }
  57. if (fdt_check_header((void *)image_get_data(fdt_hdr)) != 0) {
  58. fdt_error("uImage data is not a fdt");
  59. return NULL;
  60. }
  61. return fdt_hdr;
  62. }
  63. #endif
  64. static void boot_fdt_reserve_region(struct lmb *lmb, uint64_t addr,
  65. uint64_t size, enum lmb_flags flags)
  66. {
  67. long ret;
  68. ret = lmb_reserve_flags(lmb, addr, size, flags);
  69. if (ret >= 0) {
  70. debug(" reserving fdt memory region: addr=%llx size=%llx flags=%x\n",
  71. (unsigned long long)addr,
  72. (unsigned long long)size, flags);
  73. } else {
  74. puts("ERROR: reserving fdt memory region failed ");
  75. printf("(addr=%llx size=%llx flags=%x)\n",
  76. (unsigned long long)addr,
  77. (unsigned long long)size, flags);
  78. }
  79. }
  80. /**
  81. * boot_fdt_add_mem_rsv_regions - Mark the memreserve and reserved-memory
  82. * sections as unusable
  83. * @lmb: pointer to lmb handle, will be used for memory mgmt
  84. * @fdt_blob: pointer to fdt blob base address
  85. *
  86. * Adds the and reserved-memorymemreserve regions in the dtb to the lmb block.
  87. * Adding the memreserve regions prevents u-boot from using them to store the
  88. * initrd or the fdt blob.
  89. */
  90. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
  91. {
  92. uint64_t addr, size;
  93. int i, total, ret;
  94. int nodeoffset, subnode;
  95. struct fdt_resource res;
  96. enum lmb_flags flags;
  97. if (fdt_check_header(fdt_blob) != 0)
  98. return;
  99. /* process memreserve sections */
  100. total = fdt_num_mem_rsv(fdt_blob);
  101. for (i = 0; i < total; i++) {
  102. if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
  103. continue;
  104. boot_fdt_reserve_region(lmb, addr, size, LMB_NONE);
  105. }
  106. /* process reserved-memory */
  107. nodeoffset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory");
  108. if (nodeoffset >= 0) {
  109. subnode = fdt_first_subnode(fdt_blob, nodeoffset);
  110. while (subnode >= 0) {
  111. /* check if this subnode has a reg property */
  112. ret = fdt_get_resource(fdt_blob, subnode, "reg", 0,
  113. &res);
  114. if (!ret && fdtdec_get_is_enabled(fdt_blob, subnode)) {
  115. flags = LMB_NONE;
  116. if (fdtdec_get_bool(fdt_blob, subnode,
  117. "no-map"))
  118. flags = LMB_NOMAP;
  119. addr = res.start;
  120. size = res.end - res.start + 1;
  121. boot_fdt_reserve_region(lmb, addr, size, flags);
  122. }
  123. subnode = fdt_next_subnode(fdt_blob, subnode);
  124. }
  125. }
  126. }
  127. /**
  128. * boot_relocate_fdt - relocate flat device tree
  129. * @lmb: pointer to lmb handle, will be used for memory mgmt
  130. * @of_flat_tree: pointer to a char* variable, will hold fdt start address
  131. * @of_size: pointer to a ulong variable, will hold fdt length
  132. *
  133. * boot_relocate_fdt() allocates a region of memory within the bootmap and
  134. * relocates the of_flat_tree into that region, even if the fdt is already in
  135. * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
  136. * bytes.
  137. *
  138. * of_flat_tree and of_size are set to final (after relocation) values
  139. *
  140. * returns:
  141. * 0 - success
  142. * 1 - failure
  143. */
  144. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
  145. {
  146. void *fdt_blob = *of_flat_tree;
  147. void *of_start = NULL;
  148. u64 start, size, usable;
  149. char *fdt_high;
  150. ulong mapsize, low;
  151. ulong of_len = 0;
  152. int bank;
  153. int err;
  154. int disable_relocation = 0;
  155. /* nothing to do */
  156. if (*of_size == 0)
  157. return 0;
  158. if (fdt_check_header(fdt_blob) != 0) {
  159. fdt_error("image is not a fdt");
  160. goto error;
  161. }
  162. /* position on a 4K boundary before the alloc_current */
  163. /* Pad the FDT by a specified amount */
  164. of_len = *of_size + CONFIG_SYS_FDT_PAD;
  165. /* If fdt_high is set use it to select the relocation address */
  166. fdt_high = env_get("fdt_high");
  167. if (fdt_high) {
  168. ulong desired_addr = hextoul(fdt_high, NULL);
  169. ulong addr;
  170. if (desired_addr == ~0UL) {
  171. /* All ones means use fdt in place */
  172. of_start = fdt_blob;
  173. lmb_reserve(lmb, map_to_sysmem(of_start), of_len);
  174. disable_relocation = 1;
  175. } else if (desired_addr) {
  176. addr = lmb_alloc_base(lmb, of_len, 0x1000,
  177. desired_addr);
  178. of_start = map_sysmem(addr, of_len);
  179. if (of_start == NULL) {
  180. puts("Failed using fdt_high value for Device Tree");
  181. goto error;
  182. }
  183. } else {
  184. addr = lmb_alloc(lmb, of_len, 0x1000);
  185. of_start = map_sysmem(addr, of_len);
  186. }
  187. } else {
  188. mapsize = env_get_bootm_mapsize();
  189. low = env_get_bootm_low();
  190. of_start = NULL;
  191. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  192. start = gd->bd->bi_dram[bank].start;
  193. size = gd->bd->bi_dram[bank].size;
  194. /* DRAM bank addresses are too low, skip it. */
  195. if (start + size < low)
  196. continue;
  197. usable = min(size, (u64)mapsize);
  198. /*
  199. * At least part of this DRAM bank is usable, try
  200. * using it for LMB allocation.
  201. */
  202. of_start = map_sysmem((ulong)lmb_alloc_base(lmb,
  203. of_len, 0x1000, start + usable), of_len);
  204. /* Allocation succeeded, use this block. */
  205. if (of_start != NULL)
  206. break;
  207. /*
  208. * Reduce the mapping size in the next bank
  209. * by the size of attempt in current bank.
  210. */
  211. mapsize -= usable - max(start, (u64)low);
  212. if (!mapsize)
  213. break;
  214. }
  215. }
  216. if (of_start == NULL) {
  217. puts("device tree - allocation error\n");
  218. goto error;
  219. }
  220. if (disable_relocation) {
  221. /*
  222. * We assume there is space after the existing fdt to use
  223. * for padding
  224. */
  225. fdt_set_totalsize(of_start, of_len);
  226. printf(" Using Device Tree in place at %p, end %p\n",
  227. of_start, of_start + of_len - 1);
  228. } else {
  229. debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
  230. fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
  231. printf(" Loading Device Tree to %p, end %p ... ",
  232. of_start, of_start + of_len - 1);
  233. err = fdt_open_into(fdt_blob, of_start, of_len);
  234. if (err != 0) {
  235. fdt_error("fdt move failed");
  236. goto error;
  237. }
  238. puts("OK\n");
  239. }
  240. *of_flat_tree = of_start;
  241. *of_size = of_len;
  242. if (IS_ENABLED(CONFIG_CMD_FDT))
  243. set_working_fdt_addr(map_to_sysmem(*of_flat_tree));
  244. return 0;
  245. error:
  246. return 1;
  247. }
  248. /**
  249. * select_fdt() - Select and locate the FDT to use
  250. *
  251. * @images: pointer to the bootm images structure
  252. * @select: name of FDT to select, or NULL for any
  253. * @arch: expected FDT architecture
  254. * @fdt_addrp: pointer to a ulong variable, will hold FDT pointer
  255. * Return: 0 if OK, -ENOPKG if no FDT (but an error should not be reported),
  256. * other -ve value on other error
  257. */
  258. static int select_fdt(struct bootm_headers *images, const char *select, u8 arch,
  259. ulong *fdt_addrp)
  260. {
  261. const char *buf;
  262. ulong fdt_addr;
  263. #if CONFIG_IS_ENABLED(FIT)
  264. const char *fit_uname_config = images->fit_uname_cfg;
  265. const char *fit_uname_fdt = NULL;
  266. ulong default_addr;
  267. int fdt_noffset;
  268. if (select) {
  269. /*
  270. * If the FDT blob comes from the FIT image and the
  271. * FIT image address is omitted in the command line
  272. * argument, try to use ramdisk or os FIT image
  273. * address or default load address.
  274. */
  275. if (images->fit_uname_rd)
  276. default_addr = (ulong)images->fit_hdr_rd;
  277. else if (images->fit_uname_os)
  278. default_addr = (ulong)images->fit_hdr_os;
  279. else
  280. default_addr = image_load_addr;
  281. if (fit_parse_conf(select, default_addr, &fdt_addr,
  282. &fit_uname_config)) {
  283. debug("* fdt: config '%s' from image at 0x%08lx\n",
  284. fit_uname_config, fdt_addr);
  285. } else if (fit_parse_subimage(select, default_addr, &fdt_addr,
  286. &fit_uname_fdt)) {
  287. debug("* fdt: subimage '%s' from image at 0x%08lx\n",
  288. fit_uname_fdt, fdt_addr);
  289. } else
  290. #endif
  291. {
  292. fdt_addr = hextoul(select, NULL);
  293. debug("* fdt: cmdline image address = 0x%08lx\n",
  294. fdt_addr);
  295. }
  296. #if CONFIG_IS_ENABLED(FIT)
  297. } else {
  298. /* use FIT configuration provided in first bootm
  299. * command argument
  300. */
  301. fdt_addr = map_to_sysmem(images->fit_hdr_os);
  302. fdt_noffset = fit_get_node_from_config(images, FIT_FDT_PROP,
  303. fdt_addr);
  304. if (fdt_noffset == -ENOENT)
  305. return -ENOPKG;
  306. else if (fdt_noffset < 0)
  307. return fdt_noffset;
  308. }
  309. #endif
  310. debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
  311. fdt_addr);
  312. /*
  313. * Check if there is an FDT image at the
  314. * address provided in the second bootm argument
  315. * check image type, for FIT images get a FIT node.
  316. */
  317. buf = map_sysmem(fdt_addr, 0);
  318. switch (genimg_get_format(buf)) {
  319. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  320. case IMAGE_FORMAT_LEGACY: {
  321. const struct legacy_img_hdr *fdt_hdr;
  322. ulong load, load_end;
  323. ulong image_start, image_data, image_end;
  324. /* verify fdt_addr points to a valid image header */
  325. printf("## Flattened Device Tree from Legacy Image at %08lx\n",
  326. fdt_addr);
  327. fdt_hdr = image_get_fdt(fdt_addr);
  328. if (!fdt_hdr)
  329. return -ENOPKG;
  330. /*
  331. * move image data to the load address,
  332. * make sure we don't overwrite initial image
  333. */
  334. image_start = (ulong)fdt_hdr;
  335. image_data = (ulong)image_get_data(fdt_hdr);
  336. image_end = image_get_image_end(fdt_hdr);
  337. load = image_get_load(fdt_hdr);
  338. load_end = load + image_get_data_size(fdt_hdr);
  339. if (load == image_start ||
  340. load == image_data) {
  341. fdt_addr = load;
  342. break;
  343. }
  344. if ((load < image_end) && (load_end > image_start)) {
  345. fdt_error("fdt overwritten");
  346. return -EFAULT;
  347. }
  348. debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
  349. image_data, load);
  350. memmove((void *)load,
  351. (void *)image_data,
  352. image_get_data_size(fdt_hdr));
  353. fdt_addr = load;
  354. break;
  355. }
  356. #endif
  357. case IMAGE_FORMAT_FIT:
  358. /*
  359. * This case will catch both: new uImage format
  360. * (libfdt based) and raw FDT blob (also libfdt
  361. * based).
  362. */
  363. #if CONFIG_IS_ENABLED(FIT)
  364. /* check FDT blob vs FIT blob */
  365. if (!fit_check_format(buf, IMAGE_SIZE_INVAL)) {
  366. ulong load, len;
  367. fdt_noffset = boot_get_fdt_fit(images, fdt_addr,
  368. &fit_uname_fdt,
  369. &fit_uname_config,
  370. arch, &load, &len);
  371. if (fdt_noffset < 0)
  372. return -ENOENT;
  373. images->fit_hdr_fdt = map_sysmem(fdt_addr, 0);
  374. images->fit_uname_fdt = fit_uname_fdt;
  375. images->fit_noffset_fdt = fdt_noffset;
  376. fdt_addr = load;
  377. break;
  378. } else
  379. #endif
  380. {
  381. /*
  382. * FDT blob
  383. */
  384. debug("* fdt: raw FDT blob\n");
  385. printf("## Flattened Device Tree blob at %08lx\n",
  386. (long)fdt_addr);
  387. }
  388. break;
  389. default:
  390. puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
  391. return -ENOENT;
  392. }
  393. *fdt_addrp = fdt_addr;
  394. return 0;
  395. }
  396. /**
  397. * boot_get_fdt - main fdt handling routine
  398. * @argc: command argument count
  399. * @argv: command argument list
  400. * @arch: architecture (IH_ARCH_...)
  401. * @images: pointer to the bootm images structure
  402. * @of_flat_tree: pointer to a char* variable, will hold fdt start address
  403. * @of_size: pointer to a ulong variable, will hold fdt length
  404. *
  405. * boot_get_fdt() is responsible for finding a valid flat device tree image.
  406. * Currently supported are the following ramdisk sources:
  407. * - multicomponent kernel/ramdisk image,
  408. * - commandline provided address of decicated ramdisk image.
  409. *
  410. * returns:
  411. * 0, if fdt image was found and valid, or skipped
  412. * of_flat_tree and of_size are set to fdt start address and length if
  413. * fdt image is found and valid
  414. *
  415. * 1, if fdt image is found but corrupted
  416. * of_flat_tree and of_size are set to 0 if no fdt exists
  417. */
  418. int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
  419. struct bootm_headers *images, char **of_flat_tree, ulong *of_size)
  420. {
  421. ulong img_addr;
  422. ulong fdt_addr;
  423. char *fdt_blob = NULL;
  424. void *buf;
  425. const char *select = NULL;
  426. *of_flat_tree = NULL;
  427. *of_size = 0;
  428. img_addr = (argc == 0) ? image_load_addr : hextoul(argv[0], NULL);
  429. buf = map_sysmem(img_addr, 0);
  430. if (argc > 2)
  431. select = argv[2];
  432. if (select || genimg_has_config(images)) {
  433. int ret;
  434. ret = select_fdt(images, select, arch, &fdt_addr);
  435. if (ret == -ENOPKG)
  436. goto no_fdt;
  437. else if (ret)
  438. return 1;
  439. printf(" Booting using the fdt blob at %#08lx\n", fdt_addr);
  440. fdt_blob = map_sysmem(fdt_addr, 0);
  441. } else if (images->legacy_hdr_valid &&
  442. image_check_type(&images->legacy_hdr_os_copy,
  443. IH_TYPE_MULTI)) {
  444. ulong fdt_data, fdt_len;
  445. /*
  446. * Now check if we have a legacy multi-component image,
  447. * get second entry data start address and len.
  448. */
  449. printf("## Flattened Device Tree from multi component Image at %08lX\n",
  450. (ulong)images->legacy_hdr_os);
  451. image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
  452. &fdt_len);
  453. if (fdt_len) {
  454. fdt_blob = (char *)fdt_data;
  455. printf(" Booting using the fdt at 0x%p\n", fdt_blob);
  456. if (fdt_check_header(fdt_blob) != 0) {
  457. fdt_error("image is not a fdt");
  458. goto error;
  459. }
  460. if (fdt_totalsize(fdt_blob) != fdt_len) {
  461. fdt_error("fdt size != image size");
  462. goto error;
  463. }
  464. } else {
  465. debug("## No Flattened Device Tree\n");
  466. goto no_fdt;
  467. }
  468. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  469. } else if (genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
  470. void *hdr = buf;
  471. ulong fdt_data, fdt_len;
  472. u32 fdt_size, dtb_idx;
  473. /*
  474. * Firstly check if this android boot image has dtb field.
  475. */
  476. dtb_idx = (u32)env_get_ulong("adtb_idx", 10, 0);
  477. if (android_image_get_dtb_by_index((ulong)hdr, 0,
  478. dtb_idx, &fdt_addr, &fdt_size)) {
  479. fdt_blob = (char *)map_sysmem(fdt_addr, 0);
  480. if (fdt_check_header(fdt_blob))
  481. goto no_fdt;
  482. debug("## Using FDT in Android image dtb area with idx %u\n", dtb_idx);
  483. } else if (!android_image_get_second(hdr, &fdt_data, &fdt_len) &&
  484. !fdt_check_header((char *)fdt_data)) {
  485. fdt_blob = (char *)fdt_data;
  486. if (fdt_totalsize(fdt_blob) != fdt_len)
  487. goto error;
  488. debug("## Using FDT in Android image second area\n");
  489. } else {
  490. fdt_addr = env_get_hex("fdtaddr", 0);
  491. if (!fdt_addr)
  492. goto no_fdt;
  493. fdt_blob = map_sysmem(fdt_addr, 0);
  494. if (fdt_check_header(fdt_blob))
  495. goto no_fdt;
  496. debug("## Using FDT at ${fdtaddr}=Ox%lx\n", fdt_addr);
  497. }
  498. #endif
  499. } else {
  500. debug("## No Flattened Device Tree\n");
  501. goto no_fdt;
  502. }
  503. *of_flat_tree = fdt_blob;
  504. *of_size = fdt_totalsize(fdt_blob);
  505. debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
  506. (ulong)*of_flat_tree, *of_size);
  507. return 0;
  508. no_fdt:
  509. debug("Continuing to boot without FDT\n");
  510. return 0;
  511. error:
  512. return 1;
  513. }
  514. /*
  515. * Verify the device tree.
  516. *
  517. * This function is called after all device tree fix-ups have been enacted,
  518. * so that the final device tree can be verified. The definition of "verified"
  519. * is up to the specific implementation. However, it generally means that the
  520. * addresses of some of the devices in the device tree are compared with the
  521. * actual addresses at which U-Boot has placed them.
  522. *
  523. * Returns 1 on success, 0 on failure. If 0 is returned, U-Boot will halt the
  524. * boot process.
  525. */
  526. __weak int ft_verify_fdt(void *fdt)
  527. {
  528. return 1;
  529. }
  530. __weak int arch_fixup_fdt(void *blob)
  531. {
  532. return 0;
  533. }
  534. int image_setup_libfdt(struct bootm_headers *images, void *blob,
  535. int of_size, struct lmb *lmb)
  536. {
  537. ulong *initrd_start = &images->initrd_start;
  538. ulong *initrd_end = &images->initrd_end;
  539. int ret = -EPERM;
  540. int fdt_ret;
  541. if (fdt_root(blob) < 0) {
  542. printf("ERROR: root node setup failed\n");
  543. goto err;
  544. }
  545. if (fdt_chosen(blob) < 0) {
  546. printf("ERROR: /chosen node create failed\n");
  547. goto err;
  548. }
  549. if (arch_fixup_fdt(blob) < 0) {
  550. printf("ERROR: arch-specific fdt fixup failed\n");
  551. goto err;
  552. }
  553. fdt_ret = optee_copy_fdt_nodes(blob);
  554. if (fdt_ret) {
  555. printf("ERROR: transfer of optee nodes to new fdt failed: %s\n",
  556. fdt_strerror(fdt_ret));
  557. goto err;
  558. }
  559. /* Store name of configuration node as u-boot,bootconf in /chosen node */
  560. if (images->fit_uname_cfg)
  561. fdt_find_and_setprop(blob, "/chosen", "u-boot,bootconf",
  562. images->fit_uname_cfg,
  563. strlen(images->fit_uname_cfg) + 1, 1);
  564. /* Update ethernet nodes */
  565. fdt_fixup_ethernet(blob);
  566. #if IS_ENABLED(CONFIG_CMD_PSTORE)
  567. /* Append PStore configuration */
  568. fdt_fixup_pstore(blob);
  569. #endif
  570. if (IS_ENABLED(CONFIG_OF_BOARD_SETUP)) {
  571. const char *skip_board_fixup;
  572. skip_board_fixup = env_get("skip_board_fixup");
  573. if (skip_board_fixup && ((int)simple_strtol(skip_board_fixup, NULL, 10) == 1)) {
  574. printf("skip board fdt fixup\n");
  575. } else {
  576. fdt_ret = ft_board_setup(blob, gd->bd);
  577. if (fdt_ret) {
  578. printf("ERROR: board-specific fdt fixup failed: %s\n",
  579. fdt_strerror(fdt_ret));
  580. goto err;
  581. }
  582. }
  583. }
  584. if (IS_ENABLED(CONFIG_OF_SYSTEM_SETUP)) {
  585. fdt_ret = ft_system_setup(blob, gd->bd);
  586. if (fdt_ret) {
  587. printf("ERROR: system-specific fdt fixup failed: %s\n",
  588. fdt_strerror(fdt_ret));
  589. goto err;
  590. }
  591. }
  592. if (!of_live_active() && CONFIG_IS_ENABLED(EVENT)) {
  593. struct event_ft_fixup fixup;
  594. fixup.tree = oftree_from_fdt(blob);
  595. fixup.images = images;
  596. if (oftree_valid(fixup.tree)) {
  597. ret = event_notify(EVT_FT_FIXUP, &fixup, sizeof(fixup));
  598. if (ret) {
  599. printf("ERROR: fdt fixup event failed: %d\n",
  600. ret);
  601. goto err;
  602. }
  603. }
  604. }
  605. /* Delete the old LMB reservation */
  606. if (lmb)
  607. lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
  608. (phys_size_t)fdt_totalsize(blob));
  609. ret = fdt_shrink_to_minimum(blob, 0);
  610. if (ret < 0)
  611. goto err;
  612. of_size = ret;
  613. if (*initrd_start && *initrd_end) {
  614. of_size += FDT_RAMDISK_OVERHEAD;
  615. fdt_set_totalsize(blob, of_size);
  616. }
  617. /* Create a new LMB reservation */
  618. if (lmb)
  619. lmb_reserve(lmb, (ulong)blob, of_size);
  620. fdt_initrd(blob, *initrd_start, *initrd_end);
  621. if (!ft_verify_fdt(blob))
  622. goto err;
  623. #if defined(CONFIG_ARCH_KEYSTONE)
  624. if (IS_ENABLED(CONFIG_OF_BOARD_SETUP))
  625. ft_board_setup_ex(blob, gd->bd);
  626. #endif
  627. return 0;
  628. err:
  629. printf(" - must RESET the board to recover.\n\n");
  630. return ret;
  631. }