cmd_bootm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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(nand_info_t *nand, int nand_dev, loff_t off,
  319. 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(nand, off, &len,
  331. imgdata);
  332. if (ret < 0 && ret != -EUCLEAN) {
  333. free(imgdata);
  334. return ret;
  335. }
  336. if (!image_check_hcrc(imgdata)) {
  337. free(imgdata);
  338. return 0;
  339. }
  340. printf("Legacy Image at NAND device %d offset %08llX:\n",
  341. nand_dev, off);
  342. image_print_contents(imgdata);
  343. puts(" Verifying Checksum ... ");
  344. if (!image_check_dcrc(imgdata))
  345. puts("Bad Data CRC\n");
  346. else
  347. puts("OK\n");
  348. free(imgdata);
  349. return 0;
  350. }
  351. static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
  352. size_t len)
  353. {
  354. void *imgdata;
  355. int ret;
  356. imgdata = malloc(len);
  357. if (!imgdata) {
  358. printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  359. nand_dev, off);
  360. printf(" Low memory(cannot allocate memory for image)\n");
  361. return -ENOMEM;
  362. }
  363. ret = nand_read_skip_bad(nand, off, &len,
  364. imgdata);
  365. if (ret < 0 && ret != -EUCLEAN) {
  366. free(imgdata);
  367. return ret;
  368. }
  369. if (!fit_check_format(imgdata)) {
  370. free(imgdata);
  371. return 0;
  372. }
  373. printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  374. fit_print_contents(imgdata);
  375. free(imgdata);
  376. return 0;
  377. }
  378. static int do_imls_nand(void)
  379. {
  380. nand_info_t *nand;
  381. int nand_dev = nand_curr_device;
  382. size_t len;
  383. loff_t off;
  384. u32 buffer[16];
  385. if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  386. puts("\nNo NAND devices available\n");
  387. return -ENODEV;
  388. }
  389. printf("\n");
  390. for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  391. nand = &nand_info[nand_dev];
  392. if (!nand->name || !nand->size)
  393. continue;
  394. for (off = 0; off < nand->size; off += nand->erasesize) {
  395. const image_header_t *header;
  396. int ret;
  397. if (nand_block_isbad(nand, off))
  398. continue;
  399. len = sizeof(buffer);
  400. ret = nand_read(nand, off, &len, (u8 *)buffer);
  401. if (ret < 0 && ret != -EUCLEAN) {
  402. printf("NAND read error %d at offset %08llX\n",
  403. ret, off);
  404. continue;
  405. }
  406. switch (genimg_get_format(buffer)) {
  407. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  408. case IMAGE_FORMAT_LEGACY:
  409. header = (const image_header_t *)buffer;
  410. len = image_get_image_size(header);
  411. nand_imls_legacyimage(nand, nand_dev, off, len);
  412. break;
  413. #endif
  414. #if defined(CONFIG_FIT)
  415. case IMAGE_FORMAT_FIT:
  416. len = fit_get_size(buffer);
  417. nand_imls_fitimage(nand, nand_dev, off, len);
  418. break;
  419. #endif
  420. }
  421. }
  422. }
  423. return 0;
  424. }
  425. #endif
  426. #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  427. static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  428. {
  429. int ret_nor = 0, ret_nand = 0;
  430. #if defined(CONFIG_CMD_IMLS)
  431. ret_nor = do_imls_nor();
  432. #endif
  433. #if defined(CONFIG_CMD_IMLS_NAND)
  434. ret_nand = do_imls_nand();
  435. #endif
  436. if (ret_nor)
  437. return ret_nor;
  438. if (ret_nand)
  439. return ret_nand;
  440. return (0);
  441. }
  442. U_BOOT_CMD(
  443. imls, 1, 1, do_imls,
  444. "list all images found in flash",
  445. "\n"
  446. " - Prints information about all images found at sector/block\n"
  447. " boundaries in nor/nand flash."
  448. );
  449. #endif
  450. #ifdef CONFIG_CMD_BOOTZ
  451. int __weak bootz_setup(ulong image, ulong *start, ulong *end)
  452. {
  453. /* Please define bootz_setup() for your platform */
  454. puts("Your platform's zImage format isn't supported yet!\n");
  455. return -1;
  456. }
  457. /*
  458. * zImage booting support
  459. */
  460. static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
  461. char * const argv[], bootm_headers_t *images)
  462. {
  463. int ret;
  464. ulong zi_start, zi_end;
  465. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  466. images, 1);
  467. /* Setup Linux kernel zImage entry point */
  468. if (!argc) {
  469. images->ep = load_addr;
  470. debug("* kernel: default image load address = 0x%08lx\n",
  471. load_addr);
  472. } else {
  473. images->ep = simple_strtoul(argv[0], NULL, 16);
  474. debug("* kernel: cmdline image address = 0x%08lx\n",
  475. images->ep);
  476. }
  477. ret = bootz_setup(images->ep, &zi_start, &zi_end);
  478. if (ret != 0)
  479. return 1;
  480. lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
  481. /*
  482. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  483. * have a header that provide this informaiton.
  484. */
  485. if (bootm_find_images(flag, argc, argv))
  486. return 1;
  487. return 0;
  488. }
  489. int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  490. {
  491. int ret;
  492. /* Consume 'bootz' */
  493. argc--; argv++;
  494. if (bootz_start(cmdtp, flag, argc, argv, &images))
  495. return 1;
  496. /*
  497. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  498. * disable interrupts ourselves
  499. */
  500. bootm_disable_interrupts();
  501. images.os.os = IH_OS_LINUX;
  502. ret = do_bootm_states(cmdtp, flag, argc, argv,
  503. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  504. BOOTM_STATE_OS_GO,
  505. &images, 1);
  506. return ret;
  507. }
  508. #ifdef CONFIG_SYS_LONGHELP
  509. static char bootz_help_text[] =
  510. "[addr [initrd[:size]] [fdt]]\n"
  511. " - boot Linux zImage stored in memory\n"
  512. "\tThe argument 'initrd' is optional and specifies the address\n"
  513. "\tof the initrd in memory. The optional argument ':size' allows\n"
  514. "\tspecifying the size of RAW initrd.\n"
  515. #if defined(CONFIG_OF_LIBFDT)
  516. "\tWhen booting a Linux kernel which requires a flat device-tree\n"
  517. "\ta third argument is required which is the address of the\n"
  518. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  519. "\tuse a '-' for the second argument. If you do not pass a third\n"
  520. "\ta bd_info struct will be passed instead\n"
  521. #endif
  522. "";
  523. #endif
  524. U_BOOT_CMD(
  525. bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
  526. "boot Linux zImage image from memory", bootz_help_text
  527. );
  528. #endif /* CONFIG_CMD_BOOTZ */
  529. #ifdef CONFIG_CMD_BOOTI
  530. /* See Documentation/arm64/booting.txt in the Linux kernel */
  531. struct Image_header {
  532. uint32_t code0; /* Executable code */
  533. uint32_t code1; /* Executable code */
  534. uint64_t text_offset; /* Image load offset, LE */
  535. uint64_t image_size; /* Effective Image size, LE */
  536. uint64_t res1; /* reserved */
  537. uint64_t res2; /* reserved */
  538. uint64_t res3; /* reserved */
  539. uint64_t res4; /* reserved */
  540. uint32_t magic; /* Magic number */
  541. uint32_t res5;
  542. };
  543. #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
  544. static int booti_setup(bootm_headers_t *images)
  545. {
  546. struct Image_header *ih;
  547. uint64_t dst;
  548. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  549. if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
  550. puts("Bad Linux ARM64 Image magic!\n");
  551. return 1;
  552. }
  553. if (ih->image_size == 0) {
  554. puts("Image lacks image_size field, assuming 16MiB\n");
  555. ih->image_size = (16 << 20);
  556. }
  557. /*
  558. * If we are not at the correct run-time location, set the new
  559. * correct location and then move the image there.
  560. */
  561. dst = gd->bd->bi_dram[0].start + le32_to_cpu(ih->text_offset);
  562. if (images->ep != dst) {
  563. void *src;
  564. debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
  565. src = (void *)images->ep;
  566. images->ep = dst;
  567. memmove((void *)dst, src, le32_to_cpu(ih->image_size));
  568. }
  569. return 0;
  570. }
  571. /*
  572. * Image booting support
  573. */
  574. static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
  575. char * const argv[], bootm_headers_t *images)
  576. {
  577. int ret;
  578. struct Image_header *ih;
  579. ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
  580. images, 1);
  581. /* Setup Linux kernel Image entry point */
  582. if (!argc) {
  583. images->ep = load_addr;
  584. debug("* kernel: default image load address = 0x%08lx\n",
  585. load_addr);
  586. } else {
  587. images->ep = simple_strtoul(argv[0], NULL, 16);
  588. debug("* kernel: cmdline image address = 0x%08lx\n",
  589. images->ep);
  590. }
  591. ret = booti_setup(images);
  592. if (ret != 0)
  593. return 1;
  594. ih = (struct Image_header *)map_sysmem(images->ep, 0);
  595. lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
  596. /*
  597. * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
  598. * have a header that provide this informaiton.
  599. */
  600. if (bootm_find_images(flag, argc, argv))
  601. return 1;
  602. return 0;
  603. }
  604. int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  605. {
  606. int ret;
  607. /* Consume 'booti' */
  608. argc--; argv++;
  609. if (booti_start(cmdtp, flag, argc, argv, &images))
  610. return 1;
  611. /*
  612. * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
  613. * disable interrupts ourselves
  614. */
  615. bootm_disable_interrupts();
  616. images.os.os = IH_OS_LINUX;
  617. ret = do_bootm_states(cmdtp, flag, argc, argv,
  618. BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
  619. BOOTM_STATE_OS_GO,
  620. &images, 1);
  621. return ret;
  622. }
  623. #ifdef CONFIG_SYS_LONGHELP
  624. static char booti_help_text[] =
  625. "[addr [initrd[:size]] [fdt]]\n"
  626. " - boot Linux Image stored in memory\n"
  627. "\tThe argument 'initrd' is optional and specifies the address\n"
  628. "\tof the initrd in memory. The optional argument ':size' allows\n"
  629. "\tspecifying the size of RAW initrd.\n"
  630. #if defined(CONFIG_OF_LIBFDT)
  631. "\tSince booting a Linux kernelrequires a flat device-tree\n"
  632. "\ta third argument is required which is the address of the\n"
  633. "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
  634. "\tuse a '-' for the second argument.\n"
  635. #endif
  636. "";
  637. #endif
  638. U_BOOT_CMD(
  639. booti, CONFIG_SYS_MAXARGS, 1, do_booti,
  640. "boot arm64 Linux Image image from memory", booti_help_text
  641. );
  642. #endif /* CONFIG_CMD_BOOTI */