Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * EFI stub implementation that is shared by arm and arm64 architectures. |
| 3 | * This should be #included by the EFI stub implementation files. |
| 4 | * |
| 5 | * Copyright (C) 2013,2014 Linaro Limited |
| 6 | * Roy Franz <roy.franz@linaro.org |
| 7 | * Copyright (C) 2013 Red Hat, Inc. |
| 8 | * Mark Salter <msalter@redhat.com> |
| 9 | * |
| 10 | * This file is part of the Linux kernel, and is made available under the |
| 11 | * terms of the GNU General Public License version 2. |
| 12 | * |
| 13 | */ |
| 14 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 15 | #include <linux/efi.h> |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 16 | #include <linux/sort.h> |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 17 | #include <asm/efi.h> |
| 18 | |
| 19 | #include "efistub.h" |
| 20 | |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 21 | bool __nokaslr; |
| 22 | |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 23 | static int efi_get_secureboot(efi_system_table_t *sys_table_arg) |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 24 | { |
Linn Crosetto | 30d7bf0 | 2016-04-25 21:06:37 +0100 | [diff] [blame] | 25 | static efi_char16_t const sb_var_name[] = { |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 26 | 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 }; |
Linn Crosetto | 30d7bf0 | 2016-04-25 21:06:37 +0100 | [diff] [blame] | 27 | static efi_char16_t const sm_var_name[] = { |
| 28 | 'S', 'e', 't', 'u', 'p', 'M', 'o', 'd', 'e', 0 }; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 29 | |
Linn Crosetto | 30d7bf0 | 2016-04-25 21:06:37 +0100 | [diff] [blame] | 30 | efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 31 | efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 32 | u8 val; |
Linn Crosetto | 30d7bf0 | 2016-04-25 21:06:37 +0100 | [diff] [blame] | 33 | unsigned long size = sizeof(val); |
| 34 | efi_status_t status; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 35 | |
Linn Crosetto | 30d7bf0 | 2016-04-25 21:06:37 +0100 | [diff] [blame] | 36 | status = f_getvar((efi_char16_t *)sb_var_name, (efi_guid_t *)&var_guid, |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 37 | NULL, &size, &val); |
| 38 | |
Linn Crosetto | 30d7bf0 | 2016-04-25 21:06:37 +0100 | [diff] [blame] | 39 | if (status != EFI_SUCCESS) |
| 40 | goto out_efi_err; |
| 41 | |
| 42 | if (val == 0) |
| 43 | return 0; |
| 44 | |
| 45 | status = f_getvar((efi_char16_t *)sm_var_name, (efi_guid_t *)&var_guid, |
| 46 | NULL, &size, &val); |
| 47 | |
| 48 | if (status != EFI_SUCCESS) |
| 49 | goto out_efi_err; |
| 50 | |
| 51 | if (val == 1) |
| 52 | return 0; |
| 53 | |
| 54 | return 1; |
| 55 | |
| 56 | out_efi_err: |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 57 | switch (status) { |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 58 | case EFI_NOT_FOUND: |
| 59 | return 0; |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 60 | case EFI_DEVICE_ERROR: |
| 61 | return -EIO; |
| 62 | case EFI_SECURITY_VIOLATION: |
| 63 | return -EACCES; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 64 | default: |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 65 | return -EINVAL; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 69 | efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg, |
| 70 | void *__image, void **__fh) |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 71 | { |
| 72 | efi_file_io_interface_t *io; |
| 73 | efi_loaded_image_t *image = __image; |
| 74 | efi_file_handle_t *fh; |
| 75 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 76 | efi_status_t status; |
| 77 | void *handle = (void *)(unsigned long)image->device_handle; |
| 78 | |
| 79 | status = sys_table_arg->boottime->handle_protocol(handle, |
| 80 | &fs_proto, (void **)&io); |
| 81 | if (status != EFI_SUCCESS) { |
| 82 | efi_printk(sys_table_arg, "Failed to handle fs_proto\n"); |
| 83 | return status; |
| 84 | } |
| 85 | |
| 86 | status = io->open_volume(io, &fh); |
| 87 | if (status != EFI_SUCCESS) |
| 88 | efi_printk(sys_table_arg, "Failed to open volume\n"); |
| 89 | |
| 90 | *__fh = fh; |
| 91 | return status; |
| 92 | } |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 93 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 94 | void efi_char16_printk(efi_system_table_t *sys_table_arg, |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 95 | efi_char16_t *str) |
| 96 | { |
| 97 | struct efi_simple_text_output_protocol *out; |
| 98 | |
| 99 | out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out; |
| 100 | out->output_string(out, str); |
| 101 | } |
| 102 | |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 103 | static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg) |
| 104 | { |
| 105 | efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 106 | efi_status_t status; |
| 107 | unsigned long size; |
| 108 | void **gop_handle = NULL; |
| 109 | struct screen_info *si = NULL; |
| 110 | |
| 111 | size = 0; |
| 112 | status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
| 113 | &gop_proto, NULL, &size, gop_handle); |
| 114 | if (status == EFI_BUFFER_TOO_SMALL) { |
| 115 | si = alloc_screen_info(sys_table_arg); |
| 116 | if (!si) |
| 117 | return NULL; |
| 118 | efi_setup_gop(sys_table_arg, si, &gop_proto, size); |
| 119 | } |
| 120 | return si; |
| 121 | } |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * This function handles the architcture specific differences between arm and |
| 125 | * arm64 regarding where the kernel image must be loaded and any memory that |
| 126 | * must be reserved. On failure it is required to free all |
| 127 | * all allocations it has made. |
| 128 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 129 | efi_status_t handle_kernel_image(efi_system_table_t *sys_table, |
| 130 | unsigned long *image_addr, |
| 131 | unsigned long *image_size, |
| 132 | unsigned long *reserve_addr, |
| 133 | unsigned long *reserve_size, |
| 134 | unsigned long dram_base, |
| 135 | efi_loaded_image_t *image); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 136 | /* |
| 137 | * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint |
| 138 | * that is described in the PE/COFF header. Most of the code is the same |
| 139 | * for both archictectures, with the arch-specific code provided in the |
| 140 | * handle_kernel_image() function. |
| 141 | */ |
Ard Biesheuvel | ddeeefe | 2015-01-12 20:28:20 +0000 | [diff] [blame] | 142 | unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 143 | unsigned long *image_addr) |
| 144 | { |
| 145 | efi_loaded_image_t *image; |
| 146 | efi_status_t status; |
| 147 | unsigned long image_size = 0; |
| 148 | unsigned long dram_base; |
| 149 | /* addr/point and size pairs for memory management*/ |
| 150 | unsigned long initrd_addr; |
| 151 | u64 initrd_size = 0; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 152 | unsigned long fdt_addr = 0; /* Original DTB */ |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 153 | unsigned long fdt_size = 0; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 154 | char *cmdline_ptr = NULL; |
| 155 | int cmdline_size = 0; |
| 156 | unsigned long new_fdt_addr; |
| 157 | efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; |
| 158 | unsigned long reserve_addr = 0; |
| 159 | unsigned long reserve_size = 0; |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 160 | int secure_boot = 0; |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 161 | struct screen_info *si; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 162 | |
| 163 | /* Check if we were booted by the EFI firmware */ |
| 164 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 165 | goto fail; |
| 166 | |
| 167 | pr_efi(sys_table, "Booting Linux Kernel...\n"); |
| 168 | |
Ard Biesheuvel | b9d6769b | 2016-02-17 12:36:03 +0000 | [diff] [blame] | 169 | status = check_platform_features(sys_table); |
| 170 | if (status != EFI_SUCCESS) |
| 171 | goto fail; |
| 172 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 173 | /* |
| 174 | * Get a handle to the loaded image protocol. This is used to get |
| 175 | * information about the running image, such as size and the command |
| 176 | * line. |
| 177 | */ |
| 178 | status = sys_table->boottime->handle_protocol(handle, |
| 179 | &loaded_image_proto, (void *)&image); |
| 180 | if (status != EFI_SUCCESS) { |
| 181 | pr_efi_err(sys_table, "Failed to get loaded image protocol\n"); |
| 182 | goto fail; |
| 183 | } |
| 184 | |
| 185 | dram_base = get_dram_base(sys_table); |
| 186 | if (dram_base == EFI_ERROR) { |
| 187 | pr_efi_err(sys_table, "Failed to find DRAM base\n"); |
| 188 | goto fail; |
| 189 | } |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * Get the command line from EFI, using the LOADED_IMAGE |
| 193 | * protocol. We are going to copy the command line into the |
| 194 | * device tree, so this can be allocated anywhere. |
| 195 | */ |
| 196 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size); |
| 197 | if (!cmdline_ptr) { |
| 198 | pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n"); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 199 | goto fail; |
| 200 | } |
| 201 | |
| 202 | /* check whether 'nokaslr' was passed on the command line */ |
| 203 | if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { |
| 204 | static const u8 default_cmdline[] = CONFIG_CMDLINE; |
| 205 | const u8 *str, *cmdline = cmdline_ptr; |
| 206 | |
| 207 | if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) |
| 208 | cmdline = default_cmdline; |
| 209 | str = strstr(cmdline, "nokaslr"); |
| 210 | if (str == cmdline || (str > cmdline && *(str - 1) == ' ')) |
| 211 | __nokaslr = true; |
| 212 | } |
| 213 | |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 214 | si = setup_graphics(sys_table); |
| 215 | |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 216 | status = handle_kernel_image(sys_table, image_addr, &image_size, |
| 217 | &reserve_addr, |
| 218 | &reserve_size, |
| 219 | dram_base, image); |
| 220 | if (status != EFI_SUCCESS) { |
| 221 | pr_efi_err(sys_table, "Failed to relocate kernel\n"); |
| 222 | goto fail_free_cmdline; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 225 | status = efi_parse_options(cmdline_ptr); |
| 226 | if (status != EFI_SUCCESS) |
| 227 | pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n"); |
| 228 | |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 229 | secure_boot = efi_get_secureboot(sys_table); |
| 230 | if (secure_boot > 0) |
| 231 | pr_efi(sys_table, "UEFI Secure Boot is enabled.\n"); |
| 232 | |
| 233 | if (secure_boot < 0) { |
| 234 | pr_efi_err(sys_table, |
| 235 | "could not determine UEFI Secure Boot status.\n"); |
| 236 | } |
| 237 | |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 238 | /* |
| 239 | * Unauthenticated device tree data is a security hazard, so |
| 240 | * ignore 'dtb=' unless UEFI Secure Boot is disabled. |
| 241 | */ |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 242 | if (secure_boot != 0 && strstr(cmdline_ptr, "dtb=")) { |
| 243 | pr_efi(sys_table, "Ignoring DTB from command line.\n"); |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 244 | } else { |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 245 | status = handle_cmdline_files(sys_table, image, cmdline_ptr, |
| 246 | "dtb=", |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 247 | ~0UL, &fdt_addr, &fdt_size); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 248 | |
| 249 | if (status != EFI_SUCCESS) { |
| 250 | pr_efi_err(sys_table, "Failed to load device tree!\n"); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 251 | goto fail_free_image; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
Mark Rutland | 0bcaa90 | 2014-10-23 16:33:33 +0100 | [diff] [blame] | 254 | |
| 255 | if (fdt_addr) { |
| 256 | pr_efi(sys_table, "Using DTB from command line\n"); |
| 257 | } else { |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 258 | /* Look for a device tree configuration table entry. */ |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 259 | fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size); |
Mark Rutland | 0bcaa90 | 2014-10-23 16:33:33 +0100 | [diff] [blame] | 260 | if (fdt_addr) |
| 261 | pr_efi(sys_table, "Using DTB from configuration table\n"); |
| 262 | } |
| 263 | |
| 264 | if (!fdt_addr) |
| 265 | pr_efi(sys_table, "Generating empty DTB\n"); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 266 | |
| 267 | status = handle_cmdline_files(sys_table, image, cmdline_ptr, |
| 268 | "initrd=", dram_base + SZ_512M, |
| 269 | (unsigned long *)&initrd_addr, |
| 270 | (unsigned long *)&initrd_size); |
| 271 | if (status != EFI_SUCCESS) |
| 272 | pr_efi_err(sys_table, "Failed initrd from command line!\n"); |
| 273 | |
Ard Biesheuvel | 568bc4e | 2016-11-12 21:32:33 +0000 | [diff] [blame] | 274 | efi_random_get_seed(sys_table); |
| 275 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 276 | new_fdt_addr = fdt_addr; |
| 277 | status = allocate_new_fdt_and_exit_boot(sys_table, handle, |
| 278 | &new_fdt_addr, dram_base + MAX_FDT_OFFSET, |
| 279 | initrd_addr, initrd_size, cmdline_ptr, |
| 280 | fdt_addr, fdt_size); |
| 281 | |
| 282 | /* |
| 283 | * If all went well, we need to return the FDT address to the |
| 284 | * calling function so it can be passed to kernel as part of |
| 285 | * the kernel boot protocol. |
| 286 | */ |
| 287 | if (status == EFI_SUCCESS) |
| 288 | return new_fdt_addr; |
| 289 | |
| 290 | pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n"); |
| 291 | |
| 292 | efi_free(sys_table, initrd_size, initrd_addr); |
| 293 | efi_free(sys_table, fdt_size, fdt_addr); |
| 294 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 295 | fail_free_image: |
| 296 | efi_free(sys_table, image_size, *image_addr); |
| 297 | efi_free(sys_table, reserve_size, reserve_addr); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 298 | fail_free_cmdline: |
Ard Biesheuvel | f0827e1 | 2016-04-25 21:06:54 +0100 | [diff] [blame] | 299 | free_screen_info(sys_table, si); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 300 | efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 301 | fail: |
| 302 | return EFI_ERROR; |
| 303 | } |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * This is the base address at which to start allocating virtual memory ranges |
| 307 | * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use |
| 308 | * any allocation we choose, and eliminate the risk of a conflict after kexec. |
| 309 | * The value chosen is the largest non-zero power of 2 suitable for this purpose |
| 310 | * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can |
| 311 | * be mapped efficiently. |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 312 | * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split, |
| 313 | * map everything below 1 GB. |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 314 | */ |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 315 | #define EFI_RT_VIRTUAL_BASE SZ_512M |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 316 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 317 | static int cmp_mem_desc(const void *l, const void *r) |
| 318 | { |
| 319 | const efi_memory_desc_t *left = l, *right = r; |
| 320 | |
| 321 | return (left->phys_addr > right->phys_addr) ? 1 : -1; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Returns whether region @left ends exactly where region @right starts, |
| 326 | * or false if either argument is NULL. |
| 327 | */ |
| 328 | static bool regions_are_adjacent(efi_memory_desc_t *left, |
| 329 | efi_memory_desc_t *right) |
| 330 | { |
| 331 | u64 left_end; |
| 332 | |
| 333 | if (left == NULL || right == NULL) |
| 334 | return false; |
| 335 | |
| 336 | left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE; |
| 337 | |
| 338 | return left_end == right->phys_addr; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Returns whether region @left and region @right have compatible memory type |
| 343 | * mapping attributes, and are both EFI_MEMORY_RUNTIME regions. |
| 344 | */ |
| 345 | static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left, |
| 346 | efi_memory_desc_t *right) |
| 347 | { |
| 348 | static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT | |
| 349 | EFI_MEMORY_WC | EFI_MEMORY_UC | |
| 350 | EFI_MEMORY_RUNTIME; |
| 351 | |
| 352 | return ((left->attribute ^ right->attribute) & mem_type_mask) == 0; |
| 353 | } |
| 354 | |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 355 | /* |
| 356 | * efi_get_virtmap() - create a virtual mapping for the EFI memory map |
| 357 | * |
| 358 | * This function populates the virt_addr fields of all memory region descriptors |
| 359 | * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors |
| 360 | * are also copied to @runtime_map, and their total count is returned in @count. |
| 361 | */ |
| 362 | void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, |
| 363 | unsigned long desc_size, efi_memory_desc_t *runtime_map, |
| 364 | int *count) |
| 365 | { |
| 366 | u64 efi_virt_base = EFI_RT_VIRTUAL_BASE; |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 367 | efi_memory_desc_t *in, *prev = NULL, *out = runtime_map; |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 368 | int l; |
| 369 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 370 | /* |
| 371 | * To work around potential issues with the Properties Table feature |
| 372 | * introduced in UEFI 2.5, which may split PE/COFF executable images |
| 373 | * in memory into several RuntimeServicesCode and RuntimeServicesData |
| 374 | * regions, we need to preserve the relative offsets between adjacent |
| 375 | * EFI_MEMORY_RUNTIME regions with the same memory type attributes. |
| 376 | * The easiest way to find adjacent regions is to sort the memory map |
| 377 | * before traversing it. |
| 378 | */ |
| 379 | sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL); |
| 380 | |
| 381 | for (l = 0; l < map_size; l += desc_size, prev = in) { |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 382 | u64 paddr, size; |
| 383 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 384 | in = (void *)memory_map + l; |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 385 | if (!(in->attribute & EFI_MEMORY_RUNTIME)) |
| 386 | continue; |
| 387 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 388 | paddr = in->phys_addr; |
| 389 | size = in->num_pages * EFI_PAGE_SIZE; |
| 390 | |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 391 | /* |
| 392 | * Make the mapping compatible with 64k pages: this allows |
| 393 | * a 4k page size kernel to kexec a 64k page size kernel and |
| 394 | * vice versa. |
| 395 | */ |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 396 | if (!regions_are_adjacent(prev, in) || |
| 397 | !regions_have_compatible_memory_type_attrs(prev, in)) { |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 398 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 399 | paddr = round_down(in->phys_addr, SZ_64K); |
| 400 | size += in->phys_addr - paddr; |
| 401 | |
| 402 | /* |
| 403 | * Avoid wasting memory on PTEs by choosing a virtual |
| 404 | * base that is compatible with section mappings if this |
| 405 | * region has the appropriate size and physical |
| 406 | * alignment. (Sections are 2 MB on 4k granule kernels) |
| 407 | */ |
| 408 | if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) |
| 409 | efi_virt_base = round_up(efi_virt_base, SZ_2M); |
| 410 | else |
| 411 | efi_virt_base = round_up(efi_virt_base, SZ_64K); |
| 412 | } |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 413 | |
| 414 | in->virt_addr = efi_virt_base + in->phys_addr - paddr; |
| 415 | efi_virt_base += size; |
| 416 | |
| 417 | memcpy(out, in, desc_size); |
| 418 | out = (void *)out + desc_size; |
| 419 | ++*count; |
| 420 | } |
| 421 | } |