image-fdt.c 13 KB

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