Lots of new things this time. High level highlights are: - Shim support (to boot Fedora) - Initial set of unit tests - Preparations to support UEFI Shell -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAABAgAGBQJZ0KdpAAoJECszeR4D/txga+AQAL8ehBlkr3R6b/n00p3kuAVE cfM+o/16b+P/r3U2/wvlNtw7L+UPlGaQNk8o7n4xTu0DpVlIt7DhFrSBIcK6VbYB 8BFJ/EL9NRj/sd/wb9B1Bc0SlXJcGxzRM+JxNseVmc7rz9sWdARvx/diQlP+YAX4 qjp2YRzsErjUgix4eGDXvgFQdFmh4Pnp7UnHlSq4WkINo14navGLe/PwChxsL91M AZV7JulVcm7XtOPTON8udec3O15O8BExAM1cH2ITjtD99Je+4VcuI99nLm2aIE++ U6vTuoogHAU+oCutD3qZU85uj2VJ2kYy2k5A/RK/i6Ug/QrTa+JA8PNWP78/TZ+8 xmyDVzG5MQhMIddDjJlMXbxIwgKhr0SjUJ1iVaGI0bS/6Glc7h9m+hhIXKudqOvs lxqPhy7mahtBwETjoUlM1WJg+TG+vIGDJtT22njjOSTh0CVYw2H3xihyFKE3AjaW L26EOEsxArErtzF17GgF5KxFOGaYAmVmA3XnCcEZEIY5gzL65uok1GX/apf4sIsi j8HSbW3UH6QwjWKBhF+RVuK4CcTDUS9K2YmJlrdZ69elDZyg5PWbpMLNdYIP+VVG +j91c7e3O2q2AHAzTprpmkyeIeqZfFhgyu4H/jPQ8Yi8N/b7wbugcLm3fHuUNal4 RB0KjbyJzJPQwbYGSLYi =9HkH -----END PGP SIGNATURE----- Merge tag 'signed-efi-next' of git://github.com/agraf/u-boot Patch queue for efi - 2017-10-01 Lots of new things this time. High level highlights are: - Shim support (to boot Fedora) - Initial set of unit tests - Preparations to support UEFI Shellmaster
commit
958046fc78
@ -0,0 +1,91 @@ |
||||
/*
|
||||
* EFI application loader |
||||
* |
||||
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#ifndef _EFI_SELFTEST_H |
||||
#define _EFI_SELFTEST_H |
||||
|
||||
#include <common.h> |
||||
#include <efi.h> |
||||
#include <efi_api.h> |
||||
#include <linker_lists.h> |
||||
|
||||
/*
|
||||
* Prints an error message. |
||||
* |
||||
* @... format string followed by fields to print |
||||
*/ |
||||
#define efi_st_error(...) \ |
||||
efi_st_printf("%s(%u):\nERROR: ", __FILE__, __LINE__); \
|
||||
efi_st_printf(__VA_ARGS__) \
|
||||
|
||||
/*
|
||||
* A test may be setup and executed at boottime, |
||||
* it may be setup at boottime and executed at runtime, |
||||
* or it may be setup and executed at runtime. |
||||
*/ |
||||
enum efi_test_phase { |
||||
EFI_EXECUTE_BEFORE_BOOTTIME_EXIT = 1, |
||||
EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
||||
EFI_SETUP_AFTER_BOOTTIME_EXIT, |
||||
}; |
||||
|
||||
extern struct efi_simple_text_output_protocol *con_out; |
||||
extern struct efi_simple_input_interface *con_in; |
||||
|
||||
/*
|
||||
* Exit the boot services. |
||||
* |
||||
* The size of the memory map is determined. |
||||
* Pool memory is allocated to copy the memory map. |
||||
* The memory amp is copied and the map key is obtained. |
||||
* The map key is used to exit the boot services. |
||||
*/ |
||||
void efi_st_exit_boot_services(void); |
||||
|
||||
/*
|
||||
* Print a pointer to an u16 string |
||||
* |
||||
* @pointer: pointer |
||||
* @buf: pointer to buffer address |
||||
* on return position of terminating zero word |
||||
*/ |
||||
void efi_st_printf(const char *fmt, ...) |
||||
__attribute__ ((format (__printf__, 1, 2))); |
||||
|
||||
/*
|
||||
* Reads an Unicode character from the input device. |
||||
* |
||||
* @return: Unicode character |
||||
*/ |
||||
u16 efi_st_get_key(void); |
||||
|
||||
/**
|
||||
* struct efi_unit_test - EFI unit test |
||||
* |
||||
* An efi_unit_test provides a interface to an EFI unit test. |
||||
* |
||||
* @name: name of unit test |
||||
* @phase: specifies when setup and execute are executed |
||||
* @setup: set up the unit test |
||||
* @teardown: tear down the unit test |
||||
* @execute: execute the unit test |
||||
*/ |
||||
struct efi_unit_test { |
||||
const char *name; |
||||
const enum efi_test_phase phase; |
||||
int (*setup)(const efi_handle_t handle, |
||||
const struct efi_system_table *systable); |
||||
int (*execute)(void); |
||||
int (*teardown)(void); |
||||
}; |
||||
|
||||
/* Declare a new EFI unit test */ |
||||
#define EFI_UNIT_TEST(__name) \ |
||||
ll_entry_declare(struct efi_unit_test, __name, efi_unit_test) |
||||
|
||||
#endif /* _EFI_SELFTEST_H */ |
@ -0,0 +1,180 @@ |
||||
/*
|
||||
* EFI utils |
||||
* |
||||
* Copyright (c) 2017 Rob Clark |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <common.h> |
||||
#include <charset.h> |
||||
#include <malloc.h> |
||||
#include <efi_loader.h> |
||||
|
||||
static const struct efi_boot_services *bs; |
||||
static const struct efi_runtime_services *rs; |
||||
|
||||
#define LOAD_OPTION_ACTIVE 0x00000001 |
||||
#define LOAD_OPTION_FORCE_RECONNECT 0x00000002 |
||||
#define LOAD_OPTION_HIDDEN 0x00000008 |
||||
|
||||
/*
|
||||
* bootmgr implements the logic of trying to find a payload to boot |
||||
* based on the BootOrder + BootXXXX variables, and then loading it. |
||||
* |
||||
* TODO detecting a special key held (f9?) and displaying a boot menu |
||||
* like you would get on a PC would be clever. |
||||
* |
||||
* TODO if we had a way to write and persist variables after the OS |
||||
* has started, we'd also want to check OsIndications to see if we |
||||
* should do normal or recovery boot. |
||||
*/ |
||||
|
||||
|
||||
/*
|
||||
* See section 3.1.3 in the v2.7 UEFI spec for more details on |
||||
* the layout of EFI_LOAD_OPTION. In short it is: |
||||
* |
||||
* typedef struct _EFI_LOAD_OPTION { |
||||
* UINT32 Attributes; |
||||
* UINT16 FilePathListLength; |
||||
* // CHAR16 Description[]; <-- variable length, NULL terminated
|
||||
* // EFI_DEVICE_PATH_PROTOCOL FilePathList[]; <-- FilePathListLength bytes
|
||||
* // UINT8 OptionalData[];
|
||||
* } EFI_LOAD_OPTION; |
||||
*/ |
||||
struct load_option { |
||||
u32 attributes; |
||||
u16 file_path_length; |
||||
u16 *label; |
||||
struct efi_device_path *file_path; |
||||
u8 *optional_data; |
||||
}; |
||||
|
||||
/* parse an EFI_LOAD_OPTION, as described above */ |
||||
static void parse_load_option(struct load_option *lo, void *ptr) |
||||
{ |
||||
lo->attributes = *(u32 *)ptr; |
||||
ptr += sizeof(u32); |
||||
|
||||
lo->file_path_length = *(u16 *)ptr; |
||||
ptr += sizeof(u16); |
||||
|
||||
lo->label = ptr; |
||||
ptr += (utf16_strlen(lo->label) + 1) * 2; |
||||
|
||||
lo->file_path = ptr; |
||||
ptr += lo->file_path_length; |
||||
|
||||
lo->optional_data = ptr; |
||||
} |
||||
|
||||
/* free() the result */ |
||||
static void *get_var(u16 *name, const efi_guid_t *vendor, |
||||
unsigned long *size) |
||||
{ |
||||
efi_guid_t *v = (efi_guid_t *)vendor; |
||||
efi_status_t ret; |
||||
void *buf = NULL; |
||||
|
||||
*size = 0; |
||||
EFI_CALL(ret = rs->get_variable((s16 *)name, v, NULL, size, buf)); |
||||
if (ret == EFI_BUFFER_TOO_SMALL) { |
||||
buf = malloc(*size); |
||||
EFI_CALL(ret = rs->get_variable((s16 *)name, v, NULL, size, buf)); |
||||
} |
||||
|
||||
if (ret != EFI_SUCCESS) { |
||||
free(buf); |
||||
*size = 0; |
||||
return NULL; |
||||
} |
||||
|
||||
return buf; |
||||
} |
||||
|
||||
/*
|
||||
* Attempt to load load-option number 'n', returning device_path and file_path |
||||
* if successful. This checks that the EFI_LOAD_OPTION is active (enabled) |
||||
* and that the specified file to boot exists. |
||||
*/ |
||||
static void *try_load_entry(uint16_t n, struct efi_device_path **device_path, |
||||
struct efi_device_path **file_path) |
||||
{ |
||||
struct load_option lo; |
||||
u16 varname[] = L"Boot0000"; |
||||
u16 hexmap[] = L"0123456789ABCDEF"; |
||||
void *load_option, *image = NULL; |
||||
unsigned long size; |
||||
|
||||
varname[4] = hexmap[(n & 0xf000) >> 12]; |
||||
varname[5] = hexmap[(n & 0x0f00) >> 8]; |
||||
varname[6] = hexmap[(n & 0x00f0) >> 4]; |
||||
varname[7] = hexmap[(n & 0x000f) >> 0]; |
||||
|
||||
load_option = get_var(varname, &efi_global_variable_guid, &size); |
||||
if (!load_option) |
||||
return NULL; |
||||
|
||||
parse_load_option(&lo, load_option); |
||||
|
||||
if (lo.attributes & LOAD_OPTION_ACTIVE) { |
||||
efi_status_t ret; |
||||
u16 *str = NULL; |
||||
|
||||
debug("%s: trying to load \"%ls\" from: %ls\n", __func__, |
||||
lo.label, (str = efi_dp_str(lo.file_path))); |
||||
efi_free_pool(str); |
||||
|
||||
ret = efi_load_image_from_path(lo.file_path, &image); |
||||
|
||||
if (ret != EFI_SUCCESS) |
||||
goto error; |
||||
|
||||
printf("Booting: %ls\n", lo.label); |
||||
efi_dp_split_file_path(lo.file_path, device_path, file_path); |
||||
} |
||||
|
||||
error: |
||||
free(load_option); |
||||
|
||||
return image; |
||||
} |
||||
|
||||
/*
|
||||
* Attempt to load, in the order specified by BootOrder EFI variable, the |
||||
* available load-options, finding and returning the first one that can |
||||
* be loaded successfully. |
||||
*/ |
||||
void *efi_bootmgr_load(struct efi_device_path **device_path, |
||||
struct efi_device_path **file_path) |
||||
{ |
||||
uint16_t *bootorder; |
||||
unsigned long size; |
||||
void *image = NULL; |
||||
int i, num; |
||||
|
||||
__efi_entry_check(); |
||||
|
||||
bs = systab.boottime; |
||||
rs = systab.runtime; |
||||
|
||||
bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size); |
||||
if (!bootorder) |
||||
goto error; |
||||
|
||||
num = size / sizeof(uint16_t); |
||||
for (i = 0; i < num; i++) { |
||||
debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]); |
||||
image = try_load_entry(bootorder[i], device_path, file_path); |
||||
if (image) |
||||
break; |
||||
} |
||||
|
||||
free(bootorder); |
||||
|
||||
error: |
||||
__efi_exit_check(); |
||||
|
||||
return image; |
||||
} |
@ -0,0 +1,563 @@ |
||||
/*
|
||||
* EFI device path from u-boot device-model mapping |
||||
* |
||||
* (C) Copyright 2017 Rob Clark |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <common.h> |
||||
#include <blk.h> |
||||
#include <dm.h> |
||||
#include <usb.h> |
||||
#include <mmc.h> |
||||
#include <efi_loader.h> |
||||
#include <inttypes.h> |
||||
#include <part.h> |
||||
|
||||
/* template END node: */ |
||||
static const struct efi_device_path END = { |
||||
.type = DEVICE_PATH_TYPE_END, |
||||
.sub_type = DEVICE_PATH_SUB_TYPE_END, |
||||
.length = sizeof(END), |
||||
}; |
||||
|
||||
#define U_BOOT_GUID \ |
||||
EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
|
||||
0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b) |
||||
|
||||
/* template ROOT node: */ |
||||
static const struct efi_device_path_vendor ROOT = { |
||||
.dp = { |
||||
.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE, |
||||
.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR, |
||||
.length = sizeof(ROOT), |
||||
}, |
||||
.guid = U_BOOT_GUID, |
||||
}; |
||||
|
||||
static void *dp_alloc(size_t sz) |
||||
{ |
||||
void *buf; |
||||
|
||||
if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != EFI_SUCCESS) |
||||
return NULL; |
||||
|
||||
return buf; |
||||
} |
||||
|
||||
/*
|
||||
* Iterate to next block in device-path, terminating (returning NULL) |
||||
* at /End* node. |
||||
*/ |
||||
struct efi_device_path *efi_dp_next(const struct efi_device_path *dp) |
||||
{ |
||||
if (dp == NULL) |
||||
return NULL; |
||||
if (dp->type == DEVICE_PATH_TYPE_END) |
||||
return NULL; |
||||
dp = ((void *)dp) + dp->length; |
||||
if (dp->type == DEVICE_PATH_TYPE_END) |
||||
return NULL; |
||||
return (struct efi_device_path *)dp; |
||||
} |
||||
|
||||
/*
|
||||
* Compare two device-paths, stopping when the shorter of the two hits |
||||
* an End* node. This is useful to, for example, compare a device-path |
||||
* representing a device with one representing a file on the device, or |
||||
* a device with a parent device. |
||||
*/ |
||||
int efi_dp_match(struct efi_device_path *a, struct efi_device_path *b) |
||||
{ |
||||
while (1) { |
||||
int ret; |
||||
|
||||
ret = memcmp(&a->length, &b->length, sizeof(a->length)); |
||||
if (ret) |
||||
return ret; |
||||
|
||||
ret = memcmp(a, b, a->length); |
||||
if (ret) |
||||
return ret; |
||||
|
||||
a = efi_dp_next(a); |
||||
b = efi_dp_next(b); |
||||
|
||||
if (!a || !b) |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* See UEFI spec (section 3.1.2, about short-form device-paths.. |
||||
* tl;dr: we can have a device-path that starts with a USB WWID |
||||
* or USB Class node, and a few other cases which don't encode |
||||
* the full device path with bus hierarchy: |
||||
* |
||||
* - MESSAGING:USB_WWID |
||||
* - MESSAGING:USB_CLASS |
||||
* - MEDIA:FILE_PATH |
||||
* - MEDIA:HARD_DRIVE |
||||
* - MESSAGING:URI |
||||
*/ |
||||
static struct efi_device_path *shorten_path(struct efi_device_path *dp) |
||||
{ |
||||
while (dp) { |
||||
/*
|
||||
* TODO: Add MESSAGING:USB_WWID and MESSAGING:URI.. |
||||
* in practice fallback.efi just uses MEDIA:HARD_DRIVE |
||||
* so not sure when we would see these other cases. |
||||
*/ |
||||
if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) || |
||||
EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) || |
||||
EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH)) |
||||
return dp; |
||||
|
||||
dp = efi_dp_next(dp); |
||||
} |
||||
|
||||
return dp; |
||||
} |
||||
|
||||
static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path, |
||||
struct efi_device_path **rem) |
||||
{ |
||||
struct efi_object *efiobj; |
||||
|
||||
list_for_each_entry(efiobj, &efi_obj_list, link) { |
||||
int i; |
||||
|
||||
for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { |
||||
struct efi_handler *handler = &efiobj->protocols[i]; |
||||
struct efi_device_path *obj_dp; |
||||
|
||||
if (!handler->guid) |
||||
break; |
||||
|
||||
if (guidcmp(handler->guid, &efi_guid_device_path)) |
||||
continue; |
||||
|
||||
obj_dp = handler->protocol_interface; |
||||
|
||||
do { |
||||
if (efi_dp_match(dp, obj_dp) == 0) { |
||||
if (rem) { |
||||
*rem = ((void *)dp) + |
||||
efi_dp_size(obj_dp); |
||||
} |
||||
return efiobj; |
||||
} |
||||
|
||||
obj_dp = shorten_path(efi_dp_next(obj_dp)); |
||||
} while (short_path && obj_dp); |
||||
} |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Find an efiobj from device-path, if 'rem' is not NULL, returns the |
||||
* remaining part of the device path after the matched object. |
||||
*/ |
||||
struct efi_object *efi_dp_find_obj(struct efi_device_path *dp, |
||||
struct efi_device_path **rem) |
||||
{ |
||||
struct efi_object *efiobj; |
||||
|
||||
efiobj = find_obj(dp, false, rem); |
||||
|
||||
if (!efiobj) |
||||
efiobj = find_obj(dp, true, rem); |
||||
|
||||
return efiobj; |
||||
} |
||||
|
||||
/* return size not including End node: */ |
||||
unsigned efi_dp_size(const struct efi_device_path *dp) |
||||
{ |
||||
unsigned sz = 0; |
||||
|
||||
while (dp) { |
||||
sz += dp->length; |
||||
dp = efi_dp_next(dp); |
||||
} |
||||
|
||||
return sz; |
||||
} |
||||
|
||||
struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp) |
||||
{ |
||||
struct efi_device_path *ndp; |
||||
unsigned sz = efi_dp_size(dp) + sizeof(END); |
||||
|
||||
if (!dp) |
||||
return NULL; |
||||
|
||||
ndp = dp_alloc(sz); |
||||
memcpy(ndp, dp, sz); |
||||
|
||||
return ndp; |
||||
} |
||||
|
||||
struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1, |
||||
const struct efi_device_path *dp2) |
||||
{ |
||||
struct efi_device_path *ret; |
||||
|
||||
if (!dp1) { |
||||
ret = efi_dp_dup(dp2); |
||||
} else if (!dp2) { |
||||
ret = efi_dp_dup(dp1); |
||||
} else { |
||||
/* both dp1 and dp2 are non-null */ |
||||
unsigned sz1 = efi_dp_size(dp1); |
||||
unsigned sz2 = efi_dp_size(dp2); |
||||
void *p = dp_alloc(sz1 + sz2 + sizeof(END)); |
||||
memcpy(p, dp1, sz1); |
||||
memcpy(p + sz1, dp2, sz2); |
||||
memcpy(p + sz1 + sz2, &END, sizeof(END)); |
||||
ret = p; |
||||
} |
||||
|
||||
return ret; |
||||
} |
||||
|
||||
struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp, |
||||
const struct efi_device_path *node) |
||||
{ |
||||
struct efi_device_path *ret; |
||||
|
||||
if (!node && !dp) { |
||||
ret = efi_dp_dup(&END); |
||||
} else if (!node) { |
||||
ret = efi_dp_dup(dp); |
||||
} else if (!dp) { |
||||
unsigned sz = node->length; |
||||
void *p = dp_alloc(sz + sizeof(END)); |
||||
memcpy(p, node, sz); |
||||
memcpy(p + sz, &END, sizeof(END)); |
||||
ret = p; |
||||
} else { |
||||
/* both dp and node are non-null */ |
||||
unsigned sz = efi_dp_size(dp); |
||||
void *p = dp_alloc(sz + node->length + sizeof(END)); |
||||
memcpy(p, dp, sz); |
||||
memcpy(p + sz, node, node->length); |
||||
memcpy(p + sz + node->length, &END, sizeof(END)); |
||||
ret = p; |
||||
} |
||||
|
||||
return ret; |
||||
} |
||||
|
||||
#ifdef CONFIG_DM |
||||
/* size of device-path not including END node for device and all parents
|
||||
* up to the root device. |
||||
*/ |
||||
static unsigned dp_size(struct udevice *dev) |
||||
{ |
||||
if (!dev || !dev->driver) |
||||
return sizeof(ROOT); |
||||
|
||||
switch (dev->driver->id) { |
||||
case UCLASS_ROOT: |
||||
case UCLASS_SIMPLE_BUS: |
||||
/* stop traversing parents at this point: */ |
||||
return sizeof(ROOT); |
||||
case UCLASS_MMC: |
||||
return dp_size(dev->parent) + |
||||
sizeof(struct efi_device_path_sd_mmc_path); |
||||
case UCLASS_MASS_STORAGE: |
||||
case UCLASS_USB_HUB: |
||||
return dp_size(dev->parent) + |
||||
sizeof(struct efi_device_path_usb_class); |
||||
default: |
||||
/* just skip over unknown classes: */ |
||||
return dp_size(dev->parent); |
||||
} |
||||
} |
||||
|
||||
static void *dp_fill(void *buf, struct udevice *dev) |
||||
{ |
||||
if (!dev || !dev->driver) |
||||
return buf; |
||||
|
||||
switch (dev->driver->id) { |
||||
case UCLASS_ROOT: |
||||
case UCLASS_SIMPLE_BUS: { |
||||
/* stop traversing parents at this point: */ |
||||
struct efi_device_path_vendor *vdp = buf; |
||||
*vdp = ROOT; |
||||
return &vdp[1]; |
||||
} |
||||
#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC) |
||||
case UCLASS_MMC: { |
||||
struct efi_device_path_sd_mmc_path *sddp = |
||||
dp_fill(buf, dev->parent); |
||||
struct mmc *mmc = mmc_get_mmc_dev(dev); |
||||
struct blk_desc *desc = mmc_get_blk_desc(mmc); |
||||
|
||||
sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
||||
sddp->dp.sub_type = (desc->if_type == IF_TYPE_MMC) ? |
||||
DEVICE_PATH_SUB_TYPE_MSG_MMC : |
||||
DEVICE_PATH_SUB_TYPE_MSG_SD; |
||||
sddp->dp.length = sizeof(*sddp); |
||||
sddp->slot_number = dev->seq; |
||||
|
||||
return &sddp[1]; |
||||
} |
||||
#endif |
||||
case UCLASS_MASS_STORAGE: |
||||
case UCLASS_USB_HUB: { |
||||
struct efi_device_path_usb_class *udp = |
||||
dp_fill(buf, dev->parent); |
||||
struct usb_device *udev = dev_get_parent_priv(dev); |
||||
struct usb_device_descriptor *desc = &udev->descriptor; |
||||
|
||||
udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
||||
udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS; |
||||
udp->dp.length = sizeof(*udp); |
||||
udp->vendor_id = desc->idVendor; |
||||
udp->product_id = desc->idProduct; |
||||
udp->device_class = desc->bDeviceClass; |
||||
udp->device_subclass = desc->bDeviceSubClass; |
||||
udp->device_protocol = desc->bDeviceProtocol; |
||||
|
||||
return &udp[1]; |
||||
} |
||||
default: |
||||
debug("unhandled device class: %s (%u)\n", |
||||
dev->name, dev->driver->id); |
||||
return dp_fill(buf, dev->parent); |
||||
} |
||||
} |
||||
|
||||
/* Construct a device-path from a device: */ |
||||
struct efi_device_path *efi_dp_from_dev(struct udevice *dev) |
||||
{ |
||||
void *buf, *start; |
||||
|
||||
start = buf = dp_alloc(dp_size(dev) + sizeof(END)); |
||||
buf = dp_fill(buf, dev); |
||||
*((struct efi_device_path *)buf) = END; |
||||
|
||||
return start; |
||||
} |
||||
#endif |
||||
|
||||
static unsigned dp_part_size(struct blk_desc *desc, int part) |
||||
{ |
||||
unsigned dpsize; |
||||
|
||||
#ifdef CONFIG_BLK |
||||
dpsize = dp_size(desc->bdev->parent); |
||||
#else |
||||
dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb); |
||||
#endif |
||||
|
||||
if (part == 0) /* the actual disk, not a partition */ |
||||
return dpsize; |
||||
|
||||
if (desc->part_type == PART_TYPE_ISO) |
||||
dpsize += sizeof(struct efi_device_path_cdrom_path); |
||||
else |
||||
dpsize += sizeof(struct efi_device_path_hard_drive_path); |
||||
|
||||
return dpsize; |
||||
} |
||||
|
||||
static void *dp_part_fill(void *buf, struct blk_desc *desc, int part) |
||||
{ |
||||
disk_partition_t info; |
||||
|
||||
#ifdef CONFIG_BLK |
||||
buf = dp_fill(buf, desc->bdev->parent); |
||||
#else |
||||
/*
|
||||
* We *could* make a more accurate path, by looking at if_type |
||||
* and handling all the different cases like we do for non- |
||||
* legacy (ie CONFIG_BLK=y) case. But most important thing |
||||
* is just to have a unique device-path for if_type+devnum. |
||||
* So map things to a fictional USB device: |
||||
*/ |
||||
struct efi_device_path_usb *udp; |
||||
|
||||
memcpy(buf, &ROOT, sizeof(ROOT)); |
||||
buf += sizeof(ROOT); |
||||
|
||||
udp = buf; |
||||
udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
||||
udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB; |
||||
udp->dp.length = sizeof(*udp); |
||||
udp->parent_port_number = desc->if_type; |
||||
udp->usb_interface = desc->devnum; |
||||
buf = &udp[1]; |
||||
#endif |
||||
|
||||
if (part == 0) /* the actual disk, not a partition */ |
||||
return buf; |
||||
|
||||
part_get_info(desc, part, &info); |
||||
|
||||
if (desc->part_type == PART_TYPE_ISO) { |
||||
struct efi_device_path_cdrom_path *cddp = buf; |
||||
|
||||
cddp->boot_entry = part - 1; |
||||
cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
||||
cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH; |
||||
cddp->dp.length = sizeof(*cddp); |
||||
cddp->partition_start = info.start; |
||||
cddp->partition_end = info.size; |
||||
|
||||
buf = &cddp[1]; |
||||
} else { |
||||
struct efi_device_path_hard_drive_path *hddp = buf; |
||||
|
||||
hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
||||
hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH; |
||||
hddp->dp.length = sizeof(*hddp); |
||||
hddp->partition_number = part - 1; |
||||
hddp->partition_start = info.start; |
||||
hddp->partition_end = info.size; |
||||
if (desc->part_type == PART_TYPE_EFI) |
||||
hddp->partmap_type = 2; |
||||
else |
||||
hddp->partmap_type = 1; |
||||
hddp->signature_type = desc->sig_type; |
||||
if (hddp->signature_type != 0) |
||||
memcpy(hddp->partition_signature, &desc->guid_sig, |
||||
sizeof(hddp->partition_signature)); |
||||
|
||||
buf = &hddp[1]; |
||||
} |
||||
|
||||
return buf; |
||||
} |
||||
|
||||
|
||||
/* Construct a device-path from a partition on a blk device: */ |
||||
struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part) |
||||
{ |
||||
void *buf, *start; |
||||
|
||||
start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END)); |
||||
|
||||
buf = dp_part_fill(buf, desc, part); |
||||
|
||||
*((struct efi_device_path *)buf) = END; |
||||
|
||||
return start; |
||||
} |
||||
|
||||
/* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */ |
||||
static void path_to_uefi(u16 *uefi, const char *path) |
||||
{ |
||||
while (*path) { |
||||
char c = *(path++); |
||||
if (c == '/') |
||||
c = '\\'; |
||||
*(uefi++) = c; |
||||
} |
||||
*uefi = '\0'; |
||||
} |
||||
|
||||
/*
|
||||
* If desc is NULL, this creates a path with only the file component, |
||||
* otherwise it creates a full path with both device and file components |
||||
*/ |
||||
struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part, |
||||
const char *path) |
||||
{ |
||||
struct efi_device_path_file_path *fp; |
||||
void *buf, *start; |
||||
unsigned dpsize = 0, fpsize; |
||||
|
||||
if (desc) |
||||
dpsize = dp_part_size(desc, part); |
||||
|
||||
fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1); |
||||
dpsize += fpsize; |
||||
|
||||
start = buf = dp_alloc(dpsize + sizeof(END)); |
||||
|
||||
if (desc) |
||||
buf = dp_part_fill(buf, desc, part); |
||||
|
||||
/* add file-path: */ |
||||
fp = buf; |
||||
fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
||||
fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; |
||||
fp->dp.length = fpsize; |
||||
path_to_uefi(fp->str, path); |
||||
buf += fpsize; |
||||
|
||||
*((struct efi_device_path *)buf) = END; |
||||
|
||||
return start; |
||||
} |
||||
|
||||
#ifdef CONFIG_NET |
||||
struct efi_device_path *efi_dp_from_eth(void) |
||||
{ |
||||
struct efi_device_path_mac_addr *ndp; |
||||
void *buf, *start; |
||||
unsigned dpsize = 0; |
||||
|
||||
assert(eth_get_dev()); |
||||
|
||||
#ifdef CONFIG_DM_ETH |
||||
dpsize += dp_size(eth_get_dev()); |
||||
#else |
||||
dpsize += sizeof(ROOT); |
||||
#endif |
||||
dpsize += sizeof(*ndp); |
||||
|
||||
start = buf = dp_alloc(dpsize + sizeof(END)); |
||||
|
||||
#ifdef CONFIG_DM_ETH |
||||
buf = dp_fill(buf, eth_get_dev()); |
||||
#else |
||||
memcpy(buf, &ROOT, sizeof(ROOT)); |
||||
buf += sizeof(ROOT); |
||||
#endif |
||||
|
||||
ndp = buf; |
||||
ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
||||
ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR; |
||||
ndp->dp.length = sizeof(*ndp); |
||||
memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN); |
||||
buf = &ndp[1]; |
||||
|
||||
*((struct efi_device_path *)buf) = END; |
||||
|
||||
return start; |
||||
} |
||||
#endif |
||||
|
||||
/*
|
||||
* Helper to split a full device path (containing both device and file |
||||
* parts) into it's constituent parts. |
||||
*/ |
||||
void efi_dp_split_file_path(struct efi_device_path *full_path, |
||||
struct efi_device_path **device_path, |
||||
struct efi_device_path **file_path) |
||||
{ |
||||
struct efi_device_path *p, *dp, *fp; |
||||
|
||||
dp = efi_dp_dup(full_path); |
||||
p = dp; |
||||
while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) |
||||
p = efi_dp_next(p); |
||||
fp = efi_dp_dup(p); |
||||
|
||||
p->type = DEVICE_PATH_TYPE_END; |
||||
p->sub_type = DEVICE_PATH_SUB_TYPE_END; |
||||
p->length = sizeof(*p); |
||||
|
||||
*device_path = dp; |
||||
*file_path = fp; |
||||
} |
@ -0,0 +1,560 @@ |
||||
/*
|
||||
* EFI utils |
||||
* |
||||
* Copyright (c) 2017 Rob Clark |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <common.h> |
||||
#include <charset.h> |
||||
#include <efi_loader.h> |
||||
#include <malloc.h> |
||||
#include <fs.h> |
||||
|
||||
struct file_system { |
||||
struct efi_simple_file_system_protocol base; |
||||
struct efi_device_path *dp; |
||||
struct blk_desc *desc; |
||||
int part; |
||||
}; |
||||
#define to_fs(x) container_of(x, struct file_system, base) |
||||
|
||||
struct file_handle { |
||||
struct efi_file_handle base; |
||||
struct file_system *fs; |
||||
loff_t offset; /* current file position/cursor */ |
||||
int isdir; |
||||
|
||||
/* for reading a directory: */ |
||||
struct fs_dir_stream *dirs; |
||||
struct fs_dirent *dent; |
||||
|
||||
char path[0]; |
||||
}; |
||||
#define to_fh(x) container_of(x, struct file_handle, base) |
||||
|
||||
static const struct efi_file_handle efi_file_handle_protocol; |
||||
|
||||
static char *basename(struct file_handle *fh) |
||||
{ |
||||
char *s = strrchr(fh->path, '/'); |
||||
if (s) |
||||
return s + 1; |
||||
return fh->path; |
||||
} |
||||
|
||||
static int set_blk_dev(struct file_handle *fh) |
||||
{ |
||||
return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part); |
||||
} |
||||
|
||||
static int is_dir(struct file_handle *fh) |
||||
{ |
||||
struct fs_dir_stream *dirs; |
||||
|
||||
set_blk_dev(fh); |
||||
dirs = fs_opendir(fh->path); |
||||
if (!dirs) |
||||
return 0; |
||||
|
||||
fs_closedir(dirs); |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
/*
|
||||
* Normalize a path which may include either back or fwd slashes, |
||||
* double slashes, . or .. entries in the path, etc. |
||||
*/ |
||||
static int sanitize_path(char *path) |
||||
{ |
||||
char *p; |
||||
|
||||
/* backslash to slash: */ |
||||
p = path; |
||||
while ((p = strchr(p, '\\'))) |
||||
*p++ = '/'; |
||||
|
||||
/* handle double-slashes: */ |
||||
p = path; |
||||
while ((p = strstr(p, "//"))) { |
||||
char *src = p + 1; |
||||
memmove(p, src, strlen(src) + 1); |
||||
} |
||||
|
||||
/* handle extra /.'s */ |
||||
p = path; |
||||
while ((p = strstr(p, "/."))) { |
||||
/*
|
||||
* You'd be tempted to do this *after* handling ".."s |
||||
* below to avoid having to check if "/." is start of |
||||
* a "/..", but that won't have the correct results.. |
||||
* for example, "/foo/./../bar" would get resolved to |
||||
* "/foo/bar" if you did these two passes in the other |
||||
* order |
||||
*/ |
||||
if (p[2] == '.') { |
||||
p += 2; |
||||
continue; |
||||
} |
||||
char *src = p + 2; |
||||
memmove(p, src, strlen(src) + 1); |
||||
} |
||||
|
||||
/* handle extra /..'s: */ |
||||
p = path; |
||||
while ((p = strstr(p, "/.."))) { |
||||
char *src = p + 3; |
||||
|
||||
p--; |
||||
|
||||
/* find beginning of previous path entry: */ |
||||
while (true) { |
||||
if (p < path) |
||||
return -1; |
||||
if (*p == '/') |
||||
break; |
||||
p--; |
||||
} |
||||
|
||||
memmove(p, src, strlen(src) + 1); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
/* NOTE: despite what you would expect, 'file_name' is actually a path.
|
||||
* With windoze style backlashes, ofc. |
||||
*/ |
||||
static struct efi_file_handle *file_open(struct file_system *fs, |
||||
struct file_handle *parent, s16 *file_name, u64 mode) |
||||
{ |
||||
struct file_handle *fh; |
||||
char f0[MAX_UTF8_PER_UTF16] = {0}; |
||||
int plen = 0; |
||||
int flen = 0; |
||||
|
||||
if (file_name) { |
||||
utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1); |
||||
flen = utf16_strlen((u16 *)file_name); |
||||
} |
||||
|
||||
/* we could have a parent, but also an absolute path: */ |
||||
if (f0[0] == '\\') { |
||||
plen = 0; |
||||
} else if (parent) { |
||||
plen = strlen(parent->path) + 1; |
||||
} |
||||
|
||||
/* +2 is for null and '/' */ |
||||
fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2); |
||||
|
||||
fh->base = efi_file_handle_protocol; |
||||
fh->fs = fs; |
||||
|
||||
if (parent) { |
||||
char *p = fh->path; |
||||
|
||||
if (plen > 0) { |
||||
strcpy(p, parent->path); |
||||
p += plen - 1; |
||||
*p++ = '/'; |
||||
} |
||||
|
||||
utf16_to_utf8((u8 *)p, (u16 *)file_name, flen); |
||||
|
||||
if (sanitize_path(fh->path)) |
||||
goto error; |
||||
|
||||
/* check if file exists: */ |
||||
if (set_blk_dev(fh)) |
||||
goto error; |
||||
|
||||
if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path))) |
||||
goto error; |
||||
|
||||
/* figure out if file is a directory: */ |
||||
fh->isdir = is_dir(fh); |
||||
} else { |
||||
fh->isdir = 1; |
||||
strcpy(fh->path, ""); |
||||
} |
||||
|
||||
return &fh->base; |
||||
|
||||
error: |
||||
free(fh); |
||||
return NULL; |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file, |
||||
struct efi_file_handle **new_handle, |
||||
s16 *file_name, u64 open_mode, u64 attributes) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
|
||||
EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name, |
||||
open_mode, attributes); |
||||
|
||||
*new_handle = file_open(fh->fs, fh, file_name, open_mode); |
||||
if (!*new_handle) |
||||
return EFI_EXIT(EFI_NOT_FOUND); |
||||
|
||||
return EFI_EXIT(EFI_SUCCESS); |
||||
} |
||||
|
||||
static efi_status_t file_close(struct file_handle *fh) |
||||
{ |
||||
fs_closedir(fh->dirs); |
||||
free(fh); |
||||
return EFI_SUCCESS; |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
EFI_ENTRY("%p", file); |
||||
return EFI_EXIT(file_close(fh)); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
EFI_ENTRY("%p", file); |
||||
file_close(fh); |
||||
return EFI_EXIT(EFI_WARN_DELETE_FAILURE); |
||||
} |
||||
|
||||
static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size, |
||||
void *buffer) |
||||
{ |
||||
loff_t actread; |
||||
|
||||
if (fs_read(fh->path, (ulong)buffer, fh->offset, |
||||
*buffer_size, &actread)) |
||||
return EFI_DEVICE_ERROR; |
||||
|
||||
*buffer_size = actread; |
||||
fh->offset += actread; |
||||
|
||||
return EFI_SUCCESS; |
||||
} |
||||
|
||||
static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size, |
||||
void *buffer) |
||||
{ |
||||
struct efi_file_info *info = buffer; |
||||
struct fs_dirent *dent; |
||||
unsigned int required_size; |
||||
|
||||
if (!fh->dirs) { |
||||
assert(fh->offset == 0); |
||||
fh->dirs = fs_opendir(fh->path); |
||||
if (!fh->dirs) |
||||
return EFI_DEVICE_ERROR; |
||||
} |
||||
|
||||
/*
|
||||
* So this is a bit awkward. Since fs layer is stateful and we |
||||
* can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below |
||||
* we might have to return without consuming the dent.. so we |
||||
* have to stash it for next call. |
||||
*/ |
||||
if (fh->dent) { |
||||
dent = fh->dent; |
||||
fh->dent = NULL; |
||||
} else { |
||||
dent = fs_readdir(fh->dirs); |
||||
} |
||||
|
||||
|
||||
if (!dent) { |
||||
/* no more files in directory: */ |
||||
/* workaround shim.efi bug/quirk.. as find_boot_csv()
|
||||
* loops through directory contents, it initially calls |
||||
* read w/ zero length buffer to find out how much mem |
||||
* to allocate for the EFI_FILE_INFO, then allocates, |
||||
* and then calls a 2nd time. If we return size of |
||||
* zero the first time, it happily passes that to |
||||
* AllocateZeroPool(), and when that returns NULL it |
||||
* thinks it is EFI_OUT_OF_RESOURCES. So on first |
||||
* call return a non-zero size: |
||||
*/ |
||||
if (*buffer_size == 0) |
||||
*buffer_size = sizeof(*info); |
||||
else |
||||
*buffer_size = 0; |
||||
return EFI_SUCCESS; |
||||
} |
||||
|
||||
/* check buffer size: */ |
||||
required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1); |
||||
if (*buffer_size < required_size) { |
||||
*buffer_size = required_size; |
||||
fh->dent = dent; |
||||
return EFI_BUFFER_TOO_SMALL; |
||||
} |
||||
|
||||
*buffer_size = required_size; |
||||
memset(info, 0, required_size); |
||||
|
||||
info->size = required_size; |
||||
info->file_size = dent->size; |
||||
info->physical_size = dent->size; |
||||
|
||||
if (dent->type == FS_DT_DIR) |
||||
info->attribute |= EFI_FILE_DIRECTORY; |
||||
|
||||
ascii2unicode((u16 *)info->file_name, dent->name); |
||||
|
||||
fh->offset++; |
||||
|
||||
return EFI_SUCCESS; |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file, |
||||
u64 *buffer_size, void *buffer) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
efi_status_t ret = EFI_SUCCESS; |
||||
|
||||
EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer); |
||||
|
||||
if (set_blk_dev(fh)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
if (fh->isdir) |
||||
ret = dir_read(fh, buffer_size, buffer); |
||||
else |
||||
ret = file_read(fh, buffer_size, buffer); |
||||
|
||||
error: |
||||
return EFI_EXIT(ret); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file, |
||||
u64 *buffer_size, void *buffer) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
efi_status_t ret = EFI_SUCCESS; |
||||
loff_t actwrite; |
||||
|
||||
EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer); |
||||
|
||||
if (set_blk_dev(fh)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size, |
||||
&actwrite)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
*buffer_size = actwrite; |
||||
fh->offset += actwrite; |
||||
|
||||
error: |
||||
return EFI_EXIT(ret); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file, |
||||
u64 *pos) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
EFI_ENTRY("%p, %p", file, pos); |
||||
*pos = fh->offset; |
||||
return EFI_EXIT(EFI_SUCCESS); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, |
||||
u64 pos) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
efi_status_t ret = EFI_SUCCESS; |
||||
|
||||
EFI_ENTRY("%p, %llu", file, pos); |
||||
|
||||
if (fh->isdir) { |
||||
if (pos != 0) { |
||||
ret = EFI_UNSUPPORTED; |
||||
goto error; |
||||
} |
||||
fs_closedir(fh->dirs); |
||||
fh->dirs = NULL; |
||||
} |
||||
|
||||
if (pos == ~0ULL) { |
||||
loff_t file_size; |
||||
|
||||
if (set_blk_dev(fh)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
if (fs_size(fh->path, &file_size)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
pos = file_size; |
||||
} |
||||
|
||||
fh->offset = pos; |
||||
|
||||
error: |
||||
return EFI_EXIT(ret); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file, |
||||
efi_guid_t *info_type, u64 *buffer_size, void *buffer) |
||||
{ |
||||
struct file_handle *fh = to_fh(file); |
||||
efi_status_t ret = EFI_SUCCESS; |
||||
|
||||
EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer); |
||||
|
||||
if (!guidcmp(info_type, &efi_file_info_guid)) { |
||||
struct efi_file_info *info = buffer; |
||||
char *filename = basename(fh); |
||||
unsigned int required_size; |
||||
loff_t file_size; |
||||
|
||||
/* check buffer size: */ |
||||
required_size = sizeof(*info) + 2 * (strlen(filename) + 1); |
||||
if (*buffer_size < required_size) { |
||||
*buffer_size = required_size; |
||||
ret = EFI_BUFFER_TOO_SMALL; |
||||
goto error; |
||||
} |
||||
|
||||
if (set_blk_dev(fh)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
if (fs_size(fh->path, &file_size)) { |
||||
ret = EFI_DEVICE_ERROR; |
||||
goto error; |
||||
} |
||||
|
||||
memset(info, 0, required_size); |
||||
|
||||
info->size = required_size; |
||||
info->file_size = file_size; |
||||
info->physical_size = file_size; |
||||
|
||||
if (fh->isdir) |
||||
info->attribute |= EFI_FILE_DIRECTORY; |
||||
|
||||
ascii2unicode((u16 *)info->file_name, filename); |
||||
} else { |
||||
ret = EFI_UNSUPPORTED; |
||||
} |
||||
|
||||
error: |
||||
return EFI_EXIT(ret); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, |
||||
efi_guid_t *info_type, u64 buffer_size, void *buffer) |
||||
{ |
||||
EFI_ENTRY("%p, %p, %llu, %p", file, info_type, buffer_size, buffer); |
||||
return EFI_EXIT(EFI_UNSUPPORTED); |
||||
} |
||||
|
||||
static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file) |
||||
{ |
||||
EFI_ENTRY("%p", file); |
||||
return EFI_EXIT(EFI_SUCCESS); |
||||
} |
||||
|
||||
static const struct efi_file_handle efi_file_handle_protocol = { |
||||
.rev = EFI_FILE_PROTOCOL_REVISION, |
||||
.open = efi_file_open, |
||||
.close = efi_file_close, |
||||
.delete = efi_file_delete, |
||||
.read = efi_file_read, |
||||
.write = efi_file_write, |
||||
.getpos = efi_file_getpos, |
||||
.setpos = efi_file_setpos, |
||||
.getinfo = efi_file_getinfo, |
||||
.setinfo = efi_file_setinfo, |
||||
.flush = efi_file_flush, |
||||
}; |
||||
|
||||
struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) |
||||
{ |
||||
struct efi_simple_file_system_protocol *v; |
||||
struct efi_file_handle *f; |
||||
efi_status_t ret; |
||||
|
||||
v = efi_fs_from_path(fp); |
||||
if (!v) |
||||
return NULL; |
||||
|
||||
EFI_CALL(ret = v->open_volume(v, &f)); |
||||
if (ret != EFI_SUCCESS) |
||||
return NULL; |
||||
|
||||
/* skip over device-path nodes before the file path: */ |
||||
while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) |
||||
fp = efi_dp_next(fp); |
||||
|
||||
while (fp) { |
||||
struct efi_device_path_file_path *fdp = |
||||
container_of(fp, struct efi_device_path_file_path, dp); |
||||
struct efi_file_handle *f2; |
||||
|
||||
if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) { |
||||
printf("bad file path!\n"); |
||||
f->close(f); |
||||
return NULL; |
||||
} |
||||
|
||||
EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str, |
||||
EFI_FILE_MODE_READ, 0)); |
||||
if (ret != EFI_SUCCESS) |
||||
return NULL; |
||||
|
||||
fp = efi_dp_next(fp); |
||||
|
||||
EFI_CALL(f->close(f)); |
||||
f = f2; |
||||
} |
||||
|
||||
return f; |
||||
} |
||||
|
||||
static efi_status_t EFIAPI |
||||
efi_open_volume(struct efi_simple_file_system_protocol *this, |
||||
struct efi_file_handle **root) |
||||
{ |
||||
struct file_system *fs = to_fs(this); |
||||
|
||||
EFI_ENTRY("%p, %p", this, root); |
||||
|
||||
*root = file_open(fs, NULL, NULL, 0); |
||||
|
||||
return EFI_EXIT(EFI_SUCCESS); |
||||
} |
||||
|
||||
struct efi_simple_file_system_protocol * |
||||
efi_simple_file_system(struct blk_desc *desc, int part, |
||||
struct efi_device_path *dp) |
||||
{ |
||||
struct file_system *fs; |
||||
|
||||
fs = calloc(1, sizeof(*fs)); |
||||
fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION; |
||||
fs->base.open_volume = efi_open_volume; |
||||
fs->desc = desc; |
||||
fs->part = part; |
||||
fs->dp = dp; |
||||
|
||||
return &fs->base; |
||||
} |
@ -0,0 +1,335 @@ |
||||
/*
|
||||
* EFI utils |
||||
* |
||||
* Copyright (c) 2017 Rob Clark |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <malloc.h> |
||||
#include <charset.h> |
||||
#include <efi_loader.h> |
||||
|
||||
#define READ_ONLY BIT(31) |
||||
|
||||
/*
|
||||
* Mapping between EFI variables and u-boot variables: |
||||
* |
||||
* efi_$guid_$varname = {attributes}(type)value |
||||
* |
||||
* For example: |
||||
* |
||||
* efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported= |
||||
* "{ro,boot,run}(blob)0000000000000000" |
||||
* efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder= |
||||
* "(blob)00010000" |
||||
* |
||||
* The attributes are a comma separated list of these possible |
||||
* attributes: |
||||
* |
||||
* + ro - read-only |
||||
* + boot - boot-services access |
||||
* + run - runtime access |
||||
* |
||||
* NOTE: with current implementation, no variables are available after |
||||
* ExitBootServices, and all are persisted (if possible). |
||||
* |
||||
* If not specified, the attributes default to "{boot}". |
||||
* |
||||
* The required type is one of: |
||||
* |
||||
* + utf8 - raw utf8 string |
||||
* + blob - arbitrary length hex string |
||||
* |
||||
* Maybe a utf16 type would be useful to for a string value to be auto |
||||
* converted to utf16? |
||||
*/ |
||||
|
||||
#define MAX_VAR_NAME 31 |
||||
#define MAX_NATIVE_VAR_NAME \ |
||||
(strlen("efi_xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx_") + \
|
||||
(MAX_VAR_NAME * MAX_UTF8_PER_UTF16)) |
||||
|
||||
static int hex(unsigned char ch) |
||||
{ |
||||
if (ch >= 'a' && ch <= 'f') |
||||
return ch-'a'+10; |
||||
if (ch >= '0' && ch <= '9') |
||||
return ch-'0'; |
||||
if (ch >= 'A' && ch <= 'F') |
||||
return ch-'A'+10; |
||||
return -1; |
||||
} |
||||
|
||||
static const char *hex2mem(u8 *mem, const char *hexstr, int count) |
||||
{ |
||||
memset(mem, 0, count/2); |
||||
|
||||
do { |
||||
int nibble; |
||||
|
||||
*mem = 0; |
||||
|
||||
if (!count || !*hexstr) |
||||
break; |
||||
|
||||
nibble = hex(*hexstr); |
||||
if (nibble < 0) |
||||
break; |
||||
|
||||
*mem = nibble; |
||||
count--; |
||||
hexstr++; |
||||
|
||||
if (!count || !*hexstr) |
||||
break; |
||||
|
||||
nibble = hex(*hexstr); |
||||
if (nibble < 0) |
||||
break; |
||||
|
||||
*mem = (*mem << 4) | nibble; |
||||
count--; |
||||
hexstr++; |
||||
mem++; |
||||
|
||||
} while (1); |
||||
|
||||
if (*hexstr) |
||||
return hexstr; |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
static char *mem2hex(char *hexstr, const u8 *mem, int count) |
||||
{ |
||||
static const char hexchars[] = "0123456789abcdef"; |
||||
|
||||
while (count-- > 0) { |
||||
u8 ch = *mem++; |
||||
*hexstr++ = hexchars[ch >> 4]; |
||||
*hexstr++ = hexchars[ch & 0xf]; |
||||
} |
||||
|
||||
return hexstr; |
||||
} |
||||
|
||||
static efi_status_t efi_to_native(char *native, s16 *variable_name, |
||||
efi_guid_t *vendor) |
||||
{ |
||||
size_t len; |
||||
|
||||
len = utf16_strlen((u16 *)variable_name); |
||||
if (len >= MAX_VAR_NAME) |
||||
return EFI_DEVICE_ERROR; |
||||
|
||||
native += sprintf(native, "efi_%pUl_", vendor); |
||||
native = (char *)utf16_to_utf8((u8 *)native, (u16 *)variable_name, len); |
||||
*native = '\0'; |
||||
|
||||
return EFI_SUCCESS; |
||||
} |
||||
|
||||
static const char *prefix(const char *str, const char *prefix) |
||||
{ |
||||
size_t n = strlen(prefix); |
||||
if (!strncmp(prefix, str, n)) |
||||
return str + n; |
||||
return NULL; |
||||
} |
||||
|
||||
/* parse attributes part of variable value, if present: */ |
||||
static const char *parse_attr(const char *str, u32 *attrp) |
||||
{ |
||||
u32 attr = 0; |
||||
char sep = '{'; |
||||
|
||||
if (*str != '{') { |
||||
*attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS; |
||||
return str; |
||||
} |
||||
|
||||
while (*str == sep) { |
||||
const char *s; |
||||
|
||||
str++; |
||||
|
||||
if ((s = prefix(str, "ro"))) { |
||||
attr |= READ_ONLY; |
||||
} else if ((s = prefix(str, "boot"))) { |
||||
attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
||||
} else if ((s = prefix(str, "run"))) { |
||||
attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
||||
} else { |
||||
printf("invalid attribute: %s\n", str); |
||||
break; |
||||
} |
||||
|
||||
str = s; |
||||
sep = ','; |
||||
} |
||||
|
||||
str++; |
||||
|
||||
*attrp = attr; |
||||
|
||||
return str; |
||||
} |
||||
|
||||
/* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetVariable.28.29 */ |
||||
efi_status_t EFIAPI efi_get_variable(s16 *variable_name, |
||||
efi_guid_t *vendor, u32 *attributes, |
||||
unsigned long *data_size, void *data) |
||||
{ |
||||
char native_name[MAX_NATIVE_VAR_NAME + 1]; |
||||
efi_status_t ret; |
||||
unsigned long in_size; |
||||
const char *val, *s; |
||||
u32 attr; |
||||
|
||||
EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes, |
||||
data_size, data); |
||||
|
||||
if (!variable_name || !vendor || !data_size) |
||||
return EFI_EXIT(EFI_INVALID_PARAMETER); |
||||
|
||||
ret = efi_to_native(native_name, variable_name, vendor); |
||||
if (ret) |
||||
return EFI_EXIT(ret); |
||||
|
||||
debug("%s: get '%s'\n", __func__, native_name); |
||||
|
||||
val = env_get(native_name); |
||||
if (!val) |
||||
return EFI_EXIT(EFI_NOT_FOUND); |
||||
|
||||
val = parse_attr(val, &attr); |
||||
|
||||
in_size = *data_size; |
||||
|
||||
if ((s = prefix(val, "(blob)"))) { |
||||
unsigned len = strlen(s); |
||||
|
||||
/* two characters per byte: */ |
||||
len = DIV_ROUND_UP(len, 2); |
||||
*data_size = len; |
||||
|
||||
if (in_size < len) |
||||
return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
||||
|
||||
if (!data) |
||||
return EFI_EXIT(EFI_INVALID_PARAMETER); |
||||
|
||||
if (hex2mem(data, s, len * 2)) |
||||
return EFI_EXIT(EFI_DEVICE_ERROR); |
||||
|
||||
debug("%s: got value: \"%s\"\n", __func__, s); |
||||
} else if ((s = prefix(val, "(utf8)"))) { |
||||
unsigned len = strlen(s) + 1; |
||||
|
||||
*data_size = len; |
||||
|
||||
if (in_size < len) |
||||
return EFI_EXIT(EFI_BUFFER_TOO_SMALL); |
||||
|
||||
if (!data) |
||||
return EFI_EXIT(EFI_INVALID_PARAMETER); |
||||
|
||||
memcpy(data, s, len); |
||||
((char *)data)[len] = '\0'; |
||||
|
||||
debug("%s: got value: \"%s\"\n", __func__, (char *)data); |
||||
} else { |
||||
debug("%s: invalid value: '%s'\n", __func__, val); |
||||
return EFI_EXIT(EFI_DEVICE_ERROR); |
||||
} |
||||
|
||||
if (attributes) |
||||
*attributes = attr & EFI_VARIABLE_MASK; |
||||
|
||||
return EFI_EXIT(EFI_SUCCESS); |
||||
} |
||||
|
||||
/* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#GetNextVariableName.28.29 */ |
||||
efi_status_t EFIAPI efi_get_next_variable( |
||||
unsigned long *variable_name_size, |
||||
s16 *variable_name, efi_guid_t *vendor) |
||||
{ |
||||
EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor); |
||||
|
||||
return EFI_EXIT(EFI_DEVICE_ERROR); |
||||
} |
||||
|
||||
/* http://wiki.phoenix.com/wiki/index.php/EFI_RUNTIME_SERVICES#SetVariable.28.29 */ |
||||
efi_status_t EFIAPI efi_set_variable(s16 *variable_name, |
||||
efi_guid_t *vendor, u32 attributes, |
||||
unsigned long data_size, void *data) |
||||
{ |
||||
char native_name[MAX_NATIVE_VAR_NAME + 1]; |
||||
efi_status_t ret = EFI_SUCCESS; |
||||
char *val, *s; |
||||
u32 attr; |
||||
|
||||
EFI_ENTRY("\"%ls\" %pUl %x %lu %p", variable_name, vendor, attributes, |
||||
data_size, data); |
||||
|
||||
if (!variable_name || !vendor) |
||||
return EFI_EXIT(EFI_INVALID_PARAMETER); |
||||
|
||||
ret = efi_to_native(native_name, variable_name, vendor); |
||||
if (ret) |
||||
return EFI_EXIT(ret); |
||||
|
||||
#define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS) |
||||
|
||||
if ((data_size == 0) || !(attributes & ACCESS_ATTR)) { |
||||
/* delete the variable: */ |
||||
env_set(native_name, NULL); |
||||
return EFI_EXIT(EFI_SUCCESS); |
||||
} |
||||
|
||||
val = env_get(native_name); |
||||
if (val) { |
||||
parse_attr(val, &attr); |
||||
|
||||
if (attr & READ_ONLY) |
||||
return EFI_EXIT(EFI_WRITE_PROTECTED); |
||||
} |
||||
|
||||
val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1); |
||||
if (!val) |
||||
return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
||||
|
||||
s = val; |
||||
|
||||
/* store attributes: */ |
||||
attributes &= (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS); |
||||
s += sprintf(s, "{"); |
||||
while (attributes) { |
||||
u32 attr = 1 << (ffs(attributes) - 1); |
||||
|
||||
if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) |
||||
s += sprintf(s, "boot"); |
||||
else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) |
||||
s += sprintf(s, "run"); |
||||
|
||||
attributes &= ~attr; |
||||
if (attributes) |
||||
s += sprintf(s, ","); |
||||
} |
||||
s += sprintf(s, "}"); |
||||
|
||||
/* store payload: */ |
||||
s += sprintf(s, "(blob)"); |
||||
s = mem2hex(s, data, data_size); |
||||
*s = '\0'; |
||||
|
||||
debug("%s: setting: %s=%s\n", __func__, native_name, val); |
||||
|
||||
if (env_set(native_name, val)) |
||||
ret = EFI_DEVICE_ERROR; |
||||
|
||||
free(val); |
||||
|
||||
return EFI_EXIT(ret); |
||||
} |
@ -0,0 +1,7 @@ |
||||
config CMD_BOOTEFI_SELFTEST |
||||
bool "Allow booting an EFI efi_selftest" |
||||
depends on CMD_BOOTEFI |
||||
help |
||||
This adds an EFI test application to U-Boot that can be executed |
||||
with the 'bootefi selftest' command. It provides extended tests of |
||||
the EFI API implementation. |
@ -0,0 +1,26 @@ |
||||
: |
||||
# (C) Copyright 2017, Heinrich Schuchardt <xypron.glpk@gmx.de>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-2.0+
|
||||
#
|
||||
|
||||
# This file only gets included with CONFIG_EFI_LOADER set, so all
|
||||
# object inclusion implicitly depends on it
|
||||
|
||||
CFLAGS_efi_selftest.o := $(CFLAGS_EFI)
|
||||
CFLAGS_REMOVE_efi_selftest.o := $(CFLAGS_NON_EFI)
|
||||
CFLAGS_efi_selftest_console.o := $(CFLAGS_EFI)
|
||||
CFLAGS_REMOVE_efi_selftest_console.o := $(CFLAGS_NON_EFI)
|
||||
CFLAGS_efi_selftest_events.o := $(CFLAGS_EFI)
|
||||
CFLAGS_REMOVE_efi_selftest_events.o := $(CFLAGS_NON_EFI)
|
||||
CFLAGS_efi_selftest_exitbootservices.o := $(CFLAGS_EFI)
|
||||
CFLAGS_REMOVE_efi_selftest_exitbootservices.o := $(CFLAGS_NON_EFI)
|
||||
CFLAGS_efi_selftest_tpl.o := $(CFLAGS_EFI)
|
||||
CFLAGS_REMOVE_efi_selftest_tpl.o := $(CFLAGS_NON_EFI)
|
||||
|
||||
obj-$(CONFIG_CMD_BOOTEFI_SELFTEST) += \
|
||||
efi_selftest.o \ |
||||
efi_selftest_console.o \ |
||||
efi_selftest_events.o \ |
||||
efi_selftest_exitbootservices.o \ |
||||
efi_selftest_tpl.o |
@ -0,0 +1,219 @@ |
||||
/*
|
||||
* EFI efi_selftest |
||||
* |
||||
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <efi_selftest.h> |
||||
#include <vsprintf.h> |
||||
|
||||
static const struct efi_system_table *systable; |
||||
static const struct efi_boot_services *boottime; |
||||
static const struct efi_runtime_services *runtime; |
||||
static efi_handle_t handle; |
||||
static u16 reset_message[] = L"Selftest completed"; |
||||
|
||||
/*
|
||||
* Exit the boot services. |
||||
* |
||||
* The size of the memory map is determined. |
||||
* Pool memory is allocated to copy the memory map. |
||||
* The memory amp is copied and the map key is obtained. |
||||
* The map key is used to exit the boot services. |
||||
*/ |
||||
void efi_st_exit_boot_services(void) |
||||
{ |
||||
unsigned long map_size = 0; |
||||
unsigned long map_key; |
||||
unsigned long desc_size; |
||||
u32 desc_version; |
||||
efi_status_t ret; |
||||
struct efi_mem_desc *memory_map; |
||||
|
||||
ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size, |
||||
&desc_version); |
||||
if (ret != EFI_BUFFER_TOO_SMALL) { |
||||
efi_st_printf("ERROR: GetMemoryMap did not return " |
||||
"EFI_BUFFER_TOO_SMALL\n"); |
||||
return; |
||||
} |
||||
/* Allocate extra space for newly allocated memory */ |
||||
map_size += sizeof(struct efi_mem_desc); |
||||
ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size, |
||||
(void **)&memory_map); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_printf("ERROR: AllocatePool did not return " |
||||
"EFI_SUCCESS\n"); |
||||
return; |
||||
} |
||||
ret = boottime->get_memory_map(&map_size, memory_map, &map_key, |
||||
&desc_size, &desc_version); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_printf("ERROR: GetMemoryMap did not return " |
||||
"EFI_SUCCESS\n"); |
||||
return; |
||||
} |
||||
ret = boottime->exit_boot_services(handle, map_key); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_printf("ERROR: ExitBootServices did not return " |
||||
"EFI_SUCCESS\n"); |
||||
return; |
||||
} |
||||
efi_st_printf("\nBoot services terminated\n"); |
||||
} |
||||
|
||||
/*
|
||||
* Set up a test. |
||||
* |
||||
* @test the test to be executed |
||||
* @failures counter that will be incremented if a failure occurs |
||||
*/ |
||||
static int setup(struct efi_unit_test *test, unsigned int *failures) |
||||
{ |
||||
int ret; |
||||
|
||||
if (!test->setup) |
||||
return 0; |
||||
efi_st_printf("\nSetting up '%s'\n", test->name); |
||||
ret = test->setup(handle, systable); |
||||
if (ret) { |
||||
efi_st_printf("ERROR: Setting up '%s' failed\n", test->name); |
||||
++*failures; |
||||
} else { |
||||
efi_st_printf("Setting up '%s' succeeded\n", test->name); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
/*
|
||||
* Execute a test. |
||||
* |
||||
* @test the test to be executed |
||||
* @failures counter that will be incremented if a failure occurs |
||||
*/ |
||||
static int execute(struct efi_unit_test *test, unsigned int *failures) |
||||
{ |
||||
int ret; |
||||
|
||||
if (!test->execute) |
||||
return 0; |
||||
efi_st_printf("\nExecuting '%s'\n", test->name); |
||||
ret = test->execute(); |
||||
if (ret) { |
||||
efi_st_printf("ERROR: Executing '%s' failed\n", test->name); |
||||
++*failures; |
||||
} else { |
||||
efi_st_printf("Executing '%s' succeeded\n", test->name); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
/*
|
||||
* Tear down a test. |
||||
* |
||||
* @test the test to be torn down |
||||
* @failures counter that will be incremented if a failure occurs |
||||
*/ |
||||
static int teardown(struct efi_unit_test *test, unsigned int *failures) |
||||
{ |
||||
int ret; |
||||
|
||||
if (!test->teardown) |
||||
return 0; |
||||
efi_st_printf("\nTearing down '%s'\n", test->name); |
||||
ret = test->teardown(); |
||||
if (ret) { |
||||
efi_st_printf("ERROR: Tearing down '%s' failed\n", test->name); |
||||
++*failures; |
||||
} else { |
||||
efi_st_printf("Tearing down '%s' succeeded\n", test->name); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
/*
|
||||
* Execute selftest of the EFI API |
||||
* |
||||
* This is the main entry point of the EFI selftest application. |
||||
* |
||||
* All tests use a driver model and are run in three phases: |
||||
* setup, execute, teardown. |
||||
* |
||||
* A test may be setup and executed at boottime, |
||||
* it may be setup at boottime and executed at runtime, |
||||
* or it may be setup and executed at runtime. |
||||
* |
||||
* After executing all tests the system is reset. |
||||
* |
||||
* @image_handle: handle of the loaded EFI image |
||||
* @systab: EFI system table |
||||
*/ |
||||
efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle, |
||||
struct efi_system_table *systab) |
||||
{ |
||||
struct efi_unit_test *test; |
||||
unsigned int failures = 0; |
||||
|
||||
systable = systab; |
||||
boottime = systable->boottime; |
||||
runtime = systable->runtime; |
||||
handle = image_handle; |
||||
con_out = systable->con_out; |
||||
con_in = systable->con_in; |
||||
|
||||
efi_st_printf("\nTesting EFI API implementation\n"); |
||||
|
||||
efi_st_printf("\nNumber of tests to execute: %u\n", |
||||
ll_entry_count(struct efi_unit_test, efi_unit_test)); |
||||
|
||||
/* Execute boottime tests */ |
||||
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); |
||||
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { |
||||
if (test->phase == EFI_EXECUTE_BEFORE_BOOTTIME_EXIT) { |
||||
setup(test, &failures); |
||||
execute(test, &failures); |
||||
teardown(test, &failures); |
||||
} |
||||
} |
||||
|
||||
/* Execute mixed tests */ |
||||
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); |
||||
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { |
||||
if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT) |
||||
setup(test, &failures); |
||||
} |
||||
|
||||
efi_st_exit_boot_services(); |
||||
|
||||
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); |
||||
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { |
||||
if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT) { |
||||
execute(test, &failures); |
||||
teardown(test, &failures); |
||||
} |
||||
} |
||||
|
||||
/* Execute runtime tests */ |
||||
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); |
||||
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) { |
||||
if (test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT) { |
||||
setup(test, &failures); |
||||
execute(test, &failures); |
||||
teardown(test, &failures); |
||||
} |
||||
} |
||||
|
||||
/* Give feedback */ |
||||
efi_st_printf("\nSummary: %u failures\n\n", failures); |
||||
|
||||
/* Reset system */ |
||||
efi_st_printf("Preparing for reset. Press any key.\n"); |
||||
efi_st_get_key(); |
||||
runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY, |
||||
sizeof(reset_message), reset_message); |
||||
efi_st_printf("\nERROR: reset failed.\n"); |
||||
|
||||
return EFI_UNSUPPORTED; |
||||
} |
@ -0,0 +1,187 @@ |
||||
/*
|
||||
* EFI efi_selftest |
||||
* |
||||
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <efi_selftest.h> |
||||
#include <vsprintf.h> |
||||
|
||||
struct efi_simple_text_output_protocol *con_out; |
||||
struct efi_simple_input_interface *con_in; |
||||
|
||||
/*
|
||||
* Print a pointer to an u16 string |
||||
* |
||||
* @pointer: pointer |
||||
* @buf: pointer to buffer address |
||||
* on return position of terminating zero word |
||||
*/ |
||||
static void pointer(void *pointer, u16 **buf) |
||||
{ |
||||
int i; |
||||
u16 c; |
||||
uintptr_t p = (uintptr_t)pointer; |
||||
u16 *pos = *buf; |
||||
|
||||
for (i = 8 * sizeof(p) - 4; i >= 0; i -= 4) { |
||||
c = (p >> i) & 0x0f; |
||||
c += '0'; |
||||
if (c > '9') |
||||
c += 'a' - '9' - 1; |
||||
*pos++ = c; |
||||
} |
||||
*pos = 0; |
||||
*buf = pos; |
||||
} |
||||
|
||||
/*
|
||||
* Print an unsigned 32bit value as decimal number to an u16 string |
||||
* |
||||
* @value: value to be printed |
||||
* @buf: pointer to buffer address |
||||
* on return position of terminating zero word |
||||
*/ |
||||
static void uint2dec(u32 value, u16 **buf) |
||||
{ |
||||
u16 *pos = *buf; |
||||
int i; |
||||
u16 c; |
||||
u64 f; |
||||
|
||||
/*
|
||||
* Increment by .5 and multiply with |
||||
* (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC |
||||
* to move the first digit to bit 60-63. |
||||
*/ |
||||
f = 0x225C17D0; |
||||
f += (0x9B5A52DULL * value) >> 28; |
||||
f += 0x44B82FA0ULL * value; |
||||
|
||||
for (i = 0; i < 10; ++i) { |
||||
/* Write current digit */ |
||||
c = f >> 60; |
||||
if (c || pos != *buf) |
||||
*pos++ = c + '0'; |
||||
/* Eliminate current digit */ |
||||
f &= 0xfffffffffffffff; |
||||
/* Get next digit */ |
||||
f *= 0xaULL; |
||||
} |
||||
if (pos == *buf) |
||||
*pos++ = '0'; |
||||
*pos = 0; |
||||
*buf = pos; |
||||
} |
||||
|
||||
/*
|
||||
* Print a signed 32bit value as decimal number to an u16 string |
||||
* |
||||
* @value: value to be printed |
||||
* @buf: pointer to buffer address |
||||
* on return position of terminating zero word |
||||
*/ |
||||
static void int2dec(s32 value, u16 **buf) |
||||
{ |
||||
u32 u; |
||||
u16 *pos = *buf; |
||||
|
||||
if (value < 0) { |
||||
*pos++ = '-'; |
||||
u = -value; |
||||
} else { |
||||
u = value; |
||||
} |
||||
uint2dec(u, &pos); |
||||
*buf = pos; |
||||
} |
||||
|
||||
/*
|
||||
* Print a formatted string to the EFI console |
||||
* |
||||
* @fmt: format string |
||||
* @...: optional arguments |
||||
*/ |
||||
void efi_st_printf(const char *fmt, ...) |
||||
{ |
||||
va_list args; |
||||
u16 buf[160]; |
||||
const char *c; |
||||
u16 *pos = buf; |
||||
const char *s; |
||||
|
||||
va_start(args, fmt); |
||||
|
||||
c = fmt; |
||||
for (; *c; ++c) { |
||||
switch (*c) { |
||||
case '\\': |
||||
++c; |
||||
switch (*c) { |
||||
case '\0': |
||||
--c; |
||||
break; |
||||
case 'n': |
||||
*pos++ = '\n'; |
||||
break; |
||||
case 'r': |
||||
*pos++ = '\r'; |
||||
break; |
||||
case 't': |
||||
*pos++ = '\t'; |
||||
break; |
||||
default: |
||||
*pos++ = *c; |
||||
} |
||||
break; |
||||
case '%': |
||||
++c; |
||||
switch (*c) { |
||||
case '\0': |
||||
--c; |
||||
break; |
||||
case 'd': |
||||
int2dec(va_arg(args, s32), &pos); |
||||
break; |
||||
case 'p': |
||||
pointer(va_arg(args, void*), &pos); |
||||
break; |
||||
case 's': |
||||
s = va_arg(args, const char *); |
||||
for (; *s; ++s) |
||||
*pos++ = *s; |
||||
break; |
||||
case 'u': |
||||
uint2dec(va_arg(args, u32), &pos); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
break; |
||||
default: |
||||
*pos++ = *c; |
||||
} |
||||
} |
||||
va_end(args); |
||||
*pos = 0; |
||||
con_out->output_string(con_out, buf); |
||||
} |
||||
|
||||
/*
|
||||
* Reads an Unicode character from the input device. |
||||
* |
||||
* @return: Unicode character |
||||
*/ |
||||
u16 efi_st_get_key(void) |
||||
{ |
||||
struct efi_input_key input_key; |
||||
efi_status_t ret; |
||||
|
||||
/* Wait for next key */ |
||||
do { |
||||
ret = con_in->read_key_stroke(con_in, &input_key); |
||||
} while (ret == EFI_NOT_READY); |
||||
return input_key.unicode_char; |
||||
} |
@ -0,0 +1,195 @@ |
||||
/*
|
||||
* efi_selftest_events |
||||
* |
||||
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
* |
||||
* This unit test uses timer events to check the implementation |
||||
* of the following boottime services: |
||||
* CreateEvent, CloseEvent, WaitForEvent, CheckEvent, SetTimer. |
||||
*/ |
||||
|
||||
#include <efi_selftest.h> |
||||
|
||||
static struct efi_event *event_notify; |
||||
static struct efi_event *event_wait; |
||||
static unsigned int counter; |
||||
static struct efi_boot_services *boottime; |
||||
|
||||
/*
|
||||
* Notification function, increments a counter. |
||||
* |
||||
* @event notified event |
||||
* @context pointer to the counter |
||||
*/ |
||||
static void EFIAPI notify(struct efi_event *event, void *context) |
||||
{ |
||||
if (!context) |
||||
return; |
||||
++*(unsigned int *)context; |
||||
} |
||||
|
||||
/*
|
||||
* Setup unit test. |
||||
* |
||||
* Create two timer events. |
||||
* One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT. |
||||
* |
||||
* @handle: handle of the loaded image |
||||
* @systable: system table |
||||
*/ |
||||
static int setup(const efi_handle_t handle, |
||||
const struct efi_system_table *systable) |
||||
{ |
||||
efi_status_t ret; |
||||
|
||||
boottime = systable->boottime; |
||||
|
||||
ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, |
||||
TPL_CALLBACK, notify, (void *)&counter, |
||||
&event_notify); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not create event\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT, |
||||
TPL_CALLBACK, notify, NULL, &event_wait); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not create event\n"); |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
* Tear down unit test. |
||||
* |
||||
* Close the events created in setup. |
||||
*/ |
||||
static int teardown(void) |
||||
{ |
||||
efi_status_t ret; |
||||
|
||||
if (event_notify) { |
||||
ret = boottime->close_event(event_notify); |
||||
event_notify = NULL; |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not close event\n"); |
||||
return 1; |
||||
} |
||||
} |
||||
if (event_wait) { |
||||
ret = boottime->close_event(event_wait); |
||||
event_wait = NULL; |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not close event\n"); |
||||
return 1; |
||||
} |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
* Execute unit test. |
||||
* |
||||
* Run a 10 ms periodic timer and check that it is called 10 times |
||||
* while waiting for 100 ms single shot timer. |
||||
* |
||||
* Run a 100 ms single shot timer and check that it is called once |
||||
* while waiting for 100 ms periodic timer for two periods. |
||||
*/ |
||||
static int execute(void) |
||||
{ |
||||
unsigned long index; |
||||
efi_status_t ret; |
||||
|
||||
/* Set 10 ms timer */ |
||||
counter = 0; |
||||
ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
/* Set 100 ms timer */ |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
|
||||
index = 5; |
||||
ret = boottime->wait_for_event(1, &event_wait, &index); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not wait for event\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->check_event(event_wait); |
||||
if (ret != EFI_NOT_READY) { |
||||
efi_st_error("Signaled state was not cleared.\n"); |
||||
efi_st_printf("ret = %u\n", (unsigned int)ret); |
||||
return 1; |
||||
} |
||||
if (index != 0) { |
||||
efi_st_error("WaitForEvent returned wrong index\n"); |
||||
return 1; |
||||
} |
||||
efi_st_printf("Counter periodic: %u\n", counter); |
||||
if (counter < 8 || counter > 12) { |
||||
efi_st_error("Incorrect timing of events\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0); |
||||
if (index != 0) { |
||||
efi_st_error("Could not cancel timer\n"); |
||||
return 1; |
||||
} |
||||
/* Set 10 ms timer */ |
||||
counter = 0; |
||||
ret = boottime->set_timer(event_notify, EFI_TIMER_RELATIVE, 100000); |
||||
if (index != 0) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
/* Set 100 ms timer */ |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000); |
||||
if (index != 0) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->wait_for_event(1, &event_wait, &index); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not wait for event\n"); |
||||
return 1; |
||||
} |
||||
efi_st_printf("Counter single shot: %u\n", counter); |
||||
if (counter != 1) { |
||||
efi_st_error("Single shot timer failed\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->wait_for_event(1, &event_wait, &index); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not wait for event\n"); |
||||
return 1; |
||||
} |
||||
efi_st_printf("Stopped counter: %u\n", counter); |
||||
if (counter != 1) { |
||||
efi_st_error("Stopped timer fired\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0); |
||||
if (index != 0) { |
||||
efi_st_error("Could not cancel timer\n"); |
||||
return 1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
EFI_UNIT_TEST(events) = { |
||||
.name = "event services", |
||||
.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
||||
.setup = setup, |
||||
.execute = execute, |
||||
.teardown = teardown, |
||||
}; |
@ -0,0 +1,106 @@ |
||||
/*
|
||||
* efi_selftest_events |
||||
* |
||||
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
* |
||||
* This unit test checks that the notification function of an |
||||
* EVT_SIGNAL_EXIT_BOOT_SERVICES event is called exactly once. |
||||
*/ |
||||
|
||||
#include <efi_selftest.h> |
||||
|
||||
static struct efi_boot_services *boottime; |
||||
static struct efi_event *event_notify; |
||||
static unsigned int counter; |
||||
|
||||
/*
|
||||
* Notification function, increments a counter. |
||||
* |
||||
* @event notified event |
||||
* @context pointer to the counter |
||||
*/ |
||||
static void EFIAPI notify(struct efi_event *event, void *context) |
||||
{ |
||||
if (!context) |
||||
return; |
||||
++*(unsigned int *)context; |
||||
} |
||||
|
||||
/*
|
||||
* Setup unit test. |
||||
* |
||||
* Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event. |
||||
* |
||||
* @handle: handle of the loaded image |
||||
* @systable: system table |
||||
*/ |
||||
static int setup(const efi_handle_t handle, |
||||
const struct efi_system_table *systable) |
||||
{ |
||||
efi_status_t ret; |
||||
|
||||
boottime = systable->boottime; |
||||
|
||||
counter = 0; |
||||
ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, |
||||
TPL_CALLBACK, notify, (void *)&counter, |
||||
&event_notify); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not create event\n"); |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
* Tear down unit test. |
||||
* |
||||
* Close the event created in setup. |
||||
*/ |
||||
static int teardown(void) |
||||
{ |
||||
efi_status_t ret; |
||||
|
||||
if (event_notify) { |
||||
ret = boottime->close_event(event_notify); |
||||
event_notify = NULL; |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not close event\n"); |
||||
return 1; |
||||
} |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
* Execute unit test. |
||||
* |
||||
* Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES |
||||
* event has been called. |
||||
* |
||||
* Call ExitBootServices again and check that the notification function is |
||||
* not called again. |
||||
*/ |
||||
static int execute(void) |
||||
{ |
||||
if (counter != 1) { |
||||
efi_st_error("ExitBootServices was not notified"); |
||||
return 1; |
||||
} |
||||
efi_st_exit_boot_services(); |
||||
if (counter != 1) { |
||||
efi_st_error("ExitBootServices was notified twice"); |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
EFI_UNIT_TEST(exitbootservices) = { |
||||
.name = "ExitBootServices", |
||||
.phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
||||
.setup = setup, |
||||
.execute = execute, |
||||
.teardown = teardown, |
||||
}; |
@ -0,0 +1,214 @@ |
||||
/*
|
||||
* efi_selftest_events |
||||
* |
||||
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
* |
||||
* This unit test uses timer events to check the handling of |
||||
* task priority levels. |
||||
*/ |
||||
|
||||
#include <efi_selftest.h> |
||||
|
||||
static struct efi_event *event_notify; |
||||
static struct efi_event *event_wait; |
||||
static unsigned int counter; |
||||
static struct efi_boot_services *boottime; |
||||
|
||||
/*
|
||||
* Notification function, increments a counter. |
||||
* |
||||
* @event notified event |
||||
* @context pointer to the counter |
||||
*/ |
||||
static void EFIAPI notify(struct efi_event *event, void *context) |
||||
{ |
||||
if (!context) |
||||
return; |
||||
++*(unsigned int *)context; |
||||
} |
||||
|
||||
/*
|
||||
* Setup unit test. |
||||
* |
||||
* Create two timer events. |
||||
* One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT. |
||||
* |
||||
* @handle: handle of the loaded image |
||||
* @systable: system table |
||||
*/ |
||||
static int setup(const efi_handle_t handle, |
||||
const struct efi_system_table *systable) |
||||
{ |
||||
efi_status_t ret; |
||||
|
||||
boottime = systable->boottime; |
||||
|
||||
ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, |
||||
TPL_CALLBACK, notify, (void *)&counter, |
||||
&event_notify); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not create event\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT, |
||||
TPL_HIGH_LEVEL, notify, NULL, &event_wait); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not create event\n"); |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
* Tear down unit test. |
||||
* |
||||
* Close the events created in setup. |
||||
*/ |
||||
static int teardown(void) |
||||
{ |
||||
efi_status_t ret; |
||||
|
||||
if (event_notify) { |
||||
ret = boottime->close_event(event_notify); |
||||
event_notify = NULL; |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not close event\n"); |
||||
return 1; |
||||
} |
||||
} |
||||
if (event_wait) { |
||||
ret = boottime->close_event(event_wait); |
||||
event_wait = NULL; |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("could not close event\n"); |
||||
return 1; |
||||
} |
||||
} |
||||
boottime->restore_tpl(TPL_APPLICATION); |
||||
return 0; |
||||
} |
||||
|
||||
/*
|
||||
* Execute unit test. |
||||
* |
||||
* Run a 10 ms periodic timer and check that it is called 10 times |
||||
* while waiting for 100 ms single shot timer. |
||||
* |
||||
* Raise the TPL level to the level of the 10 ms timer and observe |
||||
* that the notification function is not called again. |
||||
* |
||||
* Lower the TPL level and check that the queued notification |
||||
* function is called. |
||||
*/ |
||||
static int execute(void) |
||||
{ |
||||
unsigned long index; |
||||
efi_status_t ret; |
||||
UINTN old_tpl; |
||||
|
||||
/* Set 10 ms timer */ |
||||
counter = 0; |
||||
ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
/* Set 100 ms timer */ |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
index = 5; |
||||
ret = boottime->wait_for_event(1, &event_wait, &index); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not wait for event\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->check_event(event_wait); |
||||
if (ret != EFI_NOT_READY) { |
||||
efi_st_error("Signaled state was not cleared.\n"); |
||||
efi_st_printf("ret = %u\n", (unsigned int)ret); |
||||
return 1; |
||||
} |
||||
if (index != 0) { |
||||
efi_st_error("WaitForEvent returned wrong index\n"); |
||||
return 1; |
||||
} |
||||
efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter); |
||||
if (counter < 8 || counter > 12) { |
||||
efi_st_error("Incorrect timing of events\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0); |
||||
if (index != 0) { |
||||
efi_st_error("Could not cancel timer\n"); |
||||
return 1; |
||||
} |
||||
/* Raise TPL level */ |
||||
old_tpl = boottime->raise_tpl(TPL_CALLBACK); |
||||
if (old_tpl != TPL_APPLICATION) { |
||||
efi_st_error("Initial TPL level was not TPL_APPLICATION"); |
||||
return 1; |
||||
} |
||||
/* Set 10 ms timer */ |
||||
counter = 0; |
||||
ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); |
||||
if (index != 0) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
/* Set 100 ms timer */ |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
do { |
||||
ret = boottime->check_event(event_wait); |
||||
} while (ret == EFI_NOT_READY); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not check event\n"); |
||||
return 1; |
||||
} |
||||
efi_st_printf("Counter with TPL level TPL_CALLBACK: %u\n", counter); |
||||
if (counter != 0) { |
||||
efi_st_error("Suppressed timer fired\n"); |
||||
return 1; |
||||
} |
||||
/* Set 1 ms timer */ |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not set timer\n"); |
||||
return 1; |
||||
} |
||||
/* Restore the old TPL level */ |
||||
boottime->restore_tpl(TPL_APPLICATION); |
||||
ret = boottime->wait_for_event(1, &event_wait, &index); |
||||
if (ret != EFI_SUCCESS) { |
||||
efi_st_error("Could not wait for event\n"); |
||||
return 1; |
||||
} |
||||
efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter); |
||||
if (counter < 1) { |
||||
efi_st_error("Queued timer event did not fire\n"); |
||||
return 1; |
||||
} |
||||
ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0); |
||||
if (index != 0) { |
||||
efi_st_error("Could not cancel timer\n"); |
||||
return 1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
EFI_UNIT_TEST(tpl) = { |
||||
.name = "task priority levels", |
||||
.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
||||
.setup = setup, |
||||
.execute = execute, |
||||
.teardown = teardown, |
||||
}; |
@ -0,0 +1,25 @@ |
||||
# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
||||
# Copyright (c) 2017, Heinrich Schuchardt <xypron.glpk@gmx.de> |
||||
# |
||||
# SPDX-License-Identifier: GPL-2.0 |
||||
|
||||
# Test efi API implementation |
||||
|
||||
import pytest |
||||
import u_boot_utils |
||||
|
||||
@pytest.mark.buildconfigspec('cmd_bootefi_selftest') |
||||
def test_efi_selftest(u_boot_console): |
||||
""" |
||||
Run bootefi selftest |
||||
""" |
||||
|
||||
u_boot_console.run_command(cmd='bootefi selftest', wait_for_prompt=False) |
||||
m = u_boot_console.p.expect(['Summary: 0 failures', 'Press any key']) |
||||
if m != 0: |
||||
raise Exception('Failures occured during the EFI selftest') |
||||
u_boot_console.run_command(cmd='', wait_for_echo=False, wait_for_prompt=False); |
||||
m = u_boot_console.p.expect(['resetting', 'U-Boot']) |
||||
if m != 0: |
||||
raise Exception('Reset failed during the EFI selftest') |
||||
u_boot_console.restart_uboot(); |
Loading…
Reference in new issue