1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
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 hextoul(argv[0], &endp);
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 if (env_get_autostart()) {
145 local_args[0] = (char *)cmd;
146 local_args[1] = NULL;
147 printf("Automatic boot of image at addr 0x%08lX ...\n",
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 extended 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_info 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(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
206 return run_command(env_get("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(struct cmd_tbl *cmdtp, int flag, int argc,
237 return image_info(image_load_addr);
240 for (arg = 1; arg < argc; ++arg) {
241 addr = hextoul(argv[arg], NULL);
242 if (image_info(addr) != 0)
248 static int image_info(ulong addr)
250 void *hdr = (void *)map_sysmem(addr, 0);
252 printf("\n## Checking Image at %08lx ...\n", addr);
254 switch (genimg_get_format(hdr)) {
255 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
256 case IMAGE_FORMAT_LEGACY:
257 puts(" Legacy image found\n");
258 if (!image_check_magic(hdr)) {
259 puts(" Bad Magic Number\n");
264 if (!image_check_hcrc(hdr)) {
265 puts(" Bad Header Checksum\n");
270 image_print_contents(hdr);
272 puts(" Verifying Checksum ... ");
273 if (!image_check_dcrc(hdr)) {
274 puts(" Bad Data CRC\n");
282 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
283 case IMAGE_FORMAT_ANDROID:
284 puts(" Android image found\n");
285 android_print_contents(hdr);
289 #if defined(CONFIG_FIT)
290 case IMAGE_FORMAT_FIT:
291 puts(" FIT image found\n");
293 if (fit_check_format(hdr, IMAGE_SIZE_INVAL)) {
294 puts("Bad FIT image format!\n");
299 fit_print_contents(hdr);
301 if (!fit_all_image_verify(hdr)) {
302 puts("Bad hash in FIT image!\n");
311 puts("Unknown image format!\n");
320 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
321 "print header information for application image",
323 " - print header information for application image starting at\n"
324 " address 'addr' in memory; this includes verification of the\n"
325 " image contents (magic number, header and payload checksums)"
330 /*******************************************************************/
331 /* imls - list all images found in flash */
332 /*******************************************************************/
333 #if defined(CONFIG_CMD_IMLS)
334 static int do_imls_nor(void)
340 for (i = 0, info = &flash_info[0];
341 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
343 if (info->flash_id == FLASH_UNKNOWN)
345 for (j = 0; j < info->sector_count; ++j) {
347 hdr = (void *)info->start[j];
351 switch (genimg_get_format(hdr)) {
352 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
353 case IMAGE_FORMAT_LEGACY:
354 if (!image_check_hcrc(hdr))
357 printf("Legacy Image at %08lX:\n", (ulong)hdr);
358 image_print_contents(hdr);
360 puts(" Verifying Checksum ... ");
361 if (!image_check_dcrc(hdr)) {
362 puts("Bad Data CRC\n");
368 #if defined(CONFIG_FIT)
369 case IMAGE_FORMAT_FIT:
370 if (fit_check_format(hdr, IMAGE_SIZE_INVAL))
373 printf("FIT Image at %08lX:\n", (ulong)hdr);
374 fit_print_contents(hdr);
389 #if defined(CONFIG_CMD_IMLS_NAND)
390 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
391 loff_t off, size_t len)
396 imgdata = malloc(len);
398 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
400 printf(" Low memory(cannot allocate memory for image)\n");
404 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
405 if (ret < 0 && ret != -EUCLEAN) {
410 if (!image_check_hcrc(imgdata)) {
415 printf("Legacy Image at NAND device %d offset %08llX:\n",
417 image_print_contents(imgdata);
419 puts(" Verifying Checksum ... ");
420 if (!image_check_dcrc(imgdata))
421 puts("Bad Data CRC\n");
430 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
436 imgdata = malloc(len);
438 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
440 printf(" Low memory(cannot allocate memory for image)\n");
444 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
445 if (ret < 0 && ret != -EUCLEAN) {
450 if (fit_check_format(imgdata, IMAGE_SIZE_INVAL)) {
455 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
457 fit_print_contents(imgdata);
463 static int do_imls_nand(void)
465 struct mtd_info *mtd;
466 int nand_dev = nand_curr_device;
471 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
472 puts("\nNo NAND devices available\n");
478 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
479 mtd = get_nand_dev_by_index(nand_dev);
480 if (!mtd->name || !mtd->size)
483 for (off = 0; off < mtd->size; off += mtd->erasesize) {
484 const image_header_t *header;
487 if (nand_block_isbad(mtd, off))
490 len = sizeof(buffer);
492 ret = nand_read(mtd, off, &len, (u8 *)buffer);
493 if (ret < 0 && ret != -EUCLEAN) {
494 printf("NAND read error %d at offset %08llX\n",
499 switch (genimg_get_format(buffer)) {
500 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
501 case IMAGE_FORMAT_LEGACY:
502 header = (const image_header_t *)buffer;
504 len = image_get_image_size(header);
505 nand_imls_legacyimage(mtd, nand_dev, off, len);
508 #if defined(CONFIG_FIT)
509 case IMAGE_FORMAT_FIT:
510 len = fit_get_size(buffer);
511 nand_imls_fitimage(mtd, nand_dev, off, len);
522 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
523 static int do_imls(struct cmd_tbl *cmdtp, int flag, int argc,
526 int ret_nor = 0, ret_nand = 0;
528 #if defined(CONFIG_CMD_IMLS)
529 ret_nor = do_imls_nor();
532 #if defined(CONFIG_CMD_IMLS_NAND)
533 ret_nand = do_imls_nand();
547 "list all images found in flash",
549 " - Prints information about all images found at sector/block\n"
550 " boundaries in nor/nand flash."