image-fdt.c 17 KB

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