2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 #include <u-boot/zlib.h>
35 #include <environment.h>
37 #include <linux/ctype.h>
38 #include <asm/byteorder.h>
39 #include <linux/compiler.h>
41 #if defined(CONFIG_CMD_USB)
45 #ifdef CONFIG_SYS_HUSH_PARSER
49 #if defined(CONFIG_OF_LIBFDT)
52 #include <fdt_support.h>
56 #include <lzma/LzmaTypes.h>
57 #include <lzma/LzmaDec.h>
58 #include <lzma/LzmaTools.h>
59 #endif /* CONFIG_LZMA */
62 #include <linux/lzo.h>
63 #endif /* CONFIG_LZO */
65 DECLARE_GLOBAL_DATA_PTR;
67 #ifndef CONFIG_SYS_BOOTM_LEN
68 #define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
72 extern void bz_internal_error(int);
75 #if defined(CONFIG_CMD_IMI)
76 static int image_info(unsigned long addr);
79 #if defined(CONFIG_CMD_IMLS)
81 #include <mtd/cfi_flash.h>
82 extern flash_info_t flash_info[]; /* info for FLASH chips */
83 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
86 #ifdef CONFIG_SILENT_CONSOLE
87 static void fixup_silent_linux(void);
90 static image_header_t *image_get_kernel(ulong img_addr, int verify);
91 #if defined(CONFIG_FIT)
92 static int fit_check_kernel(const void *fit, int os_noffset, int verify);
95 static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
96 char * const argv[], bootm_headers_t *images,
97 ulong *os_data, ulong *os_len);
100 * Continue booting an OS image; caller already has:
101 * - copied image header to global variable `header'
102 * - checked header magic number, checksums (both header & image),
103 * - verified image architecture (PPC) and type (KERNEL or MULTI),
104 * - loaded (first part of) image to header load address,
105 * - disabled interrupts.
107 typedef int boot_os_fn(int flag, int argc, char * const argv[],
108 bootm_headers_t *images); /* pointers to os/initrd/fdt */
110 #ifdef CONFIG_BOOTM_LINUX
111 extern boot_os_fn do_bootm_linux;
113 #ifdef CONFIG_BOOTM_NETBSD
114 static boot_os_fn do_bootm_netbsd;
116 #if defined(CONFIG_LYNXKDI)
117 static boot_os_fn do_bootm_lynxkdi;
118 extern void lynxkdi_boot(image_header_t *);
120 #ifdef CONFIG_BOOTM_RTEMS
121 static boot_os_fn do_bootm_rtems;
123 #if defined(CONFIG_BOOTM_OSE)
124 static boot_os_fn do_bootm_ose;
126 #if defined(CONFIG_CMD_ELF)
127 static boot_os_fn do_bootm_vxworks;
128 static boot_os_fn do_bootm_qnxelf;
129 int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
130 int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
132 #if defined(CONFIG_INTEGRITY)
133 static boot_os_fn do_bootm_integrity;
136 static boot_os_fn *boot_os[] = {
137 #ifdef CONFIG_BOOTM_LINUX
138 [IH_OS_LINUX] = do_bootm_linux,
140 #ifdef CONFIG_BOOTM_NETBSD
141 [IH_OS_NETBSD] = do_bootm_netbsd,
143 #ifdef CONFIG_LYNXKDI
144 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
146 #ifdef CONFIG_BOOTM_RTEMS
147 [IH_OS_RTEMS] = do_bootm_rtems,
149 #if defined(CONFIG_BOOTM_OSE)
150 [IH_OS_OSE] = do_bootm_ose,
152 #if defined(CONFIG_CMD_ELF)
153 [IH_OS_VXWORKS] = do_bootm_vxworks,
154 [IH_OS_QNX] = do_bootm_qnxelf,
156 #ifdef CONFIG_INTEGRITY
157 [IH_OS_INTEGRITY] = do_bootm_integrity,
161 bootm_headers_t images; /* pointers to os/initrd/fdt images */
163 /* Allow for arch specific config before we boot */
164 void __arch_preboot_os(void)
166 /* please define platform specific arch_preboot_os() */
168 void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
170 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
173 static void boot_start_lmb(bootm_headers_t *images)
176 phys_size_t mem_size;
178 lmb_init(&images->lmb);
180 mem_start = getenv_bootm_low();
181 mem_size = getenv_bootm_size();
183 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
185 arch_lmb_reserve(&images->lmb);
186 board_lmb_reserve(&images->lmb);
189 static inline void boot_start_lmb(bootm_headers_t *images) { }
192 static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
197 memset((void *)&images, 0, sizeof(images));
198 images.verify = getenv_yesno("verify");
200 boot_start_lmb(&images);
202 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
204 /* get kernel image header, start address and length */
205 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
206 &images, &images.os.image_start, &images.os.image_len);
207 if (images.os.image_len == 0) {
208 puts("ERROR: can't get kernel image!\n");
212 /* get image parameters */
213 switch (genimg_get_format(os_hdr)) {
214 case IMAGE_FORMAT_LEGACY:
215 images.os.type = image_get_type(os_hdr);
216 images.os.comp = image_get_comp(os_hdr);
217 images.os.os = image_get_os(os_hdr);
219 images.os.end = image_get_image_end(os_hdr);
220 images.os.load = image_get_load(os_hdr);
222 #if defined(CONFIG_FIT)
223 case IMAGE_FORMAT_FIT:
224 if (fit_image_get_type(images.fit_hdr_os,
225 images.fit_noffset_os, &images.os.type)) {
226 puts("Can't get image type!\n");
227 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
231 if (fit_image_get_comp(images.fit_hdr_os,
232 images.fit_noffset_os, &images.os.comp)) {
233 puts("Can't get image compression!\n");
234 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
238 if (fit_image_get_os(images.fit_hdr_os,
239 images.fit_noffset_os, &images.os.os)) {
240 puts("Can't get image OS!\n");
241 bootstage_error(BOOTSTAGE_ID_FIT_OS);
245 images.os.end = fit_get_end(images.fit_hdr_os);
247 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
249 puts("Can't get image load address!\n");
250 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
256 puts("ERROR: unknown image format type!\n");
260 /* find kernel entry point */
261 if (images.legacy_hdr_valid) {
262 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
263 #if defined(CONFIG_FIT)
264 } else if (images.fit_uname_os) {
265 ret = fit_image_get_entry(images.fit_hdr_os,
266 images.fit_noffset_os, &images.ep);
268 puts("Can't get entry point property!\n");
273 puts("Could not find kernel entry point!\n");
277 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
278 images.os.load = images.os.image_start;
279 images.ep += images.os.load;
282 if (((images.os.type == IH_TYPE_KERNEL) ||
283 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
284 (images.os.type == IH_TYPE_MULTI)) &&
285 (images.os.os == IH_OS_LINUX)) {
287 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
288 &images.rd_start, &images.rd_end);
290 puts("Ramdisk image is corrupt or invalid\n");
294 #if defined(CONFIG_OF_LIBFDT)
295 /* find flattened device tree */
296 ret = boot_get_fdt(flag, argc, argv, &images,
297 &images.ft_addr, &images.ft_len);
299 puts("Could not find a valid device tree\n");
303 set_working_fdt_addr(images.ft_addr);
307 images.os.start = (ulong)os_hdr;
308 images.state = BOOTM_STATE_START;
313 #define BOOTM_ERR_RESET -1
314 #define BOOTM_ERR_OVERLAP -2
315 #define BOOTM_ERR_UNIMPLEMENTED -3
316 static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
318 uint8_t comp = os.comp;
319 ulong load = os.load;
320 ulong blob_start = os.start;
321 ulong blob_end = os.end;
322 ulong image_start = os.image_start;
323 ulong image_len = os.image_len;
324 __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
326 #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
328 #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
330 const char *type_name = genimg_get_type_name(os.type);
334 if (load == blob_start || load == image_start) {
335 printf(" XIP %s ... ", type_name);
338 printf(" Loading %s ... ", type_name);
339 memmove_wd((void *)load, (void *)image_start,
342 *load_end = load + image_len;
347 printf(" Uncompressing %s ... ", type_name);
348 if (gunzip((void *)load, unc_len,
349 (uchar *)image_start, &image_len) != 0) {
350 puts("GUNZIP: uncompress, out-of-mem or overwrite "
351 "error - must RESET board to recover\n");
353 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
354 return BOOTM_ERR_RESET;
357 *load_end = load + image_len;
359 #endif /* CONFIG_GZIP */
362 printf(" Uncompressing %s ... ", type_name);
364 * If we've got less than 4 MB of malloc() space,
365 * use slower decompression algorithm which requires
366 * at most 2300 KB of memory.
368 int i = BZ2_bzBuffToBuffDecompress((char *)load,
369 &unc_len, (char *)image_start, image_len,
370 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
372 printf("BUNZIP2: uncompress or overwrite error %d "
373 "- must RESET board to recover\n", i);
375 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
376 return BOOTM_ERR_RESET;
379 *load_end = load + unc_len;
381 #endif /* CONFIG_BZIP2 */
384 SizeT lzma_len = unc_len;
385 printf(" Uncompressing %s ... ", type_name);
387 ret = lzmaBuffToBuffDecompress(
388 (unsigned char *)load, &lzma_len,
389 (unsigned char *)image_start, image_len);
392 printf("LZMA: uncompress or overwrite error %d "
393 "- must RESET board to recover\n", ret);
394 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
395 return BOOTM_ERR_RESET;
397 *load_end = load + unc_len;
400 #endif /* CONFIG_LZMA */
403 printf(" Uncompressing %s ... ", type_name);
405 ret = lzop_decompress((const unsigned char *)image_start,
406 image_len, (unsigned char *)load,
408 if (ret != LZO_E_OK) {
409 printf("LZO: uncompress or overwrite error %d "
410 "- must RESET board to recover\n", ret);
412 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
413 return BOOTM_ERR_RESET;
416 *load_end = load + unc_len;
418 #endif /* CONFIG_LZO */
420 printf("Unimplemented compression type %d\n", comp);
421 return BOOTM_ERR_UNIMPLEMENTED;
424 flush_cache(load, (*load_end - load) * sizeof(ulong));
427 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
428 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
430 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
431 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
432 blob_start, blob_end);
433 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
436 return BOOTM_ERR_OVERLAP;
442 static int bootm_start_standalone(ulong iflag, int argc, char * const argv[])
445 int (*appl)(int, char * const []);
447 /* Don't start if "autostart" is set to "no" */
448 if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
450 sprintf(buf, "%lX", images.os.image_len);
451 setenv("filesize", buf);
454 appl = (int (*)(int, char * const []))(ulong)ntohl(images.ep);
455 (*appl)(argc-1, &argv[1]);
459 /* we overload the cmd field with our state machine info instead of a
460 * function pointer */
461 static cmd_tbl_t cmd_bootm_sub[] = {
462 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
463 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
464 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
465 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
467 #ifdef CONFIG_OF_LIBFDT
468 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
470 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
471 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
472 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
473 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
476 int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
484 c = find_cmd_tbl(argv[1], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
487 state = (long)c->cmd;
489 /* treat start special since it resets the state machine */
490 if (state == BOOTM_STATE_START) {
493 return bootm_start(cmdtp, flag, argc, argv);
496 /* Unrecognized command */
497 return CMD_RET_USAGE;
500 if (images.state >= state) {
501 printf("Trying to execute a command out of order\n");
502 return CMD_RET_USAGE;
505 images.state |= state;
506 boot_fn = boot_os[images.os.os];
510 case BOOTM_STATE_START:
511 /* should never occur */
513 case BOOTM_STATE_LOADOS:
514 ret = bootm_load_os(images.os, &load_end, 0);
518 lmb_reserve(&images.lmb, images.os.load,
519 (load_end - images.os.load));
521 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
522 case BOOTM_STATE_RAMDISK:
524 ulong rd_len = images.rd_end - images.rd_start;
527 ret = boot_ramdisk_high(&images.lmb, images.rd_start,
528 rd_len, &images.initrd_start, &images.initrd_end);
532 sprintf(str, "%lx", images.initrd_start);
533 setenv("initrd_start", str);
534 sprintf(str, "%lx", images.initrd_end);
535 setenv("initrd_end", str);
539 #if defined(CONFIG_OF_LIBFDT)
540 case BOOTM_STATE_FDT:
542 boot_fdt_add_mem_rsv_regions(&images.lmb,
544 ret = boot_relocate_fdt(&images.lmb,
545 &images.ft_addr, &images.ft_len);
549 case BOOTM_STATE_OS_CMDLINE:
550 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, &images);
552 printf("cmdline subcommand not supported\n");
554 case BOOTM_STATE_OS_BD_T:
555 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, &images);
557 printf("bdt subcommand not supported\n");
559 case BOOTM_STATE_OS_PREP:
560 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, &images);
562 printf("prep subcommand not supported\n");
564 case BOOTM_STATE_OS_GO:
565 disable_interrupts();
567 boot_fn(BOOTM_STATE_OS_GO, argc, argv, &images);
574 /*******************************************************************/
575 /* bootm - boot application image from image in memory */
576 /*******************************************************************/
578 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
584 #ifdef CONFIG_NEEDS_MANUAL_RELOC
585 static int relocated = 0;
587 /* relocate boot function table */
590 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
591 if (boot_os[i] != NULL)
592 boot_os[i] += gd->reloc_off;
597 /* determine if we have a sub command */
601 simple_strtoul(argv[1], &endp, 16);
602 /* endp pointing to NULL means that argv[1] was just a
603 * valid number, pass it along to the normal bootm processing
605 * If endp is ':' or '#' assume a FIT identifier so pass
606 * along for normal processing.
608 * Right now we assume the first arg should never be '-'
610 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
611 return do_bootm_subcommand(cmdtp, flag, argc, argv);
614 if (bootm_start(cmdtp, flag, argc, argv))
618 * We have reached the point of no return: we are going to
619 * overwrite all exception vector code, so we cannot easily
620 * recover from any failures any more...
622 iflag = disable_interrupts();
624 #if defined(CONFIG_CMD_USB)
626 * turn off USB to prevent the host controller from writing to the
627 * SDRAM while Linux is booting. This could happen (at least for OHCI
628 * controller), because the HCCA (Host Controller Communication Area)
629 * lies within the SDRAM and the host controller writes continously to
630 * this area (as busmaster!). The HccaFrameNumber is for example
631 * updated every 1 ms within the HCCA structure in SDRAM! For more
632 * details see the OpenHCI specification.
637 ret = bootm_load_os(images.os, &load_end, 1);
640 if (ret == BOOTM_ERR_RESET)
641 do_reset(cmdtp, flag, argc, argv);
642 if (ret == BOOTM_ERR_OVERLAP) {
643 if (images.legacy_hdr_valid) {
645 hdr = &images.legacy_hdr_os_copy;
646 if (image_get_type(hdr) == IH_TYPE_MULTI)
647 puts("WARNING: legacy format multi "
651 puts("ERROR: new format image overwritten - "
652 "must RESET the board to recover\n");
653 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
654 do_reset(cmdtp, flag, argc, argv);
657 if (ret == BOOTM_ERR_UNIMPLEMENTED) {
660 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
665 lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
667 if (images.os.type == IH_TYPE_STANDALONE) {
670 /* This may return when 'autostart' is 'no' */
671 bootm_start_standalone(iflag, argc, argv);
675 bootstage_mark(BOOTSTAGE_ID_CHECK_BOOT_OS);
677 #ifdef CONFIG_SILENT_CONSOLE
678 if (images.os.os == IH_OS_LINUX)
679 fixup_silent_linux();
682 boot_fn = boot_os[images.os.os];
684 if (boot_fn == NULL) {
687 printf("ERROR: booting os '%s' (%d) is not supported\n",
688 genimg_get_os_name(images.os.os), images.os.os);
689 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
695 boot_fn(0, argc, argv, &images);
697 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
699 puts("\n## Control returned to monitor - resetting...\n");
701 do_reset(cmdtp, flag, argc, argv);
706 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
708 const char *ep = getenv("autostart");
710 if (ep && !strcmp(ep, "yes")) {
712 local_args[0] = (char *)cmd;
713 local_args[1] = NULL;
714 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
715 return do_bootm(cmdtp, 0, 1, local_args);
722 * image_get_kernel - verify legacy format kernel image
723 * @img_addr: in RAM address of the legacy format image to be verified
724 * @verify: data CRC verification flag
726 * image_get_kernel() verifies legacy image integrity and returns pointer to
727 * legacy image header if image verification was completed successfully.
730 * pointer to a legacy image header if valid image was found
731 * otherwise return NULL
733 static image_header_t *image_get_kernel(ulong img_addr, int verify)
735 image_header_t *hdr = (image_header_t *)img_addr;
737 if (!image_check_magic(hdr)) {
738 puts("Bad Magic Number\n");
739 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
742 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
744 if (!image_check_hcrc(hdr)) {
745 puts("Bad Header Checksum\n");
746 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
750 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
751 image_print_contents(hdr);
754 puts(" Verifying Checksum ... ");
755 if (!image_check_dcrc(hdr)) {
756 printf("Bad Data CRC\n");
757 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
762 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
764 if (!image_check_target_arch(hdr)) {
765 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
766 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
773 * fit_check_kernel - verify FIT format kernel subimage
774 * @fit_hdr: pointer to the FIT image header
775 * os_noffset: kernel subimage node offset within FIT image
776 * @verify: data CRC verification flag
778 * fit_check_kernel() verifies integrity of the kernel subimage and from
779 * specified FIT image.
785 #if defined(CONFIG_FIT)
786 static int fit_check_kernel(const void *fit, int os_noffset, int verify)
788 fit_image_print(fit, os_noffset, " ");
791 puts(" Verifying Hash Integrity ... ");
792 if (!fit_image_check_hashes(fit, os_noffset)) {
793 puts("Bad Data Hash\n");
794 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_HASH);
799 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_ARCH);
801 if (!fit_image_check_target_arch(fit, os_noffset)) {
802 puts("Unsupported Architecture\n");
803 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_ARCH);
807 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
808 if (!fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL) &&
809 !fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL_NOLOAD)) {
810 puts("Not a kernel image\n");
811 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
815 bootstage_mark(BOOTSTAGE_ID_FIT_CHECKED);
818 #endif /* CONFIG_FIT */
821 * boot_get_kernel - find kernel image
822 * @os_data: pointer to a ulong variable, will hold os data start address
823 * @os_len: pointer to a ulong variable, will hold os data length
825 * boot_get_kernel() tries to find a kernel image, verifies its integrity
826 * and locates kernel data.
829 * pointer to image header if valid image was found, plus kernel start
830 * address and length, otherwise NULL
832 static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
833 char * const argv[], bootm_headers_t *images, ulong *os_data,
838 #if defined(CONFIG_FIT)
840 const char *fit_uname_config = NULL;
841 const char *fit_uname_kernel = NULL;
848 /* find out kernel image address */
850 img_addr = load_addr;
851 debug("* kernel: default image load address = 0x%08lx\n",
853 #if defined(CONFIG_FIT)
854 } else if (fit_parse_conf(argv[1], load_addr, &img_addr,
855 &fit_uname_config)) {
856 debug("* kernel: config '%s' from image at 0x%08lx\n",
857 fit_uname_config, img_addr);
858 } else if (fit_parse_subimage(argv[1], load_addr, &img_addr,
859 &fit_uname_kernel)) {
860 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
861 fit_uname_kernel, img_addr);
864 img_addr = simple_strtoul(argv[1], NULL, 16);
865 debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
868 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
870 /* copy from dataflash if needed */
871 img_addr = genimg_get_image(img_addr);
873 /* check image type, for FIT images get FIT kernel node */
874 *os_data = *os_len = 0;
875 switch (genimg_get_format((void *)img_addr)) {
876 case IMAGE_FORMAT_LEGACY:
877 printf("## Booting kernel from Legacy Image at %08lx ...\n",
879 hdr = image_get_kernel(img_addr, images->verify);
882 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
884 /* get os_data and os_len */
885 switch (image_get_type(hdr)) {
887 case IH_TYPE_KERNEL_NOLOAD:
888 *os_data = image_get_data(hdr);
889 *os_len = image_get_data_size(hdr);
892 image_multi_getimg(hdr, 0, os_data, os_len);
894 case IH_TYPE_STANDALONE:
895 *os_data = image_get_data(hdr);
896 *os_len = image_get_data_size(hdr);
899 printf("Wrong Image Type for %s command\n",
901 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
906 * copy image header to allow for image overwrites during
907 * kernel decompression.
909 memmove(&images->legacy_hdr_os_copy, hdr,
910 sizeof(image_header_t));
912 /* save pointer to image header */
913 images->legacy_hdr_os = hdr;
915 images->legacy_hdr_valid = 1;
916 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
918 #if defined(CONFIG_FIT)
919 case IMAGE_FORMAT_FIT:
920 fit_hdr = (void *)img_addr;
921 printf("## Booting kernel from FIT Image at %08lx ...\n",
924 if (!fit_check_format(fit_hdr)) {
925 puts("Bad FIT kernel image format!\n");
926 bootstage_error(BOOTSTAGE_ID_FIT_FORMAT);
929 bootstage_mark(BOOTSTAGE_ID_FIT_FORMAT);
931 if (!fit_uname_kernel) {
933 * no kernel image node unit name, try to get config
934 * node first. If config unit node name is NULL
935 * fit_conf_get_node() will try to find default config
938 bootstage_mark(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
939 cfg_noffset = fit_conf_get_node(fit_hdr,
941 if (cfg_noffset < 0) {
942 bootstage_error(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
945 /* save configuration uname provided in the first
948 images->fit_uname_cfg = fdt_get_name(fit_hdr,
951 printf(" Using '%s' configuration\n",
952 images->fit_uname_cfg);
953 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
955 os_noffset = fit_conf_get_kernel_node(fit_hdr,
957 fit_uname_kernel = fit_get_name(fit_hdr, os_noffset,
960 /* get kernel component image node offset */
961 bootstage_mark(BOOTSTAGE_ID_FIT_UNIT_NAME);
962 os_noffset = fit_image_get_node(fit_hdr,
965 if (os_noffset < 0) {
966 bootstage_error(BOOTSTAGE_ID_FIT_CONFIG);
970 printf(" Trying '%s' kernel subimage\n", fit_uname_kernel);
972 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_SUBIMAGE);
973 if (!fit_check_kernel(fit_hdr, os_noffset, images->verify))
976 /* get kernel image data address and length */
977 if (fit_image_get_data(fit_hdr, os_noffset, &data, &len)) {
978 puts("Could not find kernel subimage data!\n");
979 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO_ERR);
982 bootstage_mark(BOOTSTAGE_ID_FIT_KERNEL_INFO);
985 *os_data = (ulong)data;
986 images->fit_hdr_os = fit_hdr;
987 images->fit_uname_os = fit_uname_kernel;
988 images->fit_noffset_os = os_noffset;
992 printf("Wrong Image Format for %s command\n", cmdtp->name);
993 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
997 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
998 *os_data, *os_len, *os_len);
1000 return (void *)img_addr;
1004 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
1005 "boot application image from memory",
1006 "[addr [arg ...]]\n - boot application image stored in memory\n"
1007 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1008 "\t'arg' can be the address of an initrd image\n"
1009 #if defined(CONFIG_OF_LIBFDT)
1010 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1011 "\ta third argument is required which is the address of the\n"
1012 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1013 "\tuse a '-' for the second argument. If you do not pass a third\n"
1014 "\ta bd_info struct will be passed instead\n"
1016 #if defined(CONFIG_FIT)
1017 "\t\nFor the new multi component uImage format (FIT) addresses\n"
1018 "\tmust be extened to include component or configuration unit name:\n"
1019 "\taddr:<subimg_uname> - direct component image specification\n"
1020 "\taddr#<conf_uname> - configuration specification\n"
1021 "\tUse iminfo command to get the list of existing component\n"
1022 "\timages and configurations.\n"
1024 "\nSub-commands to do part of the bootm sequence. The sub-commands "
1026 "issued in the order below (it's ok to not issue all sub-commands):\n"
1027 "\tstart [addr [arg ...]]\n"
1028 "\tloados - load OS image\n"
1029 #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
1030 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1032 #if defined(CONFIG_OF_LIBFDT)
1033 "\tfdt - relocate flat device tree\n"
1035 "\tcmdline - OS specific command line processing/setup\n"
1036 "\tbdt - OS specific bd_t processing\n"
1037 "\tprep - OS specific prep before relocation or go\n"
1041 /*******************************************************************/
1042 /* bootd - boot default image */
1043 /*******************************************************************/
1044 #if defined(CONFIG_CMD_BOOTD)
1045 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1049 if (run_command(getenv("bootcmd"), flag) < 0)
1055 boot, 1, 1, do_bootd,
1056 "boot default, i.e., run 'bootcmd'",
1060 /* keep old command name "bootd" for backward compatibility */
1062 bootd, 1, 1, do_bootd,
1063 "boot default, i.e., run 'bootcmd'",
1070 /*******************************************************************/
1071 /* iminfo - print header info for a requested image */
1072 /*******************************************************************/
1073 #if defined(CONFIG_CMD_IMI)
1074 int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1081 return image_info(load_addr);
1084 for (arg = 1; arg < argc; ++arg) {
1085 addr = simple_strtoul(argv[arg], NULL, 16);
1086 if (image_info(addr) != 0)
1092 static int image_info(ulong addr)
1094 void *hdr = (void *)addr;
1096 printf("\n## Checking Image at %08lx ...\n", addr);
1098 switch (genimg_get_format(hdr)) {
1099 case IMAGE_FORMAT_LEGACY:
1100 puts(" Legacy image found\n");
1101 if (!image_check_magic(hdr)) {
1102 puts(" Bad Magic Number\n");
1106 if (!image_check_hcrc(hdr)) {
1107 puts(" Bad Header Checksum\n");
1111 image_print_contents(hdr);
1113 puts(" Verifying Checksum ... ");
1114 if (!image_check_dcrc(hdr)) {
1115 puts(" Bad Data CRC\n");
1120 #if defined(CONFIG_FIT)
1121 case IMAGE_FORMAT_FIT:
1122 puts(" FIT image found\n");
1124 if (!fit_check_format(hdr)) {
1125 puts("Bad FIT image format!\n");
1129 fit_print_contents(hdr);
1131 if (!fit_all_image_check_hashes(hdr)) {
1132 puts("Bad hash in FIT image!\n");
1139 puts("Unknown image format!\n");
1147 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
1148 "print header information for application image",
1150 " - print header information for application image starting at\n"
1151 " address 'addr' in memory; this includes verification of the\n"
1152 " image contents (magic number, header and payload checksums)"
1157 /*******************************************************************/
1158 /* imls - list all images found in flash */
1159 /*******************************************************************/
1160 #if defined(CONFIG_CMD_IMLS)
1161 int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1167 for (i = 0, info = &flash_info[0];
1168 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
1170 if (info->flash_id == FLASH_UNKNOWN)
1172 for (j = 0; j < info->sector_count; ++j) {
1174 hdr = (void *)info->start[j];
1178 switch (genimg_get_format(hdr)) {
1179 case IMAGE_FORMAT_LEGACY:
1180 if (!image_check_hcrc(hdr))
1183 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1184 image_print_contents(hdr);
1186 puts(" Verifying Checksum ... ");
1187 if (!image_check_dcrc(hdr)) {
1188 puts("Bad Data CRC\n");
1193 #if defined(CONFIG_FIT)
1194 case IMAGE_FORMAT_FIT:
1195 if (!fit_check_format(hdr))
1198 printf("FIT Image at %08lX:\n", (ulong)hdr);
1199 fit_print_contents(hdr);
1215 imls, 1, 1, do_imls,
1216 "list all images found in flash",
1218 " - Prints information about all images found at sector\n"
1219 " boundaries in flash."
1223 /*******************************************************************/
1224 /* helper routines */
1225 /*******************************************************************/
1226 #ifdef CONFIG_SILENT_CONSOLE
1227 static void fixup_silent_linux(void)
1229 char buf[256], *start, *end;
1230 char *cmdline = getenv("bootargs");
1232 /* Only fix cmdline when requested */
1233 if (!(gd->flags & GD_FLG_SILENT))
1236 debug("before silent fix-up: %s\n", cmdline);
1238 start = strstr(cmdline, "console=");
1240 end = strchr(start, ' ');
1241 strncpy(buf, cmdline, (start - cmdline + 8));
1243 strcpy(buf + (start - cmdline + 8), end);
1245 buf[start - cmdline + 8] = '\0';
1247 strcpy(buf, cmdline);
1248 strcat(buf, " console=");
1251 strcpy(buf, "console=");
1254 setenv("bootargs", buf);
1255 debug("after silent fix-up: %s\n", buf);
1257 #endif /* CONFIG_SILENT_CONSOLE */
1260 /*******************************************************************/
1261 /* OS booting routines */
1262 /*******************************************************************/
1264 #ifdef CONFIG_BOOTM_NETBSD
1265 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
1266 bootm_headers_t *images)
1268 void (*loader)(bd_t *, image_header_t *, char *, char *);
1269 image_header_t *os_hdr, *hdr;
1270 ulong kernel_data, kernel_len;
1274 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1277 #if defined(CONFIG_FIT)
1278 if (!images->legacy_hdr_valid) {
1279 fit_unsupported_reset("NetBSD");
1283 hdr = images->legacy_hdr_os;
1286 * Booting a (NetBSD) kernel image
1288 * This process is pretty similar to a standalone application:
1289 * The (first part of an multi-) image must be a stage-2 loader,
1290 * which in turn is responsible for loading & invoking the actual
1291 * kernel. The only differences are the parameters being passed:
1292 * besides the board info strucure, the loader expects a command
1293 * line, the name of the console device, and (optionally) the
1294 * address of the original image header.
1297 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1298 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
1304 #if defined(CONFIG_8xx_CONS_SMC1)
1306 #elif defined(CONFIG_8xx_CONS_SMC2)
1308 #elif defined(CONFIG_8xx_CONS_SCC2)
1310 #elif defined(CONFIG_8xx_CONS_SCC3)
1318 for (i = 2, len = 0; i < argc; i += 1)
1319 len += strlen(argv[i]) + 1;
1320 cmdline = malloc(len);
1322 for (i = 2, len = 0; i < argc; i += 1) {
1324 cmdline[len++] = ' ';
1325 strcpy(&cmdline[len], argv[i]);
1326 len += strlen(argv[i]);
1328 } else if ((cmdline = getenv("bootargs")) == NULL) {
1332 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
1334 printf("## Transferring control to NetBSD stage-2 loader "
1335 "(at address %08lx) ...\n",
1338 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1341 * NetBSD Stage-2 Loader Parameters:
1342 * r3: ptr to board info data
1344 * r5: console device
1345 * r6: boot args string
1347 (*loader)(gd->bd, os_hdr, consdev, cmdline);
1351 #endif /* CONFIG_BOOTM_NETBSD*/
1353 #ifdef CONFIG_LYNXKDI
1354 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
1355 bootm_headers_t *images)
1357 image_header_t *hdr = &images->legacy_hdr_os_copy;
1359 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1362 #if defined(CONFIG_FIT)
1363 if (!images->legacy_hdr_valid) {
1364 fit_unsupported_reset("Lynx");
1369 lynxkdi_boot((image_header_t *)hdr);
1373 #endif /* CONFIG_LYNXKDI */
1375 #ifdef CONFIG_BOOTM_RTEMS
1376 static int do_bootm_rtems(int flag, int argc, char * const argv[],
1377 bootm_headers_t *images)
1379 void (*entry_point)(bd_t *);
1381 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1384 #if defined(CONFIG_FIT)
1385 if (!images->legacy_hdr_valid) {
1386 fit_unsupported_reset("RTEMS");
1391 entry_point = (void (*)(bd_t *))images->ep;
1393 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
1394 (ulong)entry_point);
1396 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1400 * r3: ptr to board info data
1402 (*entry_point)(gd->bd);
1406 #endif /* CONFIG_BOOTM_RTEMS */
1408 #if defined(CONFIG_BOOTM_OSE)
1409 static int do_bootm_ose(int flag, int argc, char * const argv[],
1410 bootm_headers_t *images)
1412 void (*entry_point)(void);
1414 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1417 #if defined(CONFIG_FIT)
1418 if (!images->legacy_hdr_valid) {
1419 fit_unsupported_reset("OSE");
1424 entry_point = (void (*)(void))images->ep;
1426 printf("## Transferring control to OSE (at address %08lx) ...\n",
1427 (ulong)entry_point);
1429 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1439 #endif /* CONFIG_BOOTM_OSE */
1441 #if defined(CONFIG_CMD_ELF)
1442 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
1443 bootm_headers_t *images)
1447 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1450 #if defined(CONFIG_FIT)
1451 if (!images->legacy_hdr_valid) {
1452 fit_unsupported_reset("VxWorks");
1457 sprintf(str, "%lx", images->ep); /* write entry-point into string */
1458 setenv("loadaddr", str);
1459 do_bootvx(NULL, 0, 0, NULL);
1464 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
1465 bootm_headers_t *images)
1467 char *local_args[2];
1470 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1473 #if defined(CONFIG_FIT)
1474 if (!images->legacy_hdr_valid) {
1475 fit_unsupported_reset("QNX");
1480 sprintf(str, "%lx", images->ep); /* write entry-point into string */
1481 local_args[0] = argv[0];
1482 local_args[1] = str; /* and provide it via the arguments */
1483 do_bootelf(NULL, 0, 2, local_args);
1489 #ifdef CONFIG_INTEGRITY
1490 static int do_bootm_integrity(int flag, int argc, char * const argv[],
1491 bootm_headers_t *images)
1493 void (*entry_point)(void);
1495 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1498 #if defined(CONFIG_FIT)
1499 if (!images->legacy_hdr_valid) {
1500 fit_unsupported_reset("INTEGRITY");
1505 entry_point = (void (*)(void))images->ep;
1507 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
1508 (ulong)entry_point);
1510 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1513 * INTEGRITY Parameters:
1522 #ifdef CONFIG_CMD_BOOTZ
1524 static int __bootz_setup(void *image, void **start, void **end)
1526 /* Please define bootz_setup() for your platform */
1528 puts("Your platform's zImage format isn't supported yet!\n");
1531 int bootz_setup(void *image, void **start, void **end)
1532 __attribute__((weak, alias("__bootz_setup")));
1535 * zImage booting support
1537 static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1538 char * const argv[], bootm_headers_t *images)
1541 void *zi_start, *zi_end;
1543 memset(images, 0, sizeof(bootm_headers_t));
1545 boot_start_lmb(images);
1547 /* Setup Linux kernel zImage entry point */
1549 images->ep = load_addr;
1550 debug("* kernel: default image load address = 0x%08lx\n",
1553 images->ep = simple_strtoul(argv[1], NULL, 16);
1554 debug("* kernel: cmdline image address = 0x%08lx\n",
1558 ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
1562 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1565 ret = boot_get_ramdisk(argc, argv, images, IH_INITRD_ARCH,
1566 &images->rd_start, &images->rd_end);
1568 puts("Ramdisk image is corrupt or invalid\n");
1572 #if defined(CONFIG_OF_LIBFDT)
1573 /* find flattened device tree */
1574 ret = boot_get_fdt(flag, argc, argv, images,
1575 &images->ft_addr, &images->ft_len);
1577 puts("Could not find a valid device tree\n");
1581 set_working_fdt_addr(images->ft_addr);
1587 static int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1590 bootm_headers_t images;
1592 if (bootz_start(cmdtp, flag, argc, argv, &images))
1596 * We have reached the point of no return: we are going to
1597 * overwrite all exception vector code, so we cannot easily
1598 * recover from any failures any more...
1600 iflag = disable_interrupts();
1602 #if defined(CONFIG_CMD_USB)
1604 * turn off USB to prevent the host controller from writing to the
1605 * SDRAM while Linux is booting. This could happen (at least for OHCI
1606 * controller), because the HCCA (Host Controller Communication Area)
1607 * lies within the SDRAM and the host controller writes continously to
1608 * this area (as busmaster!). The HccaFrameNumber is for example
1609 * updated every 1 ms within the HCCA structure in SDRAM! For more
1610 * details see the OpenHCI specification.
1615 #ifdef CONFIG_SILENT_CONSOLE
1616 fixup_silent_linux();
1620 do_bootm_linux(0, argc, argv, &images);
1622 puts("\n## Control returned to monitor - resetting...\n");
1624 do_reset(cmdtp, flag, argc, argv);
1630 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
1631 "boot Linux zImage image from memory",
1632 "[addr [initrd[:size]] [fdt]]\n"
1633 " - boot Linux zImage stored in memory\n"
1634 "\tThe argument 'initrd' is optional and specifies the address\n"
1635 "\tof the initrd in memory. The optional argument ':size' allows\n"
1636 "\tspecifying the size of RAW initrd.\n"
1637 #if defined(CONFIG_OF_LIBFDT)
1638 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1639 "\ta third argument is required which is the address of the\n"
1640 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1641 "\tuse a '-' for the second argument. If you do not pass a third\n"
1642 "\ta bd_info struct will be passed instead\n"
1645 #endif /* CONFIG_CMD_BOOTZ */