1 // SPDX-License-Identifier: GPL-2.0+
3 * efi_selftest_devicepath
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * This unit test checks the following protocol services:
11 #include <efi_selftest.h>
13 static struct efi_boot_services *boottime;
15 static efi_handle_t handle1;
16 static efi_handle_t handle2;
17 static efi_handle_t handle3;
20 void (EFIAPI * inc)(void);
23 static efi_guid_t guid_device_path = DEVICE_PATH_GUID;
25 static efi_guid_t guid_device_path_to_text_protocol =
26 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
28 static efi_guid_t guid_protocol =
29 EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
30 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0x7d);
32 static efi_guid_t guid_vendor1 =
33 EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
34 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xb1);
36 static efi_guid_t guid_vendor2 =
37 EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
38 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xa2);
40 static efi_guid_t guid_vendor3 =
41 EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
42 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xc3);
48 struct efi_device_path_to_text_protocol *device_path_to_text;
53 * Create three handles. Install a new protocol on two of them and
54 * provide device paths.
62 * @handle: handle of the loaded image
63 * @systable: system table
65 static int setup(const efi_handle_t img_handle,
66 const struct efi_system_table *systable)
68 struct efi_device_path_vendor vendor_node;
69 struct efi_device_path end_node;
72 boottime = systable->boottime;
74 ret = boottime->locate_protocol(&guid_device_path_to_text_protocol,
75 NULL, (void **)&device_path_to_text);
76 if (ret != EFI_SUCCESS) {
77 device_path_to_text = NULL;
79 "Device path to text protocol is not available.\n");
80 return EFI_ST_FAILURE;
83 ret = boottime->allocate_pool(EFI_LOADER_DATA,
84 sizeof(struct efi_device_path_vendor) +
85 sizeof(struct efi_device_path),
87 if (ret != EFI_SUCCESS)
90 ret = boottime->allocate_pool(EFI_LOADER_DATA, 2 *
91 sizeof(struct efi_device_path_vendor) +
92 sizeof(struct efi_device_path),
94 if (ret != EFI_SUCCESS)
97 ret = boottime->allocate_pool(EFI_LOADER_DATA, 3 *
98 sizeof(struct efi_device_path_vendor) +
99 sizeof(struct efi_device_path),
101 if (ret != EFI_SUCCESS)
104 vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
105 vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
106 vendor_node.dp.length = sizeof(struct efi_device_path_vendor);
108 boottime->copy_mem(&vendor_node.guid, &guid_vendor1,
110 boottime->copy_mem(dp1, &vendor_node,
111 sizeof(struct efi_device_path_vendor));
112 boottime->copy_mem(dp2, &vendor_node,
113 sizeof(struct efi_device_path_vendor));
114 boottime->copy_mem(dp3, &vendor_node,
115 sizeof(struct efi_device_path_vendor));
117 boottime->copy_mem(&vendor_node.guid, &guid_vendor2,
119 boottime->copy_mem(dp2 + sizeof(struct efi_device_path_vendor),
120 &vendor_node, sizeof(struct efi_device_path_vendor));
121 boottime->copy_mem(dp3 + sizeof(struct efi_device_path_vendor),
122 &vendor_node, sizeof(struct efi_device_path_vendor));
124 boottime->copy_mem(&vendor_node.guid, &guid_vendor3,
126 boottime->copy_mem(dp3 + 2 * sizeof(struct efi_device_path_vendor),
127 &vendor_node, sizeof(struct efi_device_path_vendor));
129 end_node.type = DEVICE_PATH_TYPE_END;
130 end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
131 end_node.length = sizeof(struct efi_device_path);
132 boottime->copy_mem(dp1 + sizeof(struct efi_device_path_vendor),
133 &end_node, sizeof(struct efi_device_path));
134 boottime->copy_mem(dp2 + 2 * sizeof(struct efi_device_path_vendor),
135 &end_node, sizeof(struct efi_device_path));
136 boottime->copy_mem(dp3 + 3 * sizeof(struct efi_device_path_vendor),
137 &end_node, sizeof(struct efi_device_path));
139 ret = boottime->install_protocol_interface(&handle1,
141 EFI_NATIVE_INTERFACE,
143 if (ret != EFI_SUCCESS) {
144 efi_st_error("InstallProtocolInterface failed\n");
145 return EFI_ST_FAILURE;
147 ret = boottime->install_protocol_interface(&handle1,
149 EFI_NATIVE_INTERFACE,
151 if (ret != EFI_SUCCESS) {
152 efi_st_error("InstallProtocolInterface failed\n");
153 return EFI_ST_FAILURE;
155 ret = boottime->install_protocol_interface(&handle2,
157 EFI_NATIVE_INTERFACE,
159 if (ret != EFI_SUCCESS) {
160 efi_st_error("InstallProtocolInterface failed\n");
161 return EFI_ST_FAILURE;
163 ret = boottime->install_protocol_interface(&handle2,
165 EFI_NATIVE_INTERFACE,
167 if (ret != EFI_SUCCESS) {
168 efi_st_error("InstallProtocolInterface failed\n");
169 return EFI_ST_FAILURE;
171 ret = boottime->install_protocol_interface(&handle3,
173 EFI_NATIVE_INTERFACE,
175 if (ret != EFI_SUCCESS) {
176 efi_st_error("InstallProtocolInterface failed\n");
177 return EFI_ST_FAILURE;
179 return EFI_ST_SUCCESS;
182 efi_st_error("Out of memory\n");
183 return EFI_ST_FAILURE;
187 * Tear down unit test.
190 static int teardown(void)
194 ret = boottime->uninstall_protocol_interface(handle1,
197 if (ret != EFI_SUCCESS) {
198 efi_st_error("UninstallProtocolInterface failed\n");
199 return EFI_ST_FAILURE;
201 ret = boottime->uninstall_protocol_interface(handle1,
204 if (ret != EFI_SUCCESS) {
205 efi_st_error("UninstallProtocolInterface failed\n");
206 return EFI_ST_FAILURE;
208 ret = boottime->uninstall_protocol_interface(handle2,
211 if (ret != EFI_SUCCESS) {
212 efi_st_error("UninstallProtocolInterface failed\n");
213 return EFI_ST_FAILURE;
215 ret = boottime->uninstall_protocol_interface(handle2,
218 if (ret != EFI_SUCCESS) {
219 efi_st_error("UninstallProtocolInterface failed\n");
220 return EFI_ST_FAILURE;
222 ret = boottime->uninstall_protocol_interface(handle3,
225 if (ret != EFI_SUCCESS) {
226 efi_st_error("UninstallProtocolInterface failed\n");
227 return EFI_ST_FAILURE;
230 ret = boottime->free_pool(dp1);
231 if (ret != EFI_SUCCESS) {
232 efi_st_error("FreePool failed\n");
233 return EFI_ST_FAILURE;
237 ret = boottime->free_pool(dp2);
238 if (ret != EFI_SUCCESS) {
239 efi_st_error("FreePool failed\n");
240 return EFI_ST_FAILURE;
244 ret = boottime->free_pool(dp3);
245 if (ret != EFI_SUCCESS) {
246 efi_st_error("FreePool failed\n");
247 return EFI_ST_FAILURE;
250 return EFI_ST_SUCCESS;
257 static int execute(void)
259 struct efi_device_path *remaining_dp;
262 * This device path node ends with the letter 't' of 'u-boot'.
263 * The following '.bin' does not belong to the node but is
264 * helps to test the correct truncation.
267 struct efi_device_path dp;
269 } __packed dp_node = {
270 { DEVICE_PATH_TYPE_MEDIA_DEVICE,
271 DEVICE_PATH_SUB_TYPE_FILE_PATH,
272 sizeof(struct efi_device_path) + 12},
277 efi_uintn_t i, no_handles;
278 efi_handle_t *handles;
279 struct efi_device_path *dp;
281 /* Display all available device paths */
282 ret = boottime->locate_handle_buffer(BY_PROTOCOL,
284 NULL, &no_handles, &handles);
285 if (ret != EFI_SUCCESS) {
286 efi_st_error("Cannot retrieve device path protocols.\n");
287 return EFI_ST_FAILURE;
290 efi_st_printf("Installed device path protocols:\n");
291 for (i = 0; i < no_handles; ++i) {
292 ret = boottime->open_protocol(handles[i], &guid_device_path,
293 (void **)&dp, NULL, NULL,
294 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
295 if (ret != EFI_SUCCESS) {
296 efi_st_error("Cannot open device path protocol.\n");
297 return EFI_ST_FAILURE;
299 string = device_path_to_text->convert_device_path_to_text(
302 efi_st_error("ConvertDevicePathToText failed\n");
303 return EFI_ST_FAILURE;
305 efi_st_printf("%ps\n", string);
306 ret = boottime->free_pool(string);
307 if (ret != EFI_SUCCESS) {
308 efi_st_error("FreePool failed\n");
309 return EFI_ST_FAILURE;
312 * CloseProtocol cannot be called without agent handle.
313 * There is no need to close the device path protocol.
316 ret = boottime->free_pool(handles);
317 if (ret != EFI_SUCCESS) {
318 efi_st_error("FreePool failed\n");
319 return EFI_ST_FAILURE;
322 /* Test ConvertDevicePathToText */
323 string = device_path_to_text->convert_device_path_to_text(
324 (struct efi_device_path *)dp2, true, false);
326 efi_st_error("ConvertDevicePathToText failed\n");
327 return EFI_ST_FAILURE;
329 if (efi_st_strcmp_16_8(
331 "/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbb1)/VenHw(dbca4c98-6cb0-694d-0872-819c650cbba2)")
333 efi_st_printf("dp2: %ps\n", string);
334 efi_st_error("Incorrect text from ConvertDevicePathToText\n");
335 return EFI_ST_FAILURE;
337 ret = boottime->free_pool(string);
338 if (ret != EFI_SUCCESS) {
339 efi_st_error("FreePool failed\n");
340 return EFI_ST_FAILURE;
343 /* Test ConvertDeviceNodeToText */
344 string = device_path_to_text->convert_device_node_to_text(
345 (struct efi_device_path *)&dp_node, true, false);
347 efi_st_error("ConvertDeviceNodeToText failed\n");
348 return EFI_ST_FAILURE;
350 if (efi_st_strcmp_16_8(string, "u-boot")) {
351 efi_st_printf("dp_node: %ps\n", string);
353 "Incorrect conversion by ConvertDeviceNodeToText\n");
354 return EFI_ST_FAILURE;
356 ret = boottime->free_pool(string);
357 if (ret != EFI_SUCCESS) {
358 efi_st_error("FreePool failed\n");
359 return EFI_ST_FAILURE;
362 /* Test LocateDevicePath */
363 remaining_dp = (struct efi_device_path *)dp3;
364 ret = boottime->locate_device_path(&guid_protocol, &remaining_dp,
366 if (ret != EFI_SUCCESS) {
367 efi_st_error("LocateDevicePath failed\n");
368 return EFI_ST_FAILURE;
370 if (handle != handle2) {
371 efi_st_error("LocateDevicePath returned wrong handle\n");
372 return EFI_ST_FAILURE;
374 string = device_path_to_text->convert_device_path_to_text(remaining_dp,
377 efi_st_error("ConvertDevicePathToText failed\n");
378 return EFI_ST_FAILURE;
380 if (efi_st_strcmp_16_8(string,
381 "/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbc3)")
383 efi_st_printf("remaining device path: %ps\n", string);
384 efi_st_error("LocateDevicePath: wrong remaining device path\n");
385 return EFI_ST_FAILURE;
387 ret = boottime->free_pool(string);
388 if (ret != EFI_SUCCESS) {
389 efi_st_error("FreePool failed\n");
390 return EFI_ST_FAILURE;
393 return EFI_ST_SUCCESS;
396 EFI_UNIT_TEST(devicepath) = {
397 .name = "device path",
398 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
401 .teardown = teardown,