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 #undef memcpy /* Use memcpy from misc.c */
20 static efi_system_table_t
*sys_table
;
22 static void efi_printk(char *str
)
26 for (s8
= str
; *s8
; s8
++) {
27 struct efi_simple_text_output_protocol
*out
;
28 efi_char16_t ch
[2] = { 0 };
31 out
= (struct efi_simple_text_output_protocol
*)sys_table
->con_out
;
34 efi_char16_t nl
[2] = { '\r', 0 };
35 efi_call_phys2(out
->output_string
, out
, nl
);
38 efi_call_phys2(out
->output_string
, out
, ch
);
42 static efi_status_t
__get_map(efi_memory_desc_t
**map
, unsigned long *map_size
,
43 unsigned long *desc_size
)
45 efi_memory_desc_t
*m
= NULL
;
50 *map_size
= sizeof(*m
) * 32;
53 * Add an additional efi_memory_desc_t because we're doing an
54 * allocation which may be in a new descriptor region.
56 *map_size
+= sizeof(*m
);
57 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
58 EFI_LOADER_DATA
, *map_size
, (void **)&m
);
59 if (status
!= EFI_SUCCESS
)
62 status
= efi_call_phys5(sys_table
->boottime
->get_memory_map
, map_size
,
63 m
, &key
, desc_size
, &desc_version
);
64 if (status
== EFI_BUFFER_TOO_SMALL
) {
65 efi_call_phys1(sys_table
->boottime
->free_pool
, m
);
69 if (status
!= EFI_SUCCESS
)
70 efi_call_phys1(sys_table
->boottime
->free_pool
, m
);
78 * Allocate at the highest possible address that is not above 'max'.
80 static efi_status_t
high_alloc(unsigned long size
, unsigned long align
,
81 unsigned long *addr
, unsigned long max
)
83 unsigned long map_size
, desc_size
;
84 efi_memory_desc_t
*map
;
86 unsigned long nr_pages
;
90 status
= __get_map(&map
, &map_size
, &desc_size
);
91 if (status
!= EFI_SUCCESS
)
94 nr_pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
96 for (i
= 0; i
< map_size
/ desc_size
; i
++) {
97 efi_memory_desc_t
*desc
;
98 unsigned long m
= (unsigned long)map
;
101 desc
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
102 if (desc
->type
!= EFI_CONVENTIONAL_MEMORY
)
105 if (desc
->num_pages
< nr_pages
)
108 start
= desc
->phys_addr
;
109 end
= start
+ desc
->num_pages
* (1UL << EFI_PAGE_SHIFT
);
111 if ((start
+ size
) > end
|| (start
+ size
) > max
)
114 if (end
- size
> max
)
117 if (round_down(end
- size
, align
) < start
)
120 start
= round_down(end
- size
, align
);
123 * Don't allocate at 0x0. It will confuse code that
124 * checks pointers against NULL.
129 if (start
> max_addr
)
134 status
= EFI_NOT_FOUND
;
136 status
= efi_call_phys4(sys_table
->boottime
->allocate_pages
,
137 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
138 nr_pages
, &max_addr
);
139 if (status
!= EFI_SUCCESS
) {
149 efi_call_phys1(sys_table
->boottime
->free_pool
, map
);
156 * Allocate at the lowest possible address.
158 static efi_status_t
low_alloc(unsigned long size
, unsigned long align
,
161 unsigned long map_size
, desc_size
;
162 efi_memory_desc_t
*map
;
164 unsigned long nr_pages
;
167 status
= __get_map(&map
, &map_size
, &desc_size
);
168 if (status
!= EFI_SUCCESS
)
171 nr_pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
172 for (i
= 0; i
< map_size
/ desc_size
; i
++) {
173 efi_memory_desc_t
*desc
;
174 unsigned long m
= (unsigned long)map
;
177 desc
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
179 if (desc
->type
!= EFI_CONVENTIONAL_MEMORY
)
182 if (desc
->num_pages
< nr_pages
)
185 start
= desc
->phys_addr
;
186 end
= start
+ desc
->num_pages
* (1UL << EFI_PAGE_SHIFT
);
189 * Don't allocate at 0x0. It will confuse code that
190 * checks pointers against NULL. Skip the first 8
191 * bytes so we start at a nice even number.
196 start
= round_up(start
, align
);
197 if ((start
+ size
) > end
)
200 status
= efi_call_phys4(sys_table
->boottime
->allocate_pages
,
201 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
203 if (status
== EFI_SUCCESS
) {
209 if (i
== map_size
/ desc_size
)
210 status
= EFI_NOT_FOUND
;
213 efi_call_phys1(sys_table
->boottime
->free_pool
, map
);
218 static void low_free(unsigned long size
, unsigned long addr
)
220 unsigned long nr_pages
;
222 nr_pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
223 efi_call_phys2(sys_table
->boottime
->free_pages
, addr
, size
);
226 static void find_bits(unsigned long mask
, u8
*pos
, u8
*size
)
234 while (!(mask
& 0x1)) {
249 static efi_status_t
setup_efi_pci(struct boot_params
*params
)
251 efi_pci_io_protocol
*pci
;
254 efi_guid_t pci_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
255 unsigned long nr_pci
, size
= 0;
257 struct setup_data
*data
;
259 data
= (struct setup_data
*)params
->hdr
.setup_data
;
261 while (data
&& data
->next
)
262 data
= (struct setup_data
*)data
->next
;
264 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
265 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
266 NULL
, &size
, pci_handle
);
268 if (status
== EFI_BUFFER_TOO_SMALL
) {
269 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
270 EFI_LOADER_DATA
, size
, &pci_handle
);
272 if (status
!= EFI_SUCCESS
)
275 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
276 EFI_LOCATE_BY_PROTOCOL
, &pci_proto
,
277 NULL
, &size
, pci_handle
);
280 if (status
!= EFI_SUCCESS
)
283 nr_pci
= size
/ sizeof(void *);
284 for (i
= 0; i
< nr_pci
; i
++) {
285 void *h
= pci_handle
[i
];
287 struct pci_setup_rom
*rom
;
289 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
290 h
, &pci_proto
, &pci
);
292 if (status
!= EFI_SUCCESS
)
298 status
= efi_call_phys4(pci
->attributes
, pci
,
299 EfiPciIoAttributeOperationGet
, 0,
302 if (status
!= EFI_SUCCESS
)
305 if (!attributes
& EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM
)
308 if (!pci
->romimage
|| !pci
->romsize
)
311 size
= pci
->romsize
+ sizeof(*rom
);
313 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
314 EFI_LOADER_DATA
, size
, &rom
);
316 if (status
!= EFI_SUCCESS
)
319 rom
->data
.type
= SETUP_PCI
;
320 rom
->data
.len
= size
- sizeof(struct setup_data
);
322 rom
->pcilen
= pci
->romsize
;
324 status
= efi_call_phys5(pci
->pci
.read
, pci
,
325 EfiPciIoWidthUint16
, PCI_VENDOR_ID
,
328 if (status
!= EFI_SUCCESS
)
331 status
= efi_call_phys5(pci
->pci
.read
, pci
,
332 EfiPciIoWidthUint16
, PCI_DEVICE_ID
,
335 if (status
!= EFI_SUCCESS
)
338 status
= efi_call_phys5(pci
->get_location
, pci
,
339 &(rom
->segment
), &(rom
->bus
),
340 &(rom
->device
), &(rom
->function
));
342 if (status
!= EFI_SUCCESS
)
345 memcpy(rom
->romdata
, pci
->romimage
, pci
->romsize
);
348 data
->next
= (uint64_t)rom
;
350 params
->hdr
.setup_data
= (uint64_t)rom
;
352 data
= (struct setup_data
*)rom
;
356 efi_call_phys1(sys_table
->boottime
->free_pool
, rom
);
360 efi_call_phys1(sys_table
->boottime
->free_pool
, pci_handle
);
365 * See if we have Graphics Output Protocol
367 static efi_status_t
setup_gop(struct screen_info
*si
, efi_guid_t
*proto
,
370 struct efi_graphics_output_protocol
*gop
, *first_gop
;
371 struct efi_pixel_bitmask pixel_info
;
372 unsigned long nr_gops
;
376 u32 fb_base
, fb_size
;
377 u32 pixels_per_scan_line
;
381 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
382 EFI_LOADER_DATA
, size
, &gop_handle
);
383 if (status
!= EFI_SUCCESS
)
386 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
387 EFI_LOCATE_BY_PROTOCOL
, proto
,
388 NULL
, &size
, gop_handle
);
389 if (status
!= EFI_SUCCESS
)
394 nr_gops
= size
/ sizeof(void *);
395 for (i
= 0; i
< nr_gops
; i
++) {
396 struct efi_graphics_output_mode_info
*info
;
397 efi_guid_t conout_proto
= EFI_CONSOLE_OUT_DEVICE_GUID
;
398 bool conout_found
= false;
400 void *h
= gop_handle
[i
];
402 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
404 if (status
!= EFI_SUCCESS
)
407 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
408 h
, &conout_proto
, &dummy
);
410 if (status
== EFI_SUCCESS
)
413 status
= efi_call_phys4(gop
->query_mode
, gop
,
414 gop
->mode
->mode
, &size
, &info
);
415 if (status
== EFI_SUCCESS
&& (!first_gop
|| conout_found
)) {
417 * Systems that use the UEFI Console Splitter may
418 * provide multiple GOP devices, not all of which are
419 * backed by real hardware. The workaround is to search
420 * for a GOP implementing the ConOut protocol, and if
421 * one isn't found, to just fall back to the first GOP.
423 width
= info
->horizontal_resolution
;
424 height
= info
->vertical_resolution
;
425 fb_base
= gop
->mode
->frame_buffer_base
;
426 fb_size
= gop
->mode
->frame_buffer_size
;
427 pixel_format
= info
->pixel_format
;
428 pixel_info
= info
->pixel_information
;
429 pixels_per_scan_line
= info
->pixels_per_scan_line
;
432 * Once we've found a GOP supporting ConOut,
433 * don't bother looking any further.
442 /* Did we find any GOPs? */
446 /* EFI framebuffer */
447 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
449 si
->lfb_width
= width
;
450 si
->lfb_height
= height
;
451 si
->lfb_base
= fb_base
;
454 if (pixel_format
== PIXEL_RGB_RESERVED_8BIT_PER_COLOR
) {
456 si
->lfb_linelength
= pixels_per_scan_line
* 4;
465 } else if (pixel_format
== PIXEL_BGR_RESERVED_8BIT_PER_COLOR
) {
467 si
->lfb_linelength
= pixels_per_scan_line
* 4;
476 } else if (pixel_format
== PIXEL_BIT_MASK
) {
477 find_bits(pixel_info
.red_mask
, &si
->red_pos
, &si
->red_size
);
478 find_bits(pixel_info
.green_mask
, &si
->green_pos
,
480 find_bits(pixel_info
.blue_mask
, &si
->blue_pos
, &si
->blue_size
);
481 find_bits(pixel_info
.reserved_mask
, &si
->rsvd_pos
,
483 si
->lfb_depth
= si
->red_size
+ si
->green_size
+
484 si
->blue_size
+ si
->rsvd_size
;
485 si
->lfb_linelength
= (pixels_per_scan_line
* si
->lfb_depth
) / 8;
488 si
->lfb_linelength
= si
->lfb_width
/ 2;
499 si
->lfb_size
= si
->lfb_linelength
* si
->lfb_height
;
501 si
->capabilities
|= VIDEO_CAPABILITY_SKIP_QUIRKS
;
504 efi_call_phys1(sys_table
->boottime
->free_pool
, gop_handle
);
509 * See if we have Universal Graphics Adapter (UGA) protocol
511 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
514 struct efi_uga_draw_protocol
*uga
, *first_uga
;
515 unsigned long nr_ugas
;
518 void **uga_handle
= NULL
;
521 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
522 EFI_LOADER_DATA
, size
, &uga_handle
);
523 if (status
!= EFI_SUCCESS
)
526 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
527 EFI_LOCATE_BY_PROTOCOL
, uga_proto
,
528 NULL
, &size
, uga_handle
);
529 if (status
!= EFI_SUCCESS
)
534 nr_ugas
= size
/ sizeof(void *);
535 for (i
= 0; i
< nr_ugas
; i
++) {
536 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
537 void *handle
= uga_handle
[i
];
538 u32 w
, h
, depth
, refresh
;
541 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
542 handle
, uga_proto
, &uga
);
543 if (status
!= EFI_SUCCESS
)
546 efi_call_phys3(sys_table
->boottime
->handle_protocol
,
547 handle
, &pciio_proto
, &pciio
);
549 status
= efi_call_phys5(uga
->get_mode
, uga
, &w
, &h
,
551 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
556 * Once we've found a UGA supporting PCIIO,
557 * don't bother looking any further.
569 /* EFI framebuffer */
570 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
573 si
->lfb_width
= width
;
574 si
->lfb_height
= height
;
587 efi_call_phys1(sys_table
->boottime
->free_pool
, uga_handle
);
591 void setup_graphics(struct boot_params
*boot_params
)
593 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
594 struct screen_info
*si
;
595 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
598 void **gop_handle
= NULL
;
599 void **uga_handle
= NULL
;
601 si
= &boot_params
->screen_info
;
602 memset(si
, 0, sizeof(*si
));
605 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
606 EFI_LOCATE_BY_PROTOCOL
, &graphics_proto
,
607 NULL
, &size
, gop_handle
);
608 if (status
== EFI_BUFFER_TOO_SMALL
)
609 status
= setup_gop(si
, &graphics_proto
, size
);
611 if (status
!= EFI_SUCCESS
) {
613 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
614 EFI_LOCATE_BY_PROTOCOL
, &uga_proto
,
615 NULL
, &size
, uga_handle
);
616 if (status
== EFI_BUFFER_TOO_SMALL
)
617 setup_uga(si
, &uga_proto
, size
);
622 efi_file_handle_t
*handle
;
627 * Check the cmdline for a LILO-style initrd= arguments.
629 * We only support loading an initrd from the same filesystem as the
632 static efi_status_t
handle_ramdisks(efi_loaded_image_t
*image
,
633 struct setup_header
*hdr
)
635 struct initrd
*initrds
;
636 unsigned long initrd_addr
;
637 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
639 efi_file_io_interface_t
*io
;
640 efi_file_handle_t
*fh
;
649 str
= (char *)(unsigned long)hdr
->cmd_line_ptr
;
651 j
= 0; /* See close_handles */
656 for (nr_initrds
= 0; *str
; nr_initrds
++) {
657 str
= strstr(str
, "initrd=");
663 /* Skip any leading slashes */
664 while (*str
== '/' || *str
== '\\')
667 while (*str
&& *str
!= ' ' && *str
!= '\n')
674 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
676 nr_initrds
* sizeof(*initrds
),
678 if (status
!= EFI_SUCCESS
) {
679 efi_printk("Failed to alloc mem for initrds\n");
683 str
= (char *)(unsigned long)hdr
->cmd_line_ptr
;
684 for (i
= 0; i
< nr_initrds
; i
++) {
685 struct initrd
*initrd
;
686 efi_file_handle_t
*h
;
687 efi_file_info_t
*info
;
688 efi_char16_t filename_16
[256];
689 unsigned long info_sz
;
690 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
694 str
= strstr(str
, "initrd=");
700 initrd
= &initrds
[i
];
703 /* Skip any leading slashes */
704 while (*str
== '/' || *str
== '\\')
707 while (*str
&& *str
!= ' ' && *str
!= '\n') {
708 if ((u8
*)p
>= (u8
*)filename_16
+ sizeof(filename_16
))
716 /* Only open the volume once. */
718 efi_boot_services_t
*boottime
;
720 boottime
= sys_table
->boottime
;
722 status
= efi_call_phys3(boottime
->handle_protocol
,
723 image
->device_handle
, &fs_proto
, &io
);
724 if (status
!= EFI_SUCCESS
) {
725 efi_printk("Failed to handle fs_proto\n");
729 status
= efi_call_phys2(io
->open_volume
, io
, &fh
);
730 if (status
!= EFI_SUCCESS
) {
731 efi_printk("Failed to open volume\n");
736 status
= efi_call_phys5(fh
->open
, fh
, &h
, filename_16
,
737 EFI_FILE_MODE_READ
, (u64
)0);
738 if (status
!= EFI_SUCCESS
) {
739 efi_printk("Failed to open initrd file\n");
746 status
= efi_call_phys4(h
->get_info
, h
, &info_guid
,
748 if (status
!= EFI_BUFFER_TOO_SMALL
) {
749 efi_printk("Failed to get initrd info size\n");
754 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
755 EFI_LOADER_DATA
, info_sz
, &info
);
756 if (status
!= EFI_SUCCESS
) {
757 efi_printk("Failed to alloc mem for initrd info\n");
761 status
= efi_call_phys4(h
->get_info
, h
, &info_guid
,
763 if (status
== EFI_BUFFER_TOO_SMALL
) {
764 efi_call_phys1(sys_table
->boottime
->free_pool
, info
);
768 file_sz
= info
->file_size
;
769 efi_call_phys1(sys_table
->boottime
->free_pool
, info
);
771 if (status
!= EFI_SUCCESS
) {
772 efi_printk("Failed to get initrd info\n");
776 initrd
->size
= file_sz
;
777 initrd_total
+= file_sz
;
784 * Multiple initrd's need to be at consecutive
785 * addresses in memory, so allocate enough memory for
788 status
= high_alloc(initrd_total
, 0x1000,
789 &initrd_addr
, hdr
->initrd_addr_max
);
790 if (status
!= EFI_SUCCESS
) {
791 efi_printk("Failed to alloc highmem for initrds\n");
795 /* We've run out of free low memory. */
796 if (initrd_addr
> hdr
->initrd_addr_max
) {
797 efi_printk("We've run out of free low memory\n");
798 status
= EFI_INVALID_PARAMETER
;
799 goto free_initrd_total
;
803 for (j
= 0; j
< nr_initrds
; j
++) {
806 size
= initrds
[j
].size
;
809 if (size
> EFI_READ_CHUNK_SIZE
)
810 chunksize
= EFI_READ_CHUNK_SIZE
;
813 status
= efi_call_phys3(fh
->read
,
816 if (status
!= EFI_SUCCESS
) {
817 efi_printk("Failed to read initrd\n");
818 goto free_initrd_total
;
824 efi_call_phys1(fh
->close
, initrds
[j
].handle
);
829 efi_call_phys1(sys_table
->boottime
->free_pool
, initrds
);
831 hdr
->ramdisk_image
= initrd_addr
;
832 hdr
->ramdisk_size
= initrd_total
;
837 low_free(initrd_total
, initrd_addr
);
840 for (k
= j
; k
< i
; k
++)
841 efi_call_phys1(fh
->close
, initrds
[k
].handle
);
843 efi_call_phys1(sys_table
->boottime
->free_pool
, initrds
);
845 hdr
->ramdisk_image
= 0;
846 hdr
->ramdisk_size
= 0;
852 * Because the x86 boot code expects to be passed a boot_params we
853 * need to create one ourselves (usually the bootloader would create
856 struct boot_params
*make_boot_params(void *handle
, efi_system_table_t
*_table
)
858 struct boot_params
*boot_params
;
859 struct sys_desc_table
*sdt
;
860 struct apm_bios_info
*bi
;
861 struct setup_header
*hdr
;
862 struct efi_info
*efi
;
863 efi_loaded_image_t
*image
;
865 u32 load_options_size
;
866 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
867 int options_size
= 0;
869 unsigned long cmdline
;
876 /* Check if we were booted by the EFI firmware */
877 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
880 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
881 handle
, &proto
, (void *)&image
);
882 if (status
!= EFI_SUCCESS
) {
883 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
887 status
= low_alloc(0x4000, 1, (unsigned long *)&boot_params
);
888 if (status
!= EFI_SUCCESS
) {
889 efi_printk("Failed to alloc lowmem for boot params\n");
893 memset(boot_params
, 0x0, 0x4000);
895 hdr
= &boot_params
->hdr
;
896 efi
= &boot_params
->efi_info
;
897 bi
= &boot_params
->apm_bios_info
;
898 sdt
= &boot_params
->sys_desc_table
;
900 /* Copy the second sector to boot_params */
901 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
904 * Fill out some of the header fields ourselves because the
905 * EFI firmware loader doesn't load the first sector.
908 hdr
->vid_mode
= 0xffff;
909 hdr
->boot_flag
= 0xAA55;
911 hdr
->code32_start
= (__u64
)(unsigned long)image
->image_base
;
913 hdr
->type_of_loader
= 0x21;
915 /* Convert unicode cmdline to ascii */
916 options
= image
->load_options
;
917 load_options_size
= image
->load_options_size
/ 2; /* ASCII */
922 while (*s2
&& *s2
!= '\n' && options_size
< load_options_size
) {
928 if (options_size
> hdr
->cmdline_size
)
929 options_size
= hdr
->cmdline_size
;
931 options_size
++; /* NUL termination */
933 status
= low_alloc(options_size
, 1, &cmdline
);
934 if (status
!= EFI_SUCCESS
) {
935 efi_printk("Failed to alloc mem for cmdline\n");
939 s1
= (u8
*)(unsigned long)cmdline
;
942 for (i
= 0; i
< options_size
- 1; i
++)
949 hdr
->cmd_line_ptr
= cmdline
;
951 hdr
->ramdisk_image
= 0;
952 hdr
->ramdisk_size
= 0;
954 /* Clear APM BIOS info */
955 memset(bi
, 0, sizeof(*bi
));
957 memset(sdt
, 0, sizeof(*sdt
));
959 status
= handle_ramdisks(image
, hdr
);
960 if (status
!= EFI_SUCCESS
)
966 low_free(options_size
, hdr
->cmd_line_ptr
);
968 low_free(0x4000, (unsigned long)boot_params
);
972 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
975 struct efi_info
*efi
= &boot_params
->efi_info
;
976 struct e820entry
*e820_map
= &boot_params
->e820_map
[0];
977 struct e820entry
*prev
= NULL
;
978 unsigned long size
, key
, desc_size
, _size
;
979 efi_memory_desc_t
*mem_map
;
985 size
= sizeof(*mem_map
) * 32;
988 size
+= sizeof(*mem_map
);
990 status
= low_alloc(size
, 1, (unsigned long *)&mem_map
);
991 if (status
!= EFI_SUCCESS
)
994 status
= efi_call_phys5(sys_table
->boottime
->get_memory_map
, &size
,
995 mem_map
, &key
, &desc_size
, &desc_version
);
996 if (status
== EFI_BUFFER_TOO_SMALL
) {
997 low_free(_size
, (unsigned long)mem_map
);
1001 if (status
!= EFI_SUCCESS
)
1004 memcpy(&efi
->efi_loader_signature
, EFI_LOADER_SIGNATURE
, sizeof(__u32
));
1005 efi
->efi_systab
= (unsigned long)sys_table
;
1006 efi
->efi_memdesc_size
= desc_size
;
1007 efi
->efi_memdesc_version
= desc_version
;
1008 efi
->efi_memmap
= (unsigned long)mem_map
;
1009 efi
->efi_memmap_size
= size
;
1011 #ifdef CONFIG_X86_64
1012 efi
->efi_systab_hi
= (unsigned long)sys_table
>> 32;
1013 efi
->efi_memmap_hi
= (unsigned long)mem_map
>> 32;
1016 /* Might as well exit boot services now */
1017 status
= efi_call_phys2(sys_table
->boottime
->exit_boot_services
,
1019 if (status
!= EFI_SUCCESS
)
1023 boot_params
->alt_mem_k
= 32 * 1024;
1026 * Convert the EFI memory map to E820.
1029 for (i
= 0; i
< size
/ desc_size
; i
++) {
1030 efi_memory_desc_t
*d
;
1031 unsigned int e820_type
= 0;
1032 unsigned long m
= (unsigned long)mem_map
;
1034 d
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
1036 case EFI_RESERVED_TYPE
:
1037 case EFI_RUNTIME_SERVICES_CODE
:
1038 case EFI_RUNTIME_SERVICES_DATA
:
1039 case EFI_MEMORY_MAPPED_IO
:
1040 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1042 e820_type
= E820_RESERVED
;
1045 case EFI_UNUSABLE_MEMORY
:
1046 e820_type
= E820_UNUSABLE
;
1049 case EFI_ACPI_RECLAIM_MEMORY
:
1050 e820_type
= E820_ACPI
;
1053 case EFI_LOADER_CODE
:
1054 case EFI_LOADER_DATA
:
1055 case EFI_BOOT_SERVICES_CODE
:
1056 case EFI_BOOT_SERVICES_DATA
:
1057 case EFI_CONVENTIONAL_MEMORY
:
1058 e820_type
= E820_RAM
;
1061 case EFI_ACPI_MEMORY_NVS
:
1062 e820_type
= E820_NVS
;
1069 /* Merge adjacent mappings */
1070 if (prev
&& prev
->type
== e820_type
&&
1071 (prev
->addr
+ prev
->size
) == d
->phys_addr
)
1072 prev
->size
+= d
->num_pages
<< 12;
1074 e820_map
->addr
= d
->phys_addr
;
1075 e820_map
->size
= d
->num_pages
<< 12;
1076 e820_map
->type
= e820_type
;
1082 boot_params
->e820_entries
= nr_entries
;
1087 low_free(_size
, (unsigned long)mem_map
);
1091 static efi_status_t
relocate_kernel(struct setup_header
*hdr
)
1093 unsigned long start
, nr_pages
;
1094 efi_status_t status
;
1097 * The EFI firmware loader could have placed the kernel image
1098 * anywhere in memory, but the kernel has various restrictions
1099 * on the max physical address it can run at. Attempt to move
1100 * the kernel to boot_params.pref_address, or as low as
1103 start
= hdr
->pref_address
;
1104 nr_pages
= round_up(hdr
->init_size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
1106 status
= efi_call_phys4(sys_table
->boottime
->allocate_pages
,
1107 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
1109 if (status
!= EFI_SUCCESS
) {
1110 status
= low_alloc(hdr
->init_size
, hdr
->kernel_alignment
,
1112 if (status
!= EFI_SUCCESS
)
1113 efi_printk("Failed to alloc mem for kernel\n");
1116 if (status
== EFI_SUCCESS
)
1117 memcpy((void *)start
, (void *)(unsigned long)hdr
->code32_start
,
1120 hdr
->pref_address
= hdr
->code32_start
;
1121 hdr
->code32_start
= (__u32
)start
;
1127 * On success we return a pointer to a boot_params structure, and NULL
1130 struct boot_params
*efi_main(void *handle
, efi_system_table_t
*_table
,
1131 struct boot_params
*boot_params
)
1133 struct desc_ptr
*gdt
, *idt
;
1134 efi_loaded_image_t
*image
;
1135 struct setup_header
*hdr
= &boot_params
->hdr
;
1136 efi_status_t status
;
1137 struct desc_struct
*desc
;
1141 /* Check if we were booted by the EFI firmware */
1142 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1145 setup_graphics(boot_params
);
1147 setup_efi_pci(boot_params
);
1149 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
1150 EFI_LOADER_DATA
, sizeof(*gdt
),
1152 if (status
!= EFI_SUCCESS
) {
1153 efi_printk("Failed to alloc mem for gdt structure\n");
1158 status
= low_alloc(gdt
->size
, 8, (unsigned long *)&gdt
->address
);
1159 if (status
!= EFI_SUCCESS
) {
1160 efi_printk("Failed to alloc mem for gdt\n");
1164 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
1165 EFI_LOADER_DATA
, sizeof(*idt
),
1167 if (status
!= EFI_SUCCESS
) {
1168 efi_printk("Failed to alloc mem for idt structure\n");
1176 * If the kernel isn't already loaded at the preferred load
1177 * address, relocate it.
1179 if (hdr
->pref_address
!= hdr
->code32_start
) {
1180 status
= relocate_kernel(hdr
);
1182 if (status
!= EFI_SUCCESS
)
1186 status
= exit_boot(boot_params
, handle
);
1187 if (status
!= EFI_SUCCESS
)
1190 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1191 desc
= (struct desc_struct
*)gdt
->address
;
1193 /* The first GDT is a dummy and the second is unused. */
1196 desc
->limit0
= 0xffff;
1197 desc
->base0
= 0x0000;
1198 desc
->base1
= 0x0000;
1199 desc
->type
= SEG_TYPE_CODE
| SEG_TYPE_EXEC_READ
;
1200 desc
->s
= DESC_TYPE_CODE_DATA
;
1206 desc
->d
= SEG_OP_SIZE_32BIT
;
1207 desc
->g
= SEG_GRANULARITY_4KB
;
1211 desc
->limit0
= 0xffff;
1212 desc
->base0
= 0x0000;
1213 desc
->base1
= 0x0000;
1214 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1215 desc
->s
= DESC_TYPE_CODE_DATA
;
1221 desc
->d
= SEG_OP_SIZE_32BIT
;
1222 desc
->g
= SEG_GRANULARITY_4KB
;
1225 #ifdef CONFIG_X86_64
1226 /* Task segment value */
1228 desc
->limit0
= 0x0000;
1229 desc
->base0
= 0x0000;
1230 desc
->base1
= 0x0000;
1231 desc
->type
= SEG_TYPE_TSS
;
1239 desc
->g
= SEG_GRANULARITY_4KB
;
1241 #endif /* CONFIG_X86_64 */
1243 asm volatile ("lidt %0" : : "m" (*idt
));
1244 asm volatile ("lgdt %0" : : "m" (*gdt
));
1246 asm volatile("cli");