Ard Biesheuvel | 4febfb8 | 2019-02-02 10:41:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Helper functions used by the EFI stub on multiple |
| 4 | * architectures. This should be #included by the EFI stub |
| 5 | * implementation files. |
| 6 | * |
| 7 | * Copyright 2011 Intel Corporation; author Matt Fleming |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 8 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 9 | |
| 10 | #include <linux/efi.h> |
| 11 | #include <asm/efi.h> |
| 12 | |
| 13 | #include "efistub.h" |
| 14 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 15 | /* |
| 16 | * Some firmware implementations have problems reading files in one go. |
| 17 | * A read chunk size of 1MB seems to work for most platforms. |
| 18 | * |
| 19 | * Unfortunately, reading files in chunks triggers *other* bugs on some |
| 20 | * platforms, so we provide a way to disable this workaround, which can |
| 21 | * be done by passing "efi=nochunk" on the EFI boot stub command line. |
| 22 | * |
| 23 | * If you experience issues with initrd images being corrupt it's worth |
| 24 | * trying efi=nochunk, but chunking is enabled by default because there |
| 25 | * are far more machines that require the workaround than those that |
| 26 | * break with it enabled. |
| 27 | */ |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 28 | #define EFI_READ_CHUNK_SIZE (1024 * 1024) |
| 29 | |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 30 | static unsigned long efi_chunk_size = EFI_READ_CHUNK_SIZE; |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 31 | |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 32 | static bool __efistub_global efi_nokaslr; |
| 33 | static bool __efistub_global efi_quiet; |
| 34 | static bool __efistub_global efi_novamap; |
| 35 | static bool __efistub_global efi_nosoftreserve; |
Matthew Garrett | 4444f85 | 2020-01-03 12:39:50 +0100 | [diff] [blame^] | 36 | static bool __efistub_global efi_disable_pci_dma = |
| 37 | IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA); |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 38 | |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 39 | bool __pure nokaslr(void) |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 40 | { |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 41 | return efi_nokaslr; |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 42 | } |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 43 | bool __pure is_quiet(void) |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 44 | { |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 45 | return efi_quiet; |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 46 | } |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 47 | bool __pure novamap(void) |
Ard Biesheuvel | 4e46c2a | 2019-02-02 10:41:16 +0100 | [diff] [blame] | 48 | { |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 49 | return efi_novamap; |
Ard Biesheuvel | 4e46c2a | 2019-02-02 10:41:16 +0100 | [diff] [blame] | 50 | } |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 51 | bool __pure __efi_soft_reserve_enabled(void) |
| 52 | { |
| 53 | return !efi_nosoftreserve; |
| 54 | } |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 55 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 56 | #define EFI_MMAP_NR_SLACK_SLOTS 8 |
| 57 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 58 | struct file_info { |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 59 | efi_file_handle_t *handle; |
| 60 | u64 size; |
| 61 | }; |
| 62 | |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 63 | void efi_printk(char *str) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 64 | { |
| 65 | char *s8; |
| 66 | |
| 67 | for (s8 = str; *s8; s8++) { |
| 68 | efi_char16_t ch[2] = { 0 }; |
| 69 | |
| 70 | ch[0] = *s8; |
| 71 | if (*s8 == '\n') { |
| 72 | efi_char16_t nl[2] = { '\r', 0 }; |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 73 | efi_char16_printk(nl); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 76 | efi_char16_printk(ch); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 80 | static inline bool mmap_has_headroom(unsigned long buff_size, |
| 81 | unsigned long map_size, |
| 82 | unsigned long desc_size) |
| 83 | { |
| 84 | unsigned long slack = buff_size - map_size; |
| 85 | |
| 86 | return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS; |
| 87 | } |
| 88 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 89 | efi_status_t efi_get_memory_map(struct efi_boot_memmap *map) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 90 | { |
| 91 | efi_memory_desc_t *m = NULL; |
| 92 | efi_status_t status; |
| 93 | unsigned long key; |
| 94 | u32 desc_version; |
| 95 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 96 | *map->desc_size = sizeof(*m); |
| 97 | *map->map_size = *map->desc_size * 32; |
| 98 | *map->buff_size = *map->map_size; |
Matt Fleming | 43a9f69 | 2015-02-13 15:46:56 +0000 | [diff] [blame] | 99 | again: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 100 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, |
| 101 | *map->map_size, (void **)&m); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 102 | if (status != EFI_SUCCESS) |
| 103 | goto fail; |
| 104 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 105 | *map->desc_size = 0; |
Matt Fleming | 43a9f69 | 2015-02-13 15:46:56 +0000 | [diff] [blame] | 106 | key = 0; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 107 | status = efi_bs_call(get_memory_map, map->map_size, m, |
| 108 | &key, map->desc_size, &desc_version); |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 109 | if (status == EFI_BUFFER_TOO_SMALL || |
| 110 | !mmap_has_headroom(*map->buff_size, *map->map_size, |
| 111 | *map->desc_size)) { |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 112 | efi_bs_call(free_pool, m); |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 113 | /* |
| 114 | * Make sure there is some entries of headroom so that the |
| 115 | * buffer can be reused for a new map after allocations are |
| 116 | * no longer permitted. Its unlikely that the map will grow to |
| 117 | * exceed this headroom once we are ready to trigger |
| 118 | * ExitBootServices() |
| 119 | */ |
| 120 | *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS; |
| 121 | *map->buff_size = *map->map_size; |
Matt Fleming | 43a9f69 | 2015-02-13 15:46:56 +0000 | [diff] [blame] | 122 | goto again; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (status != EFI_SUCCESS) |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 126 | efi_bs_call(free_pool, m); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 127 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 128 | if (map->key_ptr && status == EFI_SUCCESS) |
| 129 | *map->key_ptr = key; |
| 130 | if (map->desc_ver && status == EFI_SUCCESS) |
| 131 | *map->desc_ver = desc_version; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 132 | |
| 133 | fail: |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 134 | *map->map = m; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 135 | return status; |
| 136 | } |
| 137 | |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 138 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 139 | unsigned long get_dram_base(void) |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 140 | { |
| 141 | efi_status_t status; |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 142 | unsigned long map_size, buff_size; |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 143 | unsigned long membase = EFI_ERROR; |
| 144 | struct efi_memory_map map; |
| 145 | efi_memory_desc_t *md; |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 146 | struct efi_boot_memmap boot_map; |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 147 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 148 | boot_map.map = (efi_memory_desc_t **)&map.map; |
| 149 | boot_map.map_size = &map_size; |
| 150 | boot_map.desc_size = &map.desc_size; |
| 151 | boot_map.desc_ver = NULL; |
| 152 | boot_map.key_ptr = NULL; |
| 153 | boot_map.buff_size = &buff_size; |
| 154 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 155 | status = efi_get_memory_map(&boot_map); |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 156 | if (status != EFI_SUCCESS) |
| 157 | return membase; |
| 158 | |
| 159 | map.map_end = map.map + map_size; |
| 160 | |
Matt Fleming | 78ce248 | 2016-04-25 21:06:38 +0100 | [diff] [blame] | 161 | for_each_efi_memory_desc_in_map(&map, md) { |
| 162 | if (md->attribute & EFI_MEMORY_WB) { |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 163 | if (membase > md->phys_addr) |
| 164 | membase = md->phys_addr; |
Matt Fleming | 78ce248 | 2016-04-25 21:06:38 +0100 | [diff] [blame] | 165 | } |
| 166 | } |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 167 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 168 | efi_bs_call(free_pool, map.map); |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 169 | |
| 170 | return membase; |
| 171 | } |
| 172 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Allocate at the highest possible address that is not above 'max'. |
| 175 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 176 | efi_status_t efi_high_alloc(unsigned long size, unsigned long align, |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 177 | unsigned long *addr, unsigned long max) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 178 | { |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 179 | unsigned long map_size, desc_size, buff_size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 180 | efi_memory_desc_t *map; |
| 181 | efi_status_t status; |
| 182 | unsigned long nr_pages; |
| 183 | u64 max_addr = 0; |
| 184 | int i; |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 185 | struct efi_boot_memmap boot_map; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 186 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 187 | boot_map.map = ↦ |
| 188 | boot_map.map_size = &map_size; |
| 189 | boot_map.desc_size = &desc_size; |
| 190 | boot_map.desc_ver = NULL; |
| 191 | boot_map.key_ptr = NULL; |
| 192 | boot_map.buff_size = &buff_size; |
| 193 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 194 | status = efi_get_memory_map(&boot_map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 195 | if (status != EFI_SUCCESS) |
| 196 | goto fail; |
| 197 | |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 198 | /* |
Roy Franz | 5b88a31 | 2016-11-12 21:32:29 +0000 | [diff] [blame] | 199 | * Enforce minimum alignment that EFI or Linux requires when |
| 200 | * requesting a specific address. We are doing page-based (or |
| 201 | * larger) allocations, and both the address and size must meet |
| 202 | * alignment constraints. |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 203 | */ |
Ard Biesheuvel | cf2b0f1 | 2014-11-17 13:46:44 +0100 | [diff] [blame] | 204 | if (align < EFI_ALLOC_ALIGN) |
| 205 | align = EFI_ALLOC_ALIGN; |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 206 | |
Roy Franz | 5b88a31 | 2016-11-12 21:32:29 +0000 | [diff] [blame] | 207 | size = round_up(size, EFI_ALLOC_ALIGN); |
| 208 | nr_pages = size / EFI_PAGE_SIZE; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 209 | again: |
| 210 | for (i = 0; i < map_size / desc_size; i++) { |
| 211 | efi_memory_desc_t *desc; |
| 212 | unsigned long m = (unsigned long)map; |
| 213 | u64 start, end; |
| 214 | |
Baoquan He | 02e43c2 | 2017-08-16 21:46:51 +0800 | [diff] [blame] | 215 | desc = efi_early_memdesc_ptr(m, desc_size, i); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 216 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 217 | continue; |
| 218 | |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 219 | if (efi_soft_reserve_enabled() && |
| 220 | (desc->attribute & EFI_MEMORY_SP)) |
| 221 | continue; |
| 222 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 223 | if (desc->num_pages < nr_pages) |
| 224 | continue; |
| 225 | |
| 226 | start = desc->phys_addr; |
Roy Franz | 5b88a31 | 2016-11-12 21:32:29 +0000 | [diff] [blame] | 227 | end = start + desc->num_pages * EFI_PAGE_SIZE; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 228 | |
Yinghai Lu | 7ed620b | 2015-02-19 20:18:03 -0800 | [diff] [blame] | 229 | if (end > max) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 230 | end = max; |
| 231 | |
Yinghai Lu | 7ed620b | 2015-02-19 20:18:03 -0800 | [diff] [blame] | 232 | if ((start + size) > end) |
| 233 | continue; |
| 234 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 235 | if (round_down(end - size, align) < start) |
| 236 | continue; |
| 237 | |
| 238 | start = round_down(end - size, align); |
| 239 | |
| 240 | /* |
| 241 | * Don't allocate at 0x0. It will confuse code that |
| 242 | * checks pointers against NULL. |
| 243 | */ |
| 244 | if (start == 0x0) |
| 245 | continue; |
| 246 | |
| 247 | if (start > max_addr) |
| 248 | max_addr = start; |
| 249 | } |
| 250 | |
| 251 | if (!max_addr) |
| 252 | status = EFI_NOT_FOUND; |
| 253 | else { |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 254 | status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, |
| 255 | EFI_LOADER_DATA, nr_pages, &max_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 256 | if (status != EFI_SUCCESS) { |
| 257 | max = max_addr; |
| 258 | max_addr = 0; |
| 259 | goto again; |
| 260 | } |
| 261 | |
| 262 | *addr = max_addr; |
| 263 | } |
| 264 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 265 | efi_bs_call(free_pool, map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 266 | fail: |
| 267 | return status; |
| 268 | } |
| 269 | |
| 270 | /* |
Kairui Song | 220dd76 | 2019-10-29 18:37:54 +0100 | [diff] [blame] | 271 | * Allocate at the lowest possible address that is not below 'min'. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 272 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 273 | efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align, |
Kairui Song | 220dd76 | 2019-10-29 18:37:54 +0100 | [diff] [blame] | 274 | unsigned long *addr, unsigned long min) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 275 | { |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 276 | unsigned long map_size, desc_size, buff_size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 277 | efi_memory_desc_t *map; |
| 278 | efi_status_t status; |
| 279 | unsigned long nr_pages; |
| 280 | int i; |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 281 | struct efi_boot_memmap boot_map; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 282 | |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 283 | boot_map.map = ↦ |
| 284 | boot_map.map_size = &map_size; |
| 285 | boot_map.desc_size = &desc_size; |
| 286 | boot_map.desc_ver = NULL; |
| 287 | boot_map.key_ptr = NULL; |
| 288 | boot_map.buff_size = &buff_size; |
| 289 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 290 | status = efi_get_memory_map(&boot_map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 291 | if (status != EFI_SUCCESS) |
| 292 | goto fail; |
| 293 | |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 294 | /* |
Roy Franz | 5b88a31 | 2016-11-12 21:32:29 +0000 | [diff] [blame] | 295 | * Enforce minimum alignment that EFI or Linux requires when |
| 296 | * requesting a specific address. We are doing page-based (or |
| 297 | * larger) allocations, and both the address and size must meet |
| 298 | * alignment constraints. |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 299 | */ |
Ard Biesheuvel | cf2b0f1 | 2014-11-17 13:46:44 +0100 | [diff] [blame] | 300 | if (align < EFI_ALLOC_ALIGN) |
| 301 | align = EFI_ALLOC_ALIGN; |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 302 | |
Roy Franz | 5b88a31 | 2016-11-12 21:32:29 +0000 | [diff] [blame] | 303 | size = round_up(size, EFI_ALLOC_ALIGN); |
| 304 | nr_pages = size / EFI_PAGE_SIZE; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 305 | for (i = 0; i < map_size / desc_size; i++) { |
| 306 | efi_memory_desc_t *desc; |
| 307 | unsigned long m = (unsigned long)map; |
| 308 | u64 start, end; |
| 309 | |
Baoquan He | 02e43c2 | 2017-08-16 21:46:51 +0800 | [diff] [blame] | 310 | desc = efi_early_memdesc_ptr(m, desc_size, i); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 311 | |
| 312 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 313 | continue; |
| 314 | |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 315 | if (efi_soft_reserve_enabled() && |
| 316 | (desc->attribute & EFI_MEMORY_SP)) |
| 317 | continue; |
| 318 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 319 | if (desc->num_pages < nr_pages) |
| 320 | continue; |
| 321 | |
| 322 | start = desc->phys_addr; |
Roy Franz | 5b88a31 | 2016-11-12 21:32:29 +0000 | [diff] [blame] | 323 | end = start + desc->num_pages * EFI_PAGE_SIZE; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 324 | |
Kairui Song | 220dd76 | 2019-10-29 18:37:54 +0100 | [diff] [blame] | 325 | if (start < min) |
| 326 | start = min; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 327 | |
| 328 | start = round_up(start, align); |
| 329 | if ((start + size) > end) |
| 330 | continue; |
| 331 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 332 | status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, |
| 333 | EFI_LOADER_DATA, nr_pages, &start); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 334 | if (status == EFI_SUCCESS) { |
| 335 | *addr = start; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (i == map_size / desc_size) |
| 341 | status = EFI_NOT_FOUND; |
| 342 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 343 | efi_bs_call(free_pool, map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 344 | fail: |
| 345 | return status; |
| 346 | } |
| 347 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 348 | void efi_free(unsigned long size, unsigned long addr) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 349 | { |
| 350 | unsigned long nr_pages; |
| 351 | |
Roy Franz | 0e1cadb | 2013-09-22 15:45:38 -0700 | [diff] [blame] | 352 | if (!size) |
| 353 | return; |
| 354 | |
Ard Biesheuvel | cf2b0f1 | 2014-11-17 13:46:44 +0100 | [diff] [blame] | 355 | nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE; |
Arvind Sankar | ea7d87f | 2020-01-03 12:39:49 +0100 | [diff] [blame] | 356 | efi_bs_call(free_pages, addr, nr_pages); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 359 | static efi_status_t efi_file_size(void *__fh, efi_char16_t *filename_16, |
| 360 | void **handle, u64 *file_sz) |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 361 | { |
| 362 | efi_file_handle_t *h, *fh = __fh; |
| 363 | efi_file_info_t *info; |
| 364 | efi_status_t status; |
| 365 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
| 366 | unsigned long info_sz; |
| 367 | |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 368 | status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, 0); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 369 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 370 | efi_printk("Failed to open file: "); |
| 371 | efi_char16_printk(filename_16); |
| 372 | efi_printk("\n"); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 373 | return status; |
| 374 | } |
| 375 | |
| 376 | *handle = h; |
| 377 | |
| 378 | info_sz = 0; |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 379 | status = h->get_info(h, &info_guid, &info_sz, NULL); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 380 | if (status != EFI_BUFFER_TOO_SMALL) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 381 | efi_printk("Failed to get file info size\n"); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 382 | return status; |
| 383 | } |
| 384 | |
| 385 | grow: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 386 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, info_sz, |
| 387 | (void **)&info); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 388 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 389 | efi_printk("Failed to alloc mem for file info\n"); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 390 | return status; |
| 391 | } |
| 392 | |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 393 | status = h->get_info(h, &info_guid, &info_sz, info); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 394 | if (status == EFI_BUFFER_TOO_SMALL) { |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 395 | efi_bs_call(free_pool, info); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 396 | goto grow; |
| 397 | } |
| 398 | |
| 399 | *file_sz = info->file_size; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 400 | efi_bs_call(free_pool, info); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 401 | |
| 402 | if (status != EFI_SUCCESS) |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 403 | efi_printk("Failed to get initrd info\n"); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 404 | |
| 405 | return status; |
| 406 | } |
| 407 | |
Ard Biesheuvel | 960a8d0 | 2019-12-24 16:10:11 +0100 | [diff] [blame] | 408 | static efi_status_t efi_file_read(efi_file_handle_t *handle, |
| 409 | unsigned long *size, void *addr) |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 410 | { |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 411 | return handle->read(handle, size, addr); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Ard Biesheuvel | 960a8d0 | 2019-12-24 16:10:11 +0100 | [diff] [blame] | 414 | static efi_status_t efi_file_close(efi_file_handle_t *handle) |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 415 | { |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 416 | return handle->close(handle); |
Lukas Wunner | 2bd79f3 | 2017-01-31 13:21:33 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 419 | static efi_status_t efi_open_volume(efi_loaded_image_t *image, |
Lukas Wunner | c4db9c1 | 2018-07-20 10:47:23 +0900 | [diff] [blame] | 420 | efi_file_handle_t **__fh) |
| 421 | { |
| 422 | efi_file_io_interface_t *io; |
| 423 | efi_file_handle_t *fh; |
| 424 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 425 | efi_status_t status; |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 426 | efi_handle_t handle = image->device_handle; |
Lukas Wunner | c4db9c1 | 2018-07-20 10:47:23 +0900 | [diff] [blame] | 427 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 428 | status = efi_bs_call(handle_protocol, handle, &fs_proto, (void **)&io); |
Lukas Wunner | c4db9c1 | 2018-07-20 10:47:23 +0900 | [diff] [blame] | 429 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 430 | efi_printk("Failed to handle fs_proto\n"); |
Lukas Wunner | c4db9c1 | 2018-07-20 10:47:23 +0900 | [diff] [blame] | 431 | return status; |
| 432 | } |
| 433 | |
Ard Biesheuvel | 14e900c | 2019-12-24 16:10:14 +0100 | [diff] [blame] | 434 | status = io->open_volume(io, &fh); |
Lukas Wunner | c4db9c1 | 2018-07-20 10:47:23 +0900 | [diff] [blame] | 435 | if (status != EFI_SUCCESS) |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 436 | efi_printk("Failed to open volume\n"); |
Lukas Wunner | c4db9c1 | 2018-07-20 10:47:23 +0900 | [diff] [blame] | 437 | else |
| 438 | *__fh = fh; |
| 439 | |
| 440 | return status; |
| 441 | } |
| 442 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 443 | /* |
| 444 | * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi= |
| 445 | * option, e.g. efi=nochunk. |
| 446 | * |
| 447 | * It should be noted that efi= is parsed in two very different |
| 448 | * environments, first in the early boot environment of the EFI boot |
| 449 | * stub, and subsequently during the kernel boot. |
| 450 | */ |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 451 | efi_status_t efi_parse_options(char const *cmdline) |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 452 | { |
| 453 | char *str; |
| 454 | |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 455 | str = strstr(cmdline, "nokaslr"); |
| 456 | if (str == cmdline || (str && str > cmdline && *(str - 1) == ' ')) |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 457 | efi_nokaslr = true; |
Ard Biesheuvel | b3879a4 | 2017-02-06 11:22:46 +0000 | [diff] [blame] | 458 | |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 459 | str = strstr(cmdline, "quiet"); |
| 460 | if (str == cmdline || (str && str > cmdline && *(str - 1) == ' ')) |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 461 | efi_quiet = true; |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 462 | |
Ard Biesheuvel | b3879a4 | 2017-02-06 11:22:46 +0000 | [diff] [blame] | 463 | /* |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 464 | * If no EFI parameters were specified on the cmdline we've got |
| 465 | * nothing to do. |
| 466 | */ |
| 467 | str = strstr(cmdline, "efi="); |
| 468 | if (!str) |
| 469 | return EFI_SUCCESS; |
| 470 | |
| 471 | /* Skip ahead to first argument */ |
| 472 | str += strlen("efi="); |
| 473 | |
| 474 | /* |
| 475 | * Remember, because efi= is also used by the kernel we need to |
| 476 | * skip over arguments we don't understand. |
| 477 | */ |
Ard Biesheuvel | 4c3f14b | 2017-04-04 17:02:45 +0100 | [diff] [blame] | 478 | while (*str && *str != ' ') { |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 479 | if (!strncmp(str, "nochunk", 7)) { |
| 480 | str += strlen("nochunk"); |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 481 | efi_chunk_size = -1UL; |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Ard Biesheuvel | 4e46c2a | 2019-02-02 10:41:16 +0100 | [diff] [blame] | 484 | if (!strncmp(str, "novamap", 7)) { |
| 485 | str += strlen("novamap"); |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 486 | efi_novamap = true; |
Ard Biesheuvel | 4e46c2a | 2019-02-02 10:41:16 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 489 | if (IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) && |
| 490 | !strncmp(str, "nosoftreserve", 7)) { |
| 491 | str += strlen("nosoftreserve"); |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 492 | efi_nosoftreserve = true; |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Matthew Garrett | 4444f85 | 2020-01-03 12:39:50 +0100 | [diff] [blame^] | 495 | if (!strncmp(str, "disable_early_pci_dma", 21)) { |
| 496 | str += strlen("disable_early_pci_dma"); |
| 497 | efi_disable_pci_dma = true; |
| 498 | } |
| 499 | |
| 500 | if (!strncmp(str, "no_disable_early_pci_dma", 24)) { |
| 501 | str += strlen("no_disable_early_pci_dma"); |
| 502 | efi_disable_pci_dma = false; |
| 503 | } |
| 504 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 505 | /* Group words together, delimited by "," */ |
Ard Biesheuvel | 4c3f14b | 2017-04-04 17:02:45 +0100 | [diff] [blame] | 506 | while (*str && *str != ' ' && *str != ',') |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 507 | str++; |
| 508 | |
| 509 | if (*str == ',') |
| 510 | str++; |
| 511 | } |
| 512 | |
| 513 | return EFI_SUCCESS; |
| 514 | } |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 515 | |
| 516 | /* |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 517 | * Check the cmdline for a LILO-style file= arguments. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 518 | * |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 519 | * We only support loading a file from the same filesystem as |
| 520 | * the kernel image. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 521 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 522 | efi_status_t handle_cmdline_files(efi_loaded_image_t *image, |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 523 | char *cmd_line, char *option_string, |
| 524 | unsigned long max_addr, |
| 525 | unsigned long *load_addr, |
| 526 | unsigned long *load_size) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 527 | { |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 528 | struct file_info *files; |
| 529 | unsigned long file_addr; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 530 | u64 file_size_total; |
Leif Lindholm | 9403e46 | 2014-04-04 13:25:46 +0100 | [diff] [blame] | 531 | efi_file_handle_t *fh = NULL; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 532 | efi_status_t status; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 533 | int nr_files; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 534 | char *str; |
| 535 | int i, j, k; |
| 536 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 537 | file_addr = 0; |
| 538 | file_size_total = 0; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 539 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 540 | str = cmd_line; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 541 | |
| 542 | j = 0; /* See close_handles */ |
| 543 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 544 | if (!load_addr || !load_size) |
| 545 | return EFI_INVALID_PARAMETER; |
| 546 | |
| 547 | *load_addr = 0; |
| 548 | *load_size = 0; |
| 549 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 550 | if (!str || !*str) |
| 551 | return EFI_SUCCESS; |
| 552 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 553 | for (nr_files = 0; *str; nr_files++) { |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 554 | str = strstr(str, option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 555 | if (!str) |
| 556 | break; |
| 557 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 558 | str += strlen(option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 559 | |
| 560 | /* Skip any leading slashes */ |
| 561 | while (*str == '/' || *str == '\\') |
| 562 | str++; |
| 563 | |
| 564 | while (*str && *str != ' ' && *str != '\n') |
| 565 | str++; |
| 566 | } |
| 567 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 568 | if (!nr_files) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 569 | return EFI_SUCCESS; |
| 570 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 571 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, |
| 572 | nr_files * sizeof(*files), (void **)&files); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 573 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 574 | pr_efi_err("Failed to alloc mem for file handle list\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 575 | goto fail; |
| 576 | } |
| 577 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 578 | str = cmd_line; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 579 | for (i = 0; i < nr_files; i++) { |
| 580 | struct file_info *file; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 581 | efi_char16_t filename_16[256]; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 582 | efi_char16_t *p; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 583 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 584 | str = strstr(str, option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 585 | if (!str) |
| 586 | break; |
| 587 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 588 | str += strlen(option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 589 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 590 | file = &files[i]; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 591 | p = filename_16; |
| 592 | |
| 593 | /* Skip any leading slashes */ |
| 594 | while (*str == '/' || *str == '\\') |
| 595 | str++; |
| 596 | |
| 597 | while (*str && *str != ' ' && *str != '\n') { |
| 598 | if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16)) |
| 599 | break; |
| 600 | |
| 601 | if (*str == '/') { |
| 602 | *p++ = '\\'; |
Roy Franz | 4e28308 | 2013-09-22 15:45:42 -0700 | [diff] [blame] | 603 | str++; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 604 | } else { |
| 605 | *p++ = *str++; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | *p = '\0'; |
| 610 | |
| 611 | /* Only open the volume once. */ |
| 612 | if (!i) { |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 613 | status = efi_open_volume(image, &fh); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 614 | if (status != EFI_SUCCESS) |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 615 | goto free_files; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 618 | status = efi_file_size(fh, filename_16, (void **)&file->handle, |
| 619 | &file->size); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 620 | if (status != EFI_SUCCESS) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 621 | goto close_handles; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 622 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 623 | file_size_total += file->size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 626 | if (file_size_total) { |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 627 | unsigned long addr; |
| 628 | |
| 629 | /* |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 630 | * Multiple files need to be at consecutive addresses in memory, |
| 631 | * so allocate enough memory for all the files. This is used |
| 632 | * for loading multiple files. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 633 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 634 | status = efi_high_alloc(file_size_total, 0x1000, &file_addr, |
| 635 | max_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 636 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 637 | pr_efi_err("Failed to alloc highmem for files\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 638 | goto close_handles; |
| 639 | } |
| 640 | |
| 641 | /* We've run out of free low memory. */ |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 642 | if (file_addr > max_addr) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 643 | pr_efi_err("We've run out of free low memory\n"); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 644 | status = EFI_INVALID_PARAMETER; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 645 | goto free_file_total; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 648 | addr = file_addr; |
| 649 | for (j = 0; j < nr_files; j++) { |
Roy Franz | 6a5fe77 | 2013-09-22 15:45:41 -0700 | [diff] [blame] | 650 | unsigned long size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 651 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 652 | size = files[j].size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 653 | while (size) { |
Roy Franz | 6a5fe77 | 2013-09-22 15:45:41 -0700 | [diff] [blame] | 654 | unsigned long chunksize; |
Ard Biesheuvel | b3879a4 | 2017-02-06 11:22:46 +0000 | [diff] [blame] | 655 | |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 656 | if (IS_ENABLED(CONFIG_X86) && size > efi_chunk_size) |
| 657 | chunksize = efi_chunk_size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 658 | else |
| 659 | chunksize = size; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 660 | |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 661 | status = efi_file_read(files[j].handle, |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 662 | &chunksize, |
| 663 | (void *)addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 664 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 665 | pr_efi_err("Failed to read file\n"); |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 666 | goto free_file_total; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 667 | } |
| 668 | addr += chunksize; |
| 669 | size -= chunksize; |
| 670 | } |
| 671 | |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 672 | efi_file_close(files[j].handle); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | } |
| 676 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 677 | efi_bs_call(free_pool, files); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 678 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 679 | *load_addr = file_addr; |
| 680 | *load_size = file_size_total; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 681 | |
| 682 | return status; |
| 683 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 684 | free_file_total: |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 685 | efi_free(file_size_total, file_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 686 | |
| 687 | close_handles: |
| 688 | for (k = j; k < i; k++) |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 689 | efi_file_close(files[k].handle); |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 690 | free_files: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 691 | efi_bs_call(free_pool, files); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 692 | fail: |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 693 | *load_addr = 0; |
| 694 | *load_size = 0; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 695 | |
| 696 | return status; |
| 697 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 698 | /* |
| 699 | * Relocate a kernel image, either compressed or uncompressed. |
| 700 | * In the ARM64 case, all kernel images are currently |
| 701 | * uncompressed, and as such when we relocate it we need to |
| 702 | * allocate additional space for the BSS segment. Any low |
| 703 | * memory that this function should avoid needs to be |
| 704 | * unavailable in the EFI memory map, as if the preferred |
| 705 | * address is not available the lowest available address will |
| 706 | * be used. |
| 707 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 708 | efi_status_t efi_relocate_kernel(unsigned long *image_addr, |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 709 | unsigned long image_size, |
| 710 | unsigned long alloc_size, |
| 711 | unsigned long preferred_addr, |
Kairui Song | 220dd76 | 2019-10-29 18:37:54 +0100 | [diff] [blame] | 712 | unsigned long alignment, |
| 713 | unsigned long min_addr) |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 714 | { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 715 | unsigned long cur_image_addr; |
| 716 | unsigned long new_addr = 0; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 717 | efi_status_t status; |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 718 | unsigned long nr_pages; |
| 719 | efi_physical_addr_t efi_addr = preferred_addr; |
| 720 | |
| 721 | if (!image_addr || !image_size || !alloc_size) |
| 722 | return EFI_INVALID_PARAMETER; |
| 723 | if (alloc_size < image_size) |
| 724 | return EFI_INVALID_PARAMETER; |
| 725 | |
| 726 | cur_image_addr = *image_addr; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 727 | |
| 728 | /* |
| 729 | * The EFI firmware loader could have placed the kernel image |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 730 | * anywhere in memory, but the kernel has restrictions on the |
| 731 | * max physical address it can run at. Some architectures |
| 732 | * also have a prefered address, so first try to relocate |
| 733 | * to the preferred address. If that fails, allocate as low |
| 734 | * as possible while respecting the required alignment. |
| 735 | */ |
Ard Biesheuvel | cf2b0f1 | 2014-11-17 13:46:44 +0100 | [diff] [blame] | 736 | nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 737 | status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, |
| 738 | EFI_LOADER_DATA, nr_pages, &efi_addr); |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 739 | new_addr = efi_addr; |
| 740 | /* |
| 741 | * If preferred address allocation failed allocate as low as |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 742 | * possible. |
| 743 | */ |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 744 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 745 | status = efi_low_alloc_above(alloc_size, alignment, &new_addr, |
| 746 | min_addr); |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 747 | } |
| 748 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 749 | pr_efi_err("Failed to allocate usable memory for kernel.\n"); |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 750 | return status; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 753 | /* |
| 754 | * We know source/dest won't overlap since both memory ranges |
| 755 | * have been allocated by UEFI, so we can safely use memcpy. |
| 756 | */ |
| 757 | memcpy((void *)new_addr, (void *)cur_image_addr, image_size); |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 758 | |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 759 | /* Return the new address of the relocated image. */ |
| 760 | *image_addr = new_addr; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 761 | |
| 762 | return status; |
| 763 | } |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 764 | |
| 765 | /* |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 766 | * Get the number of UTF-8 bytes corresponding to an UTF-16 character. |
| 767 | * This overestimates for surrogates, but that is okay. |
| 768 | */ |
| 769 | static int efi_utf8_bytes(u16 c) |
| 770 | { |
| 771 | return 1 + (c >= 0x80) + (c >= 0x800); |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. |
| 776 | */ |
| 777 | static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) |
| 778 | { |
| 779 | unsigned int c; |
| 780 | |
| 781 | while (n--) { |
| 782 | c = *src++; |
| 783 | if (n && c >= 0xd800 && c <= 0xdbff && |
| 784 | *src >= 0xdc00 && *src <= 0xdfff) { |
| 785 | c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); |
| 786 | src++; |
| 787 | n--; |
| 788 | } |
| 789 | if (c >= 0xd800 && c <= 0xdfff) |
| 790 | c = 0xfffd; /* Unmatched surrogate */ |
| 791 | if (c < 0x80) { |
| 792 | *dst++ = c; |
| 793 | continue; |
| 794 | } |
| 795 | if (c < 0x800) { |
| 796 | *dst++ = 0xc0 + (c >> 6); |
| 797 | goto t1; |
| 798 | } |
| 799 | if (c < 0x10000) { |
| 800 | *dst++ = 0xe0 + (c >> 12); |
| 801 | goto t2; |
| 802 | } |
| 803 | *dst++ = 0xf0 + (c >> 18); |
| 804 | *dst++ = 0x80 + ((c >> 12) & 0x3f); |
| 805 | t2: |
| 806 | *dst++ = 0x80 + ((c >> 6) & 0x3f); |
| 807 | t1: |
| 808 | *dst++ = 0x80 + (c & 0x3f); |
| 809 | } |
| 810 | |
| 811 | return dst; |
| 812 | } |
| 813 | |
Ard Biesheuvel | 48fcb2d | 2016-01-11 11:47:49 +0100 | [diff] [blame] | 814 | #ifndef MAX_CMDLINE_ADDRESS |
| 815 | #define MAX_CMDLINE_ADDRESS ULONG_MAX |
| 816 | #endif |
| 817 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 818 | /* |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 819 | * Convert the unicode UEFI command line to ASCII to pass to kernel. |
| 820 | * Size of memory allocated return in *cmd_line_len. |
| 821 | * Returns NULL on error. |
| 822 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 823 | char *efi_convert_cmdline(efi_loaded_image_t *image, |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 824 | int *cmd_line_len) |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 825 | { |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 826 | const u16 *s2; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 827 | u8 *s1 = NULL; |
| 828 | unsigned long cmdline_addr = 0; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 829 | int load_options_chars = image->load_options_size / 2; /* UTF-16 */ |
| 830 | const u16 *options = image->load_options; |
| 831 | int options_bytes = 0; /* UTF-8 bytes */ |
| 832 | int options_chars = 0; /* UTF-16 chars */ |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 833 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 834 | u16 zero = 0; |
| 835 | |
| 836 | if (options) { |
| 837 | s2 = options; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 838 | while (*s2 && *s2 != '\n' |
| 839 | && options_chars < load_options_chars) { |
| 840 | options_bytes += efi_utf8_bytes(*s2++); |
| 841 | options_chars++; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 845 | if (!options_chars) { |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 846 | /* No command line options, so return empty string*/ |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 847 | options = &zero; |
| 848 | } |
| 849 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 850 | options_bytes++; /* NUL termination */ |
Leif Lindholm | 9403e46 | 2014-04-04 13:25:46 +0100 | [diff] [blame] | 851 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 852 | status = efi_high_alloc(options_bytes, 0, &cmdline_addr, |
| 853 | MAX_CMDLINE_ADDRESS); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 854 | if (status != EFI_SUCCESS) |
| 855 | return NULL; |
| 856 | |
| 857 | s1 = (u8 *)cmdline_addr; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 858 | s2 = (const u16 *)options; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 859 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 860 | s1 = efi_utf16_to_utf8(s1, s2, options_chars); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 861 | *s1 = '\0'; |
| 862 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 863 | *cmd_line_len = options_bytes; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 864 | return (char *)cmdline_addr; |
| 865 | } |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | * Handle calling ExitBootServices according to the requirements set out by the |
| 869 | * spec. Obtains the current memory map, and returns that info after calling |
| 870 | * ExitBootServices. The client must specify a function to perform any |
| 871 | * processing of the memory map data prior to ExitBootServices. A client |
| 872 | * specific structure may be passed to the function via priv. The client |
| 873 | * function may be called multiple times. |
| 874 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 875 | efi_status_t efi_exit_boot_services(void *handle, |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 876 | struct efi_boot_memmap *map, |
| 877 | void *priv, |
| 878 | efi_exit_boot_map_processing priv_func) |
| 879 | { |
| 880 | efi_status_t status; |
| 881 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 882 | status = efi_get_memory_map(map); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 883 | |
| 884 | if (status != EFI_SUCCESS) |
| 885 | goto fail; |
| 886 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 887 | status = priv_func(map, priv); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 888 | if (status != EFI_SUCCESS) |
| 889 | goto free_map; |
| 890 | |
Matthew Garrett | 4444f85 | 2020-01-03 12:39:50 +0100 | [diff] [blame^] | 891 | if (efi_disable_pci_dma) |
| 892 | efi_pci_disable_bridge_busmaster(); |
| 893 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 894 | status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 895 | |
| 896 | if (status == EFI_INVALID_PARAMETER) { |
| 897 | /* |
| 898 | * The memory map changed between efi_get_memory_map() and |
| 899 | * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4: |
| 900 | * EFI_BOOT_SERVICES.ExitBootServices we need to get the |
| 901 | * updated map, and try again. The spec implies one retry |
| 902 | * should be sufficent, which is confirmed against the EDK2 |
| 903 | * implementation. Per the spec, we can only invoke |
| 904 | * get_memory_map() and exit_boot_services() - we cannot alloc |
| 905 | * so efi_get_memory_map() cannot be used, and we must reuse |
| 906 | * the buffer. For all practical purposes, the headroom in the |
| 907 | * buffer should account for any changes in the map so the call |
| 908 | * to get_memory_map() is expected to succeed here. |
| 909 | */ |
| 910 | *map->map_size = *map->buff_size; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 911 | status = efi_bs_call(get_memory_map, |
| 912 | map->map_size, |
| 913 | *map->map, |
| 914 | map->key_ptr, |
| 915 | map->desc_size, |
| 916 | map->desc_ver); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 917 | |
| 918 | /* exit_boot_services() was called, thus cannot free */ |
| 919 | if (status != EFI_SUCCESS) |
| 920 | goto fail; |
| 921 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 922 | status = priv_func(map, priv); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 923 | /* exit_boot_services() was called, thus cannot free */ |
| 924 | if (status != EFI_SUCCESS) |
| 925 | goto fail; |
| 926 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 927 | status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /* exit_boot_services() was called, thus cannot free */ |
| 931 | if (status != EFI_SUCCESS) |
| 932 | goto fail; |
| 933 | |
| 934 | return EFI_SUCCESS; |
| 935 | |
| 936 | free_map: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 937 | efi_bs_call(free_pool, *map->map); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 938 | fail: |
| 939 | return status; |
| 940 | } |
Matthew Garrett | 82d736a | 2019-06-07 13:51:46 -0700 | [diff] [blame] | 941 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 942 | void *get_efi_config_table(efi_guid_t guid) |
Matthew Garrett | 82d736a | 2019-06-07 13:51:46 -0700 | [diff] [blame] | 943 | { |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 944 | unsigned long tables = efi_table_attr(efi_system_table(), tables); |
| 945 | int nr_tables = efi_table_attr(efi_system_table(), nr_tables); |
Ard Biesheuvel | f958efe | 2019-12-24 16:10:09 +0100 | [diff] [blame] | 946 | int i; |
| 947 | |
| 948 | for (i = 0; i < nr_tables; i++) { |
| 949 | efi_config_table_t *t = (void *)tables; |
| 950 | |
| 951 | if (efi_guidcmp(t->guid, guid) == 0) |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 952 | return efi_table_attr(t, table); |
Ard Biesheuvel | f958efe | 2019-12-24 16:10:09 +0100 | [diff] [blame] | 953 | |
| 954 | tables += efi_is_native() ? sizeof(efi_config_table_t) |
| 955 | : sizeof(efi_config_table_32_t); |
| 956 | } |
| 957 | return NULL; |
Matthew Garrett | 82d736a | 2019-06-07 13:51:46 -0700 | [diff] [blame] | 958 | } |
Ard Biesheuvel | dc29da1 | 2019-12-24 16:10:16 +0100 | [diff] [blame] | 959 | |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 960 | void efi_char16_printk(efi_char16_t *str) |
Ard Biesheuvel | dc29da1 | 2019-12-24 16:10:16 +0100 | [diff] [blame] | 961 | { |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 962 | efi_call_proto(efi_table_attr(efi_system_table(), con_out), |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 963 | output_string, str); |
Ard Biesheuvel | dc29da1 | 2019-12-24 16:10:16 +0100 | [diff] [blame] | 964 | } |