1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #include <fdt_support.h>
22 #include <asm/cache.h>
23 #include <asm/global_data.h>
25 #include <linux/sizes.h>
27 #if defined(CONFIG_CMD_USB)
37 #define MAX_CMDLINE_SIZE SZ_4K
39 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
43 DECLARE_GLOBAL_DATA_PTR
;
45 struct bootm_headers images
; /* pointers to os/initrd/fdt images */
47 static const void *boot_get_kernel(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
48 char *const argv
[], struct bootm_headers
*images
,
49 ulong
*os_data
, ulong
*os_len
);
51 __weak
void board_quiesce_devices(void)
56 static void boot_start_lmb(struct bootm_headers
*images
)
61 mem_start
= env_get_bootm_low();
62 mem_size
= env_get_bootm_size();
64 lmb_init_and_reserve_range(&images
->lmb
, (phys_addr_t
)mem_start
,
68 #define lmb_reserve(lmb, base, size)
69 static inline void boot_start_lmb(struct bootm_headers
*images
) { }
72 static int bootm_start(void)
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(const char *addr_str
)
90 addr
= hextoul(addr_str
, NULL
);
92 addr
= image_load_addr
;
98 * bootm_pre_load() - Handle the pre-load processing
100 * This can be used to do a full signature check of the image, for example.
101 * It calls image_pre_load() with the data address of the image to check.
103 * @addr_str: String containing load address in hex, or NULL to use
105 * Return: 0 if OK, CMD_RET_FAILURE on failure
107 static int bootm_pre_load(const char *addr_str
)
109 ulong data_addr
= bootm_data_addr(addr_str
);
112 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD
))
113 ret
= image_pre_load(data_addr
);
116 ret
= CMD_RET_FAILURE
;
121 static int bootm_find_os(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
125 #ifdef CONFIG_ANDROID_BOOT_IMAGE
126 const void *vendor_boot_img
;
127 const void *boot_img
;
129 bool ep_found
= false;
132 /* get kernel image header, start address and length */
133 os_hdr
= boot_get_kernel(cmdtp
, flag
, argc
, argv
,
134 &images
, &images
.os
.image_start
, &images
.os
.image_len
);
135 if (images
.os
.image_len
== 0) {
136 puts("ERROR: can't get kernel image!\n");
140 /* get image parameters */
141 switch (genimg_get_format(os_hdr
)) {
142 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
143 case IMAGE_FORMAT_LEGACY
:
144 images
.os
.type
= image_get_type(os_hdr
);
145 images
.os
.comp
= image_get_comp(os_hdr
);
146 images
.os
.os
= image_get_os(os_hdr
);
148 images
.os
.end
= image_get_image_end(os_hdr
);
149 images
.os
.load
= image_get_load(os_hdr
);
150 images
.os
.arch
= image_get_arch(os_hdr
);
153 #if CONFIG_IS_ENABLED(FIT)
154 case IMAGE_FORMAT_FIT
:
155 if (fit_image_get_type(images
.fit_hdr_os
,
156 images
.fit_noffset_os
,
158 puts("Can't get image type!\n");
159 bootstage_error(BOOTSTAGE_ID_FIT_TYPE
);
163 if (fit_image_get_comp(images
.fit_hdr_os
,
164 images
.fit_noffset_os
,
166 puts("Can't get image compression!\n");
167 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION
);
171 if (fit_image_get_os(images
.fit_hdr_os
, images
.fit_noffset_os
,
173 puts("Can't get image OS!\n");
174 bootstage_error(BOOTSTAGE_ID_FIT_OS
);
178 if (fit_image_get_arch(images
.fit_hdr_os
,
179 images
.fit_noffset_os
,
181 puts("Can't get image ARCH!\n");
185 images
.os
.end
= fit_get_end(images
.fit_hdr_os
);
187 if (fit_image_get_load(images
.fit_hdr_os
, images
.fit_noffset_os
,
189 puts("Can't get image load address!\n");
190 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR
);
195 #ifdef CONFIG_ANDROID_BOOT_IMAGE
196 case IMAGE_FORMAT_ANDROID
:
198 vendor_boot_img
= NULL
;
199 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
200 boot_img
= map_sysmem(get_abootimg_addr(), 0);
201 vendor_boot_img
= map_sysmem(get_avendor_bootimg_addr(), 0);
203 images
.os
.type
= IH_TYPE_KERNEL
;
204 images
.os
.comp
= android_image_get_kcomp(boot_img
, vendor_boot_img
);
205 images
.os
.os
= IH_OS_LINUX
;
206 images
.os
.end
= android_image_get_end(boot_img
, vendor_boot_img
);
207 images
.os
.load
= android_image_get_kload(boot_img
, vendor_boot_img
);
208 images
.ep
= images
.os
.load
;
210 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
211 unmap_sysmem(vendor_boot_img
);
212 unmap_sysmem(boot_img
);
217 puts("ERROR: unknown image format type!\n");
221 /* If we have a valid setup.bin, we will use that for entry (x86) */
222 if (images
.os
.arch
== IH_ARCH_I386
||
223 images
.os
.arch
== IH_ARCH_X86_64
) {
226 ret
= boot_get_setup(&images
, IH_ARCH_I386
, &images
.ep
, &len
);
227 if (ret
< 0 && ret
!= -ENOENT
) {
228 puts("Could not find a valid setup.bin for x86\n");
231 /* Kernel entry point is the setup.bin */
232 } else if (images
.legacy_hdr_valid
) {
233 images
.ep
= image_get_ep(&images
.legacy_hdr_os_copy
);
234 #if CONFIG_IS_ENABLED(FIT)
235 } else if (images
.fit_uname_os
) {
238 ret
= fit_image_get_entry(images
.fit_hdr_os
,
239 images
.fit_noffset_os
, &images
.ep
);
241 puts("Can't get entry point property!\n");
245 } else if (!ep_found
) {
246 puts("Could not find kernel entry point!\n");
250 if (images
.os
.type
== IH_TYPE_KERNEL_NOLOAD
) {
251 if (IS_ENABLED(CONFIG_CMD_BOOTI
) &&
252 images
.os
.arch
== IH_ARCH_ARM64
&&
253 images
.os
.os
== IH_OS_LINUX
) {
257 ret
= booti_setup(images
.os
.image_start
, &image_addr
,
262 images
.os
.type
= IH_TYPE_KERNEL
;
263 images
.os
.load
= image_addr
;
264 images
.ep
= image_addr
;
266 images
.os
.load
= images
.os
.image_start
;
267 images
.ep
+= images
.os
.image_start
;
271 images
.os
.start
= map_to_sysmem(os_hdr
);
277 * bootm_find_images - wrapper to find and locate various images
278 * @flag: Ignored Argument
279 * @argc: command argument count
280 * @argv: command argument list
281 * @start: OS image start address
282 * @size: OS image size
284 * boot_find_images() will attempt to load an available ramdisk,
285 * flattened device tree, as well as specifically marked
286 * "loadable" images (loadables are FIT only)
288 * Note: bootm_find_images will skip an image if it is not found
291 * 0, if all existing images were loaded correctly
292 * 1, if an image is found but corrupted, or invalid
294 int bootm_find_images(int flag
, int argc
, char *const argv
[], ulong start
,
300 ret
= boot_get_ramdisk(argc
, argv
, &images
, IH_INITRD_ARCH
,
301 &images
.rd_start
, &images
.rd_end
);
303 puts("Ramdisk image is corrupt or invalid\n");
307 /* check if ramdisk overlaps OS image */
308 if (images
.rd_start
&& (((ulong
)images
.rd_start
>= start
&&
309 (ulong
)images
.rd_start
< start
+ size
) ||
310 ((ulong
)images
.rd_end
> start
&&
311 (ulong
)images
.rd_end
<= start
+ size
) ||
312 ((ulong
)images
.rd_start
< start
&&
313 (ulong
)images
.rd_end
>= start
+ size
))) {
314 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
315 start
, start
+ size
);
319 #if CONFIG_IS_ENABLED(OF_LIBFDT)
320 /* find flattened device tree */
321 ret
= boot_get_fdt(flag
, argc
, argv
, IH_ARCH_DEFAULT
, &images
,
322 &images
.ft_addr
, &images
.ft_len
);
324 puts("Could not find a valid device tree\n");
328 /* check if FDT overlaps OS image */
329 if (images
.ft_addr
&&
330 (((ulong
)images
.ft_addr
>= start
&&
331 (ulong
)images
.ft_addr
< start
+ size
) ||
332 ((ulong
)images
.ft_addr
+ images
.ft_len
>= start
&&
333 (ulong
)images
.ft_addr
+ images
.ft_len
< start
+ size
))) {
334 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
335 start
, start
+ size
);
339 if (IS_ENABLED(CONFIG_CMD_FDT
))
340 set_working_fdt_addr(map_to_sysmem(images
.ft_addr
));
343 #if CONFIG_IS_ENABLED(FIT)
344 if (IS_ENABLED(CONFIG_FPGA
)) {
345 /* find bitstreams */
346 ret
= boot_get_fpga(argc
, argv
, &images
, IH_ARCH_DEFAULT
,
349 printf("FPGA image is corrupted or invalid\n");
354 /* find all of the loadables */
355 ret
= boot_get_loadable(argc
, argv
, &images
, IH_ARCH_DEFAULT
,
358 printf("Loadable(s) is corrupt or invalid\n");
366 static int bootm_find_other(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
369 if (((images
.os
.type
== IH_TYPE_KERNEL
) ||
370 (images
.os
.type
== IH_TYPE_KERNEL_NOLOAD
) ||
371 (images
.os
.type
== IH_TYPE_MULTI
)) &&
372 (images
.os
.os
== IH_OS_LINUX
||
373 images
.os
.os
== IH_OS_VXWORKS
))
374 return bootm_find_images(flag
, argc
, argv
, 0, 0);
378 #endif /* USE_HOSTC */
380 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
382 * handle_decomp_error() - display a decompression error
384 * This function tries to produce a useful message. In the case where the
385 * uncompressed size is the same as the available space, we can assume that
386 * the image is too large for the buffer.
388 * @comp_type: Compression type being used (IH_COMP_...)
389 * @uncomp_size: Number of bytes uncompressed
390 * @buf_size: Number of bytes the decompresion buffer was
391 * @ret: errno error code received from compression library
392 * Return: Appropriate BOOTM_ERR_ error code
394 static int handle_decomp_error(int comp_type
, size_t uncomp_size
,
395 size_t buf_size
, int ret
)
397 const char *name
= genimg_get_comp_name(comp_type
);
399 /* ENOSYS means unimplemented compression type, don't reset. */
401 return BOOTM_ERR_UNIMPLEMENTED
;
403 if (uncomp_size
>= buf_size
)
404 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
406 printf("%s: uncompress error %d\n", name
, ret
);
409 * The decompression routines are now safe, so will not write beyond
410 * their bounds. Probably it is not necessary to reset, but maintain
411 * the current behaviour for now.
413 printf("Must RESET board to recover\n");
415 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE
);
418 return BOOTM_ERR_RESET
;
423 static int bootm_load_os(struct bootm_headers
*images
, int boot_progress
)
425 struct image_info os
= images
->os
;
426 ulong load
= os
.load
;
428 ulong blob_start
= os
.start
;
429 ulong blob_end
= os
.end
;
430 ulong image_start
= os
.image_start
;
431 ulong image_len
= os
.image_len
;
432 ulong flush_start
= ALIGN_DOWN(load
, ARCH_DMA_MINALIGN
);
434 void *load_buf
, *image_buf
;
437 load_buf
= map_sysmem(load
, 0);
438 image_buf
= map_sysmem(os
.image_start
, image_len
);
439 err
= image_decomp(os
.comp
, load
, os
.image_start
, os
.type
,
440 load_buf
, image_buf
, image_len
,
441 CONFIG_SYS_BOOTM_LEN
, &load_end
);
443 err
= handle_decomp_error(os
.comp
, load_end
- load
,
444 CONFIG_SYS_BOOTM_LEN
, err
);
445 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE
);
448 /* We need the decompressed image size in the next steps */
449 images
->os
.image_len
= load_end
- load
;
451 flush_cache(flush_start
, ALIGN(load_end
, ARCH_DMA_MINALIGN
) - flush_start
);
453 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load
, load_end
);
454 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED
);
456 no_overlap
= (os
.comp
== IH_COMP_NONE
&& load
== image_start
);
458 if (!no_overlap
&& load
< blob_end
&& load_end
> blob_start
) {
459 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
460 blob_start
, blob_end
);
461 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load
,
464 /* Check what type of image this is. */
465 if (images
->legacy_hdr_valid
) {
466 if (image_get_type(&images
->legacy_hdr_os_copy
)
468 puts("WARNING: legacy format multi component image overwritten\n");
469 return BOOTM_ERR_OVERLAP
;
471 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
472 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN
);
473 return BOOTM_ERR_RESET
;
477 lmb_reserve(&images
->lmb
, images
->os
.load
, (load_end
-
483 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
485 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
488 ulong
bootm_disable_interrupts(void)
493 * We have reached the point of no return: we are going to
494 * overwrite all exception vector code, so we cannot easily
495 * recover from any failures any more...
497 iflag
= disable_interrupts();
498 #ifdef CONFIG_NETCONSOLE
499 /* Stop the ethernet stack if NetConsole could have left it up */
503 #if defined(CONFIG_CMD_USB)
505 * turn off USB to prevent the host controller from writing to the
506 * SDRAM while Linux is booting. This could happen (at least for OHCI
507 * controller), because the HCCA (Host Controller Communication Area)
508 * lies within the SDRAM and the host controller writes continously to
509 * this area (as busmaster!). The HccaFrameNumber is for example
510 * updated every 1 ms within the HCCA structure in SDRAM! For more
511 * details see the OpenHCI specification.
518 #define CONSOLE_ARG "console="
519 #define NULL_CONSOLE (CONSOLE_ARG "ttynull")
520 #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
523 * fixup_silent_linux() - Handle silencing the linux boot if required
525 * This uses the silent_linux envvar to control whether to add/set a "console="
526 * parameter to the command line
528 * @buf: Buffer containing the string to process
529 * @maxlen: Maximum length of buffer
530 * Return: 0 if OK, -ENOSPC if @maxlen is too small
532 static int fixup_silent_linux(char *buf
, int maxlen
)
539 * Move the input string to the end of buffer. The output string will be
540 * built up at the start.
542 size
= strlen(buf
) + 1;
543 if (size
* 2 > maxlen
)
545 cmdline
= buf
+ maxlen
- size
;
546 memmove(cmdline
, buf
, size
);
548 * Only fix cmdline when requested. The environment variable can be:
550 * no - we never fixup
551 * yes - we always fixup
552 * unset - we rely on the console silent flag
554 want_silent
= env_get_yesno("silent_linux");
555 if (want_silent
== 0)
557 else if (want_silent
== -1 && !(gd
->flags
& GD_FLG_SILENT
))
560 debug("before silent fix-up: %s\n", cmdline
);
562 char *start
= strstr(cmdline
, CONSOLE_ARG
);
564 /* Check space for maximum possible new command line */
565 if (size
+ CONSOLE_ARG_SIZE
> maxlen
)
569 char *end
= strchr(start
, ' ');
572 start_bytes
= start
- cmdline
;
573 strncpy(buf
, cmdline
, start_bytes
);
574 strncpy(buf
+ start_bytes
, NULL_CONSOLE
, CONSOLE_ARG_SIZE
);
576 strcpy(buf
+ start_bytes
+ CONSOLE_ARG_SIZE
- 1, end
);
578 buf
[start_bytes
+ CONSOLE_ARG_SIZE
] = '\0';
580 sprintf(buf
, "%s %s", cmdline
, NULL_CONSOLE
);
582 if (buf
+ strlen(buf
) >= cmdline
)
585 if (maxlen
< CONSOLE_ARG_SIZE
)
587 strcpy(buf
, NULL_CONSOLE
);
589 debug("after silent fix-up: %s\n", buf
);
595 * process_subst() - Handle substitution of ${...} fields in the environment
597 * Handle variable substitution in the provided buffer
599 * @buf: Buffer containing the string to process
600 * @maxlen: Maximum length of buffer
601 * Return: 0 if OK, -ENOSPC if @maxlen is too small
603 static int process_subst(char *buf
, int maxlen
)
609 /* Move to end of buffer */
610 size
= strlen(buf
) + 1;
611 cmdline
= buf
+ maxlen
- size
;
612 if (buf
+ size
> cmdline
)
614 memmove(cmdline
, buf
, size
);
616 ret
= cli_simple_process_macros(cmdline
, buf
, cmdline
- buf
);
621 int bootm_process_cmdline(char *buf
, int maxlen
, int flags
)
625 /* Check config first to enable compiler to eliminate code */
626 if (IS_ENABLED(CONFIG_SILENT_CONSOLE
) &&
627 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY
) &&
628 (flags
& BOOTM_CL_SILENT
)) {
629 ret
= fixup_silent_linux(buf
, maxlen
);
631 return log_msg_ret("silent", ret
);
633 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST
) && IS_ENABLED(CONFIG_CMDLINE
) &&
634 (flags
& BOOTM_CL_SUBST
)) {
635 ret
= process_subst(buf
, maxlen
);
637 return log_msg_ret("subst", ret
);
643 int bootm_process_cmdline_env(int flags
)
645 const int maxlen
= MAX_CMDLINE_SIZE
;
651 /* First check if any action is needed */
652 do_silent
= IS_ENABLED(CONFIG_SILENT_CONSOLE
) &&
653 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY
) && (flags
& BOOTM_CL_SILENT
);
654 if (!do_silent
&& !IS_ENABLED(CONFIG_BOOTARGS_SUBST
))
657 env
= env_get("bootargs");
658 if (env
&& strlen(env
) >= maxlen
)
660 buf
= malloc(maxlen
);
667 ret
= bootm_process_cmdline(buf
, maxlen
, flags
);
669 ret
= env_set("bootargs", buf
);
672 * If buf is "" and bootargs does not exist, this will produce
673 * an error trying to delete bootargs. Ignore it
680 return log_msg_ret("env", ret
);
685 int bootm_measure(struct bootm_headers
*images
)
689 /* Skip measurement if EFI is going to do it */
690 if (images
->os
.os
== IH_OS_EFI
&&
691 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL
) &&
692 IS_ENABLED(CONFIG_BOOTM_EFI
))
695 if (IS_ENABLED(CONFIG_MEASURED_BOOT
)) {
696 struct tcg2_event_log elog
;
705 ign
= IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG
);
706 ret
= tcg2_measurement_init(&dev
, &elog
, ign
);
710 image_buf
= map_sysmem(images
->os
.image_start
,
711 images
->os
.image_len
);
712 ret
= tcg2_measure_data(dev
, &elog
, 8, images
->os
.image_len
,
713 image_buf
, EV_COMPACT_HASH
,
714 strlen("linux") + 1, (u8
*)"linux");
718 rd_len
= images
->rd_end
- images
->rd_start
;
719 initrd_buf
= map_sysmem(images
->rd_start
, rd_len
);
720 ret
= tcg2_measure_data(dev
, &elog
, 9, rd_len
, initrd_buf
,
721 EV_COMPACT_HASH
, strlen("initrd") + 1,
726 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE
)) {
727 ret
= tcg2_measure_data(dev
, &elog
, 0, images
->ft_len
,
728 (u8
*)images
->ft_addr
,
736 s
= env_get("bootargs");
739 ret
= tcg2_measure_data(dev
, &elog
, 1, strlen(s
) + 1, (u8
*)s
,
740 EV_PLATFORM_CONFIG_FLAGS
,
741 strlen(s
) + 1, (u8
*)s
);
744 unmap_sysmem(initrd_buf
);
747 unmap_sysmem(image_buf
);
748 tcg2_measurement_term(dev
, &elog
, ret
!= 0);
755 * Execute selected states of the bootm command.
757 * Note the arguments to this state must be the first argument, Any 'bootm'
758 * or sub-command arguments must have already been taken.
760 * Note that if states contains more than one flag it MUST contain
761 * BOOTM_STATE_START, since this handles and consumes the command line args.
763 * Also note that aside from boot_os_fn functions and bootm_load_os no other
764 * functions we store the return value of in 'ret' may use a negative return
765 * value, without special handling.
767 * @param cmdtp Pointer to bootm command table entry
768 * @param flag Command flags (CMD_FLAG_...)
769 * @param argc Number of subcommand arguments (0 = no arguments)
770 * @param argv Arguments
771 * @param states Mask containing states to run (BOOTM_STATE_...)
772 * @param images Image header information
773 * @param boot_progress 1 to show boot progress, 0 to not do this
774 * Return: 0 if ok, something else on error. Some errors will cause this
775 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
776 * then the intent is to boot an OS, so this function will not return
777 * unless the image type is standalone.
779 int do_bootm_states(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
780 char *const argv
[], int states
, struct bootm_headers
*images
,
785 int ret
= 0, need_boot_fn
;
787 images
->state
|= states
;
790 * Work through the states and see how far we get. We stop on
793 if (states
& BOOTM_STATE_START
)
796 if (!ret
&& (states
& BOOTM_STATE_PRE_LOAD
))
797 ret
= bootm_pre_load(argv
[0]);
799 if (!ret
&& (states
& BOOTM_STATE_FINDOS
))
800 ret
= bootm_find_os(cmdtp
, flag
, argc
, argv
);
802 if (!ret
&& (states
& BOOTM_STATE_FINDOTHER
))
803 ret
= bootm_find_other(cmdtp
, flag
, argc
, argv
);
805 if (IS_ENABLED(CONFIG_MEASURED_BOOT
) && !ret
&&
806 (states
& BOOTM_STATE_MEASURE
))
807 bootm_measure(images
);
810 if (!ret
&& (states
& BOOTM_STATE_LOADOS
)) {
811 iflag
= bootm_disable_interrupts();
812 ret
= bootm_load_os(images
, 0);
813 if (ret
&& ret
!= BOOTM_ERR_OVERLAP
)
815 else if (ret
== BOOTM_ERR_OVERLAP
)
819 /* Relocate the ramdisk */
820 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
821 if (!ret
&& (states
& BOOTM_STATE_RAMDISK
)) {
822 ulong rd_len
= images
->rd_end
- images
->rd_start
;
824 ret
= boot_ramdisk_high(&images
->lmb
, images
->rd_start
,
825 rd_len
, &images
->initrd_start
, &images
->initrd_end
);
827 env_set_hex("initrd_start", images
->initrd_start
);
828 env_set_hex("initrd_end", images
->initrd_end
);
832 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
833 if (!ret
&& (states
& BOOTM_STATE_FDT
)) {
834 boot_fdt_add_mem_rsv_regions(&images
->lmb
, images
->ft_addr
);
835 ret
= boot_relocate_fdt(&images
->lmb
, &images
->ft_addr
,
840 /* From now on, we need the OS boot function */
843 boot_fn
= bootm_os_get_boot_func(images
->os
.os
);
844 need_boot_fn
= states
& (BOOTM_STATE_OS_CMDLINE
|
845 BOOTM_STATE_OS_BD_T
| BOOTM_STATE_OS_PREP
|
846 BOOTM_STATE_OS_FAKE_GO
| BOOTM_STATE_OS_GO
);
847 if (boot_fn
== NULL
&& need_boot_fn
) {
850 printf("ERROR: booting os '%s' (%d) is not supported\n",
851 genimg_get_os_name(images
->os
.os
), images
->os
.os
);
852 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS
);
857 /* Call various other states that are not generally used */
858 if (!ret
&& (states
& BOOTM_STATE_OS_CMDLINE
))
859 ret
= boot_fn(BOOTM_STATE_OS_CMDLINE
, argc
, argv
, images
);
860 if (!ret
&& (states
& BOOTM_STATE_OS_BD_T
))
861 ret
= boot_fn(BOOTM_STATE_OS_BD_T
, argc
, argv
, images
);
862 if (!ret
&& (states
& BOOTM_STATE_OS_PREP
)) {
863 ret
= bootm_process_cmdline_env(images
->os
.os
== IH_OS_LINUX
);
865 printf("Cmdline setup failed (err=%d)\n", ret
);
866 ret
= CMD_RET_FAILURE
;
869 ret
= boot_fn(BOOTM_STATE_OS_PREP
, argc
, argv
, images
);
873 /* Pretend to run the OS, then run a user command */
874 if (!ret
&& (states
& BOOTM_STATE_OS_FAKE_GO
)) {
875 char *cmd_list
= env_get("fakegocmd");
877 ret
= boot_selected_os(argc
, argv
, BOOTM_STATE_OS_FAKE_GO
,
879 if (!ret
&& cmd_list
)
880 ret
= run_command_list(cmd_list
, -1, flag
);
884 /* Check for unsupported subcommand. */
886 printf("subcommand failed (err=%d)\n", ret
);
890 /* Now run the OS! We hope this doesn't return */
891 if (!ret
&& (states
& BOOTM_STATE_OS_GO
))
892 ret
= boot_selected_os(argc
, argv
, BOOTM_STATE_OS_GO
,
895 /* Deal with any fallout */
900 if (ret
== BOOTM_ERR_UNIMPLEMENTED
)
901 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL
);
902 else if (ret
== BOOTM_ERR_RESET
)
903 do_reset(cmdtp
, flag
, argc
, argv
);
908 int bootm_boot_start(ulong addr
, const char *cmdline
)
910 static struct cmd_tbl cmd
= {"bootm"};
912 char *argv
[] = {addr_str
, NULL
};
917 * TODO(sjg@chromium.org): This uses the command-line interface, but
918 * should not. To clean this up, the various bootm states need to be
919 * passed an info structure instead of cmdline flags. Then this can
920 * set up the required info and move through the states without needing
923 states
= BOOTM_STATE_START
| BOOTM_STATE_FINDOS
| BOOTM_STATE_PRE_LOAD
|
924 BOOTM_STATE_FINDOTHER
| BOOTM_STATE_LOADOS
|
925 BOOTM_STATE_OS_PREP
| BOOTM_STATE_OS_FAKE_GO
|
927 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH
))
928 states
|= BOOTM_STATE_RAMDISK
;
929 if (IS_ENABLED(CONFIG_PPC
) || IS_ENABLED(CONFIG_MIPS
))
930 states
|= BOOTM_STATE_OS_CMDLINE
;
931 images
.state
|= states
;
933 snprintf(addr_str
, sizeof(addr_str
), "%lx", addr
);
935 ret
= env_set("bootargs", cmdline
);
937 printf("Failed to set cmdline\n");
940 ret
= do_bootm_states(&cmd
, 0, 1, argv
, states
, &images
, 1);
945 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
947 * image_get_kernel - verify legacy format kernel image
948 * @img_addr: in RAM address of the legacy format image to be verified
949 * @verify: data CRC verification flag
951 * image_get_kernel() verifies legacy image integrity and returns pointer to
952 * legacy image header if image verification was completed successfully.
955 * pointer to a legacy image header if valid image was found
956 * otherwise return NULL
958 static struct legacy_img_hdr
*image_get_kernel(ulong img_addr
, int verify
)
960 struct legacy_img_hdr
*hdr
= (struct legacy_img_hdr
*)img_addr
;
962 if (!image_check_magic(hdr
)) {
963 puts("Bad Magic Number\n");
964 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC
);
967 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER
);
969 if (!image_check_hcrc(hdr
)) {
970 puts("Bad Header Checksum\n");
971 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER
);
975 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM
);
976 image_print_contents(hdr
);
979 puts(" Verifying Checksum ... ");
980 if (!image_check_dcrc(hdr
)) {
981 printf("Bad Data CRC\n");
982 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM
);
987 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH
);
989 if (!image_check_target_arch(hdr
)) {
990 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr
));
991 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH
);
999 * boot_get_kernel - find kernel image
1000 * @os_data: pointer to a ulong variable, will hold os data start address
1001 * @os_len: pointer to a ulong variable, will hold os data length
1003 * boot_get_kernel() tries to find a kernel image, verifies its integrity
1004 * and locates kernel data.
1007 * pointer to image header if valid image was found, plus kernel start
1008 * address and length, otherwise NULL
1010 static const void *boot_get_kernel(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
1011 char *const argv
[], struct bootm_headers
*images
,
1012 ulong
*os_data
, ulong
*os_len
)
1014 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
1015 struct legacy_img_hdr
*hdr
;
1019 const char *fit_uname_config
= NULL
;
1020 const char *fit_uname_kernel
= NULL
;
1021 #if CONFIG_IS_ENABLED(FIT)
1025 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1026 const void *boot_img
;
1027 const void *vendor_boot_img
;
1029 img_addr
= genimg_get_kernel_addr_fit(argc
< 1 ? NULL
: argv
[0],
1033 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD
))
1034 img_addr
+= image_load_offset
;
1036 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC
);
1038 /* check image type, for FIT images get FIT kernel node */
1039 *os_data
= *os_len
= 0;
1040 buf
= map_sysmem(img_addr
, 0);
1041 switch (genimg_get_format(buf
)) {
1042 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
1043 case IMAGE_FORMAT_LEGACY
:
1044 printf("## Booting kernel from Legacy Image at %08lx ...\n",
1046 hdr
= image_get_kernel(img_addr
, images
->verify
);
1049 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE
);
1051 /* get os_data and os_len */
1052 switch (image_get_type(hdr
)) {
1053 case IH_TYPE_KERNEL
:
1054 case IH_TYPE_KERNEL_NOLOAD
:
1055 *os_data
= image_get_data(hdr
);
1056 *os_len
= image_get_data_size(hdr
);
1059 image_multi_getimg(hdr
, 0, os_data
, os_len
);
1061 case IH_TYPE_STANDALONE
:
1062 *os_data
= image_get_data(hdr
);
1063 *os_len
= image_get_data_size(hdr
);
1066 printf("Wrong Image Type for %s command\n",
1068 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE
);
1073 * copy image header to allow for image overwrites during
1074 * kernel decompression.
1076 memmove(&images
->legacy_hdr_os_copy
, hdr
,
1077 sizeof(struct legacy_img_hdr
));
1079 /* save pointer to image header */
1080 images
->legacy_hdr_os
= hdr
;
1082 images
->legacy_hdr_valid
= 1;
1083 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE
);
1086 #if CONFIG_IS_ENABLED(FIT)
1087 case IMAGE_FORMAT_FIT
:
1088 os_noffset
= fit_image_load(images
, img_addr
,
1089 &fit_uname_kernel
, &fit_uname_config
,
1090 IH_ARCH_DEFAULT
, IH_TYPE_KERNEL
,
1091 BOOTSTAGE_ID_FIT_KERNEL_START
,
1092 FIT_LOAD_IGNORED
, os_data
, os_len
);
1096 images
->fit_hdr_os
= map_sysmem(img_addr
, 0);
1097 images
->fit_uname_os
= fit_uname_kernel
;
1098 images
->fit_uname_cfg
= fit_uname_config
;
1099 images
->fit_noffset_os
= os_noffset
;
1102 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1103 case IMAGE_FORMAT_ANDROID
:
1105 vendor_boot_img
= NULL
;
1106 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
1107 boot_img
= map_sysmem(get_abootimg_addr(), 0);
1108 vendor_boot_img
= map_sysmem(get_avendor_bootimg_addr(), 0);
1110 printf("## Booting Android Image at 0x%08lx ...\n", img_addr
);
1111 if (android_image_get_kernel(boot_img
, vendor_boot_img
, images
->verify
,
1114 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
1115 unmap_sysmem(vendor_boot_img
);
1116 unmap_sysmem(boot_img
);
1121 printf("Wrong Image Format for %s command\n", cmdtp
->name
);
1122 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO
);
1126 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1127 *os_data
, *os_len
, *os_len
);
1133 * switch_to_non_secure_mode() - switch to non-secure mode
1135 * This routine is overridden by architectures requiring this feature.
1137 void __weak
switch_to_non_secure_mode(void)
1141 #else /* USE_HOSTCC */
1143 #if defined(CONFIG_FIT_SIGNATURE)
1144 static int bootm_host_load_image(const void *fit
, int req_image_type
,
1147 const char *fit_uname_config
= NULL
;
1149 struct bootm_headers images
;
1151 ulong load_end
, buf_size
;
1157 fit_uname_config
= fdt_get_name(fit
, cfg_noffset
, NULL
);
1158 memset(&images
, '\0', sizeof(images
));
1160 noffset
= fit_image_load(&images
, (ulong
)fit
,
1161 NULL
, &fit_uname_config
,
1162 IH_ARCH_DEFAULT
, req_image_type
, -1,
1163 FIT_LOAD_IGNORED
, &data
, &len
);
1166 if (fit_image_get_type(fit
, noffset
, &image_type
)) {
1167 puts("Can't get image type!\n");
1171 if (fit_image_get_comp(fit
, noffset
, &image_comp
))
1172 image_comp
= IH_COMP_NONE
;
1174 /* Allow the image to expand by a factor of 4, should be safe */
1175 buf_size
= (1 << 20) + len
* 4;
1176 load_buf
= malloc(buf_size
);
1177 ret
= image_decomp(image_comp
, 0, data
, image_type
, load_buf
,
1178 (void *)data
, len
, buf_size
, &load_end
);
1182 ret
= handle_decomp_error(image_comp
, load_end
- 0, buf_size
, ret
);
1183 if (ret
!= BOOTM_ERR_UNIMPLEMENTED
)
1190 int bootm_host_load_images(const void *fit
, int cfg_noffset
)
1192 static uint8_t image_types
[] = {
1200 for (i
= 0; i
< ARRAY_SIZE(image_types
); i
++) {
1203 ret
= bootm_host_load_image(fit
, image_types
[i
], cfg_noffset
);
1204 if (!err
&& ret
&& ret
!= -ENOENT
)
1208 /* Return the first error we found */
1213 #endif /* ndef USE_HOSTCC */