blob: 47d3efff6805b9787d4be1721cd8cb790818e10f [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
112__setup_efi_pci32(efi_pci_io_protocol_32 *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;
117 uint64_t attributes;
118
119 status = efi_early->call(pci->attributes, pci,
120 EfiPciIoAttributeOperationGet, 0, 0,
121 &attributes);
122 if (status != EFI_SUCCESS)
123 return status;
124
125 if (!pci->romimage || !pci->romsize)
126 return EFI_INVALID_PARAMETER;
127
128 size = pci->romsize + sizeof(*rom);
129
Matt Fleming204b0a12014-03-22 10:09:01 +0000130 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200131 if (status != EFI_SUCCESS) {
132 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000133 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200134 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000135
136 memset(rom, 0, sizeof(*rom));
137
138 rom->data.type = SETUP_PCI;
139 rom->data.len = size - sizeof(struct setup_data);
140 rom->data.next = 0;
141 rom->pcilen = pci->romsize;
142 *__rom = rom;
143
144 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
145 PCI_VENDOR_ID, 1, &(rom->vendor));
146
Andre Müller77e21e82014-09-10 01:00:22 +0200147 if (status != EFI_SUCCESS) {
148 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000149 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200150 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000151
152 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
153 PCI_DEVICE_ID, 1, &(rom->devid));
154
Andre Müller77e21e82014-09-10 01:00:22 +0200155 if (status != EFI_SUCCESS) {
156 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000157 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200158 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000159
160 status = efi_early->call(pci->get_location, pci, &(rom->segment),
161 &(rom->bus), &(rom->device), &(rom->function));
162
163 if (status != EFI_SUCCESS)
164 goto free_struct;
165
166 memcpy(rom->romdata, pci->romimage, pci->romsize);
167 return status;
168
169free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000170 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000171 return status;
172}
173
Matt Fleming56394ab2014-09-11 09:04:25 +0100174static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000175setup_efi_pci32(struct boot_params *params, void **pci_handle,
176 unsigned long size)
177{
178 efi_pci_io_protocol_32 *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700179 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000180 u32 *handles = (u32 *)(unsigned long)pci_handle;
181 efi_status_t status;
182 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700183 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000184 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700185
Jan Beulichbc754792013-01-18 12:35:14 +0000186 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700187
188 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000189 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700190
Matt Flemingc116e8d2014-01-16 11:35:43 +0000191 nr_pci = size / sizeof(u32);
192 for (i = 0; i < nr_pci; i++) {
193 struct pci_setup_rom *rom = NULL;
194 u32 h = handles[i];
195
Matt Fleming204b0a12014-03-22 10:09:01 +0000196 status = efi_call_early(handle_protocol, h,
197 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000198
199 if (status != EFI_SUCCESS)
200 continue;
201
202 if (!pci)
203 continue;
204
205 status = __setup_efi_pci32(pci, &rom);
206 if (status != EFI_SUCCESS)
207 continue;
208
209 if (data)
210 data->next = (unsigned long)rom;
211 else
212 params->hdr.setup_data = (unsigned long)rom;
213
214 data = (struct setup_data *)rom;
215
216 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000217}
218
219static efi_status_t
220__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
221{
222 struct pci_setup_rom *rom;
223 efi_status_t status;
224 unsigned long size;
225 uint64_t attributes;
226
227 status = efi_early->call(pci->attributes, pci,
228 EfiPciIoAttributeOperationGet, 0,
229 &attributes);
230 if (status != EFI_SUCCESS)
231 return status;
232
233 if (!pci->romimage || !pci->romsize)
234 return EFI_INVALID_PARAMETER;
235
236 size = pci->romsize + sizeof(*rom);
237
Matt Fleming204b0a12014-03-22 10:09:01 +0000238 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200239 if (status != EFI_SUCCESS) {
240 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000241 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200242 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000243
244 rom->data.type = SETUP_PCI;
245 rom->data.len = size - sizeof(struct setup_data);
246 rom->data.next = 0;
247 rom->pcilen = pci->romsize;
248 *__rom = rom;
249
250 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
251 PCI_VENDOR_ID, 1, &(rom->vendor));
252
Andre Müller77e21e82014-09-10 01:00:22 +0200253 if (status != EFI_SUCCESS) {
254 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000255 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200256 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000257
258 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
259 PCI_DEVICE_ID, 1, &(rom->devid));
260
Andre Müller77e21e82014-09-10 01:00:22 +0200261 if (status != EFI_SUCCESS) {
262 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000263 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200264 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000265
266 status = efi_early->call(pci->get_location, pci, &(rom->segment),
267 &(rom->bus), &(rom->device), &(rom->function));
268
269 if (status != EFI_SUCCESS)
270 goto free_struct;
271
272 memcpy(rom->romdata, pci->romimage, pci->romsize);
273 return status;
274
275free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000276 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000277 return status;
278
279}
280
Matt Fleming56394ab2014-09-11 09:04:25 +0100281static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000282setup_efi_pci64(struct boot_params *params, void **pci_handle,
283 unsigned long size)
284{
285 efi_pci_io_protocol_64 *pci = NULL;
286 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
287 u64 *handles = (u64 *)(unsigned long)pci_handle;
288 efi_status_t status;
289 unsigned long nr_pci;
290 struct setup_data *data;
291 int i;
292
293 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
294
295 while (data && data->next)
296 data = (struct setup_data *)(unsigned long)data->next;
297
298 nr_pci = size / sizeof(u64);
299 for (i = 0; i < nr_pci; i++) {
300 struct pci_setup_rom *rom = NULL;
301 u64 h = handles[i];
302
Matt Fleming204b0a12014-03-22 10:09:01 +0000303 status = efi_call_early(handle_protocol, h,
304 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000305
306 if (status != EFI_SUCCESS)
307 continue;
308
309 if (!pci)
310 continue;
311
312 status = __setup_efi_pci64(pci, &rom);
313 if (status != EFI_SUCCESS)
314 continue;
315
316 if (data)
317 data->next = (unsigned long)rom;
318 else
319 params->hdr.setup_data = (unsigned long)rom;
320
321 data = (struct setup_data *)rom;
322
323 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000324}
325
Matt Fleming56394ab2014-09-11 09:04:25 +0100326/*
327 * There's no way to return an informative status from this function,
328 * because any analysis (and printing of error messages) needs to be
329 * done directly at the EFI function call-site.
330 *
331 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
332 * just didn't find any PCI devices, but there's no way to tell outside
333 * the context of the call.
334 */
335static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000336{
337 efi_status_t status;
338 void **pci_handle = NULL;
339 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
340 unsigned long size = 0;
341
Matt Fleming204b0a12014-03-22 10:09:01 +0000342 status = efi_call_early(locate_handle,
343 EFI_LOCATE_BY_PROTOCOL,
344 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700345
346 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000347 status = efi_call_early(allocate_pool,
348 EFI_LOADER_DATA,
349 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700350
Andre Müller77e21e82014-09-10 01:00:22 +0200351 if (status != EFI_SUCCESS) {
352 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100353 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200354 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700355
Matt Fleming204b0a12014-03-22 10:09:01 +0000356 status = efi_call_early(locate_handle,
357 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
358 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700359 }
360
361 if (status != EFI_SUCCESS)
362 goto free_handle;
363
Matt Flemingc116e8d2014-01-16 11:35:43 +0000364 if (efi_early->is64)
Matt Fleming56394ab2014-09-11 09:04:25 +0100365 setup_efi_pci64(params, pci_handle, size);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000366 else
Matt Fleming56394ab2014-09-11 09:04:25 +0100367 setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700368
369free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000370 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700371}
372
Lukas Wunner58c54752016-11-12 21:32:36 +0000373static void retrieve_apple_device_properties(struct boot_params *boot_params)
374{
375 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
376 struct setup_data *data, *new;
377 efi_status_t status;
378 u32 size = 0;
379 void *p;
380
381 status = efi_call_early(locate_protocol, &guid, NULL, &p);
382 if (status != EFI_SUCCESS)
383 return;
384
385 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
386 efi_printk(sys_table, "Unsupported properties proto version\n");
387 return;
388 }
389
390 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
391 if (!size)
392 return;
393
394 do {
395 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
396 size + sizeof(struct setup_data), &new);
397 if (status != EFI_SUCCESS) {
398 efi_printk(sys_table,
399 "Failed to alloc mem for properties\n");
400 return;
401 }
402
403 status = efi_call_proto(apple_properties_protocol, get_all, p,
404 new->data, &size);
405
406 if (status == EFI_BUFFER_TOO_SMALL)
407 efi_call_early(free_pool, new);
408 } while (status == EFI_BUFFER_TOO_SMALL);
409
410 new->type = SETUP_APPLE_PROPERTIES;
411 new->len = size;
412 new->next = 0;
413
414 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
415 if (!data)
416 boot_params->hdr.setup_data = (unsigned long)new;
417 else {
418 while (data->next)
419 data = (struct setup_data *)(unsigned long)data->next;
420 data->next = (unsigned long)new;
421 }
422}
423
Ard Biesheuvel36b64972018-03-12 08:45:00 +0000424static const efi_char16_t apple[] = L"Apple";
425
Lukas Wunner58c54752016-11-12 21:32:36 +0000426static void setup_quirks(struct boot_params *boot_params)
427{
Lukas Wunner58c54752016-11-12 21:32:36 +0000428 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
429 efi_table_attr(efi_system_table, fw_vendor, sys_table);
430
431 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
432 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
433 retrieve_apple_device_properties(boot_params);
434 }
435}
436
Matt Flemingc116e8d2014-01-16 11:35:43 +0000437static efi_status_t
438setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000439{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000440 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
441 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000442 unsigned long nr_ugas;
Ingo Molnared7158b2018-02-22 10:54:55 +0100443 u32 *handles = (u32 *)uga_handle;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100444 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Fleming291f3632011-12-12 21:27:52 +0000445 int i;
446
Matt Fleming291f3632011-12-12 21:27:52 +0000447 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000448 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000449 for (i = 0; i < nr_ugas; i++) {
450 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000451 u32 w, h, depth, refresh;
452 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000453 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000454
Matt Fleming204b0a12014-03-22 10:09:01 +0000455 status = efi_call_early(handle_protocol, handle,
456 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000457 if (status != EFI_SUCCESS)
458 continue;
459
Matt Fleming204b0a12014-03-22 10:09:01 +0000460 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000461
Matt Fleming54b52d82014-01-10 15:27:14 +0000462 status = efi_early->call((unsigned long)uga->get_mode, uga,
463 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000464 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000465 *width = w;
466 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000467
468 /*
469 * Once we've found a UGA supporting PCIIO,
470 * don't bother looking any further.
471 */
472 if (pciio)
473 break;
474
475 first_uga = uga;
476 }
477 }
478
Matt Flemingc116e8d2014-01-16 11:35:43 +0000479 return status;
480}
481
482static efi_status_t
483setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
484{
485 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
486 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
487 unsigned long nr_ugas;
Ingo Molnared7158b2018-02-22 10:54:55 +0100488 u64 *handles = (u64 *)uga_handle;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100489 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000490 int i;
491
492 first_uga = NULL;
493 nr_ugas = size / sizeof(u64);
494 for (i = 0; i < nr_ugas; i++) {
495 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
496 u32 w, h, depth, refresh;
497 void *pciio;
498 u64 handle = handles[i];
499
Matt Fleming204b0a12014-03-22 10:09:01 +0000500 status = efi_call_early(handle_protocol, handle,
501 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000502 if (status != EFI_SUCCESS)
503 continue;
504
Matt Fleming204b0a12014-03-22 10:09:01 +0000505 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000506
507 status = efi_early->call((unsigned long)uga->get_mode, uga,
508 &w, &h, &depth, &refresh);
509 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
510 *width = w;
511 *height = h;
512
513 /*
514 * Once we've found a UGA supporting PCIIO,
515 * don't bother looking any further.
516 */
517 if (pciio)
518 break;
519
520 first_uga = uga;
521 }
522 }
523
524 return status;
525}
526
527/*
528 * See if we have Universal Graphics Adapter (UGA) protocol
529 */
530static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
531 unsigned long size)
532{
533 efi_status_t status;
534 u32 width, height;
535 void **uga_handle = NULL;
536
Matt Fleming204b0a12014-03-22 10:09:01 +0000537 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
538 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000539 if (status != EFI_SUCCESS)
540 return status;
541
Matt Fleming204b0a12014-03-22 10:09:01 +0000542 status = efi_call_early(locate_handle,
543 EFI_LOCATE_BY_PROTOCOL,
544 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000545 if (status != EFI_SUCCESS)
546 goto free_handle;
547
548 height = 0;
549 width = 0;
550
551 if (efi_early->is64)
552 status = setup_uga64(uga_handle, size, &width, &height);
553 else
554 status = setup_uga32(uga_handle, size, &width, &height);
555
556 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000557 goto free_handle;
558
559 /* EFI framebuffer */
560 si->orig_video_isVGA = VIDEO_TYPE_EFI;
561
562 si->lfb_depth = 32;
563 si->lfb_width = width;
564 si->lfb_height = height;
565
566 si->red_size = 8;
567 si->red_pos = 16;
568 si->green_size = 8;
569 si->green_pos = 8;
570 si->blue_size = 8;
571 si->blue_pos = 0;
572 si->rsvd_size = 8;
573 si->rsvd_pos = 24;
574
Matt Fleming291f3632011-12-12 21:27:52 +0000575free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000576 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000577 return status;
578}
579
580void setup_graphics(struct boot_params *boot_params)
581{
582 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
583 struct screen_info *si;
584 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
585 efi_status_t status;
586 unsigned long size;
587 void **gop_handle = NULL;
588 void **uga_handle = NULL;
589
590 si = &boot_params->screen_info;
591 memset(si, 0, sizeof(*si));
592
593 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000594 status = efi_call_early(locate_handle,
595 EFI_LOCATE_BY_PROTOCOL,
596 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000597 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100598 status = efi_setup_gop(NULL, si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000599
600 if (status != EFI_SUCCESS) {
601 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000602 status = efi_call_early(locate_handle,
603 EFI_LOCATE_BY_PROTOCOL,
604 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000605 if (status == EFI_BUFFER_TOO_SMALL)
606 setup_uga(si, &uga_proto, size);
607 }
608}
609
Matt Fleming291f3632011-12-12 21:27:52 +0000610/*
611 * Because the x86 boot code expects to be passed a boot_params we
612 * need to create one ourselves (usually the bootloader would create
613 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +0100614 *
615 * The caller is responsible for filling out ->code32_start in the
616 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +0000617 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000618struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +0000619{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100620 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100621 struct apm_bios_info *bi;
622 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100623 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +0000624 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100625 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000626 int options_size = 0;
627 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700628 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +0000629 u16 *s2;
630 u8 *s1;
631 int i;
Roy Franz46f45822013-09-22 15:45:39 -0700632 unsigned long ramdisk_addr;
633 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000634
Matt Fleming54b52d82014-01-10 15:27:14 +0000635 efi_early = c;
636 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
637 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100638
639 /* Check if we were booted by the EFI firmware */
640 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
641 return NULL;
642
Matt Fleming54b52d82014-01-10 15:27:14 +0000643 if (efi_early->is64)
644 setup_boot_services64(efi_early);
645 else
646 setup_boot_services32(efi_early);
647
Matt Fleming204b0a12014-03-22 10:09:01 +0000648 status = efi_call_early(handle_protocol, handle,
649 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100650 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700651 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100652 return NULL;
653 }
654
Roy Franz40e45302013-09-22 15:45:29 -0700655 status = efi_low_alloc(sys_table, 0x4000, 1,
656 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100657 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700658 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100659 return NULL;
660 }
661
662 memset(boot_params, 0x0, 0x4000);
663
664 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100665 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100666
667 /* Copy the second sector to boot_params */
668 memcpy(&hdr->jump, image->image_base + 512, 512);
669
670 /*
671 * Fill out some of the header fields ourselves because the
672 * EFI firmware loader doesn't load the first sector.
673 */
674 hdr->root_flags = 1;
675 hdr->vid_mode = 0xffff;
676 hdr->boot_flag = 0xAA55;
677
Matt Fleming291f3632011-12-12 21:27:52 +0000678 hdr->type_of_loader = 0x21;
679
680 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500681 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700682 if (!cmdline_ptr)
683 goto fail;
684 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700685 /* Fill in upper bits of command line address, NOP on 32 bit */
686 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000687
688 hdr->ramdisk_image = 0;
689 hdr->ramdisk_size = 0;
690
Matt Fleming291f3632011-12-12 21:27:52 +0000691 /* Clear APM BIOS info */
692 memset(bi, 0, sizeof(*bi));
693
Matt Fleming5a17dae2014-08-05 11:52:11 +0100694 status = efi_parse_options(cmdline_ptr);
695 if (status != EFI_SUCCESS)
696 goto fail2;
697
Roy Franz46f45822013-09-22 15:45:39 -0700698 status = handle_cmdline_files(sys_table, image,
699 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700700 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700701 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700702
703 if (status != EFI_SUCCESS &&
704 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
705 efi_printk(sys_table, "Trying to load files to higher address\n");
706 status = handle_cmdline_files(sys_table, image,
707 (char *)(unsigned long)hdr->cmd_line_ptr,
708 "initrd=", -1UL,
709 &ramdisk_addr, &ramdisk_size);
710 }
711
Matt Fleming9ca8f722012-07-19 10:23:48 +0100712 if (status != EFI_SUCCESS)
713 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700714 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
715 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
716 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
717 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100718
719 return boot_params;
720fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -0700721 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100722fail:
Roy Franz40e45302013-09-22 15:45:29 -0700723 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100724 return NULL;
725}
726
Linn Crosettod2078d52013-09-22 19:59:08 -0600727static void add_e820ext(struct boot_params *params,
728 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100729{
Linn Crosettod2078d52013-09-22 19:59:08 -0600730 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100731 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600732 unsigned long size;
733
734 e820ext->type = SETUP_E820_EXT;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100735 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600736 e820ext->next = 0;
737
738 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
739
740 while (data && data->next)
741 data = (struct setup_data *)(unsigned long)data->next;
742
743 if (data)
744 data->next = (unsigned long)e820ext;
745 else
746 params->hdr.setup_data = (unsigned long)e820ext;
747}
748
749static efi_status_t setup_e820(struct boot_params *params,
750 struct setup_data *e820ext, u32 e820ext_size)
751{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100752 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600753 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100754 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600755 u32 nr_entries;
756 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100757 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000758
Matt Fleming291f3632011-12-12 21:27:52 +0000759 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600760 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
761
762 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000763 efi_memory_desc_t *d;
764 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600765 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000766
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400767#ifdef CONFIG_X86_64
768 m |= (u64)efi->efi_memmap_hi << 32;
769#endif
770
Baoquan He02e43c22017-08-16 21:46:51 +0800771 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
Matt Fleming291f3632011-12-12 21:27:52 +0000772 switch (d->type) {
773 case EFI_RESERVED_TYPE:
774 case EFI_RUNTIME_SERVICES_CODE:
775 case EFI_RUNTIME_SERVICES_DATA:
776 case EFI_MEMORY_MAPPED_IO:
777 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
778 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100779 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000780 break;
781
782 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100783 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000784 break;
785
786 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100787 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000788 break;
789
790 case EFI_LOADER_CODE:
791 case EFI_LOADER_DATA:
792 case EFI_BOOT_SERVICES_CODE:
793 case EFI_BOOT_SERVICES_DATA:
794 case EFI_CONVENTIONAL_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100795 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000796 break;
797
798 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100799 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000800 break;
801
Dan Williamsad5fb872015-04-03 12:05:28 -0400802 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100803 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400804 break;
805
Matt Fleming291f3632011-12-12 21:27:52 +0000806 default:
807 continue;
808 }
809
810 /* Merge adjacent mappings */
811 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600812 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000813 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600814 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000815 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600816
Ingo Molnar61a50102017-01-27 13:54:38 +0100817 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100818 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600819 sizeof(struct setup_data);
820
821 if (!e820ext || e820ext_size < need)
822 return EFI_BUFFER_TOO_SMALL;
823
824 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100825 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600826 }
827
Ingo Molnar7410aa12017-01-29 12:56:13 +0100828 entry->addr = d->phys_addr;
829 entry->size = d->num_pages << PAGE_SHIFT;
830 entry->type = e820_type;
831 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600832 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000833 }
834
Ingo Molnar61a50102017-01-27 13:54:38 +0100835 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
836 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600837
838 add_e820ext(params, e820ext, nr_e820ext);
839 nr_entries -= nr_e820ext;
840 }
841
842 params->e820_entries = (u8)nr_entries;
843
844 return EFI_SUCCESS;
845}
846
847static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
848 u32 *e820ext_size)
849{
850 efi_status_t status;
851 unsigned long size;
852
853 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100854 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600855
856 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000857 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600858 *e820ext = NULL;
859 *e820ext_size = 0;
860 }
861
Matt Fleming204b0a12014-03-22 10:09:01 +0000862 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
863 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600864 if (status == EFI_SUCCESS)
865 *e820ext_size = size;
866
867 return status;
868}
869
Jeffrey Hugod6493402016-08-29 14:38:54 -0600870struct exit_boot_struct {
871 struct boot_params *boot_params;
872 struct efi_info *efi;
873 struct setup_data *e820ext;
874 __u32 e820ext_size;
875 bool is64;
876};
877
878static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
879 struct efi_boot_memmap *map,
880 void *priv)
881{
882 static bool first = true;
883 const char *signature;
884 __u32 nr_desc;
885 efi_status_t status;
886 struct exit_boot_struct *p = priv;
887
888 if (first) {
889 nr_desc = *map->buff_size / *map->desc_size;
Ingo Molnar61a50102017-01-27 13:54:38 +0100890 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
Jeffrey Hugod6493402016-08-29 14:38:54 -0600891 u32 nr_e820ext = nr_desc -
Ingo Molnar61a50102017-01-27 13:54:38 +0100892 ARRAY_SIZE(p->boot_params->e820_table);
Jeffrey Hugod6493402016-08-29 14:38:54 -0600893
894 status = alloc_e820ext(nr_e820ext, &p->e820ext,
895 &p->e820ext_size);
896 if (status != EFI_SUCCESS)
897 return status;
898 }
899 first = false;
900 }
901
902 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
903 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
904
905 p->efi->efi_systab = (unsigned long)sys_table_arg;
906 p->efi->efi_memdesc_size = *map->desc_size;
907 p->efi->efi_memdesc_version = *map->desc_ver;
908 p->efi->efi_memmap = (unsigned long)*map->map;
909 p->efi->efi_memmap_size = *map->map_size;
910
911#ifdef CONFIG_X86_64
912 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
913 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
914#endif
915
916 return EFI_SUCCESS;
917}
918
Linn Crosettod2078d52013-09-22 19:59:08 -0600919static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +0000920 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -0600921{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600922 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600923 efi_memory_desc_t *mem_map;
924 struct setup_data *e820ext;
925 __u32 e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600926 efi_status_t status;
927 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600928 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600929 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600930
Jeffrey Hugod6493402016-08-29 14:38:54 -0600931 map.map = &mem_map;
932 map.map_size = &map_sz;
933 map.desc_size = &desc_size;
934 map.desc_ver = &desc_version;
935 map.key_ptr = &key;
936 map.buff_size = &buff_size;
937 priv.boot_params = boot_params;
938 priv.efi = &boot_params->efi_info;
939 priv.e820ext = NULL;
940 priv.e820ext_size = 0;
941 priv.is64 = is64;
Linn Crosettod2078d52013-09-22 19:59:08 -0600942
Jeffrey Hugod6493402016-08-29 14:38:54 -0600943 /* Might as well exit boot services now */
944 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
945 exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600946 if (status != EFI_SUCCESS)
947 return status;
948
Jeffrey Hugod6493402016-08-29 14:38:54 -0600949 e820ext = priv.e820ext;
950 e820ext_size = priv.e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600951 /* Historic? */
952 boot_params->alt_mem_k = 32 * 1024;
953
954 status = setup_e820(boot_params, e820ext, e820ext_size);
955 if (status != EFI_SUCCESS)
956 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000957
958 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000959}
960
Matt Fleming9ca8f722012-07-19 10:23:48 +0100961/*
962 * On success we return a pointer to a boot_params structure, and NULL
963 * on failure.
964 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000965struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +0100966 struct boot_params *boot_params)
967{
Matt Fleming54b52d82014-01-10 15:27:14 +0000968 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100969 efi_loaded_image_t *image;
970 struct setup_header *hdr = &boot_params->hdr;
971 efi_status_t status;
972 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +0000973 void *handle;
974 efi_system_table_t *_table;
975 bool is64;
976
977 efi_early = c;
978
979 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
980 handle = (void *)(unsigned long)efi_early->image_handle;
981 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100982
983 sys_table = _table;
984
985 /* Check if we were booted by the EFI firmware */
986 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
987 goto fail;
988
Matt Fleming54b52d82014-01-10 15:27:14 +0000989 if (is64)
990 setup_boot_services64(efi_early);
991 else
992 setup_boot_services32(efi_early);
993
David Howellsde8cb452017-02-06 11:22:43 +0000994 /*
995 * If the boot loader gave us a value for secure_boot then we use that,
996 * otherwise we ask the BIOS.
997 */
998 if (boot_params->secure_boot == efi_secureboot_mode_unset)
999 boot_params->secure_boot = efi_get_secureboot(sys_table);
1000
Matthew Garrettccc829b2017-08-25 16:50:15 +01001001 /* Ask the firmware to clear memory on unclean shutdown */
1002 efi_enable_reset_attack_mitigation(sys_table);
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +02001003 efi_retrieve_tpm2_eventlog(sys_table);
Matthew Garrettccc829b2017-08-25 16:50:15 +01001004
Matt Fleming9ca8f722012-07-19 10:23:48 +01001005 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001006
Matt Fleming56394ab2014-09-11 09:04:25 +01001007 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001008
Lukas Wunner58c54752016-11-12 21:32:36 +00001009 setup_quirks(boot_params);
1010
Matt Fleming204b0a12014-03-22 10:09:01 +00001011 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1012 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001013 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001014 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001015 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001016 }
Matt Fleming291f3632011-12-12 21:27:52 +00001017
1018 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001019 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001020 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001021 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001022 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001023 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001024 }
Matt Fleming291f3632011-12-12 21:27:52 +00001025
Matt Fleming9ca8f722012-07-19 10:23:48 +01001026 /*
1027 * If the kernel isn't already loaded at the preferred load
1028 * address, relocate it.
1029 */
1030 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001031 unsigned long bzimage_addr = hdr->code32_start;
1032 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1033 hdr->init_size, hdr->init_size,
1034 hdr->pref_address,
1035 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001036 if (status != EFI_SUCCESS) {
1037 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001038 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001039 }
Roy Franz4a9f3a72013-09-22 15:45:32 -07001040
1041 hdr->pref_address = hdr->code32_start;
1042 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001043 }
1044
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001045 status = exit_boot(boot_params, handle, is64);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001046 if (status != EFI_SUCCESS) {
1047 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001048 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001049 }
Matt Fleming291f3632011-12-12 21:27:52 +00001050
1051 memset((char *)gdt->address, 0x0, gdt->size);
1052 desc = (struct desc_struct *)gdt->address;
1053
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +03001054 /* The first GDT is a dummy. */
1055 desc++;
1056
1057 if (IS_ENABLED(CONFIG_X86_64)) {
1058 /* __KERNEL32_CS */
1059 desc->limit0 = 0xffff;
1060 desc->base0 = 0x0000;
1061 desc->base1 = 0x0000;
1062 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1063 desc->s = DESC_TYPE_CODE_DATA;
1064 desc->dpl = 0;
1065 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001066 desc->limit1 = 0xf;
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +03001067 desc->avl = 0;
1068 desc->l = 0;
1069 desc->d = SEG_OP_SIZE_32BIT;
1070 desc->g = SEG_GRANULARITY_4KB;
1071 desc->base2 = 0x00;
1072 desc++;
1073 } else {
1074 /* Second entry is unused on 32-bit */
1075 desc++;
1076 }
Matt Fleming291f3632011-12-12 21:27:52 +00001077
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001078 /* __KERNEL_CS */
Matt Fleming291f3632011-12-12 21:27:52 +00001079 desc->limit0 = 0xffff;
1080 desc->base0 = 0x0000;
1081 desc->base1 = 0x0000;
1082 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1083 desc->s = DESC_TYPE_CODE_DATA;
1084 desc->dpl = 0;
1085 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001086 desc->limit1 = 0xf;
Matt Fleming291f3632011-12-12 21:27:52 +00001087 desc->avl = 0;
Kirill A. Shutemov4c941172017-06-06 14:31:23 +03001088 if (IS_ENABLED(CONFIG_X86_64)) {
1089 desc->l = 1;
1090 desc->d = 0;
1091 } else {
1092 desc->l = 0;
1093 desc->d = SEG_OP_SIZE_32BIT;
1094 }
Matt Fleming291f3632011-12-12 21:27:52 +00001095 desc->g = SEG_GRANULARITY_4KB;
1096 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +00001097 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001098
1099 /* __KERNEL_DS */
Matt Fleming291f3632011-12-12 21:27:52 +00001100 desc->limit0 = 0xffff;
1101 desc->base0 = 0x0000;
1102 desc->base1 = 0x0000;
1103 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1104 desc->s = DESC_TYPE_CODE_DATA;
1105 desc->dpl = 0;
1106 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001107 desc->limit1 = 0xf;
Matt Fleming291f3632011-12-12 21:27:52 +00001108 desc->avl = 0;
1109 desc->l = 0;
1110 desc->d = SEG_OP_SIZE_32BIT;
1111 desc->g = SEG_GRANULARITY_4KB;
1112 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +00001113 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001114
1115 if (IS_ENABLED(CONFIG_X86_64)) {
1116 /* Task segment value */
1117 desc->limit0 = 0x0000;
1118 desc->base0 = 0x0000;
1119 desc->base1 = 0x0000;
1120 desc->type = SEG_TYPE_TSS;
1121 desc->s = 0;
1122 desc->dpl = 0;
1123 desc->p = 1;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02001124 desc->limit1 = 0x0;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001125 desc->avl = 0;
1126 desc->l = 0;
1127 desc->d = 0;
1128 desc->g = SEG_GRANULARITY_4KB;
1129 desc->base2 = 0x00;
1130 desc++;
1131 }
Matt Fleming291f3632011-12-12 21:27:52 +00001132
Matt Fleming291f3632011-12-12 21:27:52 +00001133 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001134 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001135
1136 return boot_params;
1137fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001138 efi_printk(sys_table, "efi_main() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001139 return NULL;
1140}