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