Merge with git://www.denx.de/git/u-boot.git
[platform/kernel/u-boot.git] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Boot support
26  */
27 #include <common.h>
28 #include <watchdog.h>
29 #include <command.h>
30 #include <image.h>
31 #include <malloc.h>
32 #include <zlib.h>
33 #include <bzlib.h>
34 #include <environment.h>
35 #include <asm/byteorder.h>
36
37 #if defined(CONFIG_OF_LIBFDT)
38 #include <fdt.h>
39 #include <libfdt.h>
40 #include <fdt_support.h>
41 #endif
42 #if defined(CONFIG_OF_FLAT_TREE)
43 #include <ft_build.h>
44 #endif
45
46 DECLARE_GLOBAL_DATA_PTR;
47
48 /*cmd_boot.c*/
49 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50
51 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
52 #include <rtc.h>
53 #endif
54
55 #ifdef CFG_HUSH_PARSER
56 #include <hush.h>
57 #endif
58
59 #ifdef CFG_INIT_RAM_LOCK
60 #include <asm/cache.h>
61 #endif
62
63 #ifdef CONFIG_LOGBUFFER
64 #include <logbuff.h>
65 #endif
66
67 #ifdef CONFIG_HAS_DATAFLASH
68 #include <dataflash.h>
69 #endif
70
71 /*
72  * Some systems (for example LWMON) have very short watchdog periods;
73  * we must make sure to split long operations like memmove() or
74  * crc32() into reasonable chunks.
75  */
76 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
77 # define CHUNKSZ (64 * 1024)
78 #endif
79
80 int  gunzip (void *, int, unsigned char *, unsigned long *);
81
82 static void *zalloc(void *, unsigned, unsigned);
83 static void zfree(void *, void *, unsigned);
84
85 #if defined(CONFIG_CMD_IMI)
86 static int image_info (unsigned long addr);
87 #endif
88
89 #if defined(CONFIG_CMD_IMLS)
90 #include <flash.h>
91 extern flash_info_t flash_info[]; /* info for FLASH chips */
92 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
93 #endif
94
95 static void print_type (image_header_t *hdr);
96
97 #ifdef __I386__
98 image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
99 #endif
100
101 /*
102  *  Continue booting an OS image; caller already has:
103  *  - copied image header to global variable `header'
104  *  - checked header magic number, checksums (both header & image),
105  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
106  *  - loaded (first part of) image to header load address,
107  *  - disabled interrupts.
108  */
109 typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
110                           int   argc, char *argv[],
111                           ulong addr,           /* of image to boot */
112                           ulong *len_ptr,       /* multi-file image length table */
113                           int   verify);        /* getenv("verify")[0] != 'n' */
114
115 #ifdef  DEBUG
116 extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
117 #endif
118
119 #ifdef CONFIG_PPC
120 static boot_os_Fcn do_bootm_linux;
121 #else
122 extern boot_os_Fcn do_bootm_linux;
123 #endif
124 #ifdef CONFIG_SILENT_CONSOLE
125 static void fixup_silent_linux (void);
126 #endif
127 static boot_os_Fcn do_bootm_netbsd;
128 static boot_os_Fcn do_bootm_rtems;
129 #if defined(CONFIG_CMD_ELF)
130 static boot_os_Fcn do_bootm_vxworks;
131 static boot_os_Fcn do_bootm_qnxelf;
132 int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
133 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
134 #endif
135 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
136 static boot_os_Fcn do_bootm_artos;
137 #endif
138 #ifdef CONFIG_LYNXKDI
139 static boot_os_Fcn do_bootm_lynxkdi;
140 extern void lynxkdi_boot( image_header_t * );
141 #endif
142
143 #ifndef CFG_BOOTM_LEN
144 #define CFG_BOOTM_LEN   0x800000        /* use 8MByte as default max gunzip size */
145 #endif
146
147 image_header_t header;
148
149 ulong load_addr = CFG_LOAD_ADDR;                /* Default Load Address */
150
151 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
152 {
153         ulong   iflag;
154         ulong   addr;
155         ulong   data, len, checksum;
156         ulong  *len_ptr;
157         uint    unc_len = CFG_BOOTM_LEN;
158         int     i, verify;
159         char    *name, *s;
160         int     (*appl)(int, char *[]);
161         image_header_t *hdr = &header;
162
163         s = getenv ("verify");
164         verify = (s && (*s == 'n')) ? 0 : 1;
165
166         if (argc < 2) {
167                 addr = load_addr;
168         } else {
169                 addr = simple_strtoul(argv[1], NULL, 16);
170         }
171
172         show_boot_progress (1);
173         printf ("## Booting image at %08lx ...\n", addr);
174
175         /* Copy header so we can blank CRC field for re-calculation */
176 #ifdef CONFIG_HAS_DATAFLASH
177         if (addr_dataflash(addr)){
178                 read_dataflash(addr, sizeof(image_header_t), (char *)&header);
179         } else
180 #endif
181         memmove (&header, (char *)addr, sizeof(image_header_t));
182
183         if (ntohl(hdr->ih_magic) != IH_MAGIC) {
184 #ifdef __I386__ /* correct image format not implemented yet - fake it */
185                 if (fake_header(hdr, (void*)addr, -1) != NULL) {
186                         /* to compensate for the addition below */
187                         addr -= sizeof(image_header_t);
188                         /* turnof verify,
189                          * fake_header() does not fake the data crc
190                          */
191                         verify = 0;
192                 } else
193 #endif  /* __I386__ */
194             {
195                 puts ("Bad Magic Number\n");
196                 show_boot_progress (-1);
197                 return 1;
198             }
199         }
200         show_boot_progress (2);
201
202         data = (ulong)&header;
203         len  = sizeof(image_header_t);
204
205         checksum = ntohl(hdr->ih_hcrc);
206         hdr->ih_hcrc = 0;
207
208         if (crc32 (0, (uchar *)data, len) != checksum) {
209                 puts ("Bad Header Checksum\n");
210                 show_boot_progress (-2);
211                 return 1;
212         }
213         show_boot_progress (3);
214
215 #ifdef CONFIG_HAS_DATAFLASH
216         if (addr_dataflash(addr)){
217                 len  = ntohl(hdr->ih_size) + sizeof(image_header_t);
218                 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
219                 addr = CFG_LOAD_ADDR;
220         }
221 #endif
222
223
224         /* for multi-file images we need the data part, too */
225         print_image_hdr ((image_header_t *)addr);
226
227         data = addr + sizeof(image_header_t);
228         len  = ntohl(hdr->ih_size);
229
230         if (verify) {
231                 puts ("   Verifying Checksum ... ");
232                 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
233                         printf ("Bad Data CRC\n");
234                         show_boot_progress (-3);
235                         return 1;
236                 }
237                 puts ("OK\n");
238         }
239         show_boot_progress (4);
240
241         len_ptr = (ulong *)data;
242
243 #if defined(__ARM__)
244         if (hdr->ih_arch != IH_CPU_ARM)
245 #elif defined(__avr32__)
246         if (hdr->ih_arch != IH_CPU_AVR32)
247 #elif defined(__bfin__)
248         if (hdr->ih_arch != IH_CPU_BLACKFIN)
249 #elif defined(__I386__)
250         if (hdr->ih_arch != IH_CPU_I386)
251 #elif defined(__M68K__)
252         if (hdr->ih_arch != IH_CPU_M68K)
253 #elif defined(__microblaze__)
254         if (hdr->ih_arch != IH_CPU_MICROBLAZE)
255 #elif defined(__mips__)
256         if (hdr->ih_arch != IH_CPU_MIPS)
257 #elif defined(__nios__)
258         if (hdr->ih_arch != IH_CPU_NIOS)
259 #elif defined(__nios2__)
260         if (hdr->ih_arch != IH_CPU_NIOS2)
261 #elif defined(__PPC__)
262         if (hdr->ih_arch != IH_CPU_PPC)
263 #else
264 # error Unknown CPU type
265 #endif
266         {
267                 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
268                 show_boot_progress (-4);
269                 return 1;
270         }
271         show_boot_progress (5);
272
273         switch (hdr->ih_type) {
274         case IH_TYPE_STANDALONE:
275                 name = "Standalone Application";
276                 /* A second argument overwrites the load address */
277                 if (argc > 2) {
278                         hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
279                 }
280                 break;
281         case IH_TYPE_KERNEL:
282                 name = "Kernel Image";
283                 break;
284         case IH_TYPE_MULTI:
285                 name = "Multi-File Image";
286                 len  = ntohl(len_ptr[0]);
287                 /* OS kernel is always the first image */
288                 data += 8; /* kernel_len + terminator */
289                 for (i=1; len_ptr[i]; ++i)
290                         data += 4;
291                 break;
292         default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
293                 show_boot_progress (-5);
294                 return 1;
295         }
296         show_boot_progress (6);
297
298         /*
299          * We have reached the point of no return: we are going to
300          * overwrite all exception vector code, so we cannot easily
301          * recover from any failures any more...
302          */
303
304         iflag = disable_interrupts();
305
306 #ifdef CONFIG_AMIGAONEG3SE
307         /*
308          * We've possible left the caches enabled during
309          * bios emulation, so turn them off again
310          */
311         icache_disable();
312         invalidate_l1_instruction_cache();
313         flush_data_cache();
314         dcache_disable();
315 #endif
316
317         switch (hdr->ih_comp) {
318         case IH_COMP_NONE:
319                 if(ntohl(hdr->ih_load) == addr) {
320                         printf ("   XIP %s ... ", name);
321                 } else {
322 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
323                         size_t l = len;
324                         void *to = (void *)ntohl(hdr->ih_load);
325                         void *from = (void *)data;
326
327                         printf ("   Loading %s ... ", name);
328
329                         while (l > 0) {
330                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
331                                 WATCHDOG_RESET();
332                                 memmove (to, from, tail);
333                                 to += tail;
334                                 from += tail;
335                                 l -= tail;
336                         }
337 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
338                         memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
339 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
340                 }
341                 break;
342         case IH_COMP_GZIP:
343                 printf ("   Uncompressing %s ... ", name);
344                 if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
345                             (uchar *)data, &len) != 0) {
346                         puts ("GUNZIP ERROR - must RESET board to recover\n");
347                         show_boot_progress (-6);
348                         do_reset (cmdtp, flag, argc, argv);
349                 }
350                 break;
351 #ifdef CONFIG_BZIP2
352         case IH_COMP_BZIP2:
353                 printf ("   Uncompressing %s ... ", name);
354                 /*
355                  * If we've got less than 4 MB of malloc() space,
356                  * use slower decompression algorithm which requires
357                  * at most 2300 KB of memory.
358                  */
359                 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
360                                                 &unc_len, (char *)data, len,
361                                                 CFG_MALLOC_LEN < (4096 * 1024), 0);
362                 if (i != BZ_OK) {
363                         printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
364                         show_boot_progress (-6);
365                         do_reset (cmdtp, flag, argc, argv);
366                 }
367                 break;
368 #endif /* CONFIG_BZIP2 */
369         default:
370                 if (iflag)
371                         enable_interrupts();
372                 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
373                 show_boot_progress (-7);
374                 return 1;
375         }
376         puts ("OK\n");
377         show_boot_progress (7);
378
379         switch (hdr->ih_type) {
380         case IH_TYPE_STANDALONE:
381                 if (iflag)
382                         enable_interrupts();
383
384                 /* load (and uncompress), but don't start if "autostart"
385                  * is set to "no"
386                  */
387                 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
388                         char buf[32];
389                         sprintf(buf, "%lX", len);
390                         setenv("filesize", buf);
391                         return 0;
392                 }
393                 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
394                 (*appl)(argc-1, &argv[1]);
395                 return 0;
396         case IH_TYPE_KERNEL:
397         case IH_TYPE_MULTI:
398                 /* handled below */
399                 break;
400         default:
401                 if (iflag)
402                         enable_interrupts();
403                 printf ("Can't boot image type %d\n", hdr->ih_type);
404                 show_boot_progress (-8);
405                 return 1;
406         }
407         show_boot_progress (8);
408
409         switch (hdr->ih_os) {
410         default:                        /* handled by (original) Linux case */
411         case IH_OS_LINUX:
412 #ifdef CONFIG_SILENT_CONSOLE
413             fixup_silent_linux();
414 #endif
415             do_bootm_linux  (cmdtp, flag, argc, argv,
416                              addr, len_ptr, verify);
417             break;
418         case IH_OS_NETBSD:
419             do_bootm_netbsd (cmdtp, flag, argc, argv,
420                              addr, len_ptr, verify);
421             break;
422
423 #ifdef CONFIG_LYNXKDI
424         case IH_OS_LYNXOS:
425             do_bootm_lynxkdi (cmdtp, flag, argc, argv,
426                              addr, len_ptr, verify);
427             break;
428 #endif
429
430         case IH_OS_RTEMS:
431             do_bootm_rtems (cmdtp, flag, argc, argv,
432                              addr, len_ptr, verify);
433             break;
434
435 #if defined(CONFIG_CMD_ELF)
436         case IH_OS_VXWORKS:
437             do_bootm_vxworks (cmdtp, flag, argc, argv,
438                               addr, len_ptr, verify);
439             break;
440         case IH_OS_QNX:
441             do_bootm_qnxelf (cmdtp, flag, argc, argv,
442                               addr, len_ptr, verify);
443             break;
444 #endif
445 #ifdef CONFIG_ARTOS
446         case IH_OS_ARTOS:
447             do_bootm_artos  (cmdtp, flag, argc, argv,
448                              addr, len_ptr, verify);
449             break;
450 #endif
451         }
452
453         show_boot_progress (-9);
454 #ifdef DEBUG
455         puts ("\n## Control returned to monitor - resetting...\n");
456         do_reset (cmdtp, flag, argc, argv);
457 #endif
458         return 1;
459 }
460
461 U_BOOT_CMD(
462         bootm,  CFG_MAXARGS,    1,      do_bootm,
463         "bootm   - boot application image from memory\n",
464         "[addr [arg ...]]\n    - boot application image stored in memory\n"
465         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
466         "\t'arg' can be the address of an initrd image\n"
467 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
468         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
469         "\ta third argument is required which is the address of the of the\n"
470         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
471         "\tuse a '-' for the second argument. If you do not pass a third\n"
472         "\ta bd_info struct will be passed instead\n"
473 #endif
474 );
475
476 #ifdef CONFIG_SILENT_CONSOLE
477 static void
478 fixup_silent_linux ()
479 {
480         char buf[256], *start, *end;
481         char *cmdline = getenv ("bootargs");
482
483         /* Only fix cmdline when requested */
484         if (!(gd->flags & GD_FLG_SILENT))
485                 return;
486
487         debug ("before silent fix-up: %s\n", cmdline);
488         if (cmdline) {
489                 if ((start = strstr (cmdline, "console=")) != NULL) {
490                         end = strchr (start, ' ');
491                         strncpy (buf, cmdline, (start - cmdline + 8));
492                         if (end)
493                                 strcpy (buf + (start - cmdline + 8), end);
494                         else
495                                 buf[start - cmdline + 8] = '\0';
496                 } else {
497                         strcpy (buf, cmdline);
498                         strcat (buf, " console=");
499                 }
500         } else {
501                 strcpy (buf, "console=");
502         }
503
504         setenv ("bootargs", buf);
505         debug ("after silent fix-up: %s\n", buf);
506 }
507 #endif /* CONFIG_SILENT_CONSOLE */
508
509 #ifdef CONFIG_PPC
510 static void  __attribute__((noinline))
511 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
512                 int     argc, char *argv[],
513                 ulong   addr,
514                 ulong   *len_ptr,
515                 int     verify)
516 {
517         ulong   sp;
518         ulong   len, checksum;
519         ulong   initrd_start, initrd_end;
520         ulong   cmd_start, cmd_end;
521         ulong   initrd_high;
522         ulong   data;
523         int     initrd_copy_to_ram = 1;
524         char    *cmdline;
525         char    *s;
526         bd_t    *kbd;
527         void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
528         image_header_t *hdr = &header;
529 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
530         char    *of_flat_tree = NULL;
531         ulong   of_data = 0;
532 #endif
533
534         if ((s = getenv ("initrd_high")) != NULL) {
535                 /* a value of "no" or a similar string will act like 0,
536                  * turning the "load high" feature off. This is intentional.
537                  */
538                 initrd_high = simple_strtoul(s, NULL, 16);
539                 if (initrd_high == ~0)
540                         initrd_copy_to_ram = 0;
541         } else {        /* not set, no restrictions to load high */
542                 initrd_high = ~0;
543         }
544
545 #ifdef CONFIG_LOGBUFFER
546         kbd=gd->bd;
547         /* Prevent initrd from overwriting logbuffer */
548         if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
549                 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
550         debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
551 #endif
552
553         /*
554          * Booting a (Linux) kernel image
555          *
556          * Allocate space for command line and board info - the
557          * address should be as high as possible within the reach of
558          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
559          * memory, which means far enough below the current stack
560          * pointer.
561          */
562
563         asm( "mr %0,1": "=r"(sp) : );
564
565         debug ("## Current stack ends at 0x%08lX ", sp);
566
567         sp -= 2048;             /* just to be sure */
568         if (sp > CFG_BOOTMAPSZ)
569                 sp = CFG_BOOTMAPSZ;
570         sp &= ~0xF;
571
572         debug ("=> set upper limit to 0x%08lX\n", sp);
573
574         cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
575         kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
576
577         if ((s = getenv("bootargs")) == NULL)
578                 s = "";
579
580         strcpy (cmdline, s);
581
582         cmd_start    = (ulong)&cmdline[0];
583         cmd_end      = cmd_start + strlen(cmdline);
584
585         *kbd = *(gd->bd);
586
587 #ifdef  DEBUG
588         printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
589
590         do_bdinfo (NULL, 0, 0, NULL);
591 #endif
592
593         if ((s = getenv ("clocks_in_mhz")) != NULL) {
594                 /* convert all clock information to MHz */
595                 kbd->bi_intfreq /= 1000000L;
596                 kbd->bi_busfreq /= 1000000L;
597 #if defined(CONFIG_MPC8220)
598         kbd->bi_inpfreq /= 1000000L;
599         kbd->bi_pcifreq /= 1000000L;
600         kbd->bi_pevfreq /= 1000000L;
601         kbd->bi_flbfreq /= 1000000L;
602         kbd->bi_vcofreq /= 1000000L;
603 #endif
604 #if defined(CONFIG_CPM2)
605                 kbd->bi_cpmfreq /= 1000000L;
606                 kbd->bi_brgfreq /= 1000000L;
607                 kbd->bi_sccfreq /= 1000000L;
608                 kbd->bi_vco     /= 1000000L;
609 #endif
610 #if defined(CONFIG_MPC5xxx)
611                 kbd->bi_ipbfreq /= 1000000L;
612                 kbd->bi_pcifreq /= 1000000L;
613 #endif /* CONFIG_MPC5xxx */
614         }
615
616         kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
617
618         /*
619          * Check if there is an initrd image
620          */
621
622 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
623         /* Look for a '-' which indicates to ignore the ramdisk argument */
624         if (argc >= 3 && strcmp(argv[2], "-") ==  0) {
625                         debug ("Skipping initrd\n");
626                         len = data = 0;
627                 }
628         else
629 #endif
630         if (argc >= 3) {
631                 debug ("Not skipping initrd\n");
632                 show_boot_progress (9);
633
634                 addr = simple_strtoul(argv[2], NULL, 16);
635
636                 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
637
638                 /* Copy header so we can blank CRC field for re-calculation */
639                 memmove (&header, (char *)addr, sizeof(image_header_t));
640
641                 if (ntohl(hdr->ih_magic)  != IH_MAGIC) {
642                         puts ("Bad Magic Number\n");
643                         show_boot_progress (-10);
644                         do_reset (cmdtp, flag, argc, argv);
645                 }
646
647                 data = (ulong)&header;
648                 len  = sizeof(image_header_t);
649
650                 checksum = ntohl(hdr->ih_hcrc);
651                 hdr->ih_hcrc = 0;
652
653                 if (crc32 (0, (uchar *)data, len) != checksum) {
654                         puts ("Bad Header Checksum\n");
655                         show_boot_progress (-11);
656                         do_reset (cmdtp, flag, argc, argv);
657                 }
658
659                 show_boot_progress (10);
660
661                 print_image_hdr (hdr);
662
663                 data = addr + sizeof(image_header_t);
664                 len  = ntohl(hdr->ih_size);
665
666                 if (verify) {
667                         ulong csum = 0;
668 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
669                         ulong cdata = data, edata = cdata + len;
670 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
671
672                         puts ("   Verifying Checksum ... ");
673
674 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
675
676                         while (cdata < edata) {
677                                 ulong chunk = edata - cdata;
678
679                                 if (chunk > CHUNKSZ)
680                                         chunk = CHUNKSZ;
681                                 csum = crc32 (csum, (uchar *)cdata, chunk);
682                                 cdata += chunk;
683
684                                 WATCHDOG_RESET();
685                         }
686 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
687                         csum = crc32 (0, (uchar *)data, len);
688 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
689
690                         if (csum != ntohl(hdr->ih_dcrc)) {
691                                 puts ("Bad Data CRC\n");
692                                 show_boot_progress (-12);
693                                 do_reset (cmdtp, flag, argc, argv);
694                         }
695                         puts ("OK\n");
696                 }
697
698                 show_boot_progress (11);
699
700                 if ((hdr->ih_os   != IH_OS_LINUX)       ||
701                     (hdr->ih_arch != IH_CPU_PPC)        ||
702                     (hdr->ih_type != IH_TYPE_RAMDISK)   ) {
703                         puts ("No Linux PPC Ramdisk Image\n");
704                         show_boot_progress (-13);
705                         do_reset (cmdtp, flag, argc, argv);
706                 }
707
708                 /*
709                  * Now check if we have a multifile image
710                  */
711         } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
712                 u_long tail    = ntohl(len_ptr[0]) % 4;
713                 int i;
714
715                 show_boot_progress (13);
716
717                 /* skip kernel length and terminator */
718                 data = (ulong)(&len_ptr[2]);
719                 /* skip any additional image length fields */
720                 for (i=1; len_ptr[i]; ++i)
721                         data += 4;
722                 /* add kernel length, and align */
723                 data += ntohl(len_ptr[0]);
724                 if (tail) {
725                         data += 4 - tail;
726                 }
727
728                 len   = ntohl(len_ptr[1]);
729
730         } else {
731                 /*
732                  * no initrd image
733                  */
734                 show_boot_progress (14);
735
736                 len = data = 0;
737         }
738
739 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
740         if(argc > 3) {
741                 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
742                 hdr = (image_header_t *)of_flat_tree;
743 #if defined(CONFIG_OF_FLAT_TREE)
744                 if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
745 #else
746                 if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) != 0) {
747 #endif
748 #ifndef CFG_NO_FLASH
749                         if (addr2info((ulong)of_flat_tree) != NULL)
750                                 of_data = (ulong)of_flat_tree;
751 #endif
752                 } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
753                         printf("## Flat Device Tree at %08lX\n", hdr);
754                         print_image_hdr(hdr);
755
756                         if ((ntohl(hdr->ih_load) <  ((unsigned long)hdr + ntohl(hdr->ih_size) + sizeof(hdr))) &&
757                            ((ntohl(hdr->ih_load) + ntohl(hdr->ih_size)) > (unsigned long)hdr)) {
758                                 puts ("ERROR: fdt overwritten - "
759                                         "must RESET the board to recover.\n");
760                                 do_reset (cmdtp, flag, argc, argv);
761                         }
762
763                         puts ("   Verifying Checksum ... ");
764                         memmove (&header, (char *)hdr, sizeof(image_header_t));
765                         checksum = ntohl(header.ih_hcrc);
766                         header.ih_hcrc = 0;
767
768                         if(checksum != crc32(0, (uchar *)&header, sizeof(image_header_t))) {
769                                 puts ("ERROR: fdt header checksum invalid - "
770                                         "must RESET the board to recover.\n");
771                                 do_reset (cmdtp, flag, argc, argv);
772                         }
773
774                         checksum = ntohl(hdr->ih_dcrc);
775                         addr = (ulong)((uchar *)(hdr) + sizeof(image_header_t));
776
777                         if(checksum != crc32(0, (uchar *)addr, ntohl(hdr->ih_size))) {
778                                 puts ("ERROR: fdt checksum invalid - "
779                                         "must RESET the board to recover.\n");
780                                 do_reset (cmdtp, flag, argc, argv);
781                         }
782                         puts ("OK\n");
783
784                         if (ntohl(hdr->ih_type) != IH_TYPE_FLATDT) {
785                                 puts ("ERROR: uImage is not a fdt - "
786                                         "must RESET the board to recover.\n");
787                                 do_reset (cmdtp, flag, argc, argv);
788                         }
789                         if (ntohl(hdr->ih_comp) != IH_COMP_NONE) {
790                                 puts ("ERROR: uImage is compressed - "
791                                         "must RESET the board to recover.\n");
792                                 do_reset (cmdtp, flag, argc, argv);
793                         }
794 #if defined(CONFIG_OF_FLAT_TREE)
795                         if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
796 #else
797                         if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) != 0) {
798 #endif
799                                 puts ("ERROR: uImage data is not a fdt - "
800                                         "must RESET the board to recover.\n");
801                                 do_reset (cmdtp, flag, argc, argv);
802                         }
803
804                         memmove((void *)ntohl(hdr->ih_load),
805                                 (void *)(of_flat_tree + sizeof(image_header_t)),
806                                 ntohl(hdr->ih_size));
807                         of_flat_tree = (char *)ntohl(hdr->ih_load);
808                 } else {
809                         puts ("Did not find a flat Flat Device Tree.\n"
810                                 "Must RESET the board to recover.\n");
811                         do_reset (cmdtp, flag, argc, argv);
812                 }
813                 printf ("   Booting using the fdt at 0x%x\n",
814                                 of_flat_tree);
815         } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
816                 u_long tail    = ntohl(len_ptr[0]) % 4;
817                 int i;
818
819                 /* skip kernel length, initrd length, and terminator */
820                 of_data = (ulong)(&len_ptr[3]);
821                 /* skip any additional image length fields */
822                 for (i=2; len_ptr[i]; ++i)
823                         of_data += 4;
824                 /* add kernel length, and align */
825                 of_data += ntohl(len_ptr[0]);
826                 if (tail) {
827                         of_data += 4 - tail;
828                 }
829
830                 /* add initrd length, and align */
831                 tail = ntohl(len_ptr[1]) % 4;
832                 of_data += ntohl(len_ptr[1]);
833                 if (tail) {
834                         of_data += 4 - tail;
835                 }
836
837 #if defined(CONFIG_OF_FLAT_TREE)
838                 if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
839 #else
840                 if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) != 0) {
841 #endif
842                         puts ("ERROR: image is not a fdt - "
843                                 "must RESET the board to recover.\n");
844                         do_reset (cmdtp, flag, argc, argv);
845                 }
846
847 #if defined(CONFIG_OF_FLAT_TREE)
848                 if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
849 #else
850                 if (be32_to_cpu(fdt_totalsize(of_data)) !=  ntohl(len_ptr[2])) {
851 #endif
852                         puts ("ERROR: fdt size != image size - "
853                                 "must RESET the board to recover.\n");
854                         do_reset (cmdtp, flag, argc, argv);
855                 }
856         }
857 #endif
858         if (!data) {
859                 debug ("No initrd\n");
860         }
861
862         if (data) {
863             if (!initrd_copy_to_ram) {  /* zero-copy ramdisk support */
864                 initrd_start = data;
865                 initrd_end = initrd_start + len;
866             } else {
867                 initrd_start  = (ulong)kbd - len;
868                 initrd_start &= ~(4096 - 1);    /* align on page */
869
870                 if (initrd_high) {
871                         ulong nsp;
872
873                         /*
874                          * the inital ramdisk does not need to be within
875                          * CFG_BOOTMAPSZ as it is not accessed until after
876                          * the mm system is initialised.
877                          *
878                          * do the stack bottom calculation again and see if
879                          * the initrd will fit just below the monitor stack
880                          * bottom without overwriting the area allocated
881                          * above for command line args and board info.
882                          */
883                         asm( "mr %0,1": "=r"(nsp) : );
884                         nsp -= 2048;            /* just to be sure */
885                         nsp &= ~0xF;
886                         if (nsp > initrd_high)  /* limit as specified */
887                                 nsp = initrd_high;
888                         nsp -= len;
889                         nsp &= ~(4096 - 1);     /* align on page */
890                         if (nsp >= sp)
891                                 initrd_start = nsp;
892                 }
893
894                 show_boot_progress (12);
895
896                 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
897                         data, data + len - 1, len, len);
898
899                 initrd_end    = initrd_start + len;
900                 printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
901                         initrd_start, initrd_end);
902 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
903                 {
904                         size_t l = len;
905                         void *to = (void *)initrd_start;
906                         void *from = (void *)data;
907
908                         while (l > 0) {
909                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
910                                 WATCHDOG_RESET();
911                                 memmove (to, from, tail);
912                                 to += tail;
913                                 from += tail;
914                                 l -= tail;
915                         }
916                 }
917 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
918                 memmove ((void *)initrd_start, (void *)data, len);
919 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
920                 puts ("OK\n");
921             }
922         } else {
923                 initrd_start = 0;
924                 initrd_end = 0;
925         }
926
927 #if defined(CONFIG_OF_LIBFDT)
928
929 #ifdef CFG_BOOTMAPSZ
930         /*
931          * The blob must be within CFG_BOOTMAPSZ,
932          * so we flag it to be copied if it is not.
933          */
934         if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
935                 of_data = of_flat_tree;
936 #endif
937
938         /* move of_flat_tree if needed */
939         if (of_data) {
940                 int err;
941                 ulong of_start, of_len;
942
943                 of_len = be32_to_cpu(fdt_totalsize(of_data));
944
945                 /* position on a 4K boundary before the kbd */
946                 of_start  = (ulong)kbd - of_len;
947                 of_start &= ~(4096 - 1);        /* align on page */
948                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
949                         of_data, of_data + of_len - 1, of_len, of_len);
950
951                 of_flat_tree = (char *)of_start;
952                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
953                         of_start, of_start + of_len - 1);
954                 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
955                 if (err != 0) {
956                         puts ("ERROR: fdt move failed - "
957                                 "must RESET the board to recover.\n");
958                         do_reset (cmdtp, flag, argc, argv);
959                 }
960         }
961         /*
962          * Add the chosen node if it doesn't exist, add the env and bd_t
963          * if the user wants it (the logic is in the subroutines).
964          */
965         if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
966                 puts ("ERROR: /chosen node create failed - "
967                         "must RESET the board to recover.\n");
968                 do_reset (cmdtp, flag, argc, argv);
969         }
970 #ifdef CONFIG_OF_HAS_UBOOT_ENV
971         if (fdt_env(of_flat_tree) < 0) {
972                 puts ("ERROR: /u-boot-env node create failed - "
973                         "must RESET the board to recover.\n");
974                 do_reset (cmdtp, flag, argc, argv);
975         }
976 #endif
977 #ifdef CONFIG_OF_HAS_BD_T
978         if (fdt_bd_t(of_flat_tree) < 0) {
979                 puts ("ERROR: /bd_t node create failed - "
980                         "must RESET the board to recover.\n");
981                 do_reset (cmdtp, flag, argc, argv);
982         }
983 #endif
984 #ifdef CONFIG_OF_BOARD_SETUP
985         /* Call the board-specific fixup routine */
986         ft_board_setup(of_flat_tree, gd->bd);
987 #endif
988 #endif /* CONFIG_OF_LIBFDT */
989 #if defined(CONFIG_OF_FLAT_TREE)
990         /* move of_flat_tree if needed */
991         if (of_data) {
992                 ulong of_start, of_len;
993                 of_len = ((struct boot_param_header *)of_data)->totalsize;
994
995                 /* provide extra 8k pad */
996                 of_start  = (ulong)kbd - of_len - 8192;
997                 of_start &= ~(4096 - 1);        /* align on page */
998                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
999                         of_data, of_data + of_len - 1, of_len, of_len);
1000
1001                 of_flat_tree = (char *)of_start;
1002                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
1003                         of_start, of_start + of_len - 1);
1004                 memmove ((void *)of_start, (void *)of_data, of_len);
1005         }
1006         /*
1007          * Create the /chosen node and modify the blob with board specific
1008          * values as needed.
1009          */
1010         ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
1011         /* ft_dump_blob(of_flat_tree); */
1012 #endif
1013         debug ("## Transferring control to Linux (at address %08lx) ...\n",
1014                 (ulong)kernel);
1015
1016         show_boot_progress (15);
1017
1018 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
1019         unlock_ram_in_cache();
1020 #endif
1021
1022 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
1023         if (of_flat_tree) {     /* device tree; boot new style */
1024                 /*
1025                  * Linux Kernel Parameters (passing device tree):
1026                  *   r3: pointer to the fdt, followed by the board info data
1027                  *   r4: physical pointer to the kernel itself
1028                  *   r5: NULL
1029                  *   r6: NULL
1030                  *   r7: NULL
1031                  */
1032                 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
1033                 /* does not return */
1034         }
1035 #endif
1036         /*
1037          * Linux Kernel Parameters (passing board info data):
1038          *   r3: ptr to board info data
1039          *   r4: initrd_start or 0 if no initrd
1040          *   r5: initrd_end - unused if r4 is 0
1041          *   r6: Start of command line string
1042          *   r7: End   of command line string
1043          */
1044         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
1045         /* does not return */
1046 }
1047 #endif /* CONFIG_PPC */
1048
1049 static void
1050 do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1051                 int     argc, char *argv[],
1052                 ulong   addr,
1053                 ulong   *len_ptr,
1054                 int     verify)
1055 {
1056         image_header_t *hdr = &header;
1057
1058         void    (*loader)(bd_t *, image_header_t *, char *, char *);
1059         image_header_t *img_addr;
1060         char     *consdev;
1061         char     *cmdline;
1062
1063
1064         /*
1065          * Booting a (NetBSD) kernel image
1066          *
1067          * This process is pretty similar to a standalone application:
1068          * The (first part of an multi-) image must be a stage-2 loader,
1069          * which in turn is responsible for loading & invoking the actual
1070          * kernel.  The only differences are the parameters being passed:
1071          * besides the board info strucure, the loader expects a command
1072          * line, the name of the console device, and (optionally) the
1073          * address of the original image header.
1074          */
1075
1076         img_addr = 0;
1077         if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
1078                 img_addr = (image_header_t *) addr;
1079
1080
1081         consdev = "";
1082 #if   defined (CONFIG_8xx_CONS_SMC1)
1083         consdev = "smc1";
1084 #elif defined (CONFIG_8xx_CONS_SMC2)
1085         consdev = "smc2";
1086 #elif defined (CONFIG_8xx_CONS_SCC2)
1087         consdev = "scc2";
1088 #elif defined (CONFIG_8xx_CONS_SCC3)
1089         consdev = "scc3";
1090 #endif
1091
1092         if (argc > 2) {
1093                 ulong len;
1094                 int   i;
1095
1096                 for (i=2, len=0 ; i<argc ; i+=1)
1097                         len += strlen (argv[i]) + 1;
1098                 cmdline = malloc (len);
1099
1100                 for (i=2, len=0 ; i<argc ; i+=1) {
1101                         if (i > 2)
1102                                 cmdline[len++] = ' ';
1103                         strcpy (&cmdline[len], argv[i]);
1104                         len += strlen (argv[i]);
1105                 }
1106         } else if ((cmdline = getenv("bootargs")) == NULL) {
1107                 cmdline = "";
1108         }
1109
1110         loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
1111
1112         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1113                 (ulong)loader);
1114
1115         show_boot_progress (15);
1116
1117         /*
1118          * NetBSD Stage-2 Loader Parameters:
1119          *   r3: ptr to board info data
1120          *   r4: image address
1121          *   r5: console device
1122          *   r6: boot args string
1123          */
1124         (*loader) (gd->bd, img_addr, consdev, cmdline);
1125 }
1126
1127 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1128
1129 /* Function that returns a character from the environment */
1130 extern uchar (*env_get_char)(int);
1131
1132 static void
1133 do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1134                 int     argc, char *argv[],
1135                 ulong   addr,
1136                 ulong   *len_ptr,
1137                 int     verify)
1138 {
1139         ulong top;
1140         char *s, *cmdline;
1141         char **fwenv, **ss;
1142         int i, j, nxt, len, envno, envsz;
1143         bd_t *kbd;
1144         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1145         image_header_t *hdr = &header;
1146
1147         /*
1148          * Booting an ARTOS kernel image + application
1149          */
1150
1151         /* this used to be the top of memory, but was wrong... */
1152 #ifdef CONFIG_PPC
1153         /* get stack pointer */
1154         asm volatile ("mr %0,1" : "=r"(top) );
1155 #endif
1156         debug ("## Current stack ends at 0x%08lX ", top);
1157
1158         top -= 2048;            /* just to be sure */
1159         if (top > CFG_BOOTMAPSZ)
1160                 top = CFG_BOOTMAPSZ;
1161         top &= ~0xF;
1162
1163         debug ("=> set upper limit to 0x%08lX\n", top);
1164
1165         /* first check the artos specific boot args, then the linux args*/
1166         if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1167                 s = "";
1168
1169         /* get length of cmdline, and place it */
1170         len = strlen(s);
1171         top = (top - (len + 1)) & ~0xF;
1172         cmdline = (char *)top;
1173         debug ("## cmdline at 0x%08lX ", top);
1174         strcpy(cmdline, s);
1175
1176         /* copy bdinfo */
1177         top = (top - sizeof(bd_t)) & ~0xF;
1178         debug ("## bd at 0x%08lX ", top);
1179         kbd = (bd_t *)top;
1180         memcpy(kbd, gd->bd, sizeof(bd_t));
1181
1182         /* first find number of env entries, and their size */
1183         envno = 0;
1184         envsz = 0;
1185         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1186                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1187                         ;
1188                 envno++;
1189                 envsz += (nxt - i) + 1; /* plus trailing zero */
1190         }
1191         envno++;        /* plus the terminating zero */
1192         debug ("## %u envvars total size %u ", envno, envsz);
1193
1194         top = (top - sizeof(char **)*envno) & ~0xF;
1195         fwenv = (char **)top;
1196         debug ("## fwenv at 0x%08lX ", top);
1197
1198         top = (top - envsz) & ~0xF;
1199         s = (char *)top;
1200         ss = fwenv;
1201
1202         /* now copy them */
1203         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1204                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1205                         ;
1206                 *ss++ = s;
1207                 for (j = i; j < nxt; ++j)
1208                         *s++ = env_get_char(j);
1209                 *s++ = '\0';
1210         }
1211         *ss++ = NULL;   /* terminate */
1212
1213         entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
1214         (*entry)(kbd, cmdline, fwenv, top);
1215 }
1216 #endif
1217
1218
1219 #if defined(CONFIG_CMD_BOOTD)
1220 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1221 {
1222         int rcode = 0;
1223 #ifndef CFG_HUSH_PARSER
1224         if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1225 #else
1226         if (parse_string_outer(getenv("bootcmd"),
1227                 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1228 #endif
1229         return rcode;
1230 }
1231
1232 U_BOOT_CMD(
1233         boot,   1,      1,      do_bootd,
1234         "boot    - boot default, i.e., run 'bootcmd'\n",
1235         NULL
1236 );
1237
1238 /* keep old command name "bootd" for backward compatibility */
1239 U_BOOT_CMD(
1240         bootd, 1,       1,      do_bootd,
1241         "bootd   - boot default, i.e., run 'bootcmd'\n",
1242         NULL
1243 );
1244
1245 #endif
1246
1247 #if defined(CONFIG_CMD_IMI)
1248 int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1249 {
1250         int     arg;
1251         ulong   addr;
1252         int     rcode=0;
1253
1254         if (argc < 2) {
1255                 return image_info (load_addr);
1256         }
1257
1258         for (arg=1; arg <argc; ++arg) {
1259                 addr = simple_strtoul(argv[arg], NULL, 16);
1260                 if (image_info (addr) != 0) rcode = 1;
1261         }
1262         return rcode;
1263 }
1264
1265 static int image_info (ulong addr)
1266 {
1267         ulong   data, len, checksum;
1268         image_header_t *hdr = &header;
1269
1270         printf ("\n## Checking Image at %08lx ...\n", addr);
1271
1272         /* Copy header so we can blank CRC field for re-calculation */
1273         memmove (&header, (char *)addr, sizeof(image_header_t));
1274
1275         if (ntohl(hdr->ih_magic) != IH_MAGIC) {
1276                 puts ("   Bad Magic Number\n");
1277                 return 1;
1278         }
1279
1280         data = (ulong)&header;
1281         len  = sizeof(image_header_t);
1282
1283         checksum = ntohl(hdr->ih_hcrc);
1284         hdr->ih_hcrc = 0;
1285
1286         if (crc32 (0, (uchar *)data, len) != checksum) {
1287                 puts ("   Bad Header Checksum\n");
1288                 return 1;
1289         }
1290
1291         /* for multi-file images we need the data part, too */
1292         print_image_hdr ((image_header_t *)addr);
1293
1294         data = addr + sizeof(image_header_t);
1295         len  = ntohl(hdr->ih_size);
1296
1297         puts ("   Verifying Checksum ... ");
1298         if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
1299                 puts ("   Bad Data CRC\n");
1300                 return 1;
1301         }
1302         puts ("OK\n");
1303         return 0;
1304 }
1305
1306 U_BOOT_CMD(
1307         iminfo, CFG_MAXARGS,    1,      do_iminfo,
1308         "iminfo  - print header information for application image\n",
1309         "addr [addr ...]\n"
1310         "    - print header information for application image starting at\n"
1311         "      address 'addr' in memory; this includes verification of the\n"
1312         "      image contents (magic number, header and payload checksums)\n"
1313 );
1314
1315 #endif
1316
1317 #if defined(CONFIG_CMD_IMLS)
1318 /*-----------------------------------------------------------------------
1319  * List all images found in flash.
1320  */
1321 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1322 {
1323         flash_info_t *info;
1324         int i, j;
1325         image_header_t *hdr;
1326         ulong data, len, checksum;
1327
1328         for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1329                 if (info->flash_id == FLASH_UNKNOWN)
1330                         goto next_bank;
1331                 for (j=0; j<info->sector_count; ++j) {
1332
1333                         if (!(hdr=(image_header_t *)info->start[j]) ||
1334                             (ntohl(hdr->ih_magic) != IH_MAGIC))
1335                                 goto next_sector;
1336
1337                         /* Copy header so we can blank CRC field for re-calculation */
1338                         memmove (&header, (char *)hdr, sizeof(image_header_t));
1339
1340                         checksum = ntohl(header.ih_hcrc);
1341                         header.ih_hcrc = 0;
1342
1343                         if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
1344                             != checksum)
1345                                 goto next_sector;
1346
1347                         printf ("Image at %08lX:\n", (ulong)hdr);
1348                         print_image_hdr( hdr );
1349
1350                         data = (ulong)hdr + sizeof(image_header_t);
1351                         len  = ntohl(hdr->ih_size);
1352
1353                         puts ("   Verifying Checksum ... ");
1354                         if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
1355                                 puts ("   Bad Data CRC\n");
1356                         }
1357                         puts ("OK\n");
1358 next_sector:            ;
1359                 }
1360 next_bank:      ;
1361         }
1362
1363         return (0);
1364 }
1365
1366 U_BOOT_CMD(
1367         imls,   1,              1,      do_imls,
1368         "imls    - list all images found in flash\n",
1369         "\n"
1370         "    - Prints information about all images found at sector\n"
1371         "      boundaries in flash.\n"
1372 );
1373 #endif
1374
1375 void
1376 print_image_hdr (image_header_t *hdr)
1377 {
1378 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
1379         time_t timestamp = (time_t)ntohl(hdr->ih_time);
1380         struct rtc_time tm;
1381 #endif
1382
1383         printf ("   Image Name:   %.*s\n", IH_NMLEN, hdr->ih_name);
1384 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
1385         to_tm (timestamp, &tm);
1386         printf ("   Created:      %4d-%02d-%02d  %2d:%02d:%02d UTC\n",
1387                 tm.tm_year, tm.tm_mon, tm.tm_mday,
1388                 tm.tm_hour, tm.tm_min, tm.tm_sec);
1389 #endif
1390         puts ("   Image Type:   "); print_type(hdr);
1391         printf ("\n   Data Size:    %d Bytes = ", ntohl(hdr->ih_size));
1392         print_size (ntohl(hdr->ih_size), "\n");
1393         printf ("   Load Address: %08x\n"
1394                 "   Entry Point:  %08x\n",
1395                  ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
1396
1397         if (hdr->ih_type == IH_TYPE_MULTI) {
1398                 int i;
1399                 ulong len;
1400                 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1401
1402                 puts ("   Contents:\n");
1403                 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1404                         printf ("   Image %d: %8ld Bytes = ", i, len);
1405                         print_size (len, "\n");
1406                 }
1407         }
1408 }
1409
1410
1411 static void
1412 print_type (image_header_t *hdr)
1413 {
1414         char *os, *arch, *type, *comp;
1415
1416         switch (hdr->ih_os) {
1417         case IH_OS_INVALID:     os = "Invalid OS";              break;
1418         case IH_OS_NETBSD:      os = "NetBSD";                  break;
1419         case IH_OS_LINUX:       os = "Linux";                   break;
1420         case IH_OS_VXWORKS:     os = "VxWorks";                 break;
1421         case IH_OS_QNX:         os = "QNX";                     break;
1422         case IH_OS_U_BOOT:      os = "U-Boot";                  break;
1423         case IH_OS_RTEMS:       os = "RTEMS";                   break;
1424 #ifdef CONFIG_ARTOS
1425         case IH_OS_ARTOS:       os = "ARTOS";                   break;
1426 #endif
1427 #ifdef CONFIG_LYNXKDI
1428         case IH_OS_LYNXOS:      os = "LynxOS";                  break;
1429 #endif
1430         default:                os = "Unknown OS";              break;
1431         }
1432
1433         switch (hdr->ih_arch) {
1434         case IH_CPU_INVALID:    arch = "Invalid CPU";           break;
1435         case IH_CPU_ALPHA:      arch = "Alpha";                 break;
1436         case IH_CPU_ARM:        arch = "ARM";                   break;
1437         case IH_CPU_AVR32:      arch = "AVR32";                 break;
1438         case IH_CPU_BLACKFIN:   arch = "Blackfin";              break;
1439         case IH_CPU_I386:       arch = "Intel x86";             break;
1440         case IH_CPU_IA64:       arch = "IA64";                  break;
1441         case IH_CPU_M68K:       arch = "M68K";                  break;
1442         case IH_CPU_MICROBLAZE: arch = "Microblaze";            break;
1443         case IH_CPU_MIPS64:     arch = "MIPS 64 Bit";           break;
1444         case IH_CPU_MIPS:       arch = "MIPS";                  break;
1445         case IH_CPU_NIOS2:      arch = "Nios-II";               break;
1446         case IH_CPU_NIOS:       arch = "Nios";                  break;
1447         case IH_CPU_PPC:        arch = "PowerPC";               break;
1448         case IH_CPU_S390:       arch = "IBM S390";              break;
1449         case IH_CPU_SH:         arch = "SuperH";                break;
1450         case IH_CPU_SPARC64:    arch = "SPARC 64 Bit";          break;
1451         case IH_CPU_SPARC:      arch = "SPARC";                 break;
1452         default:                arch = "Unknown Architecture";  break;
1453         }
1454
1455         switch (hdr->ih_type) {
1456         case IH_TYPE_INVALID:   type = "Invalid Image";         break;
1457         case IH_TYPE_STANDALONE:type = "Standalone Program";    break;
1458         case IH_TYPE_KERNEL:    type = "Kernel Image";          break;
1459         case IH_TYPE_RAMDISK:   type = "RAMDisk Image";         break;
1460         case IH_TYPE_MULTI:     type = "Multi-File Image";      break;
1461         case IH_TYPE_FIRMWARE:  type = "Firmware";              break;
1462         case IH_TYPE_SCRIPT:    type = "Script";                break;
1463         case IH_TYPE_FLATDT:    type = "Flat Device Tree";      break;
1464         default:                type = "Unknown Image";         break;
1465         }
1466
1467         switch (hdr->ih_comp) {
1468         case IH_COMP_NONE:      comp = "uncompressed";          break;
1469         case IH_COMP_GZIP:      comp = "gzip compressed";       break;
1470         case IH_COMP_BZIP2:     comp = "bzip2 compressed";      break;
1471         default:                comp = "unknown compression";   break;
1472         }
1473
1474         printf ("%s %s %s (%s)", arch, os, type, comp);
1475 }
1476
1477 #define ZALLOC_ALIGNMENT        16
1478
1479 static void *zalloc(void *x, unsigned items, unsigned size)
1480 {
1481         void *p;
1482
1483         size *= items;
1484         size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1485
1486         p = malloc (size);
1487
1488         return (p);
1489 }
1490
1491 static void zfree(void *x, void *addr, unsigned nb)
1492 {
1493         free (addr);
1494 }
1495
1496 #define HEAD_CRC        2
1497 #define EXTRA_FIELD     4
1498 #define ORIG_NAME       8
1499 #define COMMENT         0x10
1500 #define RESERVED        0xe0
1501
1502 #define DEFLATED        8
1503
1504 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
1505 {
1506         z_stream s;
1507         int r, i, flags;
1508
1509         /* skip header */
1510         i = 10;
1511         flags = src[3];
1512         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
1513                 puts ("Error: Bad gzipped data\n");
1514                 return (-1);
1515         }
1516         if ((flags & EXTRA_FIELD) != 0)
1517                 i = 12 + src[10] + (src[11] << 8);
1518         if ((flags & ORIG_NAME) != 0)
1519                 while (src[i++] != 0)
1520                         ;
1521         if ((flags & COMMENT) != 0)
1522                 while (src[i++] != 0)
1523                         ;
1524         if ((flags & HEAD_CRC) != 0)
1525                 i += 2;
1526         if (i >= *lenp) {
1527                 puts ("Error: gunzip out of data in header\n");
1528                 return (-1);
1529         }
1530
1531         s.zalloc = zalloc;
1532         s.zfree = zfree;
1533 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1534         s.outcb = (cb_func)WATCHDOG_RESET;
1535 #else
1536         s.outcb = Z_NULL;
1537 #endif  /* CONFIG_HW_WATCHDOG */
1538
1539         r = inflateInit2(&s, -MAX_WBITS);
1540         if (r != Z_OK) {
1541                 printf ("Error: inflateInit2() returned %d\n", r);
1542                 return (-1);
1543         }
1544         s.next_in = src + i;
1545         s.avail_in = *lenp - i;
1546         s.next_out = dst;
1547         s.avail_out = dstlen;
1548         r = inflate(&s, Z_FINISH);
1549         if (r != Z_OK && r != Z_STREAM_END) {
1550                 printf ("Error: inflate() returned %d\n", r);
1551                 return (-1);
1552         }
1553         *lenp = s.next_out - (unsigned char *) dst;
1554         inflateEnd(&s);
1555
1556         return (0);
1557 }
1558
1559 #ifdef CONFIG_BZIP2
1560 void bz_internal_error(int errcode)
1561 {
1562         printf ("BZIP2 internal error %d\n", errcode);
1563 }
1564 #endif /* CONFIG_BZIP2 */
1565
1566 static void
1567 do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1568                 ulong addr, ulong *len_ptr, int verify)
1569 {
1570         image_header_t *hdr = &header;
1571         void    (*entry_point)(bd_t *);
1572
1573         entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
1574
1575         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1576                 (ulong)entry_point);
1577
1578         show_boot_progress (15);
1579
1580         /*
1581          * RTEMS Parameters:
1582          *   r3: ptr to board info data
1583          */
1584
1585         (*entry_point ) ( gd->bd );
1586 }
1587
1588 #if defined(CONFIG_CMD_ELF)
1589 static void
1590 do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1591                   ulong addr, ulong *len_ptr, int verify)
1592 {
1593         image_header_t *hdr = &header;
1594         char str[80];
1595
1596         sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
1597         setenv("loadaddr", str);
1598         do_bootvx(cmdtp, 0, 0, NULL);
1599 }
1600
1601 static void
1602 do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1603                  ulong addr, ulong *len_ptr, int verify)
1604 {
1605         image_header_t *hdr = &header;
1606         char *local_args[2];
1607         char str[16];
1608
1609         sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
1610         local_args[0] = argv[0];
1611         local_args[1] = str;    /* and provide it via the arguments */
1612         do_bootelf(cmdtp, 0, 2, local_args);
1613 }
1614 #endif
1615
1616 #ifdef CONFIG_LYNXKDI
1617 static void
1618 do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1619                  int    argc, char *argv[],
1620                  ulong  addr,
1621                  ulong  *len_ptr,
1622                  int    verify)
1623 {
1624         lynxkdi_boot( &header );
1625 }
1626
1627 #endif /* CONFIG_LYNXKDI */