2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2018-03-03 14:29:04 +00:00
|
|
|
/*
|
2022-02-05 07:45:55 +00:00
|
|
|
* efi_selftest_fdt
|
2018-03-03 14:29:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
|
2022-02-05 07:45:55 +00:00
|
|
|
* Copyright (c) 2022 Ventana Micro Systems Inc
|
2018-03-03 14:29:04 +00:00
|
|
|
*
|
2022-02-05 07:45:55 +00:00
|
|
|
* Check the device tree, test the RISCV_EFI_BOOT_PROTOCOL.
|
2018-03-03 14:29:04 +00:00
|
|
|
*/
|
|
|
|
|
2022-02-05 07:45:55 +00:00
|
|
|
#include <efi_riscv.h>
|
2018-03-03 14:29:04 +00:00
|
|
|
#include <efi_selftest.h>
|
2018-03-12 18:52:25 +00:00
|
|
|
#include <linux/libfdt.h>
|
2018-03-03 14:29:04 +00:00
|
|
|
|
2019-04-20 11:33:55 +00:00
|
|
|
static const struct efi_system_table *systemtab;
|
|
|
|
static const struct efi_boot_services *boottime;
|
2018-03-03 14:29:04 +00:00
|
|
|
static const char *fdt;
|
|
|
|
|
2018-09-27 18:44:40 +00:00
|
|
|
/* This should be sufficient for */
|
2018-03-03 14:29:04 +00:00
|
|
|
#define BUFFERSIZE 0x100000
|
|
|
|
|
2019-04-20 11:33:55 +00:00
|
|
|
static const efi_guid_t fdt_guid = EFI_FDT_GUID;
|
|
|
|
static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
|
2022-02-05 07:45:55 +00:00
|
|
|
static const efi_guid_t riscv_efi_boot_protocol_guid =
|
|
|
|
RISCV_EFI_BOOT_PROTOCOL_GUID;
|
2018-03-03 14:29:04 +00:00
|
|
|
|
2021-11-25 17:55:09 +00:00
|
|
|
/**
|
|
|
|
* f2h() - convert FDT value to host endianness.
|
2018-03-03 14:29:04 +00:00
|
|
|
*
|
2021-11-25 17:55:09 +00:00
|
|
|
* UEFI code is always low endian. The FDT is big endian.
|
|
|
|
*
|
|
|
|
* @val: FDT value
|
|
|
|
* Return: converted value
|
2018-03-03 14:29:04 +00:00
|
|
|
*/
|
|
|
|
static uint32_t f2h(fdt32_t val)
|
|
|
|
{
|
|
|
|
char *buf = (char *)&val;
|
|
|
|
char i;
|
|
|
|
|
|
|
|
/* Swap the bytes */
|
|
|
|
i = buf[0]; buf[0] = buf[3]; buf[3] = i;
|
|
|
|
i = buf[1]; buf[1] = buf[2]; buf[2] = i;
|
2021-11-25 17:55:09 +00:00
|
|
|
|
|
|
|
return val;
|
2018-03-03 14:29:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 05:33:29 +00:00
|
|
|
/**
|
|
|
|
* get_property() - return value of a property of an FDT node
|
2018-03-03 14:29:04 +00:00
|
|
|
*
|
2020-09-17 05:33:29 +00:00
|
|
|
* A property of the root node or one of its direct children can be
|
|
|
|
* retrieved.
|
|
|
|
*
|
|
|
|
* @property name of the property
|
|
|
|
* @node name of the node or NULL for root node
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: value of the property
|
2018-03-03 14:29:04 +00:00
|
|
|
*/
|
2020-09-17 05:33:29 +00:00
|
|
|
static char *get_property(const u16 *property, const u16 *node)
|
2018-03-03 14:29:04 +00:00
|
|
|
{
|
|
|
|
struct fdt_header *header = (struct fdt_header *)fdt;
|
2020-09-17 05:33:29 +00:00
|
|
|
const fdt32_t *end;
|
2018-03-03 14:29:04 +00:00
|
|
|
const fdt32_t *pos;
|
|
|
|
const char *strings;
|
2020-09-17 05:33:29 +00:00
|
|
|
size_t level = 0;
|
|
|
|
const char *nodelabel = NULL;
|
2018-03-03 14:29:04 +00:00
|
|
|
|
2020-09-17 05:33:29 +00:00
|
|
|
if (!header) {
|
|
|
|
efi_st_error("Missing device tree\n");
|
2018-03-03 14:29:04 +00:00
|
|
|
return NULL;
|
2020-09-17 05:33:29 +00:00
|
|
|
}
|
2018-03-03 14:29:04 +00:00
|
|
|
|
|
|
|
if (f2h(header->magic) != FDT_MAGIC) {
|
2020-09-17 05:33:29 +00:00
|
|
|
efi_st_error("Wrong device tree magic\n");
|
2018-03-03 14:29:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
|
2020-09-17 05:33:29 +00:00
|
|
|
end = &pos[f2h(header->totalsize) >> 2];
|
2018-03-03 14:29:04 +00:00
|
|
|
strings = fdt + f2h(header->off_dt_strings);
|
|
|
|
|
2020-09-17 05:33:29 +00:00
|
|
|
for (; pos < end;) {
|
2018-03-03 14:29:04 +00:00
|
|
|
switch (f2h(pos[0])) {
|
|
|
|
case FDT_BEGIN_NODE: {
|
2020-09-17 05:33:29 +00:00
|
|
|
const char *c = (char *)&pos[1];
|
2018-03-03 14:29:04 +00:00
|
|
|
size_t i;
|
|
|
|
|
2020-09-17 05:33:29 +00:00
|
|
|
if (level == 1)
|
|
|
|
nodelabel = c;
|
|
|
|
++level;
|
2018-03-03 14:29:04 +00:00
|
|
|
for (i = 0; c[i]; ++i)
|
|
|
|
;
|
|
|
|
pos = &pos[2 + (i >> 2)];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FDT_PROP: {
|
|
|
|
struct fdt_property *prop = (struct fdt_property *)pos;
|
|
|
|
const char *label = &strings[f2h(prop->nameoff)];
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
/* Check if this is the property to be returned */
|
2020-09-17 05:33:29 +00:00
|
|
|
if (!efi_st_strcmp_16_8(property, label) &&
|
|
|
|
((level == 1 && !node) ||
|
|
|
|
(level == 2 && node &&
|
|
|
|
!efi_st_strcmp_16_8(node, nodelabel)))) {
|
2018-03-03 14:29:04 +00:00
|
|
|
char *str;
|
|
|
|
efi_uintn_t len = f2h(prop->len);
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return NULL;
|
|
|
|
/*
|
|
|
|
* The string might not be 0 terminated.
|
|
|
|
* It is safer to make a copy.
|
|
|
|
*/
|
|
|
|
ret = boottime->allocate_pool(
|
|
|
|
EFI_LOADER_DATA, len + 1,
|
|
|
|
(void **)&str);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2020-09-17 05:33:29 +00:00
|
|
|
efi_st_error("AllocatePool failed\n");
|
2018-03-03 14:29:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
boottime->copy_mem(str, &pos[3], len);
|
|
|
|
str[len] = 0;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FDT_NOP:
|
2020-09-17 05:33:29 +00:00
|
|
|
++pos;
|
|
|
|
break;
|
|
|
|
case FDT_END_NODE:
|
|
|
|
--level;
|
|
|
|
++pos;
|
2018-03-03 14:29:04 +00:00
|
|
|
break;
|
2020-09-17 05:33:29 +00:00
|
|
|
case FDT_END:
|
|
|
|
return NULL;
|
2018-03-03 14:29:04 +00:00
|
|
|
default:
|
2020-09-17 05:33:29 +00:00
|
|
|
efi_st_error("Invalid device tree token\n");
|
2018-03-03 14:29:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 05:33:29 +00:00
|
|
|
efi_st_error("Missing FDT_END token\n");
|
|
|
|
return NULL;
|
2018-03-03 14:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup unit test.
|
|
|
|
*
|
|
|
|
* @handle: handle of the loaded image
|
|
|
|
* @systable: system table
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2018-03-03 14:29:04 +00:00
|
|
|
*/
|
|
|
|
static int setup(const efi_handle_t img_handle,
|
|
|
|
const struct efi_system_table *systable)
|
|
|
|
{
|
2019-04-20 11:33:55 +00:00
|
|
|
void *acpi;
|
2018-03-03 14:29:04 +00:00
|
|
|
|
2019-04-20 11:33:55 +00:00
|
|
|
systemtab = systable;
|
2018-03-03 14:29:04 +00:00
|
|
|
boottime = systable->boottime;
|
|
|
|
|
2019-04-20 11:33:55 +00:00
|
|
|
acpi = efi_st_get_config_table(&acpi_guid);
|
|
|
|
fdt = efi_st_get_config_table(&fdt_guid);
|
|
|
|
|
2018-03-03 14:29:04 +00:00
|
|
|
if (!fdt) {
|
|
|
|
efi_st_error("Missing device tree\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
2019-04-20 11:33:55 +00:00
|
|
|
if (acpi) {
|
|
|
|
efi_st_error("Found ACPI table and device tree\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
2018-03-03 14:29:04 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-02-05 07:45:55 +00:00
|
|
|
__maybe_unused static efi_status_t get_boot_hartid(efi_uintn_t *efi_hartid)
|
|
|
|
{
|
|
|
|
efi_status_t ret;
|
|
|
|
struct riscv_efi_boot_protocol *prot;
|
|
|
|
|
|
|
|
/* Get RISC-V boot protocol */
|
|
|
|
ret = boottime->locate_protocol(&riscv_efi_boot_protocol_guid, NULL,
|
|
|
|
(void **)&prot);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("RISC-V Boot Protocol not available\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get boot hart ID from EFI protocol */
|
|
|
|
ret = prot->get_boot_hartid(prot, efi_hartid);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Could not retrieve boot hart ID\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_ST_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-03 14:29:04 +00:00
|
|
|
/*
|
|
|
|
* Execute unit test.
|
|
|
|
*
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2018-03-03 14:29:04 +00:00
|
|
|
*/
|
|
|
|
static int execute(void)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
efi_status_t ret;
|
|
|
|
|
2022-01-23 19:55:12 +00:00
|
|
|
str = get_property(u"compatible", NULL);
|
2018-03-03 14:29:04 +00:00
|
|
|
if (str) {
|
|
|
|
efi_st_printf("compatible: %s\n", str);
|
|
|
|
ret = boottime->free_pool(str);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("FreePool failed\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-17 05:33:29 +00:00
|
|
|
efi_st_error("Missing property 'compatible'\n");
|
2018-03-03 14:29:04 +00:00
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
2022-01-23 19:55:12 +00:00
|
|
|
str = get_property(u"serial-number", NULL);
|
2018-03-03 14:29:04 +00:00
|
|
|
if (str) {
|
|
|
|
efi_st_printf("serial-number: %s\n", str);
|
|
|
|
ret = boottime->free_pool(str);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("FreePool failed\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 05:33:29 +00:00
|
|
|
if (IS_ENABLED(CONFIG_RISCV)) {
|
2022-02-05 07:45:55 +00:00
|
|
|
u32 fdt_hartid;
|
|
|
|
|
|
|
|
str = get_property(u"boot-hartid", u"chosen");
|
|
|
|
if (!str) {
|
|
|
|
efi_st_error("boot-hartid missing in devicetree\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
fdt_hartid = f2h(*(fdt32_t *)str);
|
|
|
|
efi_st_printf("boot-hartid: %u\n", fdt_hartid);
|
|
|
|
|
|
|
|
ret = boottime->free_pool(str);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("FreePool failed\n");
|
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
|
|
|
|
efi_uintn_t efi_hartid;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = get_boot_hartid(&efi_hartid);
|
|
|
|
if (r != EFI_ST_SUCCESS)
|
|
|
|
return r;
|
|
|
|
/* Boot hart ID should be same */
|
|
|
|
if (efi_hartid != fdt_hartid) {
|
|
|
|
efi_st_error("boot-hartid differs: prot 0x%p, DT 0x%.8x\n",
|
|
|
|
(void *)(uintptr_t)efi_hartid,
|
|
|
|
fdt_hartid);
|
2020-09-17 05:33:29 +00:00
|
|
|
return EFI_ST_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 14:29:04 +00:00
|
|
|
|
|
|
|
return EFI_ST_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
EFI_UNIT_TEST(fdt) = {
|
|
|
|
.name = "device tree",
|
|
|
|
.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
|
|
|
|
.setup = setup,
|
|
|
|
.execute = execute,
|
|
|
|
};
|