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 image_header_t *get_kernel (cmd_tbl_t *cmdtp, int flag,
69 int argc, char *argv[], int verify,
70 ulong *os_data, ulong *os_len);
71 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
74 * Continue booting an OS image; caller already has:
75 * - copied image header to global variable `header'
76 * - checked header magic number, checksums (both header & image),
77 * - verified image architecture (PPC) and type (KERNEL or MULTI),
78 * - loaded (first part of) image to header load address,
79 * - disabled interrupts.
81 typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
82 int argc, char *argv[],
83 image_header_t *hdr, /* of image to boot */
84 int verify); /* getenv("verify")[0] != 'n' */
86 extern boot_os_fn do_bootm_linux;
87 static boot_os_fn do_bootm_netbsd;
88 #if defined(CONFIG_LYNXKDI)
89 static boot_os_fn do_bootm_lynxkdi;
90 extern void lynxkdi_boot (image_header_t *);
92 static boot_os_fn do_bootm_rtems;
93 #if defined(CONFIG_CMD_ELF)
94 static boot_os_fn do_bootm_vxworks;
95 static boot_os_fn do_bootm_qnxelf;
96 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
97 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
99 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
100 extern uchar (*env_get_char)(int); /* Returns a character from the environment */
101 static boot_os_fn do_bootm_artos;
104 ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
107 /*******************************************************************/
108 /* bootm - boot application image from image in memory */
109 /*******************************************************************/
110 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
113 const char *type_name;
114 uint unc_len = CFG_BOOTM_LEN;
115 int verify = getenv_verify();
118 ulong os_data, os_len;
120 ulong image_start, image_end;
121 ulong load_start, load_end;
123 /* get kernel image header, start address and length */
124 hdr = get_kernel (cmdtp, flag, argc, argv, verify,
129 show_boot_progress (6);
132 * We have reached the point of no return: we are going to
133 * overwrite all exception vector code, so we cannot easily
134 * recover from any failures any more...
136 iflag = disable_interrupts();
138 #ifdef CONFIG_AMIGAONEG3SE
140 * We've possible left the caches enabled during
141 * bios emulation, so turn them off again
144 invalidate_l1_instruction_cache();
149 type_name = image_get_type_name (image_get_type (hdr));
151 image_start = (ulong)hdr;
152 image_end = image_get_image_end (hdr);
153 load_start = image_get_load (hdr);
156 switch (image_get_comp (hdr)) {
158 if (image_get_load (hdr) == (ulong)hdr) {
159 printf (" XIP %s ... ", type_name);
161 printf (" Loading %s ... ", type_name);
163 memmove_wd ((void *)image_get_load (hdr),
164 (void *)os_data, os_len, CHUNKSZ);
166 load_end = load_start + os_len;
171 printf (" Uncompressing %s ... ", type_name);
172 if (gunzip ((void *)image_get_load (hdr), unc_len,
173 (uchar *)os_data, &os_len) != 0) {
174 puts ("GUNZIP ERROR - must RESET board to recover\n");
175 show_boot_progress (-6);
176 do_reset (cmdtp, flag, argc, argv);
179 load_end = load_start + os_len;
183 printf (" Uncompressing %s ... ", type_name);
185 * If we've got less than 4 MB of malloc() space,
186 * use slower decompression algorithm which requires
187 * at most 2300 KB of memory.
189 int i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
190 &unc_len, (char *)os_data, os_len,
191 CFG_MALLOC_LEN < (4096 * 1024), 0);
193 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
194 show_boot_progress (-6);
195 do_reset (cmdtp, flag, argc, argv);
198 load_end = load_start + unc_len;
200 #endif /* CONFIG_BZIP2 */
204 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
205 show_boot_progress (-7);
209 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
210 show_boot_progress (7);
212 if ((load_start < image_end) && (load_end > image_start)) {
213 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
214 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
216 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
217 do_reset (cmdtp, flag, argc, argv);
220 show_boot_progress (8);
222 switch (image_get_os (hdr)) {
223 default: /* handled by (original) Linux case */
225 #ifdef CONFIG_SILENT_CONSOLE
226 fixup_silent_linux();
228 do_bootm_linux (cmdtp, flag, argc, argv, hdr, verify);
232 do_bootm_netbsd (cmdtp, flag, argc, argv, hdr, verify);
235 #ifdef CONFIG_LYNXKDI
237 do_bootm_lynxkdi (cmdtp, flag, argc, argv, hdr, verify);
242 do_bootm_rtems (cmdtp, flag, argc, argv, hdr, verify);
245 #if defined(CONFIG_CMD_ELF)
247 do_bootm_vxworks (cmdtp, flag, argc, argv, hdr, verify);
251 do_bootm_qnxelf (cmdtp, flag, argc, argv, hdr, verify);
257 do_bootm_artos (cmdtp, flag, argc, argv, hdr, verify);
262 show_boot_progress (-9);
264 puts ("\n## Control returned to monitor - resetting...\n");
265 do_reset (cmdtp, flag, argc, argv);
271 * get_kernel - find kernel image
272 * @os_data: pointer to a ulong variable, will hold os data start address
273 * @os_len: pointer to a ulong variable, will hold os data length
275 * get_kernel() tries to find a kernel image, verifies its integrity
276 * and locates kernel data.
279 * pointer to image header if valid image was found, plus kernel start
280 * address and length, otherwise NULL
282 static image_header_t *get_kernel (cmd_tbl_t *cmdtp, int flag,
283 int argc, char *argv[], int verify,
284 ulong *os_data, ulong *os_len)
290 img_addr = load_addr;
292 img_addr = simple_strtoul(argv[1], NULL, 16);
295 show_boot_progress (1);
296 printf ("## Booting image at %08lx ...\n", img_addr);
298 /* copy from dataflash if needed */
299 img_addr = gen_get_image (img_addr);
300 hdr = (image_header_t *)img_addr;
302 if (!image_check_magic(hdr)) {
303 puts ("Bad Magic Number\n");
304 show_boot_progress (-1);
307 show_boot_progress (2);
309 if (!image_check_hcrc (hdr)) {
310 puts ("Bad Header Checksum\n");
311 show_boot_progress (-2);
315 show_boot_progress (3);
316 image_print_contents (hdr);
319 puts (" Verifying Checksum ... ");
320 if (!image_check_dcrc (hdr)) {
321 printf ("Bad Data CRC\n");
322 show_boot_progress (-3);
327 show_boot_progress (4);
329 if (!image_check_target_arch (hdr)) {
330 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
331 show_boot_progress (-4);
334 show_boot_progress (5);
336 switch (image_get_type (hdr)) {
338 *os_data = image_get_data (hdr);
339 *os_len = image_get_data_size (hdr);
342 image_multi_getimg (hdr, 0, os_data, os_len);
345 printf ("Wrong Image Type for %s command\n", cmdtp->name);
346 show_boot_progress (-5);
349 debug (" kernel data at 0x%08lx, end = 0x%08lx\n",
350 *os_data, *os_data + *os_len);
356 bootm, CFG_MAXARGS, 1, do_bootm,
357 "bootm - boot application image from memory\n",
358 "[addr [arg ...]]\n - boot application image stored in memory\n"
359 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
360 "\t'arg' can be the address of an initrd image\n"
361 #if defined(CONFIG_OF_LIBFDT)
362 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
363 "\ta third argument is required which is the address of the\n"
364 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
365 "\tuse a '-' for the second argument. If you do not pass a third\n"
366 "\ta bd_info struct will be passed instead\n"
370 /*******************************************************************/
371 /* bootd - boot default image */
372 /*******************************************************************/
373 #if defined(CONFIG_CMD_BOOTD)
374 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
378 #ifndef CFG_HUSH_PARSER
379 if (run_command (getenv ("bootcmd"), flag) < 0)
382 if (parse_string_outer (getenv ("bootcmd"),
383 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
390 boot, 1, 1, do_bootd,
391 "boot - boot default, i.e., run 'bootcmd'\n",
395 /* keep old command name "bootd" for backward compatibility */
397 bootd, 1, 1, do_bootd,
398 "bootd - boot default, i.e., run 'bootcmd'\n",
405 /*******************************************************************/
406 /* iminfo - print header info for a requested image */
407 /*******************************************************************/
408 #if defined(CONFIG_CMD_IMI)
409 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
416 return image_info (load_addr);
419 for (arg = 1; arg < argc; ++arg) {
420 addr = simple_strtoul (argv[arg], NULL, 16);
421 if (image_info (addr) != 0)
427 static int image_info (ulong addr)
429 image_header_t *hdr = (image_header_t *)addr;
431 printf ("\n## Checking Image at %08lx ...\n", addr);
433 if (!image_check_magic (hdr)) {
434 puts (" Bad Magic Number\n");
438 if (!image_check_hcrc (hdr)) {
439 puts (" Bad Header Checksum\n");
443 image_print_contents (hdr);
445 puts (" Verifying Checksum ... ");
446 if (!image_check_dcrc (hdr)) {
447 puts (" Bad Data CRC\n");
455 iminfo, CFG_MAXARGS, 1, do_iminfo,
456 "iminfo - print header information for application image\n",
458 " - print header information for application image starting at\n"
459 " address 'addr' in memory; this includes verification of the\n"
460 " image contents (magic number, header and payload checksums)\n"
465 /*******************************************************************/
466 /* imls - list all images found in flash */
467 /*******************************************************************/
468 #if defined(CONFIG_CMD_IMLS)
469 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
475 for (i = 0, info = &flash_info[0];
476 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
478 if (info->flash_id == FLASH_UNKNOWN)
480 for (j = 0; j < info->sector_count; ++j) {
482 hdr = (image_header_t *)info->start[j];
484 if (!hdr || !image_check_magic (hdr))
487 if (!image_check_hcrc (hdr))
490 printf ("Image at %08lX:\n", (ulong)hdr);
491 image_print_contents (hdr);
493 puts (" Verifying Checksum ... ");
494 if (!image_check_dcrc (hdr)) {
495 puts ("Bad Data CRC\n");
509 "imls - list all images found in flash\n",
511 " - Prints information about all images found at sector\n"
512 " boundaries in flash.\n"
516 /*******************************************************************/
517 /* helper routines */
518 /*******************************************************************/
519 #ifdef CONFIG_SILENT_CONSOLE
520 static void fixup_silent_linux ()
522 char buf[256], *start, *end;
523 char *cmdline = getenv ("bootargs");
525 /* Only fix cmdline when requested */
526 if (!(gd->flags & GD_FLG_SILENT))
529 debug ("before silent fix-up: %s\n", cmdline);
531 if ((start = strstr (cmdline, "console=")) != NULL) {
532 end = strchr (start, ' ');
533 strncpy (buf, cmdline, (start - cmdline + 8));
535 strcpy (buf + (start - cmdline + 8), end);
537 buf[start - cmdline + 8] = '\0';
539 strcpy (buf, cmdline);
540 strcat (buf, " console=");
543 strcpy (buf, "console=");
546 setenv ("bootargs", buf);
547 debug ("after silent fix-up: %s\n", buf);
549 #endif /* CONFIG_SILENT_CONSOLE */
552 /*******************************************************************/
553 /* OS booting routines */
554 /*******************************************************************/
556 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
557 int argc, char *argv[],
558 image_header_t *hdr, int verify)
560 void (*loader)(bd_t *, image_header_t *, char *, char *);
561 image_header_t *img_addr;
562 ulong kernel_data, kernel_len;
567 * Booting a (NetBSD) kernel image
569 * This process is pretty similar to a standalone application:
570 * The (first part of an multi-) image must be a stage-2 loader,
571 * which in turn is responsible for loading & invoking the actual
572 * kernel. The only differences are the parameters being passed:
573 * besides the board info strucure, the loader expects a command
574 * line, the name of the console device, and (optionally) the
575 * address of the original image header.
579 if (image_check_type (hdr, IH_TYPE_MULTI)) {
580 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
586 #if defined (CONFIG_8xx_CONS_SMC1)
588 #elif defined (CONFIG_8xx_CONS_SMC2)
590 #elif defined (CONFIG_8xx_CONS_SCC2)
592 #elif defined (CONFIG_8xx_CONS_SCC3)
600 for (i = 2, len = 0; i < argc; i += 1)
601 len += strlen (argv[i]) + 1;
602 cmdline = malloc (len);
604 for (i = 2, len = 0; i < argc; i += 1) {
606 cmdline[len++] = ' ';
607 strcpy (&cmdline[len], argv[i]);
608 len += strlen (argv[i]);
610 } else if ((cmdline = getenv ("bootargs")) == NULL) {
614 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
616 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
619 show_boot_progress (15);
622 * NetBSD Stage-2 Loader Parameters:
623 * r3: ptr to board info data
626 * r6: boot args string
628 (*loader) (gd->bd, img_addr, consdev, cmdline);
631 #ifdef CONFIG_LYNXKDI
632 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
633 int argc, char *argv[],
634 image_header_t *hdr, int verify)
638 #endif /* CONFIG_LYNXKDI */
640 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
641 int argc, char *argv[],
642 image_header_t *hdr, int verify)
644 void (*entry_point)(bd_t *);
646 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
648 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
651 show_boot_progress (15);
655 * r3: ptr to board info data
657 (*entry_point)(gd->bd);
660 #if defined(CONFIG_CMD_ELF)
661 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
662 int argc, char *argv[],
663 image_header_t *hdr, int verify)
667 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
668 setenv("loadaddr", str);
669 do_bootvx(cmdtp, 0, 0, NULL);
672 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
673 int argc, char *argv[],
674 image_header_t *hdr, int verify)
679 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
680 local_args[0] = argv[0];
681 local_args[1] = str; /* and provide it via the arguments */
682 do_bootelf(cmdtp, 0, 2, local_args);
686 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
687 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
688 int argc, char *argv[],
689 image_header_t *hdr, int verify)
694 int i, j, nxt, len, envno, envsz;
696 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
699 * Booting an ARTOS kernel image + application
702 /* this used to be the top of memory, but was wrong... */
704 /* get stack pointer */
705 asm volatile ("mr %0,1" : "=r"(top) );
707 debug ("## Current stack ends at 0x%08lX ", top);
709 top -= 2048; /* just to be sure */
710 if (top > CFG_BOOTMAPSZ)
714 debug ("=> set upper limit to 0x%08lX\n", top);
716 /* first check the artos specific boot args, then the linux args*/
717 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
720 /* get length of cmdline, and place it */
722 top = (top - (len + 1)) & ~0xF;
723 cmdline = (char *)top;
724 debug ("## cmdline at 0x%08lX ", top);
728 top = (top - sizeof (bd_t)) & ~0xF;
729 debug ("## bd at 0x%08lX ", top);
731 memcpy (kbd, gd->bd, sizeof (bd_t));
733 /* first find number of env entries, and their size */
736 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
737 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
740 envsz += (nxt - i) + 1; /* plus trailing zero */
742 envno++; /* plus the terminating zero */
743 debug ("## %u envvars total size %u ", envno, envsz);
745 top = (top - sizeof (char **) * envno) & ~0xF;
746 fwenv = (char **)top;
747 debug ("## fwenv at 0x%08lX ", top);
749 top = (top - envsz) & ~0xF;
754 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
755 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
758 for (j = i; j < nxt; ++j)
759 *s++ = env_get_char (j);
762 *ss++ = NULL; /* terminate */
764 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
765 (*entry) (kbd, cmdline, fwenv, top);