cmd_bootm.c 19 KB

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