blob: ed8e06c59a4cac4c1e12c0b0a7dfaebc7025e142 [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
Matt Fleming291f3632011-12-12 21:27:52 +0000283static void find_bits(unsigned long mask, u8 *pos, u8 *size)
284{
285 u8 first, len;
286
287 first = 0;
288 len = 0;
289
290 if (mask) {
291 while (!(mask & 0x1)) {
292 mask = mask >> 1;
293 first++;
294 }
295
296 while (mask & 0x1) {
297 mask = mask >> 1;
298 len++;
299 }
300 }
301
302 *pos = first;
303 *size = len;
304}
305
Matt Flemingc116e8d2014-01-16 11:35:43 +0000306static efi_status_t
307__setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700308{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000309 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700310 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000311 unsigned long size;
312 uint64_t attributes;
313
314 status = efi_early->call(pci->attributes, pci,
315 EfiPciIoAttributeOperationGet, 0, 0,
316 &attributes);
317 if (status != EFI_SUCCESS)
318 return status;
319
320 if (!pci->romimage || !pci->romsize)
321 return EFI_INVALID_PARAMETER;
322
323 size = pci->romsize + sizeof(*rom);
324
Matt Fleming204b0a12014-03-22 10:09:01 +0000325 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200326 if (status != EFI_SUCCESS) {
327 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000328 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200329 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000330
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
Andre Müller77e21e82014-09-10 01:00:22 +0200342 if (status != EFI_SUCCESS) {
343 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000344 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200345 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000346
347 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
348 PCI_DEVICE_ID, 1, &(rom->devid));
349
Andre Müller77e21e82014-09-10 01:00:22 +0200350 if (status != EFI_SUCCESS) {
351 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000352 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200353 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000354
355 status = efi_early->call(pci->get_location, pci, &(rom->segment),
356 &(rom->bus), &(rom->device), &(rom->function));
357
358 if (status != EFI_SUCCESS)
359 goto free_struct;
360
361 memcpy(rom->romdata, pci->romimage, pci->romsize);
362 return status;
363
364free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000365 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000366 return status;
367}
368
369static efi_status_t
370setup_efi_pci32(struct boot_params *params, void **pci_handle,
371 unsigned long size)
372{
373 efi_pci_io_protocol_32 *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700374 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000375 u32 *handles = (u32 *)(unsigned long)pci_handle;
376 efi_status_t status;
377 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700378 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000379 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700380
Jan Beulichbc754792013-01-18 12:35:14 +0000381 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700382
383 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000384 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700385
Matt Flemingc116e8d2014-01-16 11:35:43 +0000386 nr_pci = size / sizeof(u32);
387 for (i = 0; i < nr_pci; i++) {
388 struct pci_setup_rom *rom = NULL;
389 u32 h = handles[i];
390
Matt Fleming204b0a12014-03-22 10:09:01 +0000391 status = efi_call_early(handle_protocol, h,
392 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000393
394 if (status != EFI_SUCCESS)
395 continue;
396
397 if (!pci)
398 continue;
399
400 status = __setup_efi_pci32(pci, &rom);
401 if (status != EFI_SUCCESS)
402 continue;
403
404 if (data)
405 data->next = (unsigned long)rom;
406 else
407 params->hdr.setup_data = (unsigned long)rom;
408
409 data = (struct setup_data *)rom;
410
411 }
412
413 return status;
414}
415
416static efi_status_t
417__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
418{
419 struct pci_setup_rom *rom;
420 efi_status_t status;
421 unsigned long size;
422 uint64_t attributes;
423
424 status = efi_early->call(pci->attributes, pci,
425 EfiPciIoAttributeOperationGet, 0,
426 &attributes);
427 if (status != EFI_SUCCESS)
428 return status;
429
430 if (!pci->romimage || !pci->romsize)
431 return EFI_INVALID_PARAMETER;
432
433 size = pci->romsize + sizeof(*rom);
434
Matt Fleming204b0a12014-03-22 10:09:01 +0000435 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
Andre Müller77e21e82014-09-10 01:00:22 +0200436 if (status != EFI_SUCCESS) {
437 efi_printk(sys_table, "Failed to alloc mem for rom\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000438 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200439 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000440
441 rom->data.type = SETUP_PCI;
442 rom->data.len = size - sizeof(struct setup_data);
443 rom->data.next = 0;
444 rom->pcilen = pci->romsize;
445 *__rom = rom;
446
447 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
448 PCI_VENDOR_ID, 1, &(rom->vendor));
449
Andre Müller77e21e82014-09-10 01:00:22 +0200450 if (status != EFI_SUCCESS) {
451 efi_printk(sys_table, "Failed to read rom->vendor\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000452 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200453 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000454
455 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
456 PCI_DEVICE_ID, 1, &(rom->devid));
457
Andre Müller77e21e82014-09-10 01:00:22 +0200458 if (status != EFI_SUCCESS) {
459 efi_printk(sys_table, "Failed to read rom->devid\n");
Matt Flemingc116e8d2014-01-16 11:35:43 +0000460 goto free_struct;
Andre Müller77e21e82014-09-10 01:00:22 +0200461 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000462
463 status = efi_early->call(pci->get_location, pci, &(rom->segment),
464 &(rom->bus), &(rom->device), &(rom->function));
465
466 if (status != EFI_SUCCESS)
467 goto free_struct;
468
469 memcpy(rom->romdata, pci->romimage, pci->romsize);
470 return status;
471
472free_struct:
Matt Fleming204b0a12014-03-22 10:09:01 +0000473 efi_call_early(free_pool, rom);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000474 return status;
475
476}
477
478static efi_status_t
479setup_efi_pci64(struct boot_params *params, void **pci_handle,
480 unsigned long size)
481{
482 efi_pci_io_protocol_64 *pci = NULL;
483 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
484 u64 *handles = (u64 *)(unsigned long)pci_handle;
485 efi_status_t status;
486 unsigned long nr_pci;
487 struct setup_data *data;
488 int i;
489
490 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
491
492 while (data && data->next)
493 data = (struct setup_data *)(unsigned long)data->next;
494
495 nr_pci = size / sizeof(u64);
496 for (i = 0; i < nr_pci; i++) {
497 struct pci_setup_rom *rom = NULL;
498 u64 h = handles[i];
499
Matt Fleming204b0a12014-03-22 10:09:01 +0000500 status = efi_call_early(handle_protocol, h,
501 &pci_proto, (void **)&pci);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000502
503 if (status != EFI_SUCCESS)
504 continue;
505
506 if (!pci)
507 continue;
508
509 status = __setup_efi_pci64(pci, &rom);
510 if (status != EFI_SUCCESS)
511 continue;
512
513 if (data)
514 data->next = (unsigned long)rom;
515 else
516 params->hdr.setup_data = (unsigned long)rom;
517
518 data = (struct setup_data *)rom;
519
520 }
521
522 return status;
523}
524
525static efi_status_t setup_efi_pci(struct boot_params *params)
526{
527 efi_status_t status;
528 void **pci_handle = NULL;
529 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
530 unsigned long size = 0;
531
Matt Fleming204b0a12014-03-22 10:09:01 +0000532 status = efi_call_early(locate_handle,
533 EFI_LOCATE_BY_PROTOCOL,
534 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700535
536 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming204b0a12014-03-22 10:09:01 +0000537 status = efi_call_early(allocate_pool,
538 EFI_LOADER_DATA,
539 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700540
Andre Müller77e21e82014-09-10 01:00:22 +0200541 if (status != EFI_SUCCESS) {
542 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700543 return status;
Andre Müller77e21e82014-09-10 01:00:22 +0200544 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700545
Matt Fleming204b0a12014-03-22 10:09:01 +0000546 status = efi_call_early(locate_handle,
547 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
548 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700549 }
550
551 if (status != EFI_SUCCESS)
552 goto free_handle;
553
Matt Flemingc116e8d2014-01-16 11:35:43 +0000554 if (efi_early->is64)
555 status = setup_efi_pci64(params, pci_handle, size);
556 else
557 status = setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700558
559free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000560 efi_call_early(free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700561 return status;
562}
563
Matt Flemingc116e8d2014-01-16 11:35:43 +0000564static void
565setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
566 struct efi_pixel_bitmask pixel_info, int pixel_format)
Matt Fleming291f3632011-12-12 21:27:52 +0000567{
Matt Fleming291f3632011-12-12 21:27:52 +0000568 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
569 si->lfb_depth = 32;
570 si->lfb_linelength = pixels_per_scan_line * 4;
571 si->red_size = 8;
572 si->red_pos = 0;
573 si->green_size = 8;
574 si->green_pos = 8;
575 si->blue_size = 8;
576 si->blue_pos = 16;
577 si->rsvd_size = 8;
578 si->rsvd_pos = 24;
579 } else if (pixel_format == PIXEL_BGR_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 = 16;
584 si->green_size = 8;
585 si->green_pos = 8;
586 si->blue_size = 8;
587 si->blue_pos = 0;
588 si->rsvd_size = 8;
589 si->rsvd_pos = 24;
590 } else if (pixel_format == PIXEL_BIT_MASK) {
591 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
592 find_bits(pixel_info.green_mask, &si->green_pos,
593 &si->green_size);
594 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
595 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
596 &si->rsvd_size);
597 si->lfb_depth = si->red_size + si->green_size +
598 si->blue_size + si->rsvd_size;
599 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
600 } else {
601 si->lfb_depth = 4;
602 si->lfb_linelength = si->lfb_width / 2;
603 si->red_size = 0;
604 si->red_pos = 0;
605 si->green_size = 0;
606 si->green_pos = 0;
607 si->blue_size = 0;
608 si->blue_pos = 0;
609 si->rsvd_size = 0;
610 si->rsvd_pos = 0;
611 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000612}
613
614static efi_status_t
615__gop_query32(struct efi_graphics_output_protocol_32 *gop32,
616 struct efi_graphics_output_mode_info **info,
617 unsigned long *size, u32 *fb_base)
618{
619 struct efi_graphics_output_protocol_mode_32 *mode;
620 efi_status_t status;
621 unsigned long m;
622
623 m = gop32->mode;
624 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
625
626 status = efi_early->call(gop32->query_mode, gop32,
627 mode->mode, size, info);
628 if (status != EFI_SUCCESS)
629 return status;
630
631 *fb_base = mode->frame_buffer_base;
632 return status;
633}
634
635static efi_status_t
636setup_gop32(struct screen_info *si, efi_guid_t *proto,
637 unsigned long size, void **gop_handle)
638{
639 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
640 unsigned long nr_gops;
641 u16 width, height;
642 u32 pixels_per_scan_line;
643 u32 fb_base;
644 struct efi_pixel_bitmask pixel_info;
645 int pixel_format;
646 efi_status_t status;
647 u32 *handles = (u32 *)(unsigned long)gop_handle;
648 int i;
649
650 first_gop = NULL;
651 gop32 = NULL;
652
653 nr_gops = size / sizeof(u32);
654 for (i = 0; i < nr_gops; i++) {
655 struct efi_graphics_output_mode_info *info = NULL;
656 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
657 bool conout_found = false;
658 void *dummy = NULL;
659 u32 h = handles[i];
660
Matt Fleming204b0a12014-03-22 10:09:01 +0000661 status = efi_call_early(handle_protocol, h,
662 proto, (void **)&gop32);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000663 if (status != EFI_SUCCESS)
664 continue;
665
Matt Fleming204b0a12014-03-22 10:09:01 +0000666 status = efi_call_early(handle_protocol, h,
667 &conout_proto, &dummy);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000668 if (status == EFI_SUCCESS)
669 conout_found = true;
670
671 status = __gop_query32(gop32, &info, &size, &fb_base);
672 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
673 /*
674 * Systems that use the UEFI Console Splitter may
675 * provide multiple GOP devices, not all of which are
676 * backed by real hardware. The workaround is to search
677 * for a GOP implementing the ConOut protocol, and if
678 * one isn't found, to just fall back to the first GOP.
679 */
680 width = info->horizontal_resolution;
681 height = info->vertical_resolution;
682 pixel_format = info->pixel_format;
683 pixel_info = info->pixel_information;
684 pixels_per_scan_line = info->pixels_per_scan_line;
685
686 /*
687 * Once we've found a GOP supporting ConOut,
688 * don't bother looking any further.
689 */
690 first_gop = gop32;
691 if (conout_found)
692 break;
693 }
694 }
695
696 /* Did we find any GOPs? */
697 if (!first_gop)
698 goto out;
699
700 /* EFI framebuffer */
701 si->orig_video_isVGA = VIDEO_TYPE_EFI;
702
703 si->lfb_width = width;
704 si->lfb_height = height;
705 si->lfb_base = fb_base;
706 si->pages = 1;
707
708 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
Matt Fleming291f3632011-12-12 21:27:52 +0000709
Matthew Garrette9b10952012-07-27 17:20:49 -0400710 si->lfb_size = si->lfb_linelength * si->lfb_height;
711
Matthew Garrettf462ed92012-07-27 12:58:53 -0400712 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000713out:
714 return status;
715}
716
717static efi_status_t
718__gop_query64(struct efi_graphics_output_protocol_64 *gop64,
719 struct efi_graphics_output_mode_info **info,
720 unsigned long *size, u32 *fb_base)
721{
722 struct efi_graphics_output_protocol_mode_64 *mode;
723 efi_status_t status;
724 unsigned long m;
725
726 m = gop64->mode;
727 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
728
729 status = efi_early->call(gop64->query_mode, gop64,
730 mode->mode, size, info);
731 if (status != EFI_SUCCESS)
732 return status;
733
734 *fb_base = mode->frame_buffer_base;
735 return status;
736}
737
738static efi_status_t
739setup_gop64(struct screen_info *si, efi_guid_t *proto,
740 unsigned long size, void **gop_handle)
741{
742 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
743 unsigned long nr_gops;
744 u16 width, height;
745 u32 pixels_per_scan_line;
746 u32 fb_base;
747 struct efi_pixel_bitmask pixel_info;
748 int pixel_format;
749 efi_status_t status;
750 u64 *handles = (u64 *)(unsigned long)gop_handle;
751 int i;
752
753 first_gop = NULL;
754 gop64 = NULL;
755
756 nr_gops = size / sizeof(u64);
757 for (i = 0; i < nr_gops; i++) {
758 struct efi_graphics_output_mode_info *info = NULL;
759 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
760 bool conout_found = false;
761 void *dummy = NULL;
762 u64 h = handles[i];
763
Matt Fleming204b0a12014-03-22 10:09:01 +0000764 status = efi_call_early(handle_protocol, h,
765 proto, (void **)&gop64);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000766 if (status != EFI_SUCCESS)
767 continue;
768
Matt Fleming204b0a12014-03-22 10:09:01 +0000769 status = efi_call_early(handle_protocol, h,
770 &conout_proto, &dummy);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000771 if (status == EFI_SUCCESS)
772 conout_found = true;
773
774 status = __gop_query64(gop64, &info, &size, &fb_base);
775 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
776 /*
777 * Systems that use the UEFI Console Splitter may
778 * provide multiple GOP devices, not all of which are
779 * backed by real hardware. The workaround is to search
780 * for a GOP implementing the ConOut protocol, and if
781 * one isn't found, to just fall back to the first GOP.
782 */
783 width = info->horizontal_resolution;
784 height = info->vertical_resolution;
785 pixel_format = info->pixel_format;
786 pixel_info = info->pixel_information;
787 pixels_per_scan_line = info->pixels_per_scan_line;
788
789 /*
790 * Once we've found a GOP supporting ConOut,
791 * don't bother looking any further.
792 */
793 first_gop = gop64;
794 if (conout_found)
795 break;
796 }
797 }
798
799 /* Did we find any GOPs? */
800 if (!first_gop)
801 goto out;
802
803 /* EFI framebuffer */
804 si->orig_video_isVGA = VIDEO_TYPE_EFI;
805
806 si->lfb_width = width;
807 si->lfb_height = height;
808 si->lfb_base = fb_base;
809 si->pages = 1;
810
811 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
812
813 si->lfb_size = si->lfb_linelength * si->lfb_height;
814
815 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
816out:
817 return status;
818}
819
820/*
821 * See if we have Graphics Output Protocol
822 */
823static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
824 unsigned long size)
825{
826 efi_status_t status;
827 void **gop_handle = NULL;
828
Matt Fleming204b0a12014-03-22 10:09:01 +0000829 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
830 size, (void **)&gop_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000831 if (status != EFI_SUCCESS)
832 return status;
833
Matt Fleming204b0a12014-03-22 10:09:01 +0000834 status = efi_call_early(locate_handle,
835 EFI_LOCATE_BY_PROTOCOL,
836 proto, NULL, &size, gop_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000837 if (status != EFI_SUCCESS)
838 goto free_handle;
839
840 if (efi_early->is64)
841 status = setup_gop64(si, proto, size, gop_handle);
842 else
843 status = setup_gop32(si, proto, size, gop_handle);
Matthew Garrettf462ed92012-07-27 12:58:53 -0400844
Matt Fleming291f3632011-12-12 21:27:52 +0000845free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000846 efi_call_early(free_pool, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000847 return status;
848}
849
Matt Flemingc116e8d2014-01-16 11:35:43 +0000850static efi_status_t
851setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000852{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000853 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
854 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000855 unsigned long nr_ugas;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000856 u32 *handles = (u32 *)uga_handle;;
Matt Fleming291f3632011-12-12 21:27:52 +0000857 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000858 int i;
859
Matt Fleming291f3632011-12-12 21:27:52 +0000860 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000861 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000862 for (i = 0; i < nr_ugas; i++) {
863 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000864 u32 w, h, depth, refresh;
865 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000866 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000867
Matt Fleming204b0a12014-03-22 10:09:01 +0000868 status = efi_call_early(handle_protocol, handle,
869 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000870 if (status != EFI_SUCCESS)
871 continue;
872
Matt Fleming204b0a12014-03-22 10:09:01 +0000873 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000874
Matt Fleming54b52d82014-01-10 15:27:14 +0000875 status = efi_early->call((unsigned long)uga->get_mode, uga,
876 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000877 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000878 *width = w;
879 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000880
881 /*
882 * Once we've found a UGA supporting PCIIO,
883 * don't bother looking any further.
884 */
885 if (pciio)
886 break;
887
888 first_uga = uga;
889 }
890 }
891
Matt Flemingc116e8d2014-01-16 11:35:43 +0000892 return status;
893}
894
895static efi_status_t
896setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
897{
898 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
899 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
900 unsigned long nr_ugas;
901 u64 *handles = (u64 *)uga_handle;;
902 efi_status_t status;
903 int i;
904
905 first_uga = NULL;
906 nr_ugas = size / sizeof(u64);
907 for (i = 0; i < nr_ugas; i++) {
908 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
909 u32 w, h, depth, refresh;
910 void *pciio;
911 u64 handle = handles[i];
912
Matt Fleming204b0a12014-03-22 10:09:01 +0000913 status = efi_call_early(handle_protocol, handle,
914 &uga_proto, (void **)&uga);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000915 if (status != EFI_SUCCESS)
916 continue;
917
Matt Fleming204b0a12014-03-22 10:09:01 +0000918 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000919
920 status = efi_early->call((unsigned long)uga->get_mode, uga,
921 &w, &h, &depth, &refresh);
922 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
923 *width = w;
924 *height = h;
925
926 /*
927 * Once we've found a UGA supporting PCIIO,
928 * don't bother looking any further.
929 */
930 if (pciio)
931 break;
932
933 first_uga = uga;
934 }
935 }
936
937 return status;
938}
939
940/*
941 * See if we have Universal Graphics Adapter (UGA) protocol
942 */
943static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
944 unsigned long size)
945{
946 efi_status_t status;
947 u32 width, height;
948 void **uga_handle = NULL;
949
Matt Fleming204b0a12014-03-22 10:09:01 +0000950 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
951 size, (void **)&uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000952 if (status != EFI_SUCCESS)
953 return status;
954
Matt Fleming204b0a12014-03-22 10:09:01 +0000955 status = efi_call_early(locate_handle,
956 EFI_LOCATE_BY_PROTOCOL,
957 uga_proto, NULL, &size, uga_handle);
Matt Flemingc116e8d2014-01-16 11:35:43 +0000958 if (status != EFI_SUCCESS)
959 goto free_handle;
960
961 height = 0;
962 width = 0;
963
964 if (efi_early->is64)
965 status = setup_uga64(uga_handle, size, &width, &height);
966 else
967 status = setup_uga32(uga_handle, size, &width, &height);
968
969 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000970 goto free_handle;
971
972 /* EFI framebuffer */
973 si->orig_video_isVGA = VIDEO_TYPE_EFI;
974
975 si->lfb_depth = 32;
976 si->lfb_width = width;
977 si->lfb_height = height;
978
979 si->red_size = 8;
980 si->red_pos = 16;
981 si->green_size = 8;
982 si->green_pos = 8;
983 si->blue_size = 8;
984 si->blue_pos = 0;
985 si->rsvd_size = 8;
986 si->rsvd_pos = 24;
987
Matt Fleming291f3632011-12-12 21:27:52 +0000988free_handle:
Matt Fleming204b0a12014-03-22 10:09:01 +0000989 efi_call_early(free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000990 return status;
991}
992
993void setup_graphics(struct boot_params *boot_params)
994{
995 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
996 struct screen_info *si;
997 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
998 efi_status_t status;
999 unsigned long size;
1000 void **gop_handle = NULL;
1001 void **uga_handle = NULL;
1002
1003 si = &boot_params->screen_info;
1004 memset(si, 0, sizeof(*si));
1005
1006 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +00001007 status = efi_call_early(locate_handle,
1008 EFI_LOCATE_BY_PROTOCOL,
1009 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001010 if (status == EFI_BUFFER_TOO_SMALL)
1011 status = setup_gop(si, &graphics_proto, size);
1012
1013 if (status != EFI_SUCCESS) {
1014 size = 0;
Matt Fleming204b0a12014-03-22 10:09:01 +00001015 status = efi_call_early(locate_handle,
1016 EFI_LOCATE_BY_PROTOCOL,
1017 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001018 if (status == EFI_BUFFER_TOO_SMALL)
1019 setup_uga(si, &uga_proto, size);
1020 }
1021}
1022
Matt Fleming291f3632011-12-12 21:27:52 +00001023/*
1024 * Because the x86 boot code expects to be passed a boot_params we
1025 * need to create one ourselves (usually the bootloader would create
1026 * one for us).
Matt Fleming7e8213c2014-04-08 13:14:00 +01001027 *
1028 * The caller is responsible for filling out ->code32_start in the
1029 * returned boot_params.
Matt Fleming291f3632011-12-12 21:27:52 +00001030 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001031struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +00001032{
Matt Fleming9ca8f722012-07-19 10:23:48 +01001033 struct boot_params *boot_params;
1034 struct sys_desc_table *sdt;
1035 struct apm_bios_info *bi;
1036 struct setup_header *hdr;
1037 struct efi_info *efi;
1038 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +00001039 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001040 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +00001041 int options_size = 0;
1042 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -07001043 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001044 u16 *s2;
1045 u8 *s1;
1046 int i;
Roy Franz46f45822013-09-22 15:45:39 -07001047 unsigned long ramdisk_addr;
1048 unsigned long ramdisk_size;
Yinghai Lu4bf71112014-06-14 12:23:41 -07001049 unsigned long initrd_addr_max;
Matt Fleming291f3632011-12-12 21:27:52 +00001050
Matt Fleming54b52d82014-01-10 15:27:14 +00001051 efi_early = c;
1052 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1053 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001054
1055 /* Check if we were booted by the EFI firmware */
1056 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1057 return NULL;
1058
Matt Fleming54b52d82014-01-10 15:27:14 +00001059 if (efi_early->is64)
1060 setup_boot_services64(efi_early);
1061 else
1062 setup_boot_services32(efi_early);
1063
Matt Fleming204b0a12014-03-22 10:09:01 +00001064 status = efi_call_early(handle_protocol, handle,
1065 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001066 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001067 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001068 return NULL;
1069 }
1070
Roy Franz40e45302013-09-22 15:45:29 -07001071 status = efi_low_alloc(sys_table, 0x4000, 1,
1072 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001073 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001074 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001075 return NULL;
1076 }
1077
1078 memset(boot_params, 0x0, 0x4000);
1079
1080 hdr = &boot_params->hdr;
1081 efi = &boot_params->efi_info;
1082 bi = &boot_params->apm_bios_info;
1083 sdt = &boot_params->sys_desc_table;
1084
1085 /* Copy the second sector to boot_params */
1086 memcpy(&hdr->jump, image->image_base + 512, 512);
1087
1088 /*
1089 * Fill out some of the header fields ourselves because the
1090 * EFI firmware loader doesn't load the first sector.
1091 */
1092 hdr->root_flags = 1;
1093 hdr->vid_mode = 0xffff;
1094 hdr->boot_flag = 0xAA55;
1095
Matt Fleming291f3632011-12-12 21:27:52 +00001096 hdr->type_of_loader = 0x21;
1097
1098 /* Convert unicode cmdline to ascii */
H. Peter Anvinc625d1c2013-09-20 09:55:39 -05001099 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
Roy Franz5fef3872013-09-22 15:45:33 -07001100 if (!cmdline_ptr)
1101 goto fail;
1102 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001103
1104 hdr->ramdisk_image = 0;
1105 hdr->ramdisk_size = 0;
1106
Matt Fleming291f3632011-12-12 21:27:52 +00001107 /* Clear APM BIOS info */
1108 memset(bi, 0, sizeof(*bi));
1109
1110 memset(sdt, 0, sizeof(*sdt));
1111
Yinghai Lu4bf71112014-06-14 12:23:41 -07001112 if (hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)
1113 initrd_addr_max = -1UL;
1114 else
1115 initrd_addr_max = hdr->initrd_addr_max;
1116
Matt Fleming5a17dae2014-08-05 11:52:11 +01001117 status = efi_parse_options(cmdline_ptr);
1118 if (status != EFI_SUCCESS)
1119 goto fail2;
1120
Roy Franz46f45822013-09-22 15:45:39 -07001121 status = handle_cmdline_files(sys_table, image,
1122 (char *)(unsigned long)hdr->cmd_line_ptr,
Yinghai Lu4bf71112014-06-14 12:23:41 -07001123 "initrd=", initrd_addr_max,
Roy Franz46f45822013-09-22 15:45:39 -07001124 &ramdisk_addr, &ramdisk_size);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001125 if (status != EFI_SUCCESS)
1126 goto fail2;
Yinghai Lu4bf71112014-06-14 12:23:41 -07001127 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1128 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
1129 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1130 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001131
1132 return boot_params;
1133fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -07001134 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001135fail:
Roy Franz40e45302013-09-22 15:45:29 -07001136 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001137 return NULL;
1138}
1139
Linn Crosettod2078d52013-09-22 19:59:08 -06001140static void add_e820ext(struct boot_params *params,
1141 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +01001142{
Linn Crosettod2078d52013-09-22 19:59:08 -06001143 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001144 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -06001145 unsigned long size;
1146
1147 e820ext->type = SETUP_E820_EXT;
1148 e820ext->len = nr_entries * sizeof(struct e820entry);
1149 e820ext->next = 0;
1150
1151 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1152
1153 while (data && data->next)
1154 data = (struct setup_data *)(unsigned long)data->next;
1155
1156 if (data)
1157 data->next = (unsigned long)e820ext;
1158 else
1159 params->hdr.setup_data = (unsigned long)e820ext;
1160}
1161
1162static efi_status_t setup_e820(struct boot_params *params,
1163 struct setup_data *e820ext, u32 e820ext_size)
1164{
1165 struct e820entry *e820_map = &params->e820_map[0];
1166 struct efi_info *efi = &params->efi_info;
1167 struct e820entry *prev = NULL;
1168 u32 nr_entries;
1169 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001170 int i;
Matt Fleming291f3632011-12-12 21:27:52 +00001171
Matt Fleming291f3632011-12-12 21:27:52 +00001172 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001173 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1174
1175 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +00001176 efi_memory_desc_t *d;
1177 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001178 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +00001179
Linn Crosettod2078d52013-09-22 19:59:08 -06001180 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
Matt Fleming291f3632011-12-12 21:27:52 +00001181 switch (d->type) {
1182 case EFI_RESERVED_TYPE:
1183 case EFI_RUNTIME_SERVICES_CODE:
1184 case EFI_RUNTIME_SERVICES_DATA:
1185 case EFI_MEMORY_MAPPED_IO:
1186 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1187 case EFI_PAL_CODE:
1188 e820_type = E820_RESERVED;
1189 break;
1190
1191 case EFI_UNUSABLE_MEMORY:
1192 e820_type = E820_UNUSABLE;
1193 break;
1194
1195 case EFI_ACPI_RECLAIM_MEMORY:
1196 e820_type = E820_ACPI;
1197 break;
1198
1199 case EFI_LOADER_CODE:
1200 case EFI_LOADER_DATA:
1201 case EFI_BOOT_SERVICES_CODE:
1202 case EFI_BOOT_SERVICES_DATA:
1203 case EFI_CONVENTIONAL_MEMORY:
1204 e820_type = E820_RAM;
1205 break;
1206
1207 case EFI_ACPI_MEMORY_NVS:
1208 e820_type = E820_NVS;
1209 break;
1210
1211 default:
1212 continue;
1213 }
1214
1215 /* Merge adjacent mappings */
1216 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -06001217 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +00001218 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -06001219 continue;
Matt Fleming291f3632011-12-12 21:27:52 +00001220 }
Linn Crosettod2078d52013-09-22 19:59:08 -06001221
1222 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1223 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1224 sizeof(struct setup_data);
1225
1226 if (!e820ext || e820ext_size < need)
1227 return EFI_BUFFER_TOO_SMALL;
1228
1229 /* boot_params map full, switch to e820 extended */
1230 e820_map = (struct e820entry *)e820ext->data;
1231 }
1232
1233 e820_map->addr = d->phys_addr;
1234 e820_map->size = d->num_pages << PAGE_SHIFT;
1235 e820_map->type = e820_type;
1236 prev = e820_map++;
1237 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +00001238 }
1239
Linn Crosettod2078d52013-09-22 19:59:08 -06001240 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1241 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1242
1243 add_e820ext(params, e820ext, nr_e820ext);
1244 nr_entries -= nr_e820ext;
1245 }
1246
1247 params->e820_entries = (u8)nr_entries;
1248
1249 return EFI_SUCCESS;
1250}
1251
1252static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1253 u32 *e820ext_size)
1254{
1255 efi_status_t status;
1256 unsigned long size;
1257
1258 size = sizeof(struct setup_data) +
1259 sizeof(struct e820entry) * nr_desc;
1260
1261 if (*e820ext) {
Matt Fleming204b0a12014-03-22 10:09:01 +00001262 efi_call_early(free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001263 *e820ext = NULL;
1264 *e820ext_size = 0;
1265 }
1266
Matt Fleming204b0a12014-03-22 10:09:01 +00001267 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1268 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001269 if (status == EFI_SUCCESS)
1270 *e820ext_size = size;
1271
1272 return status;
1273}
1274
1275static efi_status_t exit_boot(struct boot_params *boot_params,
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001276 void *handle, bool is64)
Linn Crosettod2078d52013-09-22 19:59:08 -06001277{
1278 struct efi_info *efi = &boot_params->efi_info;
1279 unsigned long map_sz, key, desc_size;
1280 efi_memory_desc_t *mem_map;
1281 struct setup_data *e820ext;
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001282 const char *signature;
Linn Crosettod2078d52013-09-22 19:59:08 -06001283 __u32 e820ext_size;
1284 __u32 nr_desc, prev_nr_desc;
1285 efi_status_t status;
1286 __u32 desc_version;
1287 bool called_exit = false;
1288 u8 nr_entries;
1289 int i;
1290
1291 nr_desc = 0;
1292 e820ext = NULL;
1293 e820ext_size = 0;
1294
1295get_map:
1296 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1297 &desc_version, &key);
1298
1299 if (status != EFI_SUCCESS)
1300 return status;
1301
1302 prev_nr_desc = nr_desc;
1303 nr_desc = map_sz / desc_size;
1304 if (nr_desc > prev_nr_desc &&
1305 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1306 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1307
1308 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1309 if (status != EFI_SUCCESS)
1310 goto free_mem_map;
1311
Matt Fleming204b0a12014-03-22 10:09:01 +00001312 efi_call_early(free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001313 goto get_map; /* Allocated memory, get map again */
1314 }
1315
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001316 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1317 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1318
Linn Crosettod2078d52013-09-22 19:59:08 -06001319 efi->efi_systab = (unsigned long)sys_table;
1320 efi->efi_memdesc_size = desc_size;
1321 efi->efi_memdesc_version = desc_version;
1322 efi->efi_memmap = (unsigned long)mem_map;
1323 efi->efi_memmap_size = map_sz;
1324
1325#ifdef CONFIG_X86_64
1326 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1327 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1328#endif
1329
1330 /* Might as well exit boot services now */
Matt Fleming204b0a12014-03-22 10:09:01 +00001331 status = efi_call_early(exit_boot_services, handle, key);
Linn Crosettod2078d52013-09-22 19:59:08 -06001332 if (status != EFI_SUCCESS) {
1333 /*
1334 * ExitBootServices() will fail if any of the event
1335 * handlers change the memory map. In which case, we
1336 * must be prepared to retry, but only once so that
1337 * we're guaranteed to exit on repeated failures instead
1338 * of spinning forever.
1339 */
1340 if (called_exit)
1341 goto free_mem_map;
1342
1343 called_exit = true;
Matt Fleming204b0a12014-03-22 10:09:01 +00001344 efi_call_early(free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001345 goto get_map;
1346 }
1347
1348 /* Historic? */
1349 boot_params->alt_mem_k = 32 * 1024;
1350
1351 status = setup_e820(boot_params, e820ext, e820ext_size);
1352 if (status != EFI_SUCCESS)
1353 return status;
Matt Fleming291f3632011-12-12 21:27:52 +00001354
1355 return EFI_SUCCESS;
1356
1357free_mem_map:
Matt Fleming204b0a12014-03-22 10:09:01 +00001358 efi_call_early(free_pool, mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +00001359 return status;
1360}
1361
Matt Fleming9ca8f722012-07-19 10:23:48 +01001362/*
1363 * On success we return a pointer to a boot_params structure, and NULL
1364 * on failure.
1365 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001366struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +01001367 struct boot_params *boot_params)
1368{
Matt Fleming54b52d82014-01-10 15:27:14 +00001369 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001370 efi_loaded_image_t *image;
1371 struct setup_header *hdr = &boot_params->hdr;
1372 efi_status_t status;
1373 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +00001374 void *handle;
1375 efi_system_table_t *_table;
1376 bool is64;
1377
1378 efi_early = c;
1379
1380 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1381 handle = (void *)(unsigned long)efi_early->image_handle;
1382 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001383
1384 sys_table = _table;
1385
1386 /* Check if we were booted by the EFI firmware */
1387 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1388 goto fail;
1389
Matt Fleming54b52d82014-01-10 15:27:14 +00001390 if (is64)
1391 setup_boot_services64(efi_early);
1392 else
1393 setup_boot_services32(efi_early);
1394
Matt Fleming9ca8f722012-07-19 10:23:48 +01001395 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001396
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001397 status = setup_efi_pci(boot_params);
1398 if (status != EFI_SUCCESS) {
1399 efi_printk(sys_table, "setup_efi_pci() failed!\n");
1400 }
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001401
Matt Fleming204b0a12014-03-22 10:09:01 +00001402 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1403 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001404 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001405 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001406 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001407 }
Matt Fleming291f3632011-12-12 21:27:52 +00001408
1409 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001410 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001411 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001412 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001413 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001414 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001415 }
Matt Fleming291f3632011-12-12 21:27:52 +00001416
Matt Fleming9ca8f722012-07-19 10:23:48 +01001417 /*
1418 * If the kernel isn't already loaded at the preferred load
1419 * address, relocate it.
1420 */
1421 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001422 unsigned long bzimage_addr = hdr->code32_start;
1423 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1424 hdr->init_size, hdr->init_size,
1425 hdr->pref_address,
1426 hdr->kernel_alignment);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001427 if (status != EFI_SUCCESS) {
1428 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001429 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001430 }
Roy Franz4a9f3a72013-09-22 15:45:32 -07001431
1432 hdr->pref_address = hdr->code32_start;
1433 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001434 }
1435
Matt Flemingb8ff87a2014-01-10 15:54:31 +00001436 status = exit_boot(boot_params, handle, is64);
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001437 if (status != EFI_SUCCESS) {
1438 efi_printk(sys_table, "exit_boot() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001439 goto fail;
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001440 }
Matt Fleming291f3632011-12-12 21:27:52 +00001441
1442 memset((char *)gdt->address, 0x0, gdt->size);
1443 desc = (struct desc_struct *)gdt->address;
1444
1445 /* The first GDT is a dummy and the second is unused. */
1446 desc += 2;
1447
1448 desc->limit0 = 0xffff;
1449 desc->base0 = 0x0000;
1450 desc->base1 = 0x0000;
1451 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1452 desc->s = DESC_TYPE_CODE_DATA;
1453 desc->dpl = 0;
1454 desc->p = 1;
1455 desc->limit = 0xf;
1456 desc->avl = 0;
1457 desc->l = 0;
1458 desc->d = SEG_OP_SIZE_32BIT;
1459 desc->g = SEG_GRANULARITY_4KB;
1460 desc->base2 = 0x00;
1461
1462 desc++;
1463 desc->limit0 = 0xffff;
1464 desc->base0 = 0x0000;
1465 desc->base1 = 0x0000;
1466 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1467 desc->s = DESC_TYPE_CODE_DATA;
1468 desc->dpl = 0;
1469 desc->p = 1;
1470 desc->limit = 0xf;
1471 desc->avl = 0;
1472 desc->l = 0;
1473 desc->d = SEG_OP_SIZE_32BIT;
1474 desc->g = SEG_GRANULARITY_4KB;
1475 desc->base2 = 0x00;
1476
1477#ifdef CONFIG_X86_64
1478 /* Task segment value */
1479 desc++;
1480 desc->limit0 = 0x0000;
1481 desc->base0 = 0x0000;
1482 desc->base1 = 0x0000;
1483 desc->type = SEG_TYPE_TSS;
1484 desc->s = 0;
1485 desc->dpl = 0;
1486 desc->p = 1;
1487 desc->limit = 0x0;
1488 desc->avl = 0;
1489 desc->l = 0;
1490 desc->d = 0;
1491 desc->g = SEG_GRANULARITY_4KB;
1492 desc->base2 = 0x00;
1493#endif /* CONFIG_X86_64 */
1494
Matt Fleming291f3632011-12-12 21:27:52 +00001495 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001496 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001497
1498 return boot_params;
1499fail:
Ulf Winkelvosfb86b242014-07-10 02:12:41 +02001500 efi_printk(sys_table, "efi_main() failed!\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001501 return NULL;
1502}