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 __weak
void board_quiesce_devices(void)
51 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
53 * image_get_kernel - verify legacy format kernel image
54 * @img_addr: in RAM address of the legacy format image to be verified
55 * @verify: data CRC verification flag
57 * image_get_kernel() verifies legacy image integrity and returns pointer to
58 * legacy image header if image verification was completed successfully.
61 * pointer to a legacy image header if valid image was found
62 * otherwise return NULL
64 static struct legacy_img_hdr
*image_get_kernel(ulong img_addr
, int verify
)
66 struct legacy_img_hdr
*hdr
= (struct legacy_img_hdr
*)img_addr
;
68 if (!image_check_magic(hdr
)) {
69 puts("Bad Magic Number\n");
70 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC
);
73 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER
);
75 if (!image_check_hcrc(hdr
)) {
76 puts("Bad Header Checksum\n");
77 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER
);
81 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM
);
82 image_print_contents(hdr
);
85 puts(" Verifying Checksum ... ");
86 if (!image_check_dcrc(hdr
)) {
87 printf("Bad Data CRC\n");
88 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM
);
93 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH
);
95 if (!image_check_target_arch(hdr
)) {
96 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr
));
97 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH
);
105 * boot_get_kernel - find kernel image
106 * @os_data: pointer to a ulong variable, will hold os data start address
107 * @os_len: pointer to a ulong variable, will hold os data length
109 * boot_get_kernel() tries to find a kernel image, verifies its integrity
110 * and locates kernel data.
113 * pointer to image header if valid image was found, plus kernel start
114 * address and length, otherwise NULL
116 static const void *boot_get_kernel(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
117 char *const argv
[], struct bootm_headers
*images
,
118 ulong
*os_data
, ulong
*os_len
)
120 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
121 struct legacy_img_hdr
*hdr
;
125 const char *fit_uname_config
= NULL
, *fit_uname_kernel
= NULL
;
126 #if CONFIG_IS_ENABLED(FIT)
130 #ifdef CONFIG_ANDROID_BOOT_IMAGE
131 const void *boot_img
;
132 const void *vendor_boot_img
;
134 img_addr
= genimg_get_kernel_addr_fit(argc
< 1 ? NULL
: argv
[0],
138 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD
))
139 img_addr
+= image_load_offset
;
141 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC
);
143 /* check image type, for FIT images get FIT kernel node */
144 *os_data
= *os_len
= 0;
145 buf
= map_sysmem(img_addr
, 0);
146 switch (genimg_get_format(buf
)) {
147 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
148 case IMAGE_FORMAT_LEGACY
:
149 printf("## Booting kernel from Legacy Image at %08lx ...\n",
151 hdr
= image_get_kernel(img_addr
, images
->verify
);
154 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE
);
156 /* get os_data and os_len */
157 switch (image_get_type(hdr
)) {
159 case IH_TYPE_KERNEL_NOLOAD
:
160 *os_data
= image_get_data(hdr
);
161 *os_len
= image_get_data_size(hdr
);
164 image_multi_getimg(hdr
, 0, os_data
, os_len
);
166 case IH_TYPE_STANDALONE
:
167 *os_data
= image_get_data(hdr
);
168 *os_len
= image_get_data_size(hdr
);
171 printf("Wrong Image Type for %s command\n",
173 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE
);
178 * copy image header to allow for image overwrites during
179 * kernel decompression.
181 memmove(&images
->legacy_hdr_os_copy
, hdr
,
182 sizeof(struct legacy_img_hdr
));
184 /* save pointer to image header */
185 images
->legacy_hdr_os
= hdr
;
187 images
->legacy_hdr_valid
= 1;
188 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE
);
191 #if CONFIG_IS_ENABLED(FIT)
192 case IMAGE_FORMAT_FIT
:
193 os_noffset
= fit_image_load(images
, img_addr
,
194 &fit_uname_kernel
, &fit_uname_config
,
195 IH_ARCH_DEFAULT
, IH_TYPE_KERNEL
,
196 BOOTSTAGE_ID_FIT_KERNEL_START
,
197 FIT_LOAD_IGNORED
, os_data
, os_len
);
201 images
->fit_hdr_os
= map_sysmem(img_addr
, 0);
202 images
->fit_uname_os
= fit_uname_kernel
;
203 images
->fit_uname_cfg
= fit_uname_config
;
204 images
->fit_noffset_os
= os_noffset
;
207 #ifdef CONFIG_ANDROID_BOOT_IMAGE
208 case IMAGE_FORMAT_ANDROID
:
210 vendor_boot_img
= NULL
;
211 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
212 boot_img
= map_sysmem(get_abootimg_addr(), 0);
213 vendor_boot_img
= map_sysmem(get_avendor_bootimg_addr(), 0);
215 printf("## Booting Android Image at 0x%08lx ...\n", img_addr
);
216 if (android_image_get_kernel(boot_img
, vendor_boot_img
, images
->verify
,
219 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
220 unmap_sysmem(vendor_boot_img
);
221 unmap_sysmem(boot_img
);
226 printf("Wrong Image Format for %s command\n", cmdtp
->name
);
227 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO
);
231 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
232 *os_data
, *os_len
, *os_len
);
238 static void boot_start_lmb(struct bootm_headers
*images
)
241 phys_size_t mem_size
;
243 mem_start
= env_get_bootm_low();
244 mem_size
= env_get_bootm_size();
246 lmb_init_and_reserve_range(&images
->lmb
, (phys_addr_t
)mem_start
,
250 #define lmb_reserve(lmb, base, size)
251 static inline void boot_start_lmb(struct bootm_headers
*images
) { }
254 static int bootm_start(void)
256 memset((void *)&images
, 0, sizeof(images
));
257 images
.verify
= env_get_yesno("verify");
259 boot_start_lmb(&images
);
261 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START
, "bootm_start");
262 images
.state
= BOOTM_STATE_START
;
267 static ulong
bootm_data_addr(const char *addr_str
)
272 addr
= hextoul(addr_str
, NULL
);
274 addr
= image_load_addr
;
280 * bootm_pre_load() - Handle the pre-load processing
282 * This can be used to do a full signature check of the image, for example.
283 * It calls image_pre_load() with the data address of the image to check.
285 * @addr_str: String containing load address in hex, or NULL to use
287 * Return: 0 if OK, CMD_RET_FAILURE on failure
289 static int bootm_pre_load(const char *addr_str
)
291 ulong data_addr
= bootm_data_addr(addr_str
);
294 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD
))
295 ret
= image_pre_load(data_addr
);
298 ret
= CMD_RET_FAILURE
;
303 static int bootm_find_os(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
307 #ifdef CONFIG_ANDROID_BOOT_IMAGE
308 const void *vendor_boot_img
;
309 const void *boot_img
;
311 bool ep_found
= false;
314 /* get kernel image header, start address and length */
315 os_hdr
= boot_get_kernel(cmdtp
, flag
, argc
, argv
,
316 &images
, &images
.os
.image_start
, &images
.os
.image_len
);
317 if (images
.os
.image_len
== 0) {
318 puts("ERROR: can't get kernel image!\n");
322 /* get image parameters */
323 switch (genimg_get_format(os_hdr
)) {
324 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
325 case IMAGE_FORMAT_LEGACY
:
326 images
.os
.type
= image_get_type(os_hdr
);
327 images
.os
.comp
= image_get_comp(os_hdr
);
328 images
.os
.os
= image_get_os(os_hdr
);
330 images
.os
.end
= image_get_image_end(os_hdr
);
331 images
.os
.load
= image_get_load(os_hdr
);
332 images
.os
.arch
= image_get_arch(os_hdr
);
335 #if CONFIG_IS_ENABLED(FIT)
336 case IMAGE_FORMAT_FIT
:
337 if (fit_image_get_type(images
.fit_hdr_os
,
338 images
.fit_noffset_os
,
340 puts("Can't get image type!\n");
341 bootstage_error(BOOTSTAGE_ID_FIT_TYPE
);
345 if (fit_image_get_comp(images
.fit_hdr_os
,
346 images
.fit_noffset_os
,
348 puts("Can't get image compression!\n");
349 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION
);
353 if (fit_image_get_os(images
.fit_hdr_os
, images
.fit_noffset_os
,
355 puts("Can't get image OS!\n");
356 bootstage_error(BOOTSTAGE_ID_FIT_OS
);
360 if (fit_image_get_arch(images
.fit_hdr_os
,
361 images
.fit_noffset_os
,
363 puts("Can't get image ARCH!\n");
367 images
.os
.end
= fit_get_end(images
.fit_hdr_os
);
369 if (fit_image_get_load(images
.fit_hdr_os
, images
.fit_noffset_os
,
371 puts("Can't get image load address!\n");
372 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR
);
377 #ifdef CONFIG_ANDROID_BOOT_IMAGE
378 case IMAGE_FORMAT_ANDROID
:
380 vendor_boot_img
= NULL
;
381 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
382 boot_img
= map_sysmem(get_abootimg_addr(), 0);
383 vendor_boot_img
= map_sysmem(get_avendor_bootimg_addr(), 0);
385 images
.os
.type
= IH_TYPE_KERNEL
;
386 images
.os
.comp
= android_image_get_kcomp(boot_img
, vendor_boot_img
);
387 images
.os
.os
= IH_OS_LINUX
;
388 images
.os
.end
= android_image_get_end(boot_img
, vendor_boot_img
);
389 images
.os
.load
= android_image_get_kload(boot_img
, vendor_boot_img
);
390 images
.ep
= images
.os
.load
;
392 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG
)) {
393 unmap_sysmem(vendor_boot_img
);
394 unmap_sysmem(boot_img
);
399 puts("ERROR: unknown image format type!\n");
403 /* If we have a valid setup.bin, we will use that for entry (x86) */
404 if (images
.os
.arch
== IH_ARCH_I386
||
405 images
.os
.arch
== IH_ARCH_X86_64
) {
408 ret
= boot_get_setup(&images
, IH_ARCH_I386
, &images
.ep
, &len
);
409 if (ret
< 0 && ret
!= -ENOENT
) {
410 puts("Could not find a valid setup.bin for x86\n");
413 /* Kernel entry point is the setup.bin */
414 } else if (images
.legacy_hdr_valid
) {
415 images
.ep
= image_get_ep(&images
.legacy_hdr_os_copy
);
416 #if CONFIG_IS_ENABLED(FIT)
417 } else if (images
.fit_uname_os
) {
420 ret
= fit_image_get_entry(images
.fit_hdr_os
,
421 images
.fit_noffset_os
, &images
.ep
);
423 puts("Can't get entry point property!\n");
427 } else if (!ep_found
) {
428 puts("Could not find kernel entry point!\n");
432 if (images
.os
.type
== IH_TYPE_KERNEL_NOLOAD
) {
433 if (IS_ENABLED(CONFIG_CMD_BOOTI
) &&
434 images
.os
.arch
== IH_ARCH_ARM64
&&
435 images
.os
.os
== IH_OS_LINUX
) {
439 ret
= booti_setup(images
.os
.image_start
, &image_addr
,
444 images
.os
.type
= IH_TYPE_KERNEL
;
445 images
.os
.load
= image_addr
;
446 images
.ep
= image_addr
;
448 images
.os
.load
= images
.os
.image_start
;
449 images
.ep
+= images
.os
.image_start
;
453 images
.os
.start
= map_to_sysmem(os_hdr
);
459 * bootm_find_images - wrapper to find and locate various images
460 * @flag: Ignored Argument
461 * @argc: command argument count
462 * @argv: command argument list
463 * @start: OS image start address
464 * @size: OS image size
466 * boot_find_images() will attempt to load an available ramdisk,
467 * flattened device tree, as well as specifically marked
468 * "loadable" images (loadables are FIT only)
470 * Note: bootm_find_images will skip an image if it is not found
473 * 0, if all existing images were loaded correctly
474 * 1, if an image is found but corrupted, or invalid
476 int bootm_find_images(int flag
, int argc
, char *const argv
[], ulong start
,
482 ret
= boot_get_ramdisk(argc
, argv
, &images
, IH_INITRD_ARCH
,
483 &images
.rd_start
, &images
.rd_end
);
485 puts("Ramdisk image is corrupt or invalid\n");
489 /* check if ramdisk overlaps OS image */
490 if (images
.rd_start
&& (((ulong
)images
.rd_start
>= start
&&
491 (ulong
)images
.rd_start
< start
+ size
) ||
492 ((ulong
)images
.rd_end
> start
&&
493 (ulong
)images
.rd_end
<= start
+ size
) ||
494 ((ulong
)images
.rd_start
< start
&&
495 (ulong
)images
.rd_end
>= start
+ size
))) {
496 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
497 start
, start
+ size
);
501 #if CONFIG_IS_ENABLED(OF_LIBFDT)
502 /* find flattened device tree */
503 ret
= boot_get_fdt(flag
, argc
, argv
, IH_ARCH_DEFAULT
, &images
,
504 &images
.ft_addr
, &images
.ft_len
);
506 puts("Could not find a valid device tree\n");
510 /* check if FDT overlaps OS image */
511 if (images
.ft_addr
&&
512 (((ulong
)images
.ft_addr
>= start
&&
513 (ulong
)images
.ft_addr
< start
+ size
) ||
514 ((ulong
)images
.ft_addr
+ images
.ft_len
>= start
&&
515 (ulong
)images
.ft_addr
+ images
.ft_len
< start
+ size
))) {
516 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
517 start
, start
+ size
);
521 if (IS_ENABLED(CONFIG_CMD_FDT
))
522 set_working_fdt_addr(map_to_sysmem(images
.ft_addr
));
525 #if CONFIG_IS_ENABLED(FIT)
526 if (IS_ENABLED(CONFIG_FPGA
)) {
527 /* find bitstreams */
528 ret
= boot_get_fpga(argc
, argv
, &images
, IH_ARCH_DEFAULT
,
531 printf("FPGA image is corrupted or invalid\n");
536 /* find all of the loadables */
537 ret
= boot_get_loadable(argc
, argv
, &images
, IH_ARCH_DEFAULT
,
540 printf("Loadable(s) is corrupt or invalid\n");
548 static int bootm_find_other(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
551 if (((images
.os
.type
== IH_TYPE_KERNEL
) ||
552 (images
.os
.type
== IH_TYPE_KERNEL_NOLOAD
) ||
553 (images
.os
.type
== IH_TYPE_MULTI
)) &&
554 (images
.os
.os
== IH_OS_LINUX
||
555 images
.os
.os
== IH_OS_VXWORKS
))
556 return bootm_find_images(flag
, argc
, argv
, 0, 0);
560 #endif /* USE_HOSTC */
562 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
564 * handle_decomp_error() - display a decompression error
566 * This function tries to produce a useful message. In the case where the
567 * uncompressed size is the same as the available space, we can assume that
568 * the image is too large for the buffer.
570 * @comp_type: Compression type being used (IH_COMP_...)
571 * @uncomp_size: Number of bytes uncompressed
572 * @buf_size: Number of bytes the decompresion buffer was
573 * @ret: errno error code received from compression library
574 * Return: Appropriate BOOTM_ERR_ error code
576 static int handle_decomp_error(int comp_type
, size_t uncomp_size
,
577 size_t buf_size
, int ret
)
579 const char *name
= genimg_get_comp_name(comp_type
);
581 /* ENOSYS means unimplemented compression type, don't reset. */
583 return BOOTM_ERR_UNIMPLEMENTED
;
585 if (uncomp_size
>= buf_size
)
586 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
588 printf("%s: uncompress error %d\n", name
, ret
);
591 * The decompression routines are now safe, so will not write beyond
592 * their bounds. Probably it is not necessary to reset, but maintain
593 * the current behaviour for now.
595 printf("Must RESET board to recover\n");
597 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE
);
600 return BOOTM_ERR_RESET
;
605 static int bootm_load_os(struct bootm_headers
*images
, int boot_progress
)
607 struct image_info os
= images
->os
;
608 ulong load
= os
.load
;
610 ulong blob_start
= os
.start
;
611 ulong blob_end
= os
.end
;
612 ulong image_start
= os
.image_start
;
613 ulong image_len
= os
.image_len
;
614 ulong flush_start
= ALIGN_DOWN(load
, ARCH_DMA_MINALIGN
);
616 void *load_buf
, *image_buf
;
619 load_buf
= map_sysmem(load
, 0);
620 image_buf
= map_sysmem(os
.image_start
, image_len
);
621 err
= image_decomp(os
.comp
, load
, os
.image_start
, os
.type
,
622 load_buf
, image_buf
, image_len
,
623 CONFIG_SYS_BOOTM_LEN
, &load_end
);
625 err
= handle_decomp_error(os
.comp
, load_end
- load
,
626 CONFIG_SYS_BOOTM_LEN
, err
);
627 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE
);
630 /* We need the decompressed image size in the next steps */
631 images
->os
.image_len
= load_end
- load
;
633 flush_cache(flush_start
, ALIGN(load_end
, ARCH_DMA_MINALIGN
) - flush_start
);
635 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load
, load_end
);
636 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED
);
638 no_overlap
= (os
.comp
== IH_COMP_NONE
&& load
== image_start
);
640 if (!no_overlap
&& load
< blob_end
&& load_end
> blob_start
) {
641 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
642 blob_start
, blob_end
);
643 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load
,
646 /* Check what type of image this is. */
647 if (images
->legacy_hdr_valid
) {
648 if (image_get_type(&images
->legacy_hdr_os_copy
)
650 puts("WARNING: legacy format multi component image overwritten\n");
651 return BOOTM_ERR_OVERLAP
;
653 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
654 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN
);
655 return BOOTM_ERR_RESET
;
659 lmb_reserve(&images
->lmb
, images
->os
.load
, (load_end
-
665 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
667 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
670 ulong
bootm_disable_interrupts(void)
675 * We have reached the point of no return: we are going to
676 * overwrite all exception vector code, so we cannot easily
677 * recover from any failures any more...
679 iflag
= disable_interrupts();
680 #ifdef CONFIG_NETCONSOLE
681 /* Stop the ethernet stack if NetConsole could have left it up */
685 #if defined(CONFIG_CMD_USB)
687 * turn off USB to prevent the host controller from writing to the
688 * SDRAM while Linux is booting. This could happen (at least for OHCI
689 * controller), because the HCCA (Host Controller Communication Area)
690 * lies within the SDRAM and the host controller writes continously to
691 * this area (as busmaster!). The HccaFrameNumber is for example
692 * updated every 1 ms within the HCCA structure in SDRAM! For more
693 * details see the OpenHCI specification.
700 #define CONSOLE_ARG "console="
701 #define NULL_CONSOLE (CONSOLE_ARG "ttynull")
702 #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
705 * fixup_silent_linux() - Handle silencing the linux boot if required
707 * This uses the silent_linux envvar to control whether to add/set a "console="
708 * parameter to the command line
710 * @buf: Buffer containing the string to process
711 * @maxlen: Maximum length of buffer
712 * Return: 0 if OK, -ENOSPC if @maxlen is too small
714 static int fixup_silent_linux(char *buf
, int maxlen
)
721 * Move the input string to the end of buffer. The output string will be
722 * built up at the start.
724 size
= strlen(buf
) + 1;
725 if (size
* 2 > maxlen
)
727 cmdline
= buf
+ maxlen
- size
;
728 memmove(cmdline
, buf
, size
);
730 * Only fix cmdline when requested. The environment variable can be:
732 * no - we never fixup
733 * yes - we always fixup
734 * unset - we rely on the console silent flag
736 want_silent
= env_get_yesno("silent_linux");
737 if (want_silent
== 0)
739 else if (want_silent
== -1 && !(gd
->flags
& GD_FLG_SILENT
))
742 debug("before silent fix-up: %s\n", cmdline
);
744 char *start
= strstr(cmdline
, CONSOLE_ARG
);
746 /* Check space for maximum possible new command line */
747 if (size
+ CONSOLE_ARG_SIZE
> maxlen
)
751 char *end
= strchr(start
, ' ');
754 start_bytes
= start
- cmdline
;
755 strncpy(buf
, cmdline
, start_bytes
);
756 strncpy(buf
+ start_bytes
, NULL_CONSOLE
, CONSOLE_ARG_SIZE
);
758 strcpy(buf
+ start_bytes
+ CONSOLE_ARG_SIZE
- 1, end
);
760 buf
[start_bytes
+ CONSOLE_ARG_SIZE
] = '\0';
762 sprintf(buf
, "%s %s", cmdline
, NULL_CONSOLE
);
764 if (buf
+ strlen(buf
) >= cmdline
)
767 if (maxlen
< CONSOLE_ARG_SIZE
)
769 strcpy(buf
, NULL_CONSOLE
);
771 debug("after silent fix-up: %s\n", buf
);
777 * process_subst() - Handle substitution of ${...} fields in the environment
779 * Handle variable substitution in the provided buffer
781 * @buf: Buffer containing the string to process
782 * @maxlen: Maximum length of buffer
783 * Return: 0 if OK, -ENOSPC if @maxlen is too small
785 static int process_subst(char *buf
, int maxlen
)
791 /* Move to end of buffer */
792 size
= strlen(buf
) + 1;
793 cmdline
= buf
+ maxlen
- size
;
794 if (buf
+ size
> cmdline
)
796 memmove(cmdline
, buf
, size
);
798 ret
= cli_simple_process_macros(cmdline
, buf
, cmdline
- buf
);
803 int bootm_process_cmdline(char *buf
, int maxlen
, int flags
)
807 /* Check config first to enable compiler to eliminate code */
808 if (IS_ENABLED(CONFIG_SILENT_CONSOLE
) &&
809 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY
) &&
810 (flags
& BOOTM_CL_SILENT
)) {
811 ret
= fixup_silent_linux(buf
, maxlen
);
813 return log_msg_ret("silent", ret
);
815 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST
) && IS_ENABLED(CONFIG_CMDLINE
) &&
816 (flags
& BOOTM_CL_SUBST
)) {
817 ret
= process_subst(buf
, maxlen
);
819 return log_msg_ret("subst", ret
);
825 int bootm_process_cmdline_env(int flags
)
827 const int maxlen
= MAX_CMDLINE_SIZE
;
833 /* First check if any action is needed */
834 do_silent
= IS_ENABLED(CONFIG_SILENT_CONSOLE
) &&
835 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY
) && (flags
& BOOTM_CL_SILENT
);
836 if (!do_silent
&& !IS_ENABLED(CONFIG_BOOTARGS_SUBST
))
839 env
= env_get("bootargs");
840 if (env
&& strlen(env
) >= maxlen
)
842 buf
= malloc(maxlen
);
849 ret
= bootm_process_cmdline(buf
, maxlen
, flags
);
851 ret
= env_set("bootargs", buf
);
854 * If buf is "" and bootargs does not exist, this will produce
855 * an error trying to delete bootargs. Ignore it
862 return log_msg_ret("env", ret
);
867 int bootm_measure(struct bootm_headers
*images
)
871 /* Skip measurement if EFI is going to do it */
872 if (images
->os
.os
== IH_OS_EFI
&&
873 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL
) &&
874 IS_ENABLED(CONFIG_BOOTM_EFI
))
877 if (IS_ENABLED(CONFIG_MEASURED_BOOT
)) {
878 struct tcg2_event_log elog
;
887 ign
= IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG
);
888 ret
= tcg2_measurement_init(&dev
, &elog
, ign
);
892 image_buf
= map_sysmem(images
->os
.image_start
,
893 images
->os
.image_len
);
894 ret
= tcg2_measure_data(dev
, &elog
, 8, images
->os
.image_len
,
895 image_buf
, EV_COMPACT_HASH
,
896 strlen("linux") + 1, (u8
*)"linux");
900 rd_len
= images
->rd_end
- images
->rd_start
;
901 initrd_buf
= map_sysmem(images
->rd_start
, rd_len
);
902 ret
= tcg2_measure_data(dev
, &elog
, 9, rd_len
, initrd_buf
,
903 EV_COMPACT_HASH
, strlen("initrd") + 1,
908 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE
)) {
909 ret
= tcg2_measure_data(dev
, &elog
, 0, images
->ft_len
,
910 (u8
*)images
->ft_addr
,
918 s
= env_get("bootargs");
921 ret
= tcg2_measure_data(dev
, &elog
, 1, strlen(s
) + 1, (u8
*)s
,
922 EV_PLATFORM_CONFIG_FLAGS
,
923 strlen(s
) + 1, (u8
*)s
);
926 unmap_sysmem(initrd_buf
);
929 unmap_sysmem(image_buf
);
930 tcg2_measurement_term(dev
, &elog
, ret
!= 0);
937 * Execute selected states of the bootm command.
939 * Note the arguments to this state must be the first argument, Any 'bootm'
940 * or sub-command arguments must have already been taken.
942 * Note that if states contains more than one flag it MUST contain
943 * BOOTM_STATE_START, since this handles and consumes the command line args.
945 * Also note that aside from boot_os_fn functions and bootm_load_os no other
946 * functions we store the return value of in 'ret' may use a negative return
947 * value, without special handling.
949 * @param cmdtp Pointer to bootm command table entry
950 * @param flag Command flags (CMD_FLAG_...)
951 * @param argc Number of subcommand arguments (0 = no arguments)
952 * @param argv Arguments
953 * @param states Mask containing states to run (BOOTM_STATE_...)
954 * @param images Image header information
955 * @param boot_progress 1 to show boot progress, 0 to not do this
956 * Return: 0 if ok, something else on error. Some errors will cause this
957 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
958 * then the intent is to boot an OS, so this function will not return
959 * unless the image type is standalone.
961 int do_bootm_states(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
962 char *const argv
[], int states
, struct bootm_headers
*images
,
967 int ret
= 0, need_boot_fn
;
969 images
->state
|= states
;
972 * Work through the states and see how far we get. We stop on
975 if (states
& BOOTM_STATE_START
)
978 if (!ret
&& (states
& BOOTM_STATE_PRE_LOAD
))
979 ret
= bootm_pre_load(argv
[0]);
981 if (!ret
&& (states
& BOOTM_STATE_FINDOS
))
982 ret
= bootm_find_os(cmdtp
, flag
, argc
, argv
);
984 if (!ret
&& (states
& BOOTM_STATE_FINDOTHER
))
985 ret
= bootm_find_other(cmdtp
, flag
, argc
, argv
);
987 if (IS_ENABLED(CONFIG_MEASURED_BOOT
) && !ret
&&
988 (states
& BOOTM_STATE_MEASURE
))
989 bootm_measure(images
);
992 if (!ret
&& (states
& BOOTM_STATE_LOADOS
)) {
993 iflag
= bootm_disable_interrupts();
994 ret
= bootm_load_os(images
, 0);
995 if (ret
&& ret
!= BOOTM_ERR_OVERLAP
)
997 else if (ret
== BOOTM_ERR_OVERLAP
)
1001 /* Relocate the ramdisk */
1002 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1003 if (!ret
&& (states
& BOOTM_STATE_RAMDISK
)) {
1004 ulong rd_len
= images
->rd_end
- images
->rd_start
;
1006 ret
= boot_ramdisk_high(&images
->lmb
, images
->rd_start
,
1007 rd_len
, &images
->initrd_start
, &images
->initrd_end
);
1009 env_set_hex("initrd_start", images
->initrd_start
);
1010 env_set_hex("initrd_end", images
->initrd_end
);
1014 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
1015 if (!ret
&& (states
& BOOTM_STATE_FDT
)) {
1016 boot_fdt_add_mem_rsv_regions(&images
->lmb
, images
->ft_addr
);
1017 ret
= boot_relocate_fdt(&images
->lmb
, &images
->ft_addr
,
1022 /* From now on, we need the OS boot function */
1025 boot_fn
= bootm_os_get_boot_func(images
->os
.os
);
1026 need_boot_fn
= states
& (BOOTM_STATE_OS_CMDLINE
|
1027 BOOTM_STATE_OS_BD_T
| BOOTM_STATE_OS_PREP
|
1028 BOOTM_STATE_OS_FAKE_GO
| BOOTM_STATE_OS_GO
);
1029 if (boot_fn
== NULL
&& need_boot_fn
) {
1031 enable_interrupts();
1032 printf("ERROR: booting os '%s' (%d) is not supported\n",
1033 genimg_get_os_name(images
->os
.os
), images
->os
.os
);
1034 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS
);
1039 /* Call various other states that are not generally used */
1040 if (!ret
&& (states
& BOOTM_STATE_OS_CMDLINE
))
1041 ret
= boot_fn(BOOTM_STATE_OS_CMDLINE
, argc
, argv
, images
);
1042 if (!ret
&& (states
& BOOTM_STATE_OS_BD_T
))
1043 ret
= boot_fn(BOOTM_STATE_OS_BD_T
, argc
, argv
, images
);
1044 if (!ret
&& (states
& BOOTM_STATE_OS_PREP
)) {
1045 ret
= bootm_process_cmdline_env(images
->os
.os
== IH_OS_LINUX
);
1047 printf("Cmdline setup failed (err=%d)\n", ret
);
1048 ret
= CMD_RET_FAILURE
;
1051 ret
= boot_fn(BOOTM_STATE_OS_PREP
, argc
, argv
, images
);
1055 /* Pretend to run the OS, then run a user command */
1056 if (!ret
&& (states
& BOOTM_STATE_OS_FAKE_GO
)) {
1057 char *cmd_list
= env_get("fakegocmd");
1059 ret
= boot_selected_os(argc
, argv
, BOOTM_STATE_OS_FAKE_GO
,
1061 if (!ret
&& cmd_list
)
1062 ret
= run_command_list(cmd_list
, -1, flag
);
1066 /* Check for unsupported subcommand. */
1068 printf("subcommand failed (err=%d)\n", ret
);
1072 /* Now run the OS! We hope this doesn't return */
1073 if (!ret
&& (states
& BOOTM_STATE_OS_GO
))
1074 ret
= boot_selected_os(argc
, argv
, BOOTM_STATE_OS_GO
,
1077 /* Deal with any fallout */
1080 enable_interrupts();
1082 if (ret
== BOOTM_ERR_UNIMPLEMENTED
)
1083 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL
);
1084 else if (ret
== BOOTM_ERR_RESET
)
1085 do_reset(cmdtp
, flag
, argc
, argv
);
1090 int bootm_boot_start(ulong addr
, const char *cmdline
)
1092 static struct cmd_tbl cmd
= {"bootm"};
1094 char *argv
[] = {addr_str
, NULL
};
1099 * TODO(sjg@chromium.org): This uses the command-line interface, but
1100 * should not. To clean this up, the various bootm states need to be
1101 * passed an info structure instead of cmdline flags. Then this can
1102 * set up the required info and move through the states without needing
1105 states
= BOOTM_STATE_START
| BOOTM_STATE_FINDOS
| BOOTM_STATE_PRE_LOAD
|
1106 BOOTM_STATE_FINDOTHER
| BOOTM_STATE_LOADOS
|
1107 BOOTM_STATE_OS_PREP
| BOOTM_STATE_OS_FAKE_GO
|
1109 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH
))
1110 states
|= BOOTM_STATE_RAMDISK
;
1111 if (IS_ENABLED(CONFIG_PPC
) || IS_ENABLED(CONFIG_MIPS
))
1112 states
|= BOOTM_STATE_OS_CMDLINE
;
1113 images
.state
|= states
;
1115 snprintf(addr_str
, sizeof(addr_str
), "%lx", addr
);
1117 ret
= env_set("bootargs", cmdline
);
1119 printf("Failed to set cmdline\n");
1122 ret
= do_bootm_states(&cmd
, 0, 1, argv
, states
, &images
, 1);
1128 * switch_to_non_secure_mode() - switch to non-secure mode
1130 * This routine is overridden by architectures requiring this feature.
1132 void __weak
switch_to_non_secure_mode(void)
1136 #else /* USE_HOSTCC */
1138 #if defined(CONFIG_FIT_SIGNATURE)
1139 static int bootm_host_load_image(const void *fit
, int req_image_type
,
1142 const char *fit_uname_config
= NULL
;
1144 struct bootm_headers images
;
1146 ulong load_end
, buf_size
;
1152 fit_uname_config
= fdt_get_name(fit
, cfg_noffset
, NULL
);
1153 memset(&images
, '\0', sizeof(images
));
1155 noffset
= fit_image_load(&images
, (ulong
)fit
,
1156 NULL
, &fit_uname_config
,
1157 IH_ARCH_DEFAULT
, req_image_type
, -1,
1158 FIT_LOAD_IGNORED
, &data
, &len
);
1161 if (fit_image_get_type(fit
, noffset
, &image_type
)) {
1162 puts("Can't get image type!\n");
1166 if (fit_image_get_comp(fit
, noffset
, &image_comp
))
1167 image_comp
= IH_COMP_NONE
;
1169 /* Allow the image to expand by a factor of 4, should be safe */
1170 buf_size
= (1 << 20) + len
* 4;
1171 load_buf
= malloc(buf_size
);
1172 ret
= image_decomp(image_comp
, 0, data
, image_type
, load_buf
,
1173 (void *)data
, len
, buf_size
, &load_end
);
1177 ret
= handle_decomp_error(image_comp
, load_end
- 0, buf_size
, ret
);
1178 if (ret
!= BOOTM_ERR_UNIMPLEMENTED
)
1185 int bootm_host_load_images(const void *fit
, int cfg_noffset
)
1187 static uint8_t image_types
[] = {
1195 for (i
= 0; i
< ARRAY_SIZE(image_types
); i
++) {
1198 ret
= bootm_host_load_image(fit
, image_types
[i
], cfg_noffset
);
1199 if (!err
&& ret
&& ret
!= -ENOENT
)
1203 /* Return the first error we found */
1208 #endif /* ndef USE_HOSTCC */