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 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 18 | #define EFI_READ_CHUNK_SIZE (1024 * 1024) |
| 19 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 20 | struct file_info { |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 21 | efi_file_handle_t *handle; |
| 22 | u64 size; |
| 23 | }; |
| 24 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 25 | void efi_printk(efi_system_table_t *sys_table_arg, char *str) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 26 | { |
| 27 | char *s8; |
| 28 | |
| 29 | for (s8 = str; *s8; s8++) { |
| 30 | efi_char16_t ch[2] = { 0 }; |
| 31 | |
| 32 | ch[0] = *s8; |
| 33 | if (*s8 == '\n') { |
| 34 | efi_char16_t nl[2] = { '\r', 0 }; |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 35 | efi_char16_printk(sys_table_arg, nl); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 38 | efi_char16_printk(sys_table_arg, ch); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 42 | efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg, |
| 43 | efi_memory_desc_t **map, |
| 44 | unsigned long *map_size, |
| 45 | unsigned long *desc_size, |
| 46 | u32 *desc_ver, |
| 47 | unsigned long *key_ptr) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 48 | { |
| 49 | efi_memory_desc_t *m = NULL; |
| 50 | efi_status_t status; |
| 51 | unsigned long key; |
| 52 | u32 desc_version; |
| 53 | |
| 54 | *map_size = sizeof(*m) * 32; |
| 55 | again: |
| 56 | /* |
| 57 | * Add an additional efi_memory_desc_t because we're doing an |
| 58 | * allocation which may be in a new descriptor region. |
| 59 | */ |
| 60 | *map_size += sizeof(*m); |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 61 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 62 | *map_size, (void **)&m); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 63 | if (status != EFI_SUCCESS) |
| 64 | goto fail; |
| 65 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 66 | *desc_size = 0; |
| 67 | key = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 68 | status = efi_call_early(get_memory_map, map_size, m, |
| 69 | &key, desc_size, &desc_version); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 70 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 71 | efi_call_early(free_pool, m); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 72 | goto again; |
| 73 | } |
| 74 | |
| 75 | if (status != EFI_SUCCESS) |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 76 | efi_call_early(free_pool, m); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 77 | |
Roy Franz | 1c089c6 | 2013-09-22 15:45:36 -0700 | [diff] [blame] | 78 | if (key_ptr && status == EFI_SUCCESS) |
| 79 | *key_ptr = key; |
| 80 | if (desc_ver && status == EFI_SUCCESS) |
| 81 | *desc_ver = desc_version; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 82 | |
| 83 | fail: |
| 84 | *map = m; |
| 85 | return status; |
| 86 | } |
| 87 | |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 88 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 89 | unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg) |
Roy Franz | 9bb4019 | 2014-01-28 10:41:28 -0800 | [diff] [blame] | 90 | { |
| 91 | efi_status_t status; |
| 92 | unsigned long map_size; |
| 93 | unsigned long membase = EFI_ERROR; |
| 94 | struct efi_memory_map map; |
| 95 | efi_memory_desc_t *md; |
| 96 | |
| 97 | status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map, |
| 98 | &map_size, &map.desc_size, NULL, NULL); |
| 99 | if (status != EFI_SUCCESS) |
| 100 | return membase; |
| 101 | |
| 102 | map.map_end = map.map + map_size; |
| 103 | |
| 104 | for_each_efi_memory_desc(&map, md) |
| 105 | if (md->attribute & EFI_MEMORY_WB) |
| 106 | if (membase > md->phys_addr) |
| 107 | membase = md->phys_addr; |
| 108 | |
| 109 | efi_call_early(free_pool, map.map); |
| 110 | |
| 111 | return membase; |
| 112 | } |
| 113 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Allocate at the highest possible address that is not above 'max'. |
| 116 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 117 | efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg, |
| 118 | unsigned long size, unsigned long align, |
| 119 | unsigned long *addr, unsigned long max) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 120 | { |
| 121 | unsigned long map_size, desc_size; |
| 122 | efi_memory_desc_t *map; |
| 123 | efi_status_t status; |
| 124 | unsigned long nr_pages; |
| 125 | u64 max_addr = 0; |
| 126 | int i; |
| 127 | |
Roy Franz | 1c089c6 | 2013-09-22 15:45:36 -0700 | [diff] [blame] | 128 | status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size, |
| 129 | NULL, NULL); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 130 | if (status != EFI_SUCCESS) |
| 131 | goto fail; |
| 132 | |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 133 | /* |
| 134 | * Enforce minimum alignment that EFI requires when requesting |
| 135 | * a specific address. We are doing page-based allocations, |
| 136 | * so we must be aligned to a page. |
| 137 | */ |
| 138 | if (align < EFI_PAGE_SIZE) |
| 139 | align = EFI_PAGE_SIZE; |
| 140 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 141 | nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
| 142 | again: |
| 143 | for (i = 0; i < map_size / desc_size; i++) { |
| 144 | efi_memory_desc_t *desc; |
| 145 | unsigned long m = (unsigned long)map; |
| 146 | u64 start, end; |
| 147 | |
| 148 | desc = (efi_memory_desc_t *)(m + (i * desc_size)); |
| 149 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 150 | continue; |
| 151 | |
| 152 | if (desc->num_pages < nr_pages) |
| 153 | continue; |
| 154 | |
| 155 | start = desc->phys_addr; |
| 156 | end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); |
| 157 | |
| 158 | if ((start + size) > end || (start + size) > max) |
| 159 | continue; |
| 160 | |
| 161 | if (end - size > max) |
| 162 | end = max; |
| 163 | |
| 164 | if (round_down(end - size, align) < start) |
| 165 | continue; |
| 166 | |
| 167 | start = round_down(end - size, align); |
| 168 | |
| 169 | /* |
| 170 | * Don't allocate at 0x0. It will confuse code that |
| 171 | * checks pointers against NULL. |
| 172 | */ |
| 173 | if (start == 0x0) |
| 174 | continue; |
| 175 | |
| 176 | if (start > max_addr) |
| 177 | max_addr = start; |
| 178 | } |
| 179 | |
| 180 | if (!max_addr) |
| 181 | status = EFI_NOT_FOUND; |
| 182 | else { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 183 | status = efi_call_early(allocate_pages, |
| 184 | EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, |
| 185 | nr_pages, &max_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 186 | if (status != EFI_SUCCESS) { |
| 187 | max = max_addr; |
| 188 | max_addr = 0; |
| 189 | goto again; |
| 190 | } |
| 191 | |
| 192 | *addr = max_addr; |
| 193 | } |
| 194 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 195 | efi_call_early(free_pool, map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 196 | fail: |
| 197 | return status; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Allocate at the lowest possible address. |
| 202 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 203 | efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg, |
| 204 | unsigned long size, unsigned long align, |
| 205 | unsigned long *addr) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 206 | { |
| 207 | unsigned long map_size, desc_size; |
| 208 | efi_memory_desc_t *map; |
| 209 | efi_status_t status; |
| 210 | unsigned long nr_pages; |
| 211 | int i; |
| 212 | |
Roy Franz | 1c089c6 | 2013-09-22 15:45:36 -0700 | [diff] [blame] | 213 | status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size, |
| 214 | NULL, NULL); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 215 | if (status != EFI_SUCCESS) |
| 216 | goto fail; |
| 217 | |
Roy Franz | 38dd9c0 | 2013-09-22 15:45:30 -0700 | [diff] [blame] | 218 | /* |
| 219 | * Enforce minimum alignment that EFI requires when requesting |
| 220 | * a specific address. We are doing page-based allocations, |
| 221 | * so we must be aligned to a page. |
| 222 | */ |
| 223 | if (align < EFI_PAGE_SIZE) |
| 224 | align = EFI_PAGE_SIZE; |
| 225 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 226 | nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
| 227 | for (i = 0; i < map_size / desc_size; i++) { |
| 228 | efi_memory_desc_t *desc; |
| 229 | unsigned long m = (unsigned long)map; |
| 230 | u64 start, end; |
| 231 | |
| 232 | desc = (efi_memory_desc_t *)(m + (i * desc_size)); |
| 233 | |
| 234 | if (desc->type != EFI_CONVENTIONAL_MEMORY) |
| 235 | continue; |
| 236 | |
| 237 | if (desc->num_pages < nr_pages) |
| 238 | continue; |
| 239 | |
| 240 | start = desc->phys_addr; |
| 241 | end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); |
| 242 | |
| 243 | /* |
| 244 | * Don't allocate at 0x0. It will confuse code that |
| 245 | * checks pointers against NULL. Skip the first 8 |
| 246 | * bytes so we start at a nice even number. |
| 247 | */ |
| 248 | if (start == 0x0) |
| 249 | start += 8; |
| 250 | |
| 251 | start = round_up(start, align); |
| 252 | if ((start + size) > end) |
| 253 | continue; |
| 254 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 255 | status = efi_call_early(allocate_pages, |
| 256 | EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, |
| 257 | nr_pages, &start); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 258 | if (status == EFI_SUCCESS) { |
| 259 | *addr = start; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (i == map_size / desc_size) |
| 265 | status = EFI_NOT_FOUND; |
| 266 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 267 | efi_call_early(free_pool, map); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 268 | fail: |
| 269 | return status; |
| 270 | } |
| 271 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 272 | void efi_free(efi_system_table_t *sys_table_arg, unsigned long size, |
| 273 | unsigned long addr) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 274 | { |
| 275 | unsigned long nr_pages; |
| 276 | |
Roy Franz | 0e1cadb | 2013-09-22 15:45:38 -0700 | [diff] [blame] | 277 | if (!size) |
| 278 | return; |
| 279 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 280 | nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 281 | efi_call_early(free_pages, addr, nr_pages); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
| 285 | /* |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 286 | * Check the cmdline for a LILO-style file= arguments. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 287 | * |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 288 | * We only support loading a file from the same filesystem as |
| 289 | * the kernel image. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 290 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 291 | efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, |
| 292 | efi_loaded_image_t *image, |
| 293 | char *cmd_line, char *option_string, |
| 294 | unsigned long max_addr, |
| 295 | unsigned long *load_addr, |
| 296 | unsigned long *load_size) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 297 | { |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 298 | struct file_info *files; |
| 299 | unsigned long file_addr; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 300 | u64 file_size_total; |
Leif Lindholm | 9403e46 | 2014-04-04 13:25:46 +0100 | [diff] [blame] | 301 | efi_file_handle_t *fh = NULL; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 302 | efi_status_t status; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 303 | int nr_files; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 304 | char *str; |
| 305 | int i, j, k; |
| 306 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 307 | file_addr = 0; |
| 308 | file_size_total = 0; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 309 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 310 | str = cmd_line; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 311 | |
| 312 | j = 0; /* See close_handles */ |
| 313 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 314 | if (!load_addr || !load_size) |
| 315 | return EFI_INVALID_PARAMETER; |
| 316 | |
| 317 | *load_addr = 0; |
| 318 | *load_size = 0; |
| 319 | |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 320 | if (!str || !*str) |
| 321 | return EFI_SUCCESS; |
| 322 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 323 | for (nr_files = 0; *str; nr_files++) { |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 324 | str = strstr(str, option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 325 | if (!str) |
| 326 | break; |
| 327 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 328 | str += strlen(option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 329 | |
| 330 | /* Skip any leading slashes */ |
| 331 | while (*str == '/' || *str == '\\') |
| 332 | str++; |
| 333 | |
| 334 | while (*str && *str != ' ' && *str != '\n') |
| 335 | str++; |
| 336 | } |
| 337 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 338 | if (!nr_files) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 339 | return EFI_SUCCESS; |
| 340 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 341 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 342 | nr_files * sizeof(*files), (void **)&files); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 343 | if (status != EFI_SUCCESS) { |
Roy Franz | f966ea0 | 2013-12-13 11:04:49 -0800 | [diff] [blame] | 344 | 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] | 345 | goto fail; |
| 346 | } |
| 347 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 348 | str = cmd_line; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 349 | for (i = 0; i < nr_files; i++) { |
| 350 | struct file_info *file; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 351 | efi_char16_t filename_16[256]; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 352 | efi_char16_t *p; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 353 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 354 | str = strstr(str, option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 355 | if (!str) |
| 356 | break; |
| 357 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 358 | str += strlen(option_string); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 359 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 360 | file = &files[i]; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 361 | p = filename_16; |
| 362 | |
| 363 | /* Skip any leading slashes */ |
| 364 | while (*str == '/' || *str == '\\') |
| 365 | str++; |
| 366 | |
| 367 | while (*str && *str != ' ' && *str != '\n') { |
| 368 | if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16)) |
| 369 | break; |
| 370 | |
| 371 | if (*str == '/') { |
| 372 | *p++ = '\\'; |
Roy Franz | 4e28308 | 2013-09-22 15:45:42 -0700 | [diff] [blame] | 373 | str++; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 374 | } else { |
| 375 | *p++ = *str++; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | *p = '\0'; |
| 380 | |
| 381 | /* Only open the volume once. */ |
| 382 | if (!i) { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 383 | status = efi_open_volume(sys_table_arg, image, |
| 384 | (void **)&fh); |
| 385 | if (status != EFI_SUCCESS) |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 386 | goto free_files; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 389 | status = efi_file_size(sys_table_arg, fh, filename_16, |
| 390 | (void **)&file->handle, &file->size); |
| 391 | if (status != EFI_SUCCESS) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 392 | goto close_handles; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 393 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 394 | file_size_total += file->size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 397 | if (file_size_total) { |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 398 | unsigned long addr; |
| 399 | |
| 400 | /* |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 401 | * Multiple files need to be at consecutive addresses in memory, |
| 402 | * so allocate enough memory for all the files. This is used |
| 403 | * for loading multiple files. |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 404 | */ |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 405 | status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000, |
| 406 | &file_addr, max_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 407 | if (status != EFI_SUCCESS) { |
Roy Franz | f966ea0 | 2013-12-13 11:04:49 -0800 | [diff] [blame] | 408 | 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] | 409 | goto close_handles; |
| 410 | } |
| 411 | |
| 412 | /* We've run out of free low memory. */ |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 413 | if (file_addr > max_addr) { |
Roy Franz | f966ea0 | 2013-12-13 11:04:49 -0800 | [diff] [blame] | 414 | 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] | 415 | status = EFI_INVALID_PARAMETER; |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 416 | goto free_file_total; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 419 | addr = file_addr; |
| 420 | for (j = 0; j < nr_files; j++) { |
Roy Franz | 6a5fe77 | 2013-09-22 15:45:41 -0700 | [diff] [blame] | 421 | unsigned long size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 422 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 423 | size = files[j].size; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 424 | while (size) { |
Roy Franz | 6a5fe77 | 2013-09-22 15:45:41 -0700 | [diff] [blame] | 425 | unsigned long chunksize; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 426 | if (size > EFI_READ_CHUNK_SIZE) |
| 427 | chunksize = EFI_READ_CHUNK_SIZE; |
| 428 | else |
| 429 | chunksize = size; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 430 | |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 431 | status = efi_file_read(files[j].handle, |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 432 | &chunksize, |
| 433 | (void *)addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 434 | if (status != EFI_SUCCESS) { |
Roy Franz | f966ea0 | 2013-12-13 11:04:49 -0800 | [diff] [blame] | 435 | pr_efi_err(sys_table_arg, "Failed to read file\n"); |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 436 | goto free_file_total; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 437 | } |
| 438 | addr += chunksize; |
| 439 | size -= chunksize; |
| 440 | } |
| 441 | |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 442 | efi_file_close(files[j].handle); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | } |
| 446 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 447 | efi_call_early(free_pool, files); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 448 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 449 | *load_addr = file_addr; |
| 450 | *load_size = file_size_total; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 451 | |
| 452 | return status; |
| 453 | |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 454 | free_file_total: |
| 455 | efi_free(sys_table_arg, file_size_total, file_addr); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 456 | |
| 457 | close_handles: |
| 458 | for (k = j; k < i; k++) |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 459 | efi_file_close(files[k].handle); |
Roy Franz | 36f8961 | 2013-09-22 15:45:40 -0700 | [diff] [blame] | 460 | free_files: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 461 | efi_call_early(free_pool, files); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 462 | fail: |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 463 | *load_addr = 0; |
| 464 | *load_size = 0; |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 465 | |
| 466 | return status; |
| 467 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 468 | /* |
| 469 | * Relocate a kernel image, either compressed or uncompressed. |
| 470 | * In the ARM64 case, all kernel images are currently |
| 471 | * uncompressed, and as such when we relocate it we need to |
| 472 | * allocate additional space for the BSS segment. Any low |
| 473 | * memory that this function should avoid needs to be |
| 474 | * unavailable in the EFI memory map, as if the preferred |
| 475 | * address is not available the lowest available address will |
| 476 | * be used. |
| 477 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 478 | efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg, |
| 479 | unsigned long *image_addr, |
| 480 | unsigned long image_size, |
| 481 | unsigned long alloc_size, |
| 482 | unsigned long preferred_addr, |
| 483 | unsigned long alignment) |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 484 | { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 485 | unsigned long cur_image_addr; |
| 486 | unsigned long new_addr = 0; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 487 | efi_status_t status; |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 488 | unsigned long nr_pages; |
| 489 | efi_physical_addr_t efi_addr = preferred_addr; |
| 490 | |
| 491 | if (!image_addr || !image_size || !alloc_size) |
| 492 | return EFI_INVALID_PARAMETER; |
| 493 | if (alloc_size < image_size) |
| 494 | return EFI_INVALID_PARAMETER; |
| 495 | |
| 496 | cur_image_addr = *image_addr; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | * The EFI firmware loader could have placed the kernel image |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 500 | * anywhere in memory, but the kernel has restrictions on the |
| 501 | * max physical address it can run at. Some architectures |
| 502 | * also have a prefered address, so first try to relocate |
| 503 | * to the preferred address. If that fails, allocate as low |
| 504 | * as possible while respecting the required alignment. |
| 505 | */ |
| 506 | nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 507 | status = efi_call_early(allocate_pages, |
| 508 | EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, |
| 509 | nr_pages, &efi_addr); |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 510 | new_addr = efi_addr; |
| 511 | /* |
| 512 | * If preferred address allocation failed allocate as low as |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 513 | * possible. |
| 514 | */ |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 515 | if (status != EFI_SUCCESS) { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 516 | status = efi_low_alloc(sys_table_arg, alloc_size, alignment, |
| 517 | &new_addr); |
| 518 | } |
| 519 | if (status != EFI_SUCCESS) { |
Roy Franz | f966ea0 | 2013-12-13 11:04:49 -0800 | [diff] [blame] | 520 | 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] | 521 | return status; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 524 | /* |
| 525 | * We know source/dest won't overlap since both memory ranges |
| 526 | * have been allocated by UEFI, so we can safely use memcpy. |
| 527 | */ |
| 528 | memcpy((void *)new_addr, (void *)cur_image_addr, image_size); |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 529 | |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 530 | /* Return the new address of the relocated image. */ |
| 531 | *image_addr = new_addr; |
Roy Franz | c6866d7 | 2013-09-22 15:45:31 -0700 | [diff] [blame] | 532 | |
| 533 | return status; |
| 534 | } |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 535 | |
| 536 | /* |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 537 | * Get the number of UTF-8 bytes corresponding to an UTF-16 character. |
| 538 | * This overestimates for surrogates, but that is okay. |
| 539 | */ |
| 540 | static int efi_utf8_bytes(u16 c) |
| 541 | { |
| 542 | return 1 + (c >= 0x80) + (c >= 0x800); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. |
| 547 | */ |
| 548 | static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) |
| 549 | { |
| 550 | unsigned int c; |
| 551 | |
| 552 | while (n--) { |
| 553 | c = *src++; |
| 554 | if (n && c >= 0xd800 && c <= 0xdbff && |
| 555 | *src >= 0xdc00 && *src <= 0xdfff) { |
| 556 | c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); |
| 557 | src++; |
| 558 | n--; |
| 559 | } |
| 560 | if (c >= 0xd800 && c <= 0xdfff) |
| 561 | c = 0xfffd; /* Unmatched surrogate */ |
| 562 | if (c < 0x80) { |
| 563 | *dst++ = c; |
| 564 | continue; |
| 565 | } |
| 566 | if (c < 0x800) { |
| 567 | *dst++ = 0xc0 + (c >> 6); |
| 568 | goto t1; |
| 569 | } |
| 570 | if (c < 0x10000) { |
| 571 | *dst++ = 0xe0 + (c >> 12); |
| 572 | goto t2; |
| 573 | } |
| 574 | *dst++ = 0xf0 + (c >> 18); |
| 575 | *dst++ = 0x80 + ((c >> 12) & 0x3f); |
| 576 | t2: |
| 577 | *dst++ = 0x80 + ((c >> 6) & 0x3f); |
| 578 | t1: |
| 579 | *dst++ = 0x80 + (c & 0x3f); |
| 580 | } |
| 581 | |
| 582 | return dst; |
| 583 | } |
| 584 | |
| 585 | /* |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 586 | * Convert the unicode UEFI command line to ASCII to pass to kernel. |
| 587 | * Size of memory allocated return in *cmd_line_len. |
| 588 | * Returns NULL on error. |
| 589 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame^] | 590 | char *efi_convert_cmdline(efi_system_table_t *sys_table_arg, |
| 591 | efi_loaded_image_t *image, |
| 592 | int *cmd_line_len) |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 593 | { |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 594 | const u16 *s2; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 595 | u8 *s1 = NULL; |
| 596 | unsigned long cmdline_addr = 0; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 597 | int load_options_chars = image->load_options_size / 2; /* UTF-16 */ |
| 598 | const u16 *options = image->load_options; |
| 599 | int options_bytes = 0; /* UTF-8 bytes */ |
| 600 | int options_chars = 0; /* UTF-16 chars */ |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 601 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 602 | u16 zero = 0; |
| 603 | |
| 604 | if (options) { |
| 605 | s2 = options; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 606 | while (*s2 && *s2 != '\n' |
| 607 | && options_chars < load_options_chars) { |
| 608 | options_bytes += efi_utf8_bytes(*s2++); |
| 609 | options_chars++; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 613 | if (!options_chars) { |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 614 | /* No command line options, so return empty string*/ |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 615 | options = &zero; |
| 616 | } |
| 617 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 618 | options_bytes++; /* NUL termination */ |
Leif Lindholm | 9403e46 | 2014-04-04 13:25:46 +0100 | [diff] [blame] | 619 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 620 | status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 621 | if (status != EFI_SUCCESS) |
| 622 | return NULL; |
| 623 | |
| 624 | s1 = (u8 *)cmdline_addr; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 625 | s2 = (const u16 *)options; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 626 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 627 | s1 = efi_utf16_to_utf8(s1, s2, options_chars); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 628 | *s1 = '\0'; |
| 629 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 630 | *cmd_line_len = options_bytes; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 631 | return (char *)cmdline_addr; |
| 632 | } |