[new uImage] Define a API for image handling operations
[oweals/u-boot.git] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2006
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 #define DEBUG
25
26 /*
27  * Boot support
28  */
29 #include <common.h>
30 #include <watchdog.h>
31 #include <command.h>
32 #include <image.h>
33 #include <malloc.h>
34 #include <zlib.h>
35 #include <bzlib.h>
36 #include <environment.h>
37 #include <asm/byteorder.h>
38
39 #if defined(CONFIG_OF_LIBFDT)
40 #include <fdt.h>
41 #include <libfdt.h>
42 #include <fdt_support.h>
43 #endif
44 #if defined(CONFIG_OF_FLAT_TREE)
45 #include <ft_build.h>
46 #endif
47
48 DECLARE_GLOBAL_DATA_PTR;
49
50 /*cmd_boot.c*/
51 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
52
53 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
54 #include <rtc.h>
55 #endif
56
57 #ifdef CFG_HUSH_PARSER
58 #include <hush.h>
59 #endif
60
61 #ifdef CFG_INIT_RAM_LOCK
62 #include <asm/cache.h>
63 #endif
64
65 #ifdef CONFIG_LOGBUFFER
66 #include <logbuff.h>
67 #endif
68
69 #ifdef CONFIG_HAS_DATAFLASH
70 #include <dataflash.h>
71 #endif
72
73 /*
74  * Some systems (for example LWMON) have very short watchdog periods;
75  * we must make sure to split long operations like memmove() or
76  * crc32() into reasonable chunks.
77  */
78 #define CHUNKSZ (64 * 1024)
79
80 int  gunzip (void *, int, unsigned char *, unsigned long *);
81
82 static void *zalloc(void *, unsigned, unsigned);
83 static void zfree(void *, void *, unsigned);
84
85 #if defined(CONFIG_CMD_IMI)
86 static int image_info (unsigned long addr);
87 #endif
88
89 #if defined(CONFIG_CMD_IMLS)
90 #include <flash.h>
91 extern flash_info_t flash_info[]; /* info for FLASH chips */
92 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
93 #endif
94
95 static void print_type (image_header_t *hdr);
96
97 #ifdef __I386__
98 image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
99 #endif
100
101 /*
102  *  Continue booting an OS image; caller already has:
103  *  - copied image header to global variable `header'
104  *  - checked header magic number, checksums (both header & image),
105  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
106  *  - loaded (first part of) image to header load address,
107  *  - disabled interrupts.
108  */
109 typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
110                           int   argc, char *argv[],
111                           ulong addr,           /* of image to boot */
112                           ulong *len_ptr,       /* multi-file image length table */
113                           int   verify);        /* getenv("verify")[0] != 'n' */
114
115 #ifdef  DEBUG
116 extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
117 #endif
118
119 #ifdef CONFIG_PPC
120 static boot_os_Fcn do_bootm_linux;
121 #else
122 extern boot_os_Fcn do_bootm_linux;
123 #endif
124 #ifdef CONFIG_SILENT_CONSOLE
125 static void fixup_silent_linux (void);
126 #endif
127 static boot_os_Fcn do_bootm_netbsd;
128 static boot_os_Fcn do_bootm_rtems;
129 #if defined(CONFIG_CMD_ELF)
130 static boot_os_Fcn do_bootm_vxworks;
131 static boot_os_Fcn do_bootm_qnxelf;
132 int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
133 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
134 #endif
135 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
136 static boot_os_Fcn do_bootm_artos;
137 #endif
138 #ifdef CONFIG_LYNXKDI
139 static boot_os_Fcn do_bootm_lynxkdi;
140 extern void lynxkdi_boot( image_header_t * );
141 #endif
142
143 #ifndef CFG_BOOTM_LEN
144 #define CFG_BOOTM_LEN   0x800000        /* use 8MByte as default max gunzip size */
145 #endif
146
147 image_header_t header;
148
149 ulong load_addr = CFG_LOAD_ADDR;                /* Default Load Address */
150
151 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
152 {
153         ulong   iflag;
154         ulong   addr;
155         ulong   data, len;
156         ulong  *len_ptr;
157         uint    unc_len = CFG_BOOTM_LEN;
158         int     i, verify;
159         char    *name, *s;
160         int     (*appl)(int, char *[]);
161         image_header_t *hdr = &header;
162
163         verify = getenv_verify ();
164
165         if (argc < 2) {
166                 addr = load_addr;
167         } else {
168                 addr = simple_strtoul(argv[1], NULL, 16);
169         }
170
171         show_boot_progress (1);
172         printf ("## Booting image at %08lx ...\n", addr);
173
174         /* Copy header so we can blank CRC field for re-calculation */
175 #ifdef CONFIG_HAS_DATAFLASH
176         if (addr_dataflash(addr)){
177                 read_dataflash (addr, image_get_header_size (), (char *)&header);
178         } else
179 #endif
180         memmove (&header, (char *)addr, image_get_header_size ());
181
182         if (!image_check_magic (hdr)) {
183 #ifdef __I386__ /* correct image format not implemented yet - fake it */
184                 if (fake_header(hdr, (void*)addr, -1) != NULL) {
185                         /* to compensate for the addition below */
186                         addr -= image_get_header_size ();
187                         /* turnof verify,
188                          * fake_header() does not fake the data crc
189                          */
190                         verify = 0;
191                 } else
192 #endif  /* __I386__ */
193             {
194                 puts ("Bad Magic Number\n");
195                 show_boot_progress (-1);
196                 return 1;
197             }
198         }
199         show_boot_progress (2);
200
201         if (!image_check_hcrc (hdr)) {
202                 puts ("Bad Header Checksum\n");
203                 show_boot_progress (-2);
204                 return 1;
205         }
206         show_boot_progress (3);
207
208 #ifdef CONFIG_HAS_DATAFLASH
209         if (addr_dataflash(addr)){
210                 len  = image_get_image_size (hdr);
211                 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
212                 addr = CFG_LOAD_ADDR;
213         }
214 #endif
215
216
217         /* for multi-file images we need the data part, too */
218         print_image_hdr ((image_header_t *)addr);
219
220         len = image_get_data_size (hdr);
221         data = addr + image_get_header_size ();
222         len_ptr = (ulong *)data;
223
224         if (verify) {
225                 puts ("   Verifying Checksum ... ");
226                 if (!image_check_dcrc ((image_header_t *)addr)) {
227                         printf ("Bad Data CRC\n");
228                         show_boot_progress (-3);
229                         return 1;
230                 }
231                 puts ("OK\n");
232         }
233         show_boot_progress (4);
234
235         if (!image_check_target_arch (hdr)) {
236                 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
237                 show_boot_progress (-4);
238                 return 1;
239         }
240         show_boot_progress (5);
241
242         switch (image_get_type (hdr)) {
243         case IH_TYPE_STANDALONE:
244                 name = "Standalone Application";
245                 /* A second argument overwrites the load address */
246                 if (argc > 2) {
247                         image_set_load (hdr, simple_strtoul (argv[2], NULL, 16));
248                 }
249                 break;
250         case IH_TYPE_KERNEL:
251                 name = "Kernel Image";
252                 break;
253         case IH_TYPE_MULTI:
254                 name = "Multi-File Image";
255                 len  = image_to_cpu (len_ptr[0]);
256                 /* OS kernel is always the first image */
257                 data += 8; /* kernel_len + terminator */
258                 for (i=1; len_ptr[i]; ++i)
259                         data += 4;
260                 break;
261         default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
262                 show_boot_progress (-5);
263                 return 1;
264         }
265         show_boot_progress (6);
266
267         /*
268          * We have reached the point of no return: we are going to
269          * overwrite all exception vector code, so we cannot easily
270          * recover from any failures any more...
271          */
272
273         iflag = disable_interrupts();
274
275 #ifdef CONFIG_AMIGAONEG3SE
276         /*
277          * We've possible left the caches enabled during
278          * bios emulation, so turn them off again
279          */
280         icache_disable();
281         invalidate_l1_instruction_cache();
282         flush_data_cache();
283         dcache_disable();
284 #endif
285
286         switch (image_get_comp (hdr)) {
287         case IH_COMP_NONE:
288                 if (image_get_load (hdr) == addr) {
289                         printf ("   XIP %s ... ", name);
290                 } else {
291 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
292                         size_t l = len;
293                         void *to = (void *)image_get_load (hdr);
294                         void *from = (void *)data;
295
296                         printf ("   Loading %s ... ", name);
297
298                         while (l > 0) {
299                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
300                                 WATCHDOG_RESET();
301                                 memmove (to, from, tail);
302                                 to += tail;
303                                 from += tail;
304                                 l -= tail;
305                         }
306 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
307                         memmove ((void *)image_get_load (hdr), (uchar *)data, len);
308 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
309                 }
310                 break;
311         case IH_COMP_GZIP:
312                 printf ("   Uncompressing %s ... ", name);
313                 if (gunzip ((void *)image_get_load (hdr), unc_len,
314                             (uchar *)data, &len) != 0) {
315                         puts ("GUNZIP ERROR - must RESET board to recover\n");
316                         show_boot_progress (-6);
317                         do_reset (cmdtp, flag, argc, argv);
318                 }
319                 break;
320 #ifdef CONFIG_BZIP2
321         case IH_COMP_BZIP2:
322                 printf ("   Uncompressing %s ... ", name);
323                 /*
324                  * If we've got less than 4 MB of malloc() space,
325                  * use slower decompression algorithm which requires
326                  * at most 2300 KB of memory.
327                  */
328                 i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
329                                                 &unc_len, (char *)data, len,
330                                                 CFG_MALLOC_LEN < (4096 * 1024), 0);
331                 if (i != BZ_OK) {
332                         printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
333                         show_boot_progress (-6);
334                         do_reset (cmdtp, flag, argc, argv);
335                 }
336                 break;
337 #endif /* CONFIG_BZIP2 */
338         default:
339                 if (iflag)
340                         enable_interrupts();
341                 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
342                 show_boot_progress (-7);
343                 return 1;
344         }
345         puts ("OK\n");
346         show_boot_progress (7);
347
348         switch (image_get_type (hdr)) {
349         case IH_TYPE_STANDALONE:
350                 if (iflag)
351                         enable_interrupts();
352
353                 /* load (and uncompress), but don't start if "autostart"
354                  * is set to "no"
355                  */
356                 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
357                         char buf[32];
358                         sprintf(buf, "%lX", len);
359                         setenv("filesize", buf);
360                         return 0;
361                 }
362                 appl = (int (*)(int, char *[]))image_get_ep (hdr);
363                 (*appl)(argc-1, &argv[1]);
364                 return 0;
365         case IH_TYPE_KERNEL:
366         case IH_TYPE_MULTI:
367                 /* handled below */
368                 break;
369         default:
370                 if (iflag)
371                         enable_interrupts();
372                 printf ("Can't boot image type %d\n", image_get_type (hdr));
373                 show_boot_progress (-8);
374                 return 1;
375         }
376         show_boot_progress (8);
377
378         switch (image_get_os (hdr)) {
379         default:                        /* handled by (original) Linux case */
380         case IH_OS_LINUX:
381 #ifdef CONFIG_SILENT_CONSOLE
382             fixup_silent_linux();
383 #endif
384             do_bootm_linux  (cmdtp, flag, argc, argv,
385                              addr, len_ptr, verify);
386             break;
387         case IH_OS_NETBSD:
388             do_bootm_netbsd (cmdtp, flag, argc, argv,
389                              addr, len_ptr, verify);
390             break;
391
392 #ifdef CONFIG_LYNXKDI
393         case IH_OS_LYNXOS:
394             do_bootm_lynxkdi (cmdtp, flag, argc, argv,
395                              addr, len_ptr, verify);
396             break;
397 #endif
398
399         case IH_OS_RTEMS:
400             do_bootm_rtems (cmdtp, flag, argc, argv,
401                              addr, len_ptr, verify);
402             break;
403
404 #if defined(CONFIG_CMD_ELF)
405         case IH_OS_VXWORKS:
406             do_bootm_vxworks (cmdtp, flag, argc, argv,
407                               addr, len_ptr, verify);
408             break;
409         case IH_OS_QNX:
410             do_bootm_qnxelf (cmdtp, flag, argc, argv,
411                               addr, len_ptr, verify);
412             break;
413 #endif
414 #ifdef CONFIG_ARTOS
415         case IH_OS_ARTOS:
416             do_bootm_artos  (cmdtp, flag, argc, argv,
417                              addr, len_ptr, verify);
418             break;
419 #endif
420         }
421
422         show_boot_progress (-9);
423 #ifdef DEBUG
424         puts ("\n## Control returned to monitor - resetting...\n");
425         do_reset (cmdtp, flag, argc, argv);
426 #endif
427         return 1;
428 }
429
430 U_BOOT_CMD(
431         bootm,  CFG_MAXARGS,    1,      do_bootm,
432         "bootm   - boot application image from memory\n",
433         "[addr [arg ...]]\n    - boot application image stored in memory\n"
434         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
435         "\t'arg' can be the address of an initrd image\n"
436 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
437         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
438         "\ta third argument is required which is the address of the\n"
439         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
440         "\tuse a '-' for the second argument. If you do not pass a third\n"
441         "\ta bd_info struct will be passed instead\n"
442 #endif
443 );
444
445 #ifdef CONFIG_SILENT_CONSOLE
446 static void
447 fixup_silent_linux ()
448 {
449         char buf[256], *start, *end;
450         char *cmdline = getenv ("bootargs");
451
452         /* Only fix cmdline when requested */
453         if (!(gd->flags & GD_FLG_SILENT))
454                 return;
455
456         debug ("before silent fix-up: %s\n", cmdline);
457         if (cmdline) {
458                 if ((start = strstr (cmdline, "console=")) != NULL) {
459                         end = strchr (start, ' ');
460                         strncpy (buf, cmdline, (start - cmdline + 8));
461                         if (end)
462                                 strcpy (buf + (start - cmdline + 8), end);
463                         else
464                                 buf[start - cmdline + 8] = '\0';
465                 } else {
466                         strcpy (buf, cmdline);
467                         strcat (buf, " console=");
468                 }
469         } else {
470                 strcpy (buf, "console=");
471         }
472
473         setenv ("bootargs", buf);
474         debug ("after silent fix-up: %s\n", buf);
475 }
476 #endif /* CONFIG_SILENT_CONSOLE */
477
478 #ifdef CONFIG_PPC
479 static void  __attribute__((noinline))
480 do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
481                 int     argc, char *argv[],
482                 ulong   addr,
483                 ulong   *len_ptr,
484                 int     verify)
485 {
486         ulong   sp;
487         ulong   len;
488         ulong   initrd_start, initrd_end;
489         ulong   cmd_start, cmd_end;
490         ulong   initrd_high;
491         ulong   data;
492         int     initrd_copy_to_ram = 1;
493         char    *cmdline;
494         char    *s;
495         bd_t    *kbd;
496         void    (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
497         image_header_t *hdr = &header;
498 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
499         char    *of_flat_tree = NULL;
500         ulong   of_data = 0;
501 #endif
502
503         if ((s = getenv ("initrd_high")) != NULL) {
504                 /* a value of "no" or a similar string will act like 0,
505                  * turning the "load high" feature off. This is intentional.
506                  */
507                 initrd_high = simple_strtoul(s, NULL, 16);
508                 if (initrd_high == ~0)
509                         initrd_copy_to_ram = 0;
510         } else {        /* not set, no restrictions to load high */
511                 initrd_high = ~0;
512         }
513
514 #ifdef CONFIG_LOGBUFFER
515         kbd=gd->bd;
516         /* Prevent initrd from overwriting logbuffer */
517         if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
518                 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
519         debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
520 #endif
521
522         /*
523          * Booting a (Linux) kernel image
524          *
525          * Allocate space for command line and board info - the
526          * address should be as high as possible within the reach of
527          * the kernel (see CFG_BOOTMAPSZ settings), but in unused
528          * memory, which means far enough below the current stack
529          * pointer.
530          */
531
532         asm( "mr %0,1": "=r"(sp) : );
533
534         debug ("## Current stack ends at 0x%08lX ", sp);
535
536         sp -= 2048;             /* just to be sure */
537         if (sp > CFG_BOOTMAPSZ)
538                 sp = CFG_BOOTMAPSZ;
539         sp &= ~0xF;
540
541         debug ("=> set upper limit to 0x%08lX\n", sp);
542
543         cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
544         kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
545
546         if ((s = getenv("bootargs")) == NULL)
547                 s = "";
548
549         strcpy (cmdline, s);
550
551         cmd_start    = (ulong)&cmdline[0];
552         cmd_end      = cmd_start + strlen(cmdline);
553
554         *kbd = *(gd->bd);
555
556 #ifdef  DEBUG
557         printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
558
559         do_bdinfo (NULL, 0, 0, NULL);
560 #endif
561
562         if ((s = getenv ("clocks_in_mhz")) != NULL) {
563                 /* convert all clock information to MHz */
564                 kbd->bi_intfreq /= 1000000L;
565                 kbd->bi_busfreq /= 1000000L;
566 #if defined(CONFIG_MPC8220)
567         kbd->bi_inpfreq /= 1000000L;
568         kbd->bi_pcifreq /= 1000000L;
569         kbd->bi_pevfreq /= 1000000L;
570         kbd->bi_flbfreq /= 1000000L;
571         kbd->bi_vcofreq /= 1000000L;
572 #endif
573 #if defined(CONFIG_CPM2)
574                 kbd->bi_cpmfreq /= 1000000L;
575                 kbd->bi_brgfreq /= 1000000L;
576                 kbd->bi_sccfreq /= 1000000L;
577                 kbd->bi_vco     /= 1000000L;
578 #endif
579 #if defined(CONFIG_MPC5xxx)
580                 kbd->bi_ipbfreq /= 1000000L;
581                 kbd->bi_pcifreq /= 1000000L;
582 #endif /* CONFIG_MPC5xxx */
583         }
584
585         kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
586
587         /*
588          * Check if there is an initrd image
589          */
590
591 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
592         /* Look for a '-' which indicates to ignore the ramdisk argument */
593         if (argc >= 3 && strcmp(argv[2], "-") ==  0) {
594                         debug ("Skipping initrd\n");
595                         len = data = 0;
596                 }
597         else
598 #endif
599         if (argc >= 3) {
600                 debug ("Not skipping initrd\n");
601                 show_boot_progress (9);
602
603                 addr = simple_strtoul(argv[2], NULL, 16);
604
605                 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
606                 hdr = (image_header_t *)addr;
607
608                 if (!image_check_magic (hdr)) {
609                         puts ("Bad Magic Number\n");
610                         show_boot_progress (-10);
611                         do_reset (cmdtp, flag, argc, argv);
612                 }
613
614                 if (!image_check_hcrc (hdr)) {
615                         puts ("Bad Header Checksum\n");
616                         show_boot_progress (-11);
617                         do_reset (cmdtp, flag, argc, argv);
618                 }
619                 show_boot_progress (10);
620
621                 print_image_hdr (hdr);
622
623                 if (verify) {
624                         puts ("   Verifying Checksum ... ");
625
626                         if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
627                                 puts ("Bad Data CRC\n");
628                                 show_boot_progress (-12);
629                                 do_reset (cmdtp, flag, argc, argv);
630                         }
631                         puts ("OK\n");
632                 }
633
634                 show_boot_progress (11);
635
636                 if (!image_check_os (hdr, IH_OS_LINUX) ||
637                     !image_check_arch (hdr, IH_ARCH_PPC) ||
638                     !image_check_type (hdr, IH_TYPE_RAMDISK)) {
639                         puts ("No Linux PPC Ramdisk Image\n");
640                         show_boot_progress (-13);
641                         do_reset (cmdtp, flag, argc, argv);
642                 }
643
644                 data = image_get_data (hdr);
645                 len = image_get_data_size (hdr);
646
647                 /*
648                  * Now check if we have a multifile image
649                  */
650         } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
651                 u_long tail    = image_to_cpu (len_ptr[0]) % 4;
652                 int i;
653
654                 show_boot_progress (13);
655
656                 /* skip kernel length and terminator */
657                 data = (ulong)(&len_ptr[2]);
658                 /* skip any additional image length fields */
659                 for (i=1; len_ptr[i]; ++i)
660                         data += 4;
661                 /* add kernel length, and align */
662                 data += image_to_cpu (len_ptr[0]);
663                 if (tail) {
664                         data += 4 - tail;
665                 }
666
667                 len   = image_to_cpu (len_ptr[1]);
668
669         } else {
670                 /*
671                  * no initrd image
672                  */
673                 show_boot_progress (14);
674
675                 len = data = 0;
676         }
677
678 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
679         if(argc > 3) {
680                 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
681                 hdr = (image_header_t *)of_flat_tree;
682 #if defined(CONFIG_OF_FLAT_TREE)
683                 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
684 #else
685                 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
686 #endif
687 #ifndef CFG_NO_FLASH
688                         if (addr2info((ulong)of_flat_tree) != NULL)
689                                 of_data = (ulong)of_flat_tree;
690 #endif
691                 } else if (image_check_magic (hdr)) {
692                         printf("## Flat Device Tree at %08lX\n", hdr);
693                         print_image_hdr (hdr);
694
695                         if ((image_get_load (hdr) <  ((unsigned long)hdr + image_get_image_size (hdr))) &&
696                            ((image_get_load (hdr) + image_get_data_size (hdr)) > (unsigned long)hdr)) {
697                                 puts ("ERROR: fdt overwritten - "
698                                         "must RESET the board to recover.\n");
699                                 do_reset (cmdtp, flag, argc, argv);
700                         }
701
702                         puts ("   Verifying Checksum ... ");
703                         if (!image_check_hcrc (hdr)) {
704                                 puts ("ERROR: fdt header checksum invalid - "
705                                         "must RESET the board to recover.\n");
706                                 do_reset (cmdtp, flag, argc, argv);
707                         }
708
709                         if (!image_check_dcrc (hdr)) {
710                                 puts ("ERROR: fdt checksum invalid - "
711                                         "must RESET the board to recover.\n");
712                                 do_reset (cmdtp, flag, argc, argv);
713                         }
714                         puts ("OK\n");
715
716                         if (!image_check_type (hdr, IH_TYPE_FLATDT)) {
717                                 puts ("ERROR: uImage is not a fdt - "
718                                         "must RESET the board to recover.\n");
719                                 do_reset (cmdtp, flag, argc, argv);
720                         }
721                         if (image_get_comp (hdr) != IH_COMP_NONE) {
722                                 puts ("ERROR: uImage is compressed - "
723                                         "must RESET the board to recover.\n");
724                                 do_reset (cmdtp, flag, argc, argv);
725                         }
726 #if defined(CONFIG_OF_FLAT_TREE)
727                         if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
728 #else
729                         if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
730 #endif
731                                 puts ("ERROR: uImage data is not a fdt - "
732                                         "must RESET the board to recover.\n");
733                                 do_reset (cmdtp, flag, argc, argv);
734                         }
735
736                         memmove ((void *)image_get_load (hdr),
737                                 (void *)(of_flat_tree + image_get_header_size ()),
738                                 image_get_data_size (hdr));
739
740                         of_flat_tree = (char *)image_get_load (hdr);
741                 } else {
742                         puts ("Did not find a flat Flat Device Tree.\n"
743                                 "Must RESET the board to recover.\n");
744                         do_reset (cmdtp, flag, argc, argv);
745                 }
746                 printf ("   Booting using the fdt at 0x%x\n",
747                                 of_flat_tree);
748         } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
749                 u_long tail    = image_to_cpu (len_ptr[0]) % 4;
750                 int i;
751
752                 /* skip kernel length, initrd length, and terminator */
753                 of_flat_tree = (char *)(&len_ptr[3]);
754                 /* skip any additional image length fields */
755                 for (i=2; len_ptr[i]; ++i)
756                         of_flat_tree += 4;
757                 /* add kernel length, and align */
758                 of_flat_tree += image_to_cpu (len_ptr[0]);
759                 if (tail) {
760                         of_flat_tree += 4 - tail;
761                 }
762
763                 /* add initrd length, and align */
764                 tail = image_to_cpu (len_ptr[1]) % 4;
765                 of_flat_tree += image_to_cpu (len_ptr[1]);
766                 if (tail) {
767                         of_flat_tree += 4 - tail;
768                 }
769
770 #ifndef CFG_NO_FLASH
771                 /* move the blob if it is in flash (set of_data to !null) */
772                 if (addr2info ((ulong)of_flat_tree) != NULL)
773                         of_data = (ulong)of_flat_tree;
774 #endif
775
776
777 #if defined(CONFIG_OF_FLAT_TREE)
778                 if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
779 #else
780                 if (fdt_check_header (of_flat_tree) != 0) {
781 #endif
782                         puts ("ERROR: image is not a fdt - "
783                                 "must RESET the board to recover.\n");
784                         do_reset (cmdtp, flag, argc, argv);
785                 }
786
787 #if defined(CONFIG_OF_FLAT_TREE)
788                 if (((struct boot_param_header *)of_flat_tree)->totalsize !=
789                         image_to_cpu (len_ptr[2])) {
790 #else
791                 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) !=
792                         image_to_cpu (len_ptr[2])) {
793 #endif
794                         puts ("ERROR: fdt size != image size - "
795                                 "must RESET the board to recover.\n");
796                         do_reset (cmdtp, flag, argc, argv);
797                 }
798         }
799 #endif
800         if (!data) {
801                 debug ("No initrd\n");
802         }
803
804         if (data) {
805             if (!initrd_copy_to_ram) {  /* zero-copy ramdisk support */
806                 initrd_start = data;
807                 initrd_end = initrd_start + len;
808             } else {
809                 initrd_start  = (ulong)kbd - len;
810                 initrd_start &= ~(4096 - 1);    /* align on page */
811
812                 if (initrd_high) {
813                         ulong nsp;
814
815                         /*
816                          * the inital ramdisk does not need to be within
817                          * CFG_BOOTMAPSZ as it is not accessed until after
818                          * the mm system is initialised.
819                          *
820                          * do the stack bottom calculation again and see if
821                          * the initrd will fit just below the monitor stack
822                          * bottom without overwriting the area allocated
823                          * above for command line args and board info.
824                          */
825                         asm( "mr %0,1": "=r"(nsp) : );
826                         nsp -= 2048;            /* just to be sure */
827                         nsp &= ~0xF;
828                         if (nsp > initrd_high)  /* limit as specified */
829                                 nsp = initrd_high;
830                         nsp -= len;
831                         nsp &= ~(4096 - 1);     /* align on page */
832                         if (nsp >= sp)
833                                 initrd_start = nsp;
834                 }
835
836                 show_boot_progress (12);
837
838                 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
839                         data, data + len - 1, len, len);
840
841                 initrd_end    = initrd_start + len;
842                 printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
843                         initrd_start, initrd_end);
844 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
845                 {
846                         size_t l = len;
847                         void *to = (void *)initrd_start;
848                         void *from = (void *)data;
849
850                         while (l > 0) {
851                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
852                                 WATCHDOG_RESET();
853                                 memmove (to, from, tail);
854                                 to += tail;
855                                 from += tail;
856                                 l -= tail;
857                         }
858                 }
859 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
860                 memmove ((void *)initrd_start, (void *)data, len);
861 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
862                 puts ("OK\n");
863             }
864         } else {
865                 initrd_start = 0;
866                 initrd_end = 0;
867         }
868
869 #if defined(CONFIG_OF_LIBFDT)
870
871 #ifdef CFG_BOOTMAPSZ
872         /*
873          * The blob must be within CFG_BOOTMAPSZ,
874          * so we flag it to be copied if it is not.
875          */
876         if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
877                 of_data = (ulong)of_flat_tree;
878 #endif
879
880         /* move of_flat_tree if needed */
881         if (of_data) {
882                 int err;
883                 ulong of_start, of_len;
884
885                 of_len = be32_to_cpu(fdt_totalsize(of_data));
886
887                 /* position on a 4K boundary before the kbd */
888                 of_start  = (ulong)kbd - of_len;
889                 of_start &= ~(4096 - 1);        /* align on page */
890                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
891                         of_data, of_data + of_len - 1, of_len, of_len);
892
893                 of_flat_tree = (char *)of_start;
894                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
895                         of_start, of_start + of_len - 1);
896                 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
897                 if (err != 0) {
898                         puts ("ERROR: fdt move failed - "
899                                 "must RESET the board to recover.\n");
900                         do_reset (cmdtp, flag, argc, argv);
901                 }
902                 puts ("OK\n");
903         }
904         /*
905          * Add the chosen node if it doesn't exist, add the env and bd_t
906          * if the user wants it (the logic is in the subroutines).
907          */
908         if (of_flat_tree) {
909                 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
910                         puts ("ERROR: /chosen node create failed - "
911                                 "must RESET the board to recover.\n");
912                         do_reset (cmdtp, flag, argc, argv);
913                 }
914 #ifdef CONFIG_OF_HAS_UBOOT_ENV
915                 if (fdt_env(of_flat_tree) < 0) {
916                         puts ("ERROR: /u-boot-env node create failed - "
917                                 "must RESET the board to recover.\n");
918                         do_reset (cmdtp, flag, argc, argv);
919                 }
920 #endif
921 #ifdef CONFIG_OF_HAS_BD_T
922                 if (fdt_bd_t(of_flat_tree) < 0) {
923                         puts ("ERROR: /bd_t node create failed - "
924                                 "must RESET the board to recover.\n");
925                         do_reset (cmdtp, flag, argc, argv);
926                 }
927 #endif
928 #ifdef CONFIG_OF_BOARD_SETUP
929                 /* Call the board-specific fixup routine */
930                 ft_board_setup(of_flat_tree, gd->bd);
931 #endif
932         }
933 #endif /* CONFIG_OF_LIBFDT */
934 #if defined(CONFIG_OF_FLAT_TREE)
935 #ifdef CFG_BOOTMAPSZ
936         /*
937          * The blob must be within CFG_BOOTMAPSZ,
938          * so we flag it to be copied if it is not.
939          */
940         if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
941                 of_data = (ulong)of_flat_tree;
942 #endif
943
944         /* move of_flat_tree if needed */
945         if (of_data) {
946                 ulong of_start, of_len;
947                 of_len = ((struct boot_param_header *)of_data)->totalsize;
948
949                 /* provide extra 8k pad */
950                 of_start  = (ulong)kbd - of_len - 8192;
951                 of_start &= ~(4096 - 1);        /* align on page */
952                 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
953                         of_data, of_data + of_len - 1, of_len, of_len);
954
955                 of_flat_tree = (char *)of_start;
956                 printf ("   Loading Device Tree to %08lx, end %08lx ... ",
957                         of_start, of_start + of_len - 1);
958                 memmove ((void *)of_start, (void *)of_data, of_len);
959                 puts ("OK\n");
960         }
961         /*
962          * Create the /chosen node and modify the blob with board specific
963          * values as needed.
964          */
965         ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
966         /* ft_dump_blob(of_flat_tree); */
967 #endif
968         debug ("## Transferring control to Linux (at address %08lx) ...\n",
969                 (ulong)kernel);
970
971         show_boot_progress (15);
972
973 #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
974         unlock_ram_in_cache();
975 #endif
976
977 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
978         if (of_flat_tree) {     /* device tree; boot new style */
979                 /*
980                  * Linux Kernel Parameters (passing device tree):
981                  *   r3: pointer to the fdt, followed by the board info data
982                  *   r4: physical pointer to the kernel itself
983                  *   r5: NULL
984                  *   r6: NULL
985                  *   r7: NULL
986                  */
987                 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
988                 /* does not return */
989         }
990 #endif
991         /*
992          * Linux Kernel Parameters (passing board info data):
993          *   r3: ptr to board info data
994          *   r4: initrd_start or 0 if no initrd
995          *   r5: initrd_end - unused if r4 is 0
996          *   r6: Start of command line string
997          *   r7: End   of command line string
998          */
999         (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
1000         /* does not return */
1001 }
1002 #endif /* CONFIG_PPC */
1003
1004 static void
1005 do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1006                 int     argc, char *argv[],
1007                 ulong   addr,
1008                 ulong   *len_ptr,
1009                 int     verify)
1010 {
1011         image_header_t *hdr = &header;
1012
1013         void    (*loader)(bd_t *, image_header_t *, char *, char *);
1014         image_header_t *img_addr;
1015         char     *consdev;
1016         char     *cmdline;
1017
1018
1019         /*
1020          * Booting a (NetBSD) kernel image
1021          *
1022          * This process is pretty similar to a standalone application:
1023          * The (first part of an multi-) image must be a stage-2 loader,
1024          * which in turn is responsible for loading & invoking the actual
1025          * kernel.  The only differences are the parameters being passed:
1026          * besides the board info strucure, the loader expects a command
1027          * line, the name of the console device, and (optionally) the
1028          * address of the original image header.
1029          */
1030
1031         img_addr = 0;
1032         if ((image_check_type (hdr, IH_TYPE_MULTI)) && (len_ptr[1]))
1033                 img_addr = (image_header_t *) addr;
1034
1035
1036         consdev = "";
1037 #if   defined (CONFIG_8xx_CONS_SMC1)
1038         consdev = "smc1";
1039 #elif defined (CONFIG_8xx_CONS_SMC2)
1040         consdev = "smc2";
1041 #elif defined (CONFIG_8xx_CONS_SCC2)
1042         consdev = "scc2";
1043 #elif defined (CONFIG_8xx_CONS_SCC3)
1044         consdev = "scc3";
1045 #endif
1046
1047         if (argc > 2) {
1048                 ulong len;
1049                 int   i;
1050
1051                 for (i=2, len=0 ; i<argc ; i+=1)
1052                         len += strlen (argv[i]) + 1;
1053                 cmdline = malloc (len);
1054
1055                 for (i=2, len=0 ; i<argc ; i+=1) {
1056                         if (i > 2)
1057                                 cmdline[len++] = ' ';
1058                         strcpy (&cmdline[len], argv[i]);
1059                         len += strlen (argv[i]);
1060                 }
1061         } else if ((cmdline = getenv("bootargs")) == NULL) {
1062                 cmdline = "";
1063         }
1064
1065         loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
1066
1067         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1068                 (ulong)loader);
1069
1070         show_boot_progress (15);
1071
1072         /*
1073          * NetBSD Stage-2 Loader Parameters:
1074          *   r3: ptr to board info data
1075          *   r4: image address
1076          *   r5: console device
1077          *   r6: boot args string
1078          */
1079         (*loader) (gd->bd, img_addr, consdev, cmdline);
1080 }
1081
1082 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1083
1084 /* Function that returns a character from the environment */
1085 extern uchar (*env_get_char)(int);
1086
1087 static void
1088 do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1089                 int     argc, char *argv[],
1090                 ulong   addr,
1091                 ulong   *len_ptr,
1092                 int     verify)
1093 {
1094         ulong top;
1095         char *s, *cmdline;
1096         char **fwenv, **ss;
1097         int i, j, nxt, len, envno, envsz;
1098         bd_t *kbd;
1099         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1100         image_header_t *hdr = &header;
1101
1102         /*
1103          * Booting an ARTOS kernel image + application
1104          */
1105
1106         /* this used to be the top of memory, but was wrong... */
1107 #ifdef CONFIG_PPC
1108         /* get stack pointer */
1109         asm volatile ("mr %0,1" : "=r"(top) );
1110 #endif
1111         debug ("## Current stack ends at 0x%08lX ", top);
1112
1113         top -= 2048;            /* just to be sure */
1114         if (top > CFG_BOOTMAPSZ)
1115                 top = CFG_BOOTMAPSZ;
1116         top &= ~0xF;
1117
1118         debug ("=> set upper limit to 0x%08lX\n", top);
1119
1120         /* first check the artos specific boot args, then the linux args*/
1121         if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1122                 s = "";
1123
1124         /* get length of cmdline, and place it */
1125         len = strlen(s);
1126         top = (top - (len + 1)) & ~0xF;
1127         cmdline = (char *)top;
1128         debug ("## cmdline at 0x%08lX ", top);
1129         strcpy(cmdline, s);
1130
1131         /* copy bdinfo */
1132         top = (top - sizeof(bd_t)) & ~0xF;
1133         debug ("## bd at 0x%08lX ", top);
1134         kbd = (bd_t *)top;
1135         memcpy(kbd, gd->bd, sizeof(bd_t));
1136
1137         /* first find number of env entries, and their size */
1138         envno = 0;
1139         envsz = 0;
1140         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1141                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1142                         ;
1143                 envno++;
1144                 envsz += (nxt - i) + 1; /* plus trailing zero */
1145         }
1146         envno++;        /* plus the terminating zero */
1147         debug ("## %u envvars total size %u ", envno, envsz);
1148
1149         top = (top - sizeof(char **)*envno) & ~0xF;
1150         fwenv = (char **)top;
1151         debug ("## fwenv at 0x%08lX ", top);
1152
1153         top = (top - envsz) & ~0xF;
1154         s = (char *)top;
1155         ss = fwenv;
1156
1157         /* now copy them */
1158         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1159                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1160                         ;
1161                 *ss++ = s;
1162                 for (j = i; j < nxt; ++j)
1163                         *s++ = env_get_char(j);
1164                 *s++ = '\0';
1165         }
1166         *ss++ = NULL;   /* terminate */
1167
1168         entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
1169         (*entry)(kbd, cmdline, fwenv, top);
1170 }
1171 #endif
1172
1173
1174 #if defined(CONFIG_CMD_BOOTD)
1175 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1176 {
1177         int rcode = 0;
1178 #ifndef CFG_HUSH_PARSER
1179         if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1180 #else
1181         if (parse_string_outer(getenv("bootcmd"),
1182                 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1183 #endif
1184         return rcode;
1185 }
1186
1187 U_BOOT_CMD(
1188         boot,   1,      1,      do_bootd,
1189         "boot    - boot default, i.e., run 'bootcmd'\n",
1190         NULL
1191 );
1192
1193 /* keep old command name "bootd" for backward compatibility */
1194 U_BOOT_CMD(
1195         bootd, 1,       1,      do_bootd,
1196         "bootd   - boot default, i.e., run 'bootcmd'\n",
1197         NULL
1198 );
1199
1200 #endif
1201
1202 #if defined(CONFIG_CMD_IMI)
1203 int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1204 {
1205         int     arg;
1206         ulong   addr;
1207         int     rcode=0;
1208
1209         if (argc < 2) {
1210                 return image_info (load_addr);
1211         }
1212
1213         for (arg=1; arg <argc; ++arg) {
1214                 addr = simple_strtoul(argv[arg], NULL, 16);
1215                 if (image_info (addr) != 0) rcode = 1;
1216         }
1217         return rcode;
1218 }
1219
1220 static int image_info (ulong addr)
1221 {
1222         image_header_t *hdr = (image_header_t *)addr;
1223
1224         printf ("\n## Checking Image at %08lx ...\n", addr);
1225
1226         if (!image_check_magic (hdr)) {
1227                 puts ("   Bad Magic Number\n");
1228                 return 1;
1229         }
1230
1231         if (!image_check_hcrc (hdr)) {
1232                 puts ("   Bad Header Checksum\n");
1233                 return 1;
1234         }
1235
1236         print_image_hdr (hdr);
1237
1238         puts ("   Verifying Checksum ... ");
1239         if (!image_check_dcrc (hdr)) {
1240                 puts ("   Bad Data CRC\n");
1241                 return 1;
1242         }
1243         puts ("OK\n");
1244         return 0;
1245 }
1246
1247 U_BOOT_CMD(
1248         iminfo, CFG_MAXARGS,    1,      do_iminfo,
1249         "iminfo  - print header information for application image\n",
1250         "addr [addr ...]\n"
1251         "    - print header information for application image starting at\n"
1252         "      address 'addr' in memory; this includes verification of the\n"
1253         "      image contents (magic number, header and payload checksums)\n"
1254 );
1255
1256 #endif
1257
1258 #if defined(CONFIG_CMD_IMLS)
1259 /*-----------------------------------------------------------------------
1260  * List all images found in flash.
1261  */
1262 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1263 {
1264         flash_info_t *info;
1265         int i, j;
1266         image_header_t *hdr;
1267
1268         for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1269                 if (info->flash_id == FLASH_UNKNOWN)
1270                         goto next_bank;
1271                 for (j=0; j<info->sector_count; ++j) {
1272
1273                         hdr = (image_header_t *)info->start[j];
1274
1275                         if (!hdr || !image_check_magic (hdr))
1276                                 goto next_sector;
1277
1278                         if (!image_check_hcrc (hdr))
1279                                 goto next_sector;
1280
1281                         printf ("Image at %08lX:\n", (ulong)hdr);
1282                         print_image_hdr (hdr);
1283
1284                         puts ("   Verifying Checksum ... ");
1285                         if (!image_check_dcrc (hdr)) {
1286                                 puts ("Bad Data CRC\n");
1287                         } else {
1288                                 puts ("OK\n");
1289                         }
1290 next_sector:            ;
1291                 }
1292 next_bank:      ;
1293         }
1294
1295         return (0);
1296 }
1297
1298 U_BOOT_CMD(
1299         imls,   1,              1,      do_imls,
1300         "imls    - list all images found in flash\n",
1301         "\n"
1302         "    - Prints information about all images found at sector\n"
1303         "      boundaries in flash.\n"
1304 );
1305 #endif
1306
1307 void
1308 print_image_hdr (image_header_t *hdr)
1309 {
1310 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
1311         time_t timestamp = (time_t)image_get_time (hdr);
1312         struct rtc_time tm;
1313 #endif
1314
1315         printf ("   Image Name:   %.*s\n", IH_NMLEN, image_get_name (hdr));
1316 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
1317         to_tm (timestamp, &tm);
1318         printf ("   Created:      %4d-%02d-%02d  %2d:%02d:%02d UTC\n",
1319                 tm.tm_year, tm.tm_mon, tm.tm_mday,
1320                 tm.tm_hour, tm.tm_min, tm.tm_sec);
1321 #endif
1322         puts ("   Image Type:   "); print_type(hdr);
1323         printf ("\n   Data Size:    %d Bytes = ", image_get_data_size (hdr));
1324         print_size (image_get_data_size (hdr), "\n");
1325         printf ("   Load Address: %08x\n"
1326                 "   Entry Point:  %08x\n",
1327                  image_get_load (hdr), image_get_ep (hdr));
1328
1329         if (image_check_type (hdr, IH_TYPE_MULTI)) {
1330                 int i;
1331                 ulong len;
1332                 ulong *len_ptr = (ulong *)((ulong)hdr + image_get_header_size ());
1333
1334                 puts ("   Contents:\n");
1335                 for (i=0; (len = image_to_cpu (*len_ptr)); ++i, ++len_ptr) {
1336                         printf ("   Image %d: %8ld Bytes = ", i, len);
1337                         print_size (len, "\n");
1338                 }
1339         }
1340 }
1341
1342
1343 static void
1344 print_type (image_header_t *hdr)
1345 {
1346         char *os, *arch, *type, *comp;
1347
1348         switch (image_get_os (hdr)) {
1349         case IH_OS_INVALID:     os = "Invalid OS";              break;
1350         case IH_OS_NETBSD:      os = "NetBSD";                  break;
1351         case IH_OS_LINUX:       os = "Linux";                   break;
1352         case IH_OS_VXWORKS:     os = "VxWorks";                 break;
1353         case IH_OS_QNX:         os = "QNX";                     break;
1354         case IH_OS_U_BOOT:      os = "U-Boot";                  break;
1355         case IH_OS_RTEMS:       os = "RTEMS";                   break;
1356 #ifdef CONFIG_ARTOS
1357         case IH_OS_ARTOS:       os = "ARTOS";                   break;
1358 #endif
1359 #ifdef CONFIG_LYNXKDI
1360         case IH_OS_LYNXOS:      os = "LynxOS";                  break;
1361 #endif
1362         default:                os = "Unknown OS";              break;
1363         }
1364
1365         switch (image_get_arch (hdr)) {
1366         case IH_ARCH_INVALID:   arch = "Invalid CPU";           break;
1367         case IH_ARCH_ALPHA:     arch = "Alpha";                 break;
1368         case IH_ARCH_ARM:       arch = "ARM";                   break;
1369         case IH_ARCH_AVR32:     arch = "AVR32";                 break;
1370         case IH_ARCH_BLACKFIN:  arch = "Blackfin";              break;
1371         case IH_ARCH_I386:      arch = "Intel x86";             break;
1372         case IH_ARCH_IA64:      arch = "IA64";                  break;
1373         case IH_ARCH_M68K:      arch = "M68K";                  break;
1374         case IH_ARCH_MICROBLAZE:arch = "Microblaze";            break;
1375         case IH_ARCH_MIPS64:    arch = "MIPS 64 Bit";           break;
1376         case IH_ARCH_MIPS:      arch = "MIPS";                  break;
1377         case IH_ARCH_NIOS2:     arch = "Nios-II";               break;
1378         case IH_ARCH_NIOS:      arch = "Nios";                  break;
1379         case IH_ARCH_PPC:       arch = "PowerPC";               break;
1380         case IH_ARCH_S390:      arch = "IBM S390";              break;
1381         case IH_ARCH_SH:        arch = "SuperH";                break;
1382         case IH_ARCH_SPARC64:   arch = "SPARC 64 Bit";          break;
1383         case IH_ARCH_SPARC:     arch = "SPARC";                 break;
1384         default:                arch = "Unknown Architecture";  break;
1385         }
1386
1387         switch (image_get_type (hdr)) {
1388         case IH_TYPE_INVALID:   type = "Invalid Image";         break;
1389         case IH_TYPE_STANDALONE:type = "Standalone Program";    break;
1390         case IH_TYPE_KERNEL:    type = "Kernel Image";          break;
1391         case IH_TYPE_RAMDISK:   type = "RAMDisk Image";         break;
1392         case IH_TYPE_MULTI:     type = "Multi-File Image";      break;
1393         case IH_TYPE_FIRMWARE:  type = "Firmware";              break;
1394         case IH_TYPE_SCRIPT:    type = "Script";                break;
1395         case IH_TYPE_FLATDT:    type = "Flat Device Tree";      break;
1396         default:                type = "Unknown Image";         break;
1397         }
1398
1399         switch (image_get_comp (hdr)) {
1400         case IH_COMP_NONE:      comp = "uncompressed";          break;
1401         case IH_COMP_GZIP:      comp = "gzip compressed";       break;
1402         case IH_COMP_BZIP2:     comp = "bzip2 compressed";      break;
1403         default:                comp = "unknown compression";   break;
1404         }
1405
1406         printf ("%s %s %s (%s)", arch, os, type, comp);
1407 }
1408
1409 #define ZALLOC_ALIGNMENT        16
1410
1411 static void *zalloc(void *x, unsigned items, unsigned size)
1412 {
1413         void *p;
1414
1415         size *= items;
1416         size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1417
1418         p = malloc (size);
1419
1420         return (p);
1421 }
1422
1423 static void zfree(void *x, void *addr, unsigned nb)
1424 {
1425         free (addr);
1426 }
1427
1428 #define HEAD_CRC        2
1429 #define EXTRA_FIELD     4
1430 #define ORIG_NAME       8
1431 #define COMMENT         0x10
1432 #define RESERVED        0xe0
1433
1434 #define DEFLATED        8
1435
1436 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
1437 {
1438         z_stream s;
1439         int r, i, flags;
1440
1441         /* skip header */
1442         i = 10;
1443         flags = src[3];
1444         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
1445                 puts ("Error: Bad gzipped data\n");
1446                 return (-1);
1447         }
1448         if ((flags & EXTRA_FIELD) != 0)
1449                 i = 12 + src[10] + (src[11] << 8);
1450         if ((flags & ORIG_NAME) != 0)
1451                 while (src[i++] != 0)
1452                         ;
1453         if ((flags & COMMENT) != 0)
1454                 while (src[i++] != 0)
1455                         ;
1456         if ((flags & HEAD_CRC) != 0)
1457                 i += 2;
1458         if (i >= *lenp) {
1459                 puts ("Error: gunzip out of data in header\n");
1460                 return (-1);
1461         }
1462
1463         s.zalloc = zalloc;
1464         s.zfree = zfree;
1465 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1466         s.outcb = (cb_func)WATCHDOG_RESET;
1467 #else
1468         s.outcb = Z_NULL;
1469 #endif  /* CONFIG_HW_WATCHDOG */
1470
1471         r = inflateInit2(&s, -MAX_WBITS);
1472         if (r != Z_OK) {
1473                 printf ("Error: inflateInit2() returned %d\n", r);
1474                 return (-1);
1475         }
1476         s.next_in = src + i;
1477         s.avail_in = *lenp - i;
1478         s.next_out = dst;
1479         s.avail_out = dstlen;
1480         r = inflate(&s, Z_FINISH);
1481         if (r != Z_OK && r != Z_STREAM_END) {
1482                 printf ("Error: inflate() returned %d\n", r);
1483                 return (-1);
1484         }
1485         *lenp = s.next_out - (unsigned char *) dst;
1486         inflateEnd(&s);
1487
1488         return (0);
1489 }
1490
1491 #ifdef CONFIG_BZIP2
1492 void bz_internal_error(int errcode)
1493 {
1494         printf ("BZIP2 internal error %d\n", errcode);
1495 }
1496 #endif /* CONFIG_BZIP2 */
1497
1498 static void
1499 do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1500                 ulong addr, ulong *len_ptr, int verify)
1501 {
1502         image_header_t *hdr = &header;
1503         void    (*entry_point)(bd_t *);
1504
1505         entry_point = (void (*)(bd_t *))image_get_ep (hdr);
1506
1507         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1508                 (ulong)entry_point);
1509
1510         show_boot_progress (15);
1511
1512         /*
1513          * RTEMS Parameters:
1514          *   r3: ptr to board info data
1515          */
1516
1517         (*entry_point ) ( gd->bd );
1518 }
1519
1520 #if defined(CONFIG_CMD_ELF)
1521 static void
1522 do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1523                   ulong addr, ulong *len_ptr, int verify)
1524 {
1525         image_header_t *hdr = &header;
1526         char str[80];
1527
1528         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
1529         setenv("loadaddr", str);
1530         do_bootvx(cmdtp, 0, 0, NULL);
1531 }
1532
1533 static void
1534 do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1535                  ulong addr, ulong *len_ptr, int verify)
1536 {
1537         image_header_t *hdr = &header;
1538         char *local_args[2];
1539         char str[16];
1540
1541         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
1542         local_args[0] = argv[0];
1543         local_args[1] = str;    /* and provide it via the arguments */
1544         do_bootelf(cmdtp, 0, 2, local_args);
1545 }
1546 #endif
1547
1548 #ifdef CONFIG_LYNXKDI
1549 static void
1550 do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1551                  int    argc, char *argv[],
1552                  ulong  addr,
1553                  ulong  *len_ptr,
1554                  int    verify)
1555 {
1556         lynxkdi_boot( &header );
1557 }
1558
1559 #endif /* CONFIG_LYNXKDI */