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>
36 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
37 static int do_imls(struct cmd_tbl *cmdtp, int flag, int argc,
41 /* we overload the cmd field with our state machine info instead of a
43 static struct cmd_tbl cmd_bootm_sub[] = {
44 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
45 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
46 #ifdef CONFIG_CMD_BOOTM_PRE_LOAD
47 U_BOOT_CMD_MKENT(preload, 0, 1, (void *)BOOTM_STATE_PRE_LOAD, "", ""),
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 #if defined(CONFIG_CMD_BOOTM_PRE_LOAD)
63 static ulong bootm_get_addr(int argc, char *const argv[])
68 addr = hextoul(argv[0], NULL);
70 addr = image_load_addr;
76 static int do_bootm_subcommand(struct cmd_tbl *cmdtp, int flag, int argc,
79 struct bootm_info bmi;
84 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
89 if (state == BOOTM_STATE_START)
90 state |= BOOTM_STATE_PRE_LOAD | BOOTM_STATE_FINDOS |
91 BOOTM_STATE_FINDOTHER;
92 #if defined(CONFIG_CMD_BOOTM_PRE_LOAD)
93 if (state == BOOTM_STATE_PRE_LOAD)
94 state |= BOOTM_STATE_START;
97 /* Unrecognized command */
101 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
102 images.state >= state) {
103 printf("Trying to execute a command out of order\n");
104 return CMD_RET_USAGE;
109 bmi.addr_img = argv[0];
111 bmi.conf_ramdisk = argv[1];
113 bmi.conf_fdt = argv[2];
114 bmi.cmd_name = "bootm";
115 bmi.boot_progress = false;
117 /* set up argc and argv[] since some OSes use them */
121 ret = bootm_run_states(&bmi, state);
123 #if defined(CONFIG_CMD_BOOTM_PRE_LOAD)
124 if (!ret && (state & BOOTM_STATE_PRE_LOAD))
125 env_set_hex("loadaddr_verified",
126 bootm_get_addr(argc, argv) + image_load_offset);
129 return ret ? CMD_RET_FAILURE : 0;
132 /*******************************************************************/
133 /* bootm - boot application image from image in memory */
134 /*******************************************************************/
136 int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
138 struct bootm_info bmi;
141 /* determine if we have a sub command */
146 hextoul(argv[0], &endp);
147 /* endp pointing to NULL means that argv[0] was just a
148 * valid number, pass it along to the normal bootm processing
150 * If endp is ':' or '#' assume a FIT identifier so pass
151 * along for normal processing.
153 * Right now we assume the first arg should never be '-'
155 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
156 return do_bootm_subcommand(cmdtp, flag, argc, argv);
161 bmi.addr_img = argv[0];
163 bmi.conf_ramdisk = argv[1];
165 bmi.conf_fdt = argv[2];
167 /* set up argc and argv[] since some OSes use them */
171 ret = bootm_run(&bmi);
173 return ret ? CMD_RET_FAILURE : 0;
176 int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd)
178 if (env_get_autostart()) {
180 local_args[0] = (char *)cmd;
181 local_args[1] = NULL;
182 printf("Automatic boot of image at addr 0x%08lX ...\n",
184 return do_bootm(cmdtp, 0, 1, local_args);
190 U_BOOT_LONGHELP(bootm,
191 "[addr [arg ...]]\n - boot application image stored in memory\n"
192 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
193 "\t'arg' can be the address of an initrd image\n"
194 #if defined(CONFIG_OF_LIBFDT)
195 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
196 "\ta third argument is required which is the address of the\n"
197 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
198 "\tuse a '-' for the second argument. If you do not pass a third\n"
199 "\ta bd_info struct will be passed instead\n"
201 #if defined(CONFIG_FIT)
202 "\t\nFor the new multi component uImage format (FIT) addresses\n"
203 "\tmust be extended to include component or configuration unit name:\n"
204 "\taddr:<subimg_uname> - direct component image specification\n"
205 "\taddr#<conf_uname> - configuration specification\n"
206 "\tUse iminfo command to get the list of existing component\n"
207 "\timages and configurations.\n"
209 "\nSub-commands to do part of the bootm sequence. The sub-commands "
211 "issued in the order below (it's ok to not issue all sub-commands):\n"
212 "\tstart [addr [arg ...]]\n"
213 #if defined(CONFIG_CMD_BOOTM_PRE_LOAD)
214 "\tpreload [addr [arg ..]] - run only the preload stage\n"
216 "\tloados - load OS image\n"
217 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
218 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
220 #if defined(CONFIG_OF_LIBFDT)
221 "\tfdt - relocate flat device tree\n"
223 "\tcmdline - OS specific command line processing/setup\n"
224 "\tbdt - OS specific bd_info processing\n"
225 "\tprep - OS specific prep before relocation or go\n"
226 #if defined(CONFIG_TRACE)
227 "\tfake - OS specific fake start without go\n"
232 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
233 "boot application image from memory", bootm_help_text
236 /*******************************************************************/
237 /* bootd - boot default image */
238 /*******************************************************************/
239 #if defined(CONFIG_CMD_BOOTD)
240 int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
242 return run_command(env_get("bootcmd"), flag);
246 boot, 1, 1, do_bootd,
247 "boot default, i.e., run 'bootcmd'",
251 /* keep old command name "bootd" for backward compatibility */
253 bootd, 1, 1, do_bootd,
254 "boot default, i.e., run 'bootcmd'",
261 /*******************************************************************/
262 /* iminfo - print header info for a requested image */
263 /*******************************************************************/
264 #if defined(CONFIG_CMD_IMI)
265 static int do_iminfo(struct cmd_tbl *cmdtp, int flag, int argc,
273 return image_info(image_load_addr);
276 for (arg = 1; arg < argc; ++arg) {
277 addr = hextoul(argv[arg], NULL);
278 if (image_info(addr) != 0)
284 static int image_info(ulong addr)
286 void *hdr = (void *)map_sysmem(addr, 0);
288 printf("\n## Checking Image at %08lx ...\n", addr);
290 switch (genimg_get_format(hdr)) {
291 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
292 case IMAGE_FORMAT_LEGACY:
293 puts(" Legacy image found\n");
294 if (!image_check_magic(hdr)) {
295 puts(" Bad Magic Number\n");
300 if (!image_check_hcrc(hdr)) {
301 puts(" Bad Header Checksum\n");
306 image_print_contents(hdr);
308 puts(" Verifying Checksum ... ");
309 if (!image_check_dcrc(hdr)) {
310 puts(" Bad Data CRC\n");
318 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
319 case IMAGE_FORMAT_ANDROID:
320 puts(" Android image found\n");
321 android_print_contents(hdr);
325 #if defined(CONFIG_FIT)
326 case IMAGE_FORMAT_FIT:
327 puts(" FIT image found\n");
329 if (fit_check_format(hdr, IMAGE_SIZE_INVAL)) {
330 puts("Bad FIT image format!\n");
335 fit_print_contents(hdr);
337 if (!fit_all_image_verify(hdr)) {
338 puts("Bad hash in FIT image!\n");
347 puts("Unknown image format!\n");
356 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
357 "print header information for application image",
359 " - print header information for application image starting at\n"
360 " address 'addr' in memory; this includes verification of the\n"
361 " image contents (magic number, header and payload checksums)"
366 /*******************************************************************/
367 /* imls - list all images found in flash */
368 /*******************************************************************/
369 #if defined(CONFIG_CMD_IMLS)
370 static int do_imls_nor(void)
376 for (i = 0, info = &flash_info[0];
377 i < CFI_FLASH_BANKS; ++i, ++info) {
379 if (info->flash_id == FLASH_UNKNOWN)
381 for (j = 0; j < info->sector_count; ++j) {
383 hdr = (void *)info->start[j];
387 switch (genimg_get_format(hdr)) {
388 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
389 case IMAGE_FORMAT_LEGACY:
390 if (!image_check_hcrc(hdr))
393 printf("Legacy Image at %08lX:\n", (ulong)hdr);
394 image_print_contents(hdr);
396 puts(" Verifying Checksum ... ");
397 if (!image_check_dcrc(hdr)) {
398 puts("Bad Data CRC\n");
404 #if defined(CONFIG_FIT)
405 case IMAGE_FORMAT_FIT:
406 if (fit_check_format(hdr, IMAGE_SIZE_INVAL))
409 printf("FIT Image at %08lX:\n", (ulong)hdr);
410 fit_print_contents(hdr);
425 #if defined(CONFIG_CMD_IMLS_NAND)
426 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
427 loff_t off, size_t len)
432 imgdata = malloc(len);
434 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
436 printf(" Low memory(cannot allocate memory for image)\n");
440 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
441 if (ret < 0 && ret != -EUCLEAN) {
446 if (!image_check_hcrc(imgdata)) {
451 printf("Legacy Image at NAND device %d offset %08llX:\n",
453 image_print_contents(imgdata);
455 puts(" Verifying Checksum ... ");
456 if (!image_check_dcrc(imgdata))
457 puts("Bad Data CRC\n");
466 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
472 imgdata = malloc(len);
474 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
476 printf(" Low memory(cannot allocate memory for image)\n");
480 ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
481 if (ret < 0 && ret != -EUCLEAN) {
486 if (fit_check_format(imgdata, IMAGE_SIZE_INVAL)) {
491 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
493 fit_print_contents(imgdata);
499 static int do_imls_nand(void)
501 struct mtd_info *mtd;
502 int nand_dev = nand_curr_device;
507 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
508 puts("\nNo NAND devices available\n");
514 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
515 mtd = get_nand_dev_by_index(nand_dev);
516 if (!mtd->name || !mtd->size)
519 for (off = 0; off < mtd->size; off += mtd->erasesize) {
520 const struct legacy_img_hdr *header;
523 if (nand_block_isbad(mtd, off))
526 len = sizeof(buffer);
528 ret = nand_read(mtd, off, &len, (u8 *)buffer);
529 if (ret < 0 && ret != -EUCLEAN) {
530 printf("NAND read error %d at offset %08llX\n",
535 switch (genimg_get_format(buffer)) {
536 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
537 case IMAGE_FORMAT_LEGACY:
538 header = (const struct legacy_img_hdr *)buffer;
540 len = image_get_image_size(header);
541 nand_imls_legacyimage(mtd, nand_dev, off, len);
544 #if defined(CONFIG_FIT)
545 case IMAGE_FORMAT_FIT:
546 len = fit_get_size(buffer);
547 nand_imls_fitimage(mtd, nand_dev, off, len);
558 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
559 static int do_imls(struct cmd_tbl *cmdtp, int flag, int argc,
562 int ret_nor = 0, ret_nand = 0;
564 #if defined(CONFIG_CMD_IMLS)
565 ret_nor = do_imls_nor();
568 #if defined(CONFIG_CMD_IMLS_NAND)
569 ret_nand = do_imls_nand();
583 "list all images found in flash",
585 " - Prints information about all images found at sector/block\n"
586 " boundaries in nor/nand flash."