2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #include <environment.h>
37 #include <asm/byteorder.h>
39 #ifdef CFG_HUSH_PARSER
43 DECLARE_GLOBAL_DATA_PTR;
45 extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
47 #define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
51 extern void bz_internal_error(int);
54 #if defined(CONFIG_CMD_IMI)
55 static int image_info (unsigned long addr);
58 #if defined(CONFIG_CMD_IMLS)
60 extern flash_info_t flash_info[]; /* info for FLASH chips */
61 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
64 #ifdef CONFIG_SILENT_CONSOLE
65 static void fixup_silent_linux (void);
68 static void *get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
69 bootm_headers_t *images, ulong *os_data, ulong *os_len);
70 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
73 * Continue booting an OS image; caller already has:
74 * - copied image header to global variable `header'
75 * - checked header magic number, checksums (both header & image),
76 * - verified image architecture (PPC) and type (KERNEL or MULTI),
77 * - loaded (first part of) image to header load address,
78 * - disabled interrupts.
80 typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
81 int argc, char *argv[],
82 bootm_headers_t *images); /* pointers to os/initrd/fdt */
84 extern boot_os_fn do_bootm_linux;
85 static boot_os_fn do_bootm_netbsd;
86 #if defined(CONFIG_LYNXKDI)
87 static boot_os_fn do_bootm_lynxkdi;
88 extern void lynxkdi_boot (image_header_t *);
90 static boot_os_fn do_bootm_rtems;
91 #if defined(CONFIG_CMD_ELF)
92 static boot_os_fn do_bootm_vxworks;
93 static boot_os_fn do_bootm_qnxelf;
94 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
95 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
97 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
98 extern uchar (*env_get_char)(int); /* Returns a character from the environment */
99 static boot_os_fn do_bootm_artos;
102 ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
103 static bootm_headers_t images; /* pointers to os/initrd/fdt images */
106 /*******************************************************************/
107 /* bootm - boot application image from image in memory */
108 /*******************************************************************/
109 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
112 const char *type_name;
113 uint unc_len = CFG_BOOTM_LEN;
114 uint8_t comp, type, os;
117 ulong os_data, os_len;
118 ulong image_start, image_end;
119 ulong load_start, load_end;
121 memset ((void *)&images, 0, sizeof (images));
122 images.verify = getenv_verify();
124 /* get kernel image header, start address and length */
125 os_hdr = get_kernel (cmdtp, flag, argc, argv,
126 &images, &os_data, &os_len);
130 show_boot_progress (6);
132 /* get image parameters */
133 switch (gen_image_get_format (os_hdr)) {
134 case IMAGE_FORMAT_LEGACY:
135 type = image_get_type (os_hdr);
136 comp = image_get_comp (os_hdr);
137 os = image_get_os (os_hdr);
139 image_end = image_get_image_end (os_hdr);
140 load_start = image_get_load (os_hdr);
142 #if defined(CONFIG_FIT)
143 case IMAGE_FORMAT_FIT:
144 fit_unsupported ("bootm");
148 puts ("ERROR: unknown image format type!\n");
152 image_start = (ulong)os_hdr;
154 type_name = image_get_type_name (type);
157 * We have reached the point of no return: we are going to
158 * overwrite all exception vector code, so we cannot easily
159 * recover from any failures any more...
161 iflag = disable_interrupts();
163 #ifdef CONFIG_AMIGAONEG3SE
165 * We've possible left the caches enabled during
166 * bios emulation, so turn them off again
169 invalidate_l1_instruction_cache();
176 if (load_start == (ulong)os_hdr) {
177 printf (" XIP %s ... ", type_name);
179 printf (" Loading %s ... ", type_name);
181 memmove_wd ((void *)load_start,
182 (void *)os_data, os_len, CHUNKSZ);
184 load_end = load_start + os_len;
189 printf (" Uncompressing %s ... ", type_name);
190 if (gunzip ((void *)load_start, unc_len,
191 (uchar *)os_data, &os_len) != 0) {
192 puts ("GUNZIP ERROR - must RESET board to recover\n");
193 show_boot_progress (-6);
194 do_reset (cmdtp, flag, argc, argv);
197 load_end = load_start + os_len;
201 printf (" Uncompressing %s ... ", type_name);
203 * If we've got less than 4 MB of malloc() space,
204 * use slower decompression algorithm which requires
205 * at most 2300 KB of memory.
207 int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
208 &unc_len, (char *)os_data, os_len,
209 CFG_MALLOC_LEN < (4096 * 1024), 0);
211 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
212 show_boot_progress (-6);
213 do_reset (cmdtp, flag, argc, argv);
216 load_end = load_start + unc_len;
218 #endif /* CONFIG_BZIP2 */
222 printf ("Unimplemented compression type %d\n", comp);
223 show_boot_progress (-7);
227 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
228 show_boot_progress (7);
230 if ((load_start < image_end) && (load_end > image_start)) {
231 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
232 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
234 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
235 do_reset (cmdtp, flag, argc, argv);
238 show_boot_progress (8);
241 default: /* handled by (original) Linux case */
243 #ifdef CONFIG_SILENT_CONSOLE
244 fixup_silent_linux();
246 do_bootm_linux (cmdtp, flag, argc, argv, &images);
250 do_bootm_netbsd (cmdtp, flag, argc, argv, &images);
253 #ifdef CONFIG_LYNXKDI
255 do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images);
260 do_bootm_rtems (cmdtp, flag, argc, argv, &images);
263 #if defined(CONFIG_CMD_ELF)
265 do_bootm_vxworks (cmdtp, flag, argc, argv, &images);
269 do_bootm_qnxelf (cmdtp, flag, argc, argv, &images);
275 do_bootm_artos (cmdtp, flag, argc, argv, &images);
280 show_boot_progress (-9);
282 puts ("\n## Control returned to monitor - resetting...\n");
283 do_reset (cmdtp, flag, argc, argv);
289 * get_kernel - find kernel image
290 * @os_data: pointer to a ulong variable, will hold os data start address
291 * @os_len: pointer to a ulong variable, will hold os data length
293 * get_kernel() tries to find a kernel image, verifies its integrity
294 * and locates kernel data.
297 * pointer to image header if valid image was found, plus kernel start
298 * address and length, otherwise NULL
300 static void *get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
301 bootm_headers_t *images, ulong *os_data, ulong *os_len)
305 #if defined(CONFIG_FIT)
307 const char *fit_uname_config = NULL;
308 const char *fit_uname_kernel = NULL;
311 /* find out kernel image address */
313 img_addr = load_addr;
314 debug ("* kernel: default image load address = 0x%08lx\n",
316 #if defined(CONFIG_FIT)
317 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
318 &fit_uname_config)) {
319 debug ("* kernel: config '%s' from image at 0x%08lx\n",
320 fit_uname_config, img_addr);
321 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
322 &fit_uname_kernel)) {
323 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
324 fit_uname_kernel, img_addr);
327 img_addr = simple_strtoul(argv[1], NULL, 16);
328 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
331 show_boot_progress (1);
332 printf ("## Booting kernel image at %08lx ...\n", img_addr);
334 /* copy from dataflash if needed */
335 img_addr = gen_get_image (img_addr);
337 /* check image type, for FIT images get FIT kernel node */
338 switch (gen_image_get_format ((void *)img_addr)) {
339 case IMAGE_FORMAT_LEGACY:
341 debug ("* kernel: legacy format image\n");
342 hdr = (image_header_t *)img_addr;
344 if (!image_check_magic(hdr)) {
345 puts ("Bad Magic Number\n");
346 show_boot_progress (-1);
349 show_boot_progress (2);
351 if (!image_check_hcrc (hdr)) {
352 puts ("Bad Header Checksum\n");
353 show_boot_progress (-2);
357 show_boot_progress (3);
358 image_print_contents (hdr);
360 if (images->verify) {
361 puts (" Verifying Checksum ... ");
362 if (!image_check_dcrc (hdr)) {
363 printf ("Bad Data CRC\n");
364 show_boot_progress (-3);
369 show_boot_progress (4);
371 if (!image_check_target_arch (hdr)) {
372 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
373 show_boot_progress (-4);
376 show_boot_progress (5);
378 switch (image_get_type (hdr)) {
380 *os_data = image_get_data (hdr);
381 *os_len = image_get_data_size (hdr);
384 image_multi_getimg (hdr, 0, os_data, os_len);
387 printf ("Wrong Image Type for %s command\n", cmdtp->name);
388 show_boot_progress (-5);
391 images->legacy_hdr_os = hdr;
392 images->legacy_hdr_valid = 1;
395 #if defined(CONFIG_FIT)
396 case IMAGE_FORMAT_FIT:
397 fit_hdr = (void *)img_addr;
398 debug ("* kernel: FIT format image\n");
399 fit_unsupported ("kernel");
403 printf ("Wrong Image Format for %s command\n", cmdtp->name);
407 debug (" kernel data at 0x%08lx, end = 0x%08lx\n",
408 *os_data, *os_data + *os_len);
410 return (void *)img_addr;
414 bootm, CFG_MAXARGS, 1, do_bootm,
415 "bootm - boot application image from memory\n",
416 "[addr [arg ...]]\n - boot application image stored in memory\n"
417 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
418 "\t'arg' can be the address of an initrd image\n"
419 #if defined(CONFIG_OF_LIBFDT)
420 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
421 "\ta third argument is required which is the address of the\n"
422 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
423 "\tuse a '-' for the second argument. If you do not pass a third\n"
424 "\ta bd_info struct will be passed instead\n"
428 /*******************************************************************/
429 /* bootd - boot default image */
430 /*******************************************************************/
431 #if defined(CONFIG_CMD_BOOTD)
432 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
436 #ifndef CFG_HUSH_PARSER
437 if (run_command (getenv ("bootcmd"), flag) < 0)
440 if (parse_string_outer (getenv ("bootcmd"),
441 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
448 boot, 1, 1, do_bootd,
449 "boot - boot default, i.e., run 'bootcmd'\n",
453 /* keep old command name "bootd" for backward compatibility */
455 bootd, 1, 1, do_bootd,
456 "bootd - boot default, i.e., run 'bootcmd'\n",
463 /*******************************************************************/
464 /* iminfo - print header info for a requested image */
465 /*******************************************************************/
466 #if defined(CONFIG_CMD_IMI)
467 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
474 return image_info (load_addr);
477 for (arg = 1; arg < argc; ++arg) {
478 addr = simple_strtoul (argv[arg], NULL, 16);
479 if (image_info (addr) != 0)
485 static int image_info (ulong addr)
487 void *hdr = (void *)addr;
489 printf ("\n## Checking Image at %08lx ...\n", addr);
491 switch (gen_image_get_format (hdr)) {
492 case IMAGE_FORMAT_LEGACY:
493 puts (" Legacy image found\n");
494 if (!image_check_magic (hdr)) {
495 puts (" Bad Magic Number\n");
499 if (!image_check_hcrc (hdr)) {
500 puts (" Bad Header Checksum\n");
504 image_print_contents (hdr);
506 puts (" Verifying Checksum ... ");
507 if (!image_check_dcrc (hdr)) {
508 puts (" Bad Data CRC\n");
513 #if defined(CONFIG_FIT)
514 case IMAGE_FORMAT_FIT:
515 puts (" FIT image found\n");
516 fit_unsupported ("iminfo");
520 puts ("Unknown image format!\n");
528 iminfo, CFG_MAXARGS, 1, do_iminfo,
529 "iminfo - print header information for application image\n",
531 " - print header information for application image starting at\n"
532 " address 'addr' in memory; this includes verification of the\n"
533 " image contents (magic number, header and payload checksums)\n"
538 /*******************************************************************/
539 /* imls - list all images found in flash */
540 /*******************************************************************/
541 #if defined(CONFIG_CMD_IMLS)
542 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
548 for (i = 0, info = &flash_info[0];
549 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
551 if (info->flash_id == FLASH_UNKNOWN)
553 for (j = 0; j < info->sector_count; ++j) {
555 hdr = (void *)info->start[j];
559 switch (gen_image_get_format (hdr)) {
560 case IMAGE_FORMAT_LEGACY:
561 if (!image_check_magic (hdr))
564 if (!image_check_hcrc (hdr))
567 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
568 image_print_contents (hdr);
570 puts (" Verifying Checksum ... ");
571 if (!image_check_dcrc (hdr)) {
572 puts ("Bad Data CRC\n");
577 #if defined(CONFIG_FIT)
578 case IMAGE_FORMAT_FIT:
579 printf ("FIT Image at %08lX:\n", (ulong)hdr);
580 fit_unsupported ("imls");
597 "imls - list all images found in flash\n",
599 " - Prints information about all images found at sector\n"
600 " boundaries in flash.\n"
604 /*******************************************************************/
605 /* helper routines */
606 /*******************************************************************/
607 #ifdef CONFIG_SILENT_CONSOLE
608 static void fixup_silent_linux ()
610 char buf[256], *start, *end;
611 char *cmdline = getenv ("bootargs");
613 /* Only fix cmdline when requested */
614 if (!(gd->flags & GD_FLG_SILENT))
617 debug ("before silent fix-up: %s\n", cmdline);
619 if ((start = strstr (cmdline, "console=")) != NULL) {
620 end = strchr (start, ' ');
621 strncpy (buf, cmdline, (start - cmdline + 8));
623 strcpy (buf + (start - cmdline + 8), end);
625 buf[start - cmdline + 8] = '\0';
627 strcpy (buf, cmdline);
628 strcat (buf, " console=");
631 strcpy (buf, "console=");
634 setenv ("bootargs", buf);
635 debug ("after silent fix-up: %s\n", buf);
637 #endif /* CONFIG_SILENT_CONSOLE */
640 /*******************************************************************/
641 /* OS booting routines */
642 /*******************************************************************/
644 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
645 int argc, char *argv[],
646 bootm_headers_t *images)
648 void (*loader)(bd_t *, image_header_t *, char *, char *);
649 image_header_t *os_hdr, *hdr;
650 ulong kernel_data, kernel_len;
654 #if defined(CONFIG_FIT)
655 if (!images->legacy_hdr_valid) {
656 fit_unsupported_reset ("NetBSD");
657 do_reset (cmdtp, flag, argc, argv);
660 hdr = images->legacy_hdr_os;
663 * Booting a (NetBSD) kernel image
665 * This process is pretty similar to a standalone application:
666 * The (first part of an multi-) image must be a stage-2 loader,
667 * which in turn is responsible for loading & invoking the actual
668 * kernel. The only differences are the parameters being passed:
669 * besides the board info strucure, the loader expects a command
670 * line, the name of the console device, and (optionally) the
671 * address of the original image header.
674 if (image_check_type (hdr, IH_TYPE_MULTI)) {
675 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
681 #if defined (CONFIG_8xx_CONS_SMC1)
683 #elif defined (CONFIG_8xx_CONS_SMC2)
685 #elif defined (CONFIG_8xx_CONS_SCC2)
687 #elif defined (CONFIG_8xx_CONS_SCC3)
695 for (i = 2, len = 0; i < argc; i += 1)
696 len += strlen (argv[i]) + 1;
697 cmdline = malloc (len);
699 for (i = 2, len = 0; i < argc; i += 1) {
701 cmdline[len++] = ' ';
702 strcpy (&cmdline[len], argv[i]);
703 len += strlen (argv[i]);
705 } else if ((cmdline = getenv ("bootargs")) == NULL) {
709 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
711 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
714 show_boot_progress (15);
717 * NetBSD Stage-2 Loader Parameters:
718 * r3: ptr to board info data
721 * r6: boot args string
723 (*loader) (gd->bd, os_hdr, consdev, cmdline);
726 #ifdef CONFIG_LYNXKDI
727 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
728 int argc, char *argv[],
729 bootm_headers_t *images)
731 image_header_t *hdr = images->legacy_hdr_os;
733 #if defined(CONFIG_FIT)
734 if (!images->legacy_hdr_valid) {
735 fit_unsupported_reset ("Lynx");
736 do_reset (cmdtp, flag, argc, argv);
740 lynxkdi_boot ((image_header_t *)hdr);
742 #endif /* CONFIG_LYNXKDI */
744 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
745 int argc, char *argv[],
746 bootm_headers_t *images)
748 image_header_t *hdr = images->legacy_hdr_os;
749 void (*entry_point)(bd_t *);
751 #if defined(CONFIG_FIT)
752 if (!images->legacy_hdr_valid) {
753 fit_unsupported_reset ("RTEMS");
754 do_reset (cmdtp, flag, argc, argv);
758 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
760 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
763 show_boot_progress (15);
767 * r3: ptr to board info data
769 (*entry_point)(gd->bd);
772 #if defined(CONFIG_CMD_ELF)
773 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
774 int argc, char *argv[],
775 bootm_headers_t *images)
778 image_header_t *hdr = images->legacy_hdr_os;
780 #if defined(CONFIG_FIT)
782 fit_unsupported_reset ("VxWorks");
783 do_reset (cmdtp, flag, argc, argv);
787 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
788 setenv("loadaddr", str);
789 do_bootvx(cmdtp, 0, 0, NULL);
792 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
793 int argc, char *argv[],
794 bootm_headers_t *images)
798 image_header_t *hdr = images->legacy_hdr_os;
800 #if defined(CONFIG_FIT)
801 if (!images->legacy_hdr_valid) {
802 fit_unsupported_reset ("QNX");
803 do_reset (cmdtp, flag, argc, argv);
807 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
808 local_args[0] = argv[0];
809 local_args[1] = str; /* and provide it via the arguments */
810 do_bootelf(cmdtp, 0, 2, local_args);
814 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
815 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
816 int argc, char *argv[],
817 bootm_headers_t *images)
822 int i, j, nxt, len, envno, envsz;
824 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
825 image_header_t *hdr = images->legacy_hdr_os;
827 #if defined(CONFIG_FIT)
828 if (!images->legacy_hdr_valid) {
829 fit_unsupported_reset ("ARTOS");
830 do_reset (cmdtp, flag, argc, argv);
835 * Booting an ARTOS kernel image + application
838 /* this used to be the top of memory, but was wrong... */
840 /* get stack pointer */
841 asm volatile ("mr %0,1" : "=r"(top) );
843 debug ("## Current stack ends at 0x%08lX ", top);
845 top -= 2048; /* just to be sure */
846 if (top > CFG_BOOTMAPSZ)
850 debug ("=> set upper limit to 0x%08lX\n", top);
852 /* first check the artos specific boot args, then the linux args*/
853 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
856 /* get length of cmdline, and place it */
858 top = (top - (len + 1)) & ~0xF;
859 cmdline = (char *)top;
860 debug ("## cmdline at 0x%08lX ", top);
864 top = (top - sizeof (bd_t)) & ~0xF;
865 debug ("## bd at 0x%08lX ", top);
867 memcpy (kbd, gd->bd, sizeof (bd_t));
869 /* first find number of env entries, and their size */
872 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
873 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
876 envsz += (nxt - i) + 1; /* plus trailing zero */
878 envno++; /* plus the terminating zero */
879 debug ("## %u envvars total size %u ", envno, envsz);
881 top = (top - sizeof (char **) * envno) & ~0xF;
882 fwenv = (char **)top;
883 debug ("## fwenv at 0x%08lX ", top);
885 top = (top - envsz) & ~0xF;
890 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
891 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
894 for (j = i; j < nxt; ++j)
895 *s++ = env_get_char (j);
898 *ss++ = NULL; /* terminate */
900 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
901 (*entry) (kbd, cmdline, fwenv, top);