blob: 739f029b8c73369e9eb25a0403306f8b5cb2f4f3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +01002/*
3 * efi_selftest_pos
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardt06c3d5b2018-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 Schuchardt9967adb2018-03-12 19:52:25 +010014#include <linux/libfdt.h>
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010015
Heinrich Schuchardt68066d52019-04-20 13:33:55 +020016static const struct efi_system_table *systemtab;
17static const struct efi_boot_services *boottime;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010018static const char *fdt;
19
Heinrich Schuchardtd8b22162018-09-27 20:44:40 +020020/* This should be sufficient for */
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010021#define BUFFERSIZE 0x100000
22
Heinrich Schuchardt68066d52019-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 Schuchardt06c3d5b2018-03-03 15:29:04 +010025
Heinrich Schuchardt9abd2ca2021-11-25 18:55:09 +010026/**
27 * f2h() - convert FDT value to host endianness.
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010028 *
Heinrich Schuchardt9abd2ca2021-11-25 18:55:09 +010029 * UEFI code is always low endian. The FDT is big endian.
30 *
31 * @val: FDT value
32 * Return: converted value
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010033 */
34static uint32_t f2h(fdt32_t val)
35{
36 char *buf = (char *)&val;
37 char i;
38
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010039 /* Swap the bytes */
40 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
41 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
Heinrich Schuchardt9abd2ca2021-11-25 18:55:09 +010042
43 return val;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010044}
45
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020046/**
47 * get_property() - return value of a property of an FDT node
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010048 *
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020049 * A property of the root node or one of its direct children can be
50 * retrieved.
51 *
52 * @property name of the property
53 * @node name of the node or NULL for root node
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010054 * Return: value of the property
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010055 */
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020056static char *get_property(const u16 *property, const u16 *node)
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010057{
58 struct fdt_header *header = (struct fdt_header *)fdt;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020059 const fdt32_t *end;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010060 const fdt32_t *pos;
61 const char *strings;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020062 size_t level = 0;
63 const char *nodelabel = NULL;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010064
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020065 if (!header) {
66 efi_st_error("Missing device tree\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010067 return NULL;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020068 }
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010069
70 if (f2h(header->magic) != FDT_MAGIC) {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020071 efi_st_error("Wrong device tree magic\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010072 return NULL;
73 }
74
75 pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020076 end = &pos[f2h(header->totalsize) >> 2];
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010077 strings = fdt + f2h(header->off_dt_strings);
78
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020079 for (; pos < end;) {
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010080 switch (f2h(pos[0])) {
81 case FDT_BEGIN_NODE: {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020082 const char *c = (char *)&pos[1];
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010083 size_t i;
84
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020085 if (level == 1)
86 nodelabel = c;
87 ++level;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010088 for (i = 0; c[i]; ++i)
89 ;
90 pos = &pos[2 + (i >> 2)];
91 break;
92 }
93 case FDT_PROP: {
94 struct fdt_property *prop = (struct fdt_property *)pos;
95 const char *label = &strings[f2h(prop->nameoff)];
96 efi_status_t ret;
97
98 /* Check if this is the property to be returned */
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020099 if (!efi_st_strcmp_16_8(property, label) &&
100 ((level == 1 && !node) ||
101 (level == 2 && node &&
102 !efi_st_strcmp_16_8(node, nodelabel)))) {
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100103 char *str;
104 efi_uintn_t len = f2h(prop->len);
105
106 if (!len)
107 return NULL;
108 /*
109 * The string might not be 0 terminated.
110 * It is safer to make a copy.
111 */
112 ret = boottime->allocate_pool(
113 EFI_LOADER_DATA, len + 1,
114 (void **)&str);
115 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200116 efi_st_error("AllocatePool failed\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100117 return NULL;
118 }
119 boottime->copy_mem(str, &pos[3], len);
120 str[len] = 0;
121
122 return str;
123 }
124
125 pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
126 break;
127 }
128 case FDT_NOP:
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200129 ++pos;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100130 break;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200131 case FDT_END_NODE:
132 --level;
133 ++pos;
134 break;
135 case FDT_END:
136 return NULL;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100137 default:
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200138 efi_st_error("Invalid device tree token\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100139 return NULL;
140 }
141 }
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200142 efi_st_error("Missing FDT_END token\n");
143 return NULL;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100144}
145
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200146/**
147 * efi_st_get_config_table() - get configuration table
148 *
149 * @guid: GUID of the configuration table
150 * Return: pointer to configuration table or NULL
151 */
152static void *efi_st_get_config_table(const efi_guid_t *guid)
153{
154 size_t i;
155
156 for (i = 0; i < systab.nr_tables; i++) {
157 if (!guidcmp(guid, &systemtab->tables[i].guid))
158 return systemtab->tables[i].table;
159 }
160 return NULL;
161}
162
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100163/*
164 * Setup unit test.
165 *
166 * @handle: handle of the loaded image
167 * @systable: system table
168 * @return: EFI_ST_SUCCESS for success
169 */
170static int setup(const efi_handle_t img_handle,
171 const struct efi_system_table *systable)
172{
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200173 void *acpi;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100174
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200175 systemtab = systable;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100176 boottime = systable->boottime;
177
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200178 acpi = efi_st_get_config_table(&acpi_guid);
179 fdt = efi_st_get_config_table(&fdt_guid);
180
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100181 if (!fdt) {
182 efi_st_error("Missing device tree\n");
183 return EFI_ST_FAILURE;
184 }
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200185 if (acpi) {
186 efi_st_error("Found ACPI table and device tree\n");
187 return EFI_ST_FAILURE;
188 }
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100189 return EFI_ST_SUCCESS;
190}
191
192/*
193 * Execute unit test.
194 *
195 * @return: EFI_ST_SUCCESS for success
196 */
197static int execute(void)
198{
199 char *str;
200 efi_status_t ret;
201
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200202 str = get_property(L"compatible", NULL);
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100203 if (str) {
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;
209 }
210 } else {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200211 efi_st_error("Missing property 'compatible'\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100212 return EFI_ST_FAILURE;
213 }
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200214 str = get_property(L"serial-number", NULL);
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100215 if (str) {
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;
221 }
222 }
Heinrich Schuchardt52a84812020-09-17 07:33:29 +0200223 str = get_property(L"boot-hartid", L"chosen");
224 if (IS_ENABLED(CONFIG_RISCV)) {
225 if (str) {
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;
232 }
233 } else {
234 efi_st_error("boot-hartid not found\n");
235 return EFI_ST_FAILURE;
236 }
237 }
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100238
239 return EFI_ST_SUCCESS;
240}
241
242EFI_UNIT_TEST(fdt) = {
243 .name = "device tree",
244 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
245 .setup = setup,
246 .execute = execute,
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100247};