1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * Test the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.
9 * The following services are tested:
10 * OutputString, TestString, SetAttribute.
13 #include <efi_selftest.h>
14 #include <linux/libfdt.h>
16 static const struct efi_system_table *systemtab;
17 static const struct efi_boot_services *boottime;
18 static const char *fdt;
20 /* This should be sufficient for */
21 #define BUFFERSIZE 0x100000
23 static const efi_guid_t fdt_guid = EFI_FDT_GUID;
24 static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
27 * f2h() - convert FDT value to host endianness.
29 * UEFI code is always low endian. The FDT is big endian.
32 * Return: converted value
34 static uint32_t f2h(fdt32_t val)
36 char *buf = (char *)&val;
40 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
41 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
47 * get_property() - return value of a property of an FDT node
49 * A property of the root node or one of its direct children can be
52 * @property name of the property
53 * @node name of the node or NULL for root node
54 * @return value of the property
56 static char *get_property(const u16 *property, const u16 *node)
58 struct fdt_header *header = (struct fdt_header *)fdt;
63 const char *nodelabel = NULL;
66 efi_st_error("Missing device tree\n");
70 if (f2h(header->magic) != FDT_MAGIC) {
71 efi_st_error("Wrong device tree magic\n");
75 pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
76 end = &pos[f2h(header->totalsize) >> 2];
77 strings = fdt + f2h(header->off_dt_strings);
80 switch (f2h(pos[0])) {
81 case FDT_BEGIN_NODE: {
82 const char *c = (char *)&pos[1];
88 for (i = 0; c[i]; ++i)
90 pos = &pos[2 + (i >> 2)];
94 struct fdt_property *prop = (struct fdt_property *)pos;
95 const char *label = &strings[f2h(prop->nameoff)];
98 /* Check if this is the property to be returned */
99 if (!efi_st_strcmp_16_8(property, label) &&
100 ((level == 1 && !node) ||
101 (level == 2 && node &&
102 !efi_st_strcmp_16_8(node, nodelabel)))) {
104 efi_uintn_t len = f2h(prop->len);
109 * The string might not be 0 terminated.
110 * It is safer to make a copy.
112 ret = boottime->allocate_pool(
113 EFI_LOADER_DATA, len + 1,
115 if (ret != EFI_SUCCESS) {
116 efi_st_error("AllocatePool failed\n");
119 boottime->copy_mem(str, &pos[3], len);
125 pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
138 efi_st_error("Invalid device tree token\n");
142 efi_st_error("Missing FDT_END token\n");
147 * efi_st_get_config_table() - get configuration table
149 * @guid: GUID of the configuration table
150 * Return: pointer to configuration table or NULL
152 static void *efi_st_get_config_table(const efi_guid_t *guid)
156 for (i = 0; i < systab.nr_tables; i++) {
157 if (!guidcmp(guid, &systemtab->tables[i].guid))
158 return systemtab->tables[i].table;
166 * @handle: handle of the loaded image
167 * @systable: system table
168 * @return: EFI_ST_SUCCESS for success
170 static int setup(const efi_handle_t img_handle,
171 const struct efi_system_table *systable)
175 systemtab = systable;
176 boottime = systable->boottime;
178 acpi = efi_st_get_config_table(&acpi_guid);
179 fdt = efi_st_get_config_table(&fdt_guid);
182 efi_st_error("Missing device tree\n");
183 return EFI_ST_FAILURE;
186 efi_st_error("Found ACPI table and device tree\n");
187 return EFI_ST_FAILURE;
189 return EFI_ST_SUCCESS;
195 * @return: EFI_ST_SUCCESS for success
197 static int execute(void)
202 str = get_property(L"compatible", NULL);
204 efi_st_printf("compatible: %s\n", str);
205 ret = boottime->free_pool(str);
206 if (ret != EFI_SUCCESS) {
207 efi_st_error("FreePool failed\n");
208 return EFI_ST_FAILURE;
211 efi_st_error("Missing property 'compatible'\n");
212 return EFI_ST_FAILURE;
214 str = get_property(L"serial-number", NULL);
216 efi_st_printf("serial-number: %s\n", str);
217 ret = boottime->free_pool(str);
218 if (ret != EFI_SUCCESS) {
219 efi_st_error("FreePool failed\n");
220 return EFI_ST_FAILURE;
223 str = get_property(L"boot-hartid", L"chosen");
224 if (IS_ENABLED(CONFIG_RISCV)) {
226 efi_st_printf("boot-hartid: %u\n",
227 f2h(*(fdt32_t *)str));
228 ret = boottime->free_pool(str);
229 if (ret != EFI_SUCCESS) {
230 efi_st_error("FreePool failed\n");
231 return EFI_ST_FAILURE;
234 efi_st_error("boot-hartid not found\n");
235 return EFI_ST_FAILURE;
239 return EFI_ST_SUCCESS;
242 EFI_UNIT_TEST(fdt) = {
243 .name = "device tree",
244 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,