blob: 072b7cf4047526736707b29edcc63aecb172857a [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>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010011
Matt Fleming291f3632011-12-12 21:27:52 +000012#include <asm/efi.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010013#include <asm/e820/types.h>
Matt Fleming291f3632011-12-12 21:27:52 +000014#include <asm/setup.h>
15#include <asm/desc.h>
Kairui Song220dd762019-10-29 18:37:54 +010016#include <asm/boot.h>
Matt Fleming291f3632011-12-12 21:27:52 +000017
Ard Biesheuvelc2d0b472020-02-10 17:02:36 +010018#include "efistub.h"
Matt Fleming291f3632011-12-12 21:27:52 +000019
Arvind Sankard5cdf4c2020-03-08 09:08:50 +010020/* Maximum physical address for 64-bit kernel with 4-level paging */
21#define MAXMEM_X86_64_4LEVEL (1ull << 46)
22
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +020023const efi_system_table_t *efi_system_table;
Arvind Sankar1887c9b2020-03-08 09:08:47 +010024extern u32 image_offset;
Arvind Sankar987053a2020-04-30 14:28:40 -040025static efi_loaded_image_t *image = NULL;
Matt Fleming204b0a12014-03-22 10:09:01 +000026
Matt Flemingc116e8d2014-01-16 11:35:43 +000027static efi_status_t
Ard Biesheuvel75c5a712018-07-20 10:47:19 +090028preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -070029{
Matt Flemingc116e8d2014-01-16 11:35:43 +000030 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -070031 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +000032 unsigned long size;
Ard Biesheuvele2967012018-07-11 11:02:35 +020033 uint64_t romsize;
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020034 void *romimage;
Matt Flemingc116e8d2014-01-16 11:35:43 +000035
Hans de Goede1de3a1b2018-05-04 08:00:01 +020036 /*
Ard Biesheuvele2967012018-07-11 11:02:35 +020037 * Some firmware images contain EFI function pointers at the place where
38 * the romimage and romsize fields are supposed to be. Typically the EFI
Hans de Goede1de3a1b2018-05-04 08:00:01 +020039 * code is mapped at high addresses, translating to an unrealistically
40 * large romsize. The UEFI spec limits the size of option ROMs to 16
41 * MiB so we reject any ROMs over 16 MiB in size to catch this.
42 */
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +010043 romimage = efi_table_attr(pci, romimage);
44 romsize = efi_table_attr(pci, romsize);
Hans de Goede1de3a1b2018-05-04 08:00:01 +020045 if (!romimage || !romsize || romsize > SZ_16M)
Matt Flemingc116e8d2014-01-16 11:35:43 +000046 return EFI_INVALID_PARAMETER;
47
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020048 size = romsize + sizeof(*rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000049
Ard Biesheuvel966291f2019-12-24 16:10:23 +010050 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
51 (void **)&rom);
Andre Müller77e21e82014-09-10 01:00:22 +020052 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -040053 efi_err("Failed to allocate memory for 'rom'\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000054 return status;
Andre Müller77e21e82014-09-10 01:00:22 +020055 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000056
57 memset(rom, 0, sizeof(*rom));
58
Ingo Molnar90a21862018-07-11 11:40:33 +020059 rom->data.type = SETUP_PCI;
60 rom->data.len = size - sizeof(struct setup_data);
61 rom->data.next = 0;
62 rom->pcilen = pci->romsize;
Matt Flemingc116e8d2014-01-16 11:35:43 +000063 *__rom = rom;
64
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010065 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
66 PCI_VENDOR_ID, 1, &rom->vendor);
Matt Flemingc116e8d2014-01-16 11:35:43 +000067
Andre Müller77e21e82014-09-10 01:00:22 +020068 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -040069 efi_err("Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000070 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020071 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000072
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010073 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
74 PCI_DEVICE_ID, 1, &rom->devid);
Matt Flemingc116e8d2014-01-16 11:35:43 +000075
Andre Müller77e21e82014-09-10 01:00:22 +020076 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -040077 efi_err("Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000078 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020079 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000080
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010081 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
82 &rom->device, &rom->function);
Matt Flemingc116e8d2014-01-16 11:35:43 +000083
84 if (status != EFI_SUCCESS)
85 goto free_struct;
86
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020087 memcpy(rom->romdata, romimage, romsize);
Matt Flemingc116e8d2014-01-16 11:35:43 +000088 return status;
89
90free_struct:
Ard Biesheuvel966291f2019-12-24 16:10:23 +010091 efi_bs_call(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000092 return status;
93}
94
Matt Fleming56394ab2014-09-11 09:04:25 +010095/*
96 * There's no way to return an informative status from this function,
97 * because any analysis (and printing of error messages) needs to be
98 * done directly at the EFI function call-site.
99 *
100 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
101 * just didn't find any PCI devices, but there's no way to tell outside
102 * the context of the call.
103 */
104static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000105{
106 efi_status_t status;
107 void **pci_handle = NULL;
108 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
109 unsigned long size = 0;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900110 struct setup_data *data;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100111 efi_handle_t h;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900112 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000113
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100114 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
115 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700116
117 if (status == EFI_BUFFER_TOO_SMALL) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100118 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
119 (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700120
Andre Müller77e21e82014-09-10 01:00:22 +0200121 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400122 efi_err("Failed to allocate memory for 'pci_handle'\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100123 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200124 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700125
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100126 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
127 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700128 }
129
130 if (status != EFI_SUCCESS)
131 goto free_handle;
132
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900133 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
134
135 while (data && data->next)
136 data = (struct setup_data *)(unsigned long)data->next;
137
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100138 for_each_efi_handle(h, pci_handle, size, i) {
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900139 efi_pci_io_protocol_t *pci = NULL;
140 struct pci_setup_rom *rom;
141
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100142 status = efi_bs_call(handle_protocol, h, &pci_proto,
143 (void **)&pci);
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900144 if (status != EFI_SUCCESS || !pci)
145 continue;
146
147 status = preserve_pci_rom_image(pci, &rom);
148 if (status != EFI_SUCCESS)
149 continue;
150
151 if (data)
152 data->next = (unsigned long)rom;
153 else
154 params->hdr.setup_data = (unsigned long)rom;
155
156 data = (struct setup_data *)rom;
157 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700158
159free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100160 efi_bs_call(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700161}
162
Lukas Wunner58c54752016-11-12 21:32:36 +0000163static void retrieve_apple_device_properties(struct boot_params *boot_params)
164{
165 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
166 struct setup_data *data, *new;
167 efi_status_t status;
168 u32 size = 0;
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100169 apple_properties_protocol_t *p;
Lukas Wunner58c54752016-11-12 21:32:36 +0000170
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100171 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
Lukas Wunner58c54752016-11-12 21:32:36 +0000172 if (status != EFI_SUCCESS)
173 return;
174
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +0100175 if (efi_table_attr(p, version) != 0x10000) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400176 efi_err("Unsupported properties proto version\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000177 return;
178 }
179
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100180 efi_call_proto(p, get_all, NULL, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000181 if (!size)
182 return;
183
184 do {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100185 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
186 size + sizeof(struct setup_data),
187 (void **)&new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000188 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400189 efi_err("Failed to allocate memory for 'properties'\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000190 return;
191 }
192
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100193 status = efi_call_proto(p, get_all, new->data, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000194
195 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100196 efi_bs_call(free_pool, new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000197 } while (status == EFI_BUFFER_TOO_SMALL);
198
199 new->type = SETUP_APPLE_PROPERTIES;
200 new->len = size;
201 new->next = 0;
202
203 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Ingo Molnar90a21862018-07-11 11:40:33 +0200204 if (!data) {
Lukas Wunner58c54752016-11-12 21:32:36 +0000205 boot_params->hdr.setup_data = (unsigned long)new;
Ingo Molnar90a21862018-07-11 11:40:33 +0200206 } else {
Lukas Wunner58c54752016-11-12 21:32:36 +0000207 while (data->next)
208 data = (struct setup_data *)(unsigned long)data->next;
209 data->next = (unsigned long)new;
210 }
211}
212
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000213static const efi_char16_t apple[] = L"Apple";
214
Lukas Wunner58c54752016-11-12 21:32:36 +0000215static void setup_quirks(struct boot_params *boot_params)
216{
Lukas Wunner58c54752016-11-12 21:32:36 +0000217 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200218 efi_table_attr(efi_system_table, fw_vendor);
Lukas Wunner58c54752016-11-12 21:32:36 +0000219
220 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
221 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
222 retrieve_apple_device_properties(boot_params);
223 }
224}
225
Matt Flemingc116e8d2014-01-16 11:35:43 +0000226/*
227 * See if we have Universal Graphics Adapter (UGA) protocol
228 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200229static efi_status_t
230setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000231{
232 efi_status_t status;
233 u32 width, height;
234 void **uga_handle = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900235 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100236 efi_handle_t handle;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900237 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000238
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100239 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
240 (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000241 if (status != EFI_SUCCESS)
242 return status;
243
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100244 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
245 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000246 if (status != EFI_SUCCESS)
247 goto free_handle;
248
249 height = 0;
250 width = 0;
251
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900252 first_uga = NULL;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100253 for_each_efi_handle(handle, uga_handle, size, i) {
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900254 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
255 u32 w, h, depth, refresh;
256 void *pciio;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900257
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100258 status = efi_bs_call(handle_protocol, handle, uga_proto,
259 (void **)&uga);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900260 if (status != EFI_SUCCESS)
261 continue;
262
Ard Biesheuvel093174f2018-07-20 10:47:22 +0900263 pciio = NULL;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100264 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900265
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100266 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900267 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
268 width = w;
269 height = h;
270
271 /*
272 * Once we've found a UGA supporting PCIIO,
273 * don't bother looking any further.
274 */
275 if (pciio)
276 break;
277
278 first_uga = uga;
279 }
280 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000281
282 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000283 goto free_handle;
284
285 /* EFI framebuffer */
Ingo Molnar90a21862018-07-11 11:40:33 +0200286 si->orig_video_isVGA = VIDEO_TYPE_EFI;
Matt Fleming291f3632011-12-12 21:27:52 +0000287
Ingo Molnar90a21862018-07-11 11:40:33 +0200288 si->lfb_depth = 32;
289 si->lfb_width = width;
290 si->lfb_height = height;
Matt Fleming291f3632011-12-12 21:27:52 +0000291
Ingo Molnar90a21862018-07-11 11:40:33 +0200292 si->red_size = 8;
293 si->red_pos = 16;
294 si->green_size = 8;
295 si->green_pos = 8;
296 si->blue_size = 8;
297 si->blue_pos = 0;
298 si->rsvd_size = 8;
299 si->rsvd_pos = 24;
Matt Fleming291f3632011-12-12 21:27:52 +0000300
Matt Fleming291f3632011-12-12 21:27:52 +0000301free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100302 efi_bs_call(free_pool, uga_handle);
Ingo Molnar90a21862018-07-11 11:40:33 +0200303
Matt Fleming291f3632011-12-12 21:27:52 +0000304 return status;
305}
306
Arvind Sankarf32ea1c2020-01-30 17:20:04 -0500307static void setup_graphics(struct boot_params *boot_params)
Matt Fleming291f3632011-12-12 21:27:52 +0000308{
309 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
310 struct screen_info *si;
311 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
312 efi_status_t status;
313 unsigned long size;
314 void **gop_handle = NULL;
315 void **uga_handle = NULL;
316
317 si = &boot_params->screen_info;
318 memset(si, 0, sizeof(*si));
319
320 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100321 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
322 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000323 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100324 status = efi_setup_gop(si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000325
326 if (status != EFI_SUCCESS) {
327 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100328 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
329 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000330 if (status == EFI_BUFFER_TOO_SMALL)
331 setup_uga(si, &uga_proto, size);
332 }
333}
334
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100335
336static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
337{
338 efi_bs_call(exit, handle, status, 0, NULL);
Ard Biesheuvelf3fa0ef2020-03-08 09:08:45 +0100339 for(;;)
340 asm("hlt");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100341}
342
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100343void startup_32(struct boot_params *boot_params);
344
345void __noreturn efi_stub_entry(efi_handle_t handle,
346 efi_system_table_t *sys_table_arg,
347 struct boot_params *boot_params);
348
Matt Fleming291f3632011-12-12 21:27:52 +0000349/*
350 * Because the x86 boot code expects to be passed a boot_params we
351 * need to create one ourselves (usually the bootloader would create
352 * one for us).
353 */
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100354efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
355 efi_system_table_t *sys_table_arg)
Matt Fleming291f3632011-12-12 21:27:52 +0000356{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100357 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100358 struct setup_header *hdr;
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100359 void *image_base;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100360 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000361 int options_size = 0;
362 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700363 char *cmdline_ptr;
Roy Franz46f45822013-09-22 15:45:39 -0700364 unsigned long ramdisk_addr;
365 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000366
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200367 efi_system_table = sys_table_arg;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100368
369 /* Check if we were booted by the EFI firmware */
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200370 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100371 efi_exit(handle, EFI_INVALID_PARAMETER);
Matt Fleming54b52d82014-01-10 15:27:14 +0000372
Arvind Sankar0347d8c22020-03-08 09:08:58 +0100373 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100374 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400375 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100376 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100377 }
378
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100379 image_base = efi_table_attr(image, image_base);
380 image_offset = (void *)startup_32 - image_base;
381
Arvind Sankar019512f2020-04-30 14:28:33 -0400382 status = efi_allocate_pages(sizeof(struct boot_params),
383 (unsigned long *)&boot_params, ULONG_MAX);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100384 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400385 efi_err("Failed to allocate lowmem for boot params\n");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100386 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100387 }
388
Arvind Sankar019512f2020-04-30 14:28:33 -0400389 memset(boot_params, 0x0, sizeof(struct boot_params));
Matt Fleming9ca8f722012-07-19 10:23:48 +0100390
391 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100392
393 /* Copy the second sector to boot_params */
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100394 memcpy(&hdr->jump, image_base + 512, 512);
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{
575 unsigned long map_size, desc_size, buff_size;
576 struct efi_boot_memmap boot_map;
577 efi_memory_desc_t *map;
578 efi_status_t status;
579 __u32 nr_desc;
580
581 boot_map.map = &map;
582 boot_map.map_size = &map_size;
583 boot_map.desc_size = &desc_size;
584 boot_map.desc_ver = NULL;
585 boot_map.key_ptr = NULL;
586 boot_map.buff_size = &buff_size;
587
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100588 status = efi_get_memory_map(&boot_map);
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100589 if (status != EFI_SUCCESS)
590 return status;
591
592 nr_desc = buff_size / desc_size;
593
594 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
595 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
596
597 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
598 if (status != EFI_SUCCESS)
599 return status;
600 }
601
602 return EFI_SUCCESS;
603}
604
Jeffrey Hugod6493402016-08-29 14:38:54 -0600605struct exit_boot_struct {
Ingo Molnar90a21862018-07-11 11:40:33 +0200606 struct boot_params *boot_params;
607 struct efi_info *efi;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600608};
609
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100610static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
Jeffrey Hugod6493402016-08-29 14:38:54 -0600611 void *priv)
612{
Jeffrey Hugod6493402016-08-29 14:38:54 -0600613 const char *signature;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600614 struct exit_boot_struct *p = priv;
615
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900616 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
617 : EFI32_LOADER_SIGNATURE;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600618 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
619
Arvind Sankareed4e012020-04-30 14:28:34 -0400620 efi_set_u64_split((unsigned long)efi_system_table,
621 &p->efi->efi_systab, &p->efi->efi_systab_hi);
Ingo Molnar90a21862018-07-11 11:40:33 +0200622 p->efi->efi_memdesc_size = *map->desc_size;
623 p->efi->efi_memdesc_version = *map->desc_ver;
Arvind Sankareed4e012020-04-30 14:28:34 -0400624 efi_set_u64_split((unsigned long)*map->map,
625 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
Ingo Molnar90a21862018-07-11 11:40:33 +0200626 p->efi->efi_memmap_size = *map->map_size;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600627
Jeffrey Hugod6493402016-08-29 14:38:54 -0600628 return EFI_SUCCESS;
629}
630
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900631static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
Linn Crosettod2078d52013-09-22 19:59:08 -0600632{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600633 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600634 efi_memory_desc_t *mem_map;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100635 struct setup_data *e820ext = NULL;
636 __u32 e820ext_size = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600637 efi_status_t status;
638 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600639 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600640 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600641
Ingo Molnar90a21862018-07-11 11:40:33 +0200642 map.map = &mem_map;
643 map.map_size = &map_sz;
644 map.desc_size = &desc_size;
645 map.desc_ver = &desc_version;
646 map.key_ptr = &key;
647 map.buff_size = &buff_size;
648 priv.boot_params = boot_params;
649 priv.efi = &boot_params->efi_info;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100650
651 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
652 if (status != EFI_SUCCESS)
653 return status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600654
Jeffrey Hugod6493402016-08-29 14:38:54 -0600655 /* Might as well exit boot services now */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100656 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600657 if (status != EFI_SUCCESS)
658 return status;
659
Linn Crosettod2078d52013-09-22 19:59:08 -0600660 /* Historic? */
Ingo Molnar90a21862018-07-11 11:40:33 +0200661 boot_params->alt_mem_k = 32 * 1024;
Linn Crosettod2078d52013-09-22 19:59:08 -0600662
663 status = setup_e820(boot_params, e820ext, e820ext_size);
664 if (status != EFI_SUCCESS)
665 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000666
667 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000668}
669
Matt Fleming9ca8f722012-07-19 10:23:48 +0100670/*
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100671 * On success, we return the address of startup_32, which has potentially been
672 * relocated by efi_relocate_kernel.
673 * On failure, we exit to the firmware via efi_exit instead of returning.
Matt Fleming9ca8f722012-07-19 10:23:48 +0100674 */
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100675unsigned long efi_main(efi_handle_t handle,
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100676 efi_system_table_t *sys_table_arg,
Ard Biesheuvel796eb8d2020-01-13 18:22:33 +0100677 struct boot_params *boot_params)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100678{
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100679 unsigned long bzimage_addr = (unsigned long)startup_32;
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100680 unsigned long buffer_start, buffer_end;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100681 struct setup_header *hdr = &boot_params->hdr;
682 efi_status_t status;
Matt Fleming54b52d82014-01-10 15:27:14 +0000683
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200684 efi_system_table = sys_table_arg;
Matt Fleming54b52d82014-01-10 15:27:14 +0000685
Matt Fleming9ca8f722012-07-19 10:23:48 +0100686 /* Check if we were booted by the EFI firmware */
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200687 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100688 efi_exit(handle, EFI_INVALID_PARAMETER);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100689
David Howellsde8cb452017-02-06 11:22:43 +0000690 /*
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100691 * If the kernel isn't already loaded at a suitable address,
692 * relocate it.
693 *
694 * It must be loaded above LOAD_PHYSICAL_ADDR.
695 *
696 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
697 * is defined as the macro MAXMEM, but unfortunately that is not a
698 * compile-time constant if 5-level paging is configured, so we instead
699 * define our own macro for use here.
700 *
701 * For 32-bit, the maximum address is complicated to figure out, for
702 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
703 * KASLR uses.
704 *
Arvind Sankar21cb9b42020-04-09 15:04:29 +0200705 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
706 * loaded by LoadImage, but rather by a bootloader that called the
707 * handover entry. The reason we must always relocate in this case is
708 * to handle the case of systemd-boot booting a unified kernel image,
709 * which is a PE executable that contains the bzImage and an initrd as
710 * COFF sections. The initrd section is placed after the bzImage
711 * without ensuring that there are at least init_size bytes available
712 * for the bzImage, and thus the compressed kernel's startup code may
713 * overwrite the initrd unless it is moved out of the way.
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100714 */
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100715
716 buffer_start = ALIGN(bzimage_addr - image_offset,
717 hdr->kernel_alignment);
718 buffer_end = buffer_start + hdr->init_size;
719
720 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
721 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
722 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
Arvind Sankar21cb9b42020-04-09 15:04:29 +0200723 (image_offset == 0)) {
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100724 status = efi_relocate_kernel(&bzimage_addr,
725 hdr->init_size, hdr->init_size,
726 hdr->pref_address,
727 hdr->kernel_alignment,
728 LOAD_PHYSICAL_ADDR);
729 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400730 efi_err("efi_relocate_kernel() failed!\n");
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100731 goto fail;
732 }
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100733 /*
734 * Now that we've copied the kernel elsewhere, we no longer
735 * have a set up block before startup_32(), so reset image_offset
736 * to zero in case it was set earlier.
737 */
738 image_offset = 0;
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100739 }
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100740
Arvind Sankar7dde67f2020-04-30 14:28:42 -0400741#ifdef CONFIG_CMDLINE_BOOL
Arvind Sankar055042b2020-04-30 14:28:43 -0400742 status = efi_parse_options(CONFIG_CMDLINE);
743 if (status != EFI_SUCCESS) {
744 efi_err("Failed to parse options\n");
745 goto fail;
746 }
Arvind Sankar7dde67f2020-04-30 14:28:42 -0400747#endif
748 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
749 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
750 ((u64)boot_params->ext_cmd_line_ptr << 32));
Arvind Sankar055042b2020-04-30 14:28:43 -0400751 status = efi_parse_options((char *)cmdline_paddr);
752 if (status != EFI_SUCCESS) {
753 efi_err("Failed to parse options\n");
754 goto fail;
755 }
Arvind Sankar7dde67f2020-04-30 14:28:42 -0400756 }
Hans de Goedec33ce982018-09-12 20:32:05 +0200757
758 /*
Arvind Sankar987053a2020-04-30 14:28:40 -0400759 * At this point, an initrd may already have been loaded by the
760 * bootloader and passed via bootparams. We permit an initrd loaded
761 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
762 *
763 * If the device path is not present, any command-line initrd=
764 * arguments will be processed only if image is not NULL, which will be
765 * the case only if we were loaded via the PE entry point.
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000766 */
Ard Biesheuvel980771f2020-04-16 18:45:24 +0200767 if (!efi_noinitrd) {
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000768 unsigned long addr, size;
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000769
Arvind Sankarf61900f2020-04-30 14:28:41 -0400770 status = efi_load_initrd(image, &addr, &size,
771 hdr->initrd_addr_max, ULONG_MAX);
Arvind Sankar987053a2020-04-30 14:28:40 -0400772
773 if (status != EFI_SUCCESS) {
774 efi_err("Failed to load initrd!\n");
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000775 goto fail;
776 }
Arvind Sankar987053a2020-04-30 14:28:40 -0400777 efi_set_u64_split(addr, &hdr->ramdisk_image,
778 &boot_params->ext_ramdisk_image);
779 efi_set_u64_split(size, &hdr->ramdisk_size,
780 &boot_params->ext_ramdisk_size);
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000781 }
782
783 /*
David Howellsde8cb452017-02-06 11:22:43 +0000784 * If the boot loader gave us a value for secure_boot then we use that,
785 * otherwise we ask the BIOS.
786 */
787 if (boot_params->secure_boot == efi_secureboot_mode_unset)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100788 boot_params->secure_boot = efi_get_secureboot();
David Howellsde8cb452017-02-06 11:22:43 +0000789
Matthew Garrettccc829b2017-08-25 16:50:15 +0100790 /* Ask the firmware to clear memory on unclean shutdown */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100791 efi_enable_reset_attack_mitigation();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100792
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100793 efi_random_get_seed();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100794
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100795 efi_retrieve_tpm2_eventlog();
Matthew Garrettccc829b2017-08-25 16:50:15 +0100796
Matt Fleming9ca8f722012-07-19 10:23:48 +0100797 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000798
Matt Fleming56394ab2014-09-11 09:04:25 +0100799 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700800
Lukas Wunner58c54752016-11-12 21:32:36 +0000801 setup_quirks(boot_params);
802
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900803 status = exit_boot(boot_params, handle);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200804 if (status != EFI_SUCCESS) {
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400805 efi_err("exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000806 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200807 }
Matt Fleming291f3632011-12-12 21:27:52 +0000808
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100809 return bzimage_addr;
Matt Fleming291f3632011-12-12 21:27:52 +0000810fail:
Arvind Sankar36bdd0a2020-04-30 14:28:36 -0400811 efi_err("efi_main() failed!\n");
Ingo Molnar90a21862018-07-11 11:40:33 +0200812
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100813 efi_exit(handle, status);
Matt Fleming291f3632011-12-12 21:27:52 +0000814}