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