aboutsummaryrefslogtreecommitdiff
path: root/lib/efi_selftest/efi_selftest_devicepath_util.c
blob: c846e057d3532bf1f505aef07366244ea316173e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-License-Identifier: GPL-2.0+
/*
 * efi_selftest_devicepath_util
 *
 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
 *
 * This unit test checks the device path utilities protocol.
 */

#include <efi_selftest.h>

static struct efi_boot_services *boottime;

static efi_guid_t guid_device_path_utilities_protocol =
	EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;

struct efi_device_path_utilities_protocol *dpu;

/*
 * Setup unit test.
 *
 * Locate the device path utilities protocol.
 *
 * @handle:	handle of the loaded image
 * @systable:	system table
 */
static int setup(const efi_handle_t img_handle,
		 const struct efi_system_table *systable)
{
	int ret;

	boottime = systable->boottime;

	ret = boottime->locate_protocol(&guid_device_path_utilities_protocol,
					NULL, (void **)&dpu);
	if (ret != EFI_SUCCESS) {
		dpu = NULL;
		efi_st_error(
			"Device path to text protocol is not available.\n");
		return EFI_ST_FAILURE;
	}

	return EFI_ST_SUCCESS;
}

/*
 * Create a device path consisting of a single media device node followed by an
 * end node.
 *
 * @length:	length of the media device node
 * @dp:		device path
 * @return:	status code
 */
static int create_single_node_device_path(unsigned int length,
					  struct efi_device_path **dp)
{
	struct efi_device_path *node;
	efi_uintn_t len;
	int ret;

	node = dpu->create_device_node(DEVICE_PATH_TYPE_MEDIA_DEVICE,
				       DEVICE_PATH_SUB_TYPE_FILE_PATH, length);
	if (!node) {
		efi_st_error("CreateDeviceNode failed\n");
		return EFI_ST_FAILURE;
	}
	*dp = dpu->append_device_node(NULL, node);
	if (!*dp) {
		efi_st_error("AppendDeviceNode failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(node);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	len = dpu->get_device_path_size(*dp);
	if (len != length + 4) {
		efi_st_error("Wrong device path length %u, expected %u\n",
			     (unsigned int)len, length);
		return EFI_ST_FAILURE;
	}
	return EFI_ST_SUCCESS;
}

/*
 * Execute unit test.
 *
 * In the test device paths are created, copied, and concatenated. The device
 * path length is used as a measure of success.
 */
static int execute(void)
{
	struct efi_device_path *dp1;
	struct efi_device_path *dp2;
	struct efi_device_path *dp3;

	efi_uintn_t len;
	int ret;

	/* IsDevicePathMultiInstance(NULL) */
	if (dpu->is_device_path_multi_instance(NULL)) {
		efi_st_error("IsDevicePathMultiInstance(NULL) returned true\n");
		return EFI_ST_FAILURE;
	}
	/* GetDevicePathSize(NULL) */
	len = dpu->get_device_path_size(NULL);
	if (len) {
		efi_st_error("Wrong device path length %u, expected 0\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	/* DuplicateDevicePath(NULL) */
	dp1 = dpu->duplicate_device_path(NULL);
	if (dp1) {
		efi_st_error("DuplicateDevicePath(NULL) failed\n");
		return EFI_ST_FAILURE;
	}
	/* AppendDevicePath(NULL, NULL) */
	dp1 = dpu->append_device_path(NULL, NULL);
	if (!dp1) {
		efi_st_error("AppendDevicePath(NULL, NULL) failed\n");
		return EFI_ST_FAILURE;
	}
	len = dpu->get_device_path_size(dp1);
	if (len != 4) {
		efi_st_error("Wrong device path length %u, expected 4\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp1);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	/* CreateDeviceNode */
	ret = create_single_node_device_path(21, &dp1);
	if (ret != EFI_ST_SUCCESS)
		return ret;
	ret = create_single_node_device_path(17, &dp2);
	if (ret != EFI_ST_SUCCESS)
		return ret;
	/* AppendDevicePath */
	dp3 = dpu->append_device_path(dp1, dp2);
	if (!dp3) {
		efi_st_error("AppendDevicePath failed\n");
		return EFI_ST_FAILURE;
	}
	if (dp3 == dp1 || dp3 == dp2) {
		efi_st_error("AppendDevicePath reused buffer\n");
		return EFI_ST_FAILURE;
	}
	len = dpu->get_device_path_size(dp3);
	/* 21 + 17 + 4 */
	if (len != 42) {
		efi_st_error("Wrong device path length %u, expected 42\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp2);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	/* AppendDeviceNode */
	dp2 = dpu->append_device_node(dp1, dp3);
	if (!dp2) {
		efi_st_error("AppendDevicePath failed\n");
		return EFI_ST_FAILURE;
	}
	len = dpu->get_device_path_size(dp2);
	/* 21 + 21 + 4 */
	if (len != 46) {
		printf("%s(%d) %s\n", __FILE__, __LINE__, __func__);
		efi_st_error("Wrong device path length %u, expected 46\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp1);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	/* IsDevicePathMultiInstance */
	if (dpu->is_device_path_multi_instance(dp2)) {
		printf("%s(%d) %s\n", __FILE__, __LINE__, __func__);
		efi_st_error("IsDevicePathMultiInstance returned true\n");
		return EFI_ST_FAILURE;
	}
	/* AppendDevicePathInstance */
	dp1 = dpu->append_device_path_instance(dp2, dp3);
	if (!dp1) {
		efi_st_error("AppendDevicePathInstance failed\n");
		return EFI_ST_FAILURE;
	}
	len = dpu->get_device_path_size(dp1);
	/* 46 + 42 */
	if (len != 88) {
		efi_st_error("Wrong device path length %u, expected 88\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	/* IsDevicePathMultiInstance */
	if (!dpu->is_device_path_multi_instance(dp1)) {
		efi_st_error("IsDevicePathMultiInstance returned false\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp2);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp3);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	/* GetNextDevicePathInstance */
	dp3 = dp1;
	dp2 = dpu->get_next_device_path_instance(&dp1, &len);
	if (!dp2) {
		efi_st_error("GetNextDevicePathInstance failed\n");
		return EFI_ST_FAILURE;
	}
	if (!dp1) {
		efi_st_error("GetNextDevicePathInstance no 2nd instance\n");
		return EFI_ST_FAILURE;
	}
	if (len != 46) {
		efi_st_error("Wrong device path length %u, expected 46\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	len = dpu->get_device_path_size(dp1);
	if (len != 42) {
		efi_st_error("Wrong device path length %u, expected 42\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp2);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	dp2 = dpu->get_next_device_path_instance(&dp1, &len);
	if (!dp2) {
		efi_st_error("GetNextDevicePathInstance failed\n");
		return EFI_ST_FAILURE;
	}
	if (len != 42) {
		efi_st_error("Wrong device path length %u, expected 46\n",
			     (unsigned int)len);
		return EFI_ST_FAILURE;
	}
	if (dp1) {
		efi_st_error("GetNextDevicePathInstance did not signal end\n");
		return EFI_ST_FAILURE;
	}

	/* Clean up */
	ret = boottime->free_pool(dp2);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(dp3);
	if (ret != EFI_ST_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}

	return EFI_ST_SUCCESS;
}

EFI_UNIT_TEST(dputil) = {
	.name = "device path utilities protocol",
	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
	.setup = setup,
	.execute = execute,
};