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 image_header_t *image_get_kernel (ulong img_addr, int verify)
302 image_header_t *hdr = (image_header_t *)img_addr;
304 if (!image_check_magic(hdr)) {
305 puts ("Bad Magic Number\n");
306 show_boot_progress (-1);
309 show_boot_progress (2);
311 if (!image_check_hcrc (hdr)) {
312 puts ("Bad Header Checksum\n");
313 show_boot_progress (-2);
317 show_boot_progress (3);
318 image_print_contents (hdr);
321 puts (" Verifying Checksum ... ");
322 if (!image_check_dcrc (hdr)) {
323 printf ("Bad Data CRC\n");
324 show_boot_progress (-3);
329 show_boot_progress (4);
331 if (!image_check_target_arch (hdr)) {
332 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
333 show_boot_progress (-4);
340 * get_kernel - find kernel image
341 * @os_data: pointer to a ulong variable, will hold os data start address
342 * @os_len: pointer to a ulong variable, will hold os data length
344 * get_kernel() tries to find a kernel image, verifies its integrity
345 * and locates kernel data.
348 * pointer to image header if valid image was found, plus kernel start
349 * address and length, otherwise NULL
351 static void *get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
352 bootm_headers_t *images, ulong *os_data, ulong *os_len)
356 #if defined(CONFIG_FIT)
358 const char *fit_uname_config = NULL;
359 const char *fit_uname_kernel = NULL;
362 /* find out kernel image address */
364 img_addr = load_addr;
365 debug ("* kernel: default image load address = 0x%08lx\n",
367 #if defined(CONFIG_FIT)
368 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
369 &fit_uname_config)) {
370 debug ("* kernel: config '%s' from image at 0x%08lx\n",
371 fit_uname_config, img_addr);
372 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
373 &fit_uname_kernel)) {
374 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
375 fit_uname_kernel, img_addr);
378 img_addr = simple_strtoul(argv[1], NULL, 16);
379 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
382 show_boot_progress (1);
383 printf ("## Booting kernel image at %08lx ...\n", img_addr);
385 /* copy from dataflash if needed */
386 img_addr = gen_get_image (img_addr);
388 /* check image type, for FIT images get FIT kernel node */
389 switch (gen_image_get_format ((void *)img_addr)) {
390 case IMAGE_FORMAT_LEGACY:
392 debug ("* kernel: legacy format image\n");
393 hdr = image_get_kernel (img_addr, images->verify);
396 show_boot_progress (5);
398 switch (image_get_type (hdr)) {
400 *os_data = image_get_data (hdr);
401 *os_len = image_get_data_size (hdr);
404 image_multi_getimg (hdr, 0, os_data, os_len);
407 printf ("Wrong Image Type for %s command\n", cmdtp->name);
408 show_boot_progress (-5);
411 images->legacy_hdr_os = hdr;
412 images->legacy_hdr_valid = 1;
415 #if defined(CONFIG_FIT)
416 case IMAGE_FORMAT_FIT:
417 fit_hdr = (void *)img_addr;
418 debug ("* kernel: FIT format image\n");
419 fit_unsupported ("kernel");
423 printf ("Wrong Image Format for %s command\n", cmdtp->name);
427 debug (" kernel data at 0x%08lx, end = 0x%08lx\n",
428 *os_data, *os_data + *os_len);
430 return (void *)img_addr;
434 bootm, CFG_MAXARGS, 1, do_bootm,
435 "bootm - boot application image from memory\n",
436 "[addr [arg ...]]\n - boot application image stored in memory\n"
437 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
438 "\t'arg' can be the address of an initrd image\n"
439 #if defined(CONFIG_OF_LIBFDT)
440 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
441 "\ta third argument is required which is the address of the\n"
442 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
443 "\tuse a '-' for the second argument. If you do not pass a third\n"
444 "\ta bd_info struct will be passed instead\n"
448 /*******************************************************************/
449 /* bootd - boot default image */
450 /*******************************************************************/
451 #if defined(CONFIG_CMD_BOOTD)
452 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
456 #ifndef CFG_HUSH_PARSER
457 if (run_command (getenv ("bootcmd"), flag) < 0)
460 if (parse_string_outer (getenv ("bootcmd"),
461 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
468 boot, 1, 1, do_bootd,
469 "boot - boot default, i.e., run 'bootcmd'\n",
473 /* keep old command name "bootd" for backward compatibility */
475 bootd, 1, 1, do_bootd,
476 "bootd - boot default, i.e., run 'bootcmd'\n",
483 /*******************************************************************/
484 /* iminfo - print header info for a requested image */
485 /*******************************************************************/
486 #if defined(CONFIG_CMD_IMI)
487 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
494 return image_info (load_addr);
497 for (arg = 1; arg < argc; ++arg) {
498 addr = simple_strtoul (argv[arg], NULL, 16);
499 if (image_info (addr) != 0)
505 static int image_info (ulong addr)
507 void *hdr = (void *)addr;
509 printf ("\n## Checking Image at %08lx ...\n", addr);
511 switch (gen_image_get_format (hdr)) {
512 case IMAGE_FORMAT_LEGACY:
513 puts (" Legacy image found\n");
514 if (!image_check_magic (hdr)) {
515 puts (" Bad Magic Number\n");
519 if (!image_check_hcrc (hdr)) {
520 puts (" Bad Header Checksum\n");
524 image_print_contents (hdr);
526 puts (" Verifying Checksum ... ");
527 if (!image_check_dcrc (hdr)) {
528 puts (" Bad Data CRC\n");
533 #if defined(CONFIG_FIT)
534 case IMAGE_FORMAT_FIT:
535 puts (" FIT image found\n");
536 fit_unsupported ("iminfo");
540 puts ("Unknown image format!\n");
548 iminfo, CFG_MAXARGS, 1, do_iminfo,
549 "iminfo - print header information for application image\n",
551 " - print header information for application image starting at\n"
552 " address 'addr' in memory; this includes verification of the\n"
553 " image contents (magic number, header and payload checksums)\n"
558 /*******************************************************************/
559 /* imls - list all images found in flash */
560 /*******************************************************************/
561 #if defined(CONFIG_CMD_IMLS)
562 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
568 for (i = 0, info = &flash_info[0];
569 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
571 if (info->flash_id == FLASH_UNKNOWN)
573 for (j = 0; j < info->sector_count; ++j) {
575 hdr = (void *)info->start[j];
579 switch (gen_image_get_format (hdr)) {
580 case IMAGE_FORMAT_LEGACY:
581 if (!image_check_magic (hdr))
584 if (!image_check_hcrc (hdr))
587 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
588 image_print_contents (hdr);
590 puts (" Verifying Checksum ... ");
591 if (!image_check_dcrc (hdr)) {
592 puts ("Bad Data CRC\n");
597 #if defined(CONFIG_FIT)
598 case IMAGE_FORMAT_FIT:
599 printf ("FIT Image at %08lX:\n", (ulong)hdr);
600 fit_unsupported ("imls");
617 "imls - list all images found in flash\n",
619 " - Prints information about all images found at sector\n"
620 " boundaries in flash.\n"
624 /*******************************************************************/
625 /* helper routines */
626 /*******************************************************************/
627 #ifdef CONFIG_SILENT_CONSOLE
628 static void fixup_silent_linux ()
630 char buf[256], *start, *end;
631 char *cmdline = getenv ("bootargs");
633 /* Only fix cmdline when requested */
634 if (!(gd->flags & GD_FLG_SILENT))
637 debug ("before silent fix-up: %s\n", cmdline);
639 if ((start = strstr (cmdline, "console=")) != NULL) {
640 end = strchr (start, ' ');
641 strncpy (buf, cmdline, (start - cmdline + 8));
643 strcpy (buf + (start - cmdline + 8), end);
645 buf[start - cmdline + 8] = '\0';
647 strcpy (buf, cmdline);
648 strcat (buf, " console=");
651 strcpy (buf, "console=");
654 setenv ("bootargs", buf);
655 debug ("after silent fix-up: %s\n", buf);
657 #endif /* CONFIG_SILENT_CONSOLE */
660 /*******************************************************************/
661 /* OS booting routines */
662 /*******************************************************************/
664 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
665 int argc, char *argv[],
666 bootm_headers_t *images)
668 void (*loader)(bd_t *, image_header_t *, char *, char *);
669 image_header_t *os_hdr, *hdr;
670 ulong kernel_data, kernel_len;
674 #if defined(CONFIG_FIT)
675 if (!images->legacy_hdr_valid) {
676 fit_unsupported_reset ("NetBSD");
677 do_reset (cmdtp, flag, argc, argv);
680 hdr = images->legacy_hdr_os;
683 * Booting a (NetBSD) kernel image
685 * This process is pretty similar to a standalone application:
686 * The (first part of an multi-) image must be a stage-2 loader,
687 * which in turn is responsible for loading & invoking the actual
688 * kernel. The only differences are the parameters being passed:
689 * besides the board info strucure, the loader expects a command
690 * line, the name of the console device, and (optionally) the
691 * address of the original image header.
694 if (image_check_type (hdr, IH_TYPE_MULTI)) {
695 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
701 #if defined (CONFIG_8xx_CONS_SMC1)
703 #elif defined (CONFIG_8xx_CONS_SMC2)
705 #elif defined (CONFIG_8xx_CONS_SCC2)
707 #elif defined (CONFIG_8xx_CONS_SCC3)
715 for (i = 2, len = 0; i < argc; i += 1)
716 len += strlen (argv[i]) + 1;
717 cmdline = malloc (len);
719 for (i = 2, len = 0; i < argc; i += 1) {
721 cmdline[len++] = ' ';
722 strcpy (&cmdline[len], argv[i]);
723 len += strlen (argv[i]);
725 } else if ((cmdline = getenv ("bootargs")) == NULL) {
729 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
731 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
734 show_boot_progress (15);
737 * NetBSD Stage-2 Loader Parameters:
738 * r3: ptr to board info data
741 * r6: boot args string
743 (*loader) (gd->bd, os_hdr, consdev, cmdline);
746 #ifdef CONFIG_LYNXKDI
747 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
748 int argc, char *argv[],
749 bootm_headers_t *images)
751 image_header_t *hdr = images->legacy_hdr_os;
753 #if defined(CONFIG_FIT)
754 if (!images->legacy_hdr_valid) {
755 fit_unsupported_reset ("Lynx");
756 do_reset (cmdtp, flag, argc, argv);
760 lynxkdi_boot ((image_header_t *)hdr);
762 #endif /* CONFIG_LYNXKDI */
764 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
765 int argc, char *argv[],
766 bootm_headers_t *images)
768 image_header_t *hdr = images->legacy_hdr_os;
769 void (*entry_point)(bd_t *);
771 #if defined(CONFIG_FIT)
772 if (!images->legacy_hdr_valid) {
773 fit_unsupported_reset ("RTEMS");
774 do_reset (cmdtp, flag, argc, argv);
778 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
780 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
783 show_boot_progress (15);
787 * r3: ptr to board info data
789 (*entry_point)(gd->bd);
792 #if defined(CONFIG_CMD_ELF)
793 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
794 int argc, char *argv[],
795 bootm_headers_t *images)
798 image_header_t *hdr = images->legacy_hdr_os;
800 #if defined(CONFIG_FIT)
802 fit_unsupported_reset ("VxWorks");
803 do_reset (cmdtp, flag, argc, argv);
807 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
808 setenv("loadaddr", str);
809 do_bootvx(cmdtp, 0, 0, NULL);
812 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
813 int argc, char *argv[],
814 bootm_headers_t *images)
818 image_header_t *hdr = images->legacy_hdr_os;
820 #if defined(CONFIG_FIT)
821 if (!images->legacy_hdr_valid) {
822 fit_unsupported_reset ("QNX");
823 do_reset (cmdtp, flag, argc, argv);
827 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
828 local_args[0] = argv[0];
829 local_args[1] = str; /* and provide it via the arguments */
830 do_bootelf(cmdtp, 0, 2, local_args);
834 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
835 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
836 int argc, char *argv[],
837 bootm_headers_t *images)
842 int i, j, nxt, len, envno, envsz;
844 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
845 image_header_t *hdr = images->legacy_hdr_os;
847 #if defined(CONFIG_FIT)
848 if (!images->legacy_hdr_valid) {
849 fit_unsupported_reset ("ARTOS");
850 do_reset (cmdtp, flag, argc, argv);
855 * Booting an ARTOS kernel image + application
858 /* this used to be the top of memory, but was wrong... */
860 /* get stack pointer */
861 asm volatile ("mr %0,1" : "=r"(top) );
863 debug ("## Current stack ends at 0x%08lX ", top);
865 top -= 2048; /* just to be sure */
866 if (top > CFG_BOOTMAPSZ)
870 debug ("=> set upper limit to 0x%08lX\n", top);
872 /* first check the artos specific boot args, then the linux args*/
873 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
876 /* get length of cmdline, and place it */
878 top = (top - (len + 1)) & ~0xF;
879 cmdline = (char *)top;
880 debug ("## cmdline at 0x%08lX ", top);
884 top = (top - sizeof (bd_t)) & ~0xF;
885 debug ("## bd at 0x%08lX ", top);
887 memcpy (kbd, gd->bd, sizeof (bd_t));
889 /* first find number of env entries, and their size */
892 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
893 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
896 envsz += (nxt - i) + 1; /* plus trailing zero */
898 envno++; /* plus the terminating zero */
899 debug ("## %u envvars total size %u ", envno, envsz);
901 top = (top - sizeof (char **) * envno) & ~0xF;
902 fwenv = (char **)top;
903 debug ("## fwenv at 0x%08lX ", top);
905 top = (top - envsz) & ~0xF;
910 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
911 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
914 for (j = i; j < nxt; ++j)
915 *s++ = env_get_char (j);
918 *ss++ = NULL; /* terminate */
920 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
921 (*entry) (kbd, cmdline, fwenv, top);