1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13 #include <environment.h>
18 #include <asm/byteorder.h>
19 #include <linux/ctype.h>
20 #include <linux/err.h>
21 #include <u-boot/zlib.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #if defined(CONFIG_CMD_IMI)
26 static int image_info(unsigned long addr);
29 #if defined(CONFIG_CMD_IMLS)
31 #include <mtd/cfi_flash.h>
32 extern flash_info_t flash_info[]; /* info for FLASH chips */
35 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
36 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
39 /* we overload the cmd field with our state machine info instead of a
41 static cmd_tbl_t cmd_bootm_sub[] = {
42 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
43 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
44 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
45 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
47 #ifdef CONFIG_OF_LIBFDT
48 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
50 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
51 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
52 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
53 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
54 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
57 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
64 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
69 if (state == BOOTM_STATE_START)
70 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
72 /* Unrecognized command */
76 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
77 images.state >= state) {
78 printf("Trying to execute a command out of order\n");
82 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
87 /*******************************************************************/
88 /* bootm - boot application image from image in memory */
89 /*******************************************************************/
91 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
93 #ifdef CONFIG_NEEDS_MANUAL_RELOC
94 static int relocated = 0;
99 /* relocate names of sub-command table */
100 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
101 cmd_bootm_sub[i].name += gd->reloc_off;
107 /* determine if we have a sub command */
112 simple_strtoul(argv[0], &endp, 16);
113 /* endp pointing to NULL means that argv[0] was just a
114 * valid number, pass it along to the normal bootm processing
116 * If endp is ':' or '#' assume a FIT identifier so pass
117 * along for normal processing.
119 * Right now we assume the first arg should never be '-'
121 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
122 return do_bootm_subcommand(cmdtp, flag, argc, argv);
125 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
126 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
128 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
129 BOOTM_STATE_RAMDISK |
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 = env_get("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 extended 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"
187 #if defined(CONFIG_TRACE)
188 "\tfake - OS specific fake start without go\n"
194 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
195 "boot application image from memory", bootm_help_text
198 /*******************************************************************/
199 /* bootd - boot default image */
200 /*******************************************************************/
201 #if defined(CONFIG_CMD_BOOTD)
202 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
204 return run_command(env_get("bootcmd"), flag);
208 boot, 1, 1, do_bootd,
209 "boot default, i.e., run 'bootcmd'",
213 /* keep old command name "bootd" for backward compatibility */
215 bootd, 1, 1, do_bootd,
216 "boot default, i.e., run 'bootcmd'",
223 /*******************************************************************/
224 /* iminfo - print header info for a requested image */
225 /*******************************************************************/
226 #if defined(CONFIG_CMD_IMI)
227 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
234 return image_info(load_addr);
237 for (arg = 1; arg < argc; ++arg) {
238 addr = simple_strtoul(argv[arg], NULL, 16);
239 if (image_info(addr) != 0)
245 static int image_info(ulong addr)
247 void *hdr = (void *)addr;
249 printf("\n## Checking Image at %08lx ...\n", addr);
251 switch (genimg_get_format(hdr)) {
252 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
253 case IMAGE_FORMAT_LEGACY:
254 puts(" Legacy image found\n");
255 if (!image_check_magic(hdr)) {
256 puts(" Bad Magic Number\n");
260 if (!image_check_hcrc(hdr)) {
261 puts(" Bad Header Checksum\n");
265 image_print_contents(hdr);
267 puts(" Verifying Checksum ... ");
268 if (!image_check_dcrc(hdr)) {
269 puts(" Bad Data CRC\n");
275 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
276 case IMAGE_FORMAT_ANDROID:
277 puts(" Android image found\n");
278 android_print_contents(hdr);
281 #if defined(CONFIG_FIT)
282 case IMAGE_FORMAT_FIT:
283 puts(" FIT image found\n");
285 if (!fit_check_format(hdr)) {
286 puts("Bad FIT image format!\n");
290 fit_print_contents(hdr);
292 if (!fit_all_image_verify(hdr)) {
293 puts("Bad hash in FIT image!\n");
300 puts("Unknown image format!\n");
308 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
309 "print header information for application image",
311 " - print header information for application image starting at\n"
312 " address 'addr' in memory; this includes verification of the\n"
313 " image contents (magic number, header and payload checksums)"
318 /*******************************************************************/
319 /* imls - list all images found in flash */
320 /*******************************************************************/
321 #if defined(CONFIG_CMD_IMLS)
322 static int do_imls_nor(void)
328 for (i = 0, info = &flash_info[0];
329 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
331 if (info->flash_id == FLASH_UNKNOWN)
333 for (j = 0; j < info->sector_count; ++j) {
335 hdr = (void *)info->start[j];
339 switch (genimg_get_format(hdr)) {
340 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
341 case IMAGE_FORMAT_LEGACY:
342 if (!image_check_hcrc(hdr))
345 printf("Legacy Image at %08lX:\n", (ulong)hdr);
346 image_print_contents(hdr);
348 puts(" Verifying Checksum ... ");
349 if (!image_check_dcrc(hdr)) {
350 puts("Bad Data CRC\n");
356 #if defined(CONFIG_FIT)
357 case IMAGE_FORMAT_FIT:
358 if (!fit_check_format(hdr))
361 printf("FIT Image at %08lX:\n", (ulong)hdr);
362 fit_print_contents(hdr);
377 #if defined(CONFIG_CMD_IMLS_NAND)
378 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
379 loff_t off, size_t len)
384 imgdata = malloc(len);
386 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
388 printf(" Low memory(cannot allocate memory for image)\n");
392 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
393 if (ret < 0 && ret != -EUCLEAN) {
398 if (!image_check_hcrc(imgdata)) {
403 printf("Legacy Image at NAND device %d offset %08llX:\n",
405 image_print_contents(imgdata);
407 puts(" Verifying Checksum ... ");
408 if (!image_check_dcrc(imgdata))
409 puts("Bad Data CRC\n");
418 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
424 imgdata = malloc(len);
426 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
428 printf(" Low memory(cannot allocate memory for image)\n");
432 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
433 if (ret < 0 && ret != -EUCLEAN) {
438 if (!fit_check_format(imgdata)) {
443 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
445 fit_print_contents(imgdata);
451 static int do_imls_nand(void)
453 struct mtd_info *mtd;
454 int nand_dev = nand_curr_device;
459 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
460 puts("\nNo NAND devices available\n");
466 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
467 mtd = get_nand_dev_by_index(nand_dev);
468 if (!mtd->name || !mtd->size)
471 for (off = 0; off < mtd->size; off += mtd->erasesize) {
472 const image_header_t *header;
475 if (nand_block_isbad(mtd, off))
478 len = sizeof(buffer);
480 ret = nand_read(mtd, off, &len, (u8 *)buffer);
481 if (ret < 0 && ret != -EUCLEAN) {
482 printf("NAND read error %d at offset %08llX\n",
487 switch (genimg_get_format(buffer)) {
488 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
489 case IMAGE_FORMAT_LEGACY:
490 header = (const image_header_t *)buffer;
492 len = image_get_image_size(header);
493 nand_imls_legacyimage(mtd, nand_dev, off, len);
496 #if defined(CONFIG_FIT)
497 case IMAGE_FORMAT_FIT:
498 len = fit_get_size(buffer);
499 nand_imls_fitimage(mtd, nand_dev, off, len);
510 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
511 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
513 int ret_nor = 0, ret_nand = 0;
515 #if defined(CONFIG_CMD_IMLS)
516 ret_nor = do_imls_nor();
519 #if defined(CONFIG_CMD_IMLS_NAND)
520 ret_nand = do_imls_nand();
534 "list all images found in flash",
536 " - Prints information about all images found at sector/block\n"
537 " boundaries in nor/nand flash."