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