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