2019-02-02 17:41:15 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-09-23 06:45:27 +08:00
|
|
|
/*
|
|
|
|
* Helper functions used by the EFI stub on multiple
|
|
|
|
* architectures. This should be #included by the EFI stub
|
|
|
|
* implementation files.
|
|
|
|
*
|
|
|
|
* Copyright 2011 Intel Corporation; author Matt Fleming
|
|
|
|
*/
|
|
|
|
|
2014-07-02 20:54:42 +08:00
|
|
|
#include <linux/efi.h>
|
|
|
|
#include <asm/efi.h>
|
|
|
|
|
|
|
|
#include "efistub.h"
|
2014-01-29 02:41:28 +08:00
|
|
|
|
2014-08-05 18:52:11 +08:00
|
|
|
/*
|
|
|
|
* Some firmware implementations have problems reading files in one go.
|
|
|
|
* A read chunk size of 1MB seems to work for most platforms.
|
|
|
|
*
|
|
|
|
* Unfortunately, reading files in chunks triggers *other* bugs on some
|
|
|
|
* platforms, so we provide a way to disable this workaround, which can
|
|
|
|
* be done by passing "efi=nochunk" on the EFI boot stub command line.
|
|
|
|
*
|
|
|
|
* If you experience issues with initrd images being corrupt it's worth
|
|
|
|
* trying efi=nochunk, but chunking is enabled by default because there
|
|
|
|
* are far more machines that require the workaround than those that
|
|
|
|
* break with it enabled.
|
|
|
|
*/
|
2014-07-02 20:54:42 +08:00
|
|
|
#define EFI_READ_CHUNK_SIZE (1024 * 1024)
|
2014-01-29 02:41:28 +08:00
|
|
|
|
2014-08-05 18:52:11 +08:00
|
|
|
static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
|
|
|
|
|
2017-04-05 00:09:08 +08:00
|
|
|
static int __section(.data) __nokaslr;
|
2017-04-05 00:09:09 +08:00
|
|
|
static int __section(.data) __quiet;
|
efi/arm/arm64: Allow SetVirtualAddressMap() to be omitted
The UEFI spec revision 2.7 errata A section 8.4 has the following to
say about the virtual memory runtime services:
"This section contains function definitions for the virtual memory
support that may be optionally used by an operating system at runtime.
If an operating system chooses to make EFI runtime service calls in a
virtual addressing mode instead of the flat physical mode, then the
operating system must use the services in this section to switch the
EFI runtime services from flat physical addressing to virtual
addressing."
So it is pretty clear that calling SetVirtualAddressMap() is entirely
optional, and so there is no point in doing so unless it achieves
anything useful for us.
This is not the case for 64-bit ARM. The identity mapping used by the
firmware is arbitrarily converted into another permutation of userland
addresses (i.e., bits [63:48] cleared), and the runtime code could easily
deal with the original layout in exactly the same way as it deals with
the converted layout. However, due to constraints related to page size
differences if the OS is not running with 4k pages, and related to
systems that may expose the individual sections of PE/COFF runtime
modules as different memory regions, creating the virtual layout is a
bit fiddly, and requires us to sort the memory map and reason about
adjacent regions with identical memory types etc etc.
So the obvious fix is to stop calling SetVirtualAddressMap() altogether
on arm64 systems. However, to avoid surprises, which are notoriously
hard to diagnose when it comes to OS<->firmware interactions, let's
start by making it an opt-out feature, and implement support for the
'efi=novamap' kernel command line parameter on ARM and arm64 systems.
( Note that 32-bit ARM generally does require SetVirtualAddressMap() to be
used, given that the physical memory map and the kernel virtual address
map are not guaranteed to be non-overlapping like on arm64. However,
having support for efi=novamap,noruntime on 32-bit ARM, combined with
the recently proposed support for earlycon=efifb, is likely to be useful
to diagnose boot issues on such systems if they have no accessible serial
port. )
Tested-by: Jeffrey Hugo <jhugo@codeaurora.org>
Tested-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Tested-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: AKASHI Takahiro <takahiro.akashi@linaro.org>
Cc: Alexander Graf <agraf@suse.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Peter Jones <pjones@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/20190202094119.13230-8-ard.biesheuvel@linaro.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-02-02 17:41:16 +08:00
|
|
|
static int __section(.data) __novamap;
|
2017-04-05 00:09:08 +08:00
|
|
|
|
|
|
|
int __pure nokaslr(void)
|
|
|
|
{
|
|
|
|
return __nokaslr;
|
|
|
|
}
|
2017-04-05 00:09:09 +08:00
|
|
|
int __pure is_quiet(void)
|
|
|
|
{
|
|
|
|
return __quiet;
|
|
|
|
}
|
efi/arm/arm64: Allow SetVirtualAddressMap() to be omitted
The UEFI spec revision 2.7 errata A section 8.4 has the following to
say about the virtual memory runtime services:
"This section contains function definitions for the virtual memory
support that may be optionally used by an operating system at runtime.
If an operating system chooses to make EFI runtime service calls in a
virtual addressing mode instead of the flat physical mode, then the
operating system must use the services in this section to switch the
EFI runtime services from flat physical addressing to virtual
addressing."
So it is pretty clear that calling SetVirtualAddressMap() is entirely
optional, and so there is no point in doing so unless it achieves
anything useful for us.
This is not the case for 64-bit ARM. The identity mapping used by the
firmware is arbitrarily converted into another permutation of userland
addresses (i.e., bits [63:48] cleared), and the runtime code could easily
deal with the original layout in exactly the same way as it deals with
the converted layout. However, due to constraints related to page size
differences if the OS is not running with 4k pages, and related to
systems that may expose the individual sections of PE/COFF runtime
modules as different memory regions, creating the virtual layout is a
bit fiddly, and requires us to sort the memory map and reason about
adjacent regions with identical memory types etc etc.
So the obvious fix is to stop calling SetVirtualAddressMap() altogether
on arm64 systems. However, to avoid surprises, which are notoriously
hard to diagnose when it comes to OS<->firmware interactions, let's
start by making it an opt-out feature, and implement support for the
'efi=novamap' kernel command line parameter on ARM and arm64 systems.
( Note that 32-bit ARM generally does require SetVirtualAddressMap() to be
used, given that the physical memory map and the kernel virtual address
map are not guaranteed to be non-overlapping like on arm64. However,
having support for efi=novamap,noruntime on 32-bit ARM, combined with
the recently proposed support for earlycon=efifb, is likely to be useful
to diagnose boot issues on such systems if they have no accessible serial
port. )
Tested-by: Jeffrey Hugo <jhugo@codeaurora.org>
Tested-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Tested-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: AKASHI Takahiro <takahiro.akashi@linaro.org>
Cc: Alexander Graf <agraf@suse.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Peter Jones <pjones@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/20190202094119.13230-8-ard.biesheuvel@linaro.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-02-02 17:41:16 +08:00
|
|
|
int __pure novamap(void)
|
|
|
|
{
|
|
|
|
return __novamap;
|
|
|
|
}
|
2017-04-05 00:09:08 +08:00
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
#define EFI_MMAP_NR_SLACK_SLOTS 8
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
struct file_info {
|
2013-09-23 06:45:27 +08:00
|
|
|
efi_file_handle_t *handle;
|
|
|
|
u64 size;
|
|
|
|
};
|
|
|
|
|
2014-07-02 20:54:42 +08:00
|
|
|
void efi_printk(efi_system_table_t *sys_table_arg, char *str)
|
2013-09-23 06:45:27 +08:00
|
|
|
{
|
|
|
|
char *s8;
|
|
|
|
|
|
|
|
for (s8 = str; *s8; s8++) {
|
|
|
|
efi_char16_t ch[2] = { 0 };
|
|
|
|
|
|
|
|
ch[0] = *s8;
|
|
|
|
if (*s8 == '\n') {
|
|
|
|
efi_char16_t nl[2] = { '\r', 0 };
|
2013-09-23 06:45:28 +08:00
|
|
|
efi_char16_printk(sys_table_arg, nl);
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:28 +08:00
|
|
|
efi_char16_printk(sys_table_arg, ch);
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
static inline bool mmap_has_headroom(unsigned long buff_size,
|
|
|
|
unsigned long map_size,
|
|
|
|
unsigned long desc_size)
|
|
|
|
{
|
|
|
|
unsigned long slack = buff_size - map_size;
|
|
|
|
|
|
|
|
return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:54:42 +08:00
|
|
|
efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
|
2016-08-30 04:38:51 +08:00
|
|
|
struct efi_boot_memmap *map)
|
2013-09-23 06:45:27 +08:00
|
|
|
{
|
|
|
|
efi_memory_desc_t *m = NULL;
|
|
|
|
efi_status_t status;
|
|
|
|
unsigned long key;
|
|
|
|
u32 desc_version;
|
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
*map->desc_size = sizeof(*m);
|
|
|
|
*map->map_size = *map->desc_size * 32;
|
|
|
|
*map->buff_size = *map->map_size;
|
2015-02-13 23:46:56 +08:00
|
|
|
again:
|
2014-03-22 18:09:01 +08:00
|
|
|
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
|
2016-08-30 04:38:51 +08:00
|
|
|
*map->map_size, (void **)&m);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
*map->desc_size = 0;
|
2015-02-13 23:46:56 +08:00
|
|
|
key = 0;
|
2016-08-30 04:38:51 +08:00
|
|
|
status = efi_call_early(get_memory_map, map->map_size, m,
|
|
|
|
&key, map->desc_size, &desc_version);
|
|
|
|
if (status == EFI_BUFFER_TOO_SMALL ||
|
|
|
|
!mmap_has_headroom(*map->buff_size, *map->map_size,
|
|
|
|
*map->desc_size)) {
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pool, m);
|
2016-08-30 04:38:51 +08:00
|
|
|
/*
|
|
|
|
* Make sure there is some entries of headroom so that the
|
|
|
|
* buffer can be reused for a new map after allocations are
|
|
|
|
* no longer permitted. Its unlikely that the map will grow to
|
|
|
|
* exceed this headroom once we are ready to trigger
|
|
|
|
* ExitBootServices()
|
|
|
|
*/
|
|
|
|
*map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
|
|
|
|
*map->buff_size = *map->map_size;
|
2015-02-13 23:46:56 +08:00
|
|
|
goto again;
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status != EFI_SUCCESS)
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pool, m);
|
2014-01-10 23:27:14 +08:00
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
if (map->key_ptr && status == EFI_SUCCESS)
|
|
|
|
*map->key_ptr = key;
|
|
|
|
if (map->desc_ver && status == EFI_SUCCESS)
|
|
|
|
*map->desc_ver = desc_version;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
fail:
|
2016-08-30 04:38:51 +08:00
|
|
|
*map->map = m;
|
2013-09-23 06:45:27 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-01-29 02:41:28 +08:00
|
|
|
|
2015-01-13 04:28:20 +08:00
|
|
|
unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
|
2014-01-29 02:41:28 +08:00
|
|
|
{
|
|
|
|
efi_status_t status;
|
2016-08-30 04:38:51 +08:00
|
|
|
unsigned long map_size, buff_size;
|
2014-01-29 02:41:28 +08:00
|
|
|
unsigned long membase = EFI_ERROR;
|
|
|
|
struct efi_memory_map map;
|
|
|
|
efi_memory_desc_t *md;
|
2016-08-30 04:38:51 +08:00
|
|
|
struct efi_boot_memmap boot_map;
|
2014-01-29 02:41:28 +08:00
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
boot_map.map = (efi_memory_desc_t **)&map.map;
|
|
|
|
boot_map.map_size = &map_size;
|
|
|
|
boot_map.desc_size = &map.desc_size;
|
|
|
|
boot_map.desc_ver = NULL;
|
|
|
|
boot_map.key_ptr = NULL;
|
|
|
|
boot_map.buff_size = &buff_size;
|
|
|
|
|
|
|
|
status = efi_get_memory_map(sys_table_arg, &boot_map);
|
2014-01-29 02:41:28 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
return membase;
|
|
|
|
|
|
|
|
map.map_end = map.map + map_size;
|
|
|
|
|
2016-04-26 04:06:38 +08:00
|
|
|
for_each_efi_memory_desc_in_map(&map, md) {
|
|
|
|
if (md->attribute & EFI_MEMORY_WB) {
|
2014-01-29 02:41:28 +08:00
|
|
|
if (membase > md->phys_addr)
|
|
|
|
membase = md->phys_addr;
|
2016-04-26 04:06:38 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 02:41:28 +08:00
|
|
|
|
|
|
|
efi_call_early(free_pool, map.map);
|
|
|
|
|
|
|
|
return membase;
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:27 +08:00
|
|
|
/*
|
|
|
|
* Allocate at the highest possible address that is not above 'max'.
|
|
|
|
*/
|
2014-07-02 20:54:42 +08:00
|
|
|
efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
|
|
|
|
unsigned long size, unsigned long align,
|
|
|
|
unsigned long *addr, unsigned long max)
|
2013-09-23 06:45:27 +08:00
|
|
|
{
|
2016-08-30 04:38:51 +08:00
|
|
|
unsigned long map_size, desc_size, buff_size;
|
2013-09-23 06:45:27 +08:00
|
|
|
efi_memory_desc_t *map;
|
|
|
|
efi_status_t status;
|
|
|
|
unsigned long nr_pages;
|
|
|
|
u64 max_addr = 0;
|
|
|
|
int i;
|
2016-08-30 04:38:51 +08:00
|
|
|
struct efi_boot_memmap boot_map;
|
|
|
|
|
|
|
|
boot_map.map = ↦
|
|
|
|
boot_map.map_size = &map_size;
|
|
|
|
boot_map.desc_size = &desc_size;
|
|
|
|
boot_map.desc_ver = NULL;
|
|
|
|
boot_map.key_ptr = NULL;
|
|
|
|
boot_map.buff_size = &buff_size;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
status = efi_get_memory_map(sys_table_arg, &boot_map);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
2013-09-23 06:45:30 +08:00
|
|
|
/*
|
2016-11-13 05:32:29 +08:00
|
|
|
* Enforce minimum alignment that EFI or Linux requires when
|
|
|
|
* requesting a specific address. We are doing page-based (or
|
|
|
|
* larger) allocations, and both the address and size must meet
|
|
|
|
* alignment constraints.
|
2013-09-23 06:45:30 +08:00
|
|
|
*/
|
2014-11-17 20:46:44 +08:00
|
|
|
if (align < EFI_ALLOC_ALIGN)
|
|
|
|
align = EFI_ALLOC_ALIGN;
|
2013-09-23 06:45:30 +08:00
|
|
|
|
2016-11-13 05:32:29 +08:00
|
|
|
size = round_up(size, EFI_ALLOC_ALIGN);
|
|
|
|
nr_pages = size / EFI_PAGE_SIZE;
|
2013-09-23 06:45:27 +08:00
|
|
|
again:
|
|
|
|
for (i = 0; i < map_size / desc_size; i++) {
|
|
|
|
efi_memory_desc_t *desc;
|
|
|
|
unsigned long m = (unsigned long)map;
|
|
|
|
u64 start, end;
|
|
|
|
|
2017-08-16 21:46:51 +08:00
|
|
|
desc = efi_early_memdesc_ptr(m, desc_size, i);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (desc->type != EFI_CONVENTIONAL_MEMORY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (desc->num_pages < nr_pages)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
start = desc->phys_addr;
|
2016-11-13 05:32:29 +08:00
|
|
|
end = start + desc->num_pages * EFI_PAGE_SIZE;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2015-02-20 12:18:03 +08:00
|
|
|
if (end > max)
|
2013-09-23 06:45:27 +08:00
|
|
|
end = max;
|
|
|
|
|
2015-02-20 12:18:03 +08:00
|
|
|
if ((start + size) > end)
|
|
|
|
continue;
|
|
|
|
|
2013-09-23 06:45:27 +08:00
|
|
|
if (round_down(end - size, align) < start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
start = round_down(end - size, align);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't allocate at 0x0. It will confuse code that
|
|
|
|
* checks pointers against NULL.
|
|
|
|
*/
|
|
|
|
if (start == 0x0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (start > max_addr)
|
|
|
|
max_addr = start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!max_addr)
|
|
|
|
status = EFI_NOT_FOUND;
|
|
|
|
else {
|
2014-03-22 18:09:01 +08:00
|
|
|
status = efi_call_early(allocate_pages,
|
|
|
|
EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
|
|
|
|
nr_pages, &max_addr);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS) {
|
|
|
|
max = max_addr;
|
|
|
|
max_addr = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
*addr = max_addr;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pool, map);
|
2013-09-23 06:45:27 +08:00
|
|
|
fail:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate at the lowest possible address.
|
|
|
|
*/
|
2014-07-02 20:54:42 +08:00
|
|
|
efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
|
|
|
|
unsigned long size, unsigned long align,
|
|
|
|
unsigned long *addr)
|
2013-09-23 06:45:27 +08:00
|
|
|
{
|
2016-08-30 04:38:51 +08:00
|
|
|
unsigned long map_size, desc_size, buff_size;
|
2013-09-23 06:45:27 +08:00
|
|
|
efi_memory_desc_t *map;
|
|
|
|
efi_status_t status;
|
|
|
|
unsigned long nr_pages;
|
|
|
|
int i;
|
2016-08-30 04:38:51 +08:00
|
|
|
struct efi_boot_memmap boot_map;
|
|
|
|
|
|
|
|
boot_map.map = ↦
|
|
|
|
boot_map.map_size = &map_size;
|
|
|
|
boot_map.desc_size = &desc_size;
|
|
|
|
boot_map.desc_ver = NULL;
|
|
|
|
boot_map.key_ptr = NULL;
|
|
|
|
boot_map.buff_size = &buff_size;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2016-08-30 04:38:51 +08:00
|
|
|
status = efi_get_memory_map(sys_table_arg, &boot_map);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
2013-09-23 06:45:30 +08:00
|
|
|
/*
|
2016-11-13 05:32:29 +08:00
|
|
|
* Enforce minimum alignment that EFI or Linux requires when
|
|
|
|
* requesting a specific address. We are doing page-based (or
|
|
|
|
* larger) allocations, and both the address and size must meet
|
|
|
|
* alignment constraints.
|
2013-09-23 06:45:30 +08:00
|
|
|
*/
|
2014-11-17 20:46:44 +08:00
|
|
|
if (align < EFI_ALLOC_ALIGN)
|
|
|
|
align = EFI_ALLOC_ALIGN;
|
2013-09-23 06:45:30 +08:00
|
|
|
|
2016-11-13 05:32:29 +08:00
|
|
|
size = round_up(size, EFI_ALLOC_ALIGN);
|
|
|
|
nr_pages = size / EFI_PAGE_SIZE;
|
2013-09-23 06:45:27 +08:00
|
|
|
for (i = 0; i < map_size / desc_size; i++) {
|
|
|
|
efi_memory_desc_t *desc;
|
|
|
|
unsigned long m = (unsigned long)map;
|
|
|
|
u64 start, end;
|
|
|
|
|
2017-08-16 21:46:51 +08:00
|
|
|
desc = efi_early_memdesc_ptr(m, desc_size, i);
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
if (desc->type != EFI_CONVENTIONAL_MEMORY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (desc->num_pages < nr_pages)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
start = desc->phys_addr;
|
2016-11-13 05:32:29 +08:00
|
|
|
end = start + desc->num_pages * EFI_PAGE_SIZE;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't allocate at 0x0. It will confuse code that
|
|
|
|
* checks pointers against NULL. Skip the first 8
|
|
|
|
* bytes so we start at a nice even number.
|
|
|
|
*/
|
|
|
|
if (start == 0x0)
|
|
|
|
start += 8;
|
|
|
|
|
|
|
|
start = round_up(start, align);
|
|
|
|
if ((start + size) > end)
|
|
|
|
continue;
|
|
|
|
|
2014-03-22 18:09:01 +08:00
|
|
|
status = efi_call_early(allocate_pages,
|
|
|
|
EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
|
|
|
|
nr_pages, &start);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status == EFI_SUCCESS) {
|
|
|
|
*addr = start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == map_size / desc_size)
|
|
|
|
status = EFI_NOT_FOUND;
|
|
|
|
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pool, map);
|
2013-09-23 06:45:27 +08:00
|
|
|
fail:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:54:42 +08:00
|
|
|
void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
|
|
|
|
unsigned long addr)
|
2013-09-23 06:45:27 +08:00
|
|
|
{
|
|
|
|
unsigned long nr_pages;
|
|
|
|
|
2013-09-23 06:45:38 +08:00
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
|
2014-11-17 20:46:44 +08:00
|
|
|
nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pages, addr, nr_pages);
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
2017-01-31 21:21:33 +08:00
|
|
|
static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
|
|
|
|
efi_char16_t *filename_16, void **handle,
|
|
|
|
u64 *file_sz)
|
|
|
|
{
|
|
|
|
efi_file_handle_t *h, *fh = __fh;
|
|
|
|
efi_file_info_t *info;
|
|
|
|
efi_status_t status;
|
|
|
|
efi_guid_t info_guid = EFI_FILE_INFO_ID;
|
|
|
|
unsigned long info_sz;
|
|
|
|
|
|
|
|
status = efi_call_proto(efi_file_handle, 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");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
*handle = h;
|
|
|
|
|
|
|
|
info_sz = 0;
|
|
|
|
status = efi_call_proto(efi_file_handle, 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");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
grow:
|
|
|
|
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
|
|
|
|
info_sz, (void **)&info);
|
|
|
|
if (status != EFI_SUCCESS) {
|
|
|
|
efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
|
|
|
|
&info_sz, info);
|
|
|
|
if (status == EFI_BUFFER_TOO_SMALL) {
|
|
|
|
efi_call_early(free_pool, info);
|
|
|
|
goto grow;
|
|
|
|
}
|
|
|
|
|
|
|
|
*file_sz = info->file_size;
|
|
|
|
efi_call_early(free_pool, info);
|
|
|
|
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
efi_printk(sys_table_arg, "Failed to get initrd info\n");
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
|
|
|
|
{
|
|
|
|
return efi_call_proto(efi_file_handle, read, handle, size, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static efi_status_t efi_file_close(void *handle)
|
|
|
|
{
|
|
|
|
return efi_call_proto(efi_file_handle, close, handle);
|
|
|
|
}
|
|
|
|
|
2018-07-20 09:47:23 +08:00
|
|
|
static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
|
|
|
|
efi_loaded_image_t *image,
|
|
|
|
efi_file_handle_t **__fh)
|
|
|
|
{
|
|
|
|
efi_file_io_interface_t *io;
|
|
|
|
efi_file_handle_t *fh;
|
|
|
|
efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
|
|
|
|
efi_status_t status;
|
|
|
|
void *handle = (void *)(unsigned long)efi_table_attr(efi_loaded_image,
|
|
|
|
device_handle,
|
|
|
|
image);
|
|
|
|
|
|
|
|
status = efi_call_early(handle_protocol, handle,
|
|
|
|
&fs_proto, (void **)&io);
|
|
|
|
if (status != EFI_SUCCESS) {
|
|
|
|
efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = efi_call_proto(efi_file_io_interface, open_volume, io, &fh);
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
efi_printk(sys_table_arg, "Failed to open volume\n");
|
|
|
|
else
|
|
|
|
*__fh = fh;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:52:11 +08:00
|
|
|
/*
|
|
|
|
* Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
|
|
|
|
* option, e.g. efi=nochunk.
|
|
|
|
*
|
|
|
|
* It should be noted that efi= is parsed in two very different
|
|
|
|
* environments, first in the early boot environment of the EFI boot
|
|
|
|
* stub, and subsequently during the kernel boot.
|
|
|
|
*/
|
2017-04-05 00:09:08 +08:00
|
|
|
efi_status_t efi_parse_options(char const *cmdline)
|
2014-08-05 18:52:11 +08:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2017-04-05 00:09:08 +08:00
|
|
|
str = strstr(cmdline, "nokaslr");
|
|
|
|
if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
|
|
|
|
__nokaslr = 1;
|
efi/libstub: Make file I/O chunking x86-specific
The ARM decompressor is finicky when it comes to uninitialized variables
with local linkage, the reason being that it may relocate .text and .bss
independently when executing from ROM. This is only possible if all
references into .bss from .text are absolute, and this happens to be the
case for references emitted under -fpic to symbols with external linkage,
and so all .bss references must involve symbols with external linkage.
When building the ARM stub using clang, the initialized local variable
__chunk_size is optimized into a zero-initialized flag that indicates
whether chunking is in effect or not. This flag is therefore emitted into
.bss, which triggers the ARM decompressor's diagnostics, resulting in a
failed build.
Under UEFI, we never execute the decompressor from ROM, so the diagnostic
makes little sense here. But we can easily work around the issue by making
__chunk_size global instead.
However, given that the file I/O chunking that is controlled by the
__chunk_size variable is intended to work around known bugs on various
x86 implementations of UEFI, we can simply make the chunking an x86
specific feature. This is an improvement by itself, and also removes the
need to parse the efi= options in the stub entirely.
Tested-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/1486380166-31868-8-git-send-email-ard.biesheuvel@linaro.org
[ Small readability edits. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-02-06 19:22:46 +08:00
|
|
|
|
2017-04-05 00:09:09 +08:00
|
|
|
str = strstr(cmdline, "quiet");
|
|
|
|
if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
|
|
|
|
__quiet = 1;
|
|
|
|
|
2014-08-05 18:52:11 +08:00
|
|
|
/*
|
|
|
|
* If no EFI parameters were specified on the cmdline we've got
|
|
|
|
* nothing to do.
|
|
|
|
*/
|
|
|
|
str = strstr(cmdline, "efi=");
|
|
|
|
if (!str)
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
|
|
|
/* Skip ahead to first argument */
|
|
|
|
str += strlen("efi=");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember, because efi= is also used by the kernel we need to
|
|
|
|
* skip over arguments we don't understand.
|
|
|
|
*/
|
2017-04-05 00:02:45 +08:00
|
|
|
while (*str && *str != ' ') {
|
2014-08-05 18:52:11 +08:00
|
|
|
if (!strncmp(str, "nochunk", 7)) {
|
|
|
|
str += strlen("nochunk");
|
|
|
|
__chunk_size = -1UL;
|
|
|
|
}
|
|
|
|
|
efi/arm/arm64: Allow SetVirtualAddressMap() to be omitted
The UEFI spec revision 2.7 errata A section 8.4 has the following to
say about the virtual memory runtime services:
"This section contains function definitions for the virtual memory
support that may be optionally used by an operating system at runtime.
If an operating system chooses to make EFI runtime service calls in a
virtual addressing mode instead of the flat physical mode, then the
operating system must use the services in this section to switch the
EFI runtime services from flat physical addressing to virtual
addressing."
So it is pretty clear that calling SetVirtualAddressMap() is entirely
optional, and so there is no point in doing so unless it achieves
anything useful for us.
This is not the case for 64-bit ARM. The identity mapping used by the
firmware is arbitrarily converted into another permutation of userland
addresses (i.e., bits [63:48] cleared), and the runtime code could easily
deal with the original layout in exactly the same way as it deals with
the converted layout. However, due to constraints related to page size
differences if the OS is not running with 4k pages, and related to
systems that may expose the individual sections of PE/COFF runtime
modules as different memory regions, creating the virtual layout is a
bit fiddly, and requires us to sort the memory map and reason about
adjacent regions with identical memory types etc etc.
So the obvious fix is to stop calling SetVirtualAddressMap() altogether
on arm64 systems. However, to avoid surprises, which are notoriously
hard to diagnose when it comes to OS<->firmware interactions, let's
start by making it an opt-out feature, and implement support for the
'efi=novamap' kernel command line parameter on ARM and arm64 systems.
( Note that 32-bit ARM generally does require SetVirtualAddressMap() to be
used, given that the physical memory map and the kernel virtual address
map are not guaranteed to be non-overlapping like on arm64. However,
having support for efi=novamap,noruntime on 32-bit ARM, combined with
the recently proposed support for earlycon=efifb, is likely to be useful
to diagnose boot issues on such systems if they have no accessible serial
port. )
Tested-by: Jeffrey Hugo <jhugo@codeaurora.org>
Tested-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Tested-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: AKASHI Takahiro <takahiro.akashi@linaro.org>
Cc: Alexander Graf <agraf@suse.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Peter Jones <pjones@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/20190202094119.13230-8-ard.biesheuvel@linaro.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2019-02-02 17:41:16 +08:00
|
|
|
if (!strncmp(str, "novamap", 7)) {
|
|
|
|
str += strlen("novamap");
|
|
|
|
__novamap = 1;
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:52:11 +08:00
|
|
|
/* Group words together, delimited by "," */
|
2017-04-05 00:02:45 +08:00
|
|
|
while (*str && *str != ' ' && *str != ',')
|
2014-08-05 18:52:11 +08:00
|
|
|
str++;
|
|
|
|
|
|
|
|
if (*str == ',')
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
/*
|
2013-09-23 06:45:40 +08:00
|
|
|
* Check the cmdline for a LILO-style file= arguments.
|
2013-09-23 06:45:27 +08:00
|
|
|
*
|
2013-09-23 06:45:40 +08:00
|
|
|
* We only support loading a file from the same filesystem as
|
|
|
|
* the kernel image.
|
2013-09-23 06:45:27 +08:00
|
|
|
*/
|
2014-07-02 20:54:42 +08:00
|
|
|
efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
|
|
|
|
efi_loaded_image_t *image,
|
|
|
|
char *cmd_line, char *option_string,
|
|
|
|
unsigned long max_addr,
|
|
|
|
unsigned long *load_addr,
|
|
|
|
unsigned long *load_size)
|
2013-09-23 06:45:27 +08:00
|
|
|
{
|
2013-09-23 06:45:40 +08:00
|
|
|
struct file_info *files;
|
|
|
|
unsigned long file_addr;
|
|
|
|
u64 file_size_total;
|
2014-04-04 20:25:46 +08:00
|
|
|
efi_file_handle_t *fh = NULL;
|
2013-09-23 06:45:27 +08:00
|
|
|
efi_status_t status;
|
2013-09-23 06:45:40 +08:00
|
|
|
int nr_files;
|
2013-09-23 06:45:27 +08:00
|
|
|
char *str;
|
|
|
|
int i, j, k;
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
file_addr = 0;
|
|
|
|
file_size_total = 0;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2013-09-23 06:45:39 +08:00
|
|
|
str = cmd_line;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
j = 0; /* See close_handles */
|
|
|
|
|
2013-09-23 06:45:39 +08:00
|
|
|
if (!load_addr || !load_size)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
*load_addr = 0;
|
|
|
|
*load_size = 0;
|
|
|
|
|
2013-09-23 06:45:27 +08:00
|
|
|
if (!str || !*str)
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
for (nr_files = 0; *str; nr_files++) {
|
2013-09-23 06:45:39 +08:00
|
|
|
str = strstr(str, option_string);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (!str)
|
|
|
|
break;
|
|
|
|
|
2013-09-23 06:45:39 +08:00
|
|
|
str += strlen(option_string);
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
/* Skip any leading slashes */
|
|
|
|
while (*str == '/' || *str == '\\')
|
|
|
|
str++;
|
|
|
|
|
|
|
|
while (*str && *str != ' ' && *str != '\n')
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
if (!nr_files)
|
2013-09-23 06:45:27 +08:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
2014-03-22 18:09:01 +08:00
|
|
|
status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
|
|
|
|
nr_files * sizeof(*files), (void **)&files);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS) {
|
2013-12-14 03:04:49 +08:00
|
|
|
pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
|
2013-09-23 06:45:27 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:39 +08:00
|
|
|
str = cmd_line;
|
2013-09-23 06:45:40 +08:00
|
|
|
for (i = 0; i < nr_files; i++) {
|
|
|
|
struct file_info *file;
|
2013-09-23 06:45:27 +08:00
|
|
|
efi_char16_t filename_16[256];
|
|
|
|
efi_char16_t *p;
|
|
|
|
|
2013-09-23 06:45:39 +08:00
|
|
|
str = strstr(str, option_string);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (!str)
|
|
|
|
break;
|
|
|
|
|
2013-09-23 06:45:39 +08:00
|
|
|
str += strlen(option_string);
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
file = &files[i];
|
2013-09-23 06:45:27 +08:00
|
|
|
p = filename_16;
|
|
|
|
|
|
|
|
/* Skip any leading slashes */
|
|
|
|
while (*str == '/' || *str == '\\')
|
|
|
|
str++;
|
|
|
|
|
|
|
|
while (*str && *str != ' ' && *str != '\n') {
|
|
|
|
if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*str == '/') {
|
|
|
|
*p++ = '\\';
|
2013-09-23 06:45:42 +08:00
|
|
|
str++;
|
2013-09-23 06:45:27 +08:00
|
|
|
} else {
|
|
|
|
*p++ = *str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* Only open the volume once. */
|
|
|
|
if (!i) {
|
2018-07-20 09:47:23 +08:00
|
|
|
status = efi_open_volume(sys_table_arg, image, &fh);
|
2014-01-10 23:27:14 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
2013-09-23 06:45:40 +08:00
|
|
|
goto free_files;
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:27:14 +08:00
|
|
|
status = efi_file_size(sys_table_arg, fh, filename_16,
|
|
|
|
(void **)&file->handle, &file->size);
|
|
|
|
if (status != EFI_SUCCESS)
|
2013-09-23 06:45:27 +08:00
|
|
|
goto close_handles;
|
|
|
|
|
2014-01-10 23:27:14 +08:00
|
|
|
file_size_total += file->size;
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
if (file_size_total) {
|
2013-09-23 06:45:27 +08:00
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
/*
|
2013-09-23 06:45:40 +08:00
|
|
|
* Multiple files need to be at consecutive addresses in memory,
|
|
|
|
* so allocate enough memory for all the files. This is used
|
|
|
|
* for loading multiple files.
|
2013-09-23 06:45:27 +08:00
|
|
|
*/
|
2013-09-23 06:45:40 +08:00
|
|
|
status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
|
|
|
|
&file_addr, max_addr);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS) {
|
2013-12-14 03:04:49 +08:00
|
|
|
pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
|
2013-09-23 06:45:27 +08:00
|
|
|
goto close_handles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've run out of free low memory. */
|
2013-09-23 06:45:40 +08:00
|
|
|
if (file_addr > max_addr) {
|
2013-12-14 03:04:49 +08:00
|
|
|
pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
|
2013-09-23 06:45:27 +08:00
|
|
|
status = EFI_INVALID_PARAMETER;
|
2013-09-23 06:45:40 +08:00
|
|
|
goto free_file_total;
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
addr = file_addr;
|
|
|
|
for (j = 0; j < nr_files; j++) {
|
2013-09-23 06:45:41 +08:00
|
|
|
unsigned long size;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
size = files[j].size;
|
2013-09-23 06:45:27 +08:00
|
|
|
while (size) {
|
2013-09-23 06:45:41 +08:00
|
|
|
unsigned long chunksize;
|
efi/libstub: Make file I/O chunking x86-specific
The ARM decompressor is finicky when it comes to uninitialized variables
with local linkage, the reason being that it may relocate .text and .bss
independently when executing from ROM. This is only possible if all
references into .bss from .text are absolute, and this happens to be the
case for references emitted under -fpic to symbols with external linkage,
and so all .bss references must involve symbols with external linkage.
When building the ARM stub using clang, the initialized local variable
__chunk_size is optimized into a zero-initialized flag that indicates
whether chunking is in effect or not. This flag is therefore emitted into
.bss, which triggers the ARM decompressor's diagnostics, resulting in a
failed build.
Under UEFI, we never execute the decompressor from ROM, so the diagnostic
makes little sense here. But we can easily work around the issue by making
__chunk_size global instead.
However, given that the file I/O chunking that is controlled by the
__chunk_size variable is intended to work around known bugs on various
x86 implementations of UEFI, we can simply make the chunking an x86
specific feature. This is an improvement by itself, and also removes the
need to parse the efi= options in the stub entirely.
Tested-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/1486380166-31868-8-git-send-email-ard.biesheuvel@linaro.org
[ Small readability edits. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-02-06 19:22:46 +08:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
|
2014-08-05 18:52:11 +08:00
|
|
|
chunksize = __chunk_size;
|
2013-09-23 06:45:27 +08:00
|
|
|
else
|
|
|
|
chunksize = size;
|
2014-01-10 23:27:14 +08:00
|
|
|
|
2014-04-10 21:11:45 +08:00
|
|
|
status = efi_file_read(files[j].handle,
|
2014-01-10 23:27:14 +08:00
|
|
|
&chunksize,
|
|
|
|
(void *)addr);
|
2013-09-23 06:45:27 +08:00
|
|
|
if (status != EFI_SUCCESS) {
|
2013-12-14 03:04:49 +08:00
|
|
|
pr_efi_err(sys_table_arg, "Failed to read file\n");
|
2013-09-23 06:45:40 +08:00
|
|
|
goto free_file_total;
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
addr += chunksize;
|
|
|
|
size -= chunksize;
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:11:45 +08:00
|
|
|
efi_file_close(files[j].handle);
|
2013-09-23 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pool, files);
|
2013-09-23 06:45:27 +08:00
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
*load_addr = file_addr;
|
|
|
|
*load_size = file_size_total;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
return status;
|
|
|
|
|
2013-09-23 06:45:40 +08:00
|
|
|
free_file_total:
|
|
|
|
efi_free(sys_table_arg, file_size_total, file_addr);
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
close_handles:
|
|
|
|
for (k = j; k < i; k++)
|
2014-04-10 21:11:45 +08:00
|
|
|
efi_file_close(files[k].handle);
|
2013-09-23 06:45:40 +08:00
|
|
|
free_files:
|
2014-03-22 18:09:01 +08:00
|
|
|
efi_call_early(free_pool, files);
|
2013-09-23 06:45:27 +08:00
|
|
|
fail:
|
2013-09-23 06:45:39 +08:00
|
|
|
*load_addr = 0;
|
|
|
|
*load_size = 0;
|
2013-09-23 06:45:27 +08:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2013-09-23 06:45:32 +08:00
|
|
|
/*
|
|
|
|
* Relocate a kernel image, either compressed or uncompressed.
|
|
|
|
* In the ARM64 case, all kernel images are currently
|
|
|
|
* uncompressed, and as such when we relocate it we need to
|
|
|
|
* allocate additional space for the BSS segment. Any low
|
|
|
|
* memory that this function should avoid needs to be
|
|
|
|
* unavailable in the EFI memory map, as if the preferred
|
|
|
|
* address is not available the lowest available address will
|
|
|
|
* be used.
|
|
|
|
*/
|
2014-07-02 20:54:42 +08:00
|
|
|
efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
|
|
|
|
unsigned long *image_addr,
|
|
|
|
unsigned long image_size,
|
|
|
|
unsigned long alloc_size,
|
|
|
|
unsigned long preferred_addr,
|
|
|
|
unsigned long alignment)
|
2013-09-23 06:45:31 +08:00
|
|
|
{
|
2013-09-23 06:45:32 +08:00
|
|
|
unsigned long cur_image_addr;
|
|
|
|
unsigned long new_addr = 0;
|
2013-09-23 06:45:31 +08:00
|
|
|
efi_status_t status;
|
2013-09-23 06:45:32 +08:00
|
|
|
unsigned long nr_pages;
|
|
|
|
efi_physical_addr_t efi_addr = preferred_addr;
|
|
|
|
|
|
|
|
if (!image_addr || !image_size || !alloc_size)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
if (alloc_size < image_size)
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
cur_image_addr = *image_addr;
|
2013-09-23 06:45:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The EFI firmware loader could have placed the kernel image
|
2013-09-23 06:45:32 +08:00
|
|
|
* anywhere in memory, but the kernel has restrictions on the
|
|
|
|
* max physical address it can run at. Some architectures
|
|
|
|
* also have a prefered address, so first try to relocate
|
|
|
|
* to the preferred address. If that fails, allocate as low
|
|
|
|
* as possible while respecting the required alignment.
|
2013-09-23 06:45:31 +08:00
|
|
|
*/
|
2014-11-17 20:46:44 +08:00
|
|
|
nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
|
2014-03-22 18:09:01 +08:00
|
|
|
status = efi_call_early(allocate_pages,
|
|
|
|
EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
|
|
|
|
nr_pages, &efi_addr);
|
2013-09-23 06:45:32 +08:00
|
|
|
new_addr = efi_addr;
|
|
|
|
/*
|
|
|
|
* If preferred address allocation failed allocate as low as
|
|
|
|
* possible.
|
|
|
|
*/
|
2013-09-23 06:45:31 +08:00
|
|
|
if (status != EFI_SUCCESS) {
|
2013-09-23 06:45:32 +08:00
|
|
|
status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
|
|
|
|
&new_addr);
|
|
|
|
}
|
|
|
|
if (status != EFI_SUCCESS) {
|
2013-12-14 03:04:49 +08:00
|
|
|
pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
|
2013-09-23 06:45:32 +08:00
|
|
|
return status;
|
2013-09-23 06:45:31 +08:00
|
|
|
}
|
|
|
|
|
2013-09-23 06:45:32 +08:00
|
|
|
/*
|
|
|
|
* We know source/dest won't overlap since both memory ranges
|
|
|
|
* have been allocated by UEFI, so we can safely use memcpy.
|
|
|
|
*/
|
|
|
|
memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
|
2013-09-23 06:45:31 +08:00
|
|
|
|
2013-09-23 06:45:32 +08:00
|
|
|
/* Return the new address of the relocated image. */
|
|
|
|
*image_addr = new_addr;
|
2013-09-23 06:45:31 +08:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2013-09-23 06:45:33 +08:00
|
|
|
|
2013-09-20 22:55:39 +08:00
|
|
|
/*
|
|
|
|
* Get the number of UTF-8 bytes corresponding to an UTF-16 character.
|
|
|
|
* This overestimates for surrogates, but that is okay.
|
|
|
|
*/
|
|
|
|
static int efi_utf8_bytes(u16 c)
|
|
|
|
{
|
|
|
|
return 1 + (c >= 0x80) + (c >= 0x800);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
|
|
|
|
*/
|
|
|
|
static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
|
|
|
|
{
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
c = *src++;
|
|
|
|
if (n && c >= 0xd800 && c <= 0xdbff &&
|
|
|
|
*src >= 0xdc00 && *src <= 0xdfff) {
|
|
|
|
c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
|
|
|
|
src++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
if (c >= 0xd800 && c <= 0xdfff)
|
|
|
|
c = 0xfffd; /* Unmatched surrogate */
|
|
|
|
if (c < 0x80) {
|
|
|
|
*dst++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c < 0x800) {
|
|
|
|
*dst++ = 0xc0 + (c >> 6);
|
|
|
|
goto t1;
|
|
|
|
}
|
|
|
|
if (c < 0x10000) {
|
|
|
|
*dst++ = 0xe0 + (c >> 12);
|
|
|
|
goto t2;
|
|
|
|
}
|
|
|
|
*dst++ = 0xf0 + (c >> 18);
|
|
|
|
*dst++ = 0x80 + ((c >> 12) & 0x3f);
|
|
|
|
t2:
|
|
|
|
*dst++ = 0x80 + ((c >> 6) & 0x3f);
|
|
|
|
t1:
|
|
|
|
*dst++ = 0x80 + (c & 0x3f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2016-01-11 18:47:49 +08:00
|
|
|
#ifndef MAX_CMDLINE_ADDRESS
|
|
|
|
#define MAX_CMDLINE_ADDRESS ULONG_MAX
|
|
|
|
#endif
|
|
|
|
|
2013-09-23 06:45:33 +08:00
|
|
|
/*
|
|
|
|
* Convert the unicode UEFI command line to ASCII to pass to kernel.
|
|
|
|
* Size of memory allocated return in *cmd_line_len.
|
|
|
|
* Returns NULL on error.
|
|
|
|
*/
|
2014-07-02 20:54:42 +08:00
|
|
|
char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
|
|
|
|
efi_loaded_image_t *image,
|
|
|
|
int *cmd_line_len)
|
2013-09-23 06:45:33 +08:00
|
|
|
{
|
2013-09-20 22:55:39 +08:00
|
|
|
const u16 *s2;
|
2013-09-23 06:45:33 +08:00
|
|
|
u8 *s1 = NULL;
|
|
|
|
unsigned long cmdline_addr = 0;
|
2013-09-20 22:55:39 +08:00
|
|
|
int load_options_chars = image->load_options_size / 2; /* UTF-16 */
|
|
|
|
const u16 *options = image->load_options;
|
|
|
|
int options_bytes = 0; /* UTF-8 bytes */
|
|
|
|
int options_chars = 0; /* UTF-16 chars */
|
2013-09-23 06:45:33 +08:00
|
|
|
efi_status_t status;
|
|
|
|
u16 zero = 0;
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
s2 = options;
|
2013-09-20 22:55:39 +08:00
|
|
|
while (*s2 && *s2 != '\n'
|
|
|
|
&& options_chars < load_options_chars) {
|
|
|
|
options_bytes += efi_utf8_bytes(*s2++);
|
|
|
|
options_chars++;
|
2013-09-23 06:45:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:55:39 +08:00
|
|
|
if (!options_chars) {
|
2013-09-23 06:45:33 +08:00
|
|
|
/* No command line options, so return empty string*/
|
|
|
|
options = &zero;
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:55:39 +08:00
|
|
|
options_bytes++; /* NUL termination */
|
2014-04-04 20:25:46 +08:00
|
|
|
|
2016-01-11 18:47:49 +08:00
|
|
|
status = efi_high_alloc(sys_table_arg, options_bytes, 0,
|
|
|
|
&cmdline_addr, MAX_CMDLINE_ADDRESS);
|
2013-09-23 06:45:33 +08:00
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s1 = (u8 *)cmdline_addr;
|
2013-09-20 22:55:39 +08:00
|
|
|
s2 = (const u16 *)options;
|
2013-09-23 06:45:33 +08:00
|
|
|
|
2013-09-20 22:55:39 +08:00
|
|
|
s1 = efi_utf16_to_utf8(s1, s2, options_chars);
|
2013-09-23 06:45:33 +08:00
|
|
|
*s1 = '\0';
|
|
|
|
|
2013-09-20 22:55:39 +08:00
|
|
|
*cmd_line_len = options_bytes;
|
2013-09-23 06:45:33 +08:00
|
|
|
return (char *)cmdline_addr;
|
|
|
|
}
|
2016-08-30 04:38:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle calling ExitBootServices according to the requirements set out by the
|
|
|
|
* spec. Obtains the current memory map, and returns that info after calling
|
|
|
|
* ExitBootServices. The client must specify a function to perform any
|
|
|
|
* processing of the memory map data prior to ExitBootServices. A client
|
|
|
|
* specific structure may be passed to the function via priv. The client
|
|
|
|
* function may be called multiple times.
|
|
|
|
*/
|
|
|
|
efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
|
|
|
|
void *handle,
|
|
|
|
struct efi_boot_memmap *map,
|
|
|
|
void *priv,
|
|
|
|
efi_exit_boot_map_processing priv_func)
|
|
|
|
{
|
|
|
|
efi_status_t status;
|
|
|
|
|
|
|
|
status = efi_get_memory_map(sys_table_arg, map);
|
|
|
|
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
status = priv_func(sys_table_arg, map, priv);
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto free_map;
|
|
|
|
|
|
|
|
status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
|
|
|
|
|
|
|
|
if (status == EFI_INVALID_PARAMETER) {
|
|
|
|
/*
|
|
|
|
* The memory map changed between efi_get_memory_map() and
|
|
|
|
* exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
|
|
|
|
* EFI_BOOT_SERVICES.ExitBootServices we need to get the
|
|
|
|
* updated map, and try again. The spec implies one retry
|
|
|
|
* should be sufficent, which is confirmed against the EDK2
|
|
|
|
* implementation. Per the spec, we can only invoke
|
|
|
|
* get_memory_map() and exit_boot_services() - we cannot alloc
|
|
|
|
* so efi_get_memory_map() cannot be used, and we must reuse
|
|
|
|
* the buffer. For all practical purposes, the headroom in the
|
|
|
|
* buffer should account for any changes in the map so the call
|
|
|
|
* to get_memory_map() is expected to succeed here.
|
|
|
|
*/
|
|
|
|
*map->map_size = *map->buff_size;
|
|
|
|
status = efi_call_early(get_memory_map,
|
|
|
|
map->map_size,
|
|
|
|
*map->map,
|
|
|
|
map->key_ptr,
|
|
|
|
map->desc_size,
|
|
|
|
map->desc_ver);
|
|
|
|
|
|
|
|
/* exit_boot_services() was called, thus cannot free */
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
status = priv_func(sys_table_arg, map, priv);
|
|
|
|
/* exit_boot_services() was called, thus cannot free */
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exit_boot_services() was called, thus cannot free */
|
|
|
|
if (status != EFI_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
|
|
|
free_map:
|
|
|
|
efi_call_early(free_pool, *map->map);
|
|
|
|
fail:
|
|
|
|
return status;
|
|
|
|
}
|