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