blob: 2db141c84e7677e1d194fb79289dc599cd545320 [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
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +010035/*
36 * Allow the platform to override the allocation granularity: this allows
37 * systems that have the capability to run with a larger page size to deal
38 * with the allocations for initrd and fdt more efficiently.
39 */
40#ifndef EFI_ALLOC_ALIGN
41#define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
42#endif
43
Ard Biesheuvele3111d52017-04-04 17:09:08 +010044static int __section(.data) __nokaslr;
45
46int __pure nokaslr(void)
47{
48 return __nokaslr;
49}
50
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060051#define EFI_MMAP_NR_SLACK_SLOTS 8
52
Roy Franz36f89612013-09-22 15:45:40 -070053struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070054 efi_file_handle_t *handle;
55 u64 size;
56};
57
Ard Biesheuvelbd669472014-07-02 14:54:42 +020058void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070059{
60 char *s8;
61
62 for (s8 = str; *s8; s8++) {
63 efi_char16_t ch[2] = { 0 };
64
65 ch[0] = *s8;
66 if (*s8 == '\n') {
67 efi_char16_t nl[2] = { '\r', 0 };
Roy Franz876dc362013-09-22 15:45:28 -070068 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070069 }
70
Roy Franz876dc362013-09-22 15:45:28 -070071 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070072 }
73}
74
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060075static inline bool mmap_has_headroom(unsigned long buff_size,
76 unsigned long map_size,
77 unsigned long desc_size)
78{
79 unsigned long slack = buff_size - map_size;
80
81 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
82}
83
Ard Biesheuvelbd669472014-07-02 14:54:42 +020084efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060085 struct efi_boot_memmap *map)
Roy Franz7721da42013-09-22 15:45:27 -070086{
87 efi_memory_desc_t *m = NULL;
88 efi_status_t status;
89 unsigned long key;
90 u32 desc_version;
91
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060092 *map->desc_size = sizeof(*m);
93 *map->map_size = *map->desc_size * 32;
94 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +000095again:
Matt Fleming204b0a12014-03-22 10:09:01 +000096 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060097 *map->map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -070098 if (status != EFI_SUCCESS)
99 goto fail;
100
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600101 *map->desc_size = 0;
Matt Fleming43a9f692015-02-13 15:46:56 +0000102 key = 0;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600103 status = efi_call_early(get_memory_map, map->map_size, m,
104 &key, map->desc_size, &desc_version);
105 if (status == EFI_BUFFER_TOO_SMALL ||
106 !mmap_has_headroom(*map->buff_size, *map->map_size,
107 *map->desc_size)) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000108 efi_call_early(free_pool, m);
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600109 /*
110 * Make sure there is some entries of headroom so that the
111 * buffer can be reused for a new map after allocations are
112 * no longer permitted. Its unlikely that the map will grow to
113 * exceed this headroom once we are ready to trigger
114 * ExitBootServices()
115 */
116 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
117 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +0000118 goto again;
Roy Franz7721da42013-09-22 15:45:27 -0700119 }
120
121 if (status != EFI_SUCCESS)
Matt Fleming204b0a12014-03-22 10:09:01 +0000122 efi_call_early(free_pool, m);
Matt Fleming54b52d82014-01-10 15:27:14 +0000123
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600124 if (map->key_ptr && status == EFI_SUCCESS)
125 *map->key_ptr = key;
126 if (map->desc_ver && status == EFI_SUCCESS)
127 *map->desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -0700128
129fail:
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600130 *map->map = m;
Roy Franz7721da42013-09-22 15:45:27 -0700131 return status;
132}
133
Roy Franz9bb40192014-01-28 10:41:28 -0800134
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000135unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
Roy Franz9bb40192014-01-28 10:41:28 -0800136{
137 efi_status_t status;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600138 unsigned long map_size, buff_size;
Roy Franz9bb40192014-01-28 10:41:28 -0800139 unsigned long membase = EFI_ERROR;
140 struct efi_memory_map map;
141 efi_memory_desc_t *md;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600142 struct efi_boot_memmap boot_map;
Roy Franz9bb40192014-01-28 10:41:28 -0800143
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600144 boot_map.map = (efi_memory_desc_t **)&map.map;
145 boot_map.map_size = &map_size;
146 boot_map.desc_size = &map.desc_size;
147 boot_map.desc_ver = NULL;
148 boot_map.key_ptr = NULL;
149 boot_map.buff_size = &buff_size;
150
151 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz9bb40192014-01-28 10:41:28 -0800152 if (status != EFI_SUCCESS)
153 return membase;
154
155 map.map_end = map.map + map_size;
156
Matt Fleming78ce2482016-04-25 21:06:38 +0100157 for_each_efi_memory_desc_in_map(&map, md) {
158 if (md->attribute & EFI_MEMORY_WB) {
Roy Franz9bb40192014-01-28 10:41:28 -0800159 if (membase > md->phys_addr)
160 membase = md->phys_addr;
Matt Fleming78ce2482016-04-25 21:06:38 +0100161 }
162 }
Roy Franz9bb40192014-01-28 10:41:28 -0800163
164 efi_call_early(free_pool, map.map);
165
166 return membase;
167}
168
Roy Franz7721da42013-09-22 15:45:27 -0700169/*
170 * Allocate at the highest possible address that is not above 'max'.
171 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200172efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
173 unsigned long size, unsigned long align,
174 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -0700175{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600176 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700177 efi_memory_desc_t *map;
178 efi_status_t status;
179 unsigned long nr_pages;
180 u64 max_addr = 0;
181 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600182 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700183
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600184 boot_map.map = &map;
185 boot_map.map_size = &map_size;
186 boot_map.desc_size = &desc_size;
187 boot_map.desc_ver = NULL;
188 boot_map.key_ptr = NULL;
189 boot_map.buff_size = &buff_size;
190
191 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz7721da42013-09-22 15:45:27 -0700192 if (status != EFI_SUCCESS)
193 goto fail;
194
Roy Franz38dd9c02013-09-22 15:45:30 -0700195 /*
196 * Enforce minimum alignment that EFI requires when requesting
197 * a specific address. We are doing page-based allocations,
198 * so we must be aligned to a page.
199 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100200 if (align < EFI_ALLOC_ALIGN)
201 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700202
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100203 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700204again:
205 for (i = 0; i < map_size / desc_size; i++) {
206 efi_memory_desc_t *desc;
207 unsigned long m = (unsigned long)map;
208 u64 start, end;
209
210 desc = (efi_memory_desc_t *)(m + (i * desc_size));
211 if (desc->type != EFI_CONVENTIONAL_MEMORY)
212 continue;
213
214 if (desc->num_pages < nr_pages)
215 continue;
216
217 start = desc->phys_addr;
218 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
219
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800220 if (end > max)
Roy Franz7721da42013-09-22 15:45:27 -0700221 end = max;
222
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800223 if ((start + size) > end)
224 continue;
225
Roy Franz7721da42013-09-22 15:45:27 -0700226 if (round_down(end - size, align) < start)
227 continue;
228
229 start = round_down(end - size, align);
230
231 /*
232 * Don't allocate at 0x0. It will confuse code that
233 * checks pointers against NULL.
234 */
235 if (start == 0x0)
236 continue;
237
238 if (start > max_addr)
239 max_addr = start;
240 }
241
242 if (!max_addr)
243 status = EFI_NOT_FOUND;
244 else {
Matt Fleming204b0a12014-03-22 10:09:01 +0000245 status = efi_call_early(allocate_pages,
246 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
247 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700248 if (status != EFI_SUCCESS) {
249 max = max_addr;
250 max_addr = 0;
251 goto again;
252 }
253
254 *addr = max_addr;
255 }
256
Matt Fleming204b0a12014-03-22 10:09:01 +0000257 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700258fail:
259 return status;
260}
261
262/*
263 * Allocate at the lowest possible address.
264 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200265efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
266 unsigned long size, unsigned long align,
267 unsigned long *addr)
Roy Franz7721da42013-09-22 15:45:27 -0700268{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600269 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700270 efi_memory_desc_t *map;
271 efi_status_t status;
272 unsigned long nr_pages;
273 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600274 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700275
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600276 boot_map.map = &map;
277 boot_map.map_size = &map_size;
278 boot_map.desc_size = &desc_size;
279 boot_map.desc_ver = NULL;
280 boot_map.key_ptr = NULL;
281 boot_map.buff_size = &buff_size;
282
283 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz7721da42013-09-22 15:45:27 -0700284 if (status != EFI_SUCCESS)
285 goto fail;
286
Roy Franz38dd9c02013-09-22 15:45:30 -0700287 /*
288 * Enforce minimum alignment that EFI requires when requesting
289 * a specific address. We are doing page-based allocations,
290 * so we must be aligned to a page.
291 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100292 if (align < EFI_ALLOC_ALIGN)
293 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700294
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100295 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700296 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
301 desc = (efi_memory_desc_t *)(m + (i * desc_size));
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;
310 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
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 Fleming204b0a12014-03-22 10:09:01 +0000324 status = efi_call_early(allocate_pages,
325 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
326 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700327 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 Fleming204b0a12014-03-22 10:09:01 +0000336 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700337fail:
338 return status;
339}
340
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200341void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
342 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700343{
344 unsigned long nr_pages;
345
Roy Franz0e1cadb2013-09-22 15:45:38 -0700346 if (!size)
347 return;
348
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100349 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000350 efi_call_early(free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700351}
352
Matt Fleming5a17dae2014-08-05 11:52:11 +0100353/*
354 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
355 * option, e.g. efi=nochunk.
356 *
357 * It should be noted that efi= is parsed in two very different
358 * environments, first in the early boot environment of the EFI boot
359 * stub, and subsequently during the kernel boot.
360 */
Ard Biesheuvele3111d52017-04-04 17:09:08 +0100361efi_status_t efi_parse_options(char const *cmdline)
Matt Fleming5a17dae2014-08-05 11:52:11 +0100362{
363 char *str;
364
Ard Biesheuvele3111d52017-04-04 17:09:08 +0100365 str = strstr(cmdline, "nokaslr");
366 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
367 __nokaslr = 1;
368
Matt Fleming5a17dae2014-08-05 11:52:11 +0100369 /*
370 * If no EFI parameters were specified on the cmdline we've got
371 * nothing to do.
372 */
373 str = strstr(cmdline, "efi=");
374 if (!str)
375 return EFI_SUCCESS;
376
377 /* Skip ahead to first argument */
378 str += strlen("efi=");
379
380 /*
381 * Remember, because efi= is also used by the kernel we need to
382 * skip over arguments we don't understand.
383 */
384 while (*str) {
385 if (!strncmp(str, "nochunk", 7)) {
386 str += strlen("nochunk");
387 __chunk_size = -1UL;
388 }
389
390 /* Group words together, delimited by "," */
391 while (*str && *str != ',')
392 str++;
393
394 if (*str == ',')
395 str++;
396 }
397
398 return EFI_SUCCESS;
399}
Roy Franz7721da42013-09-22 15:45:27 -0700400
401/*
Roy Franz36f89612013-09-22 15:45:40 -0700402 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700403 *
Roy Franz36f89612013-09-22 15:45:40 -0700404 * We only support loading a file from the same filesystem as
405 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700406 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200407efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
408 efi_loaded_image_t *image,
409 char *cmd_line, char *option_string,
410 unsigned long max_addr,
411 unsigned long *load_addr,
412 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700413{
Roy Franz36f89612013-09-22 15:45:40 -0700414 struct file_info *files;
415 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700416 u64 file_size_total;
Leif Lindholm9403e462014-04-04 13:25:46 +0100417 efi_file_handle_t *fh = NULL;
Roy Franz7721da42013-09-22 15:45:27 -0700418 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700419 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700420 char *str;
421 int i, j, k;
422
Roy Franz36f89612013-09-22 15:45:40 -0700423 file_addr = 0;
424 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700425
Roy Franz46f45822013-09-22 15:45:39 -0700426 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700427
428 j = 0; /* See close_handles */
429
Roy Franz46f45822013-09-22 15:45:39 -0700430 if (!load_addr || !load_size)
431 return EFI_INVALID_PARAMETER;
432
433 *load_addr = 0;
434 *load_size = 0;
435
Roy Franz7721da42013-09-22 15:45:27 -0700436 if (!str || !*str)
437 return EFI_SUCCESS;
438
Roy Franz36f89612013-09-22 15:45:40 -0700439 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700440 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700441 if (!str)
442 break;
443
Roy Franz46f45822013-09-22 15:45:39 -0700444 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700445
446 /* Skip any leading slashes */
447 while (*str == '/' || *str == '\\')
448 str++;
449
450 while (*str && *str != ' ' && *str != '\n')
451 str++;
452 }
453
Roy Franz36f89612013-09-22 15:45:40 -0700454 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700455 return EFI_SUCCESS;
456
Matt Fleming204b0a12014-03-22 10:09:01 +0000457 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
458 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700459 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800460 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700461 goto fail;
462 }
463
Roy Franz46f45822013-09-22 15:45:39 -0700464 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700465 for (i = 0; i < nr_files; i++) {
466 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700467 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700468 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700469
Roy Franz46f45822013-09-22 15:45:39 -0700470 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700471 if (!str)
472 break;
473
Roy Franz46f45822013-09-22 15:45:39 -0700474 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700475
Roy Franz36f89612013-09-22 15:45:40 -0700476 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700477 p = filename_16;
478
479 /* Skip any leading slashes */
480 while (*str == '/' || *str == '\\')
481 str++;
482
483 while (*str && *str != ' ' && *str != '\n') {
484 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
485 break;
486
487 if (*str == '/') {
488 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700489 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700490 } else {
491 *p++ = *str++;
492 }
493 }
494
495 *p = '\0';
496
497 /* Only open the volume once. */
498 if (!i) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000499 status = efi_open_volume(sys_table_arg, image,
500 (void **)&fh);
501 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700502 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700503 }
504
Matt Fleming54b52d82014-01-10 15:27:14 +0000505 status = efi_file_size(sys_table_arg, fh, filename_16,
506 (void **)&file->handle, &file->size);
507 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700508 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700509
Matt Fleming54b52d82014-01-10 15:27:14 +0000510 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700511 }
512
Roy Franz36f89612013-09-22 15:45:40 -0700513 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700514 unsigned long addr;
515
516 /*
Roy Franz36f89612013-09-22 15:45:40 -0700517 * Multiple files need to be at consecutive addresses in memory,
518 * so allocate enough memory for all the files. This is used
519 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700520 */
Roy Franz36f89612013-09-22 15:45:40 -0700521 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
522 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700523 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800524 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700525 goto close_handles;
526 }
527
528 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700529 if (file_addr > max_addr) {
Roy Franzf966ea02013-12-13 11:04:49 -0800530 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700531 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700532 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700533 }
534
Roy Franz36f89612013-09-22 15:45:40 -0700535 addr = file_addr;
536 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700537 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700538
Roy Franz36f89612013-09-22 15:45:40 -0700539 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700540 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700541 unsigned long chunksize;
Matt Fleming5a17dae2014-08-05 11:52:11 +0100542 if (size > __chunk_size)
543 chunksize = __chunk_size;
Roy Franz7721da42013-09-22 15:45:27 -0700544 else
545 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000546
Matt Fleming47514c92014-04-10 14:11:45 +0100547 status = efi_file_read(files[j].handle,
Matt Fleming54b52d82014-01-10 15:27:14 +0000548 &chunksize,
549 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700550 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800551 pr_efi_err(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700552 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700553 }
554 addr += chunksize;
555 size -= chunksize;
556 }
557
Matt Fleming47514c92014-04-10 14:11:45 +0100558 efi_file_close(files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700559 }
560
561 }
562
Matt Fleming204b0a12014-03-22 10:09:01 +0000563 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700564
Roy Franz36f89612013-09-22 15:45:40 -0700565 *load_addr = file_addr;
566 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700567
568 return status;
569
Roy Franz36f89612013-09-22 15:45:40 -0700570free_file_total:
571 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700572
573close_handles:
574 for (k = j; k < i; k++)
Matt Fleming47514c92014-04-10 14:11:45 +0100575 efi_file_close(files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700576free_files:
Matt Fleming204b0a12014-03-22 10:09:01 +0000577 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700578fail:
Roy Franz46f45822013-09-22 15:45:39 -0700579 *load_addr = 0;
580 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700581
582 return status;
583}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700584/*
585 * Relocate a kernel image, either compressed or uncompressed.
586 * In the ARM64 case, all kernel images are currently
587 * uncompressed, and as such when we relocate it we need to
588 * allocate additional space for the BSS segment. Any low
589 * memory that this function should avoid needs to be
590 * unavailable in the EFI memory map, as if the preferred
591 * address is not available the lowest available address will
592 * be used.
593 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200594efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
595 unsigned long *image_addr,
596 unsigned long image_size,
597 unsigned long alloc_size,
598 unsigned long preferred_addr,
599 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700600{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700601 unsigned long cur_image_addr;
602 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700603 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700604 unsigned long nr_pages;
605 efi_physical_addr_t efi_addr = preferred_addr;
606
607 if (!image_addr || !image_size || !alloc_size)
608 return EFI_INVALID_PARAMETER;
609 if (alloc_size < image_size)
610 return EFI_INVALID_PARAMETER;
611
612 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700613
614 /*
615 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700616 * anywhere in memory, but the kernel has restrictions on the
617 * max physical address it can run at. Some architectures
618 * also have a prefered address, so first try to relocate
619 * to the preferred address. If that fails, allocate as low
620 * as possible while respecting the required alignment.
621 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100622 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000623 status = efi_call_early(allocate_pages,
624 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
625 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700626 new_addr = efi_addr;
627 /*
628 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700629 * possible.
630 */
Roy Franzc6866d72013-09-22 15:45:31 -0700631 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700632 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
633 &new_addr);
634 }
635 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800636 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
Roy Franz4a9f3a72013-09-22 15:45:32 -0700637 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700638 }
639
Roy Franz4a9f3a72013-09-22 15:45:32 -0700640 /*
641 * We know source/dest won't overlap since both memory ranges
642 * have been allocated by UEFI, so we can safely use memcpy.
643 */
644 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700645
Roy Franz4a9f3a72013-09-22 15:45:32 -0700646 /* Return the new address of the relocated image. */
647 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700648
649 return status;
650}
Roy Franz5fef3872013-09-22 15:45:33 -0700651
652/*
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500653 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
654 * This overestimates for surrogates, but that is okay.
655 */
656static int efi_utf8_bytes(u16 c)
657{
658 return 1 + (c >= 0x80) + (c >= 0x800);
659}
660
661/*
662 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
663 */
664static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
665{
666 unsigned int c;
667
668 while (n--) {
669 c = *src++;
670 if (n && c >= 0xd800 && c <= 0xdbff &&
671 *src >= 0xdc00 && *src <= 0xdfff) {
672 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
673 src++;
674 n--;
675 }
676 if (c >= 0xd800 && c <= 0xdfff)
677 c = 0xfffd; /* Unmatched surrogate */
678 if (c < 0x80) {
679 *dst++ = c;
680 continue;
681 }
682 if (c < 0x800) {
683 *dst++ = 0xc0 + (c >> 6);
684 goto t1;
685 }
686 if (c < 0x10000) {
687 *dst++ = 0xe0 + (c >> 12);
688 goto t2;
689 }
690 *dst++ = 0xf0 + (c >> 18);
691 *dst++ = 0x80 + ((c >> 12) & 0x3f);
692 t2:
693 *dst++ = 0x80 + ((c >> 6) & 0x3f);
694 t1:
695 *dst++ = 0x80 + (c & 0x3f);
696 }
697
698 return dst;
699}
700
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100701#ifndef MAX_CMDLINE_ADDRESS
702#define MAX_CMDLINE_ADDRESS ULONG_MAX
703#endif
704
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500705/*
Roy Franz5fef3872013-09-22 15:45:33 -0700706 * Convert the unicode UEFI command line to ASCII to pass to kernel.
707 * Size of memory allocated return in *cmd_line_len.
708 * Returns NULL on error.
709 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200710char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
711 efi_loaded_image_t *image,
712 int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700713{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500714 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700715 u8 *s1 = NULL;
716 unsigned long cmdline_addr = 0;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500717 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
718 const u16 *options = image->load_options;
719 int options_bytes = 0; /* UTF-8 bytes */
720 int options_chars = 0; /* UTF-16 chars */
Roy Franz5fef3872013-09-22 15:45:33 -0700721 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700722 u16 zero = 0;
723
724 if (options) {
725 s2 = options;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500726 while (*s2 && *s2 != '\n'
727 && options_chars < load_options_chars) {
728 options_bytes += efi_utf8_bytes(*s2++);
729 options_chars++;
Roy Franz5fef3872013-09-22 15:45:33 -0700730 }
731 }
732
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500733 if (!options_chars) {
Roy Franz5fef3872013-09-22 15:45:33 -0700734 /* No command line options, so return empty string*/
Roy Franz5fef3872013-09-22 15:45:33 -0700735 options = &zero;
736 }
737
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500738 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100739
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100740 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
741 &cmdline_addr, MAX_CMDLINE_ADDRESS);
Roy Franz5fef3872013-09-22 15:45:33 -0700742 if (status != EFI_SUCCESS)
743 return NULL;
744
745 s1 = (u8 *)cmdline_addr;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500746 s2 = (const u16 *)options;
Roy Franz5fef3872013-09-22 15:45:33 -0700747
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500748 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
Roy Franz5fef3872013-09-22 15:45:33 -0700749 *s1 = '\0';
750
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500751 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700752 return (char *)cmdline_addr;
753}
Jeffrey Hugofc077162016-08-29 14:38:52 -0600754
755/*
756 * Handle calling ExitBootServices according to the requirements set out by the
757 * spec. Obtains the current memory map, and returns that info after calling
758 * ExitBootServices. The client must specify a function to perform any
759 * processing of the memory map data prior to ExitBootServices. A client
760 * specific structure may be passed to the function via priv. The client
761 * function may be called multiple times.
762 */
763efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
764 void *handle,
765 struct efi_boot_memmap *map,
766 void *priv,
767 efi_exit_boot_map_processing priv_func)
768{
769 efi_status_t status;
770
771 status = efi_get_memory_map(sys_table_arg, map);
772
773 if (status != EFI_SUCCESS)
774 goto fail;
775
776 status = priv_func(sys_table_arg, map, priv);
777 if (status != EFI_SUCCESS)
778 goto free_map;
779
780 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
781
782 if (status == EFI_INVALID_PARAMETER) {
783 /*
784 * The memory map changed between efi_get_memory_map() and
785 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
786 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
787 * updated map, and try again. The spec implies one retry
788 * should be sufficent, which is confirmed against the EDK2
789 * implementation. Per the spec, we can only invoke
790 * get_memory_map() and exit_boot_services() - we cannot alloc
791 * so efi_get_memory_map() cannot be used, and we must reuse
792 * the buffer. For all practical purposes, the headroom in the
793 * buffer should account for any changes in the map so the call
794 * to get_memory_map() is expected to succeed here.
795 */
796 *map->map_size = *map->buff_size;
797 status = efi_call_early(get_memory_map,
798 map->map_size,
799 *map->map,
800 map->key_ptr,
801 map->desc_size,
802 map->desc_ver);
803
804 /* exit_boot_services() was called, thus cannot free */
805 if (status != EFI_SUCCESS)
806 goto fail;
807
808 status = priv_func(sys_table_arg, map, priv);
809 /* exit_boot_services() was called, thus cannot free */
810 if (status != EFI_SUCCESS)
811 goto fail;
812
813 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
814 }
815
816 /* exit_boot_services() was called, thus cannot free */
817 if (status != EFI_SUCCESS)
818 goto fail;
819
820 return EFI_SUCCESS;
821
822free_map:
823 efi_call_early(free_pool, *map->map);
824fail:
825 return status;
826}