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