1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
10 #include <bootstage.h>
16 #include <fdt_support.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
26 #include <linux/sizes.h>
28 #if defined(CONFIG_CMD_USB)
38 #define MAX_CMDLINE_SIZE SZ_4K
40 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
44 DECLARE_GLOBAL_DATA_PTR;
46 struct bootm_headers images; /* pointers to os/initrd/fdt images */
48 __weak void board_quiesce_devices(void)
52 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
54 * image_get_kernel - verify legacy format kernel image
55 * @img_addr: in RAM address of the legacy format image to be verified
56 * @verify: data CRC verification flag
58 * image_get_kernel() verifies legacy image integrity and returns pointer to
59 * legacy image header if image verification was completed successfully.
62 * pointer to a legacy image header if valid image was found
63 * otherwise return NULL
65 static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
67 struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
69 if (!image_check_magic(hdr)) {
70 puts("Bad Magic Number\n");
71 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
74 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
76 if (!image_check_hcrc(hdr)) {
77 puts("Bad Header Checksum\n");
78 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
82 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
83 image_print_contents(hdr);
86 puts(" Verifying Checksum ... ");
87 if (!image_check_dcrc(hdr)) {
88 printf("Bad Data CRC\n");
89 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
94 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
96 if (!image_check_target_arch(hdr)) {
97 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
98 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
106 * boot_get_kernel() - find kernel image
108 * @addr_fit: first argument to bootm: address, fit configuration, etc.
109 * @os_data: pointer to a ulong variable, will hold os data start address
110 * @os_len: pointer to a ulong variable, will hold os data length
111 * address and length, otherwise NULL
112 * pointer to image header if valid image was found, plus kernel start
113 * @kernp: image header if valid image was found, otherwise NULL
115 * boot_get_kernel() tries to find a kernel image, verifies its integrity
116 * and locates kernel data.
118 * Return: 0 on success, -ve on error. -EPROTOTYPE means that the image is in
119 * a wrong or unsupported format
121 static int boot_get_kernel(const char *addr_fit, struct bootm_headers *images,
122 ulong *os_data, ulong *os_len, const void **kernp)
124 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
125 struct legacy_img_hdr *hdr;
129 const char *fit_uname_config = NULL, *fit_uname_kernel = NULL;
130 #if CONFIG_IS_ENABLED(FIT)
134 #ifdef CONFIG_ANDROID_BOOT_IMAGE
135 const void *boot_img;
136 const void *vendor_boot_img;
138 img_addr = genimg_get_kernel_addr_fit(addr_fit, &fit_uname_config,
141 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
142 img_addr += image_load_offset;
144 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
146 /* check image type, for FIT images get FIT kernel node */
147 *os_data = *os_len = 0;
148 buf = map_sysmem(img_addr, 0);
149 switch (genimg_get_format(buf)) {
150 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
151 case IMAGE_FORMAT_LEGACY:
152 printf("## Booting kernel from Legacy Image at %08lx ...\n",
154 hdr = image_get_kernel(img_addr, images->verify);
157 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
159 /* get os_data and os_len */
160 switch (image_get_type(hdr)) {
162 case IH_TYPE_KERNEL_NOLOAD:
163 *os_data = image_get_data(hdr);
164 *os_len = image_get_data_size(hdr);
167 image_multi_getimg(hdr, 0, os_data, os_len);
169 case IH_TYPE_STANDALONE:
170 *os_data = image_get_data(hdr);
171 *os_len = image_get_data_size(hdr);
174 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
179 * copy image header to allow for image overwrites during
180 * kernel decompression.
182 memmove(&images->legacy_hdr_os_copy, hdr,
183 sizeof(struct legacy_img_hdr));
185 /* save pointer to image header */
186 images->legacy_hdr_os = hdr;
188 images->legacy_hdr_valid = 1;
189 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
192 #if CONFIG_IS_ENABLED(FIT)
193 case IMAGE_FORMAT_FIT:
194 os_noffset = fit_image_load(images, img_addr,
195 &fit_uname_kernel, &fit_uname_config,
196 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
197 BOOTSTAGE_ID_FIT_KERNEL_START,
198 FIT_LOAD_IGNORED, os_data, os_len);
202 images->fit_hdr_os = map_sysmem(img_addr, 0);
203 images->fit_uname_os = fit_uname_kernel;
204 images->fit_uname_cfg = fit_uname_config;
205 images->fit_noffset_os = os_noffset;
208 #ifdef CONFIG_ANDROID_BOOT_IMAGE
209 case IMAGE_FORMAT_ANDROID: {
213 vendor_boot_img = NULL;
214 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
215 boot_img = map_sysmem(get_abootimg_addr(), 0);
216 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
218 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
219 ret = android_image_get_kernel(boot_img, vendor_boot_img,
220 images->verify, os_data, os_len);
221 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
222 unmap_sysmem(vendor_boot_img);
223 unmap_sysmem(boot_img);
231 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
235 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
236 *os_data, *os_len, *os_len);
243 static void boot_start_lmb(struct bootm_headers *images)
246 phys_size_t mem_size;
248 mem_start = env_get_bootm_low();
249 mem_size = env_get_bootm_size();
251 lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
255 #define lmb_reserve(lmb, base, size)
256 static inline void boot_start_lmb(struct bootm_headers *images) { }
259 static int bootm_start(void)
261 memset((void *)&images, 0, sizeof(images));
262 images.verify = env_get_yesno("verify");
264 boot_start_lmb(&images);
266 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
267 images.state = BOOTM_STATE_START;
272 static ulong bootm_data_addr(const char *addr_str)
277 addr = hextoul(addr_str, NULL);
279 addr = image_load_addr;
285 * bootm_pre_load() - Handle the pre-load processing
287 * This can be used to do a full signature check of the image, for example.
288 * It calls image_pre_load() with the data address of the image to check.
290 * @addr_str: String containing load address in hex, or NULL to use
292 * Return: 0 if OK, CMD_RET_FAILURE on failure
294 static int bootm_pre_load(const char *addr_str)
296 ulong data_addr = bootm_data_addr(addr_str);
299 if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
300 ret = image_pre_load(data_addr);
303 ret = CMD_RET_FAILURE;
309 * bootm_find_os(): Find the OS to boot
311 * @cmd_name: Command name that started this boot, e.g. "bootm"
312 * @addr_fit: Address and/or FIT specifier (first arg of bootm command)
313 * Return: 0 on success, -ve on error
315 static int bootm_find_os(const char *cmd_name, const char *addr_fit)
318 #ifdef CONFIG_ANDROID_BOOT_IMAGE
319 const void *vendor_boot_img;
320 const void *boot_img;
322 bool ep_found = false;
325 /* get kernel image header, start address and length */
326 ret = boot_get_kernel(addr_fit, &images, &images.os.image_start,
327 &images.os.image_len, &os_hdr);
329 if (ret == -EPROTOTYPE)
330 printf("Wrong Image Type for %s command\n", cmd_name);
332 printf("ERROR %dE: can't get kernel image!\n", ret);
336 /* get image parameters */
337 switch (genimg_get_format(os_hdr)) {
338 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
339 case IMAGE_FORMAT_LEGACY:
340 images.os.type = image_get_type(os_hdr);
341 images.os.comp = image_get_comp(os_hdr);
342 images.os.os = image_get_os(os_hdr);
344 images.os.end = image_get_image_end(os_hdr);
345 images.os.load = image_get_load(os_hdr);
346 images.os.arch = image_get_arch(os_hdr);
349 #if CONFIG_IS_ENABLED(FIT)
350 case IMAGE_FORMAT_FIT:
351 if (fit_image_get_type(images.fit_hdr_os,
352 images.fit_noffset_os,
354 puts("Can't get image type!\n");
355 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
359 if (fit_image_get_comp(images.fit_hdr_os,
360 images.fit_noffset_os,
362 puts("Can't get image compression!\n");
363 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
367 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
369 puts("Can't get image OS!\n");
370 bootstage_error(BOOTSTAGE_ID_FIT_OS);
374 if (fit_image_get_arch(images.fit_hdr_os,
375 images.fit_noffset_os,
377 puts("Can't get image ARCH!\n");
381 images.os.end = fit_get_end(images.fit_hdr_os);
383 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
385 puts("Can't get image load address!\n");
386 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
391 #ifdef CONFIG_ANDROID_BOOT_IMAGE
392 case IMAGE_FORMAT_ANDROID:
394 vendor_boot_img = NULL;
395 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
396 boot_img = map_sysmem(get_abootimg_addr(), 0);
397 vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
399 images.os.type = IH_TYPE_KERNEL;
400 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
401 images.os.os = IH_OS_LINUX;
402 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
403 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
404 images.ep = images.os.load;
406 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
407 unmap_sysmem(vendor_boot_img);
408 unmap_sysmem(boot_img);
413 puts("ERROR: unknown image format type!\n");
417 /* If we have a valid setup.bin, we will use that for entry (x86) */
418 if (images.os.arch == IH_ARCH_I386 ||
419 images.os.arch == IH_ARCH_X86_64) {
422 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
423 if (ret < 0 && ret != -ENOENT) {
424 puts("Could not find a valid setup.bin for x86\n");
427 /* Kernel entry point is the setup.bin */
428 } else if (images.legacy_hdr_valid) {
429 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
430 #if CONFIG_IS_ENABLED(FIT)
431 } else if (images.fit_uname_os) {
434 ret = fit_image_get_entry(images.fit_hdr_os,
435 images.fit_noffset_os, &images.ep);
437 puts("Can't get entry point property!\n");
441 } else if (!ep_found) {
442 puts("Could not find kernel entry point!\n");
446 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
447 images.os.load = images.os.image_start;
448 images.ep += images.os.image_start;
451 images.os.start = map_to_sysmem(os_hdr);
457 * check_overlap() - Check if an image overlaps the OS
459 * @name: Name of image to check (used to print error)
460 * @base: Base address of image
461 * @end: End address of image (+1)
462 * @os_start: Start of OS
463 * @os_size: Size of OS in bytes
464 * Return: 0 if OK, -EXDEV if the image overlaps the OS
466 static int check_overlap(const char *name, ulong base, ulong end,
467 ulong os_start, ulong os_size)
473 os_end = os_start + os_size;
475 if ((base >= os_start && base < os_end) ||
476 (end > os_start && end <= os_end) ||
477 (base < os_start && end >= os_end)) {
478 printf("ERROR: %s image overlaps OS image (OS=%lx..%lx)\n",
479 name, os_start, os_end);
487 int bootm_find_images(ulong img_addr, const char *conf_ramdisk,
488 const char *conf_fdt, ulong start, ulong size)
490 const char *select = conf_ramdisk;
495 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
496 /* Look for an Android boot image */
497 buf = map_sysmem(images.os.start, 0);
498 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
499 strcpy(addr_str, simple_xtoa(img_addr));
505 select = conf_ramdisk;
508 ret = boot_get_ramdisk(select, &images, IH_INITRD_ARCH,
509 &images.rd_start, &images.rd_end);
511 puts("Ramdisk image is corrupt or invalid\n");
515 /* check if ramdisk overlaps OS image */
516 if (check_overlap("RD", images.rd_start, images.rd_end, start, size))
519 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
520 buf = map_sysmem(img_addr, 0);
522 /* find flattened device tree */
523 ret = boot_get_fdt(buf, conf_fdt, IH_ARCH_DEFAULT, &images,
524 &images.ft_addr, &images.ft_len);
526 puts("Could not find a valid device tree\n");
530 /* check if FDT overlaps OS image */
531 if (check_overlap("FDT", map_to_sysmem(images.ft_addr),
532 images.ft_len, start, size))
535 if (IS_ENABLED(CONFIG_CMD_FDT))
536 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
539 #if CONFIG_IS_ENABLED(FIT)
540 if (IS_ENABLED(CONFIG_FPGA)) {
541 /* find bitstreams */
542 ret = boot_get_fpga(&images);
544 printf("FPGA image is corrupted or invalid\n");
549 /* find all of the loadables */
550 ret = boot_get_loadable(&images);
552 printf("Loadable(s) is corrupt or invalid\n");
560 static int bootm_find_other(ulong img_addr, const char *conf_ramdisk,
561 const char *conf_fdt)
563 if ((images.os.type == IH_TYPE_KERNEL ||
564 images.os.type == IH_TYPE_KERNEL_NOLOAD ||
565 images.os.type == IH_TYPE_MULTI) &&
566 (images.os.os == IH_OS_LINUX || images.os.os == IH_OS_VXWORKS ||
567 images.os.os == IH_OS_EFI || images.os.os == IH_OS_TEE)) {
568 return bootm_find_images(img_addr, conf_ramdisk, conf_fdt, 0,
574 #endif /* USE_HOSTC */
576 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
578 * handle_decomp_error() - display a decompression error
580 * This function tries to produce a useful message. In the case where the
581 * uncompressed size is the same as the available space, we can assume that
582 * the image is too large for the buffer.
584 * @comp_type: Compression type being used (IH_COMP_...)
585 * @uncomp_size: Number of bytes uncompressed
586 * @buf_size: Number of bytes the decompresion buffer was
587 * @ret: errno error code received from compression library
588 * Return: Appropriate BOOTM_ERR_ error code
590 static int handle_decomp_error(int comp_type, size_t uncomp_size,
591 size_t buf_size, int ret)
593 const char *name = genimg_get_comp_name(comp_type);
595 /* ENOSYS means unimplemented compression type, don't reset. */
597 return BOOTM_ERR_UNIMPLEMENTED;
599 if (uncomp_size >= buf_size)
600 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
602 printf("%s: uncompress error %d\n", name, ret);
605 * The decompression routines are now safe, so will not write beyond
606 * their bounds. Probably it is not necessary to reset, but maintain
607 * the current behaviour for now.
609 printf("Must RESET board to recover\n");
611 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
614 return BOOTM_ERR_RESET;
619 static int bootm_load_os(struct bootm_headers *images, int boot_progress)
621 struct image_info os = images->os;
622 ulong load = os.load;
624 ulong blob_start = os.start;
625 ulong blob_end = os.end;
626 ulong image_start = os.image_start;
627 ulong image_len = os.image_len;
628 ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
630 void *load_buf, *image_buf;
634 * For a "noload" compressed kernel we need to allocate a buffer large
635 * enough to decompress in to and use that as the load address now.
636 * Assume that the kernel compression is at most a factor of 4 since
637 * zstd almost achieves that.
638 * Use an alignment of 2MB since this might help arm64
640 if (os.type == IH_TYPE_KERNEL_NOLOAD && os.comp != IH_COMP_NONE) {
641 ulong req_size = ALIGN(image_len * 4, SZ_1M);
643 load = lmb_alloc(&images->lmb, req_size, SZ_2M);
648 debug("Allocated %lx bytes at %lx for kernel (size %lx) decompression\n",
649 req_size, load, image_len);
652 load_buf = map_sysmem(load, 0);
653 image_buf = map_sysmem(os.image_start, image_len);
654 err = image_decomp(os.comp, load, os.image_start, os.type,
655 load_buf, image_buf, image_len,
656 CONFIG_SYS_BOOTM_LEN, &load_end);
658 err = handle_decomp_error(os.comp, load_end - load,
659 CONFIG_SYS_BOOTM_LEN, err);
660 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
663 /* We need the decompressed image size in the next steps */
664 images->os.image_len = load_end - load;
666 flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
668 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
669 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
671 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
673 if (!no_overlap && load < blob_end && load_end > blob_start) {
674 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
675 blob_start, blob_end);
676 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
679 /* Check what type of image this is. */
680 if (images->legacy_hdr_valid) {
681 if (image_get_type(&images->legacy_hdr_os_copy)
683 puts("WARNING: legacy format multi component image overwritten\n");
684 return BOOTM_ERR_OVERLAP;
686 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
687 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
688 return BOOTM_ERR_RESET;
692 if (IS_ENABLED(CONFIG_CMD_BOOTI) && images->os.arch == IH_ARCH_ARM64 &&
693 images->os.os == IH_OS_LINUX) {
694 ulong relocated_addr;
698 ret = booti_setup(load, &relocated_addr, &image_size, false);
700 printf("Failed to prep arm64 kernel (err=%d)\n", ret);
701 return BOOTM_ERR_RESET;
704 /* Handle BOOTM_STATE_LOADOS */
705 if (relocated_addr != load) {
706 printf("Moving Image from 0x%lx to 0x%lx, end=%lx\n",
707 load, relocated_addr,
708 relocated_addr + image_size);
709 memmove((void *)relocated_addr, load_buf, image_size);
712 images->ep = relocated_addr;
713 images->os.start = relocated_addr;
714 images->os.end = relocated_addr + image_size;
717 lmb_reserve(&images->lmb, images->os.load, (load_end -
723 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
725 * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
728 ulong bootm_disable_interrupts(void)
733 * We have reached the point of no return: we are going to
734 * overwrite all exception vector code, so we cannot easily
735 * recover from any failures any more...
737 iflag = disable_interrupts();
738 #ifdef CONFIG_NETCONSOLE
739 /* Stop the ethernet stack if NetConsole could have left it up */
743 #if defined(CONFIG_CMD_USB)
745 * turn off USB to prevent the host controller from writing to the
746 * SDRAM while Linux is booting. This could happen (at least for OHCI
747 * controller), because the HCCA (Host Controller Communication Area)
748 * lies within the SDRAM and the host controller writes continously to
749 * this area (as busmaster!). The HccaFrameNumber is for example
750 * updated every 1 ms within the HCCA structure in SDRAM! For more
751 * details see the OpenHCI specification.
758 #define CONSOLE_ARG "console="
759 #define NULL_CONSOLE (CONSOLE_ARG "ttynull")
760 #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE)
763 * fixup_silent_linux() - Handle silencing the linux boot if required
765 * This uses the silent_linux envvar to control whether to add/set a "console="
766 * parameter to the command line
768 * @buf: Buffer containing the string to process
769 * @maxlen: Maximum length of buffer
770 * Return: 0 if OK, -ENOSPC if @maxlen is too small
772 static int fixup_silent_linux(char *buf, int maxlen)
779 * Move the input string to the end of buffer. The output string will be
780 * built up at the start.
782 size = strlen(buf) + 1;
783 if (size * 2 > maxlen)
785 cmdline = buf + maxlen - size;
786 memmove(cmdline, buf, size);
788 * Only fix cmdline when requested. The environment variable can be:
790 * no - we never fixup
791 * yes - we always fixup
792 * unset - we rely on the console silent flag
794 want_silent = env_get_yesno("silent_linux");
795 if (want_silent == 0)
797 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
800 debug("before silent fix-up: %s\n", cmdline);
802 char *start = strstr(cmdline, CONSOLE_ARG);
804 /* Check space for maximum possible new command line */
805 if (size + CONSOLE_ARG_SIZE > maxlen)
809 char *end = strchr(start, ' ');
812 start_bytes = start - cmdline;
813 strncpy(buf, cmdline, start_bytes);
814 strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
816 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
818 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
820 sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
822 if (buf + strlen(buf) >= cmdline)
825 if (maxlen < CONSOLE_ARG_SIZE)
827 strcpy(buf, NULL_CONSOLE);
829 debug("after silent fix-up: %s\n", buf);
835 * process_subst() - Handle substitution of ${...} fields in the environment
837 * Handle variable substitution in the provided buffer
839 * @buf: Buffer containing the string to process
840 * @maxlen: Maximum length of buffer
841 * Return: 0 if OK, -ENOSPC if @maxlen is too small
843 static int process_subst(char *buf, int maxlen)
849 /* Move to end of buffer */
850 size = strlen(buf) + 1;
851 cmdline = buf + maxlen - size;
852 if (buf + size > cmdline)
854 memmove(cmdline, buf, size);
856 ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
861 int bootm_process_cmdline(char *buf, int maxlen, int flags)
865 /* Check config first to enable compiler to eliminate code */
866 if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
867 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
868 (flags & BOOTM_CL_SILENT)) {
869 ret = fixup_silent_linux(buf, maxlen);
871 return log_msg_ret("silent", ret);
873 if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
874 (flags & BOOTM_CL_SUBST)) {
875 ret = process_subst(buf, maxlen);
877 return log_msg_ret("subst", ret);
883 int bootm_process_cmdline_env(int flags)
885 const int maxlen = MAX_CMDLINE_SIZE;
891 /* First check if any action is needed */
892 do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
893 !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
894 if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
897 env = env_get("bootargs");
898 if (env && strlen(env) >= maxlen)
900 buf = malloc(maxlen);
907 ret = bootm_process_cmdline(buf, maxlen, flags);
909 ret = env_set("bootargs", buf);
912 * If buf is "" and bootargs does not exist, this will produce
913 * an error trying to delete bootargs. Ignore it
920 return log_msg_ret("env", ret);
925 int bootm_measure(struct bootm_headers *images)
929 /* Skip measurement if EFI is going to do it */
930 if (images->os.os == IH_OS_EFI &&
931 IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) &&
932 IS_ENABLED(CONFIG_BOOTM_EFI))
935 if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
936 struct tcg2_event_log elog;
945 ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
946 ret = tcg2_measurement_init(&dev, &elog, ign);
950 image_buf = map_sysmem(images->os.image_start,
951 images->os.image_len);
952 ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
953 image_buf, EV_COMPACT_HASH,
954 strlen("linux") + 1, (u8 *)"linux");
958 rd_len = images->rd_end - images->rd_start;
959 initrd_buf = map_sysmem(images->rd_start, rd_len);
960 ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
961 EV_COMPACT_HASH, strlen("initrd") + 1,
966 if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
967 ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
968 (u8 *)images->ft_addr,
976 s = env_get("bootargs");
979 ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
980 EV_PLATFORM_CONFIG_FLAGS,
981 strlen(s) + 1, (u8 *)s);
984 unmap_sysmem(initrd_buf);
987 unmap_sysmem(image_buf);
988 tcg2_measurement_term(dev, &elog, ret != 0);
994 int bootm_run_states(struct bootm_info *bmi, int states)
996 struct bootm_headers *images = bmi->images;
999 int ret = 0, need_boot_fn;
1001 images->state |= states;
1004 * Work through the states and see how far we get. We stop on
1007 if (states & BOOTM_STATE_START)
1008 ret = bootm_start();
1010 if (!ret && (states & BOOTM_STATE_PRE_LOAD))
1011 ret = bootm_pre_load(bmi->addr_img);
1013 if (!ret && (states & BOOTM_STATE_FINDOS))
1014 ret = bootm_find_os(bmi->cmd_name, bmi->addr_img);
1016 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
1019 img_addr = bmi->addr_img ? hextoul(bmi->addr_img, NULL)
1021 ret = bootm_find_other(img_addr, bmi->conf_ramdisk,
1025 if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
1026 (states & BOOTM_STATE_MEASURE))
1027 bootm_measure(images);
1030 if (!ret && (states & BOOTM_STATE_LOADOS)) {
1031 iflag = bootm_disable_interrupts();
1032 ret = bootm_load_os(images, 0);
1033 if (ret && ret != BOOTM_ERR_OVERLAP)
1035 else if (ret == BOOTM_ERR_OVERLAP)
1039 /* Relocate the ramdisk */
1040 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1041 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
1042 ulong rd_len = images->rd_end - images->rd_start;
1044 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
1045 rd_len, &images->initrd_start, &images->initrd_end);
1047 env_set_hex("initrd_start", images->initrd_start);
1048 env_set_hex("initrd_end", images->initrd_end);
1052 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
1053 if (!ret && (states & BOOTM_STATE_FDT)) {
1054 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
1055 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
1060 /* From now on, we need the OS boot function */
1063 boot_fn = bootm_os_get_boot_func(images->os.os);
1064 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
1065 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
1066 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
1067 if (boot_fn == NULL && need_boot_fn) {
1069 enable_interrupts();
1070 printf("ERROR: booting os '%s' (%d) is not supported\n",
1071 genimg_get_os_name(images->os.os), images->os.os);
1072 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
1076 /* Call various other states that are not generally used */
1077 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
1078 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, bmi);
1079 if (!ret && (states & BOOTM_STATE_OS_BD_T))
1080 ret = boot_fn(BOOTM_STATE_OS_BD_T, bmi);
1081 if (!ret && (states & BOOTM_STATE_OS_PREP)) {
1083 /* For Linux OS do all substitutions at console processing */
1084 if (images->os.os == IH_OS_LINUX)
1085 flags = BOOTM_CL_ALL;
1086 ret = bootm_process_cmdline_env(flags);
1088 printf("Cmdline setup failed (err=%d)\n", ret);
1089 ret = CMD_RET_FAILURE;
1092 ret = boot_fn(BOOTM_STATE_OS_PREP, bmi);
1096 /* Pretend to run the OS, then run a user command */
1097 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
1098 char *cmd_list = env_get("fakegocmd");
1100 ret = boot_selected_os(BOOTM_STATE_OS_FAKE_GO, bmi, boot_fn);
1101 if (!ret && cmd_list)
1102 ret = run_command_list(cmd_list, -1, 0);
1106 /* Check for unsupported subcommand. */
1108 printf("subcommand failed (err=%d)\n", ret);
1112 /* Now run the OS! We hope this doesn't return */
1113 if (!ret && (states & BOOTM_STATE_OS_GO))
1114 ret = boot_selected_os(BOOTM_STATE_OS_GO, bmi, boot_fn);
1116 /* Deal with any fallout */
1119 enable_interrupts();
1121 if (ret == BOOTM_ERR_UNIMPLEMENTED) {
1122 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
1123 } else if (ret == BOOTM_ERR_RESET) {
1124 printf("Resetting the board...\n");
1131 int boot_run(struct bootm_info *bmi, const char *cmd, int extra_states)
1135 bmi->cmd_name = cmd;
1136 states = BOOTM_STATE_MEASURE | BOOTM_STATE_OS_PREP |
1137 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO;
1138 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
1139 states |= BOOTM_STATE_RAMDISK;
1140 states |= extra_states;
1142 return bootm_run_states(bmi, states);
1145 int bootm_run(struct bootm_info *bmi)
1147 return boot_run(bmi, "bootm", BOOTM_STATE_START | BOOTM_STATE_FINDOS |
1148 BOOTM_STATE_PRE_LOAD | BOOTM_STATE_FINDOTHER |
1149 BOOTM_STATE_LOADOS);
1152 int bootz_run(struct bootm_info *bmi)
1154 return boot_run(bmi, "bootz", 0);
1157 int booti_run(struct bootm_info *bmi)
1159 return boot_run(bmi, "booti", 0);
1162 int bootm_boot_start(ulong addr, const char *cmdline)
1165 struct bootm_info bmi;
1169 states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD |
1170 BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS |
1171 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1173 if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
1174 states |= BOOTM_STATE_RAMDISK;
1175 if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
1176 states |= BOOTM_STATE_OS_CMDLINE;
1177 images.state |= states;
1179 snprintf(addr_str, sizeof(addr_str), "%lx", addr);
1181 ret = env_set("bootargs", cmdline);
1183 printf("Failed to set cmdline\n");
1187 bmi.addr_img = addr_str;
1188 bmi.cmd_name = "bootm";
1189 ret = bootm_run_states(&bmi, states);
1194 void bootm_init(struct bootm_info *bmi)
1196 memset(bmi, '\0', sizeof(struct bootm_info));
1197 bmi->boot_progress = true;
1198 if (IS_ENABLED(CONFIG_CMD_BOOTM))
1199 bmi->images = &images;
1203 * switch_to_non_secure_mode() - switch to non-secure mode
1205 * This routine is overridden by architectures requiring this feature.
1207 void __weak switch_to_non_secure_mode(void)
1211 #else /* USE_HOSTCC */
1213 #if defined(CONFIG_FIT_SIGNATURE)
1214 static int bootm_host_load_image(const void *fit, int req_image_type,
1217 const char *fit_uname_config = NULL;
1219 struct bootm_headers images;
1221 ulong load_end, buf_size;
1227 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1228 memset(&images, '\0', sizeof(images));
1230 noffset = fit_image_load(&images, (ulong)fit,
1231 NULL, &fit_uname_config,
1232 IH_ARCH_DEFAULT, req_image_type, -1,
1233 FIT_LOAD_IGNORED, &data, &len);
1236 if (fit_image_get_type(fit, noffset, &image_type)) {
1237 puts("Can't get image type!\n");
1241 if (fit_image_get_comp(fit, noffset, &image_comp))
1242 image_comp = IH_COMP_NONE;
1244 /* Allow the image to expand by a factor of 4, should be safe */
1245 buf_size = (1 << 20) + len * 4;
1246 load_buf = malloc(buf_size);
1247 ret = image_decomp(image_comp, 0, data, image_type, load_buf,
1248 (void *)data, len, buf_size, &load_end);
1252 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
1253 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1260 int bootm_host_load_images(const void *fit, int cfg_noffset)
1262 static uint8_t image_types[] = {
1270 for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1273 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
1274 if (!err && ret && ret != -ENOENT)
1278 /* Return the first error we found */
1283 #endif /* ndef USE_HOSTCC */