efi-stub-helper.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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
  8. */
  9. #include <linux/stdarg.h>
  10. #include <linux/ctype.h>
  11. #include <linux/efi.h>
  12. #include <linux/kernel.h>
  13. #include <linux/printk.h> /* For CONSOLE_LOGLEVEL_* */
  14. #include <asm/efi.h>
  15. #include <asm/setup.h>
  16. #include "efistub.h"
  17. bool efi_nochunk;
  18. bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
  19. int efi_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  20. bool efi_novamap;
  21. static bool efi_noinitrd;
  22. static bool efi_nosoftreserve;
  23. static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
  24. bool __pure __efi_soft_reserve_enabled(void)
  25. {
  26. return !efi_nosoftreserve;
  27. }
  28. /**
  29. * efi_char16_puts() - Write a UCS-2 encoded string to the console
  30. * @str: UCS-2 encoded string
  31. */
  32. void efi_char16_puts(efi_char16_t *str)
  33. {
  34. efi_call_proto(efi_table_attr(efi_system_table, con_out),
  35. output_string, str);
  36. }
  37. static
  38. u32 utf8_to_utf32(const u8 **s8)
  39. {
  40. u32 c32;
  41. u8 c0, cx;
  42. size_t clen, i;
  43. c0 = cx = *(*s8)++;
  44. /*
  45. * The position of the most-significant 0 bit gives us the length of
  46. * a multi-octet encoding.
  47. */
  48. for (clen = 0; cx & 0x80; ++clen)
  49. cx <<= 1;
  50. /*
  51. * If the 0 bit is in position 8, this is a valid single-octet
  52. * encoding. If the 0 bit is in position 7 or positions 1-3, the
  53. * encoding is invalid.
  54. * In either case, we just return the first octet.
  55. */
  56. if (clen < 2 || clen > 4)
  57. return c0;
  58. /* Get the bits from the first octet. */
  59. c32 = cx >> clen--;
  60. for (i = 0; i < clen; ++i) {
  61. /* Trailing octets must have 10 in most significant bits. */
  62. cx = (*s8)[i] ^ 0x80;
  63. if (cx & 0xc0)
  64. return c0;
  65. c32 = (c32 << 6) | cx;
  66. }
  67. /*
  68. * Check for validity:
  69. * - The character must be in the Unicode range.
  70. * - It must not be a surrogate.
  71. * - It must be encoded using the correct number of octets.
  72. */
  73. if (c32 > 0x10ffff ||
  74. (c32 & 0xf800) == 0xd800 ||
  75. clen != (c32 >= 0x80) + (c32 >= 0x800) + (c32 >= 0x10000))
  76. return c0;
  77. *s8 += clen;
  78. return c32;
  79. }
  80. /**
  81. * efi_puts() - Write a UTF-8 encoded string to the console
  82. * @str: UTF-8 encoded string
  83. */
  84. void efi_puts(const char *str)
  85. {
  86. efi_char16_t buf[128];
  87. size_t pos = 0, lim = ARRAY_SIZE(buf);
  88. const u8 *s8 = (const u8 *)str;
  89. u32 c32;
  90. while (*s8) {
  91. if (*s8 == '\n')
  92. buf[pos++] = L'\r';
  93. c32 = utf8_to_utf32(&s8);
  94. if (c32 < 0x10000) {
  95. /* Characters in plane 0 use a single word. */
  96. buf[pos++] = c32;
  97. } else {
  98. /*
  99. * Characters in other planes encode into a surrogate
  100. * pair.
  101. */
  102. buf[pos++] = (0xd800 - (0x10000 >> 10)) + (c32 >> 10);
  103. buf[pos++] = 0xdc00 + (c32 & 0x3ff);
  104. }
  105. if (*s8 == '\0' || pos >= lim - 2) {
  106. buf[pos] = L'\0';
  107. efi_char16_puts(buf);
  108. pos = 0;
  109. }
  110. }
  111. }
  112. /**
  113. * efi_printk() - Print a kernel message
  114. * @fmt: format string
  115. *
  116. * The first letter of the format string is used to determine the logging level
  117. * of the message. If the level is less then the current EFI logging level, the
  118. * message is suppressed. The message will be truncated to 255 bytes.
  119. *
  120. * Return: number of printed characters
  121. */
  122. int efi_printk(const char *fmt, ...)
  123. {
  124. char printf_buf[256];
  125. va_list args;
  126. int printed;
  127. int loglevel = printk_get_level(fmt);
  128. switch (loglevel) {
  129. case '0' ... '9':
  130. loglevel -= '0';
  131. break;
  132. default:
  133. /*
  134. * Use loglevel -1 for cases where we just want to print to
  135. * the screen.
  136. */
  137. loglevel = -1;
  138. break;
  139. }
  140. if (loglevel >= efi_loglevel)
  141. return 0;
  142. if (loglevel >= 0)
  143. efi_puts("EFI stub: ");
  144. fmt = printk_skip_level(fmt);
  145. va_start(args, fmt);
  146. printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
  147. va_end(args);
  148. efi_puts(printf_buf);
  149. if (printed >= sizeof(printf_buf)) {
  150. efi_puts("[Message truncated]\n");
  151. return -1;
  152. }
  153. return printed;
  154. }
  155. /**
  156. * efi_parse_options() - Parse EFI command line options
  157. * @cmdline: kernel command line
  158. *
  159. * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
  160. * option, e.g. efi=nochunk.
  161. *
  162. * It should be noted that efi= is parsed in two very different
  163. * environments, first in the early boot environment of the EFI boot
  164. * stub, and subsequently during the kernel boot.
  165. *
  166. * Return: status code
  167. */
  168. efi_status_t efi_parse_options(char const *cmdline)
  169. {
  170. size_t len;
  171. efi_status_t status;
  172. char *str, *buf;
  173. if (!cmdline)
  174. return EFI_SUCCESS;
  175. len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
  176. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
  177. if (status != EFI_SUCCESS)
  178. return status;
  179. memcpy(buf, cmdline, len - 1);
  180. buf[len - 1] = '\0';
  181. str = skip_spaces(buf);
  182. while (*str) {
  183. char *param, *val;
  184. str = next_arg(str, &param, &val);
  185. if (!val && !strcmp(param, "--"))
  186. break;
  187. if (!strcmp(param, "nokaslr")) {
  188. efi_nokaslr = true;
  189. } else if (!strcmp(param, "quiet")) {
  190. efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
  191. } else if (!strcmp(param, "noinitrd")) {
  192. efi_noinitrd = true;
  193. } else if (!strcmp(param, "efi") && val) {
  194. efi_nochunk = parse_option_str(val, "nochunk");
  195. efi_novamap |= parse_option_str(val, "novamap");
  196. efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
  197. parse_option_str(val, "nosoftreserve");
  198. if (parse_option_str(val, "disable_early_pci_dma"))
  199. efi_disable_pci_dma = true;
  200. if (parse_option_str(val, "no_disable_early_pci_dma"))
  201. efi_disable_pci_dma = false;
  202. if (parse_option_str(val, "debug"))
  203. efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  204. } else if (!strcmp(param, "video") &&
  205. val && strstarts(val, "efifb:")) {
  206. efi_parse_option_graphics(val + strlen("efifb:"));
  207. }
  208. }
  209. efi_bs_call(free_pool, buf);
  210. return EFI_SUCCESS;
  211. }
  212. /*
  213. * The EFI_LOAD_OPTION descriptor has the following layout:
  214. * u32 Attributes;
  215. * u16 FilePathListLength;
  216. * u16 Description[];
  217. * efi_device_path_protocol_t FilePathList[];
  218. * u8 OptionalData[];
  219. *
  220. * This function validates and unpacks the variable-size data fields.
  221. */
  222. static
  223. bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
  224. const efi_load_option_t *src, size_t size)
  225. {
  226. const void *pos;
  227. u16 c;
  228. efi_device_path_protocol_t header;
  229. const efi_char16_t *description;
  230. const efi_device_path_protocol_t *file_path_list;
  231. if (size < offsetof(efi_load_option_t, variable_data))
  232. return false;
  233. pos = src->variable_data;
  234. size -= offsetof(efi_load_option_t, variable_data);
  235. if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
  236. return false;
  237. /* Scan description. */
  238. description = pos;
  239. do {
  240. if (size < sizeof(c))
  241. return false;
  242. c = *(const u16 *)pos;
  243. pos += sizeof(c);
  244. size -= sizeof(c);
  245. } while (c != L'\0');
  246. /* Scan file_path_list. */
  247. file_path_list = pos;
  248. do {
  249. if (size < sizeof(header))
  250. return false;
  251. header = *(const efi_device_path_protocol_t *)pos;
  252. if (header.length < sizeof(header))
  253. return false;
  254. if (size < header.length)
  255. return false;
  256. pos += header.length;
  257. size -= header.length;
  258. } while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
  259. (header.sub_type != EFI_DEV_END_ENTIRE));
  260. if (pos != (const void *)file_path_list + src->file_path_list_length)
  261. return false;
  262. dest->attributes = src->attributes;
  263. dest->file_path_list_length = src->file_path_list_length;
  264. dest->description = description;
  265. dest->file_path_list = file_path_list;
  266. dest->optional_data_size = size;
  267. dest->optional_data = size ? pos : NULL;
  268. return true;
  269. }
  270. /*
  271. * At least some versions of Dell firmware pass the entire contents of the
  272. * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
  273. * OptionalData field.
  274. *
  275. * Detect this case and extract OptionalData.
  276. */
  277. void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
  278. {
  279. const efi_load_option_t *load_option = *load_options;
  280. efi_load_option_unpacked_t load_option_unpacked;
  281. if (!IS_ENABLED(CONFIG_X86))
  282. return;
  283. if (!load_option)
  284. return;
  285. if (*load_options_size < sizeof(*load_option))
  286. return;
  287. if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
  288. return;
  289. if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
  290. return;
  291. efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
  292. efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
  293. *load_options = load_option_unpacked.optional_data;
  294. *load_options_size = load_option_unpacked.optional_data_size;
  295. }
  296. enum efistub_event {
  297. EFISTUB_EVT_INITRD,
  298. EFISTUB_EVT_LOAD_OPTIONS,
  299. EFISTUB_EVT_COUNT,
  300. };
  301. #define STR_WITH_SIZE(s) sizeof(s), s
  302. static const struct {
  303. u32 pcr_index;
  304. u32 event_id;
  305. u32 event_data_len;
  306. u8 event_data[52];
  307. } events[] = {
  308. [EFISTUB_EVT_INITRD] = {
  309. 9,
  310. INITRD_EVENT_TAG_ID,
  311. STR_WITH_SIZE("Linux initrd")
  312. },
  313. [EFISTUB_EVT_LOAD_OPTIONS] = {
  314. 9,
  315. LOAD_OPTIONS_EVENT_TAG_ID,
  316. STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
  317. },
  318. };
  319. static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
  320. unsigned long load_size,
  321. enum efistub_event event)
  322. {
  323. efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
  324. efi_tcg2_protocol_t *tcg2 = NULL;
  325. efi_status_t status;
  326. efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
  327. if (tcg2) {
  328. struct efi_measured_event {
  329. efi_tcg2_event_t event_data;
  330. efi_tcg2_tagged_event_t tagged_event;
  331. u8 tagged_event_data[];
  332. } *evt;
  333. int size = sizeof(*evt) + events[event].event_data_len;
  334. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  335. (void **)&evt);
  336. if (status != EFI_SUCCESS)
  337. goto fail;
  338. evt->event_data = (struct efi_tcg2_event){
  339. .event_size = size,
  340. .event_header.header_size = sizeof(evt->event_data.event_header),
  341. .event_header.header_version = EFI_TCG2_EVENT_HEADER_VERSION,
  342. .event_header.pcr_index = events[event].pcr_index,
  343. .event_header.event_type = EV_EVENT_TAG,
  344. };
  345. evt->tagged_event = (struct efi_tcg2_tagged_event){
  346. .tagged_event_id = events[event].event_id,
  347. .tagged_event_data_size = events[event].event_data_len,
  348. };
  349. memcpy(evt->tagged_event_data, events[event].event_data,
  350. events[event].event_data_len);
  351. status = efi_call_proto(tcg2, hash_log_extend_event, 0,
  352. load_addr, load_size, &evt->event_data);
  353. efi_bs_call(free_pool, evt);
  354. if (status != EFI_SUCCESS)
  355. goto fail;
  356. return EFI_SUCCESS;
  357. }
  358. return EFI_UNSUPPORTED;
  359. fail:
  360. efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
  361. return status;
  362. }
  363. /*
  364. * Convert the unicode UEFI command line to ASCII to pass to kernel.
  365. * Size of memory allocated return in *cmd_line_len.
  366. * Returns NULL on error.
  367. */
  368. char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
  369. {
  370. const efi_char16_t *options = efi_table_attr(image, load_options);
  371. u32 options_size = efi_table_attr(image, load_options_size);
  372. int options_bytes = 0, safe_options_bytes = 0; /* UTF-8 bytes */
  373. unsigned long cmdline_addr = 0;
  374. const efi_char16_t *s2;
  375. bool in_quote = false;
  376. efi_status_t status;
  377. u32 options_chars;
  378. if (options_size > 0)
  379. efi_measure_tagged_event((unsigned long)options, options_size,
  380. EFISTUB_EVT_LOAD_OPTIONS);
  381. efi_apply_loadoptions_quirk((const void **)&options, &options_size);
  382. options_chars = options_size / sizeof(efi_char16_t);
  383. if (options) {
  384. s2 = options;
  385. while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
  386. efi_char16_t c = *s2++;
  387. if (c < 0x80) {
  388. if (c == L'\0' || c == L'\n')
  389. break;
  390. if (c == L'"')
  391. in_quote = !in_quote;
  392. else if (!in_quote && isspace((char)c))
  393. safe_options_bytes = options_bytes;
  394. options_bytes++;
  395. continue;
  396. }
  397. /*
  398. * Get the number of UTF-8 bytes corresponding to a
  399. * UTF-16 character.
  400. * The first part handles everything in the BMP.
  401. */
  402. options_bytes += 2 + (c >= 0x800);
  403. /*
  404. * Add one more byte for valid surrogate pairs. Invalid
  405. * surrogates will be replaced with 0xfffd and take up
  406. * only 3 bytes.
  407. */
  408. if ((c & 0xfc00) == 0xd800) {
  409. /*
  410. * If the very last word is a high surrogate,
  411. * we must ignore it since we can't access the
  412. * low surrogate.
  413. */
  414. if (!options_chars) {
  415. options_bytes -= 3;
  416. } else if ((*s2 & 0xfc00) == 0xdc00) {
  417. options_bytes++;
  418. options_chars--;
  419. s2++;
  420. }
  421. }
  422. }
  423. if (options_bytes >= COMMAND_LINE_SIZE) {
  424. options_bytes = safe_options_bytes;
  425. efi_err("Command line is too long: truncated to %d bytes\n",
  426. options_bytes);
  427. }
  428. }
  429. options_bytes++; /* NUL termination */
  430. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
  431. (void **)&cmdline_addr);
  432. if (status != EFI_SUCCESS)
  433. return NULL;
  434. snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
  435. options_bytes - 1, options);
  436. *cmd_line_len = options_bytes;
  437. return (char *)cmdline_addr;
  438. }
  439. /**
  440. * efi_exit_boot_services() - Exit boot services
  441. * @handle: handle of the exiting image
  442. * @priv: argument to be passed to @priv_func
  443. * @priv_func: function to process the memory map before exiting boot services
  444. *
  445. * Handle calling ExitBootServices according to the requirements set out by the
  446. * spec. Obtains the current memory map, and returns that info after calling
  447. * ExitBootServices. The client must specify a function to perform any
  448. * processing of the memory map data prior to ExitBootServices. A client
  449. * specific structure may be passed to the function via priv. The client
  450. * function may be called multiple times.
  451. *
  452. * Return: status code
  453. */
  454. efi_status_t efi_exit_boot_services(void *handle, void *priv,
  455. efi_exit_boot_map_processing priv_func)
  456. {
  457. struct efi_boot_memmap *map;
  458. efi_status_t status;
  459. if (efi_disable_pci_dma)
  460. efi_pci_disable_bridge_busmaster();
  461. status = efi_get_memory_map(&map, true);
  462. if (status != EFI_SUCCESS)
  463. return status;
  464. status = priv_func(map, priv);
  465. if (status != EFI_SUCCESS) {
  466. efi_bs_call(free_pool, map);
  467. return status;
  468. }
  469. status = efi_bs_call(exit_boot_services, handle, map->map_key);
  470. if (status == EFI_INVALID_PARAMETER) {
  471. /*
  472. * The memory map changed between efi_get_memory_map() and
  473. * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
  474. * EFI_BOOT_SERVICES.ExitBootServices we need to get the
  475. * updated map, and try again. The spec implies one retry
  476. * should be sufficent, which is confirmed against the EDK2
  477. * implementation. Per the spec, we can only invoke
  478. * get_memory_map() and exit_boot_services() - we cannot alloc
  479. * so efi_get_memory_map() cannot be used, and we must reuse
  480. * the buffer. For all practical purposes, the headroom in the
  481. * buffer should account for any changes in the map so the call
  482. * to get_memory_map() is expected to succeed here.
  483. */
  484. map->map_size = map->buff_size;
  485. status = efi_bs_call(get_memory_map,
  486. &map->map_size,
  487. &map->map,
  488. &map->map_key,
  489. &map->desc_size,
  490. &map->desc_ver);
  491. /* exit_boot_services() was called, thus cannot free */
  492. if (status != EFI_SUCCESS)
  493. return status;
  494. status = priv_func(map, priv);
  495. /* exit_boot_services() was called, thus cannot free */
  496. if (status != EFI_SUCCESS)
  497. return status;
  498. status = efi_bs_call(exit_boot_services, handle, map->map_key);
  499. }
  500. return status;
  501. }
  502. /**
  503. * get_efi_config_table() - retrieve UEFI configuration table
  504. * @guid: GUID of the configuration table to be retrieved
  505. * Return: pointer to the configuration table or NULL
  506. */
  507. void *get_efi_config_table(efi_guid_t guid)
  508. {
  509. unsigned long tables = efi_table_attr(efi_system_table, tables);
  510. int nr_tables = efi_table_attr(efi_system_table, nr_tables);
  511. int i;
  512. for (i = 0; i < nr_tables; i++) {
  513. efi_config_table_t *t = (void *)tables;
  514. if (efi_guidcmp(t->guid, guid) == 0)
  515. return efi_table_attr(t, table);
  516. tables += efi_is_native() ? sizeof(efi_config_table_t)
  517. : sizeof(efi_config_table_32_t);
  518. }
  519. return NULL;
  520. }
  521. /*
  522. * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
  523. * for the firmware or bootloader to expose the initrd data directly to the stub
  524. * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
  525. * very easy to implement. It is a simple Linux initrd specific conduit between
  526. * kernel and firmware, allowing us to put the EFI stub (being part of the
  527. * kernel) in charge of where and when to load the initrd, while leaving it up
  528. * to the firmware to decide whether it needs to expose its filesystem hierarchy
  529. * via EFI protocols.
  530. */
  531. static const struct {
  532. struct efi_vendor_dev_path vendor;
  533. struct efi_generic_dev_path end;
  534. } __packed initrd_dev_path = {
  535. {
  536. {
  537. EFI_DEV_MEDIA,
  538. EFI_DEV_MEDIA_VENDOR,
  539. sizeof(struct efi_vendor_dev_path),
  540. },
  541. LINUX_EFI_INITRD_MEDIA_GUID
  542. }, {
  543. EFI_DEV_END_PATH,
  544. EFI_DEV_END_ENTIRE,
  545. sizeof(struct efi_generic_dev_path)
  546. }
  547. };
  548. /**
  549. * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
  550. * @load_addr: pointer to store the address where the initrd was loaded
  551. * @load_size: pointer to store the size of the loaded initrd
  552. * @max: upper limit for the initrd memory allocation
  553. *
  554. * Return:
  555. * * %EFI_SUCCESS if the initrd was loaded successfully, in which
  556. * case @load_addr and @load_size are assigned accordingly
  557. * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
  558. * * %EFI_OUT_OF_RESOURCES if memory allocation failed
  559. * * %EFI_LOAD_ERROR in all other cases
  560. */
  561. static
  562. efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
  563. unsigned long max)
  564. {
  565. efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
  566. efi_device_path_protocol_t *dp;
  567. efi_load_file2_protocol_t *lf2;
  568. efi_handle_t handle;
  569. efi_status_t status;
  570. dp = (efi_device_path_protocol_t *)&initrd_dev_path;
  571. status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
  572. if (status != EFI_SUCCESS)
  573. return status;
  574. status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
  575. (void **)&lf2);
  576. if (status != EFI_SUCCESS)
  577. return status;
  578. initrd->size = 0;
  579. status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
  580. if (status != EFI_BUFFER_TOO_SMALL)
  581. return EFI_LOAD_ERROR;
  582. status = efi_allocate_pages(initrd->size, &initrd->base, max);
  583. if (status != EFI_SUCCESS)
  584. return status;
  585. status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
  586. (void *)initrd->base);
  587. if (status != EFI_SUCCESS) {
  588. efi_free(initrd->size, initrd->base);
  589. return EFI_LOAD_ERROR;
  590. }
  591. return EFI_SUCCESS;
  592. }
  593. static
  594. efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
  595. struct linux_efi_initrd *initrd,
  596. unsigned long soft_limit,
  597. unsigned long hard_limit)
  598. {
  599. if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
  600. (IS_ENABLED(CONFIG_X86) && (!efi_is_native() || image == NULL)))
  601. return EFI_UNSUPPORTED;
  602. return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
  603. soft_limit, hard_limit,
  604. &initrd->base, &initrd->size);
  605. }
  606. /**
  607. * efi_load_initrd() - Load initial RAM disk
  608. * @image: EFI loaded image protocol
  609. * @soft_limit: preferred address for loading the initrd
  610. * @hard_limit: upper limit address for loading the initrd
  611. *
  612. * Return: status code
  613. */
  614. efi_status_t efi_load_initrd(efi_loaded_image_t *image,
  615. unsigned long soft_limit,
  616. unsigned long hard_limit,
  617. const struct linux_efi_initrd **out)
  618. {
  619. efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
  620. efi_status_t status = EFI_SUCCESS;
  621. struct linux_efi_initrd initrd, *tbl;
  622. if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
  623. return EFI_SUCCESS;
  624. status = efi_load_initrd_dev_path(&initrd, hard_limit);
  625. if (status == EFI_SUCCESS) {
  626. efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
  627. if (initrd.size > 0 &&
  628. efi_measure_tagged_event(initrd.base, initrd.size,
  629. EFISTUB_EVT_INITRD) == EFI_SUCCESS)
  630. efi_info("Measured initrd data into PCR 9\n");
  631. } else if (status == EFI_NOT_FOUND) {
  632. status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
  633. hard_limit);
  634. /* command line loader disabled or no initrd= passed? */
  635. if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
  636. return EFI_SUCCESS;
  637. if (status == EFI_SUCCESS)
  638. efi_info("Loaded initrd from command line option\n");
  639. }
  640. if (status != EFI_SUCCESS)
  641. goto failed;
  642. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
  643. (void **)&tbl);
  644. if (status != EFI_SUCCESS)
  645. goto free_initrd;
  646. *tbl = initrd;
  647. status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
  648. if (status != EFI_SUCCESS)
  649. goto free_tbl;
  650. if (out)
  651. *out = tbl;
  652. return EFI_SUCCESS;
  653. free_tbl:
  654. efi_bs_call(free_pool, tbl);
  655. free_initrd:
  656. efi_free(initrd.size, initrd.base);
  657. failed:
  658. efi_err("Failed to load initrd: 0x%lx\n", status);
  659. return status;
  660. }
  661. /**
  662. * efi_wait_for_key() - Wait for key stroke
  663. * @usec: number of microseconds to wait for key stroke
  664. * @key: key entered
  665. *
  666. * Wait for up to @usec microseconds for a key stroke.
  667. *
  668. * Return: status code, EFI_SUCCESS if key received
  669. */
  670. efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
  671. {
  672. efi_event_t events[2], timer;
  673. unsigned long index;
  674. efi_simple_text_input_protocol_t *con_in;
  675. efi_status_t status;
  676. con_in = efi_table_attr(efi_system_table, con_in);
  677. if (!con_in)
  678. return EFI_UNSUPPORTED;
  679. efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
  680. status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
  681. if (status != EFI_SUCCESS)
  682. return status;
  683. status = efi_bs_call(set_timer, timer, EfiTimerRelative,
  684. EFI_100NSEC_PER_USEC * usec);
  685. if (status != EFI_SUCCESS)
  686. return status;
  687. efi_set_event_at(events, 1, timer);
  688. status = efi_bs_call(wait_for_event, 2, events, &index);
  689. if (status == EFI_SUCCESS) {
  690. if (index == 0)
  691. status = efi_call_proto(con_in, read_keystroke, key);
  692. else
  693. status = EFI_TIMEOUT;
  694. }
  695. efi_bs_call(close_event, timer);
  696. return status;
  697. }