image-fdt.c 13 KB

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