blob: 757badc1debbedbec5ecaa64c724c85b3b2e7632 [file] [log] [blame]
Roy Franz7721da42013-09-22 15:45:27 -07001/*
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 Biesheuvelbd669472014-07-02 14:54:42 +020012
13#include <linux/efi.h>
14#include <asm/efi.h>
15
16#include "efistub.h"
17
Matt Fleming5a17dae2014-08-05 11:52:11 +010018/*
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 Franz7721da42013-09-22 15:45:27 -070031#define EFI_READ_CHUNK_SIZE (1024 * 1024)
32
Matt Fleming5a17dae2014-08-05 11:52:11 +010033static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
34
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060035#define EFI_MMAP_NR_SLACK_SLOTS 8
36
Roy Franz36f89612013-09-22 15:45:40 -070037struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070038 efi_file_handle_t *handle;
39 u64 size;
40};
41
Ard Biesheuvelbd669472014-07-02 14:54:42 +020042void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070043{
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 Franz876dc362013-09-22 15:45:28 -070052 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070053 }
54
Roy Franz876dc362013-09-22 15:45:28 -070055 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070056 }
57}
58
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060059static 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 Biesheuvelbd669472014-07-02 14:54:42 +020068efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060069 struct efi_boot_memmap *map)
Roy Franz7721da42013-09-22 15:45:27 -070070{
71 efi_memory_desc_t *m = NULL;
72 efi_status_t status;
73 unsigned long key;
74 u32 desc_version;
75
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060076 *map->desc_size = sizeof(*m);
77 *map->map_size = *map->desc_size * 32;
78 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +000079again:
Matt Fleming204b0a12014-03-22 10:09:01 +000080 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060081 *map->map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -070082 if (status != EFI_SUCCESS)
83 goto fail;
84
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060085 *map->desc_size = 0;
Matt Fleming43a9f692015-02-13 15:46:56 +000086 key = 0;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060087 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 Fleming204b0a12014-03-22 10:09:01 +000092 efi_call_early(free_pool, m);
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060093 /*
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 Fleming43a9f692015-02-13 15:46:56 +0000102 goto again;
Roy Franz7721da42013-09-22 15:45:27 -0700103 }
104
105 if (status != EFI_SUCCESS)
Matt Fleming204b0a12014-03-22 10:09:01 +0000106 efi_call_early(free_pool, m);
Matt Fleming54b52d82014-01-10 15:27:14 +0000107
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600108 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 Franz7721da42013-09-22 15:45:27 -0700112
113fail:
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600114 *map->map = m;
Roy Franz7721da42013-09-22 15:45:27 -0700115 return status;
116}
117
Roy Franz9bb40192014-01-28 10:41:28 -0800118
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000119unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
Roy Franz9bb40192014-01-28 10:41:28 -0800120{
121 efi_status_t status;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600122 unsigned long map_size, buff_size;
Roy Franz9bb40192014-01-28 10:41:28 -0800123 unsigned long membase = EFI_ERROR;
124 struct efi_memory_map map;
125 efi_memory_desc_t *md;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600126 struct efi_boot_memmap boot_map;
Roy Franz9bb40192014-01-28 10:41:28 -0800127
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600128 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 Franz9bb40192014-01-28 10:41:28 -0800136 if (status != EFI_SUCCESS)
137 return membase;
138
139 map.map_end = map.map + map_size;
140
Matt Fleming78ce2482016-04-25 21:06:38 +0100141 for_each_efi_memory_desc_in_map(&map, md) {
142 if (md->attribute & EFI_MEMORY_WB) {
Roy Franz9bb40192014-01-28 10:41:28 -0800143 if (membase > md->phys_addr)
144 membase = md->phys_addr;
Matt Fleming78ce2482016-04-25 21:06:38 +0100145 }
146 }
Roy Franz9bb40192014-01-28 10:41:28 -0800147
148 efi_call_early(free_pool, map.map);
149
150 return membase;
151}
152
Roy Franz7721da42013-09-22 15:45:27 -0700153/*
154 * Allocate at the highest possible address that is not above 'max'.
155 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200156efi_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 Franz7721da42013-09-22 15:45:27 -0700159{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600160 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700161 efi_memory_desc_t *map;
162 efi_status_t status;
163 unsigned long nr_pages;
164 u64 max_addr = 0;
165 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600166 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700167
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600168 boot_map.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 Franz7721da42013-09-22 15:45:27 -0700176 if (status != EFI_SUCCESS)
177 goto fail;
178
Roy Franz38dd9c02013-09-22 15:45:30 -0700179 /*
Roy Franz5b88a312016-11-12 21:32:29 +0000180 * 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 Franz38dd9c02013-09-22 15:45:30 -0700184 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100185 if (align < EFI_ALLOC_ALIGN)
186 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700187
Roy Franz5b88a312016-11-12 21:32:29 +0000188 size = round_up(size, EFI_ALLOC_ALIGN);
189 nr_pages = size / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700190again:
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 Franz5b88a312016-11-12 21:32:29 +0000204 end = start + desc->num_pages * EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700205
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800206 if (end > max)
Roy Franz7721da42013-09-22 15:45:27 -0700207 end = max;
208
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800209 if ((start + size) > end)
210 continue;
211
Roy Franz7721da42013-09-22 15:45:27 -0700212 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 Fleming204b0a12014-03-22 10:09:01 +0000231 status = efi_call_early(allocate_pages,
232 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
233 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700234 if (status != EFI_SUCCESS) {
235 max = max_addr;
236 max_addr = 0;
237 goto again;
238 }
239
240 *addr = max_addr;
241 }
242
Matt Fleming204b0a12014-03-22 10:09:01 +0000243 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700244fail:
245 return status;
246}
247
248/*
249 * Allocate at the lowest possible address.
250 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200251efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
252 unsigned long size, unsigned long align,
253 unsigned long *addr)
Roy Franz7721da42013-09-22 15:45:27 -0700254{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600255 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700256 efi_memory_desc_t *map;
257 efi_status_t status;
258 unsigned long nr_pages;
259 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600260 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700261
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600262 boot_map.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 Franz7721da42013-09-22 15:45:27 -0700270 if (status != EFI_SUCCESS)
271 goto fail;
272
Roy Franz38dd9c02013-09-22 15:45:30 -0700273 /*
Roy Franz5b88a312016-11-12 21:32:29 +0000274 * 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 Franz38dd9c02013-09-22 15:45:30 -0700278 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100279 if (align < EFI_ALLOC_ALIGN)
280 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700281
Roy Franz5b88a312016-11-12 21:32:29 +0000282 size = round_up(size, EFI_ALLOC_ALIGN);
283 nr_pages = size / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700284 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 Franz5b88a312016-11-12 21:32:29 +0000298 end = start + desc->num_pages * EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700299
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 Fleming204b0a12014-03-22 10:09:01 +0000312 status = efi_call_early(allocate_pages,
313 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
314 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700315 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 Fleming204b0a12014-03-22 10:09:01 +0000324 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700325fail:
326 return status;
327}
328
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200329void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
330 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700331{
332 unsigned long nr_pages;
333
Roy Franz0e1cadb2013-09-22 15:45:38 -0700334 if (!size)
335 return;
336
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100337 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000338 efi_call_early(free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700339}
340
Matt Fleming5a17dae2014-08-05 11:52:11 +0100341/*
342 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
343 * option, e.g. efi=nochunk.
344 *
345 * It should be noted that efi= is parsed in two very different
346 * environments, first in the early boot environment of the EFI boot
347 * stub, and subsequently during the kernel boot.
348 */
349efi_status_t efi_parse_options(char *cmdline)
350{
351 char *str;
352
353 /*
354 * If no EFI parameters were specified on the cmdline we've got
355 * nothing to do.
356 */
357 str = strstr(cmdline, "efi=");
358 if (!str)
359 return EFI_SUCCESS;
360
361 /* Skip ahead to first argument */
362 str += strlen("efi=");
363
364 /*
365 * Remember, because efi= is also used by the kernel we need to
366 * skip over arguments we don't understand.
367 */
368 while (*str) {
369 if (!strncmp(str, "nochunk", 7)) {
370 str += strlen("nochunk");
371 __chunk_size = -1UL;
372 }
373
374 /* Group words together, delimited by "," */
375 while (*str && *str != ',')
376 str++;
377
378 if (*str == ',')
379 str++;
380 }
381
382 return EFI_SUCCESS;
383}
Roy Franz7721da42013-09-22 15:45:27 -0700384
385/*
Roy Franz36f89612013-09-22 15:45:40 -0700386 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700387 *
Roy Franz36f89612013-09-22 15:45:40 -0700388 * We only support loading a file from the same filesystem as
389 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700390 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200391efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
392 efi_loaded_image_t *image,
393 char *cmd_line, char *option_string,
394 unsigned long max_addr,
395 unsigned long *load_addr,
396 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700397{
Roy Franz36f89612013-09-22 15:45:40 -0700398 struct file_info *files;
399 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700400 u64 file_size_total;
Leif Lindholm9403e462014-04-04 13:25:46 +0100401 efi_file_handle_t *fh = NULL;
Roy Franz7721da42013-09-22 15:45:27 -0700402 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700403 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700404 char *str;
405 int i, j, k;
406
Roy Franz36f89612013-09-22 15:45:40 -0700407 file_addr = 0;
408 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700409
Roy Franz46f45822013-09-22 15:45:39 -0700410 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700411
412 j = 0; /* See close_handles */
413
Roy Franz46f45822013-09-22 15:45:39 -0700414 if (!load_addr || !load_size)
415 return EFI_INVALID_PARAMETER;
416
417 *load_addr = 0;
418 *load_size = 0;
419
Roy Franz7721da42013-09-22 15:45:27 -0700420 if (!str || !*str)
421 return EFI_SUCCESS;
422
Roy Franz36f89612013-09-22 15:45:40 -0700423 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700424 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700425 if (!str)
426 break;
427
Roy Franz46f45822013-09-22 15:45:39 -0700428 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700429
430 /* Skip any leading slashes */
431 while (*str == '/' || *str == '\\')
432 str++;
433
434 while (*str && *str != ' ' && *str != '\n')
435 str++;
436 }
437
Roy Franz36f89612013-09-22 15:45:40 -0700438 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700439 return EFI_SUCCESS;
440
Matt Fleming204b0a12014-03-22 10:09:01 +0000441 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
442 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700443 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800444 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700445 goto fail;
446 }
447
Roy Franz46f45822013-09-22 15:45:39 -0700448 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700449 for (i = 0; i < nr_files; i++) {
450 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700451 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700452 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700453
Roy Franz46f45822013-09-22 15:45:39 -0700454 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700455 if (!str)
456 break;
457
Roy Franz46f45822013-09-22 15:45:39 -0700458 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700459
Roy Franz36f89612013-09-22 15:45:40 -0700460 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700461 p = filename_16;
462
463 /* Skip any leading slashes */
464 while (*str == '/' || *str == '\\')
465 str++;
466
467 while (*str && *str != ' ' && *str != '\n') {
468 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
469 break;
470
471 if (*str == '/') {
472 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700473 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700474 } else {
475 *p++ = *str++;
476 }
477 }
478
479 *p = '\0';
480
481 /* Only open the volume once. */
482 if (!i) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000483 status = efi_open_volume(sys_table_arg, image,
484 (void **)&fh);
485 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700486 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700487 }
488
Matt Fleming54b52d82014-01-10 15:27:14 +0000489 status = efi_file_size(sys_table_arg, fh, filename_16,
490 (void **)&file->handle, &file->size);
491 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700492 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700493
Matt Fleming54b52d82014-01-10 15:27:14 +0000494 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700495 }
496
Roy Franz36f89612013-09-22 15:45:40 -0700497 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700498 unsigned long addr;
499
500 /*
Roy Franz36f89612013-09-22 15:45:40 -0700501 * Multiple files need to be at consecutive addresses in memory,
502 * so allocate enough memory for all the files. This is used
503 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700504 */
Roy Franz36f89612013-09-22 15:45:40 -0700505 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
506 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700507 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800508 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700509 goto close_handles;
510 }
511
512 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700513 if (file_addr > max_addr) {
Roy Franzf966ea02013-12-13 11:04:49 -0800514 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700515 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700516 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700517 }
518
Roy Franz36f89612013-09-22 15:45:40 -0700519 addr = file_addr;
520 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700521 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700522
Roy Franz36f89612013-09-22 15:45:40 -0700523 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700524 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700525 unsigned long chunksize;
Matt Fleming5a17dae2014-08-05 11:52:11 +0100526 if (size > __chunk_size)
527 chunksize = __chunk_size;
Roy Franz7721da42013-09-22 15:45:27 -0700528 else
529 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000530
Matt Fleming47514c92014-04-10 14:11:45 +0100531 status = efi_file_read(files[j].handle,
Matt Fleming54b52d82014-01-10 15:27:14 +0000532 &chunksize,
533 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700534 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800535 pr_efi_err(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700536 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700537 }
538 addr += chunksize;
539 size -= chunksize;
540 }
541
Matt Fleming47514c92014-04-10 14:11:45 +0100542 efi_file_close(files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700543 }
544
545 }
546
Matt Fleming204b0a12014-03-22 10:09:01 +0000547 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700548
Roy Franz36f89612013-09-22 15:45:40 -0700549 *load_addr = file_addr;
550 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700551
552 return status;
553
Roy Franz36f89612013-09-22 15:45:40 -0700554free_file_total:
555 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700556
557close_handles:
558 for (k = j; k < i; k++)
Matt Fleming47514c92014-04-10 14:11:45 +0100559 efi_file_close(files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700560free_files:
Matt Fleming204b0a12014-03-22 10:09:01 +0000561 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700562fail:
Roy Franz46f45822013-09-22 15:45:39 -0700563 *load_addr = 0;
564 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700565
566 return status;
567}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700568/*
569 * Relocate a kernel image, either compressed or uncompressed.
570 * In the ARM64 case, all kernel images are currently
571 * uncompressed, and as such when we relocate it we need to
572 * allocate additional space for the BSS segment. Any low
573 * memory that this function should avoid needs to be
574 * unavailable in the EFI memory map, as if the preferred
575 * address is not available the lowest available address will
576 * be used.
577 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200578efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
579 unsigned long *image_addr,
580 unsigned long image_size,
581 unsigned long alloc_size,
582 unsigned long preferred_addr,
583 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700584{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700585 unsigned long cur_image_addr;
586 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700587 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700588 unsigned long nr_pages;
589 efi_physical_addr_t efi_addr = preferred_addr;
590
591 if (!image_addr || !image_size || !alloc_size)
592 return EFI_INVALID_PARAMETER;
593 if (alloc_size < image_size)
594 return EFI_INVALID_PARAMETER;
595
596 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700597
598 /*
599 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700600 * anywhere in memory, but the kernel has restrictions on the
601 * max physical address it can run at. Some architectures
602 * also have a prefered address, so first try to relocate
603 * to the preferred address. If that fails, allocate as low
604 * as possible while respecting the required alignment.
605 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100606 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000607 status = efi_call_early(allocate_pages,
608 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
609 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700610 new_addr = efi_addr;
611 /*
612 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700613 * possible.
614 */
Roy Franzc6866d72013-09-22 15:45:31 -0700615 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700616 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
617 &new_addr);
618 }
619 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800620 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
Roy Franz4a9f3a72013-09-22 15:45:32 -0700621 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700622 }
623
Roy Franz4a9f3a72013-09-22 15:45:32 -0700624 /*
625 * We know source/dest won't overlap since both memory ranges
626 * have been allocated by UEFI, so we can safely use memcpy.
627 */
628 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700629
Roy Franz4a9f3a72013-09-22 15:45:32 -0700630 /* Return the new address of the relocated image. */
631 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700632
633 return status;
634}
Roy Franz5fef3872013-09-22 15:45:33 -0700635
636/*
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500637 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
638 * This overestimates for surrogates, but that is okay.
639 */
640static int efi_utf8_bytes(u16 c)
641{
642 return 1 + (c >= 0x80) + (c >= 0x800);
643}
644
645/*
646 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
647 */
648static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
649{
650 unsigned int c;
651
652 while (n--) {
653 c = *src++;
654 if (n && c >= 0xd800 && c <= 0xdbff &&
655 *src >= 0xdc00 && *src <= 0xdfff) {
656 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
657 src++;
658 n--;
659 }
660 if (c >= 0xd800 && c <= 0xdfff)
661 c = 0xfffd; /* Unmatched surrogate */
662 if (c < 0x80) {
663 *dst++ = c;
664 continue;
665 }
666 if (c < 0x800) {
667 *dst++ = 0xc0 + (c >> 6);
668 goto t1;
669 }
670 if (c < 0x10000) {
671 *dst++ = 0xe0 + (c >> 12);
672 goto t2;
673 }
674 *dst++ = 0xf0 + (c >> 18);
675 *dst++ = 0x80 + ((c >> 12) & 0x3f);
676 t2:
677 *dst++ = 0x80 + ((c >> 6) & 0x3f);
678 t1:
679 *dst++ = 0x80 + (c & 0x3f);
680 }
681
682 return dst;
683}
684
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100685#ifndef MAX_CMDLINE_ADDRESS
686#define MAX_CMDLINE_ADDRESS ULONG_MAX
687#endif
688
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500689/*
Roy Franz5fef3872013-09-22 15:45:33 -0700690 * Convert the unicode UEFI command line to ASCII to pass to kernel.
691 * Size of memory allocated return in *cmd_line_len.
692 * Returns NULL on error.
693 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200694char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
695 efi_loaded_image_t *image,
696 int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700697{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500698 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700699 u8 *s1 = NULL;
700 unsigned long cmdline_addr = 0;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500701 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
702 const u16 *options = image->load_options;
703 int options_bytes = 0; /* UTF-8 bytes */
704 int options_chars = 0; /* UTF-16 chars */
Roy Franz5fef3872013-09-22 15:45:33 -0700705 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700706 u16 zero = 0;
707
708 if (options) {
709 s2 = options;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500710 while (*s2 && *s2 != '\n'
711 && options_chars < load_options_chars) {
712 options_bytes += efi_utf8_bytes(*s2++);
713 options_chars++;
Roy Franz5fef3872013-09-22 15:45:33 -0700714 }
715 }
716
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500717 if (!options_chars) {
Roy Franz5fef3872013-09-22 15:45:33 -0700718 /* No command line options, so return empty string*/
Roy Franz5fef3872013-09-22 15:45:33 -0700719 options = &zero;
720 }
721
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500722 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100723
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100724 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
725 &cmdline_addr, MAX_CMDLINE_ADDRESS);
Roy Franz5fef3872013-09-22 15:45:33 -0700726 if (status != EFI_SUCCESS)
727 return NULL;
728
729 s1 = (u8 *)cmdline_addr;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500730 s2 = (const u16 *)options;
Roy Franz5fef3872013-09-22 15:45:33 -0700731
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500732 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
Roy Franz5fef3872013-09-22 15:45:33 -0700733 *s1 = '\0';
734
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500735 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700736 return (char *)cmdline_addr;
737}
Jeffrey Hugofc077162016-08-29 14:38:52 -0600738
739/*
740 * Handle calling ExitBootServices according to the requirements set out by the
741 * spec. Obtains the current memory map, and returns that info after calling
742 * ExitBootServices. The client must specify a function to perform any
743 * processing of the memory map data prior to ExitBootServices. A client
744 * specific structure may be passed to the function via priv. The client
745 * function may be called multiple times.
746 */
747efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
748 void *handle,
749 struct efi_boot_memmap *map,
750 void *priv,
751 efi_exit_boot_map_processing priv_func)
752{
753 efi_status_t status;
754
755 status = efi_get_memory_map(sys_table_arg, map);
756
757 if (status != EFI_SUCCESS)
758 goto fail;
759
760 status = priv_func(sys_table_arg, map, priv);
761 if (status != EFI_SUCCESS)
762 goto free_map;
763
764 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
765
766 if (status == EFI_INVALID_PARAMETER) {
767 /*
768 * The memory map changed between efi_get_memory_map() and
769 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
770 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
771 * updated map, and try again. The spec implies one retry
772 * should be sufficent, which is confirmed against the EDK2
773 * implementation. Per the spec, we can only invoke
774 * get_memory_map() and exit_boot_services() - we cannot alloc
775 * so efi_get_memory_map() cannot be used, and we must reuse
776 * the buffer. For all practical purposes, the headroom in the
777 * buffer should account for any changes in the map so the call
778 * to get_memory_map() is expected to succeed here.
779 */
780 *map->map_size = *map->buff_size;
781 status = efi_call_early(get_memory_map,
782 map->map_size,
783 *map->map,
784 map->key_ptr,
785 map->desc_size,
786 map->desc_ver);
787
788 /* exit_boot_services() was called, thus cannot free */
789 if (status != EFI_SUCCESS)
790 goto fail;
791
792 status = priv_func(sys_table_arg, map, priv);
793 /* exit_boot_services() was called, thus cannot free */
794 if (status != EFI_SUCCESS)
795 goto fail;
796
797 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
798 }
799
800 /* exit_boot_services() was called, thus cannot free */
801 if (status != EFI_SUCCESS)
802 goto fail;
803
804 return EFI_SUCCESS;
805
806free_map:
807 efi_call_early(free_pool, *map->map);
808fail:
809 return status;
810}