1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <fdt_support.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
24 #include <linux/sizes.h>
25 #if defined(CONFIG_CMD_USB)
36 #define MAX_CMDLINE_SIZE SZ_4K
38 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
42 DECLARE_GLOBAL_DATA_PTR;
44 struct bootm_headers images; /* pointers to os/initrd/fdt images */
46 static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
47 char *const argv[], struct bootm_headers *images,
48 ulong *os_data, ulong *os_len);
50 __weak void board_quiesce_devices(void)
55 static void boot_start_lmb(struct bootm_headers *images)
60 mem_start = env_get_bootm_low();
61 mem_size = env_get_bootm_size();
63 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
67 #define lmb_reserve(lmb, base, size)
68 static inline void boot_start_lmb(struct bootm_headers *images) { }
71 static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
74 memset((void *)&images, 0, sizeof(images));
75 images.verify = env_get_yesno("verify");
77 boot_start_lmb(&images);
79 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
80 images.state = BOOTM_STATE_START;
85 static ulong bootm_data_addr(int argc, char *const argv[])
90 addr = simple_strtoul(argv[0], NULL, 16);
92 addr = image_load_addr;
97 static int bootm_pre_load(struct cmd_tbl *cmdtp, int flag, int argc,
100 ulong data_addr = bootm_data_addr(argc, argv);
103 if (CONFIG_IS_ENABLED(CMD_BOOTM_PRE_LOAD))
104 ret = image_pre_load(data_addr);
107 ret = CMD_RET_FAILURE;
112 static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
116 bool ep_found = false;
119 /* get kernel image header, start address and length */
120 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
121 &images, &images.os.image_start, &images.os.image_len);
122 if (images.os.image_len == 0) {
123 puts("ERROR: can't get kernel image!\n");
127 /* get image parameters */
128 switch (genimg_get_format(os_hdr)) {
129 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
130 case IMAGE_FORMAT_LEGACY:
131 images.os.type = image_get_type(os_hdr);
132 images.os.comp = image_get_comp(os_hdr);
133 images.os.os = image_get_os(os_hdr);
135 images.os.end = image_get_image_end(os_hdr);
136 images.os.load = image_get_load(os_hdr);
137 images.os.arch = image_get_arch(os_hdr);
140 #if CONFIG_IS_ENABLED(FIT)
141 case IMAGE_FORMAT_FIT:
142 if (fit_image_get_type(images.fit_hdr_os,
143 images.fit_noffset_os,
145 puts("Can't get image type!\n");
146 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
150 if (fit_image_get_comp(images.fit_hdr_os,
151 images.fit_noffset_os,
153 puts("Can't get image compression!\n");
154 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
158 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
160 puts("Can't get image OS!\n");
161 bootstage_error(BOOTSTAGE_ID_FIT_OS);
165 if (fit_image_get_arch(images.fit_hdr_os,
166 images.fit_noffset_os,
168 puts("Can't get image ARCH!\n");
172 images.os.end = fit_get_end(images.fit_hdr_os);
174 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
176 puts("Can't get image load address!\n");
177 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
182 #ifdef CONFIG_ANDROID_BOOT_IMAGE
183 case IMAGE_FORMAT_ANDROID:
184 images.os.type = IH_TYPE_KERNEL;
185 images.os.comp = android_image_get_kcomp(os_hdr);
186 images.os.os = IH_OS_LINUX;
188 images.os.end = android_image_get_end(os_hdr);
189 images.os.load = android_image_get_kload(os_hdr);
190 images.ep = images.os.load;
195 puts("ERROR: unknown image format type!\n");
199 /* If we have a valid setup.bin, we will use that for entry (x86) */
200 if (images.os.arch == IH_ARCH_I386 ||
201 images.os.arch == IH_ARCH_X86_64) {
204 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
205 if (ret < 0 && ret != -ENOENT) {
206 puts("Could not find a valid setup.bin for x86\n");
209 /* Kernel entry point is the setup.bin */
210 } else if (images.legacy_hdr_valid) {
211 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
212 #if CONFIG_IS_ENABLED(FIT)
213 } else if (images.fit_uname_os) {
216 ret = fit_image_get_entry(images.fit_hdr_os,
217 images.fit_noffset_os, &images.ep);
219 puts("Can't get entry point property!\n");
223 } else if (!ep_found) {
224 puts("Could not find kernel entry point!\n");
228 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
229 if (CONFIG_IS_ENABLED(CMD_BOOTI) &&
230 images.os.arch == IH_ARCH_ARM64) {
234 ret = booti_setup(images.os.image_start, &image_addr,
239 images.os.type = IH_TYPE_KERNEL;
240 images.os.load = image_addr;
241 images.ep = image_addr;
243 images.os.load = images.os.image_start;
244 images.ep += images.os.image_start;
248 images.os.start = map_to_sysmem(os_hdr);
254 * bootm_find_images - wrapper to find and locate various images
255 * @flag: Ignored Argument
256 * @argc: command argument count
257 * @argv: command argument list
258 * @start: OS image start address
259 * @size: OS image size
261 * boot_find_images() will attempt to load an available ramdisk,
262 * flattened device tree, as well as specifically marked
263 * "loadable" images (loadables are FIT only)
265 * Note: bootm_find_images will skip an image if it is not found
268 * 0, if all existing images were loaded correctly
269 * 1, if an image is found but corrupted, or invalid
271 int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
277 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
278 &images.rd_start, &images.rd_end);
280 puts("Ramdisk image is corrupt or invalid\n");
284 /* check if ramdisk overlaps OS image */
285 if (images.rd_start && (((ulong)images.rd_start >= start &&
286 (ulong)images.rd_start < start + size) ||
287 ((ulong)images.rd_end > start &&
288 (ulong)images.rd_end <= start + size) ||
289 ((ulong)images.rd_start < start &&
290 (ulong)images.rd_end >= start + size))) {
291 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
292 start, start + size);
296 #if CONFIG_IS_ENABLED(OF_LIBFDT)
297 /* find flattened device tree */
298 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
299 &images.ft_addr, &images.ft_len);
301 puts("Could not find a valid device tree\n");
305 /* check if FDT overlaps OS image */
306 if (images.ft_addr &&
307 (((ulong)images.ft_addr >= start &&
308 (ulong)images.ft_addr < start + size) ||
309 ((ulong)images.ft_addr + images.ft_len >= start &&
310 (ulong)images.ft_addr + images.ft_len < start + size))) {
311 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
312 start, start + size);
316 if (CONFIG_IS_ENABLED(CMD_FDT))
317 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
320 #if CONFIG_IS_ENABLED(FIT)
321 if (IS_ENABLED(CONFIG_FPGA)) {
322 /* find bitstreams */
323 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
326 printf("FPGA image is corrupted or invalid\n");
331 /* find all of the loadables */
332 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
335 printf("Loadable(s) is corrupt or invalid\n");
343 static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
346 if (((images.os.type == IH_TYPE_KERNEL) ||
347 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
348 (images.os.type == IH_TYPE_MULTI)) &&
349 (images.os.os == IH_OS_LINUX ||
350 images.os.os == IH_OS_VXWORKS))
351 return bootm_find_images(flag, argc, argv, 0, 0);
355 #endif /* USE_HOSTC */
357 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
359 * handle_decomp_error() - display a decompression error
361 * This function tries to produce a useful message. In the case where the
362 * uncompressed size is the same as the available space, we can assume that
363 * the image is too large for the buffer.
365 * @comp_type: Compression type being used (IH_COMP_...)
366 * @uncomp_size: Number of bytes uncompressed
367 * @buf_size: Number of bytes the decompresion buffer was
368 * @ret: errno error code received from compression library
369 * Return: Appropriate BOOTM_ERR_ error code
371 static int handle_decomp_error(int comp_type, size_t uncomp_size,
372 size_t buf_size, int ret)
374 const char *name = genimg_get_comp_name(comp_type);
376 /* ENOSYS means unimplemented compression type, don't reset. */
378 return BOOTM_ERR_UNIMPLEMENTED;
380 if (uncomp_size >= buf_size)
381 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
383 printf("%s: uncompress error %d\n", name, ret);
386 * The decompression routines are now safe, so will not write beyond
387 * their bounds. Probably it is not necessary to reset, but maintain
388 * the current behaviour for now.
390 printf("Must RESET board to recover\n");
392 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
395 return BOOTM_ERR_RESET;
400 static int bootm_load_os(struct bootm_headers *images, int boot_progress)
402 struct image_info os = images->os;
403 ulong load = os.load;
405 ulong blob_start = os.start;
406 ulong blob_end = os.end;
407 ulong image_start = os.image_start;
408 ulong image_len = os.image_len;
409 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
411 void *load_buf, *image_buf;
414 load_buf = map_sysmem(load, 0);
415 image_buf = map_sysmem(os.image_start, image_len);
416 err = image_decomp(os.comp, load, os.image_start, os.type,
417 load_buf, image_buf, image_len,
418 CONFIG_SYS_BOOTM_LEN, &load_end);
420 err = handle_decomp_error(os.comp, load_end - load,
421 CONFIG_SYS_BOOTM_LEN, err);
422 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
425 /* We need the decompressed image size in the next steps */
426 images->os.image_len = load_end - load;
428 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
430 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
431 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
433 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
435 if (!no_overlap && load < blob_end && load_end > blob_start) {
436 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
437 blob_start, blob_end);
438 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
441 /* Check what type of image this is. */
442 if (images->legacy_hdr_valid) {
443 if (image_get_type(&images->legacy_hdr_os_copy)
445 puts("WARNING: legacy format multi component image overwritten\n");
446 return BOOTM_ERR_OVERLAP;
448 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
449 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
450 return BOOTM_ERR_RESET;
454 lmb_reserve(&images->lmb, images->os.load, (load_end -
460 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
462 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
465 ulong bootm_disable_interrupts(void)
470 * We have reached the point of no return: we are going to
471 * overwrite all exception vector code, so we cannot easily
472 * recover from any failures any more...
474 iflag = disable_interrupts();
475 #ifdef CONFIG_NETCONSOLE
476 /* Stop the ethernet stack if NetConsole could have left it up */
478 # ifndef CONFIG_DM_ETH
479 eth_unregister(eth_get_dev());
483 #if defined(CONFIG_CMD_USB)
485 * turn off USB to prevent the host controller from writing to the
486 * SDRAM while Linux is booting. This could happen (at least for OHCI
487 * controller), because the HCCA (Host Controller Communication Area)
488 * lies within the SDRAM and the host controller writes continously to
489 * this area (as busmaster!). The HccaFrameNumber is for example
490 * updated every 1 ms within the HCCA structure in SDRAM! For more
491 * details see the OpenHCI specification.
498 #define CONSOLE_ARG "console="
499 #define NULL_CONSOLE (CONSOLE_ARG "ttynull")
500 #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
503 * fixup_silent_linux() - Handle silencing the linux boot if required
505 * This uses the silent_linux envvar to control whether to add/set a "console="
506 * parameter to the command line
508 * @buf: Buffer containing the string to process
509 * @maxlen: Maximum length of buffer
510 * Return: 0 if OK, -ENOSPC if @maxlen is too small
512 static int fixup_silent_linux(char *buf, int maxlen)
519 * Move the input string to the end of buffer. The output string will be
520 * built up at the start.
522 size = strlen(buf) + 1;
523 if (size * 2 > maxlen)
525 cmdline = buf + maxlen - size;
526 memmove(cmdline, buf, size);
528 * Only fix cmdline when requested. The environment variable can be:
530 * no - we never fixup
531 * yes - we always fixup
532 * unset - we rely on the console silent flag
534 want_silent = env_get_yesno("silent_linux");
535 if (want_silent == 0)
537 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
540 debug("before silent fix-up: %s\n", cmdline);
542 char *start = strstr(cmdline, CONSOLE_ARG);
544 /* Check space for maximum possible new command line */
545 if (size + CONSOLE_ARG_SIZE > maxlen)
549 char *end = strchr(start, ' ');
552 start_bytes = start - cmdline;
553 strncpy(buf, cmdline, start_bytes);
554 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
556 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
558 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
560 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
562 if (buf + strlen(buf) >= cmdline)
565 if (maxlen < CONSOLE_ARG_SIZE)
567 strcpy(buf, NULL_CONSOLE);
569 debug("after silent fix-up: %s\n", buf);
575 * process_subst() - Handle substitution of ${...} fields in the environment
577 * Handle variable substitution in the provided buffer
579 * @buf: Buffer containing the string to process
580 * @maxlen: Maximum length of buffer
581 * Return: 0 if OK, -ENOSPC if @maxlen is too small
583 static int process_subst(char *buf, int maxlen)
589 /* Move to end of buffer */
590 size = strlen(buf) + 1;
591 cmdline = buf + maxlen - size;
592 if (buf + size > cmdline)
594 memmove(cmdline, buf, size);
596 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
601 int bootm_process_cmdline(char *buf, int maxlen, int flags)
605 /* Check config first to enable compiler to eliminate code */
606 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
607 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
608 (flags & BOOTM_CL_SILENT)) {
609 ret = fixup_silent_linux(buf, maxlen);
611 return log_msg_ret("silent", ret);
613 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
614 (flags & BOOTM_CL_SUBST)) {
615 ret = process_subst(buf, maxlen);
617 return log_msg_ret("subst", ret);
623 int bootm_process_cmdline_env(int flags)
625 const int maxlen = MAX_CMDLINE_SIZE;
631 /* First check if any action is needed */
632 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
633 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
634 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
637 env = env_get("bootargs");
638 if (env && strlen(env) >= maxlen)
640 buf = malloc(maxlen);
647 ret = bootm_process_cmdline(buf, maxlen, flags);
649 ret = env_set("bootargs", buf);
652 * If buf is "" and bootargs does not exist, this will produce
653 * an error trying to delete bootargs. Ignore it
660 return log_msg_ret("env", ret);
666 * Execute selected states of the bootm command.
668 * Note the arguments to this state must be the first argument, Any 'bootm'
669 * or sub-command arguments must have already been taken.
671 * Note that if states contains more than one flag it MUST contain
672 * BOOTM_STATE_START, since this handles and consumes the command line args.
674 * Also note that aside from boot_os_fn functions and bootm_load_os no other
675 * functions we store the return value of in 'ret' may use a negative return
676 * value, without special handling.
678 * @param cmdtp Pointer to bootm command table entry
679 * @param flag Command flags (CMD_FLAG_...)
680 * @param argc Number of subcommand arguments (0 = no arguments)
681 * @param argv Arguments
682 * @param states Mask containing states to run (BOOTM_STATE_...)
683 * @param images Image header information
684 * @param boot_progress 1 to show boot progress, 0 to not do this
685 * Return: 0 if ok, something else on error. Some errors will cause this
686 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
687 * then the intent is to boot an OS, so this function will not return
688 * unless the image type is standalone.
690 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
691 char *const argv[], int states, struct bootm_headers *images,
696 int ret = 0, need_boot_fn;
698 images->state |= states;
701 * Work through the states and see how far we get. We stop on
704 if (states & BOOTM_STATE_START)
705 ret = bootm_start(cmdtp, flag, argc, argv);
707 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
708 ret = bootm_pre_load(cmdtp, flag, argc, argv);
710 if (!ret && (states & BOOTM_STATE_FINDOS))
711 ret = bootm_find_os(cmdtp, flag, argc, argv);
713 if (!ret && (states & BOOTM_STATE_FINDOTHER))
714 ret = bootm_find_other(cmdtp, flag, argc, argv);
717 if (!ret && (states & BOOTM_STATE_LOADOS)) {
718 iflag = bootm_disable_interrupts();
719 ret = bootm_load_os(images, 0);
720 if (ret && ret != BOOTM_ERR_OVERLAP)
722 else if (ret == BOOTM_ERR_OVERLAP)
726 /* Relocate the ramdisk */
727 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
728 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
729 ulong rd_len = images->rd_end - images->rd_start;
731 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
732 rd_len, &images->initrd_start, &images->initrd_end);
734 env_set_hex("initrd_start", images->initrd_start);
735 env_set_hex("initrd_end", images->initrd_end);
739 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
740 if (!ret && (states & BOOTM_STATE_FDT)) {
741 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
742 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
747 /* From now on, we need the OS boot function */
750 boot_fn = bootm_os_get_boot_func(images->os.os);
751 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
752 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
753 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
754 if (boot_fn == NULL && need_boot_fn) {
757 printf("ERROR: booting os '%s' (%d) is not supported\n",
758 genimg_get_os_name(images->os.os), images->os.os);
759 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
764 /* Call various other states that are not generally used */
765 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
766 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
767 if (!ret && (states & BOOTM_STATE_OS_BD_T))
768 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
769 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
770 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
772 printf("Cmdline setup failed (err=%d)\n", ret);
773 ret = CMD_RET_FAILURE;
776 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
780 /* Pretend to run the OS, then run a user command */
781 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
782 char *cmd_list = env_get("fakegocmd");
784 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
786 if (!ret && cmd_list)
787 ret = run_command_list(cmd_list, -1, flag);
791 /* Check for unsupported subcommand. */
793 printf("subcommand failed (err=%d)\n", ret);
797 /* Now run the OS! We hope this doesn't return */
798 if (!ret && (states & BOOTM_STATE_OS_GO))
799 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
802 /* Deal with any fallout */
807 if (ret == BOOTM_ERR_UNIMPLEMENTED)
808 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
809 else if (ret == BOOTM_ERR_RESET)
810 do_reset(cmdtp, flag, argc, argv);
815 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
817 * image_get_kernel - verify legacy format kernel image
818 * @img_addr: in RAM address of the legacy format image to be verified
819 * @verify: data CRC verification flag
821 * image_get_kernel() verifies legacy image integrity and returns pointer to
822 * legacy image header if image verification was completed successfully.
825 * pointer to a legacy image header if valid image was found
826 * otherwise return NULL
828 static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
830 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
832 if (!image_check_magic(hdr)) {
833 puts("Bad Magic Number\n");
834 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
837 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
839 if (!image_check_hcrc(hdr)) {
840 puts("Bad Header Checksum\n");
841 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
845 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
846 image_print_contents(hdr);
849 puts(" Verifying Checksum ... ");
850 if (!image_check_dcrc(hdr)) {
851 printf("Bad Data CRC\n");
852 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
857 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
859 if (!image_check_target_arch(hdr)) {
860 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
861 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
869 * boot_get_kernel - find kernel image
870 * @os_data: pointer to a ulong variable, will hold os data start address
871 * @os_len: pointer to a ulong variable, will hold os data length
873 * boot_get_kernel() tries to find a kernel image, verifies its integrity
874 * and locates kernel data.
877 * pointer to image header if valid image was found, plus kernel start
878 * address and length, otherwise NULL
880 static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
881 char *const argv[], struct bootm_headers *images,
882 ulong *os_data, ulong *os_len)
884 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
885 struct legacy_img_hdr *hdr;
889 const char *fit_uname_config = NULL;
890 const char *fit_uname_kernel = NULL;
891 #if CONFIG_IS_ENABLED(FIT)
895 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
899 if (CONFIG_IS_ENABLED(CMD_BOOTM_PRE_LOAD))
900 img_addr += image_load_offset;
902 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
904 /* check image type, for FIT images get FIT kernel node */
905 *os_data = *os_len = 0;
906 buf = map_sysmem(img_addr, 0);
907 switch (genimg_get_format(buf)) {
908 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
909 case IMAGE_FORMAT_LEGACY:
910 printf("## Booting kernel from Legacy Image at %08lx ...\n",
912 hdr = image_get_kernel(img_addr, images->verify);
915 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
917 /* get os_data and os_len */
918 switch (image_get_type(hdr)) {
920 case IH_TYPE_KERNEL_NOLOAD:
921 *os_data = image_get_data(hdr);
922 *os_len = image_get_data_size(hdr);
925 image_multi_getimg(hdr, 0, os_data, os_len);
927 case IH_TYPE_STANDALONE:
928 *os_data = image_get_data(hdr);
929 *os_len = image_get_data_size(hdr);
932 printf("Wrong Image Type for %s command\n",
934 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
939 * copy image header to allow for image overwrites during
940 * kernel decompression.
942 memmove(&images->legacy_hdr_os_copy, hdr,
943 sizeof(struct legacy_img_hdr));
945 /* save pointer to image header */
946 images->legacy_hdr_os = hdr;
948 images->legacy_hdr_valid = 1;
949 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
952 #if CONFIG_IS_ENABLED(FIT)
953 case IMAGE_FORMAT_FIT:
954 os_noffset = fit_image_load(images, img_addr,
955 &fit_uname_kernel, &fit_uname_config,
956 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
957 BOOTSTAGE_ID_FIT_KERNEL_START,
958 FIT_LOAD_IGNORED, os_data, os_len);
962 images->fit_hdr_os = map_sysmem(img_addr, 0);
963 images->fit_uname_os = fit_uname_kernel;
964 images->fit_uname_cfg = fit_uname_config;
965 images->fit_noffset_os = os_noffset;
968 #ifdef CONFIG_ANDROID_BOOT_IMAGE
969 case IMAGE_FORMAT_ANDROID:
970 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
971 if (android_image_get_kernel(buf, images->verify,
977 printf("Wrong Image Format for %s command\n", cmdtp->name);
978 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
982 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
983 *os_data, *os_len, *os_len);
989 * switch_to_non_secure_mode() - switch to non-secure mode
991 * This routine is overridden by architectures requiring this feature.
993 void __weak switch_to_non_secure_mode(void)
997 #else /* USE_HOSTCC */
999 #if defined(CONFIG_FIT_SIGNATURE)
1000 static int bootm_host_load_image(const void *fit, int req_image_type,
1003 const char *fit_uname_config = NULL;
1005 struct bootm_headers images;
1007 ulong load_end, buf_size;
1013 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1014 memset(&images, '\0', sizeof(images));
1016 noffset = fit_image_load(&images, (ulong)fit,
1017 NULL, &fit_uname_config,
1018 IH_ARCH_DEFAULT, req_image_type, -1,
1019 FIT_LOAD_IGNORED, &data, &len);
1022 if (fit_image_get_type(fit, noffset, &image_type)) {
1023 puts("Can't get image type!\n");
1027 if (fit_image_get_comp(fit, noffset, &image_comp))
1028 image_comp = IH_COMP_NONE;
1030 /* Allow the image to expand by a factor of 4, should be safe */
1031 buf_size = (1 << 20) + len * 4;
1032 load_buf = malloc(buf_size);
1033 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
1034 (void *)data, len, buf_size, &load_end);
1038 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
1039 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1046 int bootm_host_load_images(const void *fit, int cfg_noffset)
1048 static uint8_t image_types[] = {
1056 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1059 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
1060 if (!err && ret && ret != -ENOENT)
1064 /* Return the first error we found */
1069 #endif /* ndef USE_HOSTCC */