image-fdt.c 15 KB

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