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