blob: eae98208f6e0feb0d3867bf391b01ffa54cc367e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt43d27992018-03-03 15:29:04 +01002/*
3 * efi_selftest_pos
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardt43d27992018-03-03 15:29:04 +01007 * Test the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.
8 *
9 * The following services are tested:
10 * OutputString, TestString, SetAttribute.
11 */
12
13#include <efi_selftest.h>
Heinrich Schuchardt0a9b9e52018-03-12 19:52:25 +010014#include <linux/libfdt.h>
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010015
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +020016static const struct efi_system_table *systemtab;
17static const struct efi_boot_services *boottime;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010018static const char *fdt;
19
Heinrich Schuchardt702e7782018-09-27 20:44:40 +020020/* This should be sufficient for */
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010021#define BUFFERSIZE 0x100000
22
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +020023static const efi_guid_t fdt_guid = EFI_FDT_GUID;
24static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010025
26/*
27 * Convert FDT value to host endianness.
28 *
29 * @val FDT value
30 * @return converted value
31 */
32static uint32_t f2h(fdt32_t val)
33{
34 char *buf = (char *)&val;
35 char i;
36
37#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
38 /* Swap the bytes */
39 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
40 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
41#endif
42 return *(uint32_t *)buf;
43}
44
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020045/**
46 * get_property() - return value of a property of an FDT node
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010047 *
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020048 * A property of the root node or one of its direct children can be
49 * retrieved.
50 *
51 * @property name of the property
52 * @node name of the node or NULL for root node
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010053 * @return value of the property
54 */
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020055static char *get_property(const u16 *property, const u16 *node)
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010056{
57 struct fdt_header *header = (struct fdt_header *)fdt;
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020058 const fdt32_t *end;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010059 const fdt32_t *pos;
60 const char *strings;
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020061 size_t level = 0;
62 const char *nodelabel = NULL;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010063
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020064 if (!header) {
65 efi_st_error("Missing device tree\n");
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010066 return NULL;
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020067 }
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010068
69 if (f2h(header->magic) != FDT_MAGIC) {
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020070 efi_st_error("Wrong device tree magic\n");
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010071 return NULL;
72 }
73
74 pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020075 end = &pos[f2h(header->totalsize) >> 2];
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010076 strings = fdt + f2h(header->off_dt_strings);
77
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020078 for (; pos < end;) {
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010079 switch (f2h(pos[0])) {
80 case FDT_BEGIN_NODE: {
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020081 const char *c = (char *)&pos[1];
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010082 size_t i;
83
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020084 if (level == 1)
85 nodelabel = c;
86 ++level;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +010087 for (i = 0; c[i]; ++i)
88 ;
89 pos = &pos[2 + (i >> 2)];
90 break;
91 }
92 case FDT_PROP: {
93 struct fdt_property *prop = (struct fdt_property *)pos;
94 const char *label = &strings[f2h(prop->nameoff)];
95 efi_status_t ret;
96
97 /* Check if this is the property to be returned */
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +020098 if (!efi_st_strcmp_16_8(property, label) &&
99 ((level == 1 && !node) ||
100 (level == 2 && node &&
101 !efi_st_strcmp_16_8(node, nodelabel)))) {
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100102 char *str;
103 efi_uintn_t len = f2h(prop->len);
104
105 if (!len)
106 return NULL;
107 /*
108 * The string might not be 0 terminated.
109 * It is safer to make a copy.
110 */
111 ret = boottime->allocate_pool(
112 EFI_LOADER_DATA, len + 1,
113 (void **)&str);
114 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200115 efi_st_error("AllocatePool failed\n");
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100116 return NULL;
117 }
118 boottime->copy_mem(str, &pos[3], len);
119 str[len] = 0;
120
121 return str;
122 }
123
124 pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
125 break;
126 }
127 case FDT_NOP:
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200128 ++pos;
129 break;
130 case FDT_END_NODE:
131 --level;
132 ++pos;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100133 break;
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200134 case FDT_END:
135 return NULL;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100136 default:
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200137 efi_st_error("Invalid device tree token\n");
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100138 return NULL;
139 }
140 }
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200141 efi_st_error("Missing FDT_END token\n");
142 return NULL;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100143}
144
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +0200145/**
146 * efi_st_get_config_table() - get configuration table
147 *
148 * @guid: GUID of the configuration table
149 * Return: pointer to configuration table or NULL
150 */
151static void *efi_st_get_config_table(const efi_guid_t *guid)
152{
153 size_t i;
154
155 for (i = 0; i < systab.nr_tables; i++) {
156 if (!guidcmp(guid, &systemtab->tables[i].guid))
157 return systemtab->tables[i].table;
158 }
159 return NULL;
160}
161
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100162/*
163 * Setup unit test.
164 *
165 * @handle: handle of the loaded image
166 * @systable: system table
167 * @return: EFI_ST_SUCCESS for success
168 */
169static int setup(const efi_handle_t img_handle,
170 const struct efi_system_table *systable)
171{
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +0200172 void *acpi;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100173
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +0200174 systemtab = systable;
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100175 boottime = systable->boottime;
176
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +0200177 acpi = efi_st_get_config_table(&acpi_guid);
178 fdt = efi_st_get_config_table(&fdt_guid);
179
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100180 if (!fdt) {
181 efi_st_error("Missing device tree\n");
182 return EFI_ST_FAILURE;
183 }
Heinrich Schuchardt1c111f92019-04-20 13:33:55 +0200184 if (acpi) {
185 efi_st_error("Found ACPI table and device tree\n");
186 return EFI_ST_FAILURE;
187 }
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100188 return EFI_ST_SUCCESS;
189}
190
191/*
192 * Execute unit test.
193 *
194 * @return: EFI_ST_SUCCESS for success
195 */
196static int execute(void)
197{
198 char *str;
199 efi_status_t ret;
200
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200201 str = get_property(L"compatible", NULL);
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100202 if (str) {
203 efi_st_printf("compatible: %s\n", str);
204 ret = boottime->free_pool(str);
205 if (ret != EFI_SUCCESS) {
206 efi_st_error("FreePool failed\n");
207 return EFI_ST_FAILURE;
208 }
209 } else {
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200210 efi_st_error("Missing property 'compatible'\n");
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100211 return EFI_ST_FAILURE;
212 }
Heinrich Schuchardt0fe99562020-09-17 07:33:29 +0200213 str = get_property(L"serial-number", NULL);
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100214 if (str) {
215 efi_st_printf("serial-number: %s\n", str);
216 ret = boottime->free_pool(str);
217 if (ret != EFI_SUCCESS) {
218 efi_st_error("FreePool failed\n");
219 return EFI_ST_FAILURE;
220 }
221 }
Heinrich Schuchardt2bcaad82020-09-17 07:33:29 +0200222 str = get_property(L"boot-hartid", L"chosen");
223 if (IS_ENABLED(CONFIG_RISCV)) {
224 if (str) {
225 efi_st_printf("boot-hartid: %u\n",
226 f2h(*(fdt32_t *)str));
227 ret = boottime->free_pool(str);
228 if (ret != EFI_SUCCESS) {
229 efi_st_error("FreePool failed\n");
230 return EFI_ST_FAILURE;
231 }
232 } else {
233 efi_st_error("boot-hartid not found\n");
234 return EFI_ST_FAILURE;
235 }
236 }
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100237
238 return EFI_ST_SUCCESS;
239}
240
241EFI_UNIT_TEST(fdt) = {
242 .name = "device tree",
243 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
244 .setup = setup,
245 .execute = execute,
Heinrich Schuchardt43d27992018-03-03 15:29:04 +0100246};