blob: a8a8642d2b0b802424caf7b4f925edbce7e3284e [file] [log] [blame]
Matt Fleming291f3632011-12-12 21:27:52 +00001/* -----------------------------------------------------------------------
2 *
3 * Copyright 2011 Intel Corporation; author Matt Fleming
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
7 *
8 * ----------------------------------------------------------------------- */
9
10#include <linux/efi.h>
Matthew Garrettdd5fc852012-12-05 14:33:26 -070011#include <linux/pci.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010012
Matt Fleming291f3632011-12-12 21:27:52 +000013#include <asm/efi.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010014#include <asm/e820/types.h>
Matt Fleming291f3632011-12-12 21:27:52 +000015#include <asm/setup.h>
16#include <asm/desc.h>
17
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 \
David Howellsa2cd2f32017-02-06 11:22:40 +000037 c->runtime_services = table->runtime; \
Lukas Wunner0a637ee2016-08-22 12:01:21 +020038 c->boot_services = table->boottime; \
Matt Fleming54b52d82014-01-10 15:27:14 +000039 c->text_output = table->con_out; \
Matt Fleming54b52d82014-01-10 15:27:14 +000040}
41BOOT_SERVICES(32);
42BOOT_SERVICES(64);
43
Matt Flemingc116e8d2014-01-16 11:35:43 +000044static inline efi_status_t __open_volume32(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +000045{
46 efi_file_io_interface_t *io;
Matt Flemingc116e8d2014-01-16 11:35:43 +000047 efi_loaded_image_32_t *image = __image;
48 efi_file_handle_32_t *fh;
Matt Fleming54b52d82014-01-10 15:27:14 +000049 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
50 efi_status_t status;
51 void *handle = (void *)(unsigned long)image->device_handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +000052 unsigned long func;
Matt Fleming54b52d82014-01-10 15:27:14 +000053
Matt Fleming204b0a12014-03-22 10:09:01 +000054 status = efi_call_early(handle_protocol, handle,
55 &fs_proto, (void **)&io);
Matt Fleming54b52d82014-01-10 15:27:14 +000056 if (status != EFI_SUCCESS) {
57 efi_printk(sys_table, "Failed to handle fs_proto\n");
58 return status;
59 }
60
61 func = (unsigned long)io->open_volume;
62 status = efi_early->call(func, io, &fh);
63 if (status != EFI_SUCCESS)
64 efi_printk(sys_table, "Failed to open volume\n");
65
66 *__fh = fh;
67 return status;
68}
69
Matt Flemingc116e8d2014-01-16 11:35:43 +000070static inline efi_status_t __open_volume64(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +000071{
Matt Flemingc116e8d2014-01-16 11:35:43 +000072 efi_file_io_interface_t *io;
73 efi_loaded_image_64_t *image = __image;
74 efi_file_handle_64_t *fh;
75 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
76 efi_status_t status;
77 void *handle = (void *)(unsigned long)image->device_handle;
78 unsigned long func;
79
Matt Fleming204b0a12014-03-22 10:09:01 +000080 status = efi_call_early(handle_protocol, handle,
81 &fs_proto, (void **)&io);
Matt Flemingc116e8d2014-01-16 11:35:43 +000082 if (status != EFI_SUCCESS) {
83 efi_printk(sys_table, "Failed to handle fs_proto\n");
84 return status;
85 }
86
87 func = (unsigned long)io->open_volume;
88 status = efi_early->call(func, io, &fh);
89 if (status != EFI_SUCCESS)
90 efi_printk(sys_table, "Failed to open volume\n");
91
92 *__fh = fh;
93 return status;
94}
95
Ard Biesheuvelbd669472014-07-02 14:54:42 +020096efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +000097efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
98{
99 if (efi_early->is64)
100 return __open_volume64(__image, __fh);
101
102 return __open_volume32(__image, __fh);
103}
104
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200105void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000106{
Lukas Wunnerdb4545d2017-01-31 13:21:34 +0000107 efi_call_proto(efi_simple_text_output_protocol, output_string,
108 efi_early->text_output, str);
Matt Fleming54b52d82014-01-10 15:27:14 +0000109}
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800110
Matt Flemingc116e8d2014-01-16 11:35:43 +0000111static efi_status_t
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200112__setup_efi_pci(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700113{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000114 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700115 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000116 unsigned long size;
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200117 uint64_t attributes, romsize;
118 void *romimage;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000119
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200120 status = efi_call_proto(efi_pci_io_protocol, attributes, pci,
121 EfiPciIoAttributeOperationGet, 0, 0,
122 &attributes);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000123 if (status != EFI_SUCCESS)
124 return status;
125
Hans de Goede1de3a1b2018-05-04 08:00:01 +0200126 /*
127 * Some firmware images contain EFI function pointers at the place where the
128 * romimage and romsize fields are supposed to be. Typically the EFI
129 * code is mapped at high addresses, translating to an unrealistically
130 * large romsize. The UEFI spec limits the size of option ROMs to 16
131 * MiB so we reject any ROMs over 16 MiB in size to catch this.
132 */
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200133 romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
134 romimage, pci);
135 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
Hans de Goede1de3a1b2018-05-04 08:00:01 +0200136 if (!romimage || !romsize || romsize > SZ_16M)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000137 return EFI_INVALID_PARAMETER;
138
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200139 size = romsize + sizeof(*rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000140
Matt Fleming204b0a12014-03-22 10:09:01 +0000141 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200142 if (status != EFI_SUCCESS) {
143 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000144 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200145 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000146
147 memset(rom, 0, sizeof(*rom));
148
149 rom->data.type = SETUP_PCI;
150 rom->data.len = size - sizeof(struct setup_data);
151 rom->data.next = 0;
152 rom->pcilen = pci->romsize;
153 *__rom = rom;
154
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200155 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
156 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
157 &rom->vendor);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000158
Andre Müller77e21e82014-09-10 01:00:22 +0200159 if (status != EFI_SUCCESS) {
160 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000161 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200162 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000163
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200164 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
165 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
166 &rom->devid);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000167
Andre Müller77e21e82014-09-10 01:00:22 +0200168 if (status != EFI_SUCCESS) {
169 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000170 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200171 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000172
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200173 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
174 &rom->segment, &rom->bus, &rom->device,
175 &rom->function);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000176
177 if (status != EFI_SUCCESS)
178 goto free_struct;
179
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200180 memcpy(rom->romdata, romimage, romsize);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000181 return status;
182
183free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000184 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000185 return status;
186}
187
Matt Fleming56394ab2014-09-11 09:04:25 +0100188static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000189setup_efi_pci32(struct boot_params *params, void **pci_handle,
190 unsigned long size)
191{
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200192 efi_pci_io_protocol_t *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700193 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000194 u32 *handles = (u32 *)(unsigned long)pci_handle;
195 efi_status_t status;
196 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700197 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000198 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700199
Jan Beulichbc754792013-01-18 12:35:14 +0000200 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700201
202 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000203 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700204
Matt Flemingc116e8d2014-01-16 11:35:43 +0000205 nr_pci = size / sizeof(u32);
206 for (i = 0; i < nr_pci; i++) {
207 struct pci_setup_rom *rom = NULL;
208 u32 h = handles[i];
209
Matt Fleming204b0a12014-03-22 10:09:01 +0000210 status = efi_call_early(handle_protocol, h,
211 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000212
213 if (status != EFI_SUCCESS)
214 continue;
215
216 if (!pci)
217 continue;
218
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200219 status = __setup_efi_pci(pci, &rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000220 if (status != EFI_SUCCESS)
221 continue;
222
223 if (data)
224 data->next = (unsigned long)rom;
225 else
226 params->hdr.setup_data = (unsigned long)rom;
227
228 data = (struct setup_data *)rom;
229
230 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000231}
232
Matt Fleming56394ab2014-09-11 09:04:25 +0100233static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000234setup_efi_pci64(struct boot_params *params, void **pci_handle,
235 unsigned long size)
236{
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200237 efi_pci_io_protocol_t *pci = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000238 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
239 u64 *handles = (u64 *)(unsigned long)pci_handle;
240 efi_status_t status;
241 unsigned long nr_pci;
242 struct setup_data *data;
243 int i;
244
245 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
246
247 while (data && data->next)
248 data = (struct setup_data *)(unsigned long)data->next;
249
250 nr_pci = size / sizeof(u64);
251 for (i = 0; i < nr_pci; i++) {
252 struct pci_setup_rom *rom = NULL;
253 u64 h = handles[i];
254
Matt Fleming204b0a12014-03-22 10:09:01 +0000255 status = efi_call_early(handle_protocol, h,
256 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000257
258 if (status != EFI_SUCCESS)
259 continue;
260
261 if (!pci)
262 continue;
263
Ard Biesheuvel2c3625c2018-05-04 08:00:00 +0200264 status = __setup_efi_pci(pci, &rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000265 if (status != EFI_SUCCESS)
266 continue;
267
268 if (data)
269 data->next = (unsigned long)rom;
270 else
271 params->hdr.setup_data = (unsigned long)rom;
272
273 data = (struct setup_data *)rom;
274
275 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000276}
277
Matt Fleming56394ab2014-09-11 09:04:25 +0100278/*
279 * There's no way to return an informative status from this function,
280 * because any analysis (and printing of error messages) needs to be
281 * done directly at the EFI function call-site.
282 *
283 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
284 * just didn't find any PCI devices, but there's no way to tell outside
285 * the context of the call.
286 */
287static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000288{
289 efi_status_t status;
290 void **pci_handle = NULL;
291 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
292 unsigned long size = 0;
293
Matt Fleming204b0a12014-03-22 10:09:01 +0000294 status = efi_call_early(locate_handle,
295 EFI_LOCATE_BY_PROTOCOL,
296 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700297
298 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000299 status = efi_call_early(allocate_pool,
300 EFI_LOADER_DATA,
301 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700302
Andre Müller77e21e82014-09-10 01:00:22 +0200303 if (status != EFI_SUCCESS) {
304 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100305 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200306 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700307
Matt Fleming204b0a12014-03-22 10:09:01 +0000308 status = efi_call_early(locate_handle,
309 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
310 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700311 }
312
313 if (status != EFI_SUCCESS)
314 goto free_handle;
315
Matt Flemingc116e8d2014-01-16 11:35:43 +0000316 if (efi_early->is64)
Matt Fleming56394ab2014-09-11 09:04:25 +0100317 setup_efi_pci64(params, pci_handle, size);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000318 else
Matt Fleming56394ab2014-09-11 09:04:25 +0100319 setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700320
321free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000322 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700323}
324
Lukas Wunner58c54752016-11-12 21:32:36 +0000325static void retrieve_apple_device_properties(struct boot_params *boot_params)
326{
327 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
328 struct setup_data *data, *new;
329 efi_status_t status;
330 u32 size = 0;
331 void *p;
332
333 status = efi_call_early(locate_protocol, &guid, NULL, &p);
334 if (status != EFI_SUCCESS)
335 return;
336
337 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
338 efi_printk(sys_table, "Unsupported properties proto version\n");
339 return;
340 }
341
342 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
343 if (!size)
344 return;
345
346 do {
347 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
348 size + sizeof(struct setup_data), &new);
349 if (status != EFI_SUCCESS) {
350 efi_printk(sys_table,
351 "Failed to alloc mem for properties\n");
352 return;
353 }
354
355 status = efi_call_proto(apple_properties_protocol, get_all, p,
356 new->data, &size);
357
358 if (status == EFI_BUFFER_TOO_SMALL)
359 efi_call_early(free_pool, new);
360 } while (status == EFI_BUFFER_TOO_SMALL);
361
362 new->type = SETUP_APPLE_PROPERTIES;
363 new->len = size;
364 new->next = 0;
365
366 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
367 if (!data)
368 boot_params->hdr.setup_data = (unsigned long)new;
369 else {
370 while (data->next)
371 data = (struct setup_data *)(unsigned long)data->next;
372 data->next = (unsigned long)new;
373 }
374}
375
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000376static const efi_char16_t apple[] = L"Apple";
377
Lukas Wunner58c54752016-11-12 21:32:36 +0000378static void setup_quirks(struct boot_params *boot_params)
379{
Lukas Wunner58c54752016-11-12 21:32:36 +0000380 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
381 efi_table_attr(efi_system_table, fw_vendor, sys_table);
382
383 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
384 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
385 retrieve_apple_device_properties(boot_params);
386 }
387}
388
Matt Flemingc116e8d2014-01-16 11:35:43 +0000389static efi_status_t
390setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000391{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000392 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
393 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000394 unsigned long nr_ugas;
Ingo Molnared7158b2018-02-22 10:54:55 +0100395 u32 *handles = (u32 *)uga_handle;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100396 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Fleming291f3632011-12-12 21:27:52 +0000397 int i;
398
Matt Fleming291f3632011-12-12 21:27:52 +0000399 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000400 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000401 for (i = 0; i < nr_ugas; i++) {
402 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000403 u32 w, h, depth, refresh;
404 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000405 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000406
Matt Fleming204b0a12014-03-22 10:09:01 +0000407 status = efi_call_early(handle_protocol, handle,
408 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000409 if (status != EFI_SUCCESS)
410 continue;
411
Matt Fleming204b0a12014-03-22 10:09:01 +0000412 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000413
Matt Fleming54b52d82014-01-10 15:27:14 +0000414 status = efi_early->call((unsigned long)uga->get_mode, uga,
415 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000416 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000417 *width = w;
418 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000419
420 /*
421 * Once we've found a UGA supporting PCIIO,
422 * don't bother looking any further.
423 */
424 if (pciio)
425 break;
426
427 first_uga = uga;
428 }
429 }
430
Matt Flemingc116e8d2014-01-16 11:35:43 +0000431 return status;
432}
433
434static efi_status_t
435setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
436{
437 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
438 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
439 unsigned long nr_ugas;
Ingo Molnared7158b2018-02-22 10:54:55 +0100440 u64 *handles = (u64 *)uga_handle;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100441 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000442 int i;
443
444 first_uga = NULL;
445 nr_ugas = size / sizeof(u64);
446 for (i = 0; i < nr_ugas; i++) {
447 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
448 u32 w, h, depth, refresh;
449 void *pciio;
450 u64 handle = handles[i];
451
Matt Fleming204b0a12014-03-22 10:09:01 +0000452 status = efi_call_early(handle_protocol, handle,
453 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000454 if (status != EFI_SUCCESS)
455 continue;
456
Matt Fleming204b0a12014-03-22 10:09:01 +0000457 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000458
459 status = efi_early->call((unsigned long)uga->get_mode, uga,
460 &w, &h, &depth, &refresh);
461 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
462 *width = w;
463 *height = h;
464
465 /*
466 * Once we've found a UGA supporting PCIIO,
467 * don't bother looking any further.
468 */
469 if (pciio)
470 break;
471
472 first_uga = uga;
473 }
474 }
475
476 return status;
477}
478
479/*
480 * See if we have Universal Graphics Adapter (UGA) protocol
481 */
482static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
483 unsigned long size)
484{
485 efi_status_t status;
486 u32 width, height;
487 void **uga_handle = NULL;
488
Matt Fleming204b0a12014-03-22 10:09:01 +0000489 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
490 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000491 if (status != EFI_SUCCESS)
492 return status;
493
Matt Fleming204b0a12014-03-22 10:09:01 +0000494 status = efi_call_early(locate_handle,
495 EFI_LOCATE_BY_PROTOCOL,
496 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000497 if (status != EFI_SUCCESS)
498 goto free_handle;
499
500 height = 0;
501 width = 0;
502
503 if (efi_early->is64)
504 status = setup_uga64(uga_handle, size, &width, &height);
505 else
506 status = setup_uga32(uga_handle, size, &width, &height);
507
508 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000509 goto free_handle;
510
511 /* EFI framebuffer */
512 si->orig_video_isVGA = VIDEO_TYPE_EFI;
513
514 si->lfb_depth = 32;
515 si->lfb_width = width;
516 si->lfb_height = height;
517
518 si->red_size = 8;
519 si->red_pos = 16;
520 si->green_size = 8;
521 si->green_pos = 8;
522 si->blue_size = 8;
523 si->blue_pos = 0;
524 si->rsvd_size = 8;
525 si->rsvd_pos = 24;
526
Matt Fleming291f3632011-12-12 21:27:52 +0000527free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000528 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000529 return status;
530}
531
532void setup_graphics(struct boot_params *boot_params)
533{
534 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
535 struct screen_info *si;
536 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
537 efi_status_t status;
538 unsigned long size;
539 void **gop_handle = NULL;
540 void **uga_handle = NULL;
541
542 si = &boot_params->screen_info;
543 memset(si, 0, sizeof(*si));
544
545 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000546 status = efi_call_early(locate_handle,
547 EFI_LOCATE_BY_PROTOCOL,
548 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000549 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100550 status = efi_setup_gop(NULL, si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000551
552 if (status != EFI_SUCCESS) {
553 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000554 status = efi_call_early(locate_handle,
555 EFI_LOCATE_BY_PROTOCOL,
556 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000557 if (status == EFI_BUFFER_TOO_SMALL)
558 setup_uga(si, &uga_proto, size);
559 }
560}
561
Matt Fleming291f3632011-12-12 21:27:52 +0000562/*
563 * Because the x86 boot code expects to be passed a boot_params we
564 * need to create one ourselves (usually the bootloader would create
565 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +0100566 *
567 * The caller is responsible for filling out ->code32_start in the
568 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +0000569 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000570struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +0000571{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100572 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100573 struct apm_bios_info *bi;
574 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100575 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +0000576 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100577 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000578 int options_size = 0;
579 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700580 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +0000581 u16 *s2;
582 u8 *s1;
583 int i;
Roy Franz46f45822013-09-22 15:45:39 -0700584 unsigned long ramdisk_addr;
585 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000586
Matt Fleming54b52d82014-01-10 15:27:14 +0000587 efi_early = c;
588 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
589 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100590
591 /* Check if we were booted by the EFI firmware */
592 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
593 return NULL;
594
Matt Fleming54b52d82014-01-10 15:27:14 +0000595 if (efi_early->is64)
596 setup_boot_services64(efi_early);
597 else
598 setup_boot_services32(efi_early);
599
Matt Fleming204b0a12014-03-22 10:09:01 +0000600 status = efi_call_early(handle_protocol, handle,
601 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100602 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700603 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100604 return NULL;
605 }
606
Roy Franz40e45302013-09-22 15:45:29 -0700607 status = efi_low_alloc(sys_table, 0x4000, 1,
608 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100609 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700610 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100611 return NULL;
612 }
613
614 memset(boot_params, 0x0, 0x4000);
615
616 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100617 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100618
619 /* Copy the second sector to boot_params */
620 memcpy(&hdr->jump, image->image_base + 512, 512);
621
622 /*
623 * Fill out some of the header fields ourselves because the
624 * EFI firmware loader doesn't load the first sector.
625 */
626 hdr->root_flags = 1;
627 hdr->vid_mode = 0xffff;
628 hdr->boot_flag = 0xAA55;
629
Matt Fleming291f3632011-12-12 21:27:52 +0000630 hdr->type_of_loader = 0x21;
631
632 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500633 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700634 if (!cmdline_ptr)
635 goto fail;
636 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700637 /* Fill in upper bits of command line address, NOP on 32 bit */
638 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000639
640 hdr->ramdisk_image = 0;
641 hdr->ramdisk_size = 0;
642
Matt Fleming291f3632011-12-12 21:27:52 +0000643 /* Clear APM BIOS info */
644 memset(bi, 0, sizeof(*bi));
645
Matt Fleming5a17dae2014-08-05 11:52:11 +0100646 status = efi_parse_options(cmdline_ptr);
647 if (status != EFI_SUCCESS)
648 goto fail2;
649
Roy Franz46f45822013-09-22 15:45:39 -0700650 status = handle_cmdline_files(sys_table, image,
651 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700652 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700653 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700654
655 if (status != EFI_SUCCESS &&
656 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
657 efi_printk(sys_table, "Trying to load files to higher address\n");
658 status = handle_cmdline_files(sys_table, image,
659 (char *)(unsigned long)hdr->cmd_line_ptr,
660 "initrd=", -1UL,
661 &ramdisk_addr, &ramdisk_size);
662 }
663
Matt Fleming9ca8f722012-07-19 10:23:48 +0100664 if (status != EFI_SUCCESS)
665 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700666 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
667 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
668 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
669 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100670
671 return boot_params;
672fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -0700673 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100674fail:
Roy Franz40e45302013-09-22 15:45:29 -0700675 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100676 return NULL;
677}
678
Linn Crosettod2078d52013-09-22 19:59:08 -0600679static void add_e820ext(struct boot_params *params,
680 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100681{
Linn Crosettod2078d52013-09-22 19:59:08 -0600682 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100683 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600684 unsigned long size;
685
686 e820ext->type = SETUP_E820_EXT;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100687 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600688 e820ext->next = 0;
689
690 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
691
692 while (data && data->next)
693 data = (struct setup_data *)(unsigned long)data->next;
694
695 if (data)
696 data->next = (unsigned long)e820ext;
697 else
698 params->hdr.setup_data = (unsigned long)e820ext;
699}
700
701static efi_status_t setup_e820(struct boot_params *params,
702 struct setup_data *e820ext, u32 e820ext_size)
703{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100704 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600705 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100706 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600707 u32 nr_entries;
708 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100709 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000710
Matt Fleming291f3632011-12-12 21:27:52 +0000711 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600712 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
713
714 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000715 efi_memory_desc_t *d;
716 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600717 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000718
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400719#ifdef CONFIG_X86_64
720 m |= (u64)efi->efi_memmap_hi << 32;
721#endif
722
Baoquan He02e43c22017-08-16 21:46:51 +0800723 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000724 switch (d->type) {
725 case EFI_RESERVED_TYPE:
726 case EFI_RUNTIME_SERVICES_CODE:
727 case EFI_RUNTIME_SERVICES_DATA:
728 case EFI_MEMORY_MAPPED_IO:
729 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
730 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100731 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000732 break;
733
734 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100735 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000736 break;
737
738 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100739 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000740 break;
741
742 case EFI_LOADER_CODE:
743 case EFI_LOADER_DATA:
744 case EFI_BOOT_SERVICES_CODE:
745 case EFI_BOOT_SERVICES_DATA:
746 case EFI_CONVENTIONAL_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100747 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000748 break;
749
750 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100751 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000752 break;
753
Dan Williamsad5fb872015-04-03 12:05:28 -0400754 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100755 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400756 break;
757
Matt Fleming291f3632011-12-12 21:27:52 +0000758 default:
759 continue;
760 }
761
762 /* Merge adjacent mappings */
763 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600764 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000765 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600766 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000767 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600768
Ingo Molnar61a50102017-01-27 13:54:38 +0100769 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100770 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600771 sizeof(struct setup_data);
772
773 if (!e820ext || e820ext_size < need)
774 return EFI_BUFFER_TOO_SMALL;
775
776 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100777 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600778 }
779
Ingo Molnar7410aa12017-01-29 12:56:13 +0100780 entry->addr = d->phys_addr;
781 entry->size = d->num_pages << PAGE_SHIFT;
782 entry->type = e820_type;
783 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600784 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000785 }
786
Ingo Molnar61a50102017-01-27 13:54:38 +0100787 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
788 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600789
790 add_e820ext(params, e820ext, nr_e820ext);
791 nr_entries -= nr_e820ext;
792 }
793
794 params->e820_entries = (u8)nr_entries;
795
796 return EFI_SUCCESS;
797}
798
799static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
800 u32 *e820ext_size)
801{
802 efi_status_t status;
803 unsigned long size;
804
805 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100806 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600807
808 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000809 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600810 *e820ext = NULL;
811 *e820ext_size = 0;
812 }
813
Matt Fleming204b0a12014-03-22 10:09:01 +0000814 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
815 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600816 if (status == EFI_SUCCESS)
817 *e820ext_size = size;
818
819 return status;
820}
821
Jeffrey Hugod6493402016-08-29 14:38:54 -0600822struct exit_boot_struct {
823 struct boot_params *boot_params;
824 struct efi_info *efi;
825 struct setup_data *e820ext;
826 __u32 e820ext_size;
827 bool is64;
828};
829
830static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
831 struct efi_boot_memmap *map,
832 void *priv)
833{
834 static bool first = true;
835 const char *signature;
836 __u32 nr_desc;
837 efi_status_t status;
838 struct exit_boot_struct *p = priv;
839
840 if (first) {
841 nr_desc = *map->buff_size / *map->desc_size;
Ingo Molnar61a50102017-01-27 13:54:38 +0100842 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
Jeffrey Hugod6493402016-08-29 14:38:54 -0600843 u32 nr_e820ext = nr_desc -
Ingo Molnar61a50102017-01-27 13:54:38 +0100844 ARRAY_SIZE(p->boot_params->e820_table);
Jeffrey Hugod6493402016-08-29 14:38:54 -0600845
846 status = alloc_e820ext(nr_e820ext, &p->e820ext,
847 &p->e820ext_size);
848 if (status != EFI_SUCCESS)
849 return status;
850 }
851 first = false;
852 }
853
854 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
855 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
856
857 p->efi->efi_systab = (unsigned long)sys_table_arg;
858 p->efi->efi_memdesc_size = *map->desc_size;
859 p->efi->efi_memdesc_version = *map->desc_ver;
860 p->efi->efi_memmap = (unsigned long)*map->map;
861 p->efi->efi_memmap_size = *map->map_size;
862
863#ifdef CONFIG_X86_64
864 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
865 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
866#endif
867
868 return EFI_SUCCESS;
869}
870
Linn Crosettod2078d52013-09-22 19:59:08 -0600871static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +0000872 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -0600873{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600874 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600875 efi_memory_desc_t *mem_map;
876 struct setup_data *e820ext;
877 __u32 e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600878 efi_status_t status;
879 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600880 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600881 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600882
Jeffrey Hugod6493402016-08-29 14:38:54 -0600883 map.map = &mem_map;
884 map.map_size = &map_sz;
885 map.desc_size = &desc_size;
886 map.desc_ver = &desc_version;
887 map.key_ptr = &key;
888 map.buff_size = &buff_size;
889 priv.boot_params = boot_params;
890 priv.efi = &boot_params->efi_info;
891 priv.e820ext = NULL;
892 priv.e820ext_size = 0;
893 priv.is64 = is64;
Linn Crosettod2078d52013-09-22 19:59:08 -0600894
Jeffrey Hugod6493402016-08-29 14:38:54 -0600895 /* Might as well exit boot services now */
896 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
897 exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600898 if (status != EFI_SUCCESS)
899 return status;
900
Jeffrey Hugod6493402016-08-29 14:38:54 -0600901 e820ext = priv.e820ext;
902 e820ext_size = priv.e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600903 /* Historic? */
904 boot_params->alt_mem_k = 32 * 1024;
905
906 status = setup_e820(boot_params, e820ext, e820ext_size);
907 if (status != EFI_SUCCESS)
908 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000909
910 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000911}
912
Matt Fleming9ca8f722012-07-19 10:23:48 +0100913/*
914 * On success we return a pointer to a boot_params structure, and NULL
915 * on failure.
916 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000917struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +0100918 struct boot_params *boot_params)
919{
Matt Fleming54b52d82014-01-10 15:27:14 +0000920 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100921 efi_loaded_image_t *image;
922 struct setup_header *hdr = &boot_params->hdr;
923 efi_status_t status;
924 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +0000925 void *handle;
926 efi_system_table_t *_table;
927 bool is64;
928
929 efi_early = c;
930
931 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
932 handle = (void *)(unsigned long)efi_early->image_handle;
933 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100934
935 sys_table = _table;
936
937 /* Check if we were booted by the EFI firmware */
938 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
939 goto fail;
940
Matt Fleming54b52d82014-01-10 15:27:14 +0000941 if (is64)
942 setup_boot_services64(efi_early);
943 else
944 setup_boot_services32(efi_early);
945
David Howellsde8cb452017-02-06 11:22:43 +0000946 /*
947 * If the boot loader gave us a value for secure_boot then we use that,
948 * otherwise we ask the BIOS.
949 */
950 if (boot_params->secure_boot == efi_secureboot_mode_unset)
951 boot_params->secure_boot = efi_get_secureboot(sys_table);
952
Matthew Garrettccc829b2017-08-25 16:50:15 +0100953 /* Ask the firmware to clear memory on unclean shutdown */
954 efi_enable_reset_attack_mitigation(sys_table);
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200955 efi_retrieve_tpm2_eventlog(sys_table);
Matthew Garrettccc829b2017-08-25 16:50:15 +0100956
Matt Fleming9ca8f722012-07-19 10:23:48 +0100957 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +0000958
Matt Fleming56394ab2014-09-11 09:04:25 +0100959 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700960
Lukas Wunner58c54752016-11-12 21:32:36 +0000961 setup_quirks(boot_params);
962
Matt Fleming204b0a12014-03-22 10:09:01 +0000963 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
964 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000965 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700966 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000967 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000968 }
Matt Fleming291f3632011-12-12 21:27:52 +0000969
970 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -0700971 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -0700972 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000973 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700974 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000975 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000976 }
Matt Fleming291f3632011-12-12 21:27:52 +0000977
Matt Fleming9ca8f722012-07-19 10:23:48 +0100978 /*
979 * If the kernel isn't already loaded at the preferred load
980 * address, relocate it.
981 */
982 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -0700983 unsigned long bzimage_addr = hdr->code32_start;
984 status = efi_relocate_kernel(sys_table, &bzimage_addr,
985 hdr->init_size, hdr->init_size,
986 hdr->pref_address,
987 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200988 if (status != EFI_SUCCESS) {
989 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100990 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200991 }
Roy Franz4a9f3a72013-09-22 15:45:32 -0700992
993 hdr->pref_address = hdr->code32_start;
994 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100995 }
996
Matt Flemingb8ff87a2014-01-10 15:54:31 +0000997 status = exit_boot(boot_params, handle, is64);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +0200998 if (status != EFI_SUCCESS) {
999 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001000 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001001 }
Matt Fleming291f3632011-12-12 21:27:52 +00001002
1003 memset((char *)gdt->address, 0x0, gdt->size);
1004 desc = (struct desc_struct *)gdt->address;
1005
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +03001006 /* The first GDT is a dummy. */
1007 desc++;
1008
1009 if (IS_ENABLED(CONFIG_X86_64)) {
1010 /* __KERNEL32_CS */
1011 desc->limit0 = 0xffff;
1012 desc->base0 = 0x0000;
1013 desc->base1 = 0x0000;
1014 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1015 desc->s = DESC_TYPE_CODE_DATA;
1016 desc->dpl = 0;
1017 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001018 desc->limit1 = 0xf;
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +03001019 desc->avl = 0;
1020 desc->l = 0;
1021 desc->d = SEG_OP_SIZE_32BIT;
1022 desc->g = SEG_GRANULARITY_4KB;
1023 desc->base2 = 0x00;
1024 desc++;
1025 } else {
1026 /* Second entry is unused on 32-bit */
1027 desc++;
1028 }
Matt Fleming291f3632011-12-12 21:27:52 +00001029
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001030 /* __KERNEL_CS */
Matt Fleming291f3632011-12-12 21:27:52 +00001031 desc->limit0 = 0xffff;
1032 desc->base0 = 0x0000;
1033 desc->base1 = 0x0000;
1034 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1035 desc->s = DESC_TYPE_CODE_DATA;
1036 desc->dpl = 0;
1037 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001038 desc->limit1 = 0xf;
Matt Fleming291f3632011-12-12 21:27:52 +00001039 desc->avl = 0;
Kirill A. Shutemov4c941172017-06-06 14:31:23 +03001040 if (IS_ENABLED(CONFIG_X86_64)) {
1041 desc->l = 1;
1042 desc->d = 0;
1043 } else {
1044 desc->l = 0;
1045 desc->d = SEG_OP_SIZE_32BIT;
1046 }
Matt Fleming291f3632011-12-12 21:27:52 +00001047 desc->g = SEG_GRANULARITY_4KB;
1048 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +00001049 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001050
1051 /* __KERNEL_DS */
Matt Fleming291f3632011-12-12 21:27:52 +00001052 desc->limit0 = 0xffff;
1053 desc->base0 = 0x0000;
1054 desc->base1 = 0x0000;
1055 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1056 desc->s = DESC_TYPE_CODE_DATA;
1057 desc->dpl = 0;
1058 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001059 desc->limit1 = 0xf;
Matt Fleming291f3632011-12-12 21:27:52 +00001060 desc->avl = 0;
1061 desc->l = 0;
1062 desc->d = SEG_OP_SIZE_32BIT;
1063 desc->g = SEG_GRANULARITY_4KB;
1064 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +00001065 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001066
1067 if (IS_ENABLED(CONFIG_X86_64)) {
1068 /* Task segment value */
1069 desc->limit0 = 0x0000;
1070 desc->base0 = 0x0000;
1071 desc->base1 = 0x0000;
1072 desc->type = SEG_TYPE_TSS;
1073 desc->s = 0;
1074 desc->dpl = 0;
1075 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001076 desc->limit1 = 0x0;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001077 desc->avl = 0;
1078 desc->l = 0;
1079 desc->d = 0;
1080 desc->g = SEG_GRANULARITY_4KB;
1081 desc->base2 = 0x00;
1082 desc++;
1083 }
Matt Fleming291f3632011-12-12 21:27:52 +00001084
Matt Fleming291f3632011-12-12 21:27:52 +00001085 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001086 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001087
1088 return boot_params;
1089fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001090 efi_printk(sys_table, "efi_main() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001091 return NULL;
1092}