2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <fdt_support.h>
16 #include <linux/lzo.h>
17 #include <lzma/LzmaTypes.h>
18 #include <lzma/LzmaDec.h>
19 #include <lzma/LzmaTools.h>
21 #if defined(CONFIG_CMD_USB)
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifndef CONFIG_SYS_BOOTM_LEN
28 /* use 8MByte as default max gunzip size */
29 #define CONFIG_SYS_BOOTM_LEN 0x800000
32 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
34 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
35 char * const argv[], bootm_headers_t *images,
36 ulong *os_data, ulong *os_len);
39 static void boot_start_lmb(bootm_headers_t *images)
44 lmb_init(&images->lmb);
46 mem_start = getenv_bootm_low();
47 mem_size = getenv_bootm_size();
49 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
51 arch_lmb_reserve(&images->lmb);
52 board_lmb_reserve(&images->lmb);
55 #define lmb_reserve(lmb, base, size)
56 static inline void boot_start_lmb(bootm_headers_t *images) { }
59 static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc,
62 memset((void *)&images, 0, sizeof(images));
63 images.verify = getenv_yesno("verify");
65 boot_start_lmb(&images);
67 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
68 images.state = BOOTM_STATE_START;
73 static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
77 bool ep_found = false;
79 /* get kernel image header, start address and length */
80 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
81 &images, &images.os.image_start, &images.os.image_len);
82 if (images.os.image_len == 0) {
83 puts("ERROR: can't get kernel image!\n");
87 /* get image parameters */
88 switch (genimg_get_format(os_hdr)) {
89 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
90 case IMAGE_FORMAT_LEGACY:
91 images.os.type = image_get_type(os_hdr);
92 images.os.comp = image_get_comp(os_hdr);
93 images.os.os = image_get_os(os_hdr);
95 images.os.end = image_get_image_end(os_hdr);
96 images.os.load = image_get_load(os_hdr);
99 #if defined(CONFIG_FIT)
100 case IMAGE_FORMAT_FIT:
101 if (fit_image_get_type(images.fit_hdr_os,
102 images.fit_noffset_os,
104 puts("Can't get image type!\n");
105 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
109 if (fit_image_get_comp(images.fit_hdr_os,
110 images.fit_noffset_os,
112 puts("Can't get image compression!\n");
113 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
117 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
119 puts("Can't get image OS!\n");
120 bootstage_error(BOOTSTAGE_ID_FIT_OS);
124 images.os.end = fit_get_end(images.fit_hdr_os);
126 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
128 puts("Can't get image load address!\n");
129 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
134 #ifdef CONFIG_ANDROID_BOOT_IMAGE
135 case IMAGE_FORMAT_ANDROID:
136 images.os.type = IH_TYPE_KERNEL;
137 images.os.comp = IH_COMP_NONE;
138 images.os.os = IH_OS_LINUX;
139 images.ep = images.os.load;
142 images.os.end = android_image_get_end(os_hdr);
143 images.os.load = android_image_get_kload(os_hdr);
147 puts("ERROR: unknown image format type!\n");
151 /* find kernel entry point */
152 if (images.legacy_hdr_valid) {
153 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
154 #if defined(CONFIG_FIT)
155 } else if (images.fit_uname_os) {
158 ret = fit_image_get_entry(images.fit_hdr_os,
159 images.fit_noffset_os, &images.ep);
161 puts("Can't get entry point property!\n");
165 } else if (!ep_found) {
166 puts("Could not find kernel entry point!\n");
170 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
171 images.os.load = images.os.image_start;
172 images.ep += images.os.load;
175 images.os.start = (ulong)os_hdr;
180 static int bootm_find_ramdisk(int flag, int argc, char * const argv[])
185 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
186 &images.rd_start, &images.rd_end);
188 puts("Ramdisk image is corrupt or invalid\n");
195 #if defined(CONFIG_OF_LIBFDT)
196 static int bootm_find_fdt(int flag, int argc, char * const argv[])
200 /* find flattened device tree */
201 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
202 &images.ft_addr, &images.ft_len);
204 puts("Could not find a valid device tree\n");
208 set_working_fdt_addr(images.ft_addr);
214 int bootm_find_ramdisk_fdt(int flag, int argc, char * const argv[])
216 if (bootm_find_ramdisk(flag, argc, argv))
219 #if defined(CONFIG_OF_LIBFDT)
220 if (bootm_find_fdt(flag, argc, argv))
227 static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
230 if (((images.os.type == IH_TYPE_KERNEL) ||
231 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
232 (images.os.type == IH_TYPE_MULTI)) &&
233 (images.os.os == IH_OS_LINUX ||
234 images.os.os == IH_OS_VXWORKS))
235 return bootm_find_ramdisk_fdt(flag, argc, argv);
240 static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
243 image_info_t os = images->os;
244 uint8_t comp = os.comp;
245 ulong load = os.load;
246 ulong blob_start = os.start;
247 ulong blob_end = os.end;
248 ulong image_start = os.image_start;
249 ulong image_len = os.image_len;
250 __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
252 void *load_buf, *image_buf;
253 #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
255 #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
257 const char *type_name = genimg_get_type_name(os.type);
259 load_buf = map_sysmem(load, unc_len);
260 image_buf = map_sysmem(image_start, image_len);
263 if (load == image_start) {
264 printf(" XIP %s ... ", type_name);
267 printf(" Loading %s ... ", type_name);
268 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
270 *load_end = load + image_len;
274 printf(" Uncompressing %s ... ", type_name);
275 if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
276 puts("GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover\n");
278 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
279 return BOOTM_ERR_RESET;
282 *load_end = load + image_len;
284 #endif /* CONFIG_GZIP */
287 printf(" Uncompressing %s ... ", type_name);
289 * If we've got less than 4 MB of malloc() space,
290 * use slower decompression algorithm which requires
291 * at most 2300 KB of memory.
293 int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
294 image_buf, image_len,
295 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
297 printf("BUNZIP2: uncompress or overwrite error %d - must RESET board to recover\n",
300 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
301 return BOOTM_ERR_RESET;
304 *load_end = load + unc_len;
306 #endif /* CONFIG_BZIP2 */
309 SizeT lzma_len = unc_len;
310 printf(" Uncompressing %s ... ", type_name);
312 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
313 image_buf, image_len);
316 printf("LZMA: uncompress or overwrite error %d - must RESET board to recover\n",
318 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
319 return BOOTM_ERR_RESET;
321 *load_end = load + unc_len;
324 #endif /* CONFIG_LZMA */
327 size_t size = unc_len;
329 printf(" Uncompressing %s ... ", type_name);
331 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
332 if (ret != LZO_E_OK) {
333 printf("LZO: uncompress or overwrite error %d - must RESET board to recover\n",
336 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
337 return BOOTM_ERR_RESET;
340 *load_end = load + size;
343 #endif /* CONFIG_LZO */
345 printf("Unimplemented compression type %d\n", comp);
346 return BOOTM_ERR_UNIMPLEMENTED;
349 flush_cache(load, (*load_end - load) * sizeof(ulong));
352 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
353 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
355 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
356 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
357 blob_start, blob_end);
358 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
361 /* Check what type of image this is. */
362 if (images->legacy_hdr_valid) {
363 if (image_get_type(&images->legacy_hdr_os_copy)
365 puts("WARNING: legacy format multi component image overwritten\n");
366 return BOOTM_ERR_OVERLAP;
368 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
369 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
370 return BOOTM_ERR_RESET;
378 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
380 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
383 ulong bootm_disable_interrupts(void)
388 * We have reached the point of no return: we are going to
389 * overwrite all exception vector code, so we cannot easily
390 * recover from any failures any more...
392 iflag = disable_interrupts();
393 #ifdef CONFIG_NETCONSOLE
394 /* Stop the ethernet stack if NetConsole could have left it up */
396 eth_unregister(eth_get_dev());
399 #if defined(CONFIG_CMD_USB)
401 * turn off USB to prevent the host controller from writing to the
402 * SDRAM while Linux is booting. This could happen (at least for OHCI
403 * controller), because the HCCA (Host Controller Communication Area)
404 * lies within the SDRAM and the host controller writes continously to
405 * this area (as busmaster!). The HccaFrameNumber is for example
406 * updated every 1 ms within the HCCA structure in SDRAM! For more
407 * details see the OpenHCI specification.
414 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
416 #define CONSOLE_ARG "console="
417 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
419 static void fixup_silent_linux(void)
423 char *cmdline = getenv("bootargs");
427 * Only fix cmdline when requested. The environment variable can be:
429 * no - we never fixup
430 * yes - we always fixup
431 * unset - we rely on the console silent flag
433 want_silent = getenv_yesno("silent_linux");
434 if (want_silent == 0)
436 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
439 debug("before silent fix-up: %s\n", cmdline);
440 if (cmdline && (cmdline[0] != '\0')) {
441 char *start = strstr(cmdline, CONSOLE_ARG);
443 /* Allocate space for maximum possible new command line */
444 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
446 debug("%s: out of memory\n", __func__);
451 char *end = strchr(start, ' ');
452 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
454 strncpy(buf, cmdline, num_start_bytes);
456 strcpy(buf + num_start_bytes, end);
458 buf[num_start_bytes] = '\0';
460 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
465 env_val = CONSOLE_ARG;
468 setenv("bootargs", env_val);
469 debug("after silent fix-up: %s\n", env_val);
472 #endif /* CONFIG_SILENT_CONSOLE */
475 * Execute selected states of the bootm command.
477 * Note the arguments to this state must be the first argument, Any 'bootm'
478 * or sub-command arguments must have already been taken.
480 * Note that if states contains more than one flag it MUST contain
481 * BOOTM_STATE_START, since this handles and consumes the command line args.
483 * Also note that aside from boot_os_fn functions and bootm_load_os no other
484 * functions we store the return value of in 'ret' may use a negative return
485 * value, without special handling.
487 * @param cmdtp Pointer to bootm command table entry
488 * @param flag Command flags (CMD_FLAG_...)
489 * @param argc Number of subcommand arguments (0 = no arguments)
490 * @param argv Arguments
491 * @param states Mask containing states to run (BOOTM_STATE_...)
492 * @param images Image header information
493 * @param boot_progress 1 to show boot progress, 0 to not do this
494 * @return 0 if ok, something else on error. Some errors will cause this
495 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
496 * then the intent is to boot an OS, so this function will not return
497 * unless the image type is standalone.
499 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
500 int states, bootm_headers_t *images, int boot_progress)
504 int ret = 0, need_boot_fn;
506 images->state |= states;
509 * Work through the states and see how far we get. We stop on
512 if (states & BOOTM_STATE_START)
513 ret = bootm_start(cmdtp, flag, argc, argv);
515 if (!ret && (states & BOOTM_STATE_FINDOS))
516 ret = bootm_find_os(cmdtp, flag, argc, argv);
518 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
519 ret = bootm_find_other(cmdtp, flag, argc, argv);
520 argc = 0; /* consume the args */
524 if (!ret && (states & BOOTM_STATE_LOADOS)) {
527 iflag = bootm_disable_interrupts();
528 ret = bootm_load_os(images, &load_end, 0);
530 lmb_reserve(&images->lmb, images->os.load,
531 (load_end - images->os.load));
532 else if (ret && ret != BOOTM_ERR_OVERLAP)
534 else if (ret == BOOTM_ERR_OVERLAP)
536 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
537 if (images->os.os == IH_OS_LINUX)
538 fixup_silent_linux();
542 /* Relocate the ramdisk */
543 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
544 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
545 ulong rd_len = images->rd_end - images->rd_start;
547 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
548 rd_len, &images->initrd_start, &images->initrd_end);
550 setenv_hex("initrd_start", images->initrd_start);
551 setenv_hex("initrd_end", images->initrd_end);
555 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
556 if (!ret && (states & BOOTM_STATE_FDT)) {
557 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
558 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
563 /* From now on, we need the OS boot function */
566 boot_fn = bootm_os_get_boot_func(images->os.os);
567 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
568 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
569 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
570 if (boot_fn == NULL && need_boot_fn) {
573 printf("ERROR: booting os '%s' (%d) is not supported\n",
574 genimg_get_os_name(images->os.os), images->os.os);
575 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
579 /* Call various other states that are not generally used */
580 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
581 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
582 if (!ret && (states & BOOTM_STATE_OS_BD_T))
583 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
584 if (!ret && (states & BOOTM_STATE_OS_PREP))
585 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
588 /* Pretend to run the OS, then run a user command */
589 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
590 char *cmd_list = getenv("fakegocmd");
592 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
594 if (!ret && cmd_list)
595 ret = run_command_list(cmd_list, -1, flag);
599 /* Check for unsupported subcommand. */
601 puts("subcommand not supported\n");
605 /* Now run the OS! We hope this doesn't return */
606 if (!ret && (states & BOOTM_STATE_OS_GO))
607 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
610 /* Deal with any fallout */
615 if (ret == BOOTM_ERR_UNIMPLEMENTED)
616 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
617 else if (ret == BOOTM_ERR_RESET)
618 do_reset(cmdtp, flag, argc, argv);
623 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
625 * image_get_kernel - verify legacy format kernel image
626 * @img_addr: in RAM address of the legacy format image to be verified
627 * @verify: data CRC verification flag
629 * image_get_kernel() verifies legacy image integrity and returns pointer to
630 * legacy image header if image verification was completed successfully.
633 * pointer to a legacy image header if valid image was found
634 * otherwise return NULL
636 static image_header_t *image_get_kernel(ulong img_addr, int verify)
638 image_header_t *hdr = (image_header_t *)img_addr;
640 if (!image_check_magic(hdr)) {
641 puts("Bad Magic Number\n");
642 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
645 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
647 if (!image_check_hcrc(hdr)) {
648 puts("Bad Header Checksum\n");
649 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
653 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
654 image_print_contents(hdr);
657 puts(" Verifying Checksum ... ");
658 if (!image_check_dcrc(hdr)) {
659 printf("Bad Data CRC\n");
660 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
665 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
667 if (!image_check_target_arch(hdr)) {
668 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
669 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
677 * boot_get_kernel - find kernel image
678 * @os_data: pointer to a ulong variable, will hold os data start address
679 * @os_len: pointer to a ulong variable, will hold os data length
681 * boot_get_kernel() tries to find a kernel image, verifies its integrity
682 * and locates kernel data.
685 * pointer to image header if valid image was found, plus kernel start
686 * address and length, otherwise NULL
688 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
689 char * const argv[], bootm_headers_t *images,
690 ulong *os_data, ulong *os_len)
692 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
697 #if defined(CONFIG_FIT)
698 const char *fit_uname_config = NULL;
699 const char *fit_uname_kernel = NULL;
703 /* find out kernel image address */
705 img_addr = load_addr;
706 debug("* kernel: default image load address = 0x%08lx\n",
708 #if defined(CONFIG_FIT)
709 } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
710 &fit_uname_config)) {
711 debug("* kernel: config '%s' from image at 0x%08lx\n",
712 fit_uname_config, img_addr);
713 } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
714 &fit_uname_kernel)) {
715 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
716 fit_uname_kernel, img_addr);
719 img_addr = simple_strtoul(argv[0], NULL, 16);
720 debug("* kernel: cmdline image address = 0x%08lx\n",
724 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
726 /* copy from dataflash if needed */
727 img_addr = genimg_get_image(img_addr);
729 /* check image type, for FIT images get FIT kernel node */
730 *os_data = *os_len = 0;
731 buf = map_sysmem(img_addr, 0);
732 switch (genimg_get_format(buf)) {
733 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
734 case IMAGE_FORMAT_LEGACY:
735 printf("## Booting kernel from Legacy Image at %08lx ...\n",
737 hdr = image_get_kernel(img_addr, images->verify);
740 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
742 /* get os_data and os_len */
743 switch (image_get_type(hdr)) {
745 case IH_TYPE_KERNEL_NOLOAD:
746 *os_data = image_get_data(hdr);
747 *os_len = image_get_data_size(hdr);
750 image_multi_getimg(hdr, 0, os_data, os_len);
752 case IH_TYPE_STANDALONE:
753 *os_data = image_get_data(hdr);
754 *os_len = image_get_data_size(hdr);
757 printf("Wrong Image Type for %s command\n",
759 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
764 * copy image header to allow for image overwrites during
765 * kernel decompression.
767 memmove(&images->legacy_hdr_os_copy, hdr,
768 sizeof(image_header_t));
770 /* save pointer to image header */
771 images->legacy_hdr_os = hdr;
773 images->legacy_hdr_valid = 1;
774 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
777 #if defined(CONFIG_FIT)
778 case IMAGE_FORMAT_FIT:
779 os_noffset = fit_image_load(images, img_addr,
780 &fit_uname_kernel, &fit_uname_config,
781 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
782 BOOTSTAGE_ID_FIT_KERNEL_START,
783 FIT_LOAD_IGNORED, os_data, os_len);
787 images->fit_hdr_os = map_sysmem(img_addr, 0);
788 images->fit_uname_os = fit_uname_kernel;
789 images->fit_uname_cfg = fit_uname_config;
790 images->fit_noffset_os = os_noffset;
793 #ifdef CONFIG_ANDROID_BOOT_IMAGE
794 case IMAGE_FORMAT_ANDROID:
795 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
796 if (android_image_get_kernel(buf, images->verify,
802 printf("Wrong Image Format for %s command\n", cmdtp->name);
803 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
807 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
808 *os_data, *os_len, *os_len);