blob: a4b0cef20e4bf09234e34bc9d008ce7fff7c20fe [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/*
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +01003 * efi_selftest_fdt
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +01004 *
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +01006 * Copyright (c) 2022 Ventana Micro Systems Inc
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +01007 *
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +01008 * Check the device tree, test the RISCV_EFI_BOOT_PROTOCOL.
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +01009 */
10
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +010011#include <efi_riscv.h>
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010012#include <efi_selftest.h>
Heinrich Schuchardt9967adb2018-03-12 19:52:25 +010013#include <linux/libfdt.h>
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010014
Heinrich Schuchardt68066d52019-04-20 13:33:55 +020015static const struct efi_system_table *systemtab;
16static const struct efi_boot_services *boottime;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010017static const char *fdt;
18
Heinrich Schuchardtd8b22162018-09-27 20:44:40 +020019/* This should be sufficient for */
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010020#define BUFFERSIZE 0x100000
21
Heinrich Schuchardt68066d52019-04-20 13:33:55 +020022static const efi_guid_t fdt_guid = EFI_FDT_GUID;
23static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +010024static const efi_guid_t riscv_efi_boot_protocol_guid =
25 RISCV_EFI_BOOT_PROTOCOL_GUID;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010026
Heinrich Schuchardt9abd2ca2021-11-25 18:55:09 +010027/**
28 * f2h() - convert FDT value to host endianness.
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010029 *
Heinrich Schuchardt9abd2ca2021-11-25 18:55:09 +010030 * UEFI code is always low endian. The FDT is big endian.
31 *
32 * @val: FDT value
33 * Return: converted value
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010034 */
35static uint32_t f2h(fdt32_t val)
36{
37 char *buf = (char *)&val;
38 char i;
39
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010040 /* Swap the bytes */
41 i = buf[0]; buf[0] = buf[3]; buf[3] = i;
42 i = buf[1]; buf[1] = buf[2]; buf[2] = i;
Heinrich Schuchardt9abd2ca2021-11-25 18:55:09 +010043
44 return val;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010045}
46
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020047/**
48 * get_property() - return value of a property of an FDT node
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010049 *
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020050 * A property of the root node or one of its direct children can be
51 * retrieved.
52 *
53 * @property name of the property
54 * @node name of the node or NULL for root node
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010055 * Return: value of the property
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010056 */
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020057static char *get_property(const u16 *property, const u16 *node)
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010058{
59 struct fdt_header *header = (struct fdt_header *)fdt;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020060 const fdt32_t *end;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010061 const fdt32_t *pos;
62 const char *strings;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020063 size_t level = 0;
64 const char *nodelabel = NULL;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010065
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020066 if (!header) {
67 efi_st_error("Missing device tree\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010068 return NULL;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020069 }
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010070
71 if (f2h(header->magic) != FDT_MAGIC) {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020072 efi_st_error("Wrong device tree magic\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010073 return NULL;
74 }
75
76 pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020077 end = &pos[f2h(header->totalsize) >> 2];
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010078 strings = fdt + f2h(header->off_dt_strings);
79
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020080 for (; pos < end;) {
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010081 switch (f2h(pos[0])) {
82 case FDT_BEGIN_NODE: {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020083 const char *c = (char *)&pos[1];
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010084 size_t i;
85
Heinrich Schuchardt18161a82020-09-17 07:33:29 +020086 if (level == 1)
87 nodelabel = c;
88 ++level;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +010089 for (i = 0; c[i]; ++i)
90 ;
91 pos = &pos[2 + (i >> 2)];
92 break;
93 }
94 case FDT_PROP: {
95 struct fdt_property *prop = (struct fdt_property *)pos;
96 const char *label = &strings[f2h(prop->nameoff)];
97 efi_status_t ret;
98
99 /* Check if this is the property to be returned */
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200100 if (!efi_st_strcmp_16_8(property, label) &&
101 ((level == 1 && !node) ||
102 (level == 2 && node &&
103 !efi_st_strcmp_16_8(node, nodelabel)))) {
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100104 char *str;
105 efi_uintn_t len = f2h(prop->len);
106
107 if (!len)
108 return NULL;
109 /*
110 * The string might not be 0 terminated.
111 * It is safer to make a copy.
112 */
113 ret = boottime->allocate_pool(
114 EFI_LOADER_DATA, len + 1,
115 (void **)&str);
116 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200117 efi_st_error("AllocatePool failed\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100118 return NULL;
119 }
120 boottime->copy_mem(str, &pos[3], len);
121 str[len] = 0;
122
123 return str;
124 }
125
126 pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
127 break;
128 }
129 case FDT_NOP:
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200130 ++pos;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100131 break;
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200132 case FDT_END_NODE:
133 --level;
134 ++pos;
135 break;
136 case FDT_END:
137 return NULL;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100138 default:
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200139 efi_st_error("Invalid device tree token\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100140 return NULL;
141 }
142 }
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200143 efi_st_error("Missing FDT_END token\n");
144 return NULL;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100145}
146
147/*
148 * Setup unit test.
149 *
150 * @handle: handle of the loaded image
151 * @systable: system table
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100152 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100153 */
154static int setup(const efi_handle_t img_handle,
155 const struct efi_system_table *systable)
156{
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200157 void *acpi;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100158
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200159 systemtab = systable;
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100160 boottime = systable->boottime;
161
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200162 acpi = efi_st_get_config_table(&acpi_guid);
163 fdt = efi_st_get_config_table(&fdt_guid);
164
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100165 if (!fdt) {
166 efi_st_error("Missing device tree\n");
167 return EFI_ST_FAILURE;
168 }
Heinrich Schuchardt68066d52019-04-20 13:33:55 +0200169 if (acpi) {
170 efi_st_error("Found ACPI table and device tree\n");
171 return EFI_ST_FAILURE;
172 }
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100173 return EFI_ST_SUCCESS;
174}
175
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +0100176__maybe_unused static efi_status_t get_boot_hartid(efi_uintn_t *efi_hartid)
177{
178 efi_status_t ret;
179 struct riscv_efi_boot_protocol *prot;
180
181 /* Get RISC-V boot protocol */
182 ret = boottime->locate_protocol(&riscv_efi_boot_protocol_guid, NULL,
183 (void **)&prot);
184 if (ret != EFI_SUCCESS) {
185 efi_st_error("RISC-V Boot Protocol not available\n");
186 return EFI_ST_FAILURE;
187 }
188
189 /* Get boot hart ID from EFI protocol */
190 ret = prot->get_boot_hartid(prot, efi_hartid);
191 if (ret != EFI_SUCCESS) {
192 efi_st_error("Could not retrieve boot hart ID\n");
193 return EFI_ST_FAILURE;
194 }
195
196 return EFI_ST_SUCCESS;
197}
198
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100199/*
200 * Execute unit test.
201 *
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100202 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100203 */
204static int execute(void)
205{
206 char *str;
207 efi_status_t ret;
208
Simon Glass156ccbc2022-01-23 12:55:12 -0700209 str = get_property(u"compatible", NULL);
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100210 if (str) {
211 efi_st_printf("compatible: %s\n", str);
212 ret = boottime->free_pool(str);
213 if (ret != EFI_SUCCESS) {
214 efi_st_error("FreePool failed\n");
215 return EFI_ST_FAILURE;
216 }
217 } else {
Heinrich Schuchardt18161a82020-09-17 07:33:29 +0200218 efi_st_error("Missing property 'compatible'\n");
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100219 return EFI_ST_FAILURE;
220 }
Simon Glass156ccbc2022-01-23 12:55:12 -0700221 str = get_property(u"serial-number", NULL);
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100222 if (str) {
223 efi_st_printf("serial-number: %s\n", str);
224 ret = boottime->free_pool(str);
225 if (ret != EFI_SUCCESS) {
226 efi_st_error("FreePool failed\n");
227 return EFI_ST_FAILURE;
228 }
229 }
Heinrich Schuchardtb94217a2024-06-18 14:23:48 +0200230 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL_MEASURE_DTB)) {
231 str = get_property(u"kaslr-seed", u"chosen");
232 if (str) {
233 efi_st_error("kaslr-seed with measured fdt\n");
234 return EFI_ST_FAILURE;
235 }
236 }
Heinrich Schuchardt52a84812020-09-17 07:33:29 +0200237 if (IS_ENABLED(CONFIG_RISCV)) {
Heinrich Schuchardtb9b4cec2022-02-05 08:45:55 +0100238 u32 fdt_hartid;
239
240 str = get_property(u"boot-hartid", u"chosen");
241 if (!str) {
242 efi_st_error("boot-hartid missing in devicetree\n");
243 return EFI_ST_FAILURE;
244 }
245 fdt_hartid = f2h(*(fdt32_t *)str);
246 efi_st_printf("boot-hartid: %u\n", fdt_hartid);
247
248 ret = boottime->free_pool(str);
249 if (ret != EFI_SUCCESS) {
250 efi_st_error("FreePool failed\n");
251 return EFI_ST_FAILURE;
252 }
253
254 if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
255 efi_uintn_t efi_hartid;
256 int r;
257
258 r = get_boot_hartid(&efi_hartid);
259 if (r != EFI_ST_SUCCESS)
260 return r;
261 /* Boot hart ID should be same */
262 if (efi_hartid != fdt_hartid) {
263 efi_st_error("boot-hartid differs: prot 0x%p, DT 0x%.8x\n",
264 (void *)(uintptr_t)efi_hartid,
265 fdt_hartid);
Heinrich Schuchardt52a84812020-09-17 07:33:29 +0200266 return EFI_ST_FAILURE;
267 }
Heinrich Schuchardt52a84812020-09-17 07:33:29 +0200268 }
269 }
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100270
271 return EFI_ST_SUCCESS;
272}
273
274EFI_UNIT_TEST(fdt) = {
275 .name = "device tree",
276 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
277 .setup = setup,
278 .execute = execute,
Heinrich Schuchardt06c3d5b2018-03-03 15:29:04 +0100279};