4 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 * SPDX-License-Identifier: GPL-2.0+
8 * Test the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.
10 * The following services are tested:
11 * OutputString, TestString, SetAttribute.
14 #include <efi_selftest.h>
15 #include <linux/libfdt.h>
17 static struct efi_boot_services *boottime;
18 static const char *fdt;
20 /* This should be sufficent for */
21 #define BUFFERSIZE 0x100000
23 static efi_guid_t fdt_guid = EFI_FDT_GUID;
26 * Convert FDT value to host endianness.
29 * @return converted value
31 static uint32_t f2h(fdt32_t val)
33 char *buf = (char *)&val;
36 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
38 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
39 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
41 return *(uint32_t *)buf;
45 * Return the value of a property of the FDT root node.
47 * @name name of the property
48 * @return value of the property
50 static char *get_property(const u16 *property)
52 struct fdt_header *header = (struct fdt_header *)fdt;
59 if (f2h(header->magic) != FDT_MAGIC) {
60 printf("Wrong magic\n");
64 pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
65 strings = fdt + f2h(header->off_dt_strings);
68 switch (f2h(pos[0])) {
69 case FDT_BEGIN_NODE: {
70 char *c = (char *)&pos[1];
73 for (i = 0; c[i]; ++i)
75 pos = &pos[2 + (i >> 2)];
79 struct fdt_property *prop = (struct fdt_property *)pos;
80 const char *label = &strings[f2h(prop->nameoff)];
83 /* Check if this is the property to be returned */
84 if (!efi_st_strcmp_16_8(property, label)) {
86 efi_uintn_t len = f2h(prop->len);
91 * The string might not be 0 terminated.
92 * It is safer to make a copy.
94 ret = boottime->allocate_pool(
95 EFI_LOADER_DATA, len + 1,
97 if (ret != EFI_SUCCESS) {
98 efi_st_printf("AllocatePool failed\n");
101 boottime->copy_mem(str, &pos[3], len);
107 pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
122 * @handle: handle of the loaded image
123 * @systable: system table
124 * @return: EFI_ST_SUCCESS for success
126 static int setup(const efi_handle_t img_handle,
127 const struct efi_system_table *systable)
131 boottime = systable->boottime;
133 /* Find configuration tables */
134 for (i = 0; i < systable->nr_tables; ++i) {
135 if (!efi_st_memcmp(&systable->tables[i].guid, &fdt_guid,
137 fdt = systable->tables[i].table;
140 efi_st_error("Missing device tree\n");
141 return EFI_ST_FAILURE;
144 return EFI_ST_SUCCESS;
150 * @return: EFI_ST_SUCCESS for success
152 static int execute(void)
157 str = get_property(L"compatible");
159 efi_st_printf("compatible: %s\n", str);
160 ret = boottime->free_pool(str);
161 if (ret != EFI_SUCCESS) {
162 efi_st_error("FreePool failed\n");
163 return EFI_ST_FAILURE;
166 efi_st_printf("Missing property 'compatible'\n");
167 return EFI_ST_FAILURE;
169 str = get_property(L"serial-number");
171 efi_st_printf("serial-number: %s\n", str);
172 ret = boottime->free_pool(str);
173 if (ret != EFI_SUCCESS) {
174 efi_st_error("FreePool failed\n");
175 return EFI_ST_FAILURE;
179 return EFI_ST_SUCCESS;
182 EFI_UNIT_TEST(fdt) = {
183 .name = "device tree",
184 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,