image-fdt.c 13 KB

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