image-fdt.c 13 KB

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