Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2009 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | ea51a62 | 2014-06-12 07:24:51 -0600 | [diff] [blame] | 7 | #ifndef USE_HOSTCC |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | ea51a62 | 2014-06-12 07:24:51 -0600 | [diff] [blame] | 9 | #include <bootstage.h> |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 10 | #include <cli.h> |
Simon Glass | be59514 | 2023-09-27 08:22:37 -0600 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 15 | #include <fdt_support.h> |
Simon Glass | 36bf446 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 16 | #include <irq_func.h> |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 17 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 19 | #include <malloc.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 20 | #include <mapmem.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 21 | #include <net.h> |
| 22 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 24 | #include <asm/io.h> |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 25 | #include <linux/sizes.h> |
Eddie James | dec166d | 2023-10-24 10:43:50 -0500 | [diff] [blame] | 26 | #include <tpm-v2.h> |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 27 | #if defined(CONFIG_CMD_USB) |
| 28 | #include <usb.h> |
| 29 | #endif |
Simon Glass | ea51a62 | 2014-06-12 07:24:51 -0600 | [diff] [blame] | 30 | #else |
| 31 | #include "mkimage.h" |
| 32 | #endif |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 33 | |
Simon Glass | ea51a62 | 2014-06-12 07:24:51 -0600 | [diff] [blame] | 34 | #include <bootm.h> |
| 35 | #include <image.h> |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 36 | |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 37 | #define MAX_CMDLINE_SIZE SZ_4K |
| 38 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 39 | #define IH_INITRD_ARCH IH_ARCH_DEFAULT |
| 40 | |
Simon Glass | ea51a62 | 2014-06-12 07:24:51 -0600 | [diff] [blame] | 41 | #ifndef USE_HOSTCC |
| 42 | |
| 43 | DECLARE_GLOBAL_DATA_PTR; |
| 44 | |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 45 | struct bootm_headers images; /* pointers to os/initrd/fdt images */ |
Tom Rini | 5db2890 | 2016-08-12 08:31:15 -0400 | [diff] [blame] | 46 | |
Simon Glass | 329da48 | 2018-05-16 09:42:25 -0600 | [diff] [blame] | 47 | __weak void board_quiesce_devices(void) |
| 48 | { |
| 49 | } |
| 50 | |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 51 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
| 52 | /** |
| 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 |
| 56 | * |
| 57 | * image_get_kernel() verifies legacy image integrity and returns pointer to |
| 58 | * legacy image header if image verification was completed successfully. |
| 59 | * |
| 60 | * returns: |
| 61 | * pointer to a legacy image header if valid image was found |
| 62 | * otherwise return NULL |
| 63 | */ |
| 64 | static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify) |
| 65 | { |
| 66 | struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr; |
| 67 | |
| 68 | if (!image_check_magic(hdr)) { |
| 69 | puts("Bad Magic Number\n"); |
| 70 | bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC); |
| 71 | return NULL; |
| 72 | } |
| 73 | bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER); |
| 74 | |
| 75 | if (!image_check_hcrc(hdr)) { |
| 76 | puts("Bad Header Checksum\n"); |
| 77 | bootstage_error(BOOTSTAGE_ID_CHECK_HEADER); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM); |
| 82 | image_print_contents(hdr); |
| 83 | |
| 84 | if (verify) { |
| 85 | puts(" Verifying Checksum ... "); |
| 86 | if (!image_check_dcrc(hdr)) { |
| 87 | printf("Bad Data CRC\n"); |
| 88 | bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM); |
| 89 | return NULL; |
| 90 | } |
| 91 | puts("OK\n"); |
| 92 | } |
| 93 | bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH); |
| 94 | |
| 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); |
| 98 | return NULL; |
| 99 | } |
| 100 | return hdr; |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | /** |
Simon Glass | 820110c | 2023-11-18 14:04:58 -0700 | [diff] [blame] | 105 | * boot_get_kernel() - find kernel image |
| 106 | * |
| 107 | * @cmd_name: Name of the command calling this function, e.g. "bootm" |
| 108 | * @addr_fit: first argument to bootm: address, fit configuration, etc. |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 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 |
Simon Glass | 820110c | 2023-11-18 14:04:58 -0700 | [diff] [blame] | 111 | * address and length, otherwise NULL |
| 112 | * pointer to image header if valid image was found, plus kernel start |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 113 | * @kernp: image header if valid image was found, otherwise NULL |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 114 | * |
| 115 | * boot_get_kernel() tries to find a kernel image, verifies its integrity |
| 116 | * and locates kernel data. |
| 117 | * |
| 118 | * returns: |
| 119 | * pointer to image header if valid image was found, plus kernel start |
| 120 | * address and length, otherwise NULL |
| 121 | */ |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 122 | static int boot_get_kernel(const char *cmd_name, const char *addr_fit, |
| 123 | struct bootm_headers *images, ulong *os_data, |
| 124 | ulong *os_len, const void **kernp) |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 125 | { |
| 126 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
| 127 | struct legacy_img_hdr *hdr; |
| 128 | #endif |
| 129 | ulong img_addr; |
| 130 | const void *buf; |
Simon Glass | 530cc47 | 2023-11-18 14:04:57 -0700 | [diff] [blame] | 131 | const char *fit_uname_config = NULL, *fit_uname_kernel = NULL; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 132 | #if CONFIG_IS_ENABLED(FIT) |
| 133 | int os_noffset; |
| 134 | #endif |
| 135 | |
| 136 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
| 137 | const void *boot_img; |
| 138 | const void *vendor_boot_img; |
| 139 | #endif |
Simon Glass | 820110c | 2023-11-18 14:04:58 -0700 | [diff] [blame] | 140 | img_addr = genimg_get_kernel_addr_fit(addr_fit, &fit_uname_config, |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 141 | &fit_uname_kernel); |
| 142 | |
| 143 | if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD)) |
| 144 | img_addr += image_load_offset; |
| 145 | |
| 146 | bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC); |
| 147 | |
| 148 | /* check image type, for FIT images get FIT kernel node */ |
| 149 | *os_data = *os_len = 0; |
| 150 | buf = map_sysmem(img_addr, 0); |
| 151 | switch (genimg_get_format(buf)) { |
| 152 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
| 153 | case IMAGE_FORMAT_LEGACY: |
| 154 | printf("## Booting kernel from Legacy Image at %08lx ...\n", |
| 155 | img_addr); |
| 156 | hdr = image_get_kernel(img_addr, images->verify); |
| 157 | if (!hdr) |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 158 | return -EINVAL; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 159 | bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE); |
| 160 | |
| 161 | /* get os_data and os_len */ |
| 162 | switch (image_get_type(hdr)) { |
| 163 | case IH_TYPE_KERNEL: |
| 164 | case IH_TYPE_KERNEL_NOLOAD: |
| 165 | *os_data = image_get_data(hdr); |
| 166 | *os_len = image_get_data_size(hdr); |
| 167 | break; |
| 168 | case IH_TYPE_MULTI: |
| 169 | image_multi_getimg(hdr, 0, os_data, os_len); |
| 170 | break; |
| 171 | case IH_TYPE_STANDALONE: |
| 172 | *os_data = image_get_data(hdr); |
| 173 | *os_len = image_get_data_size(hdr); |
| 174 | break; |
| 175 | default: |
| 176 | printf("Wrong Image Type for %s command\n", |
Simon Glass | 820110c | 2023-11-18 14:04:58 -0700 | [diff] [blame] | 177 | cmd_name); |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 178 | bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 179 | return -EPROTOTYPE; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
| 183 | * copy image header to allow for image overwrites during |
| 184 | * kernel decompression. |
| 185 | */ |
| 186 | memmove(&images->legacy_hdr_os_copy, hdr, |
| 187 | sizeof(struct legacy_img_hdr)); |
| 188 | |
| 189 | /* save pointer to image header */ |
| 190 | images->legacy_hdr_os = hdr; |
| 191 | |
| 192 | images->legacy_hdr_valid = 1; |
| 193 | bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE); |
| 194 | break; |
| 195 | #endif |
| 196 | #if CONFIG_IS_ENABLED(FIT) |
| 197 | case IMAGE_FORMAT_FIT: |
| 198 | os_noffset = fit_image_load(images, img_addr, |
| 199 | &fit_uname_kernel, &fit_uname_config, |
| 200 | IH_ARCH_DEFAULT, IH_TYPE_KERNEL, |
| 201 | BOOTSTAGE_ID_FIT_KERNEL_START, |
| 202 | FIT_LOAD_IGNORED, os_data, os_len); |
| 203 | if (os_noffset < 0) |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 204 | return -ENOENT; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 205 | |
| 206 | images->fit_hdr_os = map_sysmem(img_addr, 0); |
| 207 | images->fit_uname_os = fit_uname_kernel; |
| 208 | images->fit_uname_cfg = fit_uname_config; |
| 209 | images->fit_noffset_os = os_noffset; |
| 210 | break; |
| 211 | #endif |
| 212 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 213 | case IMAGE_FORMAT_ANDROID: { |
| 214 | int ret; |
| 215 | |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 216 | boot_img = buf; |
| 217 | vendor_boot_img = NULL; |
| 218 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
| 219 | boot_img = map_sysmem(get_abootimg_addr(), 0); |
| 220 | vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); |
| 221 | } |
| 222 | printf("## Booting Android Image at 0x%08lx ...\n", img_addr); |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 223 | ret = android_image_get_kernel(boot_img, vendor_boot_img, |
| 224 | images->verify, os_data, os_len); |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 225 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
| 226 | unmap_sysmem(vendor_boot_img); |
| 227 | unmap_sysmem(boot_img); |
| 228 | } |
Simon Glass | 4f77169 | 2023-11-18 14:05:01 -0700 | [diff] [blame] | 229 | if (ret) |
| 230 | return ret; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 231 | break; |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 232 | } |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 233 | #endif |
| 234 | default: |
Simon Glass | 820110c | 2023-11-18 14:04:58 -0700 | [diff] [blame] | 235 | printf("Wrong Image Format for %s command\n", cmd_name); |
Simon Glass | bdfa1b6 | 2023-11-18 14:05:03 -0700 | [diff] [blame^] | 236 | bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE); |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 237 | return -EBADF; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n", |
| 241 | *os_data, *os_len, *os_len); |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 242 | *kernp = buf; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 243 | |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 244 | return 0; |
Simon Glass | 7f3b1ee | 2023-11-18 14:04:56 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 247 | #ifdef CONFIG_LMB |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 248 | static void boot_start_lmb(struct bootm_headers *images) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 249 | { |
| 250 | ulong mem_start; |
| 251 | phys_size_t mem_size; |
| 252 | |
Simon Glass | 723806c | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 253 | mem_start = env_get_bootm_low(); |
| 254 | mem_size = env_get_bootm_size(); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 255 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 256 | lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start, |
| 257 | mem_size, NULL); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 258 | } |
| 259 | #else |
| 260 | #define lmb_reserve(lmb, base, size) |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 261 | static inline void boot_start_lmb(struct bootm_headers *images) { } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 262 | #endif |
| 263 | |
Simon Glass | a50e886 | 2023-11-18 14:04:54 -0700 | [diff] [blame] | 264 | static int bootm_start(void) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 265 | { |
| 266 | memset((void *)&images, 0, sizeof(images)); |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 267 | images.verify = env_get_yesno("verify"); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 268 | |
| 269 | boot_start_lmb(&images); |
| 270 | |
| 271 | bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start"); |
| 272 | images.state = BOOTM_STATE_START; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Simon Glass | 921070b | 2023-11-18 14:04:55 -0700 | [diff] [blame] | 277 | static ulong bootm_data_addr(const char *addr_str) |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 278 | { |
| 279 | ulong addr; |
| 280 | |
Simon Glass | 921070b | 2023-11-18 14:04:55 -0700 | [diff] [blame] | 281 | if (addr_str) |
| 282 | addr = hextoul(addr_str, NULL); |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 283 | else |
| 284 | addr = image_load_addr; |
| 285 | |
| 286 | return addr; |
| 287 | } |
| 288 | |
Simon Glass | 921070b | 2023-11-18 14:04:55 -0700 | [diff] [blame] | 289 | /** |
| 290 | * bootm_pre_load() - Handle the pre-load processing |
| 291 | * |
| 292 | * This can be used to do a full signature check of the image, for example. |
| 293 | * It calls image_pre_load() with the data address of the image to check. |
| 294 | * |
| 295 | * @addr_str: String containing load address in hex, or NULL to use |
| 296 | * image_load_addr |
| 297 | * Return: 0 if OK, CMD_RET_FAILURE on failure |
| 298 | */ |
| 299 | static int bootm_pre_load(const char *addr_str) |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 300 | { |
Simon Glass | 921070b | 2023-11-18 14:04:55 -0700 | [diff] [blame] | 301 | ulong data_addr = bootm_data_addr(addr_str); |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 302 | int ret = 0; |
| 303 | |
Simon Glass | 494bcf1 | 2023-02-05 15:36:24 -0700 | [diff] [blame] | 304 | if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD)) |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 305 | ret = image_pre_load(data_addr); |
| 306 | |
| 307 | if (ret) |
| 308 | ret = CMD_RET_FAILURE; |
| 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 313 | static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc, |
| 314 | char *const argv[]) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 315 | { |
| 316 | const void *os_hdr; |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 317 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
| 318 | const void *vendor_boot_img; |
| 319 | const void *boot_img; |
| 320 | #endif |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 321 | bool ep_found = false; |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 322 | int ret; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 323 | |
| 324 | /* get kernel image header, start address and length */ |
Simon Glass | 7721e71 | 2023-11-18 14:05:00 -0700 | [diff] [blame] | 325 | ret = boot_get_kernel(cmdtp->name, argv[0], &images, |
| 326 | &images.os.image_start, &images.os.image_len, |
Simon Glass | 4c76f5e | 2023-11-18 14:05:02 -0700 | [diff] [blame] | 327 | &os_hdr); |
| 328 | if (ret) { |
| 329 | printf("ERROR %dE: can't get kernel image!\n", ret); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | /* get image parameters */ |
| 334 | switch (genimg_get_format(os_hdr)) { |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 335 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 336 | case IMAGE_FORMAT_LEGACY: |
| 337 | images.os.type = image_get_type(os_hdr); |
| 338 | images.os.comp = image_get_comp(os_hdr); |
| 339 | images.os.os = image_get_os(os_hdr); |
| 340 | |
| 341 | images.os.end = image_get_image_end(os_hdr); |
| 342 | images.os.load = image_get_load(os_hdr); |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 343 | images.os.arch = image_get_arch(os_hdr); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 344 | break; |
| 345 | #endif |
Simon Glass | bf371b4 | 2021-09-25 19:43:20 -0600 | [diff] [blame] | 346 | #if CONFIG_IS_ENABLED(FIT) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 347 | case IMAGE_FORMAT_FIT: |
| 348 | if (fit_image_get_type(images.fit_hdr_os, |
| 349 | images.fit_noffset_os, |
| 350 | &images.os.type)) { |
| 351 | puts("Can't get image type!\n"); |
| 352 | bootstage_error(BOOTSTAGE_ID_FIT_TYPE); |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | if (fit_image_get_comp(images.fit_hdr_os, |
| 357 | images.fit_noffset_os, |
| 358 | &images.os.comp)) { |
| 359 | puts("Can't get image compression!\n"); |
| 360 | bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os, |
| 365 | &images.os.os)) { |
| 366 | puts("Can't get image OS!\n"); |
| 367 | bootstage_error(BOOTSTAGE_ID_FIT_OS); |
| 368 | return 1; |
| 369 | } |
| 370 | |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 371 | if (fit_image_get_arch(images.fit_hdr_os, |
| 372 | images.fit_noffset_os, |
| 373 | &images.os.arch)) { |
| 374 | puts("Can't get image ARCH!\n"); |
| 375 | return 1; |
| 376 | } |
| 377 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 378 | images.os.end = fit_get_end(images.fit_hdr_os); |
| 379 | |
| 380 | if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os, |
| 381 | &images.os.load)) { |
| 382 | puts("Can't get image load address!\n"); |
| 383 | bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR); |
| 384 | return 1; |
| 385 | } |
| 386 | break; |
| 387 | #endif |
| 388 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
| 389 | case IMAGE_FORMAT_ANDROID: |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 390 | boot_img = os_hdr; |
| 391 | vendor_boot_img = NULL; |
| 392 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
| 393 | boot_img = map_sysmem(get_abootimg_addr(), 0); |
| 394 | vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); |
| 395 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 396 | images.os.type = IH_TYPE_KERNEL; |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 397 | images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 398 | images.os.os = IH_OS_LINUX; |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 399 | images.os.end = android_image_get_end(boot_img, vendor_boot_img); |
| 400 | images.os.load = android_image_get_kload(boot_img, vendor_boot_img); |
Ahmad Draidi | 86f4695 | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 401 | images.ep = images.os.load; |
| 402 | ep_found = true; |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 403 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
| 404 | unmap_sysmem(vendor_boot_img); |
| 405 | unmap_sysmem(boot_img); |
| 406 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 407 | break; |
| 408 | #endif |
| 409 | default: |
| 410 | puts("ERROR: unknown image format type!\n"); |
| 411 | return 1; |
| 412 | } |
| 413 | |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 414 | /* If we have a valid setup.bin, we will use that for entry (x86) */ |
Simon Glass | 5bda35c | 2014-10-10 08:21:57 -0600 | [diff] [blame] | 415 | if (images.os.arch == IH_ARCH_I386 || |
| 416 | images.os.arch == IH_ARCH_X86_64) { |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 417 | ulong len; |
| 418 | |
| 419 | ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len); |
| 420 | if (ret < 0 && ret != -ENOENT) { |
| 421 | puts("Could not find a valid setup.bin for x86\n"); |
| 422 | return 1; |
| 423 | } |
| 424 | /* Kernel entry point is the setup.bin */ |
| 425 | } else if (images.legacy_hdr_valid) { |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 426 | images.ep = image_get_ep(&images.legacy_hdr_os_copy); |
Simon Glass | bf371b4 | 2021-09-25 19:43:20 -0600 | [diff] [blame] | 427 | #if CONFIG_IS_ENABLED(FIT) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 428 | } else if (images.fit_uname_os) { |
| 429 | int ret; |
| 430 | |
| 431 | ret = fit_image_get_entry(images.fit_hdr_os, |
| 432 | images.fit_noffset_os, &images.ep); |
| 433 | if (ret) { |
| 434 | puts("Can't get entry point property!\n"); |
| 435 | return 1; |
| 436 | } |
| 437 | #endif |
| 438 | } else if (!ep_found) { |
| 439 | puts("Could not find kernel entry point!\n"); |
| 440 | return 1; |
| 441 | } |
| 442 | |
| 443 | if (images.os.type == IH_TYPE_KERNEL_NOLOAD) { |
Simon Glass | ef65aa3 | 2023-02-05 15:36:23 -0700 | [diff] [blame] | 444 | if (IS_ENABLED(CONFIG_CMD_BOOTI) && |
Heinrich Schuchardt | 4533b3d | 2023-06-13 08:18:27 +0200 | [diff] [blame] | 445 | images.os.arch == IH_ARCH_ARM64 && |
| 446 | images.os.os == IH_OS_LINUX) { |
Marek Vasut | 487b5fa | 2018-06-13 06:13:33 +0200 | [diff] [blame] | 447 | ulong image_addr; |
| 448 | ulong image_size; |
| 449 | |
| 450 | ret = booti_setup(images.os.image_start, &image_addr, |
| 451 | &image_size, true); |
| 452 | if (ret != 0) |
| 453 | return 1; |
| 454 | |
| 455 | images.os.type = IH_TYPE_KERNEL; |
| 456 | images.os.load = image_addr; |
| 457 | images.ep = image_addr; |
| 458 | } else { |
| 459 | images.os.load = images.os.image_start; |
| 460 | images.ep += images.os.image_start; |
| 461 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 462 | } |
| 463 | |
Simon Glass | 7a80de4 | 2016-02-24 09:14:42 -0700 | [diff] [blame] | 464 | images.os.start = map_to_sysmem(os_hdr); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
Karl Apsite | d52e857 | 2015-05-21 09:52:49 -0400 | [diff] [blame] | 469 | /** |
| 470 | * bootm_find_images - wrapper to find and locate various images |
| 471 | * @flag: Ignored Argument |
| 472 | * @argc: command argument count |
| 473 | * @argv: command argument list |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 474 | * @start: OS image start address |
| 475 | * @size: OS image size |
Karl Apsite | d52e857 | 2015-05-21 09:52:49 -0400 | [diff] [blame] | 476 | * |
| 477 | * boot_find_images() will attempt to load an available ramdisk, |
| 478 | * flattened device tree, as well as specifically marked |
| 479 | * "loadable" images (loadables are FIT only) |
| 480 | * |
| 481 | * Note: bootm_find_images will skip an image if it is not found |
| 482 | * |
| 483 | * @return: |
| 484 | * 0, if all existing images were loaded correctly |
| 485 | * 1, if an image is found but corrupted, or invalid |
| 486 | */ |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 487 | int bootm_find_images(int flag, int argc, char *const argv[], ulong start, |
| 488 | ulong size) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 489 | { |
| 490 | int ret; |
| 491 | |
| 492 | /* find ramdisk */ |
| 493 | ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH, |
| 494 | &images.rd_start, &images.rd_end); |
| 495 | if (ret) { |
| 496 | puts("Ramdisk image is corrupt or invalid\n"); |
| 497 | return 1; |
| 498 | } |
| 499 | |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 500 | /* check if ramdisk overlaps OS image */ |
| 501 | if (images.rd_start && (((ulong)images.rd_start >= start && |
Jaehoon Chung | ef4f4f1 | 2020-10-21 14:17:03 +0900 | [diff] [blame] | 502 | (ulong)images.rd_start < start + size) || |
| 503 | ((ulong)images.rd_end > start && |
| 504 | (ulong)images.rd_end <= start + size) || |
| 505 | ((ulong)images.rd_start < start && |
| 506 | (ulong)images.rd_end >= start + size))) { |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 507 | printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n", |
| 508 | start, start + size); |
| 509 | return 1; |
| 510 | } |
| 511 | |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 512 | #if CONFIG_IS_ENABLED(OF_LIBFDT) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 513 | /* find flattened device tree */ |
| 514 | ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images, |
| 515 | &images.ft_addr, &images.ft_len); |
| 516 | if (ret) { |
| 517 | puts("Could not find a valid device tree\n"); |
| 518 | return 1; |
| 519 | } |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 520 | |
| 521 | /* check if FDT overlaps OS image */ |
| 522 | if (images.ft_addr && |
| 523 | (((ulong)images.ft_addr >= start && |
Pali Rohár | 5acfdfb | 2022-08-27 14:48:10 +0200 | [diff] [blame] | 524 | (ulong)images.ft_addr < start + size) || |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 525 | ((ulong)images.ft_addr + images.ft_len >= start && |
Pali Rohár | 5acfdfb | 2022-08-27 14:48:10 +0200 | [diff] [blame] | 526 | (ulong)images.ft_addr + images.ft_len < start + size))) { |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 527 | printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n", |
| 528 | start, start + size); |
| 529 | return 1; |
| 530 | } |
| 531 | |
Simon Glass | 3a09f38 | 2023-02-05 15:36:30 -0700 | [diff] [blame] | 532 | if (IS_ENABLED(CONFIG_CMD_FDT)) |
Simon Goldschmidt | 596be5f | 2018-12-17 20:14:42 +0100 | [diff] [blame] | 533 | set_working_fdt_addr(map_to_sysmem(images.ft_addr)); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 534 | #endif |
| 535 | |
Simon Glass | bf371b4 | 2021-09-25 19:43:20 -0600 | [diff] [blame] | 536 | #if CONFIG_IS_ENABLED(FIT) |
Simon Glass | 4ed37ab | 2021-09-25 07:03:20 -0600 | [diff] [blame] | 537 | if (IS_ENABLED(CONFIG_FPGA)) { |
| 538 | /* find bitstreams */ |
| 539 | ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT, |
| 540 | NULL, NULL); |
| 541 | if (ret) { |
| 542 | printf("FPGA image is corrupted or invalid\n"); |
| 543 | return 1; |
| 544 | } |
Michal Simek | 62afc60 | 2016-05-17 14:03:50 +0200 | [diff] [blame] | 545 | } |
Michal Simek | 62afc60 | 2016-05-17 14:03:50 +0200 | [diff] [blame] | 546 | |
Karl Apsite | 84a07db | 2015-05-21 09:52:48 -0400 | [diff] [blame] | 547 | /* find all of the loadables */ |
| 548 | ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT, |
| 549 | NULL, NULL); |
| 550 | if (ret) { |
| 551 | printf("Loadable(s) is corrupt or invalid\n"); |
| 552 | return 1; |
| 553 | } |
Karl Apsite | 84a07db | 2015-05-21 09:52:48 -0400 | [diff] [blame] | 554 | #endif |
| 555 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 559 | static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc, |
| 560 | char *const argv[]) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 561 | { |
| 562 | if (((images.os.type == IH_TYPE_KERNEL) || |
| 563 | (images.os.type == IH_TYPE_KERNEL_NOLOAD) || |
| 564 | (images.os.type == IH_TYPE_MULTI)) && |
| 565 | (images.os.os == IH_OS_LINUX || |
| 566 | images.os.os == IH_OS_VXWORKS)) |
Tero Kristo | fbde758 | 2020-06-12 15:41:20 +0300 | [diff] [blame] | 567 | return bootm_find_images(flag, argc, argv, 0, 0); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | } |
Simon Glass | 40e5975 | 2014-12-02 13:17:30 -0700 | [diff] [blame] | 571 | #endif /* USE_HOSTC */ |
| 572 | |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 573 | #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE) |
Simon Glass | 3086c05 | 2014-12-02 13:17:37 -0700 | [diff] [blame] | 574 | /** |
| 575 | * handle_decomp_error() - display a decompression error |
| 576 | * |
| 577 | * This function tries to produce a useful message. In the case where the |
| 578 | * uncompressed size is the same as the available space, we can assume that |
| 579 | * the image is too large for the buffer. |
| 580 | * |
| 581 | * @comp_type: Compression type being used (IH_COMP_...) |
| 582 | * @uncomp_size: Number of bytes uncompressed |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 583 | * @buf_size: Number of bytes the decompresion buffer was |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 584 | * @ret: errno error code received from compression library |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 585 | * Return: Appropriate BOOTM_ERR_ error code |
Simon Glass | 3086c05 | 2014-12-02 13:17:37 -0700 | [diff] [blame] | 586 | */ |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 587 | static int handle_decomp_error(int comp_type, size_t uncomp_size, |
| 588 | size_t buf_size, int ret) |
Simon Glass | 40e5975 | 2014-12-02 13:17:30 -0700 | [diff] [blame] | 589 | { |
Simon Glass | 3086c05 | 2014-12-02 13:17:37 -0700 | [diff] [blame] | 590 | const char *name = genimg_get_comp_name(comp_type); |
| 591 | |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 592 | /* ENOSYS means unimplemented compression type, don't reset. */ |
| 593 | if (ret == -ENOSYS) |
| 594 | return BOOTM_ERR_UNIMPLEMENTED; |
| 595 | |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 596 | if (uncomp_size >= buf_size) |
Simon Glass | 3086c05 | 2014-12-02 13:17:37 -0700 | [diff] [blame] | 597 | printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n"); |
Simon Glass | 40e5975 | 2014-12-02 13:17:30 -0700 | [diff] [blame] | 598 | else |
Simon Glass | 3086c05 | 2014-12-02 13:17:37 -0700 | [diff] [blame] | 599 | printf("%s: uncompress error %d\n", name, ret); |
| 600 | |
| 601 | /* |
| 602 | * The decompression routines are now safe, so will not write beyond |
| 603 | * their bounds. Probably it is not necessary to reset, but maintain |
| 604 | * the current behaviour for now. |
| 605 | */ |
| 606 | printf("Must RESET board to recover\n"); |
Simon Glass | 40e5975 | 2014-12-02 13:17:30 -0700 | [diff] [blame] | 607 | #ifndef USE_HOSTCC |
| 608 | bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); |
| 609 | #endif |
| 610 | |
| 611 | return BOOTM_ERR_RESET; |
| 612 | } |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 613 | #endif |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 614 | |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 615 | #ifndef USE_HOSTCC |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 616 | static int bootm_load_os(struct bootm_headers *images, int boot_progress) |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 617 | { |
Simon Glass | da79b2f | 2022-09-06 20:26:51 -0600 | [diff] [blame] | 618 | struct image_info os = images->os; |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 619 | ulong load = os.load; |
Tom Rini | cc95535 | 2018-05-01 12:32:37 -0400 | [diff] [blame] | 620 | ulong load_end; |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 621 | ulong blob_start = os.start; |
| 622 | ulong blob_end = os.end; |
| 623 | ulong image_start = os.image_start; |
| 624 | ulong image_len = os.image_len; |
Bryan O'Donoghue | bbac922 | 2018-04-15 11:48:17 +0100 | [diff] [blame] | 625 | ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN); |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 626 | bool no_overlap; |
| 627 | void *load_buf, *image_buf; |
| 628 | int err; |
| 629 | |
| 630 | load_buf = map_sysmem(load, 0); |
| 631 | image_buf = map_sysmem(os.image_start, image_len); |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 632 | err = image_decomp(os.comp, load, os.image_start, os.type, |
| 633 | load_buf, image_buf, image_len, |
| 634 | CONFIG_SYS_BOOTM_LEN, &load_end); |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 635 | if (err) { |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 636 | err = handle_decomp_error(os.comp, load_end - load, |
| 637 | CONFIG_SYS_BOOTM_LEN, err); |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 638 | bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE); |
| 639 | return err; |
| 640 | } |
Heinrich Schuchardt | 21d3946 | 2020-08-30 11:34:12 +0200 | [diff] [blame] | 641 | /* We need the decompressed image size in the next steps */ |
| 642 | images->os.image_len = load_end - load; |
Bryan O'Donoghue | bbac922 | 2018-04-15 11:48:17 +0100 | [diff] [blame] | 643 | |
Trent Piepho | b4353b3 | 2019-03-27 23:50:09 +0000 | [diff] [blame] | 644 | flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 645 | |
Tom Rini | cc95535 | 2018-05-01 12:32:37 -0400 | [diff] [blame] | 646 | debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 647 | bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED); |
| 648 | |
Simon Glass | 2b164f1 | 2014-06-12 07:24:52 -0600 | [diff] [blame] | 649 | no_overlap = (os.comp == IH_COMP_NONE && load == image_start); |
| 650 | |
Tom Rini | cc95535 | 2018-05-01 12:32:37 -0400 | [diff] [blame] | 651 | if (!no_overlap && load < blob_end && load_end > blob_start) { |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 652 | debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n", |
| 653 | blob_start, blob_end); |
| 654 | debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load, |
Tom Rini | cc95535 | 2018-05-01 12:32:37 -0400 | [diff] [blame] | 655 | load_end); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 656 | |
| 657 | /* Check what type of image this is. */ |
| 658 | if (images->legacy_hdr_valid) { |
| 659 | if (image_get_type(&images->legacy_hdr_os_copy) |
| 660 | == IH_TYPE_MULTI) |
| 661 | puts("WARNING: legacy format multi component image overwritten\n"); |
| 662 | return BOOTM_ERR_OVERLAP; |
| 663 | } else { |
| 664 | puts("ERROR: new format image overwritten - must RESET the board to recover\n"); |
| 665 | bootstage_error(BOOTSTAGE_ID_OVERWRITTEN); |
| 666 | return BOOTM_ERR_RESET; |
| 667 | } |
| 668 | } |
| 669 | |
Tom Rini | cc95535 | 2018-05-01 12:32:37 -0400 | [diff] [blame] | 670 | lmb_reserve(&images->lmb, images->os.load, (load_end - |
| 671 | images->os.load)); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot |
| 677 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 678 | * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 679 | * enabled) |
| 680 | */ |
| 681 | ulong bootm_disable_interrupts(void) |
| 682 | { |
| 683 | ulong iflag; |
| 684 | |
| 685 | /* |
| 686 | * We have reached the point of no return: we are going to |
| 687 | * overwrite all exception vector code, so we cannot easily |
| 688 | * recover from any failures any more... |
| 689 | */ |
| 690 | iflag = disable_interrupts(); |
| 691 | #ifdef CONFIG_NETCONSOLE |
| 692 | /* Stop the ethernet stack if NetConsole could have left it up */ |
| 693 | eth_halt(); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 694 | #endif |
| 695 | |
| 696 | #if defined(CONFIG_CMD_USB) |
| 697 | /* |
| 698 | * turn off USB to prevent the host controller from writing to the |
| 699 | * SDRAM while Linux is booting. This could happen (at least for OHCI |
| 700 | * controller), because the HCCA (Host Controller Communication Area) |
| 701 | * lies within the SDRAM and the host controller writes continously to |
| 702 | * this area (as busmaster!). The HccaFrameNumber is for example |
| 703 | * updated every 1 ms within the HCCA structure in SDRAM! For more |
| 704 | * details see the OpenHCI specification. |
| 705 | */ |
| 706 | usb_stop(); |
| 707 | #endif |
| 708 | return iflag; |
| 709 | } |
| 710 | |
Simon Glass | 6cd92b1 | 2020-11-05 10:33:42 -0700 | [diff] [blame] | 711 | #define CONSOLE_ARG "console=" |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 712 | #define NULL_CONSOLE (CONSOLE_ARG "ttynull") |
| 713 | #define CONSOLE_ARG_SIZE sizeof(NULL_CONSOLE) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 714 | |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 715 | /** |
| 716 | * fixup_silent_linux() - Handle silencing the linux boot if required |
| 717 | * |
| 718 | * This uses the silent_linux envvar to control whether to add/set a "console=" |
| 719 | * parameter to the command line |
| 720 | * |
| 721 | * @buf: Buffer containing the string to process |
| 722 | * @maxlen: Maximum length of buffer |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 723 | * Return: 0 if OK, -ENOSPC if @maxlen is too small |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 724 | */ |
| 725 | static int fixup_silent_linux(char *buf, int maxlen) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 726 | { |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 727 | int want_silent; |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 728 | char *cmdline; |
| 729 | int size; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 730 | |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 731 | /* |
| 732 | * Move the input string to the end of buffer. The output string will be |
| 733 | * built up at the start. |
| 734 | */ |
| 735 | size = strlen(buf) + 1; |
| 736 | if (size * 2 > maxlen) |
| 737 | return -ENOSPC; |
| 738 | cmdline = buf + maxlen - size; |
| 739 | memmove(cmdline, buf, size); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 740 | /* |
| 741 | * Only fix cmdline when requested. The environment variable can be: |
| 742 | * |
| 743 | * no - we never fixup |
| 744 | * yes - we always fixup |
| 745 | * unset - we rely on the console silent flag |
| 746 | */ |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 747 | want_silent = env_get_yesno("silent_linux"); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 748 | if (want_silent == 0) |
Simon Glass | 4ae4264 | 2020-11-05 10:33:39 -0700 | [diff] [blame] | 749 | return 0; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 750 | else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT)) |
Simon Glass | 4ae4264 | 2020-11-05 10:33:39 -0700 | [diff] [blame] | 751 | return 0; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 752 | |
| 753 | debug("before silent fix-up: %s\n", cmdline); |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 754 | if (*cmdline) { |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 755 | char *start = strstr(cmdline, CONSOLE_ARG); |
| 756 | |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 757 | /* Check space for maximum possible new command line */ |
| 758 | if (size + CONSOLE_ARG_SIZE > maxlen) |
Simon Glass | 4ae4264 | 2020-11-05 10:33:39 -0700 | [diff] [blame] | 759 | return -ENOSPC; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 760 | |
| 761 | if (start) { |
| 762 | char *end = strchr(start, ' '); |
Simon Glass | 6cd92b1 | 2020-11-05 10:33:42 -0700 | [diff] [blame] | 763 | int start_bytes; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 764 | |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 765 | start_bytes = start - cmdline; |
Simon Glass | 6cd92b1 | 2020-11-05 10:33:42 -0700 | [diff] [blame] | 766 | strncpy(buf, cmdline, start_bytes); |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 767 | strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 768 | if (end) |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 769 | strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 770 | else |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 771 | buf[start_bytes + CONSOLE_ARG_SIZE] = '\0'; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 772 | } else { |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 773 | sprintf(buf, "%s %s", cmdline, NULL_CONSOLE); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 774 | } |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 775 | if (buf + strlen(buf) >= cmdline) |
| 776 | return -ENOSPC; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 777 | } else { |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 778 | if (maxlen < CONSOLE_ARG_SIZE) |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 779 | return -ENOSPC; |
Sean Anderson | ba9aa40 | 2022-05-19 18:26:05 -0400 | [diff] [blame] | 780 | strcpy(buf, NULL_CONSOLE); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 781 | } |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 782 | debug("after silent fix-up: %s\n", buf); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 783 | |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 784 | return 0; |
| 785 | } |
| 786 | |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 787 | /** |
| 788 | * process_subst() - Handle substitution of ${...} fields in the environment |
| 789 | * |
| 790 | * Handle variable substitution in the provided buffer |
| 791 | * |
| 792 | * @buf: Buffer containing the string to process |
| 793 | * @maxlen: Maximum length of buffer |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 794 | * Return: 0 if OK, -ENOSPC if @maxlen is too small |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 795 | */ |
| 796 | static int process_subst(char *buf, int maxlen) |
| 797 | { |
| 798 | char *cmdline; |
| 799 | int size; |
| 800 | int ret; |
| 801 | |
| 802 | /* Move to end of buffer */ |
| 803 | size = strlen(buf) + 1; |
| 804 | cmdline = buf + maxlen - size; |
| 805 | if (buf + size > cmdline) |
| 806 | return -ENOSPC; |
| 807 | memmove(cmdline, buf, size); |
| 808 | |
| 809 | ret = cli_simple_process_macros(cmdline, buf, cmdline - buf); |
| 810 | |
| 811 | return ret; |
| 812 | } |
| 813 | |
Simon Glass | 4448fe8 | 2020-11-05 10:33:45 -0700 | [diff] [blame] | 814 | int bootm_process_cmdline(char *buf, int maxlen, int flags) |
| 815 | { |
| 816 | int ret; |
| 817 | |
| 818 | /* Check config first to enable compiler to eliminate code */ |
| 819 | if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && |
| 820 | !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && |
| 821 | (flags & BOOTM_CL_SILENT)) { |
| 822 | ret = fixup_silent_linux(buf, maxlen); |
| 823 | if (ret) |
| 824 | return log_msg_ret("silent", ret); |
| 825 | } |
Simon Glass | a8d6962 | 2021-03-15 18:11:23 +1300 | [diff] [blame] | 826 | if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) && |
| 827 | (flags & BOOTM_CL_SUBST)) { |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 828 | ret = process_subst(buf, maxlen); |
| 829 | if (ret) |
Simon Glass | 185756e | 2021-02-06 09:57:35 -0700 | [diff] [blame] | 830 | return log_msg_ret("subst", ret); |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 831 | } |
Simon Glass | 4448fe8 | 2020-11-05 10:33:45 -0700 | [diff] [blame] | 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
Simon Glass | b3c0167 | 2020-11-05 10:33:44 -0700 | [diff] [blame] | 836 | int bootm_process_cmdline_env(int flags) |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 837 | { |
| 838 | const int maxlen = MAX_CMDLINE_SIZE; |
Simon Glass | b3c0167 | 2020-11-05 10:33:44 -0700 | [diff] [blame] | 839 | bool do_silent; |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 840 | const char *env; |
| 841 | char *buf; |
| 842 | int ret; |
| 843 | |
| 844 | /* First check if any action is needed */ |
| 845 | do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) && |
Simon Glass | b3c0167 | 2020-11-05 10:33:44 -0700 | [diff] [blame] | 846 | !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT); |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 847 | if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST)) |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 848 | return 0; |
| 849 | |
| 850 | env = env_get("bootargs"); |
| 851 | if (env && strlen(env) >= maxlen) |
| 852 | return -E2BIG; |
| 853 | buf = malloc(maxlen); |
| 854 | if (!buf) |
| 855 | return -ENOMEM; |
| 856 | if (env) |
| 857 | strcpy(buf, env); |
| 858 | else |
| 859 | *buf = '\0'; |
Simon Glass | 4448fe8 | 2020-11-05 10:33:45 -0700 | [diff] [blame] | 860 | ret = bootm_process_cmdline(buf, maxlen, flags); |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 861 | if (!ret) { |
| 862 | ret = env_set("bootargs", buf); |
| 863 | |
| 864 | /* |
| 865 | * If buf is "" and bootargs does not exist, this will produce |
| 866 | * an error trying to delete bootargs. Ignore it |
| 867 | */ |
| 868 | if (ret == -ENOENT) |
| 869 | ret = 0; |
| 870 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 871 | free(buf); |
Simon Glass | b6386f3 | 2020-11-05 10:33:43 -0700 | [diff] [blame] | 872 | if (ret) |
| 873 | return log_msg_ret("env", ret); |
Simon Glass | 4ae4264 | 2020-11-05 10:33:39 -0700 | [diff] [blame] | 874 | |
| 875 | return 0; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 876 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 877 | |
Eddie James | dec166d | 2023-10-24 10:43:50 -0500 | [diff] [blame] | 878 | int bootm_measure(struct bootm_headers *images) |
| 879 | { |
| 880 | int ret = 0; |
| 881 | |
| 882 | /* Skip measurement if EFI is going to do it */ |
| 883 | if (images->os.os == IH_OS_EFI && |
| 884 | IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL) && |
| 885 | IS_ENABLED(CONFIG_BOOTM_EFI)) |
| 886 | return ret; |
| 887 | |
| 888 | if (IS_ENABLED(CONFIG_MEASURED_BOOT)) { |
| 889 | struct tcg2_event_log elog; |
| 890 | struct udevice *dev; |
| 891 | void *initrd_buf; |
| 892 | void *image_buf; |
| 893 | const char *s; |
| 894 | u32 rd_len; |
| 895 | bool ign; |
| 896 | |
| 897 | elog.log_size = 0; |
| 898 | ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG); |
| 899 | ret = tcg2_measurement_init(&dev, &elog, ign); |
| 900 | if (ret) |
| 901 | return ret; |
| 902 | |
| 903 | image_buf = map_sysmem(images->os.image_start, |
| 904 | images->os.image_len); |
| 905 | ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len, |
| 906 | image_buf, EV_COMPACT_HASH, |
| 907 | strlen("linux") + 1, (u8 *)"linux"); |
| 908 | if (ret) |
| 909 | goto unmap_image; |
| 910 | |
| 911 | rd_len = images->rd_end - images->rd_start; |
| 912 | initrd_buf = map_sysmem(images->rd_start, rd_len); |
| 913 | ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf, |
| 914 | EV_COMPACT_HASH, strlen("initrd") + 1, |
| 915 | (u8 *)"initrd"); |
| 916 | if (ret) |
| 917 | goto unmap_initrd; |
| 918 | |
| 919 | if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) { |
| 920 | ret = tcg2_measure_data(dev, &elog, 0, images->ft_len, |
| 921 | (u8 *)images->ft_addr, |
| 922 | EV_TABLE_OF_DEVICES, |
| 923 | strlen("dts") + 1, |
| 924 | (u8 *)"dts"); |
| 925 | if (ret) |
| 926 | goto unmap_initrd; |
| 927 | } |
| 928 | |
| 929 | s = env_get("bootargs"); |
| 930 | if (!s) |
| 931 | s = ""; |
| 932 | ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s, |
| 933 | EV_PLATFORM_CONFIG_FLAGS, |
| 934 | strlen(s) + 1, (u8 *)s); |
| 935 | |
| 936 | unmap_initrd: |
| 937 | unmap_sysmem(initrd_buf); |
| 938 | |
| 939 | unmap_image: |
| 940 | unmap_sysmem(image_buf); |
| 941 | tcg2_measurement_term(dev, &elog, ret != 0); |
| 942 | } |
| 943 | |
| 944 | return ret; |
| 945 | } |
| 946 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 947 | /** |
| 948 | * Execute selected states of the bootm command. |
| 949 | * |
| 950 | * Note the arguments to this state must be the first argument, Any 'bootm' |
| 951 | * or sub-command arguments must have already been taken. |
| 952 | * |
| 953 | * Note that if states contains more than one flag it MUST contain |
| 954 | * BOOTM_STATE_START, since this handles and consumes the command line args. |
| 955 | * |
| 956 | * Also note that aside from boot_os_fn functions and bootm_load_os no other |
| 957 | * functions we store the return value of in 'ret' may use a negative return |
| 958 | * value, without special handling. |
| 959 | * |
| 960 | * @param cmdtp Pointer to bootm command table entry |
| 961 | * @param flag Command flags (CMD_FLAG_...) |
| 962 | * @param argc Number of subcommand arguments (0 = no arguments) |
| 963 | * @param argv Arguments |
| 964 | * @param states Mask containing states to run (BOOTM_STATE_...) |
| 965 | * @param images Image header information |
| 966 | * @param boot_progress 1 to show boot progress, 0 to not do this |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 967 | * Return: 0 if ok, something else on error. Some errors will cause this |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 968 | * function to perform a reboot! If states contains BOOTM_STATE_OS_GO |
| 969 | * then the intent is to boot an OS, so this function will not return |
| 970 | * unless the image type is standalone. |
| 971 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 972 | int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 973 | char *const argv[], int states, struct bootm_headers *images, |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 974 | int boot_progress) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 975 | { |
| 976 | boot_os_fn *boot_fn; |
| 977 | ulong iflag = 0; |
| 978 | int ret = 0, need_boot_fn; |
| 979 | |
| 980 | images->state |= states; |
| 981 | |
| 982 | /* |
| 983 | * Work through the states and see how far we get. We stop on |
| 984 | * any error. |
| 985 | */ |
| 986 | if (states & BOOTM_STATE_START) |
Simon Glass | a50e886 | 2023-11-18 14:04:54 -0700 | [diff] [blame] | 987 | ret = bootm_start(); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 988 | |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 989 | if (!ret && (states & BOOTM_STATE_PRE_LOAD)) |
Simon Glass | 921070b | 2023-11-18 14:04:55 -0700 | [diff] [blame] | 990 | ret = bootm_pre_load(argv[0]); |
Philippe Reynes | 9d46e63 | 2022-03-28 22:57:00 +0200 | [diff] [blame] | 991 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 992 | if (!ret && (states & BOOTM_STATE_FINDOS)) |
| 993 | ret = bootm_find_os(cmdtp, flag, argc, argv); |
| 994 | |
Zubair Lutfullah Kakakhel | ba07984 | 2016-09-09 09:18:58 +0100 | [diff] [blame] | 995 | if (!ret && (states & BOOTM_STATE_FINDOTHER)) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 996 | ret = bootm_find_other(cmdtp, flag, argc, argv); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 997 | |
Eddie James | dec166d | 2023-10-24 10:43:50 -0500 | [diff] [blame] | 998 | if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret && |
| 999 | (states & BOOTM_STATE_MEASURE)) |
| 1000 | bootm_measure(images); |
| 1001 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1002 | /* Load the OS */ |
| 1003 | if (!ret && (states & BOOTM_STATE_LOADOS)) { |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1004 | iflag = bootm_disable_interrupts(); |
Tom Rini | cc95535 | 2018-05-01 12:32:37 -0400 | [diff] [blame] | 1005 | ret = bootm_load_os(images, 0); |
| 1006 | if (ret && ret != BOOTM_ERR_OVERLAP) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1007 | goto err; |
| 1008 | else if (ret == BOOTM_ERR_OVERLAP) |
| 1009 | ret = 0; |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | /* Relocate the ramdisk */ |
| 1013 | #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH |
| 1014 | if (!ret && (states & BOOTM_STATE_RAMDISK)) { |
| 1015 | ulong rd_len = images->rd_end - images->rd_start; |
| 1016 | |
| 1017 | ret = boot_ramdisk_high(&images->lmb, images->rd_start, |
| 1018 | rd_len, &images->initrd_start, &images->initrd_end); |
| 1019 | if (!ret) { |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 1020 | env_set_hex("initrd_start", images->initrd_start); |
| 1021 | env_set_hex("initrd_end", images->initrd_end); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | #endif |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 1025 | #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB) |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1026 | if (!ret && (states & BOOTM_STATE_FDT)) { |
| 1027 | boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr); |
| 1028 | ret = boot_relocate_fdt(&images->lmb, &images->ft_addr, |
| 1029 | &images->ft_len); |
| 1030 | } |
| 1031 | #endif |
| 1032 | |
| 1033 | /* From now on, we need the OS boot function */ |
| 1034 | if (ret) |
| 1035 | return ret; |
| 1036 | boot_fn = bootm_os_get_boot_func(images->os.os); |
| 1037 | need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE | |
| 1038 | BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP | |
| 1039 | BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO); |
| 1040 | if (boot_fn == NULL && need_boot_fn) { |
| 1041 | if (iflag) |
| 1042 | enable_interrupts(); |
| 1043 | printf("ERROR: booting os '%s' (%d) is not supported\n", |
| 1044 | genimg_get_os_name(images->os.os), images->os.os); |
| 1045 | bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS); |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
Hector Palacios | 19e8649 | 2016-07-11 12:34:37 +0200 | [diff] [blame] | 1049 | |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1050 | /* Call various other states that are not generally used */ |
| 1051 | if (!ret && (states & BOOTM_STATE_OS_CMDLINE)) |
| 1052 | ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images); |
| 1053 | if (!ret && (states & BOOTM_STATE_OS_BD_T)) |
| 1054 | ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images); |
Hector Palacios | 19e8649 | 2016-07-11 12:34:37 +0200 | [diff] [blame] | 1055 | if (!ret && (states & BOOTM_STATE_OS_PREP)) { |
Simon Glass | 51bb338 | 2020-11-05 10:33:48 -0700 | [diff] [blame] | 1056 | ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX); |
Simon Glass | d9477a0 | 2020-11-05 10:33:41 -0700 | [diff] [blame] | 1057 | if (ret) { |
| 1058 | printf("Cmdline setup failed (err=%d)\n", ret); |
| 1059 | ret = CMD_RET_FAILURE; |
| 1060 | goto err; |
Simon Glass | 4ae4264 | 2020-11-05 10:33:39 -0700 | [diff] [blame] | 1061 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1062 | ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images); |
Hector Palacios | 19e8649 | 2016-07-11 12:34:37 +0200 | [diff] [blame] | 1063 | } |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1064 | |
| 1065 | #ifdef CONFIG_TRACE |
| 1066 | /* Pretend to run the OS, then run a user command */ |
| 1067 | if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1068 | char *cmd_list = env_get("fakegocmd"); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1069 | |
| 1070 | ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO, |
| 1071 | images, boot_fn); |
| 1072 | if (!ret && cmd_list) |
| 1073 | ret = run_command_list(cmd_list, -1, flag); |
| 1074 | } |
| 1075 | #endif |
| 1076 | |
| 1077 | /* Check for unsupported subcommand. */ |
| 1078 | if (ret) { |
Simon Glass | 13819f0 | 2022-10-11 09:47:07 -0600 | [diff] [blame] | 1079 | printf("subcommand failed (err=%d)\n", ret); |
Simon Glass | b639640 | 2014-06-12 07:24:46 -0600 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | /* Now run the OS! We hope this doesn't return */ |
| 1084 | if (!ret && (states & BOOTM_STATE_OS_GO)) |
| 1085 | ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO, |
| 1086 | images, boot_fn); |
| 1087 | |
| 1088 | /* Deal with any fallout */ |
| 1089 | err: |
| 1090 | if (iflag) |
| 1091 | enable_interrupts(); |
| 1092 | |
| 1093 | if (ret == BOOTM_ERR_UNIMPLEMENTED) |
| 1094 | bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL); |
| 1095 | else if (ret == BOOTM_ERR_RESET) |
| 1096 | do_reset(cmdtp, flag, argc, argv); |
| 1097 | |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
Simon Glass | daffb0b | 2023-07-30 11:17:02 -0600 | [diff] [blame] | 1101 | int bootm_boot_start(ulong addr, const char *cmdline) |
| 1102 | { |
| 1103 | static struct cmd_tbl cmd = {"bootm"}; |
| 1104 | char addr_str[30]; |
| 1105 | char *argv[] = {addr_str, NULL}; |
| 1106 | int states; |
| 1107 | int ret; |
| 1108 | |
| 1109 | /* |
| 1110 | * TODO(sjg@chromium.org): This uses the command-line interface, but |
| 1111 | * should not. To clean this up, the various bootm states need to be |
| 1112 | * passed an info structure instead of cmdline flags. Then this can |
| 1113 | * set up the required info and move through the states without needing |
| 1114 | * the command line. |
| 1115 | */ |
| 1116 | states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD | |
| 1117 | BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS | |
| 1118 | BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | |
| 1119 | BOOTM_STATE_OS_GO; |
| 1120 | if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH)) |
| 1121 | states |= BOOTM_STATE_RAMDISK; |
| 1122 | if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS)) |
| 1123 | states |= BOOTM_STATE_OS_CMDLINE; |
| 1124 | images.state |= states; |
| 1125 | |
| 1126 | snprintf(addr_str, sizeof(addr_str), "%lx", addr); |
| 1127 | |
| 1128 | ret = env_set("bootargs", cmdline); |
| 1129 | if (ret) { |
| 1130 | printf("Failed to set cmdline\n"); |
| 1131 | return ret; |
| 1132 | } |
| 1133 | ret = do_bootm_states(&cmd, 0, 1, argv, states, &images, 1); |
| 1134 | |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
Heinrich Schuchardt | f6c6df7 | 2019-01-08 18:13:06 +0100 | [diff] [blame] | 1138 | /** |
| 1139 | * switch_to_non_secure_mode() - switch to non-secure mode |
| 1140 | * |
| 1141 | * This routine is overridden by architectures requiring this feature. |
| 1142 | */ |
| 1143 | void __weak switch_to_non_secure_mode(void) |
| 1144 | { |
| 1145 | } |
| 1146 | |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1147 | #else /* USE_HOSTCC */ |
| 1148 | |
Fabrice Fontaine | 93e0788 | 2019-05-03 22:37:05 +0200 | [diff] [blame] | 1149 | #if defined(CONFIG_FIT_SIGNATURE) |
Simon Glass | 8a9d037 | 2020-03-18 11:44:02 -0600 | [diff] [blame] | 1150 | static int bootm_host_load_image(const void *fit, int req_image_type, |
| 1151 | int cfg_noffset) |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1152 | { |
| 1153 | const char *fit_uname_config = NULL; |
| 1154 | ulong data, len; |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 1155 | struct bootm_headers images; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1156 | int noffset; |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 1157 | ulong load_end, buf_size; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1158 | uint8_t image_type; |
Daniel Golle | 0cd57f2 | 2022-08-27 04:14:42 +0100 | [diff] [blame] | 1159 | uint8_t image_comp; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1160 | void *load_buf; |
| 1161 | int ret; |
| 1162 | |
Simon Glass | 8a9d037 | 2020-03-18 11:44:02 -0600 | [diff] [blame] | 1163 | fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1164 | memset(&images, '\0', sizeof(images)); |
| 1165 | images.verify = 1; |
| 1166 | noffset = fit_image_load(&images, (ulong)fit, |
| 1167 | NULL, &fit_uname_config, |
| 1168 | IH_ARCH_DEFAULT, req_image_type, -1, |
| 1169 | FIT_LOAD_IGNORED, &data, &len); |
| 1170 | if (noffset < 0) |
| 1171 | return noffset; |
| 1172 | if (fit_image_get_type(fit, noffset, &image_type)) { |
| 1173 | puts("Can't get image type!\n"); |
| 1174 | return -EINVAL; |
| 1175 | } |
| 1176 | |
Daniel Golle | 88de6c5 | 2022-08-27 04:17:28 +0100 | [diff] [blame] | 1177 | if (fit_image_get_comp(fit, noffset, &image_comp)) |
| 1178 | image_comp = IH_COMP_NONE; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1179 | |
| 1180 | /* Allow the image to expand by a factor of 4, should be safe */ |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 1181 | buf_size = (1 << 20) + len * 4; |
| 1182 | load_buf = malloc(buf_size); |
Daniel Golle | 0cd57f2 | 2022-08-27 04:14:42 +0100 | [diff] [blame] | 1183 | ret = image_decomp(image_comp, 0, data, image_type, load_buf, |
Tom Rini | c45568c | 2022-06-25 19:29:46 -0400 | [diff] [blame] | 1184 | (void *)data, len, buf_size, &load_end); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1185 | free(load_buf); |
Simon Glass | 081cc19 | 2014-12-02 13:17:33 -0700 | [diff] [blame] | 1186 | |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 1187 | if (ret) { |
Daniel Golle | 0cd57f2 | 2022-08-27 04:14:42 +0100 | [diff] [blame] | 1188 | ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret); |
Julius Werner | 2090854 | 2019-07-24 19:37:54 -0700 | [diff] [blame] | 1189 | if (ret != BOOTM_ERR_UNIMPLEMENTED) |
| 1190 | return ret; |
| 1191 | } |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | int bootm_host_load_images(const void *fit, int cfg_noffset) |
| 1197 | { |
| 1198 | static uint8_t image_types[] = { |
| 1199 | IH_TYPE_KERNEL, |
| 1200 | IH_TYPE_FLATDT, |
| 1201 | IH_TYPE_RAMDISK, |
| 1202 | }; |
| 1203 | int err = 0; |
| 1204 | int i; |
| 1205 | |
| 1206 | for (i = 0; i < ARRAY_SIZE(image_types); i++) { |
| 1207 | int ret; |
| 1208 | |
Simon Glass | 8a9d037 | 2020-03-18 11:44:02 -0600 | [diff] [blame] | 1209 | ret = bootm_host_load_image(fit, image_types[i], cfg_noffset); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1210 | if (!err && ret && ret != -ENOENT) |
| 1211 | err = ret; |
| 1212 | } |
| 1213 | |
| 1214 | /* Return the first error we found */ |
| 1215 | return err; |
| 1216 | } |
Fabrice Fontaine | 93e0788 | 2019-05-03 22:37:05 +0200 | [diff] [blame] | 1217 | #endif |
Simon Glass | ea51a62 | 2014-06-12 07:24:51 -0600 | [diff] [blame] | 1218 | |
| 1219 | #endif /* ndef USE_HOSTCC */ |