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");
70 static inline efi_status_t
__open_volume64(void *__image
, void **__fh
)
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
;
77 void *handle
= (void *)(unsigned long)image
->device_handle
;
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");
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");
97 efi_open_volume(efi_system_table_t
*sys_table
, void *__image
, void **__fh
)
100 return __open_volume64(__image
, __fh
);
102 return __open_volume32(__image
, __fh
);
105 void efi_char16_printk(efi_system_table_t
*table
, efi_char16_t
*str
)
107 efi_call_proto(efi_simple_text_output_protocol
, output_string
,
108 efi_early
->text_output
, str
);
112 __setup_efi_pci32(efi_pci_io_protocol_32
*pci
, struct pci_setup_rom
**__rom
)
114 struct pci_setup_rom
*rom
= NULL
;
119 status
= efi_early
->call(pci
->attributes
, pci
,
120 EfiPciIoAttributeOperationGet
, 0, 0,
122 if (status
!= EFI_SUCCESS
)
125 if (!pci
->romimage
|| !pci
->romsize
)
126 return EFI_INVALID_PARAMETER
;
128 size
= pci
->romsize
+ sizeof(*rom
);
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");
136 memset(rom
, 0, sizeof(*rom
));
138 rom
->data
.type
= SETUP_PCI
;
139 rom
->data
.len
= size
- sizeof(struct setup_data
);
141 rom
->pcilen
= pci
->romsize
;
144 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
145 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
147 if (status
!= EFI_SUCCESS
) {
148 efi_printk(sys_table
, "Failed to read rom->vendor\n");
152 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
153 PCI_DEVICE_ID
, 1, &(rom
->devid
));
155 if (status
!= EFI_SUCCESS
) {
156 efi_printk(sys_table
, "Failed to read rom->devid\n");
160 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
161 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
163 if (status
!= EFI_SUCCESS
)
166 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
170 efi_call_early(free_pool
, rom
);
175 setup_efi_pci32(struct boot_params
*params
, void **pci_handle
,
178 efi_pci_io_protocol_32
*pci
= NULL
;
179 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
180 u32
*handles
= (u32
*)(unsigned long)pci_handle
;
182 unsigned long nr_pci
;
183 struct setup_data
*data
;
186 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
188 while (data
&& data
->next
)
189 data
= (struct setup_data
*)(unsigned long)data
->next
;
191 nr_pci
= size
/ sizeof(u32
);
192 for (i
= 0; i
< nr_pci
; i
++) {
193 struct pci_setup_rom
*rom
= NULL
;
196 status
= efi_call_early(handle_protocol
, h
,
197 &pci_proto
, (void **)&pci
);
199 if (status
!= EFI_SUCCESS
)
205 status
= __setup_efi_pci32(pci
, &rom
);
206 if (status
!= EFI_SUCCESS
)
210 data
->next
= (unsigned long)rom
;
212 params
->hdr
.setup_data
= (unsigned long)rom
;
214 data
= (struct setup_data
*)rom
;
220 __setup_efi_pci64(efi_pci_io_protocol_64
*pci
, struct pci_setup_rom
**__rom
)
222 struct pci_setup_rom
*rom
;
227 status
= efi_early
->call(pci
->attributes
, pci
,
228 EfiPciIoAttributeOperationGet
, 0,
230 if (status
!= EFI_SUCCESS
)
233 if (!pci
->romimage
|| !pci
->romsize
)
234 return EFI_INVALID_PARAMETER
;
236 size
= pci
->romsize
+ sizeof(*rom
);
238 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
239 if (status
!= EFI_SUCCESS
) {
240 efi_printk(sys_table
, "Failed to alloc mem for rom\n");
244 rom
->data
.type
= SETUP_PCI
;
245 rom
->data
.len
= size
- sizeof(struct setup_data
);
247 rom
->pcilen
= pci
->romsize
;
250 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
251 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
253 if (status
!= EFI_SUCCESS
) {
254 efi_printk(sys_table
, "Failed to read rom->vendor\n");
258 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
259 PCI_DEVICE_ID
, 1, &(rom
->devid
));
261 if (status
!= EFI_SUCCESS
) {
262 efi_printk(sys_table
, "Failed to read rom->devid\n");
266 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
267 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
269 if (status
!= EFI_SUCCESS
)
272 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
276 efi_call_early(free_pool
, rom
);
282 setup_efi_pci64(struct boot_params
*params
, void **pci_handle
,
285 efi_pci_io_protocol_64
*pci
= NULL
;
286 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
287 u64
*handles
= (u64
*)(unsigned long)pci_handle
;
289 unsigned long nr_pci
;
290 struct setup_data
*data
;
293 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
295 while (data
&& data
->next
)
296 data
= (struct setup_data
*)(unsigned long)data
->next
;
298 nr_pci
= size
/ sizeof(u64
);
299 for (i
= 0; i
< nr_pci
; i
++) {
300 struct pci_setup_rom
*rom
= NULL
;
303 status
= efi_call_early(handle_protocol
, h
,
304 &pci_proto
, (void **)&pci
);
306 if (status
!= EFI_SUCCESS
)
312 status
= __setup_efi_pci64(pci
, &rom
);
313 if (status
!= EFI_SUCCESS
)
317 data
->next
= (unsigned long)rom
;
319 params
->hdr
.setup_data
= (unsigned long)rom
;
321 data
= (struct setup_data
*)rom
;
327 * There's no way to return an informative status from this function,
328 * because any analysis (and printing of error messages) needs to be
329 * done directly at the EFI function call-site.
331 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
332 * just didn't find any PCI devices, but there's no way to tell outside
333 * the context of the call.
335 static void setup_efi_pci(struct boot_params
*params
)
338 void **pci_handle
= NULL
;
339 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
340 unsigned long size
= 0;
342 status
= efi_call_early(locate_handle
,
343 EFI_LOCATE_BY_PROTOCOL
,
344 &pci_proto
, NULL
, &size
, pci_handle
);
346 if (status
== EFI_BUFFER_TOO_SMALL
) {
347 status
= efi_call_early(allocate_pool
,
349 size
, (void **)&pci_handle
);
351 if (status
!= EFI_SUCCESS
) {
352 efi_printk(sys_table
, "Failed to alloc mem for pci_handle\n");
356 status
= efi_call_early(locate_handle
,
357 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
358 NULL
, &size
, pci_handle
);
361 if (status
!= EFI_SUCCESS
)
365 setup_efi_pci64(params
, pci_handle
, size
);
367 setup_efi_pci32(params
, pci_handle
, size
);
370 efi_call_early(free_pool
, pci_handle
);
373 static void retrieve_apple_device_properties(struct boot_params
*boot_params
)
375 efi_guid_t guid
= APPLE_PROPERTIES_PROTOCOL_GUID
;
376 struct setup_data
*data
, *new;
381 status
= efi_call_early(locate_protocol
, &guid
, NULL
, &p
);
382 if (status
!= EFI_SUCCESS
)
385 if (efi_table_attr(apple_properties_protocol
, version
, p
) != 0x10000) {
386 efi_printk(sys_table
, "Unsupported properties proto version\n");
390 efi_call_proto(apple_properties_protocol
, get_all
, p
, NULL
, &size
);
395 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
396 size
+ sizeof(struct setup_data
), &new);
397 if (status
!= EFI_SUCCESS
) {
398 efi_printk(sys_table
,
399 "Failed to alloc mem for properties\n");
403 status
= efi_call_proto(apple_properties_protocol
, get_all
, p
,
406 if (status
== EFI_BUFFER_TOO_SMALL
)
407 efi_call_early(free_pool
, new);
408 } while (status
== EFI_BUFFER_TOO_SMALL
);
410 new->type
= SETUP_APPLE_PROPERTIES
;
414 data
= (struct setup_data
*)(unsigned long)boot_params
->hdr
.setup_data
;
416 boot_params
->hdr
.setup_data
= (unsigned long)new;
419 data
= (struct setup_data
*)(unsigned long)data
->next
;
420 data
->next
= (unsigned long)new;
424 static void setup_quirks(struct boot_params
*boot_params
)
426 efi_char16_t
const apple
[] = { 'A', 'p', 'p', 'l', 'e', 0 };
427 efi_char16_t
*fw_vendor
= (efi_char16_t
*)(unsigned long)
428 efi_table_attr(efi_system_table
, fw_vendor
, sys_table
);
430 if (!memcmp(fw_vendor
, apple
, sizeof(apple
))) {
431 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES
))
432 retrieve_apple_device_properties(boot_params
);
437 setup_uga32(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
439 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
440 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
441 unsigned long nr_ugas
;
442 u32
*handles
= (u32
*)uga_handle
;;
443 efi_status_t status
= EFI_INVALID_PARAMETER
;
447 nr_ugas
= size
/ sizeof(u32
);
448 for (i
= 0; i
< nr_ugas
; i
++) {
449 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
450 u32 w
, h
, depth
, refresh
;
452 u32 handle
= handles
[i
];
454 status
= efi_call_early(handle_protocol
, handle
,
455 &uga_proto
, (void **)&uga
);
456 if (status
!= EFI_SUCCESS
)
459 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
461 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
462 &w
, &h
, &depth
, &refresh
);
463 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
468 * Once we've found a UGA supporting PCIIO,
469 * don't bother looking any further.
482 setup_uga64(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
484 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
485 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
486 unsigned long nr_ugas
;
487 u64
*handles
= (u64
*)uga_handle
;;
488 efi_status_t status
= EFI_INVALID_PARAMETER
;
492 nr_ugas
= size
/ sizeof(u64
);
493 for (i
= 0; i
< nr_ugas
; i
++) {
494 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
495 u32 w
, h
, depth
, refresh
;
497 u64 handle
= handles
[i
];
499 status
= efi_call_early(handle_protocol
, handle
,
500 &uga_proto
, (void **)&uga
);
501 if (status
!= EFI_SUCCESS
)
504 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
506 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
507 &w
, &h
, &depth
, &refresh
);
508 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
513 * Once we've found a UGA supporting PCIIO,
514 * don't bother looking any further.
527 * See if we have Universal Graphics Adapter (UGA) protocol
529 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
534 void **uga_handle
= NULL
;
536 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
537 size
, (void **)&uga_handle
);
538 if (status
!= EFI_SUCCESS
)
541 status
= efi_call_early(locate_handle
,
542 EFI_LOCATE_BY_PROTOCOL
,
543 uga_proto
, NULL
, &size
, uga_handle
);
544 if (status
!= EFI_SUCCESS
)
551 status
= setup_uga64(uga_handle
, size
, &width
, &height
);
553 status
= setup_uga32(uga_handle
, size
, &width
, &height
);
555 if (!width
&& !height
)
558 /* EFI framebuffer */
559 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
562 si
->lfb_width
= width
;
563 si
->lfb_height
= height
;
575 efi_call_early(free_pool
, uga_handle
);
579 void setup_graphics(struct boot_params
*boot_params
)
581 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
582 struct screen_info
*si
;
583 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
586 void **gop_handle
= NULL
;
587 void **uga_handle
= NULL
;
589 si
= &boot_params
->screen_info
;
590 memset(si
, 0, sizeof(*si
));
593 status
= efi_call_early(locate_handle
,
594 EFI_LOCATE_BY_PROTOCOL
,
595 &graphics_proto
, NULL
, &size
, gop_handle
);
596 if (status
== EFI_BUFFER_TOO_SMALL
)
597 status
= efi_setup_gop(NULL
, si
, &graphics_proto
, size
);
599 if (status
!= EFI_SUCCESS
) {
601 status
= efi_call_early(locate_handle
,
602 EFI_LOCATE_BY_PROTOCOL
,
603 &uga_proto
, NULL
, &size
, uga_handle
);
604 if (status
== EFI_BUFFER_TOO_SMALL
)
605 setup_uga(si
, &uga_proto
, size
);
610 * Because the x86 boot code expects to be passed a boot_params we
611 * need to create one ourselves (usually the bootloader would create
614 * The caller is responsible for filling out ->code32_start in the
615 * returned boot_params.
617 struct boot_params
*make_boot_params(struct efi_config
*c
)
619 struct boot_params
*boot_params
;
620 struct apm_bios_info
*bi
;
621 struct setup_header
*hdr
;
622 efi_loaded_image_t
*image
;
623 void *options
, *handle
;
624 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
625 int options_size
= 0;
631 unsigned long ramdisk_addr
;
632 unsigned long ramdisk_size
;
635 sys_table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
636 handle
= (void *)(unsigned long)efi_early
->image_handle
;
638 /* Check if we were booted by the EFI firmware */
639 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
643 setup_boot_services64(efi_early
);
645 setup_boot_services32(efi_early
);
647 status
= efi_call_early(handle_protocol
, handle
,
648 &proto
, (void *)&image
);
649 if (status
!= EFI_SUCCESS
) {
650 efi_printk(sys_table
, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
654 status
= efi_low_alloc(sys_table
, 0x4000, 1,
655 (unsigned long *)&boot_params
);
656 if (status
!= EFI_SUCCESS
) {
657 efi_printk(sys_table
, "Failed to alloc lowmem for boot params\n");
661 memset(boot_params
, 0x0, 0x4000);
663 hdr
= &boot_params
->hdr
;
664 bi
= &boot_params
->apm_bios_info
;
666 /* Copy the second sector to boot_params */
667 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
670 * Fill out some of the header fields ourselves because the
671 * EFI firmware loader doesn't load the first sector.
674 hdr
->vid_mode
= 0xffff;
675 hdr
->boot_flag
= 0xAA55;
677 hdr
->type_of_loader
= 0x21;
679 /* Convert unicode cmdline to ascii */
680 cmdline_ptr
= efi_convert_cmdline(sys_table
, image
, &options_size
);
683 hdr
->cmd_line_ptr
= (unsigned long)cmdline_ptr
;
684 /* Fill in upper bits of command line address, NOP on 32 bit */
685 boot_params
->ext_cmd_line_ptr
= (u64
)(unsigned long)cmdline_ptr
>> 32;
687 hdr
->ramdisk_image
= 0;
688 hdr
->ramdisk_size
= 0;
690 /* Clear APM BIOS info */
691 memset(bi
, 0, sizeof(*bi
));
693 status
= efi_parse_options(cmdline_ptr
);
694 if (status
!= EFI_SUCCESS
)
697 status
= handle_cmdline_files(sys_table
, image
,
698 (char *)(unsigned long)hdr
->cmd_line_ptr
,
699 "initrd=", hdr
->initrd_addr_max
,
700 &ramdisk_addr
, &ramdisk_size
);
702 if (status
!= EFI_SUCCESS
&&
703 hdr
->xloadflags
& XLF_CAN_BE_LOADED_ABOVE_4G
) {
704 efi_printk(sys_table
, "Trying to load files to higher address\n");
705 status
= handle_cmdline_files(sys_table
, image
,
706 (char *)(unsigned long)hdr
->cmd_line_ptr
,
708 &ramdisk_addr
, &ramdisk_size
);
711 if (status
!= EFI_SUCCESS
)
713 hdr
->ramdisk_image
= ramdisk_addr
& 0xffffffff;
714 hdr
->ramdisk_size
= ramdisk_size
& 0xffffffff;
715 boot_params
->ext_ramdisk_image
= (u64
)ramdisk_addr
>> 32;
716 boot_params
->ext_ramdisk_size
= (u64
)ramdisk_size
>> 32;
720 efi_free(sys_table
, options_size
, hdr
->cmd_line_ptr
);
722 efi_free(sys_table
, 0x4000, (unsigned long)boot_params
);
726 static void add_e820ext(struct boot_params
*params
,
727 struct setup_data
*e820ext
, u32 nr_entries
)
729 struct setup_data
*data
;
733 e820ext
->type
= SETUP_E820_EXT
;
734 e820ext
->len
= nr_entries
* sizeof(struct boot_e820_entry
);
737 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
739 while (data
&& data
->next
)
740 data
= (struct setup_data
*)(unsigned long)data
->next
;
743 data
->next
= (unsigned long)e820ext
;
745 params
->hdr
.setup_data
= (unsigned long)e820ext
;
748 static efi_status_t
setup_e820(struct boot_params
*params
,
749 struct setup_data
*e820ext
, u32 e820ext_size
)
751 struct boot_e820_entry
*entry
= params
->e820_table
;
752 struct efi_info
*efi
= ¶ms
->efi_info
;
753 struct boot_e820_entry
*prev
= NULL
;
759 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
761 for (i
= 0; i
< nr_desc
; i
++) {
762 efi_memory_desc_t
*d
;
763 unsigned int e820_type
= 0;
764 unsigned long m
= efi
->efi_memmap
;
767 m
|= (u64
)efi
->efi_memmap_hi
<< 32;
770 d
= efi_early_memdesc_ptr(m
, efi
->efi_memdesc_size
, i
);
772 case EFI_RESERVED_TYPE
:
773 case EFI_RUNTIME_SERVICES_CODE
:
774 case EFI_RUNTIME_SERVICES_DATA
:
775 case EFI_MEMORY_MAPPED_IO
:
776 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
778 e820_type
= E820_TYPE_RESERVED
;
781 case EFI_UNUSABLE_MEMORY
:
782 e820_type
= E820_TYPE_UNUSABLE
;
785 case EFI_ACPI_RECLAIM_MEMORY
:
786 e820_type
= E820_TYPE_ACPI
;
789 case EFI_LOADER_CODE
:
790 case EFI_LOADER_DATA
:
791 case EFI_BOOT_SERVICES_CODE
:
792 case EFI_BOOT_SERVICES_DATA
:
793 case EFI_CONVENTIONAL_MEMORY
:
794 e820_type
= E820_TYPE_RAM
;
797 case EFI_ACPI_MEMORY_NVS
:
798 e820_type
= E820_TYPE_NVS
;
801 case EFI_PERSISTENT_MEMORY
:
802 e820_type
= E820_TYPE_PMEM
;
809 /* Merge adjacent mappings */
810 if (prev
&& prev
->type
== e820_type
&&
811 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
812 prev
->size
+= d
->num_pages
<< 12;
816 if (nr_entries
== ARRAY_SIZE(params
->e820_table
)) {
817 u32 need
= (nr_desc
- i
) * sizeof(struct e820_entry
) +
818 sizeof(struct setup_data
);
820 if (!e820ext
|| e820ext_size
< need
)
821 return EFI_BUFFER_TOO_SMALL
;
823 /* boot_params map full, switch to e820 extended */
824 entry
= (struct boot_e820_entry
*)e820ext
->data
;
827 entry
->addr
= d
->phys_addr
;
828 entry
->size
= d
->num_pages
<< PAGE_SHIFT
;
829 entry
->type
= e820_type
;
834 if (nr_entries
> ARRAY_SIZE(params
->e820_table
)) {
835 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_table
);
837 add_e820ext(params
, e820ext
, nr_e820ext
);
838 nr_entries
-= nr_e820ext
;
841 params
->e820_entries
= (u8
)nr_entries
;
846 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
852 size
= sizeof(struct setup_data
) +
853 sizeof(struct e820_entry
) * nr_desc
;
856 efi_call_early(free_pool
, *e820ext
);
861 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
862 size
, (void **)e820ext
);
863 if (status
== EFI_SUCCESS
)
864 *e820ext_size
= size
;
869 struct exit_boot_struct
{
870 struct boot_params
*boot_params
;
871 struct efi_info
*efi
;
872 struct setup_data
*e820ext
;
877 static efi_status_t
exit_boot_func(efi_system_table_t
*sys_table_arg
,
878 struct efi_boot_memmap
*map
,
881 static bool first
= true;
882 const char *signature
;
885 struct exit_boot_struct
*p
= priv
;
888 nr_desc
= *map
->buff_size
/ *map
->desc_size
;
889 if (nr_desc
> ARRAY_SIZE(p
->boot_params
->e820_table
)) {
890 u32 nr_e820ext
= nr_desc
-
891 ARRAY_SIZE(p
->boot_params
->e820_table
);
893 status
= alloc_e820ext(nr_e820ext
, &p
->e820ext
,
895 if (status
!= EFI_SUCCESS
)
901 signature
= p
->is64
? EFI64_LOADER_SIGNATURE
: EFI32_LOADER_SIGNATURE
;
902 memcpy(&p
->efi
->efi_loader_signature
, signature
, sizeof(__u32
));
904 p
->efi
->efi_systab
= (unsigned long)sys_table_arg
;
905 p
->efi
->efi_memdesc_size
= *map
->desc_size
;
906 p
->efi
->efi_memdesc_version
= *map
->desc_ver
;
907 p
->efi
->efi_memmap
= (unsigned long)*map
->map
;
908 p
->efi
->efi_memmap_size
= *map
->map_size
;
911 p
->efi
->efi_systab_hi
= (unsigned long)sys_table_arg
>> 32;
912 p
->efi
->efi_memmap_hi
= (unsigned long)*map
->map
>> 32;
918 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
919 void *handle
, bool is64
)
921 unsigned long map_sz
, key
, desc_size
, buff_size
;
922 efi_memory_desc_t
*mem_map
;
923 struct setup_data
*e820ext
;
927 struct efi_boot_memmap map
;
928 struct exit_boot_struct priv
;
931 map
.map_size
= &map_sz
;
932 map
.desc_size
= &desc_size
;
933 map
.desc_ver
= &desc_version
;
935 map
.buff_size
= &buff_size
;
936 priv
.boot_params
= boot_params
;
937 priv
.efi
= &boot_params
->efi_info
;
939 priv
.e820ext_size
= 0;
942 /* Might as well exit boot services now */
943 status
= efi_exit_boot_services(sys_table
, handle
, &map
, &priv
,
945 if (status
!= EFI_SUCCESS
)
948 e820ext
= priv
.e820ext
;
949 e820ext_size
= priv
.e820ext_size
;
951 boot_params
->alt_mem_k
= 32 * 1024;
953 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
954 if (status
!= EFI_SUCCESS
)
961 * On success we return a pointer to a boot_params structure, and NULL
964 struct boot_params
*efi_main(struct efi_config
*c
,
965 struct boot_params
*boot_params
)
967 struct desc_ptr
*gdt
= NULL
;
968 efi_loaded_image_t
*image
;
969 struct setup_header
*hdr
= &boot_params
->hdr
;
971 struct desc_struct
*desc
;
973 efi_system_table_t
*_table
;
978 _table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
979 handle
= (void *)(unsigned long)efi_early
->image_handle
;
980 is64
= efi_early
->is64
;
984 /* Check if we were booted by the EFI firmware */
985 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
989 setup_boot_services64(efi_early
);
991 setup_boot_services32(efi_early
);
994 * If the boot loader gave us a value for secure_boot then we use that,
995 * otherwise we ask the BIOS.
997 if (boot_params
->secure_boot
== efi_secureboot_mode_unset
)
998 boot_params
->secure_boot
= efi_get_secureboot(sys_table
);
1000 /* Ask the firmware to clear memory on unclean shutdown */
1001 efi_enable_reset_attack_mitigation(sys_table
);
1002 efi_retrieve_tpm2_eventlog(sys_table
);
1004 setup_graphics(boot_params
);
1006 setup_efi_pci(boot_params
);
1008 setup_quirks(boot_params
);
1010 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1011 sizeof(*gdt
), (void **)&gdt
);
1012 if (status
!= EFI_SUCCESS
) {
1013 efi_printk(sys_table
, "Failed to alloc mem for gdt structure\n");
1018 status
= efi_low_alloc(sys_table
, gdt
->size
, 8,
1019 (unsigned long *)&gdt
->address
);
1020 if (status
!= EFI_SUCCESS
) {
1021 efi_printk(sys_table
, "Failed to alloc mem for gdt\n");
1026 * If the kernel isn't already loaded at the preferred load
1027 * address, relocate it.
1029 if (hdr
->pref_address
!= hdr
->code32_start
) {
1030 unsigned long bzimage_addr
= hdr
->code32_start
;
1031 status
= efi_relocate_kernel(sys_table
, &bzimage_addr
,
1032 hdr
->init_size
, hdr
->init_size
,
1034 hdr
->kernel_alignment
);
1035 if (status
!= EFI_SUCCESS
) {
1036 efi_printk(sys_table
, "efi_relocate_kernel() failed!\n");
1040 hdr
->pref_address
= hdr
->code32_start
;
1041 hdr
->code32_start
= bzimage_addr
;
1044 status
= exit_boot(boot_params
, handle
, is64
);
1045 if (status
!= EFI_SUCCESS
) {
1046 efi_printk(sys_table
, "exit_boot() failed!\n");
1050 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1051 desc
= (struct desc_struct
*)gdt
->address
;
1053 /* The first GDT is a dummy. */
1056 if (IS_ENABLED(CONFIG_X86_64
)) {
1058 desc
->limit0
= 0xffff;
1059 desc
->base0
= 0x0000;
1060 desc
->base1
= 0x0000;
1061 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1062 desc
->s
= DESC_TYPE_CODE_DATA
;
1068 desc
->d
= SEG_OP_SIZE_32BIT
;
1069 desc
->g
= SEG_GRANULARITY_4KB
;
1073 /* Second entry is unused on 32-bit */
1078 desc
->limit0
= 0xffff;
1079 desc
->base0
= 0x0000;
1080 desc
->base1
= 0x0000;
1081 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1082 desc
->s
= DESC_TYPE_CODE_DATA
;
1087 if (IS_ENABLED(CONFIG_X86_64
)) {
1092 desc
->d
= SEG_OP_SIZE_32BIT
;
1094 desc
->g
= SEG_GRANULARITY_4KB
;
1099 desc
->limit0
= 0xffff;
1100 desc
->base0
= 0x0000;
1101 desc
->base1
= 0x0000;
1102 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1103 desc
->s
= DESC_TYPE_CODE_DATA
;
1109 desc
->d
= SEG_OP_SIZE_32BIT
;
1110 desc
->g
= SEG_GRANULARITY_4KB
;
1114 if (IS_ENABLED(CONFIG_X86_64
)) {
1115 /* Task segment value */
1116 desc
->limit0
= 0x0000;
1117 desc
->base0
= 0x0000;
1118 desc
->base1
= 0x0000;
1119 desc
->type
= SEG_TYPE_TSS
;
1127 desc
->g
= SEG_GRANULARITY_4KB
;
1132 asm volatile("cli");
1133 asm volatile ("lgdt %0" : : "m" (*gdt
));
1137 efi_printk(sys_table
, "efi_main() failed!\n");