]> git.sur5r.net Git - u-boot/blob - common/cmd_bootm.c
[new uImage] More verbose kernel image uncompress error message
[u-boot] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #define DEBUG
25
26 /*
27  * Boot support
28  */
29 #include <common.h>
30 #include <watchdog.h>
31 #include <command.h>
32 #include <image.h>
33 #include <malloc.h>
34 #include <zlib.h>
35 #include <bzlib.h>
36 #include <environment.h>
37 #include <lmb.h>
38 #include <asm/byteorder.h>
39
40 #ifdef CFG_HUSH_PARSER
41 #include <hush.h>
42 #endif
43
44 DECLARE_GLOBAL_DATA_PTR;
45
46 extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
47 #ifndef CFG_BOOTM_LEN
48 #define CFG_BOOTM_LEN   0x800000        /* use 8MByte as default max gunzip size */
49 #endif
50
51 #ifdef CONFIG_BZIP2
52 extern void bz_internal_error(int);
53 #endif
54
55 #if defined(CONFIG_CMD_IMI)
56 static int image_info (unsigned long addr);
57 #endif
58
59 #if defined(CONFIG_CMD_IMLS)
60 #include <flash.h>
61 extern flash_info_t flash_info[]; /* info for FLASH chips */
62 static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
63 #endif
64
65 #ifdef CONFIG_SILENT_CONSOLE
66 static void fixup_silent_linux (void);
67 #endif
68
69 static image_header_t *image_get_kernel (ulong img_addr, int verify);
70 #if defined(CONFIG_FIT)
71 static int fit_check_kernel (const void *fit, int os_noffset, int verify);
72 #endif
73
74 static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
75                 bootm_headers_t *images, ulong *os_data, ulong *os_len);
76 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
77
78 /*
79  *  Continue booting an OS image; caller already has:
80  *  - copied image header to global variable `header'
81  *  - checked header magic number, checksums (both header & image),
82  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
83  *  - loaded (first part of) image to header load address,
84  *  - disabled interrupts.
85  */
86 typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
87                         int argc, char *argv[],
88                         bootm_headers_t *images); /* pointers to os/initrd/fdt */
89
90 extern boot_os_fn do_bootm_linux;
91 static boot_os_fn do_bootm_netbsd;
92 #if defined(CONFIG_LYNXKDI)
93 static boot_os_fn do_bootm_lynxkdi;
94 extern void lynxkdi_boot (image_header_t *);
95 #endif
96 static boot_os_fn do_bootm_rtems;
97 #if defined(CONFIG_CMD_ELF)
98 static boot_os_fn do_bootm_vxworks;
99 static boot_os_fn do_bootm_qnxelf;
100 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
101 int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
102 #endif
103 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
104 extern uchar (*env_get_char)(int); /* Returns a character from the environment */
105 static boot_os_fn do_bootm_artos;
106 #endif
107
108 ulong load_addr = CFG_LOAD_ADDR;        /* Default Load Address */
109 static bootm_headers_t images;          /* pointers to os/initrd/fdt images */
110
111 void __board_lmb_reserve(struct lmb *lmb)
112 {
113         /* please define platform specific board_lmb_reserve() */
114 }
115 void board_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__board_lmb_reserve")));
116
117
118 /*******************************************************************/
119 /* bootm - boot application image from image in memory */
120 /*******************************************************************/
121 int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
122 {
123         ulong           iflag;
124         const char      *type_name;
125         uint            unc_len = CFG_BOOTM_LEN;
126         uint8_t         comp, type, os;
127
128         void            *os_hdr;
129         ulong           os_data, os_len;
130         ulong           image_start, image_end;
131         ulong           load_start, load_end;
132         ulong           mem_start, mem_size;
133
134         struct lmb lmb;
135
136         memset ((void *)&images, 0, sizeof (images));
137         images.verify = getenv_verify();
138         images.autostart = getenv_autostart();
139         images.lmb = &lmb;
140
141         lmb_init(&lmb);
142
143         mem_start = getenv_bootm_low();
144         mem_size = getenv_bootm_size();
145
146         lmb_add(&lmb, mem_start, mem_size);
147
148         board_lmb_reserve(&lmb);
149
150         /* get kernel image header, start address and length */
151         os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
152                         &images, &os_data, &os_len);
153         if (os_len == 0) {
154                 puts ("ERROR: can't get kernel image!\n");
155                 return 1;
156         }
157
158         /* get image parameters */
159         switch (genimg_get_format (os_hdr)) {
160         case IMAGE_FORMAT_LEGACY:
161                 type = image_get_type (os_hdr);
162                 comp = image_get_comp (os_hdr);
163                 os = image_get_os (os_hdr);
164
165                 image_end = image_get_image_end (os_hdr);
166                 load_start = image_get_load (os_hdr);
167                 break;
168 #if defined(CONFIG_FIT)
169         case IMAGE_FORMAT_FIT:
170                 if (fit_image_get_type (images.fit_hdr_os,
171                                         images.fit_noffset_os, &type)) {
172                         puts ("Can't get image type!\n");
173                         show_boot_progress (-109);
174                         return 1;
175                 }
176
177                 if (fit_image_get_comp (images.fit_hdr_os,
178                                         images.fit_noffset_os, &comp)) {
179                         puts ("Can't get image compression!\n");
180                         show_boot_progress (-110);
181                         return 1;
182                 }
183
184                 if (fit_image_get_os (images.fit_hdr_os,
185                                         images.fit_noffset_os, &os)) {
186                         puts ("Can't get image OS!\n");
187                         show_boot_progress (-111);
188                         return 1;
189                 }
190
191                 image_end = fit_get_end (images.fit_hdr_os);
192
193                 if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
194                                         &load_start)) {
195                         puts ("Can't get image load address!\n");
196                         show_boot_progress (-112);
197                         return 1;
198                 }
199                 break;
200 #endif
201         default:
202                 puts ("ERROR: unknown image format type!\n");
203                 return 1;
204         }
205
206         image_start = (ulong)os_hdr;
207         load_end = 0;
208         type_name = genimg_get_type_name (type);
209
210         /*
211          * We have reached the point of no return: we are going to
212          * overwrite all exception vector code, so we cannot easily
213          * recover from any failures any more...
214          */
215         iflag = disable_interrupts();
216
217 #ifdef CONFIG_AMIGAONEG3SE
218         /*
219          * We've possible left the caches enabled during
220          * bios emulation, so turn them off again
221          */
222         icache_disable();
223         invalidate_l1_instruction_cache();
224         flush_data_cache();
225         dcache_disable();
226 #endif
227
228         switch (comp) {
229         case IH_COMP_NONE:
230                 if (load_start == (ulong)os_hdr) {
231                         printf ("   XIP %s ... ", type_name);
232                 } else {
233                         printf ("   Loading %s ... ", type_name);
234
235                         memmove_wd ((void *)load_start,
236                                    (void *)os_data, os_len, CHUNKSZ);
237
238                         load_end = load_start + os_len;
239                         puts("OK\n");
240                 }
241                 break;
242         case IH_COMP_GZIP:
243                 printf ("   Uncompressing %s ... ", type_name);
244                 if (gunzip ((void *)load_start, unc_len,
245                                         (uchar *)os_data, &os_len) != 0) {
246                         puts ("GUNZIP: uncompress or overwrite error "
247                                 "- must RESET board to recover\n");
248                         show_boot_progress (-6);
249                         do_reset (cmdtp, flag, argc, argv);
250                 }
251
252                 load_end = load_start + os_len;
253                 break;
254 #ifdef CONFIG_BZIP2
255         case IH_COMP_BZIP2:
256                 printf ("   Uncompressing %s ... ", type_name);
257                 /*
258                  * If we've got less than 4 MB of malloc() space,
259                  * use slower decompression algorithm which requires
260                  * at most 2300 KB of memory.
261                  */
262                 int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
263                                         &unc_len, (char *)os_data, os_len,
264                                         CFG_MALLOC_LEN < (4096 * 1024), 0);
265                 if (i != BZ_OK) {
266                         printf ("BUNZIP2: uncompress or overwrite error %d "
267                                 "- must RESET board to recover\n", i);
268                         show_boot_progress (-6);
269                         do_reset (cmdtp, flag, argc, argv);
270                 }
271
272                 load_end = load_start + unc_len;
273                 break;
274 #endif /* CONFIG_BZIP2 */
275         default:
276                 if (iflag)
277                         enable_interrupts();
278                 printf ("Unimplemented compression type %d\n", comp);
279                 show_boot_progress (-7);
280                 return 1;
281         }
282         puts ("OK\n");
283         debug ("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
284         show_boot_progress (7);
285
286         if ((load_start < image_end) && (load_end > image_start)) {
287                 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
288                 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
289
290                 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
291                 show_boot_progress (-113);
292                 do_reset (cmdtp, flag, argc, argv);
293         }
294
295         show_boot_progress (8);
296
297         lmb_reserve(&lmb, load_start, (load_end - load_start));
298
299         switch (os) {
300         default:                        /* handled by (original) Linux case */
301         case IH_OS_LINUX:
302 #ifdef CONFIG_SILENT_CONSOLE
303             fixup_silent_linux();
304 #endif
305             do_bootm_linux (cmdtp, flag, argc, argv, &images);
306             break;
307
308         case IH_OS_NETBSD:
309             do_bootm_netbsd (cmdtp, flag, argc, argv, &images);
310             break;
311
312 #ifdef CONFIG_LYNXKDI
313         case IH_OS_LYNXOS:
314             do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images);
315             break;
316 #endif
317
318         case IH_OS_RTEMS:
319             do_bootm_rtems (cmdtp, flag, argc, argv, &images);
320             break;
321
322 #if defined(CONFIG_CMD_ELF)
323         case IH_OS_VXWORKS:
324             do_bootm_vxworks (cmdtp, flag, argc, argv, &images);
325             break;
326
327         case IH_OS_QNX:
328             do_bootm_qnxelf (cmdtp, flag, argc, argv, &images);
329             break;
330 #endif
331
332 #ifdef CONFIG_ARTOS
333         case IH_OS_ARTOS:
334             do_bootm_artos (cmdtp, flag, argc, argv, &images);
335             break;
336 #endif
337         }
338
339         show_boot_progress (-9);
340 #ifdef DEBUG
341         puts ("\n## Control returned to monitor - resetting...\n");
342         if (images.autostart)
343                 do_reset (cmdtp, flag, argc, argv);
344 #endif
345         if (!images.autostart && iflag)
346                 enable_interrupts();
347
348         return 1;
349 }
350
351 /**
352  * image_get_kernel - verify legacy format kernel image
353  * @img_addr: in RAM address of the legacy format image to be verified
354  * @verify: data CRC verification flag
355  *
356  * image_get_kernel() verifies legacy image integrity and returns pointer to
357  * legacy image header if image verification was completed successfully.
358  *
359  * returns:
360  *     pointer to a legacy image header if valid image was found
361  *     otherwise return NULL
362  */
363 static image_header_t *image_get_kernel (ulong img_addr, int verify)
364 {
365         image_header_t *hdr = (image_header_t *)img_addr;
366
367         if (!image_check_magic(hdr)) {
368                 puts ("Bad Magic Number\n");
369                 show_boot_progress (-1);
370                 return NULL;
371         }
372         show_boot_progress (2);
373
374         if (!image_check_hcrc (hdr)) {
375                 puts ("Bad Header Checksum\n");
376                 show_boot_progress (-2);
377                 return NULL;
378         }
379
380         show_boot_progress (3);
381         image_print_contents (hdr);
382
383         if (verify) {
384                 puts ("   Verifying Checksum ... ");
385                 if (!image_check_dcrc (hdr)) {
386                         printf ("Bad Data CRC\n");
387                         show_boot_progress (-3);
388                         return NULL;
389                 }
390                 puts ("OK\n");
391         }
392         show_boot_progress (4);
393
394         if (!image_check_target_arch (hdr)) {
395                 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
396                 show_boot_progress (-4);
397                 return NULL;
398         }
399         return hdr;
400 }
401
402 /**
403  * fit_check_kernel - verify FIT format kernel subimage
404  * @fit_hdr: pointer to the FIT image header
405  * os_noffset: kernel subimage node offset within FIT image
406  * @verify: data CRC verification flag
407  *
408  * fit_check_kernel() verifies integrity of the kernel subimage and from
409  * specified FIT image.
410  *
411  * returns:
412  *     1, on success
413  *     0, on failure
414  */
415 #if defined (CONFIG_FIT)
416 static int fit_check_kernel (const void *fit, int os_noffset, int verify)
417 {
418         fit_image_print (fit, os_noffset, "   ");
419
420         if (verify) {
421                 puts ("   Verifying Hash Integrity ... ");
422                 if (!fit_image_check_hashes (fit, os_noffset)) {
423                         puts ("Bad Data Hash\n");
424                         show_boot_progress (-104);
425                         return 0;
426                 }
427                 puts ("OK\n");
428         }
429         show_boot_progress (105);
430
431         if (!fit_image_check_target_arch (fit, os_noffset)) {
432                 puts ("Unsupported Architecture\n");
433                 show_boot_progress (-105);
434                 return 0;
435         }
436
437         show_boot_progress (106);
438         if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
439                 puts ("Not a kernel image\n");
440                 show_boot_progress (-106);
441                 return 0;
442         }
443
444         show_boot_progress (107);
445         return 1;
446 }
447 #endif /* CONFIG_FIT */
448
449 /**
450  * boot_get_kernel - find kernel image
451  * @os_data: pointer to a ulong variable, will hold os data start address
452  * @os_len: pointer to a ulong variable, will hold os data length
453  *
454  * boot_get_kernel() tries to find a kernel image, verifies its integrity
455  * and locates kernel data.
456  *
457  * returns:
458  *     pointer to image header if valid image was found, plus kernel start
459  *     address and length, otherwise NULL
460  */
461 static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
462                 bootm_headers_t *images, ulong *os_data, ulong *os_len)
463 {
464         image_header_t  *hdr;
465         ulong           img_addr;
466 #if defined(CONFIG_FIT)
467         void            *fit_hdr;
468         const char      *fit_uname_config = NULL;
469         const char      *fit_uname_kernel = NULL;
470         const void      *data;
471         size_t          len;
472         int             conf_noffset;
473         int             os_noffset;
474 #endif
475
476         /* find out kernel image address */
477         if (argc < 2) {
478                 img_addr = load_addr;
479                 debug ("*  kernel: default image load address = 0x%08lx\n",
480                                 load_addr);
481 #if defined(CONFIG_FIT)
482         } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
483                                                         &fit_uname_config)) {
484                 debug ("*  kernel: config '%s' from image at 0x%08lx\n",
485                                 fit_uname_config, img_addr);
486         } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
487                                                         &fit_uname_kernel)) {
488                 debug ("*  kernel: subimage '%s' from image at 0x%08lx\n",
489                                 fit_uname_kernel, img_addr);
490 #endif
491         } else {
492                 img_addr = simple_strtoul(argv[1], NULL, 16);
493                 debug ("*  kernel: cmdline image address = 0x%08lx\n", img_addr);
494         }
495
496         show_boot_progress (1);
497
498         /* copy from dataflash if needed */
499         img_addr = genimg_get_image (img_addr);
500
501         /* check image type, for FIT images get FIT kernel node */
502         *os_data = *os_len = 0;
503         switch (genimg_get_format ((void *)img_addr)) {
504         case IMAGE_FORMAT_LEGACY:
505                 printf ("## Booting kernel from Legacy Image at %08lx ...\n",
506                                 img_addr);
507                 hdr = image_get_kernel (img_addr, images->verify);
508                 if (!hdr)
509                         return NULL;
510                 show_boot_progress (5);
511
512                 /* get os_data and os_len */
513                 switch (image_get_type (hdr)) {
514                 case IH_TYPE_KERNEL:
515                         *os_data = image_get_data (hdr);
516                         *os_len = image_get_data_size (hdr);
517                         break;
518                 case IH_TYPE_MULTI:
519                         image_multi_getimg (hdr, 0, os_data, os_len);
520                         break;
521                 default:
522                         printf ("Wrong Image Type for %s command\n", cmdtp->name);
523                         show_boot_progress (-5);
524                         return NULL;
525                 }
526                 images->legacy_hdr_os = hdr;
527                 images->legacy_hdr_valid = 1;
528
529                 show_boot_progress (6);
530                 break;
531 #if defined(CONFIG_FIT)
532         case IMAGE_FORMAT_FIT:
533                 fit_hdr = (void *)img_addr;
534                 printf ("## Booting kernel from FIT Image at %08lx ...\n",
535                                 img_addr);
536
537                 if (!fit_check_format (fit_hdr)) {
538                         puts ("Bad FIT kernel image format!\n");
539                         show_boot_progress (-100);
540                         return NULL;
541                 }
542                 show_boot_progress (100);
543
544                 if (!fit_uname_kernel) {
545                         /*
546                          * no kernel image node unit name, try to get config
547                          * node first. If config unit node name is NULL
548                          * fit_conf_get_node() will try to find default config node
549                          */
550                         show_boot_progress (101);
551                         conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
552                         if (conf_noffset < 0) {
553                                 show_boot_progress (-101);
554                                 return NULL;
555                         }
556
557                         os_noffset = fit_conf_get_kernel_node (fit_hdr, conf_noffset);
558                         fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
559                 } else {
560                         /* get kernel component image node offset */
561                         show_boot_progress (102);
562                         os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
563                 }
564                 if (os_noffset < 0) {
565                         show_boot_progress (-103);
566                         return NULL;
567                 }
568
569                 printf ("   Trying '%s' kernel subimage\n", fit_uname_kernel);
570
571                 show_boot_progress (104);
572                 if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
573                         return NULL;
574
575                 /* get kernel image data address and length */
576                 if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
577                         puts ("Could not find kernel subimage data!\n");
578                         show_boot_progress (-107);
579                         return NULL;
580                 }
581                 show_boot_progress (108);
582
583                 *os_len = len;
584                 *os_data = (ulong)data;
585                 images->fit_hdr_os = fit_hdr;
586                 images->fit_uname_os = fit_uname_kernel;
587                 images->fit_noffset_os = os_noffset;
588                 break;
589 #endif
590         default:
591                 printf ("Wrong Image Format for %s command\n", cmdtp->name);
592                 show_boot_progress (-108);
593                 return NULL;
594         }
595
596         debug ("   kernel data at 0x%08lx, len = 0x%08lx (%d)\n",
597                         *os_data, *os_len, *os_len);
598
599         return (void *)img_addr;
600 }
601
602 U_BOOT_CMD(
603         bootm,  CFG_MAXARGS,    1,      do_bootm,
604         "bootm   - boot application image from memory\n",
605         "[addr [arg ...]]\n    - boot application image stored in memory\n"
606         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
607         "\t'arg' can be the address of an initrd image\n"
608 #if defined(CONFIG_OF_LIBFDT)
609         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
610         "\ta third argument is required which is the address of the\n"
611         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
612         "\tuse a '-' for the second argument. If you do not pass a third\n"
613         "\ta bd_info struct will be passed instead\n"
614 #endif
615 #if defined(CONFIG_FIT)
616         "\t\nFor the new multi component uImage format (FIT) addresses\n"
617         "\tmust be extened to include component or configuration unit name:\n"
618         "\taddr:<subimg_uname> - direct component image specification\n"
619         "\taddr#<conf_uname>   - configuration specification\n"
620         "\tUse iminfo command to get the list of existing component\n"
621         "\timages and configurations.\n"
622 #endif
623 );
624
625 /*******************************************************************/
626 /* bootd - boot default image */
627 /*******************************************************************/
628 #if defined(CONFIG_CMD_BOOTD)
629 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
630 {
631         int rcode = 0;
632
633 #ifndef CFG_HUSH_PARSER
634         if (run_command (getenv ("bootcmd"), flag) < 0)
635                 rcode = 1;
636 #else
637         if (parse_string_outer (getenv ("bootcmd"),
638                         FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
639                 rcode = 1;
640 #endif
641         return rcode;
642 }
643
644 U_BOOT_CMD(
645         boot,   1,      1,      do_bootd,
646         "boot    - boot default, i.e., run 'bootcmd'\n",
647         NULL
648 );
649
650 /* keep old command name "bootd" for backward compatibility */
651 U_BOOT_CMD(
652         bootd, 1,       1,      do_bootd,
653         "bootd   - boot default, i.e., run 'bootcmd'\n",
654         NULL
655 );
656
657 #endif
658
659
660 /*******************************************************************/
661 /* iminfo - print header info for a requested image */
662 /*******************************************************************/
663 #if defined(CONFIG_CMD_IMI)
664 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
665 {
666         int     arg;
667         ulong   addr;
668         int     rcode = 0;
669
670         if (argc < 2) {
671                 return image_info (load_addr);
672         }
673
674         for (arg = 1; arg < argc; ++arg) {
675                 addr = simple_strtoul (argv[arg], NULL, 16);
676                 if (image_info (addr) != 0)
677                         rcode = 1;
678         }
679         return rcode;
680 }
681
682 static int image_info (ulong addr)
683 {
684         void *hdr = (void *)addr;
685
686         printf ("\n## Checking Image at %08lx ...\n", addr);
687
688         switch (genimg_get_format (hdr)) {
689         case IMAGE_FORMAT_LEGACY:
690                 puts ("   Legacy image found\n");
691                 if (!image_check_magic (hdr)) {
692                         puts ("   Bad Magic Number\n");
693                         return 1;
694                 }
695
696                 if (!image_check_hcrc (hdr)) {
697                         puts ("   Bad Header Checksum\n");
698                         return 1;
699                 }
700
701                 image_print_contents (hdr);
702
703                 puts ("   Verifying Checksum ... ");
704                 if (!image_check_dcrc (hdr)) {
705                         puts ("   Bad Data CRC\n");
706                         return 1;
707                 }
708                 puts ("OK\n");
709                 return 0;
710 #if defined(CONFIG_FIT)
711         case IMAGE_FORMAT_FIT:
712                 puts ("   FIT image found\n");
713
714                 if (!fit_check_format (hdr)) {
715                         puts ("Bad FIT image format!\n");
716                         return 1;
717                 }
718
719                 fit_print_contents (hdr);
720                 return 0;
721 #endif
722         default:
723                 puts ("Unknown image format!\n");
724                 break;
725         }
726
727         return 1;
728 }
729
730 U_BOOT_CMD(
731         iminfo, CFG_MAXARGS,    1,      do_iminfo,
732         "iminfo  - print header information for application image\n",
733         "addr [addr ...]\n"
734         "    - print header information for application image starting at\n"
735         "      address 'addr' in memory; this includes verification of the\n"
736         "      image contents (magic number, header and payload checksums)\n"
737 );
738 #endif
739
740
741 /*******************************************************************/
742 /* imls - list all images found in flash */
743 /*******************************************************************/
744 #if defined(CONFIG_CMD_IMLS)
745 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
746 {
747         flash_info_t *info;
748         int i, j;
749         void *hdr;
750
751         for (i = 0, info = &flash_info[0];
752                 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
753
754                 if (info->flash_id == FLASH_UNKNOWN)
755                         goto next_bank;
756                 for (j = 0; j < info->sector_count; ++j) {
757
758                         hdr = (void *)info->start[j];
759                         if (!hdr)
760                                 goto next_sector;
761
762                         switch (genimg_get_format (hdr)) {
763                         case IMAGE_FORMAT_LEGACY:
764                                 if (!image_check_hcrc (hdr))
765                                         goto next_sector;
766
767                                 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
768                                 image_print_contents (hdr);
769
770                                 puts ("   Verifying Checksum ... ");
771                                 if (!image_check_dcrc (hdr)) {
772                                         puts ("Bad Data CRC\n");
773                                 } else {
774                                         puts ("OK\n");
775                                 }
776                                 break;
777 #if defined(CONFIG_FIT)
778                         case IMAGE_FORMAT_FIT:
779                                 if (!fit_check_format (hdr))
780                                         goto next_sector;
781
782                                 printf ("FIT Image at %08lX:\n", (ulong)hdr);
783                                 fit_print_contents (hdr);
784                                 break;
785 #endif
786                         default:
787                                 goto next_sector;
788                         }
789
790 next_sector:            ;
791                 }
792 next_bank:      ;
793         }
794
795         return (0);
796 }
797
798 U_BOOT_CMD(
799         imls,   1,              1,      do_imls,
800         "imls    - list all images found in flash\n",
801         "\n"
802         "    - Prints information about all images found at sector\n"
803         "      boundaries in flash.\n"
804 );
805 #endif
806
807 /*******************************************************************/
808 /* helper routines */
809 /*******************************************************************/
810 #ifdef CONFIG_SILENT_CONSOLE
811 static void fixup_silent_linux ()
812 {
813         char buf[256], *start, *end;
814         char *cmdline = getenv ("bootargs");
815
816         /* Only fix cmdline when requested */
817         if (!(gd->flags & GD_FLG_SILENT))
818                 return;
819
820         debug ("before silent fix-up: %s\n", cmdline);
821         if (cmdline) {
822                 if ((start = strstr (cmdline, "console=")) != NULL) {
823                         end = strchr (start, ' ');
824                         strncpy (buf, cmdline, (start - cmdline + 8));
825                         if (end)
826                                 strcpy (buf + (start - cmdline + 8), end);
827                         else
828                                 buf[start - cmdline + 8] = '\0';
829                 } else {
830                         strcpy (buf, cmdline);
831                         strcat (buf, " console=");
832                 }
833         } else {
834                 strcpy (buf, "console=");
835         }
836
837         setenv ("bootargs", buf);
838         debug ("after silent fix-up: %s\n", buf);
839 }
840 #endif /* CONFIG_SILENT_CONSOLE */
841
842
843 /*******************************************************************/
844 /* OS booting routines */
845 /*******************************************************************/
846
847 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
848                             int argc, char *argv[],
849                             bootm_headers_t *images)
850 {
851         void (*loader)(bd_t *, image_header_t *, char *, char *);
852         image_header_t *os_hdr, *hdr;
853         ulong kernel_data, kernel_len;
854         char *consdev;
855         char *cmdline;
856
857 #if defined(CONFIG_FIT)
858         if (!images->legacy_hdr_valid) {
859                 fit_unsupported_reset ("NetBSD");
860                 do_reset (cmdtp, flag, argc, argv);
861         }
862 #endif
863         hdr = images->legacy_hdr_os;
864
865         /*
866          * Booting a (NetBSD) kernel image
867          *
868          * This process is pretty similar to a standalone application:
869          * The (first part of an multi-) image must be a stage-2 loader,
870          * which in turn is responsible for loading & invoking the actual
871          * kernel.  The only differences are the parameters being passed:
872          * besides the board info strucure, the loader expects a command
873          * line, the name of the console device, and (optionally) the
874          * address of the original image header.
875          */
876         os_hdr = NULL;
877         if (image_check_type (hdr, IH_TYPE_MULTI)) {
878                 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
879                 if (kernel_len)
880                         os_hdr = hdr;
881         }
882
883         consdev = "";
884 #if   defined (CONFIG_8xx_CONS_SMC1)
885         consdev = "smc1";
886 #elif defined (CONFIG_8xx_CONS_SMC2)
887         consdev = "smc2";
888 #elif defined (CONFIG_8xx_CONS_SCC2)
889         consdev = "scc2";
890 #elif defined (CONFIG_8xx_CONS_SCC3)
891         consdev = "scc3";
892 #endif
893
894         if (argc > 2) {
895                 ulong len;
896                 int   i;
897
898                 for (i = 2, len = 0; i < argc; i += 1)
899                         len += strlen (argv[i]) + 1;
900                 cmdline = malloc (len);
901
902                 for (i = 2, len = 0; i < argc; i += 1) {
903                         if (i > 2)
904                                 cmdline[len++] = ' ';
905                         strcpy (&cmdline[len], argv[i]);
906                         len += strlen (argv[i]);
907                 }
908         } else if ((cmdline = getenv ("bootargs")) == NULL) {
909                 cmdline = "";
910         }
911
912         loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
913
914         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
915                 (ulong)loader);
916
917         show_boot_progress (15);
918
919         /*
920          * NetBSD Stage-2 Loader Parameters:
921          *   r3: ptr to board info data
922          *   r4: image address
923          *   r5: console device
924          *   r6: boot args string
925          */
926         (*loader) (gd->bd, os_hdr, consdev, cmdline);
927 }
928
929 #ifdef CONFIG_LYNXKDI
930 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
931                              int argc, char *argv[],
932                              bootm_headers_t *images)
933 {
934         image_header_t *hdr = images->legacy_hdr_os;
935
936 #if defined(CONFIG_FIT)
937         if (!images->legacy_hdr_valid) {
938                 fit_unsupported_reset ("Lynx");
939                 do_reset (cmdtp, flag, argc, argv);
940         }
941 #endif
942
943         lynxkdi_boot ((image_header_t *)hdr);
944 }
945 #endif /* CONFIG_LYNXKDI */
946
947 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
948                            int argc, char *argv[],
949                            bootm_headers_t *images)
950 {
951         image_header_t *hdr = images->legacy_hdr_os;
952         void (*entry_point)(bd_t *);
953
954 #if defined(CONFIG_FIT)
955         if (!images->legacy_hdr_valid) {
956                 fit_unsupported_reset ("RTEMS");
957                 do_reset (cmdtp, flag, argc, argv);
958         }
959 #endif
960
961         entry_point = (void (*)(bd_t *))image_get_ep (hdr);
962
963         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
964                 (ulong)entry_point);
965
966         show_boot_progress (15);
967
968         /*
969          * RTEMS Parameters:
970          *   r3: ptr to board info data
971          */
972         (*entry_point)(gd->bd);
973 }
974
975 #if defined(CONFIG_CMD_ELF)
976 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
977                              int argc, char *argv[],
978                              bootm_headers_t *images)
979 {
980         char str[80];
981         image_header_t *hdr = images->legacy_hdr_os;
982
983 #if defined(CONFIG_FIT)
984         if (hdr == NULL) {
985                 fit_unsupported_reset ("VxWorks");
986                 do_reset (cmdtp, flag, argc, argv);
987         }
988 #endif
989
990         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
991         setenv("loadaddr", str);
992         do_bootvx(cmdtp, 0, 0, NULL);
993 }
994
995 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
996                             int argc, char *argv[],
997                             bootm_headers_t *images)
998 {
999         char *local_args[2];
1000         char str[16];
1001         image_header_t *hdr = images->legacy_hdr_os;
1002
1003 #if defined(CONFIG_FIT)
1004         if (!images->legacy_hdr_valid) {
1005                 fit_unsupported_reset ("QNX");
1006                 do_reset (cmdtp, flag, argc, argv);
1007         }
1008 #endif
1009
1010         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
1011         local_args[0] = argv[0];
1012         local_args[1] = str;    /* and provide it via the arguments */
1013         do_bootelf(cmdtp, 0, 2, local_args);
1014 }
1015 #endif
1016
1017 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1018 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1019                            int argc, char *argv[],
1020                            bootm_headers_t *images)
1021 {
1022         ulong top;
1023         char *s, *cmdline;
1024         char **fwenv, **ss;
1025         int i, j, nxt, len, envno, envsz;
1026         bd_t *kbd;
1027         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1028         image_header_t *hdr = images->legacy_hdr_os;
1029
1030 #if defined(CONFIG_FIT)
1031         if (!images->legacy_hdr_valid) {
1032                 fit_unsupported_reset ("ARTOS");
1033                 do_reset (cmdtp, flag, argc, argv);
1034         }
1035 #endif
1036
1037         /*
1038          * Booting an ARTOS kernel image + application
1039          */
1040
1041         /* this used to be the top of memory, but was wrong... */
1042 #ifdef CONFIG_PPC
1043         /* get stack pointer */
1044         asm volatile ("mr %0,1" : "=r"(top) );
1045 #endif
1046         debug ("## Current stack ends at 0x%08lX ", top);
1047
1048         top -= 2048;            /* just to be sure */
1049         if (top > CFG_BOOTMAPSZ)
1050                 top = CFG_BOOTMAPSZ;
1051         top &= ~0xF;
1052
1053         debug ("=> set upper limit to 0x%08lX\n", top);
1054
1055         /* first check the artos specific boot args, then the linux args*/
1056         if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
1057                 s = "";
1058
1059         /* get length of cmdline, and place it */
1060         len = strlen (s);
1061         top = (top - (len + 1)) & ~0xF;
1062         cmdline = (char *)top;
1063         debug ("## cmdline at 0x%08lX ", top);
1064         strcpy (cmdline, s);
1065
1066         /* copy bdinfo */
1067         top = (top - sizeof (bd_t)) & ~0xF;
1068         debug ("## bd at 0x%08lX ", top);
1069         kbd = (bd_t *)top;
1070         memcpy (kbd, gd->bd, sizeof (bd_t));
1071
1072         /* first find number of env entries, and their size */
1073         envno = 0;
1074         envsz = 0;
1075         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1076                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1077                         ;
1078                 envno++;
1079                 envsz += (nxt - i) + 1; /* plus trailing zero */
1080         }
1081         envno++;        /* plus the terminating zero */
1082         debug ("## %u envvars total size %u ", envno, envsz);
1083
1084         top = (top - sizeof (char **) * envno) & ~0xF;
1085         fwenv = (char **)top;
1086         debug ("## fwenv at 0x%08lX ", top);
1087
1088         top = (top - envsz) & ~0xF;
1089         s = (char *)top;
1090         ss = fwenv;
1091
1092         /* now copy them */
1093         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1094                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1095                         ;
1096                 *ss++ = s;
1097                 for (j = i; j < nxt; ++j)
1098                         *s++ = env_get_char (j);
1099                 *s++ = '\0';
1100         }
1101         *ss++ = NULL;   /* terminate */
1102
1103         entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
1104         (*entry) (kbd, cmdline, fwenv, top);
1105 }
1106 #endif