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 | |
| 94 | efi_status_t efi_file_close(void *handle) |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 95 | { |
| 96 | efi_file_handle_t *fh = handle; |
| 97 | |
| 98 | return fh->close(handle); |
| 99 | } |
| 100 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 101 | efi_status_t |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 102 | efi_file_read(void *handle, unsigned long *size, void *addr) |
| 103 | { |
| 104 | efi_file_handle_t *fh = handle; |
| 105 | |
| 106 | return fh->read(handle, size, addr); |
| 107 | } |
| 108 | |
| 109 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 110 | efi_status_t |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 111 | efi_file_size(efi_system_table_t *sys_table_arg, void *__fh, |
| 112 | efi_char16_t *filename_16, void **handle, u64 *file_sz) |
| 113 | { |
| 114 | efi_file_handle_t *h, *fh = __fh; |
| 115 | efi_file_info_t *info; |
| 116 | efi_status_t status; |
| 117 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
| 118 | unsigned long info_sz; |
| 119 | |
| 120 | status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0); |
| 121 | if (status != EFI_SUCCESS) { |
| 122 | efi_printk(sys_table_arg, "Failed to open file: "); |
| 123 | efi_char16_printk(sys_table_arg, filename_16); |
| 124 | efi_printk(sys_table_arg, "\n"); |
| 125 | return status; |
| 126 | } |
| 127 | |
| 128 | *handle = h; |
| 129 | |
| 130 | info_sz = 0; |
| 131 | status = h->get_info(h, &info_guid, &info_sz, NULL); |
| 132 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 133 | efi_printk(sys_table_arg, "Failed to get file info size\n"); |
| 134 | return status; |
| 135 | } |
| 136 | |
| 137 | grow: |
| 138 | status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA, |
| 139 | info_sz, (void **)&info); |
| 140 | if (status != EFI_SUCCESS) { |
| 141 | efi_printk(sys_table_arg, "Failed to alloc mem for file info\n"); |
| 142 | return status; |
| 143 | } |
| 144 | |
| 145 | status = h->get_info(h, &info_guid, &info_sz, |
| 146 | info); |
| 147 | if (status == EFI_BUFFER_TOO_SMALL) { |
| 148 | sys_table_arg->boottime->free_pool(info); |
| 149 | goto grow; |
| 150 | } |
| 151 | |
| 152 | *file_sz = info->file_size; |
| 153 | sys_table_arg->boottime->free_pool(info); |
| 154 | |
| 155 | if (status != EFI_SUCCESS) |
| 156 | efi_printk(sys_table_arg, "Failed to get initrd info\n"); |
| 157 | |
| 158 | return status; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 163 | void efi_char16_printk(efi_system_table_t *sys_table_arg, |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 164 | efi_char16_t *str) |
| 165 | { |
| 166 | struct efi_simple_text_output_protocol *out; |
| 167 | |
| 168 | out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out; |
| 169 | out->output_string(out, str); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* |
| 174 | * This function handles the architcture specific differences between arm and |
| 175 | * arm64 regarding where the kernel image must be loaded and any memory that |
| 176 | * must be reserved. On failure it is required to free all |
| 177 | * all allocations it has made. |
| 178 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 179 | efi_status_t handle_kernel_image(efi_system_table_t *sys_table, |
| 180 | unsigned long *image_addr, |
| 181 | unsigned long *image_size, |
| 182 | unsigned long *reserve_addr, |
| 183 | unsigned long *reserve_size, |
| 184 | unsigned long dram_base, |
| 185 | efi_loaded_image_t *image); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 186 | /* |
| 187 | * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint |
| 188 | * that is described in the PE/COFF header. Most of the code is the same |
| 189 | * for both archictectures, with the arch-specific code provided in the |
| 190 | * handle_kernel_image() function. |
| 191 | */ |
Ard Biesheuvel | ddeeefe | 2015-01-12 20:28:20 +0000 | [diff] [blame] | 192 | unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 193 | unsigned long *image_addr) |
| 194 | { |
| 195 | efi_loaded_image_t *image; |
| 196 | efi_status_t status; |
| 197 | unsigned long image_size = 0; |
| 198 | unsigned long dram_base; |
| 199 | /* addr/point and size pairs for memory management*/ |
| 200 | unsigned long initrd_addr; |
| 201 | u64 initrd_size = 0; |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 202 | unsigned long fdt_addr = 0; /* Original DTB */ |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 203 | unsigned long fdt_size = 0; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 204 | char *cmdline_ptr = NULL; |
| 205 | int cmdline_size = 0; |
| 206 | unsigned long new_fdt_addr; |
| 207 | efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; |
| 208 | unsigned long reserve_addr = 0; |
| 209 | unsigned long reserve_size = 0; |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 210 | int secure_boot = 0; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 211 | |
| 212 | /* Check if we were booted by the EFI firmware */ |
| 213 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 214 | goto fail; |
| 215 | |
| 216 | pr_efi(sys_table, "Booting Linux Kernel...\n"); |
| 217 | |
Ard Biesheuvel | b9d6769b | 2016-02-17 12:36:03 +0000 | [diff] [blame] | 218 | status = check_platform_features(sys_table); |
| 219 | if (status != EFI_SUCCESS) |
| 220 | goto fail; |
| 221 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 222 | /* |
| 223 | * Get a handle to the loaded image protocol. This is used to get |
| 224 | * information about the running image, such as size and the command |
| 225 | * line. |
| 226 | */ |
| 227 | status = sys_table->boottime->handle_protocol(handle, |
| 228 | &loaded_image_proto, (void *)&image); |
| 229 | if (status != EFI_SUCCESS) { |
| 230 | pr_efi_err(sys_table, "Failed to get loaded image protocol\n"); |
| 231 | goto fail; |
| 232 | } |
| 233 | |
| 234 | dram_base = get_dram_base(sys_table); |
| 235 | if (dram_base == EFI_ERROR) { |
| 236 | pr_efi_err(sys_table, "Failed to find DRAM base\n"); |
| 237 | goto fail; |
| 238 | } |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Get the command line from EFI, using the LOADED_IMAGE |
| 242 | * protocol. We are going to copy the command line into the |
| 243 | * device tree, so this can be allocated anywhere. |
| 244 | */ |
| 245 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size); |
| 246 | if (!cmdline_ptr) { |
| 247 | 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] | 248 | goto fail; |
| 249 | } |
| 250 | |
| 251 | /* check whether 'nokaslr' was passed on the command line */ |
| 252 | if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { |
| 253 | static const u8 default_cmdline[] = CONFIG_CMDLINE; |
| 254 | const u8 *str, *cmdline = cmdline_ptr; |
| 255 | |
| 256 | if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) |
| 257 | cmdline = default_cmdline; |
| 258 | str = strstr(cmdline, "nokaslr"); |
| 259 | if (str == cmdline || (str > cmdline && *(str - 1) == ' ')) |
| 260 | __nokaslr = true; |
| 261 | } |
| 262 | |
| 263 | status = handle_kernel_image(sys_table, image_addr, &image_size, |
| 264 | &reserve_addr, |
| 265 | &reserve_size, |
| 266 | dram_base, image); |
| 267 | if (status != EFI_SUCCESS) { |
| 268 | pr_efi_err(sys_table, "Failed to relocate kernel\n"); |
| 269 | goto fail_free_cmdline; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 272 | status = efi_parse_options(cmdline_ptr); |
| 273 | if (status != EFI_SUCCESS) |
| 274 | pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n"); |
| 275 | |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 276 | secure_boot = efi_get_secureboot(sys_table); |
| 277 | if (secure_boot > 0) |
| 278 | pr_efi(sys_table, "UEFI Secure Boot is enabled.\n"); |
| 279 | |
| 280 | if (secure_boot < 0) { |
| 281 | pr_efi_err(sys_table, |
| 282 | "could not determine UEFI Secure Boot status.\n"); |
| 283 | } |
| 284 | |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 285 | /* |
| 286 | * Unauthenticated device tree data is a security hazard, so |
| 287 | * ignore 'dtb=' unless UEFI Secure Boot is disabled. |
| 288 | */ |
Linn Crosetto | 73a64925 | 2016-04-25 21:06:36 +0100 | [diff] [blame] | 289 | if (secure_boot != 0 && strstr(cmdline_ptr, "dtb=")) { |
| 290 | pr_efi(sys_table, "Ignoring DTB from command line.\n"); |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 291 | } else { |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 292 | status = handle_cmdline_files(sys_table, image, cmdline_ptr, |
| 293 | "dtb=", |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 294 | ~0UL, &fdt_addr, &fdt_size); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 295 | |
| 296 | if (status != EFI_SUCCESS) { |
| 297 | pr_efi_err(sys_table, "Failed to load device tree!\n"); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 298 | goto fail_free_image; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 299 | } |
| 300 | } |
Mark Rutland | 0bcaa90 | 2014-10-23 16:33:33 +0100 | [diff] [blame] | 301 | |
| 302 | if (fdt_addr) { |
| 303 | pr_efi(sys_table, "Using DTB from command line\n"); |
| 304 | } else { |
Ard Biesheuvel | 345c736 | 2014-04-03 17:46:58 +0200 | [diff] [blame] | 305 | /* Look for a device tree configuration table entry. */ |
Ard Biesheuvel | a643375 | 2015-03-04 13:02:29 +0100 | [diff] [blame] | 306 | fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size); |
Mark Rutland | 0bcaa90 | 2014-10-23 16:33:33 +0100 | [diff] [blame] | 307 | if (fdt_addr) |
| 308 | pr_efi(sys_table, "Using DTB from configuration table\n"); |
| 309 | } |
| 310 | |
| 311 | if (!fdt_addr) |
| 312 | pr_efi(sys_table, "Generating empty DTB\n"); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 313 | |
| 314 | status = handle_cmdline_files(sys_table, image, cmdline_ptr, |
| 315 | "initrd=", dram_base + SZ_512M, |
| 316 | (unsigned long *)&initrd_addr, |
| 317 | (unsigned long *)&initrd_size); |
| 318 | if (status != EFI_SUCCESS) |
| 319 | pr_efi_err(sys_table, "Failed initrd from command line!\n"); |
| 320 | |
| 321 | new_fdt_addr = fdt_addr; |
| 322 | status = allocate_new_fdt_and_exit_boot(sys_table, handle, |
| 323 | &new_fdt_addr, dram_base + MAX_FDT_OFFSET, |
| 324 | initrd_addr, initrd_size, cmdline_ptr, |
| 325 | fdt_addr, fdt_size); |
| 326 | |
| 327 | /* |
| 328 | * If all went well, we need to return the FDT address to the |
| 329 | * calling function so it can be passed to kernel as part of |
| 330 | * the kernel boot protocol. |
| 331 | */ |
| 332 | if (status == EFI_SUCCESS) |
| 333 | return new_fdt_addr; |
| 334 | |
| 335 | pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n"); |
| 336 | |
| 337 | efi_free(sys_table, initrd_size, initrd_addr); |
| 338 | efi_free(sys_table, fdt_size, fdt_addr); |
| 339 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 340 | fail_free_image: |
| 341 | efi_free(sys_table, image_size, *image_addr); |
| 342 | efi_free(sys_table, reserve_size, reserve_addr); |
Ard Biesheuvel | 2b5fe07 | 2016-01-26 14:48:29 +0100 | [diff] [blame] | 343 | fail_free_cmdline: |
| 344 | efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 345 | fail: |
| 346 | return EFI_ERROR; |
| 347 | } |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * This is the base address at which to start allocating virtual memory ranges |
| 351 | * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use |
| 352 | * any allocation we choose, and eliminate the risk of a conflict after kexec. |
| 353 | * The value chosen is the largest non-zero power of 2 suitable for this purpose |
| 354 | * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can |
| 355 | * be mapped efficiently. |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 356 | * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split, |
| 357 | * map everything below 1 GB. |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 358 | */ |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 359 | #define EFI_RT_VIRTUAL_BASE SZ_512M |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 360 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 361 | static int cmp_mem_desc(const void *l, const void *r) |
| 362 | { |
| 363 | const efi_memory_desc_t *left = l, *right = r; |
| 364 | |
| 365 | return (left->phys_addr > right->phys_addr) ? 1 : -1; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Returns whether region @left ends exactly where region @right starts, |
| 370 | * or false if either argument is NULL. |
| 371 | */ |
| 372 | static bool regions_are_adjacent(efi_memory_desc_t *left, |
| 373 | efi_memory_desc_t *right) |
| 374 | { |
| 375 | u64 left_end; |
| 376 | |
| 377 | if (left == NULL || right == NULL) |
| 378 | return false; |
| 379 | |
| 380 | left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE; |
| 381 | |
| 382 | return left_end == right->phys_addr; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Returns whether region @left and region @right have compatible memory type |
| 387 | * mapping attributes, and are both EFI_MEMORY_RUNTIME regions. |
| 388 | */ |
| 389 | static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left, |
| 390 | efi_memory_desc_t *right) |
| 391 | { |
| 392 | static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT | |
| 393 | EFI_MEMORY_WC | EFI_MEMORY_UC | |
| 394 | EFI_MEMORY_RUNTIME; |
| 395 | |
| 396 | return ((left->attribute ^ right->attribute) & mem_type_mask) == 0; |
| 397 | } |
| 398 | |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 399 | /* |
| 400 | * efi_get_virtmap() - create a virtual mapping for the EFI memory map |
| 401 | * |
| 402 | * This function populates the virt_addr fields of all memory region descriptors |
| 403 | * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors |
| 404 | * are also copied to @runtime_map, and their total count is returned in @count. |
| 405 | */ |
| 406 | void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, |
| 407 | unsigned long desc_size, efi_memory_desc_t *runtime_map, |
| 408 | int *count) |
| 409 | { |
| 410 | u64 efi_virt_base = EFI_RT_VIRTUAL_BASE; |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 411 | efi_memory_desc_t *in, *prev = NULL, *out = runtime_map; |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 412 | int l; |
| 413 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 414 | /* |
| 415 | * To work around potential issues with the Properties Table feature |
| 416 | * introduced in UEFI 2.5, which may split PE/COFF executable images |
| 417 | * in memory into several RuntimeServicesCode and RuntimeServicesData |
| 418 | * regions, we need to preserve the relative offsets between adjacent |
| 419 | * EFI_MEMORY_RUNTIME regions with the same memory type attributes. |
| 420 | * The easiest way to find adjacent regions is to sort the memory map |
| 421 | * before traversing it. |
| 422 | */ |
| 423 | sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL); |
| 424 | |
| 425 | for (l = 0; l < map_size; l += desc_size, prev = in) { |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 426 | u64 paddr, size; |
| 427 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 428 | in = (void *)memory_map + l; |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 429 | if (!(in->attribute & EFI_MEMORY_RUNTIME)) |
| 430 | continue; |
| 431 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 432 | paddr = in->phys_addr; |
| 433 | size = in->num_pages * EFI_PAGE_SIZE; |
| 434 | |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 435 | /* |
| 436 | * Make the mapping compatible with 64k pages: this allows |
| 437 | * a 4k page size kernel to kexec a 64k page size kernel and |
| 438 | * vice versa. |
| 439 | */ |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 440 | if (!regions_are_adjacent(prev, in) || |
| 441 | !regions_have_compatible_memory_type_attrs(prev, in)) { |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 442 | |
Ard Biesheuvel | 0ce3cc0 | 2015-09-25 23:02:19 +0100 | [diff] [blame] | 443 | paddr = round_down(in->phys_addr, SZ_64K); |
| 444 | size += in->phys_addr - paddr; |
| 445 | |
| 446 | /* |
| 447 | * Avoid wasting memory on PTEs by choosing a virtual |
| 448 | * base that is compatible with section mappings if this |
| 449 | * region has the appropriate size and physical |
| 450 | * alignment. (Sections are 2 MB on 4k granule kernels) |
| 451 | */ |
| 452 | if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) |
| 453 | efi_virt_base = round_up(efi_virt_base, SZ_2M); |
| 454 | else |
| 455 | efi_virt_base = round_up(efi_virt_base, SZ_64K); |
| 456 | } |
Ard Biesheuvel | f3cdfd2 | 2014-10-20 16:27:26 +0200 | [diff] [blame] | 457 | |
| 458 | in->virt_addr = efi_virt_base + in->phys_addr - paddr; |
| 459 | efi_virt_base += size; |
| 460 | |
| 461 | memcpy(out, in, desc_size); |
| 462 | out = (void *)out + desc_size; |
| 463 | ++*count; |
| 464 | } |
| 465 | } |