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