summaryrefslogtreecommitdiff
path: root/lib/efi_selftest/efi_selftest_open_protocol.c
blob: e3f351df7237a99c372733b2f2c63e5adf17fe77 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * efi_selftest_open_protocol
 *
 * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
 *
 * This unit test checks that open protocol information is correctly updated
 * when calling:
 * HandleProtocol, OpenProtocol, OpenProtocolInformation, CloseProtocol.
 */

#include <efi_selftest.h>

/*
 * The test currently does not actually call the interface function.
 * So this is just a dummy structure.
 */
struct interface {
	void (EFIAPI *inc)(void);
};

static struct efi_boot_services *boottime;
static efi_guid_t guid1 =
	EFI_GUID(0x492a0e38, 0x1442, 0xf819,
		 0x14, 0xaa, 0x4b, 0x8d, 0x09, 0xfe, 0x5a, 0xb9);
static efi_handle_t handle1;
static struct interface interface1;

/*
 * Setup unit test.
 *
 * Create a handle and install a protocol interface on it.
 *
 * @handle:	handle of the loaded image
 * @systable:	system table
 */
static int setup(const efi_handle_t img_handle,
		 const struct efi_system_table *systable)
{
	efi_status_t ret;

	boottime = systable->boottime;

	ret = boottime->install_protocol_interface(&handle1, &guid1,
						   EFI_NATIVE_INTERFACE,
						   &interface1);
	if (ret != EFI_SUCCESS) {
		efi_st_error("InstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	if (!handle1) {
		efi_st_error
			("InstallProtocolInterface failed to create handle\n");
		return EFI_ST_FAILURE;
	}
	return EFI_ST_SUCCESS;
}

/*
 * Tear down unit test.
 *
 */
static int teardown(void)
{
	efi_status_t ret;

	if (handle1) {
		ret = boottime->uninstall_protocol_interface(handle1, &guid1,
							     &interface1);
		if (ret != EFI_SUCCESS) {
			efi_st_error("UninstallProtocolInterface failed\n");
			return EFI_ST_FAILURE;
		}
	}
	return EFI_ST_SUCCESS;
}

/*
 * Execute unit test.
 *
 * Open the installed protocol twice via HandleProtocol() and once via
 * OpenProtocol(EFI_OPEN_PROTOCOL_GET_PROTOCOL). Read the open protocol
 * information and check the open counts. Finally close the protocol and
 * check again.
 */
static int execute(void)
{
	void *interface;
	struct efi_open_protocol_info_entry *entry_buffer;
	efi_uintn_t entry_count;
	efi_handle_t firmware_handle;
	efi_status_t ret;

	ret = boottime->handle_protocol(handle1, &guid1, &interface);
	if (ret != EFI_SUCCESS) {
		efi_st_error("HandleProtocol failed\n");
		return EFI_ST_FAILURE;
	}
	if (interface != &interface1) {
		efi_st_error("HandleProtocol returned wrong interface\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->open_protocol_information(handle1, &guid1,
						  &entry_buffer, &entry_count);
	if (ret != EFI_SUCCESS) {
		efi_st_error("OpenProtocolInformation failed\n");
		return EFI_ST_FAILURE;
	}
	if (entry_count != 1) {
		efi_st_error("Incorrect OpenProtocolInformation count\n");
		efi_st_printf("Expected 1, got %u\n",
			      (unsigned int)entry_count);
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(entry_buffer);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->handle_protocol(handle1, &guid1, &interface);
	if (ret != EFI_SUCCESS) {
		efi_st_error("HandleProtocol failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->open_protocol_information(handle1, &guid1,
						  &entry_buffer, &entry_count);
	if (ret != EFI_SUCCESS) {
		efi_st_error("OpenProtocolInformation failed\n");
		return EFI_ST_FAILURE;
	}
	if (entry_count != 1) {
		efi_st_error("Incorrect OpenProtocolInformation count\n");
		efi_st_printf("Expected 1, got %u\n",
			      (unsigned int)entry_count);
		return EFI_ST_FAILURE;
	}
	if (entry_buffer[0].open_count != 2) {
		efi_st_error("Incorrect open count: expected 2 got %u\n",
			     entry_buffer[0].open_count);
		return EFI_ST_FAILURE;
	}
	firmware_handle = entry_buffer[0].agent_handle;
	ret = boottime->free_pool(entry_buffer);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->open_protocol(handle1, &guid1, &interface,
				      firmware_handle, NULL,
				      EFI_OPEN_PROTOCOL_GET_PROTOCOL);
	if (ret != EFI_SUCCESS) {
		efi_st_error("OpenProtocol failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->open_protocol_information(handle1, &guid1,
						  &entry_buffer, &entry_count);
	if (ret != EFI_SUCCESS) {
		efi_st_error("OpenProtocolInformation failed\n");
		return EFI_ST_FAILURE;
	}
	if (entry_count != 2) {
		efi_st_error("Incorrect OpenProtocolInformation count\n");
		efi_st_printf("Expected 2, got %u\n",
			      (unsigned int)entry_count);
		return EFI_ST_FAILURE;
	}
	if (entry_buffer[0].open_count + entry_buffer[1].open_count != 3) {
		efi_st_error("Incorrect open count: expected 3 got %u\n",
			     entry_buffer[0].open_count +
			     entry_buffer[1].open_count);
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(entry_buffer);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->close_protocol(handle1, &guid1, firmware_handle, NULL);
	if (ret != EFI_SUCCESS) {
		efi_st_error("CloseProtocol failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->open_protocol_information(handle1, &guid1,
						  &entry_buffer, &entry_count);
	if (ret != EFI_SUCCESS) {
		efi_st_error("OpenProtocolInformation failed\n");
		return EFI_ST_FAILURE;
	}
	if (entry_count) {
		efi_st_error("Incorrect OpenProtocolInformation count\n");
		efi_st_printf("Expected 0, got %u\n",
			      (unsigned int)entry_count);
		return EFI_ST_FAILURE;
	}

	return EFI_ST_SUCCESS;
}

EFI_UNIT_TEST(openprot) = {
	.name = "open protocol",
	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
	.setup = setup,
	.execute = execute,
	.teardown = teardown,
};