cmd_bootm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 <nand.h>
  19. #include <asm/byteorder.h>
  20. #include <linux/compiler.h>
  21. #include <linux/ctype.h>
  22. #include <linux/err.h>
  23. #include <u-boot/zlib.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. #if defined(CONFIG_CMD_IMI)
  26. static int image_info(unsigned long addr);
  27. #endif
  28. #if defined(CONFIG_CMD_IMLS)
  29. #include <flash.h>
  30. #include <mtd/cfi_flash.h>
  31. extern flash_info_t flash_info[]; /* info for FLASH chips */
  32. #endif
  33. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  34. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  35. #endif
  36. bootm_headers_t images; /* pointers to os/initrd/fdt images */
  37. /* we overload the cmd field with our state machine info instead of a
  38. * function pointer */
  39. static cmd_tbl_t cmd_bootm_sub[] = {
  40. U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
  41. U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
  42. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  43. U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
  44. #endif
  45. #ifdef CONFIG_OF_LIBFDT
  46. U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
  47. #endif
  48. U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
  49. U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
  50. U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
  51. U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
  52. U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
  53. };
  54. static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
  55. char * const argv[])
  56. {
  57. int ret = 0;
  58. long state;
  59. cmd_tbl_t *c;
  60. c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
  61. argc--; argv++;
  62. if (c) {
  63. state = (long)c->cmd;
  64. if (state == BOOTM_STATE_START)
  65. state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
  66. } else {
  67. /* Unrecognized command */
  68. return CMD_RET_USAGE;
  69. }
  70. if (state != BOOTM_STATE_START && images.state >= state) {
  71. printf("Trying to execute a command out of order\n");
  72. return CMD_RET_USAGE;
  73. }
  74. ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
  75. return ret;
  76. }
  77. /*******************************************************************/
  78. /* bootm - boot application image from image in memory */
  79. /*******************************************************************/
  80. int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  81. {
  82. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  83. static int relocated = 0;
  84. if (!relocated) {
  85. int i;
  86. /* relocate names of sub-command table */
  87. for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
  88. cmd_bootm_sub[i].name += gd->reloc_off;
  89. relocated = 1;
  90. }
  91. #endif
  92. /* determine if we have a sub command */
  93. argc--; argv++;
  94. if (argc > 0) {
  95. char *endp;
  96. simple_strtoul(argv[0], &endp, 16);
  97. /* endp pointing to NULL means that argv[0] was just a
  98. * valid number, pass it along to the normal bootm processing
  99. *
  100. * If endp is ':' or '#' assume a FIT identifier so pass
  101. * along for normal processing.
  102. *
  103. * Right now we assume the first arg should never be '-'
  104. */
  105. if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
  106. return do_bootm_subcommand(cmdtp, flag, argc, argv);
  107. }
  108. return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
  109. BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
  110. BOOTM_STATE_LOADOS |
  111. #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
  112. BOOTM_STATE_OS_CMDLINE |
  113. #endif
  114. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  115. BOOTM_STATE_OS_GO, &images, 1);
  116. }
  117. int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  118. {
  119. const char *ep = getenv("autostart");
  120. if (ep && !strcmp(ep, "yes")) {
  121. char *local_args[2];
  122. local_args[0] = (char *)cmd;
  123. local_args[1] = NULL;
  124. printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
  125. return do_bootm(cmdtp, 0, 1, local_args);
  126. }
  127. return 0;
  128. }
  129. #ifdef CONFIG_SYS_LONGHELP
  130. static char bootm_help_text[] =
  131. "[addr [arg ...]]\n - boot application image stored in memory\n"
  132. "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
  133. "\t'arg' can be the address of an initrd image\n"
  134. #if defined(CONFIG_OF_LIBFDT)
  135. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  136. "\ta third argument is required which is the address of the\n"
  137. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  138. "\tuse a '-' for the second argument. If you do not pass a third\n"
  139. "\ta bd_info struct will be passed instead\n"
  140. #endif
  141. #if defined(CONFIG_FIT)
  142. "\t\nFor the new multi component uImage format (FIT) addresses\n"
  143. "\tmust be extened to include component or configuration unit name:\n"
  144. "\taddr:<subimg_uname> - direct component image specification\n"
  145. "\taddr#<conf_uname> - configuration specification\n"
  146. "\tUse iminfo command to get the list of existing component\n"
  147. "\timages and configurations.\n"
  148. #endif
  149. "\nSub-commands to do part of the bootm sequence. The sub-commands "
  150. "must be\n"
  151. "issued in the order below (it's ok to not issue all sub-commands):\n"
  152. "\tstart [addr [arg ...]]\n"
  153. "\tloados - load OS image\n"
  154. #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
  155. "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
  156. #endif
  157. #if defined(CONFIG_OF_LIBFDT)
  158. "\tfdt - relocate flat device tree\n"
  159. #endif
  160. "\tcmdline - OS specific command line processing/setup\n"
  161. "\tbdt - OS specific bd_t processing\n"
  162. "\tprep - OS specific prep before relocation or go\n"
  163. #if defined(CONFIG_TRACE)
  164. "\tfake - OS specific fake start without go\n"
  165. #endif
  166. "\tgo - start OS";
  167. #endif
  168. U_BOOT_CMD(
  169. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  170. "boot application image from memory", bootm_help_text
  171. );
  172. /*******************************************************************/
  173. /* bootd - boot default image */
  174. /*******************************************************************/
  175. #if defined(CONFIG_CMD_BOOTD)
  176. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  177. {
  178. return run_command(getenv("bootcmd"), flag);
  179. }
  180. U_BOOT_CMD(
  181. boot, 1, 1, do_bootd,
  182. "boot default, i.e., run 'bootcmd'",
  183. ""
  184. );
  185. /* keep old command name "bootd" for backward compatibility */
  186. U_BOOT_CMD(
  187. bootd, 1, 1, do_bootd,
  188. "boot default, i.e., run 'bootcmd'",
  189. ""
  190. );
  191. #endif
  192. /*******************************************************************/
  193. /* iminfo - print header info for a requested image */
  194. /*******************************************************************/
  195. #if defined(CONFIG_CMD_IMI)
  196. static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  197. {
  198. int arg;
  199. ulong addr;
  200. int rcode = 0;
  201. if (argc < 2) {
  202. return image_info(load_addr);
  203. }
  204. for (arg = 1; arg < argc; ++arg) {
  205. addr = simple_strtoul(argv[arg], NULL, 16);
  206. if (image_info(addr) != 0)
  207. rcode = 1;
  208. }
  209. return rcode;
  210. }
  211. static int image_info(ulong addr)
  212. {
  213. void *hdr = (void *)addr;
  214. printf("\n## Checking Image at %08lx ...\n", addr);
  215. switch (genimg_get_format(hdr)) {
  216. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  217. case IMAGE_FORMAT_LEGACY:
  218. puts(" Legacy image found\n");
  219. if (!image_check_magic(hdr)) {
  220. puts(" Bad Magic Number\n");
  221. return 1;
  222. }
  223. if (!image_check_hcrc(hdr)) {
  224. puts(" Bad Header Checksum\n");
  225. return 1;
  226. }
  227. image_print_contents(hdr);
  228. puts(" Verifying Checksum ... ");
  229. if (!image_check_dcrc(hdr)) {
  230. puts(" Bad Data CRC\n");
  231. return 1;
  232. }
  233. puts("OK\n");
  234. return 0;
  235. #endif
  236. #if defined(CONFIG_FIT)
  237. case IMAGE_FORMAT_FIT:
  238. puts(" FIT image found\n");
  239. if (!fit_check_format(hdr)) {
  240. puts("Bad FIT image format!\n");
  241. return 1;
  242. }
  243. fit_print_contents(hdr);
  244. if (!fit_all_image_verify(hdr)) {
  245. puts("Bad hash in FIT image!\n");
  246. return 1;
  247. }
  248. return 0;
  249. #endif
  250. default:
  251. puts("Unknown image format!\n");
  252. break;
  253. }
  254. return 1;
  255. }
  256. U_BOOT_CMD(
  257. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  258. "print header information for application image",
  259. "addr [addr ...]\n"
  260. " - print header information for application image starting at\n"
  261. " address 'addr' in memory; this includes verification of the\n"
  262. " image contents (magic number, header and payload checksums)"
  263. );
  264. #endif
  265. /*******************************************************************/
  266. /* imls - list all images found in flash */
  267. /*******************************************************************/
  268. #if defined(CONFIG_CMD_IMLS)
  269. static int do_imls_nor(void)
  270. {
  271. flash_info_t *info;
  272. int i, j;
  273. void *hdr;
  274. for (i = 0, info = &flash_info[0];
  275. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  276. if (info->flash_id == FLASH_UNKNOWN)
  277. goto next_bank;
  278. for (j = 0; j < info->sector_count; ++j) {
  279. hdr = (void *)info->start[j];
  280. if (!hdr)
  281. goto next_sector;
  282. switch (genimg_get_format(hdr)) {
  283. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  284. case IMAGE_FORMAT_LEGACY:
  285. if (!image_check_hcrc(hdr))
  286. goto next_sector;
  287. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  288. image_print_contents(hdr);
  289. puts(" Verifying Checksum ... ");
  290. if (!image_check_dcrc(hdr)) {
  291. puts("Bad Data CRC\n");
  292. } else {
  293. puts("OK\n");
  294. }
  295. break;
  296. #endif
  297. #if defined(CONFIG_FIT)
  298. case IMAGE_FORMAT_FIT:
  299. if (!fit_check_format(hdr))
  300. goto next_sector;
  301. printf("FIT Image at %08lX:\n", (ulong)hdr);
  302. fit_print_contents(hdr);
  303. break;
  304. #endif
  305. default:
  306. goto next_sector;
  307. }
  308. next_sector: ;
  309. }
  310. next_bank: ;
  311. }
  312. return 0;
  313. }
  314. #endif
  315. #if defined(CONFIG_CMD_IMLS_NAND)
  316. static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
  317. size_t len)
  318. {
  319. void *imgdata;
  320. int ret;
  321. imgdata = malloc(len);
  322. if (!imgdata) {
  323. printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  324. nand_dev, off);
  325. printf(" Low memory(cannot allocate memory for image)\n");
  326. return -ENOMEM;
  327. }
  328. ret = nand_read_skip_bad(nand, off, &len,
  329. imgdata);
  330. if (ret < 0 && ret != -EUCLEAN) {
  331. free(imgdata);
  332. return ret;
  333. }
  334. if (!image_check_hcrc(imgdata)) {
  335. free(imgdata);
  336. return 0;
  337. }
  338. printf("Legacy Image at NAND device %d offset %08llX:\n",
  339. nand_dev, off);
  340. image_print_contents(imgdata);
  341. puts(" Verifying Checksum ... ");
  342. if (!image_check_dcrc(imgdata))
  343. puts("Bad Data CRC\n");
  344. else
  345. puts("OK\n");
  346. free(imgdata);
  347. return 0;
  348. }
  349. static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
  350. size_t len)
  351. {
  352. void *imgdata;
  353. int ret;
  354. imgdata = malloc(len);
  355. if (!imgdata) {
  356. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  357. nand_dev, off);
  358. printf(" Low memory(cannot allocate memory for image)\n");
  359. return -ENOMEM;
  360. }
  361. ret = nand_read_skip_bad(nand, off, &len,
  362. 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. nand_info_t *nand;
  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. nand = &nand_info[nand_dev];
  390. if (!nand->name || !nand->size)
  391. continue;
  392. for (off = 0; off < nand->size; off += nand->erasesize) {
  393. const image_header_t *header;
  394. int ret;
  395. if (nand_block_isbad(nand, off))
  396. continue;
  397. len = sizeof(buffer);
  398. ret = nand_read(nand, 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(nand, 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(nand, 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_ramdisk_fdt(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. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  547. if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
  548. puts("Bad Linux ARM64 Image magic!\n");
  549. return 1;
  550. }
  551. if (ih->image_size == 0) {
  552. puts("Image lacks image_size field, assuming 16MiB\n");
  553. ih->image_size = (16 << 20);
  554. }
  555. /*
  556. * If we are not at the correct run-time location, set the new
  557. * correct location and then move the image there.
  558. */
  559. dst = gd->bd->bi_dram[0].start + le32_to_cpu(ih->text_offset);
  560. if (images->ep != dst) {
  561. void *src;
  562. debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
  563. src = (void *)images->ep;
  564. images->ep = dst;
  565. memmove((void *)dst, src, le32_to_cpu(ih->image_size));
  566. }
  567. return 0;
  568. }
  569. /*
  570. * Image booting support
  571. */
  572. static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
  573. char * const argv[], bootm_headers_t *images)
  574. {
  575. int ret;
  576. struct Image_header *ih;
  577. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  578. images, 1);
  579. /* Setup Linux kernel Image entry point */
  580. if (!argc) {
  581. images->ep = load_addr;
  582. debug("* kernel: default image load address = 0x%08lx\n",
  583. load_addr);
  584. } else {
  585. images->ep = simple_strtoul(argv[0], NULL, 16);
  586. debug("* kernel: cmdline image address = 0x%08lx\n",
  587. images->ep);
  588. }
  589. ret = booti_setup(images);
  590. if (ret != 0)
  591. return 1;
  592. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  593. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
  594. /*
  595. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  596. * have a header that provide this informaiton.
  597. */
  598. if (bootm_find_ramdisk_fdt(flag, argc, argv))
  599. return 1;
  600. return 0;
  601. }
  602. int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  603. {
  604. int ret;
  605. /* Consume 'booti' */
  606. argc--; argv++;
  607. if (booti_start(cmdtp, flag, argc, argv, &images))
  608. return 1;
  609. /*
  610. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  611. * disable interrupts ourselves
  612. */
  613. bootm_disable_interrupts();
  614. images.os.os = IH_OS_LINUX;
  615. ret = do_bootm_states(cmdtp, flag, argc, argv,
  616. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  617. BOOTM_STATE_OS_GO,
  618. &images, 1);
  619. return ret;
  620. }
  621. #ifdef CONFIG_SYS_LONGHELP
  622. static char booti_help_text[] =
  623. "[addr [initrd[:size]] [fdt]]\n"
  624. " - boot Linux Image stored in memory\n"
  625. "\tThe argument 'initrd' is optional and specifies the address\n"
  626. "\tof the initrd in memory. The optional argument ':size' allows\n"
  627. "\tspecifying the size of RAW initrd.\n"
  628. #if defined(CONFIG_OF_LIBFDT)
  629. "\tSince booting a Linux kernelrequires a flat device-tree\n"
  630. "\ta third argument is required which is the address of the\n"
  631. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  632. "\tuse a '-' for the second argument.\n"
  633. #endif
  634. "";
  635. #endif
  636. U_BOOT_CMD(
  637. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  638. "boot arm64 Linux Image image from memory", booti_help_text
  639. );
  640. #endif /* CONFIG_CMD_BOOTI */