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