blob: da04948d75edafb6e234daf3d4f19e4d2620bbd1 [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;
Ard Biesheuvelc3710de2019-12-24 16:10:17 +010022static bool efi_is64 = IS_ENABLED(CONFIG_X86_64);
Matt Fleming204b0a12014-03-22 10:09:01 +000023
Ard Biesheuvel2fcdad22019-12-24 16:10:15 +010024__pure efi_system_table_t *efi_system_table(void)
25{
26 return sys_table;
27}
28
Ard Biesheuvelc3710de2019-12-24 16:10:17 +010029__pure bool efi_is_64bit(void)
30{
31 return efi_is64;
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 unsigned long nr_pci;
118 struct setup_data *data;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100119 efi_handle_t h;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900120 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000121
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100122 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
123 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700124
125 if (status == EFI_BUFFER_TOO_SMALL) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100126 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
127 (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700128
Andre Müller77e21e82014-09-10 01:00:22 +0200129 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100130 efi_printk("Failed to allocate memory for 'pci_handle'\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100131 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200132 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700133
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100134 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
135 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700136 }
137
138 if (status != EFI_SUCCESS)
139 goto free_handle;
140
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900141 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
142
143 while (data && data->next)
144 data = (struct setup_data *)(unsigned long)data->next;
145
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100146 for_each_efi_handle(h, pci_handle, size, i) {
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900147 efi_pci_io_protocol_t *pci = NULL;
148 struct pci_setup_rom *rom;
149
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100150 status = efi_bs_call(handle_protocol, h, &pci_proto,
151 (void **)&pci);
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900152 if (status != EFI_SUCCESS || !pci)
153 continue;
154
155 status = preserve_pci_rom_image(pci, &rom);
156 if (status != EFI_SUCCESS)
157 continue;
158
159 if (data)
160 data->next = (unsigned long)rom;
161 else
162 params->hdr.setup_data = (unsigned long)rom;
163
164 data = (struct setup_data *)rom;
165 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700166
167free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100168 efi_bs_call(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700169}
170
Lukas Wunner58c54752016-11-12 21:32:36 +0000171static void retrieve_apple_device_properties(struct boot_params *boot_params)
172{
173 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
174 struct setup_data *data, *new;
175 efi_status_t status;
176 u32 size = 0;
Ard Biesheuvel960a8d02019-12-24 16:10:11 +0100177 apple_properties_protocol_t *p;
Lukas Wunner58c54752016-11-12 21:32:36 +0000178
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100179 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
Lukas Wunner58c54752016-11-12 21:32:36 +0000180 if (status != EFI_SUCCESS)
181 return;
182
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +0100183 if (efi_table_attr(p, version) != 0x10000) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100184 efi_printk("Unsupported properties proto version\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000185 return;
186 }
187
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100188 efi_call_proto(p, get_all, NULL, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000189 if (!size)
190 return;
191
192 do {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100193 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
194 size + sizeof(struct setup_data),
195 (void **)&new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000196 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100197 efi_printk("Failed to allocate memory for 'properties'\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000198 return;
199 }
200
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100201 status = efi_call_proto(p, get_all, new->data, &size);
Lukas Wunner58c54752016-11-12 21:32:36 +0000202
203 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100204 efi_bs_call(free_pool, new);
Lukas Wunner58c54752016-11-12 21:32:36 +0000205 } while (status == EFI_BUFFER_TOO_SMALL);
206
207 new->type = SETUP_APPLE_PROPERTIES;
208 new->len = size;
209 new->next = 0;
210
211 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Ingo Molnar90a21862018-07-11 11:40:33 +0200212 if (!data) {
Lukas Wunner58c54752016-11-12 21:32:36 +0000213 boot_params->hdr.setup_data = (unsigned long)new;
Ingo Molnar90a21862018-07-11 11:40:33 +0200214 } else {
Lukas Wunner58c54752016-11-12 21:32:36 +0000215 while (data->next)
216 data = (struct setup_data *)(unsigned long)data->next;
217 data->next = (unsigned long)new;
218 }
219}
220
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000221static const efi_char16_t apple[] = L"Apple";
222
Lukas Wunner58c54752016-11-12 21:32:36 +0000223static void setup_quirks(struct boot_params *boot_params)
224{
Lukas Wunner58c54752016-11-12 21:32:36 +0000225 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
Ard Biesheuvel99ea8b12019-12-24 16:10:22 +0100226 efi_table_attr(efi_system_table(), fw_vendor);
Lukas Wunner58c54752016-11-12 21:32:36 +0000227
228 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
229 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
230 retrieve_apple_device_properties(boot_params);
231 }
232}
233
Matt Flemingc116e8d2014-01-16 11:35:43 +0000234/*
235 * See if we have Universal Graphics Adapter (UGA) protocol
236 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200237static efi_status_t
238setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000239{
240 efi_status_t status;
241 u32 width, height;
242 void **uga_handle = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900243 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
244 unsigned long nr_ugas;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100245 efi_handle_t handle;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900246 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000247
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100248 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
249 (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000250 if (status != EFI_SUCCESS)
251 return status;
252
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100253 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
254 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000255 if (status != EFI_SUCCESS)
256 goto free_handle;
257
258 height = 0;
259 width = 0;
260
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900261 first_uga = NULL;
Ard Biesheuvel2732ea02019-12-24 16:10:07 +0100262 for_each_efi_handle(handle, uga_handle, size, i) {
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900263 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
264 u32 w, h, depth, refresh;
265 void *pciio;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900266
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100267 status = efi_bs_call(handle_protocol, handle, uga_proto,
268 (void **)&uga);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900269 if (status != EFI_SUCCESS)
270 continue;
271
Ard Biesheuvel093174f2018-07-20 10:47:22 +0900272 pciio = NULL;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100273 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900274
Ard Biesheuvel47c0fd32019-12-24 16:10:21 +0100275 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900276 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
277 width = w;
278 height = h;
279
280 /*
281 * Once we've found a UGA supporting PCIIO,
282 * don't bother looking any further.
283 */
284 if (pciio)
285 break;
286
287 first_uga = uga;
288 }
289 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000290
291 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000292 goto free_handle;
293
294 /* EFI framebuffer */
Ingo Molnar90a21862018-07-11 11:40:33 +0200295 si->orig_video_isVGA = VIDEO_TYPE_EFI;
Matt Fleming291f3632011-12-12 21:27:52 +0000296
Ingo Molnar90a21862018-07-11 11:40:33 +0200297 si->lfb_depth = 32;
298 si->lfb_width = width;
299 si->lfb_height = height;
Matt Fleming291f3632011-12-12 21:27:52 +0000300
Ingo Molnar90a21862018-07-11 11:40:33 +0200301 si->red_size = 8;
302 si->red_pos = 16;
303 si->green_size = 8;
304 si->green_pos = 8;
305 si->blue_size = 8;
306 si->blue_pos = 0;
307 si->rsvd_size = 8;
308 si->rsvd_pos = 24;
Matt Fleming291f3632011-12-12 21:27:52 +0000309
Matt Fleming291f3632011-12-12 21:27:52 +0000310free_handle:
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100311 efi_bs_call(free_pool, uga_handle);
Ingo Molnar90a21862018-07-11 11:40:33 +0200312
Matt Fleming291f3632011-12-12 21:27:52 +0000313 return status;
314}
315
316void setup_graphics(struct boot_params *boot_params)
317{
318 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
319 struct screen_info *si;
320 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
321 efi_status_t status;
322 unsigned long size;
323 void **gop_handle = NULL;
324 void **uga_handle = NULL;
325
326 si = &boot_params->screen_info;
327 memset(si, 0, sizeof(*si));
328
329 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100330 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
331 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000332 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100333 status = efi_setup_gop(si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000334
335 if (status != EFI_SUCCESS) {
336 size = 0;
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100337 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
338 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000339 if (status == EFI_BUFFER_TOO_SMALL)
340 setup_uga(si, &uga_proto, size);
341 }
342}
343
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100344void startup_32(struct boot_params *boot_params);
345
346void __noreturn efi_stub_entry(efi_handle_t handle,
347 efi_system_table_t *sys_table_arg,
348 struct boot_params *boot_params);
349
Matt Fleming291f3632011-12-12 21:27:52 +0000350/*
351 * Because the x86 boot code expects to be passed a boot_params we
352 * need to create one ourselves (usually the bootloader would create
353 * one for us).
354 */
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100355efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
356 efi_system_table_t *sys_table_arg)
Matt Fleming291f3632011-12-12 21:27:52 +0000357{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100358 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100359 struct apm_bios_info *bi;
360 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100361 efi_loaded_image_t *image;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100362 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000363 int options_size = 0;
364 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700365 char *cmdline_ptr;
Roy Franz46f45822013-09-22 15:45:39 -0700366 unsigned long ramdisk_addr;
367 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000368
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100369 sys_table = sys_table_arg;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100370
371 /* Check if we were booted by the EFI firmware */
372 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100373 return EFI_INVALID_PARAMETER;
Matt Fleming54b52d82014-01-10 15:27:14 +0000374
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100375 status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100376 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100377 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100378 return status;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100379 }
380
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100381 status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100382 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100383 efi_printk("Failed to allocate lowmem for boot params\n");
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100384 return status;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100385 }
386
387 memset(boot_params, 0x0, 0x4000);
388
389 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100390 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100391
392 /* Copy the second sector to boot_params */
393 memcpy(&hdr->jump, image->image_base + 512, 512);
394
395 /*
396 * Fill out some of the header fields ourselves because the
397 * EFI firmware loader doesn't load the first sector.
398 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200399 hdr->root_flags = 1;
400 hdr->vid_mode = 0xffff;
401 hdr->boot_flag = 0xAA55;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100402
Matt Fleming291f3632011-12-12 21:27:52 +0000403 hdr->type_of_loader = 0x21;
404
405 /* Convert unicode cmdline to ascii */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100406 cmdline_ptr = efi_convert_cmdline(image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700407 if (!cmdline_ptr)
408 goto fail;
Ingo Molnar90a21862018-07-11 11:40:33 +0200409
Roy Franz5fef3872013-09-22 15:45:33 -0700410 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700411 /* Fill in upper bits of command line address, NOP on 32 bit */
412 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000413
414 hdr->ramdisk_image = 0;
415 hdr->ramdisk_size = 0;
416
Matt Fleming291f3632011-12-12 21:27:52 +0000417 /* Clear APM BIOS info */
418 memset(bi, 0, sizeof(*bi));
419
Matt Fleming5a17dae2014-08-05 11:52:11 +0100420 status = efi_parse_options(cmdline_ptr);
421 if (status != EFI_SUCCESS)
422 goto fail2;
423
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100424 status = handle_cmdline_files(image,
Roy Franz46f45822013-09-22 15:45:39 -0700425 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700426 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700427 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700428
429 if (status != EFI_SUCCESS &&
430 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100431 efi_printk("Trying to load files to higher address\n");
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100432 status = handle_cmdline_files(image,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700433 (char *)(unsigned long)hdr->cmd_line_ptr,
434 "initrd=", -1UL,
435 &ramdisk_addr, &ramdisk_size);
436 }
437
Matt Fleming9ca8f722012-07-19 10:23:48 +0100438 if (status != EFI_SUCCESS)
439 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700440 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
441 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
442 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
443 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100444
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100445 hdr->code32_start = (u32)(unsigned long)startup_32;
446
447 efi_stub_entry(handle, sys_table, boot_params);
448 /* not reached */
Ingo Molnar90a21862018-07-11 11:40:33 +0200449
Matt Fleming9ca8f722012-07-19 10:23:48 +0100450fail2:
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100451 efi_free(options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100452fail:
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100453 efi_free(0x4000, (unsigned long)boot_params);
Ingo Molnar90a21862018-07-11 11:40:33 +0200454
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100455 return status;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100456}
457
Linn Crosettod2078d52013-09-22 19:59:08 -0600458static void add_e820ext(struct boot_params *params,
459 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100460{
Linn Crosettod2078d52013-09-22 19:59:08 -0600461 struct setup_data *data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600462
463 e820ext->type = SETUP_E820_EXT;
Ingo Molnar90a21862018-07-11 11:40:33 +0200464 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600465 e820ext->next = 0;
466
467 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
468
469 while (data && data->next)
470 data = (struct setup_data *)(unsigned long)data->next;
471
472 if (data)
473 data->next = (unsigned long)e820ext;
474 else
475 params->hdr.setup_data = (unsigned long)e820ext;
476}
477
Ingo Molnar90a21862018-07-11 11:40:33 +0200478static efi_status_t
479setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
Linn Crosettod2078d52013-09-22 19:59:08 -0600480{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100481 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600482 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100483 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600484 u32 nr_entries;
485 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100486 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000487
Matt Fleming291f3632011-12-12 21:27:52 +0000488 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600489 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
490
491 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000492 efi_memory_desc_t *d;
493 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600494 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000495
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400496#ifdef CONFIG_X86_64
497 m |= (u64)efi->efi_memmap_hi << 32;
498#endif
499
Baoquan He02e43c22017-08-16 21:46:51 +0800500 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000501 switch (d->type) {
502 case EFI_RESERVED_TYPE:
503 case EFI_RUNTIME_SERVICES_CODE:
504 case EFI_RUNTIME_SERVICES_DATA:
505 case EFI_MEMORY_MAPPED_IO:
506 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
507 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100508 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000509 break;
510
511 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100512 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000513 break;
514
515 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100516 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000517 break;
518
519 case EFI_LOADER_CODE:
520 case EFI_LOADER_DATA:
521 case EFI_BOOT_SERVICES_CODE:
522 case EFI_BOOT_SERVICES_DATA:
523 case EFI_CONVENTIONAL_MEMORY:
Dan Williams262b45a2019-11-06 17:43:16 -0800524 if (efi_soft_reserve_enabled() &&
525 (d->attribute & EFI_MEMORY_SP))
526 e820_type = E820_TYPE_SOFT_RESERVED;
527 else
528 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000529 break;
530
531 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100532 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000533 break;
534
Dan Williamsad5fb872015-04-03 12:05:28 -0400535 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100536 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400537 break;
538
Matt Fleming291f3632011-12-12 21:27:52 +0000539 default:
540 continue;
541 }
542
543 /* Merge adjacent mappings */
544 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600545 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000546 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600547 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000548 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600549
Ingo Molnar61a50102017-01-27 13:54:38 +0100550 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100551 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600552 sizeof(struct setup_data);
553
554 if (!e820ext || e820ext_size < need)
555 return EFI_BUFFER_TOO_SMALL;
556
557 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100558 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600559 }
560
Ingo Molnar7410aa12017-01-29 12:56:13 +0100561 entry->addr = d->phys_addr;
562 entry->size = d->num_pages << PAGE_SHIFT;
563 entry->type = e820_type;
564 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600565 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000566 }
567
Ingo Molnar61a50102017-01-27 13:54:38 +0100568 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
569 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600570
571 add_e820ext(params, e820ext, nr_e820ext);
572 nr_entries -= nr_e820ext;
573 }
574
575 params->e820_entries = (u8)nr_entries;
576
577 return EFI_SUCCESS;
578}
579
580static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
581 u32 *e820ext_size)
582{
583 efi_status_t status;
584 unsigned long size;
585
586 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100587 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600588
589 if (*e820ext) {
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100590 efi_bs_call(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600591 *e820ext = NULL;
592 *e820ext_size = 0;
593 }
594
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100595 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
596 (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600597 if (status == EFI_SUCCESS)
598 *e820ext_size = size;
599
600 return status;
601}
602
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100603static efi_status_t allocate_e820(struct boot_params *params,
604 struct setup_data **e820ext,
605 u32 *e820ext_size)
606{
607 unsigned long map_size, desc_size, buff_size;
608 struct efi_boot_memmap boot_map;
609 efi_memory_desc_t *map;
610 efi_status_t status;
611 __u32 nr_desc;
612
613 boot_map.map = &map;
614 boot_map.map_size = &map_size;
615 boot_map.desc_size = &desc_size;
616 boot_map.desc_ver = NULL;
617 boot_map.key_ptr = NULL;
618 boot_map.buff_size = &buff_size;
619
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100620 status = efi_get_memory_map(&boot_map);
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100621 if (status != EFI_SUCCESS)
622 return status;
623
624 nr_desc = buff_size / desc_size;
625
626 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
627 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
628
629 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
630 if (status != EFI_SUCCESS)
631 return status;
632 }
633
634 return EFI_SUCCESS;
635}
636
Jeffrey Hugod6493402016-08-29 14:38:54 -0600637struct exit_boot_struct {
Ingo Molnar90a21862018-07-11 11:40:33 +0200638 struct boot_params *boot_params;
639 struct efi_info *efi;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600640};
641
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100642static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
Jeffrey Hugod6493402016-08-29 14:38:54 -0600643 void *priv)
644{
Jeffrey Hugod6493402016-08-29 14:38:54 -0600645 const char *signature;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600646 struct exit_boot_struct *p = priv;
647
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900648 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
649 : EFI32_LOADER_SIGNATURE;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600650 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
651
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100652 p->efi->efi_systab = (unsigned long)efi_system_table();
Ingo Molnar90a21862018-07-11 11:40:33 +0200653 p->efi->efi_memdesc_size = *map->desc_size;
654 p->efi->efi_memdesc_version = *map->desc_ver;
655 p->efi->efi_memmap = (unsigned long)*map->map;
656 p->efi->efi_memmap_size = *map->map_size;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600657
658#ifdef CONFIG_X86_64
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100659 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32;
Ingo Molnar90a21862018-07-11 11:40:33 +0200660 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600661#endif
662
663 return EFI_SUCCESS;
664}
665
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900666static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
Linn Crosettod2078d52013-09-22 19:59:08 -0600667{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600668 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600669 efi_memory_desc_t *mem_map;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100670 struct setup_data *e820ext = NULL;
671 __u32 e820ext_size = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600672 efi_status_t status;
673 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600674 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600675 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600676
Ingo Molnar90a21862018-07-11 11:40:33 +0200677 map.map = &mem_map;
678 map.map_size = &map_sz;
679 map.desc_size = &desc_size;
680 map.desc_ver = &desc_version;
681 map.key_ptr = &key;
682 map.buff_size = &buff_size;
683 priv.boot_params = boot_params;
684 priv.efi = &boot_params->efi_info;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100685
686 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
687 if (status != EFI_SUCCESS)
688 return status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600689
Jeffrey Hugod6493402016-08-29 14:38:54 -0600690 /* Might as well exit boot services now */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100691 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600692 if (status != EFI_SUCCESS)
693 return status;
694
Linn Crosettod2078d52013-09-22 19:59:08 -0600695 /* Historic? */
Ingo Molnar90a21862018-07-11 11:40:33 +0200696 boot_params->alt_mem_k = 32 * 1024;
Linn Crosettod2078d52013-09-22 19:59:08 -0600697
698 status = setup_e820(boot_params, e820ext, e820ext_size);
699 if (status != EFI_SUCCESS)
700 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000701
702 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000703}
704
Matt Fleming9ca8f722012-07-19 10:23:48 +0100705/*
706 * On success we return a pointer to a boot_params structure, and NULL
707 * on failure.
708 */
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100709struct boot_params *efi_main(efi_handle_t handle,
710 efi_system_table_t *sys_table_arg,
711 struct boot_params *boot_params,
712 bool is64)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100713{
Matt Fleming54b52d82014-01-10 15:27:14 +0000714 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100715 struct setup_header *hdr = &boot_params->hdr;
716 efi_status_t status;
717 struct desc_struct *desc;
Hans de Goedec33ce982018-09-12 20:32:05 +0200718 unsigned long cmdline_paddr;
Matt Fleming54b52d82014-01-10 15:27:14 +0000719
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100720 sys_table = sys_table_arg;
Matt Fleming54b52d82014-01-10 15:27:14 +0000721
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100722 if (IS_ENABLED(CONFIG_EFI_MIXED))
723 efi_is64 = is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100724
725 /* Check if we were booted by the EFI firmware */
726 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
727 goto fail;
728
David Howellsde8cb452017-02-06 11:22:43 +0000729 /*
Hans de Goedec33ce982018-09-12 20:32:05 +0200730 * make_boot_params() may have been called before efi_main(), in which
731 * case this is the second time we parse the cmdline. This is ok,
732 * parsing the cmdline multiple times does not have side-effects.
733 */
734 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
735 ((u64)boot_params->ext_cmd_line_ptr << 32));
736 efi_parse_options((char *)cmdline_paddr);
737
738 /*
David Howellsde8cb452017-02-06 11:22:43 +0000739 * If the boot loader gave us a value for secure_boot then we use that,
740 * otherwise we ask the BIOS.
741 */
742 if (boot_params->secure_boot == efi_secureboot_mode_unset)
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100743 boot_params->secure_boot = efi_get_secureboot();
David Howellsde8cb452017-02-06 11:22:43 +0000744
Matthew Garrettccc829b2017-08-25 16:50:15 +0100745 /* Ask the firmware to clear memory on unclean shutdown */
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100746 efi_enable_reset_attack_mitigation();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100747
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100748 efi_random_get_seed();
Dominik Brodowski0d959812019-11-06 08:06:13 +0100749
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100750 efi_retrieve_tpm2_eventlog();
Matthew Garrettccc829b2017-08-25 16:50:15 +0100751
Matt Fleming9ca8f722012-07-19 10:23:48 +0100752 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000753
Matt Fleming56394ab2014-09-11 09:04:25 +0100754 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700755
Lukas Wunner58c54752016-11-12 21:32:36 +0000756 setup_quirks(boot_params);
757
Ard Biesheuvel966291f2019-12-24 16:10:23 +0100758 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*gdt),
759 (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000760 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100761 efi_printk("Failed to allocate memory for 'gdt' structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000762 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000763 }
Matt Fleming291f3632011-12-12 21:27:52 +0000764
765 gdt->size = 0x800;
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100766 status = efi_low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000767 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100768 efi_printk("Failed to allocate memory for 'gdt'\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000769 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000770 }
Matt Fleming291f3632011-12-12 21:27:52 +0000771
Matt Fleming9ca8f722012-07-19 10:23:48 +0100772 /*
773 * If the kernel isn't already loaded at the preferred load
774 * address, relocate it.
775 */
776 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700777 unsigned long bzimage_addr = hdr->code32_start;
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100778 status = efi_relocate_kernel(&bzimage_addr,
Roy Franz4a9f3a72013-09-22 15:45:32 -0700779 hdr->init_size, hdr->init_size,
780 hdr->pref_address,
Kairui Song220dd762019-10-29 18:37:54 +0100781 hdr->kernel_alignment,
782 LOAD_PHYSICAL_ADDR);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200783 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100784 efi_printk("efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100785 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200786 }
Roy Franz4a9f3a72013-09-22 15:45:32 -0700787
788 hdr->pref_address = hdr->code32_start;
789 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100790 }
791
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900792 status = exit_boot(boot_params, handle);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200793 if (status != EFI_SUCCESS) {
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100794 efi_printk("exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000795 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200796 }
Matt Fleming291f3632011-12-12 21:27:52 +0000797
798 memset((char *)gdt->address, 0x0, gdt->size);
799 desc = (struct desc_struct *)gdt->address;
800
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +0300801 /* The first GDT is a dummy. */
802 desc++;
803
804 if (IS_ENABLED(CONFIG_X86_64)) {
805 /* __KERNEL32_CS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200806 desc->limit0 = 0xffff;
807 desc->base0 = 0x0000;
808 desc->base1 = 0x0000;
809 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
810 desc->s = DESC_TYPE_CODE_DATA;
811 desc->dpl = 0;
812 desc->p = 1;
813 desc->limit1 = 0xf;
814 desc->avl = 0;
815 desc->l = 0;
816 desc->d = SEG_OP_SIZE_32BIT;
817 desc->g = SEG_GRANULARITY_4KB;
818 desc->base2 = 0x00;
819
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +0300820 desc++;
821 } else {
822 /* Second entry is unused on 32-bit */
823 desc++;
824 }
Matt Fleming291f3632011-12-12 21:27:52 +0000825
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300826 /* __KERNEL_CS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200827 desc->limit0 = 0xffff;
828 desc->base0 = 0x0000;
829 desc->base1 = 0x0000;
830 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
831 desc->s = DESC_TYPE_CODE_DATA;
832 desc->dpl = 0;
833 desc->p = 1;
834 desc->limit1 = 0xf;
835 desc->avl = 0;
836
Kirill A. Shutemov4c941172017-06-06 14:31:23 +0300837 if (IS_ENABLED(CONFIG_X86_64)) {
838 desc->l = 1;
839 desc->d = 0;
840 } else {
841 desc->l = 0;
842 desc->d = SEG_OP_SIZE_32BIT;
843 }
Ingo Molnar90a21862018-07-11 11:40:33 +0200844 desc->g = SEG_GRANULARITY_4KB;
845 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +0000846 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300847
848 /* __KERNEL_DS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200849 desc->limit0 = 0xffff;
850 desc->base0 = 0x0000;
851 desc->base1 = 0x0000;
852 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
853 desc->s = DESC_TYPE_CODE_DATA;
854 desc->dpl = 0;
855 desc->p = 1;
856 desc->limit1 = 0xf;
857 desc->avl = 0;
858 desc->l = 0;
859 desc->d = SEG_OP_SIZE_32BIT;
860 desc->g = SEG_GRANULARITY_4KB;
861 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +0000862 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300863
864 if (IS_ENABLED(CONFIG_X86_64)) {
865 /* Task segment value */
Ingo Molnar90a21862018-07-11 11:40:33 +0200866 desc->limit0 = 0x0000;
867 desc->base0 = 0x0000;
868 desc->base1 = 0x0000;
869 desc->type = SEG_TYPE_TSS;
870 desc->s = 0;
871 desc->dpl = 0;
872 desc->p = 1;
873 desc->limit1 = 0x0;
874 desc->avl = 0;
875 desc->l = 0;
876 desc->d = 0;
877 desc->g = SEG_GRANULARITY_4KB;
878 desc->base2 = 0x00;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300879 desc++;
880 }
Matt Fleming291f3632011-12-12 21:27:52 +0000881
Matt Fleming291f3632011-12-12 21:27:52 +0000882 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +0200883 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +0000884
885 return boot_params;
886fail:
Ard Biesheuvel8173ec72019-12-24 16:10:18 +0100887 efi_printk("efi_main() failed!\n");
Ingo Molnar90a21862018-07-11 11:40:33 +0200888
Ard Biesheuvelc3710de2019-12-24 16:10:17 +0100889 for (;;)
890 asm("hlt");
Matt Fleming291f3632011-12-12 21:27:52 +0000891}
Ard Biesheuvel23e60392019-12-24 16:10:20 +0100892
893#ifdef CONFIG_EFI_MIXED
894void efi_free_native(unsigned long size, unsigned long addr);
895
896void efi_free(unsigned long size, unsigned long addr)
897{
898 if (!size)
899 return;
900
901 if (efi_is_native())
902 efi_free_native(size, addr);
903 else
904 efi64_thunk(efi_system_table()->boottime->mixed_mode.free_pages,
905 addr, 0, DIV_ROUND_UP(size, EFI_PAGE_SIZE));
906}
907#endif