blob: f53652a3a1060906aeefc6936f4f70fdc1adf88e [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
Arvind Sankar2c7d1e32020-05-18 15:06:56 -040010#include <stdarg.h>
11
Arvind Sankar80b1bfe2020-05-20 20:29:21 -040012#include <linux/ctype.h>
Ard Biesheuvelbd669472014-07-02 14:54:42 +020013#include <linux/efi.h>
Arvind Sankarfd0528a2020-05-18 15:06:55 -040014#include <linux/kernel.h>
Arvind Sankar23d5b732020-05-20 19:07:54 +020015#include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
Ard Biesheuvelbd669472014-07-02 14:54:42 +020016#include <asm/efi.h>
Arvind Sankar80b1bfe2020-05-20 20:29:21 -040017#include <asm/setup.h>
Ard Biesheuvelbd669472014-07-02 14:54:42 +020018
19#include "efistub.h"
20
Ard Biesheuvel980771f2020-04-16 18:45:24 +020021bool efi_nochunk;
Will Deacon7c116db2020-07-09 21:48:41 +010022bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
Ard Biesheuvel980771f2020-04-16 18:45:24 +020023bool efi_noinitrd;
Arvind Sankar23d5b732020-05-20 19:07:54 +020024int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
Ard Biesheuvel980771f2020-04-16 18:45:24 +020025bool efi_novamap;
26
Arvind Sankar54439372020-04-16 11:12:27 -040027static bool efi_nosoftreserve;
28static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
Ard Biesheuvel60f38de2017-04-04 17:09:08 +010029
Dan Williamsb617c522019-11-06 17:43:11 -080030bool __pure __efi_soft_reserve_enabled(void)
31{
32 return !efi_nosoftreserve;
33}
Ard Biesheuvel60f38de2017-04-04 17:09:08 +010034
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +020035/**
36 * efi_char16_puts() - Write a UCS-2 encoded string to the console
37 * @str: UCS-2 encoded string
38 */
Arvind Sankarcb8c90a2020-05-18 15:06:54 -040039void efi_char16_puts(efi_char16_t *str)
40{
41 efi_call_proto(efi_table_attr(efi_system_table, con_out),
42 output_string, str);
43}
44
Arvind Sankar4b75bd32020-05-18 15:07:13 -040045static
46u32 utf8_to_utf32(const u8 **s8)
47{
48 u32 c32;
49 u8 c0, cx;
50 size_t clen, i;
51
52 c0 = cx = *(*s8)++;
53 /*
54 * The position of the most-significant 0 bit gives us the length of
55 * a multi-octet encoding.
56 */
57 for (clen = 0; cx & 0x80; ++clen)
58 cx <<= 1;
59 /*
60 * If the 0 bit is in position 8, this is a valid single-octet
61 * encoding. If the 0 bit is in position 7 or positions 1-3, the
62 * encoding is invalid.
63 * In either case, we just return the first octet.
64 */
65 if (clen < 2 || clen > 4)
66 return c0;
67 /* Get the bits from the first octet. */
68 c32 = cx >> clen--;
69 for (i = 0; i < clen; ++i) {
70 /* Trailing octets must have 10 in most significant bits. */
71 cx = (*s8)[i] ^ 0x80;
72 if (cx & 0xc0)
73 return c0;
74 c32 = (c32 << 6) | cx;
75 }
76 /*
77 * Check for validity:
78 * - The character must be in the Unicode range.
79 * - It must not be a surrogate.
80 * - It must be encoded using the correct number of octets.
81 */
82 if (c32 > 0x10ffff ||
83 (c32 & 0xf800) == 0xd800 ||
84 clen != (c32 >= 0x80) + (c32 >= 0x800) + (c32 >= 0x10000))
85 return c0;
86 *s8 += clen;
87 return c32;
88}
89
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +020090/**
91 * efi_puts() - Write a UTF-8 encoded string to the console
92 * @str: UTF-8 encoded string
93 */
Arvind Sankarcb8c90a2020-05-18 15:06:54 -040094void efi_puts(const char *str)
Roy Franz7721da42013-09-22 15:45:27 -070095{
Arvind Sankarfd0528a2020-05-18 15:06:55 -040096 efi_char16_t buf[128];
97 size_t pos = 0, lim = ARRAY_SIZE(buf);
Arvind Sankar4b75bd32020-05-18 15:07:13 -040098 const u8 *s8 = (const u8 *)str;
99 u32 c32;
Roy Franz7721da42013-09-22 15:45:27 -0700100
Arvind Sankar4b75bd32020-05-18 15:07:13 -0400101 while (*s8) {
102 if (*s8 == '\n')
Arvind Sankarfd0528a2020-05-18 15:06:55 -0400103 buf[pos++] = L'\r';
Arvind Sankar4b75bd32020-05-18 15:07:13 -0400104 c32 = utf8_to_utf32(&s8);
105 if (c32 < 0x10000) {
106 /* Characters in plane 0 use a single word. */
107 buf[pos++] = c32;
108 } else {
109 /*
110 * Characters in other planes encode into a surrogate
111 * pair.
112 */
113 buf[pos++] = (0xd800 - (0x10000 >> 10)) + (c32 >> 10);
114 buf[pos++] = 0xdc00 + (c32 & 0x3ff);
115 }
116 if (*s8 == '\0' || pos >= lim - 2) {
Arvind Sankarfd0528a2020-05-18 15:06:55 -0400117 buf[pos] = L'\0';
118 efi_char16_puts(buf);
119 pos = 0;
120 }
Roy Franz7721da42013-09-22 15:45:27 -0700121 }
122}
123
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200124/**
125 * efi_printk() - Print a kernel message
126 * @fmt: format string
127 *
128 * The first letter of the format string is used to determine the logging level
129 * of the message. If the level is less then the current EFI logging level, the
130 * message is suppressed. The message will be truncated to 255 bytes.
131 *
132 * Return: number of printed characters
133 */
Arvind Sankar2c7d1e32020-05-18 15:06:56 -0400134int efi_printk(const char *fmt, ...)
135{
136 char printf_buf[256];
137 va_list args;
138 int printed;
Arvind Sankar23d5b732020-05-20 19:07:54 +0200139 int loglevel = printk_get_level(fmt);
140
141 switch (loglevel) {
142 case '0' ... '9':
143 loglevel -= '0';
144 break;
145 default:
146 /*
147 * Use loglevel -1 for cases where we just want to print to
148 * the screen.
149 */
150 loglevel = -1;
151 break;
152 }
153
154 if (loglevel >= efi_loglevel)
155 return 0;
156
157 if (loglevel >= 0)
158 efi_puts("EFI stub: ");
159
160 fmt = printk_skip_level(fmt);
Arvind Sankar2c7d1e32020-05-18 15:06:56 -0400161
162 va_start(args, fmt);
Arvind Sankar8fb331e2020-05-18 15:07:08 -0400163 printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
Arvind Sankar2c7d1e32020-05-18 15:06:56 -0400164 va_end(args);
165
166 efi_puts(printf_buf);
Arvind Sankar8fb331e2020-05-18 15:07:08 -0400167 if (printed >= sizeof(printf_buf)) {
168 efi_puts("[Message truncated]\n");
169 return -1;
170 }
Arvind Sankar2c7d1e32020-05-18 15:06:56 -0400171
172 return printed;
173}
174
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200175/**
176 * efi_parse_options() - Parse EFI command line options
177 * @cmdline: kernel command line
178 *
179 * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
Matt Fleming5a17dae2014-08-05 11:52:11 +0100180 * option, e.g. efi=nochunk.
181 *
182 * It should be noted that efi= is parsed in two very different
183 * environments, first in the early boot environment of the EFI boot
184 * stub, and subsequently during the kernel boot.
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200185 *
186 * Return: status code
Matt Fleming5a17dae2014-08-05 11:52:11 +0100187 */
Ard Biesheuvel60f38de2017-04-04 17:09:08 +0100188efi_status_t efi_parse_options(char const *cmdline)
Matt Fleming5a17dae2014-08-05 11:52:11 +0100189{
Arvind Sankara37ca6a2020-07-29 15:33:00 -0400190 size_t len;
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100191 efi_status_t status;
192 char *str, *buf;
Matt Fleming5a17dae2014-08-05 11:52:11 +0100193
Arvind Sankara37ca6a2020-07-29 15:33:00 -0400194 if (!cmdline)
195 return EFI_SUCCESS;
196
197 len = strlen(cmdline) + 1;
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100198 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
199 if (status != EFI_SUCCESS)
200 return status;
Ard Biesheuvelb3879a42017-02-06 11:22:46 +0000201
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100202 str = skip_spaces(memcpy(buf, cmdline, len));
Ard Biesheuveleeff7d62017-04-04 17:09:09 +0100203
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100204 while (*str) {
205 char *param, *val;
Matt Fleming5a17dae2014-08-05 11:52:11 +0100206
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100207 str = next_arg(str, &param, &val);
Arvind Sankar1fd97172020-07-25 11:59:16 -0400208 if (!val && !strcmp(param, "--"))
209 break;
Matt Fleming5a17dae2014-08-05 11:52:11 +0100210
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100211 if (!strcmp(param, "nokaslr")) {
212 efi_nokaslr = true;
213 } else if (!strcmp(param, "quiet")) {
Arvind Sankar23d5b732020-05-20 19:07:54 +0200214 efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000215 } else if (!strcmp(param, "noinitrd")) {
216 efi_noinitrd = true;
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100217 } else if (!strcmp(param, "efi") && val) {
218 efi_nochunk = parse_option_str(val, "nochunk");
219 efi_novamap = parse_option_str(val, "novamap");
220
221 efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
222 parse_option_str(val, "nosoftreserve");
223
224 if (parse_option_str(val, "disable_early_pci_dma"))
225 efi_disable_pci_dma = true;
226 if (parse_option_str(val, "no_disable_early_pci_dma"))
227 efi_disable_pci_dma = false;
Arvind Sankar23d5b732020-05-20 19:07:54 +0200228 if (parse_option_str(val, "debug"))
229 efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
Arvind Sankarfffb6802020-03-19 22:00:25 -0400230 } else if (!strcmp(param, "video") &&
231 val && strstarts(val, "efifb:")) {
232 efi_parse_option_graphics(val + strlen("efifb:"));
Matt Fleming5a17dae2014-08-05 11:52:11 +0100233 }
Matt Fleming5a17dae2014-08-05 11:52:11 +0100234 }
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100235 efi_bs_call(free_pool, buf);
Matt Fleming5a17dae2014-08-05 11:52:11 +0100236 return EFI_SUCCESS;
237}
Roy Franz7721da42013-09-22 15:45:27 -0700238
239/*
Roy Franz5fef3872013-09-22 15:45:33 -0700240 * Convert the unicode UEFI command line to ASCII to pass to kernel.
241 * Size of memory allocated return in *cmd_line_len.
242 * Returns NULL on error.
243 */
Ard Biesheuvel27cd5512020-05-19 10:43:01 +0200244char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
Roy Franz5fef3872013-09-22 15:45:33 -0700245{
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500246 const u16 *s2;
Roy Franz5fef3872013-09-22 15:45:33 -0700247 unsigned long cmdline_addr = 0;
Arvind Sankar04b24402020-05-18 15:07:16 -0400248 int options_chars = efi_table_attr(image, load_options_size) / 2;
Ard Biesheuvelf7b85b32020-02-14 14:29:21 +0100249 const u16 *options = efi_table_attr(image, load_options);
Arvind Sankar80b1bfe2020-05-20 20:29:21 -0400250 int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */
251 bool in_quote = false;
Roy Franz5fef3872013-09-22 15:45:33 -0700252 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700253
254 if (options) {
255 s2 = options;
Arvind Sankar80b1bfe2020-05-20 20:29:21 -0400256 while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
Arvind Sankar15c316b2020-05-18 15:07:15 -0400257 u16 c = *s2++;
258
Arvind Sankar80b1bfe2020-05-20 20:29:21 -0400259 if (c < 0x80) {
260 if (c == L'\0' || c == L'\n')
261 break;
262 if (c == L'"')
263 in_quote = !in_quote;
264 else if (!in_quote && isspace((char)c))
265 safe_options_bytes = options_bytes;
266
267 options_bytes++;
268 continue;
269 }
270
Arvind Sankar15c316b2020-05-18 15:07:15 -0400271 /*
272 * Get the number of UTF-8 bytes corresponding to a
273 * UTF-16 character.
274 * The first part handles everything in the BMP.
275 */
Arvind Sankar80b1bfe2020-05-20 20:29:21 -0400276 options_bytes += 2 + (c >= 0x800);
Arvind Sankar15c316b2020-05-18 15:07:15 -0400277 /*
278 * Add one more byte for valid surrogate pairs. Invalid
279 * surrogates will be replaced with 0xfffd and take up
280 * only 3 bytes.
281 */
282 if ((c & 0xfc00) == 0xd800) {
283 /*
284 * If the very last word is a high surrogate,
285 * we must ignore it since we can't access the
286 * low surrogate.
287 */
Arvind Sankar04b24402020-05-18 15:07:16 -0400288 if (!options_chars) {
Arvind Sankar15c316b2020-05-18 15:07:15 -0400289 options_bytes -= 3;
Arvind Sankar15c316b2020-05-18 15:07:15 -0400290 } else if ((*s2 & 0xfc00) == 0xdc00) {
291 options_bytes++;
Arvind Sankar04b24402020-05-18 15:07:16 -0400292 options_chars--;
Arvind Sankar15c316b2020-05-18 15:07:15 -0400293 s2++;
294 }
295 }
Roy Franz5fef3872013-09-22 15:45:33 -0700296 }
Arvind Sankar80b1bfe2020-05-20 20:29:21 -0400297 if (options_bytes >= COMMAND_LINE_SIZE) {
298 options_bytes = safe_options_bytes;
299 efi_err("Command line is too long: truncated to %d bytes\n",
300 options_bytes);
301 }
Roy Franz5fef3872013-09-22 15:45:33 -0700302 }
303
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500304 options_bytes++; /* NUL termination */
Leif Lindholm9403e462014-04-04 13:25:46 +0100305
Ard Biesheuvel27cd5512020-05-19 10:43:01 +0200306 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
307 (void **)&cmdline_addr);
Roy Franz5fef3872013-09-22 15:45:33 -0700308 if (status != EFI_SUCCESS)
309 return NULL;
310
Arvind Sankar04b24402020-05-18 15:07:16 -0400311 snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
312 options_bytes - 1, options);
Roy Franz5fef3872013-09-22 15:45:33 -0700313
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500314 *cmd_line_len = options_bytes;
Roy Franz5fef3872013-09-22 15:45:33 -0700315 return (char *)cmdline_addr;
316}
Jeffrey Hugofc077162016-08-29 14:38:52 -0600317
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200318/**
319 * efi_exit_boot_services() - Exit boot services
320 * @handle: handle of the exiting image
321 * @map: pointer to receive the memory map
322 * @priv: argument to be passed to @priv_func
323 * @priv_func: function to process the memory map before exiting boot services
324 *
Jeffrey Hugofc077162016-08-29 14:38:52 -0600325 * Handle calling ExitBootServices according to the requirements set out by the
326 * spec. Obtains the current memory map, and returns that info after calling
327 * ExitBootServices. The client must specify a function to perform any
328 * processing of the memory map data prior to ExitBootServices. A client
329 * specific structure may be passed to the function via priv. The client
330 * function may be called multiple times.
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200331 *
332 * Return: status code
Jeffrey Hugofc077162016-08-29 14:38:52 -0600333 */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100334efi_status_t efi_exit_boot_services(void *handle,
Jeffrey Hugofc077162016-08-29 14:38:52 -0600335 struct efi_boot_memmap *map,
336 void *priv,
337 efi_exit_boot_map_processing priv_func)
338{
339 efi_status_t status;
340
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100341 status = efi_get_memory_map(map);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600342
343 if (status != EFI_SUCCESS)
344 goto fail;
345
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100346 status = priv_func(map, priv);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600347 if (status != EFI_SUCCESS)
348 goto free_map;
349
Matthew Garrett4444f852020-01-03 12:39:50 +0100350 if (efi_disable_pci_dma)
351 efi_pci_disable_bridge_busmaster();
352
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100353 status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600354
355 if (status == EFI_INVALID_PARAMETER) {
356 /*
357 * The memory map changed between efi_get_memory_map() and
358 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
359 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
360 * updated map, and try again. The spec implies one retry
361 * should be sufficent, which is confirmed against the EDK2
362 * implementation. Per the spec, we can only invoke
363 * get_memory_map() and exit_boot_services() - we cannot alloc
364 * so efi_get_memory_map() cannot be used, and we must reuse
365 * the buffer. For all practical purposes, the headroom in the
366 * buffer should account for any changes in the map so the call
367 * to get_memory_map() is expected to succeed here.
368 */
369 *map->map_size = *map->buff_size;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100370 status = efi_bs_call(get_memory_map,
371 map->map_size,
372 *map->map,
373 map->key_ptr,
374 map->desc_size,
375 map->desc_ver);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600376
377 /* exit_boot_services() was called, thus cannot free */
378 if (status != EFI_SUCCESS)
379 goto fail;
380
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100381 status = priv_func(map, priv);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600382 /* exit_boot_services() was called, thus cannot free */
383 if (status != EFI_SUCCESS)
384 goto fail;
385
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100386 status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600387 }
388
389 /* exit_boot_services() was called, thus cannot free */
390 if (status != EFI_SUCCESS)
391 goto fail;
392
393 return EFI_SUCCESS;
394
395free_map:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100396 efi_bs_call(free_pool, *map->map);
Jeffrey Hugofc077162016-08-29 14:38:52 -0600397fail:
398 return status;
399}
Matthew Garrett82d736a2019-06-07 13:51:46 -0700400
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200401/**
402 * get_efi_config_table() - retrieve UEFI configuration table
403 * @guid: GUID of the configuration table to be retrieved
404 * Return: pointer to the configuration table or NULL
405 */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100406void *get_efi_config_table(efi_guid_t guid)
Matthew Garrett82d736a2019-06-07 13:51:46 -0700407{
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200408 unsigned long tables = efi_table_attr(efi_system_table, tables);
409 int nr_tables = efi_table_attr(efi_system_table, nr_tables);
Ard Biesheuvelf958efe2019-12-24 16:10:09 +0100410 int i;
411
412 for (i = 0; i < nr_tables; i++) {
413 efi_config_table_t *t = (void *)tables;
414
415 if (efi_guidcmp(t->guid, guid) == 0)
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +0100416 return efi_table_attr(t, table);
Ard Biesheuvelf958efe2019-12-24 16:10:09 +0100417
418 tables += efi_is_native() ? sizeof(efi_config_table_t)
419 : sizeof(efi_config_table_32_t);
420 }
421 return NULL;
Matthew Garrett82d736a2019-06-07 13:51:46 -0700422}
Ard Biesheuveldc29da12019-12-24 16:10:16 +0100423
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000424/*
425 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
426 * for the firmware or bootloader to expose the initrd data directly to the stub
427 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
428 * very easy to implement. It is a simple Linux initrd specific conduit between
429 * kernel and firmware, allowing us to put the EFI stub (being part of the
430 * kernel) in charge of where and when to load the initrd, while leaving it up
431 * to the firmware to decide whether it needs to expose its filesystem hierarchy
432 * via EFI protocols.
433 */
434static const struct {
435 struct efi_vendor_dev_path vendor;
436 struct efi_generic_dev_path end;
437} __packed initrd_dev_path = {
438 {
439 {
440 EFI_DEV_MEDIA,
441 EFI_DEV_MEDIA_VENDOR,
442 sizeof(struct efi_vendor_dev_path),
443 },
444 LINUX_EFI_INITRD_MEDIA_GUID
445 }, {
446 EFI_DEV_END_PATH,
447 EFI_DEV_END_ENTIRE,
448 sizeof(struct efi_generic_dev_path)
449 }
450};
451
452/**
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200453 * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000454 * @load_addr: pointer to store the address where the initrd was loaded
455 * @load_size: pointer to store the size of the loaded initrd
456 * @max: upper limit for the initrd memory allocation
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200457 *
458 * Return:
459 * * %EFI_SUCCESS if the initrd was loaded successfully, in which
460 * case @load_addr and @load_size are assigned accordingly
461 * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
462 * * %EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
463 * * %EFI_OUT_OF_RESOURCES if memory allocation failed
464 * * %EFI_LOAD_ERROR in all other cases
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000465 */
Arvind Sankarf61900f2020-04-30 14:28:41 -0400466static
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000467efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
468 unsigned long *load_size,
469 unsigned long max)
470{
471 efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
472 efi_device_path_protocol_t *dp;
473 efi_load_file2_protocol_t *lf2;
474 unsigned long initrd_addr;
475 unsigned long initrd_size;
476 efi_handle_t handle;
477 efi_status_t status;
478
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000479 dp = (efi_device_path_protocol_t *)&initrd_dev_path;
480 status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
481 if (status != EFI_SUCCESS)
482 return status;
483
484 status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
485 (void **)&lf2);
486 if (status != EFI_SUCCESS)
487 return status;
488
489 status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL);
490 if (status != EFI_BUFFER_TOO_SMALL)
491 return EFI_LOAD_ERROR;
492
493 status = efi_allocate_pages(initrd_size, &initrd_addr, max);
494 if (status != EFI_SUCCESS)
495 return status;
496
497 status = efi_call_proto(lf2, load_file, dp, false, &initrd_size,
498 (void *)initrd_addr);
499 if (status != EFI_SUCCESS) {
500 efi_free(initrd_size, initrd_addr);
501 return EFI_LOAD_ERROR;
502 }
503
504 *load_addr = initrd_addr;
505 *load_size = initrd_size;
506 return EFI_SUCCESS;
507}
Arvind Sankarf61900f2020-04-30 14:28:41 -0400508
509static
510efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
511 unsigned long *load_addr,
512 unsigned long *load_size,
513 unsigned long soft_limit,
514 unsigned long hard_limit)
515{
516 if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
517 (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL))) {
518 *load_addr = *load_size = 0;
519 return EFI_SUCCESS;
520 }
521
522 return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
523 soft_limit, hard_limit,
524 load_addr, load_size);
525}
526
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200527/**
528 * efi_load_initrd() - Load initial RAM disk
529 * @image: EFI loaded image protocol
530 * @load_addr: pointer to loaded initrd
531 * @load_size: size of loaded initrd
532 * @soft_limit: preferred size of allocated memory for loading the initrd
533 * @hard_limit: minimum size of allocated memory
534 *
535 * Return: status code
536 */
Arvind Sankarf61900f2020-04-30 14:28:41 -0400537efi_status_t efi_load_initrd(efi_loaded_image_t *image,
538 unsigned long *load_addr,
539 unsigned long *load_size,
540 unsigned long soft_limit,
541 unsigned long hard_limit)
542{
543 efi_status_t status;
544
545 if (!load_addr || !load_size)
546 return EFI_INVALID_PARAMETER;
547
548 status = efi_load_initrd_dev_path(load_addr, load_size, hard_limit);
549 if (status == EFI_SUCCESS) {
550 efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
551 } else if (status == EFI_NOT_FOUND) {
552 status = efi_load_initrd_cmdline(image, load_addr, load_size,
553 soft_limit, hard_limit);
554 if (status == EFI_SUCCESS && *load_size > 0)
555 efi_info("Loaded initrd from command line option\n");
556 }
557
558 return status;
559}
Arvind Sankar14c574f2020-05-18 15:07:11 -0400560
Heinrich Schuchardt8c0a8392020-06-16 01:42:31 +0200561/**
562 * efi_wait_for_key() - Wait for key stroke
563 * @usec: number of microseconds to wait for key stroke
564 * @key: key entered
565 *
566 * Wait for up to @usec microseconds for a key stroke.
567 *
568 * Return: status code, EFI_SUCCESS if key received
569 */
Arvind Sankar14c574f2020-05-18 15:07:11 -0400570efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
571{
572 efi_event_t events[2], timer;
573 unsigned long index;
574 efi_simple_text_input_protocol_t *con_in;
575 efi_status_t status;
576
577 con_in = efi_table_attr(efi_system_table, con_in);
578 if (!con_in)
579 return EFI_UNSUPPORTED;
580 efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
581
582 status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
583 if (status != EFI_SUCCESS)
584 return status;
585
586 status = efi_bs_call(set_timer, timer, EfiTimerRelative,
587 EFI_100NSEC_PER_USEC * usec);
588 if (status != EFI_SUCCESS)
589 return status;
590 efi_set_event_at(events, 1, timer);
591
592 status = efi_bs_call(wait_for_event, 2, events, &index);
593 if (status == EFI_SUCCESS) {
594 if (index == 0)
595 status = efi_call_proto(con_in, read_keystroke, key);
596 else
597 status = EFI_TIMEOUT;
598 }
599
600 efi_bs_call(close_event, timer);
601
602 return status;
603}