blob: ab1f3a2f1e1eea02d12f9e7142e4f1f7769e7f4b [file] [log] [blame]
Matt Fleming291f3632011-12-12 21:27:52 +00001/* -----------------------------------------------------------------------
2 *
3 * Copyright 2011 Intel Corporation; author Matt Fleming
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
7 *
8 * ----------------------------------------------------------------------- */
9
10#include <linux/efi.h>
Matthew Garrettdd5fc852012-12-05 14:33:26 -070011#include <linux/pci.h>
Matt Fleming291f3632011-12-12 21:27:52 +000012#include <asm/efi.h>
13#include <asm/setup.h>
14#include <asm/desc.h>
15
Matt Fleming0f905a42012-11-20 13:07:46 +000016#undef memcpy /* Use memcpy from misc.c */
17
Matt Fleming291f3632011-12-12 21:27:52 +000018#include "eboot.h"
19
20static efi_system_table_t *sys_table;
21
Matt Fleming54b52d82014-01-10 15:27:14 +000022static struct efi_config *efi_early;
23
24#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
48static void efi_printk(efi_system_table_t *, char *);
49static void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
50
51static efi_status_t
Matt Flemingc116e8d2014-01-16 11:35:43 +000052__file_size32(void *__fh, efi_char16_t *filename_16,
53 void **handle, u64 *file_sz)
Matt Fleming54b52d82014-01-10 15:27:14 +000054{
Matt Flemingc116e8d2014-01-16 11:35:43 +000055 efi_file_handle_32_t *h, *fh = __fh;
Matt Fleming54b52d82014-01-10 15:27:14 +000056 efi_file_info_t *info;
57 efi_status_t status;
58 efi_guid_t info_guid = EFI_FILE_INFO_ID;
59 u32 info_sz;
60
61 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
62 EFI_FILE_MODE_READ, (u64)0);
63 if (status != EFI_SUCCESS) {
64 efi_printk(sys_table, "Failed to open file: ");
65 efi_char16_printk(sys_table, filename_16);
66 efi_printk(sys_table, "\n");
67 return status;
68 }
69
70 *handle = h;
71
72 info_sz = 0;
73 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
74 &info_sz, NULL);
75 if (status != EFI_BUFFER_TOO_SMALL) {
76 efi_printk(sys_table, "Failed to get file info size\n");
77 return status;
78 }
79
80grow:
81 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
82 info_sz, (void **)&info);
83 if (status != EFI_SUCCESS) {
84 efi_printk(sys_table, "Failed to alloc mem for file info\n");
85 return status;
86 }
87
88 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
89 &info_sz, info);
90 if (status == EFI_BUFFER_TOO_SMALL) {
91 efi_early->call(efi_early->free_pool, info);
92 goto grow;
93 }
94
95 *file_sz = info->file_size;
96 efi_early->call(efi_early->free_pool, info);
97
98 if (status != EFI_SUCCESS)
99 efi_printk(sys_table, "Failed to get initrd info\n");
100
101 return status;
102}
103
Matt Flemingc116e8d2014-01-16 11:35:43 +0000104static efi_status_t
105__file_size64(void *__fh, efi_char16_t *filename_16,
106 void **handle, u64 *file_sz)
107{
108 efi_file_handle_64_t *h, *fh = __fh;
109 efi_file_info_t *info;
110 efi_status_t status;
111 efi_guid_t info_guid = EFI_FILE_INFO_ID;
112 u32 info_sz;
113
114 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
115 EFI_FILE_MODE_READ, (u64)0);
116 if (status != EFI_SUCCESS) {
117 efi_printk(sys_table, "Failed to open file: ");
118 efi_char16_printk(sys_table, filename_16);
119 efi_printk(sys_table, "\n");
120 return status;
121 }
122
123 *handle = h;
124
125 info_sz = 0;
126 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
127 &info_sz, NULL);
128 if (status != EFI_BUFFER_TOO_SMALL) {
129 efi_printk(sys_table, "Failed to get file info size\n");
130 return status;
131 }
132
133grow:
134 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
135 info_sz, (void **)&info);
136 if (status != EFI_SUCCESS) {
137 efi_printk(sys_table, "Failed to alloc mem for file info\n");
138 return status;
139 }
140
141 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
142 &info_sz, info);
143 if (status == EFI_BUFFER_TOO_SMALL) {
144 efi_early->call(efi_early->free_pool, info);
145 goto grow;
146 }
147
148 *file_sz = info->file_size;
149 efi_early->call(efi_early->free_pool, info);
150
151 if (status != EFI_SUCCESS)
152 efi_printk(sys_table, "Failed to get initrd info\n");
153
154 return status;
155}
156static efi_status_t
157efi_file_size(efi_system_table_t *sys_table, void *__fh,
158 efi_char16_t *filename_16, void **handle, u64 *file_sz)
159{
160 if (efi_early->is64)
161 return __file_size64(__fh, filename_16, handle, file_sz);
162
163 return __file_size32(__fh, filename_16, handle, file_sz);
164}
165
Matt Fleming54b52d82014-01-10 15:27:14 +0000166static inline efi_status_t
167efi_file_read(void *__fh, void *handle, unsigned long *size, void *addr)
168{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000169 unsigned long func;
170
171 if (efi_early->is64) {
172 efi_file_handle_64_t *fh = __fh;
173
174 func = (unsigned long)fh->read;
175 return efi_early->call(func, handle, size, addr);
176 } else {
177 efi_file_handle_32_t *fh = __fh;
178
179 func = (unsigned long)fh->read;
180 return efi_early->call(func, handle, size, addr);
181 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000182}
183
184static inline efi_status_t efi_file_close(void *__fh, void *handle)
185{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000186 if (efi_early->is64) {
187 efi_file_handle_64_t *fh = __fh;
Matt Fleming54b52d82014-01-10 15:27:14 +0000188
Matt Flemingc116e8d2014-01-16 11:35:43 +0000189 return efi_early->call((unsigned long)fh->close, handle);
190 } else {
191 efi_file_handle_32_t *fh = __fh;
192
193 return efi_early->call((unsigned long)fh->close, handle);
194 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000195}
196
Matt Flemingc116e8d2014-01-16 11:35:43 +0000197static inline efi_status_t __open_volume32(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000198{
199 efi_file_io_interface_t *io;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000200 efi_loaded_image_32_t *image = __image;
201 efi_file_handle_32_t *fh;
Matt Fleming54b52d82014-01-10 15:27:14 +0000202 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
203 efi_status_t status;
204 void *handle = (void *)(unsigned long)image->device_handle;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000205 unsigned long func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000206
207 status = efi_early->call(efi_early->handle_protocol, handle,
208 &fs_proto, (void **)&io);
209 if (status != EFI_SUCCESS) {
210 efi_printk(sys_table, "Failed to handle fs_proto\n");
211 return status;
212 }
213
214 func = (unsigned long)io->open_volume;
215 status = efi_early->call(func, io, &fh);
216 if (status != EFI_SUCCESS)
217 efi_printk(sys_table, "Failed to open volume\n");
218
219 *__fh = fh;
220 return status;
221}
222
Matt Flemingc116e8d2014-01-16 11:35:43 +0000223static inline efi_status_t __open_volume64(void *__image, void **__fh)
Matt Fleming54b52d82014-01-10 15:27:14 +0000224{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000225 efi_file_io_interface_t *io;
226 efi_loaded_image_64_t *image = __image;
227 efi_file_handle_64_t *fh;
228 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
229 efi_status_t status;
230 void *handle = (void *)(unsigned long)image->device_handle;
231 unsigned long func;
232
233 status = efi_early->call(efi_early->handle_protocol, handle,
234 &fs_proto, (void **)&io);
235 if (status != EFI_SUCCESS) {
236 efi_printk(sys_table, "Failed to handle fs_proto\n");
237 return status;
238 }
239
240 func = (unsigned long)io->open_volume;
241 status = efi_early->call(func, io, &fh);
242 if (status != EFI_SUCCESS)
243 efi_printk(sys_table, "Failed to open volume\n");
244
245 *__fh = fh;
246 return status;
247}
248
249static inline efi_status_t
250efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
251{
252 if (efi_early->is64)
253 return __open_volume64(__image, __fh);
254
255 return __open_volume32(__image, __fh);
256}
257
258static void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
259{
Matt Fleming54b52d82014-01-10 15:27:14 +0000260 unsigned long output_string;
261 size_t offset;
Matt Fleming54b52d82014-01-10 15:27:14 +0000262
Matt Flemingc116e8d2014-01-16 11:35:43 +0000263 if (efi_early->is64) {
264 struct efi_simple_text_output_protocol_64 *out;
265 u64 *func;
Matt Fleming54b52d82014-01-10 15:27:14 +0000266
Matt Flemingc116e8d2014-01-16 11:35:43 +0000267 offset = offsetof(typeof(*out), output_string);
268 output_string = efi_early->text_output + offset;
269 func = (u64 *)output_string;
270
271 efi_early->call(*func, efi_early->text_output, str);
272 } else {
273 struct efi_simple_text_output_protocol_32 *out;
274 u32 *func;
275
276 offset = offsetof(typeof(*out), output_string);
277 output_string = efi_early->text_output + offset;
278 func = (u32 *)output_string;
279
280 efi_early->call(*func, efi_early->text_output, str);
281 }
Matt Fleming54b52d82014-01-10 15:27:14 +0000282}
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800283
Roy Franz7721da42013-09-22 15:45:27 -0700284#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
Lee, Chun-Yideb94102012-12-20 19:33:22 +0800285
Matt Fleming291f3632011-12-12 21:27:52 +0000286static void find_bits(unsigned long mask, u8 *pos, u8 *size)
287{
288 u8 first, len;
289
290 first = 0;
291 len = 0;
292
293 if (mask) {
294 while (!(mask & 0x1)) {
295 mask = mask >> 1;
296 first++;
297 }
298
299 while (mask & 0x1) {
300 mask = mask >> 1;
301 len++;
302 }
303 }
304
305 *pos = first;
306 *size = len;
307}
308
Matt Flemingc116e8d2014-01-16 11:35:43 +0000309static efi_status_t
310__setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700311{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000312 struct pci_setup_rom *rom = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700313 efi_status_t status;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000314 unsigned long size;
315 uint64_t attributes;
316
317 status = efi_early->call(pci->attributes, pci,
318 EfiPciIoAttributeOperationGet, 0, 0,
319 &attributes);
320 if (status != EFI_SUCCESS)
321 return status;
322
323 if (!pci->romimage || !pci->romsize)
324 return EFI_INVALID_PARAMETER;
325
326 size = pci->romsize + sizeof(*rom);
327
328 status = efi_early->call(efi_early->allocate_pool,
329 EFI_LOADER_DATA, size, &rom);
330
331 if (status != EFI_SUCCESS)
332 return status;
333
334 memset(rom, 0, sizeof(*rom));
335
336 rom->data.type = SETUP_PCI;
337 rom->data.len = size - sizeof(struct setup_data);
338 rom->data.next = 0;
339 rom->pcilen = pci->romsize;
340 *__rom = rom;
341
342 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
343 PCI_VENDOR_ID, 1, &(rom->vendor));
344
345 if (status != EFI_SUCCESS)
346 goto free_struct;
347
348 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
349 PCI_DEVICE_ID, 1, &(rom->devid));
350
351 if (status != EFI_SUCCESS)
352 goto free_struct;
353
354 status = efi_early->call(pci->get_location, pci, &(rom->segment),
355 &(rom->bus), &(rom->device), &(rom->function));
356
357 if (status != EFI_SUCCESS)
358 goto free_struct;
359
360 memcpy(rom->romdata, pci->romimage, pci->romsize);
361 return status;
362
363free_struct:
364 efi_early->call(efi_early->free_pool, rom);
365 return status;
366}
367
368static efi_status_t
369setup_efi_pci32(struct boot_params *params, void **pci_handle,
370 unsigned long size)
371{
372 efi_pci_io_protocol_32 *pci = NULL;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700373 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000374 u32 *handles = (u32 *)(unsigned long)pci_handle;
375 efi_status_t status;
376 unsigned long nr_pci;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700377 struct setup_data *data;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000378 int i;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700379
Jan Beulichbc754792013-01-18 12:35:14 +0000380 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700381
382 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000383 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700384
Matt Flemingc116e8d2014-01-16 11:35:43 +0000385 nr_pci = size / sizeof(u32);
386 for (i = 0; i < nr_pci; i++) {
387 struct pci_setup_rom *rom = NULL;
388 u32 h = handles[i];
389
390 status = efi_early->call(efi_early->handle_protocol, h,
391 &pci_proto, (void **)&pci);
392
393 if (status != EFI_SUCCESS)
394 continue;
395
396 if (!pci)
397 continue;
398
399 status = __setup_efi_pci32(pci, &rom);
400 if (status != EFI_SUCCESS)
401 continue;
402
403 if (data)
404 data->next = (unsigned long)rom;
405 else
406 params->hdr.setup_data = (unsigned long)rom;
407
408 data = (struct setup_data *)rom;
409
410 }
411
412 return status;
413}
414
415static efi_status_t
416__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
417{
418 struct pci_setup_rom *rom;
419 efi_status_t status;
420 unsigned long size;
421 uint64_t attributes;
422
423 status = efi_early->call(pci->attributes, pci,
424 EfiPciIoAttributeOperationGet, 0,
425 &attributes);
426 if (status != EFI_SUCCESS)
427 return status;
428
429 if (!pci->romimage || !pci->romsize)
430 return EFI_INVALID_PARAMETER;
431
432 size = pci->romsize + sizeof(*rom);
433
434 status = efi_early->call(efi_early->allocate_pool,
435 EFI_LOADER_DATA, size, &rom);
436
437 if (status != EFI_SUCCESS)
438 return status;
439
440 rom->data.type = SETUP_PCI;
441 rom->data.len = size - sizeof(struct setup_data);
442 rom->data.next = 0;
443 rom->pcilen = pci->romsize;
444 *__rom = rom;
445
446 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
447 PCI_VENDOR_ID, 1, &(rom->vendor));
448
449 if (status != EFI_SUCCESS)
450 goto free_struct;
451
452 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
453 PCI_DEVICE_ID, 1, &(rom->devid));
454
455 if (status != EFI_SUCCESS)
456 goto free_struct;
457
458 status = efi_early->call(pci->get_location, pci, &(rom->segment),
459 &(rom->bus), &(rom->device), &(rom->function));
460
461 if (status != EFI_SUCCESS)
462 goto free_struct;
463
464 memcpy(rom->romdata, pci->romimage, pci->romsize);
465 return status;
466
467free_struct:
468 efi_early->call(efi_early->free_pool, rom);
469 return status;
470
471}
472
473static efi_status_t
474setup_efi_pci64(struct boot_params *params, void **pci_handle,
475 unsigned long size)
476{
477 efi_pci_io_protocol_64 *pci = NULL;
478 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
479 u64 *handles = (u64 *)(unsigned long)pci_handle;
480 efi_status_t status;
481 unsigned long nr_pci;
482 struct setup_data *data;
483 int i;
484
485 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
486
487 while (data && data->next)
488 data = (struct setup_data *)(unsigned long)data->next;
489
490 nr_pci = size / sizeof(u64);
491 for (i = 0; i < nr_pci; i++) {
492 struct pci_setup_rom *rom = NULL;
493 u64 h = handles[i];
494
495 status = efi_early->call(efi_early->handle_protocol, h,
496 &pci_proto, (void **)&pci);
497
498 if (status != EFI_SUCCESS)
499 continue;
500
501 if (!pci)
502 continue;
503
504 status = __setup_efi_pci64(pci, &rom);
505 if (status != EFI_SUCCESS)
506 continue;
507
508 if (data)
509 data->next = (unsigned long)rom;
510 else
511 params->hdr.setup_data = (unsigned long)rom;
512
513 data = (struct setup_data *)rom;
514
515 }
516
517 return status;
518}
519
520static efi_status_t setup_efi_pci(struct boot_params *params)
521{
522 efi_status_t status;
523 void **pci_handle = NULL;
524 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
525 unsigned long size = 0;
526
Matt Fleming54b52d82014-01-10 15:27:14 +0000527 status = efi_early->call(efi_early->locate_handle,
528 EFI_LOCATE_BY_PROTOCOL,
529 &pci_proto, NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700530
531 if (status == EFI_BUFFER_TOO_SMALL) {
Matt Fleming54b52d82014-01-10 15:27:14 +0000532 status = efi_early->call(efi_early->allocate_pool,
533 EFI_LOADER_DATA,
534 size, (void **)&pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700535
536 if (status != EFI_SUCCESS)
537 return status;
538
Matt Fleming54b52d82014-01-10 15:27:14 +0000539 status = efi_early->call(efi_early->locate_handle,
540 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
541 NULL, &size, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700542 }
543
544 if (status != EFI_SUCCESS)
545 goto free_handle;
546
Matt Flemingc116e8d2014-01-16 11:35:43 +0000547 if (efi_early->is64)
548 status = setup_efi_pci64(params, pci_handle, size);
549 else
550 status = setup_efi_pci32(params, pci_handle, size);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700551
552free_handle:
Matt Fleming54b52d82014-01-10 15:27:14 +0000553 efi_early->call(efi_early->free_pool, pci_handle);
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700554 return status;
555}
556
Matt Flemingc116e8d2014-01-16 11:35:43 +0000557static void
558setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
559 struct efi_pixel_bitmask pixel_info, int pixel_format)
Matt Fleming291f3632011-12-12 21:27:52 +0000560{
Matt Fleming291f3632011-12-12 21:27:52 +0000561 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
562 si->lfb_depth = 32;
563 si->lfb_linelength = pixels_per_scan_line * 4;
564 si->red_size = 8;
565 si->red_pos = 0;
566 si->green_size = 8;
567 si->green_pos = 8;
568 si->blue_size = 8;
569 si->blue_pos = 16;
570 si->rsvd_size = 8;
571 si->rsvd_pos = 24;
572 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
573 si->lfb_depth = 32;
574 si->lfb_linelength = pixels_per_scan_line * 4;
575 si->red_size = 8;
576 si->red_pos = 16;
577 si->green_size = 8;
578 si->green_pos = 8;
579 si->blue_size = 8;
580 si->blue_pos = 0;
581 si->rsvd_size = 8;
582 si->rsvd_pos = 24;
583 } else if (pixel_format == PIXEL_BIT_MASK) {
584 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
585 find_bits(pixel_info.green_mask, &si->green_pos,
586 &si->green_size);
587 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
588 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
589 &si->rsvd_size);
590 si->lfb_depth = si->red_size + si->green_size +
591 si->blue_size + si->rsvd_size;
592 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
593 } else {
594 si->lfb_depth = 4;
595 si->lfb_linelength = si->lfb_width / 2;
596 si->red_size = 0;
597 si->red_pos = 0;
598 si->green_size = 0;
599 si->green_pos = 0;
600 si->blue_size = 0;
601 si->blue_pos = 0;
602 si->rsvd_size = 0;
603 si->rsvd_pos = 0;
604 }
Matt Flemingc116e8d2014-01-16 11:35:43 +0000605}
606
607static efi_status_t
608__gop_query32(struct efi_graphics_output_protocol_32 *gop32,
609 struct efi_graphics_output_mode_info **info,
610 unsigned long *size, u32 *fb_base)
611{
612 struct efi_graphics_output_protocol_mode_32 *mode;
613 efi_status_t status;
614 unsigned long m;
615
616 m = gop32->mode;
617 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
618
619 status = efi_early->call(gop32->query_mode, gop32,
620 mode->mode, size, info);
621 if (status != EFI_SUCCESS)
622 return status;
623
624 *fb_base = mode->frame_buffer_base;
625 return status;
626}
627
628static efi_status_t
629setup_gop32(struct screen_info *si, efi_guid_t *proto,
630 unsigned long size, void **gop_handle)
631{
632 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
633 unsigned long nr_gops;
634 u16 width, height;
635 u32 pixels_per_scan_line;
636 u32 fb_base;
637 struct efi_pixel_bitmask pixel_info;
638 int pixel_format;
639 efi_status_t status;
640 u32 *handles = (u32 *)(unsigned long)gop_handle;
641 int i;
642
643 first_gop = NULL;
644 gop32 = NULL;
645
646 nr_gops = size / sizeof(u32);
647 for (i = 0; i < nr_gops; i++) {
648 struct efi_graphics_output_mode_info *info = NULL;
649 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
650 bool conout_found = false;
651 void *dummy = NULL;
652 u32 h = handles[i];
653
654 status = efi_early->call(efi_early->handle_protocol, h,
655 proto, (void **)&gop32);
656 if (status != EFI_SUCCESS)
657 continue;
658
659 status = efi_early->call(efi_early->handle_protocol, h,
660 &conout_proto, &dummy);
661 if (status == EFI_SUCCESS)
662 conout_found = true;
663
664 status = __gop_query32(gop32, &info, &size, &fb_base);
665 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
666 /*
667 * Systems that use the UEFI Console Splitter may
668 * provide multiple GOP devices, not all of which are
669 * backed by real hardware. The workaround is to search
670 * for a GOP implementing the ConOut protocol, and if
671 * one isn't found, to just fall back to the first GOP.
672 */
673 width = info->horizontal_resolution;
674 height = info->vertical_resolution;
675 pixel_format = info->pixel_format;
676 pixel_info = info->pixel_information;
677 pixels_per_scan_line = info->pixels_per_scan_line;
678
679 /*
680 * Once we've found a GOP supporting ConOut,
681 * don't bother looking any further.
682 */
683 first_gop = gop32;
684 if (conout_found)
685 break;
686 }
687 }
688
689 /* Did we find any GOPs? */
690 if (!first_gop)
691 goto out;
692
693 /* EFI framebuffer */
694 si->orig_video_isVGA = VIDEO_TYPE_EFI;
695
696 si->lfb_width = width;
697 si->lfb_height = height;
698 si->lfb_base = fb_base;
699 si->pages = 1;
700
701 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
Matt Fleming291f3632011-12-12 21:27:52 +0000702
Matthew Garrette9b10952012-07-27 17:20:49 -0400703 si->lfb_size = si->lfb_linelength * si->lfb_height;
704
Matthew Garrettf462ed92012-07-27 12:58:53 -0400705 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000706out:
707 return status;
708}
709
710static efi_status_t
711__gop_query64(struct efi_graphics_output_protocol_64 *gop64,
712 struct efi_graphics_output_mode_info **info,
713 unsigned long *size, u32 *fb_base)
714{
715 struct efi_graphics_output_protocol_mode_64 *mode;
716 efi_status_t status;
717 unsigned long m;
718
719 m = gop64->mode;
720 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
721
722 status = efi_early->call(gop64->query_mode, gop64,
723 mode->mode, size, info);
724 if (status != EFI_SUCCESS)
725 return status;
726
727 *fb_base = mode->frame_buffer_base;
728 return status;
729}
730
731static efi_status_t
732setup_gop64(struct screen_info *si, efi_guid_t *proto,
733 unsigned long size, void **gop_handle)
734{
735 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
736 unsigned long nr_gops;
737 u16 width, height;
738 u32 pixels_per_scan_line;
739 u32 fb_base;
740 struct efi_pixel_bitmask pixel_info;
741 int pixel_format;
742 efi_status_t status;
743 u64 *handles = (u64 *)(unsigned long)gop_handle;
744 int i;
745
746 first_gop = NULL;
747 gop64 = NULL;
748
749 nr_gops = size / sizeof(u64);
750 for (i = 0; i < nr_gops; i++) {
751 struct efi_graphics_output_mode_info *info = NULL;
752 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
753 bool conout_found = false;
754 void *dummy = NULL;
755 u64 h = handles[i];
756
757 status = efi_early->call(efi_early->handle_protocol, h,
758 proto, (void **)&gop64);
759 if (status != EFI_SUCCESS)
760 continue;
761
762 status = efi_early->call(efi_early->handle_protocol, h,
763 &conout_proto, &dummy);
764 if (status == EFI_SUCCESS)
765 conout_found = true;
766
767 status = __gop_query64(gop64, &info, &size, &fb_base);
768 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
769 /*
770 * Systems that use the UEFI Console Splitter may
771 * provide multiple GOP devices, not all of which are
772 * backed by real hardware. The workaround is to search
773 * for a GOP implementing the ConOut protocol, and if
774 * one isn't found, to just fall back to the first GOP.
775 */
776 width = info->horizontal_resolution;
777 height = info->vertical_resolution;
778 pixel_format = info->pixel_format;
779 pixel_info = info->pixel_information;
780 pixels_per_scan_line = info->pixels_per_scan_line;
781
782 /*
783 * Once we've found a GOP supporting ConOut,
784 * don't bother looking any further.
785 */
786 first_gop = gop64;
787 if (conout_found)
788 break;
789 }
790 }
791
792 /* Did we find any GOPs? */
793 if (!first_gop)
794 goto out;
795
796 /* EFI framebuffer */
797 si->orig_video_isVGA = VIDEO_TYPE_EFI;
798
799 si->lfb_width = width;
800 si->lfb_height = height;
801 si->lfb_base = fb_base;
802 si->pages = 1;
803
804 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
805
806 si->lfb_size = si->lfb_linelength * si->lfb_height;
807
808 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
809out:
810 return status;
811}
812
813/*
814 * See if we have Graphics Output Protocol
815 */
816static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
817 unsigned long size)
818{
819 efi_status_t status;
820 void **gop_handle = NULL;
821
822 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
823 size, (void **)&gop_handle);
824 if (status != EFI_SUCCESS)
825 return status;
826
827 status = efi_early->call(efi_early->locate_handle,
828 EFI_LOCATE_BY_PROTOCOL,
829 proto, NULL, &size, gop_handle);
830 if (status != EFI_SUCCESS)
831 goto free_handle;
832
833 if (efi_early->is64)
834 status = setup_gop64(si, proto, size, gop_handle);
835 else
836 status = setup_gop32(si, proto, size, gop_handle);
Matthew Garrettf462ed92012-07-27 12:58:53 -0400837
Matt Fleming291f3632011-12-12 21:27:52 +0000838free_handle:
Matt Fleming54b52d82014-01-10 15:27:14 +0000839 efi_early->call(efi_early->free_pool, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000840 return status;
841}
842
Matt Flemingc116e8d2014-01-16 11:35:43 +0000843static efi_status_t
844setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
Matt Fleming291f3632011-12-12 21:27:52 +0000845{
Matt Flemingc116e8d2014-01-16 11:35:43 +0000846 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
847 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000848 unsigned long nr_ugas;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000849 u32 *handles = (u32 *)uga_handle;;
Matt Fleming291f3632011-12-12 21:27:52 +0000850 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000851 int i;
852
Matt Fleming291f3632011-12-12 21:27:52 +0000853 first_uga = NULL;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000854 nr_ugas = size / sizeof(u32);
Matt Fleming291f3632011-12-12 21:27:52 +0000855 for (i = 0; i < nr_ugas; i++) {
856 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000857 u32 w, h, depth, refresh;
858 void *pciio;
Matt Flemingc116e8d2014-01-16 11:35:43 +0000859 u32 handle = handles[i];
Matt Fleming291f3632011-12-12 21:27:52 +0000860
Matt Fleming54b52d82014-01-10 15:27:14 +0000861 status = efi_early->call(efi_early->handle_protocol, handle,
Matt Flemingc116e8d2014-01-16 11:35:43 +0000862 &uga_proto, (void **)&uga);
Matt Fleming291f3632011-12-12 21:27:52 +0000863 if (status != EFI_SUCCESS)
864 continue;
865
Matt Fleming54b52d82014-01-10 15:27:14 +0000866 efi_early->call(efi_early->handle_protocol, handle,
867 &pciio_proto, &pciio);
Matt Fleming291f3632011-12-12 21:27:52 +0000868
Matt Fleming54b52d82014-01-10 15:27:14 +0000869 status = efi_early->call((unsigned long)uga->get_mode, uga,
870 &w, &h, &depth, &refresh);
Matt Fleming291f3632011-12-12 21:27:52 +0000871 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
Matt Flemingc116e8d2014-01-16 11:35:43 +0000872 *width = w;
873 *height = h;
Matt Fleming291f3632011-12-12 21:27:52 +0000874
875 /*
876 * Once we've found a UGA supporting PCIIO,
877 * don't bother looking any further.
878 */
879 if (pciio)
880 break;
881
882 first_uga = uga;
883 }
884 }
885
Matt Flemingc116e8d2014-01-16 11:35:43 +0000886 return status;
887}
888
889static efi_status_t
890setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
891{
892 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
893 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
894 unsigned long nr_ugas;
895 u64 *handles = (u64 *)uga_handle;;
896 efi_status_t status;
897 int i;
898
899 first_uga = NULL;
900 nr_ugas = size / sizeof(u64);
901 for (i = 0; i < nr_ugas; i++) {
902 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
903 u32 w, h, depth, refresh;
904 void *pciio;
905 u64 handle = handles[i];
906
907 status = efi_early->call(efi_early->handle_protocol, handle,
908 &uga_proto, (void **)&uga);
909 if (status != EFI_SUCCESS)
910 continue;
911
912 efi_early->call(efi_early->handle_protocol, handle,
913 &pciio_proto, &pciio);
914
915 status = efi_early->call((unsigned long)uga->get_mode, uga,
916 &w, &h, &depth, &refresh);
917 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
918 *width = w;
919 *height = h;
920
921 /*
922 * Once we've found a UGA supporting PCIIO,
923 * don't bother looking any further.
924 */
925 if (pciio)
926 break;
927
928 first_uga = uga;
929 }
930 }
931
932 return status;
933}
934
935/*
936 * See if we have Universal Graphics Adapter (UGA) protocol
937 */
938static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
939 unsigned long size)
940{
941 efi_status_t status;
942 u32 width, height;
943 void **uga_handle = NULL;
944
945 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
946 size, (void **)&uga_handle);
947 if (status != EFI_SUCCESS)
948 return status;
949
950 status = efi_early->call(efi_early->locate_handle,
951 EFI_LOCATE_BY_PROTOCOL,
952 uga_proto, NULL, &size, uga_handle);
953 if (status != EFI_SUCCESS)
954 goto free_handle;
955
956 height = 0;
957 width = 0;
958
959 if (efi_early->is64)
960 status = setup_uga64(uga_handle, size, &width, &height);
961 else
962 status = setup_uga32(uga_handle, size, &width, &height);
963
964 if (!width && !height)
Matt Fleming291f3632011-12-12 21:27:52 +0000965 goto free_handle;
966
967 /* EFI framebuffer */
968 si->orig_video_isVGA = VIDEO_TYPE_EFI;
969
970 si->lfb_depth = 32;
971 si->lfb_width = width;
972 si->lfb_height = height;
973
974 si->red_size = 8;
975 si->red_pos = 16;
976 si->green_size = 8;
977 si->green_pos = 8;
978 si->blue_size = 8;
979 si->blue_pos = 0;
980 si->rsvd_size = 8;
981 si->rsvd_pos = 24;
982
Matt Fleming291f3632011-12-12 21:27:52 +0000983free_handle:
Matt Fleming54b52d82014-01-10 15:27:14 +0000984 efi_early->call(efi_early->free_pool, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000985 return status;
986}
987
988void setup_graphics(struct boot_params *boot_params)
989{
990 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
991 struct screen_info *si;
992 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
993 efi_status_t status;
994 unsigned long size;
995 void **gop_handle = NULL;
996 void **uga_handle = NULL;
997
998 si = &boot_params->screen_info;
999 memset(si, 0, sizeof(*si));
1000
1001 size = 0;
Matt Fleming54b52d82014-01-10 15:27:14 +00001002 status = efi_early->call(efi_early->locate_handle,
1003 EFI_LOCATE_BY_PROTOCOL,
1004 &graphics_proto, NULL, &size, gop_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001005 if (status == EFI_BUFFER_TOO_SMALL)
1006 status = setup_gop(si, &graphics_proto, size);
1007
1008 if (status != EFI_SUCCESS) {
1009 size = 0;
Matt Fleming54b52d82014-01-10 15:27:14 +00001010 status = efi_early->call(efi_early->locate_handle,
1011 EFI_LOCATE_BY_PROTOCOL,
1012 &uga_proto, NULL, &size, uga_handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001013 if (status == EFI_BUFFER_TOO_SMALL)
1014 setup_uga(si, &uga_proto, size);
1015 }
1016}
1017
Matt Fleming291f3632011-12-12 21:27:52 +00001018/*
1019 * Because the x86 boot code expects to be passed a boot_params we
1020 * need to create one ourselves (usually the bootloader would create
1021 * one for us).
1022 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001023struct boot_params *make_boot_params(struct efi_config *c)
Matt Fleming291f3632011-12-12 21:27:52 +00001024{
Matt Fleming9ca8f722012-07-19 10:23:48 +01001025 struct boot_params *boot_params;
1026 struct sys_desc_table *sdt;
1027 struct apm_bios_info *bi;
1028 struct setup_header *hdr;
1029 struct efi_info *efi;
1030 efi_loaded_image_t *image;
Matt Fleming54b52d82014-01-10 15:27:14 +00001031 void *options, *handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001032 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +00001033 int options_size = 0;
1034 efi_status_t status;
Roy Franz5fef3872013-09-22 15:45:33 -07001035 char *cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001036 u16 *s2;
1037 u8 *s1;
1038 int i;
Roy Franz46f45822013-09-22 15:45:39 -07001039 unsigned long ramdisk_addr;
1040 unsigned long ramdisk_size;
Matt Fleming291f3632011-12-12 21:27:52 +00001041
Matt Fleming54b52d82014-01-10 15:27:14 +00001042 efi_early = c;
1043 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1044 handle = (void *)(unsigned long)efi_early->image_handle;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001045
1046 /* Check if we were booted by the EFI firmware */
1047 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1048 return NULL;
1049
Matt Fleming54b52d82014-01-10 15:27:14 +00001050 if (efi_early->is64)
1051 setup_boot_services64(efi_early);
1052 else
1053 setup_boot_services32(efi_early);
1054
1055 status = efi_early->call(efi_early->handle_protocol, handle,
1056 &proto, (void *)&image);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001057 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001058 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001059 return NULL;
1060 }
1061
Roy Franz40e45302013-09-22 15:45:29 -07001062 status = efi_low_alloc(sys_table, 0x4000, 1,
1063 (unsigned long *)&boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001064 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001065 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
Matt Fleming9ca8f722012-07-19 10:23:48 +01001066 return NULL;
1067 }
1068
1069 memset(boot_params, 0x0, 0x4000);
1070
1071 hdr = &boot_params->hdr;
1072 efi = &boot_params->efi_info;
1073 bi = &boot_params->apm_bios_info;
1074 sdt = &boot_params->sys_desc_table;
1075
1076 /* Copy the second sector to boot_params */
1077 memcpy(&hdr->jump, image->image_base + 512, 512);
1078
1079 /*
1080 * Fill out some of the header fields ourselves because the
1081 * EFI firmware loader doesn't load the first sector.
1082 */
1083 hdr->root_flags = 1;
1084 hdr->vid_mode = 0xffff;
1085 hdr->boot_flag = 0xAA55;
1086
1087 hdr->code32_start = (__u64)(unsigned long)image->image_base;
1088
Matt Fleming291f3632011-12-12 21:27:52 +00001089 hdr->type_of_loader = 0x21;
1090
1091 /* Convert unicode cmdline to ascii */
Roy Franz5fef3872013-09-22 15:45:33 -07001092 cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image,
1093 &options_size);
1094 if (!cmdline_ptr)
1095 goto fail;
1096 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
Matt Fleming291f3632011-12-12 21:27:52 +00001097
1098 hdr->ramdisk_image = 0;
1099 hdr->ramdisk_size = 0;
1100
Matt Fleming291f3632011-12-12 21:27:52 +00001101 /* Clear APM BIOS info */
1102 memset(bi, 0, sizeof(*bi));
1103
1104 memset(sdt, 0, sizeof(*sdt));
1105
Roy Franz46f45822013-09-22 15:45:39 -07001106 status = handle_cmdline_files(sys_table, image,
1107 (char *)(unsigned long)hdr->cmd_line_ptr,
1108 "initrd=", hdr->initrd_addr_max,
1109 &ramdisk_addr, &ramdisk_size);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001110 if (status != EFI_SUCCESS)
1111 goto fail2;
Roy Franz46f45822013-09-22 15:45:39 -07001112 hdr->ramdisk_image = ramdisk_addr;
1113 hdr->ramdisk_size = ramdisk_size;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001114
1115 return boot_params;
1116fail2:
Roy Franz0e1cadb2013-09-22 15:45:38 -07001117 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001118fail:
Roy Franz40e45302013-09-22 15:45:29 -07001119 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001120 return NULL;
1121}
1122
Linn Crosettod2078d52013-09-22 19:59:08 -06001123static void add_e820ext(struct boot_params *params,
1124 struct setup_data *e820ext, u32 nr_entries)
Matt Fleming9ca8f722012-07-19 10:23:48 +01001125{
Linn Crosettod2078d52013-09-22 19:59:08 -06001126 struct setup_data *data;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001127 efi_status_t status;
Linn Crosettod2078d52013-09-22 19:59:08 -06001128 unsigned long size;
1129
1130 e820ext->type = SETUP_E820_EXT;
1131 e820ext->len = nr_entries * sizeof(struct e820entry);
1132 e820ext->next = 0;
1133
1134 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1135
1136 while (data && data->next)
1137 data = (struct setup_data *)(unsigned long)data->next;
1138
1139 if (data)
1140 data->next = (unsigned long)e820ext;
1141 else
1142 params->hdr.setup_data = (unsigned long)e820ext;
1143}
1144
1145static efi_status_t setup_e820(struct boot_params *params,
1146 struct setup_data *e820ext, u32 e820ext_size)
1147{
1148 struct e820entry *e820_map = &params->e820_map[0];
1149 struct efi_info *efi = &params->efi_info;
1150 struct e820entry *prev = NULL;
1151 u32 nr_entries;
1152 u32 nr_desc;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001153 int i;
Matt Fleming291f3632011-12-12 21:27:52 +00001154
Matt Fleming291f3632011-12-12 21:27:52 +00001155 nr_entries = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001156 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1157
1158 for (i = 0; i < nr_desc; i++) {
Matt Fleming291f3632011-12-12 21:27:52 +00001159 efi_memory_desc_t *d;
1160 unsigned int e820_type = 0;
Linn Crosettod2078d52013-09-22 19:59:08 -06001161 unsigned long m = efi->efi_memmap;
Matt Fleming291f3632011-12-12 21:27:52 +00001162
Linn Crosettod2078d52013-09-22 19:59:08 -06001163 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
Matt Fleming291f3632011-12-12 21:27:52 +00001164 switch (d->type) {
1165 case EFI_RESERVED_TYPE:
1166 case EFI_RUNTIME_SERVICES_CODE:
1167 case EFI_RUNTIME_SERVICES_DATA:
1168 case EFI_MEMORY_MAPPED_IO:
1169 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1170 case EFI_PAL_CODE:
1171 e820_type = E820_RESERVED;
1172 break;
1173
1174 case EFI_UNUSABLE_MEMORY:
1175 e820_type = E820_UNUSABLE;
1176 break;
1177
1178 case EFI_ACPI_RECLAIM_MEMORY:
1179 e820_type = E820_ACPI;
1180 break;
1181
1182 case EFI_LOADER_CODE:
1183 case EFI_LOADER_DATA:
1184 case EFI_BOOT_SERVICES_CODE:
1185 case EFI_BOOT_SERVICES_DATA:
1186 case EFI_CONVENTIONAL_MEMORY:
1187 e820_type = E820_RAM;
1188 break;
1189
1190 case EFI_ACPI_MEMORY_NVS:
1191 e820_type = E820_NVS;
1192 break;
1193
1194 default:
1195 continue;
1196 }
1197
1198 /* Merge adjacent mappings */
1199 if (prev && prev->type == e820_type &&
Linn Crosettod2078d52013-09-22 19:59:08 -06001200 (prev->addr + prev->size) == d->phys_addr) {
Matt Fleming291f3632011-12-12 21:27:52 +00001201 prev->size += d->num_pages << 12;
Linn Crosettod2078d52013-09-22 19:59:08 -06001202 continue;
Matt Fleming291f3632011-12-12 21:27:52 +00001203 }
Linn Crosettod2078d52013-09-22 19:59:08 -06001204
1205 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1206 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1207 sizeof(struct setup_data);
1208
1209 if (!e820ext || e820ext_size < need)
1210 return EFI_BUFFER_TOO_SMALL;
1211
1212 /* boot_params map full, switch to e820 extended */
1213 e820_map = (struct e820entry *)e820ext->data;
1214 }
1215
1216 e820_map->addr = d->phys_addr;
1217 e820_map->size = d->num_pages << PAGE_SHIFT;
1218 e820_map->type = e820_type;
1219 prev = e820_map++;
1220 nr_entries++;
Matt Fleming291f3632011-12-12 21:27:52 +00001221 }
1222
Linn Crosettod2078d52013-09-22 19:59:08 -06001223 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1224 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1225
1226 add_e820ext(params, e820ext, nr_e820ext);
1227 nr_entries -= nr_e820ext;
1228 }
1229
1230 params->e820_entries = (u8)nr_entries;
1231
1232 return EFI_SUCCESS;
1233}
1234
1235static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1236 u32 *e820ext_size)
1237{
1238 efi_status_t status;
1239 unsigned long size;
1240
1241 size = sizeof(struct setup_data) +
1242 sizeof(struct e820entry) * nr_desc;
1243
1244 if (*e820ext) {
Matt Fleming54b52d82014-01-10 15:27:14 +00001245 efi_early->call(efi_early->free_pool, *e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001246 *e820ext = NULL;
1247 *e820ext_size = 0;
1248 }
1249
Matt Fleming54b52d82014-01-10 15:27:14 +00001250 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
1251 size, (void **)e820ext);
Linn Crosettod2078d52013-09-22 19:59:08 -06001252 if (status == EFI_SUCCESS)
1253 *e820ext_size = size;
1254
1255 return status;
1256}
1257
1258static efi_status_t exit_boot(struct boot_params *boot_params,
1259 void *handle)
1260{
1261 struct efi_info *efi = &boot_params->efi_info;
1262 unsigned long map_sz, key, desc_size;
1263 efi_memory_desc_t *mem_map;
1264 struct setup_data *e820ext;
1265 __u32 e820ext_size;
1266 __u32 nr_desc, prev_nr_desc;
1267 efi_status_t status;
1268 __u32 desc_version;
1269 bool called_exit = false;
1270 u8 nr_entries;
1271 int i;
1272
1273 nr_desc = 0;
1274 e820ext = NULL;
1275 e820ext_size = 0;
1276
1277get_map:
1278 status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1279 &desc_version, &key);
1280
1281 if (status != EFI_SUCCESS)
1282 return status;
1283
1284 prev_nr_desc = nr_desc;
1285 nr_desc = map_sz / desc_size;
1286 if (nr_desc > prev_nr_desc &&
1287 nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1288 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1289
1290 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1291 if (status != EFI_SUCCESS)
1292 goto free_mem_map;
1293
Matt Fleming54b52d82014-01-10 15:27:14 +00001294 efi_early->call(efi_early->free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001295 goto get_map; /* Allocated memory, get map again */
1296 }
1297
1298 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
1299 efi->efi_systab = (unsigned long)sys_table;
1300 efi->efi_memdesc_size = desc_size;
1301 efi->efi_memdesc_version = desc_version;
1302 efi->efi_memmap = (unsigned long)mem_map;
1303 efi->efi_memmap_size = map_sz;
1304
1305#ifdef CONFIG_X86_64
1306 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1307 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1308#endif
1309
1310 /* Might as well exit boot services now */
Matt Fleming54b52d82014-01-10 15:27:14 +00001311 status = efi_early->call(efi_early->exit_boot_services, handle, key);
Linn Crosettod2078d52013-09-22 19:59:08 -06001312 if (status != EFI_SUCCESS) {
1313 /*
1314 * ExitBootServices() will fail if any of the event
1315 * handlers change the memory map. In which case, we
1316 * must be prepared to retry, but only once so that
1317 * we're guaranteed to exit on repeated failures instead
1318 * of spinning forever.
1319 */
1320 if (called_exit)
1321 goto free_mem_map;
1322
1323 called_exit = true;
Matt Fleming54b52d82014-01-10 15:27:14 +00001324 efi_early->call(efi_early->free_pool, mem_map);
Linn Crosettod2078d52013-09-22 19:59:08 -06001325 goto get_map;
1326 }
1327
1328 /* Historic? */
1329 boot_params->alt_mem_k = 32 * 1024;
1330
1331 status = setup_e820(boot_params, e820ext, e820ext_size);
1332 if (status != EFI_SUCCESS)
1333 return status;
Matt Fleming291f3632011-12-12 21:27:52 +00001334
1335 return EFI_SUCCESS;
1336
1337free_mem_map:
Matt Fleming54b52d82014-01-10 15:27:14 +00001338 efi_early->call(efi_early->free_pool, mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +00001339 return status;
1340}
1341
Matt Fleming9ca8f722012-07-19 10:23:48 +01001342/*
1343 * On success we return a pointer to a boot_params structure, and NULL
1344 * on failure.
1345 */
Matt Fleming54b52d82014-01-10 15:27:14 +00001346struct boot_params *efi_main(struct efi_config *c,
Matt Fleming9ca8f722012-07-19 10:23:48 +01001347 struct boot_params *boot_params)
1348{
Matt Fleming54b52d82014-01-10 15:27:14 +00001349 struct desc_ptr *gdt = NULL;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001350 efi_loaded_image_t *image;
1351 struct setup_header *hdr = &boot_params->hdr;
1352 efi_status_t status;
1353 struct desc_struct *desc;
Matt Fleming54b52d82014-01-10 15:27:14 +00001354 void *handle;
1355 efi_system_table_t *_table;
1356 bool is64;
1357
1358 efi_early = c;
1359
1360 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1361 handle = (void *)(unsigned long)efi_early->image_handle;
1362 is64 = efi_early->is64;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001363
1364 sys_table = _table;
1365
1366 /* Check if we were booted by the EFI firmware */
1367 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1368 goto fail;
1369
Matt Fleming54b52d82014-01-10 15:27:14 +00001370 if (is64)
1371 setup_boot_services64(efi_early);
1372 else
1373 setup_boot_services32(efi_early);
1374
Matt Fleming9ca8f722012-07-19 10:23:48 +01001375 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001376
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001377 setup_efi_pci(boot_params);
1378
Matt Fleming54b52d82014-01-10 15:27:14 +00001379 status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
1380 sizeof(*gdt), (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001381 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001382 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001383 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001384 }
Matt Fleming291f3632011-12-12 21:27:52 +00001385
1386 gdt->size = 0x800;
Roy Franz40e45302013-09-22 15:45:29 -07001387 status = efi_low_alloc(sys_table, gdt->size, 8,
Roy Franz876dc362013-09-22 15:45:28 -07001388 (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001389 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -07001390 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001391 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001392 }
Matt Fleming291f3632011-12-12 21:27:52 +00001393
Matt Fleming9ca8f722012-07-19 10:23:48 +01001394 /*
1395 * If the kernel isn't already loaded at the preferred load
1396 * address, relocate it.
1397 */
1398 if (hdr->pref_address != hdr->code32_start) {
Roy Franz4a9f3a72013-09-22 15:45:32 -07001399 unsigned long bzimage_addr = hdr->code32_start;
1400 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1401 hdr->init_size, hdr->init_size,
1402 hdr->pref_address,
1403 hdr->kernel_alignment);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001404 if (status != EFI_SUCCESS)
1405 goto fail;
Roy Franz4a9f3a72013-09-22 15:45:32 -07001406
1407 hdr->pref_address = hdr->code32_start;
1408 hdr->code32_start = bzimage_addr;
Matt Fleming9ca8f722012-07-19 10:23:48 +01001409 }
1410
1411 status = exit_boot(boot_params, handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001412 if (status != EFI_SUCCESS)
1413 goto fail;
1414
1415 memset((char *)gdt->address, 0x0, gdt->size);
1416 desc = (struct desc_struct *)gdt->address;
1417
1418 /* The first GDT is a dummy and the second is unused. */
1419 desc += 2;
1420
1421 desc->limit0 = 0xffff;
1422 desc->base0 = 0x0000;
1423 desc->base1 = 0x0000;
1424 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1425 desc->s = DESC_TYPE_CODE_DATA;
1426 desc->dpl = 0;
1427 desc->p = 1;
1428 desc->limit = 0xf;
1429 desc->avl = 0;
1430 desc->l = 0;
1431 desc->d = SEG_OP_SIZE_32BIT;
1432 desc->g = SEG_GRANULARITY_4KB;
1433 desc->base2 = 0x00;
1434
1435 desc++;
1436 desc->limit0 = 0xffff;
1437 desc->base0 = 0x0000;
1438 desc->base1 = 0x0000;
1439 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1440 desc->s = DESC_TYPE_CODE_DATA;
1441 desc->dpl = 0;
1442 desc->p = 1;
1443 desc->limit = 0xf;
1444 desc->avl = 0;
1445 desc->l = 0;
1446 desc->d = SEG_OP_SIZE_32BIT;
1447 desc->g = SEG_GRANULARITY_4KB;
1448 desc->base2 = 0x00;
1449
1450#ifdef CONFIG_X86_64
1451 /* Task segment value */
1452 desc++;
1453 desc->limit0 = 0x0000;
1454 desc->base0 = 0x0000;
1455 desc->base1 = 0x0000;
1456 desc->type = SEG_TYPE_TSS;
1457 desc->s = 0;
1458 desc->dpl = 0;
1459 desc->p = 1;
1460 desc->limit = 0x0;
1461 desc->avl = 0;
1462 desc->l = 0;
1463 desc->d = 0;
1464 desc->g = SEG_GRANULARITY_4KB;
1465 desc->base2 = 0x00;
1466#endif /* CONFIG_X86_64 */
1467
Matt Fleming291f3632011-12-12 21:27:52 +00001468 asm volatile("cli");
Bart Kuivenhoven0ce6cda2013-09-23 11:45:28 +02001469 asm volatile ("lgdt %0" : : "m" (*gdt));
Matt Fleming291f3632011-12-12 21:27:52 +00001470
1471 return boot_params;
1472fail:
1473 return NULL;
1474}