]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_bootm.c
2ac576307125415e1d3b1da1dc77bfbd30d6fbb0
[karo-tx-uboot.git] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2002
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 <cmd_boot.h>
31 #include <image.h>
32 #include <malloc.h>
33 #include <zlib.h>
34 #include <asm/byteorder.h>
35 #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
36 #include <rtc.h>
37 #endif
38
39 #ifdef CFG_HUSH_PARSER
40 #include <hush.h>
41 #endif
42
43 #ifdef CONFIG_SHOW_BOOT_PROGRESS
44 # include <status_led.h>
45 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress(arg)
46 #else
47 # define SHOW_BOOT_PROGRESS(arg)
48 #endif
49
50 #ifdef CFG_INIT_RAM_LOCK
51 #include <asm/cache.h>
52 #endif
53
54 #ifdef CONFIG_LOGBUFFER
55 #include <logbuff.h>
56 #endif
57
58 /*
59  * Some systems (for example LWMON) have very short watchdog periods;
60  * we must make sure to split long operations like memmove() or
61  * crc32() into reasonable chunks.
62  */
63 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
64 # define CHUNKSZ (64 * 1024)
65 #endif
66
67 int  gunzip (void *, int, unsigned char *, int *);
68
69 static void *zalloc(void *, unsigned, unsigned);
70 static void zfree(void *, void *, unsigned);
71
72 #if (CONFIG_COMMANDS & CFG_CMD_IMI)
73 static int image_info (unsigned long addr);
74 #endif
75 static void print_type (image_header_t *hdr);
76
77 #ifdef __I386__
78 image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
79 #endif
80
81 /*
82  *  Continue booting an OS image; caller already has:
83  *  - copied image header to global variable `header'
84  *  - checked header magic number, checksums (both header & image),
85  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
86  *  - loaded (first part of) image to header load address,
87  *  - disabled interrupts.
88  */
89 typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
90                           int   argc, char *argv[],
91                           ulong addr,           /* of image to boot */
92                           ulong *len_ptr,       /* multi-file image length table */
93                           int   verify);        /* getenv("verify")[0] != 'n' */
94
95 #ifdef CONFIG_PPC
96 static boot_os_Fcn do_bootm_linux;
97 #else
98 extern boot_os_Fcn do_bootm_linux;
99 #endif
100 static boot_os_Fcn do_bootm_netbsd;
101 #if (CONFIG_COMMANDS & CFG_CMD_ELF)
102 static boot_os_Fcn do_bootm_vxworks;
103 static boot_os_Fcn do_bootm_qnxelf;
104 int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
105 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
106 #endif /* CFG_CMD_ELF */
107
108 image_header_t header;
109
110 ulong load_addr = CFG_LOAD_ADDR;                /* Default Load Address */
111
112 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
113 {
114         ulong   iflag;
115         ulong   addr;
116         ulong   data, len, checksum;
117         ulong  *len_ptr;
118         int     i, verify;
119         char    *name, *s;
120         int     (*appl)(cmd_tbl_t *, int, int, char *[]);
121         image_header_t *hdr = &header;
122
123         s = getenv ("verify");
124         verify = (s && (*s == 'n')) ? 0 : 1;
125
126         if (argc < 2) {
127                 addr = load_addr;
128         } else {
129                 addr = simple_strtoul(argv[1], NULL, 16);
130         }
131
132         SHOW_BOOT_PROGRESS (1);
133         printf ("## Booting image at %08lx ...\n", addr);
134
135         /* Copy header so we can blank CRC field for re-calculation */
136         memmove (&header, (char *)addr, sizeof(image_header_t));
137
138         if (ntohl(hdr->ih_magic) != IH_MAGIC) {
139 #ifdef __I386__ /* correct image format not implemented yet - fake it */
140                 if (fake_header(hdr, (void*)addr, -1) != NULL) {
141                         /* to compensate for the addition below */
142                         addr -= sizeof(image_header_t);
143                         /* turnof verify,
144                          * fake_header() does not fake the data crc
145                          */
146                         verify = 0;
147                 } else
148 #endif  /* __I386__ */
149             {
150                 printf ("Bad Magic Number\n");
151                 SHOW_BOOT_PROGRESS (-1);
152                 return 1;
153             }
154         }
155         SHOW_BOOT_PROGRESS (2);
156
157         data = (ulong)&header;
158         len  = sizeof(image_header_t);
159
160         checksum = ntohl(hdr->ih_hcrc);
161         hdr->ih_hcrc = 0;
162
163         if (crc32 (0, (char *)data, len) != checksum) {
164                 printf ("Bad Header Checksum\n");
165                 SHOW_BOOT_PROGRESS (-2);
166                 return 1;
167         }
168         SHOW_BOOT_PROGRESS (3);
169
170         /* for multi-file images we need the data part, too */
171         print_image_hdr (hdr);
172
173         data = addr + sizeof(image_header_t);
174         len  = ntohl(hdr->ih_size);
175
176         if (verify) {
177                 printf ("   Verifying Checksum ... ");
178                 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
179                         printf ("Bad Data CRC\n");
180                         SHOW_BOOT_PROGRESS (-3);
181                         return 1;
182                 }
183                 printf ("OK\n");
184         }
185         SHOW_BOOT_PROGRESS (4);
186
187         len_ptr = (ulong *)data;
188
189 #if defined(__PPC__)
190         if (hdr->ih_arch != IH_CPU_PPC)
191 #elif defined(__ARM__)
192         if (hdr->ih_arch != IH_CPU_ARM)
193 #elif defined(__I386__)
194         if (hdr->ih_arch != IH_CPU_I386)
195 #elif defined(__mips__)
196         if (hdr->ih_arch != IH_CPU_MIPS)        
197 #else
198 # error Unknown CPU type
199 #endif
200         {
201                 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
202                 SHOW_BOOT_PROGRESS (-4);
203                 return 1;
204         }
205         SHOW_BOOT_PROGRESS (5);
206
207         switch (hdr->ih_type) {
208         case IH_TYPE_STANDALONE:        name = "Standalone Application";
209                                         break;
210         case IH_TYPE_KERNEL:            name = "Kernel Image";
211                                         break;
212         case IH_TYPE_MULTI:             name = "Multi-File Image";
213                                         len  = ntohl(len_ptr[0]);
214                                         /* OS kernel is always the first image */
215                                         data += 8; /* kernel_len + terminator */
216                                         for (i=1; len_ptr[i]; ++i)
217                                                 data += 4;
218                                         break;
219         default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
220                 SHOW_BOOT_PROGRESS (-5);
221                 return 1;
222         }
223         SHOW_BOOT_PROGRESS (6);
224
225         /*
226          * We have reached the point of no return: we are going to
227          * overwrite all exception vector code, so we cannot easily
228          * recover from any failures any more...
229          */
230
231         iflag = disable_interrupts();
232
233 #ifdef CONFIG_AMIGAONEG3SE
234         /*
235          * We've possible left the caches enabled during 
236          * bios emulation, so turn them off again
237          */
238         icache_disable();
239         invalidate_l1_instruction_cache();
240         flush_data_cache();
241         dcache_disable();
242 #endif
243
244         switch (hdr->ih_comp) {
245         case IH_COMP_NONE:
246                 if(ntohl(hdr->ih_load) == addr) {
247                         printf ("   XIP %s ... ", name);
248                 } else {
249 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
250                         size_t l = len;
251                         void *to = (void *)ntohl(hdr->ih_load);
252                         void *from = (void *)data;
253
254                         printf ("   Loading %s ... ", name);
255
256                         while (l > 0) {
257                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
258                                 WATCHDOG_RESET();
259                                 memmove (to, from, tail);
260                                 to += tail;
261                                 from += tail;
262                                 l -= tail;
263                         }
264 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
265                         memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
266 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
267                 }
268                 break;
269         case IH_COMP_GZIP:
270                 printf ("   Uncompressing %s ... ", name);
271                 if (gunzip ((void *)ntohl(hdr->ih_load), 0x400000,
272                             (uchar *)data, (int *)&len) != 0) {
273                         printf ("GUNZIP ERROR - must RESET board to recover\n");
274                         SHOW_BOOT_PROGRESS (-6);
275                         do_reset (cmdtp, flag, argc, argv);
276                 }
277                 break;
278         default:
279                 if (iflag)
280                         enable_interrupts();
281                 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
282                 SHOW_BOOT_PROGRESS (-7);
283                 return 1;
284         }
285         printf ("OK\n");
286         SHOW_BOOT_PROGRESS (7);
287
288         switch (hdr->ih_type) {
289         case IH_TYPE_STANDALONE:
290                 appl = (int (*)(cmd_tbl_t *, int, int, char *[]))ntohl(hdr->ih_ep);
291                 if (iflag)
292                         enable_interrupts();
293
294                 (*appl)(cmdtp, flag, argc-1, &argv[1]);
295                 break;
296         case IH_TYPE_KERNEL:
297         case IH_TYPE_MULTI:
298                 /* handled below */
299                 break;
300         default:
301                 if (iflag)
302                         enable_interrupts();
303                 printf ("Can't boot image type %d\n", hdr->ih_type);
304                 SHOW_BOOT_PROGRESS (-8);
305                 return 1;
306         }
307         SHOW_BOOT_PROGRESS (8);
308
309         switch (hdr->ih_os) {
310         default:                        /* handled by (original) Linux case */
311         case IH_OS_LINUX:
312             do_bootm_linux  (cmdtp, flag, argc, argv,
313                              addr, len_ptr, verify);
314             break;
315         case IH_OS_NETBSD:
316             do_bootm_netbsd (cmdtp, flag, argc, argv,
317                              addr, len_ptr, verify);
318             break;
319 #if (CONFIG_COMMANDS & CFG_CMD_ELF)
320         case IH_OS_VXWORKS:
321             do_bootm_vxworks (cmdtp, flag, argc, argv,
322                               addr, len_ptr, verify);
323             break;
324         case IH_OS_QNX:
325             do_bootm_qnxelf (cmdtp, flag, argc, argv,
326                               addr, len_ptr, verify);
327             break;
328 #endif /* CFG_CMD_ELF */
329         }
330
331         SHOW_BOOT_PROGRESS (-9);
332 #ifdef DEBUG
333         printf ("\n## Control returned to monitor - resetting...\n");
334         do_reset (cmdtp, flag, argc, argv);
335 #endif
336         return 1;
337 }
338
339 #ifdef CONFIG_PPC
340 static void
341 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
342                 int     argc, char *argv[],
343                 ulong   addr,
344                 ulong   *len_ptr,
345                 int     verify)
346 {
347         DECLARE_GLOBAL_DATA_PTR;
348
349         ulong   sp;
350         ulong   len, checksum;
351         ulong   initrd_start, initrd_end;
352         ulong   cmd_start, cmd_end;
353         ulong   initrd_high;
354         ulong   data;
355         char    *cmdline;
356         char    *s;
357         bd_t    *kbd;
358         void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
359         image_header_t *hdr = &header;
360
361         if ((s = getenv ("initrd_high")) != NULL) {
362                 /* a value of "no" or a similar string will act like 0,
363                  * turning the "load high" feature off. This is intentional.
364                  */
365                 initrd_high = simple_strtoul(s, NULL, 16);
366         } else {        /* not set, no restrictions to load high */
367                 initrd_high = ~0;
368         }
369
370 #ifdef CONFIG_LOGBUFFER
371         kbd=gd->bd;
372         /* Prevent initrd from overwriting logbuffer */
373         if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
374                 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
375         debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
376 #endif
377
378         /*
379          * Booting a (Linux) kernel image
380          *
381          * Allocate space for command line and board info - the
382          * address should be as high as possible within the reach of
383          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
384          * memory, which means far enough below the current stack
385          * pointer.
386          */
387
388         asm( "mr %0,1": "=r"(sp) : );
389
390         debug ("## Current stack ends at 0x%08lX ", sp);
391
392         sp -= 2048;             /* just to be sure */
393         if (sp > CFG_BOOTMAPSZ)
394                 sp = CFG_BOOTMAPSZ;
395         sp &= ~0xF;
396
397         debug ("=> set upper limit to 0x%08lX\n", sp);
398
399         cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
400         kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
401
402         if ((s = getenv("bootargs")) == NULL)
403                 s = "";
404
405         strcpy (cmdline, s);
406
407         cmd_start    = (ulong)&cmdline[0];
408         cmd_end      = cmd_start + strlen(cmdline);
409
410         *kbd = *(gd->bd);
411
412 #ifdef  DEBUG
413         printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
414
415         do_bdinfo (NULL, 0, 0, NULL);
416 #endif
417
418         if ((s = getenv ("clocks_in_mhz")) != NULL) {
419                 /* convert all clock information to MHz */
420                 kbd->bi_intfreq /= 1000000L;
421                 kbd->bi_busfreq /= 1000000L;
422 #if defined(CONFIG_8260)
423                 kbd->bi_cpmfreq /= 1000000L;
424                 kbd->bi_brgfreq /= 1000000L;
425                 kbd->bi_sccfreq /= 1000000L;
426                 kbd->bi_vco     /= 1000000L;
427 #endif /* CONFIG_8260 */
428         }
429
430         kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
431
432         /*
433          * Check if there is an initrd image
434          */
435         if (argc >= 3) {
436                 SHOW_BOOT_PROGRESS (9);
437
438                 addr = simple_strtoul(argv[2], NULL, 16);
439
440                 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
441
442                 /* Copy header so we can blank CRC field for re-calculation */
443                 memmove (&header, (char *)addr, sizeof(image_header_t));
444
445                 if (hdr->ih_magic  != IH_MAGIC) {
446                         printf ("Bad Magic Number\n");
447                         SHOW_BOOT_PROGRESS (-10);
448                         do_reset (cmdtp, flag, argc, argv);
449                 }
450
451                 data = (ulong)&header;
452                 len  = sizeof(image_header_t);
453
454                 checksum = hdr->ih_hcrc;
455                 hdr->ih_hcrc = 0;
456
457                 if (crc32 (0, (char *)data, len) != checksum) {
458                         printf ("Bad Header Checksum\n");
459                         SHOW_BOOT_PROGRESS (-11);
460                         do_reset (cmdtp, flag, argc, argv);
461                 }
462
463                 SHOW_BOOT_PROGRESS (10);
464
465                 print_image_hdr (hdr);
466
467                 data = addr + sizeof(image_header_t);
468                 len  = hdr->ih_size;
469
470                 if (verify) {
471                         ulong csum = 0;
472 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
473                         ulong cdata = data, edata = cdata + len;
474 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
475
476                         printf ("   Verifying Checksum ... ");
477
478 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
479
480                         while (cdata < edata) {
481                                 ulong chunk = edata - cdata;
482
483                                 if (chunk > CHUNKSZ)
484                                         chunk = CHUNKSZ;
485                                 csum = crc32 (csum, (char *)cdata, chunk);
486                                 cdata += chunk;
487
488                                 WATCHDOG_RESET();
489                         }
490 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
491                         csum = crc32 (0, (char *)data, len);
492 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
493
494                         if (csum != hdr->ih_dcrc) {
495                                 printf ("Bad Data CRC\n");
496                                 SHOW_BOOT_PROGRESS (-12);
497                                 do_reset (cmdtp, flag, argc, argv);
498                         }
499                         printf ("OK\n");
500                 }
501
502                 SHOW_BOOT_PROGRESS (11);
503
504                 if ((hdr->ih_os   != IH_OS_LINUX)       ||
505                     (hdr->ih_arch != IH_CPU_PPC)        ||
506                     (hdr->ih_type != IH_TYPE_RAMDISK)   ) {
507                         printf ("No Linux PPC Ramdisk Image\n");
508                         SHOW_BOOT_PROGRESS (-13);
509                         do_reset (cmdtp, flag, argc, argv);
510                 }
511
512                 /*
513                  * Now check if we have a multifile image
514                  */
515         } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
516                 u_long tail    = ntohl(len_ptr[0]) % 4;
517                 int i;
518
519                 SHOW_BOOT_PROGRESS (13);
520
521                 /* skip kernel length and terminator */
522                 data = (ulong)(&len_ptr[2]);
523                 /* skip any additional image length fields */
524                 for (i=1; len_ptr[i]; ++i)
525                         data += 4;
526                 /* add kernel length, and align */
527                 data += ntohl(len_ptr[0]);
528                 if (tail) {
529                         data += 4 - tail;
530                 }
531
532                 len   = ntohl(len_ptr[1]);
533
534         } else {
535                 /*
536                  * no initrd image
537                  */
538                 SHOW_BOOT_PROGRESS (14);
539
540                 len = data = 0;
541         }
542
543         if (!data) {
544                 debug ("No initrd\n");
545         }
546
547         if (data) {
548                 initrd_start  = (ulong)kbd - len;
549                 initrd_start &= ~(4096 - 1);    /* align on page */
550
551                 if (initrd_high) {
552                         ulong nsp;
553
554                         /*
555                          * the inital ramdisk does not need to be within
556                          * CFG_BOOTMAPSZ as it is not accessed until after
557                          * the mm system is initialised.
558                          *
559                          * do the stack bottom calculation again and see if
560                          * the initrd will fit just below the monitor stack
561                          * bottom without overwriting the area allocated
562                          * above for command line args and board info.
563                          */
564                         asm( "mr %0,1": "=r"(nsp) : );
565                         nsp -= 2048;            /* just to be sure */
566                         nsp &= ~0xF;
567                         if (nsp > initrd_high)  /* limit as specified */
568                                 nsp = initrd_high;
569                         nsp -= len;
570                         nsp &= ~(4096 - 1);     /* align on page */
571                         if (nsp >= sp)
572                                 initrd_start = nsp;
573                 }
574
575                 SHOW_BOOT_PROGRESS (12);
576
577                 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
578                         data, data + len - 1, len, len);
579
580                 initrd_end    = initrd_start + len;
581                 printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
582                         initrd_start, initrd_end);
583 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
584                 {
585                         size_t l = len;
586                         void *to = (void *)initrd_start;
587                         void *from = (void *)data;
588
589                         while (l > 0) {
590                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
591                                 WATCHDOG_RESET();
592                                 memmove (to, from, tail);
593                                 to += tail;
594                                 from += tail;
595                                 l -= tail;
596                         }
597                 }
598 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
599                 memmove ((void *)initrd_start, (void *)data, len);
600 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
601                 printf ("OK\n");
602         } else {
603                 initrd_start = 0;
604                 initrd_end = 0;
605         }
606
607
608         debug ("## Transferring control to Linux (at address %08lx) ...\n",
609                 (ulong)kernel);
610
611         SHOW_BOOT_PROGRESS (15);
612
613 #ifdef CFG_INIT_RAM_LOCK
614         unlock_ram_in_cache();
615 #endif
616         /*
617          * Linux Kernel Parameters:
618          *   r3: ptr to board info data
619          *   r4: initrd_start or 0 if no initrd
620          *   r5: initrd_end - unused if r4 is 0
621          *   r6: Start of command line string
622          *   r7: End   of command line string
623          */
624         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
625 }
626 #endif /* CONFIG_ARM */
627
628 static void
629 do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
630                 int     argc, char *argv[],
631                 ulong   addr,
632                 ulong   *len_ptr,
633                 int     verify)
634 {
635         DECLARE_GLOBAL_DATA_PTR;
636
637         image_header_t *hdr = &header;
638
639         void    (*loader)(bd_t *, image_header_t *, char *, char *);
640         image_header_t *img_addr;
641         char     *consdev;
642         char     *cmdline;
643
644
645         /*
646          * Booting a (NetBSD) kernel image
647          *
648          * This process is pretty similar to a standalone application:
649          * The (first part of an multi-) image must be a stage-2 loader,
650          * which in turn is responsible for loading & invoking the actual
651          * kernel.  The only differences are the parameters being passed:
652          * besides the board info strucure, the loader expects a command
653          * line, the name of the console device, and (optionally) the
654          * address of the original image header.
655          */
656
657         img_addr = 0;
658         if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
659                 img_addr = (image_header_t *) addr;
660
661
662         consdev = "";
663 #if   defined (CONFIG_8xx_CONS_SMC1)
664         consdev = "smc1";
665 #elif defined (CONFIG_8xx_CONS_SMC2)
666         consdev = "smc2";
667 #elif defined (CONFIG_8xx_CONS_SCC2)
668         consdev = "scc2";
669 #elif defined (CONFIG_8xx_CONS_SCC3)
670         consdev = "scc3";
671 #endif
672
673         if (argc > 2) {
674                 ulong len;
675                 int   i;
676
677                 for (i=2, len=0 ; i<argc ; i+=1)
678                         len += strlen (argv[i]) + 1;
679                 cmdline = malloc (len);
680
681                 for (i=2, len=0 ; i<argc ; i+=1) {
682                         if (i > 2)
683                                 cmdline[len++] = ' ';
684                         strcpy (&cmdline[len], argv[i]);
685                         len += strlen (argv[i]);
686                 }
687         } else if ((cmdline = getenv("bootargs")) == NULL) {
688                 cmdline = "";
689         }
690
691         loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
692
693         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
694                 (ulong)loader);
695
696         SHOW_BOOT_PROGRESS (15);
697
698         /*
699          * NetBSD Stage-2 Loader Parameters:
700          *   r3: ptr to board info data
701          *   r4: image address
702          *   r5: console device
703          *   r6: boot args string
704          */
705         (*loader) (gd->bd, img_addr, consdev, cmdline);
706 }
707
708 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
709 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
710 {
711         int rcode = 0;
712 #ifndef CFG_HUSH_PARSER
713         if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
714 #else
715         if (parse_string_outer(getenv("bootcmd"),
716                 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
717 #endif
718         return rcode;
719 }
720 #endif
721
722 #if (CONFIG_COMMANDS & CFG_CMD_IMI)
723 int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
724 {
725         int     arg;
726         ulong   addr;
727         int     rcode=0;
728
729         if (argc < 2) {
730                 return image_info (load_addr);
731         }
732
733         for (arg=1; arg <argc; ++arg) {
734                 addr = simple_strtoul(argv[arg], NULL, 16);
735                 if (image_info (addr) != 0) rcode = 1;
736         }
737         return rcode;
738 }
739
740 static int image_info (ulong addr)
741 {
742         ulong   data, len, checksum;
743         image_header_t *hdr = &header;
744
745         printf ("\n## Checking Image at %08lx ...\n", addr);
746
747         /* Copy header so we can blank CRC field for re-calculation */
748         memmove (&header, (char *)addr, sizeof(image_header_t));
749
750         if (ntohl(hdr->ih_magic) != IH_MAGIC) {
751                 printf ("   Bad Magic Number\n");
752                 return 1;
753         }
754
755         data = (ulong)&header;
756         len  = sizeof(image_header_t);
757
758         checksum = ntohl(hdr->ih_hcrc);
759         hdr->ih_hcrc = 0;
760
761         if (crc32 (0, (char *)data, len) != checksum) {
762                 printf ("   Bad Header Checksum\n");
763                 return 1;
764         }
765
766         /* for multi-file images we need the data part, too */
767         print_image_hdr ((image_header_t *)addr);
768
769         data = addr + sizeof(image_header_t);
770         len  = ntohl(hdr->ih_size);
771
772         printf ("   Verifying Checksum ... ");
773         if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
774                 printf ("   Bad Data CRC\n");
775                 return 1;
776         }
777         printf ("OK\n");
778         return 0;
779 }
780 #endif  /* CFG_CMD_IMI */
781
782 void
783 print_image_hdr (image_header_t *hdr)
784 {
785 #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
786         time_t timestamp = (time_t)ntohl(hdr->ih_time);
787         struct rtc_time tm;
788 #endif
789
790         printf ("   Image Name:   %.*s\n", IH_NMLEN, hdr->ih_name);
791 #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
792         to_tm (timestamp, &tm);
793         printf ("   Created:      %4d-%02d-%02d  %2d:%02d:%02d UTC\n",
794                 tm.tm_year, tm.tm_mon, tm.tm_mday,
795                 tm.tm_hour, tm.tm_min, tm.tm_sec);
796 #endif  /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
797         printf ("   Image Type:   "); print_type(hdr); printf ("\n");
798         printf ("   Data Size:    %d Bytes = ", ntohl(hdr->ih_size));
799         print_size (ntohl(hdr->ih_size), "\n");
800         printf ("   Load Address: %08x\n", ntohl(hdr->ih_load));
801         printf ("   Entry Point:  %08x\n", ntohl(hdr->ih_ep));
802
803         if (hdr->ih_type == IH_TYPE_MULTI) {
804                 int i;
805                 ulong len;
806                 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
807
808                 printf ("   Contents:\n");
809                 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
810                         printf ("   Image %d: %8ld Bytes = ", i, len);
811                         print_size (len, "\n");
812                 }
813         }
814 }
815
816
817 static void
818 print_type (image_header_t *hdr)
819 {
820         char *os, *arch, *type, *comp;
821
822         switch (hdr->ih_os) {
823         case IH_OS_INVALID:     os = "Invalid OS";              break;
824         case IH_OS_NETBSD:      os = "NetBSD";                  break;
825         case IH_OS_LINUX:       os = "Linux";                   break;
826         case IH_OS_VXWORKS:     os = "VxWorks";                 break;
827         case IH_OS_QNX:         os = "QNX";                     break;
828         case IH_OS_U_BOOT:      os = "U-Boot";                  break;
829         default:                os = "Unknown OS";              break;
830         }
831
832         switch (hdr->ih_arch) {
833         case IH_CPU_INVALID:    arch = "Invalid CPU";           break;
834         case IH_CPU_ALPHA:      arch = "Alpha";                 break;
835         case IH_CPU_ARM:        arch = "ARM";                   break;
836         case IH_CPU_I386:       arch = "Intel x86";             break;
837         case IH_CPU_IA64:       arch = "IA64";                  break;
838         case IH_CPU_MIPS:       arch = "MIPS";                  break;
839         case IH_CPU_MIPS64:     arch = "MIPS 64 Bit";           break;
840         case IH_CPU_PPC:        arch = "PowerPC";               break;
841         case IH_CPU_S390:       arch = "IBM S390";              break;
842         case IH_CPU_SH:         arch = "SuperH";                break;
843         case IH_CPU_SPARC:      arch = "SPARC";                 break;
844         case IH_CPU_SPARC64:    arch = "SPARC 64 Bit";          break;
845         default:                arch = "Unknown Architecture";  break;
846         }
847
848         switch (hdr->ih_type) {
849         case IH_TYPE_INVALID:   type = "Invalid Image";         break;
850         case IH_TYPE_STANDALONE:type = "Standalone Program";    break;
851         case IH_TYPE_KERNEL:    type = "Kernel Image";          break;
852         case IH_TYPE_RAMDISK:   type = "RAMDisk Image";         break;
853         case IH_TYPE_MULTI:     type = "Multi-File Image";      break;
854         case IH_TYPE_FIRMWARE:  type = "Firmware";              break;
855         case IH_TYPE_SCRIPT:    type = "Script";                break;
856         default:                type = "Unknown Image";         break;
857         }
858
859         switch (hdr->ih_comp) {
860         case IH_COMP_NONE:      comp = "uncompressed";          break;
861         case IH_COMP_GZIP:      comp = "gzip compressed";       break;
862         case IH_COMP_BZIP2:     comp = "bzip2 compressed";      break;
863         default:                comp = "unknown compression";   break;
864         }
865
866         printf ("%s %s %s (%s)", arch, os, type, comp);
867 }
868
869 #define ZALLOC_ALIGNMENT        16
870
871 static void *zalloc(void *x, unsigned items, unsigned size)
872 {
873         void *p;
874
875         size *= items;
876         size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
877
878         p = malloc (size);
879
880         return (p);
881 }
882
883 static void zfree(void *x, void *addr, unsigned nb)
884 {
885         free (addr);
886 }
887
888 #define HEAD_CRC        2
889 #define EXTRA_FIELD     4
890 #define ORIG_NAME       8
891 #define COMMENT         0x10
892 #define RESERVED        0xe0
893
894 #define DEFLATED        8
895
896 int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
897 {
898         z_stream s;
899         int r, i, flags;
900
901         /* skip header */
902         i = 10;
903         flags = src[3];
904         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
905                 printf ("Error: Bad gzipped data\n");
906                 return (-1);
907         }
908         if ((flags & EXTRA_FIELD) != 0)
909                 i = 12 + src[10] + (src[11] << 8);
910         if ((flags & ORIG_NAME) != 0)
911                 while (src[i++] != 0)
912                         ;
913         if ((flags & COMMENT) != 0)
914                 while (src[i++] != 0)
915                         ;
916         if ((flags & HEAD_CRC) != 0)
917                 i += 2;
918         if (i >= *lenp) {
919                 printf ("Error: gunzip out of data in header\n");
920                 return (-1);
921         }
922
923         s.zalloc = zalloc;
924         s.zfree = zfree;
925 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
926         s.outcb = (cb_func)WATCHDOG_RESET;
927 #else
928         s.outcb = Z_NULL;
929 #endif  /* CONFIG_HW_WATCHDOG */
930
931         r = inflateInit2(&s, -MAX_WBITS);
932         if (r != Z_OK) {
933                 printf ("Error: inflateInit2() returned %d\n", r);
934                 return (-1);
935         }
936         s.next_in = src + i;
937         s.avail_in = *lenp - i;
938         s.next_out = dst;
939         s.avail_out = dstlen;
940         r = inflate(&s, Z_FINISH);
941         if (r != Z_OK && r != Z_STREAM_END) {
942                 printf ("Error: inflate() returned %d\n", r);
943                 return (-1);
944         }
945         *lenp = s.next_out - (unsigned char *) dst;
946         inflateEnd(&s);
947
948         return (0);
949 }
950
951 #if (CONFIG_COMMANDS & CFG_CMD_ELF)
952 static void
953 do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
954                   ulong addr, ulong *len_ptr, int verify)
955 {
956         image_header_t *hdr = &header;
957         char str[80];
958
959         sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
960         setenv("loadaddr", str);
961         do_bootvx(cmdtp, 0, 0, NULL);
962 }
963
964 static void
965 do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
966                  ulong addr, ulong *len_ptr, int verify)
967 {
968         image_header_t *hdr = &header;
969         char *local_args[2];
970         char str[16];
971
972         sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
973         local_args[0] = argv[0];
974         local_args[1] = str;    /* and provide it via the arguments */
975         do_bootelf(cmdtp, 0, 2, local_args);
976 }
977 #endif /* CFG_CMD_ELF */