blob: cc69e37548db221f128fbadb217f1e6385991b0c [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>
Matt Fleming291f3632011-12-12 21:27:52 +000012#include <asm/efi.h>
13#include <asm/setup.h>
14#include <asm/desc.h>
15
Andrey Ryabinin393f2032015-02-13 14:39:56 -080016#include "../string.h"
Matt Fleming291f3632011-12-12 21:27:52 +000017#include "eboot.h"
18
19static efi_system_table_t *sys_table;
20
Matt Fleming84be8802014-09-23 10:37:43 +010021static struct efi_config *efi_early;
22
Ard Biesheuvel243b6752014-11-05 17:00:56 +010023__pure const struct efi_config *__efi_early(void)
24{
25 return efi_early;
26}
Matt Fleming204b0a12014-03-22 10:09:01 +000027
Matt Fleming54b52d82014-01-10 15:27:14 +000028#define BOOT_SERVICES(bits) \
29static void setup_boot_services##bits(struct efi_config *c) \
30{ \
31 efi_system_table_##bits##_t *table; \
Matt Fleming54b52d82014-01-10 15:27:14 +000032 \
33 table = (typeof(table))sys_table; \
34 \
Lukas Wunner0a637ee2016-08-22 12:01:21 +020035 c->boot_services = table->boottime; \
Matt Fleming54b52d82014-01-10 15:27:14 +000036 c->text_output = table->con_out; \
Matt Fleming54b52d82014-01-10 15:27:14 +000037}
38BOOT_SERVICES(32);
39BOOT_SERVICES(64);
40
Ard Biesheuvelbd669472014-07-02 14:54:42 +020041void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
Matt Fleming54b52d82014-01-10 15:27:14 +000042
43static efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +000044__file_size32(void *__fh, efi_char16_t *filename_16,
45 void **handle, u64 *file_sz)
Matt Fleming54b52d82014-01-10 15:27:14 +000046{
Matt Flemingc116e8d2014-01-16 11:35:43 +000047 efi_file_handle_32_t *h, *fh = __fh;
Matt Fleming54b52d82014-01-10 15:27:14 +000048 efi_file_info_t *info;
49 efi_status_t status;
50 efi_guid_t info_guid = EFI_FILE_INFO_ID;
51 u32 info_sz;
52
53 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
54 EFI_FILE_MODE_READ, (u64)0);
55 if (status != EFI_SUCCESS) {
56 efi_printk(sys_table, "Failed to open file: ");
57 efi_char16_printk(sys_table, filename_16);
58 efi_printk(sys_table, "\n");
59 return status;
60 }
61
62 *handle = h;
63
64 info_sz = 0;
65 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
66 &info_sz, NULL);
67 if (status != EFI_BUFFER_TOO_SMALL) {
68 efi_printk(sys_table, "Failed to get file info size\n");
69 return status;
70 }
71
72grow:
Matt Fleming204b0a12014-03-22 10:09:01 +000073 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
74 info_sz, (void **)&info);
Matt Fleming54b52d82014-01-10 15:27:14 +000075 if (status != EFI_SUCCESS) {
76 efi_printk(sys_table, "Failed to alloc mem for file info\n");
77 return status;
78 }
79
80 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
81 &info_sz, info);
82 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +000083 efi_call_early(free_pool, info);
Matt Fleming54b52d82014-01-10 15:27:14 +000084 goto grow;
85 }
86
87 *file_sz = info->file_size;
Matt Fleming204b0a12014-03-22 10:09:01 +000088 efi_call_early(free_pool, info);
Matt Fleming54b52d82014-01-10 15:27:14 +000089
90 if (status != EFI_SUCCESS)
91 efi_printk(sys_table, "Failed to get initrd info\n");
92
93 return status;
94}
95
Matt Flemingc116e8d2014-01-16 11:35:43 +000096static efi_status_t
97__file_size64(void *__fh, efi_char16_t *filename_16,
98 void **handle, u64 *file_sz)
99{
100 efi_file_handle_64_t *h, *fh = __fh;
101 efi_file_info_t *info;
102 efi_status_t status;
103 efi_guid_t info_guid = EFI_FILE_INFO_ID;
Matt Fleming396f1a02014-04-10 13:30:13 +0100104 u64 info_sz;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000105
106 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
107 EFI_FILE_MODE_READ, (u64)0);
108 if (status != EFI_SUCCESS) {
109 efi_printk(sys_table, "Failed to open file: ");
110 efi_char16_printk(sys_table, filename_16);
111 efi_printk(sys_table, "\n");
112 return status;
113 }
114
115 *handle = h;
116
117 info_sz = 0;
118 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
119 &info_sz, NULL);
120 if (status != EFI_BUFFER_TOO_SMALL) {
121 efi_printk(sys_table, "Failed to get file info size\n");
122 return status;
123 }
124
125grow:
Matt Fleming204b0a12014-03-22 10:09:01 +0000126 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
127 info_sz, (void **)&info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000128 if (status != EFI_SUCCESS) {
129 efi_printk(sys_table, "Failed to alloc mem for file info\n");
130 return status;
131 }
132
133 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
134 &info_sz, info);
135 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000136 efi_call_early(free_pool, info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000137 goto grow;
138 }
139
140 *file_sz = info->file_size;
Matt Fleming204b0a12014-03-22 10:09:01 +0000141 efi_call_early(free_pool, info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000142
143 if (status != EFI_SUCCESS)
144 efi_printk(sys_table, "Failed to get initrd info\n");
145
146 return status;
147}
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200148efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +0000149efi_file_size(efi_system_table_t *sys_table, void *__fh,
150 efi_char16_t *filename_16, void **handle, u64 *file_sz)
151{
152 if (efi_early->is64)
153 return __file_size64(__fh, filename_16, handle, file_sz);
154
155 return __file_size32(__fh, filename_16, handle, file_sz);
156}
157
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200158efi_status_t
Matt Fleming47514c92014-04-10 14:11:45 +0100159efi_file_read(void *handle, unsigned long *size, void *addr)
Matt Fleming54b52d82014-01-10 15:27:14 +0000160{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000161 unsigned long func;
162
163 if (efi_early->is64) {
Matt Fleming47514c92014-04-10 14:11:45 +0100164 efi_file_handle_64_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000165
166 func = (unsigned long)fh->read;
167 return efi_early->call(func, handle, size, addr);
168 } else {
Matt Fleming47514c92014-04-10 14:11:45 +0100169 efi_file_handle_32_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000170
171 func = (unsigned long)fh->read;
172 return efi_early->call(func, handle, size, addr);
173 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000174}
175
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200176efi_status_t efi_file_close(void *handle)
Matt Fleming54b52d82014-01-10 15:27:14 +0000177{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000178 if (efi_early->is64) {
Matt Fleming47514c92014-04-10 14:11:45 +0100179 efi_file_handle_64_t *fh = handle;
Matt Fleming54b52d82014-01-10 15:27:14 +0000180
Matt Flemingc116e8d2014-01-16 11:35:43 +0000181 return efi_early->call((unsigned long)fh->close, handle);
182 } else {
Matt Fleming47514c92014-04-10 14:11:45 +0100183 efi_file_handle_32_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000184
185 return efi_early->call((unsigned long)fh->close, handle);
186 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000187}
188
Matt Flemingc116e8d2014-01-16 11:35:43 +0000189static inline efi_status_t __open_volume32(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000190{
191 efi_file_io_interface_t *io;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000192 efi_loaded_image_32_t *image = __image;
193 efi_file_handle_32_t *fh;
Matt Fleming54b52d82014-01-10 15:27:14 +0000194 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
195 efi_status_t status;
196 void *handle = (void *)(unsigned long)image->device_handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000197 unsigned long func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000198
Matt Fleming204b0a12014-03-22 10:09:01 +0000199 status = efi_call_early(handle_protocol, handle,
200 &fs_proto, (void **)&io);
Matt Fleming54b52d82014-01-10 15:27:14 +0000201 if (status != EFI_SUCCESS) {
202 efi_printk(sys_table, "Failed to handle fs_proto\n");
203 return status;
204 }
205
206 func = (unsigned long)io->open_volume;
207 status = efi_early->call(func, io, &fh);
208 if (status != EFI_SUCCESS)
209 efi_printk(sys_table, "Failed to open volume\n");
210
211 *__fh = fh;
212 return status;
213}
214
Matt Flemingc116e8d2014-01-16 11:35:43 +0000215static inline efi_status_t __open_volume64(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000216{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000217 efi_file_io_interface_t *io;
218 efi_loaded_image_64_t *image = __image;
219 efi_file_handle_64_t *fh;
220 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
221 efi_status_t status;
222 void *handle = (void *)(unsigned long)image->device_handle;
223 unsigned long func;
224
Matt Fleming204b0a12014-03-22 10:09:01 +0000225 status = efi_call_early(handle_protocol, handle,
226 &fs_proto, (void **)&io);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000227 if (status != EFI_SUCCESS) {
228 efi_printk(sys_table, "Failed to handle fs_proto\n");
229 return status;
230 }
231
232 func = (unsigned long)io->open_volume;
233 status = efi_early->call(func, io, &fh);
234 if (status != EFI_SUCCESS)
235 efi_printk(sys_table, "Failed to open volume\n");
236
237 *__fh = fh;
238 return status;
239}
240
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200241efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +0000242efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
243{
244 if (efi_early->is64)
245 return __open_volume64(__image, __fh);
246
247 return __open_volume32(__image, __fh);
248}
249
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200250void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000251{
Matt Fleming54b52d82014-01-10 15:27:14 +0000252 unsigned long output_string;
253 size_t offset;
Matt Fleming54b52d82014-01-10 15:27:14 +0000254
Matt Flemingc116e8d2014-01-16 11:35:43 +0000255 if (efi_early->is64) {
256 struct efi_simple_text_output_protocol_64 *out;
257 u64 *func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000258
Matt Flemingc116e8d2014-01-16 11:35:43 +0000259 offset = offsetof(typeof(*out), output_string);
260 output_string = efi_early->text_output + offset;
Matt Fleming115c6622014-09-24 11:56:10 +0100261 out = (typeof(out))(unsigned long)efi_early->text_output;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000262 func = (u64 *)output_string;
263
Matt Fleming115c6622014-09-24 11:56:10 +0100264 efi_early->call(*func, out, str);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000265 } else {
266 struct efi_simple_text_output_protocol_32 *out;
267 u32 *func;
268
269 offset = offsetof(typeof(*out), output_string);
270 output_string = efi_early->text_output + offset;
Matt Fleming115c6622014-09-24 11:56:10 +0100271 out = (typeof(out))(unsigned long)efi_early->text_output;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000272 func = (u32 *)output_string;
273
Matt Fleming115c6622014-09-24 11:56:10 +0100274 efi_early->call(*func, out, str);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000275 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000276}
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800277
Matt Flemingc116e8d2014-01-16 11:35:43 +0000278static efi_status_t
279__setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700280{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000281 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700282 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000283 unsigned long size;
284 uint64_t attributes;
285
286 status = efi_early->call(pci->attributes, pci,
287 EfiPciIoAttributeOperationGet, 0, 0,
288 &attributes);
289 if (status != EFI_SUCCESS)
290 return status;
291
292 if (!pci->romimage || !pci->romsize)
293 return EFI_INVALID_PARAMETER;
294
295 size = pci->romsize + sizeof(*rom);
296
Matt Fleming204b0a12014-03-22 10:09:01 +0000297 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200298 if (status != EFI_SUCCESS) {
299 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000300 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200301 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000302
303 memset(rom, 0, sizeof(*rom));
304
305 rom->data.type = SETUP_PCI;
306 rom->data.len = size - sizeof(struct setup_data);
307 rom->data.next = 0;
308 rom->pcilen = pci->romsize;
309 *__rom = rom;
310
311 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
312 PCI_VENDOR_ID, 1, &(rom->vendor));
313
Andre Müller77e21e82014-09-10 01:00:22 +0200314 if (status != EFI_SUCCESS) {
315 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000316 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200317 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000318
319 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
320 PCI_DEVICE_ID, 1, &(rom->devid));
321
Andre Müller77e21e82014-09-10 01:00:22 +0200322 if (status != EFI_SUCCESS) {
323 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000324 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200325 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000326
327 status = efi_early->call(pci->get_location, pci, &(rom->segment),
328 &(rom->bus), &(rom->device), &(rom->function));
329
330 if (status != EFI_SUCCESS)
331 goto free_struct;
332
333 memcpy(rom->romdata, pci->romimage, pci->romsize);
334 return status;
335
336free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000337 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000338 return status;
339}
340
Matt Fleming56394ab2014-09-11 09:04:25 +0100341static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000342setup_efi_pci32(struct boot_params *params, void **pci_handle,
343 unsigned long size)
344{
345 efi_pci_io_protocol_32 *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700346 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000347 u32 *handles = (u32 *)(unsigned long)pci_handle;
348 efi_status_t status;
349 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700350 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000351 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700352
Jan Beulichbc754792013-01-18 12:35:14 +0000353 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700354
355 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000356 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700357
Matt Flemingc116e8d2014-01-16 11:35:43 +0000358 nr_pci = size / sizeof(u32);
359 for (i = 0; i < nr_pci; i++) {
360 struct pci_setup_rom *rom = NULL;
361 u32 h = handles[i];
362
Matt Fleming204b0a12014-03-22 10:09:01 +0000363 status = efi_call_early(handle_protocol, h,
364 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000365
366 if (status != EFI_SUCCESS)
367 continue;
368
369 if (!pci)
370 continue;
371
372 status = __setup_efi_pci32(pci, &rom);
373 if (status != EFI_SUCCESS)
374 continue;
375
376 if (data)
377 data->next = (unsigned long)rom;
378 else
379 params->hdr.setup_data = (unsigned long)rom;
380
381 data = (struct setup_data *)rom;
382
383 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000384}
385
386static efi_status_t
387__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
388{
389 struct pci_setup_rom *rom;
390 efi_status_t status;
391 unsigned long size;
392 uint64_t attributes;
393
394 status = efi_early->call(pci->attributes, pci,
395 EfiPciIoAttributeOperationGet, 0,
396 &attributes);
397 if (status != EFI_SUCCESS)
398 return status;
399
400 if (!pci->romimage || !pci->romsize)
401 return EFI_INVALID_PARAMETER;
402
403 size = pci->romsize + sizeof(*rom);
404
Matt Fleming204b0a12014-03-22 10:09:01 +0000405 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200406 if (status != EFI_SUCCESS) {
407 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000408 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200409 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000410
411 rom->data.type = SETUP_PCI;
412 rom->data.len = size - sizeof(struct setup_data);
413 rom->data.next = 0;
414 rom->pcilen = pci->romsize;
415 *__rom = rom;
416
417 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
418 PCI_VENDOR_ID, 1, &(rom->vendor));
419
Andre Müller77e21e82014-09-10 01:00:22 +0200420 if (status != EFI_SUCCESS) {
421 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000422 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200423 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000424
425 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
426 PCI_DEVICE_ID, 1, &(rom->devid));
427
Andre Müller77e21e82014-09-10 01:00:22 +0200428 if (status != EFI_SUCCESS) {
429 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000430 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200431 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000432
433 status = efi_early->call(pci->get_location, pci, &(rom->segment),
434 &(rom->bus), &(rom->device), &(rom->function));
435
436 if (status != EFI_SUCCESS)
437 goto free_struct;
438
439 memcpy(rom->romdata, pci->romimage, pci->romsize);
440 return status;
441
442free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000443 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000444 return status;
445
446}
447
Matt Fleming56394ab2014-09-11 09:04:25 +0100448static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000449setup_efi_pci64(struct boot_params *params, void **pci_handle,
450 unsigned long size)
451{
452 efi_pci_io_protocol_64 *pci = NULL;
453 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
454 u64 *handles = (u64 *)(unsigned long)pci_handle;
455 efi_status_t status;
456 unsigned long nr_pci;
457 struct setup_data *data;
458 int i;
459
460 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
461
462 while (data && data->next)
463 data = (struct setup_data *)(unsigned long)data->next;
464
465 nr_pci = size / sizeof(u64);
466 for (i = 0; i < nr_pci; i++) {
467 struct pci_setup_rom *rom = NULL;
468 u64 h = handles[i];
469
Matt Fleming204b0a12014-03-22 10:09:01 +0000470 status = efi_call_early(handle_protocol, h,
471 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000472
473 if (status != EFI_SUCCESS)
474 continue;
475
476 if (!pci)
477 continue;
478
479 status = __setup_efi_pci64(pci, &rom);
480 if (status != EFI_SUCCESS)
481 continue;
482
483 if (data)
484 data->next = (unsigned long)rom;
485 else
486 params->hdr.setup_data = (unsigned long)rom;
487
488 data = (struct setup_data *)rom;
489
490 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000491}
492
Matt Fleming56394ab2014-09-11 09:04:25 +0100493/*
494 * There's no way to return an informative status from this function,
495 * because any analysis (and printing of error messages) needs to be
496 * done directly at the EFI function call-site.
497 *
498 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
499 * just didn't find any PCI devices, but there's no way to tell outside
500 * the context of the call.
501 */
502static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000503{
504 efi_status_t status;
505 void **pci_handle = NULL;
506 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
507 unsigned long size = 0;
508
Matt Fleming204b0a12014-03-22 10:09:01 +0000509 status = efi_call_early(locate_handle,
510 EFI_LOCATE_BY_PROTOCOL,
511 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700512
513 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000514 status = efi_call_early(allocate_pool,
515 EFI_LOADER_DATA,
516 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700517
Andre Müller77e21e82014-09-10 01:00:22 +0200518 if (status != EFI_SUCCESS) {
519 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100520 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200521 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700522
Matt Fleming204b0a12014-03-22 10:09:01 +0000523 status = efi_call_early(locate_handle,
524 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
525 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700526 }
527
528 if (status != EFI_SUCCESS)
529 goto free_handle;
530
Matt Flemingc116e8d2014-01-16 11:35:43 +0000531 if (efi_early->is64)
Matt Fleming56394ab2014-09-11 09:04:25 +0100532 setup_efi_pci64(params, pci_handle, size);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000533 else
Matt Fleming56394ab2014-09-11 09:04:25 +0100534 setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700535
536free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000537 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700538}
539
Matt Flemingc116e8d2014-01-16 11:35:43 +0000540static efi_status_t
541setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000542{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000543 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
544 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000545 unsigned long nr_ugas;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000546 u32 *handles = (u32 *)uga_handle;;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100547 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Fleming291f3632011-12-12 21:27:52 +0000548 int i;
549
Matt Fleming291f3632011-12-12 21:27:52 +0000550 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000551 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000552 for (i = 0; i < nr_ugas; i++) {
553 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000554 u32 w, h, depth, refresh;
555 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000556 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000557
Matt Fleming204b0a12014-03-22 10:09:01 +0000558 status = efi_call_early(handle_protocol, handle,
559 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000560 if (status != EFI_SUCCESS)
561 continue;
562
Matt Fleming204b0a12014-03-22 10:09:01 +0000563 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000564
Matt Fleming54b52d82014-01-10 15:27:14 +0000565 status = efi_early->call((unsigned long)uga->get_mode, uga,
566 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000567 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000568 *width = w;
569 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000570
571 /*
572 * Once we've found a UGA supporting PCIIO,
573 * don't bother looking any further.
574 */
575 if (pciio)
576 break;
577
578 first_uga = uga;
579 }
580 }
581
Matt Flemingc116e8d2014-01-16 11:35:43 +0000582 return status;
583}
584
585static efi_status_t
586setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
587{
588 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
589 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
590 unsigned long nr_ugas;
591 u64 *handles = (u64 *)uga_handle;;
Colin Ian Kingac0e94b2016-07-20 11:11:06 +0100592 efi_status_t status = EFI_INVALID_PARAMETER;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000593 int i;
594
595 first_uga = NULL;
596 nr_ugas = size / sizeof(u64);
597 for (i = 0; i < nr_ugas; i++) {
598 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
599 u32 w, h, depth, refresh;
600 void *pciio;
601 u64 handle = handles[i];
602
Matt Fleming204b0a12014-03-22 10:09:01 +0000603 status = efi_call_early(handle_protocol, handle,
604 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000605 if (status != EFI_SUCCESS)
606 continue;
607
Matt Fleming204b0a12014-03-22 10:09:01 +0000608 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000609
610 status = efi_early->call((unsigned long)uga->get_mode, uga,
611 &w, &h, &depth, &refresh);
612 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
613 *width = w;
614 *height = h;
615
616 /*
617 * Once we've found a UGA supporting PCIIO,
618 * don't bother looking any further.
619 */
620 if (pciio)
621 break;
622
623 first_uga = uga;
624 }
625 }
626
627 return status;
628}
629
630/*
631 * See if we have Universal Graphics Adapter (UGA) protocol
632 */
633static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
634 unsigned long size)
635{
636 efi_status_t status;
637 u32 width, height;
638 void **uga_handle = NULL;
639
Matt Fleming204b0a12014-03-22 10:09:01 +0000640 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
641 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000642 if (status != EFI_SUCCESS)
643 return status;
644
Matt Fleming204b0a12014-03-22 10:09:01 +0000645 status = efi_call_early(locate_handle,
646 EFI_LOCATE_BY_PROTOCOL,
647 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000648 if (status != EFI_SUCCESS)
649 goto free_handle;
650
651 height = 0;
652 width = 0;
653
654 if (efi_early->is64)
655 status = setup_uga64(uga_handle, size, &width, &height);
656 else
657 status = setup_uga32(uga_handle, size, &width, &height);
658
659 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000660 goto free_handle;
661
662 /* EFI framebuffer */
663 si->orig_video_isVGA = VIDEO_TYPE_EFI;
664
665 si->lfb_depth = 32;
666 si->lfb_width = width;
667 si->lfb_height = height;
668
669 si->red_size = 8;
670 si->red_pos = 16;
671 si->green_size = 8;
672 si->green_pos = 8;
673 si->blue_size = 8;
674 si->blue_pos = 0;
675 si->rsvd_size = 8;
676 si->rsvd_pos = 24;
677
Matt Fleming291f3632011-12-12 21:27:52 +0000678free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000679 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000680 return status;
681}
682
683void setup_graphics(struct boot_params *boot_params)
684{
685 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
686 struct screen_info *si;
687 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
688 efi_status_t status;
689 unsigned long size;
690 void **gop_handle = NULL;
691 void **uga_handle = NULL;
692
693 si = &boot_params->screen_info;
694 memset(si, 0, sizeof(*si));
695
696 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000697 status = efi_call_early(locate_handle,
698 EFI_LOCATE_BY_PROTOCOL,
699 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000700 if (status == EFI_BUFFER_TOO_SMALL)
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100701 status = efi_setup_gop(NULL, si, &graphics_proto, size);
Matt Fleming291f3632011-12-12 21:27:52 +0000702
703 if (status != EFI_SUCCESS) {
704 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000705 status = efi_call_early(locate_handle,
706 EFI_LOCATE_BY_PROTOCOL,
707 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000708 if (status == EFI_BUFFER_TOO_SMALL)
709 setup_uga(si, &uga_proto, size);
710 }
711}
712
Matt Fleming291f3632011-12-12 21:27:52 +0000713/*
714 * Because the x86 boot code expects to be passed a boot_params we
715 * need to create one ourselves (usually the bootloader would create
716 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +0100717 *
718 * The caller is responsible for filling out ->code32_start in the
719 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +0000720 */
Matt Fleming54b52d82014-01-10 15:27:14 +0000721struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +0000722{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100723 struct boot_params *boot_params;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100724 struct apm_bios_info *bi;
725 struct setup_header *hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100726 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +0000727 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100728 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000729 int options_size = 0;
730 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -0700731 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +0000732 u16 *s2;
733 u8 *s1;
734 int i;
Roy Franz46f45822013-09-22 15:45:39 -0700735 unsigned long ramdisk_addr;
736 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +0000737
Matt Fleming54b52d82014-01-10 15:27:14 +0000738 efi_early = c;
739 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
740 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100741
742 /* Check if we were booted by the EFI firmware */
743 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
744 return NULL;
745
Matt Fleming54b52d82014-01-10 15:27:14 +0000746 if (efi_early->is64)
747 setup_boot_services64(efi_early);
748 else
749 setup_boot_services32(efi_early);
750
Matt Fleming204b0a12014-03-22 10:09:01 +0000751 status = efi_call_early(handle_protocol, handle,
752 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100753 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700754 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100755 return NULL;
756 }
757
Roy Franz40e45302013-09-22 15:45:29 -0700758 status = efi_low_alloc(sys_table, 0x4000, 1,
759 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100760 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700761 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +0100762 return NULL;
763 }
764
765 memset(boot_params, 0x0, 0x4000);
766
767 hdr = &boot_params->hdr;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100768 bi = &boot_params->apm_bios_info;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100769
770 /* Copy the second sector to boot_params */
771 memcpy(&hdr->jump, image->image_base + 512, 512);
772
773 /*
774 * Fill out some of the header fields ourselves because the
775 * EFI firmware loader doesn't load the first sector.
776 */
777 hdr->root_flags = 1;
778 hdr->vid_mode = 0xffff;
779 hdr->boot_flag = 0xAA55;
780
Matt Fleming291f3632011-12-12 21:27:52 +0000781 hdr->type_of_loader = 0x21;
782
783 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -0500784 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -0700785 if (!cmdline_ptr)
786 goto fail;
787 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Roy Franz98b228f2015-04-15 16:32:24 -0700788 /* Fill in upper bits of command line address, NOP on 32 bit */
789 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
Matt Fleming291f3632011-12-12 21:27:52 +0000790
791 hdr->ramdisk_image = 0;
792 hdr->ramdisk_size = 0;
793
Matt Fleming291f3632011-12-12 21:27:52 +0000794 /* Clear APM BIOS info */
795 memset(bi, 0, sizeof(*bi));
796
Matt Fleming5a17dae2014-08-05 11:52:11 +0100797 status = efi_parse_options(cmdline_ptr);
798 if (status != EFI_SUCCESS)
799 goto fail2;
800
Roy Franz46f45822013-09-22 15:45:39 -0700801 status = handle_cmdline_files(sys_table, image,
802 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -0700803 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -0700804 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -0700805
806 if (status != EFI_SUCCESS &&
807 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
808 efi_printk(sys_table, "Trying to load files to higher address\n");
809 status = handle_cmdline_files(sys_table, image,
810 (char *)(unsigned long)hdr->cmd_line_ptr,
811 "initrd=", -1UL,
812 &ramdisk_addr, &ramdisk_size);
813 }
814
Matt Fleming9ca8f722012-07-19 10:23:48 +0100815 if (status != EFI_SUCCESS)
816 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -0700817 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
818 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
819 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
820 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100821
822 return boot_params;
823fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -0700824 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100825fail:
Roy Franz40e45302013-09-22 15:45:29 -0700826 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100827 return NULL;
828}
829
Linn Crosettod2078d52013-09-22 19:59:08 -0600830static void add_e820ext(struct boot_params *params,
831 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100832{
Linn Crosettod2078d52013-09-22 19:59:08 -0600833 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100834 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -0600835 unsigned long size;
836
837 e820ext->type = SETUP_E820_EXT;
838 e820ext->len = nr_entries * sizeof(struct e820entry);
839 e820ext->next = 0;
840
841 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
842
843 while (data && data->next)
844 data = (struct setup_data *)(unsigned long)data->next;
845
846 if (data)
847 data->next = (unsigned long)e820ext;
848 else
849 params->hdr.setup_data = (unsigned long)e820ext;
850}
851
852static efi_status_t setup_e820(struct boot_params *params,
853 struct setup_data *e820ext, u32 e820ext_size)
854{
855 struct e820entry *e820_map = &params->e820_map[0];
856 struct efi_info *efi = &params->efi_info;
857 struct e820entry *prev = NULL;
858 u32 nr_entries;
859 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +0100860 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000861
Matt Fleming291f3632011-12-12 21:27:52 +0000862 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600863 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
864
865 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +0000866 efi_memory_desc_t *d;
867 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -0600868 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +0000869
Dmitry Skorodumov7cc03e42015-07-28 18:38:32 +0400870#ifdef CONFIG_X86_64
871 m |= (u64)efi->efi_memmap_hi << 32;
872#endif
873
Linn Crosettod2078d52013-09-22 19:59:08 -0600874 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
Matt Fleming291f3632011-12-12 21:27:52 +0000875 switch (d->type) {
876 case EFI_RESERVED_TYPE:
877 case EFI_RUNTIME_SERVICES_CODE:
878 case EFI_RUNTIME_SERVICES_DATA:
879 case EFI_MEMORY_MAPPED_IO:
880 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
881 case EFI_PAL_CODE:
882 e820_type = E820_RESERVED;
883 break;
884
885 case EFI_UNUSABLE_MEMORY:
886 e820_type = E820_UNUSABLE;
887 break;
888
889 case EFI_ACPI_RECLAIM_MEMORY:
890 e820_type = E820_ACPI;
891 break;
892
893 case EFI_LOADER_CODE:
894 case EFI_LOADER_DATA:
895 case EFI_BOOT_SERVICES_CODE:
896 case EFI_BOOT_SERVICES_DATA:
897 case EFI_CONVENTIONAL_MEMORY:
898 e820_type = E820_RAM;
899 break;
900
901 case EFI_ACPI_MEMORY_NVS:
902 e820_type = E820_NVS;
903 break;
904
Dan Williamsad5fb872015-04-03 12:05:28 -0400905 case EFI_PERSISTENT_MEMORY:
906 e820_type = E820_PMEM;
907 break;
908
Matt Fleming291f3632011-12-12 21:27:52 +0000909 default:
910 continue;
911 }
912
913 /* Merge adjacent mappings */
914 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -0600915 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +0000916 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -0600917 continue;
Matt Fleming291f3632011-12-12 21:27:52 +0000918 }
Linn Crosettod2078d52013-09-22 19:59:08 -0600919
920 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
921 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
922 sizeof(struct setup_data);
923
924 if (!e820ext || e820ext_size < need)
925 return EFI_BUFFER_TOO_SMALL;
926
927 /* boot_params map full, switch to e820 extended */
928 e820_map = (struct e820entry *)e820ext->data;
929 }
930
931 e820_map->addr = d->phys_addr;
932 e820_map->size = d->num_pages << PAGE_SHIFT;
933 e820_map->type = e820_type;
934 prev = e820_map++;
935 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +0000936 }
937
Linn Crosettod2078d52013-09-22 19:59:08 -0600938 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
939 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
940
941 add_e820ext(params, e820ext, nr_e820ext);
942 nr_entries -= nr_e820ext;
943 }
944
945 params->e820_entries = (u8)nr_entries;
946
947 return EFI_SUCCESS;
948}
949
950static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
951 u32 *e820ext_size)
952{
953 efi_status_t status;
954 unsigned long size;
955
956 size = sizeof(struct setup_data) +
957 sizeof(struct e820entry) * nr_desc;
958
959 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000960 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600961 *e820ext = NULL;
962 *e820ext_size = 0;
963 }
964
Matt Fleming204b0a12014-03-22 10:09:01 +0000965 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
966 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -0600967 if (status == EFI_SUCCESS)
968 *e820ext_size = size;
969
970 return status;
971}
972
Jeffrey Hugod6493402016-08-29 14:38:54 -0600973struct exit_boot_struct {
974 struct boot_params *boot_params;
975 struct efi_info *efi;
976 struct setup_data *e820ext;
977 __u32 e820ext_size;
978 bool is64;
979};
980
981static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
982 struct efi_boot_memmap *map,
983 void *priv)
984{
985 static bool first = true;
986 const char *signature;
987 __u32 nr_desc;
988 efi_status_t status;
989 struct exit_boot_struct *p = priv;
990
991 if (first) {
992 nr_desc = *map->buff_size / *map->desc_size;
993 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
994 u32 nr_e820ext = nr_desc -
995 ARRAY_SIZE(p->boot_params->e820_map);
996
997 status = alloc_e820ext(nr_e820ext, &p->e820ext,
998 &p->e820ext_size);
999 if (status != EFI_SUCCESS)
1000 return status;
1001 }
1002 first = false;
1003 }
1004
1005 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1006 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
1007
1008 p->efi->efi_systab = (unsigned long)sys_table_arg;
1009 p->efi->efi_memdesc_size = *map->desc_size;
1010 p->efi->efi_memdesc_version = *map->desc_ver;
1011 p->efi->efi_memmap = (unsigned long)*map->map;
1012 p->efi->efi_memmap_size = *map->map_size;
1013
1014#ifdef CONFIG_X86_64
1015 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
1016 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
1017#endif
1018
1019 return EFI_SUCCESS;
1020}
1021
Linn Crosettod2078d52013-09-22 19:59:08 -06001022static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001023 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -06001024{
Jeffrey Hugodadb57a2016-08-29 14:38:51 -06001025 unsigned long map_sz, key, desc_size, buff_size;
Linn Crosettod2078d52013-09-22 19:59:08 -06001026 efi_memory_desc_t *mem_map;
1027 struct setup_data *e820ext;
1028 __u32 e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -06001029 efi_status_t status;
1030 __u32 desc_version;
Jeffrey Hugodadb57a2016-08-29 14:38:51 -06001031 struct efi_boot_memmap map;
Jeffrey Hugod6493402016-08-29 14:38:54 -06001032 struct exit_boot_struct priv;
Linn Crosettod2078d52013-09-22 19:59:08 -06001033
Jeffrey Hugod6493402016-08-29 14:38:54 -06001034 map.map = &mem_map;
1035 map.map_size = &map_sz;
1036 map.desc_size = &desc_size;
1037 map.desc_ver = &desc_version;
1038 map.key_ptr = &key;
1039 map.buff_size = &buff_size;
1040 priv.boot_params = boot_params;
1041 priv.efi = &boot_params->efi_info;
1042 priv.e820ext = NULL;
1043 priv.e820ext_size = 0;
1044 priv.is64 = is64;
Linn Crosettod2078d52013-09-22 19:59:08 -06001045
Jeffrey Hugod6493402016-08-29 14:38:54 -06001046 /* Might as well exit boot services now */
1047 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
1048 exit_boot_func);
Linn Crosettod2078d52013-09-22 19:59:08 -06001049 if (status != EFI_SUCCESS)
1050 return status;
1051
Jeffrey Hugod6493402016-08-29 14:38:54 -06001052 e820ext = priv.e820ext;
1053 e820ext_size = priv.e820ext_size;
Linn Crosettod2078d52013-09-22 19:59:08 -06001054 /* Historic? */
1055 boot_params->alt_mem_k = 32 * 1024;
1056
1057 status = setup_e820(boot_params, e820ext, e820ext_size);
1058 if (status != EFI_SUCCESS)
1059 return status;
Matt Fleming291f3632011-12-12 21:27:52 +00001060
1061 return EFI_SUCCESS;
Matt Fleming291f3632011-12-12 21:27:52 +00001062}
1063
Matt Fleming9ca8f722012-07-19 10:23:48 +01001064/*
1065 * On success we return a pointer to a boot_params structure, and NULL
1066 * on failure.
1067 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001068struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +01001069 struct boot_params *boot_params)
1070{
Matt Fleming54b52d82014-01-10 15:27:14 +00001071 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001072 efi_loaded_image_t *image;
1073 struct setup_header *hdr = &boot_params->hdr;
1074 efi_status_t status;
1075 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +00001076 void *handle;
1077 efi_system_table_t *_table;
1078 bool is64;
1079
1080 efi_early = c;
1081
1082 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1083 handle = (void *)(unsigned long)efi_early->image_handle;
1084 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001085
1086 sys_table = _table;
1087
1088 /* Check if we were booted by the EFI firmware */
1089 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1090 goto fail;
1091
Matt Fleming54b52d82014-01-10 15:27:14 +00001092 if (is64)
1093 setup_boot_services64(efi_early);
1094 else
1095 setup_boot_services32(efi_early);
1096
Matt Fleming9ca8f722012-07-19 10:23:48 +01001097 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001098
Matt Fleming56394ab2014-09-11 09:04:25 +01001099 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001100
Matt Fleming204b0a12014-03-22 10:09:01 +00001101 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1102 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001103 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001104 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001105 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001106 }
Matt Fleming291f3632011-12-12 21:27:52 +00001107
1108 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001109 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001110 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001111 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001112 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001113 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001114 }
Matt Fleming291f3632011-12-12 21:27:52 +00001115
Matt Fleming9ca8f722012-07-19 10:23:48 +01001116 /*
1117 * If the kernel isn't already loaded at the preferred load
1118 * address, relocate it.
1119 */
1120 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001121 unsigned long bzimage_addr = hdr->code32_start;
1122 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1123 hdr->init_size, hdr->init_size,
1124 hdr->pref_address,
1125 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001126 if (status != EFI_SUCCESS) {
1127 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001128 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001129 }
Roy Franz4a9f3a72013-09-22 15:45:32 -07001130
1131 hdr->pref_address = hdr->code32_start;
1132 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001133 }
1134
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001135 status = exit_boot(boot_params, handle, is64);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001136 if (status != EFI_SUCCESS) {
1137 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001138 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001139 }
Matt Fleming291f3632011-12-12 21:27:52 +00001140
1141 memset((char *)gdt->address, 0x0, gdt->size);
1142 desc = (struct desc_struct *)gdt->address;
1143
1144 /* The first GDT is a dummy and the second is unused. */
1145 desc += 2;
1146
1147 desc->limit0 = 0xffff;
1148 desc->base0 = 0x0000;
1149 desc->base1 = 0x0000;
1150 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1151 desc->s = DESC_TYPE_CODE_DATA;
1152 desc->dpl = 0;
1153 desc->p = 1;
1154 desc->limit = 0xf;
1155 desc->avl = 0;
1156 desc->l = 0;
1157 desc->d = SEG_OP_SIZE_32BIT;
1158 desc->g = SEG_GRANULARITY_4KB;
1159 desc->base2 = 0x00;
1160
1161 desc++;
1162 desc->limit0 = 0xffff;
1163 desc->base0 = 0x0000;
1164 desc->base1 = 0x0000;
1165 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1166 desc->s = DESC_TYPE_CODE_DATA;
1167 desc->dpl = 0;
1168 desc->p = 1;
1169 desc->limit = 0xf;
1170 desc->avl = 0;
1171 desc->l = 0;
1172 desc->d = SEG_OP_SIZE_32BIT;
1173 desc->g = SEG_GRANULARITY_4KB;
1174 desc->base2 = 0x00;
1175
1176#ifdef CONFIG_X86_64
1177 /* Task segment value */
1178 desc++;
1179 desc->limit0 = 0x0000;
1180 desc->base0 = 0x0000;
1181 desc->base1 = 0x0000;
1182 desc->type = SEG_TYPE_TSS;
1183 desc->s = 0;
1184 desc->dpl = 0;
1185 desc->p = 1;
1186 desc->limit = 0x0;
1187 desc->avl = 0;
1188 desc->l = 0;
1189 desc->d = 0;
1190 desc->g = SEG_GRANULARITY_4KB;
1191 desc->base2 = 0x00;
1192#endif /* CONFIG_X86_64 */
1193
Matt Fleming291f3632011-12-12 21:27:52 +00001194 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001195 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001196
1197 return boot_params;
1198fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001199 efi_printk(sys_table, "efi_main() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001200 return NULL;
1201}