]> git.sur5r.net Git - u-boot/blob - common/cmd_bootm.c
[new uImage] Add new uImage format support for kernel booting
[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 #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         do_reset (cmdtp, flag, argc, argv);
346 #endif
347         return 1;
348 }
349
350 /**
351  * image_get_kernel - verify legacy format kernel image
352  * @img_addr: in RAM address of the legacy format image to be verified
353  * @verify: data CRC verification flag
354  *
355  * image_get_kernel() verifies legacy image integrity and returns pointer to
356  * legacy image header if image verification was completed successfully.
357  *
358  * returns:
359  *     pointer to a legacy image header if valid image was found
360  *     otherwise return NULL
361  */
362 static image_header_t *image_get_kernel (ulong img_addr, int verify)
363 {
364         image_header_t *hdr = (image_header_t *)img_addr;
365
366         if (!image_check_magic(hdr)) {
367                 puts ("Bad Magic Number\n");
368                 show_boot_progress (-1);
369                 return NULL;
370         }
371         show_boot_progress (2);
372
373         if (!image_check_hcrc (hdr)) {
374                 puts ("Bad Header Checksum\n");
375                 show_boot_progress (-2);
376                 return NULL;
377         }
378
379         show_boot_progress (3);
380         image_print_contents (hdr);
381
382         if (verify) {
383                 puts ("   Verifying Checksum ... ");
384                 if (!image_check_dcrc (hdr)) {
385                         printf ("Bad Data CRC\n");
386                         show_boot_progress (-3);
387                         return NULL;
388                 }
389                 puts ("OK\n");
390         }
391         show_boot_progress (4);
392
393         if (!image_check_target_arch (hdr)) {
394                 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
395                 show_boot_progress (-4);
396                 return NULL;
397         }
398         return hdr;
399 }
400
401 /**
402  * fit_check_kernel - verify FIT format kernel subimage
403  * @fit_hdr: pointer to the FIT image header
404  * os_noffset: kernel subimage node offset within FIT image
405  * @verify: data CRC verification flag
406  *
407  * fit_check_kernel() verifies integrity of the kernel subimage and from
408  * specified FIT image.
409  *
410  * returns:
411  *     1, on success
412  *     0, on failure
413  */
414 #if defined (CONFIG_FIT)
415 static int fit_check_kernel (const void *fit, int os_noffset, int verify)
416 {
417         fit_image_print (fit, os_noffset, "   ");
418
419         if (verify) {
420                 puts ("   Verifying Hash Integrity ... ");
421                 if (!fit_image_check_hashes (fit, os_noffset)) {
422                         puts ("Bad Data Hash\n");
423                         return 0;
424                 }
425                 puts ("OK\n");
426         }
427
428         if (!fit_image_check_target_arch (fit, os_noffset)) {
429                 puts ("Unsupported Architecture\n");
430                 return 0;
431         }
432
433         if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
434                 puts ("Not a kernel image\n");
435                 return 0;
436         }
437
438         return 1;
439 }
440 #endif /* CONFIG_FIT */
441
442 /**
443  * boot_get_kernel - find kernel image
444  * @os_data: pointer to a ulong variable, will hold os data start address
445  * @os_len: pointer to a ulong variable, will hold os data length
446  *
447  * boot_get_kernel() tries to find a kernel image, verifies its integrity
448  * and locates kernel data.
449  *
450  * returns:
451  *     pointer to image header if valid image was found, plus kernel start
452  *     address and length, otherwise NULL
453  */
454 static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
455                 bootm_headers_t *images, ulong *os_data, ulong *os_len)
456 {
457         image_header_t  *hdr;
458         ulong           img_addr;
459 #if defined(CONFIG_FIT)
460         void            *fit_hdr;
461         const char      *fit_uname_config = NULL;
462         const char      *fit_uname_kernel = NULL;
463         const void      *data;
464         size_t          len;
465         int             conf_noffset;
466         int             os_noffset;
467 #endif
468
469         /* find out kernel image address */
470         if (argc < 2) {
471                 img_addr = load_addr;
472                 debug ("*  kernel: default image load address = 0x%08lx\n",
473                                 load_addr);
474 #if defined(CONFIG_FIT)
475         } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
476                                                         &fit_uname_config)) {
477                 debug ("*  kernel: config '%s' from image at 0x%08lx\n",
478                                 fit_uname_config, img_addr);
479         } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
480                                                         &fit_uname_kernel)) {
481                 debug ("*  kernel: subimage '%s' from image at 0x%08lx\n",
482                                 fit_uname_kernel, img_addr);
483 #endif
484         } else {
485                 img_addr = simple_strtoul(argv[1], NULL, 16);
486                 debug ("*  kernel: cmdline image address = 0x%08lx\n", img_addr);
487         }
488
489         show_boot_progress (1);
490
491         /* copy from dataflash if needed */
492         img_addr = genimg_get_image (img_addr);
493
494         /* check image type, for FIT images get FIT kernel node */
495         *os_data = *os_len = 0;
496         switch (genimg_get_format ((void *)img_addr)) {
497         case IMAGE_FORMAT_LEGACY:
498                 printf ("## Booting kernel from Legacy Image at %08lx ...\n",
499                                 img_addr);
500                 hdr = image_get_kernel (img_addr, images->verify);
501                 if (!hdr)
502                         return NULL;
503                 show_boot_progress (5);
504
505                 /* get os_data and os_len */
506                 switch (image_get_type (hdr)) {
507                 case IH_TYPE_KERNEL:
508                         *os_data = image_get_data (hdr);
509                         *os_len = image_get_data_size (hdr);
510                         break;
511                 case IH_TYPE_MULTI:
512                         image_multi_getimg (hdr, 0, os_data, os_len);
513                         break;
514                 default:
515                         printf ("Wrong Image Type for %s command\n", cmdtp->name);
516                         show_boot_progress (-5);
517                         return NULL;
518                 }
519                 images->legacy_hdr_os = hdr;
520                 images->legacy_hdr_valid = 1;
521
522                 break;
523 #if defined(CONFIG_FIT)
524         case IMAGE_FORMAT_FIT:
525                 fit_hdr = (void *)img_addr;
526                 printf ("## Booting kernel from FIT Image at %08lx ...\n",
527                                 img_addr);
528
529                 if (!fit_check_format (fit_hdr)) {
530                         puts ("Bad FIT kernel image format!\n");
531                         return NULL;
532                 }
533
534                 if (!fit_uname_kernel) {
535                         /*
536                          * no kernel image node unit name, try to get config
537                          * node first. If config unit node name is NULL
538                          * fit_conf_get_node() will try to find default config node
539                          */
540                         conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
541                         if (conf_noffset < 0)
542                                 return NULL;
543
544                         os_noffset = fit_conf_get_kernel_node (fit_hdr, conf_noffset);
545                         fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
546                 } else {
547                         /* get kernel component image node offset */
548                         os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
549                 }
550                 if (os_noffset < 0)
551                         return NULL;
552
553                 printf ("   Trying '%s' kernel subimage\n", fit_uname_kernel);
554
555                 if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
556                         return NULL;
557
558                 /* get kernel image data address and length */
559                 if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
560                         puts ("Could not find kernel subimage data!\n");
561                         return NULL;
562                 }
563
564                 *os_len = len;
565                 *os_data = (ulong)data;
566                 images->fit_hdr_os = fit_hdr;
567                 images->fit_uname_os = fit_uname_kernel;
568                 break;
569 #endif
570         default:
571                 printf ("Wrong Image Format for %s command\n", cmdtp->name);
572                 return NULL;
573         }
574
575         debug ("   kernel data at 0x%08lx, len = 0x%08lx (%d)\n",
576                         *os_data, *os_len, *os_len);
577
578         return (void *)img_addr;
579 }
580
581 U_BOOT_CMD(
582         bootm,  CFG_MAXARGS,    1,      do_bootm,
583         "bootm   - boot application image from memory\n",
584         "[addr [arg ...]]\n    - boot application image stored in memory\n"
585         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
586         "\t'arg' can be the address of an initrd image\n"
587 #if defined(CONFIG_OF_LIBFDT)
588         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
589         "\ta third argument is required which is the address of the\n"
590         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
591         "\tuse a '-' for the second argument. If you do not pass a third\n"
592         "\ta bd_info struct will be passed instead\n"
593 #endif
594 #if defined(CONFIG_FIT)
595         "\t\nFor the new multi component uImage format (FIT) addresses\n"
596         "\tmust be extened to include component or configuration unit name:\n"
597         "\taddr:<subimg_uname> - direct component image specification\n"
598         "\taddr#<conf_uname>   - configuration specification\n"
599         "\tUse iminfo command to get the list of existing component\n"
600         "\timages and configurations.\n"
601 #endif
602 );
603
604 /*******************************************************************/
605 /* bootd - boot default image */
606 /*******************************************************************/
607 #if defined(CONFIG_CMD_BOOTD)
608 int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
609 {
610         int rcode = 0;
611
612 #ifndef CFG_HUSH_PARSER
613         if (run_command (getenv ("bootcmd"), flag) < 0)
614                 rcode = 1;
615 #else
616         if (parse_string_outer (getenv ("bootcmd"),
617                         FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
618                 rcode = 1;
619 #endif
620         return rcode;
621 }
622
623 U_BOOT_CMD(
624         boot,   1,      1,      do_bootd,
625         "boot    - boot default, i.e., run 'bootcmd'\n",
626         NULL
627 );
628
629 /* keep old command name "bootd" for backward compatibility */
630 U_BOOT_CMD(
631         bootd, 1,       1,      do_bootd,
632         "bootd   - boot default, i.e., run 'bootcmd'\n",
633         NULL
634 );
635
636 #endif
637
638
639 /*******************************************************************/
640 /* iminfo - print header info for a requested image */
641 /*******************************************************************/
642 #if defined(CONFIG_CMD_IMI)
643 int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
644 {
645         int     arg;
646         ulong   addr;
647         int     rcode = 0;
648
649         if (argc < 2) {
650                 return image_info (load_addr);
651         }
652
653         for (arg = 1; arg < argc; ++arg) {
654                 addr = simple_strtoul (argv[arg], NULL, 16);
655                 if (image_info (addr) != 0)
656                         rcode = 1;
657         }
658         return rcode;
659 }
660
661 static int image_info (ulong addr)
662 {
663         void *hdr = (void *)addr;
664
665         printf ("\n## Checking Image at %08lx ...\n", addr);
666
667         switch (genimg_get_format (hdr)) {
668         case IMAGE_FORMAT_LEGACY:
669                 puts ("   Legacy image found\n");
670                 if (!image_check_magic (hdr)) {
671                         puts ("   Bad Magic Number\n");
672                         return 1;
673                 }
674
675                 if (!image_check_hcrc (hdr)) {
676                         puts ("   Bad Header Checksum\n");
677                         return 1;
678                 }
679
680                 image_print_contents (hdr);
681
682                 puts ("   Verifying Checksum ... ");
683                 if (!image_check_dcrc (hdr)) {
684                         puts ("   Bad Data CRC\n");
685                         return 1;
686                 }
687                 puts ("OK\n");
688                 return 0;
689 #if defined(CONFIG_FIT)
690         case IMAGE_FORMAT_FIT:
691                 puts ("   FIT image found\n");
692
693                 if (!fit_check_format (hdr)) {
694                         puts ("Bad FIT image format!\n");
695                         return 1;
696                 }
697
698                 fit_print_contents (hdr);
699                 return 0;
700 #endif
701         default:
702                 puts ("Unknown image format!\n");
703                 break;
704         }
705
706         return 1;
707 }
708
709 U_BOOT_CMD(
710         iminfo, CFG_MAXARGS,    1,      do_iminfo,
711         "iminfo  - print header information for application image\n",
712         "addr [addr ...]\n"
713         "    - print header information for application image starting at\n"
714         "      address 'addr' in memory; this includes verification of the\n"
715         "      image contents (magic number, header and payload checksums)\n"
716 );
717 #endif
718
719
720 /*******************************************************************/
721 /* imls - list all images found in flash */
722 /*******************************************************************/
723 #if defined(CONFIG_CMD_IMLS)
724 int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
725 {
726         flash_info_t *info;
727         int i, j;
728         void *hdr;
729
730         for (i = 0, info = &flash_info[0];
731                 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
732
733                 if (info->flash_id == FLASH_UNKNOWN)
734                         goto next_bank;
735                 for (j = 0; j < info->sector_count; ++j) {
736
737                         hdr = (void *)info->start[j];
738                         if (!hdr)
739                                 goto next_sector;
740
741                         switch (genimg_get_format (hdr)) {
742                         case IMAGE_FORMAT_LEGACY:
743                                 if (!image_check_hcrc (hdr))
744                                         goto next_sector;
745
746                                 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
747                                 image_print_contents (hdr);
748
749                                 puts ("   Verifying Checksum ... ");
750                                 if (!image_check_dcrc (hdr)) {
751                                         puts ("Bad Data CRC\n");
752                                 } else {
753                                         puts ("OK\n");
754                                 }
755                                 break;
756 #if defined(CONFIG_FIT)
757                         case IMAGE_FORMAT_FIT:
758                                 if (!fit_check_format (hdr))
759                                         goto next_sector;
760
761                                 printf ("FIT Image at %08lX:\n", (ulong)hdr);
762                                 fit_print_contents (hdr);
763                                 break;
764 #endif
765                         default:
766                                 goto next_sector;
767                         }
768
769 next_sector:            ;
770                 }
771 next_bank:      ;
772         }
773
774         return (0);
775 }
776
777 U_BOOT_CMD(
778         imls,   1,              1,      do_imls,
779         "imls    - list all images found in flash\n",
780         "\n"
781         "    - Prints information about all images found at sector\n"
782         "      boundaries in flash.\n"
783 );
784 #endif
785
786 /*******************************************************************/
787 /* helper routines */
788 /*******************************************************************/
789 #ifdef CONFIG_SILENT_CONSOLE
790 static void fixup_silent_linux ()
791 {
792         char buf[256], *start, *end;
793         char *cmdline = getenv ("bootargs");
794
795         /* Only fix cmdline when requested */
796         if (!(gd->flags & GD_FLG_SILENT))
797                 return;
798
799         debug ("before silent fix-up: %s\n", cmdline);
800         if (cmdline) {
801                 if ((start = strstr (cmdline, "console=")) != NULL) {
802                         end = strchr (start, ' ');
803                         strncpy (buf, cmdline, (start - cmdline + 8));
804                         if (end)
805                                 strcpy (buf + (start - cmdline + 8), end);
806                         else
807                                 buf[start - cmdline + 8] = '\0';
808                 } else {
809                         strcpy (buf, cmdline);
810                         strcat (buf, " console=");
811                 }
812         } else {
813                 strcpy (buf, "console=");
814         }
815
816         setenv ("bootargs", buf);
817         debug ("after silent fix-up: %s\n", buf);
818 }
819 #endif /* CONFIG_SILENT_CONSOLE */
820
821
822 /*******************************************************************/
823 /* OS booting routines */
824 /*******************************************************************/
825
826 static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
827                             int argc, char *argv[],
828                             bootm_headers_t *images)
829 {
830         void (*loader)(bd_t *, image_header_t *, char *, char *);
831         image_header_t *os_hdr, *hdr;
832         ulong kernel_data, kernel_len;
833         char *consdev;
834         char *cmdline;
835
836 #if defined(CONFIG_FIT)
837         if (!images->legacy_hdr_valid) {
838                 fit_unsupported_reset ("NetBSD");
839                 do_reset (cmdtp, flag, argc, argv);
840         }
841 #endif
842         hdr = images->legacy_hdr_os;
843
844         /*
845          * Booting a (NetBSD) kernel image
846          *
847          * This process is pretty similar to a standalone application:
848          * The (first part of an multi-) image must be a stage-2 loader,
849          * which in turn is responsible for loading & invoking the actual
850          * kernel.  The only differences are the parameters being passed:
851          * besides the board info strucure, the loader expects a command
852          * line, the name of the console device, and (optionally) the
853          * address of the original image header.
854          */
855         os_hdr = NULL;
856         if (image_check_type (hdr, IH_TYPE_MULTI)) {
857                 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
858                 if (kernel_len)
859                         os_hdr = hdr;
860         }
861
862         consdev = "";
863 #if   defined (CONFIG_8xx_CONS_SMC1)
864         consdev = "smc1";
865 #elif defined (CONFIG_8xx_CONS_SMC2)
866         consdev = "smc2";
867 #elif defined (CONFIG_8xx_CONS_SCC2)
868         consdev = "scc2";
869 #elif defined (CONFIG_8xx_CONS_SCC3)
870         consdev = "scc3";
871 #endif
872
873         if (argc > 2) {
874                 ulong len;
875                 int   i;
876
877                 for (i = 2, len = 0; i < argc; i += 1)
878                         len += strlen (argv[i]) + 1;
879                 cmdline = malloc (len);
880
881                 for (i = 2, len = 0; i < argc; i += 1) {
882                         if (i > 2)
883                                 cmdline[len++] = ' ';
884                         strcpy (&cmdline[len], argv[i]);
885                         len += strlen (argv[i]);
886                 }
887         } else if ((cmdline = getenv ("bootargs")) == NULL) {
888                 cmdline = "";
889         }
890
891         loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
892
893         printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
894                 (ulong)loader);
895
896         show_boot_progress (15);
897
898         /*
899          * NetBSD Stage-2 Loader Parameters:
900          *   r3: ptr to board info data
901          *   r4: image address
902          *   r5: console device
903          *   r6: boot args string
904          */
905         (*loader) (gd->bd, os_hdr, consdev, cmdline);
906 }
907
908 #ifdef CONFIG_LYNXKDI
909 static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
910                              int argc, char *argv[],
911                              bootm_headers_t *images)
912 {
913         image_header_t *hdr = images->legacy_hdr_os;
914
915 #if defined(CONFIG_FIT)
916         if (!images->legacy_hdr_valid) {
917                 fit_unsupported_reset ("Lynx");
918                 do_reset (cmdtp, flag, argc, argv);
919         }
920 #endif
921
922         lynxkdi_boot ((image_header_t *)hdr);
923 }
924 #endif /* CONFIG_LYNXKDI */
925
926 static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
927                            int argc, char *argv[],
928                            bootm_headers_t *images)
929 {
930         image_header_t *hdr = images->legacy_hdr_os;
931         void (*entry_point)(bd_t *);
932
933 #if defined(CONFIG_FIT)
934         if (!images->legacy_hdr_valid) {
935                 fit_unsupported_reset ("RTEMS");
936                 do_reset (cmdtp, flag, argc, argv);
937         }
938 #endif
939
940         entry_point = (void (*)(bd_t *))image_get_ep (hdr);
941
942         printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
943                 (ulong)entry_point);
944
945         show_boot_progress (15);
946
947         /*
948          * RTEMS Parameters:
949          *   r3: ptr to board info data
950          */
951         (*entry_point)(gd->bd);
952 }
953
954 #if defined(CONFIG_CMD_ELF)
955 static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
956                              int argc, char *argv[],
957                              bootm_headers_t *images)
958 {
959         char str[80];
960         image_header_t *hdr = images->legacy_hdr_os;
961
962 #if defined(CONFIG_FIT)
963         if (hdr == NULL) {
964                 fit_unsupported_reset ("VxWorks");
965                 do_reset (cmdtp, flag, argc, argv);
966         }
967 #endif
968
969         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
970         setenv("loadaddr", str);
971         do_bootvx(cmdtp, 0, 0, NULL);
972 }
973
974 static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
975                             int argc, char *argv[],
976                             bootm_headers_t *images)
977 {
978         char *local_args[2];
979         char str[16];
980         image_header_t *hdr = images->legacy_hdr_os;
981
982 #if defined(CONFIG_FIT)
983         if (!images->legacy_hdr_valid) {
984                 fit_unsupported_reset ("QNX");
985                 do_reset (cmdtp, flag, argc, argv);
986         }
987 #endif
988
989         sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
990         local_args[0] = argv[0];
991         local_args[1] = str;    /* and provide it via the arguments */
992         do_bootelf(cmdtp, 0, 2, local_args);
993 }
994 #endif
995
996 #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
997 static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
998                            int argc, char *argv[],
999                            bootm_headers_t *images)
1000 {
1001         ulong top;
1002         char *s, *cmdline;
1003         char **fwenv, **ss;
1004         int i, j, nxt, len, envno, envsz;
1005         bd_t *kbd;
1006         void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1007         image_header_t *hdr = images->legacy_hdr_os;
1008
1009 #if defined(CONFIG_FIT)
1010         if (!images->legacy_hdr_valid) {
1011                 fit_unsupported_reset ("ARTOS");
1012                 do_reset (cmdtp, flag, argc, argv);
1013         }
1014 #endif
1015
1016         /*
1017          * Booting an ARTOS kernel image + application
1018          */
1019
1020         /* this used to be the top of memory, but was wrong... */
1021 #ifdef CONFIG_PPC
1022         /* get stack pointer */
1023         asm volatile ("mr %0,1" : "=r"(top) );
1024 #endif
1025         debug ("## Current stack ends at 0x%08lX ", top);
1026
1027         top -= 2048;            /* just to be sure */
1028         if (top > CFG_BOOTMAPSZ)
1029                 top = CFG_BOOTMAPSZ;
1030         top &= ~0xF;
1031
1032         debug ("=> set upper limit to 0x%08lX\n", top);
1033
1034         /* first check the artos specific boot args, then the linux args*/
1035         if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
1036                 s = "";
1037
1038         /* get length of cmdline, and place it */
1039         len = strlen (s);
1040         top = (top - (len + 1)) & ~0xF;
1041         cmdline = (char *)top;
1042         debug ("## cmdline at 0x%08lX ", top);
1043         strcpy (cmdline, s);
1044
1045         /* copy bdinfo */
1046         top = (top - sizeof (bd_t)) & ~0xF;
1047         debug ("## bd at 0x%08lX ", top);
1048         kbd = (bd_t *)top;
1049         memcpy (kbd, gd->bd, sizeof (bd_t));
1050
1051         /* first find number of env entries, and their size */
1052         envno = 0;
1053         envsz = 0;
1054         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1055                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1056                         ;
1057                 envno++;
1058                 envsz += (nxt - i) + 1; /* plus trailing zero */
1059         }
1060         envno++;        /* plus the terminating zero */
1061         debug ("## %u envvars total size %u ", envno, envsz);
1062
1063         top = (top - sizeof (char **) * envno) & ~0xF;
1064         fwenv = (char **)top;
1065         debug ("## fwenv at 0x%08lX ", top);
1066
1067         top = (top - envsz) & ~0xF;
1068         s = (char *)top;
1069         ss = fwenv;
1070
1071         /* now copy them */
1072         for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1073                 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1074                         ;
1075                 *ss++ = s;
1076                 for (j = i; j < nxt; ++j)
1077                         *s++ = env_get_char (j);
1078                 *s++ = '\0';
1079         }
1080         *ss++ = NULL;   /* terminate */
1081
1082         entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
1083         (*entry) (kbd, cmdline, fwenv, top);
1084 }
1085 #endif