blob: 01ddd4502e28a116f553ffb5b3383e76def9fded [file] [log] [blame]
Thomas Gleixner97873a32019-06-04 10:11:30 +02001// SPDX-License-Identifier: GPL-2.0-only
Eric Snowbergb84a64f2018-11-29 18:12:20 +01002
Matt Fleming291f3632011-12-12 21:27:52 +00003/* -----------------------------------------------------------------------
4 *
5 * Copyright 2011 Intel Corporation; author Matt Fleming
6 *
Matt Fleming291f3632011-12-12 21:27:52 +00007 * ----------------------------------------------------------------------- */
8
9#include <linux/efi.h>
Matthew Garrettdd5fc852012-12-05 14:33:26 -070010#include <linux/pci.h>
Arvind Sankar59476f802020-06-18 16:43:15 -040011#include <linux/stddef.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010012
Matt Fleming291f3632011-12-12 21:27:52 +000013#include <asm/efi.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010014#include <asm/e820/types.h>
Matt Fleming291f3632011-12-12 21:27:52 +000015#include <asm/setup.h>
16#include <asm/desc.h>
Kairui Song220dd762019-10-29 18:37:54 +010017#include <asm/boot.h>
Matt Fleming291f3632011-12-12 21:27:52 +000018
Ard Biesheuvelc2d0b472020-02-10 17:02:36 +010019#include "efistub.h"
Matt Fleming291f3632011-12-12 21:27:52 +000020
Arvind Sankard5cdf4c2020-03-08 09:08:50 +010021/* Maximum physical address for 64-bit kernel with 4-level paging */
22#define MAXMEM_X86_64_4LEVEL (1ull << 46)
23
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +020024const efi_system_table_t *efi_system_table;
Arvind Sankar1887c9b2020-03-08 09:08:47 +010025extern u32 image_offset;
Arvind Sankar987053a2020-04-30 14:28:40 -040026static efi_loaded_image_t *image = NULL;
Matt Fleming204b0a12014-03-22 10:09:01 +000027
Matt Flemingc116e8d2014-01-16 11:35:43 +000028static efi_status_t
Ard Biesheuvel75c5a712018-07-20 10:47:19 +090029preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -070030{
Matt Flemingc116e8d2014-01-16 11:35:43 +000031 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -070032 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +000033 unsigned long size;
Ard Biesheuvele2967012018-07-11 11:02:35 +020034 uint64_t romsize;
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020035 void *romimage;
Matt Flemingc116e8d2014-01-16 11:35:43 +000036
Hans de Goede1de3a1b2018-05-04 08:00:01 +020037 /*
Ard Biesheuvele2967012018-07-11 11:02:35 +020038 * Some firmware images contain EFI function pointers at the place where
39 * the romimage and romsize fields are supposed to be. Typically the EFI
Hans de Goede1de3a1b2018-05-04 08:00:01 +020040 * code is mapped at high addresses, translating to an unrealistically
41 * large romsize. The UEFI spec limits the size of option ROMs to 16
42 * MiB so we reject any ROMs over 16 MiB in size to catch this.
43 */
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +010044 romimage = efi_table_attr(pci, romimage);
45 romsize = efi_table_attr(pci, romsize);
Hans de Goede1de3a1b2018-05-04 08:00:01 +020046 if (!romimage || !romsize || romsize > SZ_16M)
Matt Flemingc116e8d2014-01-16 11:35:43 +000047 return EFI_INVALID_PARAMETER;
48
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020049 size = romsize + sizeof(*rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000050
Ard Biesheuvel966291f2019-12-24 16:10:23 +010051 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
52 (void **)&rom);
Andre Müller77e21e82014-09-10 01:00:22 +020053 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -040054 efi_err("Failed to allocate memory for 'rom'\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000055 return status;
Andre Müller77e21e82014-09-10 01:00:22 +020056 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000057
58 memset(rom, 0, sizeof(*rom));
59
Ingo Molnar90a21862018-07-11 11:40:33 +020060 rom->data.type = SETUP_PCI;
61 rom->data.len = size - sizeof(struct setup_data);
62 rom->data.next = 0;
63 rom->pcilen = pci->romsize;
Matt Flemingc116e8d2014-01-16 11:35:43 +000064 *__rom = rom;
65
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010066 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
67 PCI_VENDOR_ID, 1, &rom->vendor);
Matt Flemingc116e8d2014-01-16 11:35:43 +000068
Andre Müller77e21e82014-09-10 01:00:22 +020069 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -040070 efi_err("Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000071 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020072 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000073
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010074 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
75 PCI_DEVICE_ID, 1, &rom->devid);
Matt Flemingc116e8d2014-01-16 11:35:43 +000076
Andre Müller77e21e82014-09-10 01:00:22 +020077 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -040078 efi_err("Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000079 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020080 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000081
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010082 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
83 &rom->device, &rom->function);
Matt Flemingc116e8d2014-01-16 11:35:43 +000084
85 if (status != EFI_SUCCESS)
86 goto free_struct;
87
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020088 memcpy(rom->romdata, romimage, romsize);
Matt Flemingc116e8d2014-01-16 11:35:43 +000089 return status;
90
91free_struct:
Ard Biesheuvel966291f2019-12-24 16:10:23 +010092 efi_bs_call(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000093 return status;
94}
95
Matt Fleming56394ab2014-09-11 09:04:25 +010096/*
97 * There's no way to return an informative status from this function,
98 * because any analysis (and printing of error messages) needs to be
99 * done directly at the EFI function call-site.
100 *
101 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
102 * just didn't find any PCI devices, but there's no way to tell outside
103 * the context of the call.
104 */
105static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000106{
107 efi_status_t status;
108 void **pci_handle = NULL;
109 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
110 unsigned long size = 0;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900111 struct setup_data *data;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100112 efi_handle_t h;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900113 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000114
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100115 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
116 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700117
118 if (status == EFI_BUFFER_TOO_SMALL) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100119 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
120 (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700121
Andre Müller77e21e82014-09-10 01:00:22 +0200122 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400123 efi_err("Failed to allocate memory for 'pci_handle'\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100124 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200125 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700126
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100127 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700129 }
130
131 if (status != EFI_SUCCESS)
132 goto free_handle;
133
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900134 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
135
136 while (data && data->next)
137 data = (struct setup_data *)(unsigned long)data->next;
138
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100139 for_each_efi_handle(h, pci_handle, size, i) {
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900140 efi_pci_io_protocol_t *pci = NULL;
141 struct pci_setup_rom *rom;
142
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100143 status = efi_bs_call(handle_protocol, h, &pci_proto,
144 (void **)&pci);
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900145 if (status != EFI_SUCCESS || !pci)
146 continue;
147
148 status = preserve_pci_rom_image(pci, &rom);
149 if (status != EFI_SUCCESS)
150 continue;
151
152 if (data)
153 data->next = (unsigned long)rom;
154 else
155 params->hdr.setup_data = (unsigned long)rom;
156
157 data = (struct setup_data *)rom;
158 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700159
160free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100161 efi_bs_call(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700162}
163
Lukas Wunner58c54752016-11-12 21:32:36 +0000164static void retrieve_apple_device_properties(struct boot_params *boot_params)
165{
166 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
167 struct setup_data *data, *new;
168 efi_status_t status;
169 u32 size = 0;
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100170 apple_properties_protocol_t *p;
Lukas Wunner58c54752016-11-12 21:32:36 +0000171
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100172 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
Lukas Wunner58c54752016-11-12 21:32:36 +0000173 if (status != EFI_SUCCESS)
174 return;
175
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +0100176 if (efi_table_attr(p, version) != 0x10000) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400177 efi_err("Unsupported properties proto version\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000178 return;
179 }
180
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100181 efi_call_proto(p, get_all, NULL, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000182 if (!size)
183 return;
184
185 do {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100186 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
187 size + sizeof(struct setup_data),
188 (void **)&new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000189 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400190 efi_err("Failed to allocate memory for 'properties'\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000191 return;
192 }
193
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100194 status = efi_call_proto(p, get_all, new->data, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000195
196 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100197 efi_bs_call(free_pool, new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000198 } while (status == EFI_BUFFER_TOO_SMALL);
199
200 new->type = SETUP_APPLE_PROPERTIES;
201 new->len = size;
202 new->next = 0;
203
204 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Ingo Molnar90a21862018-07-11 11:40:33 +0200205 if (!data) {
Lukas Wunner58c54752016-11-12 21:32:36 +0000206 boot_params->hdr.setup_data = (unsigned long)new;
Ingo Molnar90a21862018-07-11 11:40:33 +0200207 } else {
Lukas Wunner58c54752016-11-12 21:32:36 +0000208 while (data->next)
209 data = (struct setup_data *)(unsigned long)data->next;
210 data->next = (unsigned long)new;
211 }
212}
213
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000214static const efi_char16_t apple[] = L"Apple";
215
Lukas Wunner58c54752016-11-12 21:32:36 +0000216static void setup_quirks(struct boot_params *boot_params)
217{
Lukas Wunner58c54752016-11-12 21:32:36 +0000218 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200219 efi_table_attr(efi_system_table, fw_vendor);
Lukas Wunner58c54752016-11-12 21:32:36 +0000220
221 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
222 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
223 retrieve_apple_device_properties(boot_params);
224 }
225}
226
Matt Flemingc116e8d2014-01-16 11:35:43 +0000227/*
228 * See if we have Universal Graphics Adapter (UGA) protocol
229 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200230static efi_status_t
231setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000232{
233 efi_status_t status;
234 u32 width, height;
235 void **uga_handle = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900236 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100237 efi_handle_t handle;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900238 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000239
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100240 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
241 (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000242 if (status != EFI_SUCCESS)
243 return status;
244
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100245 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
246 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000247 if (status != EFI_SUCCESS)
248 goto free_handle;
249
250 height = 0;
251 width = 0;
252
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900253 first_uga = NULL;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100254 for_each_efi_handle(handle, uga_handle, size, i) {
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900255 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
256 u32 w, h, depth, refresh;
257 void *pciio;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900258
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100259 status = efi_bs_call(handle_protocol, handle, uga_proto,
260 (void **)&uga);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900261 if (status != EFI_SUCCESS)
262 continue;
263
Ard Biesheuvel093174f2018-07-20 10:47:22 +0900264 pciio = NULL;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100265 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900266
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100267 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900268 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
269 width = w;
270 height = h;
271
272 /*
273 * Once we've found a UGA supporting PCIIO,
274 * don't bother looking any further.
275 */
276 if (pciio)
277 break;
278
279 first_uga = uga;
280 }
281 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000282
283 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000284 goto free_handle;
285
286 /* EFI framebuffer */
Ingo Molnar90a21862018-07-11 11:40:33 +0200287 si->orig_video_isVGA = VIDEO_TYPE_EFI;
Matt Fleming291f3632011-12-12 21:27:52 +0000288
Ingo Molnar90a21862018-07-11 11:40:33 +0200289 si->lfb_depth = 32;
290 si->lfb_width = width;
291 si->lfb_height = height;
Matt Fleming291f3632011-12-12 21:27:52 +0000292
Ingo Molnar90a21862018-07-11 11:40:33 +0200293 si->red_size = 8;
294 si->red_pos = 16;
295 si->green_size = 8;
296 si->green_pos = 8;
297 si->blue_size = 8;
298 si->blue_pos = 0;
299 si->rsvd_size = 8;
300 si->rsvd_pos = 24;
Matt Fleming291f3632011-12-12 21:27:52 +0000301
Matt Fleming291f3632011-12-12 21:27:52 +0000302free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100303 efi_bs_call(free_pool, uga_handle);
Ingo Molnar90a21862018-07-11 11:40:33 +0200304
Matt Fleming291f3632011-12-12 21:27:52 +0000305 return status;
306}
307
Arvind Sankarf32ea1c2020-01-30 17:20:04 -0500308static void setup_graphics(struct boot_params *boot_params)
Matt Fleming291f3632011-12-12 21:27:52 +0000309{
310 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
311 struct screen_info *si;
312 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
313 efi_status_t status;
314 unsigned long size;
315 void **gop_handle = NULL;
316 void **uga_handle = NULL;
317
318 si = &boot_params->screen_info;
319 memset(si, 0, sizeof(*si));
320
321 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100322 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
323 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000324 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100325 status = efi_setup_gop(si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000326
327 if (status != EFI_SUCCESS) {
328 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100329 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
330 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000331 if (status == EFI_BUFFER_TOO_SMALL)
332 setup_uga(si, &uga_proto, size);
333 }
334}
335
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100336
337static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
338{
339 efi_bs_call(exit, handle, status, 0, NULL);
Ard Biesheuvelf3fa0ef2020-03-08 09:08:45 +0100340 for(;;)
341 asm("hlt");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100342}
343
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100344void startup_32(struct boot_params *boot_params);
345
346void __noreturn efi_stub_entry(efi_handle_t handle,
347 efi_system_table_t *sys_table_arg,
348 struct boot_params *boot_params);
349
Matt Fleming291f3632011-12-12 21:27:52 +0000350/*
351 * Because the x86 boot code expects to be passed a boot_params we
352 * need to create one ourselves (usually the bootloader would create
353 * one for us).
354 */
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100355efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
356 efi_system_table_t *sys_table_arg)
Matt Fleming291f3632011-12-12 21:27:52 +0000357{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100358 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100359 struct setup_header *hdr;
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100360 void *image_base;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100361 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000362 int options_size = 0;
363 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700364 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +0000365
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200366 efi_system_table = sys_table_arg;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100367
368 /* Check if we were booted by the EFI firmware */
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200369 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100370 efi_exit(handle, EFI_INVALID_PARAMETER);
Matt Fleming54b52d82014-01-10 15:27:14 +0000371
Arvind Sankar0347d8c22020-03-08 09:08:58 +0100372 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100373 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400374 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100375 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100376 }
377
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100378 image_base = efi_table_attr(image, image_base);
379 image_offset = (void *)startup_32 - image_base;
380
Arvind Sankar019512f2020-04-30 14:28:33 -0400381 status = efi_allocate_pages(sizeof(struct boot_params),
382 (unsigned long *)&boot_params, ULONG_MAX);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100383 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400384 efi_err("Failed to allocate lowmem for boot params\n");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100385 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100386 }
387
Arvind Sankar019512f2020-04-30 14:28:33 -0400388 memset(boot_params, 0x0, sizeof(struct boot_params));
Matt Fleming9ca8f722012-07-19 10:23:48 +0100389
390 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100391
Arvind Sankar59476f802020-06-18 16:43:15 -0400392 /* Copy the setup header from the second sector to boot_params */
393 memcpy(&hdr->jump, image_base + 512,
394 sizeof(struct setup_header) - offsetof(struct setup_header, jump));
Matt Fleming9ca8f722012-07-19 10:23:48 +0100395
396 /*
397 * Fill out some of the header fields ourselves because the
398 * EFI firmware loader doesn't load the first sector.
399 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200400 hdr->root_flags = 1;
401 hdr->vid_mode = 0xffff;
402 hdr->boot_flag = 0xAA55;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100403
Matt Fleming291f3632011-12-12 21:27:52 +0000404 hdr->type_of_loader = 0x21;
405
406 /* Convert unicode cmdline to ascii */
Ard Biesheuvel27cd5512020-05-19 10:43:01 +0200407 cmdline_ptr = efi_convert_cmdline(image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700408 if (!cmdline_ptr)
409 goto fail;
Ingo Molnar90a21862018-07-11 11:40:33 +0200410
Arvind Sankareed4e012020-04-30 14:28:34 -0400411 efi_set_u64_split((unsigned long)cmdline_ptr,
412 &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
Matt Fleming291f3632011-12-12 21:27:52 +0000413
414 hdr->ramdisk_image = 0;
415 hdr->ramdisk_size = 0;
416
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200417 efi_stub_entry(handle, sys_table_arg, boot_params);
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100418 /* not reached */
Ingo Molnar90a21862018-07-11 11:40:33 +0200419
Matt Fleming9ca8f722012-07-19 10:23:48 +0100420fail:
Arvind Sankar019512f2020-04-30 14:28:33 -0400421 efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
Ingo Molnar90a21862018-07-11 11:40:33 +0200422
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100423 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100424}
425
Linn Crosettod2078d52013-09-22 19:59:08 -0600426static void add_e820ext(struct boot_params *params,
427 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100428{
Linn Crosettod2078d52013-09-22 19:59:08 -0600429 struct setup_data *data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600430
431 e820ext->type = SETUP_E820_EXT;
Ingo Molnar90a21862018-07-11 11:40:33 +0200432 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600433 e820ext->next = 0;
434
435 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
436
437 while (data && data->next)
438 data = (struct setup_data *)(unsigned long)data->next;
439
440 if (data)
441 data->next = (unsigned long)e820ext;
442 else
443 params->hdr.setup_data = (unsigned long)e820ext;
444}
445
Ingo Molnar90a21862018-07-11 11:40:33 +0200446static efi_status_t
447setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
Linn Crosettod2078d52013-09-22 19:59:08 -0600448{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100449 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600450 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100451 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600452 u32 nr_entries;
453 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100454 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000455
Matt Fleming291f3632011-12-12 21:27:52 +0000456 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600457 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
458
459 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000460 efi_memory_desc_t *d;
461 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600462 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000463
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400464#ifdef CONFIG_X86_64
465 m |= (u64)efi->efi_memmap_hi << 32;
466#endif
467
Baoquan He02e43c22017-08-16 21:46:51 +0800468 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000469 switch (d->type) {
470 case EFI_RESERVED_TYPE:
471 case EFI_RUNTIME_SERVICES_CODE:
472 case EFI_RUNTIME_SERVICES_DATA:
473 case EFI_MEMORY_MAPPED_IO:
474 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
475 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100476 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000477 break;
478
479 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100480 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000481 break;
482
483 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100484 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000485 break;
486
487 case EFI_LOADER_CODE:
488 case EFI_LOADER_DATA:
489 case EFI_BOOT_SERVICES_CODE:
490 case EFI_BOOT_SERVICES_DATA:
491 case EFI_CONVENTIONAL_MEMORY:
Dan Williams262b45a2019-11-06 17:43:16 -0800492 if (efi_soft_reserve_enabled() &&
493 (d->attribute & EFI_MEMORY_SP))
494 e820_type = E820_TYPE_SOFT_RESERVED;
495 else
496 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000497 break;
498
499 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100500 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000501 break;
502
Dan Williamsad5fb872015-04-03 12:05:28 -0400503 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100504 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400505 break;
506
Matt Fleming291f3632011-12-12 21:27:52 +0000507 default:
508 continue;
509 }
510
511 /* Merge adjacent mappings */
512 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600513 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000514 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600515 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000516 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600517
Ingo Molnar61a50102017-01-27 13:54:38 +0100518 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100519 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600520 sizeof(struct setup_data);
521
522 if (!e820ext || e820ext_size < need)
523 return EFI_BUFFER_TOO_SMALL;
524
525 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100526 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600527 }
528
Ingo Molnar7410aa12017-01-29 12:56:13 +0100529 entry->addr = d->phys_addr;
530 entry->size = d->num_pages << PAGE_SHIFT;
531 entry->type = e820_type;
532 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600533 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000534 }
535
Ingo Molnar61a50102017-01-27 13:54:38 +0100536 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
537 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600538
539 add_e820ext(params, e820ext, nr_e820ext);
540 nr_entries -= nr_e820ext;
541 }
542
543 params->e820_entries = (u8)nr_entries;
544
545 return EFI_SUCCESS;
546}
547
548static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
549 u32 *e820ext_size)
550{
551 efi_status_t status;
552 unsigned long size;
553
554 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100555 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600556
557 if (*e820ext) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100558 efi_bs_call(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600559 *e820ext = NULL;
560 *e820ext_size = 0;
561 }
562
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100563 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
564 (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600565 if (status == EFI_SUCCESS)
566 *e820ext_size = size;
567
568 return status;
569}
570
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100571static efi_status_t allocate_e820(struct boot_params *params,
572 struct setup_data **e820ext,
573 u32 *e820ext_size)
574{
Lenny Szubowiczfd626192020-05-07 14:33:32 -0400575 unsigned long map_size, desc_size, map_key;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100576 efi_status_t status;
Lenny Szubowiczfd626192020-05-07 14:33:32 -0400577 __u32 nr_desc, desc_version;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100578
Lenny Szubowiczfd626192020-05-07 14:33:32 -0400579 /* Only need the size of the mem map and size of each mem descriptor */
580 map_size = 0;
581 status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
582 &desc_size, &desc_version);
583 if (status != EFI_BUFFER_TOO_SMALL)
584 return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100585
Lenny Szubowiczfd626192020-05-07 14:33:32 -0400586 nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100587
588 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
589 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
590
591 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
592 if (status != EFI_SUCCESS)
593 return status;
594 }
595
596 return EFI_SUCCESS;
597}
598
Jeffrey Hugod6493402016-08-29 14:38:54 -0600599struct exit_boot_struct {
Ingo Molnar90a21862018-07-11 11:40:33 +0200600 struct boot_params *boot_params;
601 struct efi_info *efi;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600602};
603
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100604static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
Jeffrey Hugod6493402016-08-29 14:38:54 -0600605 void *priv)
606{
Jeffrey Hugod6493402016-08-29 14:38:54 -0600607 const char *signature;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600608 struct exit_boot_struct *p = priv;
609
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900610 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
611 : EFI32_LOADER_SIGNATURE;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600612 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
613
Arvind Sankareed4e012020-04-30 14:28:34 -0400614 efi_set_u64_split((unsigned long)efi_system_table,
615 &p->efi->efi_systab, &p->efi->efi_systab_hi);
Ingo Molnar90a21862018-07-11 11:40:33 +0200616 p->efi->efi_memdesc_size = *map->desc_size;
617 p->efi->efi_memdesc_version = *map->desc_ver;
Arvind Sankareed4e012020-04-30 14:28:34 -0400618 efi_set_u64_split((unsigned long)*map->map,
619 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
Ingo Molnar90a21862018-07-11 11:40:33 +0200620 p->efi->efi_memmap_size = *map->map_size;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600621
Jeffrey Hugod6493402016-08-29 14:38:54 -0600622 return EFI_SUCCESS;
623}
624
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900625static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
Linn Crosettod2078d52013-09-22 19:59:08 -0600626{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600627 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600628 efi_memory_desc_t *mem_map;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100629 struct setup_data *e820ext = NULL;
630 __u32 e820ext_size = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600631 efi_status_t status;
632 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600633 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600634 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600635
Ingo Molnar90a21862018-07-11 11:40:33 +0200636 map.map = &mem_map;
637 map.map_size = &map_sz;
638 map.desc_size = &desc_size;
639 map.desc_ver = &desc_version;
640 map.key_ptr = &key;
641 map.buff_size = &buff_size;
642 priv.boot_params = boot_params;
643 priv.efi = &boot_params->efi_info;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100644
645 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
646 if (status != EFI_SUCCESS)
647 return status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600648
Jeffrey Hugod6493402016-08-29 14:38:54 -0600649 /* Might as well exit boot services now */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100650 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600651 if (status != EFI_SUCCESS)
652 return status;
653
Linn Crosettod2078d52013-09-22 19:59:08 -0600654 /* Historic? */
Ingo Molnar90a21862018-07-11 11:40:33 +0200655 boot_params->alt_mem_k = 32 * 1024;
Linn Crosettod2078d52013-09-22 19:59:08 -0600656
657 status = setup_e820(boot_params, e820ext, e820ext_size);
658 if (status != EFI_SUCCESS)
659 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000660
661 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000662}
663
Matt Fleming9ca8f722012-07-19 10:23:48 +0100664/*
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100665 * On success, we return the address of startup_32, which has potentially been
666 * relocated by efi_relocate_kernel.
667 * On failure, we exit to the firmware via efi_exit instead of returning.
Matt Fleming9ca8f722012-07-19 10:23:48 +0100668 */
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100669unsigned long efi_main(efi_handle_t handle,
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100670 efi_system_table_t *sys_table_arg,
Ard Biesheuvel796eb8d2020-01-13 18:22:33 +0100671 struct boot_params *boot_params)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100672{
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100673 unsigned long bzimage_addr = (unsigned long)startup_32;
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100674 unsigned long buffer_start, buffer_end;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100675 struct setup_header *hdr = &boot_params->hdr;
Ard Biesheuvel20287d52021-11-19 13:47:44 +0200676 unsigned long addr, size;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100677 efi_status_t status;
Matt Fleming54b52d82014-01-10 15:27:14 +0000678
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200679 efi_system_table = sys_table_arg;
Matt Fleming54b52d82014-01-10 15:27:14 +0000680
Matt Fleming9ca8f722012-07-19 10:23:48 +0100681 /* Check if we were booted by the EFI firmware */
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200682 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100683 efi_exit(handle, EFI_INVALID_PARAMETER);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100684
David Howellsde8cb452017-02-06 11:22:43 +0000685 /*
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100686 * If the kernel isn't already loaded at a suitable address,
687 * relocate it.
688 *
689 * It must be loaded above LOAD_PHYSICAL_ADDR.
690 *
691 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
692 * is defined as the macro MAXMEM, but unfortunately that is not a
693 * compile-time constant if 5-level paging is configured, so we instead
694 * define our own macro for use here.
695 *
696 * For 32-bit, the maximum address is complicated to figure out, for
697 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
698 * KASLR uses.
699 *
Arvind Sankar21cb9b42020-04-09 15:04:29 +0200700 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
701 * loaded by LoadImage, but rather by a bootloader that called the
702 * handover entry. The reason we must always relocate in this case is
703 * to handle the case of systemd-boot booting a unified kernel image,
704 * which is a PE executable that contains the bzImage and an initrd as
705 * COFF sections. The initrd section is placed after the bzImage
706 * without ensuring that there are at least init_size bytes available
707 * for the bzImage, and thus the compressed kernel's startup code may
708 * overwrite the initrd unless it is moved out of the way.
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100709 */
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100710
711 buffer_start = ALIGN(bzimage_addr - image_offset,
712 hdr->kernel_alignment);
713 buffer_end = buffer_start + hdr->init_size;
714
715 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
716 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
717 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
Arvind Sankar21cb9b42020-04-09 15:04:29 +0200718 (image_offset == 0)) {
Arvind Sankar688eb282020-10-11 10:20:12 -0400719 extern char _bss[];
720
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100721 status = efi_relocate_kernel(&bzimage_addr,
Arvind Sankar688eb282020-10-11 10:20:12 -0400722 (unsigned long)_bss - bzimage_addr,
723 hdr->init_size,
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100724 hdr->pref_address,
725 hdr->kernel_alignment,
726 LOAD_PHYSICAL_ADDR);
727 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400728 efi_err("efi_relocate_kernel() failed!\n");
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100729 goto fail;
730 }
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100731 /*
732 * Now that we've copied the kernel elsewhere, we no longer
733 * have a set up block before startup_32(), so reset image_offset
734 * to zero in case it was set earlier.
735 */
736 image_offset = 0;
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100737 }
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100738
Arvind Sankar7dde67f2020-04-30 14:28:42 -0400739#ifdef CONFIG_CMDLINE_BOOL
Arvind Sankar055042b2020-04-30 14:28:43 -0400740 status = efi_parse_options(CONFIG_CMDLINE);
741 if (status != EFI_SUCCESS) {
742 efi_err("Failed to parse options\n");
743 goto fail;
744 }
Arvind Sankar7dde67f2020-04-30 14:28:42 -0400745#endif
746 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
747 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
748 ((u64)boot_params->ext_cmd_line_ptr << 32));
Arvind Sankar055042b2020-04-30 14:28:43 -0400749 status = efi_parse_options((char *)cmdline_paddr);
750 if (status != EFI_SUCCESS) {
751 efi_err("Failed to parse options\n");
752 goto fail;
753 }
Arvind Sankar7dde67f2020-04-30 14:28:42 -0400754 }
Hans de Goedec33ce982018-09-12 20:32:05 +0200755
756 /*
Arvind Sankar987053a2020-04-30 14:28:40 -0400757 * At this point, an initrd may already have been loaded by the
758 * bootloader and passed via bootparams. We permit an initrd loaded
759 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
760 *
761 * If the device path is not present, any command-line initrd=
762 * arguments will be processed only if image is not NULL, which will be
763 * the case only if we were loaded via the PE entry point.
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000764 */
Ard Biesheuvel20287d52021-11-19 13:47:44 +0200765 status = efi_load_initrd(image, &addr, &size, hdr->initrd_addr_max,
766 ULONG_MAX);
767 if (status != EFI_SUCCESS)
768 goto fail;
769 if (size > 0) {
770 efi_set_u64_split(addr, &hdr->ramdisk_image,
771 &boot_params->ext_ramdisk_image);
772 efi_set_u64_split(size, &hdr->ramdisk_size,
773 &boot_params->ext_ramdisk_size);
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000774 }
775
776 /*
David Howellsde8cb452017-02-06 11:22:43 +0000777 * If the boot loader gave us a value for secure_boot then we use that,
778 * otherwise we ask the BIOS.
779 */
780 if (boot_params->secure_boot == efi_secureboot_mode_unset)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100781 boot_params->secure_boot = efi_get_secureboot();
David Howellsde8cb452017-02-06 11:22:43 +0000782
Matthew Garrettccc829b2017-08-25 16:50:15 +0100783 /* Ask the firmware to clear memory on unclean shutdown */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100784 efi_enable_reset_attack_mitigation();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100785
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100786 efi_random_get_seed();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100787
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100788 efi_retrieve_tpm2_eventlog();
Matthew Garrettccc829b2017-08-25 16:50:15 +0100789
Matt Fleming9ca8f722012-07-19 10:23:48 +0100790 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000791
Matt Fleming56394ab2014-09-11 09:04:25 +0100792 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700793
Lukas Wunner58c54752016-11-12 21:32:36 +0000794 setup_quirks(boot_params);
795
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900796 status = exit_boot(boot_params, handle);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200797 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400798 efi_err("exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000799 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200800 }
Matt Fleming291f3632011-12-12 21:27:52 +0000801
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100802 return bzimage_addr;
Matt Fleming291f3632011-12-12 21:27:52 +0000803fail:
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400804 efi_err("efi_main() failed!\n");
Ingo Molnar90a21862018-07-11 11:40:33 +0200805
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100806 efi_exit(handle, status);
Matt Fleming291f3632011-12-12 21:27:52 +0000807}