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