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