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 (IS_ENABLED(CONFIG_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 #ifdef CONFIG_ANDROID_BOOT_IMAGE
117 const void *vendor_boot_img;
118 const void *boot_img;
120 bool ep_found = false;
123 /* get kernel image header, start address and length */
124 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
125 &images, &images.os.image_start, &images.os.image_len);
126 if (images.os.image_len == 0) {
127 puts("ERROR: can't get kernel image!\n");
131 /* get image parameters */
132 switch (genimg_get_format(os_hdr)) {
133 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
134 case IMAGE_FORMAT_LEGACY:
135 images.os.type = image_get_type(os_hdr);
136 images.os.comp = image_get_comp(os_hdr);
137 images.os.os = image_get_os(os_hdr);
139 images.os.end = image_get_image_end(os_hdr);
140 images.os.load = image_get_load(os_hdr);
141 images.os.arch = image_get_arch(os_hdr);
144 #if CONFIG_IS_ENABLED(FIT)
145 case IMAGE_FORMAT_FIT:
146 if (fit_image_get_type(images.fit_hdr_os,
147 images.fit_noffset_os,
149 puts("Can't get image type!\n");
150 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
154 if (fit_image_get_comp(images.fit_hdr_os,
155 images.fit_noffset_os,
157 puts("Can't get image compression!\n");
158 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
162 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
164 puts("Can't get image OS!\n");
165 bootstage_error(BOOTSTAGE_ID_FIT_OS);
169 if (fit_image_get_arch(images.fit_hdr_os,
170 images.fit_noffset_os,
172 puts("Can't get image ARCH!\n");
176 images.os.end = fit_get_end(images.fit_hdr_os);
178 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
180 puts("Can't get image load address!\n");
181 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
186 #ifdef CONFIG_ANDROID_BOOT_IMAGE
187 case IMAGE_FORMAT_ANDROID:
189 vendor_boot_img = NULL;
190 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
191 boot_img = map_sysmem(get_abootimg_addr(), 0);
192 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
194 images.os.type = IH_TYPE_KERNEL;
195 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
196 images.os.os = IH_OS_LINUX;
197 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
198 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
199 images.ep = images.os.load;
201 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
202 unmap_sysmem(vendor_boot_img);
203 unmap_sysmem(boot_img);
208 puts("ERROR: unknown image format type!\n");
212 /* If we have a valid setup.bin, we will use that for entry (x86) */
213 if (images.os.arch == IH_ARCH_I386 ||
214 images.os.arch == IH_ARCH_X86_64) {
217 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
218 if (ret < 0 && ret != -ENOENT) {
219 puts("Could not find a valid setup.bin for x86\n");
222 /* Kernel entry point is the setup.bin */
223 } else if (images.legacy_hdr_valid) {
224 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
225 #if CONFIG_IS_ENABLED(FIT)
226 } else if (images.fit_uname_os) {
229 ret = fit_image_get_entry(images.fit_hdr_os,
230 images.fit_noffset_os, &images.ep);
232 puts("Can't get entry point property!\n");
236 } else if (!ep_found) {
237 puts("Could not find kernel entry point!\n");
241 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
242 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
243 images.os.arch == IH_ARCH_ARM64) {
247 ret = booti_setup(images.os.image_start, &image_addr,
252 images.os.type = IH_TYPE_KERNEL;
253 images.os.load = image_addr;
254 images.ep = image_addr;
256 images.os.load = images.os.image_start;
257 images.ep += images.os.image_start;
261 images.os.start = map_to_sysmem(os_hdr);
267 * bootm_find_images - wrapper to find and locate various images
268 * @flag: Ignored Argument
269 * @argc: command argument count
270 * @argv: command argument list
271 * @start: OS image start address
272 * @size: OS image size
274 * boot_find_images() will attempt to load an available ramdisk,
275 * flattened device tree, as well as specifically marked
276 * "loadable" images (loadables are FIT only)
278 * Note: bootm_find_images will skip an image if it is not found
281 * 0, if all existing images were loaded correctly
282 * 1, if an image is found but corrupted, or invalid
284 int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
290 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
291 &images.rd_start, &images.rd_end);
293 puts("Ramdisk image is corrupt or invalid\n");
297 /* check if ramdisk overlaps OS image */
298 if (images.rd_start && (((ulong)images.rd_start >= start &&
299 (ulong)images.rd_start < start + size) ||
300 ((ulong)images.rd_end > start &&
301 (ulong)images.rd_end <= start + size) ||
302 ((ulong)images.rd_start < start &&
303 (ulong)images.rd_end >= start + size))) {
304 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
305 start, start + size);
309 #if CONFIG_IS_ENABLED(OF_LIBFDT)
310 /* find flattened device tree */
311 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
312 &images.ft_addr, &images.ft_len);
314 puts("Could not find a valid device tree\n");
318 /* check if FDT overlaps OS image */
319 if (images.ft_addr &&
320 (((ulong)images.ft_addr >= start &&
321 (ulong)images.ft_addr < start + size) ||
322 ((ulong)images.ft_addr + images.ft_len >= start &&
323 (ulong)images.ft_addr + images.ft_len < start + size))) {
324 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
325 start, start + size);
329 if (IS_ENABLED(CONFIG_CMD_FDT))
330 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
333 #if CONFIG_IS_ENABLED(FIT)
334 if (IS_ENABLED(CONFIG_FPGA)) {
335 /* find bitstreams */
336 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
339 printf("FPGA image is corrupted or invalid\n");
344 /* find all of the loadables */
345 ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
348 printf("Loadable(s) is corrupt or invalid\n");
356 static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
359 if (((images.os.type == IH_TYPE_KERNEL) ||
360 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
361 (images.os.type == IH_TYPE_MULTI)) &&
362 (images.os.os == IH_OS_LINUX ||
363 images.os.os == IH_OS_VXWORKS))
364 return bootm_find_images(flag, argc, argv, 0, 0);
368 #endif /* USE_HOSTC */
370 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
372 * handle_decomp_error() - display a decompression error
374 * This function tries to produce a useful message. In the case where the
375 * uncompressed size is the same as the available space, we can assume that
376 * the image is too large for the buffer.
378 * @comp_type: Compression type being used (IH_COMP_...)
379 * @uncomp_size: Number of bytes uncompressed
380 * @buf_size: Number of bytes the decompresion buffer was
381 * @ret: errno error code received from compression library
382 * Return: Appropriate BOOTM_ERR_ error code
384 static int handle_decomp_error(int comp_type, size_t uncomp_size,
385 size_t buf_size, int ret)
387 const char *name = genimg_get_comp_name(comp_type);
389 /* ENOSYS means unimplemented compression type, don't reset. */
391 return BOOTM_ERR_UNIMPLEMENTED;
393 if (uncomp_size >= buf_size)
394 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
396 printf("%s: uncompress error %d\n", name, ret);
399 * The decompression routines are now safe, so will not write beyond
400 * their bounds. Probably it is not necessary to reset, but maintain
401 * the current behaviour for now.
403 printf("Must RESET board to recover\n");
405 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
408 return BOOTM_ERR_RESET;
413 static int bootm_load_os(struct bootm_headers *images, int boot_progress)
415 struct image_info os = images->os;
416 ulong load = os.load;
418 ulong blob_start = os.start;
419 ulong blob_end = os.end;
420 ulong image_start = os.image_start;
421 ulong image_len = os.image_len;
422 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
424 void *load_buf, *image_buf;
427 load_buf = map_sysmem(load, 0);
428 image_buf = map_sysmem(os.image_start, image_len);
429 err = image_decomp(os.comp, load, os.image_start, os.type,
430 load_buf, image_buf, image_len,
431 CONFIG_SYS_BOOTM_LEN, &load_end);
433 err = handle_decomp_error(os.comp, load_end - load,
434 CONFIG_SYS_BOOTM_LEN, err);
435 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
438 /* We need the decompressed image size in the next steps */
439 images->os.image_len = load_end - load;
441 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
443 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
444 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
446 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
448 if (!no_overlap && load < blob_end && load_end > blob_start) {
449 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
450 blob_start, blob_end);
451 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
454 /* Check what type of image this is. */
455 if (images->legacy_hdr_valid) {
456 if (image_get_type(&images->legacy_hdr_os_copy)
458 puts("WARNING: legacy format multi component image overwritten\n");
459 return BOOTM_ERR_OVERLAP;
461 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
462 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
463 return BOOTM_ERR_RESET;
467 lmb_reserve(&images->lmb, images->os.load, (load_end -
473 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
475 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
478 ulong bootm_disable_interrupts(void)
483 * We have reached the point of no return: we are going to
484 * overwrite all exception vector code, so we cannot easily
485 * recover from any failures any more...
487 iflag = disable_interrupts();
488 #ifdef CONFIG_NETCONSOLE
489 /* Stop the ethernet stack if NetConsole could have left it up */
493 #if defined(CONFIG_CMD_USB)
495 * turn off USB to prevent the host controller from writing to the
496 * SDRAM while Linux is booting. This could happen (at least for OHCI
497 * controller), because the HCCA (Host Controller Communication Area)
498 * lies within the SDRAM and the host controller writes continously to
499 * this area (as busmaster!). The HccaFrameNumber is for example
500 * updated every 1 ms within the HCCA structure in SDRAM! For more
501 * details see the OpenHCI specification.
508 #define CONSOLE_ARG "console="
509 #define NULL_CONSOLE (CONSOLE_ARG "ttynull")
510 #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
513 * fixup_silent_linux() - Handle silencing the linux boot if required
515 * This uses the silent_linux envvar to control whether to add/set a "console="
516 * parameter to the command line
518 * @buf: Buffer containing the string to process
519 * @maxlen: Maximum length of buffer
520 * Return: 0 if OK, -ENOSPC if @maxlen is too small
522 static int fixup_silent_linux(char *buf, int maxlen)
529 * Move the input string to the end of buffer. The output string will be
530 * built up at the start.
532 size = strlen(buf) + 1;
533 if (size * 2 > maxlen)
535 cmdline = buf + maxlen - size;
536 memmove(cmdline, buf, size);
538 * Only fix cmdline when requested. The environment variable can be:
540 * no - we never fixup
541 * yes - we always fixup
542 * unset - we rely on the console silent flag
544 want_silent = env_get_yesno("silent_linux");
545 if (want_silent == 0)
547 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
550 debug("before silent fix-up: %s\n", cmdline);
552 char *start = strstr(cmdline, CONSOLE_ARG);
554 /* Check space for maximum possible new command line */
555 if (size + CONSOLE_ARG_SIZE > maxlen)
559 char *end = strchr(start, ' ');
562 start_bytes = start - cmdline;
563 strncpy(buf, cmdline, start_bytes);
564 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
566 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
568 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
570 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
572 if (buf + strlen(buf) >= cmdline)
575 if (maxlen < CONSOLE_ARG_SIZE)
577 strcpy(buf, NULL_CONSOLE);
579 debug("after silent fix-up: %s\n", buf);
585 * process_subst() - Handle substitution of ${...} fields in the environment
587 * Handle variable substitution in the provided buffer
589 * @buf: Buffer containing the string to process
590 * @maxlen: Maximum length of buffer
591 * Return: 0 if OK, -ENOSPC if @maxlen is too small
593 static int process_subst(char *buf, int maxlen)
599 /* Move to end of buffer */
600 size = strlen(buf) + 1;
601 cmdline = buf + maxlen - size;
602 if (buf + size > cmdline)
604 memmove(cmdline, buf, size);
606 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
611 int bootm_process_cmdline(char *buf, int maxlen, int flags)
615 /* Check config first to enable compiler to eliminate code */
616 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
617 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
618 (flags & BOOTM_CL_SILENT)) {
619 ret = fixup_silent_linux(buf, maxlen);
621 return log_msg_ret("silent", ret);
623 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
624 (flags & BOOTM_CL_SUBST)) {
625 ret = process_subst(buf, maxlen);
627 return log_msg_ret("subst", ret);
633 int bootm_process_cmdline_env(int flags)
635 const int maxlen = MAX_CMDLINE_SIZE;
641 /* First check if any action is needed */
642 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
643 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
644 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
647 env = env_get("bootargs");
648 if (env && strlen(env) >= maxlen)
650 buf = malloc(maxlen);
657 ret = bootm_process_cmdline(buf, maxlen, flags);
659 ret = env_set("bootargs", buf);
662 * If buf is "" and bootargs does not exist, this will produce
663 * an error trying to delete bootargs. Ignore it
670 return log_msg_ret("env", ret);
676 * Execute selected states of the bootm command.
678 * Note the arguments to this state must be the first argument, Any 'bootm'
679 * or sub-command arguments must have already been taken.
681 * Note that if states contains more than one flag it MUST contain
682 * BOOTM_STATE_START, since this handles and consumes the command line args.
684 * Also note that aside from boot_os_fn functions and bootm_load_os no other
685 * functions we store the return value of in 'ret' may use a negative return
686 * value, without special handling.
688 * @param cmdtp Pointer to bootm command table entry
689 * @param flag Command flags (CMD_FLAG_...)
690 * @param argc Number of subcommand arguments (0 = no arguments)
691 * @param argv Arguments
692 * @param states Mask containing states to run (BOOTM_STATE_...)
693 * @param images Image header information
694 * @param boot_progress 1 to show boot progress, 0 to not do this
695 * Return: 0 if ok, something else on error. Some errors will cause this
696 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
697 * then the intent is to boot an OS, so this function will not return
698 * unless the image type is standalone.
700 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
701 char *const argv[], int states, struct bootm_headers *images,
706 int ret = 0, need_boot_fn;
708 images->state |= states;
711 * Work through the states and see how far we get. We stop on
714 if (states & BOOTM_STATE_START)
715 ret = bootm_start(cmdtp, flag, argc, argv);
717 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
718 ret = bootm_pre_load(cmdtp, flag, argc, argv);
720 if (!ret && (states & BOOTM_STATE_FINDOS))
721 ret = bootm_find_os(cmdtp, flag, argc, argv);
723 if (!ret && (states & BOOTM_STATE_FINDOTHER))
724 ret = bootm_find_other(cmdtp, flag, argc, argv);
727 if (!ret && (states & BOOTM_STATE_LOADOS)) {
728 iflag = bootm_disable_interrupts();
729 ret = bootm_load_os(images, 0);
730 if (ret && ret != BOOTM_ERR_OVERLAP)
732 else if (ret == BOOTM_ERR_OVERLAP)
736 /* Relocate the ramdisk */
737 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
738 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
739 ulong rd_len = images->rd_end - images->rd_start;
741 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
742 rd_len, &images->initrd_start, &images->initrd_end);
744 env_set_hex("initrd_start", images->initrd_start);
745 env_set_hex("initrd_end", images->initrd_end);
749 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
750 if (!ret && (states & BOOTM_STATE_FDT)) {
751 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
752 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
757 /* From now on, we need the OS boot function */
760 boot_fn = bootm_os_get_boot_func(images->os.os);
761 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
762 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
763 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
764 if (boot_fn == NULL && need_boot_fn) {
767 printf("ERROR: booting os '%s' (%d) is not supported\n",
768 genimg_get_os_name(images->os.os), images->os.os);
769 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
774 /* Call various other states that are not generally used */
775 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
776 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
777 if (!ret && (states & BOOTM_STATE_OS_BD_T))
778 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
779 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
780 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
782 printf("Cmdline setup failed (err=%d)\n", ret);
783 ret = CMD_RET_FAILURE;
786 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
790 /* Pretend to run the OS, then run a user command */
791 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
792 char *cmd_list = env_get("fakegocmd");
794 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
796 if (!ret && cmd_list)
797 ret = run_command_list(cmd_list, -1, flag);
801 /* Check for unsupported subcommand. */
803 printf("subcommand failed (err=%d)\n", ret);
807 /* Now run the OS! We hope this doesn't return */
808 if (!ret && (states & BOOTM_STATE_OS_GO))
809 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
812 /* Deal with any fallout */
817 if (ret == BOOTM_ERR_UNIMPLEMENTED)
818 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
819 else if (ret == BOOTM_ERR_RESET)
820 do_reset(cmdtp, flag, argc, argv);
825 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
827 * image_get_kernel - verify legacy format kernel image
828 * @img_addr: in RAM address of the legacy format image to be verified
829 * @verify: data CRC verification flag
831 * image_get_kernel() verifies legacy image integrity and returns pointer to
832 * legacy image header if image verification was completed successfully.
835 * pointer to a legacy image header if valid image was found
836 * otherwise return NULL
838 static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
840 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
842 if (!image_check_magic(hdr)) {
843 puts("Bad Magic Number\n");
844 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
847 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
849 if (!image_check_hcrc(hdr)) {
850 puts("Bad Header Checksum\n");
851 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
855 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
856 image_print_contents(hdr);
859 puts(" Verifying Checksum ... ");
860 if (!image_check_dcrc(hdr)) {
861 printf("Bad Data CRC\n");
862 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
867 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
869 if (!image_check_target_arch(hdr)) {
870 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
871 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
879 * boot_get_kernel - find kernel image
880 * @os_data: pointer to a ulong variable, will hold os data start address
881 * @os_len: pointer to a ulong variable, will hold os data length
883 * boot_get_kernel() tries to find a kernel image, verifies its integrity
884 * and locates kernel data.
887 * pointer to image header if valid image was found, plus kernel start
888 * address and length, otherwise NULL
890 static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
891 char *const argv[], struct bootm_headers *images,
892 ulong *os_data, ulong *os_len)
894 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
895 struct legacy_img_hdr *hdr;
899 const char *fit_uname_config = NULL;
900 const char *fit_uname_kernel = NULL;
901 #if CONFIG_IS_ENABLED(FIT)
905 #ifdef CONFIG_ANDROID_BOOT_IMAGE
906 const void *boot_img;
907 const void *vendor_boot_img;
909 img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
913 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
914 img_addr += image_load_offset;
916 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
918 /* check image type, for FIT images get FIT kernel node */
919 *os_data = *os_len = 0;
920 buf = map_sysmem(img_addr, 0);
921 switch (genimg_get_format(buf)) {
922 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
923 case IMAGE_FORMAT_LEGACY:
924 printf("## Booting kernel from Legacy Image at %08lx ...\n",
926 hdr = image_get_kernel(img_addr, images->verify);
929 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
931 /* get os_data and os_len */
932 switch (image_get_type(hdr)) {
934 case IH_TYPE_KERNEL_NOLOAD:
935 *os_data = image_get_data(hdr);
936 *os_len = image_get_data_size(hdr);
939 image_multi_getimg(hdr, 0, os_data, os_len);
941 case IH_TYPE_STANDALONE:
942 *os_data = image_get_data(hdr);
943 *os_len = image_get_data_size(hdr);
946 printf("Wrong Image Type for %s command\n",
948 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
953 * copy image header to allow for image overwrites during
954 * kernel decompression.
956 memmove(&images->legacy_hdr_os_copy, hdr,
957 sizeof(struct legacy_img_hdr));
959 /* save pointer to image header */
960 images->legacy_hdr_os = hdr;
962 images->legacy_hdr_valid = 1;
963 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
966 #if CONFIG_IS_ENABLED(FIT)
967 case IMAGE_FORMAT_FIT:
968 os_noffset = fit_image_load(images, img_addr,
969 &fit_uname_kernel, &fit_uname_config,
970 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
971 BOOTSTAGE_ID_FIT_KERNEL_START,
972 FIT_LOAD_IGNORED, os_data, os_len);
976 images->fit_hdr_os = map_sysmem(img_addr, 0);
977 images->fit_uname_os = fit_uname_kernel;
978 images->fit_uname_cfg = fit_uname_config;
979 images->fit_noffset_os = os_noffset;
982 #ifdef CONFIG_ANDROID_BOOT_IMAGE
983 case IMAGE_FORMAT_ANDROID:
985 vendor_boot_img = NULL;
986 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
987 boot_img = map_sysmem(get_abootimg_addr(), 0);
988 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
990 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
991 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
994 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
995 unmap_sysmem(vendor_boot_img);
996 unmap_sysmem(boot_img);
1001 printf("Wrong Image Format for %s command\n", cmdtp->name);
1002 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1006 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1007 *os_data, *os_len, *os_len);
1013 * switch_to_non_secure_mode() - switch to non-secure mode
1015 * This routine is overridden by architectures requiring this feature.
1017 void __weak switch_to_non_secure_mode(void)
1021 #else /* USE_HOSTCC */
1023 #if defined(CONFIG_FIT_SIGNATURE)
1024 static int bootm_host_load_image(const void *fit, int req_image_type,
1027 const char *fit_uname_config = NULL;
1029 struct bootm_headers images;
1031 ulong load_end, buf_size;
1037 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1038 memset(&images, '\0', sizeof(images));
1040 noffset = fit_image_load(&images, (ulong)fit,
1041 NULL, &fit_uname_config,
1042 IH_ARCH_DEFAULT, req_image_type, -1,
1043 FIT_LOAD_IGNORED, &data, &len);
1046 if (fit_image_get_type(fit, noffset, &image_type)) {
1047 puts("Can't get image type!\n");
1051 if (fit_image_get_comp(fit, noffset, &image_comp))
1052 image_comp = IH_COMP_NONE;
1054 /* Allow the image to expand by a factor of 4, should be safe */
1055 buf_size = (1 << 20) + len * 4;
1056 load_buf = malloc(buf_size);
1057 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
1058 (void *)data, len, buf_size, &load_end);
1062 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
1063 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1070 int bootm_host_load_images(const void *fit, int cfg_noffset)
1072 static uint8_t image_types[] = {
1080 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1083 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
1084 if (!err && ret && ret != -ENOENT)
1088 /* Return the first error we found */
1093 #endif /* ndef USE_HOSTCC */