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