]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_bootm.c
Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'
[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 <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 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
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_BOOTM_PLAN9)
132 static boot_os_fn do_bootm_plan9;
133 #endif
134 #if defined(CONFIG_CMD_ELF)
135 static boot_os_fn do_bootm_vxworks;
136 static boot_os_fn do_bootm_qnxelf;
137 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
138 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
139 #endif
140 #if defined(CONFIG_INTEGRITY)
141 static boot_os_fn do_bootm_integrity;
142 #endif
143
144 static boot_os_fn *boot_os[] = {
145 #ifdef CONFIG_BOOTM_LINUX
146         [IH_OS_LINUX] = do_bootm_linux,
147 #endif
148 #ifdef CONFIG_BOOTM_NETBSD
149         [IH_OS_NETBSD] = do_bootm_netbsd,
150 #endif
151 #ifdef CONFIG_LYNXKDI
152         [IH_OS_LYNXOS] = do_bootm_lynxkdi,
153 #endif
154 #ifdef CONFIG_BOOTM_RTEMS
155         [IH_OS_RTEMS] = do_bootm_rtems,
156 #endif
157 #if defined(CONFIG_BOOTM_OSE)
158         [IH_OS_OSE] = do_bootm_ose,
159 #endif
160 #if defined(CONFIG_BOOTM_PLAN9)
161         [IH_OS_PLAN9] = do_bootm_plan9,
162 #endif
163 #if defined(CONFIG_CMD_ELF)
164         [IH_OS_VXWORKS] = do_bootm_vxworks,
165         [IH_OS_QNX] = do_bootm_qnxelf,
166 #endif
167 #ifdef CONFIG_INTEGRITY
168         [IH_OS_INTEGRITY] = do_bootm_integrity,
169 #endif
170 };
171
172 bootm_headers_t images;         /* pointers to os/initrd/fdt images */
173
174 /* Allow for arch specific config before we boot */
175 static void __arch_preboot_os(void)
176 {
177         /* please define platform specific arch_preboot_os() */
178 }
179 void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
180
181 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
182
183 #ifdef CONFIG_LMB
184 static void boot_start_lmb(bootm_headers_t *images)
185 {
186         ulong           mem_start;
187         phys_size_t     mem_size;
188
189         lmb_init(&images->lmb);
190
191         mem_start = getenv_bootm_low();
192         mem_size = getenv_bootm_size();
193
194         lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
195
196         arch_lmb_reserve(&images->lmb);
197         board_lmb_reserve(&images->lmb);
198 }
199 #else
200 #define lmb_reserve(lmb, base, size)
201 static inline void boot_start_lmb(bootm_headers_t *images) { }
202 #endif
203
204 static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
205 {
206         void            *os_hdr;
207         int             ret;
208
209         memset((void *)&images, 0, sizeof(images));
210         images.verify = getenv_yesno("verify");
211
212         boot_start_lmb(&images);
213
214         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
215
216         /* get kernel image header, start address and length */
217         os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
218                         &images, &images.os.image_start, &images.os.image_len);
219         if (images.os.image_len == 0) {
220                 puts("ERROR: can't get kernel image!\n");
221                 return 1;
222         }
223
224         /* get image parameters */
225         switch (genimg_get_format(os_hdr)) {
226         case IMAGE_FORMAT_LEGACY:
227                 images.os.type = image_get_type(os_hdr);
228                 images.os.comp = image_get_comp(os_hdr);
229                 images.os.os = image_get_os(os_hdr);
230
231                 images.os.end = image_get_image_end(os_hdr);
232                 images.os.load = image_get_load(os_hdr);
233                 break;
234 #if defined(CONFIG_FIT)
235         case IMAGE_FORMAT_FIT:
236                 if (fit_image_get_type(images.fit_hdr_os,
237                                         images.fit_noffset_os, &images.os.type)) {
238                         puts("Can't get image type!\n");
239                         bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
240                         return 1;
241                 }
242
243                 if (fit_image_get_comp(images.fit_hdr_os,
244                                         images.fit_noffset_os, &images.os.comp)) {
245                         puts("Can't get image compression!\n");
246                         bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
247                         return 1;
248                 }
249
250                 if (fit_image_get_os(images.fit_hdr_os,
251                                         images.fit_noffset_os, &images.os.os)) {
252                         puts("Can't get image OS!\n");
253                         bootstage_error(BOOTSTAGE_ID_FIT_OS);
254                         return 1;
255                 }
256
257                 images.os.end = fit_get_end(images.fit_hdr_os);
258
259                 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
260                                         &images.os.load)) {
261                         puts("Can't get image load address!\n");
262                         bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
263                         return 1;
264                 }
265                 break;
266 #endif
267         default:
268                 puts("ERROR: unknown image format type!\n");
269                 return 1;
270         }
271
272         /* find kernel entry point */
273         if (images.legacy_hdr_valid) {
274                 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
275 #if defined(CONFIG_FIT)
276         } else if (images.fit_uname_os) {
277                 ret = fit_image_get_entry(images.fit_hdr_os,
278                                 images.fit_noffset_os, &images.ep);
279                 if (ret) {
280                         puts("Can't get entry point property!\n");
281                         return 1;
282                 }
283 #endif
284         } else {
285                 puts("Could not find kernel entry point!\n");
286                 return 1;
287         }
288
289         if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
290                 images.os.load = images.os.image_start;
291                 images.ep += images.os.load;
292         }
293
294         if (((images.os.type == IH_TYPE_KERNEL) ||
295              (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
296              (images.os.type == IH_TYPE_MULTI)) &&
297             (images.os.os == IH_OS_LINUX)) {
298                 /* find ramdisk */
299                 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
300                                 &images.rd_start, &images.rd_end);
301                 if (ret) {
302                         puts("Ramdisk image is corrupt or invalid\n");
303                         return 1;
304                 }
305
306 #if defined(CONFIG_OF_LIBFDT)
307                 /* find flattened device tree */
308                 ret = boot_get_fdt(flag, argc, argv, &images,
309                                    &images.ft_addr, &images.ft_len);
310                 if (ret) {
311                         puts("Could not find a valid device tree\n");
312                         return 1;
313                 }
314
315                 set_working_fdt_addr(images.ft_addr);
316 #endif
317         }
318
319         images.os.start = (ulong)os_hdr;
320         images.state = BOOTM_STATE_START;
321
322         return 0;
323 }
324
325 #define BOOTM_ERR_RESET         -1
326 #define BOOTM_ERR_OVERLAP       -2
327 #define BOOTM_ERR_UNIMPLEMENTED -3
328 static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
329 {
330         uint8_t comp = os.comp;
331         ulong load = os.load;
332         ulong blob_start = os.start;
333         ulong blob_end = os.end;
334         ulong image_start = os.image_start;
335         ulong image_len = os.image_len;
336         __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
337         int no_overlap = 0;
338 #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
339         int ret;
340 #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
341
342         const char *type_name = genimg_get_type_name(os.type);
343
344         switch (comp) {
345         case IH_COMP_NONE:
346                 if (load == blob_start || load == image_start) {
347                         printf("   XIP %s ... ", type_name);
348                         no_overlap = 1;
349                 } else {
350                         printf("   Loading %s ... ", type_name);
351                         memmove_wd((void *)load, (void *)image_start,
352                                         image_len, CHUNKSZ);
353                 }
354                 *load_end = load + image_len;
355                 puts("OK\n");
356                 break;
357 #ifdef CONFIG_GZIP
358         case IH_COMP_GZIP:
359                 printf("   Uncompressing %s ... ", type_name);
360                 if (gunzip((void *)load, unc_len,
361                                 (uchar *)image_start, &image_len) != 0) {
362                         puts("GUNZIP: uncompress, out-of-mem or overwrite "
363                                 "error - must RESET board to recover\n");
364                         if (boot_progress)
365                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
366                         return BOOTM_ERR_RESET;
367                 }
368
369                 *load_end = load + image_len;
370                 break;
371 #endif /* CONFIG_GZIP */
372 #ifdef CONFIG_BZIP2
373         case IH_COMP_BZIP2:
374                 printf("   Uncompressing %s ... ", type_name);
375                 /*
376                  * If we've got less than 4 MB of malloc() space,
377                  * use slower decompression algorithm which requires
378                  * at most 2300 KB of memory.
379                  */
380                 int i = BZ2_bzBuffToBuffDecompress((char *)load,
381                                         &unc_len, (char *)image_start, image_len,
382                                         CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
383                 if (i != BZ_OK) {
384                         printf("BUNZIP2: uncompress or overwrite error %d "
385                                 "- must RESET board to recover\n", i);
386                         if (boot_progress)
387                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
388                         return BOOTM_ERR_RESET;
389                 }
390
391                 *load_end = load + unc_len;
392                 break;
393 #endif /* CONFIG_BZIP2 */
394 #ifdef CONFIG_LZMA
395         case IH_COMP_LZMA: {
396                 SizeT lzma_len = unc_len;
397                 printf("   Uncompressing %s ... ", type_name);
398
399                 ret = lzmaBuffToBuffDecompress(
400                         (unsigned char *)load, &lzma_len,
401                         (unsigned char *)image_start, image_len);
402                 unc_len = lzma_len;
403                 if (ret != SZ_OK) {
404                         printf("LZMA: uncompress or overwrite error %d "
405                                 "- must RESET board to recover\n", ret);
406                         bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
407                         return BOOTM_ERR_RESET;
408                 }
409                 *load_end = load + unc_len;
410                 break;
411         }
412 #endif /* CONFIG_LZMA */
413 #ifdef CONFIG_LZO
414         case IH_COMP_LZO:
415                 printf("   Uncompressing %s ... ", type_name);
416
417                 ret = lzop_decompress((const unsigned char *)image_start,
418                                           image_len, (unsigned char *)load,
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_check_hashes(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 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 #if defined(CONFIG_FIT)
865         void            *fit_hdr;
866         const char      *fit_uname_config = NULL;
867         const char      *fit_uname_kernel = NULL;
868         const void      *data;
869         size_t          len;
870         int             cfg_noffset;
871         int             os_noffset;
872 #endif
873
874         /* find out kernel image address */
875         if (argc < 2) {
876                 img_addr = load_addr;
877                 debug("*  kernel: default image load address = 0x%08lx\n",
878                                 load_addr);
879 #if defined(CONFIG_FIT)
880         } else if (fit_parse_conf(argv[1], load_addr, &img_addr,
881                                                         &fit_uname_config)) {
882                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
883                                 fit_uname_config, img_addr);
884         } else if (fit_parse_subimage(argv[1], load_addr, &img_addr,
885                                                         &fit_uname_kernel)) {
886                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
887                                 fit_uname_kernel, img_addr);
888 #endif
889         } else {
890                 img_addr = simple_strtoul(argv[1], NULL, 16);
891                 debug("*  kernel: cmdline image address = 0x%08lx\n", img_addr);
892         }
893
894         bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
895
896         /* copy from dataflash if needed */
897         img_addr = genimg_get_image(img_addr);
898
899         /* check image type, for FIT images get FIT kernel node */
900         *os_data = *os_len = 0;
901         switch (genimg_get_format((void *)img_addr)) {
902         case IMAGE_FORMAT_LEGACY:
903                 printf("## Booting kernel from Legacy Image at %08lx ...\n",
904                                 img_addr);
905                 hdr = image_get_kernel(img_addr, images->verify);
906                 if (!hdr)
907                         return NULL;
908                 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
909
910                 /* get os_data and os_len */
911                 switch (image_get_type(hdr)) {
912                 case IH_TYPE_KERNEL:
913                 case IH_TYPE_KERNEL_NOLOAD:
914                         *os_data = image_get_data(hdr);
915                         *os_len = image_get_data_size(hdr);
916                         break;
917                 case IH_TYPE_MULTI:
918                         image_multi_getimg(hdr, 0, os_data, os_len);
919                         break;
920                 case IH_TYPE_STANDALONE:
921                         *os_data = image_get_data(hdr);
922                         *os_len = image_get_data_size(hdr);
923                         break;
924                 default:
925                         printf("Wrong Image Type for %s command\n",
926                                 cmdtp->name);
927                         bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
928                         return NULL;
929                 }
930
931                 /*
932                  * copy image header to allow for image overwrites during
933                  * kernel decompression.
934                  */
935                 memmove(&images->legacy_hdr_os_copy, hdr,
936                         sizeof(image_header_t));
937
938                 /* save pointer to image header */
939                 images->legacy_hdr_os = hdr;
940
941                 images->legacy_hdr_valid = 1;
942                 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
943                 break;
944 #if defined(CONFIG_FIT)
945         case IMAGE_FORMAT_FIT:
946                 fit_hdr = (void *)img_addr;
947                 printf("## Booting kernel from FIT Image at %08lx ...\n",
948                                 img_addr);
949
950                 if (!fit_check_format(fit_hdr)) {
951                         puts("Bad FIT kernel image format!\n");
952                         bootstage_error(BOOTSTAGE_ID_FIT_FORMAT);
953                         return NULL;
954                 }
955                 bootstage_mark(BOOTSTAGE_ID_FIT_FORMAT);
956
957                 if (!fit_uname_kernel) {
958                         /*
959                          * no kernel image node unit name, try to get config
960                          * node first. If config unit node name is NULL
961                          * fit_conf_get_node() will try to find default config
962                          * node
963                          */
964                         bootstage_mark(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
965 #ifdef CONFIG_FIT_BEST_MATCH
966                         if (fit_uname_config)
967                                 cfg_noffset =
968                                         fit_conf_get_node(fit_hdr,
969                                                           fit_uname_config);
970                         else
971                                 cfg_noffset =
972                                         fit_conf_find_compat(fit_hdr,
973                                                              gd->fdt_blob);
974 #else
975                         cfg_noffset = fit_conf_get_node(fit_hdr,
976                                                         fit_uname_config);
977 #endif
978                         if (cfg_noffset < 0) {
979                                 bootstage_error(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
980                                 return NULL;
981                         }
982                         /* save configuration uname provided in the first
983                          * bootm argument
984                          */
985                         images->fit_uname_cfg = fdt_get_name(fit_hdr,
986                                                                 cfg_noffset,
987                                                                 NULL);
988                         printf("   Using '%s' configuration\n",
989                                 images->fit_uname_cfg);
990                         bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
991
992                         os_noffset = fit_conf_get_kernel_node(fit_hdr,
993                                                                 cfg_noffset);
994                         fit_uname_kernel = fit_get_name(fit_hdr, os_noffset,
995                                                         NULL);
996                 } else {
997                         /* get kernel component image node offset */
998                         bootstage_mark(BOOTSTAGE_ID_FIT_UNIT_NAME);
999                         os_noffset = fit_image_get_node(fit_hdr,
1000                                                         fit_uname_kernel);
1001                 }
1002                 if (os_noffset < 0) {
1003                         bootstage_error(BOOTSTAGE_ID_FIT_CONFIG);
1004                         return NULL;
1005                 }
1006
1007                 printf("   Trying '%s' kernel subimage\n", fit_uname_kernel);
1008
1009                 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_SUBIMAGE);
1010                 if (!fit_check_kernel(fit_hdr, os_noffset, images->verify))
1011                         return NULL;
1012
1013                 /* get kernel image data address and length */
1014                 if (fit_image_get_data(fit_hdr, os_noffset, &data, &len)) {
1015                         puts("Could not find kernel subimage data!\n");
1016                         bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO_ERR);
1017                         return NULL;
1018                 }
1019                 bootstage_mark(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1020
1021                 *os_len = len;
1022                 *os_data = (ulong)data;
1023                 images->fit_hdr_os = fit_hdr;
1024                 images->fit_uname_os = fit_uname_kernel;
1025                 images->fit_noffset_os = os_noffset;
1026                 break;
1027 #endif
1028         default:
1029                 printf("Wrong Image Format for %s command\n", cmdtp->name);
1030                 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1031                 return NULL;
1032         }
1033
1034         debug("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1035                         *os_data, *os_len, *os_len);
1036
1037         return (void *)img_addr;
1038 }
1039
1040 #ifdef CONFIG_SYS_LONGHELP
1041 static char bootm_help_text[] =
1042         "[addr [arg ...]]\n    - boot application image stored in memory\n"
1043         "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1044         "\t'arg' can be the address of an initrd image\n"
1045 #if defined(CONFIG_OF_LIBFDT)
1046         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1047         "\ta third argument is required which is the address of the\n"
1048         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1049         "\tuse a '-' for the second argument. If you do not pass a third\n"
1050         "\ta bd_info struct will be passed instead\n"
1051 #endif
1052 #if defined(CONFIG_FIT)
1053         "\t\nFor the new multi component uImage format (FIT) addresses\n"
1054         "\tmust be extened to include component or configuration unit name:\n"
1055         "\taddr:<subimg_uname> - direct component image specification\n"
1056         "\taddr#<conf_uname>   - configuration specification\n"
1057         "\tUse iminfo command to get the list of existing component\n"
1058         "\timages and configurations.\n"
1059 #endif
1060         "\nSub-commands to do part of the bootm sequence.  The sub-commands "
1061         "must be\n"
1062         "issued in the order below (it's ok to not issue all sub-commands):\n"
1063         "\tstart [addr [arg ...]]\n"
1064         "\tloados  - load OS image\n"
1065 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
1066         "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1067 #endif
1068 #if defined(CONFIG_OF_LIBFDT)
1069         "\tfdt     - relocate flat device tree\n"
1070 #endif
1071         "\tcmdline - OS specific command line processing/setup\n"
1072         "\tbdt     - OS specific bd_t processing\n"
1073         "\tprep    - OS specific prep before relocation or go\n"
1074         "\tgo      - start OS";
1075 #endif
1076
1077 U_BOOT_CMD(
1078         bootm,  CONFIG_SYS_MAXARGS,     1,      do_bootm,
1079         "boot application image from memory", bootm_help_text
1080 );
1081
1082 /*******************************************************************/
1083 /* bootd - boot default image */
1084 /*******************************************************************/
1085 #if defined(CONFIG_CMD_BOOTD)
1086 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1087 {
1088         int rcode = 0;
1089
1090         if (run_command(getenv("bootcmd"), flag) < 0)
1091                 rcode = 1;
1092         return rcode;
1093 }
1094
1095 U_BOOT_CMD(
1096         boot,   1,      1,      do_bootd,
1097         "boot default, i.e., run 'bootcmd'",
1098         ""
1099 );
1100
1101 /* keep old command name "bootd" for backward compatibility */
1102 U_BOOT_CMD(
1103         bootd, 1,       1,      do_bootd,
1104         "boot default, i.e., run 'bootcmd'",
1105         ""
1106 );
1107
1108 #endif
1109
1110
1111 /*******************************************************************/
1112 /* iminfo - print header info for a requested image */
1113 /*******************************************************************/
1114 #if defined(CONFIG_CMD_IMI)
1115 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1116 {
1117         int     arg;
1118         ulong   addr;
1119         int     rcode = 0;
1120
1121         if (argc < 2) {
1122                 return image_info(load_addr);
1123         }
1124
1125         for (arg = 1; arg < argc; ++arg) {
1126                 addr = simple_strtoul(argv[arg], NULL, 16);
1127                 if (image_info(addr) != 0)
1128                         rcode = 1;
1129         }
1130         return rcode;
1131 }
1132
1133 static int image_info(ulong addr)
1134 {
1135         void *hdr = (void *)addr;
1136
1137         printf("\n## Checking Image at %08lx ...\n", addr);
1138
1139         switch (genimg_get_format(hdr)) {
1140         case IMAGE_FORMAT_LEGACY:
1141                 puts("   Legacy image found\n");
1142                 if (!image_check_magic(hdr)) {
1143                         puts("   Bad Magic Number\n");
1144                         return 1;
1145                 }
1146
1147                 if (!image_check_hcrc(hdr)) {
1148                         puts("   Bad Header Checksum\n");
1149                         return 1;
1150                 }
1151
1152                 image_print_contents(hdr);
1153
1154                 puts("   Verifying Checksum ... ");
1155                 if (!image_check_dcrc(hdr)) {
1156                         puts("   Bad Data CRC\n");
1157                         return 1;
1158                 }
1159                 puts("OK\n");
1160                 return 0;
1161 #if defined(CONFIG_FIT)
1162         case IMAGE_FORMAT_FIT:
1163                 puts("   FIT image found\n");
1164
1165                 if (!fit_check_format(hdr)) {
1166                         puts("Bad FIT image format!\n");
1167                         return 1;
1168                 }
1169
1170                 fit_print_contents(hdr);
1171
1172                 if (!fit_all_image_check_hashes(hdr)) {
1173                         puts("Bad hash in FIT image!\n");
1174                         return 1;
1175                 }
1176
1177                 return 0;
1178 #endif
1179         default:
1180                 puts("Unknown image format!\n");
1181                 break;
1182         }
1183
1184         return 1;
1185 }
1186
1187 U_BOOT_CMD(
1188         iminfo, CONFIG_SYS_MAXARGS,     1,      do_iminfo,
1189         "print header information for application image",
1190         "addr [addr ...]\n"
1191         "    - print header information for application image starting at\n"
1192         "      address 'addr' in memory; this includes verification of the\n"
1193         "      image contents (magic number, header and payload checksums)"
1194 );
1195 #endif
1196
1197
1198 /*******************************************************************/
1199 /* imls - list all images found in flash */
1200 /*******************************************************************/
1201 #if defined(CONFIG_CMD_IMLS)
1202 static int do_imls_nor(void)
1203 {
1204         flash_info_t *info;
1205         int i, j;
1206         void *hdr;
1207
1208         for (i = 0, info = &flash_info[0];
1209                 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
1210
1211                 if (info->flash_id == FLASH_UNKNOWN)
1212                         goto next_bank;
1213                 for (j = 0; j < info->sector_count; ++j) {
1214
1215                         hdr = (void *)info->start[j];
1216                         if (!hdr)
1217                                 goto next_sector;
1218
1219                         switch (genimg_get_format(hdr)) {
1220                         case IMAGE_FORMAT_LEGACY:
1221                                 if (!image_check_hcrc(hdr))
1222                                         goto next_sector;
1223
1224                                 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1225                                 image_print_contents(hdr);
1226
1227                                 puts("   Verifying Checksum ... ");
1228                                 if (!image_check_dcrc(hdr)) {
1229                                         puts("Bad Data CRC\n");
1230                                 } else {
1231                                         puts("OK\n");
1232                                 }
1233                                 break;
1234 #if defined(CONFIG_FIT)
1235                         case IMAGE_FORMAT_FIT:
1236                                 if (!fit_check_format(hdr))
1237                                         goto next_sector;
1238
1239                                 printf("FIT Image at %08lX:\n", (ulong)hdr);
1240                                 fit_print_contents(hdr);
1241                                 break;
1242 #endif
1243                         default:
1244                                 goto next_sector;
1245                         }
1246
1247 next_sector:            ;
1248                 }
1249 next_bank:      ;
1250         }
1251         return 0;
1252 }
1253 #endif
1254
1255 #if defined(CONFIG_CMD_IMLS_NAND)
1256 static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
1257                 size_t len)
1258 {
1259         void *imgdata;
1260         int ret;
1261
1262         imgdata = malloc(len);
1263         if (!imgdata) {
1264                 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
1265                                 nand_dev, off);
1266                 printf("   Low memory(cannot allocate memory for image)\n");
1267                 return -ENOMEM;
1268         }
1269
1270         ret = nand_read_skip_bad(nand, off, &len,
1271                         imgdata);
1272         if (ret < 0 && ret != -EUCLEAN) {
1273                 free(imgdata);
1274                 return ret;
1275         }
1276
1277         if (!image_check_hcrc(imgdata)) {
1278                 free(imgdata);
1279                 return 0;
1280         }
1281
1282         printf("Legacy Image at NAND device %d offset %08llX:\n",
1283                         nand_dev, off);
1284         image_print_contents(imgdata);
1285
1286         puts("   Verifying Checksum ... ");
1287         if (!image_check_dcrc(imgdata))
1288                 puts("Bad Data CRC\n");
1289         else
1290                 puts("OK\n");
1291
1292         free(imgdata);
1293
1294         return 0;
1295 }
1296
1297 static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
1298                 size_t len)
1299 {
1300         void *imgdata;
1301         int ret;
1302
1303         imgdata = malloc(len);
1304         if (!imgdata) {
1305                 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
1306                                 nand_dev, off);
1307                 printf("   Low memory(cannot allocate memory for image)\n");
1308                 return -ENOMEM;
1309         }
1310
1311         ret = nand_read_skip_bad(nand, off, &len,
1312                         imgdata);
1313         if (ret < 0 && ret != -EUCLEAN) {
1314                 free(imgdata);
1315                 return ret;
1316         }
1317
1318         if (!fit_check_format(imgdata)) {
1319                 free(imgdata);
1320                 return 0;
1321         }
1322
1323         printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
1324
1325         fit_print_contents(imgdata);
1326         free(imgdata);
1327
1328         return 0;
1329 }
1330
1331 static int do_imls_nand(void)
1332 {
1333         nand_info_t *nand;
1334         int nand_dev = nand_curr_device;
1335         size_t len;
1336         loff_t off;
1337         u32 buffer[16];
1338
1339         if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
1340                 puts("\nNo NAND devices available\n");
1341                 return -ENODEV;
1342         }
1343
1344         printf("\n");
1345
1346         for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
1347                 nand = &nand_info[nand_dev];
1348                 if (!nand->name || !nand->size)
1349                         continue;
1350
1351                 for (off = 0; off < nand->size; off += nand->erasesize) {
1352                         const image_header_t *header;
1353                         int ret;
1354
1355                         if (nand_block_isbad(nand, off))
1356                                 continue;
1357
1358                         len = sizeof(buffer);
1359
1360                         ret = nand_read(nand, off, &len, (u8 *)buffer);
1361                         if (ret < 0 && ret != -EUCLEAN) {
1362                                 printf("NAND read error %d at offset %08llX\n",
1363                                                 ret, off);
1364                                 continue;
1365                         }
1366
1367                         switch (genimg_get_format(buffer)) {
1368                         case IMAGE_FORMAT_LEGACY:
1369                                 header = (const image_header_t *)buffer;
1370
1371                                 len = image_get_image_size(header);
1372                                 nand_imls_legacyimage(nand, nand_dev, off, len);
1373                                 break;
1374 #if defined(CONFIG_FIT)
1375                         case IMAGE_FORMAT_FIT:
1376                                 len = fit_get_size(buffer);
1377                                 nand_imls_fitimage(nand, nand_dev, off, len);
1378                                 break;
1379 #endif
1380                         }
1381                 }
1382         }
1383
1384         return 0;
1385 }
1386 #endif
1387
1388 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
1389 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1390 {
1391         int ret_nor = 0, ret_nand = 0;
1392
1393 #if defined(CONFIG_CMD_IMLS)
1394         ret_nor = do_imls_nor();
1395 #endif
1396
1397 #if defined(CONFIG_CMD_IMLS_NAND)
1398         ret_nand = do_imls_nand();
1399 #endif
1400
1401         if (ret_nor)
1402                 return ret_nor;
1403
1404         if (ret_nand)
1405                 return ret_nand;
1406
1407         return (0);
1408 }
1409
1410 U_BOOT_CMD(
1411         imls,   1,              1,      do_imls,
1412         "list all images found in flash",
1413         "\n"
1414         "    - Prints information about all images found at sector/block\n"
1415         "      boundaries in nor/nand flash."
1416 );
1417 #endif
1418
1419 /*******************************************************************/
1420 /* helper routines */
1421 /*******************************************************************/
1422 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
1423 static void fixup_silent_linux(void)
1424 {
1425         char buf[256], *start, *end;
1426         char *cmdline = getenv("bootargs");
1427
1428         /* Only fix cmdline when requested */
1429         if (!(gd->flags & GD_FLG_SILENT))
1430                 return;
1431
1432         debug("before silent fix-up: %s\n", cmdline);
1433         if (cmdline) {
1434                 start = strstr(cmdline, "console=");
1435                 if (start) {
1436                         end = strchr(start, ' ');
1437                         strncpy(buf, cmdline, (start - cmdline + 8));
1438                         if (end)
1439                                 strcpy(buf + (start - cmdline + 8), end);
1440                         else
1441                                 buf[start - cmdline + 8] = '\0';
1442                 } else {
1443                         strcpy(buf, cmdline);
1444                         strcat(buf, " console=");
1445                 }
1446         } else {
1447                 strcpy(buf, "console=");
1448         }
1449
1450         setenv("bootargs", buf);
1451         debug("after silent fix-up: %s\n", buf);
1452 }
1453 #endif /* CONFIG_SILENT_CONSOLE */
1454
1455
1456 /*******************************************************************/
1457 /* OS booting routines */
1458 /*******************************************************************/
1459
1460 #ifdef CONFIG_BOOTM_NETBSD
1461 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
1462                             bootm_headers_t *images)
1463 {
1464         void (*loader)(bd_t *, image_header_t *, char *, char *);
1465         image_header_t *os_hdr, *hdr;
1466         ulong kernel_data, kernel_len;
1467         char *consdev;
1468         char *cmdline;
1469
1470         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1471                 return 1;
1472
1473 #if defined(CONFIG_FIT)
1474         if (!images->legacy_hdr_valid) {
1475                 fit_unsupported_reset("NetBSD");
1476                 return 1;
1477         }
1478 #endif
1479         hdr = images->legacy_hdr_os;
1480
1481         /*
1482          * Booting a (NetBSD) kernel image
1483          *
1484          * This process is pretty similar to a standalone application:
1485          * The (first part of an multi-) image must be a stage-2 loader,
1486          * which in turn is responsible for loading & invoking the actual
1487          * kernel.  The only differences are the parameters being passed:
1488          * besides the board info strucure, the loader expects a command
1489          * line, the name of the console device, and (optionally) the
1490          * address of the original image header.
1491          */
1492         os_hdr = NULL;
1493         if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1494                 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
1495                 if (kernel_len)
1496                         os_hdr = hdr;
1497         }
1498
1499         consdev = "";
1500 #if   defined(CONFIG_8xx_CONS_SMC1)
1501         consdev = "smc1";
1502 #elif defined(CONFIG_8xx_CONS_SMC2)
1503         consdev = "smc2";
1504 #elif defined(CONFIG_8xx_CONS_SCC2)
1505         consdev = "scc2";
1506 #elif defined(CONFIG_8xx_CONS_SCC3)
1507         consdev = "scc3";
1508 #endif
1509
1510         if (argc > 2) {
1511                 ulong len;
1512                 int   i;
1513
1514                 for (i = 2, len = 0; i < argc; i += 1)
1515                         len += strlen(argv[i]) + 1;
1516                 cmdline = malloc(len);
1517
1518                 for (i = 2, len = 0; i < argc; i += 1) {
1519                         if (i > 2)
1520                                 cmdline[len++] = ' ';
1521                         strcpy(&cmdline[len], argv[i]);
1522                         len += strlen(argv[i]);
1523                 }
1524         } else if ((cmdline = getenv("bootargs")) == NULL) {
1525                 cmdline = "";
1526         }
1527
1528         loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
1529
1530         printf("## Transferring control to NetBSD stage-2 loader "
1531                 "(at address %08lx) ...\n",
1532                 (ulong)loader);
1533
1534         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1535
1536         /*
1537          * NetBSD Stage-2 Loader Parameters:
1538          *   r3: ptr to board info data
1539          *   r4: image address
1540          *   r5: console device
1541          *   r6: boot args string
1542          */
1543         (*loader)(gd->bd, os_hdr, consdev, cmdline);
1544
1545         return 1;
1546 }
1547 #endif /* CONFIG_BOOTM_NETBSD*/
1548
1549 #ifdef CONFIG_LYNXKDI
1550 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
1551                              bootm_headers_t *images)
1552 {
1553         image_header_t *hdr = &images->legacy_hdr_os_copy;
1554
1555         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1556                 return 1;
1557
1558 #if defined(CONFIG_FIT)
1559         if (!images->legacy_hdr_valid) {
1560                 fit_unsupported_reset("Lynx");
1561                 return 1;
1562         }
1563 #endif
1564
1565         lynxkdi_boot((image_header_t *)hdr);
1566
1567         return 1;
1568 }
1569 #endif /* CONFIG_LYNXKDI */
1570
1571 #ifdef CONFIG_BOOTM_RTEMS
1572 static int do_bootm_rtems(int flag, int argc, char * const argv[],
1573                            bootm_headers_t *images)
1574 {
1575         void (*entry_point)(bd_t *);
1576
1577         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1578                 return 1;
1579
1580 #if defined(CONFIG_FIT)
1581         if (!images->legacy_hdr_valid) {
1582                 fit_unsupported_reset("RTEMS");
1583                 return 1;
1584         }
1585 #endif
1586
1587         entry_point = (void (*)(bd_t *))images->ep;
1588
1589         printf("## Transferring control to RTEMS (at address %08lx) ...\n",
1590                 (ulong)entry_point);
1591
1592         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1593
1594         /*
1595          * RTEMS Parameters:
1596          *   r3: ptr to board info data
1597          */
1598         (*entry_point)(gd->bd);
1599
1600         return 1;
1601 }
1602 #endif /* CONFIG_BOOTM_RTEMS */
1603
1604 #if defined(CONFIG_BOOTM_OSE)
1605 static int do_bootm_ose(int flag, int argc, char * const argv[],
1606                            bootm_headers_t *images)
1607 {
1608         void (*entry_point)(void);
1609
1610         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1611                 return 1;
1612
1613 #if defined(CONFIG_FIT)
1614         if (!images->legacy_hdr_valid) {
1615                 fit_unsupported_reset("OSE");
1616                 return 1;
1617         }
1618 #endif
1619
1620         entry_point = (void (*)(void))images->ep;
1621
1622         printf("## Transferring control to OSE (at address %08lx) ...\n",
1623                 (ulong)entry_point);
1624
1625         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1626
1627         /*
1628          * OSE Parameters:
1629          *   None
1630          */
1631         (*entry_point)();
1632
1633         return 1;
1634 }
1635 #endif /* CONFIG_BOOTM_OSE */
1636
1637 #if defined(CONFIG_BOOTM_PLAN9)
1638 static int do_bootm_plan9(int flag, int argc, char * const argv[],
1639                            bootm_headers_t *images)
1640 {
1641         void (*entry_point)(void);
1642
1643         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1644                 return 1;
1645
1646 #if defined(CONFIG_FIT)
1647         if (!images->legacy_hdr_valid) {
1648                 fit_unsupported_reset("Plan 9");
1649                 return 1;
1650         }
1651 #endif
1652
1653         entry_point = (void (*)(void))images->ep;
1654
1655         printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
1656                 (ulong)entry_point);
1657
1658         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1659
1660         /*
1661          * Plan 9 Parameters:
1662          *   None
1663          */
1664         (*entry_point)();
1665
1666         return 1;
1667 }
1668 #endif /* CONFIG_BOOTM_PLAN9 */
1669
1670 #if defined(CONFIG_CMD_ELF)
1671 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
1672                              bootm_headers_t *images)
1673 {
1674         char str[80];
1675
1676         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1677                 return 1;
1678
1679 #if defined(CONFIG_FIT)
1680         if (!images->legacy_hdr_valid) {
1681                 fit_unsupported_reset("VxWorks");
1682                 return 1;
1683         }
1684 #endif
1685
1686         sprintf(str, "%lx", images->ep); /* write entry-point into string */
1687         setenv("loadaddr", str);
1688         do_bootvx(NULL, 0, 0, NULL);
1689
1690         return 1;
1691 }
1692
1693 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
1694                             bootm_headers_t *images)
1695 {
1696         char *local_args[2];
1697         char str[16];
1698
1699         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1700                 return 1;
1701
1702 #if defined(CONFIG_FIT)
1703         if (!images->legacy_hdr_valid) {
1704                 fit_unsupported_reset("QNX");
1705                 return 1;
1706         }
1707 #endif
1708
1709         sprintf(str, "%lx", images->ep); /* write entry-point into string */
1710         local_args[0] = argv[0];
1711         local_args[1] = str;    /* and provide it via the arguments */
1712         do_bootelf(NULL, 0, 2, local_args);
1713
1714         return 1;
1715 }
1716 #endif
1717
1718 #ifdef CONFIG_INTEGRITY
1719 static int do_bootm_integrity(int flag, int argc, char * const argv[],
1720                            bootm_headers_t *images)
1721 {
1722         void (*entry_point)(void);
1723
1724         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1725                 return 1;
1726
1727 #if defined(CONFIG_FIT)
1728         if (!images->legacy_hdr_valid) {
1729                 fit_unsupported_reset("INTEGRITY");
1730                 return 1;
1731         }
1732 #endif
1733
1734         entry_point = (void (*)(void))images->ep;
1735
1736         printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
1737                 (ulong)entry_point);
1738
1739         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1740
1741         /*
1742          * INTEGRITY Parameters:
1743          *   None
1744          */
1745         (*entry_point)();
1746
1747         return 1;
1748 }
1749 #endif
1750
1751 #ifdef CONFIG_CMD_BOOTZ
1752
1753 static int __bootz_setup(void *image, void **start, void **end)
1754 {
1755         /* Please define bootz_setup() for your platform */
1756
1757         puts("Your platform's zImage format isn't supported yet!\n");
1758         return -1;
1759 }
1760 int bootz_setup(void *image, void **start, void **end)
1761         __attribute__((weak, alias("__bootz_setup")));
1762
1763 /*
1764  * zImage booting support
1765  */
1766 static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1767                         char * const argv[], bootm_headers_t *images)
1768 {
1769         int ret;
1770         void *zi_start, *zi_end;
1771
1772         memset(images, 0, sizeof(bootm_headers_t));
1773
1774         boot_start_lmb(images);
1775
1776         /* Setup Linux kernel zImage entry point */
1777         if (argc < 2) {
1778                 images->ep = load_addr;
1779                 debug("*  kernel: default image load address = 0x%08lx\n",
1780                                 load_addr);
1781         } else {
1782                 images->ep = simple_strtoul(argv[1], NULL, 16);
1783                 debug("*  kernel: cmdline image address = 0x%08lx\n",
1784                         images->ep);
1785         }
1786
1787         ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
1788         if (ret != 0)
1789                 return 1;
1790
1791         lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1792
1793         /* Find ramdisk */
1794         ret = boot_get_ramdisk(argc, argv, images, IH_INITRD_ARCH,
1795                         &images->rd_start, &images->rd_end);
1796         if (ret) {
1797                 puts("Ramdisk image is corrupt or invalid\n");
1798                 return 1;
1799         }
1800
1801 #if defined(CONFIG_OF_LIBFDT)
1802         /* find flattened device tree */
1803         ret = boot_get_fdt(flag, argc, argv, images,
1804                            &images->ft_addr, &images->ft_len);
1805         if (ret) {
1806                 puts("Could not find a valid device tree\n");
1807                 return 1;
1808         }
1809
1810         set_working_fdt_addr(images->ft_addr);
1811 #endif
1812
1813         return 0;
1814 }
1815
1816 static int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1817 {
1818         bootm_headers_t images;
1819
1820         if (bootz_start(cmdtp, flag, argc, argv, &images))
1821                 return 1;
1822
1823         /*
1824          * We have reached the point of no return: we are going to
1825          * overwrite all exception vector code, so we cannot easily
1826          * recover from any failures any more...
1827          */
1828         disable_interrupts();
1829
1830 #ifdef CONFIG_NETCONSOLE
1831         /* Stop the ethernet stack if NetConsole could have left it up */
1832         eth_halt();
1833 #endif
1834
1835 #if defined(CONFIG_CMD_USB)
1836         /*
1837          * turn off USB to prevent the host controller from writing to the
1838          * SDRAM while Linux is booting. This could happen (at least for OHCI
1839          * controller), because the HCCA (Host Controller Communication Area)
1840          * lies within the SDRAM and the host controller writes continously to
1841          * this area (as busmaster!). The HccaFrameNumber is for example
1842          * updated every 1 ms within the HCCA structure in SDRAM! For more
1843          * details see the OpenHCI specification.
1844          */
1845         usb_stop();
1846 #endif
1847
1848 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
1849         fixup_silent_linux();
1850 #endif
1851         arch_preboot_os();
1852
1853         do_bootm_linux(0, argc, argv, &images);
1854 #ifdef DEBUG
1855         puts("\n## Control returned to monitor - resetting...\n");
1856 #endif
1857         do_reset(cmdtp, flag, argc, argv);
1858
1859         return 1;
1860 }
1861
1862 #ifdef CONFIG_SYS_LONGHELP
1863 static char bootz_help_text[] =
1864         "[addr [initrd[:size]] [fdt]]\n"
1865         "    - boot Linux zImage stored in memory\n"
1866         "\tThe argument 'initrd' is optional and specifies the address\n"
1867         "\tof the initrd in memory. The optional argument ':size' allows\n"
1868         "\tspecifying the size of RAW initrd.\n"
1869 #if defined(CONFIG_OF_LIBFDT)
1870         "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1871         "\ta third argument is required which is the address of the\n"
1872         "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1873         "\tuse a '-' for the second argument. If you do not pass a third\n"
1874         "\ta bd_info struct will be passed instead\n"
1875 #endif
1876         "";
1877 #endif
1878
1879 U_BOOT_CMD(
1880         bootz,  CONFIG_SYS_MAXARGS,     1,      do_bootz,
1881         "boot Linux zImage image from memory", bootz_help_text
1882 );
1883 #endif  /* CONFIG_CMD_BOOTZ */