1 /* -----------------------------------------------------------------------
3 * Copyright 2011 Intel Corporation; author Matt Fleming
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.
8 * ----------------------------------------------------------------------- */
10 #include <linux/efi.h>
11 #include <linux/pci.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
18 #include "../string.h"
21 static efi_system_table_t
*sys_table
;
23 static struct efi_config
*efi_early
;
25 __pure
const struct efi_config
*__efi_early(void)
30 #define BOOT_SERVICES(bits) \
31 static void setup_boot_services##bits(struct efi_config *c) \
33 efi_system_table_##bits##_t *table; \
35 table = (typeof(table))sys_table; \
37 c->runtime_services = table->runtime; \
38 c->boot_services = table->boottime; \
39 c->text_output = table->con_out; \
44 static inline efi_status_t
__open_volume32(void *__image
, void **__fh
)
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
;
51 void *handle
= (void *)(unsigned long)image
->device_handle
;
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");
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");
71 static inline efi_status_t
__open_volume64(void *__image
, void **__fh
)
73 efi_file_io_interface_t
*io
;
74 efi_loaded_image_64_t
*image
= __image
;
75 efi_file_handle_64_t
*fh
;
76 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
78 void *handle
= (void *)(unsigned long)image
->device_handle
;
81 status
= efi_call_early(handle_protocol
, handle
,
82 &fs_proto
, (void **)&io
);
83 if (status
!= EFI_SUCCESS
) {
84 efi_printk(sys_table
, "Failed to handle fs_proto\n");
88 func
= (unsigned long)io
->open_volume
;
89 status
= efi_early
->call(func
, io
, &fh
);
90 if (status
!= EFI_SUCCESS
)
91 efi_printk(sys_table
, "Failed to open volume\n");
99 efi_open_volume(efi_system_table_t
*sys_table
, void *__image
, void **__fh
)
102 return __open_volume64(__image
, __fh
);
104 return __open_volume32(__image
, __fh
);
107 void efi_char16_printk(efi_system_table_t
*table
, efi_char16_t
*str
)
109 efi_call_proto(efi_simple_text_output_protocol
, output_string
,
110 efi_early
->text_output
, str
);
114 preserve_pci_rom_image(efi_pci_io_protocol_t
*pci
, struct pci_setup_rom
**__rom
)
116 struct pci_setup_rom
*rom
= NULL
;
123 * Some firmware images contain EFI function pointers at the place where
124 * the romimage and romsize fields are supposed to be. Typically the EFI
125 * code is mapped at high addresses, translating to an unrealistically
126 * large romsize. The UEFI spec limits the size of option ROMs to 16
127 * MiB so we reject any ROMs over 16 MiB in size to catch this.
129 romimage
= (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol
,
131 romsize
= efi_table_attr(efi_pci_io_protocol
, romsize
, pci
);
132 if (!romimage
|| !romsize
|| romsize
> SZ_16M
)
133 return EFI_INVALID_PARAMETER
;
135 size
= romsize
+ sizeof(*rom
);
137 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
138 if (status
!= EFI_SUCCESS
) {
139 efi_printk(sys_table
, "Failed to allocate memory for 'rom'\n");
143 memset(rom
, 0, sizeof(*rom
));
145 rom
->data
.type
= SETUP_PCI
;
146 rom
->data
.len
= size
- sizeof(struct setup_data
);
148 rom
->pcilen
= pci
->romsize
;
151 status
= efi_call_proto(efi_pci_io_protocol
, pci
.read
, pci
,
152 EfiPciIoWidthUint16
, PCI_VENDOR_ID
, 1,
155 if (status
!= EFI_SUCCESS
) {
156 efi_printk(sys_table
, "Failed to read rom->vendor\n");
160 status
= efi_call_proto(efi_pci_io_protocol
, pci
.read
, pci
,
161 EfiPciIoWidthUint16
, PCI_DEVICE_ID
, 1,
164 if (status
!= EFI_SUCCESS
) {
165 efi_printk(sys_table
, "Failed to read rom->devid\n");
169 status
= efi_call_proto(efi_pci_io_protocol
, get_location
, pci
,
170 &rom
->segment
, &rom
->bus
, &rom
->device
,
173 if (status
!= EFI_SUCCESS
)
176 memcpy(rom
->romdata
, romimage
, romsize
);
180 efi_call_early(free_pool
, rom
);
185 * There's no way to return an informative status from this function,
186 * because any analysis (and printing of error messages) needs to be
187 * done directly at the EFI function call-site.
189 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
190 * just didn't find any PCI devices, but there's no way to tell outside
191 * the context of the call.
193 static void setup_efi_pci(struct boot_params
*params
)
196 void **pci_handle
= NULL
;
197 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
198 unsigned long size
= 0;
199 unsigned long nr_pci
;
200 struct setup_data
*data
;
203 status
= efi_call_early(locate_handle
,
204 EFI_LOCATE_BY_PROTOCOL
,
205 &pci_proto
, NULL
, &size
, pci_handle
);
207 if (status
== EFI_BUFFER_TOO_SMALL
) {
208 status
= efi_call_early(allocate_pool
,
210 size
, (void **)&pci_handle
);
212 if (status
!= EFI_SUCCESS
) {
213 efi_printk(sys_table
, "Failed to allocate memory for 'pci_handle'\n");
217 status
= efi_call_early(locate_handle
,
218 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
219 NULL
, &size
, pci_handle
);
222 if (status
!= EFI_SUCCESS
)
225 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
227 while (data
&& data
->next
)
228 data
= (struct setup_data
*)(unsigned long)data
->next
;
230 nr_pci
= size
/ (efi_is_64bit() ? sizeof(u64
) : sizeof(u32
));
231 for (i
= 0; i
< nr_pci
; i
++) {
232 efi_pci_io_protocol_t
*pci
= NULL
;
233 struct pci_setup_rom
*rom
;
235 status
= efi_call_early(handle_protocol
,
236 efi_is_64bit() ? ((u64
*)pci_handle
)[i
]
237 : ((u32
*)pci_handle
)[i
],
238 &pci_proto
, (void **)&pci
);
239 if (status
!= EFI_SUCCESS
|| !pci
)
242 status
= preserve_pci_rom_image(pci
, &rom
);
243 if (status
!= EFI_SUCCESS
)
247 data
->next
= (unsigned long)rom
;
249 params
->hdr
.setup_data
= (unsigned long)rom
;
251 data
= (struct setup_data
*)rom
;
255 efi_call_early(free_pool
, pci_handle
);
258 static void retrieve_apple_device_properties(struct boot_params
*boot_params
)
260 efi_guid_t guid
= APPLE_PROPERTIES_PROTOCOL_GUID
;
261 struct setup_data
*data
, *new;
266 status
= efi_call_early(locate_protocol
, &guid
, NULL
, &p
);
267 if (status
!= EFI_SUCCESS
)
270 if (efi_table_attr(apple_properties_protocol
, version
, p
) != 0x10000) {
271 efi_printk(sys_table
, "Unsupported properties proto version\n");
275 efi_call_proto(apple_properties_protocol
, get_all
, p
, NULL
, &size
);
280 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
281 size
+ sizeof(struct setup_data
), &new);
282 if (status
!= EFI_SUCCESS
) {
283 efi_printk(sys_table
, "Failed to allocate memory for 'properties'\n");
287 status
= efi_call_proto(apple_properties_protocol
, get_all
, p
,
290 if (status
== EFI_BUFFER_TOO_SMALL
)
291 efi_call_early(free_pool
, new);
292 } while (status
== EFI_BUFFER_TOO_SMALL
);
294 new->type
= SETUP_APPLE_PROPERTIES
;
298 data
= (struct setup_data
*)(unsigned long)boot_params
->hdr
.setup_data
;
300 boot_params
->hdr
.setup_data
= (unsigned long)new;
303 data
= (struct setup_data
*)(unsigned long)data
->next
;
304 data
->next
= (unsigned long)new;
308 static const efi_char16_t apple
[] = L
"Apple";
310 static void setup_quirks(struct boot_params
*boot_params
)
312 efi_char16_t
*fw_vendor
= (efi_char16_t
*)(unsigned long)
313 efi_table_attr(efi_system_table
, fw_vendor
, sys_table
);
315 if (!memcmp(fw_vendor
, apple
, sizeof(apple
))) {
316 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES
))
317 retrieve_apple_device_properties(boot_params
);
322 * See if we have Universal Graphics Adapter (UGA) protocol
325 setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
, unsigned long size
)
329 void **uga_handle
= NULL
;
330 efi_uga_draw_protocol_t
*uga
= NULL
, *first_uga
;
331 unsigned long nr_ugas
;
334 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
335 size
, (void **)&uga_handle
);
336 if (status
!= EFI_SUCCESS
)
339 status
= efi_call_early(locate_handle
,
340 EFI_LOCATE_BY_PROTOCOL
,
341 uga_proto
, NULL
, &size
, uga_handle
);
342 if (status
!= EFI_SUCCESS
)
349 nr_ugas
= size
/ (efi_is_64bit() ? sizeof(u64
) : sizeof(u32
));
350 for (i
= 0; i
< nr_ugas
; i
++) {
351 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
352 u32 w
, h
, depth
, refresh
;
354 unsigned long handle
= efi_is_64bit() ? ((u64
*)uga_handle
)[i
]
355 : ((u32
*)uga_handle
)[i
];
357 status
= efi_call_early(handle_protocol
, handle
,
358 uga_proto
, (void **)&uga
);
359 if (status
!= EFI_SUCCESS
)
362 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
364 status
= efi_call_proto(efi_uga_draw_protocol
, get_mode
, uga
,
365 &w
, &h
, &depth
, &refresh
);
366 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
371 * Once we've found a UGA supporting PCIIO,
372 * don't bother looking any further.
381 if (!width
&& !height
)
384 /* EFI framebuffer */
385 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
388 si
->lfb_width
= width
;
389 si
->lfb_height
= height
;
401 efi_call_early(free_pool
, uga_handle
);
406 void setup_graphics(struct boot_params
*boot_params
)
408 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
409 struct screen_info
*si
;
410 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
413 void **gop_handle
= NULL
;
414 void **uga_handle
= NULL
;
416 si
= &boot_params
->screen_info
;
417 memset(si
, 0, sizeof(*si
));
420 status
= efi_call_early(locate_handle
,
421 EFI_LOCATE_BY_PROTOCOL
,
422 &graphics_proto
, NULL
, &size
, gop_handle
);
423 if (status
== EFI_BUFFER_TOO_SMALL
)
424 status
= efi_setup_gop(NULL
, si
, &graphics_proto
, size
);
426 if (status
!= EFI_SUCCESS
) {
428 status
= efi_call_early(locate_handle
,
429 EFI_LOCATE_BY_PROTOCOL
,
430 &uga_proto
, NULL
, &size
, uga_handle
);
431 if (status
== EFI_BUFFER_TOO_SMALL
)
432 setup_uga(si
, &uga_proto
, size
);
437 * Because the x86 boot code expects to be passed a boot_params we
438 * need to create one ourselves (usually the bootloader would create
441 * The caller is responsible for filling out ->code32_start in the
442 * returned boot_params.
444 struct boot_params
*make_boot_params(struct efi_config
*c
)
446 struct boot_params
*boot_params
;
447 struct apm_bios_info
*bi
;
448 struct setup_header
*hdr
;
449 efi_loaded_image_t
*image
;
450 void *options
, *handle
;
451 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
452 int options_size
= 0;
458 unsigned long ramdisk_addr
;
459 unsigned long ramdisk_size
;
462 sys_table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
463 handle
= (void *)(unsigned long)efi_early
->image_handle
;
465 /* Check if we were booted by the EFI firmware */
466 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
470 setup_boot_services64(efi_early
);
472 setup_boot_services32(efi_early
);
474 status
= efi_call_early(handle_protocol
, handle
,
475 &proto
, (void *)&image
);
476 if (status
!= EFI_SUCCESS
) {
477 efi_printk(sys_table
, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
481 status
= efi_low_alloc(sys_table
, 0x4000, 1,
482 (unsigned long *)&boot_params
);
483 if (status
!= EFI_SUCCESS
) {
484 efi_printk(sys_table
, "Failed to allocate lowmem for boot params\n");
488 memset(boot_params
, 0x0, 0x4000);
490 hdr
= &boot_params
->hdr
;
491 bi
= &boot_params
->apm_bios_info
;
493 /* Copy the second sector to boot_params */
494 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
497 * Fill out some of the header fields ourselves because the
498 * EFI firmware loader doesn't load the first sector.
501 hdr
->vid_mode
= 0xffff;
502 hdr
->boot_flag
= 0xAA55;
504 hdr
->type_of_loader
= 0x21;
506 /* Convert unicode cmdline to ascii */
507 cmdline_ptr
= efi_convert_cmdline(sys_table
, image
, &options_size
);
511 hdr
->cmd_line_ptr
= (unsigned long)cmdline_ptr
;
512 /* Fill in upper bits of command line address, NOP on 32 bit */
513 boot_params
->ext_cmd_line_ptr
= (u64
)(unsigned long)cmdline_ptr
>> 32;
515 hdr
->ramdisk_image
= 0;
516 hdr
->ramdisk_size
= 0;
518 /* Clear APM BIOS info */
519 memset(bi
, 0, sizeof(*bi
));
521 status
= efi_parse_options(cmdline_ptr
);
522 if (status
!= EFI_SUCCESS
)
525 status
= handle_cmdline_files(sys_table
, image
,
526 (char *)(unsigned long)hdr
->cmd_line_ptr
,
527 "initrd=", hdr
->initrd_addr_max
,
528 &ramdisk_addr
, &ramdisk_size
);
530 if (status
!= EFI_SUCCESS
&&
531 hdr
->xloadflags
& XLF_CAN_BE_LOADED_ABOVE_4G
) {
532 efi_printk(sys_table
, "Trying to load files to higher address\n");
533 status
= handle_cmdline_files(sys_table
, image
,
534 (char *)(unsigned long)hdr
->cmd_line_ptr
,
536 &ramdisk_addr
, &ramdisk_size
);
539 if (status
!= EFI_SUCCESS
)
541 hdr
->ramdisk_image
= ramdisk_addr
& 0xffffffff;
542 hdr
->ramdisk_size
= ramdisk_size
& 0xffffffff;
543 boot_params
->ext_ramdisk_image
= (u64
)ramdisk_addr
>> 32;
544 boot_params
->ext_ramdisk_size
= (u64
)ramdisk_size
>> 32;
549 efi_free(sys_table
, options_size
, hdr
->cmd_line_ptr
);
551 efi_free(sys_table
, 0x4000, (unsigned long)boot_params
);
556 static void add_e820ext(struct boot_params
*params
,
557 struct setup_data
*e820ext
, u32 nr_entries
)
559 struct setup_data
*data
;
563 e820ext
->type
= SETUP_E820_EXT
;
564 e820ext
->len
= nr_entries
* sizeof(struct boot_e820_entry
);
567 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
569 while (data
&& data
->next
)
570 data
= (struct setup_data
*)(unsigned long)data
->next
;
573 data
->next
= (unsigned long)e820ext
;
575 params
->hdr
.setup_data
= (unsigned long)e820ext
;
579 setup_e820(struct boot_params
*params
, struct setup_data
*e820ext
, u32 e820ext_size
)
581 struct boot_e820_entry
*entry
= params
->e820_table
;
582 struct efi_info
*efi
= ¶ms
->efi_info
;
583 struct boot_e820_entry
*prev
= NULL
;
589 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
591 for (i
= 0; i
< nr_desc
; i
++) {
592 efi_memory_desc_t
*d
;
593 unsigned int e820_type
= 0;
594 unsigned long m
= efi
->efi_memmap
;
597 m
|= (u64
)efi
->efi_memmap_hi
<< 32;
600 d
= efi_early_memdesc_ptr(m
, efi
->efi_memdesc_size
, i
);
602 case EFI_RESERVED_TYPE
:
603 case EFI_RUNTIME_SERVICES_CODE
:
604 case EFI_RUNTIME_SERVICES_DATA
:
605 case EFI_MEMORY_MAPPED_IO
:
606 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
608 e820_type
= E820_TYPE_RESERVED
;
611 case EFI_UNUSABLE_MEMORY
:
612 e820_type
= E820_TYPE_UNUSABLE
;
615 case EFI_ACPI_RECLAIM_MEMORY
:
616 e820_type
= E820_TYPE_ACPI
;
619 case EFI_LOADER_CODE
:
620 case EFI_LOADER_DATA
:
621 case EFI_BOOT_SERVICES_CODE
:
622 case EFI_BOOT_SERVICES_DATA
:
623 case EFI_CONVENTIONAL_MEMORY
:
624 e820_type
= E820_TYPE_RAM
;
627 case EFI_ACPI_MEMORY_NVS
:
628 e820_type
= E820_TYPE_NVS
;
631 case EFI_PERSISTENT_MEMORY
:
632 e820_type
= E820_TYPE_PMEM
;
639 /* Merge adjacent mappings */
640 if (prev
&& prev
->type
== e820_type
&&
641 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
642 prev
->size
+= d
->num_pages
<< 12;
646 if (nr_entries
== ARRAY_SIZE(params
->e820_table
)) {
647 u32 need
= (nr_desc
- i
) * sizeof(struct e820_entry
) +
648 sizeof(struct setup_data
);
650 if (!e820ext
|| e820ext_size
< need
)
651 return EFI_BUFFER_TOO_SMALL
;
653 /* boot_params map full, switch to e820 extended */
654 entry
= (struct boot_e820_entry
*)e820ext
->data
;
657 entry
->addr
= d
->phys_addr
;
658 entry
->size
= d
->num_pages
<< PAGE_SHIFT
;
659 entry
->type
= e820_type
;
664 if (nr_entries
> ARRAY_SIZE(params
->e820_table
)) {
665 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_table
);
667 add_e820ext(params
, e820ext
, nr_e820ext
);
668 nr_entries
-= nr_e820ext
;
671 params
->e820_entries
= (u8
)nr_entries
;
676 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
682 size
= sizeof(struct setup_data
) +
683 sizeof(struct e820_entry
) * nr_desc
;
686 efi_call_early(free_pool
, *e820ext
);
691 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
692 size
, (void **)e820ext
);
693 if (status
== EFI_SUCCESS
)
694 *e820ext_size
= size
;
699 struct exit_boot_struct
{
700 struct boot_params
*boot_params
;
701 struct efi_info
*efi
;
702 struct setup_data
*e820ext
;
707 static efi_status_t
exit_boot_func(efi_system_table_t
*sys_table_arg
,
708 struct efi_boot_memmap
*map
,
711 static bool first
= true;
712 const char *signature
;
715 struct exit_boot_struct
*p
= priv
;
718 nr_desc
= *map
->buff_size
/ *map
->desc_size
;
719 if (nr_desc
> ARRAY_SIZE(p
->boot_params
->e820_table
)) {
720 u32 nr_e820ext
= nr_desc
-
721 ARRAY_SIZE(p
->boot_params
->e820_table
);
723 status
= alloc_e820ext(nr_e820ext
, &p
->e820ext
,
725 if (status
!= EFI_SUCCESS
)
731 signature
= p
->is64
? EFI64_LOADER_SIGNATURE
: EFI32_LOADER_SIGNATURE
;
732 memcpy(&p
->efi
->efi_loader_signature
, signature
, sizeof(__u32
));
734 p
->efi
->efi_systab
= (unsigned long)sys_table_arg
;
735 p
->efi
->efi_memdesc_size
= *map
->desc_size
;
736 p
->efi
->efi_memdesc_version
= *map
->desc_ver
;
737 p
->efi
->efi_memmap
= (unsigned long)*map
->map
;
738 p
->efi
->efi_memmap_size
= *map
->map_size
;
741 p
->efi
->efi_systab_hi
= (unsigned long)sys_table_arg
>> 32;
742 p
->efi
->efi_memmap_hi
= (unsigned long)*map
->map
>> 32;
748 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
749 void *handle
, bool is64
)
751 unsigned long map_sz
, key
, desc_size
, buff_size
;
752 efi_memory_desc_t
*mem_map
;
753 struct setup_data
*e820ext
;
757 struct efi_boot_memmap map
;
758 struct exit_boot_struct priv
;
761 map
.map_size
= &map_sz
;
762 map
.desc_size
= &desc_size
;
763 map
.desc_ver
= &desc_version
;
765 map
.buff_size
= &buff_size
;
766 priv
.boot_params
= boot_params
;
767 priv
.efi
= &boot_params
->efi_info
;
769 priv
.e820ext_size
= 0;
772 /* Might as well exit boot services now */
773 status
= efi_exit_boot_services(sys_table
, handle
, &map
, &priv
,
775 if (status
!= EFI_SUCCESS
)
778 e820ext
= priv
.e820ext
;
779 e820ext_size
= priv
.e820ext_size
;
782 boot_params
->alt_mem_k
= 32 * 1024;
784 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
785 if (status
!= EFI_SUCCESS
)
792 * On success we return a pointer to a boot_params structure, and NULL
796 efi_main(struct efi_config
*c
, struct boot_params
*boot_params
)
798 struct desc_ptr
*gdt
= NULL
;
799 efi_loaded_image_t
*image
;
800 struct setup_header
*hdr
= &boot_params
->hdr
;
802 struct desc_struct
*desc
;
804 efi_system_table_t
*_table
;
809 _table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
810 handle
= (void *)(unsigned long)efi_early
->image_handle
;
811 is64
= efi_early
->is64
;
815 /* Check if we were booted by the EFI firmware */
816 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
820 setup_boot_services64(efi_early
);
822 setup_boot_services32(efi_early
);
825 * If the boot loader gave us a value for secure_boot then we use that,
826 * otherwise we ask the BIOS.
828 if (boot_params
->secure_boot
== efi_secureboot_mode_unset
)
829 boot_params
->secure_boot
= efi_get_secureboot(sys_table
);
831 /* Ask the firmware to clear memory on unclean shutdown */
832 efi_enable_reset_attack_mitigation(sys_table
);
833 efi_retrieve_tpm2_eventlog(sys_table
);
835 setup_graphics(boot_params
);
837 setup_efi_pci(boot_params
);
839 setup_quirks(boot_params
);
841 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
842 sizeof(*gdt
), (void **)&gdt
);
843 if (status
!= EFI_SUCCESS
) {
844 efi_printk(sys_table
, "Failed to allocate memory for 'gdt' structure\n");
849 status
= efi_low_alloc(sys_table
, gdt
->size
, 8,
850 (unsigned long *)&gdt
->address
);
851 if (status
!= EFI_SUCCESS
) {
852 efi_printk(sys_table
, "Failed to allocate memory for 'gdt'\n");
857 * If the kernel isn't already loaded at the preferred load
858 * address, relocate it.
860 if (hdr
->pref_address
!= hdr
->code32_start
) {
861 unsigned long bzimage_addr
= hdr
->code32_start
;
862 status
= efi_relocate_kernel(sys_table
, &bzimage_addr
,
863 hdr
->init_size
, hdr
->init_size
,
865 hdr
->kernel_alignment
);
866 if (status
!= EFI_SUCCESS
) {
867 efi_printk(sys_table
, "efi_relocate_kernel() failed!\n");
871 hdr
->pref_address
= hdr
->code32_start
;
872 hdr
->code32_start
= bzimage_addr
;
875 status
= exit_boot(boot_params
, handle
, is64
);
876 if (status
!= EFI_SUCCESS
) {
877 efi_printk(sys_table
, "exit_boot() failed!\n");
881 memset((char *)gdt
->address
, 0x0, gdt
->size
);
882 desc
= (struct desc_struct
*)gdt
->address
;
884 /* The first GDT is a dummy. */
887 if (IS_ENABLED(CONFIG_X86_64
)) {
889 desc
->limit0
= 0xffff;
890 desc
->base0
= 0x0000;
891 desc
->base1
= 0x0000;
892 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
893 desc
->s
= DESC_TYPE_CODE_DATA
;
899 desc
->d
= SEG_OP_SIZE_32BIT
;
900 desc
->g
= SEG_GRANULARITY_4KB
;
905 /* Second entry is unused on 32-bit */
910 desc
->limit0
= 0xffff;
911 desc
->base0
= 0x0000;
912 desc
->base1
= 0x0000;
913 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
914 desc
->s
= DESC_TYPE_CODE_DATA
;
920 if (IS_ENABLED(CONFIG_X86_64
)) {
925 desc
->d
= SEG_OP_SIZE_32BIT
;
927 desc
->g
= SEG_GRANULARITY_4KB
;
932 desc
->limit0
= 0xffff;
933 desc
->base0
= 0x0000;
934 desc
->base1
= 0x0000;
935 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
936 desc
->s
= DESC_TYPE_CODE_DATA
;
942 desc
->d
= SEG_OP_SIZE_32BIT
;
943 desc
->g
= SEG_GRANULARITY_4KB
;
947 if (IS_ENABLED(CONFIG_X86_64
)) {
948 /* Task segment value */
949 desc
->limit0
= 0x0000;
950 desc
->base0
= 0x0000;
951 desc
->base1
= 0x0000;
952 desc
->type
= SEG_TYPE_TSS
;
960 desc
->g
= SEG_GRANULARITY_4KB
;
966 asm volatile ("lgdt %0" : : "m" (*gdt
));
970 efi_printk(sys_table
, "efi_main() failed!\n");