2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <environment.h>
21 #include <asm/byteorder.h>
22 #include <linux/compiler.h>
23 #include <linux/ctype.h>
24 #include <linux/err.h>
25 #include <u-boot/zlib.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 #if defined(CONFIG_CMD_IMI)
30 static int image_info(unsigned long addr);
33 #if defined(CONFIG_CMD_IMLS)
35 #include <mtd/cfi_flash.h>
36 extern flash_info_t flash_info[]; /* info for FLASH chips */
39 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
40 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
43 bootm_headers_t images; /* pointers to os/initrd/fdt images */
45 /* we overload the cmd field with our state machine info instead of a
47 static cmd_tbl_t cmd_bootm_sub[] = {
48 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
49 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
50 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
51 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
53 #ifdef CONFIG_OF_LIBFDT
54 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
56 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
57 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
58 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
59 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
60 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
63 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
70 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
75 if (state == BOOTM_STATE_START)
76 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
78 /* Unrecognized command */
82 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
83 images.state >= state) {
84 printf("Trying to execute a command out of order\n");
88 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
93 /*******************************************************************/
94 /* bootm - boot application image from image in memory */
95 /*******************************************************************/
97 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
99 #ifdef CONFIG_NEEDS_MANUAL_RELOC
100 static int relocated = 0;
105 /* relocate names of sub-command table */
106 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
107 cmd_bootm_sub[i].name += gd->reloc_off;
113 /* determine if we have a sub command */
118 simple_strtoul(argv[0], &endp, 16);
119 /* endp pointing to NULL means that argv[0] was just a
120 * valid number, pass it along to the normal bootm processing
122 * If endp is ':' or '#' assume a FIT identifier so pass
123 * along for normal processing.
125 * Right now we assume the first arg should never be '-'
127 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
128 return do_bootm_subcommand(cmdtp, flag, argc, argv);
131 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
132 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
134 #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
135 BOOTM_STATE_OS_CMDLINE |
137 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
138 BOOTM_STATE_OS_GO, &images, 1);
141 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
143 const char *ep = getenv("autostart");
145 if (ep && !strcmp(ep, "yes")) {
147 local_args[0] = (char *)cmd;
148 local_args[1] = NULL;
149 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
150 return do_bootm(cmdtp, 0, 1, local_args);
156 #ifdef CONFIG_SYS_LONGHELP
157 static char bootm_help_text[] =
158 "[addr [arg ...]]\n - boot application image stored in memory\n"
159 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
160 "\t'arg' can be the address of an initrd image\n"
161 #if defined(CONFIG_OF_LIBFDT)
162 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
163 "\ta third argument is required which is the address of the\n"
164 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
165 "\tuse a '-' for the second argument. If you do not pass a third\n"
166 "\ta bd_info struct will be passed instead\n"
168 #if defined(CONFIG_FIT)
169 "\t\nFor the new multi component uImage format (FIT) addresses\n"
170 "\tmust be extened to include component or configuration unit name:\n"
171 "\taddr:<subimg_uname> - direct component image specification\n"
172 "\taddr#<conf_uname> - configuration specification\n"
173 "\tUse iminfo command to get the list of existing component\n"
174 "\timages and configurations.\n"
176 "\nSub-commands to do part of the bootm sequence. The sub-commands "
178 "issued in the order below (it's ok to not issue all sub-commands):\n"
179 "\tstart [addr [arg ...]]\n"
180 "\tloados - load OS image\n"
181 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
182 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
184 #if defined(CONFIG_OF_LIBFDT)
185 "\tfdt - relocate flat device tree\n"
187 "\tcmdline - OS specific command line processing/setup\n"
188 "\tbdt - OS specific bd_t processing\n"
189 "\tprep - OS specific prep before relocation or go\n"
190 #if defined(CONFIG_TRACE)
191 "\tfake - OS specific fake start without go\n"
197 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
198 "boot application image from memory", bootm_help_text
201 /*******************************************************************/
202 /* bootd - boot default image */
203 /*******************************************************************/
204 #if defined(CONFIG_CMD_BOOTD)
205 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
207 return run_command(getenv("bootcmd"), flag);
211 boot, 1, 1, do_bootd,
212 "boot default, i.e., run 'bootcmd'",
216 /* keep old command name "bootd" for backward compatibility */
218 bootd, 1, 1, do_bootd,
219 "boot default, i.e., run 'bootcmd'",
226 /*******************************************************************/
227 /* iminfo - print header info for a requested image */
228 /*******************************************************************/
229 #if defined(CONFIG_CMD_IMI)
230 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
237 return image_info(load_addr);
240 for (arg = 1; arg < argc; ++arg) {
241 addr = simple_strtoul(argv[arg], NULL, 16);
242 if (image_info(addr) != 0)
248 static int image_info(ulong addr)
250 void *hdr = (void *)addr;
252 printf("\n## Checking Image at %08lx ...\n", addr);
254 switch (genimg_get_format(hdr)) {
255 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
256 case IMAGE_FORMAT_LEGACY:
257 puts(" Legacy image found\n");
258 if (!image_check_magic(hdr)) {
259 puts(" Bad Magic Number\n");
263 if (!image_check_hcrc(hdr)) {
264 puts(" Bad Header Checksum\n");
268 image_print_contents(hdr);
270 puts(" Verifying Checksum ... ");
271 if (!image_check_dcrc(hdr)) {
272 puts(" Bad Data CRC\n");
278 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
279 case IMAGE_FORMAT_ANDROID:
280 puts(" Android image found\n");
281 android_print_contents(hdr);
284 #if defined(CONFIG_FIT)
285 case IMAGE_FORMAT_FIT:
286 puts(" FIT image found\n");
288 if (!fit_check_format(hdr)) {
289 puts("Bad FIT image format!\n");
293 fit_print_contents(hdr);
295 if (!fit_all_image_verify(hdr)) {
296 puts("Bad hash in FIT image!\n");
303 puts("Unknown image format!\n");
311 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
312 "print header information for application image",
314 " - print header information for application image starting at\n"
315 " address 'addr' in memory; this includes verification of the\n"
316 " image contents (magic number, header and payload checksums)"
321 /*******************************************************************/
322 /* imls - list all images found in flash */
323 /*******************************************************************/
324 #if defined(CONFIG_CMD_IMLS)
325 static int do_imls_nor(void)
331 for (i = 0, info = &flash_info[0];
332 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
334 if (info->flash_id == FLASH_UNKNOWN)
336 for (j = 0; j < info->sector_count; ++j) {
338 hdr = (void *)info->start[j];
342 switch (genimg_get_format(hdr)) {
343 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
344 case IMAGE_FORMAT_LEGACY:
345 if (!image_check_hcrc(hdr))
348 printf("Legacy Image at %08lX:\n", (ulong)hdr);
349 image_print_contents(hdr);
351 puts(" Verifying Checksum ... ");
352 if (!image_check_dcrc(hdr)) {
353 puts("Bad Data CRC\n");
359 #if defined(CONFIG_FIT)
360 case IMAGE_FORMAT_FIT:
361 if (!fit_check_format(hdr))
364 printf("FIT Image at %08lX:\n", (ulong)hdr);
365 fit_print_contents(hdr);
380 #if defined(CONFIG_CMD_IMLS_NAND)
381 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
382 loff_t off, size_t len)
387 imgdata = malloc(len);
389 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
391 printf(" Low memory(cannot allocate memory for image)\n");
395 ret = nand_read_skip_bad(mtd, off, &len, imgdata);
396 if (ret < 0 && ret != -EUCLEAN) {
401 if (!image_check_hcrc(imgdata)) {
406 printf("Legacy Image at NAND device %d offset %08llX:\n",
408 image_print_contents(imgdata);
410 puts(" Verifying Checksum ... ");
411 if (!image_check_dcrc(imgdata))
412 puts("Bad Data CRC\n");
421 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
427 imgdata = malloc(len);
429 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
431 printf(" Low memory(cannot allocate memory for image)\n");
435 ret = nand_read_skip_bad(mtd, off, &len, imgdata);
436 if (ret < 0 && ret != -EUCLEAN) {
441 if (!fit_check_format(imgdata)) {
446 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
448 fit_print_contents(imgdata);
454 static int do_imls_nand(void)
456 struct mtd_info *mtd;
457 int nand_dev = nand_curr_device;
462 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
463 puts("\nNo NAND devices available\n");
469 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
470 mtd = nand_info[nand_dev];
471 if (!mtd->name || !mtd->size)
474 for (off = 0; off < mtd->size; off += mtd->erasesize) {
475 const image_header_t *header;
478 if (nand_block_isbad(mtd, off))
481 len = sizeof(buffer);
483 ret = nand_read(mtd, off, &len, (u8 *)buffer);
484 if (ret < 0 && ret != -EUCLEAN) {
485 printf("NAND read error %d at offset %08llX\n",
490 switch (genimg_get_format(buffer)) {
491 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
492 case IMAGE_FORMAT_LEGACY:
493 header = (const image_header_t *)buffer;
495 len = image_get_image_size(header);
496 nand_imls_legacyimage(mtd, nand_dev, off, len);
499 #if defined(CONFIG_FIT)
500 case IMAGE_FORMAT_FIT:
501 len = fit_get_size(buffer);
502 nand_imls_fitimage(mtd, nand_dev, off, len);
513 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
514 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
516 int ret_nor = 0, ret_nand = 0;
518 #if defined(CONFIG_CMD_IMLS)
519 ret_nor = do_imls_nor();
522 #if defined(CONFIG_CMD_IMLS_NAND)
523 ret_nand = do_imls_nand();
537 "list all images found in flash",
539 " - Prints information about all images found at sector/block\n"
540 " boundaries in nor/nand flash."
544 #ifdef CONFIG_CMD_BOOTZ
546 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
548 /* Please define bootz_setup() for your platform */
550 puts("Your platform's zImage format isn't supported yet!\n");
555 * zImage booting support
557 static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
558 char * const argv[], bootm_headers_t *images)
561 ulong zi_start, zi_end;
563 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
566 /* Setup Linux kernel zImage entry point */
568 images->ep = load_addr;
569 debug("* kernel: default image load address = 0x%08lx\n",
572 images->ep = simple_strtoul(argv[0], NULL, 16);
573 debug("* kernel: cmdline image address = 0x%08lx\n",
577 ret = bootz_setup(images->ep, &zi_start, &zi_end);
581 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
584 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
585 * have a header that provide this informaiton.
587 if (bootm_find_images(flag, argc, argv))
593 int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
597 /* Consume 'bootz' */
600 if (bootz_start(cmdtp, flag, argc, argv, &images))
604 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
605 * disable interrupts ourselves
607 bootm_disable_interrupts();
609 images.os.os = IH_OS_LINUX;
610 ret = do_bootm_states(cmdtp, flag, argc, argv,
611 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
618 #ifdef CONFIG_SYS_LONGHELP
619 static char bootz_help_text[] =
620 "[addr [initrd[:size]] [fdt]]\n"
621 " - boot Linux zImage 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 "\tWhen booting a Linux kernel which requires 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. If you do not pass a third\n"
630 "\ta bd_info struct will be passed instead\n"
636 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
637 "boot Linux zImage image from memory", bootz_help_text
639 #endif /* CONFIG_CMD_BOOTZ */
641 #ifdef CONFIG_CMD_BOOTI
642 /* See Documentation/arm64/booting.txt in the Linux kernel */
643 struct Image_header {
644 uint32_t code0; /* Executable code */
645 uint32_t code1; /* Executable code */
646 uint64_t text_offset; /* Image load offset, LE */
647 uint64_t image_size; /* Effective Image size, LE */
648 uint64_t res1; /* reserved */
649 uint64_t res2; /* reserved */
650 uint64_t res3; /* reserved */
651 uint64_t res4; /* reserved */
652 uint32_t magic; /* Magic number */
656 #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
658 static int booti_setup(bootm_headers_t *images)
660 struct Image_header *ih;
664 ih = (struct Image_header *)map_sysmem(images->ep, 0);
666 if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
667 puts("Bad Linux ARM64 Image magic!\n");
671 if (ih->image_size == 0) {
672 puts("Image lacks image_size field, assuming 16MiB\n");
673 image_size = 16 << 20;
675 image_size = le64_to_cpu(ih->image_size);
679 * If we are not at the correct run-time location, set the new
680 * correct location and then move the image there.
682 dst = gd->bd->bi_dram[0].start + le64_to_cpu(ih->text_offset);
686 if (images->ep != dst) {
689 debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
691 src = (void *)images->ep;
693 memmove((void *)dst, src, image_size);
700 * Image booting support
702 static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
703 char * const argv[], bootm_headers_t *images)
706 struct Image_header *ih;
708 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
711 /* Setup Linux kernel Image entry point */
713 images->ep = load_addr;
714 debug("* kernel: default image load address = 0x%08lx\n",
717 images->ep = simple_strtoul(argv[0], NULL, 16);
718 debug("* kernel: cmdline image address = 0x%08lx\n",
722 ret = booti_setup(images);
726 ih = (struct Image_header *)map_sysmem(images->ep, 0);
728 lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
733 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
734 * have a header that provide this informaiton.
736 if (bootm_find_images(flag, argc, argv))
742 int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
746 /* Consume 'booti' */
749 if (booti_start(cmdtp, flag, argc, argv, &images))
753 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
754 * disable interrupts ourselves
756 bootm_disable_interrupts();
758 images.os.os = IH_OS_LINUX;
759 ret = do_bootm_states(cmdtp, flag, argc, argv,
760 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
767 #ifdef CONFIG_SYS_LONGHELP
768 static char booti_help_text[] =
769 "[addr [initrd[:size]] [fdt]]\n"
770 " - boot arm64 Linux Image stored in memory\n"
771 "\tThe argument 'initrd' is optional and specifies the address\n"
772 "\tof an initrd in memory. The optional parameter ':size' allows\n"
773 "\tspecifying the size of a RAW initrd.\n"
774 #if defined(CONFIG_OF_LIBFDT)
775 "\tSince booting a Linux kernel requires a flat device-tree, a\n"
776 "\tthird argument providing the address of the device-tree blob\n"
777 "\tis required. To boot a kernel with a device-tree blob but\n"
778 "\twithout an initrd image, use a '-' for the initrd argument.\n"
784 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
785 "boot arm64 Linux Image image from memory", booti_help_text
787 #endif /* CONFIG_CMD_BOOTI */