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