blob: 1acf605a646dd87830f22694991142415c6ff351 [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
Matt Fleming0f905a42012-11-20 13:07:46 +000016#undef memcpy /* Use memcpy from misc.c */
17
Matt Fleming291f3632011-12-12 21:27:52 +000018#include "eboot.h"
19
20static efi_system_table_t *sys_table;
21
Matt Fleming84be8802014-09-23 10:37:43 +010022static struct efi_config *efi_early;
23
24#define efi_call_early(f, ...) \
25 efi_early->call(efi_early->f, __VA_ARGS__);
Matt Fleming204b0a12014-03-22 10:09:01 +000026
Matt Fleming54b52d82014-01-10 15:27:14 +000027#define BOOT_SERVICES(bits) \
28static void setup_boot_services##bits(struct efi_config *c) \
29{ \
30 efi_system_table_##bits##_t *table; \
31 efi_boot_services_##bits##_t *bt; \
32 \
33 table = (typeof(table))sys_table; \
34 \
35 c->text_output = table->con_out; \
36 \
37 bt = (typeof(bt))(unsigned long)(table->boottime); \
38 \
39 c->allocate_pool = bt->allocate_pool; \
40 c->allocate_pages = bt->allocate_pages; \
41 c->get_memory_map = bt->get_memory_map; \
42 c->free_pool = bt->free_pool; \
43 c->free_pages = bt->free_pages; \
44 c->locate_handle = bt->locate_handle; \
45 c->handle_protocol = bt->handle_protocol; \
46 c->exit_boot_services = bt->exit_boot_services; \
47}
48BOOT_SERVICES(32);
49BOOT_SERVICES(64);
50
Ard Biesheuvelbd669472014-07-02 14:54:42 +020051void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
Matt Fleming54b52d82014-01-10 15:27:14 +000052
53static efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +000054__file_size32(void *__fh, efi_char16_t *filename_16,
55 void **handle, u64 *file_sz)
Matt Fleming54b52d82014-01-10 15:27:14 +000056{
Matt Flemingc116e8d2014-01-16 11:35:43 +000057 efi_file_handle_32_t *h, *fh = __fh;
Matt Fleming54b52d82014-01-10 15:27:14 +000058 efi_file_info_t *info;
59 efi_status_t status;
60 efi_guid_t info_guid = EFI_FILE_INFO_ID;
61 u32 info_sz;
62
63 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
64 EFI_FILE_MODE_READ, (u64)0);
65 if (status != EFI_SUCCESS) {
66 efi_printk(sys_table, "Failed to open file: ");
67 efi_char16_printk(sys_table, filename_16);
68 efi_printk(sys_table, "\n");
69 return status;
70 }
71
72 *handle = h;
73
74 info_sz = 0;
75 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
76 &info_sz, NULL);
77 if (status != EFI_BUFFER_TOO_SMALL) {
78 efi_printk(sys_table, "Failed to get file info size\n");
79 return status;
80 }
81
82grow:
Matt Fleming204b0a12014-03-22 10:09:01 +000083 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
84 info_sz, (void **)&info);
Matt Fleming54b52d82014-01-10 15:27:14 +000085 if (status != EFI_SUCCESS) {
86 efi_printk(sys_table, "Failed to alloc mem for file info\n");
87 return status;
88 }
89
90 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
91 &info_sz, info);
92 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +000093 efi_call_early(free_pool, info);
Matt Fleming54b52d82014-01-10 15:27:14 +000094 goto grow;
95 }
96
97 *file_sz = info->file_size;
Matt Fleming204b0a12014-03-22 10:09:01 +000098 efi_call_early(free_pool, info);
Matt Fleming54b52d82014-01-10 15:27:14 +000099
100 if (status != EFI_SUCCESS)
101 efi_printk(sys_table, "Failed to get initrd info\n");
102
103 return status;
104}
105
Matt Flemingc116e8d2014-01-16 11:35:43 +0000106static efi_status_t
107__file_size64(void *__fh, efi_char16_t *filename_16,
108 void **handle, u64 *file_sz)
109{
110 efi_file_handle_64_t *h, *fh = __fh;
111 efi_file_info_t *info;
112 efi_status_t status;
113 efi_guid_t info_guid = EFI_FILE_INFO_ID;
Matt Fleming396f1a02014-04-10 13:30:13 +0100114 u64 info_sz;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000115
116 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
117 EFI_FILE_MODE_READ, (u64)0);
118 if (status != EFI_SUCCESS) {
119 efi_printk(sys_table, "Failed to open file: ");
120 efi_char16_printk(sys_table, filename_16);
121 efi_printk(sys_table, "\n");
122 return status;
123 }
124
125 *handle = h;
126
127 info_sz = 0;
128 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
129 &info_sz, NULL);
130 if (status != EFI_BUFFER_TOO_SMALL) {
131 efi_printk(sys_table, "Failed to get file info size\n");
132 return status;
133 }
134
135grow:
Matt Fleming204b0a12014-03-22 10:09:01 +0000136 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
137 info_sz, (void **)&info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000138 if (status != EFI_SUCCESS) {
139 efi_printk(sys_table, "Failed to alloc mem for file info\n");
140 return status;
141 }
142
143 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
144 &info_sz, info);
145 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000146 efi_call_early(free_pool, info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000147 goto grow;
148 }
149
150 *file_sz = info->file_size;
Matt Fleming204b0a12014-03-22 10:09:01 +0000151 efi_call_early(free_pool, info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000152
153 if (status != EFI_SUCCESS)
154 efi_printk(sys_table, "Failed to get initrd info\n");
155
156 return status;
157}
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200158efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +0000159efi_file_size(efi_system_table_t *sys_table, void *__fh,
160 efi_char16_t *filename_16, void **handle, u64 *file_sz)
161{
162 if (efi_early->is64)
163 return __file_size64(__fh, filename_16, handle, file_sz);
164
165 return __file_size32(__fh, filename_16, handle, file_sz);
166}
167
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200168efi_status_t
Matt Fleming47514c92014-04-10 14:11:45 +0100169efi_file_read(void *handle, unsigned long *size, void *addr)
Matt Fleming54b52d82014-01-10 15:27:14 +0000170{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000171 unsigned long func;
172
173 if (efi_early->is64) {
Matt Fleming47514c92014-04-10 14:11:45 +0100174 efi_file_handle_64_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000175
176 func = (unsigned long)fh->read;
177 return efi_early->call(func, handle, size, addr);
178 } else {
Matt Fleming47514c92014-04-10 14:11:45 +0100179 efi_file_handle_32_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000180
181 func = (unsigned long)fh->read;
182 return efi_early->call(func, handle, size, addr);
183 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000184}
185
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200186efi_status_t efi_file_close(void *handle)
Matt Fleming54b52d82014-01-10 15:27:14 +0000187{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000188 if (efi_early->is64) {
Matt Fleming47514c92014-04-10 14:11:45 +0100189 efi_file_handle_64_t *fh = handle;
Matt Fleming54b52d82014-01-10 15:27:14 +0000190
Matt Flemingc116e8d2014-01-16 11:35:43 +0000191 return efi_early->call((unsigned long)fh->close, handle);
192 } else {
Matt Fleming47514c92014-04-10 14:11:45 +0100193 efi_file_handle_32_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000194
195 return efi_early->call((unsigned long)fh->close, handle);
196 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000197}
198
Matt Flemingc116e8d2014-01-16 11:35:43 +0000199static inline efi_status_t __open_volume32(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000200{
201 efi_file_io_interface_t *io;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000202 efi_loaded_image_32_t *image = __image;
203 efi_file_handle_32_t *fh;
Matt Fleming54b52d82014-01-10 15:27:14 +0000204 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
205 efi_status_t status;
206 void *handle = (void *)(unsigned long)image->device_handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000207 unsigned long func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000208
Matt Fleming204b0a12014-03-22 10:09:01 +0000209 status = efi_call_early(handle_protocol, handle,
210 &fs_proto, (void **)&io);
Matt Fleming54b52d82014-01-10 15:27:14 +0000211 if (status != EFI_SUCCESS) {
212 efi_printk(sys_table, "Failed to handle fs_proto\n");
213 return status;
214 }
215
216 func = (unsigned long)io->open_volume;
217 status = efi_early->call(func, io, &fh);
218 if (status != EFI_SUCCESS)
219 efi_printk(sys_table, "Failed to open volume\n");
220
221 *__fh = fh;
222 return status;
223}
224
Matt Flemingc116e8d2014-01-16 11:35:43 +0000225static inline efi_status_t __open_volume64(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000226{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000227 efi_file_io_interface_t *io;
228 efi_loaded_image_64_t *image = __image;
229 efi_file_handle_64_t *fh;
230 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
231 efi_status_t status;
232 void *handle = (void *)(unsigned long)image->device_handle;
233 unsigned long func;
234
Matt Fleming204b0a12014-03-22 10:09:01 +0000235 status = efi_call_early(handle_protocol, handle,
236 &fs_proto, (void **)&io);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000237 if (status != EFI_SUCCESS) {
238 efi_printk(sys_table, "Failed to handle fs_proto\n");
239 return status;
240 }
241
242 func = (unsigned long)io->open_volume;
243 status = efi_early->call(func, io, &fh);
244 if (status != EFI_SUCCESS)
245 efi_printk(sys_table, "Failed to open volume\n");
246
247 *__fh = fh;
248 return status;
249}
250
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200251efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +0000252efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
253{
254 if (efi_early->is64)
255 return __open_volume64(__image, __fh);
256
257 return __open_volume32(__image, __fh);
258}
259
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200260void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000261{
Matt Fleming54b52d82014-01-10 15:27:14 +0000262 unsigned long output_string;
263 size_t offset;
Matt Fleming54b52d82014-01-10 15:27:14 +0000264
Matt Flemingc116e8d2014-01-16 11:35:43 +0000265 if (efi_early->is64) {
266 struct efi_simple_text_output_protocol_64 *out;
267 u64 *func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000268
Matt Flemingc116e8d2014-01-16 11:35:43 +0000269 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 = (u64 *)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 } else {
276 struct efi_simple_text_output_protocol_32 *out;
277 u32 *func;
278
279 offset = offsetof(typeof(*out), output_string);
280 output_string = efi_early->text_output + offset;
Matt Fleming115c6622014-09-24 11:56:10 +0100281 out = (typeof(out))(unsigned long)efi_early->text_output;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000282 func = (u32 *)output_string;
283
Matt Fleming115c6622014-09-24 11:56:10 +0100284 efi_early->call(*func, out, str);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000285 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000286}
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800287
Matt Fleming84be8802014-09-23 10:37:43 +0100288#include "../../../../drivers/firmware/efi/libstub/efi-stub-helper.c"
289
Matt Fleming291f3632011-12-12 21:27:52 +0000290static void find_bits(unsigned long mask, u8 *pos, u8 *size)
291{
292 u8 first, len;
293
294 first = 0;
295 len = 0;
296
297 if (mask) {
298 while (!(mask & 0x1)) {
299 mask = mask >> 1;
300 first++;
301 }
302
303 while (mask & 0x1) {
304 mask = mask >> 1;
305 len++;
306 }
307 }
308
309 *pos = first;
310 *size = len;
311}
312
Matt Flemingc116e8d2014-01-16 11:35:43 +0000313static efi_status_t
314__setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700315{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000316 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700317 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000318 unsigned long size;
319 uint64_t attributes;
320
321 status = efi_early->call(pci->attributes, pci,
322 EfiPciIoAttributeOperationGet, 0, 0,
323 &attributes);
324 if (status != EFI_SUCCESS)
325 return status;
326
327 if (!pci->romimage || !pci->romsize)
328 return EFI_INVALID_PARAMETER;
329
330 size = pci->romsize + sizeof(*rom);
331
Matt Fleming204b0a12014-03-22 10:09:01 +0000332 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200333 if (status != EFI_SUCCESS) {
334 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000335 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200336 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000337
338 memset(rom, 0, sizeof(*rom));
339
340 rom->data.type = SETUP_PCI;
341 rom->data.len = size - sizeof(struct setup_data);
342 rom->data.next = 0;
343 rom->pcilen = pci->romsize;
344 *__rom = rom;
345
346 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
347 PCI_VENDOR_ID, 1, &(rom->vendor));
348
Andre Müller77e21e82014-09-10 01:00:22 +0200349 if (status != EFI_SUCCESS) {
350 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000351 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200352 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000353
354 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
355 PCI_DEVICE_ID, 1, &(rom->devid));
356
Andre Müller77e21e82014-09-10 01:00:22 +0200357 if (status != EFI_SUCCESS) {
358 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000359 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200360 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000361
362 status = efi_early->call(pci->get_location, pci, &(rom->segment),
363 &(rom->bus), &(rom->device), &(rom->function));
364
365 if (status != EFI_SUCCESS)
366 goto free_struct;
367
368 memcpy(rom->romdata, pci->romimage, pci->romsize);
369 return status;
370
371free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000372 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000373 return status;
374}
375
Matt Fleming56394ab2014-09-11 09:04:25 +0100376static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000377setup_efi_pci32(struct boot_params *params, void **pci_handle,
378 unsigned long size)
379{
380 efi_pci_io_protocol_32 *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700381 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000382 u32 *handles = (u32 *)(unsigned long)pci_handle;
383 efi_status_t status;
384 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700385 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000386 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700387
Jan Beulichbc754792013-01-18 12:35:14 +0000388 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700389
390 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000391 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700392
Matt Flemingc116e8d2014-01-16 11:35:43 +0000393 nr_pci = size / sizeof(u32);
394 for (i = 0; i < nr_pci; i++) {
395 struct pci_setup_rom *rom = NULL;
396 u32 h = handles[i];
397
Matt Fleming204b0a12014-03-22 10:09:01 +0000398 status = efi_call_early(handle_protocol, h,
399 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000400
401 if (status != EFI_SUCCESS)
402 continue;
403
404 if (!pci)
405 continue;
406
407 status = __setup_efi_pci32(pci, &rom);
408 if (status != EFI_SUCCESS)
409 continue;
410
411 if (data)
412 data->next = (unsigned long)rom;
413 else
414 params->hdr.setup_data = (unsigned long)rom;
415
416 data = (struct setup_data *)rom;
417
418 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000419}
420
421static efi_status_t
422__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
423{
424 struct pci_setup_rom *rom;
425 efi_status_t status;
426 unsigned long size;
427 uint64_t attributes;
428
429 status = efi_early->call(pci->attributes, pci,
430 EfiPciIoAttributeOperationGet, 0,
431 &attributes);
432 if (status != EFI_SUCCESS)
433 return status;
434
435 if (!pci->romimage || !pci->romsize)
436 return EFI_INVALID_PARAMETER;
437
438 size = pci->romsize + sizeof(*rom);
439
Matt Fleming204b0a12014-03-22 10:09:01 +0000440 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200441 if (status != EFI_SUCCESS) {
442 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000443 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200444 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000445
446 rom->data.type = SETUP_PCI;
447 rom->data.len = size - sizeof(struct setup_data);
448 rom->data.next = 0;
449 rom->pcilen = pci->romsize;
450 *__rom = rom;
451
452 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
453 PCI_VENDOR_ID, 1, &(rom->vendor));
454
Andre Müller77e21e82014-09-10 01:00:22 +0200455 if (status != EFI_SUCCESS) {
456 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000457 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200458 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000459
460 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
461 PCI_DEVICE_ID, 1, &(rom->devid));
462
Andre Müller77e21e82014-09-10 01:00:22 +0200463 if (status != EFI_SUCCESS) {
464 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000465 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200466 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000467
468 status = efi_early->call(pci->get_location, pci, &(rom->segment),
469 &(rom->bus), &(rom->device), &(rom->function));
470
471 if (status != EFI_SUCCESS)
472 goto free_struct;
473
474 memcpy(rom->romdata, pci->romimage, pci->romsize);
475 return status;
476
477free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000478 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000479 return status;
480
481}
482
Matt Fleming56394ab2014-09-11 09:04:25 +0100483static void
Matt Flemingc116e8d2014-01-16 11:35:43 +0000484setup_efi_pci64(struct boot_params *params, void **pci_handle,
485 unsigned long size)
486{
487 efi_pci_io_protocol_64 *pci = NULL;
488 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
489 u64 *handles = (u64 *)(unsigned long)pci_handle;
490 efi_status_t status;
491 unsigned long nr_pci;
492 struct setup_data *data;
493 int i;
494
495 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
496
497 while (data && data->next)
498 data = (struct setup_data *)(unsigned long)data->next;
499
500 nr_pci = size / sizeof(u64);
501 for (i = 0; i < nr_pci; i++) {
502 struct pci_setup_rom *rom = NULL;
503 u64 h = handles[i];
504
Matt Fleming204b0a12014-03-22 10:09:01 +0000505 status = efi_call_early(handle_protocol, h,
506 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000507
508 if (status != EFI_SUCCESS)
509 continue;
510
511 if (!pci)
512 continue;
513
514 status = __setup_efi_pci64(pci, &rom);
515 if (status != EFI_SUCCESS)
516 continue;
517
518 if (data)
519 data->next = (unsigned long)rom;
520 else
521 params->hdr.setup_data = (unsigned long)rom;
522
523 data = (struct setup_data *)rom;
524
525 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000526}
527
Matt Fleming56394ab2014-09-11 09:04:25 +0100528/*
529 * There's no way to return an informative status from this function,
530 * because any analysis (and printing of error messages) needs to be
531 * done directly at the EFI function call-site.
532 *
533 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
534 * just didn't find any PCI devices, but there's no way to tell outside
535 * the context of the call.
536 */
537static void setup_efi_pci(struct boot_params *params)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000538{
539 efi_status_t status;
540 void **pci_handle = NULL;
541 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
542 unsigned long size = 0;
543
Matt Fleming204b0a12014-03-22 10:09:01 +0000544 status = efi_call_early(locate_handle,
545 EFI_LOCATE_BY_PROTOCOL,
546 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700547
548 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000549 status = efi_call_early(allocate_pool,
550 EFI_LOADER_DATA,
551 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700552
Andre Müller77e21e82014-09-10 01:00:22 +0200553 if (status != EFI_SUCCESS) {
554 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
Matt Fleming56394ab2014-09-11 09:04:25 +0100555 return;
Andre Müller77e21e82014-09-10 01:00:22 +0200556 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700557
Matt Fleming204b0a12014-03-22 10:09:01 +0000558 status = efi_call_early(locate_handle,
559 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
560 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700561 }
562
563 if (status != EFI_SUCCESS)
564 goto free_handle;
565
Matt Flemingc116e8d2014-01-16 11:35:43 +0000566 if (efi_early->is64)
Matt Fleming56394ab2014-09-11 09:04:25 +0100567 setup_efi_pci64(params, pci_handle, size);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000568 else
Matt Fleming56394ab2014-09-11 09:04:25 +0100569 setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700570
571free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000572 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700573}
574
Matt Flemingc116e8d2014-01-16 11:35:43 +0000575static void
576setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
577 struct efi_pixel_bitmask pixel_info, int pixel_format)
Matt Fleming291f3632011-12-12 21:27:52 +0000578{
Matt Fleming291f3632011-12-12 21:27:52 +0000579 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
580 si->lfb_depth = 32;
581 si->lfb_linelength = pixels_per_scan_line * 4;
582 si->red_size = 8;
583 si->red_pos = 0;
584 si->green_size = 8;
585 si->green_pos = 8;
586 si->blue_size = 8;
587 si->blue_pos = 16;
588 si->rsvd_size = 8;
589 si->rsvd_pos = 24;
590 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
591 si->lfb_depth = 32;
592 si->lfb_linelength = pixels_per_scan_line * 4;
593 si->red_size = 8;
594 si->red_pos = 16;
595 si->green_size = 8;
596 si->green_pos = 8;
597 si->blue_size = 8;
598 si->blue_pos = 0;
599 si->rsvd_size = 8;
600 si->rsvd_pos = 24;
601 } else if (pixel_format == PIXEL_BIT_MASK) {
602 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
603 find_bits(pixel_info.green_mask, &si->green_pos,
604 &si->green_size);
605 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
606 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
607 &si->rsvd_size);
608 si->lfb_depth = si->red_size + si->green_size +
609 si->blue_size + si->rsvd_size;
610 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
611 } else {
612 si->lfb_depth = 4;
613 si->lfb_linelength = si->lfb_width / 2;
614 si->red_size = 0;
615 si->red_pos = 0;
616 si->green_size = 0;
617 si->green_pos = 0;
618 si->blue_size = 0;
619 si->blue_pos = 0;
620 si->rsvd_size = 0;
621 si->rsvd_pos = 0;
622 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000623}
624
625static efi_status_t
626__gop_query32(struct efi_graphics_output_protocol_32 *gop32,
627 struct efi_graphics_output_mode_info **info,
628 unsigned long *size, u32 *fb_base)
629{
630 struct efi_graphics_output_protocol_mode_32 *mode;
631 efi_status_t status;
632 unsigned long m;
633
634 m = gop32->mode;
635 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
636
637 status = efi_early->call(gop32->query_mode, gop32,
638 mode->mode, size, info);
639 if (status != EFI_SUCCESS)
640 return status;
641
642 *fb_base = mode->frame_buffer_base;
643 return status;
644}
645
646static efi_status_t
647setup_gop32(struct screen_info *si, efi_guid_t *proto,
648 unsigned long size, void **gop_handle)
649{
650 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
651 unsigned long nr_gops;
652 u16 width, height;
653 u32 pixels_per_scan_line;
654 u32 fb_base;
655 struct efi_pixel_bitmask pixel_info;
656 int pixel_format;
657 efi_status_t status;
658 u32 *handles = (u32 *)(unsigned long)gop_handle;
659 int i;
660
661 first_gop = NULL;
662 gop32 = NULL;
663
664 nr_gops = size / sizeof(u32);
665 for (i = 0; i < nr_gops; i++) {
666 struct efi_graphics_output_mode_info *info = NULL;
667 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
668 bool conout_found = false;
669 void *dummy = NULL;
670 u32 h = handles[i];
671
Matt Fleming204b0a12014-03-22 10:09:01 +0000672 status = efi_call_early(handle_protocol, h,
673 proto, (void **)&gop32);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000674 if (status != EFI_SUCCESS)
675 continue;
676
Matt Fleming204b0a12014-03-22 10:09:01 +0000677 status = efi_call_early(handle_protocol, h,
678 &conout_proto, &dummy);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000679 if (status == EFI_SUCCESS)
680 conout_found = true;
681
682 status = __gop_query32(gop32, &info, &size, &fb_base);
683 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
684 /*
685 * Systems that use the UEFI Console Splitter may
686 * provide multiple GOP devices, not all of which are
687 * backed by real hardware. The workaround is to search
688 * for a GOP implementing the ConOut protocol, and if
689 * one isn't found, to just fall back to the first GOP.
690 */
691 width = info->horizontal_resolution;
692 height = info->vertical_resolution;
693 pixel_format = info->pixel_format;
694 pixel_info = info->pixel_information;
695 pixels_per_scan_line = info->pixels_per_scan_line;
696
697 /*
698 * Once we've found a GOP supporting ConOut,
699 * don't bother looking any further.
700 */
701 first_gop = gop32;
702 if (conout_found)
703 break;
704 }
705 }
706
707 /* Did we find any GOPs? */
708 if (!first_gop)
709 goto out;
710
711 /* EFI framebuffer */
712 si->orig_video_isVGA = VIDEO_TYPE_EFI;
713
714 si->lfb_width = width;
715 si->lfb_height = height;
716 si->lfb_base = fb_base;
717 si->pages = 1;
718
719 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
Matt Fleming291f3632011-12-12 21:27:52 +0000720
Matthew Garrette9b10952012-07-27 17:20:49 -0400721 si->lfb_size = si->lfb_linelength * si->lfb_height;
722
Matthew Garrettf462ed92012-07-27 12:58:53 -0400723 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000724out:
725 return status;
726}
727
728static efi_status_t
729__gop_query64(struct efi_graphics_output_protocol_64 *gop64,
730 struct efi_graphics_output_mode_info **info,
731 unsigned long *size, u32 *fb_base)
732{
733 struct efi_graphics_output_protocol_mode_64 *mode;
734 efi_status_t status;
735 unsigned long m;
736
737 m = gop64->mode;
738 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
739
740 status = efi_early->call(gop64->query_mode, gop64,
741 mode->mode, size, info);
742 if (status != EFI_SUCCESS)
743 return status;
744
745 *fb_base = mode->frame_buffer_base;
746 return status;
747}
748
749static efi_status_t
750setup_gop64(struct screen_info *si, efi_guid_t *proto,
751 unsigned long size, void **gop_handle)
752{
753 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
754 unsigned long nr_gops;
755 u16 width, height;
756 u32 pixels_per_scan_line;
757 u32 fb_base;
758 struct efi_pixel_bitmask pixel_info;
759 int pixel_format;
760 efi_status_t status;
761 u64 *handles = (u64 *)(unsigned long)gop_handle;
762 int i;
763
764 first_gop = NULL;
765 gop64 = NULL;
766
767 nr_gops = size / sizeof(u64);
768 for (i = 0; i < nr_gops; i++) {
769 struct efi_graphics_output_mode_info *info = NULL;
770 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
771 bool conout_found = false;
772 void *dummy = NULL;
773 u64 h = handles[i];
774
Matt Fleming204b0a12014-03-22 10:09:01 +0000775 status = efi_call_early(handle_protocol, h,
776 proto, (void **)&gop64);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000777 if (status != EFI_SUCCESS)
778 continue;
779
Matt Fleming204b0a12014-03-22 10:09:01 +0000780 status = efi_call_early(handle_protocol, h,
781 &conout_proto, &dummy);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000782 if (status == EFI_SUCCESS)
783 conout_found = true;
784
785 status = __gop_query64(gop64, &info, &size, &fb_base);
786 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
787 /*
788 * Systems that use the UEFI Console Splitter may
789 * provide multiple GOP devices, not all of which are
790 * backed by real hardware. The workaround is to search
791 * for a GOP implementing the ConOut protocol, and if
792 * one isn't found, to just fall back to the first GOP.
793 */
794 width = info->horizontal_resolution;
795 height = info->vertical_resolution;
796 pixel_format = info->pixel_format;
797 pixel_info = info->pixel_information;
798 pixels_per_scan_line = info->pixels_per_scan_line;
799
800 /*
801 * Once we've found a GOP supporting ConOut,
802 * don't bother looking any further.
803 */
804 first_gop = gop64;
805 if (conout_found)
806 break;
807 }
808 }
809
810 /* Did we find any GOPs? */
811 if (!first_gop)
812 goto out;
813
814 /* EFI framebuffer */
815 si->orig_video_isVGA = VIDEO_TYPE_EFI;
816
817 si->lfb_width = width;
818 si->lfb_height = height;
819 si->lfb_base = fb_base;
820 si->pages = 1;
821
822 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
823
824 si->lfb_size = si->lfb_linelength * si->lfb_height;
825
826 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
827out:
828 return status;
829}
830
831/*
832 * See if we have Graphics Output Protocol
833 */
834static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
835 unsigned long size)
836{
837 efi_status_t status;
838 void **gop_handle = NULL;
839
Matt Fleming204b0a12014-03-22 10:09:01 +0000840 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
841 size, (void **)&gop_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000842 if (status != EFI_SUCCESS)
843 return status;
844
Matt Fleming204b0a12014-03-22 10:09:01 +0000845 status = efi_call_early(locate_handle,
846 EFI_LOCATE_BY_PROTOCOL,
847 proto, NULL, &size, gop_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000848 if (status != EFI_SUCCESS)
849 goto free_handle;
850
851 if (efi_early->is64)
852 status = setup_gop64(si, proto, size, gop_handle);
853 else
854 status = setup_gop32(si, proto, size, gop_handle);
Matthew Garrettf462ed92012-07-27 12:58:53 -0400855
Matt Fleming291f3632011-12-12 21:27:52 +0000856free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000857 efi_call_early(free_pool, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000858 return status;
859}
860
Matt Flemingc116e8d2014-01-16 11:35:43 +0000861static efi_status_t
862setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000863{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000864 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
865 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000866 unsigned long nr_ugas;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000867 u32 *handles = (u32 *)uga_handle;;
Matt Fleming291f3632011-12-12 21:27:52 +0000868 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000869 int i;
870
Matt Fleming291f3632011-12-12 21:27:52 +0000871 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000872 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000873 for (i = 0; i < nr_ugas; i++) {
874 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000875 u32 w, h, depth, refresh;
876 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000877 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000878
Matt Fleming204b0a12014-03-22 10:09:01 +0000879 status = efi_call_early(handle_protocol, handle,
880 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000881 if (status != EFI_SUCCESS)
882 continue;
883
Matt Fleming204b0a12014-03-22 10:09:01 +0000884 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000885
Matt Fleming54b52d82014-01-10 15:27:14 +0000886 status = efi_early->call((unsigned long)uga->get_mode, uga,
887 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000888 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000889 *width = w;
890 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000891
892 /*
893 * Once we've found a UGA supporting PCIIO,
894 * don't bother looking any further.
895 */
896 if (pciio)
897 break;
898
899 first_uga = uga;
900 }
901 }
902
Matt Flemingc116e8d2014-01-16 11:35:43 +0000903 return status;
904}
905
906static efi_status_t
907setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
908{
909 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
910 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
911 unsigned long nr_ugas;
912 u64 *handles = (u64 *)uga_handle;;
913 efi_status_t status;
914 int i;
915
916 first_uga = NULL;
917 nr_ugas = size / sizeof(u64);
918 for (i = 0; i < nr_ugas; i++) {
919 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
920 u32 w, h, depth, refresh;
921 void *pciio;
922 u64 handle = handles[i];
923
Matt Fleming204b0a12014-03-22 10:09:01 +0000924 status = efi_call_early(handle_protocol, handle,
925 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000926 if (status != EFI_SUCCESS)
927 continue;
928
Matt Fleming204b0a12014-03-22 10:09:01 +0000929 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000930
931 status = efi_early->call((unsigned long)uga->get_mode, uga,
932 &w, &h, &depth, &refresh);
933 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
934 *width = w;
935 *height = h;
936
937 /*
938 * Once we've found a UGA supporting PCIIO,
939 * don't bother looking any further.
940 */
941 if (pciio)
942 break;
943
944 first_uga = uga;
945 }
946 }
947
948 return status;
949}
950
951/*
952 * See if we have Universal Graphics Adapter (UGA) protocol
953 */
954static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
955 unsigned long size)
956{
957 efi_status_t status;
958 u32 width, height;
959 void **uga_handle = NULL;
960
Matt Fleming204b0a12014-03-22 10:09:01 +0000961 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
962 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000963 if (status != EFI_SUCCESS)
964 return status;
965
Matt Fleming204b0a12014-03-22 10:09:01 +0000966 status = efi_call_early(locate_handle,
967 EFI_LOCATE_BY_PROTOCOL,
968 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000969 if (status != EFI_SUCCESS)
970 goto free_handle;
971
972 height = 0;
973 width = 0;
974
975 if (efi_early->is64)
976 status = setup_uga64(uga_handle, size, &width, &height);
977 else
978 status = setup_uga32(uga_handle, size, &width, &height);
979
980 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000981 goto free_handle;
982
983 /* EFI framebuffer */
984 si->orig_video_isVGA = VIDEO_TYPE_EFI;
985
986 si->lfb_depth = 32;
987 si->lfb_width = width;
988 si->lfb_height = height;
989
990 si->red_size = 8;
991 si->red_pos = 16;
992 si->green_size = 8;
993 si->green_pos = 8;
994 si->blue_size = 8;
995 si->blue_pos = 0;
996 si->rsvd_size = 8;
997 si->rsvd_pos = 24;
998
Matt Fleming291f3632011-12-12 21:27:52 +0000999free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +00001000 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001001 return status;
1002}
1003
1004void setup_graphics(struct boot_params *boot_params)
1005{
1006 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
1007 struct screen_info *si;
1008 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
1009 efi_status_t status;
1010 unsigned long size;
1011 void **gop_handle = NULL;
1012 void **uga_handle = NULL;
1013
1014 si = &boot_params->screen_info;
1015 memset(si, 0, sizeof(*si));
1016
1017 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +00001018 status = efi_call_early(locate_handle,
1019 EFI_LOCATE_BY_PROTOCOL,
1020 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001021 if (status == EFI_BUFFER_TOO_SMALL)
1022 status = setup_gop(si, &graphics_proto, size);
1023
1024 if (status != EFI_SUCCESS) {
1025 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +00001026 status = efi_call_early(locate_handle,
1027 EFI_LOCATE_BY_PROTOCOL,
1028 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001029 if (status == EFI_BUFFER_TOO_SMALL)
1030 setup_uga(si, &uga_proto, size);
1031 }
1032}
1033
Matt Fleming291f3632011-12-12 21:27:52 +00001034/*
1035 * Because the x86 boot code expects to be passed a boot_params we
1036 * need to create one ourselves (usually the bootloader would create
1037 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +01001038 *
1039 * The caller is responsible for filling out ->code32_start in the
1040 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +00001041 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001042struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +00001043{
Matt Fleming9ca8f722012-07-19 10:23:48 +01001044 struct boot_params *boot_params;
1045 struct sys_desc_table *sdt;
1046 struct apm_bios_info *bi;
1047 struct setup_header *hdr;
1048 struct efi_info *efi;
1049 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +00001050 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001051 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +00001052 int options_size = 0;
1053 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -07001054 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001055 u16 *s2;
1056 u8 *s1;
1057 int i;
Roy Franz46f45822013-09-22 15:45:39 -07001058 unsigned long ramdisk_addr;
1059 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +00001060
Matt Fleming54b52d82014-01-10 15:27:14 +00001061 efi_early = c;
1062 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1063 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001064
1065 /* Check if we were booted by the EFI firmware */
1066 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1067 return NULL;
1068
Matt Fleming54b52d82014-01-10 15:27:14 +00001069 if (efi_early->is64)
1070 setup_boot_services64(efi_early);
1071 else
1072 setup_boot_services32(efi_early);
1073
Matt Fleming204b0a12014-03-22 10:09:01 +00001074 status = efi_call_early(handle_protocol, handle,
1075 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001076 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001077 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001078 return NULL;
1079 }
1080
Roy Franz40e45302013-09-22 15:45:29 -07001081 status = efi_low_alloc(sys_table, 0x4000, 1,
1082 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001083 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001084 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001085 return NULL;
1086 }
1087
1088 memset(boot_params, 0x0, 0x4000);
1089
1090 hdr = &boot_params->hdr;
1091 efi = &boot_params->efi_info;
1092 bi = &boot_params->apm_bios_info;
1093 sdt = &boot_params->sys_desc_table;
1094
1095 /* Copy the second sector to boot_params */
1096 memcpy(&hdr->jump, image->image_base + 512, 512);
1097
1098 /*
1099 * Fill out some of the header fields ourselves because the
1100 * EFI firmware loader doesn't load the first sector.
1101 */
1102 hdr->root_flags = 1;
1103 hdr->vid_mode = 0xffff;
1104 hdr->boot_flag = 0xAA55;
1105
Matt Fleming291f3632011-12-12 21:27:52 +00001106 hdr->type_of_loader = 0x21;
1107
1108 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -05001109 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -07001110 if (!cmdline_ptr)
1111 goto fail;
1112 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001113
1114 hdr->ramdisk_image = 0;
1115 hdr->ramdisk_size = 0;
1116
Matt Fleming291f3632011-12-12 21:27:52 +00001117 /* Clear APM BIOS info */
1118 memset(bi, 0, sizeof(*bi));
1119
1120 memset(sdt, 0, sizeof(*sdt));
1121
Matt Fleming5a17dae2014-08-05 11:52:11 +01001122 status = efi_parse_options(cmdline_ptr);
1123 if (status != EFI_SUCCESS)
1124 goto fail2;
1125
Roy Franz46f45822013-09-22 15:45:39 -07001126 status = handle_cmdline_files(sys_table, image,
1127 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu47226ad2014-09-03 21:50:07 -07001128 "initrd=", hdr->initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -07001129 &ramdisk_addr, &ramdisk_size);
Yinghai Lu47226ad2014-09-03 21:50:07 -07001130
1131 if (status != EFI_SUCCESS &&
1132 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
1133 efi_printk(sys_table, "Trying to load files to higher address\n");
1134 status = handle_cmdline_files(sys_table, image,
1135 (char *)(unsigned long)hdr->cmd_line_ptr,
1136 "initrd=", -1UL,
1137 &ramdisk_addr, &ramdisk_size);
1138 }
1139
Matt Fleming9ca8f722012-07-19 10:23:48 +01001140 if (status != EFI_SUCCESS)
1141 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -07001142 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1143 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
1144 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1145 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001146
1147 return boot_params;
1148fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -07001149 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001150fail:
Roy Franz40e45302013-09-22 15:45:29 -07001151 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001152 return NULL;
1153}
1154
Linn Crosettod2078d52013-09-22 19:59:08 -06001155static void add_e820ext(struct boot_params *params,
1156 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +01001157{
Linn Crosettod2078d52013-09-22 19:59:08 -06001158 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001159 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -06001160 unsigned long size;
1161
1162 e820ext->type = SETUP_E820_EXT;
1163 e820ext->len = nr_entries * sizeof(struct e820entry);
1164 e820ext->next = 0;
1165
1166 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1167
1168 while (data && data->next)
1169 data = (struct setup_data *)(unsigned long)data->next;
1170
1171 if (data)
1172 data->next = (unsigned long)e820ext;
1173 else
1174 params->hdr.setup_data = (unsigned long)e820ext;
1175}
1176
1177static efi_status_t setup_e820(struct boot_params *params,
1178 struct setup_data *e820ext, u32 e820ext_size)
1179{
1180 struct e820entry *e820_map = &params->e820_map[0];
1181 struct efi_info *efi = &params->efi_info;
1182 struct e820entry *prev = NULL;
1183 u32 nr_entries;
1184 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001185 int i;
Matt Fleming291f3632011-12-12 21:27:52 +00001186
Matt Fleming291f3632011-12-12 21:27:52 +00001187 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001188 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1189
1190 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +00001191 efi_memory_desc_t *d;
1192 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001193 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +00001194
Linn Crosettod2078d52013-09-22 19:59:08 -06001195 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
Matt Fleming291f3632011-12-12 21:27:52 +00001196 switch (d->type) {
1197 case EFI_RESERVED_TYPE:
1198 case EFI_RUNTIME_SERVICES_CODE:
1199 case EFI_RUNTIME_SERVICES_DATA:
1200 case EFI_MEMORY_MAPPED_IO:
1201 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1202 case EFI_PAL_CODE:
1203 e820_type = E820_RESERVED;
1204 break;
1205
1206 case EFI_UNUSABLE_MEMORY:
1207 e820_type = E820_UNUSABLE;
1208 break;
1209
1210 case EFI_ACPI_RECLAIM_MEMORY:
1211 e820_type = E820_ACPI;
1212 break;
1213
1214 case EFI_LOADER_CODE:
1215 case EFI_LOADER_DATA:
1216 case EFI_BOOT_SERVICES_CODE:
1217 case EFI_BOOT_SERVICES_DATA:
1218 case EFI_CONVENTIONAL_MEMORY:
1219 e820_type = E820_RAM;
1220 break;
1221
1222 case EFI_ACPI_MEMORY_NVS:
1223 e820_type = E820_NVS;
1224 break;
1225
1226 default:
1227 continue;
1228 }
1229
1230 /* Merge adjacent mappings */
1231 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -06001232 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +00001233 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -06001234 continue;
Matt Fleming291f3632011-12-12 21:27:52 +00001235 }
Linn Crosettod2078d52013-09-22 19:59:08 -06001236
1237 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1238 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1239 sizeof(struct setup_data);
1240
1241 if (!e820ext || e820ext_size < need)
1242 return EFI_BUFFER_TOO_SMALL;
1243
1244 /* boot_params map full, switch to e820 extended */
1245 e820_map = (struct e820entry *)e820ext->data;
1246 }
1247
1248 e820_map->addr = d->phys_addr;
1249 e820_map->size = d->num_pages << PAGE_SHIFT;
1250 e820_map->type = e820_type;
1251 prev = e820_map++;
1252 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +00001253 }
1254
Linn Crosettod2078d52013-09-22 19:59:08 -06001255 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1256 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1257
1258 add_e820ext(params, e820ext, nr_e820ext);
1259 nr_entries -= nr_e820ext;
1260 }
1261
1262 params->e820_entries = (u8)nr_entries;
1263
1264 return EFI_SUCCESS;
1265}
1266
1267static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1268 u32 *e820ext_size)
1269{
1270 efi_status_t status;
1271 unsigned long size;
1272
1273 size = sizeof(struct setup_data) +
1274 sizeof(struct e820entry) * nr_desc;
1275
1276 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +00001277 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001278 *e820ext = NULL;
1279 *e820ext_size = 0;
1280 }
1281
Matt Fleming204b0a12014-03-22 10:09:01 +00001282 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1283 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001284 if (status == EFI_SUCCESS)
1285 *e820ext_size = size;
1286
1287 return status;
1288}
1289
1290static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001291 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -06001292{
1293 struct efi_info *efi = &boot_params->efi_info;
1294 unsigned long map_sz, key, desc_size;
1295 efi_memory_desc_t *mem_map;
1296 struct setup_data *e820ext;
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001297 const char *signature;
Linn Crosettod2078d52013-09-22 19:59:08 -06001298 __u32 e820ext_size;
1299 __u32 nr_desc, prev_nr_desc;
1300 efi_status_t status;
1301 __u32 desc_version;
1302 bool called_exit = false;
1303 u8 nr_entries;
1304 int i;
1305
1306 nr_desc = 0;
1307 e820ext = NULL;
1308 e820ext_size = 0;
1309
1310get_map:
1311 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1312 &desc_version, &key);
1313
1314 if (status != EFI_SUCCESS)
1315 return status;
1316
1317 prev_nr_desc = nr_desc;
1318 nr_desc = map_sz / desc_size;
1319 if (nr_desc > prev_nr_desc &&
1320 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1321 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1322
1323 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1324 if (status != EFI_SUCCESS)
1325 goto free_mem_map;
1326
Matt Fleming204b0a12014-03-22 10:09:01 +00001327 efi_call_early(free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001328 goto get_map; /* Allocated memory, get map again */
1329 }
1330
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001331 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1332 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1333
Linn Crosettod2078d52013-09-22 19:59:08 -06001334 efi->efi_systab = (unsigned long)sys_table;
1335 efi->efi_memdesc_size = desc_size;
1336 efi->efi_memdesc_version = desc_version;
1337 efi->efi_memmap = (unsigned long)mem_map;
1338 efi->efi_memmap_size = map_sz;
1339
1340#ifdef CONFIG_X86_64
1341 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1342 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1343#endif
1344
1345 /* Might as well exit boot services now */
Matt Fleming204b0a12014-03-22 10:09:01 +00001346 status = efi_call_early(exit_boot_services, handle, key);
Linn Crosettod2078d52013-09-22 19:59:08 -06001347 if (status != EFI_SUCCESS) {
1348 /*
1349 * ExitBootServices() will fail if any of the event
1350 * handlers change the memory map. In which case, we
1351 * must be prepared to retry, but only once so that
1352 * we're guaranteed to exit on repeated failures instead
1353 * of spinning forever.
1354 */
1355 if (called_exit)
1356 goto free_mem_map;
1357
1358 called_exit = true;
Matt Fleming204b0a12014-03-22 10:09:01 +00001359 efi_call_early(free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001360 goto get_map;
1361 }
1362
1363 /* Historic? */
1364 boot_params->alt_mem_k = 32 * 1024;
1365
1366 status = setup_e820(boot_params, e820ext, e820ext_size);
1367 if (status != EFI_SUCCESS)
1368 return status;
Matt Fleming291f3632011-12-12 21:27:52 +00001369
1370 return EFI_SUCCESS;
1371
1372free_mem_map:
Matt Fleming204b0a12014-03-22 10:09:01 +00001373 efi_call_early(free_pool, mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +00001374 return status;
1375}
1376
Matt Fleming9ca8f722012-07-19 10:23:48 +01001377/*
1378 * On success we return a pointer to a boot_params structure, and NULL
1379 * on failure.
1380 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001381struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +01001382 struct boot_params *boot_params)
1383{
Matt Fleming54b52d82014-01-10 15:27:14 +00001384 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001385 efi_loaded_image_t *image;
1386 struct setup_header *hdr = &boot_params->hdr;
1387 efi_status_t status;
1388 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +00001389 void *handle;
1390 efi_system_table_t *_table;
1391 bool is64;
1392
1393 efi_early = c;
1394
1395 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1396 handle = (void *)(unsigned long)efi_early->image_handle;
1397 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001398
1399 sys_table = _table;
1400
1401 /* Check if we were booted by the EFI firmware */
1402 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1403 goto fail;
1404
Matt Fleming54b52d82014-01-10 15:27:14 +00001405 if (is64)
1406 setup_boot_services64(efi_early);
1407 else
1408 setup_boot_services32(efi_early);
1409
Matt Fleming9ca8f722012-07-19 10:23:48 +01001410 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001411
Matt Fleming56394ab2014-09-11 09:04:25 +01001412 setup_efi_pci(boot_params);
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001413
Matt Fleming204b0a12014-03-22 10:09:01 +00001414 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1415 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001416 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001417 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001418 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001419 }
Matt Fleming291f3632011-12-12 21:27:52 +00001420
1421 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001422 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001423 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001424 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001425 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001426 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001427 }
Matt Fleming291f3632011-12-12 21:27:52 +00001428
Matt Fleming9ca8f722012-07-19 10:23:48 +01001429 /*
1430 * If the kernel isn't already loaded at the preferred load
1431 * address, relocate it.
1432 */
1433 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001434 unsigned long bzimage_addr = hdr->code32_start;
1435 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1436 hdr->init_size, hdr->init_size,
1437 hdr->pref_address,
1438 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001439 if (status != EFI_SUCCESS) {
1440 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001441 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001442 }
Roy Franz4a9f3a72013-09-22 15:45:32 -07001443
1444 hdr->pref_address = hdr->code32_start;
1445 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001446 }
1447
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001448 status = exit_boot(boot_params, handle, is64);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001449 if (status != EFI_SUCCESS) {
1450 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001451 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001452 }
Matt Fleming291f3632011-12-12 21:27:52 +00001453
1454 memset((char *)gdt->address, 0x0, gdt->size);
1455 desc = (struct desc_struct *)gdt->address;
1456
1457 /* The first GDT is a dummy and the second is unused. */
1458 desc += 2;
1459
1460 desc->limit0 = 0xffff;
1461 desc->base0 = 0x0000;
1462 desc->base1 = 0x0000;
1463 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1464 desc->s = DESC_TYPE_CODE_DATA;
1465 desc->dpl = 0;
1466 desc->p = 1;
1467 desc->limit = 0xf;
1468 desc->avl = 0;
1469 desc->l = 0;
1470 desc->d = SEG_OP_SIZE_32BIT;
1471 desc->g = SEG_GRANULARITY_4KB;
1472 desc->base2 = 0x00;
1473
1474 desc++;
1475 desc->limit0 = 0xffff;
1476 desc->base0 = 0x0000;
1477 desc->base1 = 0x0000;
1478 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1479 desc->s = DESC_TYPE_CODE_DATA;
1480 desc->dpl = 0;
1481 desc->p = 1;
1482 desc->limit = 0xf;
1483 desc->avl = 0;
1484 desc->l = 0;
1485 desc->d = SEG_OP_SIZE_32BIT;
1486 desc->g = SEG_GRANULARITY_4KB;
1487 desc->base2 = 0x00;
1488
1489#ifdef CONFIG_X86_64
1490 /* Task segment value */
1491 desc++;
1492 desc->limit0 = 0x0000;
1493 desc->base0 = 0x0000;
1494 desc->base1 = 0x0000;
1495 desc->type = SEG_TYPE_TSS;
1496 desc->s = 0;
1497 desc->dpl = 0;
1498 desc->p = 1;
1499 desc->limit = 0x0;
1500 desc->avl = 0;
1501 desc->l = 0;
1502 desc->d = 0;
1503 desc->g = SEG_GRANULARITY_4KB;
1504 desc->base2 = 0x00;
1505#endif /* CONFIG_X86_64 */
1506
Matt Fleming291f3632011-12-12 21:27:52 +00001507 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001508 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001509
1510 return boot_params;
1511fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001512 efi_printk(sys_table, "efi_main() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001513 return NULL;
1514}