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