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