Commit | Line | Data |
---|---|---|
97873a3d | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b84a64fa | 2 | |
291f3632 MF |
3 | /* ----------------------------------------------------------------------- |
4 | * | |
5 | * Copyright 2011 Intel Corporation; author Matt Fleming | |
6 | * | |
291f3632 MF |
7 | * ----------------------------------------------------------------------- */ |
8 | ||
bc7c9d62 AB |
9 | #pragma GCC visibility push(hidden) |
10 | ||
291f3632 | 11 | #include <linux/efi.h> |
dd5fc854 | 12 | #include <linux/pci.h> |
5520b7e7 | 13 | |
291f3632 | 14 | #include <asm/efi.h> |
5520b7e7 | 15 | #include <asm/e820/types.h> |
291f3632 MF |
16 | #include <asm/setup.h> |
17 | #include <asm/desc.h> | |
220dd769 | 18 | #include <asm/boot.h> |
291f3632 | 19 | |
393f203f | 20 | #include "../string.h" |
291f3632 MF |
21 | #include "eboot.h" |
22 | ||
23 | static efi_system_table_t *sys_table; | |
796eb8d2 | 24 | extern const bool efi_is64; |
204b0a1a | 25 | |
2fcdad2a AB |
26 | __pure efi_system_table_t *efi_system_table(void) |
27 | { | |
28 | return sys_table; | |
29 | } | |
30 | ||
796eb8d2 | 31 | __attribute_const__ bool efi_is_64bit(void) |
c3710de5 | 32 | { |
796eb8d2 AB |
33 | if (IS_ENABLED(CONFIG_EFI_MIXED)) |
34 | return efi_is64; | |
35 | return IS_ENABLED(CONFIG_X64_64); | |
54b52d87 | 36 | } |
291f3632 | 37 | |
c116e8d6 | 38 | static efi_status_t |
75c5a713 | 39 | preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) |
dd5fc854 | 40 | { |
c116e8d6 | 41 | struct pci_setup_rom *rom = NULL; |
dd5fc854 | 42 | efi_status_t status; |
c116e8d6 | 43 | unsigned long size; |
e2967018 | 44 | uint64_t romsize; |
2c3625cb | 45 | void *romimage; |
dd5fc854 | 46 | |
1de3a1be | 47 | /* |
e2967018 AB |
48 | * Some firmware images contain EFI function pointers at the place where |
49 | * the romimage and romsize fields are supposed to be. Typically the EFI | |
1de3a1be HG |
50 | * code is mapped at high addresses, translating to an unrealistically |
51 | * large romsize. The UEFI spec limits the size of option ROMs to 16 | |
52 | * MiB so we reject any ROMs over 16 MiB in size to catch this. | |
53 | */ | |
99ea8b1d AB |
54 | romimage = efi_table_attr(pci, romimage); |
55 | romsize = efi_table_attr(pci, romsize); | |
1de3a1be | 56 | if (!romimage || !romsize || romsize > SZ_16M) |
c116e8d6 | 57 | return EFI_INVALID_PARAMETER; |
dd5fc854 | 58 | |
2c3625cb | 59 | size = romsize + sizeof(*rom); |
dd5fc854 | 60 | |
966291f6 AB |
61 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
62 | (void **)&rom); | |
77e21e87 | 63 | if (status != EFI_SUCCESS) { |
8173ec79 | 64 | efi_printk("Failed to allocate memory for 'rom'\n"); |
c116e8d6 | 65 | return status; |
77e21e87 | 66 | } |
dd5fc854 | 67 | |
c116e8d6 MF |
68 | memset(rom, 0, sizeof(*rom)); |
69 | ||
90a2186b IM |
70 | rom->data.type = SETUP_PCI; |
71 | rom->data.len = size - sizeof(struct setup_data); | |
72 | rom->data.next = 0; | |
73 | rom->pcilen = pci->romsize; | |
c116e8d6 MF |
74 | *__rom = rom; |
75 | ||
47c0fd39 AB |
76 | status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, |
77 | PCI_VENDOR_ID, 1, &rom->vendor); | |
dd5fc854 | 78 | |
77e21e87 | 79 | if (status != EFI_SUCCESS) { |
8173ec79 | 80 | efi_printk("Failed to read rom->vendor\n"); |
c116e8d6 | 81 | goto free_struct; |
77e21e87 | 82 | } |
c116e8d6 | 83 | |
47c0fd39 AB |
84 | status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, |
85 | PCI_DEVICE_ID, 1, &rom->devid); | |
c116e8d6 | 86 | |
77e21e87 | 87 | if (status != EFI_SUCCESS) { |
8173ec79 | 88 | efi_printk("Failed to read rom->devid\n"); |
c116e8d6 | 89 | goto free_struct; |
77e21e87 | 90 | } |
c116e8d6 | 91 | |
47c0fd39 AB |
92 | status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus, |
93 | &rom->device, &rom->function); | |
c116e8d6 MF |
94 | |
95 | if (status != EFI_SUCCESS) | |
96 | goto free_struct; | |
97 | ||
2c3625cb | 98 | memcpy(rom->romdata, romimage, romsize); |
c116e8d6 MF |
99 | return status; |
100 | ||
101 | free_struct: | |
966291f6 | 102 | efi_bs_call(free_pool, rom); |
c116e8d6 MF |
103 | return status; |
104 | } | |
105 | ||
56394ab8 MF |
106 | /* |
107 | * There's no way to return an informative status from this function, | |
108 | * because any analysis (and printing of error messages) needs to be | |
109 | * done directly at the EFI function call-site. | |
110 | * | |
111 | * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we | |
112 | * just didn't find any PCI devices, but there's no way to tell outside | |
113 | * the context of the call. | |
114 | */ | |
115 | static void setup_efi_pci(struct boot_params *params) | |
291f3632 | 116 | { |
291f3632 | 117 | efi_status_t status; |
c116e8d6 MF |
118 | void **pci_handle = NULL; |
119 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
120 | unsigned long size = 0; | |
75c5a713 | 121 | struct setup_data *data; |
2732ea0d | 122 | efi_handle_t h; |
75c5a713 | 123 | int i; |
291f3632 | 124 | |
966291f6 AB |
125 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
126 | &pci_proto, NULL, &size, pci_handle); | |
291f3632 | 127 | |
c116e8d6 | 128 | if (status == EFI_BUFFER_TOO_SMALL) { |
966291f6 AB |
129 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
130 | (void **)&pci_handle); | |
291f3632 | 131 | |
77e21e87 | 132 | if (status != EFI_SUCCESS) { |
8173ec79 | 133 | efi_printk("Failed to allocate memory for 'pci_handle'\n"); |
56394ab8 | 134 | return; |
77e21e87 | 135 | } |
291f3632 | 136 | |
966291f6 AB |
137 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
138 | &pci_proto, NULL, &size, pci_handle); | |
291f3632 MF |
139 | } |
140 | ||
c116e8d6 | 141 | if (status != EFI_SUCCESS) |
291f3632 MF |
142 | goto free_handle; |
143 | ||
75c5a713 AB |
144 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
145 | ||
146 | while (data && data->next) | |
147 | data = (struct setup_data *)(unsigned long)data->next; | |
148 | ||
2732ea0d | 149 | for_each_efi_handle(h, pci_handle, size, i) { |
75c5a713 AB |
150 | efi_pci_io_protocol_t *pci = NULL; |
151 | struct pci_setup_rom *rom; | |
152 | ||
966291f6 AB |
153 | status = efi_bs_call(handle_protocol, h, &pci_proto, |
154 | (void **)&pci); | |
75c5a713 AB |
155 | if (status != EFI_SUCCESS || !pci) |
156 | continue; | |
157 | ||
158 | status = preserve_pci_rom_image(pci, &rom); | |
159 | if (status != EFI_SUCCESS) | |
160 | continue; | |
161 | ||
162 | if (data) | |
163 | data->next = (unsigned long)rom; | |
164 | else | |
165 | params->hdr.setup_data = (unsigned long)rom; | |
166 | ||
167 | data = (struct setup_data *)rom; | |
168 | } | |
291f3632 | 169 | |
c116e8d6 | 170 | free_handle: |
966291f6 | 171 | efi_bs_call(free_pool, pci_handle); |
c116e8d6 | 172 | } |
291f3632 | 173 | |
58c5475a LW |
174 | static void retrieve_apple_device_properties(struct boot_params *boot_params) |
175 | { | |
176 | efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; | |
177 | struct setup_data *data, *new; | |
178 | efi_status_t status; | |
179 | u32 size = 0; | |
960a8d01 | 180 | apple_properties_protocol_t *p; |
58c5475a | 181 | |
966291f6 | 182 | status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p); |
58c5475a LW |
183 | if (status != EFI_SUCCESS) |
184 | return; | |
185 | ||
99ea8b1d | 186 | if (efi_table_attr(p, version) != 0x10000) { |
8173ec79 | 187 | efi_printk("Unsupported properties proto version\n"); |
58c5475a LW |
188 | return; |
189 | } | |
190 | ||
47c0fd39 | 191 | efi_call_proto(p, get_all, NULL, &size); |
58c5475a LW |
192 | if (!size) |
193 | return; | |
194 | ||
195 | do { | |
966291f6 AB |
196 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, |
197 | size + sizeof(struct setup_data), | |
198 | (void **)&new); | |
58c5475a | 199 | if (status != EFI_SUCCESS) { |
8173ec79 | 200 | efi_printk("Failed to allocate memory for 'properties'\n"); |
58c5475a LW |
201 | return; |
202 | } | |
203 | ||
47c0fd39 | 204 | status = efi_call_proto(p, get_all, new->data, &size); |
58c5475a LW |
205 | |
206 | if (status == EFI_BUFFER_TOO_SMALL) | |
966291f6 | 207 | efi_bs_call(free_pool, new); |
58c5475a LW |
208 | } while (status == EFI_BUFFER_TOO_SMALL); |
209 | ||
210 | new->type = SETUP_APPLE_PROPERTIES; | |
211 | new->len = size; | |
212 | new->next = 0; | |
213 | ||
214 | data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; | |
90a2186b | 215 | if (!data) { |
58c5475a | 216 | boot_params->hdr.setup_data = (unsigned long)new; |
90a2186b | 217 | } else { |
58c5475a LW |
218 | while (data->next) |
219 | data = (struct setup_data *)(unsigned long)data->next; | |
220 | data->next = (unsigned long)new; | |
221 | } | |
222 | } | |
223 | ||
36b64976 AB |
224 | static const efi_char16_t apple[] = L"Apple"; |
225 | ||
58c5475a LW |
226 | static void setup_quirks(struct boot_params *boot_params) |
227 | { | |
58c5475a | 228 | efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) |
99ea8b1d | 229 | efi_table_attr(efi_system_table(), fw_vendor); |
58c5475a LW |
230 | |
231 | if (!memcmp(fw_vendor, apple, sizeof(apple))) { | |
232 | if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) | |
233 | retrieve_apple_device_properties(boot_params); | |
234 | } | |
235 | } | |
236 | ||
290084c2 AB |
237 | /* |
238 | * See if we have Universal Graphics Adapter (UGA) protocol | |
239 | */ | |
c116e8d6 | 240 | static efi_status_t |
290084c2 | 241 | setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) |
c116e8d6 | 242 | { |
290084c2 AB |
243 | efi_status_t status; |
244 | u32 width, height; | |
245 | void **uga_handle = NULL; | |
0b767b16 | 246 | efi_uga_draw_protocol_t *uga = NULL, *first_uga; |
2732ea0d | 247 | efi_handle_t handle; |
c116e8d6 MF |
248 | int i; |
249 | ||
966291f6 AB |
250 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
251 | (void **)&uga_handle); | |
290084c2 AB |
252 | if (status != EFI_SUCCESS) |
253 | return status; | |
c116e8d6 | 254 | |
966291f6 AB |
255 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
256 | uga_proto, NULL, &size, uga_handle); | |
290084c2 AB |
257 | if (status != EFI_SUCCESS) |
258 | goto free_handle; | |
291f3632 | 259 | |
290084c2 AB |
260 | height = 0; |
261 | width = 0; | |
c116e8d6 MF |
262 | |
263 | first_uga = NULL; | |
2732ea0d | 264 | for_each_efi_handle(handle, uga_handle, size, i) { |
291f3632 | 265 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
291f3632 MF |
266 | u32 w, h, depth, refresh; |
267 | void *pciio; | |
268 | ||
966291f6 AB |
269 | status = efi_bs_call(handle_protocol, handle, uga_proto, |
270 | (void **)&uga); | |
291f3632 MF |
271 | if (status != EFI_SUCCESS) |
272 | continue; | |
273 | ||
093174f5 | 274 | pciio = NULL; |
966291f6 | 275 | efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); |
291f3632 | 276 | |
47c0fd39 | 277 | status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); |
291f3632 | 278 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
290084c2 AB |
279 | width = w; |
280 | height = h; | |
291f3632 MF |
281 | |
282 | /* | |
283 | * Once we've found a UGA supporting PCIIO, | |
284 | * don't bother looking any further. | |
285 | */ | |
286 | if (pciio) | |
287 | break; | |
288 | ||
289 | first_uga = uga; | |
290 | } | |
291 | } | |
292 | ||
c116e8d6 | 293 | if (!width && !height) |
291f3632 MF |
294 | goto free_handle; |
295 | ||
296 | /* EFI framebuffer */ | |
90a2186b | 297 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
291f3632 | 298 | |
90a2186b IM |
299 | si->lfb_depth = 32; |
300 | si->lfb_width = width; | |
301 | si->lfb_height = height; | |
291f3632 | 302 | |
90a2186b IM |
303 | si->red_size = 8; |
304 | si->red_pos = 16; | |
305 | si->green_size = 8; | |
306 | si->green_pos = 8; | |
307 | si->blue_size = 8; | |
308 | si->blue_pos = 0; | |
309 | si->rsvd_size = 8; | |
310 | si->rsvd_pos = 24; | |
291f3632 | 311 | |
291f3632 | 312 | free_handle: |
966291f6 | 313 | efi_bs_call(free_pool, uga_handle); |
90a2186b | 314 | |
291f3632 MF |
315 | return status; |
316 | } | |
317 | ||
318 | void setup_graphics(struct boot_params *boot_params) | |
319 | { | |
320 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; | |
321 | struct screen_info *si; | |
322 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; | |
323 | efi_status_t status; | |
324 | unsigned long size; | |
325 | void **gop_handle = NULL; | |
326 | void **uga_handle = NULL; | |
327 | ||
328 | si = &boot_params->screen_info; | |
329 | memset(si, 0, sizeof(*si)); | |
330 | ||
331 | size = 0; | |
966291f6 AB |
332 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
333 | &graphics_proto, NULL, &size, gop_handle); | |
291f3632 | 334 | if (status == EFI_BUFFER_TOO_SMALL) |
cd33a5c1 | 335 | status = efi_setup_gop(si, &graphics_proto, size); |
291f3632 MF |
336 | |
337 | if (status != EFI_SUCCESS) { | |
338 | size = 0; | |
966291f6 AB |
339 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
340 | &uga_proto, NULL, &size, uga_handle); | |
291f3632 MF |
341 | if (status == EFI_BUFFER_TOO_SMALL) |
342 | setup_uga(si, &uga_proto, size); | |
343 | } | |
344 | } | |
345 | ||
c3710de5 AB |
346 | void startup_32(struct boot_params *boot_params); |
347 | ||
348 | void __noreturn efi_stub_entry(efi_handle_t handle, | |
349 | efi_system_table_t *sys_table_arg, | |
350 | struct boot_params *boot_params); | |
351 | ||
291f3632 MF |
352 | /* |
353 | * Because the x86 boot code expects to be passed a boot_params we | |
354 | * need to create one ourselves (usually the bootloader would create | |
355 | * one for us). | |
356 | */ | |
c3710de5 AB |
357 | efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, |
358 | efi_system_table_t *sys_table_arg) | |
291f3632 | 359 | { |
9ca8f72a | 360 | struct boot_params *boot_params; |
9ca8f72a MF |
361 | struct apm_bios_info *bi; |
362 | struct setup_header *hdr; | |
9ca8f72a | 363 | efi_loaded_image_t *image; |
9ca8f72a | 364 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
291f3632 MF |
365 | int options_size = 0; |
366 | efi_status_t status; | |
5fef3870 | 367 | char *cmdline_ptr; |
46f4582e RF |
368 | unsigned long ramdisk_addr; |
369 | unsigned long ramdisk_size; | |
291f3632 | 370 | |
c3710de5 | 371 | sys_table = sys_table_arg; |
9ca8f72a MF |
372 | |
373 | /* Check if we were booted by the EFI firmware */ | |
374 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
c3710de5 | 375 | return EFI_INVALID_PARAMETER; |
54b52d87 | 376 | |
966291f6 | 377 | status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image); |
9ca8f72a | 378 | if (status != EFI_SUCCESS) { |
8173ec79 | 379 | efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); |
c3710de5 | 380 | return status; |
9ca8f72a MF |
381 | } |
382 | ||
cd33a5c1 | 383 | status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params); |
9ca8f72a | 384 | if (status != EFI_SUCCESS) { |
8173ec79 | 385 | efi_printk("Failed to allocate lowmem for boot params\n"); |
c3710de5 | 386 | return status; |
9ca8f72a MF |
387 | } |
388 | ||
389 | memset(boot_params, 0x0, 0x4000); | |
390 | ||
391 | hdr = &boot_params->hdr; | |
9ca8f72a | 392 | bi = &boot_params->apm_bios_info; |
9ca8f72a MF |
393 | |
394 | /* Copy the second sector to boot_params */ | |
395 | memcpy(&hdr->jump, image->image_base + 512, 512); | |
396 | ||
397 | /* | |
398 | * Fill out some of the header fields ourselves because the | |
399 | * EFI firmware loader doesn't load the first sector. | |
400 | */ | |
90a2186b IM |
401 | hdr->root_flags = 1; |
402 | hdr->vid_mode = 0xffff; | |
403 | hdr->boot_flag = 0xAA55; | |
9ca8f72a | 404 | |
291f3632 MF |
405 | hdr->type_of_loader = 0x21; |
406 | ||
407 | /* Convert unicode cmdline to ascii */ | |
cd33a5c1 | 408 | cmdline_ptr = efi_convert_cmdline(image, &options_size); |
5fef3870 RF |
409 | if (!cmdline_ptr) |
410 | goto fail; | |
90a2186b | 411 | |
5fef3870 | 412 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; |
98b228f5 RF |
413 | /* Fill in upper bits of command line address, NOP on 32 bit */ |
414 | boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; | |
291f3632 MF |
415 | |
416 | hdr->ramdisk_image = 0; | |
417 | hdr->ramdisk_size = 0; | |
418 | ||
291f3632 MF |
419 | /* Clear APM BIOS info */ |
420 | memset(bi, 0, sizeof(*bi)); | |
421 | ||
5a17dae4 MF |
422 | status = efi_parse_options(cmdline_ptr); |
423 | if (status != EFI_SUCCESS) | |
424 | goto fail2; | |
425 | ||
cd33a5c1 | 426 | status = handle_cmdline_files(image, |
46f4582e | 427 | (char *)(unsigned long)hdr->cmd_line_ptr, |
47226ad4 | 428 | "initrd=", hdr->initrd_addr_max, |
46f4582e | 429 | &ramdisk_addr, &ramdisk_size); |
47226ad4 YL |
430 | |
431 | if (status != EFI_SUCCESS && | |
432 | hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) { | |
8173ec79 | 433 | efi_printk("Trying to load files to higher address\n"); |
cd33a5c1 | 434 | status = handle_cmdline_files(image, |
47226ad4 YL |
435 | (char *)(unsigned long)hdr->cmd_line_ptr, |
436 | "initrd=", -1UL, | |
437 | &ramdisk_addr, &ramdisk_size); | |
438 | } | |
439 | ||
9ca8f72a MF |
440 | if (status != EFI_SUCCESS) |
441 | goto fail2; | |
4bf7111f YL |
442 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
443 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; | |
444 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; | |
445 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; | |
9ca8f72a | 446 | |
c3710de5 AB |
447 | hdr->code32_start = (u32)(unsigned long)startup_32; |
448 | ||
449 | efi_stub_entry(handle, sys_table, boot_params); | |
450 | /* not reached */ | |
90a2186b | 451 | |
9ca8f72a | 452 | fail2: |
cd33a5c1 | 453 | efi_free(options_size, hdr->cmd_line_ptr); |
9ca8f72a | 454 | fail: |
cd33a5c1 | 455 | efi_free(0x4000, (unsigned long)boot_params); |
90a2186b | 456 | |
c3710de5 | 457 | return status; |
9ca8f72a MF |
458 | } |
459 | ||
d2078d5a LC |
460 | static void add_e820ext(struct boot_params *params, |
461 | struct setup_data *e820ext, u32 nr_entries) | |
9ca8f72a | 462 | { |
d2078d5a | 463 | struct setup_data *data; |
291f3632 | 464 | |
d2078d5a | 465 | e820ext->type = SETUP_E820_EXT; |
90a2186b | 466 | e820ext->len = nr_entries * sizeof(struct boot_e820_entry); |
d2078d5a | 467 | e820ext->next = 0; |
291f3632 | 468 | |
d2078d5a | 469 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
291f3632 | 470 | |
d2078d5a LC |
471 | while (data && data->next) |
472 | data = (struct setup_data *)(unsigned long)data->next; | |
d3768d88 | 473 | |
d2078d5a LC |
474 | if (data) |
475 | data->next = (unsigned long)e820ext; | |
476 | else | |
477 | params->hdr.setup_data = (unsigned long)e820ext; | |
478 | } | |
291f3632 | 479 | |
90a2186b IM |
480 | static efi_status_t |
481 | setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) | |
d2078d5a | 482 | { |
7410aa1c | 483 | struct boot_e820_entry *entry = params->e820_table; |
d2078d5a | 484 | struct efi_info *efi = ¶ms->efi_info; |
7410aa1c | 485 | struct boot_e820_entry *prev = NULL; |
d2078d5a LC |
486 | u32 nr_entries; |
487 | u32 nr_desc; | |
488 | int i; | |
291f3632 | 489 | |
291f3632 | 490 | nr_entries = 0; |
d2078d5a LC |
491 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
492 | ||
493 | for (i = 0; i < nr_desc; i++) { | |
291f3632 MF |
494 | efi_memory_desc_t *d; |
495 | unsigned int e820_type = 0; | |
d2078d5a | 496 | unsigned long m = efi->efi_memmap; |
291f3632 | 497 | |
7cc03e48 DS |
498 | #ifdef CONFIG_X86_64 |
499 | m |= (u64)efi->efi_memmap_hi << 32; | |
500 | #endif | |
501 | ||
02e43c2d | 502 | d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); |
291f3632 MF |
503 | switch (d->type) { |
504 | case EFI_RESERVED_TYPE: | |
505 | case EFI_RUNTIME_SERVICES_CODE: | |
506 | case EFI_RUNTIME_SERVICES_DATA: | |
507 | case EFI_MEMORY_MAPPED_IO: | |
508 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: | |
509 | case EFI_PAL_CODE: | |
09821ff1 | 510 | e820_type = E820_TYPE_RESERVED; |
291f3632 MF |
511 | break; |
512 | ||
513 | case EFI_UNUSABLE_MEMORY: | |
09821ff1 | 514 | e820_type = E820_TYPE_UNUSABLE; |
291f3632 MF |
515 | break; |
516 | ||
517 | case EFI_ACPI_RECLAIM_MEMORY: | |
09821ff1 | 518 | e820_type = E820_TYPE_ACPI; |
291f3632 MF |
519 | break; |
520 | ||
521 | case EFI_LOADER_CODE: | |
522 | case EFI_LOADER_DATA: | |
523 | case EFI_BOOT_SERVICES_CODE: | |
524 | case EFI_BOOT_SERVICES_DATA: | |
525 | case EFI_CONVENTIONAL_MEMORY: | |
262b45ae DW |
526 | if (efi_soft_reserve_enabled() && |
527 | (d->attribute & EFI_MEMORY_SP)) | |
528 | e820_type = E820_TYPE_SOFT_RESERVED; | |
529 | else | |
530 | e820_type = E820_TYPE_RAM; | |
291f3632 MF |
531 | break; |
532 | ||
533 | case EFI_ACPI_MEMORY_NVS: | |
09821ff1 | 534 | e820_type = E820_TYPE_NVS; |
291f3632 MF |
535 | break; |
536 | ||
ad5fb870 | 537 | case EFI_PERSISTENT_MEMORY: |
09821ff1 | 538 | e820_type = E820_TYPE_PMEM; |
ad5fb870 DW |
539 | break; |
540 | ||
291f3632 MF |
541 | default: |
542 | continue; | |
543 | } | |
544 | ||
545 | /* Merge adjacent mappings */ | |
546 | if (prev && prev->type == e820_type && | |
d2078d5a | 547 | (prev->addr + prev->size) == d->phys_addr) { |
291f3632 | 548 | prev->size += d->num_pages << 12; |
d2078d5a | 549 | continue; |
291f3632 | 550 | } |
d2078d5a | 551 | |
61a50101 | 552 | if (nr_entries == ARRAY_SIZE(params->e820_table)) { |
8ec67d97 | 553 | u32 need = (nr_desc - i) * sizeof(struct e820_entry) + |
d2078d5a LC |
554 | sizeof(struct setup_data); |
555 | ||
556 | if (!e820ext || e820ext_size < need) | |
557 | return EFI_BUFFER_TOO_SMALL; | |
558 | ||
559 | /* boot_params map full, switch to e820 extended */ | |
7410aa1c | 560 | entry = (struct boot_e820_entry *)e820ext->data; |
d2078d5a LC |
561 | } |
562 | ||
7410aa1c IM |
563 | entry->addr = d->phys_addr; |
564 | entry->size = d->num_pages << PAGE_SHIFT; | |
565 | entry->type = e820_type; | |
566 | prev = entry++; | |
d2078d5a | 567 | nr_entries++; |
291f3632 MF |
568 | } |
569 | ||
61a50101 IM |
570 | if (nr_entries > ARRAY_SIZE(params->e820_table)) { |
571 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); | |
d2078d5a LC |
572 | |
573 | add_e820ext(params, e820ext, nr_e820ext); | |
574 | nr_entries -= nr_e820ext; | |
575 | } | |
576 | ||
577 | params->e820_entries = (u8)nr_entries; | |
578 | ||
579 | return EFI_SUCCESS; | |
580 | } | |
581 | ||
582 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, | |
583 | u32 *e820ext_size) | |
584 | { | |
585 | efi_status_t status; | |
586 | unsigned long size; | |
587 | ||
588 | size = sizeof(struct setup_data) + | |
8ec67d97 | 589 | sizeof(struct e820_entry) * nr_desc; |
d2078d5a LC |
590 | |
591 | if (*e820ext) { | |
966291f6 | 592 | efi_bs_call(free_pool, *e820ext); |
d2078d5a LC |
593 | *e820ext = NULL; |
594 | *e820ext_size = 0; | |
595 | } | |
596 | ||
966291f6 AB |
597 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
598 | (void **)e820ext); | |
d2078d5a LC |
599 | if (status == EFI_SUCCESS) |
600 | *e820ext_size = size; | |
601 | ||
602 | return status; | |
603 | } | |
604 | ||
b84a64fa ES |
605 | static efi_status_t allocate_e820(struct boot_params *params, |
606 | struct setup_data **e820ext, | |
607 | u32 *e820ext_size) | |
608 | { | |
609 | unsigned long map_size, desc_size, buff_size; | |
610 | struct efi_boot_memmap boot_map; | |
611 | efi_memory_desc_t *map; | |
612 | efi_status_t status; | |
613 | __u32 nr_desc; | |
614 | ||
615 | boot_map.map = ↦ | |
616 | boot_map.map_size = &map_size; | |
617 | boot_map.desc_size = &desc_size; | |
618 | boot_map.desc_ver = NULL; | |
619 | boot_map.key_ptr = NULL; | |
620 | boot_map.buff_size = &buff_size; | |
621 | ||
cd33a5c1 | 622 | status = efi_get_memory_map(&boot_map); |
b84a64fa ES |
623 | if (status != EFI_SUCCESS) |
624 | return status; | |
625 | ||
626 | nr_desc = buff_size / desc_size; | |
627 | ||
628 | if (nr_desc > ARRAY_SIZE(params->e820_table)) { | |
629 | u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); | |
630 | ||
631 | status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); | |
632 | if (status != EFI_SUCCESS) | |
633 | return status; | |
634 | } | |
635 | ||
636 | return EFI_SUCCESS; | |
637 | } | |
638 | ||
d6493401 | 639 | struct exit_boot_struct { |
90a2186b IM |
640 | struct boot_params *boot_params; |
641 | struct efi_info *efi; | |
d6493401 JH |
642 | }; |
643 | ||
cd33a5c1 | 644 | static efi_status_t exit_boot_func(struct efi_boot_memmap *map, |
d6493401 JH |
645 | void *priv) |
646 | { | |
d6493401 | 647 | const char *signature; |
d6493401 JH |
648 | struct exit_boot_struct *p = priv; |
649 | ||
aab9593c AB |
650 | signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE |
651 | : EFI32_LOADER_SIGNATURE; | |
d6493401 JH |
652 | memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); |
653 | ||
cd33a5c1 | 654 | p->efi->efi_systab = (unsigned long)efi_system_table(); |
90a2186b IM |
655 | p->efi->efi_memdesc_size = *map->desc_size; |
656 | p->efi->efi_memdesc_version = *map->desc_ver; | |
657 | p->efi->efi_memmap = (unsigned long)*map->map; | |
658 | p->efi->efi_memmap_size = *map->map_size; | |
d6493401 JH |
659 | |
660 | #ifdef CONFIG_X86_64 | |
cd33a5c1 | 661 | p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32; |
90a2186b | 662 | p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32; |
d6493401 JH |
663 | #endif |
664 | ||
665 | return EFI_SUCCESS; | |
666 | } | |
667 | ||
aab9593c | 668 | static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) |
d2078d5a | 669 | { |
dadb57ab | 670 | unsigned long map_sz, key, desc_size, buff_size; |
d2078d5a | 671 | efi_memory_desc_t *mem_map; |
b84a64fa ES |
672 | struct setup_data *e820ext = NULL; |
673 | __u32 e820ext_size = 0; | |
d2078d5a LC |
674 | efi_status_t status; |
675 | __u32 desc_version; | |
dadb57ab | 676 | struct efi_boot_memmap map; |
d6493401 JH |
677 | struct exit_boot_struct priv; |
678 | ||
90a2186b IM |
679 | map.map = &mem_map; |
680 | map.map_size = &map_sz; | |
681 | map.desc_size = &desc_size; | |
682 | map.desc_ver = &desc_version; | |
683 | map.key_ptr = &key; | |
684 | map.buff_size = &buff_size; | |
685 | priv.boot_params = boot_params; | |
686 | priv.efi = &boot_params->efi_info; | |
b84a64fa ES |
687 | |
688 | status = allocate_e820(boot_params, &e820ext, &e820ext_size); | |
689 | if (status != EFI_SUCCESS) | |
690 | return status; | |
dadb57ab | 691 | |
d6493401 | 692 | /* Might as well exit boot services now */ |
cd33a5c1 | 693 | status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func); |
d2078d5a LC |
694 | if (status != EFI_SUCCESS) |
695 | return status; | |
696 | ||
d2078d5a | 697 | /* Historic? */ |
90a2186b | 698 | boot_params->alt_mem_k = 32 * 1024; |
d2078d5a LC |
699 | |
700 | status = setup_e820(boot_params, e820ext, e820ext_size); | |
701 | if (status != EFI_SUCCESS) | |
702 | return status; | |
291f3632 MF |
703 | |
704 | return EFI_SUCCESS; | |
291f3632 MF |
705 | } |
706 | ||
9ca8f72a MF |
707 | /* |
708 | * On success we return a pointer to a boot_params structure, and NULL | |
709 | * on failure. | |
710 | */ | |
c3710de5 AB |
711 | struct boot_params *efi_main(efi_handle_t handle, |
712 | efi_system_table_t *sys_table_arg, | |
796eb8d2 | 713 | struct boot_params *boot_params) |
9ca8f72a | 714 | { |
54b52d87 | 715 | struct desc_ptr *gdt = NULL; |
9ca8f72a MF |
716 | struct setup_header *hdr = &boot_params->hdr; |
717 | efi_status_t status; | |
718 | struct desc_struct *desc; | |
c33ce984 | 719 | unsigned long cmdline_paddr; |
54b52d87 | 720 | |
c3710de5 | 721 | sys_table = sys_table_arg; |
9ca8f72a | 722 | |
9ca8f72a MF |
723 | /* Check if we were booted by the EFI firmware */ |
724 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
725 | goto fail; | |
726 | ||
c33ce984 HG |
727 | /* |
728 | * make_boot_params() may have been called before efi_main(), in which | |
729 | * case this is the second time we parse the cmdline. This is ok, | |
730 | * parsing the cmdline multiple times does not have side-effects. | |
731 | */ | |
732 | cmdline_paddr = ((u64)hdr->cmd_line_ptr | | |
733 | ((u64)boot_params->ext_cmd_line_ptr << 32)); | |
734 | efi_parse_options((char *)cmdline_paddr); | |
735 | ||
de8cb458 DH |
736 | /* |
737 | * If the boot loader gave us a value for secure_boot then we use that, | |
738 | * otherwise we ask the BIOS. | |
739 | */ | |
740 | if (boot_params->secure_boot == efi_secureboot_mode_unset) | |
cd33a5c1 | 741 | boot_params->secure_boot = efi_get_secureboot(); |
ccc829ba MG |
742 | |
743 | /* Ask the firmware to clear memory on unclean shutdown */ | |
cd33a5c1 | 744 | efi_enable_reset_attack_mitigation(); |
0d959814 | 745 | |
cd33a5c1 | 746 | efi_random_get_seed(); |
0d959814 | 747 | |
cd33a5c1 | 748 | efi_retrieve_tpm2_eventlog(); |
de8cb458 | 749 | |
9ca8f72a | 750 | setup_graphics(boot_params); |
291f3632 | 751 | |
56394ab8 | 752 | setup_efi_pci(boot_params); |
dd5fc854 | 753 | |
58c5475a LW |
754 | setup_quirks(boot_params); |
755 | ||
966291f6 AB |
756 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*gdt), |
757 | (void **)&gdt); | |
9fa7deda | 758 | if (status != EFI_SUCCESS) { |
8173ec79 | 759 | efi_printk("Failed to allocate memory for 'gdt' structure\n"); |
291f3632 | 760 | goto fail; |
9fa7deda | 761 | } |
291f3632 MF |
762 | |
763 | gdt->size = 0x800; | |
cd33a5c1 | 764 | status = efi_low_alloc(gdt->size, 8, (unsigned long *)&gdt->address); |
9fa7deda | 765 | if (status != EFI_SUCCESS) { |
8173ec79 | 766 | efi_printk("Failed to allocate memory for 'gdt'\n"); |
291f3632 | 767 | goto fail; |
9fa7deda | 768 | } |
291f3632 | 769 | |
9ca8f72a MF |
770 | /* |
771 | * If the kernel isn't already loaded at the preferred load | |
772 | * address, relocate it. | |
773 | */ | |
774 | if (hdr->pref_address != hdr->code32_start) { | |
4a9f3a7c | 775 | unsigned long bzimage_addr = hdr->code32_start; |
cd33a5c1 | 776 | status = efi_relocate_kernel(&bzimage_addr, |
4a9f3a7c RF |
777 | hdr->init_size, hdr->init_size, |
778 | hdr->pref_address, | |
220dd769 KS |
779 | hdr->kernel_alignment, |
780 | LOAD_PHYSICAL_ADDR); | |
fb86b244 | 781 | if (status != EFI_SUCCESS) { |
8173ec79 | 782 | efi_printk("efi_relocate_kernel() failed!\n"); |
9ca8f72a | 783 | goto fail; |
fb86b244 | 784 | } |
4a9f3a7c RF |
785 | |
786 | hdr->pref_address = hdr->code32_start; | |
787 | hdr->code32_start = bzimage_addr; | |
9ca8f72a MF |
788 | } |
789 | ||
aab9593c | 790 | status = exit_boot(boot_params, handle); |
fb86b244 | 791 | if (status != EFI_SUCCESS) { |
8173ec79 | 792 | efi_printk("exit_boot() failed!\n"); |
291f3632 | 793 | goto fail; |
fb86b244 | 794 | } |
291f3632 MF |
795 | |
796 | memset((char *)gdt->address, 0x0, gdt->size); | |
797 | desc = (struct desc_struct *)gdt->address; | |
798 | ||
919a02d1 KS |
799 | /* The first GDT is a dummy. */ |
800 | desc++; | |
801 | ||
802 | if (IS_ENABLED(CONFIG_X86_64)) { | |
803 | /* __KERNEL32_CS */ | |
90a2186b IM |
804 | desc->limit0 = 0xffff; |
805 | desc->base0 = 0x0000; | |
806 | desc->base1 = 0x0000; | |
807 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; | |
808 | desc->s = DESC_TYPE_CODE_DATA; | |
809 | desc->dpl = 0; | |
810 | desc->p = 1; | |
811 | desc->limit1 = 0xf; | |
812 | desc->avl = 0; | |
813 | desc->l = 0; | |
814 | desc->d = SEG_OP_SIZE_32BIT; | |
815 | desc->g = SEG_GRANULARITY_4KB; | |
816 | desc->base2 = 0x00; | |
817 | ||
919a02d1 KS |
818 | desc++; |
819 | } else { | |
820 | /* Second entry is unused on 32-bit */ | |
821 | desc++; | |
822 | } | |
291f3632 | 823 | |
f8fceacb | 824 | /* __KERNEL_CS */ |
90a2186b IM |
825 | desc->limit0 = 0xffff; |
826 | desc->base0 = 0x0000; | |
827 | desc->base1 = 0x0000; | |
828 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; | |
829 | desc->s = DESC_TYPE_CODE_DATA; | |
830 | desc->dpl = 0; | |
831 | desc->p = 1; | |
832 | desc->limit1 = 0xf; | |
833 | desc->avl = 0; | |
834 | ||
4c94117c KS |
835 | if (IS_ENABLED(CONFIG_X86_64)) { |
836 | desc->l = 1; | |
837 | desc->d = 0; | |
838 | } else { | |
839 | desc->l = 0; | |
840 | desc->d = SEG_OP_SIZE_32BIT; | |
841 | } | |
90a2186b IM |
842 | desc->g = SEG_GRANULARITY_4KB; |
843 | desc->base2 = 0x00; | |
291f3632 | 844 | desc++; |
f8fceacb KS |
845 | |
846 | /* __KERNEL_DS */ | |
90a2186b IM |
847 | desc->limit0 = 0xffff; |
848 | desc->base0 = 0x0000; | |
849 | desc->base1 = 0x0000; | |
850 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; | |
851 | desc->s = DESC_TYPE_CODE_DATA; | |
852 | desc->dpl = 0; | |
853 | desc->p = 1; | |
854 | desc->limit1 = 0xf; | |
855 | desc->avl = 0; | |
856 | desc->l = 0; | |
857 | desc->d = SEG_OP_SIZE_32BIT; | |
858 | desc->g = SEG_GRANULARITY_4KB; | |
859 | desc->base2 = 0x00; | |
291f3632 | 860 | desc++; |
f8fceacb KS |
861 | |
862 | if (IS_ENABLED(CONFIG_X86_64)) { | |
863 | /* Task segment value */ | |
90a2186b IM |
864 | desc->limit0 = 0x0000; |
865 | desc->base0 = 0x0000; | |
866 | desc->base1 = 0x0000; | |
867 | desc->type = SEG_TYPE_TSS; | |
868 | desc->s = 0; | |
869 | desc->dpl = 0; | |
870 | desc->p = 1; | |
871 | desc->limit1 = 0x0; | |
872 | desc->avl = 0; | |
873 | desc->l = 0; | |
874 | desc->d = 0; | |
875 | desc->g = SEG_GRANULARITY_4KB; | |
876 | desc->base2 = 0x00; | |
f8fceacb KS |
877 | desc++; |
878 | } | |
291f3632 | 879 | |
291f3632 | 880 | asm volatile("cli"); |
0ce6cda2 | 881 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
291f3632 MF |
882 | |
883 | return boot_params; | |
884 | fail: | |
8173ec79 | 885 | efi_printk("efi_main() failed!\n"); |
90a2186b | 886 | |
c3710de5 AB |
887 | for (;;) |
888 | asm("hlt"); | |
291f3632 | 889 | } |