blob: 2e17d2b8787cacef3f837da572795904bfe22cef [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 Biesheuvel60f38de2017-04-04 17:09:08 +010035static int __section(.data) __nokaslr;
36
37int __pure nokaslr(void)
38{
39 return __nokaslr;
40}
41
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060042#define EFI_MMAP_NR_SLACK_SLOTS 8
43
Roy Franz36f89612013-09-22 15:45:40 -070044struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070045 efi_file_handle_t *handle;
46 u64 size;
47};
48
Ard Biesheuvelbd669472014-07-02 14:54:42 +020049void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070050{
51 char *s8;
52
53 for (s8 = str; *s8; s8++) {
54 efi_char16_t ch[2] = { 0 };
55
56 ch[0] = *s8;
57 if (*s8 == '\n') {
58 efi_char16_t nl[2] = { '\r', 0 };
Roy Franz876dc362013-09-22 15:45:28 -070059 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070060 }
61
Roy Franz876dc362013-09-22 15:45:28 -070062 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070063 }
64}
65
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060066static inline bool mmap_has_headroom(unsigned long buff_size,
67 unsigned long map_size,
68 unsigned long desc_size)
69{
70 unsigned long slack = buff_size - map_size;
71
72 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
73}
74
Ard Biesheuvelbd669472014-07-02 14:54:42 +020075efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060076 struct efi_boot_memmap *map)
Roy Franz7721da42013-09-22 15:45:27 -070077{
78 efi_memory_desc_t *m = NULL;
79 efi_status_t status;
80 unsigned long key;
81 u32 desc_version;
82
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060083 *map->desc_size = sizeof(*m);
84 *map->map_size = *map->desc_size * 32;
85 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +000086again:
Matt Fleming204b0a12014-03-22 10:09:01 +000087 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060088 *map->map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -070089 if (status != EFI_SUCCESS)
90 goto fail;
91
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060092 *map->desc_size = 0;
Matt Fleming43a9f692015-02-13 15:46:56 +000093 key = 0;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060094 status = efi_call_early(get_memory_map, map->map_size, m,
95 &key, map->desc_size, &desc_version);
96 if (status == EFI_BUFFER_TOO_SMALL ||
97 !mmap_has_headroom(*map->buff_size, *map->map_size,
98 *map->desc_size)) {
Matt Fleming204b0a12014-03-22 10:09:01 +000099 efi_call_early(free_pool, m);
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600100 /*
101 * Make sure there is some entries of headroom so that the
102 * buffer can be reused for a new map after allocations are
103 * no longer permitted. Its unlikely that the map will grow to
104 * exceed this headroom once we are ready to trigger
105 * ExitBootServices()
106 */
107 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
108 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +0000109 goto again;
Roy Franz7721da42013-09-22 15:45:27 -0700110 }
111
112 if (status != EFI_SUCCESS)
Matt Fleming204b0a12014-03-22 10:09:01 +0000113 efi_call_early(free_pool, m);
Matt Fleming54b52d82014-01-10 15:27:14 +0000114
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600115 if (map->key_ptr && status == EFI_SUCCESS)
116 *map->key_ptr = key;
117 if (map->desc_ver && status == EFI_SUCCESS)
118 *map->desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -0700119
120fail:
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600121 *map->map = m;
Roy Franz7721da42013-09-22 15:45:27 -0700122 return status;
123}
124
Roy Franz9bb40192014-01-28 10:41:28 -0800125
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000126unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
Roy Franz9bb40192014-01-28 10:41:28 -0800127{
128 efi_status_t status;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600129 unsigned long map_size, buff_size;
Roy Franz9bb40192014-01-28 10:41:28 -0800130 unsigned long membase = EFI_ERROR;
131 struct efi_memory_map map;
132 efi_memory_desc_t *md;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600133 struct efi_boot_memmap boot_map;
Roy Franz9bb40192014-01-28 10:41:28 -0800134
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600135 boot_map.map = (efi_memory_desc_t **)&map.map;
136 boot_map.map_size = &map_size;
137 boot_map.desc_size = &map.desc_size;
138 boot_map.desc_ver = NULL;
139 boot_map.key_ptr = NULL;
140 boot_map.buff_size = &buff_size;
141
142 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz9bb40192014-01-28 10:41:28 -0800143 if (status != EFI_SUCCESS)
144 return membase;
145
146 map.map_end = map.map + map_size;
147
Matt Fleming78ce2482016-04-25 21:06:38 +0100148 for_each_efi_memory_desc_in_map(&map, md) {
149 if (md->attribute & EFI_MEMORY_WB) {
Roy Franz9bb40192014-01-28 10:41:28 -0800150 if (membase > md->phys_addr)
151 membase = md->phys_addr;
Matt Fleming78ce2482016-04-25 21:06:38 +0100152 }
153 }
Roy Franz9bb40192014-01-28 10:41:28 -0800154
155 efi_call_early(free_pool, map.map);
156
157 return membase;
158}
159
Roy Franz7721da42013-09-22 15:45:27 -0700160/*
161 * Allocate at the highest possible address that is not above 'max'.
162 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200163efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
164 unsigned long size, unsigned long align,
165 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -0700166{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600167 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700168 efi_memory_desc_t *map;
169 efi_status_t status;
170 unsigned long nr_pages;
171 u64 max_addr = 0;
172 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600173 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700174
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600175 boot_map.map = &map;
176 boot_map.map_size = &map_size;
177 boot_map.desc_size = &desc_size;
178 boot_map.desc_ver = NULL;
179 boot_map.key_ptr = NULL;
180 boot_map.buff_size = &buff_size;
181
182 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz7721da42013-09-22 15:45:27 -0700183 if (status != EFI_SUCCESS)
184 goto fail;
185
Roy Franz38dd9c02013-09-22 15:45:30 -0700186 /*
Roy Franz5b88a312016-11-12 21:32:29 +0000187 * Enforce minimum alignment that EFI or Linux requires when
188 * requesting a specific address. We are doing page-based (or
189 * larger) allocations, and both the address and size must meet
190 * alignment constraints.
Roy Franz38dd9c02013-09-22 15:45:30 -0700191 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100192 if (align < EFI_ALLOC_ALIGN)
193 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700194
Roy Franz5b88a312016-11-12 21:32:29 +0000195 size = round_up(size, EFI_ALLOC_ALIGN);
196 nr_pages = size / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700197again:
198 for (i = 0; i < map_size / desc_size; i++) {
199 efi_memory_desc_t *desc;
200 unsigned long m = (unsigned long)map;
201 u64 start, end;
202
203 desc = (efi_memory_desc_t *)(m + (i * desc_size));
204 if (desc->type != EFI_CONVENTIONAL_MEMORY)
205 continue;
206
207 if (desc->num_pages < nr_pages)
208 continue;
209
210 start = desc->phys_addr;
Roy Franz5b88a312016-11-12 21:32:29 +0000211 end = start + desc->num_pages * EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700212
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800213 if (end > max)
Roy Franz7721da42013-09-22 15:45:27 -0700214 end = max;
215
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800216 if ((start + size) > end)
217 continue;
218
Roy Franz7721da42013-09-22 15:45:27 -0700219 if (round_down(end - size, align) < start)
220 continue;
221
222 start = round_down(end - size, align);
223
224 /*
225 * Don't allocate at 0x0. It will confuse code that
226 * checks pointers against NULL.
227 */
228 if (start == 0x0)
229 continue;
230
231 if (start > max_addr)
232 max_addr = start;
233 }
234
235 if (!max_addr)
236 status = EFI_NOT_FOUND;
237 else {
Matt Fleming204b0a12014-03-22 10:09:01 +0000238 status = efi_call_early(allocate_pages,
239 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
240 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700241 if (status != EFI_SUCCESS) {
242 max = max_addr;
243 max_addr = 0;
244 goto again;
245 }
246
247 *addr = max_addr;
248 }
249
Matt Fleming204b0a12014-03-22 10:09:01 +0000250 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700251fail:
252 return status;
253}
254
255/*
256 * Allocate at the lowest possible address.
257 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200258efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
259 unsigned long size, unsigned long align,
260 unsigned long *addr)
Roy Franz7721da42013-09-22 15:45:27 -0700261{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600262 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700263 efi_memory_desc_t *map;
264 efi_status_t status;
265 unsigned long nr_pages;
266 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600267 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700268
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600269 boot_map.map = &map;
270 boot_map.map_size = &map_size;
271 boot_map.desc_size = &desc_size;
272 boot_map.desc_ver = NULL;
273 boot_map.key_ptr = NULL;
274 boot_map.buff_size = &buff_size;
275
276 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz7721da42013-09-22 15:45:27 -0700277 if (status != EFI_SUCCESS)
278 goto fail;
279
Roy Franz38dd9c02013-09-22 15:45:30 -0700280 /*
Roy Franz5b88a312016-11-12 21:32:29 +0000281 * Enforce minimum alignment that EFI or Linux requires when
282 * requesting a specific address. We are doing page-based (or
283 * larger) allocations, and both the address and size must meet
284 * alignment constraints.
Roy Franz38dd9c02013-09-22 15:45:30 -0700285 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100286 if (align < EFI_ALLOC_ALIGN)
287 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700288
Roy Franz5b88a312016-11-12 21:32:29 +0000289 size = round_up(size, EFI_ALLOC_ALIGN);
290 nr_pages = size / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700291 for (i = 0; i < map_size / desc_size; i++) {
292 efi_memory_desc_t *desc;
293 unsigned long m = (unsigned long)map;
294 u64 start, end;
295
296 desc = (efi_memory_desc_t *)(m + (i * desc_size));
297
298 if (desc->type != EFI_CONVENTIONAL_MEMORY)
299 continue;
300
301 if (desc->num_pages < nr_pages)
302 continue;
303
304 start = desc->phys_addr;
Roy Franz5b88a312016-11-12 21:32:29 +0000305 end = start + desc->num_pages * EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700306
307 /*
308 * Don't allocate at 0x0. It will confuse code that
309 * checks pointers against NULL. Skip the first 8
310 * bytes so we start at a nice even number.
311 */
312 if (start == 0x0)
313 start += 8;
314
315 start = round_up(start, align);
316 if ((start + size) > end)
317 continue;
318
Matt Fleming204b0a12014-03-22 10:09:01 +0000319 status = efi_call_early(allocate_pages,
320 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
321 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700322 if (status == EFI_SUCCESS) {
323 *addr = start;
324 break;
325 }
326 }
327
328 if (i == map_size / desc_size)
329 status = EFI_NOT_FOUND;
330
Matt Fleming204b0a12014-03-22 10:09:01 +0000331 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700332fail:
333 return status;
334}
335
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200336void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
337 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700338{
339 unsigned long nr_pages;
340
Roy Franz0e1cadb2013-09-22 15:45:38 -0700341 if (!size)
342 return;
343
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100344 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000345 efi_call_early(free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700346}
347
Lukas Wunner2bd79f32017-01-31 13:21:33 +0000348static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
349 efi_char16_t *filename_16, void **handle,
350 u64 *file_sz)
351{
352 efi_file_handle_t *h, *fh = __fh;
353 efi_file_info_t *info;
354 efi_status_t status;
355 efi_guid_t info_guid = EFI_FILE_INFO_ID;
356 unsigned long info_sz;
357
358 status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
359 EFI_FILE_MODE_READ, (u64)0);
360 if (status != EFI_SUCCESS) {
361 efi_printk(sys_table_arg, "Failed to open file: ");
362 efi_char16_printk(sys_table_arg, filename_16);
363 efi_printk(sys_table_arg, "\n");
364 return status;
365 }
366
367 *handle = h;
368
369 info_sz = 0;
370 status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
371 &info_sz, NULL);
372 if (status != EFI_BUFFER_TOO_SMALL) {
373 efi_printk(sys_table_arg, "Failed to get file info size\n");
374 return status;
375 }
376
377grow:
378 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
379 info_sz, (void **)&info);
380 if (status != EFI_SUCCESS) {
381 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
382 return status;
383 }
384
385 status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
386 &info_sz, info);
387 if (status == EFI_BUFFER_TOO_SMALL) {
388 efi_call_early(free_pool, info);
389 goto grow;
390 }
391
392 *file_sz = info->file_size;
393 efi_call_early(free_pool, info);
394
395 if (status != EFI_SUCCESS)
396 efi_printk(sys_table_arg, "Failed to get initrd info\n");
397
398 return status;
399}
400
401static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
402{
403 return efi_call_proto(efi_file_handle, read, handle, size, addr);
404}
405
406static efi_status_t efi_file_close(void *handle)
407{
408 return efi_call_proto(efi_file_handle, close, handle);
409}
410
Matt Fleming5a17dae2014-08-05 11:52:11 +0100411/*
412 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
413 * option, e.g. efi=nochunk.
414 *
415 * It should be noted that efi= is parsed in two very different
416 * environments, first in the early boot environment of the EFI boot
417 * stub, and subsequently during the kernel boot.
418 */
Ard Biesheuvel60f38de2017-04-04 17:09:08 +0100419efi_status_t efi_parse_options(char const *cmdline)
Matt Fleming5a17dae2014-08-05 11:52:11 +0100420{
421 char *str;
422
Ard Biesheuvel60f38de2017-04-04 17:09:08 +0100423 str = strstr(cmdline, "nokaslr");
424 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
425 __nokaslr = 1;
Ard Biesheuvelb3879a42017-02-06 11:22:46 +0000426
427 /*
Matt Fleming5a17dae2014-08-05 11:52:11 +0100428 * If no EFI parameters were specified on the cmdline we've got
429 * nothing to do.
430 */
431 str = strstr(cmdline, "efi=");
432 if (!str)
433 return EFI_SUCCESS;
434
435 /* Skip ahead to first argument */
436 str += strlen("efi=");
437
438 /*
439 * Remember, because efi= is also used by the kernel we need to
440 * skip over arguments we don't understand.
441 */
Ard Biesheuvel4c3f14b2017-04-04 17:02:45 +0100442 while (*str && *str != ' ') {
Matt Fleming5a17dae2014-08-05 11:52:11 +0100443 if (!strncmp(str, "nochunk", 7)) {
444 str += strlen("nochunk");
445 __chunk_size = -1UL;
446 }
447
448 /* Group words together, delimited by "," */
Ard Biesheuvel4c3f14b2017-04-04 17:02:45 +0100449 while (*str && *str != ' ' && *str != ',')
Matt Fleming5a17dae2014-08-05 11:52:11 +0100450 str++;
451
452 if (*str == ',')
453 str++;
454 }
455
456 return EFI_SUCCESS;
457}
Roy Franz7721da42013-09-22 15:45:27 -0700458
459/*
Roy Franz36f89612013-09-22 15:45:40 -0700460 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700461 *
Roy Franz36f89612013-09-22 15:45:40 -0700462 * We only support loading a file from the same filesystem as
463 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700464 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200465efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
466 efi_loaded_image_t *image,
467 char *cmd_line, char *option_string,
468 unsigned long max_addr,
469 unsigned long *load_addr,
470 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700471{
Roy Franz36f89612013-09-22 15:45:40 -0700472 struct file_info *files;
473 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700474 u64 file_size_total;
Leif Lindholm9403e462014-04-04 13:25:46 +0100475 efi_file_handle_t *fh = NULL;
Roy Franz7721da42013-09-22 15:45:27 -0700476 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700477 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700478 char *str;
479 int i, j, k;
480
Roy Franz36f89612013-09-22 15:45:40 -0700481 file_addr = 0;
482 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700483
Roy Franz46f45822013-09-22 15:45:39 -0700484 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700485
486 j = 0; /* See close_handles */
487
Roy Franz46f45822013-09-22 15:45:39 -0700488 if (!load_addr || !load_size)
489 return EFI_INVALID_PARAMETER;
490
491 *load_addr = 0;
492 *load_size = 0;
493
Roy Franz7721da42013-09-22 15:45:27 -0700494 if (!str || !*str)
495 return EFI_SUCCESS;
496
Roy Franz36f89612013-09-22 15:45:40 -0700497 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700498 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700499 if (!str)
500 break;
501
Roy Franz46f45822013-09-22 15:45:39 -0700502 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700503
504 /* Skip any leading slashes */
505 while (*str == '/' || *str == '\\')
506 str++;
507
508 while (*str && *str != ' ' && *str != '\n')
509 str++;
510 }
511
Roy Franz36f89612013-09-22 15:45:40 -0700512 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700513 return EFI_SUCCESS;
514
Matt Fleming204b0a12014-03-22 10:09:01 +0000515 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
516 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700517 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800518 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700519 goto fail;
520 }
521
Roy Franz46f45822013-09-22 15:45:39 -0700522 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700523 for (i = 0; i < nr_files; i++) {
524 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700525 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700526 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700527
Roy Franz46f45822013-09-22 15:45:39 -0700528 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700529 if (!str)
530 break;
531
Roy Franz46f45822013-09-22 15:45:39 -0700532 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700533
Roy Franz36f89612013-09-22 15:45:40 -0700534 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700535 p = filename_16;
536
537 /* Skip any leading slashes */
538 while (*str == '/' || *str == '\\')
539 str++;
540
541 while (*str && *str != ' ' && *str != '\n') {
542 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
543 break;
544
545 if (*str == '/') {
546 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700547 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700548 } else {
549 *p++ = *str++;
550 }
551 }
552
553 *p = '\0';
554
555 /* Only open the volume once. */
556 if (!i) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000557 status = efi_open_volume(sys_table_arg, image,
558 (void **)&fh);
559 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700560 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700561 }
562
Matt Fleming54b52d82014-01-10 15:27:14 +0000563 status = efi_file_size(sys_table_arg, fh, filename_16,
564 (void **)&file->handle, &file->size);
565 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700566 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700567
Matt Fleming54b52d82014-01-10 15:27:14 +0000568 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700569 }
570
Roy Franz36f89612013-09-22 15:45:40 -0700571 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700572 unsigned long addr;
573
574 /*
Roy Franz36f89612013-09-22 15:45:40 -0700575 * Multiple files need to be at consecutive addresses in memory,
576 * so allocate enough memory for all the files. This is used
577 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700578 */
Roy Franz36f89612013-09-22 15:45:40 -0700579 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
580 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700581 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800582 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700583 goto close_handles;
584 }
585
586 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700587 if (file_addr > max_addr) {
Roy Franzf966ea02013-12-13 11:04:49 -0800588 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700589 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700590 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700591 }
592
Roy Franz36f89612013-09-22 15:45:40 -0700593 addr = file_addr;
594 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700595 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700596
Roy Franz36f89612013-09-22 15:45:40 -0700597 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700598 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700599 unsigned long chunksize;
Ard Biesheuvelb3879a42017-02-06 11:22:46 +0000600
601 if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
Matt Fleming5a17dae2014-08-05 11:52:11 +0100602 chunksize = __chunk_size;
Roy Franz7721da42013-09-22 15:45:27 -0700603 else
604 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000605
Matt Fleming47514c92014-04-10 14:11:45 +0100606 status = efi_file_read(files[j].handle,
Matt Fleming54b52d82014-01-10 15:27:14 +0000607 &chunksize,
608 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700609 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800610 pr_efi_err(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700611 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700612 }
613 addr += chunksize;
614 size -= chunksize;
615 }
616
Matt Fleming47514c92014-04-10 14:11:45 +0100617 efi_file_close(files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700618 }
619
620 }
621
Matt Fleming204b0a12014-03-22 10:09:01 +0000622 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700623
Roy Franz36f89612013-09-22 15:45:40 -0700624 *load_addr = file_addr;
625 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700626
627 return status;
628
Roy Franz36f89612013-09-22 15:45:40 -0700629free_file_total:
630 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700631
632close_handles:
633 for (k = j; k < i; k++)
Matt Fleming47514c92014-04-10 14:11:45 +0100634 efi_file_close(files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700635free_files:
Matt Fleming204b0a12014-03-22 10:09:01 +0000636 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700637fail:
Roy Franz46f45822013-09-22 15:45:39 -0700638 *load_addr = 0;
639 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700640
641 return status;
642}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700643/*
644 * Relocate a kernel image, either compressed or uncompressed.
645 * In the ARM64 case, all kernel images are currently
646 * uncompressed, and as such when we relocate it we need to
647 * allocate additional space for the BSS segment. Any low
648 * memory that this function should avoid needs to be
649 * unavailable in the EFI memory map, as if the preferred
650 * address is not available the lowest available address will
651 * be used.
652 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200653efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
654 unsigned long *image_addr,
655 unsigned long image_size,
656 unsigned long alloc_size,
657 unsigned long preferred_addr,
658 unsigned long alignment)
Roy Franzc6866d72013-09-22 15:45:31 -0700659{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700660 unsigned long cur_image_addr;
661 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700662 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700663 unsigned long nr_pages;
664 efi_physical_addr_t efi_addr = preferred_addr;
665
666 if (!image_addr || !image_size || !alloc_size)
667 return EFI_INVALID_PARAMETER;
668 if (alloc_size < image_size)
669 return EFI_INVALID_PARAMETER;
670
671 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700672
673 /*
674 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700675 * anywhere in memory, but the kernel has restrictions on the
676 * max physical address it can run at. Some architectures
677 * also have a prefered address, so first try to relocate
678 * to the preferred address. If that fails, allocate as low
679 * as possible while respecting the required alignment.
680 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100681 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000682 status = efi_call_early(allocate_pages,
683 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
684 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700685 new_addr = efi_addr;
686 /*
687 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700688 * possible.
689 */
Roy Franzc6866d72013-09-22 15:45:31 -0700690 if (status != EFI_SUCCESS) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700691 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
692 &new_addr);
693 }
694 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800695 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
Roy Franz4a9f3a72013-09-22 15:45:32 -0700696 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700697 }
698
Roy Franz4a9f3a72013-09-22 15:45:32 -0700699 /*
700 * We know source/dest won't overlap since both memory ranges
701 * have been allocated by UEFI, so we can safely use memcpy.
702 */
703 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700704
Roy Franz4a9f3a72013-09-22 15:45:32 -0700705 /* Return the new address of the relocated image. */
706 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700707
708 return status;
709}
Roy Franz5fef3872013-09-22 15:45:33 -0700710
711/*
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500712 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
713 * This overestimates for surrogates, but that is okay.
714 */
715static int efi_utf8_bytes(u16 c)
716{
717 return 1 + (c >= 0x80) + (c >= 0x800);
718}
719
720/*
721 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
722 */
723static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
724{
725 unsigned int c;
726
727 while (n--) {
728 c = *src++;
729 if (n && c >= 0xd800 && c <= 0xdbff &&
730 *src >= 0xdc00 && *src <= 0xdfff) {
731 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
732 src++;
733 n--;
734 }
735 if (c >= 0xd800 && c <= 0xdfff)
736 c = 0xfffd; /* Unmatched surrogate */
737 if (c < 0x80) {
738 *dst++ = c;
739 continue;
740 }
741 if (c < 0x800) {
742 *dst++ = 0xc0 + (c >> 6);
743 goto t1;
744 }
745 if (c < 0x10000) {
746 *dst++ = 0xe0 + (c >> 12);
747 goto t2;
748 }
749 *dst++ = 0xf0 + (c >> 18);
750 *dst++ = 0x80 + ((c >> 12) & 0x3f);
751 t2:
752 *dst++ = 0x80 + ((c >> 6) & 0x3f);
753 t1:
754 *dst++ = 0x80 + (c & 0x3f);
755 }
756
757 return dst;
758}
759
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100760#ifndef MAX_CMDLINE_ADDRESS
761#define MAX_CMDLINE_ADDRESS ULONG_MAX
762#endif
763
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500764/*
Roy Franz5fef3872013-09-22 15:45:33 -0700765 * Convert the unicode UEFI command line to ASCII to pass to kernel.
766 * Size of memory allocated return in *cmd_line_len.
767 * Returns NULL on error.
768 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200769char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
770 efi_loaded_image_t *image,
771 int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700772{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500773 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700774 u8 *s1 = NULL;
775 unsigned long cmdline_addr = 0;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500776 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
777 const u16 *options = image->load_options;
778 int options_bytes = 0; /* UTF-8 bytes */
779 int options_chars = 0; /* UTF-16 chars */
Roy Franz5fef3872013-09-22 15:45:33 -0700780 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700781 u16 zero = 0;
782
783 if (options) {
784 s2 = options;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500785 while (*s2 && *s2 != '\n'
786 && options_chars < load_options_chars) {
787 options_bytes += efi_utf8_bytes(*s2++);
788 options_chars++;
Roy Franz5fef3872013-09-22 15:45:33 -0700789 }
790 }
791
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500792 if (!options_chars) {
Roy Franz5fef3872013-09-22 15:45:33 -0700793 /* No command line options, so return empty string*/
Roy Franz5fef3872013-09-22 15:45:33 -0700794 options = &zero;
795 }
796
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500797 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100798
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100799 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
800 &cmdline_addr, MAX_CMDLINE_ADDRESS);
Roy Franz5fef3872013-09-22 15:45:33 -0700801 if (status != EFI_SUCCESS)
802 return NULL;
803
804 s1 = (u8 *)cmdline_addr;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500805 s2 = (const u16 *)options;
Roy Franz5fef3872013-09-22 15:45:33 -0700806
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500807 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
Roy Franz5fef3872013-09-22 15:45:33 -0700808 *s1 = '\0';
809
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500810 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700811 return (char *)cmdline_addr;
812}
Jeffrey Hugofc077162016-08-29 14:38:52 -0600813
814/*
815 * Handle calling ExitBootServices according to the requirements set out by the
816 * spec. Obtains the current memory map, and returns that info after calling
817 * ExitBootServices. The client must specify a function to perform any
818 * processing of the memory map data prior to ExitBootServices. A client
819 * specific structure may be passed to the function via priv. The client
820 * function may be called multiple times.
821 */
822efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
823 void *handle,
824 struct efi_boot_memmap *map,
825 void *priv,
826 efi_exit_boot_map_processing priv_func)
827{
828 efi_status_t status;
829
830 status = efi_get_memory_map(sys_table_arg, map);
831
832 if (status != EFI_SUCCESS)
833 goto fail;
834
835 status = priv_func(sys_table_arg, map, priv);
836 if (status != EFI_SUCCESS)
837 goto free_map;
838
839 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
840
841 if (status == EFI_INVALID_PARAMETER) {
842 /*
843 * The memory map changed between efi_get_memory_map() and
844 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
845 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
846 * updated map, and try again. The spec implies one retry
847 * should be sufficent, which is confirmed against the EDK2
848 * implementation. Per the spec, we can only invoke
849 * get_memory_map() and exit_boot_services() - we cannot alloc
850 * so efi_get_memory_map() cannot be used, and we must reuse
851 * the buffer. For all practical purposes, the headroom in the
852 * buffer should account for any changes in the map so the call
853 * to get_memory_map() is expected to succeed here.
854 */
855 *map->map_size = *map->buff_size;
856 status = efi_call_early(get_memory_map,
857 map->map_size,
858 *map->map,
859 map->key_ptr,
860 map->desc_size,
861 map->desc_ver);
862
863 /* exit_boot_services() was called, thus cannot free */
864 if (status != EFI_SUCCESS)
865 goto fail;
866
867 status = priv_func(sys_table_arg, map, priv);
868 /* exit_boot_services() was called, thus cannot free */
869 if (status != EFI_SUCCESS)
870 goto fail;
871
872 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
873 }
874
875 /* exit_boot_services() was called, thus cannot free */
876 if (status != EFI_SUCCESS)
877 goto fail;
878
879 return EFI_SUCCESS;
880
881free_map:
882 efi_call_early(free_pool, *map->map);
883fail:
884 return status;
885}