blob: a1686f3dc295b0aa79084a33bf0adfecf9d57b1b [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
424static void setup_quirks(struct boot_params *boot_params)
425{
426 efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
427 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
428 efi_table_attr(efi_system_table, fw_vendor, sys_table);
429
430 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
431 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
432 retrieve_apple_device_properties(boot_params);
433 }
434}
435
Matt Flemingc116e8d2014-01-16 11:35:43 +0000436static efi_status_t
437setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000438{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000439 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
440 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000441 unsigned long nr_ugas;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000442 u32 *handles = (u32 *)uga_handle;;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100443 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Fleming291f3632011-12-12 21:27:52 +0000444 int i;
445
Matt Fleming291f3632011-12-12 21:27:52 +0000446 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000447 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000448 for (i = 0; i < nr_ugas; i++) {
449 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000450 u32 w, h, depth, refresh;
451 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000452 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000453
Matt Fleming204b0a12014-03-22 10:09:01 +0000454 status = efi_call_early(handle_protocol, handle,
455 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000456 if (status != EFI_SUCCESS)
457 continue;
458
Matt Fleming204b0a12014-03-22 10:09:01 +0000459 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000460
Matt Fleming54b52d82014-01-10 15:27:14 +0000461 status = efi_early->call((unsigned long)uga->get_mode, uga,
462 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000463 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000464 *width = w;
465 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000466
467 /*
468 * Once we've found a UGA supporting PCIIO,
469 * don't bother looking any further.
470 */
471 if (pciio)
472 break;
473
474 first_uga = uga;
475 }
476 }
477
Matt Flemingc116e8d2014-01-16 11:35:43 +0000478 return status;
479}
480
481static efi_status_t
482setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
483{
484 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
485 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
486 unsigned long nr_ugas;
487 u64 *handles = (u64 *)uga_handle;;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100488 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000489 int i;
490
491 first_uga = NULL;
492 nr_ugas = size / sizeof(u64);
493 for (i = 0; i < nr_ugas; i++) {
494 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
495 u32 w, h, depth, refresh;
496 void *pciio;
497 u64 handle = handles[i];
498
Matt Fleming204b0a12014-03-22 10:09:01 +0000499 status = efi_call_early(handle_protocol, handle,
500 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000501 if (status != EFI_SUCCESS)
502 continue;
503
Matt Fleming204b0a12014-03-22 10:09:01 +0000504 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000505
506 status = efi_early->call((unsigned long)uga->get_mode, uga,
507 &w, &h, &depth, &refresh);
508 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
509 *width = w;
510 *height = h;
511
512 /*
513 * Once we've found a UGA supporting PCIIO,
514 * don't bother looking any further.
515 */
516 if (pciio)
517 break;
518
519 first_uga = uga;
520 }
521 }
522
523 return status;
524}
525
526/*
527 * See if we have Universal Graphics Adapter (UGA) protocol
528 */
529static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
530 unsigned long size)
531{
532 efi_status_t status;
533 u32 width, height;
534 void **uga_handle = NULL;
535
Matt Fleming204b0a12014-03-22 10:09:01 +0000536 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
537 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000538 if (status != EFI_SUCCESS)
539 return status;
540
Matt Fleming204b0a12014-03-22 10:09:01 +0000541 status = efi_call_early(locate_handle,
542 EFI_LOCATE_BY_PROTOCOL,
543 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000544 if (status != EFI_SUCCESS)
545 goto free_handle;
546
547 height = 0;
548 width = 0;
549
550 if (efi_early->is64)
551 status = setup_uga64(uga_handle, size, &width, &height);
552 else
553 status = setup_uga32(uga_handle, size, &width, &height);
554
555 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000556 goto free_handle;
557
558 /* EFI framebuffer */
559 si->orig_video_isVGA = VIDEO_TYPE_EFI;
560
561 si->lfb_depth = 32;
562 si->lfb_width = width;
563 si->lfb_height = height;
564
565 si->red_size = 8;
566 si->red_pos = 16;
567 si->green_size = 8;
568 si->green_pos = 8;
569 si->blue_size = 8;
570 si->blue_pos = 0;
571 si->rsvd_size = 8;
572 si->rsvd_pos = 24;
573
Matt Fleming291f3632011-12-12 21:27:52 +0000574free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000575 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000576 return status;
577}
578
579void setup_graphics(struct boot_params *boot_params)
580{
581 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
582 struct screen_info *si;
583 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
584 efi_status_t status;
585 unsigned long size;
586 void **gop_handle = NULL;
587 void **uga_handle = NULL;
588
589 si = &boot_params->screen_info;
590 memset(si, 0, sizeof(*si));
591
592 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000593 status = efi_call_early(locate_handle,
594 EFI_LOCATE_BY_PROTOCOL,
595 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000596 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100597 status = efi_setup_gop(NULL, si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000598
599 if (status != EFI_SUCCESS) {
600 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000601 status = efi_call_early(locate_handle,
602 EFI_LOCATE_BY_PROTOCOL,
603 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000604 if (status == EFI_BUFFER_TOO_SMALL)
605 setup_uga(si, &uga_proto, size);
606 }
607}
608
Matt Fleming291f3632011-12-12 21:27:52 +0000609/*
610 * Because the x86 boot code expects to be passed a boot_params we
611 * need to create one ourselves (usually the bootloader would create
612 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +0100613 *
614 * The caller is responsible for filling out ->code32_start in the
615 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +0000616 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000617struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +0000618{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100619 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100620 struct apm_bios_info *bi;
621 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100622 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +0000623 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100624 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000625 int options_size = 0;
626 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700627 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +0000628 u16 *s2;
629 u8 *s1;
630 int i;
Roy Franz46f45822013-09-22 15:45:39 -0700631 unsigned long ramdisk_addr;
632 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000633
Matt Fleming54b52d82014-01-10 15:27:14 +0000634 efi_early = c;
635 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
636 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100637
638 /* Check if we were booted by the EFI firmware */
639 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
640 return NULL;
641
Matt Fleming54b52d82014-01-10 15:27:14 +0000642 if (efi_early->is64)
643 setup_boot_services64(efi_early);
644 else
645 setup_boot_services32(efi_early);
646
Matt Fleming204b0a12014-03-22 10:09:01 +0000647 status = efi_call_early(handle_protocol, handle,
648 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100649 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700650 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100651 return NULL;
652 }
653
Roy Franz40e45302013-09-22 15:45:29 -0700654 status = efi_low_alloc(sys_table, 0x4000, 1,
655 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100656 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700657 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100658 return NULL;
659 }
660
661 memset(boot_params, 0x0, 0x4000);
662
663 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100664 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100665
666 /* Copy the second sector to boot_params */
667 memcpy(&hdr->jump, image->image_base + 512, 512);
668
669 /*
670 * Fill out some of the header fields ourselves because the
671 * EFI firmware loader doesn't load the first sector.
672 */
673 hdr->root_flags = 1;
674 hdr->vid_mode = 0xffff;
675 hdr->boot_flag = 0xAA55;
676
Matt Fleming291f3632011-12-12 21:27:52 +0000677 hdr->type_of_loader = 0x21;
678
679 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500680 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700681 if (!cmdline_ptr)
682 goto fail;
683 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700684 /* Fill in upper bits of command line address, NOP on 32 bit */
685 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000686
687 hdr->ramdisk_image = 0;
688 hdr->ramdisk_size = 0;
689
Matt Fleming291f3632011-12-12 21:27:52 +0000690 /* Clear APM BIOS info */
691 memset(bi, 0, sizeof(*bi));
692
Matt Fleming5a17dae2014-08-05 11:52:11 +0100693 status = efi_parse_options(cmdline_ptr);
694 if (status != EFI_SUCCESS)
695 goto fail2;
696
Roy Franz46f45822013-09-22 15:45:39 -0700697 status = handle_cmdline_files(sys_table, image,
698 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700699 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700700 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700701
702 if (status != EFI_SUCCESS &&
703 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
704 efi_printk(sys_table, "Trying to load files to higher address\n");
705 status = handle_cmdline_files(sys_table, image,
706 (char *)(unsigned long)hdr->cmd_line_ptr,
707 "initrd=", -1UL,
708 &ramdisk_addr, &ramdisk_size);
709 }
710
Matt Fleming9ca8f722012-07-19 10:23:48 +0100711 if (status != EFI_SUCCESS)
712 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700713 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
714 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
715 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
716 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100717
718 return boot_params;
719fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -0700720 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100721fail:
Roy Franz40e45302013-09-22 15:45:29 -0700722 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100723 return NULL;
724}
725
Linn Crosettod2078d52013-09-22 19:59:08 -0600726static void add_e820ext(struct boot_params *params,
727 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100728{
Linn Crosettod2078d52013-09-22 19:59:08 -0600729 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100730 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600731 unsigned long size;
732
733 e820ext->type = SETUP_E820_EXT;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100734 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
Linn Crosettod2078d52013-09-22 19:59:08 -0600735 e820ext->next = 0;
736
737 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
738
739 while (data && data->next)
740 data = (struct setup_data *)(unsigned long)data->next;
741
742 if (data)
743 data->next = (unsigned long)e820ext;
744 else
745 params->hdr.setup_data = (unsigned long)e820ext;
746}
747
748static efi_status_t setup_e820(struct boot_params *params,
749 struct setup_data *e820ext, u32 e820ext_size)
750{
Ingo Molnar7410aa12017-01-29 12:56:13 +0100751 struct boot_e820_entry *entry = params->e820_table;
Linn Crosettod2078d52013-09-22 19:59:08 -0600752 struct efi_info *efi = &params->efi_info;
Ingo Molnar7410aa12017-01-29 12:56:13 +0100753 struct boot_e820_entry *prev = NULL;
Linn Crosettod2078d52013-09-22 19:59:08 -0600754 u32 nr_entries;
755 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100756 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000757
Matt Fleming291f3632011-12-12 21:27:52 +0000758 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600759 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
760
761 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000762 efi_memory_desc_t *d;
763 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600764 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000765
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400766#ifdef CONFIG_X86_64
767 m |= (u64)efi->efi_memmap_hi << 32;
768#endif
769
Linn Crosettod2078d52013-09-22 19:59:08 -0600770 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
Matt Fleming291f3632011-12-12 21:27:52 +0000771 switch (d->type) {
772 case EFI_RESERVED_TYPE:
773 case EFI_RUNTIME_SERVICES_CODE:
774 case EFI_RUNTIME_SERVICES_DATA:
775 case EFI_MEMORY_MAPPED_IO:
776 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
777 case EFI_PAL_CODE:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100778 e820_type = E820_TYPE_RESERVED;
Matt Fleming291f3632011-12-12 21:27:52 +0000779 break;
780
781 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100782 e820_type = E820_TYPE_UNUSABLE;
Matt Fleming291f3632011-12-12 21:27:52 +0000783 break;
784
785 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100786 e820_type = E820_TYPE_ACPI;
Matt Fleming291f3632011-12-12 21:27:52 +0000787 break;
788
789 case EFI_LOADER_CODE:
790 case EFI_LOADER_DATA:
791 case EFI_BOOT_SERVICES_CODE:
792 case EFI_BOOT_SERVICES_DATA:
793 case EFI_CONVENTIONAL_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100794 e820_type = E820_TYPE_RAM;
Matt Fleming291f3632011-12-12 21:27:52 +0000795 break;
796
797 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100798 e820_type = E820_TYPE_NVS;
Matt Fleming291f3632011-12-12 21:27:52 +0000799 break;
800
Dan Williamsad5fb872015-04-03 12:05:28 -0400801 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100802 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400803 break;
804
Matt Fleming291f3632011-12-12 21:27:52 +0000805 default:
806 continue;
807 }
808
809 /* Merge adjacent mappings */
810 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600811 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000812 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600813 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000814 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600815
Ingo Molnar61a50102017-01-27 13:54:38 +0100816 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100817 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
Linn Crosettod2078d52013-09-22 19:59:08 -0600818 sizeof(struct setup_data);
819
820 if (!e820ext || e820ext_size < need)
821 return EFI_BUFFER_TOO_SMALL;
822
823 /* boot_params map full, switch to e820 extended */
Ingo Molnar7410aa12017-01-29 12:56:13 +0100824 entry = (struct boot_e820_entry *)e820ext->data;
Linn Crosettod2078d52013-09-22 19:59:08 -0600825 }
826
Ingo Molnar7410aa12017-01-29 12:56:13 +0100827 entry->addr = d->phys_addr;
828 entry->size = d->num_pages << PAGE_SHIFT;
829 entry->type = e820_type;
830 prev = entry++;
Linn Crosettod2078d52013-09-22 19:59:08 -0600831 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000832 }
833
Ingo Molnar61a50102017-01-27 13:54:38 +0100834 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
835 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
Linn Crosettod2078d52013-09-22 19:59:08 -0600836
837 add_e820ext(params, e820ext, nr_e820ext);
838 nr_entries -= nr_e820ext;
839 }
840
841 params->e820_entries = (u8)nr_entries;
842
843 return EFI_SUCCESS;
844}
845
846static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
847 u32 *e820ext_size)
848{
849 efi_status_t status;
850 unsigned long size;
851
852 size = sizeof(struct setup_data) +
Ingo Molnar8ec67d92017-01-27 12:54:38 +0100853 sizeof(struct e820_entry) * nr_desc;
Linn Crosettod2078d52013-09-22 19:59:08 -0600854
855 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000856 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600857 *e820ext = NULL;
858 *e820ext_size = 0;
859 }
860
Matt Fleming204b0a12014-03-22 10:09:01 +0000861 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
862 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600863 if (status == EFI_SUCCESS)
864 *e820ext_size = size;
865
866 return status;
867}
868
Jeffrey Hugod6493402016-08-29 14:38:54 -0600869struct exit_boot_struct {
870 struct boot_params *boot_params;
871 struct efi_info *efi;
872 struct setup_data *e820ext;
873 __u32 e820ext_size;
874 bool is64;
875};
876
877static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
878 struct efi_boot_memmap *map,
879 void *priv)
880{
881 static bool first = true;
882 const char *signature;
883 __u32 nr_desc;
884 efi_status_t status;
885 struct exit_boot_struct *p = priv;
886
887 if (first) {
888 nr_desc = *map->buff_size / *map->desc_size;
Ingo Molnar61a50102017-01-27 13:54:38 +0100889 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
Jeffrey Hugod6493402016-08-29 14:38:54 -0600890 u32 nr_e820ext = nr_desc -
Ingo Molnar61a50102017-01-27 13:54:38 +0100891 ARRAY_SIZE(p->boot_params->e820_table);
Jeffrey Hugod6493402016-08-29 14:38:54 -0600892
893 status = alloc_e820ext(nr_e820ext, &p->e820ext,
894 &p->e820ext_size);
895 if (status != EFI_SUCCESS)
896 return status;
897 }
898 first = false;
899 }
900
901 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
902 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
903
904 p->efi->efi_systab = (unsigned long)sys_table_arg;
905 p->efi->efi_memdesc_size = *map->desc_size;
906 p->efi->efi_memdesc_version = *map->desc_ver;
907 p->efi->efi_memmap = (unsigned long)*map->map;
908 p->efi->efi_memmap_size = *map->map_size;
909
910#ifdef CONFIG_X86_64
911 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
912 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
913#endif
914
915 return EFI_SUCCESS;
916}
917
Linn Crosettod2078d52013-09-22 19:59:08 -0600918static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +0000919 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -0600920{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600921 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600922 efi_memory_desc_t *mem_map;
923 struct setup_data *e820ext;
924 __u32 e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600925 efi_status_t status;
926 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -0600927 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -0600928 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -0600929
Jeffrey Hugod6493402016-08-29 14:38:54 -0600930 map.map = &mem_map;
931 map.map_size = &map_sz;
932 map.desc_size = &desc_size;
933 map.desc_ver = &desc_version;
934 map.key_ptr = &key;
935 map.buff_size = &buff_size;
936 priv.boot_params = boot_params;
937 priv.efi = &boot_params->efi_info;
938 priv.e820ext = NULL;
939 priv.e820ext_size = 0;
940 priv.is64 = is64;
Linn Crosettod2078d52013-09-22 19:59:08 -0600941
Jeffrey Hugod6493402016-08-29 14:38:54 -0600942 /* Might as well exit boot services now */
943 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
944 exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -0600945 if (status != EFI_SUCCESS)
946 return status;
947
Jeffrey Hugod6493402016-08-29 14:38:54 -0600948 e820ext = priv.e820ext;
949 e820ext_size = priv.e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -0600950 /* Historic? */
951 boot_params->alt_mem_k = 32 * 1024;
952
953 status = setup_e820(boot_params, e820ext, e820ext_size);
954 if (status != EFI_SUCCESS)
955 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000956
957 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +0000958}
959
Matt Fleming9ca8f722012-07-19 10:23:48 +0100960/*
961 * On success we return a pointer to a boot_params structure, and NULL
962 * on failure.
963 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000964struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +0100965 struct boot_params *boot_params)
966{
Matt Fleming54b52d82014-01-10 15:27:14 +0000967 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100968 efi_loaded_image_t *image;
969 struct setup_header *hdr = &boot_params->hdr;
970 efi_status_t status;
971 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +0000972 void *handle;
973 efi_system_table_t *_table;
974 bool is64;
975
976 efi_early = c;
977
978 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
979 handle = (void *)(unsigned long)efi_early->image_handle;
980 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100981
982 sys_table = _table;
983
984 /* Check if we were booted by the EFI firmware */
985 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
986 goto fail;
987
Matt Fleming54b52d82014-01-10 15:27:14 +0000988 if (is64)
989 setup_boot_services64(efi_early);
990 else
991 setup_boot_services32(efi_early);
992
David Howellsde8cb452017-02-06 11:22:43 +0000993 /*
994 * If the boot loader gave us a value for secure_boot then we use that,
995 * otherwise we ask the BIOS.
996 */
997 if (boot_params->secure_boot == efi_secureboot_mode_unset)
998 boot_params->secure_boot = efi_get_secureboot(sys_table);
999
Matthew Garrettccc829b2017-08-25 16:50:15 +01001000 /* Ask the firmware to clear memory on unclean shutdown */
1001 efi_enable_reset_attack_mitigation(sys_table);
1002
Matt Fleming9ca8f722012-07-19 10:23:48 +01001003 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001004
Matt Fleming56394ab2014-09-11 09:04:25 +01001005 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001006
Lukas Wunner58c54752016-11-12 21:32:36 +00001007 setup_quirks(boot_params);
1008
Matt Fleming204b0a12014-03-22 10:09:01 +00001009 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1010 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001011 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001012 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001013 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001014 }
Matt Fleming291f3632011-12-12 21:27:52 +00001015
1016 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001017 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001018 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001019 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001020 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001021 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001022 }
Matt Fleming291f3632011-12-12 21:27:52 +00001023
Matt Fleming9ca8f722012-07-19 10:23:48 +01001024 /*
1025 * If the kernel isn't already loaded at the preferred load
1026 * address, relocate it.
1027 */
1028 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001029 unsigned long bzimage_addr = hdr->code32_start;
1030 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1031 hdr->init_size, hdr->init_size,
1032 hdr->pref_address,
1033 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001034 if (status != EFI_SUCCESS) {
1035 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001036 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001037 }
Roy Franz4a9f3a72013-09-22 15:45:32 -07001038
1039 hdr->pref_address = hdr->code32_start;
1040 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001041 }
1042
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001043 status = exit_boot(boot_params, handle, is64);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001044 if (status != EFI_SUCCESS) {
1045 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001046 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001047 }
Matt Fleming291f3632011-12-12 21:27:52 +00001048
1049 memset((char *)gdt->address, 0x0, gdt->size);
1050 desc = (struct desc_struct *)gdt->address;
1051
Kirill A. Shutemov919a02d2017-06-06 14:31:24 +03001052 /* The first GDT is a dummy. */
1053 desc++;
1054
1055 if (IS_ENABLED(CONFIG_X86_64)) {
1056 /* __KERNEL32_CS */
1057 desc->limit0 = 0xffff;
1058 desc->base0 = 0x0000;
1059 desc->base1 = 0x0000;
1060 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1061 desc->s = DESC_TYPE_CODE_DATA;
1062 desc->dpl = 0;
1063 desc->p = 1;
1064 desc->limit = 0xf;
1065 desc->avl = 0;
1066 desc->l = 0;
1067 desc->d = SEG_OP_SIZE_32BIT;
1068 desc->g = SEG_GRANULARITY_4KB;
1069 desc->base2 = 0x00;
1070 desc++;
1071 } else {
1072 /* Second entry is unused on 32-bit */
1073 desc++;
1074 }
Matt Fleming291f3632011-12-12 21:27:52 +00001075
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001076 /* __KERNEL_CS */
Matt Fleming291f3632011-12-12 21:27:52 +00001077 desc->limit0 = 0xffff;
1078 desc->base0 = 0x0000;
1079 desc->base1 = 0x0000;
1080 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1081 desc->s = DESC_TYPE_CODE_DATA;
1082 desc->dpl = 0;
1083 desc->p = 1;
1084 desc->limit = 0xf;
1085 desc->avl = 0;
Kirill A. Shutemov4c941172017-06-06 14:31:23 +03001086 if (IS_ENABLED(CONFIG_X86_64)) {
1087 desc->l = 1;
1088 desc->d = 0;
1089 } else {
1090 desc->l = 0;
1091 desc->d = SEG_OP_SIZE_32BIT;
1092 }
Matt Fleming291f3632011-12-12 21:27:52 +00001093 desc->g = SEG_GRANULARITY_4KB;
1094 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +00001095 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001096
1097 /* __KERNEL_DS */
Matt Fleming291f3632011-12-12 21:27:52 +00001098 desc->limit0 = 0xffff;
1099 desc->base0 = 0x0000;
1100 desc->base1 = 0x0000;
1101 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1102 desc->s = DESC_TYPE_CODE_DATA;
1103 desc->dpl = 0;
1104 desc->p = 1;
1105 desc->limit = 0xf;
1106 desc->avl = 0;
1107 desc->l = 0;
1108 desc->d = SEG_OP_SIZE_32BIT;
1109 desc->g = SEG_GRANULARITY_4KB;
1110 desc->base2 = 0x00;
Matt Fleming291f3632011-12-12 21:27:52 +00001111 desc++;
Kirill A. Shutemovf8fceac2017-06-06 14:31:22 +03001112
1113 if (IS_ENABLED(CONFIG_X86_64)) {
1114 /* Task segment value */
1115 desc->limit0 = 0x0000;
1116 desc->base0 = 0x0000;
1117 desc->base1 = 0x0000;
1118 desc->type = SEG_TYPE_TSS;
1119 desc->s = 0;
1120 desc->dpl = 0;
1121 desc->p = 1;
1122 desc->limit = 0x0;
1123 desc->avl = 0;
1124 desc->l = 0;
1125 desc->d = 0;
1126 desc->g = SEG_GRANULARITY_4KB;
1127 desc->base2 = 0x00;
1128 desc++;
1129 }
Matt Fleming291f3632011-12-12 21:27:52 +00001130
Matt Fleming291f3632011-12-12 21:27:52 +00001131 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001132 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001133
1134 return boot_params;
1135fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001136 efi_printk(sys_table, "efi_main() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001137 return NULL;
1138}