]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/x86/boot/compressed/eboot.c
UBUNTU: SAUCE: UEFI: efi: Disable secure boot if shim is in insecure mode
[mirror_ubuntu-zesty-kernel.git] / arch / x86 / boot / compressed / eboot.c
1 /* -----------------------------------------------------------------------
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>
11 #include <linux/pci.h>
12 #include <asm/efi.h>
13 #include <asm/setup.h>
14 #include <asm/desc.h>
15 #include <asm/bootparam_utils.h>
16
17 #include "../string.h"
18 #include "eboot.h"
19
20 static efi_system_table_t *sys_table;
21
22 static struct efi_config *efi_early;
23
24 __pure const struct efi_config *__efi_early(void)
25 {
26 return efi_early;
27 }
28
29 #define BOOT_SERVICES(bits) \
30 static void setup_boot_services##bits(struct efi_config *c) \
31 { \
32 efi_system_table_##bits##_t *table; \
33 \
34 table = (typeof(table))sys_table; \
35 \
36 c->boot_services = table->boottime; \
37 c->text_output = table->con_out; \
38 }
39 BOOT_SERVICES(32);
40 BOOT_SERVICES(64);
41
42 void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
43
44 static efi_status_t
45 __file_size32(void *__fh, efi_char16_t *filename_16,
46 void **handle, u64 *file_sz)
47 {
48 efi_file_handle_32_t *h, *fh = __fh;
49 efi_file_info_t *info;
50 efi_status_t status;
51 efi_guid_t info_guid = EFI_FILE_INFO_ID;
52 u32 info_sz;
53
54 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
55 EFI_FILE_MODE_READ, (u64)0);
56 if (status != EFI_SUCCESS) {
57 efi_printk(sys_table, "Failed to open file: ");
58 efi_char16_printk(sys_table, filename_16);
59 efi_printk(sys_table, "\n");
60 return status;
61 }
62
63 *handle = h;
64
65 info_sz = 0;
66 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
67 &info_sz, NULL);
68 if (status != EFI_BUFFER_TOO_SMALL) {
69 efi_printk(sys_table, "Failed to get file info size\n");
70 return status;
71 }
72
73 grow:
74 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
75 info_sz, (void **)&info);
76 if (status != EFI_SUCCESS) {
77 efi_printk(sys_table, "Failed to alloc mem for file info\n");
78 return status;
79 }
80
81 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
82 &info_sz, info);
83 if (status == EFI_BUFFER_TOO_SMALL) {
84 efi_call_early(free_pool, info);
85 goto grow;
86 }
87
88 *file_sz = info->file_size;
89 efi_call_early(free_pool, info);
90
91 if (status != EFI_SUCCESS)
92 efi_printk(sys_table, "Failed to get initrd info\n");
93
94 return status;
95 }
96
97 static efi_status_t
98 __file_size64(void *__fh, efi_char16_t *filename_16,
99 void **handle, u64 *file_sz)
100 {
101 efi_file_handle_64_t *h, *fh = __fh;
102 efi_file_info_t *info;
103 efi_status_t status;
104 efi_guid_t info_guid = EFI_FILE_INFO_ID;
105 u64 info_sz;
106
107 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
108 EFI_FILE_MODE_READ, (u64)0);
109 if (status != EFI_SUCCESS) {
110 efi_printk(sys_table, "Failed to open file: ");
111 efi_char16_printk(sys_table, filename_16);
112 efi_printk(sys_table, "\n");
113 return status;
114 }
115
116 *handle = h;
117
118 info_sz = 0;
119 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
120 &info_sz, NULL);
121 if (status != EFI_BUFFER_TOO_SMALL) {
122 efi_printk(sys_table, "Failed to get file info size\n");
123 return status;
124 }
125
126 grow:
127 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
128 info_sz, (void **)&info);
129 if (status != EFI_SUCCESS) {
130 efi_printk(sys_table, "Failed to alloc mem for file info\n");
131 return status;
132 }
133
134 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
135 &info_sz, info);
136 if (status == EFI_BUFFER_TOO_SMALL) {
137 efi_call_early(free_pool, info);
138 goto grow;
139 }
140
141 *file_sz = info->file_size;
142 efi_call_early(free_pool, info);
143
144 if (status != EFI_SUCCESS)
145 efi_printk(sys_table, "Failed to get initrd info\n");
146
147 return status;
148 }
149 efi_status_t
150 efi_file_size(efi_system_table_t *sys_table, void *__fh,
151 efi_char16_t *filename_16, void **handle, u64 *file_sz)
152 {
153 if (efi_early->is64)
154 return __file_size64(__fh, filename_16, handle, file_sz);
155
156 return __file_size32(__fh, filename_16, handle, file_sz);
157 }
158
159 efi_status_t
160 efi_file_read(void *handle, unsigned long *size, void *addr)
161 {
162 unsigned long func;
163
164 if (efi_early->is64) {
165 efi_file_handle_64_t *fh = handle;
166
167 func = (unsigned long)fh->read;
168 return efi_early->call(func, handle, size, addr);
169 } else {
170 efi_file_handle_32_t *fh = handle;
171
172 func = (unsigned long)fh->read;
173 return efi_early->call(func, handle, size, addr);
174 }
175 }
176
177 efi_status_t efi_file_close(void *handle)
178 {
179 if (efi_early->is64) {
180 efi_file_handle_64_t *fh = handle;
181
182 return efi_early->call((unsigned long)fh->close, handle);
183 } else {
184 efi_file_handle_32_t *fh = handle;
185
186 return efi_early->call((unsigned long)fh->close, handle);
187 }
188 }
189
190 static inline efi_status_t __open_volume32(void *__image, void **__fh)
191 {
192 efi_file_io_interface_t *io;
193 efi_loaded_image_32_t *image = __image;
194 efi_file_handle_32_t *fh;
195 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
196 efi_status_t status;
197 void *handle = (void *)(unsigned long)image->device_handle;
198 unsigned long func;
199
200 status = efi_call_early(handle_protocol, handle,
201 &fs_proto, (void **)&io);
202 if (status != EFI_SUCCESS) {
203 efi_printk(sys_table, "Failed to handle fs_proto\n");
204 return status;
205 }
206
207 func = (unsigned long)io->open_volume;
208 status = efi_early->call(func, io, &fh);
209 if (status != EFI_SUCCESS)
210 efi_printk(sys_table, "Failed to open volume\n");
211
212 *__fh = fh;
213 return status;
214 }
215
216 static inline efi_status_t __open_volume64(void *__image, void **__fh)
217 {
218 efi_file_io_interface_t *io;
219 efi_loaded_image_64_t *image = __image;
220 efi_file_handle_64_t *fh;
221 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
222 efi_status_t status;
223 void *handle = (void *)(unsigned long)image->device_handle;
224 unsigned long func;
225
226 status = efi_call_early(handle_protocol, handle,
227 &fs_proto, (void **)&io);
228 if (status != EFI_SUCCESS) {
229 efi_printk(sys_table, "Failed to handle fs_proto\n");
230 return status;
231 }
232
233 func = (unsigned long)io->open_volume;
234 status = efi_early->call(func, io, &fh);
235 if (status != EFI_SUCCESS)
236 efi_printk(sys_table, "Failed to open volume\n");
237
238 *__fh = fh;
239 return status;
240 }
241
242 efi_status_t
243 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
244 {
245 if (efi_early->is64)
246 return __open_volume64(__image, __fh);
247
248 return __open_volume32(__image, __fh);
249 }
250
251 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
252 {
253 unsigned long output_string;
254 size_t offset;
255
256 if (efi_early->is64) {
257 struct efi_simple_text_output_protocol_64 *out;
258 u64 *func;
259
260 offset = offsetof(typeof(*out), output_string);
261 output_string = efi_early->text_output + offset;
262 out = (typeof(out))(unsigned long)efi_early->text_output;
263 func = (u64 *)output_string;
264
265 efi_early->call(*func, out, str);
266 } else {
267 struct efi_simple_text_output_protocol_32 *out;
268 u32 *func;
269
270 offset = offsetof(typeof(*out), output_string);
271 output_string = efi_early->text_output + offset;
272 out = (typeof(out))(unsigned long)efi_early->text_output;
273 func = (u32 *)output_string;
274
275 efi_early->call(*func, out, str);
276 }
277 }
278
279 static efi_status_t
280 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
281 {
282 struct pci_setup_rom *rom = NULL;
283 efi_status_t status;
284 unsigned long size;
285 uint64_t attributes;
286
287 status = efi_early->call(pci->attributes, pci,
288 EfiPciIoAttributeOperationGet, 0, 0,
289 &attributes);
290 if (status != EFI_SUCCESS)
291 return status;
292
293 if (!pci->romimage || !pci->romsize)
294 return EFI_INVALID_PARAMETER;
295
296 size = pci->romsize + sizeof(*rom);
297
298 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
299 if (status != EFI_SUCCESS) {
300 efi_printk(sys_table, "Failed to alloc mem for rom\n");
301 return status;
302 }
303
304 memset(rom, 0, sizeof(*rom));
305
306 rom->data.type = SETUP_PCI;
307 rom->data.len = size - sizeof(struct setup_data);
308 rom->data.next = 0;
309 rom->pcilen = pci->romsize;
310 *__rom = rom;
311
312 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
313 PCI_VENDOR_ID, 1, &(rom->vendor));
314
315 if (status != EFI_SUCCESS) {
316 efi_printk(sys_table, "Failed to read rom->vendor\n");
317 goto free_struct;
318 }
319
320 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
321 PCI_DEVICE_ID, 1, &(rom->devid));
322
323 if (status != EFI_SUCCESS) {
324 efi_printk(sys_table, "Failed to read rom->devid\n");
325 goto free_struct;
326 }
327
328 status = efi_early->call(pci->get_location, pci, &(rom->segment),
329 &(rom->bus), &(rom->device), &(rom->function));
330
331 if (status != EFI_SUCCESS)
332 goto free_struct;
333
334 memcpy(rom->romdata, pci->romimage, pci->romsize);
335 return status;
336
337 free_struct:
338 efi_call_early(free_pool, rom);
339 return status;
340 }
341
342 static void
343 setup_efi_pci32(struct boot_params *params, void **pci_handle,
344 unsigned long size)
345 {
346 efi_pci_io_protocol_32 *pci = NULL;
347 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
348 u32 *handles = (u32 *)(unsigned long)pci_handle;
349 efi_status_t status;
350 unsigned long nr_pci;
351 struct setup_data *data;
352 int i;
353
354 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
355
356 while (data && data->next)
357 data = (struct setup_data *)(unsigned long)data->next;
358
359 nr_pci = size / sizeof(u32);
360 for (i = 0; i < nr_pci; i++) {
361 struct pci_setup_rom *rom = NULL;
362 u32 h = handles[i];
363
364 status = efi_call_early(handle_protocol, h,
365 &pci_proto, (void **)&pci);
366
367 if (status != EFI_SUCCESS)
368 continue;
369
370 if (!pci)
371 continue;
372
373 status = __setup_efi_pci32(pci, &rom);
374 if (status != EFI_SUCCESS)
375 continue;
376
377 if (data)
378 data->next = (unsigned long)rom;
379 else
380 params->hdr.setup_data = (unsigned long)rom;
381
382 data = (struct setup_data *)rom;
383
384 }
385 }
386
387 static efi_status_t
388 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
389 {
390 struct pci_setup_rom *rom;
391 efi_status_t status;
392 unsigned long size;
393 uint64_t attributes;
394
395 status = efi_early->call(pci->attributes, pci,
396 EfiPciIoAttributeOperationGet, 0,
397 &attributes);
398 if (status != EFI_SUCCESS)
399 return status;
400
401 if (!pci->romimage || !pci->romsize)
402 return EFI_INVALID_PARAMETER;
403
404 size = pci->romsize + sizeof(*rom);
405
406 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
407 if (status != EFI_SUCCESS) {
408 efi_printk(sys_table, "Failed to alloc mem for rom\n");
409 return status;
410 }
411
412 rom->data.type = SETUP_PCI;
413 rom->data.len = size - sizeof(struct setup_data);
414 rom->data.next = 0;
415 rom->pcilen = pci->romsize;
416 *__rom = rom;
417
418 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
419 PCI_VENDOR_ID, 1, &(rom->vendor));
420
421 if (status != EFI_SUCCESS) {
422 efi_printk(sys_table, "Failed to read rom->vendor\n");
423 goto free_struct;
424 }
425
426 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
427 PCI_DEVICE_ID, 1, &(rom->devid));
428
429 if (status != EFI_SUCCESS) {
430 efi_printk(sys_table, "Failed to read rom->devid\n");
431 goto free_struct;
432 }
433
434 status = efi_early->call(pci->get_location, pci, &(rom->segment),
435 &(rom->bus), &(rom->device), &(rom->function));
436
437 if (status != EFI_SUCCESS)
438 goto free_struct;
439
440 memcpy(rom->romdata, pci->romimage, pci->romsize);
441 return status;
442
443 free_struct:
444 efi_call_early(free_pool, rom);
445 return status;
446
447 }
448
449 static void
450 setup_efi_pci64(struct boot_params *params, void **pci_handle,
451 unsigned long size)
452 {
453 efi_pci_io_protocol_64 *pci = NULL;
454 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
455 u64 *handles = (u64 *)(unsigned long)pci_handle;
456 efi_status_t status;
457 unsigned long nr_pci;
458 struct setup_data *data;
459 int i;
460
461 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
462
463 while (data && data->next)
464 data = (struct setup_data *)(unsigned long)data->next;
465
466 nr_pci = size / sizeof(u64);
467 for (i = 0; i < nr_pci; i++) {
468 struct pci_setup_rom *rom = NULL;
469 u64 h = handles[i];
470
471 status = efi_call_early(handle_protocol, h,
472 &pci_proto, (void **)&pci);
473
474 if (status != EFI_SUCCESS)
475 continue;
476
477 if (!pci)
478 continue;
479
480 status = __setup_efi_pci64(pci, &rom);
481 if (status != EFI_SUCCESS)
482 continue;
483
484 if (data)
485 data->next = (unsigned long)rom;
486 else
487 params->hdr.setup_data = (unsigned long)rom;
488
489 data = (struct setup_data *)rom;
490
491 }
492 }
493
494 /*
495 * There's no way to return an informative status from this function,
496 * because any analysis (and printing of error messages) needs to be
497 * done directly at the EFI function call-site.
498 *
499 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
500 * just didn't find any PCI devices, but there's no way to tell outside
501 * the context of the call.
502 */
503 static void setup_efi_pci(struct boot_params *params)
504 {
505 efi_status_t status;
506 void **pci_handle = NULL;
507 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
508 unsigned long size = 0;
509
510 status = efi_call_early(locate_handle,
511 EFI_LOCATE_BY_PROTOCOL,
512 &pci_proto, NULL, &size, pci_handle);
513
514 if (status == EFI_BUFFER_TOO_SMALL) {
515 status = efi_call_early(allocate_pool,
516 EFI_LOADER_DATA,
517 size, (void **)&pci_handle);
518
519 if (status != EFI_SUCCESS) {
520 efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
521 return;
522 }
523
524 status = efi_call_early(locate_handle,
525 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
526 NULL, &size, pci_handle);
527 }
528
529 if (status != EFI_SUCCESS)
530 goto free_handle;
531
532 if (efi_early->is64)
533 setup_efi_pci64(params, pci_handle, size);
534 else
535 setup_efi_pci32(params, pci_handle, size);
536
537 free_handle:
538 efi_call_early(free_pool, pci_handle);
539 }
540
541 static void retrieve_apple_device_properties(struct boot_params *boot_params)
542 {
543 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
544 struct setup_data *data, *new;
545 efi_status_t status;
546 u32 size = 0;
547 void *p;
548
549 status = efi_call_early(locate_protocol, &guid, NULL, &p);
550 if (status != EFI_SUCCESS)
551 return;
552
553 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
554 efi_printk(sys_table, "Unsupported properties proto version\n");
555 return;
556 }
557
558 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
559 if (!size)
560 return;
561
562 do {
563 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
564 size + sizeof(struct setup_data), &new);
565 if (status != EFI_SUCCESS) {
566 efi_printk(sys_table,
567 "Failed to alloc mem for properties\n");
568 return;
569 }
570
571 status = efi_call_proto(apple_properties_protocol, get_all, p,
572 new->data, &size);
573
574 if (status == EFI_BUFFER_TOO_SMALL)
575 efi_call_early(free_pool, new);
576 } while (status == EFI_BUFFER_TOO_SMALL);
577
578 new->type = SETUP_APPLE_PROPERTIES;
579 new->len = size;
580 new->next = 0;
581
582 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
583 if (!data)
584 boot_params->hdr.setup_data = (unsigned long)new;
585 else {
586 while (data->next)
587 data = (struct setup_data *)(unsigned long)data->next;
588 data->next = (unsigned long)new;
589 }
590 }
591
592 static void setup_quirks(struct boot_params *boot_params)
593 {
594 efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
595 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
596 efi_table_attr(efi_system_table, fw_vendor, sys_table);
597
598 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
599 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
600 retrieve_apple_device_properties(boot_params);
601 }
602 }
603
604 static int get_secure_boot(void)
605 {
606 u8 sb, setup, moksbstate;
607 unsigned long datasize = sizeof(sb);
608 u32 attr;
609 efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID;
610 efi_status_t status;
611
612 status = efi_early->call((unsigned long)sys_table->runtime->get_variable,
613 L"SecureBoot", &var_guid, NULL, &datasize, &sb);
614
615 if (status != EFI_SUCCESS)
616 return 0;
617
618 if (sb == 0)
619 return 0;
620
621
622 status = efi_early->call((unsigned long)sys_table->runtime->get_variable,
623 L"SetupMode", &var_guid, NULL, &datasize,
624 &setup);
625
626 if (status != EFI_SUCCESS)
627 return 0;
628
629 if (setup == 1)
630 return 0;
631
632 /* See if a user has put shim into insecure_mode. If so, and the variable
633 * doesn't have the runtime attribute set, we might as well honor that.
634 */
635 var_guid = EFI_SHIM_LOCK_GUID;
636 status = efi_early->call((unsigned long)sys_table->runtime->get_variable,
637 L"MokSBState", &var_guid, &attr, &datasize,
638 &moksbstate);
639
640 /* If it fails, we don't care why. Default to secure */
641 if (status != EFI_SUCCESS)
642 return 1;
643
644 if (!(attr & EFI_VARIABLE_RUNTIME_ACCESS)) {
645 if (moksbstate == 1)
646 return 0;
647 }
648
649 return 1;
650 }
651
652
653 /*
654 * See if we have Graphics Output Protocol
655 */
656 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
657 unsigned long size)
658 {
659 efi_status_t status;
660 void **gop_handle = NULL;
661
662 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
663 size, (void **)&gop_handle);
664 if (status != EFI_SUCCESS)
665 return status;
666
667 status = efi_call_early(locate_handle,
668 EFI_LOCATE_BY_PROTOCOL,
669 proto, NULL, &size, gop_handle);
670 if (status != EFI_SUCCESS)
671 goto free_handle;
672
673 if (efi_early->is64)
674 status = setup_gop64(si, proto, size, gop_handle);
675 else
676 status = setup_gop32(si, proto, size, gop_handle);
677
678 free_handle:
679 efi_call_early(free_pool, gop_handle);
680 return status;
681 }
682
683 static efi_status_t
684 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
685 {
686 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
687 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
688 unsigned long nr_ugas;
689 u32 *handles = (u32 *)uga_handle;;
690 efi_status_t status = EFI_INVALID_PARAMETER;
691 int i;
692
693 first_uga = NULL;
694 nr_ugas = size / sizeof(u32);
695 for (i = 0; i < nr_ugas; i++) {
696 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
697 u32 w, h, depth, refresh;
698 void *pciio;
699 u32 handle = handles[i];
700
701 status = efi_call_early(handle_protocol, handle,
702 &uga_proto, (void **)&uga);
703 if (status != EFI_SUCCESS)
704 continue;
705
706 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
707
708 status = efi_early->call((unsigned long)uga->get_mode, uga,
709 &w, &h, &depth, &refresh);
710 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
711 *width = w;
712 *height = h;
713
714 /*
715 * Once we've found a UGA supporting PCIIO,
716 * don't bother looking any further.
717 */
718 if (pciio)
719 break;
720
721 first_uga = uga;
722 }
723 }
724
725 return status;
726 }
727
728 static efi_status_t
729 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
730 {
731 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
732 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
733 unsigned long nr_ugas;
734 u64 *handles = (u64 *)uga_handle;;
735 efi_status_t status = EFI_INVALID_PARAMETER;
736 int i;
737
738 first_uga = NULL;
739 nr_ugas = size / sizeof(u64);
740 for (i = 0; i < nr_ugas; i++) {
741 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
742 u32 w, h, depth, refresh;
743 void *pciio;
744 u64 handle = handles[i];
745
746 status = efi_call_early(handle_protocol, handle,
747 &uga_proto, (void **)&uga);
748 if (status != EFI_SUCCESS)
749 continue;
750
751 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
752
753 status = efi_early->call((unsigned long)uga->get_mode, uga,
754 &w, &h, &depth, &refresh);
755 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
756 *width = w;
757 *height = h;
758
759 /*
760 * Once we've found a UGA supporting PCIIO,
761 * don't bother looking any further.
762 */
763 if (pciio)
764 break;
765
766 first_uga = uga;
767 }
768 }
769
770 return status;
771 }
772
773 /*
774 * See if we have Universal Graphics Adapter (UGA) protocol
775 */
776 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
777 unsigned long size)
778 {
779 efi_status_t status;
780 u32 width, height;
781 void **uga_handle = NULL;
782
783 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
784 size, (void **)&uga_handle);
785 if (status != EFI_SUCCESS)
786 return status;
787
788 status = efi_call_early(locate_handle,
789 EFI_LOCATE_BY_PROTOCOL,
790 uga_proto, NULL, &size, uga_handle);
791 if (status != EFI_SUCCESS)
792 goto free_handle;
793
794 height = 0;
795 width = 0;
796
797 if (efi_early->is64)
798 status = setup_uga64(uga_handle, size, &width, &height);
799 else
800 status = setup_uga32(uga_handle, size, &width, &height);
801
802 if (!width && !height)
803 goto free_handle;
804
805 /* EFI framebuffer */
806 si->orig_video_isVGA = VIDEO_TYPE_EFI;
807
808 si->lfb_depth = 32;
809 si->lfb_width = width;
810 si->lfb_height = height;
811
812 si->red_size = 8;
813 si->red_pos = 16;
814 si->green_size = 8;
815 si->green_pos = 8;
816 si->blue_size = 8;
817 si->blue_pos = 0;
818 si->rsvd_size = 8;
819 si->rsvd_pos = 24;
820
821 free_handle:
822 efi_call_early(free_pool, uga_handle);
823 return status;
824 }
825
826 void setup_graphics(struct boot_params *boot_params)
827 {
828 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
829 struct screen_info *si;
830 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
831 efi_status_t status;
832 unsigned long size;
833 void **gop_handle = NULL;
834 void **uga_handle = NULL;
835
836 si = &boot_params->screen_info;
837 memset(si, 0, sizeof(*si));
838
839 size = 0;
840 status = efi_call_early(locate_handle,
841 EFI_LOCATE_BY_PROTOCOL,
842 &graphics_proto, NULL, &size, gop_handle);
843 if (status == EFI_BUFFER_TOO_SMALL)
844 status = efi_setup_gop(NULL, si, &graphics_proto, size);
845
846 if (status != EFI_SUCCESS) {
847 size = 0;
848 status = efi_call_early(locate_handle,
849 EFI_LOCATE_BY_PROTOCOL,
850 &uga_proto, NULL, &size, uga_handle);
851 if (status == EFI_BUFFER_TOO_SMALL)
852 setup_uga(si, &uga_proto, size);
853 }
854 }
855
856 /*
857 * Because the x86 boot code expects to be passed a boot_params we
858 * need to create one ourselves (usually the bootloader would create
859 * one for us).
860 *
861 * The caller is responsible for filling out ->code32_start in the
862 * returned boot_params.
863 */
864 struct boot_params *make_boot_params(struct efi_config *c)
865 {
866 struct boot_params *boot_params;
867 struct apm_bios_info *bi;
868 struct setup_header *hdr;
869 efi_loaded_image_t *image;
870 void *options, *handle;
871 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
872 int options_size = 0;
873 efi_status_t status;
874 char *cmdline_ptr;
875 u16 *s2;
876 u8 *s1;
877 int i;
878 unsigned long ramdisk_addr;
879 unsigned long ramdisk_size;
880
881 efi_early = c;
882 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
883 handle = (void *)(unsigned long)efi_early->image_handle;
884
885 /* Check if we were booted by the EFI firmware */
886 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
887 return NULL;
888
889 if (efi_early->is64)
890 setup_boot_services64(efi_early);
891 else
892 setup_boot_services32(efi_early);
893
894 status = efi_call_early(handle_protocol, handle,
895 &proto, (void *)&image);
896 if (status != EFI_SUCCESS) {
897 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
898 return NULL;
899 }
900
901 status = efi_low_alloc(sys_table, 0x4000, 1,
902 (unsigned long *)&boot_params);
903 if (status != EFI_SUCCESS) {
904 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
905 return NULL;
906 }
907
908 memset(boot_params, 0x0, 0x4000);
909
910 hdr = &boot_params->hdr;
911 bi = &boot_params->apm_bios_info;
912
913 /* Copy the second sector to boot_params */
914 memcpy(&hdr->jump, image->image_base + 512, 512);
915
916 /*
917 * Fill out some of the header fields ourselves because the
918 * EFI firmware loader doesn't load the first sector.
919 */
920 hdr->root_flags = 1;
921 hdr->vid_mode = 0xffff;
922 hdr->boot_flag = 0xAA55;
923
924 hdr->type_of_loader = 0x21;
925
926 /* Convert unicode cmdline to ascii */
927 cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
928 if (!cmdline_ptr)
929 goto fail;
930 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
931 /* Fill in upper bits of command line address, NOP on 32 bit */
932 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
933
934 hdr->ramdisk_image = 0;
935 hdr->ramdisk_size = 0;
936
937 /* Clear APM BIOS info */
938 memset(bi, 0, sizeof(*bi));
939
940 status = efi_parse_options(cmdline_ptr);
941 if (status != EFI_SUCCESS)
942 goto fail2;
943
944 status = handle_cmdline_files(sys_table, image,
945 (char *)(unsigned long)hdr->cmd_line_ptr,
946 "initrd=", hdr->initrd_addr_max,
947 &ramdisk_addr, &ramdisk_size);
948
949 if (status != EFI_SUCCESS &&
950 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
951 efi_printk(sys_table, "Trying to load files to higher address\n");
952 status = handle_cmdline_files(sys_table, image,
953 (char *)(unsigned long)hdr->cmd_line_ptr,
954 "initrd=", -1UL,
955 &ramdisk_addr, &ramdisk_size);
956 }
957
958 if (status != EFI_SUCCESS)
959 goto fail2;
960 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
961 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
962 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
963 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
964
965 return boot_params;
966 fail2:
967 efi_free(sys_table, options_size, hdr->cmd_line_ptr);
968 fail:
969 efi_free(sys_table, 0x4000, (unsigned long)boot_params);
970 return NULL;
971 }
972
973 static void add_e820ext(struct boot_params *params,
974 struct setup_data *e820ext, u32 nr_entries)
975 {
976 struct setup_data *data;
977 efi_status_t status;
978 unsigned long size;
979
980 e820ext->type = SETUP_E820_EXT;
981 e820ext->len = nr_entries * sizeof(struct e820entry);
982 e820ext->next = 0;
983
984 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
985
986 while (data && data->next)
987 data = (struct setup_data *)(unsigned long)data->next;
988
989 if (data)
990 data->next = (unsigned long)e820ext;
991 else
992 params->hdr.setup_data = (unsigned long)e820ext;
993 }
994
995 static efi_status_t setup_e820(struct boot_params *params,
996 struct setup_data *e820ext, u32 e820ext_size)
997 {
998 struct e820entry *e820_map = &params->e820_map[0];
999 struct efi_info *efi = &params->efi_info;
1000 struct e820entry *prev = NULL;
1001 u32 nr_entries;
1002 u32 nr_desc;
1003 int i;
1004
1005 nr_entries = 0;
1006 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1007
1008 for (i = 0; i < nr_desc; i++) {
1009 efi_memory_desc_t *d;
1010 unsigned int e820_type = 0;
1011 unsigned long m = efi->efi_memmap;
1012
1013 #ifdef CONFIG_X86_64
1014 m |= (u64)efi->efi_memmap_hi << 32;
1015 #endif
1016
1017 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
1018 switch (d->type) {
1019 case EFI_RESERVED_TYPE:
1020 case EFI_RUNTIME_SERVICES_CODE:
1021 case EFI_RUNTIME_SERVICES_DATA:
1022 case EFI_MEMORY_MAPPED_IO:
1023 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1024 case EFI_PAL_CODE:
1025 e820_type = E820_RESERVED;
1026 break;
1027
1028 case EFI_UNUSABLE_MEMORY:
1029 e820_type = E820_UNUSABLE;
1030 break;
1031
1032 case EFI_ACPI_RECLAIM_MEMORY:
1033 e820_type = E820_ACPI;
1034 break;
1035
1036 case EFI_LOADER_CODE:
1037 case EFI_LOADER_DATA:
1038 case EFI_BOOT_SERVICES_CODE:
1039 case EFI_BOOT_SERVICES_DATA:
1040 case EFI_CONVENTIONAL_MEMORY:
1041 e820_type = E820_RAM;
1042 break;
1043
1044 case EFI_ACPI_MEMORY_NVS:
1045 e820_type = E820_NVS;
1046 break;
1047
1048 case EFI_PERSISTENT_MEMORY:
1049 e820_type = E820_PMEM;
1050 break;
1051
1052 default:
1053 continue;
1054 }
1055
1056 /* Merge adjacent mappings */
1057 if (prev && prev->type == e820_type &&
1058 (prev->addr + prev->size) == d->phys_addr) {
1059 prev->size += d->num_pages << 12;
1060 continue;
1061 }
1062
1063 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1064 u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1065 sizeof(struct setup_data);
1066
1067 if (!e820ext || e820ext_size < need)
1068 return EFI_BUFFER_TOO_SMALL;
1069
1070 /* boot_params map full, switch to e820 extended */
1071 e820_map = (struct e820entry *)e820ext->data;
1072 }
1073
1074 e820_map->addr = d->phys_addr;
1075 e820_map->size = d->num_pages << PAGE_SHIFT;
1076 e820_map->type = e820_type;
1077 prev = e820_map++;
1078 nr_entries++;
1079 }
1080
1081 if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1082 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1083
1084 add_e820ext(params, e820ext, nr_e820ext);
1085 nr_entries -= nr_e820ext;
1086 }
1087
1088 params->e820_entries = (u8)nr_entries;
1089
1090 return EFI_SUCCESS;
1091 }
1092
1093 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1094 u32 *e820ext_size)
1095 {
1096 efi_status_t status;
1097 unsigned long size;
1098
1099 size = sizeof(struct setup_data) +
1100 sizeof(struct e820entry) * nr_desc;
1101
1102 if (*e820ext) {
1103 efi_call_early(free_pool, *e820ext);
1104 *e820ext = NULL;
1105 *e820ext_size = 0;
1106 }
1107
1108 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1109 size, (void **)e820ext);
1110 if (status == EFI_SUCCESS)
1111 *e820ext_size = size;
1112
1113 return status;
1114 }
1115
1116 struct exit_boot_struct {
1117 struct boot_params *boot_params;
1118 struct efi_info *efi;
1119 struct setup_data *e820ext;
1120 __u32 e820ext_size;
1121 bool is64;
1122 };
1123
1124 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
1125 struct efi_boot_memmap *map,
1126 void *priv)
1127 {
1128 static bool first = true;
1129 const char *signature;
1130 __u32 nr_desc;
1131 efi_status_t status;
1132 struct exit_boot_struct *p = priv;
1133
1134 if (first) {
1135 nr_desc = *map->buff_size / *map->desc_size;
1136 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
1137 u32 nr_e820ext = nr_desc -
1138 ARRAY_SIZE(p->boot_params->e820_map);
1139
1140 status = alloc_e820ext(nr_e820ext, &p->e820ext,
1141 &p->e820ext_size);
1142 if (status != EFI_SUCCESS)
1143 return status;
1144 }
1145 first = false;
1146 }
1147
1148 signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1149 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
1150
1151 p->efi->efi_systab = (unsigned long)sys_table_arg;
1152 p->efi->efi_memdesc_size = *map->desc_size;
1153 p->efi->efi_memdesc_version = *map->desc_ver;
1154 p->efi->efi_memmap = (unsigned long)*map->map;
1155 p->efi->efi_memmap_size = *map->map_size;
1156
1157 #ifdef CONFIG_X86_64
1158 p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
1159 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
1160 #endif
1161
1162 return EFI_SUCCESS;
1163 }
1164
1165 static efi_status_t exit_boot(struct boot_params *boot_params,
1166 void *handle, bool is64)
1167 {
1168 unsigned long map_sz, key, desc_size, buff_size;
1169 efi_memory_desc_t *mem_map;
1170 struct setup_data *e820ext;
1171 __u32 e820ext_size;
1172 efi_status_t status;
1173 __u32 desc_version;
1174 struct efi_boot_memmap map;
1175 struct exit_boot_struct priv;
1176
1177 map.map = &mem_map;
1178 map.map_size = &map_sz;
1179 map.desc_size = &desc_size;
1180 map.desc_ver = &desc_version;
1181 map.key_ptr = &key;
1182 map.buff_size = &buff_size;
1183 priv.boot_params = boot_params;
1184 priv.efi = &boot_params->efi_info;
1185 priv.e820ext = NULL;
1186 priv.e820ext_size = 0;
1187 priv.is64 = is64;
1188
1189 /* Might as well exit boot services now */
1190 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
1191 exit_boot_func);
1192 if (status != EFI_SUCCESS)
1193 return status;
1194
1195 e820ext = priv.e820ext;
1196 e820ext_size = priv.e820ext_size;
1197 /* Historic? */
1198 boot_params->alt_mem_k = 32 * 1024;
1199
1200 status = setup_e820(boot_params, e820ext, e820ext_size);
1201 if (status != EFI_SUCCESS)
1202 return status;
1203
1204 return EFI_SUCCESS;
1205 }
1206
1207 /*
1208 * On success we return a pointer to a boot_params structure, and NULL
1209 * on failure.
1210 */
1211 struct boot_params *efi_main(struct efi_config *c,
1212 struct boot_params *boot_params)
1213 {
1214 struct desc_ptr *gdt = NULL;
1215 efi_loaded_image_t *image;
1216 struct setup_header *hdr = &boot_params->hdr;
1217 efi_status_t status;
1218 struct desc_struct *desc;
1219 void *handle;
1220 efi_system_table_t *_table;
1221 bool is64;
1222
1223 efi_early = c;
1224
1225 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1226 handle = (void *)(unsigned long)efi_early->image_handle;
1227 is64 = efi_early->is64;
1228
1229 sys_table = _table;
1230
1231 /* Check if we were booted by the EFI firmware */
1232 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1233 goto fail;
1234
1235 if (is64)
1236 setup_boot_services64(efi_early);
1237 else
1238 setup_boot_services32(efi_early);
1239
1240 sanitize_boot_params(boot_params);
1241
1242 boot_params->secure_boot = get_secure_boot();
1243
1244 setup_graphics(boot_params);
1245
1246 setup_efi_pci(boot_params);
1247
1248 setup_quirks(boot_params);
1249
1250 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1251 sizeof(*gdt), (void **)&gdt);
1252 if (status != EFI_SUCCESS) {
1253 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1254 goto fail;
1255 }
1256
1257 gdt->size = 0x800;
1258 status = efi_low_alloc(sys_table, gdt->size, 8,
1259 (unsigned long *)&gdt->address);
1260 if (status != EFI_SUCCESS) {
1261 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1262 goto fail;
1263 }
1264
1265 /*
1266 * If the kernel isn't already loaded at the preferred load
1267 * address, relocate it.
1268 */
1269 if (hdr->pref_address != hdr->code32_start) {
1270 unsigned long bzimage_addr = hdr->code32_start;
1271 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1272 hdr->init_size, hdr->init_size,
1273 hdr->pref_address,
1274 hdr->kernel_alignment);
1275 if (status != EFI_SUCCESS) {
1276 efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1277 goto fail;
1278 }
1279
1280 hdr->pref_address = hdr->code32_start;
1281 hdr->code32_start = bzimage_addr;
1282 }
1283
1284 status = exit_boot(boot_params, handle, is64);
1285 if (status != EFI_SUCCESS) {
1286 efi_printk(sys_table, "exit_boot() failed!\n");
1287 goto fail;
1288 }
1289
1290 memset((char *)gdt->address, 0x0, gdt->size);
1291 desc = (struct desc_struct *)gdt->address;
1292
1293 /* The first GDT is a dummy and the second is unused. */
1294 desc += 2;
1295
1296 desc->limit0 = 0xffff;
1297 desc->base0 = 0x0000;
1298 desc->base1 = 0x0000;
1299 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1300 desc->s = DESC_TYPE_CODE_DATA;
1301 desc->dpl = 0;
1302 desc->p = 1;
1303 desc->limit = 0xf;
1304 desc->avl = 0;
1305 desc->l = 0;
1306 desc->d = SEG_OP_SIZE_32BIT;
1307 desc->g = SEG_GRANULARITY_4KB;
1308 desc->base2 = 0x00;
1309
1310 desc++;
1311 desc->limit0 = 0xffff;
1312 desc->base0 = 0x0000;
1313 desc->base1 = 0x0000;
1314 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1315 desc->s = DESC_TYPE_CODE_DATA;
1316 desc->dpl = 0;
1317 desc->p = 1;
1318 desc->limit = 0xf;
1319 desc->avl = 0;
1320 desc->l = 0;
1321 desc->d = SEG_OP_SIZE_32BIT;
1322 desc->g = SEG_GRANULARITY_4KB;
1323 desc->base2 = 0x00;
1324
1325 #ifdef CONFIG_X86_64
1326 /* Task segment value */
1327 desc++;
1328 desc->limit0 = 0x0000;
1329 desc->base0 = 0x0000;
1330 desc->base1 = 0x0000;
1331 desc->type = SEG_TYPE_TSS;
1332 desc->s = 0;
1333 desc->dpl = 0;
1334 desc->p = 1;
1335 desc->limit = 0x0;
1336 desc->avl = 0;
1337 desc->l = 0;
1338 desc->d = 0;
1339 desc->g = SEG_GRANULARITY_4KB;
1340 desc->base2 = 0x00;
1341 #endif /* CONFIG_X86_64 */
1342
1343 asm volatile("cli");
1344 asm volatile ("lgdt %0" : : "m" (*gdt));
1345
1346 return boot_params;
1347 fail:
1348 efi_printk(sys_table, "efi_main() failed!\n");
1349 return NULL;
1350 }