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,
34 #include <environment.h>
35 #include <asm/byteorder.h>
37 #if defined(CONFIG_OF_LIBFDT)
41 #if defined(CONFIG_OF_FLAT_TREE)
45 DECLARE_GLOBAL_DATA_PTR;
48 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50 #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
54 #ifdef CFG_HUSH_PARSER
58 #ifdef CONFIG_SHOW_BOOT_PROGRESS
59 # include <status_led.h>
60 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
62 # define SHOW_BOOT_PROGRESS(arg)
65 #ifdef CFG_INIT_RAM_LOCK
66 #include <asm/cache.h>
69 #ifdef CONFIG_LOGBUFFER
73 #ifdef CONFIG_HAS_DATAFLASH
74 #include <dataflash.h>
78 * Some systems (for example LWMON) have very short watchdog periods;
79 * we must make sure to split long operations like memmove() or
80 * crc32() into reasonable chunks.
82 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
83 # define CHUNKSZ (64 * 1024)
86 int gunzip (void *, int, unsigned char *, unsigned long *);
88 static void *zalloc(void *, unsigned, unsigned);
89 static void zfree(void *, void *, unsigned);
91 #if (CONFIG_COMMANDS & CFG_CMD_IMI)
92 static int image_info (unsigned long addr);
95 #if (CONFIG_COMMANDS & CFG_CMD_IMLS)
97 extern flash_info_t flash_info[]; /* info for FLASH chips */
98 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
101 static void print_type (image_header_t *hdr);
104 image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
108 * Continue booting an OS image; caller already has:
109 * - copied image header to global variable `header'
110 * - checked header magic number, checksums (both header & image),
111 * - verified image architecture (PPC) and type (KERNEL or MULTI),
112 * - loaded (first part of) image to header load address,
113 * - disabled interrupts.
115 typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
116 int argc, char *argv[],
117 ulong addr, /* of image to boot */
118 ulong *len_ptr, /* multi-file image length table */
119 int verify); /* getenv("verify")[0] != 'n' */
122 extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
126 static boot_os_Fcn do_bootm_linux;
128 extern boot_os_Fcn do_bootm_linux;
130 #ifdef CONFIG_SILENT_CONSOLE
131 static void fixup_silent_linux (void);
133 static boot_os_Fcn do_bootm_netbsd;
134 static boot_os_Fcn do_bootm_rtems;
135 #if (CONFIG_COMMANDS & CFG_CMD_ELF)
136 static boot_os_Fcn do_bootm_vxworks;
137 static boot_os_Fcn do_bootm_qnxelf;
138 int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
139 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
140 #endif /* CFG_CMD_ELF */
141 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
142 static boot_os_Fcn do_bootm_artos;
144 #ifdef CONFIG_LYNXKDI
145 static boot_os_Fcn do_bootm_lynxkdi;
146 extern void lynxkdi_boot( image_header_t * );
149 #ifndef CFG_BOOTM_LEN
150 #define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
153 image_header_t header;
155 ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
157 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
161 ulong data, len, checksum;
163 uint unc_len = CFG_BOOTM_LEN;
166 int (*appl)(int, char *[]);
167 image_header_t *hdr = &header;
169 s = getenv ("verify");
170 verify = (s && (*s == 'n')) ? 0 : 1;
175 addr = simple_strtoul(argv[1], NULL, 16);
178 SHOW_BOOT_PROGRESS (1);
179 printf ("## Booting image at %08lx ...\n", addr);
181 /* Copy header so we can blank CRC field for re-calculation */
182 #ifdef CONFIG_HAS_DATAFLASH
183 if (addr_dataflash(addr)){
184 read_dataflash(addr, sizeof(image_header_t), (char *)&header);
187 memmove (&header, (char *)addr, sizeof(image_header_t));
189 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
190 #ifdef __I386__ /* correct image format not implemented yet - fake it */
191 if (fake_header(hdr, (void*)addr, -1) != NULL) {
192 /* to compensate for the addition below */
193 addr -= sizeof(image_header_t);
195 * fake_header() does not fake the data crc
199 #endif /* __I386__ */
201 puts ("Bad Magic Number\n");
202 SHOW_BOOT_PROGRESS (-1);
206 SHOW_BOOT_PROGRESS (2);
208 data = (ulong)&header;
209 len = sizeof(image_header_t);
211 checksum = ntohl(hdr->ih_hcrc);
214 if (crc32 (0, (uchar *)data, len) != checksum) {
215 puts ("Bad Header Checksum\n");
216 SHOW_BOOT_PROGRESS (-2);
219 SHOW_BOOT_PROGRESS (3);
221 #ifdef CONFIG_HAS_DATAFLASH
222 if (addr_dataflash(addr)){
223 len = ntohl(hdr->ih_size) + sizeof(image_header_t);
224 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
225 addr = CFG_LOAD_ADDR;
230 /* for multi-file images we need the data part, too */
231 print_image_hdr ((image_header_t *)addr);
233 data = addr + sizeof(image_header_t);
234 len = ntohl(hdr->ih_size);
237 puts (" Verifying Checksum ... ");
238 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
239 printf ("Bad Data CRC\n");
240 SHOW_BOOT_PROGRESS (-3);
245 SHOW_BOOT_PROGRESS (4);
247 len_ptr = (ulong *)data;
250 if (hdr->ih_arch != IH_CPU_ARM)
251 #elif defined(__avr32__)
252 if (hdr->ih_arch != IH_CPU_AVR32)
253 #elif defined(__bfin__)
254 if (hdr->ih_arch != IH_CPU_BLACKFIN)
255 #elif defined(__I386__)
256 if (hdr->ih_arch != IH_CPU_I386)
257 #elif defined(__M68K__)
258 if (hdr->ih_arch != IH_CPU_M68K)
259 #elif defined(__microblaze__)
260 if (hdr->ih_arch != IH_CPU_MICROBLAZE)
261 #elif defined(__mips__)
262 if (hdr->ih_arch != IH_CPU_MIPS)
263 #elif defined(__nios__)
264 if (hdr->ih_arch != IH_CPU_NIOS)
265 #elif defined(__nios2__)
266 if (hdr->ih_arch != IH_CPU_NIOS2)
267 #elif defined(__PPC__)
268 if (hdr->ih_arch != IH_CPU_PPC)
270 # error Unknown CPU type
273 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
274 SHOW_BOOT_PROGRESS (-4);
277 SHOW_BOOT_PROGRESS (5);
279 switch (hdr->ih_type) {
280 case IH_TYPE_STANDALONE:
281 name = "Standalone Application";
282 /* A second argument overwrites the load address */
284 hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
288 name = "Kernel Image";
291 name = "Multi-File Image";
292 len = ntohl(len_ptr[0]);
293 /* OS kernel is always the first image */
294 data += 8; /* kernel_len + terminator */
295 for (i=1; len_ptr[i]; ++i)
298 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
299 SHOW_BOOT_PROGRESS (-5);
302 SHOW_BOOT_PROGRESS (6);
305 * We have reached the point of no return: we are going to
306 * overwrite all exception vector code, so we cannot easily
307 * recover from any failures any more...
310 iflag = disable_interrupts();
312 #ifdef CONFIG_AMIGAONEG3SE
314 * We've possible left the caches enabled during
315 * bios emulation, so turn them off again
318 invalidate_l1_instruction_cache();
323 switch (hdr->ih_comp) {
325 if(ntohl(hdr->ih_load) == addr) {
326 printf (" XIP %s ... ", name);
328 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
330 void *to = (void *)ntohl(hdr->ih_load);
331 void *from = (void *)data;
333 printf (" Loading %s ... ", name);
336 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
338 memmove (to, from, tail);
343 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
344 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
345 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
349 printf (" Uncompressing %s ... ", name);
350 if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
351 (uchar *)data, &len) != 0) {
352 puts ("GUNZIP ERROR - must RESET board to recover\n");
353 SHOW_BOOT_PROGRESS (-6);
354 do_reset (cmdtp, flag, argc, argv);
359 printf (" Uncompressing %s ... ", name);
361 * If we've got less than 4 MB of malloc() space,
362 * use slower decompression algorithm which requires
363 * at most 2300 KB of memory.
365 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
366 &unc_len, (char *)data, len,
367 CFG_MALLOC_LEN < (4096 * 1024), 0);
369 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
370 SHOW_BOOT_PROGRESS (-6);
372 do_reset (cmdtp, flag, argc, argv);
375 #endif /* CONFIG_BZIP2 */
379 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
380 SHOW_BOOT_PROGRESS (-7);
384 SHOW_BOOT_PROGRESS (7);
386 switch (hdr->ih_type) {
387 case IH_TYPE_STANDALONE:
391 /* load (and uncompress), but don't start if "autostart"
394 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
396 sprintf(buf, "%lX", len);
397 setenv("filesize", buf);
400 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
401 (*appl)(argc-1, &argv[1]);
410 printf ("Can't boot image type %d\n", hdr->ih_type);
411 SHOW_BOOT_PROGRESS (-8);
414 SHOW_BOOT_PROGRESS (8);
416 switch (hdr->ih_os) {
417 default: /* handled by (original) Linux case */
419 #ifdef CONFIG_SILENT_CONSOLE
420 fixup_silent_linux();
422 do_bootm_linux (cmdtp, flag, argc, argv,
423 addr, len_ptr, verify);
426 do_bootm_netbsd (cmdtp, flag, argc, argv,
427 addr, len_ptr, verify);
430 #ifdef CONFIG_LYNXKDI
432 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
433 addr, len_ptr, verify);
438 do_bootm_rtems (cmdtp, flag, argc, argv,
439 addr, len_ptr, verify);
442 #if (CONFIG_COMMANDS & CFG_CMD_ELF)
444 do_bootm_vxworks (cmdtp, flag, argc, argv,
445 addr, len_ptr, verify);
448 do_bootm_qnxelf (cmdtp, flag, argc, argv,
449 addr, len_ptr, verify);
451 #endif /* CFG_CMD_ELF */
454 do_bootm_artos (cmdtp, flag, argc, argv,
455 addr, len_ptr, verify);
460 SHOW_BOOT_PROGRESS (-9);
462 puts ("\n## Control returned to monitor - resetting...\n");
463 do_reset (cmdtp, flag, argc, argv);
469 bootm, CFG_MAXARGS, 1, do_bootm,
470 "bootm - boot application image from memory\n",
471 "[addr [arg ...]]\n - boot application image stored in memory\n"
472 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
473 "\t'arg' can be the address of an initrd image\n"
474 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
475 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
476 "\ta third argument is required which is the address of the of the\n"
477 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
478 "\tuse a '-' for the second argument. If you do not pass a third\n"
479 "\ta bd_info struct will be passed instead\n"
483 #ifdef CONFIG_SILENT_CONSOLE
485 fixup_silent_linux ()
487 char buf[256], *start, *end;
488 char *cmdline = getenv ("bootargs");
490 /* Only fix cmdline when requested */
491 if (!(gd->flags & GD_FLG_SILENT))
494 debug ("before silent fix-up: %s\n", cmdline);
496 if ((start = strstr (cmdline, "console=")) != NULL) {
497 end = strchr (start, ' ');
498 strncpy (buf, cmdline, (start - cmdline + 8));
500 strcpy (buf + (start - cmdline + 8), end);
502 buf[start - cmdline + 8] = '\0';
504 strcpy (buf, cmdline);
505 strcat (buf, " console=");
508 strcpy (buf, "console=");
511 setenv ("bootargs", buf);
512 debug ("after silent fix-up: %s\n", buf);
514 #endif /* CONFIG_SILENT_CONSOLE */
517 static void __attribute__((noinline))
518 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
519 int argc, char *argv[],
526 ulong initrd_start, initrd_end;
527 ulong cmd_start, cmd_end;
530 int initrd_copy_to_ram = 1;
534 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
535 image_header_t *hdr = &header;
536 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
537 char *of_flat_tree = NULL;
541 if ((s = getenv ("initrd_high")) != NULL) {
542 /* a value of "no" or a similar string will act like 0,
543 * turning the "load high" feature off. This is intentional.
545 initrd_high = simple_strtoul(s, NULL, 16);
546 if (initrd_high == ~0)
547 initrd_copy_to_ram = 0;
548 } else { /* not set, no restrictions to load high */
552 #ifdef CONFIG_LOGBUFFER
554 /* Prevent initrd from overwriting logbuffer */
555 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
556 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
557 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
561 * Booting a (Linux) kernel image
563 * Allocate space for command line and board info - the
564 * address should be as high as possible within the reach of
565 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
566 * memory, which means far enough below the current stack
570 asm( "mr %0,1": "=r"(sp) : );
572 debug ("## Current stack ends at 0x%08lX ", sp);
574 sp -= 2048; /* just to be sure */
575 if (sp > CFG_BOOTMAPSZ)
579 debug ("=> set upper limit to 0x%08lX\n", sp);
581 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
582 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
584 if ((s = getenv("bootargs")) == NULL)
589 cmd_start = (ulong)&cmdline[0];
590 cmd_end = cmd_start + strlen(cmdline);
595 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
597 do_bdinfo (NULL, 0, 0, NULL);
600 if ((s = getenv ("clocks_in_mhz")) != NULL) {
601 /* convert all clock information to MHz */
602 kbd->bi_intfreq /= 1000000L;
603 kbd->bi_busfreq /= 1000000L;
604 #if defined(CONFIG_MPC8220)
605 kbd->bi_inpfreq /= 1000000L;
606 kbd->bi_pcifreq /= 1000000L;
607 kbd->bi_pevfreq /= 1000000L;
608 kbd->bi_flbfreq /= 1000000L;
609 kbd->bi_vcofreq /= 1000000L;
611 #if defined(CONFIG_CPM2)
612 kbd->bi_cpmfreq /= 1000000L;
613 kbd->bi_brgfreq /= 1000000L;
614 kbd->bi_sccfreq /= 1000000L;
615 kbd->bi_vco /= 1000000L;
617 #if defined(CONFIG_MPC5xxx)
618 kbd->bi_ipbfreq /= 1000000L;
619 kbd->bi_pcifreq /= 1000000L;
620 #endif /* CONFIG_MPC5xxx */
623 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
626 * Check if there is an initrd image
629 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
630 /* Look for a '-' which indicates to ignore the ramdisk argument */
631 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
632 debug ("Skipping initrd\n");
638 debug ("Not skipping initrd\n");
639 SHOW_BOOT_PROGRESS (9);
641 addr = simple_strtoul(argv[2], NULL, 16);
643 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
645 /* Copy header so we can blank CRC field for re-calculation */
646 memmove (&header, (char *)addr, sizeof(image_header_t));
648 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
649 puts ("Bad Magic Number\n");
650 SHOW_BOOT_PROGRESS (-10);
651 do_reset (cmdtp, flag, argc, argv);
654 data = (ulong)&header;
655 len = sizeof(image_header_t);
657 checksum = ntohl(hdr->ih_hcrc);
660 if (crc32 (0, (uchar *)data, len) != checksum) {
661 puts ("Bad Header Checksum\n");
662 SHOW_BOOT_PROGRESS (-11);
663 do_reset (cmdtp, flag, argc, argv);
666 SHOW_BOOT_PROGRESS (10);
668 print_image_hdr (hdr);
670 data = addr + sizeof(image_header_t);
671 len = ntohl(hdr->ih_size);
675 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
676 ulong cdata = data, edata = cdata + len;
677 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
679 puts (" Verifying Checksum ... ");
681 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
683 while (cdata < edata) {
684 ulong chunk = edata - cdata;
688 csum = crc32 (csum, (uchar *)cdata, chunk);
693 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
694 csum = crc32 (0, (uchar *)data, len);
695 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
697 if (csum != ntohl(hdr->ih_dcrc)) {
698 puts ("Bad Data CRC\n");
699 SHOW_BOOT_PROGRESS (-12);
700 do_reset (cmdtp, flag, argc, argv);
705 SHOW_BOOT_PROGRESS (11);
707 if ((hdr->ih_os != IH_OS_LINUX) ||
708 (hdr->ih_arch != IH_CPU_PPC) ||
709 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
710 puts ("No Linux PPC Ramdisk Image\n");
711 SHOW_BOOT_PROGRESS (-13);
712 do_reset (cmdtp, flag, argc, argv);
716 * Now check if we have a multifile image
718 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
719 u_long tail = ntohl(len_ptr[0]) % 4;
722 SHOW_BOOT_PROGRESS (13);
724 /* skip kernel length and terminator */
725 data = (ulong)(&len_ptr[2]);
726 /* skip any additional image length fields */
727 for (i=1; len_ptr[i]; ++i)
729 /* add kernel length, and align */
730 data += ntohl(len_ptr[0]);
735 len = ntohl(len_ptr[1]);
741 SHOW_BOOT_PROGRESS (14);
746 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
748 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
749 hdr = (image_header_t *)of_flat_tree;
750 #if defined(CONFIG_OF_LIBFDT)
751 if (be32_to_cpu(fdt_magic(of_flat_tree)) == FDT_MAGIC) {
753 if (*(ulong *)of_flat_tree == OF_DT_HEADER) {
756 if (addr2info((ulong)of_flat_tree) != NULL)
757 of_data = (ulong)of_flat_tree;
759 } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
760 printf("## Flat Device Tree Image at %08lX\n", hdr);
761 print_image_hdr(hdr);
763 if ((ntohl(hdr->ih_load) < ((unsigned long)hdr + ntohl(hdr->ih_size) + sizeof(hdr))) &&
764 ((ntohl(hdr->ih_load) + ntohl(hdr->ih_size)) > (unsigned long)hdr)) {
765 printf ("ERROR: Load address overwrites Flat Device Tree uImage\n");
769 printf(" Verifying Checksum ... ");
770 memmove (&header, (char *)hdr, sizeof(image_header_t));
771 checksum = ntohl(header.ih_hcrc);
774 if(checksum != crc32(0, (uchar *)&header, sizeof(image_header_t))) {
775 printf("ERROR: Flat Device Tree header checksum is invalid\n");
779 checksum = ntohl(hdr->ih_dcrc);
780 addr = (ulong)((uchar *)(hdr) + sizeof(image_header_t));
781 len = ntohl(hdr->ih_size);
783 if(checksum != crc32(0, (uchar *)addr, len)) {
784 printf("ERROR: Flat Device Tree checksum is invalid\n");
789 if (ntohl(hdr->ih_type) != IH_TYPE_FLATDT) {
790 printf ("ERROR: uImage not Flat Device Tree type\n");
793 if (ntohl(hdr->ih_comp) != IH_COMP_NONE) {
794 printf("ERROR: uImage is not uncompressed\n");
797 #if defined(CONFIG_OF_LIBFDT)
798 if (be32_to_cpu(fdt_magic(of_flat_tree + sizeof(image_header_t))) != FDT_MAGIC) {
800 if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
802 printf ("ERROR: uImage data is not a flat device tree\n");
806 memmove((void *)ntohl(hdr->ih_load),
807 (void *)(of_flat_tree + sizeof(image_header_t)),
808 ntohl(hdr->ih_size));
809 of_flat_tree = (char *)ntohl(hdr->ih_load);
811 printf ("Did not find a flat flat device tree at address %08lX\n", of_flat_tree);
814 printf (" Booting using flat device tree at 0x%x\n",
816 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
817 u_long tail = ntohl(len_ptr[0]) % 4;
820 /* skip kernel length, initrd length, and terminator */
821 of_data = (ulong)(&len_ptr[3]);
822 /* skip any additional image length fields */
823 for (i=2; len_ptr[i]; ++i)
825 /* add kernel length, and align */
826 of_data += ntohl(len_ptr[0]);
831 /* add initrd length, and align */
832 tail = ntohl(len_ptr[1]) % 4;
833 of_data += ntohl(len_ptr[1]);
838 #if defined(CONFIG_OF_LIBFDT)
839 if (be32_to_cpu(fdt_magic(of_data)) != FDT_MAGIC) {
841 if (((struct boot_param_header *)of_data)->magic != OF_DT_HEADER) {
843 printf ("ERROR: image is not a flat device tree\n");
847 #if defined(CONFIG_OF_LIBFDT)
848 if (be32_to_cpu(fdt_totalsize(of_data)) != ntohl(len_ptr[2])) {
850 if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
852 printf ("ERROR: flat device tree size does not agree with image\n");
858 debug ("No initrd\n");
862 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
864 initrd_end = initrd_start + len;
866 initrd_start = (ulong)kbd - len;
867 initrd_start &= ~(4096 - 1); /* align on page */
873 * the inital ramdisk does not need to be within
874 * CFG_BOOTMAPSZ as it is not accessed until after
875 * the mm system is initialised.
877 * do the stack bottom calculation again and see if
878 * the initrd will fit just below the monitor stack
879 * bottom without overwriting the area allocated
880 * above for command line args and board info.
882 asm( "mr %0,1": "=r"(nsp) : );
883 nsp -= 2048; /* just to be sure */
885 if (nsp > initrd_high) /* limit as specified */
888 nsp &= ~(4096 - 1); /* align on page */
893 SHOW_BOOT_PROGRESS (12);
895 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
896 data, data + len - 1, len, len);
898 initrd_end = initrd_start + len;
899 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
900 initrd_start, initrd_end);
901 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
904 void *to = (void *)initrd_start;
905 void *from = (void *)data;
908 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
910 memmove (to, from, tail);
916 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
917 memmove ((void *)initrd_start, (void *)data, len);
918 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
926 debug ("## Transferring control to Linux (at address %08lx) ...\n",
929 SHOW_BOOT_PROGRESS (15);
931 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
932 unlock_ram_in_cache();
935 #if defined(CONFIG_OF_LIBFDT)
936 /* move of_flat_tree if needed */
939 ulong of_start, of_len;
940 of_len = be32_to_cpu(fdt_totalsize(of_data));
941 /* provide extra 8k pad */
943 of_start = initrd_start - of_len - 8192;
945 of_start = (ulong)kbd - of_len - 8192;
946 of_start &= ~(4096 - 1); /* align on page */
947 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
948 of_data, of_data + of_len - 1, of_len, of_len);
951 printf (" Loading Device Tree to %08lx, end %08lx ... ",
952 of_start, of_start + of_len - 1);
953 err = fdt_open_into(of_start, of_data, of_len);
955 printf ("libfdt: %s\n", fdt_strerror(err));
959 #if defined(CONFIG_OF_FLAT_TREE)
960 /* move of_flat_tree if needed */
962 ulong of_start, of_len;
963 of_len = ((struct boot_param_header *)of_data)->totalsize;
964 /* provide extra 8k pad */
966 of_start = initrd_start - of_len - 8192;
968 of_start = (ulong)kbd - of_len - 8192;
969 of_start &= ~(4096 - 1); /* align on page */
970 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
971 of_data, of_data + of_len - 1, of_len, of_len);
973 of_flat_tree = (char *)of_start;
974 printf (" Loading Device Tree to %08lx, end %08lx ... ",
975 of_start, of_start + of_len - 1);
976 memmove ((void *)of_start, (void *)of_data, of_len);
981 * Linux Kernel Parameters (passing board info data):
982 * r3: ptr to board info data
983 * r4: initrd_start or 0 if no initrd
984 * r5: initrd_end - unused if r4 is 0
985 * r6: Start of command line string
986 * r7: End of command line string
988 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
989 if (!of_flat_tree) /* no device tree; boot old style */
991 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
992 /* does not return */
994 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
996 * Linux Kernel Parameters (passing device tree):
997 * r3: ptr to OF flat tree, followed by the board info data
998 * r4: physical pointer to the kernel itself
1003 #if defined(CONFIG_OF_FLAT_TREE)
1004 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
1005 /* ft_dump_blob(of_flat_tree); */
1008 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
1011 #endif /* CONFIG_PPC */
1014 do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1015 int argc, char *argv[],
1020 image_header_t *hdr = &header;
1022 void (*loader)(bd_t *, image_header_t *, char *, char *);
1023 image_header_t *img_addr;
1029 * Booting a (NetBSD) kernel image
1031 * This process is pretty similar to a standalone application:
1032 * The (first part of an multi-) image must be a stage-2 loader,
1033 * which in turn is responsible for loading & invoking the actual
1034 * kernel. The only differences are the parameters being passed:
1035 * besides the board info strucure, the loader expects a command
1036 * line, the name of the console device, and (optionally) the
1037 * address of the original image header.
1041 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
1042 img_addr = (image_header_t *) addr;
1046 #if defined (CONFIG_8xx_CONS_SMC1)
1048 #elif defined (CONFIG_8xx_CONS_SMC2)
1050 #elif defined (CONFIG_8xx_CONS_SCC2)
1052 #elif defined (CONFIG_8xx_CONS_SCC3)
1060 for (i=2, len=0 ; i<argc ; i+=1)
1061 len += strlen (argv[i]) + 1;
1062 cmdline = malloc (len);
1064 for (i=2, len=0 ; i<argc ; i+=1) {
1066 cmdline[len++] = ' ';
1067 strcpy (&cmdline[len], argv[i]);
1068 len += strlen (argv[i]);
1070 } else if ((cmdline = getenv("bootargs")) == NULL) {
1074 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
1076 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1079 SHOW_BOOT_PROGRESS (15);
1082 * NetBSD Stage-2 Loader Parameters:
1083 * r3: ptr to board info data
1085 * r5: console device
1086 * r6: boot args string
1088 (*loader) (gd->bd, img_addr, consdev, cmdline);
1091 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1093 /* Function that returns a character from the environment */
1094 extern uchar (*env_get_char)(int);
1097 do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1098 int argc, char *argv[],
1106 int i, j, nxt, len, envno, envsz;
1108 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1109 image_header_t *hdr = &header;
1112 * Booting an ARTOS kernel image + application
1115 /* this used to be the top of memory, but was wrong... */
1117 /* get stack pointer */
1118 asm volatile ("mr %0,1" : "=r"(top) );
1120 debug ("## Current stack ends at 0x%08lX ", top);
1122 top -= 2048; /* just to be sure */
1123 if (top > CFG_BOOTMAPSZ)
1124 top = CFG_BOOTMAPSZ;
1127 debug ("=> set upper limit to 0x%08lX\n", top);
1129 /* first check the artos specific boot args, then the linux args*/
1130 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1133 /* get length of cmdline, and place it */
1135 top = (top - (len + 1)) & ~0xF;
1136 cmdline = (char *)top;
1137 debug ("## cmdline at 0x%08lX ", top);
1141 top = (top - sizeof(bd_t)) & ~0xF;
1142 debug ("## bd at 0x%08lX ", top);
1144 memcpy(kbd, gd->bd, sizeof(bd_t));
1146 /* first find number of env entries, and their size */
1149 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1150 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1153 envsz += (nxt - i) + 1; /* plus trailing zero */
1155 envno++; /* plus the terminating zero */
1156 debug ("## %u envvars total size %u ", envno, envsz);
1158 top = (top - sizeof(char **)*envno) & ~0xF;
1159 fwenv = (char **)top;
1160 debug ("## fwenv at 0x%08lX ", top);
1162 top = (top - envsz) & ~0xF;
1167 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1168 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1171 for (j = i; j < nxt; ++j)
1172 *s++ = env_get_char(j);
1175 *ss++ = NULL; /* terminate */
1177 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
1178 (*entry)(kbd, cmdline, fwenv, top);
1183 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1184 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1187 #ifndef CFG_HUSH_PARSER
1188 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1190 if (parse_string_outer(getenv("bootcmd"),
1191 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1197 boot, 1, 1, do_bootd,
1198 "boot - boot default, i.e., run 'bootcmd'\n",
1202 /* keep old command name "bootd" for backward compatibility */
1204 bootd, 1, 1, do_bootd,
1205 "bootd - boot default, i.e., run 'bootcmd'\n",
1211 #if (CONFIG_COMMANDS & CFG_CMD_IMI)
1212 int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1219 return image_info (load_addr);
1222 for (arg=1; arg <argc; ++arg) {
1223 addr = simple_strtoul(argv[arg], NULL, 16);
1224 if (image_info (addr) != 0) rcode = 1;
1229 static int image_info (ulong addr)
1231 ulong data, len, checksum;
1232 image_header_t *hdr = &header;
1234 printf ("\n## Checking Image at %08lx ...\n", addr);
1236 /* Copy header so we can blank CRC field for re-calculation */
1237 memmove (&header, (char *)addr, sizeof(image_header_t));
1239 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
1240 puts (" Bad Magic Number\n");
1244 data = (ulong)&header;
1245 len = sizeof(image_header_t);
1247 checksum = ntohl(hdr->ih_hcrc);
1250 if (crc32 (0, (uchar *)data, len) != checksum) {
1251 puts (" Bad Header Checksum\n");
1255 /* for multi-file images we need the data part, too */
1256 print_image_hdr ((image_header_t *)addr);
1258 data = addr + sizeof(image_header_t);
1259 len = ntohl(hdr->ih_size);
1261 puts (" Verifying Checksum ... ");
1262 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
1263 puts (" Bad Data CRC\n");
1271 iminfo, CFG_MAXARGS, 1, do_iminfo,
1272 "iminfo - print header information for application image\n",
1274 " - print header information for application image starting at\n"
1275 " address 'addr' in memory; this includes verification of the\n"
1276 " image contents (magic number, header and payload checksums)\n"
1279 #endif /* CFG_CMD_IMI */
1281 #if (CONFIG_COMMANDS & CFG_CMD_IMLS)
1282 /*-----------------------------------------------------------------------
1283 * List all images found in flash.
1285 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1289 image_header_t *hdr;
1290 ulong data, len, checksum;
1292 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1293 if (info->flash_id == FLASH_UNKNOWN)
1295 for (j=0; j<info->sector_count; ++j) {
1297 if (!(hdr=(image_header_t *)info->start[j]) ||
1298 (ntohl(hdr->ih_magic) != IH_MAGIC))
1301 /* Copy header so we can blank CRC field for re-calculation */
1302 memmove (&header, (char *)hdr, sizeof(image_header_t));
1304 checksum = ntohl(header.ih_hcrc);
1307 if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
1311 printf ("Image at %08lX:\n", (ulong)hdr);
1312 print_image_hdr( hdr );
1314 data = (ulong)hdr + sizeof(image_header_t);
1315 len = ntohl(hdr->ih_size);
1317 puts (" Verifying Checksum ... ");
1318 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
1319 puts (" Bad Data CRC\n");
1331 imls, 1, 1, do_imls,
1332 "imls - list all images found in flash\n",
1334 " - Prints information about all images found at sector\n"
1335 " boundaries in flash.\n"
1337 #endif /* CFG_CMD_IMLS */
1340 print_image_hdr (image_header_t *hdr)
1342 #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1343 time_t timestamp = (time_t)ntohl(hdr->ih_time);
1347 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
1348 #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1349 to_tm (timestamp, &tm);
1350 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1351 tm.tm_year, tm.tm_mon, tm.tm_mday,
1352 tm.tm_hour, tm.tm_min, tm.tm_sec);
1353 #endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
1354 puts (" Image Type: "); print_type(hdr);
1355 printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
1356 print_size (ntohl(hdr->ih_size), "\n");
1357 printf (" Load Address: %08x\n"
1358 " Entry Point: %08x\n",
1359 ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
1361 if (hdr->ih_type == IH_TYPE_MULTI) {
1364 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1366 puts (" Contents:\n");
1367 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1368 printf (" Image %d: %8ld Bytes = ", i, len);
1369 print_size (len, "\n");
1376 print_type (image_header_t *hdr)
1378 char *os, *arch, *type, *comp;
1380 switch (hdr->ih_os) {
1381 case IH_OS_INVALID: os = "Invalid OS"; break;
1382 case IH_OS_NETBSD: os = "NetBSD"; break;
1383 case IH_OS_LINUX: os = "Linux"; break;
1384 case IH_OS_VXWORKS: os = "VxWorks"; break;
1385 case IH_OS_QNX: os = "QNX"; break;
1386 case IH_OS_U_BOOT: os = "U-Boot"; break;
1387 case IH_OS_RTEMS: os = "RTEMS"; break;
1389 case IH_OS_ARTOS: os = "ARTOS"; break;
1391 #ifdef CONFIG_LYNXKDI
1392 case IH_OS_LYNXOS: os = "LynxOS"; break;
1394 default: os = "Unknown OS"; break;
1397 switch (hdr->ih_arch) {
1398 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1399 case IH_CPU_ALPHA: arch = "Alpha"; break;
1400 case IH_CPU_ARM: arch = "ARM"; break;
1401 case IH_CPU_AVR32: arch = "AVR32"; break;
1402 case IH_CPU_BLACKFIN: arch = "Blackfin"; break;
1403 case IH_CPU_I386: arch = "Intel x86"; break;
1404 case IH_CPU_IA64: arch = "IA64"; break;
1405 case IH_CPU_M68K: arch = "M68K"; break;
1406 case IH_CPU_MICROBLAZE: arch = "Microblaze"; break;
1407 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
1408 case IH_CPU_MIPS: arch = "MIPS"; break;
1409 case IH_CPU_NIOS2: arch = "Nios-II"; break;
1410 case IH_CPU_NIOS: arch = "Nios"; break;
1411 case IH_CPU_PPC: arch = "PowerPC"; break;
1412 case IH_CPU_S390: arch = "IBM S390"; break;
1413 case IH_CPU_SH: arch = "SuperH"; break;
1414 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
1415 case IH_CPU_SPARC: arch = "SPARC"; break;
1416 default: arch = "Unknown Architecture"; break;
1419 switch (hdr->ih_type) {
1420 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1421 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1422 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1423 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1424 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1425 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1426 case IH_TYPE_SCRIPT: type = "Script"; break;
1427 case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
1428 default: type = "Unknown Image"; break;
1431 switch (hdr->ih_comp) {
1432 case IH_COMP_NONE: comp = "uncompressed"; break;
1433 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1434 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1435 default: comp = "unknown compression"; break;
1438 printf ("%s %s %s (%s)", arch, os, type, comp);
1441 #define ZALLOC_ALIGNMENT 16
1443 static void *zalloc(void *x, unsigned items, unsigned size)
1448 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1455 static void zfree(void *x, void *addr, unsigned nb)
1461 #define EXTRA_FIELD 4
1463 #define COMMENT 0x10
1464 #define RESERVED 0xe0
1468 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
1476 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
1477 puts ("Error: Bad gzipped data\n");
1480 if ((flags & EXTRA_FIELD) != 0)
1481 i = 12 + src[10] + (src[11] << 8);
1482 if ((flags & ORIG_NAME) != 0)
1483 while (src[i++] != 0)
1485 if ((flags & COMMENT) != 0)
1486 while (src[i++] != 0)
1488 if ((flags & HEAD_CRC) != 0)
1491 puts ("Error: gunzip out of data in header\n");
1497 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1498 s.outcb = (cb_func)WATCHDOG_RESET;
1501 #endif /* CONFIG_HW_WATCHDOG */
1503 r = inflateInit2(&s, -MAX_WBITS);
1505 printf ("Error: inflateInit2() returned %d\n", r);
1508 s.next_in = src + i;
1509 s.avail_in = *lenp - i;
1511 s.avail_out = dstlen;
1512 r = inflate(&s, Z_FINISH);
1513 if (r != Z_OK && r != Z_STREAM_END) {
1514 printf ("Error: inflate() returned %d\n", r);
1517 *lenp = s.next_out - (unsigned char *) dst;
1524 void bz_internal_error(int errcode)
1526 printf ("BZIP2 internal error %d\n", errcode);
1528 #endif /* CONFIG_BZIP2 */
1531 do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1532 ulong addr, ulong *len_ptr, int verify)
1534 image_header_t *hdr = &header;
1535 void (*entry_point)(bd_t *);
1537 entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
1539 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1540 (ulong)entry_point);
1542 SHOW_BOOT_PROGRESS (15);
1546 * r3: ptr to board info data
1549 (*entry_point ) ( gd->bd );
1552 #if (CONFIG_COMMANDS & CFG_CMD_ELF)
1554 do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1555 ulong addr, ulong *len_ptr, int verify)
1557 image_header_t *hdr = &header;
1560 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
1561 setenv("loadaddr", str);
1562 do_bootvx(cmdtp, 0, 0, NULL);
1566 do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1567 ulong addr, ulong *len_ptr, int verify)
1569 image_header_t *hdr = &header;
1570 char *local_args[2];
1573 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
1574 local_args[0] = argv[0];
1575 local_args[1] = str; /* and provide it via the arguments */
1576 do_bootelf(cmdtp, 0, 2, local_args);
1578 #endif /* CFG_CMD_ELF */
1580 #ifdef CONFIG_LYNXKDI
1582 do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1583 int argc, char *argv[],
1588 lynxkdi_boot( &header );
1591 #endif /* CONFIG_LYNXKDI */