blob: 1a814dc235ba669cc82433d4b3e6182ba0b9c676 [file] [log] [blame]
Ard Biesheuvel4febfb82019-02-02 10:41:15 +01001// SPDX-License-Identifier: GPL-2.0
Roy Franz7721da42013-09-22 15:45:27 -07002/*
3 * Helper functions used by the EFI stub on multiple
4 * architectures. This should be #included by the EFI stub
5 * implementation files.
6 *
7 * Copyright 2011 Intel Corporation; author Matt Fleming
Roy Franz7721da42013-09-22 15:45:27 -07008 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +02009
10#include <linux/efi.h>
11#include <asm/efi.h>
12
13#include "efistub.h"
14
Matt Fleming5a17dae2014-08-05 11:52:11 +010015/*
16 * Some firmware implementations have problems reading files in one go.
17 * A read chunk size of 1MB seems to work for most platforms.
18 *
19 * Unfortunately, reading files in chunks triggers *other* bugs on some
20 * platforms, so we provide a way to disable this workaround, which can
21 * be done by passing "efi=nochunk" on the EFI boot stub command line.
22 *
23 * If you experience issues with initrd images being corrupt it's worth
24 * trying efi=nochunk, but chunking is enabled by default because there
25 * are far more machines that require the workaround than those that
26 * break with it enabled.
27 */
Roy Franz7721da42013-09-22 15:45:27 -070028#define EFI_READ_CHUNK_SIZE (1024 * 1024)
29
Matt Fleming5a17dae2014-08-05 11:52:11 +010030static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
31
Ard Biesheuvel60f38de2017-04-04 17:09:08 +010032static int __section(.data) __nokaslr;
Ard Biesheuveleeff7d62017-04-04 17:09:09 +010033static int __section(.data) __quiet;
Ard Biesheuvel4e46c2a2019-02-02 10:41:16 +010034static int __section(.data) __novamap;
Dan Williamsb617c522019-11-06 17:43:11 -080035static bool __section(.data) efi_nosoftreserve;
Ard Biesheuvel60f38de2017-04-04 17:09:08 +010036
37int __pure nokaslr(void)
38{
39 return __nokaslr;
40}
Ard Biesheuveleeff7d62017-04-04 17:09:09 +010041int __pure is_quiet(void)
42{
43 return __quiet;
44}
Ard Biesheuvel4e46c2a2019-02-02 10:41:16 +010045int __pure novamap(void)
46{
47 return __novamap;
48}
Dan Williamsb617c522019-11-06 17:43:11 -080049bool __pure __efi_soft_reserve_enabled(void)
50{
51 return !efi_nosoftreserve;
52}
Ard Biesheuvel60f38de2017-04-04 17:09:08 +010053
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060054#define EFI_MMAP_NR_SLACK_SLOTS 8
55
Roy Franz36f89612013-09-22 15:45:40 -070056struct file_info {
Roy Franz7721da42013-09-22 15:45:27 -070057 efi_file_handle_t *handle;
58 u64 size;
59};
60
Ard Biesheuvelbd669472014-07-02 14:54:42 +020061void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070062{
63 char *s8;
64
65 for (s8 = str; *s8; s8++) {
66 efi_char16_t ch[2] = { 0 };
67
68 ch[0] = *s8;
69 if (*s8 == '\n') {
70 efi_char16_t nl[2] = { '\r', 0 };
Roy Franz876dc362013-09-22 15:45:28 -070071 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070072 }
73
Roy Franz876dc362013-09-22 15:45:28 -070074 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070075 }
76}
77
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060078static inline bool mmap_has_headroom(unsigned long buff_size,
79 unsigned long map_size,
80 unsigned long desc_size)
81{
82 unsigned long slack = buff_size - map_size;
83
84 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
85}
86
Ard Biesheuvelbd669472014-07-02 14:54:42 +020087efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060088 struct efi_boot_memmap *map)
Roy Franz7721da42013-09-22 15:45:27 -070089{
90 efi_memory_desc_t *m = NULL;
91 efi_status_t status;
92 unsigned long key;
93 u32 desc_version;
94
Jeffrey Hugodadb57a2016-08-29 14:38:51 -060095 *map->desc_size = sizeof(*m);
96 *map->map_size = *map->desc_size * 32;
97 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +000098again:
Matt Fleming204b0a12014-03-22 10:09:01 +000099 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600100 *map->map_size, (void **)&m);
Roy Franz7721da42013-09-22 15:45:27 -0700101 if (status != EFI_SUCCESS)
102 goto fail;
103
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600104 *map->desc_size = 0;
Matt Fleming43a9f692015-02-13 15:46:56 +0000105 key = 0;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600106 status = efi_call_early(get_memory_map, map->map_size, m,
107 &key, map->desc_size, &desc_version);
108 if (status == EFI_BUFFER_TOO_SMALL ||
109 !mmap_has_headroom(*map->buff_size, *map->map_size,
110 *map->desc_size)) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000111 efi_call_early(free_pool, m);
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600112 /*
113 * Make sure there is some entries of headroom so that the
114 * buffer can be reused for a new map after allocations are
115 * no longer permitted. Its unlikely that the map will grow to
116 * exceed this headroom once we are ready to trigger
117 * ExitBootServices()
118 */
119 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
120 *map->buff_size = *map->map_size;
Matt Fleming43a9f692015-02-13 15:46:56 +0000121 goto again;
Roy Franz7721da42013-09-22 15:45:27 -0700122 }
123
124 if (status != EFI_SUCCESS)
Matt Fleming204b0a12014-03-22 10:09:01 +0000125 efi_call_early(free_pool, m);
Matt Fleming54b52d82014-01-10 15:27:14 +0000126
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600127 if (map->key_ptr && status == EFI_SUCCESS)
128 *map->key_ptr = key;
129 if (map->desc_ver && status == EFI_SUCCESS)
130 *map->desc_ver = desc_version;
Roy Franz7721da42013-09-22 15:45:27 -0700131
132fail:
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600133 *map->map = m;
Roy Franz7721da42013-09-22 15:45:27 -0700134 return status;
135}
136
Roy Franz9bb40192014-01-28 10:41:28 -0800137
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000138unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
Roy Franz9bb40192014-01-28 10:41:28 -0800139{
140 efi_status_t status;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600141 unsigned long map_size, buff_size;
Roy Franz9bb40192014-01-28 10:41:28 -0800142 unsigned long membase = EFI_ERROR;
143 struct efi_memory_map map;
144 efi_memory_desc_t *md;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600145 struct efi_boot_memmap boot_map;
Roy Franz9bb40192014-01-28 10:41:28 -0800146
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600147 boot_map.map = (efi_memory_desc_t **)&map.map;
148 boot_map.map_size = &map_size;
149 boot_map.desc_size = &map.desc_size;
150 boot_map.desc_ver = NULL;
151 boot_map.key_ptr = NULL;
152 boot_map.buff_size = &buff_size;
153
154 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz9bb40192014-01-28 10:41:28 -0800155 if (status != EFI_SUCCESS)
156 return membase;
157
158 map.map_end = map.map + map_size;
159
Matt Fleming78ce2482016-04-25 21:06:38 +0100160 for_each_efi_memory_desc_in_map(&map, md) {
161 if (md->attribute & EFI_MEMORY_WB) {
Roy Franz9bb40192014-01-28 10:41:28 -0800162 if (membase > md->phys_addr)
163 membase = md->phys_addr;
Matt Fleming78ce2482016-04-25 21:06:38 +0100164 }
165 }
Roy Franz9bb40192014-01-28 10:41:28 -0800166
167 efi_call_early(free_pool, map.map);
168
169 return membase;
170}
171
Roy Franz7721da42013-09-22 15:45:27 -0700172/*
173 * Allocate at the highest possible address that is not above 'max'.
174 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200175efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
176 unsigned long size, unsigned long align,
177 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -0700178{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600179 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700180 efi_memory_desc_t *map;
181 efi_status_t status;
182 unsigned long nr_pages;
183 u64 max_addr = 0;
184 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600185 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700186
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600187 boot_map.map = &map;
188 boot_map.map_size = &map_size;
189 boot_map.desc_size = &desc_size;
190 boot_map.desc_ver = NULL;
191 boot_map.key_ptr = NULL;
192 boot_map.buff_size = &buff_size;
193
194 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz7721da42013-09-22 15:45:27 -0700195 if (status != EFI_SUCCESS)
196 goto fail;
197
Roy Franz38dd9c02013-09-22 15:45:30 -0700198 /*
Roy Franz5b88a312016-11-12 21:32:29 +0000199 * Enforce minimum alignment that EFI or Linux requires when
200 * requesting a specific address. We are doing page-based (or
201 * larger) allocations, and both the address and size must meet
202 * alignment constraints.
Roy Franz38dd9c02013-09-22 15:45:30 -0700203 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100204 if (align < EFI_ALLOC_ALIGN)
205 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700206
Roy Franz5b88a312016-11-12 21:32:29 +0000207 size = round_up(size, EFI_ALLOC_ALIGN);
208 nr_pages = size / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700209again:
210 for (i = 0; i < map_size / desc_size; i++) {
211 efi_memory_desc_t *desc;
212 unsigned long m = (unsigned long)map;
213 u64 start, end;
214
Baoquan He02e43c22017-08-16 21:46:51 +0800215 desc = efi_early_memdesc_ptr(m, desc_size, i);
Roy Franz7721da42013-09-22 15:45:27 -0700216 if (desc->type != EFI_CONVENTIONAL_MEMORY)
217 continue;
218
Dan Williamsb617c522019-11-06 17:43:11 -0800219 if (efi_soft_reserve_enabled() &&
220 (desc->attribute & EFI_MEMORY_SP))
221 continue;
222
Roy Franz7721da42013-09-22 15:45:27 -0700223 if (desc->num_pages < nr_pages)
224 continue;
225
226 start = desc->phys_addr;
Roy Franz5b88a312016-11-12 21:32:29 +0000227 end = start + desc->num_pages * EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700228
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800229 if (end > max)
Roy Franz7721da42013-09-22 15:45:27 -0700230 end = max;
231
Yinghai Lu7ed620b2015-02-19 20:18:03 -0800232 if ((start + size) > end)
233 continue;
234
Roy Franz7721da42013-09-22 15:45:27 -0700235 if (round_down(end - size, align) < start)
236 continue;
237
238 start = round_down(end - size, align);
239
240 /*
241 * Don't allocate at 0x0. It will confuse code that
242 * checks pointers against NULL.
243 */
244 if (start == 0x0)
245 continue;
246
247 if (start > max_addr)
248 max_addr = start;
249 }
250
251 if (!max_addr)
252 status = EFI_NOT_FOUND;
253 else {
Matt Fleming204b0a12014-03-22 10:09:01 +0000254 status = efi_call_early(allocate_pages,
255 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
256 nr_pages, &max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700257 if (status != EFI_SUCCESS) {
258 max = max_addr;
259 max_addr = 0;
260 goto again;
261 }
262
263 *addr = max_addr;
264 }
265
Matt Fleming204b0a12014-03-22 10:09:01 +0000266 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700267fail:
268 return status;
269}
270
271/*
Kairui Song220dd762019-10-29 18:37:54 +0100272 * Allocate at the lowest possible address that is not below 'min'.
Roy Franz7721da42013-09-22 15:45:27 -0700273 */
Kairui Song220dd762019-10-29 18:37:54 +0100274efi_status_t efi_low_alloc_above(efi_system_table_t *sys_table_arg,
275 unsigned long size, unsigned long align,
276 unsigned long *addr, unsigned long min)
Roy Franz7721da42013-09-22 15:45:27 -0700277{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600278 unsigned long map_size, desc_size, buff_size;
Roy Franz7721da42013-09-22 15:45:27 -0700279 efi_memory_desc_t *map;
280 efi_status_t status;
281 unsigned long nr_pages;
282 int i;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600283 struct efi_boot_memmap boot_map;
Roy Franz7721da42013-09-22 15:45:27 -0700284
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600285 boot_map.map = &map;
286 boot_map.map_size = &map_size;
287 boot_map.desc_size = &desc_size;
288 boot_map.desc_ver = NULL;
289 boot_map.key_ptr = NULL;
290 boot_map.buff_size = &buff_size;
291
292 status = efi_get_memory_map(sys_table_arg, &boot_map);
Roy Franz7721da42013-09-22 15:45:27 -0700293 if (status != EFI_SUCCESS)
294 goto fail;
295
Roy Franz38dd9c02013-09-22 15:45:30 -0700296 /*
Roy Franz5b88a312016-11-12 21:32:29 +0000297 * Enforce minimum alignment that EFI or Linux requires when
298 * requesting a specific address. We are doing page-based (or
299 * larger) allocations, and both the address and size must meet
300 * alignment constraints.
Roy Franz38dd9c02013-09-22 15:45:30 -0700301 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100302 if (align < EFI_ALLOC_ALIGN)
303 align = EFI_ALLOC_ALIGN;
Roy Franz38dd9c02013-09-22 15:45:30 -0700304
Roy Franz5b88a312016-11-12 21:32:29 +0000305 size = round_up(size, EFI_ALLOC_ALIGN);
306 nr_pages = size / EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700307 for (i = 0; i < map_size / desc_size; i++) {
308 efi_memory_desc_t *desc;
309 unsigned long m = (unsigned long)map;
310 u64 start, end;
311
Baoquan He02e43c22017-08-16 21:46:51 +0800312 desc = efi_early_memdesc_ptr(m, desc_size, i);
Roy Franz7721da42013-09-22 15:45:27 -0700313
314 if (desc->type != EFI_CONVENTIONAL_MEMORY)
315 continue;
316
Dan Williamsb617c522019-11-06 17:43:11 -0800317 if (efi_soft_reserve_enabled() &&
318 (desc->attribute & EFI_MEMORY_SP))
319 continue;
320
Roy Franz7721da42013-09-22 15:45:27 -0700321 if (desc->num_pages < nr_pages)
322 continue;
323
324 start = desc->phys_addr;
Roy Franz5b88a312016-11-12 21:32:29 +0000325 end = start + desc->num_pages * EFI_PAGE_SIZE;
Roy Franz7721da42013-09-22 15:45:27 -0700326
Kairui Song220dd762019-10-29 18:37:54 +0100327 if (start < min)
328 start = min;
Roy Franz7721da42013-09-22 15:45:27 -0700329
330 start = round_up(start, align);
331 if ((start + size) > end)
332 continue;
333
Matt Fleming204b0a12014-03-22 10:09:01 +0000334 status = efi_call_early(allocate_pages,
335 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
336 nr_pages, &start);
Roy Franz7721da42013-09-22 15:45:27 -0700337 if (status == EFI_SUCCESS) {
338 *addr = start;
339 break;
340 }
341 }
342
343 if (i == map_size / desc_size)
344 status = EFI_NOT_FOUND;
345
Matt Fleming204b0a12014-03-22 10:09:01 +0000346 efi_call_early(free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700347fail:
348 return status;
349}
350
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200351void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
352 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700353{
354 unsigned long nr_pages;
355
Roy Franz0e1cadb2013-09-22 15:45:38 -0700356 if (!size)
357 return;
358
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100359 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000360 efi_call_early(free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700361}
362
Lukas Wunner2bd79f32017-01-31 13:21:33 +0000363static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
364 efi_char16_t *filename_16, void **handle,
365 u64 *file_sz)
366{
367 efi_file_handle_t *h, *fh = __fh;
368 efi_file_info_t *info;
369 efi_status_t status;
370 efi_guid_t info_guid = EFI_FILE_INFO_ID;
371 unsigned long info_sz;
372
373 status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
374 EFI_FILE_MODE_READ, (u64)0);
375 if (status != EFI_SUCCESS) {
376 efi_printk(sys_table_arg, "Failed to open file: ");
377 efi_char16_printk(sys_table_arg, filename_16);
378 efi_printk(sys_table_arg, "\n");
379 return status;
380 }
381
382 *handle = h;
383
384 info_sz = 0;
385 status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
386 &info_sz, NULL);
387 if (status != EFI_BUFFER_TOO_SMALL) {
388 efi_printk(sys_table_arg, "Failed to get file info size\n");
389 return status;
390 }
391
392grow:
393 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
394 info_sz, (void **)&info);
395 if (status != EFI_SUCCESS) {
396 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
397 return status;
398 }
399
400 status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
401 &info_sz, info);
402 if (status == EFI_BUFFER_TOO_SMALL) {
403 efi_call_early(free_pool, info);
404 goto grow;
405 }
406
407 *file_sz = info->file_size;
408 efi_call_early(free_pool, info);
409
410 if (status != EFI_SUCCESS)
411 efi_printk(sys_table_arg, "Failed to get initrd info\n");
412
413 return status;
414}
415
416static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
417{
418 return efi_call_proto(efi_file_handle, read, handle, size, addr);
419}
420
421static efi_status_t efi_file_close(void *handle)
422{
423 return efi_call_proto(efi_file_handle, close, handle);
424}
425
Lukas Wunnerc4db9c12018-07-20 10:47:23 +0900426static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
427 efi_loaded_image_t *image,
428 efi_file_handle_t **__fh)
429{
430 efi_file_io_interface_t *io;
431 efi_file_handle_t *fh;
432 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
433 efi_status_t status;
Ard Biesheuvelf958efe2019-12-24 16:10:09 +0100434 void *handle = efi_table_attr(efi_loaded_image, device_handle, image);
Lukas Wunnerc4db9c12018-07-20 10:47:23 +0900435
436 status = efi_call_early(handle_protocol, handle,
437 &fs_proto, (void **)&io);
438 if (status != EFI_SUCCESS) {
439 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
440 return status;
441 }
442
443 status = efi_call_proto(efi_file_io_interface, open_volume, io, &fh);
444 if (status != EFI_SUCCESS)
445 efi_printk(sys_table_arg, "Failed to open volume\n");
446 else
447 *__fh = fh;
448
449 return status;
450}
451
Matt Fleming5a17dae2014-08-05 11:52:11 +0100452/*
453 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
454 * option, e.g. efi=nochunk.
455 *
456 * It should be noted that efi= is parsed in two very different
457 * environments, first in the early boot environment of the EFI boot
458 * stub, and subsequently during the kernel boot.
459 */
Ard Biesheuvel60f38de2017-04-04 17:09:08 +0100460efi_status_t efi_parse_options(char const *cmdline)
Matt Fleming5a17dae2014-08-05 11:52:11 +0100461{
462 char *str;
463
Ard Biesheuvel60f38de2017-04-04 17:09:08 +0100464 str = strstr(cmdline, "nokaslr");
465 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
466 __nokaslr = 1;
Ard Biesheuvelb3879a42017-02-06 11:22:46 +0000467
Ard Biesheuveleeff7d62017-04-04 17:09:09 +0100468 str = strstr(cmdline, "quiet");
469 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
470 __quiet = 1;
471
Ard Biesheuvelb3879a42017-02-06 11:22:46 +0000472 /*
Matt Fleming5a17dae2014-08-05 11:52:11 +0100473 * If no EFI parameters were specified on the cmdline we've got
474 * nothing to do.
475 */
476 str = strstr(cmdline, "efi=");
477 if (!str)
478 return EFI_SUCCESS;
479
480 /* Skip ahead to first argument */
481 str += strlen("efi=");
482
483 /*
484 * Remember, because efi= is also used by the kernel we need to
485 * skip over arguments we don't understand.
486 */
Ard Biesheuvel4c3f14b2017-04-04 17:02:45 +0100487 while (*str && *str != ' ') {
Matt Fleming5a17dae2014-08-05 11:52:11 +0100488 if (!strncmp(str, "nochunk", 7)) {
489 str += strlen("nochunk");
490 __chunk_size = -1UL;
491 }
492
Ard Biesheuvel4e46c2a2019-02-02 10:41:16 +0100493 if (!strncmp(str, "novamap", 7)) {
494 str += strlen("novamap");
495 __novamap = 1;
496 }
497
Dan Williamsb617c522019-11-06 17:43:11 -0800498 if (IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
499 !strncmp(str, "nosoftreserve", 7)) {
500 str += strlen("nosoftreserve");
501 efi_nosoftreserve = 1;
502 }
503
Matt Fleming5a17dae2014-08-05 11:52:11 +0100504 /* Group words together, delimited by "," */
Ard Biesheuvel4c3f14b2017-04-04 17:02:45 +0100505 while (*str && *str != ' ' && *str != ',')
Matt Fleming5a17dae2014-08-05 11:52:11 +0100506 str++;
507
508 if (*str == ',')
509 str++;
510 }
511
512 return EFI_SUCCESS;
513}
Roy Franz7721da42013-09-22 15:45:27 -0700514
515/*
Roy Franz36f89612013-09-22 15:45:40 -0700516 * Check the cmdline for a LILO-style file= arguments.
Roy Franz7721da42013-09-22 15:45:27 -0700517 *
Roy Franz36f89612013-09-22 15:45:40 -0700518 * We only support loading a file from the same filesystem as
519 * the kernel image.
Roy Franz7721da42013-09-22 15:45:27 -0700520 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200521efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
522 efi_loaded_image_t *image,
523 char *cmd_line, char *option_string,
524 unsigned long max_addr,
525 unsigned long *load_addr,
526 unsigned long *load_size)
Roy Franz7721da42013-09-22 15:45:27 -0700527{
Roy Franz36f89612013-09-22 15:45:40 -0700528 struct file_info *files;
529 unsigned long file_addr;
Roy Franz36f89612013-09-22 15:45:40 -0700530 u64 file_size_total;
Leif Lindholm9403e462014-04-04 13:25:46 +0100531 efi_file_handle_t *fh = NULL;
Roy Franz7721da42013-09-22 15:45:27 -0700532 efi_status_t status;
Roy Franz36f89612013-09-22 15:45:40 -0700533 int nr_files;
Roy Franz7721da42013-09-22 15:45:27 -0700534 char *str;
535 int i, j, k;
536
Roy Franz36f89612013-09-22 15:45:40 -0700537 file_addr = 0;
538 file_size_total = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700539
Roy Franz46f45822013-09-22 15:45:39 -0700540 str = cmd_line;
Roy Franz7721da42013-09-22 15:45:27 -0700541
542 j = 0; /* See close_handles */
543
Roy Franz46f45822013-09-22 15:45:39 -0700544 if (!load_addr || !load_size)
545 return EFI_INVALID_PARAMETER;
546
547 *load_addr = 0;
548 *load_size = 0;
549
Roy Franz7721da42013-09-22 15:45:27 -0700550 if (!str || !*str)
551 return EFI_SUCCESS;
552
Roy Franz36f89612013-09-22 15:45:40 -0700553 for (nr_files = 0; *str; nr_files++) {
Roy Franz46f45822013-09-22 15:45:39 -0700554 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700555 if (!str)
556 break;
557
Roy Franz46f45822013-09-22 15:45:39 -0700558 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700559
560 /* Skip any leading slashes */
561 while (*str == '/' || *str == '\\')
562 str++;
563
564 while (*str && *str != ' ' && *str != '\n')
565 str++;
566 }
567
Roy Franz36f89612013-09-22 15:45:40 -0700568 if (!nr_files)
Roy Franz7721da42013-09-22 15:45:27 -0700569 return EFI_SUCCESS;
570
Matt Fleming204b0a12014-03-22 10:09:01 +0000571 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
572 nr_files * sizeof(*files), (void **)&files);
Roy Franz7721da42013-09-22 15:45:27 -0700573 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800574 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
Roy Franz7721da42013-09-22 15:45:27 -0700575 goto fail;
576 }
577
Roy Franz46f45822013-09-22 15:45:39 -0700578 str = cmd_line;
Roy Franz36f89612013-09-22 15:45:40 -0700579 for (i = 0; i < nr_files; i++) {
580 struct file_info *file;
Roy Franz7721da42013-09-22 15:45:27 -0700581 efi_char16_t filename_16[256];
Roy Franz7721da42013-09-22 15:45:27 -0700582 efi_char16_t *p;
Roy Franz7721da42013-09-22 15:45:27 -0700583
Roy Franz46f45822013-09-22 15:45:39 -0700584 str = strstr(str, option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700585 if (!str)
586 break;
587
Roy Franz46f45822013-09-22 15:45:39 -0700588 str += strlen(option_string);
Roy Franz7721da42013-09-22 15:45:27 -0700589
Roy Franz36f89612013-09-22 15:45:40 -0700590 file = &files[i];
Roy Franz7721da42013-09-22 15:45:27 -0700591 p = filename_16;
592
593 /* Skip any leading slashes */
594 while (*str == '/' || *str == '\\')
595 str++;
596
597 while (*str && *str != ' ' && *str != '\n') {
598 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
599 break;
600
601 if (*str == '/') {
602 *p++ = '\\';
Roy Franz4e283082013-09-22 15:45:42 -0700603 str++;
Roy Franz7721da42013-09-22 15:45:27 -0700604 } else {
605 *p++ = *str++;
606 }
607 }
608
609 *p = '\0';
610
611 /* Only open the volume once. */
612 if (!i) {
Lukas Wunnerc4db9c12018-07-20 10:47:23 +0900613 status = efi_open_volume(sys_table_arg, image, &fh);
Matt Fleming54b52d82014-01-10 15:27:14 +0000614 if (status != EFI_SUCCESS)
Roy Franz36f89612013-09-22 15:45:40 -0700615 goto free_files;
Roy Franz7721da42013-09-22 15:45:27 -0700616 }
617
Matt Fleming54b52d82014-01-10 15:27:14 +0000618 status = efi_file_size(sys_table_arg, fh, filename_16,
619 (void **)&file->handle, &file->size);
620 if (status != EFI_SUCCESS)
Roy Franz7721da42013-09-22 15:45:27 -0700621 goto close_handles;
Roy Franz7721da42013-09-22 15:45:27 -0700622
Matt Fleming54b52d82014-01-10 15:27:14 +0000623 file_size_total += file->size;
Roy Franz7721da42013-09-22 15:45:27 -0700624 }
625
Roy Franz36f89612013-09-22 15:45:40 -0700626 if (file_size_total) {
Roy Franz7721da42013-09-22 15:45:27 -0700627 unsigned long addr;
628
629 /*
Roy Franz36f89612013-09-22 15:45:40 -0700630 * Multiple files need to be at consecutive addresses in memory,
631 * so allocate enough memory for all the files. This is used
632 * for loading multiple files.
Roy Franz7721da42013-09-22 15:45:27 -0700633 */
Roy Franz36f89612013-09-22 15:45:40 -0700634 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
635 &file_addr, max_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700636 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800637 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
Roy Franz7721da42013-09-22 15:45:27 -0700638 goto close_handles;
639 }
640
641 /* We've run out of free low memory. */
Roy Franz36f89612013-09-22 15:45:40 -0700642 if (file_addr > max_addr) {
Roy Franzf966ea02013-12-13 11:04:49 -0800643 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700644 status = EFI_INVALID_PARAMETER;
Roy Franz36f89612013-09-22 15:45:40 -0700645 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700646 }
647
Roy Franz36f89612013-09-22 15:45:40 -0700648 addr = file_addr;
649 for (j = 0; j < nr_files; j++) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700650 unsigned long size;
Roy Franz7721da42013-09-22 15:45:27 -0700651
Roy Franz36f89612013-09-22 15:45:40 -0700652 size = files[j].size;
Roy Franz7721da42013-09-22 15:45:27 -0700653 while (size) {
Roy Franz6a5fe772013-09-22 15:45:41 -0700654 unsigned long chunksize;
Ard Biesheuvelb3879a42017-02-06 11:22:46 +0000655
656 if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
Matt Fleming5a17dae2014-08-05 11:52:11 +0100657 chunksize = __chunk_size;
Roy Franz7721da42013-09-22 15:45:27 -0700658 else
659 chunksize = size;
Matt Fleming54b52d82014-01-10 15:27:14 +0000660
Matt Fleming47514c92014-04-10 14:11:45 +0100661 status = efi_file_read(files[j].handle,
Matt Fleming54b52d82014-01-10 15:27:14 +0000662 &chunksize,
663 (void *)addr);
Roy Franz7721da42013-09-22 15:45:27 -0700664 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800665 pr_efi_err(sys_table_arg, "Failed to read file\n");
Roy Franz36f89612013-09-22 15:45:40 -0700666 goto free_file_total;
Roy Franz7721da42013-09-22 15:45:27 -0700667 }
668 addr += chunksize;
669 size -= chunksize;
670 }
671
Matt Fleming47514c92014-04-10 14:11:45 +0100672 efi_file_close(files[j].handle);
Roy Franz7721da42013-09-22 15:45:27 -0700673 }
674
675 }
676
Matt Fleming204b0a12014-03-22 10:09:01 +0000677 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700678
Roy Franz36f89612013-09-22 15:45:40 -0700679 *load_addr = file_addr;
680 *load_size = file_size_total;
Roy Franz7721da42013-09-22 15:45:27 -0700681
682 return status;
683
Roy Franz36f89612013-09-22 15:45:40 -0700684free_file_total:
685 efi_free(sys_table_arg, file_size_total, file_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700686
687close_handles:
688 for (k = j; k < i; k++)
Matt Fleming47514c92014-04-10 14:11:45 +0100689 efi_file_close(files[k].handle);
Roy Franz36f89612013-09-22 15:45:40 -0700690free_files:
Matt Fleming204b0a12014-03-22 10:09:01 +0000691 efi_call_early(free_pool, files);
Roy Franz7721da42013-09-22 15:45:27 -0700692fail:
Roy Franz46f45822013-09-22 15:45:39 -0700693 *load_addr = 0;
694 *load_size = 0;
Roy Franz7721da42013-09-22 15:45:27 -0700695
696 return status;
697}
Roy Franz4a9f3a72013-09-22 15:45:32 -0700698/*
699 * Relocate a kernel image, either compressed or uncompressed.
700 * In the ARM64 case, all kernel images are currently
701 * uncompressed, and as such when we relocate it we need to
702 * allocate additional space for the BSS segment. Any low
703 * memory that this function should avoid needs to be
704 * unavailable in the EFI memory map, as if the preferred
705 * address is not available the lowest available address will
706 * be used.
707 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200708efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
709 unsigned long *image_addr,
710 unsigned long image_size,
711 unsigned long alloc_size,
712 unsigned long preferred_addr,
Kairui Song220dd762019-10-29 18:37:54 +0100713 unsigned long alignment,
714 unsigned long min_addr)
Roy Franzc6866d72013-09-22 15:45:31 -0700715{
Roy Franz4a9f3a72013-09-22 15:45:32 -0700716 unsigned long cur_image_addr;
717 unsigned long new_addr = 0;
Roy Franzc6866d72013-09-22 15:45:31 -0700718 efi_status_t status;
Roy Franz4a9f3a72013-09-22 15:45:32 -0700719 unsigned long nr_pages;
720 efi_physical_addr_t efi_addr = preferred_addr;
721
722 if (!image_addr || !image_size || !alloc_size)
723 return EFI_INVALID_PARAMETER;
724 if (alloc_size < image_size)
725 return EFI_INVALID_PARAMETER;
726
727 cur_image_addr = *image_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700728
729 /*
730 * The EFI firmware loader could have placed the kernel image
Roy Franz4a9f3a72013-09-22 15:45:32 -0700731 * anywhere in memory, but the kernel has restrictions on the
732 * max physical address it can run at. Some architectures
733 * also have a prefered address, so first try to relocate
734 * to the preferred address. If that fails, allocate as low
735 * as possible while respecting the required alignment.
736 */
Ard Biesheuvelcf2b0f12014-11-17 13:46:44 +0100737 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
Matt Fleming204b0a12014-03-22 10:09:01 +0000738 status = efi_call_early(allocate_pages,
739 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
740 nr_pages, &efi_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700741 new_addr = efi_addr;
742 /*
743 * If preferred address allocation failed allocate as low as
Roy Franzc6866d72013-09-22 15:45:31 -0700744 * possible.
745 */
Roy Franzc6866d72013-09-22 15:45:31 -0700746 if (status != EFI_SUCCESS) {
Kairui Song220dd762019-10-29 18:37:54 +0100747 status = efi_low_alloc_above(sys_table_arg, alloc_size,
748 alignment, &new_addr, min_addr);
Roy Franz4a9f3a72013-09-22 15:45:32 -0700749 }
750 if (status != EFI_SUCCESS) {
Roy Franzf966ea02013-12-13 11:04:49 -0800751 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
Roy Franz4a9f3a72013-09-22 15:45:32 -0700752 return status;
Roy Franzc6866d72013-09-22 15:45:31 -0700753 }
754
Roy Franz4a9f3a72013-09-22 15:45:32 -0700755 /*
756 * We know source/dest won't overlap since both memory ranges
757 * have been allocated by UEFI, so we can safely use memcpy.
758 */
759 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
Roy Franzc6866d72013-09-22 15:45:31 -0700760
Roy Franz4a9f3a72013-09-22 15:45:32 -0700761 /* Return the new address of the relocated image. */
762 *image_addr = new_addr;
Roy Franzc6866d72013-09-22 15:45:31 -0700763
764 return status;
765}
Roy Franz5fef3872013-09-22 15:45:33 -0700766
767/*
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500768 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
769 * This overestimates for surrogates, but that is okay.
770 */
771static int efi_utf8_bytes(u16 c)
772{
773 return 1 + (c >= 0x80) + (c >= 0x800);
774}
775
776/*
777 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
778 */
779static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
780{
781 unsigned int c;
782
783 while (n--) {
784 c = *src++;
785 if (n && c >= 0xd800 && c <= 0xdbff &&
786 *src >= 0xdc00 && *src <= 0xdfff) {
787 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
788 src++;
789 n--;
790 }
791 if (c >= 0xd800 && c <= 0xdfff)
792 c = 0xfffd; /* Unmatched surrogate */
793 if (c < 0x80) {
794 *dst++ = c;
795 continue;
796 }
797 if (c < 0x800) {
798 *dst++ = 0xc0 + (c >> 6);
799 goto t1;
800 }
801 if (c < 0x10000) {
802 *dst++ = 0xe0 + (c >> 12);
803 goto t2;
804 }
805 *dst++ = 0xf0 + (c >> 18);
806 *dst++ = 0x80 + ((c >> 12) & 0x3f);
807 t2:
808 *dst++ = 0x80 + ((c >> 6) & 0x3f);
809 t1:
810 *dst++ = 0x80 + (c & 0x3f);
811 }
812
813 return dst;
814}
815
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100816#ifndef MAX_CMDLINE_ADDRESS
817#define MAX_CMDLINE_ADDRESS ULONG_MAX
818#endif
819
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500820/*
Roy Franz5fef3872013-09-22 15:45:33 -0700821 * Convert the unicode UEFI command line to ASCII to pass to kernel.
822 * Size of memory allocated return in *cmd_line_len.
823 * Returns NULL on error.
824 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200825char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
826 efi_loaded_image_t *image,
827 int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700828{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500829 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700830 u8 *s1 = NULL;
831 unsigned long cmdline_addr = 0;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500832 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
833 const u16 *options = image->load_options;
834 int options_bytes = 0; /* UTF-8 bytes */
835 int options_chars = 0; /* UTF-16 chars */
Roy Franz5fef3872013-09-22 15:45:33 -0700836 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700837 u16 zero = 0;
838
839 if (options) {
840 s2 = options;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500841 while (*s2 && *s2 != '\n'
842 && options_chars < load_options_chars) {
843 options_bytes += efi_utf8_bytes(*s2++);
844 options_chars++;
Roy Franz5fef3872013-09-22 15:45:33 -0700845 }
846 }
847
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500848 if (!options_chars) {
Roy Franz5fef3872013-09-22 15:45:33 -0700849 /* No command line options, so return empty string*/
Roy Franz5fef3872013-09-22 15:45:33 -0700850 options = &zero;
851 }
852
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500853 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100854
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +0100855 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
856 &cmdline_addr, MAX_CMDLINE_ADDRESS);
Roy Franz5fef3872013-09-22 15:45:33 -0700857 if (status != EFI_SUCCESS)
858 return NULL;
859
860 s1 = (u8 *)cmdline_addr;
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500861 s2 = (const u16 *)options;
Roy Franz5fef3872013-09-22 15:45:33 -0700862
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500863 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
Roy Franz5fef3872013-09-22 15:45:33 -0700864 *s1 = '\0';
865
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500866 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700867 return (char *)cmdline_addr;
868}
Jeffrey Hugofc077162016-08-29 14:38:52 -0600869
870/*
871 * Handle calling ExitBootServices according to the requirements set out by the
872 * spec. Obtains the current memory map, and returns that info after calling
873 * ExitBootServices. The client must specify a function to perform any
874 * processing of the memory map data prior to ExitBootServices. A client
875 * specific structure may be passed to the function via priv. The client
876 * function may be called multiple times.
877 */
878efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
879 void *handle,
880 struct efi_boot_memmap *map,
881 void *priv,
882 efi_exit_boot_map_processing priv_func)
883{
884 efi_status_t status;
885
886 status = efi_get_memory_map(sys_table_arg, map);
887
888 if (status != EFI_SUCCESS)
889 goto fail;
890
891 status = priv_func(sys_table_arg, map, priv);
892 if (status != EFI_SUCCESS)
893 goto free_map;
894
895 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
896
897 if (status == EFI_INVALID_PARAMETER) {
898 /*
899 * The memory map changed between efi_get_memory_map() and
900 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
901 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
902 * updated map, and try again. The spec implies one retry
903 * should be sufficent, which is confirmed against the EDK2
904 * implementation. Per the spec, we can only invoke
905 * get_memory_map() and exit_boot_services() - we cannot alloc
906 * so efi_get_memory_map() cannot be used, and we must reuse
907 * the buffer. For all practical purposes, the headroom in the
908 * buffer should account for any changes in the map so the call
909 * to get_memory_map() is expected to succeed here.
910 */
911 *map->map_size = *map->buff_size;
912 status = efi_call_early(get_memory_map,
913 map->map_size,
914 *map->map,
915 map->key_ptr,
916 map->desc_size,
917 map->desc_ver);
918
919 /* exit_boot_services() was called, thus cannot free */
920 if (status != EFI_SUCCESS)
921 goto fail;
922
923 status = priv_func(sys_table_arg, map, priv);
924 /* exit_boot_services() was called, thus cannot free */
925 if (status != EFI_SUCCESS)
926 goto fail;
927
928 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
929 }
930
931 /* exit_boot_services() was called, thus cannot free */
932 if (status != EFI_SUCCESS)
933 goto fail;
934
935 return EFI_SUCCESS;
936
937free_map:
938 efi_call_early(free_pool, *map->map);
939fail:
940 return status;
941}
Matthew Garrett82d736a2019-06-07 13:51:46 -0700942
943void *get_efi_config_table(efi_system_table_t *sys_table, efi_guid_t guid)
944{
Ard Biesheuvelf958efe2019-12-24 16:10:09 +0100945 unsigned long tables = efi_table_attr(efi_system_table, tables, sys_table);
946 int nr_tables = efi_table_attr(efi_system_table, nr_tables, sys_table);
947 int i;
948
949 for (i = 0; i < nr_tables; i++) {
950 efi_config_table_t *t = (void *)tables;
951
952 if (efi_guidcmp(t->guid, guid) == 0)
953 return efi_table_attr(efi_config_table, table, t);
954
955 tables += efi_is_native() ? sizeof(efi_config_table_t)
956 : sizeof(efi_config_table_32_t);
957 }
958 return NULL;
Matthew Garrett82d736a2019-06-07 13:51:46 -0700959}