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 struct efi_boot_services *boottime;
17 static const char *fdt;
19 /* This should be sufficient for */
20 #define BUFFERSIZE 0x100000
22 static efi_guid_t fdt_guid = EFI_FDT_GUID;
25 * Convert FDT value to host endianness.
28 * @return converted value
30 static uint32_t f2h(fdt32_t val)
32 char *buf = (char *)&val;
35 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
37 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
38 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
40 return *(uint32_t *)buf;
44 * Return the value of a property of the FDT root node.
46 * @name name of the property
47 * @return value of the property
49 static char *get_property(const u16 *property)
51 struct fdt_header *header = (struct fdt_header *)fdt;
58 if (f2h(header->magic) != FDT_MAGIC) {
59 printf("Wrong magic\n");
63 pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
64 strings = fdt + f2h(header->off_dt_strings);
67 switch (f2h(pos[0])) {
68 case FDT_BEGIN_NODE: {
69 char *c = (char *)&pos[1];
72 for (i = 0; c[i]; ++i)
74 pos = &pos[2 + (i >> 2)];
78 struct fdt_property *prop = (struct fdt_property *)pos;
79 const char *label = &strings[f2h(prop->nameoff)];
82 /* Check if this is the property to be returned */
83 if (!efi_st_strcmp_16_8(property, label)) {
85 efi_uintn_t len = f2h(prop->len);
90 * The string might not be 0 terminated.
91 * It is safer to make a copy.
93 ret = boottime->allocate_pool(
94 EFI_LOADER_DATA, len + 1,
96 if (ret != EFI_SUCCESS) {
97 efi_st_printf("AllocatePool failed\n");
100 boottime->copy_mem(str, &pos[3], len);
106 pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
121 * @handle: handle of the loaded image
122 * @systable: system table
123 * @return: EFI_ST_SUCCESS for success
125 static int setup(const efi_handle_t img_handle,
126 const struct efi_system_table *systable)
130 boottime = systable->boottime;
132 /* Find configuration tables */
133 for (i = 0; i < systable->nr_tables; ++i) {
134 if (!efi_st_memcmp(&systable->tables[i].guid, &fdt_guid,
136 fdt = systable->tables[i].table;
139 efi_st_error("Missing device tree\n");
140 return EFI_ST_FAILURE;
143 return EFI_ST_SUCCESS;
149 * @return: EFI_ST_SUCCESS for success
151 static int execute(void)
156 str = get_property(L"compatible");
158 efi_st_printf("compatible: %s\n", str);
159 ret = boottime->free_pool(str);
160 if (ret != EFI_SUCCESS) {
161 efi_st_error("FreePool failed\n");
162 return EFI_ST_FAILURE;
165 efi_st_printf("Missing property 'compatible'\n");
166 return EFI_ST_FAILURE;
168 str = get_property(L"serial-number");
170 efi_st_printf("serial-number: %s\n", str);
171 ret = boottime->free_pool(str);
172 if (ret != EFI_SUCCESS) {
173 efi_st_error("FreePool failed\n");
174 return EFI_ST_FAILURE;
178 return EFI_ST_SUCCESS;
181 EFI_UNIT_TEST(fdt) = {
182 .name = "device tree",
183 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,