1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
18 #include <asm/byteorder.h>
19 #include <asm/global_data.h>
20 #include <linux/ctype.h>
21 #include <linux/err.h>
22 #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(struct cmd_tbl *cmdtp, int flag, int argc,
42 /* we overload the cmd field with our state machine info instead of a
44 static struct cmd_tbl cmd_bootm_sub[] = {
45 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
46 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
47 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
48 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
50 #ifdef CONFIG_OF_LIBFDT
51 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
53 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
54 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
55 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
56 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
57 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
60 static int do_bootm_subcommand(struct cmd_tbl *cmdtp, int flag, int argc,
67 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
72 if (state == BOOTM_STATE_START)
73 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
75 /* Unrecognized command */
79 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
80 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(struct cmd_tbl *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 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
132 BOOTM_STATE_RAMDISK |
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(struct cmd_tbl *cmdtp, const char *cmd)
143 const char *ep = env_get("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",
151 return do_bootm(cmdtp, 0, 1, local_args);
157 #ifdef CONFIG_SYS_LONGHELP
158 static char bootm_help_text[] =
159 "[addr [arg ...]]\n - boot application image stored in memory\n"
160 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
161 "\t'arg' can be the address of an initrd image\n"
162 #if defined(CONFIG_OF_LIBFDT)
163 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
164 "\ta third argument is required which is the address of the\n"
165 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
166 "\tuse a '-' for the second argument. If you do not pass a third\n"
167 "\ta bd_info struct will be passed instead\n"
169 #if defined(CONFIG_FIT)
170 "\t\nFor the new multi component uImage format (FIT) addresses\n"
171 "\tmust be extended to include component or configuration unit name:\n"
172 "\taddr:<subimg_uname> - direct component image specification\n"
173 "\taddr#<conf_uname> - configuration specification\n"
174 "\tUse iminfo command to get the list of existing component\n"
175 "\timages and configurations.\n"
177 "\nSub-commands to do part of the bootm sequence. The sub-commands "
179 "issued in the order below (it's ok to not issue all sub-commands):\n"
180 "\tstart [addr [arg ...]]\n"
181 "\tloados - load OS image\n"
182 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
183 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
185 #if defined(CONFIG_OF_LIBFDT)
186 "\tfdt - relocate flat device tree\n"
188 "\tcmdline - OS specific command line processing/setup\n"
189 "\tbdt - OS specific bd_info processing\n"
190 "\tprep - OS specific prep before relocation or go\n"
191 #if defined(CONFIG_TRACE)
192 "\tfake - OS specific fake start without go\n"
198 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
199 "boot application image from memory", bootm_help_text
202 /*******************************************************************/
203 /* bootd - boot default image */
204 /*******************************************************************/
205 #if defined(CONFIG_CMD_BOOTD)
206 int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
208 return run_command(env_get("bootcmd"), flag);
212 boot, 1, 1, do_bootd,
213 "boot default, i.e., run 'bootcmd'",
217 /* keep old command name "bootd" for backward compatibility */
219 bootd, 1, 1, do_bootd,
220 "boot default, i.e., run 'bootcmd'",
227 /*******************************************************************/
228 /* iminfo - print header info for a requested image */
229 /*******************************************************************/
230 #if defined(CONFIG_CMD_IMI)
231 static int do_iminfo(struct cmd_tbl *cmdtp, int flag, int argc,
239 return image_info(image_load_addr);
242 for (arg = 1; arg < argc; ++arg) {
243 addr = simple_strtoul(argv[arg], NULL, 16);
244 if (image_info(addr) != 0)
250 static int image_info(ulong addr)
252 void *hdr = (void *)map_sysmem(addr, 0);
254 printf("\n## Checking Image at %08lx ...\n", addr);
256 switch (genimg_get_format(hdr)) {
257 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
258 case IMAGE_FORMAT_LEGACY:
259 puts(" Legacy image found\n");
260 if (!image_check_magic(hdr)) {
261 puts(" Bad Magic Number\n");
266 if (!image_check_hcrc(hdr)) {
267 puts(" Bad Header Checksum\n");
272 image_print_contents(hdr);
274 puts(" Verifying Checksum ... ");
275 if (!image_check_dcrc(hdr)) {
276 puts(" Bad Data CRC\n");
284 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
285 case IMAGE_FORMAT_ANDROID:
286 puts(" Android image found\n");
287 android_print_contents(hdr);
291 #if defined(CONFIG_FIT)
292 case IMAGE_FORMAT_FIT:
293 puts(" FIT image found\n");
295 if (fit_check_format(hdr, IMAGE_SIZE_INVAL)) {
296 puts("Bad FIT image format!\n");
301 fit_print_contents(hdr);
303 if (!fit_all_image_verify(hdr)) {
304 puts("Bad hash in FIT image!\n");
313 puts("Unknown image format!\n");
322 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
323 "print header information for application image",
325 " - print header information for application image starting at\n"
326 " address 'addr' in memory; this includes verification of the\n"
327 " image contents (magic number, header and payload checksums)"
332 /*******************************************************************/
333 /* imls - list all images found in flash */
334 /*******************************************************************/
335 #if defined(CONFIG_CMD_IMLS)
336 static int do_imls_nor(void)
342 for (i = 0, info = &flash_info[0];
343 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
345 if (info->flash_id == FLASH_UNKNOWN)
347 for (j = 0; j < info->sector_count; ++j) {
349 hdr = (void *)info->start[j];
353 switch (genimg_get_format(hdr)) {
354 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
355 case IMAGE_FORMAT_LEGACY:
356 if (!image_check_hcrc(hdr))
359 printf("Legacy Image at %08lX:\n", (ulong)hdr);
360 image_print_contents(hdr);
362 puts(" Verifying Checksum ... ");
363 if (!image_check_dcrc(hdr)) {
364 puts("Bad Data CRC\n");
370 #if defined(CONFIG_FIT)
371 case IMAGE_FORMAT_FIT:
372 if (fit_check_format(hdr, IMAGE_SIZE_INVAL))
375 printf("FIT Image at %08lX:\n", (ulong)hdr);
376 fit_print_contents(hdr);
391 #if defined(CONFIG_CMD_IMLS_NAND)
392 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
393 loff_t off, size_t len)
398 imgdata = malloc(len);
400 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
402 printf(" Low memory(cannot allocate memory for image)\n");
406 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
407 if (ret < 0 && ret != -EUCLEAN) {
412 if (!image_check_hcrc(imgdata)) {
417 printf("Legacy Image at NAND device %d offset %08llX:\n",
419 image_print_contents(imgdata);
421 puts(" Verifying Checksum ... ");
422 if (!image_check_dcrc(imgdata))
423 puts("Bad Data CRC\n");
432 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
438 imgdata = malloc(len);
440 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
442 printf(" Low memory(cannot allocate memory for image)\n");
446 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
447 if (ret < 0 && ret != -EUCLEAN) {
452 if (fit_check_format(imgdata, IMAGE_SIZE_INVAL)) {
457 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
459 fit_print_contents(imgdata);
465 static int do_imls_nand(void)
467 struct mtd_info *mtd;
468 int nand_dev = nand_curr_device;
473 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
474 puts("\nNo NAND devices available\n");
480 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
481 mtd = get_nand_dev_by_index(nand_dev);
482 if (!mtd->name || !mtd->size)
485 for (off = 0; off < mtd->size; off += mtd->erasesize) {
486 const image_header_t *header;
489 if (nand_block_isbad(mtd, off))
492 len = sizeof(buffer);
494 ret = nand_read(mtd, off, &len, (u8 *)buffer);
495 if (ret < 0 && ret != -EUCLEAN) {
496 printf("NAND read error %d at offset %08llX\n",
501 switch (genimg_get_format(buffer)) {
502 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
503 case IMAGE_FORMAT_LEGACY:
504 header = (const image_header_t *)buffer;
506 len = image_get_image_size(header);
507 nand_imls_legacyimage(mtd, nand_dev, off, len);
510 #if defined(CONFIG_FIT)
511 case IMAGE_FORMAT_FIT:
512 len = fit_get_size(buffer);
513 nand_imls_fitimage(mtd, nand_dev, off, len);
524 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
525 static int do_imls(struct cmd_tbl *cmdtp, int flag, int argc,
528 int ret_nor = 0, ret_nand = 0;
530 #if defined(CONFIG_CMD_IMLS)
531 ret_nor = do_imls_nor();
534 #if defined(CONFIG_CMD_IMLS_NAND)
535 ret_nand = do_imls_nand();
549 "list all images found in flash",
551 " - Prints information about all images found at sector/block\n"
552 " boundaries in nor/nand flash."