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