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 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43 #ifdef CFG_HUSH_PARSER
47 DECLARE_GLOBAL_DATA_PTR;
49 extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
51 #define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
55 extern void bz_internal_error(int);
58 #if defined(CONFIG_CMD_IMI)
59 static int image_info (unsigned long addr);
62 #if defined(CONFIG_CMD_IMLS)
64 extern flash_info_t flash_info[]; /* info for FLASH chips */
65 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
68 #ifdef CONFIG_SILENT_CONSOLE
69 static void fixup_silent_linux (void);
72 static void print_type (image_header_t *hdr);
73 static image_header_t *get_kernel (cmd_tbl_t *cmdtp, int flag,
74 int argc, char *argv[], int verify,
75 ulong *os_data, ulong *os_len);
76 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
79 * Continue booting an OS image; caller already has:
80 * - copied image header to global variable `header'
81 * - checked header magic number, checksums (both header & image),
82 * - verified image architecture (PPC) and type (KERNEL or MULTI),
83 * - loaded (first part of) image to header load address,
84 * - disabled interrupts.
86 typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
87 int argc, char *argv[],
88 image_header_t *hdr, /* of image to boot */
89 int verify); /* getenv("verify")[0] != 'n' */
91 extern boot_os_fn do_bootm_linux;
92 static boot_os_fn do_bootm_netbsd;
93 #if defined(CONFIG_LYNXKDI)
94 static boot_os_fn do_bootm_lynxkdi;
95 extern void lynxkdi_boot (image_header_t *);
97 static boot_os_fn do_bootm_rtems;
98 #if defined(CONFIG_CMD_ELF)
99 static boot_os_fn do_bootm_vxworks;
100 static boot_os_fn do_bootm_qnxelf;
101 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
102 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
104 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
105 extern uchar (*env_get_char)(int); /* Returns a character from the environment */
106 static boot_os_fn do_bootm_artos;
109 ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
112 /*******************************************************************/
113 /* bootm - boot application image from image in memory */
114 /*******************************************************************/
115 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
118 const char *type_name;
119 uint unc_len = CFG_BOOTM_LEN;
120 int verify = getenv_verify();
123 ulong os_data, os_len;
125 ulong image_start, image_end;
126 ulong load_start, load_end;
128 /* get kernel image header, start address and length */
129 hdr = get_kernel (cmdtp, flag, argc, argv, verify,
134 show_boot_progress (6);
137 * We have reached the point of no return: we are going to
138 * overwrite all exception vector code, so we cannot easily
139 * recover from any failures any more...
141 iflag = disable_interrupts();
143 #ifdef CONFIG_AMIGAONEG3SE
145 * We've possible left the caches enabled during
146 * bios emulation, so turn them off again
149 invalidate_l1_instruction_cache();
154 type_name = image_get_type_name (image_get_type (hdr));
156 image_start = (ulong)hdr;
157 image_end = image_get_image_end (hdr);
158 load_start = image_get_load (hdr);
161 switch (image_get_comp (hdr)) {
163 if (image_get_load (hdr) == (ulong)hdr) {
164 printf (" XIP %s ... ", type_name);
166 printf (" Loading %s ... ", type_name);
168 memmove_wd ((void *)image_get_load (hdr),
169 (void *)os_data, os_len, CHUNKSZ);
171 load_end = load_start + os_len;
176 printf (" Uncompressing %s ... ", type_name);
177 if (gunzip ((void *)image_get_load (hdr), unc_len,
178 (uchar *)os_data, &os_len) != 0) {
179 puts ("GUNZIP ERROR - must RESET board to recover\n");
180 show_boot_progress (-6);
181 do_reset (cmdtp, flag, argc, argv);
184 load_end = load_start + os_len;
188 printf (" Uncompressing %s ... ", type_name);
190 * If we've got less than 4 MB of malloc() space,
191 * use slower decompression algorithm which requires
192 * at most 2300 KB of memory.
194 int i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
195 &unc_len, (char *)os_data, os_len,
196 CFG_MALLOC_LEN < (4096 * 1024), 0);
198 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
199 show_boot_progress (-6);
200 do_reset (cmdtp, flag, argc, argv);
203 load_end = load_start + unc_len;
205 #endif /* CONFIG_BZIP2 */
209 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
210 show_boot_progress (-7);
214 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
215 show_boot_progress (7);
217 if ((load_start < image_end) && (load_end > image_start)) {
218 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
219 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
221 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
222 do_reset (cmdtp, flag, argc, argv);
225 show_boot_progress (8);
227 switch (image_get_os (hdr)) {
228 default: /* handled by (original) Linux case */
230 #ifdef CONFIG_SILENT_CONSOLE
231 fixup_silent_linux();
233 do_bootm_linux (cmdtp, flag, argc, argv, hdr, verify);
237 do_bootm_netbsd (cmdtp, flag, argc, argv, hdr, verify);
240 #ifdef CONFIG_LYNXKDI
242 do_bootm_lynxkdi (cmdtp, flag, argc, argv, hdr, verify);
247 do_bootm_rtems (cmdtp, flag, argc, argv, hdr, verify);
250 #if defined(CONFIG_CMD_ELF)
252 do_bootm_vxworks (cmdtp, flag, argc, argv, hdr, verify);
256 do_bootm_qnxelf (cmdtp, flag, argc, argv, hdr, verify);
262 do_bootm_artos (cmdtp, flag, argc, argv, hdr, verify);
267 show_boot_progress (-9);
269 puts ("\n## Control returned to monitor - resetting...\n");
270 do_reset (cmdtp, flag, argc, argv);
276 * get_kernel - find kernel image
277 * @os_data: pointer to a ulong variable, will hold os data start address
278 * @os_len: pointer to a ulong variable, will hold os data length
280 * get_kernel() tries to find a kernel image, verifies its integrity
281 * and locates kernel data.
284 * pointer to image header if valid image was found, plus kernel start
285 * address and length, otherwise NULL
287 static image_header_t *get_kernel (cmd_tbl_t *cmdtp, int flag,
288 int argc, char *argv[], int verify,
289 ulong *os_data, ulong *os_len)
295 img_addr = load_addr;
297 img_addr = simple_strtoul(argv[1], NULL, 16);
300 show_boot_progress (1);
301 printf ("## Booting image at %08lx ...\n", img_addr);
303 /* copy from dataflash if needed */
304 img_addr = gen_get_image (img_addr);
305 hdr = (image_header_t *)img_addr;
307 if (!image_check_magic(hdr)) {
308 puts ("Bad Magic Number\n");
309 show_boot_progress (-1);
312 show_boot_progress (2);
314 if (!image_check_hcrc (hdr)) {
315 puts ("Bad Header Checksum\n");
316 show_boot_progress (-2);
320 show_boot_progress (3);
321 print_image_hdr (hdr);
324 puts (" Verifying Checksum ... ");
325 if (!image_check_dcrc (hdr)) {
326 printf ("Bad Data CRC\n");
327 show_boot_progress (-3);
332 show_boot_progress (4);
334 if (!image_check_target_arch (hdr)) {
335 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
336 show_boot_progress (-4);
339 show_boot_progress (5);
341 switch (image_get_type (hdr)) {
343 *os_data = image_get_data (hdr);
344 *os_len = image_get_data_size (hdr);
347 image_multi_getimg (hdr, 0, os_data, os_len);
350 printf ("Wrong Image Type for %s command\n", cmdtp->name);
351 show_boot_progress (-5);
354 debug (" kernel data at 0x%08lx, end = 0x%08lx\n",
355 *os_data, *os_data + *os_len);
361 bootm, CFG_MAXARGS, 1, do_bootm,
362 "bootm - boot application image from memory\n",
363 "[addr [arg ...]]\n - boot application image stored in memory\n"
364 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
365 "\t'arg' can be the address of an initrd image\n"
366 #if defined(CONFIG_OF_LIBFDT)
367 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
368 "\ta third argument is required which is the address of the\n"
369 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
370 "\tuse a '-' for the second argument. If you do not pass a third\n"
371 "\ta bd_info struct will be passed instead\n"
375 /*******************************************************************/
376 /* bootd - boot default image */
377 /*******************************************************************/
378 #if defined(CONFIG_CMD_BOOTD)
379 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
383 #ifndef CFG_HUSH_PARSER
384 if (run_command (getenv ("bootcmd"), flag) < 0)
387 if (parse_string_outer (getenv ("bootcmd"),
388 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
395 boot, 1, 1, do_bootd,
396 "boot - boot default, i.e., run 'bootcmd'\n",
400 /* keep old command name "bootd" for backward compatibility */
402 bootd, 1, 1, do_bootd,
403 "bootd - boot default, i.e., run 'bootcmd'\n",
410 /*******************************************************************/
411 /* iminfo - print header info for a requested image */
412 /*******************************************************************/
413 #if defined(CONFIG_CMD_IMI)
414 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
421 return image_info (load_addr);
424 for (arg = 1; arg < argc; ++arg) {
425 addr = simple_strtoul (argv[arg], NULL, 16);
426 if (image_info (addr) != 0)
432 static int image_info (ulong addr)
434 image_header_t *hdr = (image_header_t *)addr;
436 printf ("\n## Checking Image at %08lx ...\n", addr);
438 if (!image_check_magic (hdr)) {
439 puts (" Bad Magic Number\n");
443 if (!image_check_hcrc (hdr)) {
444 puts (" Bad Header Checksum\n");
448 print_image_hdr (hdr);
450 puts (" Verifying Checksum ... ");
451 if (!image_check_dcrc (hdr)) {
452 puts (" Bad Data CRC\n");
460 iminfo, CFG_MAXARGS, 1, do_iminfo,
461 "iminfo - print header information for application image\n",
463 " - print header information for application image starting at\n"
464 " address 'addr' in memory; this includes verification of the\n"
465 " image contents (magic number, header and payload checksums)\n"
470 /*******************************************************************/
471 /* imls - list all images found in flash */
472 /*******************************************************************/
473 #if defined(CONFIG_CMD_IMLS)
474 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
480 for (i = 0, info = &flash_info[0];
481 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
483 if (info->flash_id == FLASH_UNKNOWN)
485 for (j = 0; j < info->sector_count; ++j) {
487 hdr = (image_header_t *)info->start[j];
489 if (!hdr || !image_check_magic (hdr))
492 if (!image_check_hcrc (hdr))
495 printf ("Image at %08lX:\n", (ulong)hdr);
496 print_image_hdr (hdr);
498 puts (" Verifying Checksum ... ");
499 if (!image_check_dcrc (hdr)) {
500 puts ("Bad Data CRC\n");
514 "imls - list all images found in flash\n",
516 " - Prints information about all images found at sector\n"
517 " boundaries in flash.\n"
521 /*******************************************************************/
522 /* helper routines */
523 /*******************************************************************/
524 void print_image_hdr (image_header_t *hdr)
526 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
527 time_t timestamp = (time_t)image_get_time (hdr);
531 printf (" Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
533 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
534 to_tm (timestamp, &tm);
535 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
536 tm.tm_year, tm.tm_mon, tm.tm_mday,
537 tm.tm_hour, tm.tm_min, tm.tm_sec);
539 puts (" Image Type: ");
542 printf ("\n Data Size: %d Bytes = ", image_get_data_size (hdr));
543 print_size (image_get_data_size (hdr), "\n");
544 printf (" Load Address: %08x\n"
545 " Entry Point: %08x\n",
546 image_get_load (hdr), image_get_ep (hdr));
548 if (image_check_type (hdr, IH_TYPE_MULTI)) {
551 ulong count = image_multi_count (hdr);
553 puts (" Contents:\n");
554 for (i = 0; i < count; i++) {
555 image_multi_getimg (hdr, i, &data, &len);
556 printf (" Image %d: %8ld Bytes = ", i, len);
557 print_size (len, "\n");
562 static void print_type (image_header_t *hdr)
564 const char *os, *arch, *type, *comp;
566 os = image_get_os_name (image_get_os (hdr));
567 arch = image_get_arch_name (image_get_arch (hdr));
568 type = image_get_type_name (image_get_type (hdr));
569 comp = image_get_comp_name (image_get_comp (hdr));
571 printf ("%s %s %s (%s)", arch, os, type, comp);
574 #ifdef CONFIG_SILENT_CONSOLE
575 static void fixup_silent_linux ()
577 char buf[256], *start, *end;
578 char *cmdline = getenv ("bootargs");
580 /* Only fix cmdline when requested */
581 if (!(gd->flags & GD_FLG_SILENT))
584 debug ("before silent fix-up: %s\n", cmdline);
586 if ((start = strstr (cmdline, "console=")) != NULL) {
587 end = strchr (start, ' ');
588 strncpy (buf, cmdline, (start - cmdline + 8));
590 strcpy (buf + (start - cmdline + 8), end);
592 buf[start - cmdline + 8] = '\0';
594 strcpy (buf, cmdline);
595 strcat (buf, " console=");
598 strcpy (buf, "console=");
601 setenv ("bootargs", buf);
602 debug ("after silent fix-up: %s\n", buf);
604 #endif /* CONFIG_SILENT_CONSOLE */
607 /*******************************************************************/
608 /* OS booting routines */
609 /*******************************************************************/
611 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
612 int argc, char *argv[],
613 image_header_t *hdr, int verify)
615 void (*loader)(bd_t *, image_header_t *, char *, char *);
616 image_header_t *img_addr;
617 ulong kernel_data, kernel_len;
622 * Booting a (NetBSD) kernel image
624 * This process is pretty similar to a standalone application:
625 * The (first part of an multi-) image must be a stage-2 loader,
626 * which in turn is responsible for loading & invoking the actual
627 * kernel. The only differences are the parameters being passed:
628 * besides the board info strucure, the loader expects a command
629 * line, the name of the console device, and (optionally) the
630 * address of the original image header.
634 if (image_check_type (hdr, IH_TYPE_MULTI)) {
635 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
641 #if defined (CONFIG_8xx_CONS_SMC1)
643 #elif defined (CONFIG_8xx_CONS_SMC2)
645 #elif defined (CONFIG_8xx_CONS_SCC2)
647 #elif defined (CONFIG_8xx_CONS_SCC3)
655 for (i = 2, len = 0; i < argc; i += 1)
656 len += strlen (argv[i]) + 1;
657 cmdline = malloc (len);
659 for (i = 2, len = 0; i < argc; i += 1) {
661 cmdline[len++] = ' ';
662 strcpy (&cmdline[len], argv[i]);
663 len += strlen (argv[i]);
665 } else if ((cmdline = getenv ("bootargs")) == NULL) {
669 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
671 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
674 show_boot_progress (15);
677 * NetBSD Stage-2 Loader Parameters:
678 * r3: ptr to board info data
681 * r6: boot args string
683 (*loader) (gd->bd, img_addr, consdev, cmdline);
686 #ifdef CONFIG_LYNXKDI
687 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
688 int argc, char *argv[],
689 image_header_t *hdr, int verify)
693 #endif /* CONFIG_LYNXKDI */
695 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
696 int argc, char *argv[],
697 image_header_t *hdr, int verify)
699 void (*entry_point)(bd_t *);
701 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
703 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
706 show_boot_progress (15);
710 * r3: ptr to board info data
712 (*entry_point)(gd->bd);
715 #if defined(CONFIG_CMD_ELF)
716 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
717 int argc, char *argv[],
718 image_header_t *hdr, int verify)
722 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
723 setenv("loadaddr", str);
724 do_bootvx(cmdtp, 0, 0, NULL);
727 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
728 int argc, char *argv[],
729 image_header_t *hdr, int verify)
734 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
735 local_args[0] = argv[0];
736 local_args[1] = str; /* and provide it via the arguments */
737 do_bootelf(cmdtp, 0, 2, local_args);
741 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
742 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
743 int argc, char *argv[],
744 image_header_t *hdr, int verify)
749 int i, j, nxt, len, envno, envsz;
751 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
754 * Booting an ARTOS kernel image + application
757 /* this used to be the top of memory, but was wrong... */
759 /* get stack pointer */
760 asm volatile ("mr %0,1" : "=r"(top) );
762 debug ("## Current stack ends at 0x%08lX ", top);
764 top -= 2048; /* just to be sure */
765 if (top > CFG_BOOTMAPSZ)
769 debug ("=> set upper limit to 0x%08lX\n", top);
771 /* first check the artos specific boot args, then the linux args*/
772 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
775 /* get length of cmdline, and place it */
777 top = (top - (len + 1)) & ~0xF;
778 cmdline = (char *)top;
779 debug ("## cmdline at 0x%08lX ", top);
783 top = (top - sizeof (bd_t)) & ~0xF;
784 debug ("## bd at 0x%08lX ", top);
786 memcpy (kbd, gd->bd, sizeof (bd_t));
788 /* first find number of env entries, and their size */
791 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
792 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
795 envsz += (nxt - i) + 1; /* plus trailing zero */
797 envno++; /* plus the terminating zero */
798 debug ("## %u envvars total size %u ", envno, envsz);
800 top = (top - sizeof (char **) * envno) & ~0xF;
801 fwenv = (char **)top;
802 debug ("## fwenv at 0x%08lX ", top);
804 top = (top - envsz) & ~0xF;
809 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
810 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
813 for (j = i; j < nxt; ++j)
814 *s++ = env_get_char (j);
817 *ss++ = NULL; /* terminate */
819 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
820 (*entry) (kbd, cmdline, fwenv, top);