image-fdt.c 15 KB

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