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