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