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