bootm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Boot support
  9. */
  10. #include <common.h>
  11. #include <bootm.h>
  12. #include <command.h>
  13. #include <environment.h>
  14. #include <errno.h>
  15. #include <image.h>
  16. #include <lmb.h>
  17. #include <malloc.h>
  18. #include <mapmem.h>
  19. #include <nand.h>
  20. #include <asm/byteorder.h>
  21. #include <linux/compiler.h>
  22. #include <linux/ctype.h>
  23. #include <linux/err.h>
  24. #include <u-boot/zlib.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. #if defined(CONFIG_CMD_IMI)
  27. static int image_info(unsigned long addr);
  28. #endif
  29. #if defined(CONFIG_CMD_IMLS)
  30. #include <flash.h>
  31. #include <mtd/cfi_flash.h>
  32. extern flash_info_t flash_info[]; /* info for FLASH chips */
  33. #endif
  34. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  35. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  36. #endif
  37. bootm_headers_t images; /* pointers to os/initrd/fdt images */
  38. /* we overload the cmd field with our state machine info instead of a
  39. * function pointer */
  40. static cmd_tbl_t cmd_bootm_sub[] = {
  41. U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
  42. U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
  43. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  44. U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
  45. #endif
  46. #ifdef CONFIG_OF_LIBFDT
  47. U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
  48. #endif
  49. U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
  50. U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
  51. U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
  52. U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
  53. U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
  54. };
  55. static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
  56. char * const argv[])
  57. {
  58. int ret = 0;
  59. long state;
  60. cmd_tbl_t *c;
  61. c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
  62. argc--; argv++;
  63. if (c) {
  64. state = (long)c->cmd;
  65. if (state == BOOTM_STATE_START)
  66. state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
  67. } else {
  68. /* Unrecognized command */
  69. return CMD_RET_USAGE;
  70. }
  71. if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
  72. images.state >= state) {
  73. printf("Trying to execute a command out of order\n");
  74. return CMD_RET_USAGE;
  75. }
  76. ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
  77. return ret;
  78. }
  79. /*******************************************************************/
  80. /* bootm - boot application image from image in memory */
  81. /*******************************************************************/
  82. int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  83. {
  84. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  85. static int relocated = 0;
  86. if (!relocated) {
  87. int i;
  88. /* relocate names of sub-command table */
  89. for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
  90. cmd_bootm_sub[i].name += gd->reloc_off;
  91. relocated = 1;
  92. }
  93. #endif
  94. /* determine if we have a sub command */
  95. argc--; argv++;
  96. if (argc > 0) {
  97. char *endp;
  98. simple_strtoul(argv[0], &endp, 16);
  99. /* endp pointing to NULL means that argv[0] was just a
  100. * valid number, pass it along to the normal bootm processing
  101. *
  102. * If endp is ':' or '#' assume a FIT identifier so pass
  103. * along for normal processing.
  104. *
  105. * Right now we assume the first arg should never be '-'
  106. */
  107. if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
  108. return do_bootm_subcommand(cmdtp, flag, argc, argv);
  109. }
  110. return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
  111. BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
  112. BOOTM_STATE_LOADOS |
  113. #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
  114. BOOTM_STATE_OS_CMDLINE |
  115. #endif
  116. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  117. BOOTM_STATE_OS_GO, &images, 1);
  118. }
  119. int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  120. {
  121. const char *ep = getenv("autostart");
  122. if (ep && !strcmp(ep, "yes")) {
  123. char *local_args[2];
  124. local_args[0] = (char *)cmd;
  125. local_args[1] = NULL;
  126. printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
  127. return do_bootm(cmdtp, 0, 1, local_args);
  128. }
  129. return 0;
  130. }
  131. #ifdef CONFIG_SYS_LONGHELP
  132. static char bootm_help_text[] =
  133. "[addr [arg ...]]\n - boot application image stored in memory\n"
  134. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  135. "\t'arg' can be the address of an initrd image\n"
  136. #if defined(CONFIG_OF_LIBFDT)
  137. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  138. "\ta third argument is required which is the address of the\n"
  139. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  140. "\tuse a '-' for the second argument. If you do not pass a third\n"
  141. "\ta bd_info struct will be passed instead\n"
  142. #endif
  143. #if defined(CONFIG_FIT)
  144. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  145. "\tmust be extened to include component or configuration unit name:\n"
  146. "\taddr:<subimg_uname> - direct component image specification\n"
  147. "\taddr#<conf_uname> - configuration specification\n"
  148. "\tUse iminfo command to get the list of existing component\n"
  149. "\timages and configurations.\n"
  150. #endif
  151. "\nSub-commands to do part of the bootm sequence. The sub-commands "
  152. "must be\n"
  153. "issued in the order below (it's ok to not issue all sub-commands):\n"
  154. "\tstart [addr [arg ...]]\n"
  155. "\tloados - load OS image\n"
  156. #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
  157. "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
  158. #endif
  159. #if defined(CONFIG_OF_LIBFDT)
  160. "\tfdt - relocate flat device tree\n"
  161. #endif
  162. "\tcmdline - OS specific command line processing/setup\n"
  163. "\tbdt - OS specific bd_t processing\n"
  164. "\tprep - OS specific prep before relocation or go\n"
  165. #if defined(CONFIG_TRACE)
  166. "\tfake - OS specific fake start without go\n"
  167. #endif
  168. "\tgo - start OS";
  169. #endif
  170. U_BOOT_CMD(
  171. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  172. "boot application image from memory", bootm_help_text
  173. );
  174. /*******************************************************************/
  175. /* bootd - boot default image */
  176. /*******************************************************************/
  177. #if defined(CONFIG_CMD_BOOTD)
  178. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  179. {
  180. return run_command(getenv("bootcmd"), flag);
  181. }
  182. U_BOOT_CMD(
  183. boot, 1, 1, do_bootd,
  184. "boot default, i.e., run 'bootcmd'",
  185. ""
  186. );
  187. /* keep old command name "bootd" for backward compatibility */
  188. U_BOOT_CMD(
  189. bootd, 1, 1, do_bootd,
  190. "boot default, i.e., run 'bootcmd'",
  191. ""
  192. );
  193. #endif
  194. /*******************************************************************/
  195. /* iminfo - print header info for a requested image */
  196. /*******************************************************************/
  197. #if defined(CONFIG_CMD_IMI)
  198. static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  199. {
  200. int arg;
  201. ulong addr;
  202. int rcode = 0;
  203. if (argc < 2) {
  204. return image_info(load_addr);
  205. }
  206. for (arg = 1; arg < argc; ++arg) {
  207. addr = simple_strtoul(argv[arg], NULL, 16);
  208. if (image_info(addr) != 0)
  209. rcode = 1;
  210. }
  211. return rcode;
  212. }
  213. static int image_info(ulong addr)
  214. {
  215. void *hdr = (void *)addr;
  216. printf("\n## Checking Image at %08lx ...\n", addr);
  217. switch (genimg_get_format(hdr)) {
  218. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  219. case IMAGE_FORMAT_LEGACY:
  220. puts(" Legacy image found\n");
  221. if (!image_check_magic(hdr)) {
  222. puts(" Bad Magic Number\n");
  223. return 1;
  224. }
  225. if (!image_check_hcrc(hdr)) {
  226. puts(" Bad Header Checksum\n");
  227. return 1;
  228. }
  229. image_print_contents(hdr);
  230. puts(" Verifying Checksum ... ");
  231. if (!image_check_dcrc(hdr)) {
  232. puts(" Bad Data CRC\n");
  233. return 1;
  234. }
  235. puts("OK\n");
  236. return 0;
  237. #endif
  238. #if defined(CONFIG_FIT)
  239. case IMAGE_FORMAT_FIT:
  240. puts(" FIT image found\n");
  241. if (!fit_check_format(hdr)) {
  242. puts("Bad FIT image format!\n");
  243. return 1;
  244. }
  245. fit_print_contents(hdr);
  246. if (!fit_all_image_verify(hdr)) {
  247. puts("Bad hash in FIT image!\n");
  248. return 1;
  249. }
  250. return 0;
  251. #endif
  252. default:
  253. puts("Unknown image format!\n");
  254. break;
  255. }
  256. return 1;
  257. }
  258. U_BOOT_CMD(
  259. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  260. "print header information for application image",
  261. "addr [addr ...]\n"
  262. " - print header information for application image starting at\n"
  263. " address 'addr' in memory; this includes verification of the\n"
  264. " image contents (magic number, header and payload checksums)"
  265. );
  266. #endif
  267. /*******************************************************************/
  268. /* imls - list all images found in flash */
  269. /*******************************************************************/
  270. #if defined(CONFIG_CMD_IMLS)
  271. static int do_imls_nor(void)
  272. {
  273. flash_info_t *info;
  274. int i, j;
  275. void *hdr;
  276. for (i = 0, info = &flash_info[0];
  277. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  278. if (info->flash_id == FLASH_UNKNOWN)
  279. goto next_bank;
  280. for (j = 0; j < info->sector_count; ++j) {
  281. hdr = (void *)info->start[j];
  282. if (!hdr)
  283. goto next_sector;
  284. switch (genimg_get_format(hdr)) {
  285. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  286. case IMAGE_FORMAT_LEGACY:
  287. if (!image_check_hcrc(hdr))
  288. goto next_sector;
  289. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  290. image_print_contents(hdr);
  291. puts(" Verifying Checksum ... ");
  292. if (!image_check_dcrc(hdr)) {
  293. puts("Bad Data CRC\n");
  294. } else {
  295. puts("OK\n");
  296. }
  297. break;
  298. #endif
  299. #if defined(CONFIG_FIT)
  300. case IMAGE_FORMAT_FIT:
  301. if (!fit_check_format(hdr))
  302. goto next_sector;
  303. printf("FIT Image at %08lX:\n", (ulong)hdr);
  304. fit_print_contents(hdr);
  305. break;
  306. #endif
  307. default:
  308. goto next_sector;
  309. }
  310. next_sector: ;
  311. }
  312. next_bank: ;
  313. }
  314. return 0;
  315. }
  316. #endif
  317. #if defined(CONFIG_CMD_IMLS_NAND)
  318. static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
  319. loff_t off, size_t len)
  320. {
  321. void *imgdata;
  322. int ret;
  323. imgdata = malloc(len);
  324. if (!imgdata) {
  325. printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  326. nand_dev, off);
  327. printf(" Low memory(cannot allocate memory for image)\n");
  328. return -ENOMEM;
  329. }
  330. ret = nand_read_skip_bad(mtd, off, &len, imgdata);
  331. if (ret < 0 && ret != -EUCLEAN) {
  332. free(imgdata);
  333. return ret;
  334. }
  335. if (!image_check_hcrc(imgdata)) {
  336. free(imgdata);
  337. return 0;
  338. }
  339. printf("Legacy Image at NAND device %d offset %08llX:\n",
  340. nand_dev, off);
  341. image_print_contents(imgdata);
  342. puts(" Verifying Checksum ... ");
  343. if (!image_check_dcrc(imgdata))
  344. puts("Bad Data CRC\n");
  345. else
  346. puts("OK\n");
  347. free(imgdata);
  348. return 0;
  349. }
  350. static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
  351. size_t len)
  352. {
  353. void *imgdata;
  354. int ret;
  355. imgdata = malloc(len);
  356. if (!imgdata) {
  357. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  358. nand_dev, off);
  359. printf(" Low memory(cannot allocate memory for image)\n");
  360. return -ENOMEM;
  361. }
  362. ret = nand_read_skip_bad(mtd, off, &len, imgdata);
  363. if (ret < 0 && ret != -EUCLEAN) {
  364. free(imgdata);
  365. return ret;
  366. }
  367. if (!fit_check_format(imgdata)) {
  368. free(imgdata);
  369. return 0;
  370. }
  371. printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  372. fit_print_contents(imgdata);
  373. free(imgdata);
  374. return 0;
  375. }
  376. static int do_imls_nand(void)
  377. {
  378. struct mtd_info *mtd;
  379. int nand_dev = nand_curr_device;
  380. size_t len;
  381. loff_t off;
  382. u32 buffer[16];
  383. if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  384. puts("\nNo NAND devices available\n");
  385. return -ENODEV;
  386. }
  387. printf("\n");
  388. for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  389. mtd = nand_info[nand_dev];
  390. if (!mtd->name || !mtd->size)
  391. continue;
  392. for (off = 0; off < mtd->size; off += mtd->erasesize) {
  393. const image_header_t *header;
  394. int ret;
  395. if (nand_block_isbad(mtd, off))
  396. continue;
  397. len = sizeof(buffer);
  398. ret = nand_read(mtd, off, &len, (u8 *)buffer);
  399. if (ret < 0 && ret != -EUCLEAN) {
  400. printf("NAND read error %d at offset %08llX\n",
  401. ret, off);
  402. continue;
  403. }
  404. switch (genimg_get_format(buffer)) {
  405. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  406. case IMAGE_FORMAT_LEGACY:
  407. header = (const image_header_t *)buffer;
  408. len = image_get_image_size(header);
  409. nand_imls_legacyimage(mtd, nand_dev, off, len);
  410. break;
  411. #endif
  412. #if defined(CONFIG_FIT)
  413. case IMAGE_FORMAT_FIT:
  414. len = fit_get_size(buffer);
  415. nand_imls_fitimage(mtd, nand_dev, off, len);
  416. break;
  417. #endif
  418. }
  419. }
  420. }
  421. return 0;
  422. }
  423. #endif
  424. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  425. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  426. {
  427. int ret_nor = 0, ret_nand = 0;
  428. #if defined(CONFIG_CMD_IMLS)
  429. ret_nor = do_imls_nor();
  430. #endif
  431. #if defined(CONFIG_CMD_IMLS_NAND)
  432. ret_nand = do_imls_nand();
  433. #endif
  434. if (ret_nor)
  435. return ret_nor;
  436. if (ret_nand)
  437. return ret_nand;
  438. return (0);
  439. }
  440. U_BOOT_CMD(
  441. imls, 1, 1, do_imls,
  442. "list all images found in flash",
  443. "\n"
  444. " - Prints information about all images found at sector/block\n"
  445. " boundaries in nor/nand flash."
  446. );
  447. #endif
  448. #ifdef CONFIG_CMD_BOOTZ
  449. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  450. {
  451. /* Please define bootz_setup() for your platform */
  452. puts("Your platform's zImage format isn't supported yet!\n");
  453. return -1;
  454. }
  455. /*
  456. * zImage booting support
  457. */
  458. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  459. char * const argv[], bootm_headers_t *images)
  460. {
  461. int ret;
  462. ulong zi_start, zi_end;
  463. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  464. images, 1);
  465. /* Setup Linux kernel zImage entry point */
  466. if (!argc) {
  467. images->ep = load_addr;
  468. debug("* kernel: default image load address = 0x%08lx\n",
  469. load_addr);
  470. } else {
  471. images->ep = simple_strtoul(argv[0], NULL, 16);
  472. debug("* kernel: cmdline image address = 0x%08lx\n",
  473. images->ep);
  474. }
  475. ret = bootz_setup(images->ep, &zi_start, &zi_end);
  476. if (ret != 0)
  477. return 1;
  478. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  479. /*
  480. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  481. * have a header that provide this informaiton.
  482. */
  483. if (bootm_find_images(flag, argc, argv))
  484. return 1;
  485. return 0;
  486. }
  487. int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  488. {
  489. int ret;
  490. /* Consume 'bootz' */
  491. argc--; argv++;
  492. if (bootz_start(cmdtp, flag, argc, argv, &images))
  493. return 1;
  494. /*
  495. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  496. * disable interrupts ourselves
  497. */
  498. bootm_disable_interrupts();
  499. images.os.os = IH_OS_LINUX;
  500. ret = do_bootm_states(cmdtp, flag, argc, argv,
  501. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  502. BOOTM_STATE_OS_GO,
  503. &images, 1);
  504. return ret;
  505. }
  506. #ifdef CONFIG_SYS_LONGHELP
  507. static char bootz_help_text[] =
  508. "[addr [initrd[:size]] [fdt]]\n"
  509. " - boot Linux zImage stored in memory\n"
  510. "\tThe argument 'initrd' is optional and specifies the address\n"
  511. "\tof the initrd in memory. The optional argument ':size' allows\n"
  512. "\tspecifying the size of RAW initrd.\n"
  513. #if defined(CONFIG_OF_LIBFDT)
  514. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  515. "\ta third argument is required which is the address of the\n"
  516. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  517. "\tuse a '-' for the second argument. If you do not pass a third\n"
  518. "\ta bd_info struct will be passed instead\n"
  519. #endif
  520. "";
  521. #endif
  522. U_BOOT_CMD(
  523. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  524. "boot Linux zImage image from memory", bootz_help_text
  525. );
  526. #endif /* CONFIG_CMD_BOOTZ */
  527. #ifdef CONFIG_CMD_BOOTI
  528. /* See Documentation/arm64/booting.txt in the Linux kernel */
  529. struct Image_header {
  530. uint32_t code0; /* Executable code */
  531. uint32_t code1; /* Executable code */
  532. uint64_t text_offset; /* Image load offset, LE */
  533. uint64_t image_size; /* Effective Image size, LE */
  534. uint64_t res1; /* reserved */
  535. uint64_t res2; /* reserved */
  536. uint64_t res3; /* reserved */
  537. uint64_t res4; /* reserved */
  538. uint32_t magic; /* Magic number */
  539. uint32_t res5;
  540. };
  541. #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
  542. static int booti_setup(bootm_headers_t *images)
  543. {
  544. struct Image_header *ih;
  545. uint64_t dst;
  546. uint64_t image_size;
  547. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  548. if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
  549. puts("Bad Linux ARM64 Image magic!\n");
  550. return 1;
  551. }
  552. if (ih->image_size == 0) {
  553. puts("Image lacks image_size field, assuming 16MiB\n");
  554. image_size = 16 << 20;
  555. } else {
  556. image_size = le64_to_cpu(ih->image_size);
  557. }
  558. /*
  559. * If we are not at the correct run-time location, set the new
  560. * correct location and then move the image there.
  561. */
  562. dst = gd->bd->bi_dram[0].start + le64_to_cpu(ih->text_offset);
  563. unmap_sysmem(ih);
  564. if (images->ep != dst) {
  565. void *src;
  566. debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
  567. src = (void *)images->ep;
  568. images->ep = dst;
  569. memmove((void *)dst, src, image_size);
  570. }
  571. return 0;
  572. }
  573. /*
  574. * Image booting support
  575. */
  576. static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
  577. char * const argv[], bootm_headers_t *images)
  578. {
  579. int ret;
  580. struct Image_header *ih;
  581. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  582. images, 1);
  583. /* Setup Linux kernel Image entry point */
  584. if (!argc) {
  585. images->ep = load_addr;
  586. debug("* kernel: default image load address = 0x%08lx\n",
  587. load_addr);
  588. } else {
  589. images->ep = simple_strtoul(argv[0], NULL, 16);
  590. debug("* kernel: cmdline image address = 0x%08lx\n",
  591. images->ep);
  592. }
  593. ret = booti_setup(images);
  594. if (ret != 0)
  595. return 1;
  596. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  597. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
  598. unmap_sysmem(ih);
  599. /*
  600. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  601. * have a header that provide this informaiton.
  602. */
  603. if (bootm_find_images(flag, argc, argv))
  604. return 1;
  605. return 0;
  606. }
  607. int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  608. {
  609. int ret;
  610. /* Consume 'booti' */
  611. argc--; argv++;
  612. if (booti_start(cmdtp, flag, argc, argv, &images))
  613. return 1;
  614. /*
  615. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  616. * disable interrupts ourselves
  617. */
  618. bootm_disable_interrupts();
  619. images.os.os = IH_OS_LINUX;
  620. ret = do_bootm_states(cmdtp, flag, argc, argv,
  621. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  622. BOOTM_STATE_OS_GO,
  623. &images, 1);
  624. return ret;
  625. }
  626. #ifdef CONFIG_SYS_LONGHELP
  627. static char booti_help_text[] =
  628. "[addr [initrd[:size]] [fdt]]\n"
  629. " - boot arm64 Linux Image stored in memory\n"
  630. "\tThe argument 'initrd' is optional and specifies the address\n"
  631. "\tof an initrd in memory. The optional parameter ':size' allows\n"
  632. "\tspecifying the size of a RAW initrd.\n"
  633. #if defined(CONFIG_OF_LIBFDT)
  634. "\tSince booting a Linux kernel requires a flat device-tree, a\n"
  635. "\tthird argument providing the address of the device-tree blob\n"
  636. "\tis required. To boot a kernel with a device-tree blob but\n"
  637. "\twithout an initrd image, use a '-' for the initrd argument.\n"
  638. #endif
  639. "";
  640. #endif
  641. U_BOOT_CMD(
  642. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  643. "boot arm64 Linux Image image from memory", booti_help_text
  644. );
  645. #endif /* CONFIG_CMD_BOOTI */