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