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