Ard Biesheuvel | 4febfb8 | 2019-02-02 10:41:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 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 |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 8 | */ |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 9 | |
| 10 | #include <linux/efi.h> |
| 11 | #include <asm/efi.h> |
| 12 | |
| 13 | #include "efistub.h" |
| 14 | |
Arvind Sankar | 5443937 | 2020-04-16 11:12:27 -0400 | [diff] [blame^] | 15 | static bool efi_nochunk; |
| 16 | static bool efi_nokaslr; |
| 17 | static bool efi_noinitrd; |
| 18 | static bool efi_quiet; |
| 19 | static bool efi_novamap; |
| 20 | static bool efi_nosoftreserve; |
| 21 | static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA); |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 22 | |
Ard Biesheuvel | 5193a33 | 2020-02-10 17:02:43 +0100 | [diff] [blame] | 23 | bool __pure nochunk(void) |
| 24 | { |
| 25 | return efi_nochunk; |
| 26 | } |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 27 | bool __pure nokaslr(void) |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 28 | { |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 29 | return efi_nokaslr; |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 30 | } |
Ard Biesheuvel | 79d3219 | 2020-02-04 22:01:22 +0000 | [diff] [blame] | 31 | bool __pure noinitrd(void) |
| 32 | { |
| 33 | return efi_noinitrd; |
| 34 | } |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 35 | bool __pure is_quiet(void) |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 36 | { |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 37 | return efi_quiet; |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 38 | } |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 39 | bool __pure novamap(void) |
Ard Biesheuvel | 4e46c2a | 2019-02-02 10:41:16 +0100 | [diff] [blame] | 40 | { |
Ard Biesheuvel | 7d4e323 | 2019-12-24 16:10:24 +0100 | [diff] [blame] | 41 | return efi_novamap; |
Ard Biesheuvel | 4e46c2a | 2019-02-02 10:41:16 +0100 | [diff] [blame] | 42 | } |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 43 | bool __pure __efi_soft_reserve_enabled(void) |
| 44 | { |
| 45 | return !efi_nosoftreserve; |
| 46 | } |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 47 | |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 48 | void efi_printk(char *str) |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 49 | { |
| 50 | char *s8; |
| 51 | |
| 52 | for (s8 = str; *s8; s8++) { |
| 53 | efi_char16_t ch[2] = { 0 }; |
| 54 | |
| 55 | ch[0] = *s8; |
| 56 | if (*s8 == '\n') { |
| 57 | efi_char16_t nl[2] = { '\r', 0 }; |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 58 | efi_char16_printk(nl); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 61 | efi_char16_printk(ch); |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 65 | /* |
| 66 | * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi= |
| 67 | * option, e.g. efi=nochunk. |
| 68 | * |
| 69 | * It should be noted that efi= is parsed in two very different |
| 70 | * environments, first in the early boot environment of the EFI boot |
| 71 | * stub, and subsequently during the kernel boot. |
| 72 | */ |
Ard Biesheuvel | 60f38de | 2017-04-04 17:09:08 +0100 | [diff] [blame] | 73 | efi_status_t efi_parse_options(char const *cmdline) |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 74 | { |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 75 | size_t len = strlen(cmdline) + 1; |
| 76 | efi_status_t status; |
| 77 | char *str, *buf; |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 78 | |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 79 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf); |
| 80 | if (status != EFI_SUCCESS) |
| 81 | return status; |
Ard Biesheuvel | b3879a4 | 2017-02-06 11:22:46 +0000 | [diff] [blame] | 82 | |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 83 | str = skip_spaces(memcpy(buf, cmdline, len)); |
Ard Biesheuvel | eeff7d6 | 2017-04-04 17:09:09 +0100 | [diff] [blame] | 84 | |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 85 | while (*str) { |
| 86 | char *param, *val; |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 87 | |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 88 | str = next_arg(str, ¶m, &val); |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 89 | |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 90 | if (!strcmp(param, "nokaslr")) { |
| 91 | efi_nokaslr = true; |
| 92 | } else if (!strcmp(param, "quiet")) { |
| 93 | efi_quiet = true; |
Ard Biesheuvel | 79d3219 | 2020-02-04 22:01:22 +0000 | [diff] [blame] | 94 | } else if (!strcmp(param, "noinitrd")) { |
| 95 | efi_noinitrd = true; |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 96 | } else if (!strcmp(param, "efi") && val) { |
| 97 | efi_nochunk = parse_option_str(val, "nochunk"); |
| 98 | efi_novamap = parse_option_str(val, "novamap"); |
| 99 | |
| 100 | efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) && |
| 101 | parse_option_str(val, "nosoftreserve"); |
| 102 | |
| 103 | if (parse_option_str(val, "disable_early_pci_dma")) |
| 104 | efi_disable_pci_dma = true; |
| 105 | if (parse_option_str(val, "no_disable_early_pci_dma")) |
| 106 | efi_disable_pci_dma = false; |
Arvind Sankar | fffb680 | 2020-03-19 22:00:25 -0400 | [diff] [blame] | 107 | } else if (!strcmp(param, "video") && |
| 108 | val && strstarts(val, "efifb:")) { |
| 109 | efi_parse_option_graphics(val + strlen("efifb:")); |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 110 | } |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 111 | } |
Ard Biesheuvel | 91d150c | 2020-02-10 17:02:46 +0100 | [diff] [blame] | 112 | efi_bs_call(free_pool, buf); |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 113 | return EFI_SUCCESS; |
| 114 | } |
Roy Franz | 7721da4 | 2013-09-22 15:45:27 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 117 | * Get the number of UTF-8 bytes corresponding to an UTF-16 character. |
| 118 | * This overestimates for surrogates, but that is okay. |
| 119 | */ |
| 120 | static int efi_utf8_bytes(u16 c) |
| 121 | { |
| 122 | return 1 + (c >= 0x80) + (c >= 0x800); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. |
| 127 | */ |
| 128 | static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) |
| 129 | { |
| 130 | unsigned int c; |
| 131 | |
| 132 | while (n--) { |
| 133 | c = *src++; |
| 134 | if (n && c >= 0xd800 && c <= 0xdbff && |
| 135 | *src >= 0xdc00 && *src <= 0xdfff) { |
| 136 | c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); |
| 137 | src++; |
| 138 | n--; |
| 139 | } |
| 140 | if (c >= 0xd800 && c <= 0xdfff) |
| 141 | c = 0xfffd; /* Unmatched surrogate */ |
| 142 | if (c < 0x80) { |
| 143 | *dst++ = c; |
| 144 | continue; |
| 145 | } |
| 146 | if (c < 0x800) { |
| 147 | *dst++ = 0xc0 + (c >> 6); |
| 148 | goto t1; |
| 149 | } |
| 150 | if (c < 0x10000) { |
| 151 | *dst++ = 0xe0 + (c >> 12); |
| 152 | goto t2; |
| 153 | } |
| 154 | *dst++ = 0xf0 + (c >> 18); |
| 155 | *dst++ = 0x80 + ((c >> 12) & 0x3f); |
| 156 | t2: |
| 157 | *dst++ = 0x80 + ((c >> 6) & 0x3f); |
| 158 | t1: |
| 159 | *dst++ = 0x80 + (c & 0x3f); |
| 160 | } |
| 161 | |
| 162 | return dst; |
| 163 | } |
| 164 | |
| 165 | /* |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 166 | * Convert the unicode UEFI command line to ASCII to pass to kernel. |
| 167 | * Size of memory allocated return in *cmd_line_len. |
| 168 | * Returns NULL on error. |
| 169 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 170 | char *efi_convert_cmdline(efi_loaded_image_t *image, |
Ard Biesheuvel | 1e45bf7 | 2020-02-10 17:02:40 +0100 | [diff] [blame] | 171 | int *cmd_line_len, unsigned long max_addr) |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 172 | { |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 173 | const u16 *s2; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 174 | u8 *s1 = NULL; |
| 175 | unsigned long cmdline_addr = 0; |
Ard Biesheuvel | f7b85b3 | 2020-02-14 14:29:21 +0100 | [diff] [blame] | 176 | int load_options_chars = efi_table_attr(image, load_options_size) / 2; |
| 177 | const u16 *options = efi_table_attr(image, load_options); |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 178 | int options_bytes = 0; /* UTF-8 bytes */ |
| 179 | int options_chars = 0; /* UTF-16 chars */ |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 180 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 181 | u16 zero = 0; |
| 182 | |
| 183 | if (options) { |
| 184 | s2 = options; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 185 | while (*s2 && *s2 != '\n' |
| 186 | && options_chars < load_options_chars) { |
| 187 | options_bytes += efi_utf8_bytes(*s2++); |
| 188 | options_chars++; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 192 | if (!options_chars) { |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 193 | /* No command line options, so return empty string*/ |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 194 | options = &zero; |
| 195 | } |
| 196 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 197 | options_bytes++; /* NUL termination */ |
Leif Lindholm | 9403e46 | 2014-04-04 13:25:46 +0100 | [diff] [blame] | 198 | |
Ard Biesheuvel | 1e45bf7 | 2020-02-10 17:02:40 +0100 | [diff] [blame] | 199 | status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 200 | if (status != EFI_SUCCESS) |
| 201 | return NULL; |
| 202 | |
| 203 | s1 = (u8 *)cmdline_addr; |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 204 | s2 = (const u16 *)options; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 205 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 206 | s1 = efi_utf16_to_utf8(s1, s2, options_chars); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 207 | *s1 = '\0'; |
| 208 | |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 209 | *cmd_line_len = options_bytes; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 210 | return (char *)cmdline_addr; |
| 211 | } |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Handle calling ExitBootServices according to the requirements set out by the |
| 215 | * spec. Obtains the current memory map, and returns that info after calling |
| 216 | * ExitBootServices. The client must specify a function to perform any |
| 217 | * processing of the memory map data prior to ExitBootServices. A client |
| 218 | * specific structure may be passed to the function via priv. The client |
| 219 | * function may be called multiple times. |
| 220 | */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 221 | efi_status_t efi_exit_boot_services(void *handle, |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 222 | struct efi_boot_memmap *map, |
| 223 | void *priv, |
| 224 | efi_exit_boot_map_processing priv_func) |
| 225 | { |
| 226 | efi_status_t status; |
| 227 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 228 | status = efi_get_memory_map(map); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 229 | |
| 230 | if (status != EFI_SUCCESS) |
| 231 | goto fail; |
| 232 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 233 | status = priv_func(map, priv); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 234 | if (status != EFI_SUCCESS) |
| 235 | goto free_map; |
| 236 | |
Matthew Garrett | 4444f85 | 2020-01-03 12:39:50 +0100 | [diff] [blame] | 237 | if (efi_disable_pci_dma) |
| 238 | efi_pci_disable_bridge_busmaster(); |
| 239 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 240 | status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 241 | |
| 242 | if (status == EFI_INVALID_PARAMETER) { |
| 243 | /* |
| 244 | * The memory map changed between efi_get_memory_map() and |
| 245 | * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4: |
| 246 | * EFI_BOOT_SERVICES.ExitBootServices we need to get the |
| 247 | * updated map, and try again. The spec implies one retry |
| 248 | * should be sufficent, which is confirmed against the EDK2 |
| 249 | * implementation. Per the spec, we can only invoke |
| 250 | * get_memory_map() and exit_boot_services() - we cannot alloc |
| 251 | * so efi_get_memory_map() cannot be used, and we must reuse |
| 252 | * the buffer. For all practical purposes, the headroom in the |
| 253 | * buffer should account for any changes in the map so the call |
| 254 | * to get_memory_map() is expected to succeed here. |
| 255 | */ |
| 256 | *map->map_size = *map->buff_size; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 257 | status = efi_bs_call(get_memory_map, |
| 258 | map->map_size, |
| 259 | *map->map, |
| 260 | map->key_ptr, |
| 261 | map->desc_size, |
| 262 | map->desc_ver); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 263 | |
| 264 | /* exit_boot_services() was called, thus cannot free */ |
| 265 | if (status != EFI_SUCCESS) |
| 266 | goto fail; |
| 267 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 268 | status = priv_func(map, priv); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 269 | /* exit_boot_services() was called, thus cannot free */ |
| 270 | if (status != EFI_SUCCESS) |
| 271 | goto fail; |
| 272 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 273 | status = efi_bs_call(exit_boot_services, handle, *map->key_ptr); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* exit_boot_services() was called, thus cannot free */ |
| 277 | if (status != EFI_SUCCESS) |
| 278 | goto fail; |
| 279 | |
| 280 | return EFI_SUCCESS; |
| 281 | |
| 282 | free_map: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame] | 283 | efi_bs_call(free_pool, *map->map); |
Jeffrey Hugo | fc07716 | 2016-08-29 14:38:52 -0600 | [diff] [blame] | 284 | fail: |
| 285 | return status; |
| 286 | } |
Matthew Garrett | 82d736a | 2019-06-07 13:51:46 -0700 | [diff] [blame] | 287 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 288 | void *get_efi_config_table(efi_guid_t guid) |
Matthew Garrett | 82d736a | 2019-06-07 13:51:46 -0700 | [diff] [blame] | 289 | { |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 290 | unsigned long tables = efi_table_attr(efi_system_table(), tables); |
| 291 | int nr_tables = efi_table_attr(efi_system_table(), nr_tables); |
Ard Biesheuvel | f958efe | 2019-12-24 16:10:09 +0100 | [diff] [blame] | 292 | int i; |
| 293 | |
| 294 | for (i = 0; i < nr_tables; i++) { |
| 295 | efi_config_table_t *t = (void *)tables; |
| 296 | |
| 297 | if (efi_guidcmp(t->guid, guid) == 0) |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 298 | return efi_table_attr(t, table); |
Ard Biesheuvel | f958efe | 2019-12-24 16:10:09 +0100 | [diff] [blame] | 299 | |
| 300 | tables += efi_is_native() ? sizeof(efi_config_table_t) |
| 301 | : sizeof(efi_config_table_32_t); |
| 302 | } |
| 303 | return NULL; |
Matthew Garrett | 82d736a | 2019-06-07 13:51:46 -0700 | [diff] [blame] | 304 | } |
Ard Biesheuvel | dc29da1 | 2019-12-24 16:10:16 +0100 | [diff] [blame] | 305 | |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 306 | void efi_char16_printk(efi_char16_t *str) |
Ard Biesheuvel | dc29da1 | 2019-12-24 16:10:16 +0100 | [diff] [blame] | 307 | { |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 308 | efi_call_proto(efi_table_attr(efi_system_table(), con_out), |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 309 | output_string, str); |
Ard Biesheuvel | dc29da1 | 2019-12-24 16:10:16 +0100 | [diff] [blame] | 310 | } |
Ard Biesheuvel | ec93fc3 | 2020-02-03 23:45:14 +0000 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way |
| 314 | * for the firmware or bootloader to expose the initrd data directly to the stub |
| 315 | * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is |
| 316 | * very easy to implement. It is a simple Linux initrd specific conduit between |
| 317 | * kernel and firmware, allowing us to put the EFI stub (being part of the |
| 318 | * kernel) in charge of where and when to load the initrd, while leaving it up |
| 319 | * to the firmware to decide whether it needs to expose its filesystem hierarchy |
| 320 | * via EFI protocols. |
| 321 | */ |
| 322 | static const struct { |
| 323 | struct efi_vendor_dev_path vendor; |
| 324 | struct efi_generic_dev_path end; |
| 325 | } __packed initrd_dev_path = { |
| 326 | { |
| 327 | { |
| 328 | EFI_DEV_MEDIA, |
| 329 | EFI_DEV_MEDIA_VENDOR, |
| 330 | sizeof(struct efi_vendor_dev_path), |
| 331 | }, |
| 332 | LINUX_EFI_INITRD_MEDIA_GUID |
| 333 | }, { |
| 334 | EFI_DEV_END_PATH, |
| 335 | EFI_DEV_END_ENTIRE, |
| 336 | sizeof(struct efi_generic_dev_path) |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | /** |
| 341 | * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path |
| 342 | * @load_addr: pointer to store the address where the initrd was loaded |
| 343 | * @load_size: pointer to store the size of the loaded initrd |
| 344 | * @max: upper limit for the initrd memory allocation |
| 345 | * @return: %EFI_SUCCESS if the initrd was loaded successfully, in which |
| 346 | * case @load_addr and @load_size are assigned accordingly |
| 347 | * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd |
| 348 | * device path |
| 349 | * %EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL |
| 350 | * %EFI_OUT_OF_RESOURCES if memory allocation failed |
| 351 | * %EFI_LOAD_ERROR in all other cases |
| 352 | */ |
| 353 | efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr, |
| 354 | unsigned long *load_size, |
| 355 | unsigned long max) |
| 356 | { |
| 357 | efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID; |
| 358 | efi_device_path_protocol_t *dp; |
| 359 | efi_load_file2_protocol_t *lf2; |
| 360 | unsigned long initrd_addr; |
| 361 | unsigned long initrd_size; |
| 362 | efi_handle_t handle; |
| 363 | efi_status_t status; |
| 364 | |
| 365 | if (!load_addr || !load_size) |
| 366 | return EFI_INVALID_PARAMETER; |
| 367 | |
| 368 | dp = (efi_device_path_protocol_t *)&initrd_dev_path; |
| 369 | status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle); |
| 370 | if (status != EFI_SUCCESS) |
| 371 | return status; |
| 372 | |
| 373 | status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid, |
| 374 | (void **)&lf2); |
| 375 | if (status != EFI_SUCCESS) |
| 376 | return status; |
| 377 | |
| 378 | status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, NULL); |
| 379 | if (status != EFI_BUFFER_TOO_SMALL) |
| 380 | return EFI_LOAD_ERROR; |
| 381 | |
| 382 | status = efi_allocate_pages(initrd_size, &initrd_addr, max); |
| 383 | if (status != EFI_SUCCESS) |
| 384 | return status; |
| 385 | |
| 386 | status = efi_call_proto(lf2, load_file, dp, false, &initrd_size, |
| 387 | (void *)initrd_addr); |
| 388 | if (status != EFI_SUCCESS) { |
| 389 | efi_free(initrd_size, initrd_addr); |
| 390 | return EFI_LOAD_ERROR; |
| 391 | } |
| 392 | |
| 393 | *load_addr = initrd_addr; |
| 394 | *load_size = initrd_size; |
| 395 | return EFI_SUCCESS; |
| 396 | } |