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>
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>
26 DECLARE_GLOBAL_DATA_PTR;
28 #if defined(CONFIG_CMD_IMI)
29 static int image_info(unsigned long addr);
32 #if defined(CONFIG_CMD_IMLS)
34 #include <mtd/cfi_flash.h>
35 extern flash_info_t flash_info[]; /* info for FLASH chips */
38 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
39 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
42 bootm_headers_t images; /* pointers to os/initrd/fdt images */
44 /* we overload the cmd field with our state machine info instead of a
46 static cmd_tbl_t cmd_bootm_sub[] = {
47 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
48 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
49 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
50 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
52 #ifdef CONFIG_OF_LIBFDT
53 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
55 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
56 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
57 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
58 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
59 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
62 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
69 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
74 if (state == BOOTM_STATE_START)
75 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
77 /* Unrecognized command */
81 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
82 images.state >= state) {
83 printf("Trying to execute a command out of order\n");
87 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
92 /*******************************************************************/
93 /* bootm - boot application image from image in memory */
94 /*******************************************************************/
96 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
98 #ifdef CONFIG_NEEDS_MANUAL_RELOC
99 static int relocated = 0;
104 /* relocate names of sub-command table */
105 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
106 cmd_bootm_sub[i].name += gd->reloc_off;
112 /* determine if we have a sub command */
117 simple_strtoul(argv[0], &endp, 16);
118 /* endp pointing to NULL means that argv[0] was just a
119 * valid number, pass it along to the normal bootm processing
121 * If endp is ':' or '#' assume a FIT identifier so pass
122 * along for normal processing.
124 * Right now we assume the first arg should never be '-'
126 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
127 return do_bootm_subcommand(cmdtp, flag, argc, argv);
130 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
131 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
133 #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
134 BOOTM_STATE_OS_CMDLINE |
136 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
137 BOOTM_STATE_OS_GO, &images, 1);
140 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
142 const char *ep = getenv("autostart");
144 if (ep && !strcmp(ep, "yes")) {
146 local_args[0] = (char *)cmd;
147 local_args[1] = NULL;
148 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
149 return do_bootm(cmdtp, 0, 1, local_args);
155 #ifdef CONFIG_SYS_LONGHELP
156 static char bootm_help_text[] =
157 "[addr [arg ...]]\n - boot application image stored in memory\n"
158 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
159 "\t'arg' can be the address of an initrd image\n"
160 #if defined(CONFIG_OF_LIBFDT)
161 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
162 "\ta third argument is required which is the address of the\n"
163 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
164 "\tuse a '-' for the second argument. If you do not pass a third\n"
165 "\ta bd_info struct will be passed instead\n"
167 #if defined(CONFIG_FIT)
168 "\t\nFor the new multi component uImage format (FIT) addresses\n"
169 "\tmust be extened to include component or configuration unit name:\n"
170 "\taddr:<subimg_uname> - direct component image specification\n"
171 "\taddr#<conf_uname> - configuration specification\n"
172 "\tUse iminfo command to get the list of existing component\n"
173 "\timages and configurations.\n"
175 "\nSub-commands to do part of the bootm sequence. The sub-commands "
177 "issued in the order below (it's ok to not issue all sub-commands):\n"
178 "\tstart [addr [arg ...]]\n"
179 "\tloados - load OS image\n"
180 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
181 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
183 #if defined(CONFIG_OF_LIBFDT)
184 "\tfdt - relocate flat device tree\n"
186 "\tcmdline - OS specific command line processing/setup\n"
187 "\tbdt - OS specific bd_t processing\n"
188 "\tprep - OS specific prep before relocation or go\n"
189 #if defined(CONFIG_TRACE)
190 "\tfake - OS specific fake start without go\n"
196 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
197 "boot application image from memory", bootm_help_text
200 /*******************************************************************/
201 /* bootd - boot default image */
202 /*******************************************************************/
203 #if defined(CONFIG_CMD_BOOTD)
204 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
206 return run_command(getenv("bootcmd"), flag);
210 boot, 1, 1, do_bootd,
211 "boot default, i.e., run 'bootcmd'",
215 /* keep old command name "bootd" for backward compatibility */
217 bootd, 1, 1, do_bootd,
218 "boot default, i.e., run 'bootcmd'",
225 /*******************************************************************/
226 /* iminfo - print header info for a requested image */
227 /*******************************************************************/
228 #if defined(CONFIG_CMD_IMI)
229 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
236 return image_info(load_addr);
239 for (arg = 1; arg < argc; ++arg) {
240 addr = simple_strtoul(argv[arg], NULL, 16);
241 if (image_info(addr) != 0)
247 static int image_info(ulong addr)
249 void *hdr = (void *)addr;
251 printf("\n## Checking Image at %08lx ...\n", addr);
253 switch (genimg_get_format(hdr)) {
254 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
255 case IMAGE_FORMAT_LEGACY:
256 puts(" Legacy image found\n");
257 if (!image_check_magic(hdr)) {
258 puts(" Bad Magic Number\n");
262 if (!image_check_hcrc(hdr)) {
263 puts(" Bad Header Checksum\n");
267 image_print_contents(hdr);
269 puts(" Verifying Checksum ... ");
270 if (!image_check_dcrc(hdr)) {
271 puts(" Bad Data CRC\n");
277 #if defined(CONFIG_FIT)
278 case IMAGE_FORMAT_FIT:
279 puts(" FIT image found\n");
281 if (!fit_check_format(hdr)) {
282 puts("Bad FIT image format!\n");
286 fit_print_contents(hdr);
288 if (!fit_all_image_verify(hdr)) {
289 puts("Bad hash in FIT image!\n");
296 puts("Unknown image format!\n");
304 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
305 "print header information for application image",
307 " - print header information for application image starting at\n"
308 " address 'addr' in memory; this includes verification of the\n"
309 " image contents (magic number, header and payload checksums)"
314 /*******************************************************************/
315 /* imls - list all images found in flash */
316 /*******************************************************************/
317 #if defined(CONFIG_CMD_IMLS)
318 static int do_imls_nor(void)
324 for (i = 0, info = &flash_info[0];
325 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
327 if (info->flash_id == FLASH_UNKNOWN)
329 for (j = 0; j < info->sector_count; ++j) {
331 hdr = (void *)info->start[j];
335 switch (genimg_get_format(hdr)) {
336 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
337 case IMAGE_FORMAT_LEGACY:
338 if (!image_check_hcrc(hdr))
341 printf("Legacy Image at %08lX:\n", (ulong)hdr);
342 image_print_contents(hdr);
344 puts(" Verifying Checksum ... ");
345 if (!image_check_dcrc(hdr)) {
346 puts("Bad Data CRC\n");
352 #if defined(CONFIG_FIT)
353 case IMAGE_FORMAT_FIT:
354 if (!fit_check_format(hdr))
357 printf("FIT Image at %08lX:\n", (ulong)hdr);
358 fit_print_contents(hdr);
373 #if defined(CONFIG_CMD_IMLS_NAND)
374 static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
380 imgdata = malloc(len);
382 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
384 printf(" Low memory(cannot allocate memory for image)\n");
388 ret = nand_read_skip_bad(nand, off, &len,
390 if (ret < 0 && ret != -EUCLEAN) {
395 if (!image_check_hcrc(imgdata)) {
400 printf("Legacy Image at NAND device %d offset %08llX:\n",
402 image_print_contents(imgdata);
404 puts(" Verifying Checksum ... ");
405 if (!image_check_dcrc(imgdata))
406 puts("Bad Data CRC\n");
415 static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
421 imgdata = malloc(len);
423 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
425 printf(" Low memory(cannot allocate memory for image)\n");
429 ret = nand_read_skip_bad(nand, off, &len,
431 if (ret < 0 && ret != -EUCLEAN) {
436 if (!fit_check_format(imgdata)) {
441 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
443 fit_print_contents(imgdata);
449 static int do_imls_nand(void)
452 int nand_dev = nand_curr_device;
457 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
458 puts("\nNo NAND devices available\n");
464 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
465 nand = &nand_info[nand_dev];
466 if (!nand->name || !nand->size)
469 for (off = 0; off < nand->size; off += nand->erasesize) {
470 const image_header_t *header;
473 if (nand_block_isbad(nand, off))
476 len = sizeof(buffer);
478 ret = nand_read(nand, off, &len, (u8 *)buffer);
479 if (ret < 0 && ret != -EUCLEAN) {
480 printf("NAND read error %d at offset %08llX\n",
485 switch (genimg_get_format(buffer)) {
486 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
487 case IMAGE_FORMAT_LEGACY:
488 header = (const image_header_t *)buffer;
490 len = image_get_image_size(header);
491 nand_imls_legacyimage(nand, nand_dev, off, len);
494 #if defined(CONFIG_FIT)
495 case IMAGE_FORMAT_FIT:
496 len = fit_get_size(buffer);
497 nand_imls_fitimage(nand, nand_dev, off, len);
508 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
509 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
511 int ret_nor = 0, ret_nand = 0;
513 #if defined(CONFIG_CMD_IMLS)
514 ret_nor = do_imls_nor();
517 #if defined(CONFIG_CMD_IMLS_NAND)
518 ret_nand = do_imls_nand();
532 "list all images found in flash",
534 " - Prints information about all images found at sector/block\n"
535 " boundaries in nor/nand flash."
539 #ifdef CONFIG_CMD_BOOTZ
541 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
543 /* Please define bootz_setup() for your platform */
545 puts("Your platform's zImage format isn't supported yet!\n");
550 * zImage booting support
552 static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
553 char * const argv[], bootm_headers_t *images)
556 ulong zi_start, zi_end;
558 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
561 /* Setup Linux kernel zImage entry point */
563 images->ep = load_addr;
564 debug("* kernel: default image load address = 0x%08lx\n",
567 images->ep = simple_strtoul(argv[0], NULL, 16);
568 debug("* kernel: cmdline image address = 0x%08lx\n",
572 ret = bootz_setup(images->ep, &zi_start, &zi_end);
576 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
579 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
580 * have a header that provide this informaiton.
582 if (bootm_find_ramdisk_fdt(flag, argc, argv))
588 int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
592 /* Consume 'bootz' */
595 if (bootz_start(cmdtp, flag, argc, argv, &images))
599 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
600 * disable interrupts ourselves
602 bootm_disable_interrupts();
604 images.os.os = IH_OS_LINUX;
605 ret = do_bootm_states(cmdtp, flag, argc, argv,
606 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
613 #ifdef CONFIG_SYS_LONGHELP
614 static char bootz_help_text[] =
615 "[addr [initrd[:size]] [fdt]]\n"
616 " - boot Linux zImage stored in memory\n"
617 "\tThe argument 'initrd' is optional and specifies the address\n"
618 "\tof the initrd in memory. The optional argument ':size' allows\n"
619 "\tspecifying the size of RAW initrd.\n"
620 #if defined(CONFIG_OF_LIBFDT)
621 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
622 "\ta third argument is required which is the address of the\n"
623 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
624 "\tuse a '-' for the second argument. If you do not pass a third\n"
625 "\ta bd_info struct will be passed instead\n"
631 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
632 "boot Linux zImage image from memory", bootz_help_text
634 #endif /* CONFIG_CMD_BOOTZ */
636 #ifdef CONFIG_CMD_BOOTI
637 /* See Documentation/arm64/booting.txt in the Linux kernel */
638 struct Image_header {
639 uint32_t code0; /* Executable code */
640 uint32_t code1; /* Executable code */
641 uint64_t text_offset; /* Image load offset, LE */
642 uint64_t image_size; /* Effective Image size, LE */
643 uint64_t res1; /* reserved */
644 uint64_t res2; /* reserved */
645 uint64_t res3; /* reserved */
646 uint64_t res4; /* reserved */
647 uint32_t magic; /* Magic number */
651 #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
653 static int booti_setup(bootm_headers_t *images)
655 struct Image_header *ih;
658 ih = (struct Image_header *)map_sysmem(images->ep, 0);
660 if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
661 puts("Bad Linux ARM64 Image magic!\n");
665 if (ih->image_size == 0) {
666 puts("Image lacks image_size field, assuming 16MiB\n");
667 ih->image_size = (16 << 20);
671 * If we are not at the correct run-time location, set the new
672 * correct location and then move the image there.
674 dst = gd->bd->bi_dram[0].start + le32_to_cpu(ih->text_offset);
675 if (images->ep != dst) {
678 debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
680 src = (void *)images->ep;
682 memmove((void *)dst, src, le32_to_cpu(ih->image_size));
689 * Image booting support
691 static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
692 char * const argv[], bootm_headers_t *images)
695 struct Image_header *ih;
697 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
700 /* Setup Linux kernel Image entry point */
702 images->ep = load_addr;
703 debug("* kernel: default image load address = 0x%08lx\n",
706 images->ep = simple_strtoul(argv[0], NULL, 16);
707 debug("* kernel: cmdline image address = 0x%08lx\n",
711 ret = booti_setup(images);
715 ih = (struct Image_header *)map_sysmem(images->ep, 0);
717 lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
720 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
721 * have a header that provide this informaiton.
723 if (bootm_find_ramdisk_fdt(flag, argc, argv))
729 int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
733 /* Consume 'booti' */
736 if (booti_start(cmdtp, flag, argc, argv, &images))
740 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
741 * disable interrupts ourselves
743 bootm_disable_interrupts();
745 images.os.os = IH_OS_LINUX;
746 ret = do_bootm_states(cmdtp, flag, argc, argv,
747 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
754 #ifdef CONFIG_SYS_LONGHELP
755 static char booti_help_text[] =
756 "[addr [initrd[:size]] [fdt]]\n"
757 " - boot Linux Image stored in memory\n"
758 "\tThe argument 'initrd' is optional and specifies the address\n"
759 "\tof the initrd in memory. The optional argument ':size' allows\n"
760 "\tspecifying the size of RAW initrd.\n"
761 #if defined(CONFIG_OF_LIBFDT)
762 "\tSince booting a Linux kernelrequires a flat device-tree\n"
763 "\ta third argument is required which is the address of the\n"
764 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
765 "\tuse a '-' for the second argument.\n"
771 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
772 "boot arm64 Linux Image image from memory", booti_help_text
774 #endif /* CONFIG_CMD_BOOTI */