blob: bddbc103a34b05ebf5fea087a1f9051d73121833 [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;
Ard Biesheuvel796eb8d2020-01-13 18:22:33 +010024extern const bool efi_is64;
Arvind Sankar1887c9b2020-03-08 09:08:47 +010025extern u32 image_offset;
Matt Fleming204b0a12014-03-22 10:09:01 +000026
Ard Biesheuvel796eb8d2020-01-13 18:22:33 +010027__attribute_const__ bool efi_is_64bit(void)
Ard Biesheuvelc3710de2019-12-24 16:10:17 +010028{
Ard Biesheuvel796eb8d2020-01-13 18:22:33 +010029 if (IS_ENABLED(CONFIG_EFI_MIXED))
30 return efi_is64;
Qian Caicada0b62020-01-22 14:14:30 -050031 return IS_ENABLED(CONFIG_X86_64);
Matt Fleming54b52d82014-01-10 15:27:14 +000032}
Matt Fleming54b52d82014-01-10 15:27:14 +000033
Matt Flemingc116e8d2014-01-16 11:35:43 +000034static efi_status_t
Ard Biesheuvel75c5a712018-07-20 10:47:19 +090035preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -070036{
Matt Flemingc116e8d2014-01-16 11:35:43 +000037 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -070038 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +000039 unsigned long size;
Ard Biesheuvele2967012018-07-11 11:02:35 +020040 uint64_t romsize;
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020041 void *romimage;
Matt Flemingc116e8d2014-01-16 11:35:43 +000042
Hans de Goede1de3a1b2018-05-04 08:00:01 +020043 /*
Ard Biesheuvele2967012018-07-11 11:02:35 +020044 * Some firmware images contain EFI function pointers at the place where
45 * the romimage and romsize fields are supposed to be. Typically the EFI
Hans de Goede1de3a1b2018-05-04 08:00:01 +020046 * code is mapped at high addresses, translating to an unrealistically
47 * large romsize. The UEFI spec limits the size of option ROMs to 16
48 * MiB so we reject any ROMs over 16 MiB in size to catch this.
49 */
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +010050 romimage = efi_table_attr(pci, romimage);
51 romsize = efi_table_attr(pci, romsize);
Hans de Goede1de3a1b2018-05-04 08:00:01 +020052 if (!romimage || !romsize || romsize > SZ_16M)
Matt Flemingc116e8d2014-01-16 11:35:43 +000053 return EFI_INVALID_PARAMETER;
54
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020055 size = romsize + sizeof(*rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000056
Ard Biesheuvel966291f2019-12-24 16:10:23 +010057 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
58 (void **)&rom);
Andre Müller77e21e82014-09-10 01:00:22 +020059 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +010060 efi_printk("Failed to allocate memory for 'rom'\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000061 return status;
Andre Müller77e21e82014-09-10 01:00:22 +020062 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000063
64 memset(rom, 0, sizeof(*rom));
65
Ingo Molnar90a21862018-07-11 11:40:33 +020066 rom->data.type = SETUP_PCI;
67 rom->data.len = size - sizeof(struct setup_data);
68 rom->data.next = 0;
69 rom->pcilen = pci->romsize;
Matt Flemingc116e8d2014-01-16 11:35:43 +000070 *__rom = rom;
71
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010072 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
73 PCI_VENDOR_ID, 1, &rom->vendor);
Matt Flemingc116e8d2014-01-16 11:35:43 +000074
Andre Müller77e21e82014-09-10 01:00:22 +020075 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +010076 efi_printk("Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000077 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020078 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000079
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010080 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
81 PCI_DEVICE_ID, 1, &rom->devid);
Matt Flemingc116e8d2014-01-16 11:35:43 +000082
Andre Müller77e21e82014-09-10 01:00:22 +020083 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +010084 efi_printk("Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000085 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020086 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000087
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +010088 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
89 &rom->device, &rom->function);
Matt Flemingc116e8d2014-01-16 11:35:43 +000090
91 if (status != EFI_SUCCESS)
92 goto free_struct;
93
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020094 memcpy(rom->romdata, romimage, romsize);
Matt Flemingc116e8d2014-01-16 11:35:43 +000095 return status;
96
97free_struct:
Ard Biesheuvel966291f2019-12-24 16:10:23 +010098 efi_bs_call(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000099 return status;
100}
101
Matt Fleming56394ab2014-09-11 09:04:25 +0100102/*
103 * There's no way to return an informative status from this function,
104 * because any analysis (and printing of error messages) needs to be
105 * done directly at the EFI function call-site.
106 *
107 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
108 * just didn't find any PCI devices, but there's no way to tell outside
109 * the context of the call.
110 */
111static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000112{
113 efi_status_t status;
114 void **pci_handle = NULL;
115 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
116 unsigned long size = 0;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900117 struct setup_data *data;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100118 efi_handle_t h;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900119 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000120
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100121 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
122 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700123
124 if (status == EFI_BUFFER_TOO_SMALL) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100125 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
126 (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700127
Andre Müller77e21e82014-09-10 01:00:22 +0200128 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100129 efi_printk("Failed to allocate memory for 'pci_handle'\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100130 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200131 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700132
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100133 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
134 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700135 }
136
137 if (status != EFI_SUCCESS)
138 goto free_handle;
139
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900140 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
141
142 while (data && data->next)
143 data = (struct setup_data *)(unsigned long)data->next;
144
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100145 for_each_efi_handle(h, pci_handle, size, i) {
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900146 efi_pci_io_protocol_t *pci = NULL;
147 struct pci_setup_rom *rom;
148
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100149 status = efi_bs_call(handle_protocol, h, &pci_proto,
150 (void **)&pci);
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900151 if (status != EFI_SUCCESS || !pci)
152 continue;
153
154 status = preserve_pci_rom_image(pci, &rom);
155 if (status != EFI_SUCCESS)
156 continue;
157
158 if (data)
159 data->next = (unsigned long)rom;
160 else
161 params->hdr.setup_data = (unsigned long)rom;
162
163 data = (struct setup_data *)rom;
164 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700165
166free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100167 efi_bs_call(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700168}
169
Lukas Wunner58c54752016-11-12 21:32:36 +0000170static void retrieve_apple_device_properties(struct boot_params *boot_params)
171{
172 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
173 struct setup_data *data, *new;
174 efi_status_t status;
175 u32 size = 0;
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100176 apple_properties_protocol_t *p;
Lukas Wunner58c54752016-11-12 21:32:36 +0000177
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100178 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
Lukas Wunner58c54752016-11-12 21:32:36 +0000179 if (status != EFI_SUCCESS)
180 return;
181
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +0100182 if (efi_table_attr(p, version) != 0x10000) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100183 efi_printk("Unsupported properties proto version\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000184 return;
185 }
186
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100187 efi_call_proto(p, get_all, NULL, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000188 if (!size)
189 return;
190
191 do {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100192 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
193 size + sizeof(struct setup_data),
194 (void **)&new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000195 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100196 efi_printk("Failed to allocate memory for 'properties'\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000197 return;
198 }
199
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100200 status = efi_call_proto(p, get_all, new->data, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000201
202 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100203 efi_bs_call(free_pool, new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000204 } while (status == EFI_BUFFER_TOO_SMALL);
205
206 new->type = SETUP_APPLE_PROPERTIES;
207 new->len = size;
208 new->next = 0;
209
210 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Ingo Molnar90a21862018-07-11 11:40:33 +0200211 if (!data) {
Lukas Wunner58c54752016-11-12 21:32:36 +0000212 boot_params->hdr.setup_data = (unsigned long)new;
Ingo Molnar90a21862018-07-11 11:40:33 +0200213 } else {
Lukas Wunner58c54752016-11-12 21:32:36 +0000214 while (data->next)
215 data = (struct setup_data *)(unsigned long)data->next;
216 data->next = (unsigned long)new;
217 }
218}
219
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000220static const efi_char16_t apple[] = L"Apple";
221
Lukas Wunner58c54752016-11-12 21:32:36 +0000222static void setup_quirks(struct boot_params *boot_params)
223{
Lukas Wunner58c54752016-11-12 21:32:36 +0000224 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200225 efi_table_attr(efi_system_table, fw_vendor);
Lukas Wunner58c54752016-11-12 21:32:36 +0000226
227 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
228 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
229 retrieve_apple_device_properties(boot_params);
230 }
231}
232
Matt Flemingc116e8d2014-01-16 11:35:43 +0000233/*
234 * See if we have Universal Graphics Adapter (UGA) protocol
235 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200236static efi_status_t
237setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000238{
239 efi_status_t status;
240 u32 width, height;
241 void **uga_handle = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900242 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100243 efi_handle_t handle;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900244 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000245
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100246 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
247 (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000248 if (status != EFI_SUCCESS)
249 return status;
250
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100251 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
252 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000253 if (status != EFI_SUCCESS)
254 goto free_handle;
255
256 height = 0;
257 width = 0;
258
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900259 first_uga = NULL;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100260 for_each_efi_handle(handle, uga_handle, size, i) {
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900261 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
262 u32 w, h, depth, refresh;
263 void *pciio;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900264
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100265 status = efi_bs_call(handle_protocol, handle, uga_proto,
266 (void **)&uga);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900267 if (status != EFI_SUCCESS)
268 continue;
269
Ard Biesheuvel093174f2018-07-20 10:47:22 +0900270 pciio = NULL;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100271 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900272
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100273 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900274 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
275 width = w;
276 height = h;
277
278 /*
279 * Once we've found a UGA supporting PCIIO,
280 * don't bother looking any further.
281 */
282 if (pciio)
283 break;
284
285 first_uga = uga;
286 }
287 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000288
289 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000290 goto free_handle;
291
292 /* EFI framebuffer */
Ingo Molnar90a21862018-07-11 11:40:33 +0200293 si->orig_video_isVGA = VIDEO_TYPE_EFI;
Matt Fleming291f3632011-12-12 21:27:52 +0000294
Ingo Molnar90a21862018-07-11 11:40:33 +0200295 si->lfb_depth = 32;
296 si->lfb_width = width;
297 si->lfb_height = height;
Matt Fleming291f3632011-12-12 21:27:52 +0000298
Ingo Molnar90a21862018-07-11 11:40:33 +0200299 si->red_size = 8;
300 si->red_pos = 16;
301 si->green_size = 8;
302 si->green_pos = 8;
303 si->blue_size = 8;
304 si->blue_pos = 0;
305 si->rsvd_size = 8;
306 si->rsvd_pos = 24;
Matt Fleming291f3632011-12-12 21:27:52 +0000307
Matt Fleming291f3632011-12-12 21:27:52 +0000308free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100309 efi_bs_call(free_pool, uga_handle);
Ingo Molnar90a21862018-07-11 11:40:33 +0200310
Matt Fleming291f3632011-12-12 21:27:52 +0000311 return status;
312}
313
Arvind Sankarf32ea1c2020-01-30 17:20:04 -0500314static void setup_graphics(struct boot_params *boot_params)
Matt Fleming291f3632011-12-12 21:27:52 +0000315{
316 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
317 struct screen_info *si;
318 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
319 efi_status_t status;
320 unsigned long size;
321 void **gop_handle = NULL;
322 void **uga_handle = NULL;
323
324 si = &boot_params->screen_info;
325 memset(si, 0, sizeof(*si));
326
327 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100328 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
329 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000330 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100331 status = efi_setup_gop(si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000332
333 if (status != EFI_SUCCESS) {
334 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100335 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
336 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000337 if (status == EFI_BUFFER_TOO_SMALL)
338 setup_uga(si, &uga_proto, size);
339 }
340}
341
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100342
343static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
344{
345 efi_bs_call(exit, handle, status, 0, NULL);
Ard Biesheuvelf3fa0ef2020-03-08 09:08:45 +0100346 for(;;)
347 asm("hlt");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100348}
349
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100350void startup_32(struct boot_params *boot_params);
351
352void __noreturn efi_stub_entry(efi_handle_t handle,
353 efi_system_table_t *sys_table_arg,
354 struct boot_params *boot_params);
355
Matt Fleming291f3632011-12-12 21:27:52 +0000356/*
357 * Because the x86 boot code expects to be passed a boot_params we
358 * need to create one ourselves (usually the bootloader would create
359 * one for us).
360 */
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100361efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
362 efi_system_table_t *sys_table_arg)
Matt Fleming291f3632011-12-12 21:27:52 +0000363{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100364 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100365 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100366 efi_loaded_image_t *image;
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100367 void *image_base;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100368 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000369 int options_size = 0;
370 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700371 char *cmdline_ptr;
Roy Franz46f45822013-09-22 15:45:39 -0700372 unsigned long ramdisk_addr;
373 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000374
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200375 efi_system_table = sys_table_arg;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100376
377 /* Check if we were booted by the EFI firmware */
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200378 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100379 efi_exit(handle, EFI_INVALID_PARAMETER);
Matt Fleming54b52d82014-01-10 15:27:14 +0000380
Arvind Sankar0347d8c22020-03-08 09:08:58 +0100381 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100382 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100383 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100384 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100385 }
386
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100387 image_base = efi_table_attr(image, image_base);
388 image_offset = (void *)startup_32 - image_base;
389
Ard Biesheuvelac82d352020-03-08 09:08:57 +0100390 status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params, ULONG_MAX);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100391 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100392 efi_printk("Failed to allocate lowmem for boot params\n");
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100393 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100394 }
395
396 memset(boot_params, 0x0, 0x4000);
397
398 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100399
400 /* Copy the second sector to boot_params */
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100401 memcpy(&hdr->jump, image_base + 512, 512);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100402
403 /*
404 * Fill out some of the header fields ourselves because the
405 * EFI firmware loader doesn't load the first sector.
406 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200407 hdr->root_flags = 1;
408 hdr->vid_mode = 0xffff;
409 hdr->boot_flag = 0xAA55;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100410
Matt Fleming291f3632011-12-12 21:27:52 +0000411 hdr->type_of_loader = 0x21;
412
413 /* Convert unicode cmdline to ascii */
Ard Biesheuvelac82d352020-03-08 09:08:57 +0100414 cmdline_ptr = efi_convert_cmdline(image, &options_size, ULONG_MAX);
Roy Franz5fef3872013-09-22 15:45:33 -0700415 if (!cmdline_ptr)
416 goto fail;
Ingo Molnar90a21862018-07-11 11:40:33 +0200417
Roy Franz5fef3872013-09-22 15:45:33 -0700418 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700419 /* Fill in upper bits of command line address, NOP on 32 bit */
420 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000421
422 hdr->ramdisk_image = 0;
423 hdr->ramdisk_size = 0;
424
Ard Biesheuvel17054f42020-02-12 23:20:54 +0100425 if (efi_is_native()) {
426 status = efi_parse_options(cmdline_ptr);
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000427 if (status != EFI_SUCCESS)
428 goto fail2;
Ard Biesheuvel17054f42020-02-12 23:20:54 +0100429
Ard Biesheuvel980771f2020-04-16 18:45:24 +0200430 if (!efi_noinitrd) {
Ard Biesheuvel17054f42020-02-12 23:20:54 +0100431 status = efi_load_initrd(image, &ramdisk_addr,
432 &ramdisk_size,
433 hdr->initrd_addr_max,
Ard Biesheuvelac82d352020-03-08 09:08:57 +0100434 ULONG_MAX);
Ard Biesheuvel17054f42020-02-12 23:20:54 +0100435 if (status != EFI_SUCCESS)
436 goto fail2;
437 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
438 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
439 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
440 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
441 }
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000442 }
Matt Fleming9ca8f722012-07-19 10:23:48 +0100443
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200444 efi_stub_entry(handle, sys_table_arg, boot_params);
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100445 /* not reached */
Ingo Molnar90a21862018-07-11 11:40:33 +0200446
Matt Fleming9ca8f722012-07-19 10:23:48 +0100447fail2:
Ard Biesheuvel1e45bf72020-02-10 17:02:40 +0100448 efi_free(options_size, (unsigned long)cmdline_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100449fail:
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100450 efi_free(0x4000, (unsigned long)boot_params);
Ingo Molnar90a21862018-07-11 11:40:33 +0200451
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100452 efi_exit(handle, status);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100453}
454
Linn Crosettod2078d52013-09-22 19:59:08 -0600455static void add_e820ext(struct boot_params *params,
456 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100457{
Linn Crosettod2078d52013-09-22 19:59:08 -0600458 struct setup_data *data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600459
460 e820ext->type = SETUP_E820_EXT;
Ingo Molnar90a21862018-07-11 11:40:33 +0200461 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600462 e820ext->next = 0;
463
464 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
465
466 while (data && data->next)
467 data = (struct setup_data *)(unsigned long)data->next;
468
469 if (data)
470 data->next = (unsigned long)e820ext;
471 else
472 params->hdr.setup_data = (unsigned long)e820ext;
473}
474
Ingo Molnar90a21862018-07-11 11:40:33 +0200475static efi_status_t
476setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
Linn Crosettod2078d52013-09-22 19:59:08 -0600477{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100478 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600479 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100480 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600481 u32 nr_entries;
482 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100483 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000484
Matt Fleming291f3632011-12-12 21:27:52 +0000485 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600486 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
487
488 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000489 efi_memory_desc_t *d;
490 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600491 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000492
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400493#ifdef CONFIG_X86_64
494 m |= (u64)efi->efi_memmap_hi << 32;
495#endif
496
Baoquan He02e43c22017-08-16 21:46:51 +0800497 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000498 switch (d->type) {
499 case EFI_RESERVED_TYPE:
500 case EFI_RUNTIME_SERVICES_CODE:
501 case EFI_RUNTIME_SERVICES_DATA:
502 case EFI_MEMORY_MAPPED_IO:
503 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
504 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100505 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000506 break;
507
508 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100509 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000510 break;
511
512 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100513 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000514 break;
515
516 case EFI_LOADER_CODE:
517 case EFI_LOADER_DATA:
518 case EFI_BOOT_SERVICES_CODE:
519 case EFI_BOOT_SERVICES_DATA:
520 case EFI_CONVENTIONAL_MEMORY:
Dan Williams262b45a2019-11-06 17:43:16 -0800521 if (efi_soft_reserve_enabled() &&
522 (d->attribute & EFI_MEMORY_SP))
523 e820_type = E820_TYPE_SOFT_RESERVED;
524 else
525 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000526 break;
527
528 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100529 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000530 break;
531
Dan Williamsad5fb872015-04-03 12:05:28 -0400532 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100533 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400534 break;
535
Matt Fleming291f3632011-12-12 21:27:52 +0000536 default:
537 continue;
538 }
539
540 /* Merge adjacent mappings */
541 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600542 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000543 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600544 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000545 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600546
Ingo Molnar61a50102017-01-27 13:54:38 +0100547 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100548 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600549 sizeof(struct setup_data);
550
551 if (!e820ext || e820ext_size < need)
552 return EFI_BUFFER_TOO_SMALL;
553
554 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100555 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600556 }
557
Ingo Molnar7410aa12017-01-29 12:56:13 +0100558 entry->addr = d->phys_addr;
559 entry->size = d->num_pages << PAGE_SHIFT;
560 entry->type = e820_type;
561 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600562 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000563 }
564
Ingo Molnar61a50102017-01-27 13:54:38 +0100565 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
566 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600567
568 add_e820ext(params, e820ext, nr_e820ext);
569 nr_entries -= nr_e820ext;
570 }
571
572 params->e820_entries = (u8)nr_entries;
573
574 return EFI_SUCCESS;
575}
576
577static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
578 u32 *e820ext_size)
579{
580 efi_status_t status;
581 unsigned long size;
582
583 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100584 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600585
586 if (*e820ext) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100587 efi_bs_call(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600588 *e820ext = NULL;
589 *e820ext_size = 0;
590 }
591
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100592 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
593 (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600594 if (status == EFI_SUCCESS)
595 *e820ext_size = size;
596
597 return status;
598}
599
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100600static efi_status_t allocate_e820(struct boot_params *params,
601 struct setup_data **e820ext,
602 u32 *e820ext_size)
603{
604 unsigned long map_size, desc_size, buff_size;
605 struct efi_boot_memmap boot_map;
606 efi_memory_desc_t *map;
607 efi_status_t status;
608 __u32 nr_desc;
609
610 boot_map.map = &map;
611 boot_map.map_size = &map_size;
612 boot_map.desc_size = &desc_size;
613 boot_map.desc_ver = NULL;
614 boot_map.key_ptr = NULL;
615 boot_map.buff_size = &buff_size;
616
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100617 status = efi_get_memory_map(&boot_map);
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100618 if (status != EFI_SUCCESS)
619 return status;
620
621 nr_desc = buff_size / desc_size;
622
623 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
624 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
625
626 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
627 if (status != EFI_SUCCESS)
628 return status;
629 }
630
631 return EFI_SUCCESS;
632}
633
Jeffrey Hugod6493402016-08-29 14:38:54 -0600634struct exit_boot_struct {
Ingo Molnar90a21862018-07-11 11:40:33 +0200635 struct boot_params *boot_params;
636 struct efi_info *efi;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600637};
638
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100639static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
Jeffrey Hugod6493402016-08-29 14:38:54 -0600640 void *priv)
641{
Jeffrey Hugod6493402016-08-29 14:38:54 -0600642 const char *signature;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600643 struct exit_boot_struct *p = priv;
644
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900645 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
646 : EFI32_LOADER_SIGNATURE;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600647 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
648
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200649 p->efi->efi_systab = (unsigned long)efi_system_table;
Ingo Molnar90a21862018-07-11 11:40:33 +0200650 p->efi->efi_memdesc_size = *map->desc_size;
651 p->efi->efi_memdesc_version = *map->desc_ver;
652 p->efi->efi_memmap = (unsigned long)*map->map;
653 p->efi->efi_memmap_size = *map->map_size;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600654
655#ifdef CONFIG_X86_64
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200656 p->efi->efi_systab_hi = (unsigned long)efi_system_table >> 32;
Ingo Molnar90a21862018-07-11 11:40:33 +0200657 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600658#endif
659
660 return EFI_SUCCESS;
661}
662
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900663static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
Linn Crosettod2078d52013-09-22 19:59:08 -0600664{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600665 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600666 efi_memory_desc_t *mem_map;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100667 struct setup_data *e820ext = NULL;
668 __u32 e820ext_size = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600669 efi_status_t status;
670 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600671 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600672 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600673
Ingo Molnar90a21862018-07-11 11:40:33 +0200674 map.map = &mem_map;
675 map.map_size = &map_sz;
676 map.desc_size = &desc_size;
677 map.desc_ver = &desc_version;
678 map.key_ptr = &key;
679 map.buff_size = &buff_size;
680 priv.boot_params = boot_params;
681 priv.efi = &boot_params->efi_info;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100682
683 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
684 if (status != EFI_SUCCESS)
685 return status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600686
Jeffrey Hugod6493402016-08-29 14:38:54 -0600687 /* Might as well exit boot services now */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100688 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600689 if (status != EFI_SUCCESS)
690 return status;
691
Linn Crosettod2078d52013-09-22 19:59:08 -0600692 /* Historic? */
Ingo Molnar90a21862018-07-11 11:40:33 +0200693 boot_params->alt_mem_k = 32 * 1024;
Linn Crosettod2078d52013-09-22 19:59:08 -0600694
695 status = setup_e820(boot_params, e820ext, e820ext_size);
696 if (status != EFI_SUCCESS)
697 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000698
699 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000700}
701
Matt Fleming9ca8f722012-07-19 10:23:48 +0100702/*
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100703 * On success, we return the address of startup_32, which has potentially been
704 * relocated by efi_relocate_kernel.
705 * On failure, we exit to the firmware via efi_exit instead of returning.
Matt Fleming9ca8f722012-07-19 10:23:48 +0100706 */
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100707unsigned long efi_main(efi_handle_t handle,
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100708 efi_system_table_t *sys_table_arg,
Ard Biesheuvel796eb8d2020-01-13 18:22:33 +0100709 struct boot_params *boot_params)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100710{
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100711 unsigned long bzimage_addr = (unsigned long)startup_32;
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100712 unsigned long buffer_start, buffer_end;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100713 struct setup_header *hdr = &boot_params->hdr;
714 efi_status_t status;
Hans de Goedec33ce982018-09-12 20:32:05 +0200715 unsigned long cmdline_paddr;
Matt Fleming54b52d82014-01-10 15:27:14 +0000716
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200717 efi_system_table = sys_table_arg;
Matt Fleming54b52d82014-01-10 15:27:14 +0000718
Matt Fleming9ca8f722012-07-19 10:23:48 +0100719 /* Check if we were booted by the EFI firmware */
Ard Biesheuvelccc27ae2020-04-16 18:38:06 +0200720 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100721 efi_exit(handle, EFI_INVALID_PARAMETER);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100722
David Howellsde8cb452017-02-06 11:22:43 +0000723 /*
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100724 * If the kernel isn't already loaded at a suitable address,
725 * relocate it.
726 *
727 * It must be loaded above LOAD_PHYSICAL_ADDR.
728 *
729 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
730 * is defined as the macro MAXMEM, but unfortunately that is not a
731 * compile-time constant if 5-level paging is configured, so we instead
732 * define our own macro for use here.
733 *
734 * For 32-bit, the maximum address is complicated to figure out, for
735 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
736 * KASLR uses.
737 *
Arvind Sankar21cb9b42020-04-09 15:04:29 +0200738 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
739 * loaded by LoadImage, but rather by a bootloader that called the
740 * handover entry. The reason we must always relocate in this case is
741 * to handle the case of systemd-boot booting a unified kernel image,
742 * which is a PE executable that contains the bzImage and an initrd as
743 * COFF sections. The initrd section is placed after the bzImage
744 * without ensuring that there are at least init_size bytes available
745 * for the bzImage, and thus the compressed kernel's startup code may
746 * overwrite the initrd unless it is moved out of the way.
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100747 */
Arvind Sankard5cdf4c2020-03-08 09:08:50 +0100748
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) ||
Arvind Sankar21cb9b42020-04-09 15:04:29 +0200756 (image_offset == 0)) {
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100757 status = efi_relocate_kernel(&bzimage_addr,
758 hdr->init_size, hdr->init_size,
759 hdr->pref_address,
760 hdr->kernel_alignment,
761 LOAD_PHYSICAL_ADDR);
762 if (status != EFI_SUCCESS) {
763 efi_printk("efi_relocate_kernel() failed!\n");
764 goto fail;
765 }
Arvind Sankar1887c9b2020-03-08 09:08:47 +0100766 /*
767 * Now that we've copied the kernel elsewhere, we no longer
768 * have a set up block before startup_32(), so reset image_offset
769 * to zero in case it was set earlier.
770 */
771 image_offset = 0;
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100772 }
Ard Biesheuvel04a7d0e2020-02-10 17:02:31 +0100773
774 /*
Ard Biesheuvel91d150c2020-02-10 17:02:46 +0100775 * efi_pe_entry() may have been called before efi_main(), in which
Hans de Goedec33ce982018-09-12 20:32:05 +0200776 * case this is the second time we parse the cmdline. This is ok,
777 * parsing the cmdline multiple times does not have side-effects.
778 */
779 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
780 ((u64)boot_params->ext_cmd_line_ptr << 32));
781 efi_parse_options((char *)cmdline_paddr);
782
783 /*
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000784 * At this point, an initrd may already have been loaded, either by
785 * the bootloader and passed via bootparams, or loaded from a initrd=
786 * command line option by efi_pe_entry() above. In either case, we
787 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device
788 * path to supersede it.
789 */
Ard Biesheuvel980771f2020-04-16 18:45:24 +0200790 if (!efi_noinitrd) {
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000791 unsigned long addr, size;
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000792
Ard Biesheuvelac82d352020-03-08 09:08:57 +0100793 status = efi_load_initrd_dev_path(&addr, &size, ULONG_MAX);
Ard Biesheuvel79d32192020-02-04 22:01:22 +0000794 if (status == EFI_SUCCESS) {
795 hdr->ramdisk_image = (u32)addr;
796 hdr->ramdisk_size = (u32)size;
797 boot_params->ext_ramdisk_image = (u64)addr >> 32;
798 boot_params->ext_ramdisk_size = (u64)size >> 32;
799 } else if (status != EFI_NOT_FOUND) {
800 efi_printk("efi_load_initrd_dev_path() failed!\n");
801 goto fail;
802 }
Ard Biesheuvelec93fc32020-02-03 23:45:14 +0000803 }
804
805 /*
David Howellsde8cb452017-02-06 11:22:43 +0000806 * If the boot loader gave us a value for secure_boot then we use that,
807 * otherwise we ask the BIOS.
808 */
809 if (boot_params->secure_boot == efi_secureboot_mode_unset)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100810 boot_params->secure_boot = efi_get_secureboot();
David Howellsde8cb452017-02-06 11:22:43 +0000811
Matthew Garrettccc829b2017-08-25 16:50:15 +0100812 /* Ask the firmware to clear memory on unclean shutdown */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100813 efi_enable_reset_attack_mitigation();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100814
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100815 efi_random_get_seed();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100816
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100817 efi_retrieve_tpm2_eventlog();
Matthew Garrettccc829b2017-08-25 16:50:15 +0100818
Matt Fleming9ca8f722012-07-19 10:23:48 +0100819 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000820
Matt Fleming56394ab2014-09-11 09:04:25 +0100821 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700822
Lukas Wunner58c54752016-11-12 21:32:36 +0000823 setup_quirks(boot_params);
824
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900825 status = exit_boot(boot_params, handle);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200826 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100827 efi_printk("exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000828 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200829 }
Matt Fleming291f3632011-12-12 21:27:52 +0000830
Arvind Sankar8acf63e2020-03-08 09:08:43 +0100831 return bzimage_addr;
Matt Fleming291f3632011-12-12 21:27:52 +0000832fail:
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100833 efi_printk("efi_main() failed!\n");
Ingo Molnar90a21862018-07-11 11:40:33 +0200834
Ard Biesheuvel3b8f44f2020-02-16 00:03:25 +0100835 efi_exit(handle, status);
Matt Fleming291f3632011-12-12 21:27:52 +0000836}