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