blob: 2733bc263c04a2a00f4b032df755d1a1be9c25be [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
Andrey Ryabinin393f2032015-02-13 14:39:56 -080018#include "../string.h"
Matt Fleming291f3632011-12-12 21:27:52 +000019#include "eboot.h"
20
21static efi_system_table_t *sys_table;
22
Matt Fleming84be8802014-09-23 10:37:43 +010023static struct efi_config *efi_early;
24
Ard Biesheuvel243b6752014-11-05 17:00:56 +010025__pure const struct efi_config *__efi_early(void)
26{
27 return efi_early;
28}
Matt Fleming204b0a12014-03-22 10:09:01 +000029
Matt Fleming54b52d82014-01-10 15:27:14 +000030#define BOOT_SERVICES(bits) \
31static void setup_boot_services##bits(struct efi_config *c) \
32{ \
33 efi_system_table_##bits##_t *table; \
Matt Fleming54b52d82014-01-10 15:27:14 +000034 \
35 table = (typeof(table))sys_table; \
36 \
Ingo Molnar90a21862018-07-11 11:40:33 +020037 c->runtime_services = table->runtime; \
38 c->boot_services = table->boottime; \
39 c->text_output = table->con_out; \
Matt Fleming54b52d82014-01-10 15:27:14 +000040}
41BOOT_SERVICES(32);
42BOOT_SERVICES(64);
43
Ard Biesheuvelbd669472014-07-02 14:54:42 +020044void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
Matt Flemingc116e8d2014-01-16 11:35:43 +000045{
Lukas Wunnerdb4545d2017-01-31 13:21:34 +000046 efi_call_proto(efi_simple_text_output_protocol, output_string,
47 efi_early->text_output, str);
Matt Fleming54b52d82014-01-10 15:27:14 +000048}
Lee, Chun-Yideb94102012-12-20 19:33:22 +080049
Matt Flemingc116e8d2014-01-16 11:35:43 +000050static efi_status_t
Ard Biesheuvel75c5a712018-07-20 10:47:19 +090051preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -070052{
Matt Flemingc116e8d2014-01-16 11:35:43 +000053 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -070054 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +000055 unsigned long size;
Ard Biesheuvele2967012018-07-11 11:02:35 +020056 uint64_t romsize;
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020057 void *romimage;
Matt Flemingc116e8d2014-01-16 11:35:43 +000058
Hans de Goede1de3a1b2018-05-04 08:00:01 +020059 /*
Ard Biesheuvele2967012018-07-11 11:02:35 +020060 * Some firmware images contain EFI function pointers at the place where
61 * the romimage and romsize fields are supposed to be. Typically the EFI
Hans de Goede1de3a1b2018-05-04 08:00:01 +020062 * code is mapped at high addresses, translating to an unrealistically
63 * large romsize. The UEFI spec limits the size of option ROMs to 16
64 * MiB so we reject any ROMs over 16 MiB in size to catch this.
65 */
Ard Biesheuvelf958efe2019-12-24 16:10:09 +010066 romimage = efi_table_attr(efi_pci_io_protocol, romimage, pci);
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020067 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
Hans de Goede1de3a1b2018-05-04 08:00:01 +020068 if (!romimage || !romsize || romsize > SZ_16M)
Matt Flemingc116e8d2014-01-16 11:35:43 +000069 return EFI_INVALID_PARAMETER;
70
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020071 size = romsize + sizeof(*rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000072
Ard Biesheuvel960a8d02019-12-24 16:10:11 +010073 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size,
74 (void **)&rom);
Andre Müller77e21e82014-09-10 01:00:22 +020075 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +020076 efi_printk(sys_table, "Failed to allocate memory for 'rom'\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000077 return status;
Andre Müller77e21e82014-09-10 01:00:22 +020078 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000079
80 memset(rom, 0, sizeof(*rom));
81
Ingo Molnar90a21862018-07-11 11:40:33 +020082 rom->data.type = SETUP_PCI;
83 rom->data.len = size - sizeof(struct setup_data);
84 rom->data.next = 0;
85 rom->pcilen = pci->romsize;
Matt Flemingc116e8d2014-01-16 11:35:43 +000086 *__rom = rom;
87
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020088 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
89 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
90 &rom->vendor);
Matt Flemingc116e8d2014-01-16 11:35:43 +000091
Andre Müller77e21e82014-09-10 01:00:22 +020092 if (status != EFI_SUCCESS) {
93 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000094 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020095 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000096
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020097 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
98 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
99 &rom->devid);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000100
Andre Müller77e21e82014-09-10 01:00:22 +0200101 if (status != EFI_SUCCESS) {
102 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000103 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200104 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000105
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200106 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
107 &rom->segment, &rom->bus, &rom->device,
108 &rom->function);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000109
110 if (status != EFI_SUCCESS)
111 goto free_struct;
112
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200113 memcpy(rom->romdata, romimage, romsize);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000114 return status;
115
116free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000117 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000118 return status;
119}
120
Matt Fleming56394ab2014-09-11 09:04:25 +0100121/*
122 * There's no way to return an informative status from this function,
123 * because any analysis (and printing of error messages) needs to be
124 * done directly at the EFI function call-site.
125 *
126 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
127 * just didn't find any PCI devices, but there's no way to tell outside
128 * the context of the call.
129 */
130static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000131{
132 efi_status_t status;
133 void **pci_handle = NULL;
134 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
135 unsigned long size = 0;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900136 unsigned long nr_pci;
137 struct setup_data *data;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100138 efi_handle_t h;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900139 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000140
Matt Fleming204b0a12014-03-22 10:09:01 +0000141 status = efi_call_early(locate_handle,
142 EFI_LOCATE_BY_PROTOCOL,
143 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700144
145 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000146 status = efi_call_early(allocate_pool,
147 EFI_LOADER_DATA,
148 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700149
Andre Müller77e21e82014-09-10 01:00:22 +0200150 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200151 efi_printk(sys_table, "Failed to allocate memory for 'pci_handle'\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100152 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200153 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700154
Matt Fleming204b0a12014-03-22 10:09:01 +0000155 status = efi_call_early(locate_handle,
156 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
157 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700158 }
159
160 if (status != EFI_SUCCESS)
161 goto free_handle;
162
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900163 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
164
165 while (data && data->next)
166 data = (struct setup_data *)(unsigned long)data->next;
167
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100168 for_each_efi_handle(h, pci_handle, size, i) {
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900169 efi_pci_io_protocol_t *pci = NULL;
170 struct pci_setup_rom *rom;
171
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100172 status = efi_call_early(handle_protocol, h,
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900173 &pci_proto, (void **)&pci);
174 if (status != EFI_SUCCESS || !pci)
175 continue;
176
177 status = preserve_pci_rom_image(pci, &rom);
178 if (status != EFI_SUCCESS)
179 continue;
180
181 if (data)
182 data->next = (unsigned long)rom;
183 else
184 params->hdr.setup_data = (unsigned long)rom;
185
186 data = (struct setup_data *)rom;
187 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700188
189free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000190 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700191}
192
Lukas Wunner58c54752016-11-12 21:32:36 +0000193static void retrieve_apple_device_properties(struct boot_params *boot_params)
194{
195 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
196 struct setup_data *data, *new;
197 efi_status_t status;
198 u32 size = 0;
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100199 apple_properties_protocol_t *p;
Lukas Wunner58c54752016-11-12 21:32:36 +0000200
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100201 status = efi_call_early(locate_protocol, &guid, NULL, (void **)&p);
Lukas Wunner58c54752016-11-12 21:32:36 +0000202 if (status != EFI_SUCCESS)
203 return;
204
205 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
206 efi_printk(sys_table, "Unsupported properties proto version\n");
207 return;
208 }
209
210 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
211 if (!size)
212 return;
213
214 do {
215 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100216 size + sizeof(struct setup_data),
217 (void **)&new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000218 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200219 efi_printk(sys_table, "Failed to allocate memory for 'properties'\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000220 return;
221 }
222
223 status = efi_call_proto(apple_properties_protocol, get_all, p,
224 new->data, &size);
225
226 if (status == EFI_BUFFER_TOO_SMALL)
227 efi_call_early(free_pool, new);
228 } while (status == EFI_BUFFER_TOO_SMALL);
229
230 new->type = SETUP_APPLE_PROPERTIES;
231 new->len = size;
232 new->next = 0;
233
234 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Ingo Molnar90a21862018-07-11 11:40:33 +0200235 if (!data) {
Lukas Wunner58c54752016-11-12 21:32:36 +0000236 boot_params->hdr.setup_data = (unsigned long)new;
Ingo Molnar90a21862018-07-11 11:40:33 +0200237 } else {
Lukas Wunner58c54752016-11-12 21:32:36 +0000238 while (data->next)
239 data = (struct setup_data *)(unsigned long)data->next;
240 data->next = (unsigned long)new;
241 }
242}
243
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000244static const efi_char16_t apple[] = L"Apple";
245
Lukas Wunner58c54752016-11-12 21:32:36 +0000246static void setup_quirks(struct boot_params *boot_params)
247{
Lukas Wunner58c54752016-11-12 21:32:36 +0000248 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
249 efi_table_attr(efi_system_table, fw_vendor, sys_table);
250
251 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
252 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
253 retrieve_apple_device_properties(boot_params);
254 }
255}
256
Matt Flemingc116e8d2014-01-16 11:35:43 +0000257/*
258 * See if we have Universal Graphics Adapter (UGA) protocol
259 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200260static efi_status_t
261setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000262{
263 efi_status_t status;
264 u32 width, height;
265 void **uga_handle = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900266 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
267 unsigned long nr_ugas;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100268 efi_handle_t handle;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900269 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000270
Matt Fleming204b0a12014-03-22 10:09:01 +0000271 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
272 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000273 if (status != EFI_SUCCESS)
274 return status;
275
Matt Fleming204b0a12014-03-22 10:09:01 +0000276 status = efi_call_early(locate_handle,
277 EFI_LOCATE_BY_PROTOCOL,
278 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000279 if (status != EFI_SUCCESS)
280 goto free_handle;
281
282 height = 0;
283 width = 0;
284
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900285 first_uga = NULL;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100286 for_each_efi_handle(handle, uga_handle, size, i) {
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900287 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
288 u32 w, h, depth, refresh;
289 void *pciio;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900290
291 status = efi_call_early(handle_protocol, handle,
292 uga_proto, (void **)&uga);
293 if (status != EFI_SUCCESS)
294 continue;
295
Ard Biesheuvel093174f2018-07-20 10:47:22 +0900296 pciio = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900297 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
298
299 status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
300 &w, &h, &depth, &refresh);
301 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
302 width = w;
303 height = h;
304
305 /*
306 * Once we've found a UGA supporting PCIIO,
307 * don't bother looking any further.
308 */
309 if (pciio)
310 break;
311
312 first_uga = uga;
313 }
314 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000315
316 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000317 goto free_handle;
318
319 /* EFI framebuffer */
Ingo Molnar90a21862018-07-11 11:40:33 +0200320 si->orig_video_isVGA = VIDEO_TYPE_EFI;
Matt Fleming291f3632011-12-12 21:27:52 +0000321
Ingo Molnar90a21862018-07-11 11:40:33 +0200322 si->lfb_depth = 32;
323 si->lfb_width = width;
324 si->lfb_height = height;
Matt Fleming291f3632011-12-12 21:27:52 +0000325
Ingo Molnar90a21862018-07-11 11:40:33 +0200326 si->red_size = 8;
327 si->red_pos = 16;
328 si->green_size = 8;
329 si->green_pos = 8;
330 si->blue_size = 8;
331 si->blue_pos = 0;
332 si->rsvd_size = 8;
333 si->rsvd_pos = 24;
Matt Fleming291f3632011-12-12 21:27:52 +0000334
Matt Fleming291f3632011-12-12 21:27:52 +0000335free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000336 efi_call_early(free_pool, uga_handle);
Ingo Molnar90a21862018-07-11 11:40:33 +0200337
Matt Fleming291f3632011-12-12 21:27:52 +0000338 return status;
339}
340
341void setup_graphics(struct boot_params *boot_params)
342{
343 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
344 struct screen_info *si;
345 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
346 efi_status_t status;
347 unsigned long size;
348 void **gop_handle = NULL;
349 void **uga_handle = NULL;
350
351 si = &boot_params->screen_info;
352 memset(si, 0, sizeof(*si));
353
354 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000355 status = efi_call_early(locate_handle,
356 EFI_LOCATE_BY_PROTOCOL,
357 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000358 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100359 status = efi_setup_gop(NULL, si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000360
361 if (status != EFI_SUCCESS) {
362 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000363 status = efi_call_early(locate_handle,
364 EFI_LOCATE_BY_PROTOCOL,
365 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000366 if (status == EFI_BUFFER_TOO_SMALL)
367 setup_uga(si, &uga_proto, size);
368 }
369}
370
Matt Fleming291f3632011-12-12 21:27:52 +0000371/*
372 * Because the x86 boot code expects to be passed a boot_params we
373 * need to create one ourselves (usually the bootloader would create
374 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +0100375 *
376 * The caller is responsible for filling out ->code32_start in the
377 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +0000378 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000379struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +0000380{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100381 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100382 struct apm_bios_info *bi;
383 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100384 efi_loaded_image_t *image;
Zhenzhong Duancd6697b2019-07-16 21:15:57 +0800385 void *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100386 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000387 int options_size = 0;
388 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700389 char *cmdline_ptr;
Roy Franz46f45822013-09-22 15:45:39 -0700390 unsigned long ramdisk_addr;
391 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000392
Matt Fleming54b52d82014-01-10 15:27:14 +0000393 efi_early = c;
394 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
395 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100396
397 /* Check if we were booted by the EFI firmware */
398 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
399 return NULL;
400
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900401 if (efi_is_64bit())
Matt Fleming54b52d82014-01-10 15:27:14 +0000402 setup_boot_services64(efi_early);
403 else
404 setup_boot_services32(efi_early);
405
Matt Fleming204b0a12014-03-22 10:09:01 +0000406 status = efi_call_early(handle_protocol, handle,
407 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100408 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700409 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100410 return NULL;
411 }
412
Roy Franz40e45302013-09-22 15:45:29 -0700413 status = efi_low_alloc(sys_table, 0x4000, 1,
414 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100415 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200416 efi_printk(sys_table, "Failed to allocate lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100417 return NULL;
418 }
419
420 memset(boot_params, 0x0, 0x4000);
421
422 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100423 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100424
425 /* Copy the second sector to boot_params */
426 memcpy(&hdr->jump, image->image_base + 512, 512);
427
428 /*
429 * Fill out some of the header fields ourselves because the
430 * EFI firmware loader doesn't load the first sector.
431 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200432 hdr->root_flags = 1;
433 hdr->vid_mode = 0xffff;
434 hdr->boot_flag = 0xAA55;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100435
Matt Fleming291f3632011-12-12 21:27:52 +0000436 hdr->type_of_loader = 0x21;
437
438 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500439 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700440 if (!cmdline_ptr)
441 goto fail;
Ingo Molnar90a21862018-07-11 11:40:33 +0200442
Roy Franz5fef3872013-09-22 15:45:33 -0700443 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700444 /* Fill in upper bits of command line address, NOP on 32 bit */
445 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000446
447 hdr->ramdisk_image = 0;
448 hdr->ramdisk_size = 0;
449
Matt Fleming291f3632011-12-12 21:27:52 +0000450 /* Clear APM BIOS info */
451 memset(bi, 0, sizeof(*bi));
452
Matt Fleming5a17dae2014-08-05 11:52:11 +0100453 status = efi_parse_options(cmdline_ptr);
454 if (status != EFI_SUCCESS)
455 goto fail2;
456
Roy Franz46f45822013-09-22 15:45:39 -0700457 status = handle_cmdline_files(sys_table, image,
458 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700459 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700460 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700461
462 if (status != EFI_SUCCESS &&
463 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
464 efi_printk(sys_table, "Trying to load files to higher address\n");
465 status = handle_cmdline_files(sys_table, image,
466 (char *)(unsigned long)hdr->cmd_line_ptr,
467 "initrd=", -1UL,
468 &ramdisk_addr, &ramdisk_size);
469 }
470
Matt Fleming9ca8f722012-07-19 10:23:48 +0100471 if (status != EFI_SUCCESS)
472 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700473 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
474 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
475 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
476 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100477
478 return boot_params;
Ingo Molnar90a21862018-07-11 11:40:33 +0200479
Matt Fleming9ca8f722012-07-19 10:23:48 +0100480fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -0700481 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100482fail:
Roy Franz40e45302013-09-22 15:45:29 -0700483 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Ingo Molnar90a21862018-07-11 11:40:33 +0200484
Matt Fleming9ca8f722012-07-19 10:23:48 +0100485 return NULL;
486}
487
Linn Crosettod2078d52013-09-22 19:59:08 -0600488static void add_e820ext(struct boot_params *params,
489 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100490{
Linn Crosettod2078d52013-09-22 19:59:08 -0600491 struct setup_data *data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600492
493 e820ext->type = SETUP_E820_EXT;
Ingo Molnar90a21862018-07-11 11:40:33 +0200494 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600495 e820ext->next = 0;
496
497 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
498
499 while (data && data->next)
500 data = (struct setup_data *)(unsigned long)data->next;
501
502 if (data)
503 data->next = (unsigned long)e820ext;
504 else
505 params->hdr.setup_data = (unsigned long)e820ext;
506}
507
Ingo Molnar90a21862018-07-11 11:40:33 +0200508static efi_status_t
509setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
Linn Crosettod2078d52013-09-22 19:59:08 -0600510{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100511 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600512 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100513 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600514 u32 nr_entries;
515 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100516 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000517
Matt Fleming291f3632011-12-12 21:27:52 +0000518 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600519 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
520
521 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000522 efi_memory_desc_t *d;
523 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600524 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000525
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400526#ifdef CONFIG_X86_64
527 m |= (u64)efi->efi_memmap_hi << 32;
528#endif
529
Baoquan He02e43c22017-08-16 21:46:51 +0800530 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000531 switch (d->type) {
532 case EFI_RESERVED_TYPE:
533 case EFI_RUNTIME_SERVICES_CODE:
534 case EFI_RUNTIME_SERVICES_DATA:
535 case EFI_MEMORY_MAPPED_IO:
536 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
537 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100538 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000539 break;
540
541 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100542 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000543 break;
544
545 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100546 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000547 break;
548
549 case EFI_LOADER_CODE:
550 case EFI_LOADER_DATA:
551 case EFI_BOOT_SERVICES_CODE:
552 case EFI_BOOT_SERVICES_DATA:
553 case EFI_CONVENTIONAL_MEMORY:
Dan Williams262b45a2019-11-06 17:43:16 -0800554 if (efi_soft_reserve_enabled() &&
555 (d->attribute & EFI_MEMORY_SP))
556 e820_type = E820_TYPE_SOFT_RESERVED;
557 else
558 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000559 break;
560
561 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100562 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000563 break;
564
Dan Williamsad5fb872015-04-03 12:05:28 -0400565 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100566 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400567 break;
568
Matt Fleming291f3632011-12-12 21:27:52 +0000569 default:
570 continue;
571 }
572
573 /* Merge adjacent mappings */
574 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600575 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000576 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600577 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000578 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600579
Ingo Molnar61a50102017-01-27 13:54:38 +0100580 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100581 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600582 sizeof(struct setup_data);
583
584 if (!e820ext || e820ext_size < need)
585 return EFI_BUFFER_TOO_SMALL;
586
587 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100588 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600589 }
590
Ingo Molnar7410aa12017-01-29 12:56:13 +0100591 entry->addr = d->phys_addr;
592 entry->size = d->num_pages << PAGE_SHIFT;
593 entry->type = e820_type;
594 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600595 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000596 }
597
Ingo Molnar61a50102017-01-27 13:54:38 +0100598 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
599 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600600
601 add_e820ext(params, e820ext, nr_e820ext);
602 nr_entries -= nr_e820ext;
603 }
604
605 params->e820_entries = (u8)nr_entries;
606
607 return EFI_SUCCESS;
608}
609
610static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
611 u32 *e820ext_size)
612{
613 efi_status_t status;
614 unsigned long size;
615
616 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100617 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600618
619 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000620 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600621 *e820ext = NULL;
622 *e820ext_size = 0;
623 }
624
Matt Fleming204b0a12014-03-22 10:09:01 +0000625 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
626 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600627 if (status == EFI_SUCCESS)
628 *e820ext_size = size;
629
630 return status;
631}
632
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100633static efi_status_t allocate_e820(struct boot_params *params,
634 struct setup_data **e820ext,
635 u32 *e820ext_size)
636{
637 unsigned long map_size, desc_size, buff_size;
638 struct efi_boot_memmap boot_map;
639 efi_memory_desc_t *map;
640 efi_status_t status;
641 __u32 nr_desc;
642
643 boot_map.map = &map;
644 boot_map.map_size = &map_size;
645 boot_map.desc_size = &desc_size;
646 boot_map.desc_ver = NULL;
647 boot_map.key_ptr = NULL;
648 boot_map.buff_size = &buff_size;
649
650 status = efi_get_memory_map(sys_table, &boot_map);
651 if (status != EFI_SUCCESS)
652 return status;
653
654 nr_desc = buff_size / desc_size;
655
656 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
657 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
658
659 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
660 if (status != EFI_SUCCESS)
661 return status;
662 }
663
664 return EFI_SUCCESS;
665}
666
Jeffrey Hugod6493402016-08-29 14:38:54 -0600667struct exit_boot_struct {
Ingo Molnar90a21862018-07-11 11:40:33 +0200668 struct boot_params *boot_params;
669 struct efi_info *efi;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600670};
671
672static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
673 struct efi_boot_memmap *map,
674 void *priv)
675{
Jeffrey Hugod6493402016-08-29 14:38:54 -0600676 const char *signature;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600677 struct exit_boot_struct *p = priv;
678
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900679 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
680 : EFI32_LOADER_SIGNATURE;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600681 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
682
Ingo Molnar90a21862018-07-11 11:40:33 +0200683 p->efi->efi_systab = (unsigned long)sys_table_arg;
684 p->efi->efi_memdesc_size = *map->desc_size;
685 p->efi->efi_memdesc_version = *map->desc_ver;
686 p->efi->efi_memmap = (unsigned long)*map->map;
687 p->efi->efi_memmap_size = *map->map_size;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600688
689#ifdef CONFIG_X86_64
Ingo Molnar90a21862018-07-11 11:40:33 +0200690 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
691 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600692#endif
693
694 return EFI_SUCCESS;
695}
696
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900697static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
Linn Crosettod2078d52013-09-22 19:59:08 -0600698{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600699 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600700 efi_memory_desc_t *mem_map;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100701 struct setup_data *e820ext = NULL;
702 __u32 e820ext_size = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600703 efi_status_t status;
704 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600705 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600706 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600707
Ingo Molnar90a21862018-07-11 11:40:33 +0200708 map.map = &mem_map;
709 map.map_size = &map_sz;
710 map.desc_size = &desc_size;
711 map.desc_ver = &desc_version;
712 map.key_ptr = &key;
713 map.buff_size = &buff_size;
714 priv.boot_params = boot_params;
715 priv.efi = &boot_params->efi_info;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100716
717 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
718 if (status != EFI_SUCCESS)
719 return status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600720
Jeffrey Hugod6493402016-08-29 14:38:54 -0600721 /* Might as well exit boot services now */
722 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
723 exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600724 if (status != EFI_SUCCESS)
725 return status;
726
Linn Crosettod2078d52013-09-22 19:59:08 -0600727 /* Historic? */
Ingo Molnar90a21862018-07-11 11:40:33 +0200728 boot_params->alt_mem_k = 32 * 1024;
Linn Crosettod2078d52013-09-22 19:59:08 -0600729
730 status = setup_e820(boot_params, e820ext, e820ext_size);
731 if (status != EFI_SUCCESS)
732 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000733
734 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000735}
736
Matt Fleming9ca8f722012-07-19 10:23:48 +0100737/*
738 * On success we return a pointer to a boot_params structure, and NULL
739 * on failure.
740 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200741struct boot_params *
742efi_main(struct efi_config *c, struct boot_params *boot_params)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100743{
Matt Fleming54b52d82014-01-10 15:27:14 +0000744 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100745 struct setup_header *hdr = &boot_params->hdr;
746 efi_status_t status;
747 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +0000748 void *handle;
749 efi_system_table_t *_table;
Hans de Goedec33ce982018-09-12 20:32:05 +0200750 unsigned long cmdline_paddr;
Matt Fleming54b52d82014-01-10 15:27:14 +0000751
752 efi_early = c;
753
754 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
755 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100756
757 sys_table = _table;
758
759 /* Check if we were booted by the EFI firmware */
760 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
761 goto fail;
762
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900763 if (efi_is_64bit())
Matt Fleming54b52d82014-01-10 15:27:14 +0000764 setup_boot_services64(efi_early);
765 else
766 setup_boot_services32(efi_early);
767
David Howellsde8cb452017-02-06 11:22:43 +0000768 /*
Hans de Goedec33ce982018-09-12 20:32:05 +0200769 * make_boot_params() may have been called before efi_main(), in which
770 * case this is the second time we parse the cmdline. This is ok,
771 * parsing the cmdline multiple times does not have side-effects.
772 */
773 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
774 ((u64)boot_params->ext_cmd_line_ptr << 32));
775 efi_parse_options((char *)cmdline_paddr);
776
777 /*
David Howellsde8cb452017-02-06 11:22:43 +0000778 * If the boot loader gave us a value for secure_boot then we use that,
779 * otherwise we ask the BIOS.
780 */
781 if (boot_params->secure_boot == efi_secureboot_mode_unset)
782 boot_params->secure_boot = efi_get_secureboot(sys_table);
783
Matthew Garrettccc829b2017-08-25 16:50:15 +0100784 /* Ask the firmware to clear memory on unclean shutdown */
785 efi_enable_reset_attack_mitigation(sys_table);
Dominik Brodowski0d959812019-11-06 08:06:13 +0100786
787 efi_random_get_seed(sys_table);
788
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200789 efi_retrieve_tpm2_eventlog(sys_table);
Matthew Garrettccc829b2017-08-25 16:50:15 +0100790
Matt Fleming9ca8f722012-07-19 10:23:48 +0100791 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000792
Matt Fleming56394ab2014-09-11 09:04:25 +0100793 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700794
Lukas Wunner58c54752016-11-12 21:32:36 +0000795 setup_quirks(boot_params);
796
Matt Fleming204b0a12014-03-22 10:09:01 +0000797 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
798 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000799 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200800 efi_printk(sys_table, "Failed to allocate memory for 'gdt' structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000801 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000802 }
Matt Fleming291f3632011-12-12 21:27:52 +0000803
804 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -0700805 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -0700806 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000807 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200808 efi_printk(sys_table, "Failed to allocate memory for 'gdt'\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000809 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000810 }
Matt Fleming291f3632011-12-12 21:27:52 +0000811
Matt Fleming9ca8f722012-07-19 10:23:48 +0100812 /*
813 * If the kernel isn't already loaded at the preferred load
814 * address, relocate it.
815 */
816 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700817 unsigned long bzimage_addr = hdr->code32_start;
818 status = efi_relocate_kernel(sys_table, &bzimage_addr,
819 hdr->init_size, hdr->init_size,
820 hdr->pref_address,
Kairui Song220dd762019-10-29 18:37:54 +0100821 hdr->kernel_alignment,
822 LOAD_PHYSICAL_ADDR);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200823 if (status != EFI_SUCCESS) {
824 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100825 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200826 }
Roy Franz4a9f3a72013-09-22 15:45:32 -0700827
828 hdr->pref_address = hdr->code32_start;
829 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100830 }
831
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900832 status = exit_boot(boot_params, handle);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200833 if (status != EFI_SUCCESS) {
834 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000835 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200836 }
Matt Fleming291f3632011-12-12 21:27:52 +0000837
838 memset((char *)gdt->address, 0x0, gdt->size);
839 desc = (struct desc_struct *)gdt->address;
840
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +0300841 /* The first GDT is a dummy. */
842 desc++;
843
844 if (IS_ENABLED(CONFIG_X86_64)) {
845 /* __KERNEL32_CS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200846 desc->limit0 = 0xffff;
847 desc->base0 = 0x0000;
848 desc->base1 = 0x0000;
849 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
850 desc->s = DESC_TYPE_CODE_DATA;
851 desc->dpl = 0;
852 desc->p = 1;
853 desc->limit1 = 0xf;
854 desc->avl = 0;
855 desc->l = 0;
856 desc->d = SEG_OP_SIZE_32BIT;
857 desc->g = SEG_GRANULARITY_4KB;
858 desc->base2 = 0x00;
859
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +0300860 desc++;
861 } else {
862 /* Second entry is unused on 32-bit */
863 desc++;
864 }
Matt Fleming291f3632011-12-12 21:27:52 +0000865
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300866 /* __KERNEL_CS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200867 desc->limit0 = 0xffff;
868 desc->base0 = 0x0000;
869 desc->base1 = 0x0000;
870 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
871 desc->s = DESC_TYPE_CODE_DATA;
872 desc->dpl = 0;
873 desc->p = 1;
874 desc->limit1 = 0xf;
875 desc->avl = 0;
876
Kirill A. Shutemov4c941172017-06-06 14:31:23 +0300877 if (IS_ENABLED(CONFIG_X86_64)) {
878 desc->l = 1;
879 desc->d = 0;
880 } else {
881 desc->l = 0;
882 desc->d = SEG_OP_SIZE_32BIT;
883 }
Ingo Molnar90a21862018-07-11 11:40:33 +0200884 desc->g = SEG_GRANULARITY_4KB;
885 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +0000886 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300887
888 /* __KERNEL_DS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200889 desc->limit0 = 0xffff;
890 desc->base0 = 0x0000;
891 desc->base1 = 0x0000;
892 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
893 desc->s = DESC_TYPE_CODE_DATA;
894 desc->dpl = 0;
895 desc->p = 1;
896 desc->limit1 = 0xf;
897 desc->avl = 0;
898 desc->l = 0;
899 desc->d = SEG_OP_SIZE_32BIT;
900 desc->g = SEG_GRANULARITY_4KB;
901 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +0000902 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300903
904 if (IS_ENABLED(CONFIG_X86_64)) {
905 /* Task segment value */
Ingo Molnar90a21862018-07-11 11:40:33 +0200906 desc->limit0 = 0x0000;
907 desc->base0 = 0x0000;
908 desc->base1 = 0x0000;
909 desc->type = SEG_TYPE_TSS;
910 desc->s = 0;
911 desc->dpl = 0;
912 desc->p = 1;
913 desc->limit1 = 0x0;
914 desc->avl = 0;
915 desc->l = 0;
916 desc->d = 0;
917 desc->g = SEG_GRANULARITY_4KB;
918 desc->base2 = 0x00;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300919 desc++;
920 }
Matt Fleming291f3632011-12-12 21:27:52 +0000921
Matt Fleming291f3632011-12-12 21:27:52 +0000922 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +0200923 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +0000924
925 return boot_params;
926fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200927 efi_printk(sys_table, "efi_main() failed!\n");
Ingo Molnar90a21862018-07-11 11:40:33 +0200928
Matt Fleming291f3632011-12-12 21:27:52 +0000929 return NULL;
930}