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>
12 #include <asm/setup.h>
15 #undef memcpy /* Use memcpy from misc.c */
19 static efi_system_table_t
*sys_table
;
21 static void efi_printk(char *str
)
25 for (s8
= str
; *s8
; s8
++) {
26 struct efi_simple_text_output_protocol
*out
;
27 efi_char16_t ch
[2] = { 0 };
30 out
= (struct efi_simple_text_output_protocol
*)sys_table
->con_out
;
33 efi_char16_t nl
[2] = { '\r', 0 };
34 efi_call_phys2(out
->output_string
, out
, nl
);
37 efi_call_phys2(out
->output_string
, out
, ch
);
41 static efi_status_t
__get_map(efi_memory_desc_t
**map
, unsigned long *map_size
,
42 unsigned long *desc_size
)
44 efi_memory_desc_t
*m
= NULL
;
49 *map_size
= sizeof(*m
) * 32;
52 * Add an additional efi_memory_desc_t because we're doing an
53 * allocation which may be in a new descriptor region.
55 *map_size
+= sizeof(*m
);
56 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
57 EFI_LOADER_DATA
, *map_size
, (void **)&m
);
58 if (status
!= EFI_SUCCESS
)
61 status
= efi_call_phys5(sys_table
->boottime
->get_memory_map
, map_size
,
62 m
, &key
, desc_size
, &desc_version
);
63 if (status
== EFI_BUFFER_TOO_SMALL
) {
64 efi_call_phys1(sys_table
->boottime
->free_pool
, m
);
68 if (status
!= EFI_SUCCESS
)
69 efi_call_phys1(sys_table
->boottime
->free_pool
, m
);
77 * Allocate at the highest possible address that is not above 'max'.
79 static efi_status_t
high_alloc(unsigned long size
, unsigned long align
,
80 unsigned long *addr
, unsigned long max
)
82 unsigned long map_size
, desc_size
;
83 efi_memory_desc_t
*map
;
85 unsigned long nr_pages
;
89 status
= __get_map(&map
, &map_size
, &desc_size
);
90 if (status
!= EFI_SUCCESS
)
93 nr_pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
95 for (i
= 0; i
< map_size
/ desc_size
; i
++) {
96 efi_memory_desc_t
*desc
;
97 unsigned long m
= (unsigned long)map
;
100 desc
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
101 if (desc
->type
!= EFI_CONVENTIONAL_MEMORY
)
104 if (desc
->num_pages
< nr_pages
)
107 start
= desc
->phys_addr
;
108 end
= start
+ desc
->num_pages
* (1UL << EFI_PAGE_SHIFT
);
110 if ((start
+ size
) > end
|| (start
+ size
) > max
)
113 if (end
- size
> max
)
116 if (round_down(end
- size
, align
) < start
)
119 start
= round_down(end
- size
, align
);
122 * Don't allocate at 0x0. It will confuse code that
123 * checks pointers against NULL.
128 if (start
> max_addr
)
133 status
= EFI_NOT_FOUND
;
135 status
= efi_call_phys4(sys_table
->boottime
->allocate_pages
,
136 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
137 nr_pages
, &max_addr
);
138 if (status
!= EFI_SUCCESS
) {
148 efi_call_phys1(sys_table
->boottime
->free_pool
, map
);
155 * Allocate at the lowest possible address.
157 static efi_status_t
low_alloc(unsigned long size
, unsigned long align
,
160 unsigned long map_size
, desc_size
;
161 efi_memory_desc_t
*map
;
163 unsigned long nr_pages
;
166 status
= __get_map(&map
, &map_size
, &desc_size
);
167 if (status
!= EFI_SUCCESS
)
170 nr_pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
171 for (i
= 0; i
< map_size
/ desc_size
; i
++) {
172 efi_memory_desc_t
*desc
;
173 unsigned long m
= (unsigned long)map
;
176 desc
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
178 if (desc
->type
!= EFI_CONVENTIONAL_MEMORY
)
181 if (desc
->num_pages
< nr_pages
)
184 start
= desc
->phys_addr
;
185 end
= start
+ desc
->num_pages
* (1UL << EFI_PAGE_SHIFT
);
188 * Don't allocate at 0x0. It will confuse code that
189 * checks pointers against NULL. Skip the first 8
190 * bytes so we start at a nice even number.
195 start
= round_up(start
, align
);
196 if ((start
+ size
) > end
)
199 status
= efi_call_phys4(sys_table
->boottime
->allocate_pages
,
200 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
202 if (status
== EFI_SUCCESS
) {
208 if (i
== map_size
/ desc_size
)
209 status
= EFI_NOT_FOUND
;
212 efi_call_phys1(sys_table
->boottime
->free_pool
, map
);
217 static void low_free(unsigned long size
, unsigned long addr
)
219 unsigned long nr_pages
;
221 nr_pages
= round_up(size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
222 efi_call_phys2(sys_table
->boottime
->free_pages
, addr
, size
);
225 static void find_bits(unsigned long mask
, u8
*pos
, u8
*size
)
233 while (!(mask
& 0x1)) {
249 * See if we have Graphics Output Protocol
251 static efi_status_t
setup_gop(struct screen_info
*si
, efi_guid_t
*proto
,
254 struct efi_graphics_output_protocol
*gop
, *first_gop
;
255 struct efi_pixel_bitmask pixel_info
;
256 unsigned long nr_gops
;
260 u32 fb_base
, fb_size
;
261 u32 pixels_per_scan_line
;
265 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
266 EFI_LOADER_DATA
, size
, &gop_handle
);
267 if (status
!= EFI_SUCCESS
)
270 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
271 EFI_LOCATE_BY_PROTOCOL
, proto
,
272 NULL
, &size
, gop_handle
);
273 if (status
!= EFI_SUCCESS
)
278 nr_gops
= size
/ sizeof(void *);
279 for (i
= 0; i
< nr_gops
; i
++) {
280 struct efi_graphics_output_mode_info
*info
;
281 efi_guid_t conout_proto
= EFI_CONSOLE_OUT_DEVICE_GUID
;
282 bool conout_found
= false;
284 void *h
= gop_handle
[i
];
286 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
288 if (status
!= EFI_SUCCESS
)
291 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
292 h
, &conout_proto
, &dummy
);
294 if (status
== EFI_SUCCESS
)
297 status
= efi_call_phys4(gop
->query_mode
, gop
,
298 gop
->mode
->mode
, &size
, &info
);
299 if (status
== EFI_SUCCESS
&& (!first_gop
|| conout_found
)) {
301 * Systems that use the UEFI Console Splitter may
302 * provide multiple GOP devices, not all of which are
303 * backed by real hardware. The workaround is to search
304 * for a GOP implementing the ConOut protocol, and if
305 * one isn't found, to just fall back to the first GOP.
307 width
= info
->horizontal_resolution
;
308 height
= info
->vertical_resolution
;
309 fb_base
= gop
->mode
->frame_buffer_base
;
310 fb_size
= gop
->mode
->frame_buffer_size
;
311 pixel_format
= info
->pixel_format
;
312 pixel_info
= info
->pixel_information
;
313 pixels_per_scan_line
= info
->pixels_per_scan_line
;
316 * Once we've found a GOP supporting ConOut,
317 * don't bother looking any further.
326 /* Did we find any GOPs? */
330 /* EFI framebuffer */
331 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
333 si
->lfb_width
= width
;
334 si
->lfb_height
= height
;
335 si
->lfb_base
= fb_base
;
338 if (pixel_format
== PIXEL_RGB_RESERVED_8BIT_PER_COLOR
) {
340 si
->lfb_linelength
= pixels_per_scan_line
* 4;
349 } else if (pixel_format
== PIXEL_BGR_RESERVED_8BIT_PER_COLOR
) {
351 si
->lfb_linelength
= pixels_per_scan_line
* 4;
360 } else if (pixel_format
== PIXEL_BIT_MASK
) {
361 find_bits(pixel_info
.red_mask
, &si
->red_pos
, &si
->red_size
);
362 find_bits(pixel_info
.green_mask
, &si
->green_pos
,
364 find_bits(pixel_info
.blue_mask
, &si
->blue_pos
, &si
->blue_size
);
365 find_bits(pixel_info
.reserved_mask
, &si
->rsvd_pos
,
367 si
->lfb_depth
= si
->red_size
+ si
->green_size
+
368 si
->blue_size
+ si
->rsvd_size
;
369 si
->lfb_linelength
= (pixels_per_scan_line
* si
->lfb_depth
) / 8;
372 si
->lfb_linelength
= si
->lfb_width
/ 2;
383 si
->lfb_size
= si
->lfb_linelength
* si
->lfb_height
;
385 si
->capabilities
|= VIDEO_CAPABILITY_SKIP_QUIRKS
;
388 efi_call_phys1(sys_table
->boottime
->free_pool
, gop_handle
);
393 * See if we have Universal Graphics Adapter (UGA) protocol
395 static efi_status_t
setup_uga(struct screen_info
*si
, efi_guid_t
*uga_proto
,
398 struct efi_uga_draw_protocol
*uga
, *first_uga
;
399 unsigned long nr_ugas
;
402 void **uga_handle
= NULL
;
405 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
406 EFI_LOADER_DATA
, size
, &uga_handle
);
407 if (status
!= EFI_SUCCESS
)
410 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
411 EFI_LOCATE_BY_PROTOCOL
, uga_proto
,
412 NULL
, &size
, uga_handle
);
413 if (status
!= EFI_SUCCESS
)
418 nr_ugas
= size
/ sizeof(void *);
419 for (i
= 0; i
< nr_ugas
; i
++) {
420 efi_guid_t pciio_proto
= EFI_PCI_IO_PROTOCOL_GUID
;
421 void *handle
= uga_handle
[i
];
422 u32 w
, h
, depth
, refresh
;
425 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
426 handle
, uga_proto
, &uga
);
427 if (status
!= EFI_SUCCESS
)
430 efi_call_phys3(sys_table
->boottime
->handle_protocol
,
431 handle
, &pciio_proto
, &pciio
);
433 status
= efi_call_phys5(uga
->get_mode
, uga
, &w
, &h
,
435 if (status
== EFI_SUCCESS
&& (!first_uga
|| pciio
)) {
440 * Once we've found a UGA supporting PCIIO,
441 * don't bother looking any further.
453 /* EFI framebuffer */
454 si
->orig_video_isVGA
= VIDEO_TYPE_EFI
;
457 si
->lfb_width
= width
;
458 si
->lfb_height
= height
;
471 efi_call_phys1(sys_table
->boottime
->free_pool
, uga_handle
);
475 void setup_graphics(struct boot_params
*boot_params
)
477 efi_guid_t graphics_proto
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
478 struct screen_info
*si
;
479 efi_guid_t uga_proto
= EFI_UGA_PROTOCOL_GUID
;
482 void **gop_handle
= NULL
;
483 void **uga_handle
= NULL
;
485 si
= &boot_params
->screen_info
;
486 memset(si
, 0, sizeof(*si
));
489 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
490 EFI_LOCATE_BY_PROTOCOL
, &graphics_proto
,
491 NULL
, &size
, gop_handle
);
492 if (status
== EFI_BUFFER_TOO_SMALL
)
493 status
= setup_gop(si
, &graphics_proto
, size
);
495 if (status
!= EFI_SUCCESS
) {
497 status
= efi_call_phys5(sys_table
->boottime
->locate_handle
,
498 EFI_LOCATE_BY_PROTOCOL
, &uga_proto
,
499 NULL
, &size
, uga_handle
);
500 if (status
== EFI_BUFFER_TOO_SMALL
)
501 setup_uga(si
, &uga_proto
, size
);
506 efi_file_handle_t
*handle
;
511 * Check the cmdline for a LILO-style initrd= arguments.
513 * We only support loading an initrd from the same filesystem as the
516 static efi_status_t
handle_ramdisks(efi_loaded_image_t
*image
,
517 struct setup_header
*hdr
)
519 struct initrd
*initrds
;
520 unsigned long initrd_addr
;
521 efi_guid_t fs_proto
= EFI_FILE_SYSTEM_GUID
;
523 efi_file_io_interface_t
*io
;
524 efi_file_handle_t
*fh
;
533 str
= (char *)(unsigned long)hdr
->cmd_line_ptr
;
535 j
= 0; /* See close_handles */
540 for (nr_initrds
= 0; *str
; nr_initrds
++) {
541 str
= strstr(str
, "initrd=");
547 /* Skip any leading slashes */
548 while (*str
== '/' || *str
== '\\')
551 while (*str
&& *str
!= ' ' && *str
!= '\n')
558 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
560 nr_initrds
* sizeof(*initrds
),
562 if (status
!= EFI_SUCCESS
) {
563 efi_printk("Failed to alloc mem for initrds\n");
567 str
= (char *)(unsigned long)hdr
->cmd_line_ptr
;
568 for (i
= 0; i
< nr_initrds
; i
++) {
569 struct initrd
*initrd
;
570 efi_file_handle_t
*h
;
571 efi_file_info_t
*info
;
572 efi_char16_t filename_16
[256];
573 unsigned long info_sz
;
574 efi_guid_t info_guid
= EFI_FILE_INFO_ID
;
578 str
= strstr(str
, "initrd=");
584 initrd
= &initrds
[i
];
587 /* Skip any leading slashes */
588 while (*str
== '/' || *str
== '\\')
591 while (*str
&& *str
!= ' ' && *str
!= '\n') {
592 if ((u8
*)p
>= (u8
*)filename_16
+ sizeof(filename_16
))
600 /* Only open the volume once. */
602 efi_boot_services_t
*boottime
;
604 boottime
= sys_table
->boottime
;
606 status
= efi_call_phys3(boottime
->handle_protocol
,
607 image
->device_handle
, &fs_proto
, &io
);
608 if (status
!= EFI_SUCCESS
) {
609 efi_printk("Failed to handle fs_proto\n");
613 status
= efi_call_phys2(io
->open_volume
, io
, &fh
);
614 if (status
!= EFI_SUCCESS
) {
615 efi_printk("Failed to open volume\n");
620 status
= efi_call_phys5(fh
->open
, fh
, &h
, filename_16
,
621 EFI_FILE_MODE_READ
, (u64
)0);
622 if (status
!= EFI_SUCCESS
) {
623 efi_printk("Failed to open initrd file\n");
630 status
= efi_call_phys4(h
->get_info
, h
, &info_guid
,
632 if (status
!= EFI_BUFFER_TOO_SMALL
) {
633 efi_printk("Failed to get initrd info size\n");
638 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
639 EFI_LOADER_DATA
, info_sz
, &info
);
640 if (status
!= EFI_SUCCESS
) {
641 efi_printk("Failed to alloc mem for initrd info\n");
645 status
= efi_call_phys4(h
->get_info
, h
, &info_guid
,
647 if (status
== EFI_BUFFER_TOO_SMALL
) {
648 efi_call_phys1(sys_table
->boottime
->free_pool
, info
);
652 file_sz
= info
->file_size
;
653 efi_call_phys1(sys_table
->boottime
->free_pool
, info
);
655 if (status
!= EFI_SUCCESS
) {
656 efi_printk("Failed to get initrd info\n");
660 initrd
->size
= file_sz
;
661 initrd_total
+= file_sz
;
668 * Multiple initrd's need to be at consecutive
669 * addresses in memory, so allocate enough memory for
672 status
= high_alloc(initrd_total
, 0x1000,
673 &initrd_addr
, hdr
->initrd_addr_max
);
674 if (status
!= EFI_SUCCESS
) {
675 efi_printk("Failed to alloc highmem for initrds\n");
679 /* We've run out of free low memory. */
680 if (initrd_addr
> hdr
->initrd_addr_max
) {
681 efi_printk("We've run out of free low memory\n");
682 status
= EFI_INVALID_PARAMETER
;
683 goto free_initrd_total
;
687 for (j
= 0; j
< nr_initrds
; j
++) {
690 size
= initrds
[j
].size
;
693 if (size
> EFI_READ_CHUNK_SIZE
)
694 chunksize
= EFI_READ_CHUNK_SIZE
;
697 status
= efi_call_phys3(fh
->read
,
700 if (status
!= EFI_SUCCESS
) {
701 efi_printk("Failed to read initrd\n");
702 goto free_initrd_total
;
708 efi_call_phys1(fh
->close
, initrds
[j
].handle
);
713 efi_call_phys1(sys_table
->boottime
->free_pool
, initrds
);
715 hdr
->ramdisk_image
= initrd_addr
;
716 hdr
->ramdisk_size
= initrd_total
;
721 low_free(initrd_total
, initrd_addr
);
724 for (k
= j
; k
< i
; k
++)
725 efi_call_phys1(fh
->close
, initrds
[k
].handle
);
727 efi_call_phys1(sys_table
->boottime
->free_pool
, initrds
);
729 hdr
->ramdisk_image
= 0;
730 hdr
->ramdisk_size
= 0;
736 * Because the x86 boot code expects to be passed a boot_params we
737 * need to create one ourselves (usually the bootloader would create
740 struct boot_params
*make_boot_params(void *handle
, efi_system_table_t
*_table
)
742 struct boot_params
*boot_params
;
743 struct sys_desc_table
*sdt
;
744 struct apm_bios_info
*bi
;
745 struct setup_header
*hdr
;
746 struct efi_info
*efi
;
747 efi_loaded_image_t
*image
;
749 u32 load_options_size
;
750 efi_guid_t proto
= LOADED_IMAGE_PROTOCOL_GUID
;
751 int options_size
= 0;
753 unsigned long cmdline
;
760 /* Check if we were booted by the EFI firmware */
761 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
764 status
= efi_call_phys3(sys_table
->boottime
->handle_protocol
,
765 handle
, &proto
, (void *)&image
);
766 if (status
!= EFI_SUCCESS
) {
767 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
771 status
= low_alloc(0x4000, 1, (unsigned long *)&boot_params
);
772 if (status
!= EFI_SUCCESS
) {
773 efi_printk("Failed to alloc lowmem for boot params\n");
777 memset(boot_params
, 0x0, 0x4000);
779 hdr
= &boot_params
->hdr
;
780 efi
= &boot_params
->efi_info
;
781 bi
= &boot_params
->apm_bios_info
;
782 sdt
= &boot_params
->sys_desc_table
;
784 /* Copy the second sector to boot_params */
785 memcpy(&hdr
->jump
, image
->image_base
+ 512, 512);
788 * Fill out some of the header fields ourselves because the
789 * EFI firmware loader doesn't load the first sector.
792 hdr
->vid_mode
= 0xffff;
793 hdr
->boot_flag
= 0xAA55;
795 hdr
->code32_start
= (__u64
)(unsigned long)image
->image_base
;
797 hdr
->type_of_loader
= 0x21;
799 /* Convert unicode cmdline to ascii */
800 options
= image
->load_options
;
801 load_options_size
= image
->load_options_size
/ 2; /* ASCII */
806 while (*s2
&& *s2
!= '\n' && options_size
< load_options_size
) {
812 if (options_size
> hdr
->cmdline_size
)
813 options_size
= hdr
->cmdline_size
;
815 options_size
++; /* NUL termination */
817 status
= low_alloc(options_size
, 1, &cmdline
);
818 if (status
!= EFI_SUCCESS
) {
819 efi_printk("Failed to alloc mem for cmdline\n");
823 s1
= (u8
*)(unsigned long)cmdline
;
826 for (i
= 0; i
< options_size
- 1; i
++)
833 hdr
->cmd_line_ptr
= cmdline
;
835 hdr
->ramdisk_image
= 0;
836 hdr
->ramdisk_size
= 0;
838 /* Clear APM BIOS info */
839 memset(bi
, 0, sizeof(*bi
));
841 memset(sdt
, 0, sizeof(*sdt
));
843 status
= handle_ramdisks(image
, hdr
);
844 if (status
!= EFI_SUCCESS
)
850 low_free(options_size
, hdr
->cmd_line_ptr
);
852 low_free(0x4000, (unsigned long)boot_params
);
856 static efi_status_t
exit_boot(struct boot_params
*boot_params
,
859 struct efi_info
*efi
= &boot_params
->efi_info
;
860 struct e820entry
*e820_map
= &boot_params
->e820_map
[0];
861 struct e820entry
*prev
= NULL
;
862 unsigned long size
, key
, desc_size
, _size
;
863 efi_memory_desc_t
*mem_map
;
869 size
= sizeof(*mem_map
) * 32;
872 size
+= sizeof(*mem_map
);
874 status
= low_alloc(size
, 1, (unsigned long *)&mem_map
);
875 if (status
!= EFI_SUCCESS
)
878 status
= efi_call_phys5(sys_table
->boottime
->get_memory_map
, &size
,
879 mem_map
, &key
, &desc_size
, &desc_version
);
880 if (status
== EFI_BUFFER_TOO_SMALL
) {
881 low_free(_size
, (unsigned long)mem_map
);
885 if (status
!= EFI_SUCCESS
)
888 memcpy(&efi
->efi_loader_signature
, EFI_LOADER_SIGNATURE
, sizeof(__u32
));
889 efi
->efi_systab
= (unsigned long)sys_table
;
890 efi
->efi_memdesc_size
= desc_size
;
891 efi
->efi_memdesc_version
= desc_version
;
892 efi
->efi_memmap
= (unsigned long)mem_map
;
893 efi
->efi_memmap_size
= size
;
896 efi
->efi_systab_hi
= (unsigned long)sys_table
>> 32;
897 efi
->efi_memmap_hi
= (unsigned long)mem_map
>> 32;
900 /* Might as well exit boot services now */
901 status
= efi_call_phys2(sys_table
->boottime
->exit_boot_services
,
903 if (status
!= EFI_SUCCESS
)
907 boot_params
->alt_mem_k
= 32 * 1024;
910 * Convert the EFI memory map to E820.
913 for (i
= 0; i
< size
/ desc_size
; i
++) {
914 efi_memory_desc_t
*d
;
915 unsigned int e820_type
= 0;
916 unsigned long m
= (unsigned long)mem_map
;
918 d
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
920 case EFI_RESERVED_TYPE
:
921 case EFI_RUNTIME_SERVICES_CODE
:
922 case EFI_RUNTIME_SERVICES_DATA
:
923 case EFI_MEMORY_MAPPED_IO
:
924 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
926 e820_type
= E820_RESERVED
;
929 case EFI_UNUSABLE_MEMORY
:
930 e820_type
= E820_UNUSABLE
;
933 case EFI_ACPI_RECLAIM_MEMORY
:
934 e820_type
= E820_ACPI
;
937 case EFI_LOADER_CODE
:
938 case EFI_LOADER_DATA
:
939 case EFI_BOOT_SERVICES_CODE
:
940 case EFI_BOOT_SERVICES_DATA
:
941 case EFI_CONVENTIONAL_MEMORY
:
942 e820_type
= E820_RAM
;
945 case EFI_ACPI_MEMORY_NVS
:
946 e820_type
= E820_NVS
;
953 /* Merge adjacent mappings */
954 if (prev
&& prev
->type
== e820_type
&&
955 (prev
->addr
+ prev
->size
) == d
->phys_addr
)
956 prev
->size
+= d
->num_pages
<< 12;
958 e820_map
->addr
= d
->phys_addr
;
959 e820_map
->size
= d
->num_pages
<< 12;
960 e820_map
->type
= e820_type
;
966 boot_params
->e820_entries
= nr_entries
;
971 low_free(_size
, (unsigned long)mem_map
);
975 static efi_status_t
relocate_kernel(struct setup_header
*hdr
)
977 unsigned long start
, nr_pages
;
981 * The EFI firmware loader could have placed the kernel image
982 * anywhere in memory, but the kernel has various restrictions
983 * on the max physical address it can run at. Attempt to move
984 * the kernel to boot_params.pref_address, or as low as
987 start
= hdr
->pref_address
;
988 nr_pages
= round_up(hdr
->init_size
, EFI_PAGE_SIZE
) / EFI_PAGE_SIZE
;
990 status
= efi_call_phys4(sys_table
->boottime
->allocate_pages
,
991 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
993 if (status
!= EFI_SUCCESS
) {
994 status
= low_alloc(hdr
->init_size
, hdr
->kernel_alignment
,
996 if (status
!= EFI_SUCCESS
)
997 efi_printk("Failed to alloc mem for kernel\n");
1000 if (status
== EFI_SUCCESS
)
1001 memcpy((void *)start
, (void *)(unsigned long)hdr
->code32_start
,
1004 hdr
->pref_address
= hdr
->code32_start
;
1005 hdr
->code32_start
= (__u32
)start
;
1011 * On success we return a pointer to a boot_params structure, and NULL
1014 struct boot_params
*efi_main(void *handle
, efi_system_table_t
*_table
,
1015 struct boot_params
*boot_params
)
1017 struct desc_ptr
*gdt
, *idt
;
1018 efi_loaded_image_t
*image
;
1019 struct setup_header
*hdr
= &boot_params
->hdr
;
1020 efi_status_t status
;
1021 struct desc_struct
*desc
;
1025 /* Check if we were booted by the EFI firmware */
1026 if (sys_table
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
1029 setup_graphics(boot_params
);
1031 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
1032 EFI_LOADER_DATA
, sizeof(*gdt
),
1034 if (status
!= EFI_SUCCESS
) {
1035 efi_printk("Failed to alloc mem for gdt structure\n");
1040 status
= low_alloc(gdt
->size
, 8, (unsigned long *)&gdt
->address
);
1041 if (status
!= EFI_SUCCESS
) {
1042 efi_printk("Failed to alloc mem for gdt\n");
1046 status
= efi_call_phys3(sys_table
->boottime
->allocate_pool
,
1047 EFI_LOADER_DATA
, sizeof(*idt
),
1049 if (status
!= EFI_SUCCESS
) {
1050 efi_printk("Failed to alloc mem for idt structure\n");
1058 * If the kernel isn't already loaded at the preferred load
1059 * address, relocate it.
1061 if (hdr
->pref_address
!= hdr
->code32_start
) {
1062 status
= relocate_kernel(hdr
);
1064 if (status
!= EFI_SUCCESS
)
1068 status
= exit_boot(boot_params
, handle
);
1069 if (status
!= EFI_SUCCESS
)
1072 memset((char *)gdt
->address
, 0x0, gdt
->size
);
1073 desc
= (struct desc_struct
*)gdt
->address
;
1075 /* The first GDT is a dummy and the second is unused. */
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
;
1088 desc
->d
= SEG_OP_SIZE_32BIT
;
1089 desc
->g
= SEG_GRANULARITY_4KB
;
1093 desc
->limit0
= 0xffff;
1094 desc
->base0
= 0x0000;
1095 desc
->base1
= 0x0000;
1096 desc
->type
= SEG_TYPE_DATA
| SEG_TYPE_READ_WRITE
;
1097 desc
->s
= DESC_TYPE_CODE_DATA
;
1103 desc
->d
= SEG_OP_SIZE_32BIT
;
1104 desc
->g
= SEG_GRANULARITY_4KB
;
1107 #ifdef CONFIG_X86_64
1108 /* Task segment value */
1110 desc
->limit0
= 0x0000;
1111 desc
->base0
= 0x0000;
1112 desc
->base1
= 0x0000;
1113 desc
->type
= SEG_TYPE_TSS
;
1121 desc
->g
= SEG_GRANULARITY_4KB
;
1123 #endif /* CONFIG_X86_64 */
1125 asm volatile ("lidt %0" : : "m" (*idt
));
1126 asm volatile ("lgdt %0" : : "m" (*gdt
));
1128 asm volatile("cli");