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