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>
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>
25 DECLARE_GLOBAL_DATA_PTR;
27 #if defined(CONFIG_CMD_IMI)
28 static int image_info(unsigned long addr);
31 #if defined(CONFIG_CMD_IMLS)
33 #include <mtd/cfi_flash.h>
34 extern flash_info_t flash_info[]; /* info for FLASH chips */
37 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
38 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
41 bootm_headers_t images; /* pointers to os/initrd/fdt images */
43 /* we overload the cmd field with our state machine info instead of a
45 static cmd_tbl_t cmd_bootm_sub[] = {
46 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
47 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
48 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
49 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
51 #ifdef CONFIG_OF_LIBFDT
52 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
54 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
55 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
56 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
57 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
58 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
61 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
68 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
73 if (state == BOOTM_STATE_START)
74 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
76 /* Unrecognized command */
80 if (state != BOOTM_STATE_START && images.state >= state) {
81 printf("Trying to execute a command out of order\n");
85 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
90 /*******************************************************************/
91 /* bootm - boot application image from image in memory */
92 /*******************************************************************/
94 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
96 #ifdef CONFIG_NEEDS_MANUAL_RELOC
97 static int relocated = 0;
102 /* relocate names of sub-command table */
103 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
104 cmd_bootm_sub[i].name += gd->reloc_off;
110 /* determine if we have a sub command */
115 simple_strtoul(argv[0], &endp, 16);
116 /* endp pointing to NULL means that argv[0] was just a
117 * valid number, pass it along to the normal bootm processing
119 * If endp is ':' or '#' assume a FIT identifier so pass
120 * along for normal processing.
122 * Right now we assume the first arg should never be '-'
124 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
125 return do_bootm_subcommand(cmdtp, flag, argc, argv);
128 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
129 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
131 #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
132 BOOTM_STATE_OS_CMDLINE |
134 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
135 BOOTM_STATE_OS_GO, &images, 1);
138 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
140 const char *ep = getenv("autostart");
142 if (ep && !strcmp(ep, "yes")) {
144 local_args[0] = (char *)cmd;
145 local_args[1] = NULL;
146 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
147 return do_bootm(cmdtp, 0, 1, local_args);
153 #ifdef CONFIG_SYS_LONGHELP
154 static char bootm_help_text[] =
155 "[addr [arg ...]]\n - boot application image stored in memory\n"
156 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
157 "\t'arg' can be the address of an initrd image\n"
158 #if defined(CONFIG_OF_LIBFDT)
159 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
160 "\ta third argument is required which is the address of the\n"
161 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
162 "\tuse a '-' for the second argument. If you do not pass a third\n"
163 "\ta bd_info struct will be passed instead\n"
165 #if defined(CONFIG_FIT)
166 "\t\nFor the new multi component uImage format (FIT) addresses\n"
167 "\tmust be extened to include component or configuration unit name:\n"
168 "\taddr:<subimg_uname> - direct component image specification\n"
169 "\taddr#<conf_uname> - configuration specification\n"
170 "\tUse iminfo command to get the list of existing component\n"
171 "\timages and configurations.\n"
173 "\nSub-commands to do part of the bootm sequence. The sub-commands "
175 "issued in the order below (it's ok to not issue all sub-commands):\n"
176 "\tstart [addr [arg ...]]\n"
177 "\tloados - load OS image\n"
178 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
179 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
181 #if defined(CONFIG_OF_LIBFDT)
182 "\tfdt - relocate flat device tree\n"
184 "\tcmdline - OS specific command line processing/setup\n"
185 "\tbdt - OS specific bd_t processing\n"
186 "\tprep - OS specific prep before relocation or go\n"
191 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
192 "boot application image from memory", bootm_help_text
195 /*******************************************************************/
196 /* bootd - boot default image */
197 /*******************************************************************/
198 #if defined(CONFIG_CMD_BOOTD)
199 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
201 return run_command(getenv("bootcmd"), flag);
205 boot, 1, 1, do_bootd,
206 "boot default, i.e., run 'bootcmd'",
210 /* keep old command name "bootd" for backward compatibility */
212 bootd, 1, 1, do_bootd,
213 "boot default, i.e., run 'bootcmd'",
220 /*******************************************************************/
221 /* iminfo - print header info for a requested image */
222 /*******************************************************************/
223 #if defined(CONFIG_CMD_IMI)
224 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
231 return image_info(load_addr);
234 for (arg = 1; arg < argc; ++arg) {
235 addr = simple_strtoul(argv[arg], NULL, 16);
236 if (image_info(addr) != 0)
242 static int image_info(ulong addr)
244 void *hdr = (void *)addr;
246 printf("\n## Checking Image at %08lx ...\n", addr);
248 switch (genimg_get_format(hdr)) {
249 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
250 case IMAGE_FORMAT_LEGACY:
251 puts(" Legacy image found\n");
252 if (!image_check_magic(hdr)) {
253 puts(" Bad Magic Number\n");
257 if (!image_check_hcrc(hdr)) {
258 puts(" Bad Header Checksum\n");
262 image_print_contents(hdr);
264 puts(" Verifying Checksum ... ");
265 if (!image_check_dcrc(hdr)) {
266 puts(" Bad Data CRC\n");
272 #if defined(CONFIG_FIT)
273 case IMAGE_FORMAT_FIT:
274 puts(" FIT image found\n");
276 if (!fit_check_format(hdr)) {
277 puts("Bad FIT image format!\n");
281 fit_print_contents(hdr);
283 if (!fit_all_image_verify(hdr)) {
284 puts("Bad hash in FIT image!\n");
291 puts("Unknown image format!\n");
299 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
300 "print header information for application image",
302 " - print header information for application image starting at\n"
303 " address 'addr' in memory; this includes verification of the\n"
304 " image contents (magic number, header and payload checksums)"
309 /*******************************************************************/
310 /* imls - list all images found in flash */
311 /*******************************************************************/
312 #if defined(CONFIG_CMD_IMLS)
313 static int do_imls_nor(void)
319 for (i = 0, info = &flash_info[0];
320 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
322 if (info->flash_id == FLASH_UNKNOWN)
324 for (j = 0; j < info->sector_count; ++j) {
326 hdr = (void *)info->start[j];
330 switch (genimg_get_format(hdr)) {
331 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
332 case IMAGE_FORMAT_LEGACY:
333 if (!image_check_hcrc(hdr))
336 printf("Legacy Image at %08lX:\n", (ulong)hdr);
337 image_print_contents(hdr);
339 puts(" Verifying Checksum ... ");
340 if (!image_check_dcrc(hdr)) {
341 puts("Bad Data CRC\n");
347 #if defined(CONFIG_FIT)
348 case IMAGE_FORMAT_FIT:
349 if (!fit_check_format(hdr))
352 printf("FIT Image at %08lX:\n", (ulong)hdr);
353 fit_print_contents(hdr);
368 #if defined(CONFIG_CMD_IMLS_NAND)
369 static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
375 imgdata = malloc(len);
377 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
379 printf(" Low memory(cannot allocate memory for image)\n");
383 ret = nand_read_skip_bad(nand, off, &len,
385 if (ret < 0 && ret != -EUCLEAN) {
390 if (!image_check_hcrc(imgdata)) {
395 printf("Legacy Image at NAND device %d offset %08llX:\n",
397 image_print_contents(imgdata);
399 puts(" Verifying Checksum ... ");
400 if (!image_check_dcrc(imgdata))
401 puts("Bad Data CRC\n");
410 static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
416 imgdata = malloc(len);
418 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
420 printf(" Low memory(cannot allocate memory for image)\n");
424 ret = nand_read_skip_bad(nand, off, &len,
426 if (ret < 0 && ret != -EUCLEAN) {
431 if (!fit_check_format(imgdata)) {
436 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
438 fit_print_contents(imgdata);
444 static int do_imls_nand(void)
447 int nand_dev = nand_curr_device;
452 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
453 puts("\nNo NAND devices available\n");
459 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
460 nand = &nand_info[nand_dev];
461 if (!nand->name || !nand->size)
464 for (off = 0; off < nand->size; off += nand->erasesize) {
465 const image_header_t *header;
468 if (nand_block_isbad(nand, off))
471 len = sizeof(buffer);
473 ret = nand_read(nand, off, &len, (u8 *)buffer);
474 if (ret < 0 && ret != -EUCLEAN) {
475 printf("NAND read error %d at offset %08llX\n",
480 switch (genimg_get_format(buffer)) {
481 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
482 case IMAGE_FORMAT_LEGACY:
483 header = (const image_header_t *)buffer;
485 len = image_get_image_size(header);
486 nand_imls_legacyimage(nand, nand_dev, off, len);
489 #if defined(CONFIG_FIT)
490 case IMAGE_FORMAT_FIT:
491 len = fit_get_size(buffer);
492 nand_imls_fitimage(nand, nand_dev, off, len);
503 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
504 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
506 int ret_nor = 0, ret_nand = 0;
508 #if defined(CONFIG_CMD_IMLS)
509 ret_nor = do_imls_nor();
512 #if defined(CONFIG_CMD_IMLS_NAND)
513 ret_nand = do_imls_nand();
527 "list all images found in flash",
529 " - Prints information about all images found at sector/block\n"
530 " boundaries in nor/nand flash."
534 #ifdef CONFIG_CMD_BOOTZ
536 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
538 /* Please define bootz_setup() for your platform */
540 puts("Your platform's zImage format isn't supported yet!\n");
545 * zImage booting support
547 static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
548 char * const argv[], bootm_headers_t *images)
551 ulong zi_start, zi_end;
553 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
556 /* Setup Linux kernel zImage entry point */
558 images->ep = load_addr;
559 debug("* kernel: default image load address = 0x%08lx\n",
562 images->ep = simple_strtoul(argv[0], NULL, 16);
563 debug("* kernel: cmdline image address = 0x%08lx\n",
567 ret = bootz_setup(images->ep, &zi_start, &zi_end);
571 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
574 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
575 * have a header that provide this informaiton.
577 if (bootm_find_ramdisk_fdt(flag, argc, argv))
583 int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
587 /* Consume 'bootz' */
590 if (bootz_start(cmdtp, flag, argc, argv, &images))
594 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
595 * disable interrupts ourselves
597 bootm_disable_interrupts();
599 images.os.os = IH_OS_LINUX;
600 ret = do_bootm_states(cmdtp, flag, argc, argv,
601 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
608 #ifdef CONFIG_SYS_LONGHELP
609 static char bootz_help_text[] =
610 "[addr [initrd[:size]] [fdt]]\n"
611 " - boot Linux zImage stored in memory\n"
612 "\tThe argument 'initrd' is optional and specifies the address\n"
613 "\tof the initrd in memory. The optional argument ':size' allows\n"
614 "\tspecifying the size of RAW initrd.\n"
615 #if defined(CONFIG_OF_LIBFDT)
616 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
617 "\ta third argument is required which is the address of the\n"
618 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
619 "\tuse a '-' for the second argument. If you do not pass a third\n"
620 "\ta bd_info struct will be passed instead\n"
626 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
627 "boot Linux zImage image from memory", bootz_help_text
629 #endif /* CONFIG_CMD_BOOTZ */