image-fdt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 <tee/optee.h>
  24. #ifndef CONFIG_SYS_FDT_PAD
  25. #define CONFIG_SYS_FDT_PAD 0x3000
  26. #endif
  27. /* adding a ramdisk needs 0x44 bytes in version 2008.10 */
  28. #define FDT_RAMDISK_OVERHEAD 0x80
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static void fdt_error(const char *msg)
  31. {
  32. puts("ERROR: ");
  33. puts(msg);
  34. puts(" - must RESET the board to recover.\n");
  35. }
  36. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  37. static const image_header_t *image_get_fdt(ulong fdt_addr)
  38. {
  39. const image_header_t *fdt_hdr = map_sysmem(fdt_addr, 0);
  40. image_print_contents(fdt_hdr);
  41. puts(" Verifying Checksum ... ");
  42. if (!image_check_hcrc(fdt_hdr)) {
  43. fdt_error("fdt header checksum invalid");
  44. return NULL;
  45. }
  46. if (!image_check_dcrc(fdt_hdr)) {
  47. fdt_error("fdt checksum invalid");
  48. return NULL;
  49. }
  50. puts("OK\n");
  51. if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
  52. fdt_error("uImage is not a fdt");
  53. return NULL;
  54. }
  55. if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
  56. fdt_error("uImage is compressed");
  57. return NULL;
  58. }
  59. if (fdt_check_header((void *)image_get_data(fdt_hdr)) != 0) {
  60. fdt_error("uImage data is not a fdt");
  61. return NULL;
  62. }
  63. return fdt_hdr;
  64. }
  65. #endif
  66. static void boot_fdt_reserve_region(struct lmb *lmb, uint64_t addr,
  67. uint64_t size)
  68. {
  69. long ret;
  70. ret = lmb_reserve(lmb, addr, size);
  71. if (ret >= 0) {
  72. debug(" reserving fdt memory region: addr=%llx size=%llx\n",
  73. (unsigned long long)addr, (unsigned long long)size);
  74. } else {
  75. puts("ERROR: reserving fdt memory region failed ");
  76. printf("(addr=%llx size=%llx)\n",
  77. (unsigned long long)addr, (unsigned long long)size);
  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. if (fdt_check_header(fdt_blob) != 0)
  97. return;
  98. /* process memreserve sections */
  99. total = fdt_num_mem_rsv(fdt_blob);
  100. for (i = 0; i < total; i++) {
  101. if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
  102. continue;
  103. boot_fdt_reserve_region(lmb, addr, size);
  104. }
  105. /* process reserved-memory */
  106. nodeoffset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory");
  107. if (nodeoffset >= 0) {
  108. subnode = fdt_first_subnode(fdt_blob, nodeoffset);
  109. while (subnode >= 0) {
  110. /* check if this subnode has a reg property */
  111. ret = fdt_get_resource(fdt_blob, subnode, "reg", 0,
  112. &res);
  113. if (!ret && fdtdec_get_is_enabled(fdt_blob, subnode)) {
  114. addr = res.start;
  115. size = res.end - res.start + 1;
  116. boot_fdt_reserve_region(lmb, addr, size);
  117. }
  118. subnode = fdt_next_subnode(fdt_blob, subnode);
  119. }
  120. }
  121. }
  122. /**
  123. * boot_relocate_fdt - relocate flat device tree
  124. * @lmb: pointer to lmb handle, will be used for memory mgmt
  125. * @of_flat_tree: pointer to a char* variable, will hold fdt start address
  126. * @of_size: pointer to a ulong variable, will hold fdt length
  127. *
  128. * boot_relocate_fdt() allocates a region of memory within the bootmap and
  129. * relocates the of_flat_tree into that region, even if the fdt is already in
  130. * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
  131. * bytes.
  132. *
  133. * of_flat_tree and of_size are set to final (after relocation) values
  134. *
  135. * returns:
  136. * 0 - success
  137. * 1 - failure
  138. */
  139. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
  140. {
  141. void *fdt_blob = *of_flat_tree;
  142. void *of_start = NULL;
  143. char *fdt_high;
  144. ulong of_len = 0;
  145. int err;
  146. int disable_relocation = 0;
  147. /* nothing to do */
  148. if (*of_size == 0)
  149. return 0;
  150. if (fdt_check_header(fdt_blob) != 0) {
  151. fdt_error("image is not a fdt");
  152. goto error;
  153. }
  154. /* position on a 4K boundary before the alloc_current */
  155. /* Pad the FDT by a specified amount */
  156. of_len = *of_size + CONFIG_SYS_FDT_PAD;
  157. /* If fdt_high is set use it to select the relocation address */
  158. fdt_high = env_get("fdt_high");
  159. if (fdt_high) {
  160. void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
  161. if (((ulong) desired_addr) == ~0UL) {
  162. /* All ones means use fdt in place */
  163. of_start = fdt_blob;
  164. lmb_reserve(lmb, (ulong)of_start, of_len);
  165. disable_relocation = 1;
  166. } else if (desired_addr) {
  167. of_start =
  168. (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
  169. (ulong)desired_addr);
  170. if (of_start == NULL) {
  171. puts("Failed using fdt_high value for Device Tree");
  172. goto error;
  173. }
  174. } else {
  175. of_start =
  176. (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
  177. }
  178. } else {
  179. of_start =
  180. (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
  181. env_get_bootm_mapsize()
  182. + env_get_bootm_low());
  183. }
  184. if (of_start == NULL) {
  185. puts("device tree - allocation error\n");
  186. goto error;
  187. }
  188. if (disable_relocation) {
  189. /*
  190. * We assume there is space after the existing fdt to use
  191. * for padding
  192. */
  193. fdt_set_totalsize(of_start, of_len);
  194. printf(" Using Device Tree in place at %p, end %p\n",
  195. of_start, of_start + of_len - 1);
  196. } else {
  197. debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
  198. fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
  199. printf(" Loading Device Tree to %p, end %p ... ",
  200. of_start, of_start + of_len - 1);
  201. err = fdt_open_into(fdt_blob, of_start, of_len);
  202. if (err != 0) {
  203. fdt_error("fdt move failed");
  204. goto error;
  205. }
  206. puts("OK\n");
  207. }
  208. *of_flat_tree = of_start;
  209. *of_size = of_len;
  210. if (CONFIG_IS_ENABLED(CMD_FDT))
  211. set_working_fdt_addr(map_to_sysmem(*of_flat_tree));
  212. return 0;
  213. error:
  214. return 1;
  215. }
  216. /**
  217. * boot_get_fdt - main fdt handling routine
  218. * @argc: command argument count
  219. * @argv: command argument list
  220. * @arch: architecture (IH_ARCH_...)
  221. * @images: pointer to the bootm images structure
  222. * @of_flat_tree: pointer to a char* variable, will hold fdt start address
  223. * @of_size: pointer to a ulong variable, will hold fdt length
  224. *
  225. * boot_get_fdt() is responsible for finding a valid flat device tree image.
  226. * Curently supported are the following ramdisk sources:
  227. * - multicomponent kernel/ramdisk image,
  228. * - commandline provided address of decicated ramdisk image.
  229. *
  230. * returns:
  231. * 0, if fdt image was found and valid, or skipped
  232. * of_flat_tree and of_size are set to fdt start address and length if
  233. * fdt image is found and valid
  234. *
  235. * 1, if fdt image is found but corrupted
  236. * of_flat_tree and of_size are set to 0 if no fdt exists
  237. */
  238. int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
  239. bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
  240. {
  241. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  242. const image_header_t *fdt_hdr;
  243. ulong load, load_end;
  244. ulong image_start, image_data, image_end;
  245. #endif
  246. ulong img_addr;
  247. ulong fdt_addr;
  248. char *fdt_blob = NULL;
  249. void *buf;
  250. #if CONFIG_IS_ENABLED(FIT)
  251. const char *fit_uname_config = images->fit_uname_cfg;
  252. const char *fit_uname_fdt = NULL;
  253. ulong default_addr;
  254. int fdt_noffset;
  255. #endif
  256. const char *select = NULL;
  257. *of_flat_tree = NULL;
  258. *of_size = 0;
  259. img_addr = (argc == 0) ? image_load_addr :
  260. simple_strtoul(argv[0], NULL, 16);
  261. buf = map_sysmem(img_addr, 0);
  262. if (argc > 2)
  263. select = argv[2];
  264. if (select || genimg_has_config(images)) {
  265. #if CONFIG_IS_ENABLED(FIT)
  266. if (select) {
  267. /*
  268. * If the FDT blob comes from the FIT image and the
  269. * FIT image address is omitted in the command line
  270. * argument, try to use ramdisk or os FIT image
  271. * address or default load address.
  272. */
  273. if (images->fit_uname_rd)
  274. default_addr = (ulong)images->fit_hdr_rd;
  275. else if (images->fit_uname_os)
  276. default_addr = (ulong)images->fit_hdr_os;
  277. else
  278. default_addr = image_load_addr;
  279. if (fit_parse_conf(select, default_addr,
  280. &fdt_addr, &fit_uname_config)) {
  281. debug("* fdt: config '%s' from image at 0x%08lx\n",
  282. fit_uname_config, fdt_addr);
  283. } else if (fit_parse_subimage(select, default_addr,
  284. &fdt_addr, &fit_uname_fdt)) {
  285. debug("* fdt: subimage '%s' from image at 0x%08lx\n",
  286. fit_uname_fdt, fdt_addr);
  287. } else
  288. #endif
  289. {
  290. fdt_addr = simple_strtoul(select, NULL, 16);
  291. debug("* fdt: cmdline image address = 0x%08lx\n",
  292. fdt_addr);
  293. }
  294. #if CONFIG_IS_ENABLED(FIT)
  295. } else {
  296. /* use FIT configuration provided in first bootm
  297. * command argument
  298. */
  299. fdt_addr = map_to_sysmem(images->fit_hdr_os);
  300. fdt_noffset = fit_get_node_from_config(images,
  301. FIT_FDT_PROP,
  302. fdt_addr);
  303. if (fdt_noffset == -ENOENT)
  304. return 0;
  305. else if (fdt_noffset < 0)
  306. return 1;
  307. }
  308. #endif
  309. debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
  310. fdt_addr);
  311. /*
  312. * Check if there is an FDT image at the
  313. * address provided in the second bootm argument
  314. * check image type, for FIT images get a FIT node.
  315. */
  316. buf = map_sysmem(fdt_addr, 0);
  317. switch (genimg_get_format(buf)) {
  318. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  319. case IMAGE_FORMAT_LEGACY:
  320. /* verify fdt_addr points to a valid image header */
  321. printf("## Flattened Device Tree from Legacy Image at %08lx\n",
  322. fdt_addr);
  323. fdt_hdr = image_get_fdt(fdt_addr);
  324. if (!fdt_hdr)
  325. goto no_fdt;
  326. /*
  327. * move image data to the load address,
  328. * make sure we don't overwrite initial image
  329. */
  330. image_start = (ulong)fdt_hdr;
  331. image_data = (ulong)image_get_data(fdt_hdr);
  332. image_end = image_get_image_end(fdt_hdr);
  333. load = image_get_load(fdt_hdr);
  334. load_end = load + image_get_data_size(fdt_hdr);
  335. if (load == image_start ||
  336. load == image_data) {
  337. fdt_addr = load;
  338. break;
  339. }
  340. if ((load < image_end) && (load_end > image_start)) {
  341. fdt_error("fdt overwritten");
  342. goto error;
  343. }
  344. debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
  345. image_data, load);
  346. memmove((void *)load,
  347. (void *)image_data,
  348. image_get_data_size(fdt_hdr));
  349. fdt_addr = load;
  350. break;
  351. #endif
  352. case IMAGE_FORMAT_FIT:
  353. /*
  354. * This case will catch both: new uImage format
  355. * (libfdt based) and raw FDT blob (also libfdt
  356. * based).
  357. */
  358. #if CONFIG_IS_ENABLED(FIT)
  359. /* check FDT blob vs FIT blob */
  360. if (!fit_check_format(buf, IMAGE_SIZE_INVAL)) {
  361. ulong load, len;
  362. fdt_noffset = boot_get_fdt_fit(images,
  363. fdt_addr, &fit_uname_fdt,
  364. &fit_uname_config,
  365. arch, &load, &len);
  366. if (fdt_noffset < 0)
  367. goto error;
  368. images->fit_hdr_fdt = map_sysmem(fdt_addr, 0);
  369. images->fit_uname_fdt = fit_uname_fdt;
  370. images->fit_noffset_fdt = fdt_noffset;
  371. fdt_addr = load;
  372. break;
  373. } else
  374. #endif
  375. {
  376. /*
  377. * FDT blob
  378. */
  379. debug("* fdt: raw FDT blob\n");
  380. printf("## Flattened Device Tree blob at %08lx\n",
  381. (long)fdt_addr);
  382. }
  383. break;
  384. default:
  385. puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
  386. goto error;
  387. }
  388. printf(" Booting using the fdt blob at %#08lx\n", fdt_addr);
  389. fdt_blob = map_sysmem(fdt_addr, 0);
  390. } else if (images->legacy_hdr_valid &&
  391. image_check_type(&images->legacy_hdr_os_copy,
  392. IH_TYPE_MULTI)) {
  393. ulong fdt_data, fdt_len;
  394. /*
  395. * Now check if we have a legacy multi-component image,
  396. * get second entry data start address and len.
  397. */
  398. printf("## Flattened Device Tree from multi component Image at %08lX\n",
  399. (ulong)images->legacy_hdr_os);
  400. image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
  401. &fdt_len);
  402. if (fdt_len) {
  403. fdt_blob = (char *)fdt_data;
  404. printf(" Booting using the fdt at 0x%p\n", fdt_blob);
  405. if (fdt_check_header(fdt_blob) != 0) {
  406. fdt_error("image is not a fdt");
  407. goto error;
  408. }
  409. if (fdt_totalsize(fdt_blob) != fdt_len) {
  410. fdt_error("fdt size != image size");
  411. goto error;
  412. }
  413. } else {
  414. debug("## No Flattened Device Tree\n");
  415. goto no_fdt;
  416. }
  417. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  418. } else if (genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
  419. struct andr_img_hdr *hdr = buf;
  420. ulong fdt_data, fdt_len;
  421. u32 fdt_size, dtb_idx;
  422. /*
  423. * Firstly check if this android boot image has dtb field.
  424. */
  425. dtb_idx = (u32)env_get_ulong("adtb_idx", 10, 0);
  426. if (android_image_get_dtb_by_index((ulong)hdr, dtb_idx, &fdt_addr, &fdt_size)) {
  427. fdt_blob = (char *)map_sysmem(fdt_addr, 0);
  428. if (fdt_check_header(fdt_blob))
  429. goto no_fdt;
  430. debug("## Using FDT in Android image dtb area with idx %u\n", dtb_idx);
  431. } else if (!android_image_get_second(hdr, &fdt_data, &fdt_len) &&
  432. !fdt_check_header((char *)fdt_data)) {
  433. fdt_blob = (char *)fdt_data;
  434. if (fdt_totalsize(fdt_blob) != fdt_len)
  435. goto error;
  436. debug("## Using FDT in Android image second area\n");
  437. } else {
  438. fdt_addr = env_get_hex("fdtaddr", 0);
  439. if (!fdt_addr)
  440. goto no_fdt;
  441. fdt_blob = map_sysmem(fdt_addr, 0);
  442. if (fdt_check_header(fdt_blob))
  443. goto no_fdt;
  444. debug("## Using FDT at ${fdtaddr}=Ox%lx\n", fdt_addr);
  445. }
  446. #endif
  447. } else {
  448. debug("## No Flattened Device Tree\n");
  449. goto no_fdt;
  450. }
  451. *of_flat_tree = fdt_blob;
  452. *of_size = fdt_totalsize(fdt_blob);
  453. debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
  454. (ulong)*of_flat_tree, *of_size);
  455. return 0;
  456. no_fdt:
  457. debug("Continuing to boot without FDT\n");
  458. return 0;
  459. error:
  460. return 1;
  461. }
  462. /*
  463. * Verify the device tree.
  464. *
  465. * This function is called after all device tree fix-ups have been enacted,
  466. * so that the final device tree can be verified. The definition of "verified"
  467. * is up to the specific implementation. However, it generally means that the
  468. * addresses of some of the devices in the device tree are compared with the
  469. * actual addresses at which U-Boot has placed them.
  470. *
  471. * Returns 1 on success, 0 on failure. If 0 is returned, U-Boot will halt the
  472. * boot process.
  473. */
  474. __weak int ft_verify_fdt(void *fdt)
  475. {
  476. return 1;
  477. }
  478. __weak int arch_fixup_fdt(void *blob)
  479. {
  480. return 0;
  481. }
  482. int image_setup_libfdt(bootm_headers_t *images, void *blob,
  483. int of_size, struct lmb *lmb)
  484. {
  485. ulong *initrd_start = &images->initrd_start;
  486. ulong *initrd_end = &images->initrd_end;
  487. int ret = -EPERM;
  488. int fdt_ret;
  489. if (fdt_root(blob) < 0) {
  490. printf("ERROR: root node setup failed\n");
  491. goto err;
  492. }
  493. if (fdt_chosen(blob) < 0) {
  494. printf("ERROR: /chosen node create failed\n");
  495. goto err;
  496. }
  497. if (arch_fixup_fdt(blob) < 0) {
  498. printf("ERROR: arch-specific fdt fixup failed\n");
  499. goto err;
  500. }
  501. fdt_ret = optee_copy_fdt_nodes(gd->fdt_blob, blob);
  502. if (fdt_ret) {
  503. printf("ERROR: transfer of optee nodes to new fdt failed: %s\n",
  504. fdt_strerror(fdt_ret));
  505. goto err;
  506. }
  507. /* Update ethernet nodes */
  508. fdt_fixup_ethernet(blob);
  509. #if CONFIG_IS_ENABLED(CMD_PSTORE)
  510. /* Append PStore configuration */
  511. fdt_fixup_pstore(blob);
  512. #endif
  513. if (IMAGE_OF_BOARD_SETUP) {
  514. fdt_ret = ft_board_setup(blob, gd->bd);
  515. if (fdt_ret) {
  516. printf("ERROR: board-specific fdt fixup failed: %s\n",
  517. fdt_strerror(fdt_ret));
  518. goto err;
  519. }
  520. }
  521. if (IMAGE_OF_SYSTEM_SETUP) {
  522. fdt_ret = ft_system_setup(blob, gd->bd);
  523. if (fdt_ret) {
  524. printf("ERROR: system-specific fdt fixup failed: %s\n",
  525. fdt_strerror(fdt_ret));
  526. goto err;
  527. }
  528. }
  529. /* Delete the old LMB reservation */
  530. if (lmb)
  531. lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
  532. (phys_size_t)fdt_totalsize(blob));
  533. ret = fdt_shrink_to_minimum(blob, 0);
  534. if (ret < 0)
  535. goto err;
  536. of_size = ret;
  537. if (*initrd_start && *initrd_end) {
  538. of_size += FDT_RAMDISK_OVERHEAD;
  539. fdt_set_totalsize(blob, of_size);
  540. }
  541. /* Create a new LMB reservation */
  542. if (lmb)
  543. lmb_reserve(lmb, (ulong)blob, of_size);
  544. fdt_initrd(blob, *initrd_start, *initrd_end);
  545. if (!ft_verify_fdt(blob))
  546. goto err;
  547. #if defined(CONFIG_SOC_KEYSTONE)
  548. if (IMAGE_OF_BOARD_SETUP)
  549. ft_board_setup_ex(blob, gd->bd);
  550. #endif
  551. return 0;
  552. err:
  553. printf(" - must RESET the board to recover.\n\n");
  554. return ret;
  555. }