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