blob: 544ac4fafd112a8b32802e0c4c5a4392b71bd67c [file] [log] [blame]
Eric Snowbergb84a64f2018-11-29 18:12:20 +01001
Matt Fleming291f3632011-12-12 21:27:52 +00002/* -----------------------------------------------------------------------
3 *
4 * Copyright 2011 Intel Corporation; author Matt Fleming
5 *
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
8 *
9 * ----------------------------------------------------------------------- */
10
11#include <linux/efi.h>
Matthew Garrettdd5fc852012-12-05 14:33:26 -070012#include <linux/pci.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010013
Matt Fleming291f3632011-12-12 21:27:52 +000014#include <asm/efi.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010015#include <asm/e820/types.h>
Matt Fleming291f3632011-12-12 21:27:52 +000016#include <asm/setup.h>
17#include <asm/desc.h>
18
Andrey Ryabinin393f2032015-02-13 14:39:56 -080019#include "../string.h"
Matt Fleming291f3632011-12-12 21:27:52 +000020#include "eboot.h"
21
22static efi_system_table_t *sys_table;
23
Matt Fleming84be8802014-09-23 10:37:43 +010024static struct efi_config *efi_early;
25
Ard Biesheuvel243b6752014-11-05 17:00:56 +010026__pure const struct efi_config *__efi_early(void)
27{
28 return efi_early;
29}
Matt Fleming204b0a12014-03-22 10:09:01 +000030
Matt Fleming54b52d82014-01-10 15:27:14 +000031#define BOOT_SERVICES(bits) \
32static void setup_boot_services##bits(struct efi_config *c) \
33{ \
34 efi_system_table_##bits##_t *table; \
Matt Fleming54b52d82014-01-10 15:27:14 +000035 \
36 table = (typeof(table))sys_table; \
37 \
Ingo Molnar90a21862018-07-11 11:40:33 +020038 c->runtime_services = table->runtime; \
39 c->boot_services = table->boottime; \
40 c->text_output = table->con_out; \
Matt Fleming54b52d82014-01-10 15:27:14 +000041}
42BOOT_SERVICES(32);
43BOOT_SERVICES(64);
44
Ard Biesheuvelbd669472014-07-02 14:54:42 +020045void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
Matt Flemingc116e8d2014-01-16 11:35:43 +000046{
Lukas Wunnerdb4545d2017-01-31 13:21:34 +000047 efi_call_proto(efi_simple_text_output_protocol, output_string,
48 efi_early->text_output, str);
Matt Fleming54b52d82014-01-10 15:27:14 +000049}
Lee, Chun-Yideb94102012-12-20 19:33:22 +080050
Matt Flemingc116e8d2014-01-16 11:35:43 +000051static efi_status_t
Ard Biesheuvel75c5a712018-07-20 10:47:19 +090052preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -070053{
Matt Flemingc116e8d2014-01-16 11:35:43 +000054 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -070055 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +000056 unsigned long size;
Ard Biesheuvele2967012018-07-11 11:02:35 +020057 uint64_t romsize;
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020058 void *romimage;
Matt Flemingc116e8d2014-01-16 11:35:43 +000059
Hans de Goede1de3a1b2018-05-04 08:00:01 +020060 /*
Ard Biesheuvele2967012018-07-11 11:02:35 +020061 * Some firmware images contain EFI function pointers at the place where
62 * the romimage and romsize fields are supposed to be. Typically the EFI
Hans de Goede1de3a1b2018-05-04 08:00:01 +020063 * code is mapped at high addresses, translating to an unrealistically
64 * large romsize. The UEFI spec limits the size of option ROMs to 16
65 * MiB so we reject any ROMs over 16 MiB in size to catch this.
66 */
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020067 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
68 romimage, pci);
69 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
Hans de Goede1de3a1b2018-05-04 08:00:01 +020070 if (!romimage || !romsize || romsize > SZ_16M)
Matt Flemingc116e8d2014-01-16 11:35:43 +000071 return EFI_INVALID_PARAMETER;
72
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020073 size = romsize + sizeof(*rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +000074
Matt Fleming204b0a12014-03-22 10:09:01 +000075 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +020076 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +020077 efi_printk(sys_table, "Failed to allocate memory for 'rom'\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000078 return status;
Andre Müller77e21e82014-09-10 01:00:22 +020079 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000080
81 memset(rom, 0, sizeof(*rom));
82
Ingo Molnar90a21862018-07-11 11:40:33 +020083 rom->data.type = SETUP_PCI;
84 rom->data.len = size - sizeof(struct setup_data);
85 rom->data.next = 0;
86 rom->pcilen = pci->romsize;
Matt Flemingc116e8d2014-01-16 11:35:43 +000087 *__rom = rom;
88
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020089 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
90 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
91 &rom->vendor);
Matt Flemingc116e8d2014-01-16 11:35:43 +000092
Andre Müller77e21e82014-09-10 01:00:22 +020093 if (status != EFI_SUCCESS) {
94 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +000095 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +020096 }
Matt Flemingc116e8d2014-01-16 11:35:43 +000097
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +020098 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
99 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
100 &rom->devid);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000101
Andre Müller77e21e82014-09-10 01:00:22 +0200102 if (status != EFI_SUCCESS) {
103 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000104 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200105 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000106
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200107 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
108 &rom->segment, &rom->bus, &rom->device,
109 &rom->function);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000110
111 if (status != EFI_SUCCESS)
112 goto free_struct;
113
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200114 memcpy(rom->romdata, romimage, romsize);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000115 return status;
116
117free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000118 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000119 return status;
120}
121
Matt Fleming56394ab2014-09-11 09:04:25 +0100122/*
123 * There's no way to return an informative status from this function,
124 * because any analysis (and printing of error messages) needs to be
125 * done directly at the EFI function call-site.
126 *
127 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
128 * just didn't find any PCI devices, but there's no way to tell outside
129 * the context of the call.
130 */
131static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000132{
133 efi_status_t status;
134 void **pci_handle = NULL;
135 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
136 unsigned long size = 0;
Ard Biesheuvel75c5a712018-07-20 10:47:19 +0900137 unsigned long nr_pci;
138 struct setup_data *data;
139 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
168 nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
169 for (i = 0; i < nr_pci; i++) {
170 efi_pci_io_protocol_t *pci = NULL;
171 struct pci_setup_rom *rom;
172
173 status = efi_call_early(handle_protocol,
174 efi_is_64bit() ? ((u64 *)pci_handle)[i]
175 : ((u32 *)pci_handle)[i],
176 &pci_proto, (void **)&pci);
177 if (status != EFI_SUCCESS || !pci)
178 continue;
179
180 status = preserve_pci_rom_image(pci, &rom);
181 if (status != EFI_SUCCESS)
182 continue;
183
184 if (data)
185 data->next = (unsigned long)rom;
186 else
187 params->hdr.setup_data = (unsigned long)rom;
188
189 data = (struct setup_data *)rom;
190 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700191
192free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000193 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700194}
195
Lukas Wunner58c54752016-11-12 21:32:36 +0000196static void retrieve_apple_device_properties(struct boot_params *boot_params)
197{
198 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
199 struct setup_data *data, *new;
200 efi_status_t status;
201 u32 size = 0;
202 void *p;
203
204 status = efi_call_early(locate_protocol, &guid, NULL, &p);
205 if (status != EFI_SUCCESS)
206 return;
207
208 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
209 efi_printk(sys_table, "Unsupported properties proto version\n");
210 return;
211 }
212
213 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
214 if (!size)
215 return;
216
217 do {
218 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
219 size + sizeof(struct setup_data), &new);
220 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200221 efi_printk(sys_table, "Failed to allocate memory for 'properties'\n");
Lukas Wunner58c54752016-11-12 21:32:36 +0000222 return;
223 }
224
225 status = efi_call_proto(apple_properties_protocol, get_all, p,
226 new->data, &size);
227
228 if (status == EFI_BUFFER_TOO_SMALL)
229 efi_call_early(free_pool, new);
230 } while (status == EFI_BUFFER_TOO_SMALL);
231
232 new->type = SETUP_APPLE_PROPERTIES;
233 new->len = size;
234 new->next = 0;
235
236 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
Ingo Molnar90a21862018-07-11 11:40:33 +0200237 if (!data) {
Lukas Wunner58c54752016-11-12 21:32:36 +0000238 boot_params->hdr.setup_data = (unsigned long)new;
Ingo Molnar90a21862018-07-11 11:40:33 +0200239 } else {
Lukas Wunner58c54752016-11-12 21:32:36 +0000240 while (data->next)
241 data = (struct setup_data *)(unsigned long)data->next;
242 data->next = (unsigned long)new;
243 }
244}
245
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000246static const efi_char16_t apple[] = L"Apple";
247
Lukas Wunner58c54752016-11-12 21:32:36 +0000248static void setup_quirks(struct boot_params *boot_params)
249{
Lukas Wunner58c54752016-11-12 21:32:36 +0000250 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
251 efi_table_attr(efi_system_table, fw_vendor, sys_table);
252
253 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
254 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
255 retrieve_apple_device_properties(boot_params);
256 }
257}
258
Matt Flemingc116e8d2014-01-16 11:35:43 +0000259/*
260 * See if we have Universal Graphics Adapter (UGA) protocol
261 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200262static efi_status_t
263setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000264{
265 efi_status_t status;
266 u32 width, height;
267 void **uga_handle = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900268 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
269 unsigned long nr_ugas;
270 int i;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000271
Matt Fleming204b0a12014-03-22 10:09:01 +0000272 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
273 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000274 if (status != EFI_SUCCESS)
275 return status;
276
Matt Fleming204b0a12014-03-22 10:09:01 +0000277 status = efi_call_early(locate_handle,
278 EFI_LOCATE_BY_PROTOCOL,
279 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000280 if (status != EFI_SUCCESS)
281 goto free_handle;
282
283 height = 0;
284 width = 0;
285
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900286 first_uga = NULL;
287 nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
288 for (i = 0; i < nr_ugas; i++) {
289 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
290 u32 w, h, depth, refresh;
291 void *pciio;
292 unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
293 : ((u32 *)uga_handle)[i];
294
295 status = efi_call_early(handle_protocol, handle,
296 uga_proto, (void **)&uga);
297 if (status != EFI_SUCCESS)
298 continue;
299
Ard Biesheuvel093174f2018-07-20 10:47:22 +0900300 pciio = NULL;
Ard Biesheuvel290084c2018-07-20 10:47:21 +0900301 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
302
303 status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
304 &w, &h, &depth, &refresh);
305 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
306 width = w;
307 height = h;
308
309 /*
310 * Once we've found a UGA supporting PCIIO,
311 * don't bother looking any further.
312 */
313 if (pciio)
314 break;
315
316 first_uga = uga;
317 }
318 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000319
320 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000321 goto free_handle;
322
323 /* EFI framebuffer */
Ingo Molnar90a21862018-07-11 11:40:33 +0200324 si->orig_video_isVGA = VIDEO_TYPE_EFI;
Matt Fleming291f3632011-12-12 21:27:52 +0000325
Ingo Molnar90a21862018-07-11 11:40:33 +0200326 si->lfb_depth = 32;
327 si->lfb_width = width;
328 si->lfb_height = height;
Matt Fleming291f3632011-12-12 21:27:52 +0000329
Ingo Molnar90a21862018-07-11 11:40:33 +0200330 si->red_size = 8;
331 si->red_pos = 16;
332 si->green_size = 8;
333 si->green_pos = 8;
334 si->blue_size = 8;
335 si->blue_pos = 0;
336 si->rsvd_size = 8;
337 si->rsvd_pos = 24;
Matt Fleming291f3632011-12-12 21:27:52 +0000338
Matt Fleming291f3632011-12-12 21:27:52 +0000339free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000340 efi_call_early(free_pool, uga_handle);
Ingo Molnar90a21862018-07-11 11:40:33 +0200341
Matt Fleming291f3632011-12-12 21:27:52 +0000342 return status;
343}
344
345void setup_graphics(struct boot_params *boot_params)
346{
347 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
348 struct screen_info *si;
349 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
350 efi_status_t status;
351 unsigned long size;
352 void **gop_handle = NULL;
353 void **uga_handle = NULL;
354
355 si = &boot_params->screen_info;
356 memset(si, 0, sizeof(*si));
357
358 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000359 status = efi_call_early(locate_handle,
360 EFI_LOCATE_BY_PROTOCOL,
361 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000362 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100363 status = efi_setup_gop(NULL, si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000364
365 if (status != EFI_SUCCESS) {
366 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000367 status = efi_call_early(locate_handle,
368 EFI_LOCATE_BY_PROTOCOL,
369 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000370 if (status == EFI_BUFFER_TOO_SMALL)
371 setup_uga(si, &uga_proto, size);
372 }
373}
374
Matt Fleming291f3632011-12-12 21:27:52 +0000375/*
376 * Because the x86 boot code expects to be passed a boot_params we
377 * need to create one ourselves (usually the bootloader would create
378 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +0100379 *
380 * The caller is responsible for filling out ->code32_start in the
381 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +0000382 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000383struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +0000384{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100385 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100386 struct apm_bios_info *bi;
387 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100388 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +0000389 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100390 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000391 int options_size = 0;
392 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700393 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +0000394 u16 *s2;
395 u8 *s1;
396 int i;
Roy Franz46f45822013-09-22 15:45:39 -0700397 unsigned long ramdisk_addr;
398 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000399
Matt Fleming54b52d82014-01-10 15:27:14 +0000400 efi_early = c;
401 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
402 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100403
404 /* Check if we were booted by the EFI firmware */
405 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
406 return NULL;
407
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900408 if (efi_is_64bit())
Matt Fleming54b52d82014-01-10 15:27:14 +0000409 setup_boot_services64(efi_early);
410 else
411 setup_boot_services32(efi_early);
412
Matt Fleming204b0a12014-03-22 10:09:01 +0000413 status = efi_call_early(handle_protocol, handle,
414 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100415 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700416 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100417 return NULL;
418 }
419
Roy Franz40e45302013-09-22 15:45:29 -0700420 status = efi_low_alloc(sys_table, 0x4000, 1,
421 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100422 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200423 efi_printk(sys_table, "Failed to allocate lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100424 return NULL;
425 }
426
427 memset(boot_params, 0x0, 0x4000);
428
429 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100430 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100431
432 /* Copy the second sector to boot_params */
433 memcpy(&hdr->jump, image->image_base + 512, 512);
434
435 /*
436 * Fill out some of the header fields ourselves because the
437 * EFI firmware loader doesn't load the first sector.
438 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200439 hdr->root_flags = 1;
440 hdr->vid_mode = 0xffff;
441 hdr->boot_flag = 0xAA55;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100442
Matt Fleming291f3632011-12-12 21:27:52 +0000443 hdr->type_of_loader = 0x21;
444
445 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500446 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700447 if (!cmdline_ptr)
448 goto fail;
Ingo Molnar90a21862018-07-11 11:40:33 +0200449
Roy Franz5fef3872013-09-22 15:45:33 -0700450 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700451 /* Fill in upper bits of command line address, NOP on 32 bit */
452 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000453
454 hdr->ramdisk_image = 0;
455 hdr->ramdisk_size = 0;
456
Matt Fleming291f3632011-12-12 21:27:52 +0000457 /* Clear APM BIOS info */
458 memset(bi, 0, sizeof(*bi));
459
Matt Fleming5a17dae2014-08-05 11:52:11 +0100460 status = efi_parse_options(cmdline_ptr);
461 if (status != EFI_SUCCESS)
462 goto fail2;
463
Roy Franz46f45822013-09-22 15:45:39 -0700464 status = handle_cmdline_files(sys_table, image,
465 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700466 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700467 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700468
469 if (status != EFI_SUCCESS &&
470 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
471 efi_printk(sys_table, "Trying to load files to higher address\n");
472 status = handle_cmdline_files(sys_table, image,
473 (char *)(unsigned long)hdr->cmd_line_ptr,
474 "initrd=", -1UL,
475 &ramdisk_addr, &ramdisk_size);
476 }
477
Matt Fleming9ca8f722012-07-19 10:23:48 +0100478 if (status != EFI_SUCCESS)
479 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700480 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
481 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
482 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
483 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100484
485 return boot_params;
Ingo Molnar90a21862018-07-11 11:40:33 +0200486
Matt Fleming9ca8f722012-07-19 10:23:48 +0100487fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -0700488 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100489fail:
Roy Franz40e45302013-09-22 15:45:29 -0700490 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Ingo Molnar90a21862018-07-11 11:40:33 +0200491
Matt Fleming9ca8f722012-07-19 10:23:48 +0100492 return NULL;
493}
494
Linn Crosettod2078d52013-09-22 19:59:08 -0600495static void add_e820ext(struct boot_params *params,
496 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100497{
Linn Crosettod2078d52013-09-22 19:59:08 -0600498 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100499 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600500 unsigned long size;
501
502 e820ext->type = SETUP_E820_EXT;
Ingo Molnar90a21862018-07-11 11:40:33 +0200503 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600504 e820ext->next = 0;
505
506 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
507
508 while (data && data->next)
509 data = (struct setup_data *)(unsigned long)data->next;
510
511 if (data)
512 data->next = (unsigned long)e820ext;
513 else
514 params->hdr.setup_data = (unsigned long)e820ext;
515}
516
Ingo Molnar90a21862018-07-11 11:40:33 +0200517static efi_status_t
518setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
Linn Crosettod2078d52013-09-22 19:59:08 -0600519{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100520 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600521 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100522 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600523 u32 nr_entries;
524 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100525 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000526
Matt Fleming291f3632011-12-12 21:27:52 +0000527 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600528 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
529
530 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000531 efi_memory_desc_t *d;
532 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600533 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000534
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400535#ifdef CONFIG_X86_64
536 m |= (u64)efi->efi_memmap_hi << 32;
537#endif
538
Baoquan He02e43c22017-08-16 21:46:51 +0800539 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000540 switch (d->type) {
541 case EFI_RESERVED_TYPE:
542 case EFI_RUNTIME_SERVICES_CODE:
543 case EFI_RUNTIME_SERVICES_DATA:
544 case EFI_MEMORY_MAPPED_IO:
545 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
546 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100547 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000548 break;
549
550 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100551 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000552 break;
553
554 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100555 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000556 break;
557
558 case EFI_LOADER_CODE:
559 case EFI_LOADER_DATA:
560 case EFI_BOOT_SERVICES_CODE:
561 case EFI_BOOT_SERVICES_DATA:
562 case EFI_CONVENTIONAL_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100563 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000564 break;
565
566 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100567 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000568 break;
569
Dan Williamsad5fb872015-04-03 12:05:28 -0400570 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100571 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400572 break;
573
Matt Fleming291f3632011-12-12 21:27:52 +0000574 default:
575 continue;
576 }
577
578 /* Merge adjacent mappings */
579 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600580 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000581 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600582 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000583 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600584
Ingo Molnar61a50102017-01-27 13:54:38 +0100585 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100586 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600587 sizeof(struct setup_data);
588
589 if (!e820ext || e820ext_size < need)
590 return EFI_BUFFER_TOO_SMALL;
591
592 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100593 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600594 }
595
Ingo Molnar7410aa12017-01-29 12:56:13 +0100596 entry->addr = d->phys_addr;
597 entry->size = d->num_pages << PAGE_SHIFT;
598 entry->type = e820_type;
599 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600600 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000601 }
602
Ingo Molnar61a50102017-01-27 13:54:38 +0100603 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
604 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600605
606 add_e820ext(params, e820ext, nr_e820ext);
607 nr_entries -= nr_e820ext;
608 }
609
610 params->e820_entries = (u8)nr_entries;
611
612 return EFI_SUCCESS;
613}
614
615static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
616 u32 *e820ext_size)
617{
618 efi_status_t status;
619 unsigned long size;
620
621 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100622 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600623
624 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000625 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600626 *e820ext = NULL;
627 *e820ext_size = 0;
628 }
629
Matt Fleming204b0a12014-03-22 10:09:01 +0000630 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
631 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600632 if (status == EFI_SUCCESS)
633 *e820ext_size = size;
634
635 return status;
636}
637
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100638static efi_status_t allocate_e820(struct boot_params *params,
639 struct setup_data **e820ext,
640 u32 *e820ext_size)
641{
642 unsigned long map_size, desc_size, buff_size;
643 struct efi_boot_memmap boot_map;
644 efi_memory_desc_t *map;
645 efi_status_t status;
646 __u32 nr_desc;
647
648 boot_map.map = &map;
649 boot_map.map_size = &map_size;
650 boot_map.desc_size = &desc_size;
651 boot_map.desc_ver = NULL;
652 boot_map.key_ptr = NULL;
653 boot_map.buff_size = &buff_size;
654
655 status = efi_get_memory_map(sys_table, &boot_map);
656 if (status != EFI_SUCCESS)
657 return status;
658
659 nr_desc = buff_size / desc_size;
660
661 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
662 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
663
664 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
665 if (status != EFI_SUCCESS)
666 return status;
667 }
668
669 return EFI_SUCCESS;
670}
671
Jeffrey Hugod6493402016-08-29 14:38:54 -0600672struct exit_boot_struct {
Ingo Molnar90a21862018-07-11 11:40:33 +0200673 struct boot_params *boot_params;
674 struct efi_info *efi;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600675};
676
677static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
678 struct efi_boot_memmap *map,
679 void *priv)
680{
Jeffrey Hugod6493402016-08-29 14:38:54 -0600681 const char *signature;
682 __u32 nr_desc;
683 efi_status_t status;
684 struct exit_boot_struct *p = priv;
685
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900686 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
687 : EFI32_LOADER_SIGNATURE;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600688 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
689
Ingo Molnar90a21862018-07-11 11:40:33 +0200690 p->efi->efi_systab = (unsigned long)sys_table_arg;
691 p->efi->efi_memdesc_size = *map->desc_size;
692 p->efi->efi_memdesc_version = *map->desc_ver;
693 p->efi->efi_memmap = (unsigned long)*map->map;
694 p->efi->efi_memmap_size = *map->map_size;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600695
696#ifdef CONFIG_X86_64
Ingo Molnar90a21862018-07-11 11:40:33 +0200697 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
698 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600699#endif
700
701 return EFI_SUCCESS;
702}
703
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900704static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
Linn Crosettod2078d52013-09-22 19:59:08 -0600705{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600706 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600707 efi_memory_desc_t *mem_map;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100708 struct setup_data *e820ext = NULL;
709 __u32 e820ext_size = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600710 efi_status_t status;
711 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600712 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600713 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600714
Ingo Molnar90a21862018-07-11 11:40:33 +0200715 map.map = &mem_map;
716 map.map_size = &map_sz;
717 map.desc_size = &desc_size;
718 map.desc_ver = &desc_version;
719 map.key_ptr = &key;
720 map.buff_size = &buff_size;
721 priv.boot_params = boot_params;
722 priv.efi = &boot_params->efi_info;
Eric Snowbergb84a64f2018-11-29 18:12:20 +0100723
724 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
725 if (status != EFI_SUCCESS)
726 return status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600727
Jeffrey Hugod6493402016-08-29 14:38:54 -0600728 /* Might as well exit boot services now */
729 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
730 exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600731 if (status != EFI_SUCCESS)
732 return status;
733
Linn Crosettod2078d52013-09-22 19:59:08 -0600734 /* Historic? */
Ingo Molnar90a21862018-07-11 11:40:33 +0200735 boot_params->alt_mem_k = 32 * 1024;
Linn Crosettod2078d52013-09-22 19:59:08 -0600736
737 status = setup_e820(boot_params, e820ext, e820ext_size);
738 if (status != EFI_SUCCESS)
739 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000740
741 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000742}
743
Matt Fleming9ca8f722012-07-19 10:23:48 +0100744/*
745 * On success we return a pointer to a boot_params structure, and NULL
746 * on failure.
747 */
Ingo Molnar90a21862018-07-11 11:40:33 +0200748struct boot_params *
749efi_main(struct efi_config *c, struct boot_params *boot_params)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100750{
Matt Fleming54b52d82014-01-10 15:27:14 +0000751 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100752 efi_loaded_image_t *image;
753 struct setup_header *hdr = &boot_params->hdr;
754 efi_status_t status;
755 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +0000756 void *handle;
757 efi_system_table_t *_table;
Hans de Goedec33ce982018-09-12 20:32:05 +0200758 unsigned long cmdline_paddr;
Matt Fleming54b52d82014-01-10 15:27:14 +0000759
760 efi_early = c;
761
762 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
763 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100764
765 sys_table = _table;
766
767 /* Check if we were booted by the EFI firmware */
768 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
769 goto fail;
770
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900771 if (efi_is_64bit())
Matt Fleming54b52d82014-01-10 15:27:14 +0000772 setup_boot_services64(efi_early);
773 else
774 setup_boot_services32(efi_early);
775
David Howellsde8cb452017-02-06 11:22:43 +0000776 /*
Hans de Goedec33ce982018-09-12 20:32:05 +0200777 * make_boot_params() may have been called before efi_main(), in which
778 * case this is the second time we parse the cmdline. This is ok,
779 * parsing the cmdline multiple times does not have side-effects.
780 */
781 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
782 ((u64)boot_params->ext_cmd_line_ptr << 32));
783 efi_parse_options((char *)cmdline_paddr);
784
785 /*
David Howellsde8cb452017-02-06 11:22:43 +0000786 * If the boot loader gave us a value for secure_boot then we use that,
787 * otherwise we ask the BIOS.
788 */
789 if (boot_params->secure_boot == efi_secureboot_mode_unset)
790 boot_params->secure_boot = efi_get_secureboot(sys_table);
791
Matthew Garrettccc829b2017-08-25 16:50:15 +0100792 /* Ask the firmware to clear memory on unclean shutdown */
793 efi_enable_reset_attack_mitigation(sys_table);
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200794 efi_retrieve_tpm2_eventlog(sys_table);
Matthew Garrettccc829b2017-08-25 16:50:15 +0100795
Matt Fleming9ca8f722012-07-19 10:23:48 +0100796 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000797
Matt Fleming56394ab2014-09-11 09:04:25 +0100798 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700799
Lukas Wunner58c54752016-11-12 21:32:36 +0000800 setup_quirks(boot_params);
801
Matt Fleming204b0a12014-03-22 10:09:01 +0000802 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
803 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000804 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200805 efi_printk(sys_table, "Failed to allocate memory for 'gdt' structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000806 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000807 }
Matt Fleming291f3632011-12-12 21:27:52 +0000808
809 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -0700810 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -0700811 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000812 if (status != EFI_SUCCESS) {
Ingo Molnar90a21862018-07-11 11:40:33 +0200813 efi_printk(sys_table, "Failed to allocate memory for 'gdt'\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000814 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000815 }
Matt Fleming291f3632011-12-12 21:27:52 +0000816
Matt Fleming9ca8f722012-07-19 10:23:48 +0100817 /*
818 * If the kernel isn't already loaded at the preferred load
819 * address, relocate it.
820 */
821 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700822 unsigned long bzimage_addr = hdr->code32_start;
823 status = efi_relocate_kernel(sys_table, &bzimage_addr,
824 hdr->init_size, hdr->init_size,
825 hdr->pref_address,
826 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200827 if (status != EFI_SUCCESS) {
828 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100829 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200830 }
Roy Franz4a9f3a72013-09-22 15:45:32 -0700831
832 hdr->pref_address = hdr->code32_start;
833 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100834 }
835
Ard Biesheuvelaab95932018-07-20 10:47:24 +0900836 status = exit_boot(boot_params, handle);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200837 if (status != EFI_SUCCESS) {
838 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000839 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200840 }
Matt Fleming291f3632011-12-12 21:27:52 +0000841
842 memset((char *)gdt->address, 0x0, gdt->size);
843 desc = (struct desc_struct *)gdt->address;
844
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +0300845 /* The first GDT is a dummy. */
846 desc++;
847
848 if (IS_ENABLED(CONFIG_X86_64)) {
849 /* __KERNEL32_CS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200850 desc->limit0 = 0xffff;
851 desc->base0 = 0x0000;
852 desc->base1 = 0x0000;
853 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
854 desc->s = DESC_TYPE_CODE_DATA;
855 desc->dpl = 0;
856 desc->p = 1;
857 desc->limit1 = 0xf;
858 desc->avl = 0;
859 desc->l = 0;
860 desc->d = SEG_OP_SIZE_32BIT;
861 desc->g = SEG_GRANULARITY_4KB;
862 desc->base2 = 0x00;
863
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +0300864 desc++;
865 } else {
866 /* Second entry is unused on 32-bit */
867 desc++;
868 }
Matt Fleming291f3632011-12-12 21:27:52 +0000869
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300870 /* __KERNEL_CS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200871 desc->limit0 = 0xffff;
872 desc->base0 = 0x0000;
873 desc->base1 = 0x0000;
874 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
875 desc->s = DESC_TYPE_CODE_DATA;
876 desc->dpl = 0;
877 desc->p = 1;
878 desc->limit1 = 0xf;
879 desc->avl = 0;
880
Kirill A. Shutemov4c941172017-06-06 14:31:23 +0300881 if (IS_ENABLED(CONFIG_X86_64)) {
882 desc->l = 1;
883 desc->d = 0;
884 } else {
885 desc->l = 0;
886 desc->d = SEG_OP_SIZE_32BIT;
887 }
Ingo Molnar90a21862018-07-11 11:40:33 +0200888 desc->g = SEG_GRANULARITY_4KB;
889 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +0000890 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300891
892 /* __KERNEL_DS */
Ingo Molnar90a21862018-07-11 11:40:33 +0200893 desc->limit0 = 0xffff;
894 desc->base0 = 0x0000;
895 desc->base1 = 0x0000;
896 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
897 desc->s = DESC_TYPE_CODE_DATA;
898 desc->dpl = 0;
899 desc->p = 1;
900 desc->limit1 = 0xf;
901 desc->avl = 0;
902 desc->l = 0;
903 desc->d = SEG_OP_SIZE_32BIT;
904 desc->g = SEG_GRANULARITY_4KB;
905 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +0000906 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300907
908 if (IS_ENABLED(CONFIG_X86_64)) {
909 /* Task segment value */
Ingo Molnar90a21862018-07-11 11:40:33 +0200910 desc->limit0 = 0x0000;
911 desc->base0 = 0x0000;
912 desc->base1 = 0x0000;
913 desc->type = SEG_TYPE_TSS;
914 desc->s = 0;
915 desc->dpl = 0;
916 desc->p = 1;
917 desc->limit1 = 0x0;
918 desc->avl = 0;
919 desc->l = 0;
920 desc->d = 0;
921 desc->g = SEG_GRANULARITY_4KB;
922 desc->base2 = 0x00;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +0300923 desc++;
924 }
Matt Fleming291f3632011-12-12 21:27:52 +0000925
Matt Fleming291f3632011-12-12 21:27:52 +0000926 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +0200927 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +0000928
929 return boot_params;
930fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200931 efi_printk(sys_table, "efi_main() failed!\n");
Ingo Molnar90a21862018-07-11 11:40:33 +0200932
Matt Fleming291f3632011-12-12 21:27:52 +0000933 return NULL;
934}