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