Commit 54b52d87 authored by Matt Fleming's avatar Matt Fleming

x86/efi: Build our own EFI services pointer table

It's not possible to dereference the EFI System table directly when
booting a 64-bit kernel on a 32-bit EFI firmware because the size of
pointers don't match.

In preparation for supporting the above use case, build a list of
function pointers on boot so that callers don't have to worry about
converting pointer sizes through multiple levels of indirection.
Signed-off-by: default avatarMatt Fleming <matt.fleming@intel.com>
parent 677703ce
This diff is collapsed.
...@@ -103,4 +103,20 @@ struct efi_uga_draw_protocol { ...@@ -103,4 +103,20 @@ struct efi_uga_draw_protocol {
void *blt; void *blt;
}; };
struct efi_config {
u64 image_handle;
u64 table;
u64 allocate_pool;
u64 allocate_pages;
u64 get_memory_map;
u64 free_pool;
u64 free_pages;
u64 locate_handle;
u64 handle_protocol;
u64 exit_boot_services;
u64 text_output;
efi_status_t (*call)(unsigned long, ...);
bool is64;
} __packed;
#endif /* BOOT_COMPRESSED_EBOOT_H */ #endif /* BOOT_COMPRESSED_EBOOT_H */
...@@ -42,26 +42,53 @@ ENTRY(startup_32) ...@@ -42,26 +42,53 @@ ENTRY(startup_32)
ENTRY(efi_pe_entry) ENTRY(efi_pe_entry)
add $0x4, %esp add $0x4, %esp
call 1f
1: popl %esi
subl $1b, %esi
popl %ecx
movl %ecx, efi32_config(%esi) /* Handle */
popl %ecx
movl %ecx, efi32_config+8(%esi) /* EFI System table pointer */
/* Relocate efi_config->call() */
leal efi32_config(%esi), %eax
add %esi, 88(%eax)
pushl %eax
call make_boot_params call make_boot_params
cmpl $0, %eax cmpl $0, %eax
je 1f je fail
movl 0x4(%esp), %esi popl %ecx
movl (%esp), %ecx
pushl %eax pushl %eax
pushl %esi
pushl %ecx pushl %ecx
sub $0x4, %esp jmp 2f /* Skip efi_config initialization */
ENTRY(efi_stub_entry) ENTRY(efi_stub_entry)
add $0x4, %esp add $0x4, %esp
popl %ecx
popl %edx
call 1f
1: popl %esi
subl $1b, %esi
movl %ecx, efi32_config(%esi) /* Handle */
movl %edx, efi32_config+8(%esi) /* EFI System table pointer */
/* Relocate efi_config->call() */
leal efi32_config(%esi), %eax
add %esi, 88(%eax)
pushl %eax
2:
call efi_main call efi_main
cmpl $0, %eax cmpl $0, %eax
movl %eax, %esi movl %eax, %esi
jne 2f jne 2f
1: fail:
/* EFI init failed, so hang. */ /* EFI init failed, so hang. */
hlt hlt
jmp 1b jmp fail
2: 2:
call 3f call 3f
3: 3:
...@@ -202,6 +229,13 @@ relocated: ...@@ -202,6 +229,13 @@ relocated:
xorl %ebx, %ebx xorl %ebx, %ebx
jmp *%eax jmp *%eax
.data
efi32_config:
.fill 11,8,0
.long efi_call_phys
.long 0
.byte 0
/* /*
* Stack and heap for uncompression * Stack and heap for uncompression
*/ */
......
...@@ -209,26 +209,55 @@ ENTRY(startup_64) ...@@ -209,26 +209,55 @@ ENTRY(startup_64)
jmp preferred_addr jmp preferred_addr
ENTRY(efi_pe_entry) ENTRY(efi_pe_entry)
mov %rcx, %rdi movq %rcx, efi64_config(%rip) /* Handle */
mov %rdx, %rsi movq %rdx, efi64_config+8(%rip) /* EFI System table pointer */
pushq %rdi
pushq %rsi leaq efi64_config(%rip), %rax
movq %rax, efi_config(%rip)
call 1f
1: popq %rbp
subq $1b, %rbp
/*
* Relocate efi_config->call().
*/
addq %rbp, efi64_config+88(%rip)
movq %rax, %rdi
call make_boot_params call make_boot_params
cmpq $0,%rax cmpq $0,%rax
je 1f je fail
mov %rax, %rdx mov %rax, %rsi
popq %rsi jmp 2f /* Skip the relocation */
popq %rdi
ENTRY(efi_stub_entry) ENTRY(efi_stub_entry)
movq %rdi, efi64_config(%rip) /* Handle */
movq %rsi, efi64_config+8(%rip) /* EFI System table pointer */
leaq efi64_config(%rip), %rax
movq %rax, efi_config(%rip)
call 1f
1: popq %rbp
subq $1b, %rbp
/*
* Relocate efi_config->call().
*/
movq efi_config(%rip), %rax
addq %rbp, 88(%rax)
movq %rdx, %rsi
2:
movq efi_config(%rip), %rdi
call efi_main call efi_main
movq %rax,%rsi movq %rax,%rsi
cmpq $0,%rax cmpq $0,%rax
jne 2f jne 2f
1: fail:
/* EFI init failed, so hang. */ /* EFI init failed, so hang. */
hlt hlt
jmp 1b jmp fail
2: 2:
call 3f call 3f
3: 3:
...@@ -372,6 +401,14 @@ gdt: ...@@ -372,6 +401,14 @@ gdt:
.quad 0x0000000000000000 /* TS continued */ .quad 0x0000000000000000 /* TS continued */
gdt_end: gdt_end:
efi_config:
.quad 0
.global efi64_config
efi64_config:
.fill 11,8,0
.quad efi_call6
.byte 1
/* /*
* Stack and heap for uncompression * Stack and heap for uncompression
*/ */
......
...@@ -16,18 +16,6 @@ struct file_info { ...@@ -16,18 +16,6 @@ struct file_info {
u64 size; u64 size;
}; };
static void efi_char16_printk(efi_system_table_t *sys_table_arg,
efi_char16_t *str)
{
struct efi_simple_text_output_protocol *out;
out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
efi_call_phys2(out->output_string, out, str);
}
static void efi_printk(efi_system_table_t *sys_table_arg, char *str) static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
{ {
char *s8; char *s8;
...@@ -65,20 +53,23 @@ static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg, ...@@ -65,20 +53,23 @@ static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
* allocation which may be in a new descriptor region. * allocation which may be in a new descriptor region.
*/ */
*map_size += sizeof(*m); *map_size += sizeof(*m);
status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
EFI_LOADER_DATA, *map_size, (void **)&m); *map_size, (void **)&m);
if (status != EFI_SUCCESS) if (status != EFI_SUCCESS)
goto fail; goto fail;
status = efi_call_phys5(sys_table_arg->boottime->get_memory_map, *desc_size = 0;
map_size, m, &key, desc_size, &desc_version); key = 0;
status = efi_early->call(efi_early->get_memory_map, map_size, m,
&key, desc_size, &desc_version);
if (status == EFI_BUFFER_TOO_SMALL) { if (status == EFI_BUFFER_TOO_SMALL) {
efi_call_phys1(sys_table_arg->boottime->free_pool, m); efi_early->call(efi_early->free_pool, m);
goto again; goto again;
} }
if (status != EFI_SUCCESS) if (status != EFI_SUCCESS)
efi_call_phys1(sys_table_arg->boottime->free_pool, m); efi_early->call(efi_early->free_pool, m);
if (key_ptr && status == EFI_SUCCESS) if (key_ptr && status == EFI_SUCCESS)
*key_ptr = key; *key_ptr = key;
if (desc_ver && status == EFI_SUCCESS) if (desc_ver && status == EFI_SUCCESS)
...@@ -158,7 +149,7 @@ static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg, ...@@ -158,7 +149,7 @@ static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
if (!max_addr) if (!max_addr)
status = EFI_NOT_FOUND; status = EFI_NOT_FOUND;
else { else {
status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, status = efi_early->call(efi_early->allocate_pages,
EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
nr_pages, &max_addr); nr_pages, &max_addr);
if (status != EFI_SUCCESS) { if (status != EFI_SUCCESS) {
...@@ -170,8 +161,7 @@ static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg, ...@@ -170,8 +161,7 @@ static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
*addr = max_addr; *addr = max_addr;
} }
efi_call_phys1(sys_table_arg->boottime->free_pool, map); efi_early->call(efi_early->free_pool, map);
fail: fail:
return status; return status;
} }
...@@ -231,7 +221,7 @@ static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg, ...@@ -231,7 +221,7 @@ static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
if ((start + size) > end) if ((start + size) > end)
continue; continue;
status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, status = efi_early->call(efi_early->allocate_pages,
EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
nr_pages, &start); nr_pages, &start);
if (status == EFI_SUCCESS) { if (status == EFI_SUCCESS) {
...@@ -243,7 +233,7 @@ static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg, ...@@ -243,7 +233,7 @@ static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
if (i == map_size / desc_size) if (i == map_size / desc_size)
status = EFI_NOT_FOUND; status = EFI_NOT_FOUND;
efi_call_phys1(sys_table_arg->boottime->free_pool, map); efi_early->call(efi_early->free_pool, map);
fail: fail:
return status; return status;
} }
...@@ -257,7 +247,7 @@ static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size, ...@@ -257,7 +247,7 @@ static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
return; return;
nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages); efi_early->call(efi_early->free_pages, addr, nr_pages);
} }
...@@ -276,9 +266,7 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -276,9 +266,7 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
{ {
struct file_info *files; struct file_info *files;
unsigned long file_addr; unsigned long file_addr;
efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
u64 file_size_total; u64 file_size_total;
efi_file_io_interface_t *io;
efi_file_handle_t *fh; efi_file_handle_t *fh;
efi_status_t status; efi_status_t status;
int nr_files; int nr_files;
...@@ -319,10 +307,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -319,10 +307,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
if (!nr_files) if (!nr_files)
return EFI_SUCCESS; return EFI_SUCCESS;
status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, status = efi_early->call(efi_early->allocate_pool, EFI_LOADER_DATA,
EFI_LOADER_DATA, nr_files * sizeof(*files), (void **)&files);
nr_files * sizeof(*files),
(void **)&files);
if (status != EFI_SUCCESS) { if (status != EFI_SUCCESS) {
efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n"); efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n");
goto fail; goto fail;
...@@ -331,13 +317,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -331,13 +317,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
str = cmd_line; str = cmd_line;
for (i = 0; i < nr_files; i++) { for (i = 0; i < nr_files; i++) {
struct file_info *file; struct file_info *file;
efi_file_handle_t *h;
efi_file_info_t *info;
efi_char16_t filename_16[256]; efi_char16_t filename_16[256];
unsigned long info_sz;
efi_guid_t info_guid = EFI_FILE_INFO_ID;
efi_char16_t *p; efi_char16_t *p;
u64 file_sz;
str = strstr(str, option_string); str = strstr(str, option_string);
if (!str) if (!str)
...@@ -368,71 +349,18 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -368,71 +349,18 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
/* Only open the volume once. */ /* Only open the volume once. */
if (!i) { if (!i) {
efi_boot_services_t *boottime; status = efi_open_volume(sys_table_arg, image,
(void **)&fh);
boottime = sys_table_arg->boottime; if (status != EFI_SUCCESS)
status = efi_call_phys3(boottime->handle_protocol,
image->device_handle, &fs_proto,
(void **)&io);
if (status != EFI_SUCCESS) {
efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
goto free_files;
}
status = efi_call_phys2(io->open_volume, io, &fh);
if (status != EFI_SUCCESS) {
efi_printk(sys_table_arg, "Failed to open volume\n");
goto free_files; goto free_files;
} }
}
status = efi_call_phys5(fh->open, fh, &h, filename_16,
EFI_FILE_MODE_READ, (u64)0);
if (status != EFI_SUCCESS) {
efi_printk(sys_table_arg, "Failed to open file: ");
efi_char16_printk(sys_table_arg, filename_16);
efi_printk(sys_table_arg, "\n");
goto close_handles;
}
file->handle = h;
info_sz = 0;
status = efi_call_phys4(h->get_info, h, &info_guid,
&info_sz, NULL);
if (status != EFI_BUFFER_TOO_SMALL) {
efi_printk(sys_table_arg, "Failed to get file info size\n");
goto close_handles;
}
grow: status = efi_file_size(sys_table_arg, fh, filename_16,
status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, (void **)&file->handle, &file->size);
EFI_LOADER_DATA, info_sz, if (status != EFI_SUCCESS)
(void **)&info);
if (status != EFI_SUCCESS) {
efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
goto close_handles;
}
status = efi_call_phys4(h->get_info, h, &info_guid,
&info_sz, info);
if (status == EFI_BUFFER_TOO_SMALL) {
efi_call_phys1(sys_table_arg->boottime->free_pool,
info);
goto grow;
}
file_sz = info->file_size;
efi_call_phys1(sys_table_arg->boottime->free_pool, info);
if (status != EFI_SUCCESS) {
efi_printk(sys_table_arg, "Failed to get file info\n");
goto close_handles; goto close_handles;
}
file->size = file_sz; file_size_total += file->size;
file_size_total += file_sz;
} }
if (file_size_total) { if (file_size_total) {
...@@ -468,8 +396,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -468,8 +396,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
chunksize = EFI_READ_CHUNK_SIZE; chunksize = EFI_READ_CHUNK_SIZE;
else else
chunksize = size; chunksize = size;
status = efi_call_phys3(fh->read,
files[j].handle, status = efi_file_read(fh, files[j].handle,
&chunksize, &chunksize,
(void *)addr); (void *)addr);
if (status != EFI_SUCCESS) { if (status != EFI_SUCCESS) {
...@@ -480,12 +408,12 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -480,12 +408,12 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
size -= chunksize; size -= chunksize;
} }
efi_call_phys1(fh->close, files[j].handle); efi_file_close(fh, files[j].handle);
} }
} }
efi_call_phys1(sys_table_arg->boottime->free_pool, files); efi_early->call(efi_early->free_pool, files);
*load_addr = file_addr; *load_addr = file_addr;
*load_size = file_size_total; *load_size = file_size_total;
...@@ -497,9 +425,9 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg, ...@@ -497,9 +425,9 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
close_handles: close_handles:
for (k = j; k < i; k++) for (k = j; k < i; k++)
efi_call_phys1(fh->close, files[k].handle); efi_file_close(fh, files[k].handle);
free_files: free_files:
efi_call_phys1(sys_table_arg->boottime->free_pool, files); efi_early->call(efi_early->free_pool, files);
fail: fail:
*load_addr = 0; *load_addr = 0;
*load_size = 0; *load_size = 0;
...@@ -545,7 +473,7 @@ static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg, ...@@ -545,7 +473,7 @@ static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
* as possible while respecting the required alignment. * as possible while respecting the required alignment.
*/ */
nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, status = efi_early->call(efi_early->allocate_pages,
EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
nr_pages, &efi_addr); nr_pages, &efi_addr);
new_addr = efi_addr; new_addr = efi_addr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment