1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
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
107 * @addr_fit: first argument to bootm: address, fit configuration, etc.
108 * @os_data: pointer to a ulong variable, will hold os data start address
109 * @os_len: pointer to a ulong variable, will hold os data length
110 * address and length, otherwise NULL
111 * pointer to image header if valid image was found, plus kernel start
112 * @kernp: image header if valid image was found, otherwise NULL
114 * boot_get_kernel() tries to find a kernel image, verifies its integrity
115 * and locates kernel data.
117 * Return: 0 on success, -ve on error. -EPROTOTYPE means that the image is in
118 * a wrong or unsupported format
120 static int boot_get_kernel(const char *addr_fit, struct bootm_headers *images,
121 ulong *os_data, ulong *os_len, const void **kernp)
123 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
124 struct legacy_img_hdr *hdr;
128 const char *fit_uname_config = NULL, *fit_uname_kernel = NULL;
129 #if CONFIG_IS_ENABLED(FIT)
133 #ifdef CONFIG_ANDROID_BOOT_IMAGE
134 const void *boot_img;
135 const void *vendor_boot_img;
137 img_addr = genimg_get_kernel_addr_fit(addr_fit, &fit_uname_config,
140 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
141 img_addr += image_load_offset;
143 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
145 /* check image type, for FIT images get FIT kernel node */
146 *os_data = *os_len = 0;
147 buf = map_sysmem(img_addr, 0);
148 switch (genimg_get_format(buf)) {
149 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
150 case IMAGE_FORMAT_LEGACY:
151 printf("## Booting kernel from Legacy Image at %08lx ...\n",
153 hdr = image_get_kernel(img_addr, images->verify);
156 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
158 /* get os_data and os_len */
159 switch (image_get_type(hdr)) {
161 case IH_TYPE_KERNEL_NOLOAD:
162 *os_data = image_get_data(hdr);
163 *os_len = image_get_data_size(hdr);
166 image_multi_getimg(hdr, 0, os_data, os_len);
168 case IH_TYPE_STANDALONE:
169 *os_data = image_get_data(hdr);
170 *os_len = image_get_data_size(hdr);
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: {
212 vendor_boot_img = NULL;
213 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
214 boot_img = map_sysmem(get_abootimg_addr(), 0);
215 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
217 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
218 ret = android_image_get_kernel(boot_img, vendor_boot_img,
219 images->verify, os_data, os_len);
220 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
221 unmap_sysmem(vendor_boot_img);
222 unmap_sysmem(boot_img);
230 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
234 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
235 *os_data, *os_len, *os_len);
242 static void boot_start_lmb(struct bootm_headers *images)
245 phys_size_t mem_size;
247 mem_start = env_get_bootm_low();
248 mem_size = env_get_bootm_size();
250 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
254 #define lmb_reserve(lmb, base, size)
255 static inline void boot_start_lmb(struct bootm_headers *images) { }
258 static int bootm_start(void)
260 memset((void *)&images, 0, sizeof(images));
261 images.verify = env_get_yesno("verify");
263 boot_start_lmb(&images);
265 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
266 images.state = BOOTM_STATE_START;
271 static ulong bootm_data_addr(const char *addr_str)
276 addr = hextoul(addr_str, NULL);
278 addr = image_load_addr;
284 * bootm_pre_load() - Handle the pre-load processing
286 * This can be used to do a full signature check of the image, for example.
287 * It calls image_pre_load() with the data address of the image to check.
289 * @addr_str: String containing load address in hex, or NULL to use
291 * Return: 0 if OK, CMD_RET_FAILURE on failure
293 static int bootm_pre_load(const char *addr_str)
295 ulong data_addr = bootm_data_addr(addr_str);
298 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
299 ret = image_pre_load(data_addr);
302 ret = CMD_RET_FAILURE;
308 * bootm_find_os(): Find the OS to boot
310 * @cmd_name: Command name that started this boot, e.g. "bootm"
311 * @addr_fit: Address and/or FIT specifier (first arg of bootm command)
312 * Return: 0 on success, -ve on error
314 static int bootm_find_os(const char *cmd_name, const char *addr_fit)
317 #ifdef CONFIG_ANDROID_BOOT_IMAGE
318 const void *vendor_boot_img;
319 const void *boot_img;
321 bool ep_found = false;
324 /* get kernel image header, start address and length */
325 ret = boot_get_kernel(addr_fit, &images, &images.os.image_start,
326 &images.os.image_len, &os_hdr);
328 if (ret == -EPROTOTYPE)
329 printf("Wrong Image Type for %s command\n", cmd_name);
331 printf("ERROR %dE: can't get kernel image!\n", ret);
335 /* get image parameters */
336 switch (genimg_get_format(os_hdr)) {
337 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
338 case IMAGE_FORMAT_LEGACY:
339 images.os.type = image_get_type(os_hdr);
340 images.os.comp = image_get_comp(os_hdr);
341 images.os.os = image_get_os(os_hdr);
343 images.os.end = image_get_image_end(os_hdr);
344 images.os.load = image_get_load(os_hdr);
345 images.os.arch = image_get_arch(os_hdr);
348 #if CONFIG_IS_ENABLED(FIT)
349 case IMAGE_FORMAT_FIT:
350 if (fit_image_get_type(images.fit_hdr_os,
351 images.fit_noffset_os,
353 puts("Can't get image type!\n");
354 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
358 if (fit_image_get_comp(images.fit_hdr_os,
359 images.fit_noffset_os,
361 puts("Can't get image compression!\n");
362 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
366 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
368 puts("Can't get image OS!\n");
369 bootstage_error(BOOTSTAGE_ID_FIT_OS);
373 if (fit_image_get_arch(images.fit_hdr_os,
374 images.fit_noffset_os,
376 puts("Can't get image ARCH!\n");
380 images.os.end = fit_get_end(images.fit_hdr_os);
382 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
384 puts("Can't get image load address!\n");
385 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
390 #ifdef CONFIG_ANDROID_BOOT_IMAGE
391 case IMAGE_FORMAT_ANDROID:
393 vendor_boot_img = NULL;
394 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
395 boot_img = map_sysmem(get_abootimg_addr(), 0);
396 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
398 images.os.type = IH_TYPE_KERNEL;
399 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
400 images.os.os = IH_OS_LINUX;
401 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
402 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
403 images.ep = images.os.load;
405 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
406 unmap_sysmem(vendor_boot_img);
407 unmap_sysmem(boot_img);
412 puts("ERROR: unknown image format type!\n");
416 /* If we have a valid setup.bin, we will use that for entry (x86) */
417 if (images.os.arch == IH_ARCH_I386 ||
418 images.os.arch == IH_ARCH_X86_64) {
421 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
422 if (ret < 0 && ret != -ENOENT) {
423 puts("Could not find a valid setup.bin for x86\n");
426 /* Kernel entry point is the setup.bin */
427 } else if (images.legacy_hdr_valid) {
428 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
429 #if CONFIG_IS_ENABLED(FIT)
430 } else if (images.fit_uname_os) {
433 ret = fit_image_get_entry(images.fit_hdr_os,
434 images.fit_noffset_os, &images.ep);
436 puts("Can't get entry point property!\n");
440 } else if (!ep_found) {
441 puts("Could not find kernel entry point!\n");
445 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
446 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
447 images.os.arch == IH_ARCH_ARM64 &&
448 images.os.os == IH_OS_LINUX) {
452 ret = booti_setup(images.os.image_start, &image_addr,
457 images.os.type = IH_TYPE_KERNEL;
458 images.os.load = image_addr;
459 images.ep = image_addr;
461 images.os.load = images.os.image_start;
462 images.ep += images.os.image_start;
466 images.os.start = map_to_sysmem(os_hdr);
472 * check_overlap() - Check if an image overlaps the OS
474 * @name: Name of image to check (used to print error)
475 * @base: Base address of image
476 * @end: End address of image (+1)
477 * @os_start: Start of OS
478 * @os_size: Size of OS in bytes
479 * Return: 0 if OK, -EXDEV if the image overlaps the OS
481 static int check_overlap(const char *name, ulong base, ulong end,
482 ulong os_start, ulong os_size)
488 os_end = os_start + os_size;
490 if ((base >= os_start && base < os_end) ||
491 (end > os_start && end <= os_end) ||
492 (base < os_start && end >= os_end)) {
493 printf("ERROR: %s image overlaps OS image (OS=%lx..%lx)\n",
494 name, os_start, os_end);
502 int bootm_find_images(ulong img_addr, const char *conf_ramdisk,
503 const char *conf_fdt, ulong start, ulong size)
505 const char *select = conf_ramdisk;
510 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
511 /* Look for an Android boot image */
512 buf = map_sysmem(images.os.start, 0);
513 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
514 strcpy(addr_str, simple_xtoa(img_addr));
520 select = conf_ramdisk;
523 ret = boot_get_ramdisk(select, &images, IH_INITRD_ARCH,
524 &images.rd_start, &images.rd_end);
526 puts("Ramdisk image is corrupt or invalid\n");
530 /* check if ramdisk overlaps OS image */
531 if (check_overlap("RD", images.rd_start, images.rd_end, start, size))
534 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
535 buf = map_sysmem(img_addr, 0);
537 /* find flattened device tree */
538 ret = boot_get_fdt(buf, conf_fdt, IH_ARCH_DEFAULT, &images,
539 &images.ft_addr, &images.ft_len);
541 puts("Could not find a valid device tree\n");
545 /* check if FDT overlaps OS image */
546 if (check_overlap("FDT", map_to_sysmem(images.ft_addr),
547 images.ft_len, start, size))
550 if (IS_ENABLED(CONFIG_CMD_FDT))
551 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
554 #if CONFIG_IS_ENABLED(FIT)
555 if (IS_ENABLED(CONFIG_FPGA)) {
556 /* find bitstreams */
557 ret = boot_get_fpga(&images);
559 printf("FPGA image is corrupted or invalid\n");
564 /* find all of the loadables */
565 ret = boot_get_loadable(&images);
567 printf("Loadable(s) is corrupt or invalid\n");
575 static int bootm_find_other(ulong img_addr, const char *conf_ramdisk,
576 const char *conf_fdt)
578 if ((images.os.type == IH_TYPE_KERNEL ||
579 images.os.type == IH_TYPE_KERNEL_NOLOAD ||
580 images.os.type == IH_TYPE_MULTI) &&
581 (images.os.os == IH_OS_LINUX || images.os.os == IH_OS_VXWORKS ||
582 images.os.os == IH_OS_EFI || images.os.os == IH_OS_TEE)) {
583 return bootm_find_images(img_addr, conf_ramdisk, conf_fdt, 0,
589 #endif /* USE_HOSTC */
591 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
593 * handle_decomp_error() - display a decompression error
595 * This function tries to produce a useful message. In the case where the
596 * uncompressed size is the same as the available space, we can assume that
597 * the image is too large for the buffer.
599 * @comp_type: Compression type being used (IH_COMP_...)
600 * @uncomp_size: Number of bytes uncompressed
601 * @buf_size: Number of bytes the decompresion buffer was
602 * @ret: errno error code received from compression library
603 * Return: Appropriate BOOTM_ERR_ error code
605 static int handle_decomp_error(int comp_type, size_t uncomp_size,
606 size_t buf_size, int ret)
608 const char *name = genimg_get_comp_name(comp_type);
610 /* ENOSYS means unimplemented compression type, don't reset. */
612 return BOOTM_ERR_UNIMPLEMENTED;
614 if (uncomp_size >= buf_size)
615 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
617 printf("%s: uncompress error %d\n", name, ret);
620 * The decompression routines are now safe, so will not write beyond
621 * their bounds. Probably it is not necessary to reset, but maintain
622 * the current behaviour for now.
624 printf("Must RESET board to recover\n");
626 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
629 return BOOTM_ERR_RESET;
634 static int bootm_load_os(struct bootm_headers *images, int boot_progress)
636 struct image_info os = images->os;
637 ulong load = os.load;
639 ulong blob_start = os.start;
640 ulong blob_end = os.end;
641 ulong image_start = os.image_start;
642 ulong image_len = os.image_len;
643 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
645 void *load_buf, *image_buf;
648 load_buf = map_sysmem(load, 0);
649 image_buf = map_sysmem(os.image_start, image_len);
650 err = image_decomp(os.comp, load, os.image_start, os.type,
651 load_buf, image_buf, image_len,
652 CONFIG_SYS_BOOTM_LEN, &load_end);
654 err = handle_decomp_error(os.comp, load_end - load,
655 CONFIG_SYS_BOOTM_LEN, err);
656 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
659 /* We need the decompressed image size in the next steps */
660 images->os.image_len = load_end - load;
662 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
664 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
665 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
667 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
669 if (!no_overlap && load < blob_end && load_end > blob_start) {
670 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
671 blob_start, blob_end);
672 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
675 /* Check what type of image this is. */
676 if (images->legacy_hdr_valid) {
677 if (image_get_type(&images->legacy_hdr_os_copy)
679 puts("WARNING: legacy format multi component image overwritten\n");
680 return BOOTM_ERR_OVERLAP;
682 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
683 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
684 return BOOTM_ERR_RESET;
688 lmb_reserve(&images->lmb, images->os.load, (load_end -
694 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
696 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
699 ulong bootm_disable_interrupts(void)
704 * We have reached the point of no return: we are going to
705 * overwrite all exception vector code, so we cannot easily
706 * recover from any failures any more...
708 iflag = disable_interrupts();
709 #ifdef CONFIG_NETCONSOLE
710 /* Stop the ethernet stack if NetConsole could have left it up */
714 #if defined(CONFIG_CMD_USB)
716 * turn off USB to prevent the host controller from writing to the
717 * SDRAM while Linux is booting. This could happen (at least for OHCI
718 * controller), because the HCCA (Host Controller Communication Area)
719 * lies within the SDRAM and the host controller writes continously to
720 * this area (as busmaster!). The HccaFrameNumber is for example
721 * updated every 1 ms within the HCCA structure in SDRAM! For more
722 * details see the OpenHCI specification.
729 #define CONSOLE_ARG "console="
730 #define NULL_CONSOLE (CONSOLE_ARG "ttynull")
731 #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
734 * fixup_silent_linux() - Handle silencing the linux boot if required
736 * This uses the silent_linux envvar to control whether to add/set a "console="
737 * parameter to the command line
739 * @buf: Buffer containing the string to process
740 * @maxlen: Maximum length of buffer
741 * Return: 0 if OK, -ENOSPC if @maxlen is too small
743 static int fixup_silent_linux(char *buf, int maxlen)
750 * Move the input string to the end of buffer. The output string will be
751 * built up at the start.
753 size = strlen(buf) + 1;
754 if (size * 2 > maxlen)
756 cmdline = buf + maxlen - size;
757 memmove(cmdline, buf, size);
759 * Only fix cmdline when requested. The environment variable can be:
761 * no - we never fixup
762 * yes - we always fixup
763 * unset - we rely on the console silent flag
765 want_silent = env_get_yesno("silent_linux");
766 if (want_silent == 0)
768 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
771 debug("before silent fix-up: %s\n", cmdline);
773 char *start = strstr(cmdline, CONSOLE_ARG);
775 /* Check space for maximum possible new command line */
776 if (size + CONSOLE_ARG_SIZE > maxlen)
780 char *end = strchr(start, ' ');
783 start_bytes = start - cmdline;
784 strncpy(buf, cmdline, start_bytes);
785 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
787 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
789 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
791 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
793 if (buf + strlen(buf) >= cmdline)
796 if (maxlen < CONSOLE_ARG_SIZE)
798 strcpy(buf, NULL_CONSOLE);
800 debug("after silent fix-up: %s\n", buf);
806 * process_subst() - Handle substitution of ${...} fields in the environment
808 * Handle variable substitution in the provided buffer
810 * @buf: Buffer containing the string to process
811 * @maxlen: Maximum length of buffer
812 * Return: 0 if OK, -ENOSPC if @maxlen is too small
814 static int process_subst(char *buf, int maxlen)
820 /* Move to end of buffer */
821 size = strlen(buf) + 1;
822 cmdline = buf + maxlen - size;
823 if (buf + size > cmdline)
825 memmove(cmdline, buf, size);
827 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
832 int bootm_process_cmdline(char *buf, int maxlen, int flags)
836 /* Check config first to enable compiler to eliminate code */
837 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
838 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
839 (flags & BOOTM_CL_SILENT)) {
840 ret = fixup_silent_linux(buf, maxlen);
842 return log_msg_ret("silent", ret);
844 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
845 (flags & BOOTM_CL_SUBST)) {
846 ret = process_subst(buf, maxlen);
848 return log_msg_ret("subst", ret);
854 int bootm_process_cmdline_env(int flags)
856 const int maxlen = MAX_CMDLINE_SIZE;
862 /* First check if any action is needed */
863 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
864 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
865 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
868 env = env_get("bootargs");
869 if (env && strlen(env) >= maxlen)
871 buf = malloc(maxlen);
878 ret = bootm_process_cmdline(buf, maxlen, flags);
880 ret = env_set("bootargs", buf);
883 * If buf is "" and bootargs does not exist, this will produce
884 * an error trying to delete bootargs. Ignore it
891 return log_msg_ret("env", ret);
896 int bootm_measure(struct bootm_headers *images)
900 /* Skip measurement if EFI is going to do it */
901 if (images->os.os == IH_OS_EFI &&
902 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
903 IS_ENABLED(CONFIG_BOOTM_EFI))
906 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
907 struct tcg2_event_log elog;
916 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
917 ret = tcg2_measurement_init(&dev, &elog, ign);
921 image_buf = map_sysmem(images->os.image_start,
922 images->os.image_len);
923 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
924 image_buf, EV_COMPACT_HASH,
925 strlen("linux") + 1, (u8 *)"linux");
929 rd_len = images->rd_end - images->rd_start;
930 initrd_buf = map_sysmem(images->rd_start, rd_len);
931 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
932 EV_COMPACT_HASH, strlen("initrd") + 1,
937 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
938 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
939 (u8 *)images->ft_addr,
947 s = env_get("bootargs");
950 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
951 EV_PLATFORM_CONFIG_FLAGS,
952 strlen(s) + 1, (u8 *)s);
955 unmap_sysmem(initrd_buf);
958 unmap_sysmem(image_buf);
959 tcg2_measurement_term(dev, &elog, ret != 0);
966 * Execute selected states of the bootm command.
968 * Note the arguments to this state must be the first argument, Any 'bootm'
969 * or sub-command arguments must have already been taken.
971 * Note that if states contains more than one flag it MUST contain
972 * BOOTM_STATE_START, since this handles and consumes the command line args.
974 * Also note that aside from boot_os_fn functions and bootm_load_os no other
975 * functions we store the return value of in 'ret' may use a negative return
976 * value, without special handling.
978 * @param cmdtp Pointer to bootm command table entry
979 * @param flag Command flags (CMD_FLAG_...)
980 * @param argc Number of subcommand arguments (0 = no arguments)
981 * @param argv Arguments
982 * @param states Mask containing states to run (BOOTM_STATE_...)
983 * @param images Image header information
984 * @param boot_progress 1 to show boot progress, 0 to not do this
985 * Return: 0 if ok, something else on error. Some errors will cause this
986 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
987 * then the intent is to boot an OS, so this function will not return
988 * unless the image type is standalone.
990 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
991 char *const argv[], int states, struct bootm_headers *images,
996 int ret = 0, need_boot_fn;
998 images->state |= states;
1001 * Work through the states and see how far we get. We stop on
1004 if (states & BOOTM_STATE_START)
1005 ret = bootm_start();
1007 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
1008 ret = bootm_pre_load(argv[0]);
1010 if (!ret && (states & BOOTM_STATE_FINDOS))
1011 ret = bootm_find_os(cmdtp->name, argv[0]);
1013 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
1016 img_addr = argc ? hextoul(argv[0], NULL) : image_load_addr;
1017 ret = bootm_find_other(img_addr, cmd_arg1(argc, argv),
1018 cmd_arg2(argc, argv));
1021 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
1022 (states & BOOTM_STATE_MEASURE))
1023 bootm_measure(images);
1026 if (!ret && (states & BOOTM_STATE_LOADOS)) {
1027 iflag = bootm_disable_interrupts();
1028 ret = bootm_load_os(images, 0);
1029 if (ret && ret != BOOTM_ERR_OVERLAP)
1031 else if (ret == BOOTM_ERR_OVERLAP)
1035 /* Relocate the ramdisk */
1036 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1037 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
1038 ulong rd_len = images->rd_end - images->rd_start;
1040 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
1041 rd_len, &images->initrd_start, &images->initrd_end);
1043 env_set_hex("initrd_start", images->initrd_start);
1044 env_set_hex("initrd_end", images->initrd_end);
1048 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
1049 if (!ret && (states & BOOTM_STATE_FDT)) {
1050 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
1051 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
1056 /* From now on, we need the OS boot function */
1059 boot_fn = bootm_os_get_boot_func(images->os.os);
1060 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
1061 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
1062 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
1063 if (boot_fn == NULL && need_boot_fn) {
1065 enable_interrupts();
1066 printf("ERROR: booting os '%s' (%d) is not supported\n",
1067 genimg_get_os_name(images->os.os), images->os.os);
1068 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
1073 /* Call various other states that are not generally used */
1074 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
1075 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
1076 if (!ret && (states & BOOTM_STATE_OS_BD_T))
1077 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
1078 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
1079 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
1081 printf("Cmdline setup failed (err=%d)\n", ret);
1082 ret = CMD_RET_FAILURE;
1085 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
1089 /* Pretend to run the OS, then run a user command */
1090 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
1091 char *cmd_list = env_get("fakegocmd");
1093 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
1095 if (!ret && cmd_list)
1096 ret = run_command_list(cmd_list, -1, flag);
1100 /* Check for unsupported subcommand. */
1102 printf("subcommand failed (err=%d)\n", ret);
1106 /* Now run the OS! We hope this doesn't return */
1107 if (!ret && (states & BOOTM_STATE_OS_GO))
1108 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
1111 /* Deal with any fallout */
1114 enable_interrupts();
1116 if (ret == BOOTM_ERR_UNIMPLEMENTED)
1117 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
1118 else if (ret == BOOTM_ERR_RESET)
1119 do_reset(cmdtp, flag, argc, argv);
1124 int bootm_boot_start(ulong addr, const char *cmdline)
1126 static struct cmd_tbl cmd = {"bootm"};
1128 char *argv[] = {addr_str, NULL};
1134 * should not. To clean this up, the various bootm states need to be
1135 * passed an info structure instead of cmdline flags. Then this can
1136 * set up the required info and move through the states without needing
1139 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
1140 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
1141 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1143 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
1144 states |= BOOTM_STATE_RAMDISK;
1145 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
1146 states |= BOOTM_STATE_OS_CMDLINE;
1147 images.state |= states;
1149 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
1151 ret = env_set("bootargs", cmdline);
1153 printf("Failed to set cmdline\n");
1156 ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1);
1162 * switch_to_non_secure_mode() - switch to non-secure mode
1164 * This routine is overridden by architectures requiring this feature.
1166 void __weak switch_to_non_secure_mode(void)
1170 #else /* USE_HOSTCC */
1172 #if defined(CONFIG_FIT_SIGNATURE)
1173 static int bootm_host_load_image(const void *fit, int req_image_type,
1176 const char *fit_uname_config = NULL;
1178 struct bootm_headers images;
1180 ulong load_end, buf_size;
1186 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1187 memset(&images, '\0', sizeof(images));
1189 noffset = fit_image_load(&images, (ulong)fit,
1190 NULL, &fit_uname_config,
1191 IH_ARCH_DEFAULT, req_image_type, -1,
1192 FIT_LOAD_IGNORED, &data, &len);
1195 if (fit_image_get_type(fit, noffset, &image_type)) {
1196 puts("Can't get image type!\n");
1200 if (fit_image_get_comp(fit, noffset, &image_comp))
1201 image_comp = IH_COMP_NONE;
1203 /* Allow the image to expand by a factor of 4, should be safe */
1204 buf_size = (1 << 20) + len * 4;
1205 load_buf = malloc(buf_size);
1206 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
1207 (void *)data, len, buf_size, &load_end);
1211 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
1212 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1219 int bootm_host_load_images(const void *fit, int cfg_noffset)
1221 static uint8_t image_types[] = {
1229 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1232 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
1233 if (!err && ret && ret != -ENOENT)
1237 /* Return the first error we found */
1242 #endif /* ndef USE_HOSTCC */