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