cmd_bootm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. "\tgo - start OS";
  164. #endif
  165. U_BOOT_CMD(
  166. bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
  167. "boot application image from memory", bootm_help_text
  168. );
  169. /*******************************************************************/
  170. /* bootd - boot default image */
  171. /*******************************************************************/
  172. #if defined(CONFIG_CMD_BOOTD)
  173. int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  174. {
  175. return run_command(getenv("bootcmd"), flag);
  176. }
  177. U_BOOT_CMD(
  178. boot, 1, 1, do_bootd,
  179. "boot default, i.e., run 'bootcmd'",
  180. ""
  181. );
  182. /* keep old command name "bootd" for backward compatibility */
  183. U_BOOT_CMD(
  184. bootd, 1, 1, do_bootd,
  185. "boot default, i.e., run 'bootcmd'",
  186. ""
  187. );
  188. #endif
  189. /*******************************************************************/
  190. /* iminfo - print header info for a requested image */
  191. /*******************************************************************/
  192. #if defined(CONFIG_CMD_IMI)
  193. static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  194. {
  195. int arg;
  196. ulong addr;
  197. int rcode = 0;
  198. if (argc < 2) {
  199. return image_info(load_addr);
  200. }
  201. for (arg = 1; arg < argc; ++arg) {
  202. addr = simple_strtoul(argv[arg], NULL, 16);
  203. if (image_info(addr) != 0)
  204. rcode = 1;
  205. }
  206. return rcode;
  207. }
  208. static int image_info(ulong addr)
  209. {
  210. void *hdr = (void *)addr;
  211. printf("\n## Checking Image at %08lx ...\n", addr);
  212. switch (genimg_get_format(hdr)) {
  213. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  214. case IMAGE_FORMAT_LEGACY:
  215. puts(" Legacy image found\n");
  216. if (!image_check_magic(hdr)) {
  217. puts(" Bad Magic Number\n");
  218. return 1;
  219. }
  220. if (!image_check_hcrc(hdr)) {
  221. puts(" Bad Header Checksum\n");
  222. return 1;
  223. }
  224. image_print_contents(hdr);
  225. puts(" Verifying Checksum ... ");
  226. if (!image_check_dcrc(hdr)) {
  227. puts(" Bad Data CRC\n");
  228. return 1;
  229. }
  230. puts("OK\n");
  231. return 0;
  232. #endif
  233. #if defined(CONFIG_FIT)
  234. case IMAGE_FORMAT_FIT:
  235. puts(" FIT image found\n");
  236. if (!fit_check_format(hdr)) {
  237. puts("Bad FIT image format!\n");
  238. return 1;
  239. }
  240. fit_print_contents(hdr);
  241. if (!fit_all_image_verify(hdr)) {
  242. puts("Bad hash in FIT image!\n");
  243. return 1;
  244. }
  245. return 0;
  246. #endif
  247. default:
  248. puts("Unknown image format!\n");
  249. break;
  250. }
  251. return 1;
  252. }
  253. U_BOOT_CMD(
  254. iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
  255. "print header information for application image",
  256. "addr [addr ...]\n"
  257. " - print header information for application image starting at\n"
  258. " address 'addr' in memory; this includes verification of the\n"
  259. " image contents (magic number, header and payload checksums)"
  260. );
  261. #endif
  262. /*******************************************************************/
  263. /* imls - list all images found in flash */
  264. /*******************************************************************/
  265. #if defined(CONFIG_CMD_IMLS)
  266. static int do_imls_nor(void)
  267. {
  268. flash_info_t *info;
  269. int i, j;
  270. void *hdr;
  271. for (i = 0, info = &flash_info[0];
  272. i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
  273. if (info->flash_id == FLASH_UNKNOWN)
  274. goto next_bank;
  275. for (j = 0; j < info->sector_count; ++j) {
  276. hdr = (void *)info->start[j];
  277. if (!hdr)
  278. goto next_sector;
  279. switch (genimg_get_format(hdr)) {
  280. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  281. case IMAGE_FORMAT_LEGACY:
  282. if (!image_check_hcrc(hdr))
  283. goto next_sector;
  284. printf("Legacy Image at %08lX:\n", (ulong)hdr);
  285. image_print_contents(hdr);
  286. puts(" Verifying Checksum ... ");
  287. if (!image_check_dcrc(hdr)) {
  288. puts("Bad Data CRC\n");
  289. } else {
  290. puts("OK\n");
  291. }
  292. break;
  293. #endif
  294. #if defined(CONFIG_FIT)
  295. case IMAGE_FORMAT_FIT:
  296. if (!fit_check_format(hdr))
  297. goto next_sector;
  298. printf("FIT Image at %08lX:\n", (ulong)hdr);
  299. fit_print_contents(hdr);
  300. break;
  301. #endif
  302. default:
  303. goto next_sector;
  304. }
  305. next_sector: ;
  306. }
  307. next_bank: ;
  308. }
  309. return 0;
  310. }
  311. #endif
  312. #if defined(CONFIG_CMD_IMLS_NAND)
  313. static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
  314. size_t len)
  315. {
  316. void *imgdata;
  317. int ret;
  318. imgdata = malloc(len);
  319. if (!imgdata) {
  320. printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  321. nand_dev, off);
  322. printf(" Low memory(cannot allocate memory for image)\n");
  323. return -ENOMEM;
  324. }
  325. ret = nand_read_skip_bad(nand, off, &len,
  326. imgdata);
  327. if (ret < 0 && ret != -EUCLEAN) {
  328. free(imgdata);
  329. return ret;
  330. }
  331. if (!image_check_hcrc(imgdata)) {
  332. free(imgdata);
  333. return 0;
  334. }
  335. printf("Legacy Image at NAND device %d offset %08llX:\n",
  336. nand_dev, off);
  337. image_print_contents(imgdata);
  338. puts(" Verifying Checksum ... ");
  339. if (!image_check_dcrc(imgdata))
  340. puts("Bad Data CRC\n");
  341. else
  342. puts("OK\n");
  343. free(imgdata);
  344. return 0;
  345. }
  346. static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
  347. size_t len)
  348. {
  349. void *imgdata;
  350. int ret;
  351. imgdata = malloc(len);
  352. if (!imgdata) {
  353. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  354. nand_dev, off);
  355. printf(" Low memory(cannot allocate memory for image)\n");
  356. return -ENOMEM;
  357. }
  358. ret = nand_read_skip_bad(nand, off, &len,
  359. imgdata);
  360. if (ret < 0 && ret != -EUCLEAN) {
  361. free(imgdata);
  362. return ret;
  363. }
  364. if (!fit_check_format(imgdata)) {
  365. free(imgdata);
  366. return 0;
  367. }
  368. printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  369. fit_print_contents(imgdata);
  370. free(imgdata);
  371. return 0;
  372. }
  373. static int do_imls_nand(void)
  374. {
  375. nand_info_t *nand;
  376. int nand_dev = nand_curr_device;
  377. size_t len;
  378. loff_t off;
  379. u32 buffer[16];
  380. if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  381. puts("\nNo NAND devices available\n");
  382. return -ENODEV;
  383. }
  384. printf("\n");
  385. for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  386. nand = &nand_info[nand_dev];
  387. if (!nand->name || !nand->size)
  388. continue;
  389. for (off = 0; off < nand->size; off += nand->erasesize) {
  390. const image_header_t *header;
  391. int ret;
  392. if (nand_block_isbad(nand, off))
  393. continue;
  394. len = sizeof(buffer);
  395. ret = nand_read(nand, off, &len, (u8 *)buffer);
  396. if (ret < 0 && ret != -EUCLEAN) {
  397. printf("NAND read error %d at offset %08llX\n",
  398. ret, off);
  399. continue;
  400. }
  401. switch (genimg_get_format(buffer)) {
  402. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  403. case IMAGE_FORMAT_LEGACY:
  404. header = (const image_header_t *)buffer;
  405. len = image_get_image_size(header);
  406. nand_imls_legacyimage(nand, nand_dev, off, len);
  407. break;
  408. #endif
  409. #if defined(CONFIG_FIT)
  410. case IMAGE_FORMAT_FIT:
  411. len = fit_get_size(buffer);
  412. nand_imls_fitimage(nand, nand_dev, off, len);
  413. break;
  414. #endif
  415. }
  416. }
  417. }
  418. return 0;
  419. }
  420. #endif
  421. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  422. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  423. {
  424. int ret_nor = 0, ret_nand = 0;
  425. #if defined(CONFIG_CMD_IMLS)
  426. ret_nor = do_imls_nor();
  427. #endif
  428. #if defined(CONFIG_CMD_IMLS_NAND)
  429. ret_nand = do_imls_nand();
  430. #endif
  431. if (ret_nor)
  432. return ret_nor;
  433. if (ret_nand)
  434. return ret_nand;
  435. return (0);
  436. }
  437. U_BOOT_CMD(
  438. imls, 1, 1, do_imls,
  439. "list all images found in flash",
  440. "\n"
  441. " - Prints information about all images found at sector/block\n"
  442. " boundaries in nor/nand flash."
  443. );
  444. #endif
  445. #ifdef CONFIG_CMD_BOOTZ
  446. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  447. {
  448. /* Please define bootz_setup() for your platform */
  449. puts("Your platform's zImage format isn't supported yet!\n");
  450. return -1;
  451. }
  452. /*
  453. * zImage booting support
  454. */
  455. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  456. char * const argv[], bootm_headers_t *images)
  457. {
  458. int ret;
  459. ulong zi_start, zi_end;
  460. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  461. images, 1);
  462. /* Setup Linux kernel zImage entry point */
  463. if (!argc) {
  464. images->ep = load_addr;
  465. debug("* kernel: default image load address = 0x%08lx\n",
  466. load_addr);
  467. } else {
  468. images->ep = simple_strtoul(argv[0], NULL, 16);
  469. debug("* kernel: cmdline image address = 0x%08lx\n",
  470. images->ep);
  471. }
  472. ret = bootz_setup(images->ep, &zi_start, &zi_end);
  473. if (ret != 0)
  474. return 1;
  475. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  476. /*
  477. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  478. * have a header that provide this informaiton.
  479. */
  480. if (bootm_find_ramdisk_fdt(flag, argc, argv))
  481. return 1;
  482. return 0;
  483. }
  484. int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  485. {
  486. int ret;
  487. /* Consume 'bootz' */
  488. argc--; argv++;
  489. if (bootz_start(cmdtp, flag, argc, argv, &images))
  490. return 1;
  491. /*
  492. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  493. * disable interrupts ourselves
  494. */
  495. bootm_disable_interrupts();
  496. images.os.os = IH_OS_LINUX;
  497. ret = do_bootm_states(cmdtp, flag, argc, argv,
  498. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  499. BOOTM_STATE_OS_GO,
  500. &images, 1);
  501. return ret;
  502. }
  503. #ifdef CONFIG_SYS_LONGHELP
  504. static char bootz_help_text[] =
  505. "[addr [initrd[:size]] [fdt]]\n"
  506. " - boot Linux zImage stored in memory\n"
  507. "\tThe argument 'initrd' is optional and specifies the address\n"
  508. "\tof the initrd in memory. The optional argument ':size' allows\n"
  509. "\tspecifying the size of RAW initrd.\n"
  510. #if defined(CONFIG_OF_LIBFDT)
  511. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  512. "\ta third argument is required which is the address of the\n"
  513. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  514. "\tuse a '-' for the second argument. If you do not pass a third\n"
  515. "\ta bd_info struct will be passed instead\n"
  516. #endif
  517. "";
  518. #endif
  519. U_BOOT_CMD(
  520. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  521. "boot Linux zImage image from memory", bootz_help_text
  522. );
  523. #endif /* CONFIG_CMD_BOOTZ */
  524. #ifdef CONFIG_CMD_BOOTI
  525. /* See Documentation/arm64/booting.txt in the Linux kernel */
  526. struct Image_header {
  527. uint32_t code0; /* Executable code */
  528. uint32_t code1; /* Executable code */
  529. uint64_t text_offset; /* Image load offset, LE */
  530. uint64_t image_size; /* Effective Image size, LE */
  531. uint64_t res1; /* reserved */
  532. uint64_t res2; /* reserved */
  533. uint64_t res3; /* reserved */
  534. uint64_t res4; /* reserved */
  535. uint32_t magic; /* Magic number */
  536. uint32_t res5;
  537. };
  538. #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
  539. static int booti_setup(bootm_headers_t *images)
  540. {
  541. struct Image_header *ih;
  542. uint64_t dst;
  543. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  544. if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
  545. puts("Bad Linux ARM64 Image magic!\n");
  546. return 1;
  547. }
  548. if (ih->image_size == 0) {
  549. puts("Image lacks image_size field, assuming 16MiB\n");
  550. ih->image_size = (16 << 20);
  551. }
  552. /*
  553. * If we are not at the correct run-time location, set the new
  554. * correct location and then move the image there.
  555. */
  556. dst = gd->bd->bi_dram[0].start + le32_to_cpu(ih->text_offset);
  557. if (images->ep != dst) {
  558. void *src;
  559. debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
  560. src = (void *)images->ep;
  561. images->ep = dst;
  562. memmove((void *)dst, src, le32_to_cpu(ih->image_size));
  563. }
  564. return 0;
  565. }
  566. /*
  567. * Image booting support
  568. */
  569. static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
  570. char * const argv[], bootm_headers_t *images)
  571. {
  572. int ret;
  573. struct Image_header *ih;
  574. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  575. images, 1);
  576. /* Setup Linux kernel Image entry point */
  577. if (!argc) {
  578. images->ep = load_addr;
  579. debug("* kernel: default image load address = 0x%08lx\n",
  580. load_addr);
  581. } else {
  582. images->ep = simple_strtoul(argv[0], NULL, 16);
  583. debug("* kernel: cmdline image address = 0x%08lx\n",
  584. images->ep);
  585. }
  586. ret = booti_setup(images);
  587. if (ret != 0)
  588. return 1;
  589. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  590. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
  591. /*
  592. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  593. * have a header that provide this informaiton.
  594. */
  595. if (bootm_find_ramdisk_fdt(flag, argc, argv))
  596. return 1;
  597. return 0;
  598. }
  599. int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  600. {
  601. int ret;
  602. /* Consume 'booti' */
  603. argc--; argv++;
  604. if (booti_start(cmdtp, flag, argc, argv, &images))
  605. return 1;
  606. /*
  607. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  608. * disable interrupts ourselves
  609. */
  610. bootm_disable_interrupts();
  611. images.os.os = IH_OS_LINUX;
  612. ret = do_bootm_states(cmdtp, flag, argc, argv,
  613. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  614. BOOTM_STATE_OS_GO,
  615. &images, 1);
  616. return ret;
  617. }
  618. #ifdef CONFIG_SYS_LONGHELP
  619. static char booti_help_text[] =
  620. "[addr [initrd[:size]] [fdt]]\n"
  621. " - boot Linux Image stored in memory\n"
  622. "\tThe argument 'initrd' is optional and specifies the address\n"
  623. "\tof the initrd in memory. The optional argument ':size' allows\n"
  624. "\tspecifying the size of RAW initrd.\n"
  625. #if defined(CONFIG_OF_LIBFDT)
  626. "\tSince booting a Linux kernelrequires a flat device-tree\n"
  627. "\ta third argument is required which is the address of the\n"
  628. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  629. "\tuse a '-' for the second argument.\n"
  630. #endif
  631. "";
  632. #endif
  633. U_BOOT_CMD(
  634. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  635. "boot arm64 Linux Image image from memory", booti_help_text
  636. );
  637. #endif /* CONFIG_CMD_BOOTI */