Merge branch 'u-boot-atmel/master' into 'u-boot-arm/master'
[platform/kernel/u-boot.git] / common / cmd_bootm.c
1 /*
2  * (C) Copyright 2000-2009
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 <u-boot/zlib.h>
34 #include <bzlib.h>
35 #include <environment.h>
36 #include <lmb.h>
37 #include <linux/ctype.h>
38 #include <asm/byteorder.h>
39 #include <linux/compiler.h>
40
41 #if defined(CONFIG_CMD_USB)
42 #include <usb.h>
43 #endif
44
45 #ifdef CONFIG_SYS_HUSH_PARSER
46 #include <hush.h>
47 #endif
48
49 #if defined(CONFIG_OF_LIBFDT)
50 #include <libfdt.h>
51 #include <fdt_support.h>
52 #endif
53
54 #ifdef CONFIG_LZMA
55 #include <lzma/LzmaTypes.h>
56 #include <lzma/LzmaDec.h>
57 #include <lzma/LzmaTools.h>
58 #endif /* CONFIG_LZMA */
59
60 #ifdef CONFIG_LZO
61 #include <linux/lzo.h>
62 #endif /* CONFIG_LZO */
63
64 DECLARE_GLOBAL_DATA_PTR;
65
66 #ifndef CONFIG_SYS_BOOTM_LEN
67 #define CONFIG_SYS_BOOTM_LEN    0x800000        /* use 8MByte as default max gunzip size */
68 #endif
69
70 #ifdef CONFIG_BZIP2
71 extern void bz_internal_error(int);
72 #endif
73
74 #if defined(CONFIG_CMD_IMI)
75 static int image_info(unsigned long addr);
76 #endif
77
78 #if defined(CONFIG_CMD_IMLS)
79 #include <flash.h>
80 #include <mtd/cfi_flash.h>
81 extern flash_info_t flash_info[]; /* info for FLASH chips */
82 #endif
83
84 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
85 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
86 #endif
87
88 #include <linux/err.h>
89 #include <nand.h>
90
91 #ifdef CONFIG_SILENT_CONSOLE
92 static void fixup_silent_linux(void);
93 #endif
94
95 static image_header_t *image_get_kernel(ulong img_addr, int verify);
96 #if defined(CONFIG_FIT)
97 static int fit_check_kernel(const void *fit, int os_noffset, int verify);
98 #endif
99
100 static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
101                                 char * const argv[], bootm_headers_t *images,
102                                 ulong *os_data, ulong *os_len);
103
104 /*
105  *  Continue booting an OS image; caller already has:
106  *  - copied image header to global variable `header'
107  *  - checked header magic number, checksums (both header & image),
108  *  - verified image architecture (PPC) and type (KERNEL or MULTI),
109  *  - loaded (first part of) image to header load address,
110  *  - disabled interrupts.
111  */
112 typedef int boot_os_fn(int flag, int argc, char * const argv[],
113                         bootm_headers_t *images); /* pointers to os/initrd/fdt */
114
115 #ifdef CONFIG_BOOTM_LINUX
116 extern boot_os_fn do_bootm_linux;
117 #endif
118 #ifdef CONFIG_BOOTM_NETBSD
119 static boot_os_fn do_bootm_netbsd;
120 #endif
121 #if defined(CONFIG_LYNXKDI)
122 static boot_os_fn do_bootm_lynxkdi;
123 extern void lynxkdi_boot(image_header_t *);
124 #endif
125 #ifdef CONFIG_BOOTM_RTEMS
126 static boot_os_fn do_bootm_rtems;
127 #endif
128 #if defined(CONFIG_BOOTM_OSE)
129 static boot_os_fn do_bootm_ose;
130 #endif
131 #if defined(CONFIG_CMD_ELF)
132 static boot_os_fn do_bootm_vxworks;
133 static boot_os_fn do_bootm_qnxelf;
134 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
135 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
136 #endif
137 #if defined(CONFIG_INTEGRITY)
138 static boot_os_fn do_bootm_integrity;
139 #endif
140
141 static boot_os_fn *boot_os[] = {
142 #ifdef CONFIG_BOOTM_LINUX
143         [IH_OS_LINUX] = do_bootm_linux,
144 #endif
145 #ifdef CONFIG_BOOTM_NETBSD
146         [IH_OS_NETBSD] = do_bootm_netbsd,
147 #endif
148 #ifdef CONFIG_LYNXKDI
149         [IH_OS_LYNXOS] = do_bootm_lynxkdi,
150 #endif
151 #ifdef CONFIG_BOOTM_RTEMS
152         [IH_OS_RTEMS] = do_bootm_rtems,
153 #endif
154 #if defined(CONFIG_BOOTM_OSE)
155         [IH_OS_OSE] = do_bootm_ose,
156 #endif
157 #if defined(CONFIG_CMD_ELF)
158         [IH_OS_VXWORKS] = do_bootm_vxworks,
159         [IH_OS_QNX] = do_bootm_qnxelf,
160 #endif
161 #ifdef CONFIG_INTEGRITY
162         [IH_OS_INTEGRITY] = do_bootm_integrity,
163 #endif
164 };
165
166 bootm_headers_t images;         /* pointers to os/initrd/fdt images */
167
168 /* Allow for arch specific config before we boot */
169 static void __arch_preboot_os(void)
170 {
171         /* please define platform specific arch_preboot_os() */
172 }
173 void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
174
175 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
176
177 #ifdef CONFIG_LMB
178 static void boot_start_lmb(bootm_headers_t *images)
179 {
180         ulong           mem_start;
181         phys_size_t     mem_size;
182
183         lmb_init(&images->lmb);
184
185         mem_start = getenv_bootm_low();
186         mem_size = getenv_bootm_size();
187
188         lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
189
190         arch_lmb_reserve(&images->lmb);
191         board_lmb_reserve(&images->lmb);
192 }
193 #else
194 #define lmb_reserve(lmb, base, size)
195 static inline void boot_start_lmb(bootm_headers_t *images) { }
196 #endif
197
198 static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
199 {
200         void            *os_hdr;
201         int             ret;
202
203         memset((void *)&images, 0, sizeof(images));
204         images.verify = getenv_yesno("verify");
205
206         boot_start_lmb(&images);
207
208         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
209
210         /* get kernel image header, start address and length */
211         os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
212                         &images, &images.os.image_start, &images.os.image_len);
213         if (images.os.image_len == 0) {
214                 puts("ERROR: can't get kernel image!\n");
215                 return 1;
216         }
217
218         /* get image parameters */
219         switch (genimg_get_format(os_hdr)) {
220         case IMAGE_FORMAT_LEGACY:
221                 images.os.type = image_get_type(os_hdr);
222                 images.os.comp = image_get_comp(os_hdr);
223                 images.os.os = image_get_os(os_hdr);
224
225                 images.os.end = image_get_image_end(os_hdr);
226                 images.os.load = image_get_load(os_hdr);
227                 break;
228 #if defined(CONFIG_FIT)
229         case IMAGE_FORMAT_FIT:
230                 if (fit_image_get_type(images.fit_hdr_os,
231                                         images.fit_noffset_os, &images.os.type)) {
232                         puts("Can't get image type!\n");
233                         bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
234                         return 1;
235                 }
236
237                 if (fit_image_get_comp(images.fit_hdr_os,
238                                         images.fit_noffset_os, &images.os.comp)) {
239                         puts("Can't get image compression!\n");
240                         bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
241                         return 1;
242                 }
243
244                 if (fit_image_get_os(images.fit_hdr_os,
245                                         images.fit_noffset_os, &images.os.os)) {
246                         puts("Can't get image OS!\n");
247                         bootstage_error(BOOTSTAGE_ID_FIT_OS);
248                         return 1;
249                 }
250
251                 images.os.end = fit_get_end(images.fit_hdr_os);
252
253                 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
254                                         &images.os.load)) {
255                         puts("Can't get image load address!\n");
256                         bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
257                         return 1;
258                 }
259                 break;
260 #endif
261         default:
262                 puts("ERROR: unknown image format type!\n");
263                 return 1;
264         }
265
266         /* find kernel entry point */
267         if (images.legacy_hdr_valid) {
268                 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
269 #if defined(CONFIG_FIT)
270         } else if (images.fit_uname_os) {
271                 ret = fit_image_get_entry(images.fit_hdr_os,
272                                 images.fit_noffset_os, &images.ep);
273                 if (ret) {
274                         puts("Can't get entry point property!\n");
275                         return 1;
276                 }
277 #endif
278         } else {
279                 puts("Could not find kernel entry point!\n");
280                 return 1;
281         }
282
283         if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
284                 images.os.load = images.os.image_start;
285                 images.ep += images.os.load;
286         }
287
288         if (((images.os.type == IH_TYPE_KERNEL) ||
289              (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
290              (images.os.type == IH_TYPE_MULTI)) &&
291             (images.os.os == IH_OS_LINUX)) {
292                 /* find ramdisk */
293                 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
294                                 &images.rd_start, &images.rd_end);
295                 if (ret) {
296                         puts("Ramdisk image is corrupt or invalid\n");
297                         return 1;
298                 }
299
300 #if defined(CONFIG_OF_LIBFDT)
301                 /* find flattened device tree */
302                 ret = boot_get_fdt(flag, argc, argv, &images,
303                                    &images.ft_addr, &images.ft_len);
304                 if (ret) {
305                         puts("Could not find a valid device tree\n");
306                         return 1;
307                 }
308
309                 set_working_fdt_addr(images.ft_addr);
310 #endif
311         }
312
313         images.os.start = (ulong)os_hdr;
314         images.state = BOOTM_STATE_START;
315
316         return 0;
317 }
318
319 #define BOOTM_ERR_RESET         -1
320 #define BOOTM_ERR_OVERLAP       -2
321 #define BOOTM_ERR_UNIMPLEMENTED -3
322 static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
323 {
324         uint8_t comp = os.comp;
325         ulong load = os.load;
326         ulong blob_start = os.start;
327         ulong blob_end = os.end;
328         ulong image_start = os.image_start;
329         ulong image_len = os.image_len;
330         __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
331         int no_overlap = 0;
332 #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
333         int ret;
334 #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
335
336         const char *type_name = genimg_get_type_name(os.type);
337
338         switch (comp) {
339         case IH_COMP_NONE:
340                 if (load == blob_start || load == image_start) {
341                         printf("   XIP %s ... ", type_name);
342                         no_overlap = 1;
343                 } else {
344                         printf("   Loading %s ... ", type_name);
345                         memmove_wd((void *)load, (void *)image_start,
346                                         image_len, CHUNKSZ);
347                 }
348                 *load_end = load + image_len;
349                 puts("OK\n");
350                 break;
351 #ifdef CONFIG_GZIP
352         case IH_COMP_GZIP:
353                 printf("   Uncompressing %s ... ", type_name);
354                 if (gunzip((void *)load, unc_len,
355                                 (uchar *)image_start, &image_len) != 0) {
356                         puts("GUNZIP: uncompress, out-of-mem or overwrite "
357                                 "error - must RESET board to recover\n");
358                         if (boot_progress)
359                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
360                         return BOOTM_ERR_RESET;
361                 }
362
363                 *load_end = load + image_len;
364                 break;
365 #endif /* CONFIG_GZIP */
366 #ifdef CONFIG_BZIP2
367         case IH_COMP_BZIP2:
368                 printf("   Uncompressing %s ... ", type_name);
369                 /*
370                  * If we've got less than 4 MB of malloc() space,
371                  * use slower decompression algorithm which requires
372                  * at most 2300 KB of memory.
373                  */
374                 int i = BZ2_bzBuffToBuffDecompress((char *)load,
375                                         &unc_len, (char *)image_start, image_len,
376                                         CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
377                 if (i != BZ_OK) {
378                         printf("BUNZIP2: uncompress or overwrite error %d "
379                                 "- must RESET board to recover\n", i);
380                         if (boot_progress)
381                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
382                         return BOOTM_ERR_RESET;
383                 }
384
385                 *load_end = load + unc_len;
386                 break;
387 #endif /* CONFIG_BZIP2 */
388 #ifdef CONFIG_LZMA
389         case IH_COMP_LZMA: {
390                 SizeT lzma_len = unc_len;
391                 printf("   Uncompressing %s ... ", type_name);
392
393                 ret = lzmaBuffToBuffDecompress(
394                         (unsigned char *)load, &lzma_len,
395                         (unsigned char *)image_start, image_len);
396                 unc_len = lzma_len;
397                 if (ret != SZ_OK) {
398                         printf("LZMA: uncompress or overwrite error %d "
399                                 "- must RESET board to recover\n", ret);
400                         bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
401                         return BOOTM_ERR_RESET;
402                 }
403                 *load_end = load + unc_len;
404                 break;
405         }
406 #endif /* CONFIG_LZMA */
407 #ifdef CONFIG_LZO
408         case IH_COMP_LZO:
409                 printf("   Uncompressing %s ... ", type_name);
410
411                 ret = lzop_decompress((const unsigned char *)image_start,
412                                           image_len, (unsigned char *)load,
413                                           &unc_len);
414                 if (ret != LZO_E_OK) {
415                         printf("LZO: uncompress or overwrite error %d "
416                               "- must RESET board to recover\n", ret);
417                         if (boot_progress)
418                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
419                         return BOOTM_ERR_RESET;
420                 }
421
422                 *load_end = load + unc_len;
423                 break;
424 #endif /* CONFIG_LZO */
425         default:
426                 printf("Unimplemented compression type %d\n", comp);
427                 return BOOTM_ERR_UNIMPLEMENTED;
428         }
429
430         flush_cache(load, (*load_end - load) * sizeof(ulong));
431
432         puts("OK\n");
433         debug("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
434         bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
435
436         if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
437                 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
438                         blob_start, blob_end);
439                 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
440                         *load_end);
441
442                 return BOOTM_ERR_OVERLAP;
443         }
444
445         return 0;
446 }
447
448 static int bootm_start_standalone(ulong iflag, int argc, char * const argv[])
449 {
450         char  *s;
451         int   (*appl)(int, char * const []);
452
453         /* Don't start if "autostart" is set to "no" */
454         if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
455                 setenv_hex("filesize", images.os.image_len);
456                 return 0;
457         }
458         appl = (int (*)(int, char * const []))(ulong)ntohl(images.ep);
459         (*appl)(argc-1, &argv[1]);
460         return 0;
461 }
462
463 /* we overload the cmd field with our state machine info instead of a
464  * function pointer */
465 static cmd_tbl_t cmd_bootm_sub[] = {
466         U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
467         U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
468 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
469         U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
470 #endif
471 #ifdef CONFIG_OF_LIBFDT
472         U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
473 #endif
474         U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
475         U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
476         U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
477         U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
478 };
479
480 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
481                         char * const argv[])
482 {
483         int ret = 0;
484         long state;
485         cmd_tbl_t *c;
486         boot_os_fn *boot_fn;
487
488         c = find_cmd_tbl(argv[1], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
489
490         if (c) {
491                 state = (long)c->cmd;
492
493                 /* treat start special since it resets the state machine */
494                 if (state == BOOTM_STATE_START) {
495                         argc--;
496                         argv++;
497                         return bootm_start(cmdtp, flag, argc, argv);
498                 }
499         } else {
500                 /* Unrecognized command */
501                 return CMD_RET_USAGE;
502         }
503
504         if (images.state < BOOTM_STATE_START ||
505             images.state >= state) {
506                 printf("Trying to execute a command out of order\n");
507                 return CMD_RET_USAGE;
508         }
509
510         images.state |= state;
511         boot_fn = boot_os[images.os.os];
512
513         switch (state) {
514                 ulong load_end;
515                 case BOOTM_STATE_START:
516                         /* should never occur */
517                         break;
518                 case BOOTM_STATE_LOADOS:
519                         ret = bootm_load_os(images.os, &load_end, 0);
520                         if (ret)
521                                 return ret;
522
523                         lmb_reserve(&images.lmb, images.os.load,
524                                         (load_end - images.os.load));
525                         break;
526 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
527                 case BOOTM_STATE_RAMDISK:
528                 {
529                         ulong rd_len = images.rd_end - images.rd_start;
530
531                         ret = boot_ramdisk_high(&images.lmb, images.rd_start,
532                                 rd_len, &images.initrd_start, &images.initrd_end);
533                         if (ret)
534                                 return ret;
535
536                         setenv_hex("initrd_start", images.initrd_start);
537                         setenv_hex("initrd_end", images.initrd_end);
538                 }
539                         break;
540 #endif
541 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
542                 case BOOTM_STATE_FDT:
543                 {
544                         boot_fdt_add_mem_rsv_regions(&images.lmb,
545                                                      images.ft_addr);
546                         ret = boot_relocate_fdt(&images.lmb,
547                                 &images.ft_addr, &images.ft_len);
548                         break;
549                 }
550 #endif
551                 case BOOTM_STATE_OS_CMDLINE:
552                         ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, &images);
553                         if (ret)
554                                 printf("cmdline subcommand not supported\n");
555                         break;
556                 case BOOTM_STATE_OS_BD_T:
557                         ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, &images);
558                         if (ret)
559                                 printf("bdt subcommand not supported\n");
560                         break;
561                 case BOOTM_STATE_OS_PREP:
562                         ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, &images);
563                         if (ret)
564                                 printf("prep subcommand not supported\n");
565                         break;
566                 case BOOTM_STATE_OS_GO:
567                         disable_interrupts();
568 #ifdef CONFIG_NETCONSOLE
569                         /*
570                          * Stop the ethernet stack if NetConsole could have
571                          * left it up
572                          */
573                         eth_halt();
574 #endif
575                         arch_preboot_os();
576                         boot_fn(BOOTM_STATE_OS_GO, argc, argv, &images);
577                         break;
578         }
579
580         return ret;
581 }
582
583 /*******************************************************************/
584 /* bootm - boot application image from image in memory */
585 /*******************************************************************/
586
587 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
588 {
589         ulong           iflag;
590         ulong           load_end = 0;
591         int             ret;
592         boot_os_fn      *boot_fn;
593 #ifdef CONFIG_NEEDS_MANUAL_RELOC
594         static int relocated = 0;
595
596         if (!relocated) {
597                 int i;
598
599                 /* relocate boot function table */
600                 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
601                         if (boot_os[i] != NULL)
602                                 boot_os[i] += gd->reloc_off;
603
604                 /* relocate names of sub-command table */
605                 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
606                         cmd_bootm_sub[i].name += gd->reloc_off;
607
608                 relocated = 1;
609         }
610 #endif
611
612         /* determine if we have a sub command */
613         if (argc > 1) {
614                 char *endp;
615
616                 simple_strtoul(argv[1], &endp, 16);
617                 /* endp pointing to NULL means that argv[1] was just a
618                  * valid number, pass it along to the normal bootm processing
619                  *
620                  * If endp is ':' or '#' assume a FIT identifier so pass
621                  * along for normal processing.
622                  *
623                  * Right now we assume the first arg should never be '-'
624                  */
625                 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
626                         return do_bootm_subcommand(cmdtp, flag, argc, argv);
627         }
628
629         if (bootm_start(cmdtp, flag, argc, argv))
630                 return 1;
631
632         /*
633          * We have reached the point of no return: we are going to
634          * overwrite all exception vector code, so we cannot easily
635          * recover from any failures any more...
636          */
637         iflag = disable_interrupts();
638
639 #ifdef CONFIG_NETCONSOLE
640         /* Stop the ethernet stack if NetConsole could have left it up */
641         eth_halt();
642 #endif
643
644 #if defined(CONFIG_CMD_USB)
645         /*
646          * turn off USB to prevent the host controller from writing to the
647          * SDRAM while Linux is booting. This could happen (at least for OHCI
648          * controller), because the HCCA (Host Controller Communication Area)
649          * lies within the SDRAM and the host controller writes continously to
650          * this area (as busmaster!). The HccaFrameNumber is for example
651          * updated every 1 ms within the HCCA structure in SDRAM! For more
652          * details see the OpenHCI specification.
653          */
654         usb_stop();
655 #endif
656
657         ret = bootm_load_os(images.os, &load_end, 1);
658
659         if (ret < 0) {
660                 if (ret == BOOTM_ERR_RESET)
661                         do_reset(cmdtp, flag, argc, argv);
662                 if (ret == BOOTM_ERR_OVERLAP) {
663                         if (images.legacy_hdr_valid) {
664                                 image_header_t *hdr;
665                                 hdr = &images.legacy_hdr_os_copy;
666                                 if (image_get_type(hdr) == IH_TYPE_MULTI)
667                                         puts("WARNING: legacy format multi "
668                                                 "component image "
669                                                 "overwritten\n");
670                         } else {
671                                 puts("ERROR: new format image overwritten - "
672                                         "must RESET the board to recover\n");
673                                 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
674                                 do_reset(cmdtp, flag, argc, argv);
675                         }
676                 }
677                 if (ret == BOOTM_ERR_UNIMPLEMENTED) {
678                         if (iflag)
679                                 enable_interrupts();
680                         bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
681                         return 1;
682                 }
683         }
684
685         lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
686
687         if (images.os.type == IH_TYPE_STANDALONE) {
688                 if (iflag)
689                         enable_interrupts();
690                 /* This may return when 'autostart' is 'no' */
691                 bootm_start_standalone(iflag, argc, argv);
692                 return 0;
693         }
694
695         bootstage_mark(BOOTSTAGE_ID_CHECK_BOOT_OS);
696
697 #ifdef CONFIG_SILENT_CONSOLE
698         if (images.os.os == IH_OS_LINUX)
699                 fixup_silent_linux();
700 #endif
701
702         boot_fn = boot_os[images.os.os];
703
704         if (boot_fn == NULL) {
705                 if (iflag)
706                         enable_interrupts();
707                 printf("ERROR: booting os '%s' (%d) is not supported\n",
708                         genimg_get_os_name(images.os.os), images.os.os);
709                 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
710                 return 1;
711         }
712
713         arch_preboot_os();
714
715         boot_fn(0, argc, argv, &images);
716
717         bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
718 #ifdef DEBUG
719         puts("\n## Control returned to monitor - resetting...\n");
720 #endif
721         do_reset(cmdtp, flag, argc, argv);
722
723         return 1;
724 }
725
726 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
727 {
728         const char *ep = getenv("autostart");
729
730         if (ep && !strcmp(ep, "yes")) {
731                 char *local_args[2];
732                 local_args[0] = (char *)cmd;
733                 local_args[1] = NULL;
734                 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
735                 return do_bootm(cmdtp, 0, 1, local_args);
736         }
737
738         return 0;
739 }
740
741 /**
742  * image_get_kernel - verify legacy format kernel image
743  * @img_addr: in RAM address of the legacy format image to be verified
744  * @verify: data CRC verification flag
745  *
746  * image_get_kernel() verifies legacy image integrity and returns pointer to
747  * legacy image header if image verification was completed successfully.
748  *
749  * returns:
750  *     pointer to a legacy image header if valid image was found
751  *     otherwise return NULL
752  */
753 static image_header_t *image_get_kernel(ulong img_addr, int verify)
754 {
755         image_header_t *hdr = (image_header_t *)img_addr;
756
757         if (!image_check_magic(hdr)) {
758                 puts("Bad Magic Number\n");
759                 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
760                 return NULL;
761         }
762         bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
763
764         if (!image_check_hcrc(hdr)) {
765                 puts("Bad Header Checksum\n");
766                 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
767                 return NULL;
768         }
769
770         bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
771         image_print_contents(hdr);
772
773         if (verify) {
774                 puts("   Verifying Checksum ... ");
775                 if (!image_check_dcrc(hdr)) {
776                         printf("Bad Data CRC\n");
777                         bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
778                         return NULL;
779                 }
780                 puts("OK\n");
781         }
782         bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
783
784         if (!image_check_target_arch(hdr)) {
785                 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
786                 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
787                 return NULL;
788         }
789         return hdr;
790 }
791
792 /**
793  * fit_check_kernel - verify FIT format kernel subimage
794  * @fit_hdr: pointer to the FIT image header
795  * os_noffset: kernel subimage node offset within FIT image
796  * @verify: data CRC verification flag
797  *
798  * fit_check_kernel() verifies integrity of the kernel subimage and from
799  * specified FIT image.
800  *
801  * returns:
802  *     1, on success
803  *     0, on failure
804  */
805 #if defined(CONFIG_FIT)
806 static int fit_check_kernel(const void *fit, int os_noffset, int verify)
807 {
808         fit_image_print(fit, os_noffset, "   ");
809
810         if (verify) {
811                 puts("   Verifying Hash Integrity ... ");
812                 if (!fit_image_check_hashes(fit, os_noffset)) {
813                         puts("Bad Data Hash\n");
814                         bootstage_error(BOOTSTAGE_ID_FIT_CHECK_HASH);
815                         return 0;
816                 }
817                 puts("OK\n");
818         }
819         bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_ARCH);
820
821         if (!fit_image_check_target_arch(fit, os_noffset)) {
822                 puts("Unsupported Architecture\n");
823                 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_ARCH);
824                 return 0;
825         }
826
827         bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
828         if (!fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL) &&
829             !fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL_NOLOAD)) {
830                 puts("Not a kernel image\n");
831                 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
832                 return 0;
833         }
834
835         bootstage_mark(BOOTSTAGE_ID_FIT_CHECKED);
836         return 1;
837 }
838 #endif /* CONFIG_FIT */
839
840 /**
841  * boot_get_kernel - find kernel image
842  * @os_data: pointer to a ulong variable, will hold os data start address
843  * @os_len: pointer to a ulong variable, will hold os data length
844  *
845  * boot_get_kernel() tries to find a kernel image, verifies its integrity
846  * and locates kernel data.
847  *
848  * returns:
849  *     pointer to image header if valid image was found, plus kernel start
850  *     address and length, otherwise NULL
851  */
852 static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
853                 char * const argv[], bootm_headers_t *images, ulong *os_data,
854                 ulong *os_len)
855 {
856         image_header_t  *hdr;
857         ulong           img_addr;
858 #if defined(CONFIG_FIT)
859         void            *fit_hdr;
860         const char      *fit_uname_config = NULL;
861         const char      *fit_uname_kernel = NULL;
862         const void      *data;
863         size_t          len;
864         int             cfg_noffset;
865         int             os_noffset;
866 #endif
867
868         /* find out kernel image address */
869         if (argc < 2) {
870                 img_addr = load_addr;
871                 debug("*  kernel: default image load address = 0x%08lx\n",
872                                 load_addr);
873 #if defined(CONFIG_FIT)
874         } else if (fit_parse_conf(argv[1], load_addr, &img_addr,
875                                                         &fit_uname_config)) {
876                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
877                                 fit_uname_config, img_addr);
878         } else if (fit_parse_subimage(argv[1], load_addr, &img_addr,
879                                                         &fit_uname_kernel)) {
880                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
881                                 fit_uname_kernel, img_addr);
882 #endif
883         } else {
884                 img_addr = simple_strtoul(argv[1], NULL, 16);
885                 debug("*  kernel: cmdline image address = 0x%08lx\n", img_addr);
886         }
887
888         bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
889
890         /* copy from dataflash if needed */
891         img_addr = genimg_get_image(img_addr);
892
893         /* check image type, for FIT images get FIT kernel node */
894         *os_data = *os_len = 0;
895         switch (genimg_get_format((void *)img_addr)) {
896         case IMAGE_FORMAT_LEGACY:
897                 printf("## Booting kernel from Legacy Image at %08lx ...\n",
898                                 img_addr);
899                 hdr = image_get_kernel(img_addr, images->verify);
900                 if (!hdr)
901                         return NULL;
902                 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
903
904                 /* get os_data and os_len */
905                 switch (image_get_type(hdr)) {
906                 case IH_TYPE_KERNEL:
907                 case IH_TYPE_KERNEL_NOLOAD:
908                         *os_data = image_get_data(hdr);
909                         *os_len = image_get_data_size(hdr);
910                         break;
911                 case IH_TYPE_MULTI:
912                         image_multi_getimg(hdr, 0, os_data, os_len);
913                         break;
914                 case IH_TYPE_STANDALONE:
915                         *os_data = image_get_data(hdr);
916                         *os_len = image_get_data_size(hdr);
917                         break;
918                 default:
919                         printf("Wrong Image Type for %s command\n",
920                                 cmdtp->name);
921                         bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
922                         return NULL;
923                 }
924
925                 /*
926                  * copy image header to allow for image overwrites during
927                  * kernel decompression.
928                  */
929                 memmove(&images->legacy_hdr_os_copy, hdr,
930                         sizeof(image_header_t));
931
932                 /* save pointer to image header */
933                 images->legacy_hdr_os = hdr;
934
935                 images->legacy_hdr_valid = 1;
936                 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
937                 break;
938 #if defined(CONFIG_FIT)
939         case IMAGE_FORMAT_FIT:
940                 fit_hdr = (void *)img_addr;
941                 printf("## Booting kernel from FIT Image at %08lx ...\n",
942                                 img_addr);
943
944                 if (!fit_check_format(fit_hdr)) {
945                         puts("Bad FIT kernel image format!\n");
946                         bootstage_error(BOOTSTAGE_ID_FIT_FORMAT);
947                         return NULL;
948                 }
949                 bootstage_mark(BOOTSTAGE_ID_FIT_FORMAT);
950
951                 if (!fit_uname_kernel) {
952                         /*
953                          * no kernel image node unit name, try to get config
954                          * node first. If config unit node name is NULL
955                          * fit_conf_get_node() will try to find default config
956                          * node
957                          */
958                         bootstage_mark(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
959 #ifdef CONFIG_FIT_BEST_MATCH
960                         if (fit_uname_config)
961                                 cfg_noffset =
962                                         fit_conf_get_node(fit_hdr,
963                                                           fit_uname_config);
964                         else
965                                 cfg_noffset =
966                                         fit_conf_find_compat(fit_hdr,
967                                                              gd->fdt_blob);
968 #else
969                         cfg_noffset = fit_conf_get_node(fit_hdr,
970                                                         fit_uname_config);
971 #endif
972                         if (cfg_noffset < 0) {
973                                 bootstage_error(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
974                                 return NULL;
975                         }
976                         /* save configuration uname provided in the first
977                          * bootm argument
978                          */
979                         images->fit_uname_cfg = fdt_get_name(fit_hdr,
980                                                                 cfg_noffset,
981                                                                 NULL);
982                         printf("   Using '%s' configuration\n",
983                                 images->fit_uname_cfg);
984                         bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
985
986                         os_noffset = fit_conf_get_kernel_node(fit_hdr,
987                                                                 cfg_noffset);
988                         fit_uname_kernel = fit_get_name(fit_hdr, os_noffset,
989                                                         NULL);
990                 } else {
991                         /* get kernel component image node offset */
992                         bootstage_mark(BOOTSTAGE_ID_FIT_UNIT_NAME);
993                         os_noffset = fit_image_get_node(fit_hdr,
994                                                         fit_uname_kernel);
995                 }
996                 if (os_noffset < 0) {
997                         bootstage_error(BOOTSTAGE_ID_FIT_CONFIG);
998                         return NULL;
999                 }
1000
1001                 printf("   Trying '%s' kernel subimage\n", fit_uname_kernel);
1002
1003                 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_SUBIMAGE);
1004                 if (!fit_check_kernel(fit_hdr, os_noffset, images->verify))
1005                         return NULL;
1006
1007                 /* get kernel image data address and length */
1008                 if (fit_image_get_data(fit_hdr, os_noffset, &data, &len)) {
1009                         puts("Could not find kernel subimage data!\n");
1010                         bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO_ERR);
1011                         return NULL;
1012                 }
1013                 bootstage_mark(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1014
1015                 *os_len = len;
1016                 *os_data = (ulong)data;
1017                 images->fit_hdr_os = fit_hdr;
1018                 images->fit_uname_os = fit_uname_kernel;
1019                 images->fit_noffset_os = os_noffset;
1020                 break;
1021 #endif
1022         default:
1023                 printf("Wrong Image Format for %s command\n", cmdtp->name);
1024                 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1025                 return NULL;
1026         }
1027
1028         debug("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1029                         *os_data, *os_len, *os_len);
1030
1031         return (void *)img_addr;
1032 }
1033
1034 #ifdef CONFIG_SYS_LONGHELP
1035 static char bootm_help_text[] =
1036         "[addr [arg ...]]\n    - boot application image stored in memory\n"
1037         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1038         "\t'arg' can be the address of an initrd image\n"
1039 #if defined(CONFIG_OF_LIBFDT)
1040         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1041         "\ta third argument is required which is the address of the\n"
1042         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1043         "\tuse a '-' for the second argument. If you do not pass a third\n"
1044         "\ta bd_info struct will be passed instead\n"
1045 #endif
1046 #if defined(CONFIG_FIT)
1047         "\t\nFor the new multi component uImage format (FIT) addresses\n"
1048         "\tmust be extened to include component or configuration unit name:\n"
1049         "\taddr:<subimg_uname> - direct component image specification\n"
1050         "\taddr#<conf_uname>   - configuration specification\n"
1051         "\tUse iminfo command to get the list of existing component\n"
1052         "\timages and configurations.\n"
1053 #endif
1054         "\nSub-commands to do part of the bootm sequence.  The sub-commands "
1055         "must be\n"
1056         "issued in the order below (it's ok to not issue all sub-commands):\n"
1057         "\tstart [addr [arg ...]]\n"
1058         "\tloados  - load OS image\n"
1059 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
1060         "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1061 #endif
1062 #if defined(CONFIG_OF_LIBFDT)
1063         "\tfdt     - relocate flat device tree\n"
1064 #endif
1065         "\tcmdline - OS specific command line processing/setup\n"
1066         "\tbdt     - OS specific bd_t processing\n"
1067         "\tprep    - OS specific prep before relocation or go\n"
1068         "\tgo      - start OS";
1069 #endif
1070
1071 U_BOOT_CMD(
1072         bootm,  CONFIG_SYS_MAXARGS,     1,      do_bootm,
1073         "boot application image from memory", bootm_help_text
1074 );
1075
1076 /*******************************************************************/
1077 /* bootd - boot default image */
1078 /*******************************************************************/
1079 #if defined(CONFIG_CMD_BOOTD)
1080 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1081 {
1082         int rcode = 0;
1083
1084         if (run_command(getenv("bootcmd"), flag) < 0)
1085                 rcode = 1;
1086         return rcode;
1087 }
1088
1089 U_BOOT_CMD(
1090         boot,   1,      1,      do_bootd,
1091         "boot default, i.e., run 'bootcmd'",
1092         ""
1093 );
1094
1095 /* keep old command name "bootd" for backward compatibility */
1096 U_BOOT_CMD(
1097         bootd, 1,       1,      do_bootd,
1098         "boot default, i.e., run 'bootcmd'",
1099         ""
1100 );
1101
1102 #endif
1103
1104
1105 /*******************************************************************/
1106 /* iminfo - print header info for a requested image */
1107 /*******************************************************************/
1108 #if defined(CONFIG_CMD_IMI)
1109 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1110 {
1111         int     arg;
1112         ulong   addr;
1113         int     rcode = 0;
1114
1115         if (argc < 2) {
1116                 return image_info(load_addr);
1117         }
1118
1119         for (arg = 1; arg < argc; ++arg) {
1120                 addr = simple_strtoul(argv[arg], NULL, 16);
1121                 if (image_info(addr) != 0)
1122                         rcode = 1;
1123         }
1124         return rcode;
1125 }
1126
1127 static int image_info(ulong addr)
1128 {
1129         void *hdr = (void *)addr;
1130
1131         printf("\n## Checking Image at %08lx ...\n", addr);
1132
1133         switch (genimg_get_format(hdr)) {
1134         case IMAGE_FORMAT_LEGACY:
1135                 puts("   Legacy image found\n");
1136                 if (!image_check_magic(hdr)) {
1137                         puts("   Bad Magic Number\n");
1138                         return 1;
1139                 }
1140
1141                 if (!image_check_hcrc(hdr)) {
1142                         puts("   Bad Header Checksum\n");
1143                         return 1;
1144                 }
1145
1146                 image_print_contents(hdr);
1147
1148                 puts("   Verifying Checksum ... ");
1149                 if (!image_check_dcrc(hdr)) {
1150                         puts("   Bad Data CRC\n");
1151                         return 1;
1152                 }
1153                 puts("OK\n");
1154                 return 0;
1155 #if defined(CONFIG_FIT)
1156         case IMAGE_FORMAT_FIT:
1157                 puts("   FIT image found\n");
1158
1159                 if (!fit_check_format(hdr)) {
1160                         puts("Bad FIT image format!\n");
1161                         return 1;
1162                 }
1163
1164                 fit_print_contents(hdr);
1165
1166                 if (!fit_all_image_check_hashes(hdr)) {
1167                         puts("Bad hash in FIT image!\n");
1168                         return 1;
1169                 }
1170
1171                 return 0;
1172 #endif
1173         default:
1174                 puts("Unknown image format!\n");
1175                 break;
1176         }
1177
1178         return 1;
1179 }
1180
1181 U_BOOT_CMD(
1182         iminfo, CONFIG_SYS_MAXARGS,     1,      do_iminfo,
1183         "print header information for application image",
1184         "addr [addr ...]\n"
1185         "    - print header information for application image starting at\n"
1186         "      address 'addr' in memory; this includes verification of the\n"
1187         "      image contents (magic number, header and payload checksums)"
1188 );
1189 #endif
1190
1191
1192 /*******************************************************************/
1193 /* imls - list all images found in flash */
1194 /*******************************************************************/
1195 #if defined(CONFIG_CMD_IMLS)
1196 static int do_imls_nor(void)
1197 {
1198         flash_info_t *info;
1199         int i, j;
1200         void *hdr;
1201
1202         for (i = 0, info = &flash_info[0];
1203                 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
1204
1205                 if (info->flash_id == FLASH_UNKNOWN)
1206                         goto next_bank;
1207                 for (j = 0; j < info->sector_count; ++j) {
1208
1209                         hdr = (void *)info->start[j];
1210                         if (!hdr)
1211                                 goto next_sector;
1212
1213                         switch (genimg_get_format(hdr)) {
1214                         case IMAGE_FORMAT_LEGACY:
1215                                 if (!image_check_hcrc(hdr))
1216                                         goto next_sector;
1217
1218                                 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1219                                 image_print_contents(hdr);
1220
1221                                 puts("   Verifying Checksum ... ");
1222                                 if (!image_check_dcrc(hdr)) {
1223                                         puts("Bad Data CRC\n");
1224                                 } else {
1225                                         puts("OK\n");
1226                                 }
1227                                 break;
1228 #if defined(CONFIG_FIT)
1229                         case IMAGE_FORMAT_FIT:
1230                                 if (!fit_check_format(hdr))
1231                                         goto next_sector;
1232
1233                                 printf("FIT Image at %08lX:\n", (ulong)hdr);
1234                                 fit_print_contents(hdr);
1235                                 break;
1236 #endif
1237                         default:
1238                                 goto next_sector;
1239                         }
1240
1241 next_sector:            ;
1242                 }
1243 next_bank:      ;
1244         }
1245         return 0;
1246 }
1247 #endif
1248
1249 #if defined(CONFIG_CMD_IMLS_NAND)
1250 static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
1251                 size_t len)
1252 {
1253         void *imgdata;
1254         int ret;
1255
1256         imgdata = malloc(len);
1257         if (!imgdata) {
1258                 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
1259                                 nand_dev, off);
1260                 printf("   Low memory(cannot allocate memory for image)\n");
1261                 return -ENOMEM;
1262         }
1263
1264         ret = nand_read_skip_bad(nand, off, &len,
1265                         imgdata);
1266         if (ret < 0 && ret != -EUCLEAN) {
1267                 free(imgdata);
1268                 return ret;
1269         }
1270
1271         if (!image_check_hcrc(imgdata)) {
1272                 free(imgdata);
1273                 return 0;
1274         }
1275
1276         printf("Legacy Image at NAND device %d offset %08llX:\n",
1277                         nand_dev, off);
1278         image_print_contents(imgdata);
1279
1280         puts("   Verifying Checksum ... ");
1281         if (!image_check_dcrc(imgdata))
1282                 puts("Bad Data CRC\n");
1283         else
1284                 puts("OK\n");
1285
1286         free(imgdata);
1287
1288         return 0;
1289 }
1290
1291 static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
1292                 size_t len)
1293 {
1294         void *imgdata;
1295         int ret;
1296
1297         imgdata = malloc(len);
1298         if (!imgdata) {
1299                 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
1300                                 nand_dev, off);
1301                 printf("   Low memory(cannot allocate memory for image)\n");
1302                 return -ENOMEM;
1303         }
1304
1305         ret = nand_read_skip_bad(nand, off, &len,
1306                         imgdata);
1307         if (ret < 0 && ret != -EUCLEAN) {
1308                 free(imgdata);
1309                 return ret;
1310         }
1311
1312         if (!fit_check_format(imgdata)) {
1313                 free(imgdata);
1314                 return 0;
1315         }
1316
1317         printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
1318
1319         fit_print_contents(imgdata);
1320         free(imgdata);
1321
1322         return 0;
1323 }
1324
1325 static int do_imls_nand(void)
1326 {
1327         nand_info_t *nand;
1328         int nand_dev = nand_curr_device;
1329         size_t len;
1330         loff_t off;
1331         u32 buffer[16];
1332
1333         if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
1334                 puts("\nNo NAND devices available\n");
1335                 return -ENODEV;
1336         }
1337
1338         printf("\n");
1339
1340         for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
1341                 nand = &nand_info[nand_dev];
1342                 if (!nand->name || !nand->size)
1343                         continue;
1344
1345                 for (off = 0; off < nand->size; off += nand->erasesize) {
1346                         const image_header_t *header;
1347                         int ret;
1348
1349                         if (nand_block_isbad(nand, off))
1350                                 continue;
1351
1352                         len = sizeof(buffer);
1353
1354                         ret = nand_read(nand, off, &len, (u8 *)buffer);
1355                         if (ret < 0 && ret != -EUCLEAN) {
1356                                 printf("NAND read error %d at offset %08llX\n",
1357                                                 ret, off);
1358                                 continue;
1359                         }
1360
1361                         switch (genimg_get_format(buffer)) {
1362                         case IMAGE_FORMAT_LEGACY:
1363                                 header = (const image_header_t *)buffer;
1364
1365                                 len = image_get_image_size(header);
1366                                 nand_imls_legacyimage(nand, nand_dev, off, len);
1367                                 break;
1368 #if defined(CONFIG_FIT)
1369                         case IMAGE_FORMAT_FIT:
1370                                 len = fit_get_size(buffer);
1371                                 nand_imls_fitimage(nand, nand_dev, off, len);
1372                                 break;
1373 #endif
1374                         }
1375                 }
1376         }
1377
1378         return 0;
1379 }
1380 #endif
1381
1382 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
1383 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1384 {
1385         int ret_nor = 0, ret_nand = 0;
1386
1387 #if defined(CONFIG_CMD_IMLS)
1388         ret_nor = do_imls_nor();
1389 #endif
1390
1391 #if defined(CONFIG_CMD_IMLS_NAND)
1392         ret_nand = do_imls_nand();
1393 #endif
1394
1395         if (ret_nor)
1396                 return ret_nor;
1397
1398         if (ret_nand)
1399                 return ret_nand;
1400
1401         return (0);
1402 }
1403
1404 U_BOOT_CMD(
1405         imls,   1,              1,      do_imls,
1406         "list all images found in flash",
1407         "\n"
1408         "    - Prints information about all images found at sector/block\n"
1409         "      boundaries in nor/nand flash."
1410 );
1411 #endif
1412
1413 /*******************************************************************/
1414 /* helper routines */
1415 /*******************************************************************/
1416 #ifdef CONFIG_SILENT_CONSOLE
1417 static void fixup_silent_linux(void)
1418 {
1419         char buf[256], *start, *end;
1420         char *cmdline = getenv("bootargs");
1421
1422         /* Only fix cmdline when requested */
1423         if (!(gd->flags & GD_FLG_SILENT))
1424                 return;
1425
1426         debug("before silent fix-up: %s\n", cmdline);
1427         if (cmdline) {
1428                 start = strstr(cmdline, "console=");
1429                 if (start) {
1430                         end = strchr(start, ' ');
1431                         strncpy(buf, cmdline, (start - cmdline + 8));
1432                         if (end)
1433                                 strcpy(buf + (start - cmdline + 8), end);
1434                         else
1435                                 buf[start - cmdline + 8] = '\0';
1436                 } else {
1437                         strcpy(buf, cmdline);
1438                         strcat(buf, " console=");
1439                 }
1440         } else {
1441                 strcpy(buf, "console=");
1442         }
1443
1444         setenv("bootargs", buf);
1445         debug("after silent fix-up: %s\n", buf);
1446 }
1447 #endif /* CONFIG_SILENT_CONSOLE */
1448
1449
1450 /*******************************************************************/
1451 /* OS booting routines */
1452 /*******************************************************************/
1453
1454 #ifdef CONFIG_BOOTM_NETBSD
1455 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
1456                             bootm_headers_t *images)
1457 {
1458         void (*loader)(bd_t *, image_header_t *, char *, char *);
1459         image_header_t *os_hdr, *hdr;
1460         ulong kernel_data, kernel_len;
1461         char *consdev;
1462         char *cmdline;
1463
1464         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1465                 return 1;
1466
1467 #if defined(CONFIG_FIT)
1468         if (!images->legacy_hdr_valid) {
1469                 fit_unsupported_reset("NetBSD");
1470                 return 1;
1471         }
1472 #endif
1473         hdr = images->legacy_hdr_os;
1474
1475         /*
1476          * Booting a (NetBSD) kernel image
1477          *
1478          * This process is pretty similar to a standalone application:
1479          * The (first part of an multi-) image must be a stage-2 loader,
1480          * which in turn is responsible for loading & invoking the actual
1481          * kernel.  The only differences are the parameters being passed:
1482          * besides the board info strucure, the loader expects a command
1483          * line, the name of the console device, and (optionally) the
1484          * address of the original image header.
1485          */
1486         os_hdr = NULL;
1487         if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1488                 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
1489                 if (kernel_len)
1490                         os_hdr = hdr;
1491         }
1492
1493         consdev = "";
1494 #if   defined(CONFIG_8xx_CONS_SMC1)
1495         consdev = "smc1";
1496 #elif defined(CONFIG_8xx_CONS_SMC2)
1497         consdev = "smc2";
1498 #elif defined(CONFIG_8xx_CONS_SCC2)
1499         consdev = "scc2";
1500 #elif defined(CONFIG_8xx_CONS_SCC3)
1501         consdev = "scc3";
1502 #endif
1503
1504         if (argc > 2) {
1505                 ulong len;
1506                 int   i;
1507
1508                 for (i = 2, len = 0; i < argc; i += 1)
1509                         len += strlen(argv[i]) + 1;
1510                 cmdline = malloc(len);
1511
1512                 for (i = 2, len = 0; i < argc; i += 1) {
1513                         if (i > 2)
1514                                 cmdline[len++] = ' ';
1515                         strcpy(&cmdline[len], argv[i]);
1516                         len += strlen(argv[i]);
1517                 }
1518         } else if ((cmdline = getenv("bootargs")) == NULL) {
1519                 cmdline = "";
1520         }
1521
1522         loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
1523
1524         printf("## Transferring control to NetBSD stage-2 loader "
1525                 "(at address %08lx) ...\n",
1526                 (ulong)loader);
1527
1528         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1529
1530         /*
1531          * NetBSD Stage-2 Loader Parameters:
1532          *   r3: ptr to board info data
1533          *   r4: image address
1534          *   r5: console device
1535          *   r6: boot args string
1536          */
1537         (*loader)(gd->bd, os_hdr, consdev, cmdline);
1538
1539         return 1;
1540 }
1541 #endif /* CONFIG_BOOTM_NETBSD*/
1542
1543 #ifdef CONFIG_LYNXKDI
1544 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
1545                              bootm_headers_t *images)
1546 {
1547         image_header_t *hdr = &images->legacy_hdr_os_copy;
1548
1549         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1550                 return 1;
1551
1552 #if defined(CONFIG_FIT)
1553         if (!images->legacy_hdr_valid) {
1554                 fit_unsupported_reset("Lynx");
1555                 return 1;
1556         }
1557 #endif
1558
1559         lynxkdi_boot((image_header_t *)hdr);
1560
1561         return 1;
1562 }
1563 #endif /* CONFIG_LYNXKDI */
1564
1565 #ifdef CONFIG_BOOTM_RTEMS
1566 static int do_bootm_rtems(int flag, int argc, char * const argv[],
1567                            bootm_headers_t *images)
1568 {
1569         void (*entry_point)(bd_t *);
1570
1571         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1572                 return 1;
1573
1574 #if defined(CONFIG_FIT)
1575         if (!images->legacy_hdr_valid) {
1576                 fit_unsupported_reset("RTEMS");
1577                 return 1;
1578         }
1579 #endif
1580
1581         entry_point = (void (*)(bd_t *))images->ep;
1582
1583         printf("## Transferring control to RTEMS (at address %08lx) ...\n",
1584                 (ulong)entry_point);
1585
1586         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1587
1588         /*
1589          * RTEMS Parameters:
1590          *   r3: ptr to board info data
1591          */
1592         (*entry_point)(gd->bd);
1593
1594         return 1;
1595 }
1596 #endif /* CONFIG_BOOTM_RTEMS */
1597
1598 #if defined(CONFIG_BOOTM_OSE)
1599 static int do_bootm_ose(int flag, int argc, char * const argv[],
1600                            bootm_headers_t *images)
1601 {
1602         void (*entry_point)(void);
1603
1604         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1605                 return 1;
1606
1607 #if defined(CONFIG_FIT)
1608         if (!images->legacy_hdr_valid) {
1609                 fit_unsupported_reset("OSE");
1610                 return 1;
1611         }
1612 #endif
1613
1614         entry_point = (void (*)(void))images->ep;
1615
1616         printf("## Transferring control to OSE (at address %08lx) ...\n",
1617                 (ulong)entry_point);
1618
1619         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1620
1621         /*
1622          * OSE Parameters:
1623          *   None
1624          */
1625         (*entry_point)();
1626
1627         return 1;
1628 }
1629 #endif /* CONFIG_BOOTM_OSE */
1630
1631 #if defined(CONFIG_CMD_ELF)
1632 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
1633                              bootm_headers_t *images)
1634 {
1635         char str[80];
1636
1637         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1638                 return 1;
1639
1640 #if defined(CONFIG_FIT)
1641         if (!images->legacy_hdr_valid) {
1642                 fit_unsupported_reset("VxWorks");
1643                 return 1;
1644         }
1645 #endif
1646
1647         sprintf(str, "%lx", images->ep); /* write entry-point into string */
1648         setenv("loadaddr", str);
1649         do_bootvx(NULL, 0, 0, NULL);
1650
1651         return 1;
1652 }
1653
1654 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
1655                             bootm_headers_t *images)
1656 {
1657         char *local_args[2];
1658         char str[16];
1659
1660         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1661                 return 1;
1662
1663 #if defined(CONFIG_FIT)
1664         if (!images->legacy_hdr_valid) {
1665                 fit_unsupported_reset("QNX");
1666                 return 1;
1667         }
1668 #endif
1669
1670         sprintf(str, "%lx", images->ep); /* write entry-point into string */
1671         local_args[0] = argv[0];
1672         local_args[1] = str;    /* and provide it via the arguments */
1673         do_bootelf(NULL, 0, 2, local_args);
1674
1675         return 1;
1676 }
1677 #endif
1678
1679 #ifdef CONFIG_INTEGRITY
1680 static int do_bootm_integrity(int flag, int argc, char * const argv[],
1681                            bootm_headers_t *images)
1682 {
1683         void (*entry_point)(void);
1684
1685         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1686                 return 1;
1687
1688 #if defined(CONFIG_FIT)
1689         if (!images->legacy_hdr_valid) {
1690                 fit_unsupported_reset("INTEGRITY");
1691                 return 1;
1692         }
1693 #endif
1694
1695         entry_point = (void (*)(void))images->ep;
1696
1697         printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
1698                 (ulong)entry_point);
1699
1700         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1701
1702         /*
1703          * INTEGRITY Parameters:
1704          *   None
1705          */
1706         (*entry_point)();
1707
1708         return 1;
1709 }
1710 #endif
1711
1712 #ifdef CONFIG_CMD_BOOTZ
1713
1714 static int __bootz_setup(void *image, void **start, void **end)
1715 {
1716         /* Please define bootz_setup() for your platform */
1717
1718         puts("Your platform's zImage format isn't supported yet!\n");
1719         return -1;
1720 }
1721 int bootz_setup(void *image, void **start, void **end)
1722         __attribute__((weak, alias("__bootz_setup")));
1723
1724 /*
1725  * zImage booting support
1726  */
1727 static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1728                         char * const argv[], bootm_headers_t *images)
1729 {
1730         int ret;
1731         void *zi_start, *zi_end;
1732
1733         memset(images, 0, sizeof(bootm_headers_t));
1734
1735         boot_start_lmb(images);
1736
1737         /* Setup Linux kernel zImage entry point */
1738         if (argc < 2) {
1739                 images->ep = load_addr;
1740                 debug("*  kernel: default image load address = 0x%08lx\n",
1741                                 load_addr);
1742         } else {
1743                 images->ep = simple_strtoul(argv[1], NULL, 16);
1744                 debug("*  kernel: cmdline image address = 0x%08lx\n",
1745                         images->ep);
1746         }
1747
1748         ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
1749         if (ret != 0)
1750                 return 1;
1751
1752         lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1753
1754         /* Find ramdisk */
1755         ret = boot_get_ramdisk(argc, argv, images, IH_INITRD_ARCH,
1756                         &images->rd_start, &images->rd_end);
1757         if (ret) {
1758                 puts("Ramdisk image is corrupt or invalid\n");
1759                 return 1;
1760         }
1761
1762 #if defined(CONFIG_OF_LIBFDT)
1763         /* find flattened device tree */
1764         ret = boot_get_fdt(flag, argc, argv, images,
1765                            &images->ft_addr, &images->ft_len);
1766         if (ret) {
1767                 puts("Could not find a valid device tree\n");
1768                 return 1;
1769         }
1770
1771         set_working_fdt_addr(images->ft_addr);
1772 #endif
1773
1774         return 0;
1775 }
1776
1777 static int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1778 {
1779         bootm_headers_t images;
1780
1781         if (bootz_start(cmdtp, flag, argc, argv, &images))
1782                 return 1;
1783
1784         /*
1785          * We have reached the point of no return: we are going to
1786          * overwrite all exception vector code, so we cannot easily
1787          * recover from any failures any more...
1788          */
1789         disable_interrupts();
1790
1791 #ifdef CONFIG_NETCONSOLE
1792         /* Stop the ethernet stack if NetConsole could have left it up */
1793         eth_halt();
1794 #endif
1795
1796 #if defined(CONFIG_CMD_USB)
1797         /*
1798          * turn off USB to prevent the host controller from writing to the
1799          * SDRAM while Linux is booting. This could happen (at least for OHCI
1800          * controller), because the HCCA (Host Controller Communication Area)
1801          * lies within the SDRAM and the host controller writes continously to
1802          * this area (as busmaster!). The HccaFrameNumber is for example
1803          * updated every 1 ms within the HCCA structure in SDRAM! For more
1804          * details see the OpenHCI specification.
1805          */
1806         usb_stop();
1807 #endif
1808
1809 #ifdef CONFIG_SILENT_CONSOLE
1810         fixup_silent_linux();
1811 #endif
1812         arch_preboot_os();
1813
1814         do_bootm_linux(0, argc, argv, &images);
1815 #ifdef DEBUG
1816         puts("\n## Control returned to monitor - resetting...\n");
1817 #endif
1818         do_reset(cmdtp, flag, argc, argv);
1819
1820         return 1;
1821 }
1822
1823 #ifdef CONFIG_SYS_LONGHELP
1824 static char bootz_help_text[] =
1825         "[addr [initrd[:size]] [fdt]]\n"
1826         "    - boot Linux zImage stored in memory\n"
1827         "\tThe argument 'initrd' is optional and specifies the address\n"
1828         "\tof the initrd in memory. The optional argument ':size' allows\n"
1829         "\tspecifying the size of RAW initrd.\n"
1830 #if defined(CONFIG_OF_LIBFDT)
1831         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1832         "\ta third argument is required which is the address of the\n"
1833         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1834         "\tuse a '-' for the second argument. If you do not pass a third\n"
1835         "\ta bd_info struct will be passed instead\n"
1836 #endif
1837         "";
1838 #endif
1839
1840 U_BOOT_CMD(
1841         bootz,  CONFIG_SYS_MAXARGS,     1,      do_bootz,
1842         "boot Linux zImage image from memory", bootz_help_text
1843 );
1844 #endif  /* CONFIG_CMD_BOOTZ */