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