]> git.sur5r.net Git - u-boot/blob - common/cmd_bootm.c
[new uImage] Cleanup OF/FDT #if/#elif/#endif use in do_bootm_linux()
[u-boot] / 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 #elif defined(CONFIG_OF_FLAT_TREE)
44 #include <ft_build.h>
45 #endif
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 /*cmd_boot.c*/
50 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
51
52 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
53 #include <rtc.h>
54 #endif
55
56 #ifdef CFG_HUSH_PARSER
57 #include <hush.h>
58 #endif
59
60 #ifdef CONFIG_HAS_DATAFLASH
61 #include <dataflash.h>
62 #endif
63
64 /*
65  * Some systems (for example LWMON) have very short watchdog periods;
66  * we must make sure to split long operations like memmove() or
67  * crc32() into reasonable chunks.
68  */
69 #define CHUNKSZ (64 * 1024)
70
71 int  gunzip (void *, int, unsigned char *, unsigned long *);
72
73 static void *zalloc(void *, unsigned, unsigned);
74 static void zfree(void *, void *, unsigned);
75
76 #if defined(CONFIG_CMD_IMI)
77 static int image_info (unsigned long addr);
78 #endif
79
80 #if defined(CONFIG_CMD_IMLS)
81 #include <flash.h>
82 extern flash_info_t flash_info[]; /* info for FLASH chips */
83 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
84 #endif
85
86 static void print_type (image_header_t *hdr);
87
88 #ifdef __I386__
89 image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
90 #endif
91
92 /*
93  *  Continue booting an OS image; caller already has:
94  *  - copied image header to global variable `header'
95  *  - checked header magic number, checksums (both header & image),
96  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
97  *  - loaded (first part of) image to header load address,
98  *  - disabled interrupts.
99  */
100 typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
101                           int   argc, char *argv[],
102                           ulong addr,           /* of image to boot */
103                           ulong *len_ptr,       /* multi-file image length table */
104                           int   verify);        /* getenv("verify")[0] != 'n' */
105
106 extern boot_os_Fcn do_bootm_linux;
107
108 #ifdef CONFIG_SILENT_CONSOLE
109 static void fixup_silent_linux (void);
110 #endif
111 static boot_os_Fcn do_bootm_netbsd;
112 static boot_os_Fcn do_bootm_rtems;
113 #if defined(CONFIG_CMD_ELF)
114 static boot_os_Fcn do_bootm_vxworks;
115 static boot_os_Fcn do_bootm_qnxelf;
116 int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
117 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
118 #endif
119 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
120 static boot_os_Fcn do_bootm_artos;
121 #endif
122 #ifdef CONFIG_LYNXKDI
123 static boot_os_Fcn do_bootm_lynxkdi;
124 extern void lynxkdi_boot( image_header_t * );
125 #endif
126
127 #ifndef CFG_BOOTM_LEN
128 #define CFG_BOOTM_LEN   0x800000        /* use 8MByte as default max gunzip size */
129 #endif
130
131 image_header_t header;
132
133 ulong load_addr = CFG_LOAD_ADDR;                /* Default Load Address */
134
135 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
136 {
137         ulong   iflag;
138         ulong   addr;
139         ulong   data, len;
140         ulong  *len_ptr;
141         uint    unc_len = CFG_BOOTM_LEN;
142         int     i, verify;
143         char    *name, *s;
144         int     (*appl)(int, char *[]);
145         image_header_t *hdr = &header;
146
147         verify = getenv_verify ();
148
149         if (argc < 2) {
150                 addr = load_addr;
151         } else {
152                 addr = simple_strtoul(argv[1], NULL, 16);
153         }
154
155         show_boot_progress (1);
156         printf ("## Booting image at %08lx ...\n", addr);
157
158         /* Copy header so we can blank CRC field for re-calculation */
159 #ifdef CONFIG_HAS_DATAFLASH
160         if (addr_dataflash(addr)){
161                 read_dataflash (addr, image_get_header_size (), (char *)&header);
162         } else
163 #endif
164         memmove (&header, (char *)addr, image_get_header_size ());
165
166         if (!image_check_magic (hdr)) {
167 #ifdef __I386__ /* correct image format not implemented yet - fake it */
168                 if (fake_header(hdr, (void*)addr, -1) != NULL) {
169                         /* to compensate for the addition below */
170                         addr -= image_get_header_size ();
171                         /* turnof verify,
172                          * fake_header() does not fake the data crc
173                          */
174                         verify = 0;
175                 } else
176 #endif  /* __I386__ */
177             {
178                 puts ("Bad Magic Number\n");
179                 show_boot_progress (-1);
180                 return 1;
181             }
182         }
183         show_boot_progress (2);
184
185         if (!image_check_hcrc (hdr)) {
186                 puts ("Bad Header Checksum\n");
187                 show_boot_progress (-2);
188                 return 1;
189         }
190         show_boot_progress (3);
191
192 #ifdef CONFIG_HAS_DATAFLASH
193         if (addr_dataflash(addr)){
194                 len  = image_get_image_size (hdr);
195                 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
196                 addr = CFG_LOAD_ADDR;
197         }
198 #endif
199
200
201         /* for multi-file images we need the data part, too */
202         print_image_hdr ((image_header_t *)addr);
203
204         len = image_get_data_size (hdr);
205         data = addr + image_get_header_size ();
206         len_ptr = (ulong *)data;
207
208         if (verify) {
209                 puts ("   Verifying Checksum ... ");
210                 if (!image_check_dcrc ((image_header_t *)addr)) {
211                         printf ("Bad Data CRC\n");
212                         show_boot_progress (-3);
213                         return 1;
214                 }
215                 puts ("OK\n");
216         }
217         show_boot_progress (4);
218
219         if (!image_check_target_arch (hdr)) {
220                 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
221                 show_boot_progress (-4);
222                 return 1;
223         }
224         show_boot_progress (5);
225
226         switch (image_get_type (hdr)) {
227         case IH_TYPE_STANDALONE:
228                 name = "Standalone Application";
229                 /* A second argument overwrites the load address */
230                 if (argc > 2) {
231                         image_set_load (hdr, simple_strtoul (argv[2], NULL, 16));
232                 }
233                 break;
234         case IH_TYPE_KERNEL:
235                 name = "Kernel Image";
236                 break;
237         case IH_TYPE_MULTI:
238                 name = "Multi-File Image";
239                 len  = image_to_cpu (len_ptr[0]);
240                 /* OS kernel is always the first image */
241                 data += 8; /* kernel_len + terminator */
242                 for (i=1; len_ptr[i]; ++i)
243                         data += 4;
244                 break;
245         default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
246                 show_boot_progress (-5);
247                 return 1;
248         }
249         show_boot_progress (6);
250
251         /*
252          * We have reached the point of no return: we are going to
253          * overwrite all exception vector code, so we cannot easily
254          * recover from any failures any more...
255          */
256
257         iflag = disable_interrupts();
258
259 #ifdef CONFIG_AMIGAONEG3SE
260         /*
261          * We've possible left the caches enabled during
262          * bios emulation, so turn them off again
263          */
264         icache_disable();
265         invalidate_l1_instruction_cache();
266         flush_data_cache();
267         dcache_disable();
268 #endif
269
270         switch (image_get_comp (hdr)) {
271         case IH_COMP_NONE:
272                 if (image_get_load (hdr) == addr) {
273                         printf ("   XIP %s ... ", name);
274                 } else {
275 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
276                         size_t l = len;
277                         void *to = (void *)image_get_load (hdr);
278                         void *from = (void *)data;
279
280                         printf ("   Loading %s ... ", name);
281
282                         while (l > 0) {
283                                 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
284                                 WATCHDOG_RESET();
285                                 memmove (to, from, tail);
286                                 to += tail;
287                                 from += tail;
288                                 l -= tail;
289                         }
290 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
291                         memmove ((void *)image_get_load (hdr), (uchar *)data, len);
292 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
293                 }
294                 break;
295         case IH_COMP_GZIP:
296                 printf ("   Uncompressing %s ... ", name);
297                 if (gunzip ((void *)image_get_load (hdr), unc_len,
298                             (uchar *)data, &len) != 0) {
299                         puts ("GUNZIP ERROR - must RESET board to recover\n");
300                         show_boot_progress (-6);
301                         do_reset (cmdtp, flag, argc, argv);
302                 }
303                 break;
304 #ifdef CONFIG_BZIP2
305         case IH_COMP_BZIP2:
306                 printf ("   Uncompressing %s ... ", name);
307                 /*
308                  * If we've got less than 4 MB of malloc() space,
309                  * use slower decompression algorithm which requires
310                  * at most 2300 KB of memory.
311                  */
312                 i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
313                                                 &unc_len, (char *)data, len,
314                                                 CFG_MALLOC_LEN < (4096 * 1024), 0);
315                 if (i != BZ_OK) {
316                         printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
317                         show_boot_progress (-6);
318                         do_reset (cmdtp, flag, argc, argv);
319                 }
320                 break;
321 #endif /* CONFIG_BZIP2 */
322         default:
323                 if (iflag)
324                         enable_interrupts();
325                 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
326                 show_boot_progress (-7);
327                 return 1;
328         }
329         puts ("OK\n");
330         show_boot_progress (7);
331
332         switch (image_get_type (hdr)) {
333         case IH_TYPE_STANDALONE:
334                 if (iflag)
335                         enable_interrupts();
336
337                 /* load (and uncompress), but don't start if "autostart"
338                  * is set to "no"
339                  */
340                 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
341                         char buf[32];
342                         sprintf(buf, "%lX", len);
343                         setenv("filesize", buf);
344                         return 0;
345                 }
346                 appl = (int (*)(int, char *[]))image_get_ep (hdr);
347                 (*appl)(argc-1, &argv[1]);
348                 return 0;
349         case IH_TYPE_KERNEL:
350         case IH_TYPE_MULTI:
351                 /* handled below */
352                 break;
353         default:
354                 if (iflag)
355                         enable_interrupts();
356                 printf ("Can't boot image type %d\n", image_get_type (hdr));
357                 show_boot_progress (-8);
358                 return 1;
359         }
360         show_boot_progress (8);
361
362         switch (image_get_os (hdr)) {
363         default:                        /* handled by (original) Linux case */
364         case IH_OS_LINUX:
365 #ifdef CONFIG_SILENT_CONSOLE
366             fixup_silent_linux();
367 #endif
368             do_bootm_linux  (cmdtp, flag, argc, argv,
369                              addr, len_ptr, verify);
370             break;
371         case IH_OS_NETBSD:
372             do_bootm_netbsd (cmdtp, flag, argc, argv,
373                              addr, len_ptr, verify);
374             break;
375
376 #ifdef CONFIG_LYNXKDI
377         case IH_OS_LYNXOS:
378             do_bootm_lynxkdi (cmdtp, flag, argc, argv,
379                              addr, len_ptr, verify);
380             break;
381 #endif
382
383         case IH_OS_RTEMS:
384             do_bootm_rtems (cmdtp, flag, argc, argv,
385                              addr, len_ptr, verify);
386             break;
387
388 #if defined(CONFIG_CMD_ELF)
389         case IH_OS_VXWORKS:
390             do_bootm_vxworks (cmdtp, flag, argc, argv,
391                               addr, len_ptr, verify);
392             break;
393         case IH_OS_QNX:
394             do_bootm_qnxelf (cmdtp, flag, argc, argv,
395                               addr, len_ptr, verify);
396             break;
397 #endif
398 #ifdef CONFIG_ARTOS
399         case IH_OS_ARTOS:
400             do_bootm_artos  (cmdtp, flag, argc, argv,
401                              addr, len_ptr, verify);
402             break;
403 #endif
404         }
405
406         show_boot_progress (-9);
407 #ifdef DEBUG
408         puts ("\n## Control returned to monitor - resetting...\n");
409         do_reset (cmdtp, flag, argc, argv);
410 #endif
411         return 1;
412 }
413
414 U_BOOT_CMD(
415         bootm,  CFG_MAXARGS,    1,      do_bootm,
416         "bootm   - boot application image from memory\n",
417         "[addr [arg ...]]\n    - boot application image stored in memory\n"
418         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
419         "\t'arg' can be the address of an initrd image\n"
420 #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
421         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
422         "\ta third argument is required which is the address of the\n"
423         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
424         "\tuse a '-' for the second argument. If you do not pass a third\n"
425         "\ta bd_info struct will be passed instead\n"
426 #endif
427 );
428
429 #ifdef CONFIG_SILENT_CONSOLE
430 static void
431 fixup_silent_linux ()
432 {
433         char buf[256], *start, *end;
434         char *cmdline = getenv ("bootargs");
435
436         /* Only fix cmdline when requested */
437         if (!(gd->flags & GD_FLG_SILENT))
438                 return;
439
440         debug ("before silent fix-up: %s\n", cmdline);
441         if (cmdline) {
442                 if ((start = strstr (cmdline, "console=")) != NULL) {
443                         end = strchr (start, ' ');
444                         strncpy (buf, cmdline, (start - cmdline + 8));
445                         if (end)
446                                 strcpy (buf + (start - cmdline + 8), end);
447                         else
448                                 buf[start - cmdline + 8] = '\0';
449                 } else {
450                         strcpy (buf, cmdline);
451                         strcat (buf, " console=");
452                 }
453         } else {
454                 strcpy (buf, "console=");
455         }
456
457         setenv ("bootargs", buf);
458         debug ("after silent fix-up: %s\n", buf);
459 }
460 #endif /* CONFIG_SILENT_CONSOLE */
461
462 static void
463 do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
464                 int     argc, char *argv[],
465                 ulong   addr,
466                 ulong   *len_ptr,
467                 int     verify)
468 {
469         image_header_t *hdr = &header;
470
471         void    (*loader)(bd_t *, image_header_t *, char *, char *);
472         image_header_t *img_addr;
473         char     *consdev;
474         char     *cmdline;
475
476
477         /*
478          * Booting a (NetBSD) kernel image
479          *
480          * This process is pretty similar to a standalone application:
481          * The (first part of an multi-) image must be a stage-2 loader,
482          * which in turn is responsible for loading & invoking the actual
483          * kernel.  The only differences are the parameters being passed:
484          * besides the board info strucure, the loader expects a command
485          * line, the name of the console device, and (optionally) the
486          * address of the original image header.
487          */
488
489         img_addr = 0;
490         if ((image_check_type (hdr, IH_TYPE_MULTI)) && (len_ptr[1]))
491                 img_addr = (image_header_t *) addr;
492
493
494         consdev = "";
495 #if   defined (CONFIG_8xx_CONS_SMC1)
496         consdev = "smc1";
497 #elif defined (CONFIG_8xx_CONS_SMC2)
498         consdev = "smc2";
499 #elif defined (CONFIG_8xx_CONS_SCC2)
500         consdev = "scc2";
501 #elif defined (CONFIG_8xx_CONS_SCC3)
502         consdev = "scc3";
503 #endif
504
505         if (argc > 2) {
506                 ulong len;
507                 int   i;
508
509                 for (i=2, len=0 ; i<argc ; i+=1)
510                         len += strlen (argv[i]) + 1;
511                 cmdline = malloc (len);
512
513                 for (i=2, len=0 ; i<argc ; i+=1) {
514                         if (i > 2)
515                                 cmdline[len++] = ' ';
516                         strcpy (&cmdline[len], argv[i]);
517                         len += strlen (argv[i]);
518                 }
519         } else if ((cmdline = getenv("bootargs")) == NULL) {
520                 cmdline = "";
521         }
522
523         loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
524
525         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
526                 (ulong)loader);
527
528         show_boot_progress (15);
529
530         /*
531          * NetBSD Stage-2 Loader Parameters:
532          *   r3: ptr to board info data
533          *   r4: image address
534          *   r5: console device
535          *   r6: boot args string
536          */
537         (*loader) (gd->bd, img_addr, consdev, cmdline);
538 }
539
540 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
541
542 /* Function that returns a character from the environment */
543 extern uchar (*env_get_char)(int);
544
545 static void
546 do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
547                 int     argc, char *argv[],
548                 ulong   addr,
549                 ulong   *len_ptr,
550                 int     verify)
551 {
552         ulong top;
553         char *s, *cmdline;
554         char **fwenv, **ss;
555         int i, j, nxt, len, envno, envsz;
556         bd_t *kbd;
557         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
558         image_header_t *hdr = &header;
559
560         /*
561          * Booting an ARTOS kernel image + application
562          */
563
564         /* this used to be the top of memory, but was wrong... */
565 #ifdef CONFIG_PPC
566         /* get stack pointer */
567         asm volatile ("mr %0,1" : "=r"(top) );
568 #endif
569         debug ("## Current stack ends at 0x%08lX ", top);
570
571         top -= 2048;            /* just to be sure */
572         if (top > CFG_BOOTMAPSZ)
573                 top = CFG_BOOTMAPSZ;
574         top &= ~0xF;
575
576         debug ("=> set upper limit to 0x%08lX\n", top);
577
578         /* first check the artos specific boot args, then the linux args*/
579         if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
580                 s = "";
581
582         /* get length of cmdline, and place it */
583         len = strlen(s);
584         top = (top - (len + 1)) & ~0xF;
585         cmdline = (char *)top;
586         debug ("## cmdline at 0x%08lX ", top);
587         strcpy(cmdline, s);
588
589         /* copy bdinfo */
590         top = (top - sizeof(bd_t)) & ~0xF;
591         debug ("## bd at 0x%08lX ", top);
592         kbd = (bd_t *)top;
593         memcpy(kbd, gd->bd, sizeof(bd_t));
594
595         /* first find number of env entries, and their size */
596         envno = 0;
597         envsz = 0;
598         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
599                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
600                         ;
601                 envno++;
602                 envsz += (nxt - i) + 1; /* plus trailing zero */
603         }
604         envno++;        /* plus the terminating zero */
605         debug ("## %u envvars total size %u ", envno, envsz);
606
607         top = (top - sizeof(char **)*envno) & ~0xF;
608         fwenv = (char **)top;
609         debug ("## fwenv at 0x%08lX ", top);
610
611         top = (top - envsz) & ~0xF;
612         s = (char *)top;
613         ss = fwenv;
614
615         /* now copy them */
616         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
617                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
618                         ;
619                 *ss++ = s;
620                 for (j = i; j < nxt; ++j)
621                         *s++ = env_get_char(j);
622                 *s++ = '\0';
623         }
624         *ss++ = NULL;   /* terminate */
625
626         entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
627         (*entry)(kbd, cmdline, fwenv, top);
628 }
629 #endif
630
631
632 #if defined(CONFIG_CMD_BOOTD)
633 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
634 {
635         int rcode = 0;
636 #ifndef CFG_HUSH_PARSER
637         if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
638 #else
639         if (parse_string_outer(getenv("bootcmd"),
640                 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
641 #endif
642         return rcode;
643 }
644
645 U_BOOT_CMD(
646         boot,   1,      1,      do_bootd,
647         "boot    - boot default, i.e., run 'bootcmd'\n",
648         NULL
649 );
650
651 /* keep old command name "bootd" for backward compatibility */
652 U_BOOT_CMD(
653         bootd, 1,       1,      do_bootd,
654         "bootd   - boot default, i.e., run 'bootcmd'\n",
655         NULL
656 );
657
658 #endif
659
660 #if defined(CONFIG_CMD_IMI)
661 int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
662 {
663         int     arg;
664         ulong   addr;
665         int     rcode=0;
666
667         if (argc < 2) {
668                 return image_info (load_addr);
669         }
670
671         for (arg=1; arg <argc; ++arg) {
672                 addr = simple_strtoul(argv[arg], NULL, 16);
673                 if (image_info (addr) != 0) rcode = 1;
674         }
675         return rcode;
676 }
677
678 static int image_info (ulong addr)
679 {
680         image_header_t *hdr = (image_header_t *)addr;
681
682         printf ("\n## Checking Image at %08lx ...\n", addr);
683
684         if (!image_check_magic (hdr)) {
685                 puts ("   Bad Magic Number\n");
686                 return 1;
687         }
688
689         if (!image_check_hcrc (hdr)) {
690                 puts ("   Bad Header Checksum\n");
691                 return 1;
692         }
693
694         print_image_hdr (hdr);
695
696         puts ("   Verifying Checksum ... ");
697         if (!image_check_dcrc (hdr)) {
698                 puts ("   Bad Data CRC\n");
699                 return 1;
700         }
701         puts ("OK\n");
702         return 0;
703 }
704
705 U_BOOT_CMD(
706         iminfo, CFG_MAXARGS,    1,      do_iminfo,
707         "iminfo  - print header information for application image\n",
708         "addr [addr ...]\n"
709         "    - print header information for application image starting at\n"
710         "      address 'addr' in memory; this includes verification of the\n"
711         "      image contents (magic number, header and payload checksums)\n"
712 );
713
714 #endif
715
716 #if defined(CONFIG_CMD_IMLS)
717 /*-----------------------------------------------------------------------
718  * List all images found in flash.
719  */
720 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
721 {
722         flash_info_t *info;
723         int i, j;
724         image_header_t *hdr;
725
726         for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
727                 if (info->flash_id == FLASH_UNKNOWN)
728                         goto next_bank;
729                 for (j=0; j<info->sector_count; ++j) {
730
731                         hdr = (image_header_t *)info->start[j];
732
733                         if (!hdr || !image_check_magic (hdr))
734                                 goto next_sector;
735
736                         if (!image_check_hcrc (hdr))
737                                 goto next_sector;
738
739                         printf ("Image at %08lX:\n", (ulong)hdr);
740                         print_image_hdr (hdr);
741
742                         puts ("   Verifying Checksum ... ");
743                         if (!image_check_dcrc (hdr)) {
744                                 puts ("Bad Data CRC\n");
745                         } else {
746                                 puts ("OK\n");
747                         }
748 next_sector:            ;
749                 }
750 next_bank:      ;
751         }
752
753         return (0);
754 }
755
756 U_BOOT_CMD(
757         imls,   1,              1,      do_imls,
758         "imls    - list all images found in flash\n",
759         "\n"
760         "    - Prints information about all images found at sector\n"
761         "      boundaries in flash.\n"
762 );
763 #endif
764
765 void
766 print_image_hdr (image_header_t *hdr)
767 {
768 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
769         time_t timestamp = (time_t)image_get_time (hdr);
770         struct rtc_time tm;
771 #endif
772
773         printf ("   Image Name:   %.*s\n", IH_NMLEN, image_get_name (hdr));
774 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
775         to_tm (timestamp, &tm);
776         printf ("   Created:      %4d-%02d-%02d  %2d:%02d:%02d UTC\n",
777                 tm.tm_year, tm.tm_mon, tm.tm_mday,
778                 tm.tm_hour, tm.tm_min, tm.tm_sec);
779 #endif
780         puts ("   Image Type:   "); print_type(hdr);
781         printf ("\n   Data Size:    %d Bytes = ", image_get_data_size (hdr));
782         print_size (image_get_data_size (hdr), "\n");
783         printf ("   Load Address: %08x\n"
784                 "   Entry Point:  %08x\n",
785                  image_get_load (hdr), image_get_ep (hdr));
786
787         if (image_check_type (hdr, IH_TYPE_MULTI)) {
788                 int i;
789                 ulong len;
790                 ulong *len_ptr = (ulong *)((ulong)hdr + image_get_header_size ());
791
792                 puts ("   Contents:\n");
793                 for (i=0; (len = image_to_cpu (*len_ptr)); ++i, ++len_ptr) {
794                         printf ("   Image %d: %8ld Bytes = ", i, len);
795                         print_size (len, "\n");
796                 }
797         }
798 }
799
800
801 static void
802 print_type (image_header_t *hdr)
803 {
804         char *os, *arch, *type, *comp;
805
806         switch (image_get_os (hdr)) {
807         case IH_OS_INVALID:     os = "Invalid OS";              break;
808         case IH_OS_NETBSD:      os = "NetBSD";                  break;
809         case IH_OS_LINUX:       os = "Linux";                   break;
810         case IH_OS_VXWORKS:     os = "VxWorks";                 break;
811         case IH_OS_QNX:         os = "QNX";                     break;
812         case IH_OS_U_BOOT:      os = "U-Boot";                  break;
813         case IH_OS_RTEMS:       os = "RTEMS";                   break;
814 #ifdef CONFIG_ARTOS
815         case IH_OS_ARTOS:       os = "ARTOS";                   break;
816 #endif
817 #ifdef CONFIG_LYNXKDI
818         case IH_OS_LYNXOS:      os = "LynxOS";                  break;
819 #endif
820         default:                os = "Unknown OS";              break;
821         }
822
823         switch (image_get_arch (hdr)) {
824         case IH_ARCH_INVALID:   arch = "Invalid CPU";           break;
825         case IH_ARCH_ALPHA:     arch = "Alpha";                 break;
826         case IH_ARCH_ARM:       arch = "ARM";                   break;
827         case IH_ARCH_AVR32:     arch = "AVR32";                 break;
828         case IH_ARCH_BLACKFIN:  arch = "Blackfin";              break;
829         case IH_ARCH_I386:      arch = "Intel x86";             break;
830         case IH_ARCH_IA64:      arch = "IA64";                  break;
831         case IH_ARCH_M68K:      arch = "M68K";                  break;
832         case IH_ARCH_MICROBLAZE:arch = "Microblaze";            break;
833         case IH_ARCH_MIPS64:    arch = "MIPS 64 Bit";           break;
834         case IH_ARCH_MIPS:      arch = "MIPS";                  break;
835         case IH_ARCH_NIOS2:     arch = "Nios-II";               break;
836         case IH_ARCH_NIOS:      arch = "Nios";                  break;
837         case IH_ARCH_PPC:       arch = "PowerPC";               break;
838         case IH_ARCH_S390:      arch = "IBM S390";              break;
839         case IH_ARCH_SH:        arch = "SuperH";                break;
840         case IH_ARCH_SPARC64:   arch = "SPARC 64 Bit";          break;
841         case IH_ARCH_SPARC:     arch = "SPARC";                 break;
842         default:                arch = "Unknown Architecture";  break;
843         }
844
845         switch (image_get_type (hdr)) {
846         case IH_TYPE_INVALID:   type = "Invalid Image";         break;
847         case IH_TYPE_STANDALONE:type = "Standalone Program";    break;
848         case IH_TYPE_KERNEL:    type = "Kernel Image";          break;
849         case IH_TYPE_RAMDISK:   type = "RAMDisk Image";         break;
850         case IH_TYPE_MULTI:     type = "Multi-File Image";      break;
851         case IH_TYPE_FIRMWARE:  type = "Firmware";              break;
852         case IH_TYPE_SCRIPT:    type = "Script";                break;
853         case IH_TYPE_FLATDT:    type = "Flat Device Tree";      break;
854         default:                type = "Unknown Image";         break;
855         }
856
857         switch (image_get_comp (hdr)) {
858         case IH_COMP_NONE:      comp = "uncompressed";          break;
859         case IH_COMP_GZIP:      comp = "gzip compressed";       break;
860         case IH_COMP_BZIP2:     comp = "bzip2 compressed";      break;
861         default:                comp = "unknown compression";   break;
862         }
863
864         printf ("%s %s %s (%s)", arch, os, type, comp);
865 }
866
867 #define ZALLOC_ALIGNMENT        16
868
869 static void *zalloc(void *x, unsigned items, unsigned size)
870 {
871         void *p;
872
873         size *= items;
874         size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
875
876         p = malloc (size);
877
878         return (p);
879 }
880
881 static void zfree(void *x, void *addr, unsigned nb)
882 {
883         free (addr);
884 }
885
886 #define HEAD_CRC        2
887 #define EXTRA_FIELD     4
888 #define ORIG_NAME       8
889 #define COMMENT         0x10
890 #define RESERVED        0xe0
891
892 #define DEFLATED        8
893
894 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
895 {
896         z_stream s;
897         int r, i, flags;
898
899         /* skip header */
900         i = 10;
901         flags = src[3];
902         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
903                 puts ("Error: Bad gzipped data\n");
904                 return (-1);
905         }
906         if ((flags & EXTRA_FIELD) != 0)
907                 i = 12 + src[10] + (src[11] << 8);
908         if ((flags & ORIG_NAME) != 0)
909                 while (src[i++] != 0)
910                         ;
911         if ((flags & COMMENT) != 0)
912                 while (src[i++] != 0)
913                         ;
914         if ((flags & HEAD_CRC) != 0)
915                 i += 2;
916         if (i >= *lenp) {
917                 puts ("Error: gunzip out of data in header\n");
918                 return (-1);
919         }
920
921         s.zalloc = zalloc;
922         s.zfree = zfree;
923 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
924         s.outcb = (cb_func)WATCHDOG_RESET;
925 #else
926         s.outcb = Z_NULL;
927 #endif  /* CONFIG_HW_WATCHDOG */
928
929         r = inflateInit2(&s, -MAX_WBITS);
930         if (r != Z_OK) {
931                 printf ("Error: inflateInit2() returned %d\n", r);
932                 return (-1);
933         }
934         s.next_in = src + i;
935         s.avail_in = *lenp - i;
936         s.next_out = dst;
937         s.avail_out = dstlen;
938         r = inflate(&s, Z_FINISH);
939         if (r != Z_OK && r != Z_STREAM_END) {
940                 printf ("Error: inflate() returned %d\n", r);
941                 return (-1);
942         }
943         *lenp = s.next_out - (unsigned char *) dst;
944         inflateEnd(&s);
945
946         return (0);
947 }
948
949 #ifdef CONFIG_BZIP2
950 void bz_internal_error(int errcode)
951 {
952         printf ("BZIP2 internal error %d\n", errcode);
953 }
954 #endif /* CONFIG_BZIP2 */
955
956 static void
957 do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
958                 ulong addr, ulong *len_ptr, int verify)
959 {
960         image_header_t *hdr = &header;
961         void    (*entry_point)(bd_t *);
962
963         entry_point = (void (*)(bd_t *))image_get_ep (hdr);
964
965         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
966                 (ulong)entry_point);
967
968         show_boot_progress (15);
969
970         /*
971          * RTEMS Parameters:
972          *   r3: ptr to board info data
973          */
974
975         (*entry_point ) ( gd->bd );
976 }
977
978 #if defined(CONFIG_CMD_ELF)
979 static void
980 do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
981                   ulong addr, ulong *len_ptr, int verify)
982 {
983         image_header_t *hdr = &header;
984         char str[80];
985
986         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
987         setenv("loadaddr", str);
988         do_bootvx(cmdtp, 0, 0, NULL);
989 }
990
991 static void
992 do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
993                  ulong addr, ulong *len_ptr, int verify)
994 {
995         image_header_t *hdr = &header;
996         char *local_args[2];
997         char str[16];
998
999         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
1000         local_args[0] = argv[0];
1001         local_args[1] = str;    /* and provide it via the arguments */
1002         do_bootelf(cmdtp, 0, 2, local_args);
1003 }
1004 #endif
1005
1006 #ifdef CONFIG_LYNXKDI
1007 static void
1008 do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1009                  int    argc, char *argv[],
1010                  ulong  addr,
1011                  ulong  *len_ptr,
1012                  int    verify)
1013 {
1014         lynxkdi_boot( &header );
1015 }
1016
1017 #endif /* CONFIG_LYNXKDI */