2 * efi_selftest_devicepath_util
4 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 * SPDX-License-Identifier: GPL-2.0+
8 * This unit test checks the device path utilities protocol.
11 #include <efi_selftest.h>
13 static struct efi_boot_services *boottime;
15 static efi_guid_t guid_device_path_utilities_protocol =
16 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
18 struct efi_device_path_utilities_protocol *dpu;
23 * Locate the device path utilities protocol.
25 * @handle: handle of the loaded image
26 * @systable: system table
28 static int setup(const efi_handle_t img_handle,
29 const struct efi_system_table *systable)
33 boottime = systable->boottime;
35 ret = boottime->locate_protocol(&guid_device_path_utilities_protocol,
37 if (ret != EFI_SUCCESS) {
40 "Device path to text protocol is not available.\n");
41 return EFI_ST_FAILURE;
44 return EFI_ST_SUCCESS;
48 * Create a device path consisting of a single media device node followed by an
51 * @length: length of the media device node
53 * @return: status code
55 static int create_single_node_device_path(unsigned int length,
56 struct efi_device_path **dp)
58 struct efi_device_path *node;
62 node = dpu->create_device_node(DEVICE_PATH_TYPE_MEDIA_DEVICE,
63 DEVICE_PATH_SUB_TYPE_FILE_PATH, length);
65 efi_st_error("CreateDeviceNode failed\n");
66 return EFI_ST_FAILURE;
68 *dp = dpu->append_device_node(NULL, node);
70 efi_st_error("AppendDeviceNode failed\n");
71 return EFI_ST_FAILURE;
73 ret = boottime->free_pool(node);
74 if (ret != EFI_ST_SUCCESS) {
75 efi_st_error("FreePool failed\n");
76 return EFI_ST_FAILURE;
78 len = dpu->get_device_path_size(*dp);
79 if (len != length + 4) {
80 efi_st_error("Wrong device path length %u, expected %u\n",
81 (unsigned int)len, length);
82 return EFI_ST_FAILURE;
84 return EFI_ST_SUCCESS;
90 * In the test device paths are created, copied, and concatenated. The device
91 * path length is used as a measure of success.
93 static int execute(void)
95 struct efi_device_path *dp1;
96 struct efi_device_path *dp2;
97 struct efi_device_path *dp3;
102 /* IsDevicePathMultiInstance(NULL) */
103 if (dpu->is_device_path_multi_instance(NULL)) {
104 efi_st_error("IsDevicePathMultiInstance(NULL) returned true\n");
105 return EFI_ST_FAILURE;
107 /* GetDevicePathSize(NULL) */
108 len = dpu->get_device_path_size(NULL);
110 efi_st_error("Wrong device path length %u, expected 0\n",
112 return EFI_ST_FAILURE;
114 /* DuplicateDevicePath(NULL) */
115 dp1 = dpu->duplicate_device_path(NULL);
117 efi_st_error("DuplicateDevicePath(NULL) failed\n");
118 return EFI_ST_FAILURE;
120 /* AppendDevicePath(NULL, NULL) */
121 dp1 = dpu->append_device_path(NULL, NULL);
123 efi_st_error("AppendDevicePath(NULL, NULL) failed\n");
124 return EFI_ST_FAILURE;
126 len = dpu->get_device_path_size(dp1);
128 efi_st_error("Wrong device path length %u, expected 4\n",
130 return EFI_ST_FAILURE;
132 ret = boottime->free_pool(dp1);
133 if (ret != EFI_ST_SUCCESS) {
134 efi_st_error("FreePool failed\n");
135 return EFI_ST_FAILURE;
137 /* CreateDeviceNode */
138 ret = create_single_node_device_path(21, &dp1);
139 if (ret != EFI_ST_SUCCESS)
141 ret = create_single_node_device_path(17, &dp2);
142 if (ret != EFI_ST_SUCCESS)
144 /* AppendDevicePath */
145 dp3 = dpu->append_device_path(dp1, dp2);
147 efi_st_error("AppendDevicePath failed\n");
148 return EFI_ST_FAILURE;
150 if (dp3 == dp1 || dp3 == dp2) {
151 efi_st_error("AppendDevicePath reused buffer\n");
152 return EFI_ST_FAILURE;
154 len = dpu->get_device_path_size(dp3);
157 efi_st_error("Wrong device path length %u, expected 42\n",
159 return EFI_ST_FAILURE;
161 ret = boottime->free_pool(dp2);
162 if (ret != EFI_ST_SUCCESS) {
163 efi_st_error("FreePool failed\n");
164 return EFI_ST_FAILURE;
166 /* AppendDeviceNode */
167 dp2 = dpu->append_device_node(dp1, dp3);
169 efi_st_error("AppendDevicePath failed\n");
170 return EFI_ST_FAILURE;
172 len = dpu->get_device_path_size(dp2);
175 printf("%s(%d) %s\n", __FILE__, __LINE__, __func__);
176 efi_st_error("Wrong device path length %u, expected 46\n",
178 return EFI_ST_FAILURE;
180 ret = boottime->free_pool(dp1);
181 if (ret != EFI_ST_SUCCESS) {
182 efi_st_error("FreePool failed\n");
183 return EFI_ST_FAILURE;
185 /* IsDevicePathMultiInstance */
186 if (dpu->is_device_path_multi_instance(dp2)) {
187 printf("%s(%d) %s\n", __FILE__, __LINE__, __func__);
188 efi_st_error("IsDevicePathMultiInstance returned true\n");
189 return EFI_ST_FAILURE;
191 /* AppendDevicePathInstance */
192 dp1 = dpu->append_device_path_instance(dp2, dp3);
194 efi_st_error("AppendDevicePathInstance failed\n");
195 return EFI_ST_FAILURE;
197 len = dpu->get_device_path_size(dp1);
200 efi_st_error("Wrong device path length %u, expected 88\n",
202 return EFI_ST_FAILURE;
204 /* IsDevicePathMultiInstance */
205 if (!dpu->is_device_path_multi_instance(dp1)) {
206 efi_st_error("IsDevicePathMultiInstance returned false\n");
207 return EFI_ST_FAILURE;
209 ret = boottime->free_pool(dp2);
210 if (ret != EFI_ST_SUCCESS) {
211 efi_st_error("FreePool failed\n");
212 return EFI_ST_FAILURE;
214 ret = boottime->free_pool(dp3);
215 if (ret != EFI_ST_SUCCESS) {
216 efi_st_error("FreePool failed\n");
217 return EFI_ST_FAILURE;
219 /* GetNextDevicePathInstance */
221 dp2 = dpu->get_next_device_path_instance(&dp1, &len);
223 efi_st_error("GetNextDevicePathInstance failed\n");
224 return EFI_ST_FAILURE;
227 efi_st_error("GetNextDevicePathInstance no 2nd instance\n");
228 return EFI_ST_FAILURE;
231 efi_st_error("Wrong device path length %u, expected 46\n",
233 return EFI_ST_FAILURE;
235 len = dpu->get_device_path_size(dp1);
237 efi_st_error("Wrong device path length %u, expected 42\n",
239 return EFI_ST_FAILURE;
241 ret = boottime->free_pool(dp2);
242 if (ret != EFI_ST_SUCCESS) {
243 efi_st_error("FreePool failed\n");
244 return EFI_ST_FAILURE;
246 dp2 = dpu->get_next_device_path_instance(&dp1, &len);
248 efi_st_error("GetNextDevicePathInstance failed\n");
249 return EFI_ST_FAILURE;
252 efi_st_error("Wrong device path length %u, expected 46\n",
254 return EFI_ST_FAILURE;
257 efi_st_error("GetNextDevicePathInstance did not signal end\n");
258 return EFI_ST_FAILURE;
260 ret = boottime->free_pool(dp2);
261 if (ret != EFI_ST_SUCCESS) {
262 efi_st_error("FreePool failed\n");
263 return EFI_ST_FAILURE;
267 ret = boottime->free_pool(dp2);
268 if (ret != EFI_ST_SUCCESS) {
269 efi_st_error("FreePool failed\n");
270 return EFI_ST_FAILURE;
272 ret = boottime->free_pool(dp3);
273 if (ret != EFI_ST_SUCCESS) {
274 efi_st_error("FreePool failed\n");
275 return EFI_ST_FAILURE;
278 return EFI_ST_SUCCESS;
281 EFI_UNIT_TEST(dputil) = {
282 .name = "device path utilities protocol",
283 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,