image-fdt.c 16 KB

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