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>
13 #include <asm/setup.h>
16 #include "../string.h"
19 static efi_system_table_t
*sys_table
;
21 static struct efi_config
*efi_early
;
23 __pure
const struct efi_config
*__efi_early(void)
28 #define BOOT_SERVICES(bits) \
29 static void setup_boot_services##bits(struct efi_config *c) \
31 efi_system_table_##bits##_t *table; \
32 efi_boot_services_##bits##_t *bt; \
34 table = (typeof(table))sys_table; \
36 c->text_output = table->con_out; \
38 bt = (typeof(bt))(unsigned long)(table->boottime); \
40 c->allocate_pool = bt->allocate_pool; \
41 c->allocate_pages = bt->allocate_pages; \
42 c->get_memory_map = bt->get_memory_map; \
43 c->free_pool = bt->free_pool; \
44 c->free_pages = bt->free_pages; \
45 c->locate_handle = bt->locate_handle; \
46 c->handle_protocol = bt->handle_protocol; \
47 c->exit_boot_services = bt->exit_boot_services; \
52 void efi_char16_printk(efi_system_table_t
*, efi_char16_t
*);
55 __file_size32(void *__fh
, efi_char16_t
*filename_16
,
56 void **handle
, u64
*file_sz
)
58 efi_file_handle_32_t
*h
, *fh
= __fh
;
59 efi_file_info_t
*info
;
61 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
64 status
= efi_early
->call((unsigned long)fh
->open
, fh
, &h
, filename_16
,
65 EFI_FILE_MODE_READ
, (u64
)0);
66 if (status
!= EFI_SUCCESS
) {
67 efi_printk(sys_table
, "Failed to open file: ");
68 efi_char16_printk(sys_table
, filename_16
);
69 efi_printk(sys_table
, "\n");
76 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
78 if (status
!= EFI_BUFFER_TOO_SMALL
) {
79 efi_printk(sys_table
, "Failed to get file info size\n");
84 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
85 info_sz
, (void **)&info
);
86 if (status
!= EFI_SUCCESS
) {
87 efi_printk(sys_table
, "Failed to alloc mem for file info\n");
91 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
93 if (status
== EFI_BUFFER_TOO_SMALL
) {
94 efi_call_early(free_pool
, info
);
98 *file_sz
= info
->file_size
;
99 efi_call_early(free_pool
, info
);
101 if (status
!= EFI_SUCCESS
)
102 efi_printk(sys_table
, "Failed to get initrd info\n");
108 __file_size64(void *__fh
, efi_char16_t
*filename_16
,
109 void **handle
, u64
*file_sz
)
111 efi_file_handle_64_t
*h
, *fh
= __fh
;
112 efi_file_info_t
*info
;
114 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
117 status
= efi_early
->call((unsigned long)fh
->open
, fh
, &h
, filename_16
,
118 EFI_FILE_MODE_READ
, (u64
)0);
119 if (status
!= EFI_SUCCESS
) {
120 efi_printk(sys_table
, "Failed to open file: ");
121 efi_char16_printk(sys_table
, filename_16
);
122 efi_printk(sys_table
, "\n");
129 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
131 if (status
!= EFI_BUFFER_TOO_SMALL
) {
132 efi_printk(sys_table
, "Failed to get file info size\n");
137 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
138 info_sz
, (void **)&info
);
139 if (status
!= EFI_SUCCESS
) {
140 efi_printk(sys_table
, "Failed to alloc mem for file info\n");
144 status
= efi_early
->call((unsigned long)h
->get_info
, h
, &info_guid
,
146 if (status
== EFI_BUFFER_TOO_SMALL
) {
147 efi_call_early(free_pool
, info
);
151 *file_sz
= info
->file_size
;
152 efi_call_early(free_pool
, info
);
154 if (status
!= EFI_SUCCESS
)
155 efi_printk(sys_table
, "Failed to get initrd info\n");
160 efi_file_size(efi_system_table_t
*sys_table
, void *__fh
,
161 efi_char16_t
*filename_16
, void **handle
, u64
*file_sz
)
164 return __file_size64(__fh
, filename_16
, handle
, file_sz
);
166 return __file_size32(__fh
, filename_16
, handle
, file_sz
);
170 efi_file_read(void *handle
, unsigned long *size
, void *addr
)
174 if (efi_early
->is64
) {
175 efi_file_handle_64_t
*fh
= handle
;
177 func
= (unsigned long)fh
->read
;
178 return efi_early
->call(func
, handle
, size
, addr
);
180 efi_file_handle_32_t
*fh
= handle
;
182 func
= (unsigned long)fh
->read
;
183 return efi_early
->call(func
, handle
, size
, addr
);
187 efi_status_t
efi_file_close(void *handle
)
189 if (efi_early
->is64
) {
190 efi_file_handle_64_t
*fh
= handle
;
192 return efi_early
->call((unsigned long)fh
->close
, handle
);
194 efi_file_handle_32_t
*fh
= handle
;
196 return efi_early
->call((unsigned long)fh
->close
, handle
);
200 static inline efi_status_t
__open_volume32(void *__image
, void **__fh
)
202 efi_file_io_interface_t
*io
;
203 efi_loaded_image_32_t
*image
= __image
;
204 efi_file_handle_32_t
*fh
;
205 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
207 void *handle
= (void *)(unsigned long)image
->device_handle
;
210 status
= efi_call_early(handle_protocol
, handle
,
211 &fs_proto
, (void **)&io
);
212 if (status
!= EFI_SUCCESS
) {
213 efi_printk(sys_table
, "Failed to handle fs_proto\n");
217 func
= (unsigned long)io
->open_volume
;
218 status
= efi_early
->call(func
, io
, &fh
);
219 if (status
!= EFI_SUCCESS
)
220 efi_printk(sys_table
, "Failed to open volume\n");
226 static inline efi_status_t
__open_volume64(void *__image
, void **__fh
)
228 efi_file_io_interface_t
*io
;
229 efi_loaded_image_64_t
*image
= __image
;
230 efi_file_handle_64_t
*fh
;
231 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
233 void *handle
= (void *)(unsigned long)image
->device_handle
;
236 status
= efi_call_early(handle_protocol
, handle
,
237 &fs_proto
, (void **)&io
);
238 if (status
!= EFI_SUCCESS
) {
239 efi_printk(sys_table
, "Failed to handle fs_proto\n");
243 func
= (unsigned long)io
->open_volume
;
244 status
= efi_early
->call(func
, io
, &fh
);
245 if (status
!= EFI_SUCCESS
)
246 efi_printk(sys_table
, "Failed to open volume\n");
253 efi_open_volume(efi_system_table_t
*sys_table
, void *__image
, void **__fh
)
256 return __open_volume64(__image
, __fh
);
258 return __open_volume32(__image
, __fh
);
261 void efi_char16_printk(efi_system_table_t
*table
, efi_char16_t
*str
)
263 unsigned long output_string
;
266 if (efi_early
->is64
) {
267 struct efi_simple_text_output_protocol_64
*out
;
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
= (u64
*)output_string
;
275 efi_early
->call(*func
, out
, str
);
277 struct efi_simple_text_output_protocol_32
*out
;
280 offset
= offsetof(typeof(*out
), output_string
);
281 output_string
= efi_early
->text_output
+ offset
;
282 out
= (typeof(out
))(unsigned long)efi_early
->text_output
;
283 func
= (u32
*)output_string
;
285 efi_early
->call(*func
, out
, str
);
289 static void find_bits(unsigned long mask
, u8
*pos
, u8
*size
)
297 while (!(mask
& 0x1)) {
313 __setup_efi_pci32(efi_pci_io_protocol_32
*pci
, struct pci_setup_rom
**__rom
)
315 struct pci_setup_rom
*rom
= NULL
;
320 status
= efi_early
->call(pci
->attributes
, pci
,
321 EfiPciIoAttributeOperationGet
, 0, 0,
323 if (status
!= EFI_SUCCESS
)
326 if (!pci
->romimage
|| !pci
->romsize
)
327 return EFI_INVALID_PARAMETER
;
329 size
= pci
->romsize
+ sizeof(*rom
);
331 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
332 if (status
!= EFI_SUCCESS
) {
333 efi_printk(sys_table
, "Failed to alloc mem for rom\n");
337 memset(rom
, 0, sizeof(*rom
));
339 rom
->data
.type
= SETUP_PCI
;
340 rom
->data
.len
= size
- sizeof(struct setup_data
);
342 rom
->pcilen
= pci
->romsize
;
345 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
346 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
348 if (status
!= EFI_SUCCESS
) {
349 efi_printk(sys_table
, "Failed to read rom->vendor\n");
353 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
354 PCI_DEVICE_ID
, 1, &(rom
->devid
));
356 if (status
!= EFI_SUCCESS
) {
357 efi_printk(sys_table
, "Failed to read rom->devid\n");
361 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
362 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
364 if (status
!= EFI_SUCCESS
)
367 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
371 efi_call_early(free_pool
, rom
);
376 setup_efi_pci32(struct boot_params
*params
, void **pci_handle
,
379 efi_pci_io_protocol_32
*pci
= NULL
;
380 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
381 u32
*handles
= (u32
*)(unsigned long)pci_handle
;
383 unsigned long nr_pci
;
384 struct setup_data
*data
;
387 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
389 while (data
&& data
->next
)
390 data
= (struct setup_data
*)(unsigned long)data
->next
;
392 nr_pci
= size
/ sizeof(u32
);
393 for (i
= 0; i
< nr_pci
; i
++) {
394 struct pci_setup_rom
*rom
= NULL
;
397 status
= efi_call_early(handle_protocol
, h
,
398 &pci_proto
, (void **)&pci
);
400 if (status
!= EFI_SUCCESS
)
406 status
= __setup_efi_pci32(pci
, &rom
);
407 if (status
!= EFI_SUCCESS
)
411 data
->next
= (unsigned long)rom
;
413 params
->hdr
.setup_data
= (unsigned long)rom
;
415 data
= (struct setup_data
*)rom
;
421 __setup_efi_pci64(efi_pci_io_protocol_64
*pci
, struct pci_setup_rom
**__rom
)
423 struct pci_setup_rom
*rom
;
428 status
= efi_early
->call(pci
->attributes
, pci
,
429 EfiPciIoAttributeOperationGet
, 0,
431 if (status
!= EFI_SUCCESS
)
434 if (!pci
->romimage
|| !pci
->romsize
)
435 return EFI_INVALID_PARAMETER
;
437 size
= pci
->romsize
+ sizeof(*rom
);
439 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
, size
, &rom
);
440 if (status
!= EFI_SUCCESS
) {
441 efi_printk(sys_table
, "Failed to alloc mem for rom\n");
445 rom
->data
.type
= SETUP_PCI
;
446 rom
->data
.len
= size
- sizeof(struct setup_data
);
448 rom
->pcilen
= pci
->romsize
;
451 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
452 PCI_VENDOR_ID
, 1, &(rom
->vendor
));
454 if (status
!= EFI_SUCCESS
) {
455 efi_printk(sys_table
, "Failed to read rom->vendor\n");
459 status
= efi_early
->call(pci
->pci
.read
, pci
, EfiPciIoWidthUint16
,
460 PCI_DEVICE_ID
, 1, &(rom
->devid
));
462 if (status
!= EFI_SUCCESS
) {
463 efi_printk(sys_table
, "Failed to read rom->devid\n");
467 status
= efi_early
->call(pci
->get_location
, pci
, &(rom
->segment
),
468 &(rom
->bus
), &(rom
->device
), &(rom
->function
));
470 if (status
!= EFI_SUCCESS
)
473 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
477 efi_call_early(free_pool
, rom
);
483 setup_efi_pci64(struct boot_params
*params
, void **pci_handle
,
486 efi_pci_io_protocol_64
*pci
= NULL
;
487 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
488 u64
*handles
= (u64
*)(unsigned long)pci_handle
;
490 unsigned long nr_pci
;
491 struct setup_data
*data
;
494 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
496 while (data
&& data
->next
)
497 data
= (struct setup_data
*)(unsigned long)data
->next
;
499 nr_pci
= size
/ sizeof(u64
);
500 for (i
= 0; i
< nr_pci
; i
++) {
501 struct pci_setup_rom
*rom
= NULL
;
504 status
= efi_call_early(handle_protocol
, h
,
505 &pci_proto
, (void **)&pci
);
507 if (status
!= EFI_SUCCESS
)
513 status
= __setup_efi_pci64(pci
, &rom
);
514 if (status
!= EFI_SUCCESS
)
518 data
->next
= (unsigned long)rom
;
520 params
->hdr
.setup_data
= (unsigned long)rom
;
522 data
= (struct setup_data
*)rom
;
528 * There's no way to return an informative status from this function,
529 * because any analysis (and printing of error messages) needs to be
530 * done directly at the EFI function call-site.
532 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
533 * just didn't find any PCI devices, but there's no way to tell outside
534 * the context of the call.
536 static void setup_efi_pci(struct boot_params
*params
)
539 void **pci_handle
= NULL
;
540 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
541 unsigned long size
= 0;
543 status
= efi_call_early(locate_handle
,
544 EFI_LOCATE_BY_PROTOCOL
,
545 &pci_proto
, NULL
, &size
, pci_handle
);
547 if (status
== EFI_BUFFER_TOO_SMALL
) {
548 status
= efi_call_early(allocate_pool
,
550 size
, (void **)&pci_handle
);
552 if (status
!= EFI_SUCCESS
) {
553 efi_printk(sys_table
, "Failed to alloc mem for pci_handle\n");
557 status
= efi_call_early(locate_handle
,
558 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
559 NULL
, &size
, pci_handle
);
562 if (status
!= EFI_SUCCESS
)
566 setup_efi_pci64(params
, pci_handle
, size
);
568 setup_efi_pci32(params
, pci_handle
, size
);
571 efi_call_early(free_pool
, pci_handle
);
575 setup_uga32(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
577 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
578 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
579 unsigned long nr_ugas
;
580 u32
*handles
= (u32
*)uga_handle
;;
585 nr_ugas
= size
/ sizeof(u32
);
586 for (i
= 0; i
< nr_ugas
; i
++) {
587 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
588 u32 w
, h
, depth
, refresh
;
590 u32 handle
= handles
[i
];
592 status
= efi_call_early(handle_protocol
, handle
,
593 &uga_proto
, (void **)&uga
);
594 if (status
!= EFI_SUCCESS
)
597 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
599 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
600 &w
, &h
, &depth
, &refresh
);
601 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
606 * Once we've found a UGA supporting PCIIO,
607 * don't bother looking any further.
620 setup_uga64(void **uga_handle
, unsigned long size
, u32
*width
, u32
*height
)
622 struct efi_uga_draw_protocol
*uga
= NULL
, *first_uga
;
623 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
624 unsigned long nr_ugas
;
625 u64
*handles
= (u64
*)uga_handle
;;
630 nr_ugas
= size
/ sizeof(u64
);
631 for (i
= 0; i
< nr_ugas
; i
++) {
632 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
633 u32 w
, h
, depth
, refresh
;
635 u64 handle
= handles
[i
];
637 status
= efi_call_early(handle_protocol
, handle
,
638 &uga_proto
, (void **)&uga
);
639 if (status
!= EFI_SUCCESS
)
642 efi_call_early(handle_protocol
, handle
, &pciio_proto
, &pciio
);
644 status
= efi_early
->call((unsigned long)uga
->get_mode
, uga
,
645 &w
, &h
, &depth
, &refresh
);
646 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
651 * Once we've found a UGA supporting PCIIO,
652 * don't bother looking any further.
665 * See if we have Universal Graphics Adapter (UGA) protocol
667 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
672 void **uga_handle
= NULL
;
674 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
675 size
, (void **)&uga_handle
);
676 if (status
!= EFI_SUCCESS
)
679 status
= efi_call_early(locate_handle
,
680 EFI_LOCATE_BY_PROTOCOL
,
681 uga_proto
, NULL
, &size
, uga_handle
);
682 if (status
!= EFI_SUCCESS
)
689 status
= setup_uga64(uga_handle
, size
, &width
, &height
);
691 status
= setup_uga32(uga_handle
, size
, &width
, &height
);
693 if (!width
&& !height
)
696 /* EFI framebuffer */
697 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
700 si
->lfb_width
= width
;
701 si
->lfb_height
= height
;
713 efi_call_early(free_pool
, uga_handle
);
717 void setup_graphics(struct boot_params
*boot_params
)
719 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
720 struct screen_info
*si
;
721 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
724 void **gop_handle
= NULL
;
725 void **uga_handle
= NULL
;
727 si
= &boot_params
->screen_info
;
728 memset(si
, 0, sizeof(*si
));
731 status
= efi_call_early(locate_handle
,
732 EFI_LOCATE_BY_PROTOCOL
,
733 &graphics_proto
, NULL
, &size
, gop_handle
);
734 if (status
== EFI_BUFFER_TOO_SMALL
)
735 status
= efi_setup_gop(NULL
, si
, &graphics_proto
, size
);
737 if (status
!= EFI_SUCCESS
) {
739 status
= efi_call_early(locate_handle
,
740 EFI_LOCATE_BY_PROTOCOL
,
741 &uga_proto
, NULL
, &size
, uga_handle
);
742 if (status
== EFI_BUFFER_TOO_SMALL
)
743 setup_uga(si
, &uga_proto
, size
);
748 * Because the x86 boot code expects to be passed a boot_params we
749 * need to create one ourselves (usually the bootloader would create
752 * The caller is responsible for filling out ->code32_start in the
753 * returned boot_params.
755 struct boot_params
*make_boot_params(struct efi_config
*c
)
757 struct boot_params
*boot_params
;
758 struct apm_bios_info
*bi
;
759 struct setup_header
*hdr
;
760 struct efi_info
*efi
;
761 efi_loaded_image_t
*image
;
762 void *options
, *handle
;
763 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
764 int options_size
= 0;
770 unsigned long ramdisk_addr
;
771 unsigned long ramdisk_size
;
774 sys_table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
775 handle
= (void *)(unsigned long)efi_early
->image_handle
;
777 /* Check if we were booted by the EFI firmware */
778 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
782 setup_boot_services64(efi_early
);
784 setup_boot_services32(efi_early
);
786 status
= efi_call_early(handle_protocol
, handle
,
787 &proto
, (void *)&image
);
788 if (status
!= EFI_SUCCESS
) {
789 efi_printk(sys_table
, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
793 status
= efi_low_alloc(sys_table
, 0x4000, 1,
794 (unsigned long *)&boot_params
);
795 if (status
!= EFI_SUCCESS
) {
796 efi_printk(sys_table
, "Failed to alloc lowmem for boot params\n");
800 memset(boot_params
, 0x0, 0x4000);
802 hdr
= &boot_params
->hdr
;
803 efi
= &boot_params
->efi_info
;
804 bi
= &boot_params
->apm_bios_info
;
806 /* Copy the second sector to boot_params */
807 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
810 * Fill out some of the header fields ourselves because the
811 * EFI firmware loader doesn't load the first sector.
814 hdr
->vid_mode
= 0xffff;
815 hdr
->boot_flag
= 0xAA55;
817 hdr
->type_of_loader
= 0x21;
819 /* Convert unicode cmdline to ascii */
820 cmdline_ptr
= efi_convert_cmdline(sys_table
, image
, &options_size
);
823 hdr
->cmd_line_ptr
= (unsigned long)cmdline_ptr
;
824 /* Fill in upper bits of command line address, NOP on 32 bit */
825 boot_params
->ext_cmd_line_ptr
= (u64
)(unsigned long)cmdline_ptr
>> 32;
827 hdr
->ramdisk_image
= 0;
828 hdr
->ramdisk_size
= 0;
830 /* Clear APM BIOS info */
831 memset(bi
, 0, sizeof(*bi
));
833 status
= efi_parse_options(cmdline_ptr
);
834 if (status
!= EFI_SUCCESS
)
837 status
= handle_cmdline_files(sys_table
, image
,
838 (char *)(unsigned long)hdr
->cmd_line_ptr
,
839 "initrd=", hdr
->initrd_addr_max
,
840 &ramdisk_addr
, &ramdisk_size
);
842 if (status
!= EFI_SUCCESS
&&
843 hdr
->xloadflags
& XLF_CAN_BE_LOADED_ABOVE_4G
) {
844 efi_printk(sys_table
, "Trying to load files to higher address\n");
845 status
= handle_cmdline_files(sys_table
, image
,
846 (char *)(unsigned long)hdr
->cmd_line_ptr
,
848 &ramdisk_addr
, &ramdisk_size
);
851 if (status
!= EFI_SUCCESS
)
853 hdr
->ramdisk_image
= ramdisk_addr
& 0xffffffff;
854 hdr
->ramdisk_size
= ramdisk_size
& 0xffffffff;
855 boot_params
->ext_ramdisk_image
= (u64
)ramdisk_addr
>> 32;
856 boot_params
->ext_ramdisk_size
= (u64
)ramdisk_size
>> 32;
860 efi_free(sys_table
, options_size
, hdr
->cmd_line_ptr
);
862 efi_free(sys_table
, 0x4000, (unsigned long)boot_params
);
866 static void add_e820ext(struct boot_params
*params
,
867 struct setup_data
*e820ext
, u32 nr_entries
)
869 struct setup_data
*data
;
873 e820ext
->type
= SETUP_E820_EXT
;
874 e820ext
->len
= nr_entries
* sizeof(struct e820entry
);
877 data
= (struct setup_data
*)(unsigned long)params
->hdr
.setup_data
;
879 while (data
&& data
->next
)
880 data
= (struct setup_data
*)(unsigned long)data
->next
;
883 data
->next
= (unsigned long)e820ext
;
885 params
->hdr
.setup_data
= (unsigned long)e820ext
;
888 static efi_status_t
setup_e820(struct boot_params
*params
,
889 struct setup_data
*e820ext
, u32 e820ext_size
)
891 struct e820entry
*e820_map
= ¶ms
->e820_map
[0];
892 struct efi_info
*efi
= ¶ms
->efi_info
;
893 struct e820entry
*prev
= NULL
;
899 nr_desc
= efi
->efi_memmap_size
/ efi
->efi_memdesc_size
;
901 for (i
= 0; i
< nr_desc
; i
++) {
902 efi_memory_desc_t
*d
;
903 unsigned int e820_type
= 0;
904 unsigned long m
= efi
->efi_memmap
;
907 m
|= (u64
)efi
->efi_memmap_hi
<< 32;
910 d
= (efi_memory_desc_t
*)(m
+ (i
* efi
->efi_memdesc_size
));
912 case EFI_RESERVED_TYPE
:
913 case EFI_RUNTIME_SERVICES_CODE
:
914 case EFI_RUNTIME_SERVICES_DATA
:
915 case EFI_MEMORY_MAPPED_IO
:
916 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
918 e820_type
= E820_RESERVED
;
921 case EFI_UNUSABLE_MEMORY
:
922 e820_type
= E820_UNUSABLE
;
925 case EFI_ACPI_RECLAIM_MEMORY
:
926 e820_type
= E820_ACPI
;
929 case EFI_LOADER_CODE
:
930 case EFI_LOADER_DATA
:
931 case EFI_BOOT_SERVICES_CODE
:
932 case EFI_BOOT_SERVICES_DATA
:
933 case EFI_CONVENTIONAL_MEMORY
:
934 e820_type
= E820_RAM
;
937 case EFI_ACPI_MEMORY_NVS
:
938 e820_type
= E820_NVS
;
941 case EFI_PERSISTENT_MEMORY
:
942 e820_type
= E820_PMEM
;
949 /* Merge adjacent mappings */
950 if (prev
&& prev
->type
== e820_type
&&
951 (prev
->addr
+ prev
->size
) == d
->phys_addr
) {
952 prev
->size
+= d
->num_pages
<< 12;
956 if (nr_entries
== ARRAY_SIZE(params
->e820_map
)) {
957 u32 need
= (nr_desc
- i
) * sizeof(struct e820entry
) +
958 sizeof(struct setup_data
);
960 if (!e820ext
|| e820ext_size
< need
)
961 return EFI_BUFFER_TOO_SMALL
;
963 /* boot_params map full, switch to e820 extended */
964 e820_map
= (struct e820entry
*)e820ext
->data
;
967 e820_map
->addr
= d
->phys_addr
;
968 e820_map
->size
= d
->num_pages
<< PAGE_SHIFT
;
969 e820_map
->type
= e820_type
;
974 if (nr_entries
> ARRAY_SIZE(params
->e820_map
)) {
975 u32 nr_e820ext
= nr_entries
- ARRAY_SIZE(params
->e820_map
);
977 add_e820ext(params
, e820ext
, nr_e820ext
);
978 nr_entries
-= nr_e820ext
;
981 params
->e820_entries
= (u8
)nr_entries
;
986 static efi_status_t
alloc_e820ext(u32 nr_desc
, struct setup_data
**e820ext
,
992 size
= sizeof(struct setup_data
) +
993 sizeof(struct e820entry
) * nr_desc
;
996 efi_call_early(free_pool
, *e820ext
);
1001 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1002 size
, (void **)e820ext
);
1003 if (status
== EFI_SUCCESS
)
1004 *e820ext_size
= size
;
1009 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
1010 void *handle
, bool is64
)
1012 struct efi_info
*efi
= &boot_params
->efi_info
;
1013 unsigned long map_sz
, key
, desc_size
;
1014 efi_memory_desc_t
*mem_map
;
1015 struct setup_data
*e820ext
;
1016 const char *signature
;
1018 __u32 nr_desc
, prev_nr_desc
;
1019 efi_status_t status
;
1021 bool called_exit
= false;
1030 status
= efi_get_memory_map(sys_table
, &mem_map
, &map_sz
, &desc_size
,
1031 &desc_version
, &key
);
1033 if (status
!= EFI_SUCCESS
)
1036 prev_nr_desc
= nr_desc
;
1037 nr_desc
= map_sz
/ desc_size
;
1038 if (nr_desc
> prev_nr_desc
&&
1039 nr_desc
> ARRAY_SIZE(boot_params
->e820_map
)) {
1040 u32 nr_e820ext
= nr_desc
- ARRAY_SIZE(boot_params
->e820_map
);
1042 status
= alloc_e820ext(nr_e820ext
, &e820ext
, &e820ext_size
);
1043 if (status
!= EFI_SUCCESS
)
1046 efi_call_early(free_pool
, mem_map
);
1047 goto get_map
; /* Allocated memory, get map again */
1050 signature
= is64
? EFI64_LOADER_SIGNATURE
: EFI32_LOADER_SIGNATURE
;
1051 memcpy(&efi
->efi_loader_signature
, signature
, sizeof(__u32
));
1053 efi
->efi_systab
= (unsigned long)sys_table
;
1054 efi
->efi_memdesc_size
= desc_size
;
1055 efi
->efi_memdesc_version
= desc_version
;
1056 efi
->efi_memmap
= (unsigned long)mem_map
;
1057 efi
->efi_memmap_size
= map_sz
;
1059 #ifdef CONFIG_X86_64
1060 efi
->efi_systab_hi
= (unsigned long)sys_table
>> 32;
1061 efi
->efi_memmap_hi
= (unsigned long)mem_map
>> 32;
1064 /* Might as well exit boot services now */
1065 status
= efi_call_early(exit_boot_services
, handle
, key
);
1066 if (status
!= EFI_SUCCESS
) {
1068 * ExitBootServices() will fail if any of the event
1069 * handlers change the memory map. In which case, we
1070 * must be prepared to retry, but only once so that
1071 * we're guaranteed to exit on repeated failures instead
1072 * of spinning forever.
1078 efi_call_early(free_pool
, mem_map
);
1083 boot_params
->alt_mem_k
= 32 * 1024;
1085 status
= setup_e820(boot_params
, e820ext
, e820ext_size
);
1086 if (status
!= EFI_SUCCESS
)
1092 efi_call_early(free_pool
, mem_map
);
1097 * On success we return a pointer to a boot_params structure, and NULL
1100 struct boot_params
*efi_main(struct efi_config
*c
,
1101 struct boot_params
*boot_params
)
1103 struct desc_ptr
*gdt
= NULL
;
1104 efi_loaded_image_t
*image
;
1105 struct setup_header
*hdr
= &boot_params
->hdr
;
1106 efi_status_t status
;
1107 struct desc_struct
*desc
;
1109 efi_system_table_t
*_table
;
1114 _table
= (efi_system_table_t
*)(unsigned long)efi_early
->table
;
1115 handle
= (void *)(unsigned long)efi_early
->image_handle
;
1116 is64
= efi_early
->is64
;
1120 /* Check if we were booted by the EFI firmware */
1121 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1125 setup_boot_services64(efi_early
);
1127 setup_boot_services32(efi_early
);
1129 setup_graphics(boot_params
);
1131 setup_efi_pci(boot_params
);
1133 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
1134 sizeof(*gdt
), (void **)&gdt
);
1135 if (status
!= EFI_SUCCESS
) {
1136 efi_printk(sys_table
, "Failed to alloc mem for gdt structure\n");
1141 status
= efi_low_alloc(sys_table
, gdt
->size
, 8,
1142 (unsigned long *)&gdt
->address
);
1143 if (status
!= EFI_SUCCESS
) {
1144 efi_printk(sys_table
, "Failed to alloc mem for gdt\n");
1149 * If the kernel isn't already loaded at the preferred load
1150 * address, relocate it.
1152 if (hdr
->pref_address
!= hdr
->code32_start
) {
1153 unsigned long bzimage_addr
= hdr
->code32_start
;
1154 status
= efi_relocate_kernel(sys_table
, &bzimage_addr
,
1155 hdr
->init_size
, hdr
->init_size
,
1157 hdr
->kernel_alignment
);
1158 if (status
!= EFI_SUCCESS
) {
1159 efi_printk(sys_table
, "efi_relocate_kernel() failed!\n");
1163 hdr
->pref_address
= hdr
->code32_start
;
1164 hdr
->code32_start
= bzimage_addr
;
1167 status
= exit_boot(boot_params
, handle
, is64
);
1168 if (status
!= EFI_SUCCESS
) {
1169 efi_printk(sys_table
, "exit_boot() failed!\n");
1173 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1174 desc
= (struct desc_struct
*)gdt
->address
;
1176 /* The first GDT is a dummy and the second is unused. */
1179 desc
->limit0
= 0xffff;
1180 desc
->base0
= 0x0000;
1181 desc
->base1
= 0x0000;
1182 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1183 desc
->s
= DESC_TYPE_CODE_DATA
;
1189 desc
->d
= SEG_OP_SIZE_32BIT
;
1190 desc
->g
= SEG_GRANULARITY_4KB
;
1194 desc
->limit0
= 0xffff;
1195 desc
->base0
= 0x0000;
1196 desc
->base1
= 0x0000;
1197 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1198 desc
->s
= DESC_TYPE_CODE_DATA
;
1204 desc
->d
= SEG_OP_SIZE_32BIT
;
1205 desc
->g
= SEG_GRANULARITY_4KB
;
1208 #ifdef CONFIG_X86_64
1209 /* Task segment value */
1211 desc
->limit0
= 0x0000;
1212 desc
->base0
= 0x0000;
1213 desc
->base1
= 0x0000;
1214 desc
->type
= SEG_TYPE_TSS
;
1222 desc
->g
= SEG_GRANULARITY_4KB
;
1224 #endif /* CONFIG_X86_64 */
1226 asm volatile("cli");
1227 asm volatile ("lgdt %0" : : "m" (*gdt
));
1231 efi_printk(sys_table
, "efi_main() failed!\n");