blob: 916bbdd7dd28ab2fe334f1c4e3cd5455e89a0cbc [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
Ard Biesheuvelf23cf8b2014-07-02 14:54:40 +020022struct efi_config *efi_early;
Matt Fleming204b0a12014-03-22 10:09:01 +000023
Matt Fleming54b52d82014-01-10 15:27:14 +000024#define BOOT_SERVICES(bits) \
25static void setup_boot_services##bits(struct efi_config *c) \
26{ \
27 efi_system_table_##bits##_t *table; \
28 efi_boot_services_##bits##_t *bt; \
29 \
30 table = (typeof(table))sys_table; \
31 \
32 c->text_output = table->con_out; \
33 \
34 bt = (typeof(bt))(unsigned long)(table->boottime); \
35 \
36 c->allocate_pool = bt->allocate_pool; \
37 c->allocate_pages = bt->allocate_pages; \
38 c->get_memory_map = bt->get_memory_map; \
39 c->free_pool = bt->free_pool; \
40 c->free_pages = bt->free_pages; \
41 c->locate_handle = bt->locate_handle; \
42 c->handle_protocol = bt->handle_protocol; \
43 c->exit_boot_services = bt->exit_boot_services; \
44}
45BOOT_SERVICES(32);
46BOOT_SERVICES(64);
47
Ard Biesheuvelbd669472014-07-02 14:54:42 +020048void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
Matt Fleming54b52d82014-01-10 15:27:14 +000049
50static efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +000051__file_size32(void *__fh, efi_char16_t *filename_16,
52 void **handle, u64 *file_sz)
Matt Fleming54b52d82014-01-10 15:27:14 +000053{
Matt Flemingc116e8d2014-01-16 11:35:43 +000054 efi_file_handle_32_t *h, *fh = __fh;
Matt Fleming54b52d82014-01-10 15:27:14 +000055 efi_file_info_t *info;
56 efi_status_t status;
57 efi_guid_t info_guid = EFI_FILE_INFO_ID;
58 u32 info_sz;
59
60 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
61 EFI_FILE_MODE_READ, (u64)0);
62 if (status != EFI_SUCCESS) {
63 efi_printk(sys_table, "Failed to open file: ");
64 efi_char16_printk(sys_table, filename_16);
65 efi_printk(sys_table, "\n");
66 return status;
67 }
68
69 *handle = h;
70
71 info_sz = 0;
72 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
73 &info_sz, NULL);
74 if (status != EFI_BUFFER_TOO_SMALL) {
75 efi_printk(sys_table, "Failed to get file info size\n");
76 return status;
77 }
78
79grow:
Matt Fleming204b0a12014-03-22 10:09:01 +000080 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
81 info_sz, (void **)&info);
Matt Fleming54b52d82014-01-10 15:27:14 +000082 if (status != EFI_SUCCESS) {
83 efi_printk(sys_table, "Failed to alloc mem for file info\n");
84 return status;
85 }
86
87 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
88 &info_sz, info);
89 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +000090 efi_call_early(free_pool, info);
Matt Fleming54b52d82014-01-10 15:27:14 +000091 goto grow;
92 }
93
94 *file_sz = info->file_size;
Matt Fleming204b0a12014-03-22 10:09:01 +000095 efi_call_early(free_pool, info);
Matt Fleming54b52d82014-01-10 15:27:14 +000096
97 if (status != EFI_SUCCESS)
98 efi_printk(sys_table, "Failed to get initrd info\n");
99
100 return status;
101}
102
Matt Flemingc116e8d2014-01-16 11:35:43 +0000103static efi_status_t
104__file_size64(void *__fh, efi_char16_t *filename_16,
105 void **handle, u64 *file_sz)
106{
107 efi_file_handle_64_t *h, *fh = __fh;
108 efi_file_info_t *info;
109 efi_status_t status;
110 efi_guid_t info_guid = EFI_FILE_INFO_ID;
Matt Fleming396f1a02014-04-10 13:30:13 +0100111 u64 info_sz;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000112
113 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
114 EFI_FILE_MODE_READ, (u64)0);
115 if (status != EFI_SUCCESS) {
116 efi_printk(sys_table, "Failed to open file: ");
117 efi_char16_printk(sys_table, filename_16);
118 efi_printk(sys_table, "\n");
119 return status;
120 }
121
122 *handle = h;
123
124 info_sz = 0;
125 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
126 &info_sz, NULL);
127 if (status != EFI_BUFFER_TOO_SMALL) {
128 efi_printk(sys_table, "Failed to get file info size\n");
129 return status;
130 }
131
132grow:
Matt Fleming204b0a12014-03-22 10:09:01 +0000133 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
134 info_sz, (void **)&info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000135 if (status != EFI_SUCCESS) {
136 efi_printk(sys_table, "Failed to alloc mem for file info\n");
137 return status;
138 }
139
140 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
141 &info_sz, info);
142 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000143 efi_call_early(free_pool, info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000144 goto grow;
145 }
146
147 *file_sz = info->file_size;
Matt Fleming204b0a12014-03-22 10:09:01 +0000148 efi_call_early(free_pool, info);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000149
150 if (status != EFI_SUCCESS)
151 efi_printk(sys_table, "Failed to get initrd info\n");
152
153 return status;
154}
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200155efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +0000156efi_file_size(efi_system_table_t *sys_table, void *__fh,
157 efi_char16_t *filename_16, void **handle, u64 *file_sz)
158{
159 if (efi_early->is64)
160 return __file_size64(__fh, filename_16, handle, file_sz);
161
162 return __file_size32(__fh, filename_16, handle, file_sz);
163}
164
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200165efi_status_t
Matt Fleming47514c92014-04-10 14:11:45 +0100166efi_file_read(void *handle, unsigned long *size, void *addr)
Matt Fleming54b52d82014-01-10 15:27:14 +0000167{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000168 unsigned long func;
169
170 if (efi_early->is64) {
Matt Fleming47514c92014-04-10 14:11:45 +0100171 efi_file_handle_64_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000172
173 func = (unsigned long)fh->read;
174 return efi_early->call(func, handle, size, addr);
175 } else {
Matt Fleming47514c92014-04-10 14:11:45 +0100176 efi_file_handle_32_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000177
178 func = (unsigned long)fh->read;
179 return efi_early->call(func, handle, size, addr);
180 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000181}
182
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200183efi_status_t efi_file_close(void *handle)
Matt Fleming54b52d82014-01-10 15:27:14 +0000184{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000185 if (efi_early->is64) {
Matt Fleming47514c92014-04-10 14:11:45 +0100186 efi_file_handle_64_t *fh = handle;
Matt Fleming54b52d82014-01-10 15:27:14 +0000187
Matt Flemingc116e8d2014-01-16 11:35:43 +0000188 return efi_early->call((unsigned long)fh->close, handle);
189 } else {
Matt Fleming47514c92014-04-10 14:11:45 +0100190 efi_file_handle_32_t *fh = handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000191
192 return efi_early->call((unsigned long)fh->close, handle);
193 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000194}
195
Matt Flemingc116e8d2014-01-16 11:35:43 +0000196static inline efi_status_t __open_volume32(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000197{
198 efi_file_io_interface_t *io;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000199 efi_loaded_image_32_t *image = __image;
200 efi_file_handle_32_t *fh;
Matt Fleming54b52d82014-01-10 15:27:14 +0000201 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
202 efi_status_t status;
203 void *handle = (void *)(unsigned long)image->device_handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000204 unsigned long func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000205
Matt Fleming204b0a12014-03-22 10:09:01 +0000206 status = efi_call_early(handle_protocol, handle,
207 &fs_proto, (void **)&io);
Matt Fleming54b52d82014-01-10 15:27:14 +0000208 if (status != EFI_SUCCESS) {
209 efi_printk(sys_table, "Failed to handle fs_proto\n");
210 return status;
211 }
212
213 func = (unsigned long)io->open_volume;
214 status = efi_early->call(func, io, &fh);
215 if (status != EFI_SUCCESS)
216 efi_printk(sys_table, "Failed to open volume\n");
217
218 *__fh = fh;
219 return status;
220}
221
Matt Flemingc116e8d2014-01-16 11:35:43 +0000222static inline efi_status_t __open_volume64(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000223{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000224 efi_file_io_interface_t *io;
225 efi_loaded_image_64_t *image = __image;
226 efi_file_handle_64_t *fh;
227 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
228 efi_status_t status;
229 void *handle = (void *)(unsigned long)image->device_handle;
230 unsigned long func;
231
Matt Fleming204b0a12014-03-22 10:09:01 +0000232 status = efi_call_early(handle_protocol, handle,
233 &fs_proto, (void **)&io);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000234 if (status != EFI_SUCCESS) {
235 efi_printk(sys_table, "Failed to handle fs_proto\n");
236 return status;
237 }
238
239 func = (unsigned long)io->open_volume;
240 status = efi_early->call(func, io, &fh);
241 if (status != EFI_SUCCESS)
242 efi_printk(sys_table, "Failed to open volume\n");
243
244 *__fh = fh;
245 return status;
246}
247
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200248efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +0000249efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
250{
251 if (efi_early->is64)
252 return __open_volume64(__image, __fh);
253
254 return __open_volume32(__image, __fh);
255}
256
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200257void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
Matt Flemingc116e8d2014-01-16 11:35:43 +0000258{
Matt Fleming54b52d82014-01-10 15:27:14 +0000259 unsigned long output_string;
260 size_t offset;
Matt Fleming54b52d82014-01-10 15:27:14 +0000261
Matt Flemingc116e8d2014-01-16 11:35:43 +0000262 if (efi_early->is64) {
263 struct efi_simple_text_output_protocol_64 *out;
264 u64 *func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000265
Matt Flemingc116e8d2014-01-16 11:35:43 +0000266 offset = offsetof(typeof(*out), output_string);
267 output_string = efi_early->text_output + offset;
268 func = (u64 *)output_string;
269
270 efi_early->call(*func, efi_early->text_output, str);
271 } else {
272 struct efi_simple_text_output_protocol_32 *out;
273 u32 *func;
274
275 offset = offsetof(typeof(*out), output_string);
276 output_string = efi_early->text_output + offset;
277 func = (u32 *)output_string;
278
279 efi_early->call(*func, efi_early->text_output, str);
280 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000281}
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800282
Roy Franz7721da42013-09-22 15:45:27 -0700283#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800284
Matt Fleming291f3632011-12-12 21:27:52 +0000285static void find_bits(unsigned long mask, u8 *pos, u8 *size)
286{
287 u8 first, len;
288
289 first = 0;
290 len = 0;
291
292 if (mask) {
293 while (!(mask & 0x1)) {
294 mask = mask >> 1;
295 first++;
296 }
297
298 while (mask & 0x1) {
299 mask = mask >> 1;
300 len++;
301 }
302 }
303
304 *pos = first;
305 *size = len;
306}
307
Matt Flemingc116e8d2014-01-16 11:35:43 +0000308static efi_status_t
309__setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700310{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000311 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700312 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000313 unsigned long size;
314 uint64_t attributes;
315
316 status = efi_early->call(pci->attributes, pci,
317 EfiPciIoAttributeOperationGet, 0, 0,
318 &attributes);
319 if (status != EFI_SUCCESS)
320 return status;
321
322 if (!pci->romimage || !pci->romsize)
323 return EFI_INVALID_PARAMETER;
324
325 size = pci->romsize + sizeof(*rom);
326
Matt Fleming204b0a12014-03-22 10:09:01 +0000327 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000328 if (status != EFI_SUCCESS)
329 return status;
330
331 memset(rom, 0, sizeof(*rom));
332
333 rom->data.type = SETUP_PCI;
334 rom->data.len = size - sizeof(struct setup_data);
335 rom->data.next = 0;
336 rom->pcilen = pci->romsize;
337 *__rom = rom;
338
339 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
340 PCI_VENDOR_ID, 1, &(rom->vendor));
341
342 if (status != EFI_SUCCESS)
343 goto free_struct;
344
345 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
346 PCI_DEVICE_ID, 1, &(rom->devid));
347
348 if (status != EFI_SUCCESS)
349 goto free_struct;
350
351 status = efi_early->call(pci->get_location, pci, &(rom->segment),
352 &(rom->bus), &(rom->device), &(rom->function));
353
354 if (status != EFI_SUCCESS)
355 goto free_struct;
356
357 memcpy(rom->romdata, pci->romimage, pci->romsize);
358 return status;
359
360free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000361 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000362 return status;
363}
364
365static efi_status_t
366setup_efi_pci32(struct boot_params *params, void **pci_handle,
367 unsigned long size)
368{
369 efi_pci_io_protocol_32 *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700370 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000371 u32 *handles = (u32 *)(unsigned long)pci_handle;
372 efi_status_t status;
373 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700374 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000375 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700376
Jan Beulichbc754792013-01-18 12:35:14 +0000377 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700378
379 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000380 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700381
Matt Flemingc116e8d2014-01-16 11:35:43 +0000382 nr_pci = size / sizeof(u32);
383 for (i = 0; i < nr_pci; i++) {
384 struct pci_setup_rom *rom = NULL;
385 u32 h = handles[i];
386
Matt Fleming204b0a12014-03-22 10:09:01 +0000387 status = efi_call_early(handle_protocol, h,
388 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000389
390 if (status != EFI_SUCCESS)
391 continue;
392
393 if (!pci)
394 continue;
395
396 status = __setup_efi_pci32(pci, &rom);
397 if (status != EFI_SUCCESS)
398 continue;
399
400 if (data)
401 data->next = (unsigned long)rom;
402 else
403 params->hdr.setup_data = (unsigned long)rom;
404
405 data = (struct setup_data *)rom;
406
407 }
408
409 return status;
410}
411
412static efi_status_t
413__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
414{
415 struct pci_setup_rom *rom;
416 efi_status_t status;
417 unsigned long size;
418 uint64_t attributes;
419
420 status = efi_early->call(pci->attributes, pci,
421 EfiPciIoAttributeOperationGet, 0,
422 &attributes);
423 if (status != EFI_SUCCESS)
424 return status;
425
426 if (!pci->romimage || !pci->romsize)
427 return EFI_INVALID_PARAMETER;
428
429 size = pci->romsize + sizeof(*rom);
430
Matt Fleming204b0a12014-03-22 10:09:01 +0000431 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000432 if (status != EFI_SUCCESS)
433 return status;
434
435 rom->data.type = SETUP_PCI;
436 rom->data.len = size - sizeof(struct setup_data);
437 rom->data.next = 0;
438 rom->pcilen = pci->romsize;
439 *__rom = rom;
440
441 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
442 PCI_VENDOR_ID, 1, &(rom->vendor));
443
444 if (status != EFI_SUCCESS)
445 goto free_struct;
446
447 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
448 PCI_DEVICE_ID, 1, &(rom->devid));
449
450 if (status != EFI_SUCCESS)
451 goto free_struct;
452
453 status = efi_early->call(pci->get_location, pci, &(rom->segment),
454 &(rom->bus), &(rom->device), &(rom->function));
455
456 if (status != EFI_SUCCESS)
457 goto free_struct;
458
459 memcpy(rom->romdata, pci->romimage, pci->romsize);
460 return status;
461
462free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000463 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000464 return status;
465
466}
467
468static efi_status_t
469setup_efi_pci64(struct boot_params *params, void **pci_handle,
470 unsigned long size)
471{
472 efi_pci_io_protocol_64 *pci = NULL;
473 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
474 u64 *handles = (u64 *)(unsigned long)pci_handle;
475 efi_status_t status;
476 unsigned long nr_pci;
477 struct setup_data *data;
478 int i;
479
480 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
481
482 while (data && data->next)
483 data = (struct setup_data *)(unsigned long)data->next;
484
485 nr_pci = size / sizeof(u64);
486 for (i = 0; i < nr_pci; i++) {
487 struct pci_setup_rom *rom = NULL;
488 u64 h = handles[i];
489
Matt Fleming204b0a12014-03-22 10:09:01 +0000490 status = efi_call_early(handle_protocol, h,
491 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000492
493 if (status != EFI_SUCCESS)
494 continue;
495
496 if (!pci)
497 continue;
498
499 status = __setup_efi_pci64(pci, &rom);
500 if (status != EFI_SUCCESS)
501 continue;
502
503 if (data)
504 data->next = (unsigned long)rom;
505 else
506 params->hdr.setup_data = (unsigned long)rom;
507
508 data = (struct setup_data *)rom;
509
510 }
511
512 return status;
513}
514
515static efi_status_t setup_efi_pci(struct boot_params *params)
516{
517 efi_status_t status;
518 void **pci_handle = NULL;
519 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
520 unsigned long size = 0;
521
Matt Fleming204b0a12014-03-22 10:09:01 +0000522 status = efi_call_early(locate_handle,
523 EFI_LOCATE_BY_PROTOCOL,
524 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700525
526 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000527 status = efi_call_early(allocate_pool,
528 EFI_LOADER_DATA,
529 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700530
531 if (status != EFI_SUCCESS)
532 return status;
533
Matt Fleming204b0a12014-03-22 10:09:01 +0000534 status = efi_call_early(locate_handle,
535 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
536 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700537 }
538
539 if (status != EFI_SUCCESS)
540 goto free_handle;
541
Matt Flemingc116e8d2014-01-16 11:35:43 +0000542 if (efi_early->is64)
543 status = setup_efi_pci64(params, pci_handle, size);
544 else
545 status = setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700546
547free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000548 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700549 return status;
550}
551
Matt Flemingc116e8d2014-01-16 11:35:43 +0000552static void
553setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
554 struct efi_pixel_bitmask pixel_info, int pixel_format)
Matt Fleming291f3632011-12-12 21:27:52 +0000555{
Matt Fleming291f3632011-12-12 21:27:52 +0000556 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
557 si->lfb_depth = 32;
558 si->lfb_linelength = pixels_per_scan_line * 4;
559 si->red_size = 8;
560 si->red_pos = 0;
561 si->green_size = 8;
562 si->green_pos = 8;
563 si->blue_size = 8;
564 si->blue_pos = 16;
565 si->rsvd_size = 8;
566 si->rsvd_pos = 24;
567 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
568 si->lfb_depth = 32;
569 si->lfb_linelength = pixels_per_scan_line * 4;
570 si->red_size = 8;
571 si->red_pos = 16;
572 si->green_size = 8;
573 si->green_pos = 8;
574 si->blue_size = 8;
575 si->blue_pos = 0;
576 si->rsvd_size = 8;
577 si->rsvd_pos = 24;
578 } else if (pixel_format == PIXEL_BIT_MASK) {
579 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
580 find_bits(pixel_info.green_mask, &si->green_pos,
581 &si->green_size);
582 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
583 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
584 &si->rsvd_size);
585 si->lfb_depth = si->red_size + si->green_size +
586 si->blue_size + si->rsvd_size;
587 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
588 } else {
589 si->lfb_depth = 4;
590 si->lfb_linelength = si->lfb_width / 2;
591 si->red_size = 0;
592 si->red_pos = 0;
593 si->green_size = 0;
594 si->green_pos = 0;
595 si->blue_size = 0;
596 si->blue_pos = 0;
597 si->rsvd_size = 0;
598 si->rsvd_pos = 0;
599 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000600}
601
602static efi_status_t
603__gop_query32(struct efi_graphics_output_protocol_32 *gop32,
604 struct efi_graphics_output_mode_info **info,
605 unsigned long *size, u32 *fb_base)
606{
607 struct efi_graphics_output_protocol_mode_32 *mode;
608 efi_status_t status;
609 unsigned long m;
610
611 m = gop32->mode;
612 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
613
614 status = efi_early->call(gop32->query_mode, gop32,
615 mode->mode, size, info);
616 if (status != EFI_SUCCESS)
617 return status;
618
619 *fb_base = mode->frame_buffer_base;
620 return status;
621}
622
623static efi_status_t
624setup_gop32(struct screen_info *si, efi_guid_t *proto,
625 unsigned long size, void **gop_handle)
626{
627 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
628 unsigned long nr_gops;
629 u16 width, height;
630 u32 pixels_per_scan_line;
631 u32 fb_base;
632 struct efi_pixel_bitmask pixel_info;
633 int pixel_format;
634 efi_status_t status;
635 u32 *handles = (u32 *)(unsigned long)gop_handle;
636 int i;
637
638 first_gop = NULL;
639 gop32 = NULL;
640
641 nr_gops = size / sizeof(u32);
642 for (i = 0; i < nr_gops; i++) {
643 struct efi_graphics_output_mode_info *info = NULL;
644 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
645 bool conout_found = false;
646 void *dummy = NULL;
647 u32 h = handles[i];
648
Matt Fleming204b0a12014-03-22 10:09:01 +0000649 status = efi_call_early(handle_protocol, h,
650 proto, (void **)&gop32);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000651 if (status != EFI_SUCCESS)
652 continue;
653
Matt Fleming204b0a12014-03-22 10:09:01 +0000654 status = efi_call_early(handle_protocol, h,
655 &conout_proto, &dummy);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000656 if (status == EFI_SUCCESS)
657 conout_found = true;
658
659 status = __gop_query32(gop32, &info, &size, &fb_base);
660 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
661 /*
662 * Systems that use the UEFI Console Splitter may
663 * provide multiple GOP devices, not all of which are
664 * backed by real hardware. The workaround is to search
665 * for a GOP implementing the ConOut protocol, and if
666 * one isn't found, to just fall back to the first GOP.
667 */
668 width = info->horizontal_resolution;
669 height = info->vertical_resolution;
670 pixel_format = info->pixel_format;
671 pixel_info = info->pixel_information;
672 pixels_per_scan_line = info->pixels_per_scan_line;
673
674 /*
675 * Once we've found a GOP supporting ConOut,
676 * don't bother looking any further.
677 */
678 first_gop = gop32;
679 if (conout_found)
680 break;
681 }
682 }
683
684 /* Did we find any GOPs? */
685 if (!first_gop)
686 goto out;
687
688 /* EFI framebuffer */
689 si->orig_video_isVGA = VIDEO_TYPE_EFI;
690
691 si->lfb_width = width;
692 si->lfb_height = height;
693 si->lfb_base = fb_base;
694 si->pages = 1;
695
696 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
Matt Fleming291f3632011-12-12 21:27:52 +0000697
Matthew Garrette9b10952012-07-27 17:20:49 -0400698 si->lfb_size = si->lfb_linelength * si->lfb_height;
699
Matthew Garrettf462ed92012-07-27 12:58:53 -0400700 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000701out:
702 return status;
703}
704
705static efi_status_t
706__gop_query64(struct efi_graphics_output_protocol_64 *gop64,
707 struct efi_graphics_output_mode_info **info,
708 unsigned long *size, u32 *fb_base)
709{
710 struct efi_graphics_output_protocol_mode_64 *mode;
711 efi_status_t status;
712 unsigned long m;
713
714 m = gop64->mode;
715 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
716
717 status = efi_early->call(gop64->query_mode, gop64,
718 mode->mode, size, info);
719 if (status != EFI_SUCCESS)
720 return status;
721
722 *fb_base = mode->frame_buffer_base;
723 return status;
724}
725
726static efi_status_t
727setup_gop64(struct screen_info *si, efi_guid_t *proto,
728 unsigned long size, void **gop_handle)
729{
730 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
731 unsigned long nr_gops;
732 u16 width, height;
733 u32 pixels_per_scan_line;
734 u32 fb_base;
735 struct efi_pixel_bitmask pixel_info;
736 int pixel_format;
737 efi_status_t status;
738 u64 *handles = (u64 *)(unsigned long)gop_handle;
739 int i;
740
741 first_gop = NULL;
742 gop64 = NULL;
743
744 nr_gops = size / sizeof(u64);
745 for (i = 0; i < nr_gops; i++) {
746 struct efi_graphics_output_mode_info *info = NULL;
747 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
748 bool conout_found = false;
749 void *dummy = NULL;
750 u64 h = handles[i];
751
Matt Fleming204b0a12014-03-22 10:09:01 +0000752 status = efi_call_early(handle_protocol, h,
753 proto, (void **)&gop64);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000754 if (status != EFI_SUCCESS)
755 continue;
756
Matt Fleming204b0a12014-03-22 10:09:01 +0000757 status = efi_call_early(handle_protocol, h,
758 &conout_proto, &dummy);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000759 if (status == EFI_SUCCESS)
760 conout_found = true;
761
762 status = __gop_query64(gop64, &info, &size, &fb_base);
763 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
764 /*
765 * Systems that use the UEFI Console Splitter may
766 * provide multiple GOP devices, not all of which are
767 * backed by real hardware. The workaround is to search
768 * for a GOP implementing the ConOut protocol, and if
769 * one isn't found, to just fall back to the first GOP.
770 */
771 width = info->horizontal_resolution;
772 height = info->vertical_resolution;
773 pixel_format = info->pixel_format;
774 pixel_info = info->pixel_information;
775 pixels_per_scan_line = info->pixels_per_scan_line;
776
777 /*
778 * Once we've found a GOP supporting ConOut,
779 * don't bother looking any further.
780 */
781 first_gop = gop64;
782 if (conout_found)
783 break;
784 }
785 }
786
787 /* Did we find any GOPs? */
788 if (!first_gop)
789 goto out;
790
791 /* EFI framebuffer */
792 si->orig_video_isVGA = VIDEO_TYPE_EFI;
793
794 si->lfb_width = width;
795 si->lfb_height = height;
796 si->lfb_base = fb_base;
797 si->pages = 1;
798
799 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
800
801 si->lfb_size = si->lfb_linelength * si->lfb_height;
802
803 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
804out:
805 return status;
806}
807
808/*
809 * See if we have Graphics Output Protocol
810 */
811static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
812 unsigned long size)
813{
814 efi_status_t status;
815 void **gop_handle = NULL;
816
Matt Fleming204b0a12014-03-22 10:09:01 +0000817 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
818 size, (void **)&gop_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000819 if (status != EFI_SUCCESS)
820 return status;
821
Matt Fleming204b0a12014-03-22 10:09:01 +0000822 status = efi_call_early(locate_handle,
823 EFI_LOCATE_BY_PROTOCOL,
824 proto, NULL, &size, gop_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000825 if (status != EFI_SUCCESS)
826 goto free_handle;
827
828 if (efi_early->is64)
829 status = setup_gop64(si, proto, size, gop_handle);
830 else
831 status = setup_gop32(si, proto, size, gop_handle);
Matthew Garrettf462ed92012-07-27 12:58:53 -0400832
Matt Fleming291f3632011-12-12 21:27:52 +0000833free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000834 efi_call_early(free_pool, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000835 return status;
836}
837
Matt Flemingc116e8d2014-01-16 11:35:43 +0000838static efi_status_t
839setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000840{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000841 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
842 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000843 unsigned long nr_ugas;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000844 u32 *handles = (u32 *)uga_handle;;
Matt Fleming291f3632011-12-12 21:27:52 +0000845 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000846 int i;
847
Matt Fleming291f3632011-12-12 21:27:52 +0000848 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000849 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000850 for (i = 0; i < nr_ugas; i++) {
851 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000852 u32 w, h, depth, refresh;
853 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000854 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000855
Matt Fleming204b0a12014-03-22 10:09:01 +0000856 status = efi_call_early(handle_protocol, handle,
857 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000858 if (status != EFI_SUCCESS)
859 continue;
860
Matt Fleming204b0a12014-03-22 10:09:01 +0000861 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000862
Matt Fleming54b52d82014-01-10 15:27:14 +0000863 status = efi_early->call((unsigned long)uga->get_mode, uga,
864 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000865 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000866 *width = w;
867 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000868
869 /*
870 * Once we've found a UGA supporting PCIIO,
871 * don't bother looking any further.
872 */
873 if (pciio)
874 break;
875
876 first_uga = uga;
877 }
878 }
879
Matt Flemingc116e8d2014-01-16 11:35:43 +0000880 return status;
881}
882
883static efi_status_t
884setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
885{
886 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
887 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
888 unsigned long nr_ugas;
889 u64 *handles = (u64 *)uga_handle;;
890 efi_status_t status;
891 int i;
892
893 first_uga = NULL;
894 nr_ugas = size / sizeof(u64);
895 for (i = 0; i < nr_ugas; i++) {
896 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
897 u32 w, h, depth, refresh;
898 void *pciio;
899 u64 handle = handles[i];
900
Matt Fleming204b0a12014-03-22 10:09:01 +0000901 status = efi_call_early(handle_protocol, handle,
902 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000903 if (status != EFI_SUCCESS)
904 continue;
905
Matt Fleming204b0a12014-03-22 10:09:01 +0000906 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000907
908 status = efi_early->call((unsigned long)uga->get_mode, uga,
909 &w, &h, &depth, &refresh);
910 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
911 *width = w;
912 *height = h;
913
914 /*
915 * Once we've found a UGA supporting PCIIO,
916 * don't bother looking any further.
917 */
918 if (pciio)
919 break;
920
921 first_uga = uga;
922 }
923 }
924
925 return status;
926}
927
928/*
929 * See if we have Universal Graphics Adapter (UGA) protocol
930 */
931static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
932 unsigned long size)
933{
934 efi_status_t status;
935 u32 width, height;
936 void **uga_handle = NULL;
937
Matt Fleming204b0a12014-03-22 10:09:01 +0000938 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
939 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000940 if (status != EFI_SUCCESS)
941 return status;
942
Matt Fleming204b0a12014-03-22 10:09:01 +0000943 status = efi_call_early(locate_handle,
944 EFI_LOCATE_BY_PROTOCOL,
945 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000946 if (status != EFI_SUCCESS)
947 goto free_handle;
948
949 height = 0;
950 width = 0;
951
952 if (efi_early->is64)
953 status = setup_uga64(uga_handle, size, &width, &height);
954 else
955 status = setup_uga32(uga_handle, size, &width, &height);
956
957 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000958 goto free_handle;
959
960 /* EFI framebuffer */
961 si->orig_video_isVGA = VIDEO_TYPE_EFI;
962
963 si->lfb_depth = 32;
964 si->lfb_width = width;
965 si->lfb_height = height;
966
967 si->red_size = 8;
968 si->red_pos = 16;
969 si->green_size = 8;
970 si->green_pos = 8;
971 si->blue_size = 8;
972 si->blue_pos = 0;
973 si->rsvd_size = 8;
974 si->rsvd_pos = 24;
975
Matt Fleming291f3632011-12-12 21:27:52 +0000976free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000977 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000978 return status;
979}
980
981void setup_graphics(struct boot_params *boot_params)
982{
983 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
984 struct screen_info *si;
985 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
986 efi_status_t status;
987 unsigned long size;
988 void **gop_handle = NULL;
989 void **uga_handle = NULL;
990
991 si = &boot_params->screen_info;
992 memset(si, 0, sizeof(*si));
993
994 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +0000995 status = efi_call_early(locate_handle,
996 EFI_LOCATE_BY_PROTOCOL,
997 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000998 if (status == EFI_BUFFER_TOO_SMALL)
999 status = setup_gop(si, &graphics_proto, size);
1000
1001 if (status != EFI_SUCCESS) {
1002 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +00001003 status = efi_call_early(locate_handle,
1004 EFI_LOCATE_BY_PROTOCOL,
1005 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001006 if (status == EFI_BUFFER_TOO_SMALL)
1007 setup_uga(si, &uga_proto, size);
1008 }
1009}
1010
Matt Fleming291f3632011-12-12 21:27:52 +00001011/*
1012 * Because the x86 boot code expects to be passed a boot_params we
1013 * need to create one ourselves (usually the bootloader would create
1014 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +01001015 *
1016 * The caller is responsible for filling out ->code32_start in the
1017 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +00001018 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001019struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +00001020{
Matt Fleming9ca8f722012-07-19 10:23:48 +01001021 struct boot_params *boot_params;
1022 struct sys_desc_table *sdt;
1023 struct apm_bios_info *bi;
1024 struct setup_header *hdr;
1025 struct efi_info *efi;
1026 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +00001027 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001028 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +00001029 int options_size = 0;
1030 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -07001031 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001032 u16 *s2;
1033 u8 *s1;
1034 int i;
Roy Franz46f45822013-09-22 15:45:39 -07001035 unsigned long ramdisk_addr;
1036 unsigned long ramdisk_size;
Yinghai Lu4bf71112014-06-14 12:23:41 -07001037 unsigned long initrd_addr_max;
Matt Fleming291f3632011-12-12 21:27:52 +00001038
Matt Fleming54b52d82014-01-10 15:27:14 +00001039 efi_early = c;
1040 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1041 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001042
1043 /* Check if we were booted by the EFI firmware */
1044 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1045 return NULL;
1046
Matt Fleming54b52d82014-01-10 15:27:14 +00001047 if (efi_early->is64)
1048 setup_boot_services64(efi_early);
1049 else
1050 setup_boot_services32(efi_early);
1051
Matt Fleming204b0a12014-03-22 10:09:01 +00001052 status = efi_call_early(handle_protocol, handle,
1053 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001054 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001055 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001056 return NULL;
1057 }
1058
Roy Franz40e45302013-09-22 15:45:29 -07001059 status = efi_low_alloc(sys_table, 0x4000, 1,
1060 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001061 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001062 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001063 return NULL;
1064 }
1065
1066 memset(boot_params, 0x0, 0x4000);
1067
1068 hdr = &boot_params->hdr;
1069 efi = &boot_params->efi_info;
1070 bi = &boot_params->apm_bios_info;
1071 sdt = &boot_params->sys_desc_table;
1072
1073 /* Copy the second sector to boot_params */
1074 memcpy(&hdr->jump, image->image_base + 512, 512);
1075
1076 /*
1077 * Fill out some of the header fields ourselves because the
1078 * EFI firmware loader doesn't load the first sector.
1079 */
1080 hdr->root_flags = 1;
1081 hdr->vid_mode = 0xffff;
1082 hdr->boot_flag = 0xAA55;
1083
Matt Fleming291f3632011-12-12 21:27:52 +00001084 hdr->type_of_loader = 0x21;
1085
1086 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -05001087 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -07001088 if (!cmdline_ptr)
1089 goto fail;
1090 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001091
1092 hdr->ramdisk_image = 0;
1093 hdr->ramdisk_size = 0;
1094
Matt Fleming291f3632011-12-12 21:27:52 +00001095 /* Clear APM BIOS info */
1096 memset(bi, 0, sizeof(*bi));
1097
1098 memset(sdt, 0, sizeof(*sdt));
1099
Yinghai Lu4bf71112014-06-14 12:23:41 -07001100 if (hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)
1101 initrd_addr_max = -1UL;
1102 else
1103 initrd_addr_max = hdr->initrd_addr_max;
1104
Roy Franz46f45822013-09-22 15:45:39 -07001105 status = handle_cmdline_files(sys_table, image,
1106 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu4bf71112014-06-14 12:23:41 -07001107 "initrd=", initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -07001108 &ramdisk_addr, &ramdisk_size);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001109 if (status != EFI_SUCCESS)
1110 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -07001111 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1112 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
1113 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1114 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001115
1116 return boot_params;
1117fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -07001118 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001119fail:
Roy Franz40e45302013-09-22 15:45:29 -07001120 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001121 return NULL;
1122}
1123
Linn Crosettod2078d52013-09-22 19:59:08 -06001124static void add_e820ext(struct boot_params *params,
1125 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +01001126{
Linn Crosettod2078d52013-09-22 19:59:08 -06001127 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001128 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -06001129 unsigned long size;
1130
1131 e820ext->type = SETUP_E820_EXT;
1132 e820ext->len = nr_entries * sizeof(struct e820entry);
1133 e820ext->next = 0;
1134
1135 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1136
1137 while (data && data->next)
1138 data = (struct setup_data *)(unsigned long)data->next;
1139
1140 if (data)
1141 data->next = (unsigned long)e820ext;
1142 else
1143 params->hdr.setup_data = (unsigned long)e820ext;
1144}
1145
1146static efi_status_t setup_e820(struct boot_params *params,
1147 struct setup_data *e820ext, u32 e820ext_size)
1148{
1149 struct e820entry *e820_map = &params->e820_map[0];
1150 struct efi_info *efi = &params->efi_info;
1151 struct e820entry *prev = NULL;
1152 u32 nr_entries;
1153 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001154 int i;
Matt Fleming291f3632011-12-12 21:27:52 +00001155
Matt Fleming291f3632011-12-12 21:27:52 +00001156 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001157 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1158
1159 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +00001160 efi_memory_desc_t *d;
1161 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001162 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +00001163
Linn Crosettod2078d52013-09-22 19:59:08 -06001164 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
Matt Fleming291f3632011-12-12 21:27:52 +00001165 switch (d->type) {
1166 case EFI_RESERVED_TYPE:
1167 case EFI_RUNTIME_SERVICES_CODE:
1168 case EFI_RUNTIME_SERVICES_DATA:
1169 case EFI_MEMORY_MAPPED_IO:
1170 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1171 case EFI_PAL_CODE:
1172 e820_type = E820_RESERVED;
1173 break;
1174
1175 case EFI_UNUSABLE_MEMORY:
1176 e820_type = E820_UNUSABLE;
1177 break;
1178
1179 case EFI_ACPI_RECLAIM_MEMORY:
1180 e820_type = E820_ACPI;
1181 break;
1182
1183 case EFI_LOADER_CODE:
1184 case EFI_LOADER_DATA:
1185 case EFI_BOOT_SERVICES_CODE:
1186 case EFI_BOOT_SERVICES_DATA:
1187 case EFI_CONVENTIONAL_MEMORY:
1188 e820_type = E820_RAM;
1189 break;
1190
1191 case EFI_ACPI_MEMORY_NVS:
1192 e820_type = E820_NVS;
1193 break;
1194
1195 default:
1196 continue;
1197 }
1198
1199 /* Merge adjacent mappings */
1200 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -06001201 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +00001202 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -06001203 continue;
Matt Fleming291f3632011-12-12 21:27:52 +00001204 }
Linn Crosettod2078d52013-09-22 19:59:08 -06001205
1206 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1207 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1208 sizeof(struct setup_data);
1209
1210 if (!e820ext || e820ext_size < need)
1211 return EFI_BUFFER_TOO_SMALL;
1212
1213 /* boot_params map full, switch to e820 extended */
1214 e820_map = (struct e820entry *)e820ext->data;
1215 }
1216
1217 e820_map->addr = d->phys_addr;
1218 e820_map->size = d->num_pages << PAGE_SHIFT;
1219 e820_map->type = e820_type;
1220 prev = e820_map++;
1221 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +00001222 }
1223
Linn Crosettod2078d52013-09-22 19:59:08 -06001224 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1225 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1226
1227 add_e820ext(params, e820ext, nr_e820ext);
1228 nr_entries -= nr_e820ext;
1229 }
1230
1231 params->e820_entries = (u8)nr_entries;
1232
1233 return EFI_SUCCESS;
1234}
1235
1236static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1237 u32 *e820ext_size)
1238{
1239 efi_status_t status;
1240 unsigned long size;
1241
1242 size = sizeof(struct setup_data) +
1243 sizeof(struct e820entry) * nr_desc;
1244
1245 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +00001246 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001247 *e820ext = NULL;
1248 *e820ext_size = 0;
1249 }
1250
Matt Fleming204b0a12014-03-22 10:09:01 +00001251 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1252 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001253 if (status == EFI_SUCCESS)
1254 *e820ext_size = size;
1255
1256 return status;
1257}
1258
1259static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001260 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -06001261{
1262 struct efi_info *efi = &boot_params->efi_info;
1263 unsigned long map_sz, key, desc_size;
1264 efi_memory_desc_t *mem_map;
1265 struct setup_data *e820ext;
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001266 const char *signature;
Linn Crosettod2078d52013-09-22 19:59:08 -06001267 __u32 e820ext_size;
1268 __u32 nr_desc, prev_nr_desc;
1269 efi_status_t status;
1270 __u32 desc_version;
1271 bool called_exit = false;
1272 u8 nr_entries;
1273 int i;
1274
1275 nr_desc = 0;
1276 e820ext = NULL;
1277 e820ext_size = 0;
1278
1279get_map:
1280 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1281 &desc_version, &key);
1282
1283 if (status != EFI_SUCCESS)
1284 return status;
1285
1286 prev_nr_desc = nr_desc;
1287 nr_desc = map_sz / desc_size;
1288 if (nr_desc > prev_nr_desc &&
1289 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1290 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1291
1292 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1293 if (status != EFI_SUCCESS)
1294 goto free_mem_map;
1295
Matt Fleming204b0a12014-03-22 10:09:01 +00001296 efi_call_early(free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001297 goto get_map; /* Allocated memory, get map again */
1298 }
1299
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001300 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1301 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1302
Linn Crosettod2078d52013-09-22 19:59:08 -06001303 efi->efi_systab = (unsigned long)sys_table;
1304 efi->efi_memdesc_size = desc_size;
1305 efi->efi_memdesc_version = desc_version;
1306 efi->efi_memmap = (unsigned long)mem_map;
1307 efi->efi_memmap_size = map_sz;
1308
1309#ifdef CONFIG_X86_64
1310 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1311 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1312#endif
1313
1314 /* Might as well exit boot services now */
Matt Fleming204b0a12014-03-22 10:09:01 +00001315 status = efi_call_early(exit_boot_services, handle, key);
Linn Crosettod2078d52013-09-22 19:59:08 -06001316 if (status != EFI_SUCCESS) {
1317 /*
1318 * ExitBootServices() will fail if any of the event
1319 * handlers change the memory map. In which case, we
1320 * must be prepared to retry, but only once so that
1321 * we're guaranteed to exit on repeated failures instead
1322 * of spinning forever.
1323 */
1324 if (called_exit)
1325 goto free_mem_map;
1326
1327 called_exit = true;
Matt Fleming204b0a12014-03-22 10:09:01 +00001328 efi_call_early(free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001329 goto get_map;
1330 }
1331
1332 /* Historic? */
1333 boot_params->alt_mem_k = 32 * 1024;
1334
1335 status = setup_e820(boot_params, e820ext, e820ext_size);
1336 if (status != EFI_SUCCESS)
1337 return status;
Matt Fleming291f3632011-12-12 21:27:52 +00001338
1339 return EFI_SUCCESS;
1340
1341free_mem_map:
Matt Fleming204b0a12014-03-22 10:09:01 +00001342 efi_call_early(free_pool, mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +00001343 return status;
1344}
1345
Matt Fleming9ca8f722012-07-19 10:23:48 +01001346/*
1347 * On success we return a pointer to a boot_params structure, and NULL
1348 * on failure.
1349 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001350struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +01001351 struct boot_params *boot_params)
1352{
Matt Fleming54b52d82014-01-10 15:27:14 +00001353 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001354 efi_loaded_image_t *image;
1355 struct setup_header *hdr = &boot_params->hdr;
1356 efi_status_t status;
1357 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +00001358 void *handle;
1359 efi_system_table_t *_table;
1360 bool is64;
1361
1362 efi_early = c;
1363
1364 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1365 handle = (void *)(unsigned long)efi_early->image_handle;
1366 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001367
1368 sys_table = _table;
1369
1370 /* Check if we were booted by the EFI firmware */
1371 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1372 goto fail;
1373
Matt Fleming54b52d82014-01-10 15:27:14 +00001374 if (is64)
1375 setup_boot_services64(efi_early);
1376 else
1377 setup_boot_services32(efi_early);
1378
Matt Fleming9ca8f722012-07-19 10:23:48 +01001379 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001380
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001381 setup_efi_pci(boot_params);
1382
Matt Fleming204b0a12014-03-22 10:09:01 +00001383 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1384 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001385 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001386 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001387 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001388 }
Matt Fleming291f3632011-12-12 21:27:52 +00001389
1390 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001391 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001392 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001393 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001394 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001395 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001396 }
Matt Fleming291f3632011-12-12 21:27:52 +00001397
Matt Fleming9ca8f722012-07-19 10:23:48 +01001398 /*
1399 * If the kernel isn't already loaded at the preferred load
1400 * address, relocate it.
1401 */
1402 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001403 unsigned long bzimage_addr = hdr->code32_start;
1404 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1405 hdr->init_size, hdr->init_size,
1406 hdr->pref_address,
1407 hdr->kernel_alignment);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001408 if (status != EFI_SUCCESS)
1409 goto fail;
Roy Franz4a9f3a72013-09-22 15:45:32 -07001410
1411 hdr->pref_address = hdr->code32_start;
1412 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001413 }
1414
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001415 status = exit_boot(boot_params, handle, is64);
Matt Fleming291f3632011-12-12 21:27:52 +00001416 if (status != EFI_SUCCESS)
1417 goto fail;
1418
1419 memset((char *)gdt->address, 0x0, gdt->size);
1420 desc = (struct desc_struct *)gdt->address;
1421
1422 /* The first GDT is a dummy and the second is unused. */
1423 desc += 2;
1424
1425 desc->limit0 = 0xffff;
1426 desc->base0 = 0x0000;
1427 desc->base1 = 0x0000;
1428 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1429 desc->s = DESC_TYPE_CODE_DATA;
1430 desc->dpl = 0;
1431 desc->p = 1;
1432 desc->limit = 0xf;
1433 desc->avl = 0;
1434 desc->l = 0;
1435 desc->d = SEG_OP_SIZE_32BIT;
1436 desc->g = SEG_GRANULARITY_4KB;
1437 desc->base2 = 0x00;
1438
1439 desc++;
1440 desc->limit0 = 0xffff;
1441 desc->base0 = 0x0000;
1442 desc->base1 = 0x0000;
1443 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1444 desc->s = DESC_TYPE_CODE_DATA;
1445 desc->dpl = 0;
1446 desc->p = 1;
1447 desc->limit = 0xf;
1448 desc->avl = 0;
1449 desc->l = 0;
1450 desc->d = SEG_OP_SIZE_32BIT;
1451 desc->g = SEG_GRANULARITY_4KB;
1452 desc->base2 = 0x00;
1453
1454#ifdef CONFIG_X86_64
1455 /* Task segment value */
1456 desc++;
1457 desc->limit0 = 0x0000;
1458 desc->base0 = 0x0000;
1459 desc->base1 = 0x0000;
1460 desc->type = SEG_TYPE_TSS;
1461 desc->s = 0;
1462 desc->dpl = 0;
1463 desc->p = 1;
1464 desc->limit = 0x0;
1465 desc->avl = 0;
1466 desc->l = 0;
1467 desc->d = 0;
1468 desc->g = SEG_GRANULARITY_4KB;
1469 desc->base2 = 0x00;
1470#endif /* CONFIG_X86_64 */
1471
Matt Fleming291f3632011-12-12 21:27:52 +00001472 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001473 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001474
1475 return boot_params;
1476fail:
1477 return NULL;
1478}