blob: c4f66952b9f8bc28929fa7bcff7fbc787d74ca6f [file] [log] [blame]
Heinrich Schuchardtb277ced2017-09-15 10:06:15 +02001/*
2 * efi_selftest_events
3 *
4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This unit test uses timer events to check the implementation
9 * of the following boottime services:
10 * CreateEvent, CloseEvent, WaitForEvent, CheckEvent, SetTimer.
11 */
12
13#include <efi_selftest.h>
14
15static struct efi_event *event_notify;
16static struct efi_event *event_wait;
17static unsigned int counter;
18static struct efi_boot_services *boottime;
19
20/*
21 * Notification function, increments a counter.
22 *
23 * @event notified event
24 * @context pointer to the counter
25 */
26static void EFIAPI notify(struct efi_event *event, void *context)
27{
28 if (!context)
29 return;
30 ++*(unsigned int *)context;
31}
32
33/*
34 * Setup unit test.
35 *
36 * Create two timer events.
37 * One with EVT_NOTIFY_SIGNAL, the other with EVT_NOTIFY_WAIT.
38 *
39 * @handle: handle of the loaded image
40 * @systable: system table
41 */
42static int setup(const efi_handle_t handle,
43 const struct efi_system_table *systable)
44{
45 efi_status_t ret;
46
47 boottime = systable->boottime;
48
49 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL,
50 TPL_CALLBACK, notify, (void *)&counter,
51 &event_notify);
52 if (ret != EFI_SUCCESS) {
53 efi_st_error("could not create event\n");
54 return 1;
55 }
56 ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT,
57 TPL_CALLBACK, notify, NULL, &event_wait);
58 if (ret != EFI_SUCCESS) {
59 efi_st_error("could not create event\n");
60 return 1;
61 }
62 return 0;
63}
64
65/*
66 * Tear down unit test.
67 *
68 * Close the events created in setup.
69 */
70static int teardown(void)
71{
72 efi_status_t ret;
73
74 if (event_notify) {
75 ret = boottime->close_event(event_notify);
76 event_notify = NULL;
77 if (ret != EFI_SUCCESS) {
78 efi_st_error("could not close event\n");
79 return 1;
80 }
81 }
82 if (event_wait) {
83 ret = boottime->close_event(event_wait);
84 event_wait = NULL;
85 if (ret != EFI_SUCCESS) {
86 efi_st_error("could not close event\n");
87 return 1;
88 }
89 }
90 return 0;
91}
92
93/*
94 * Execute unit test.
95 *
96 * Run a 10 ms periodic timer and check that it is called 10 times
97 * while waiting for 100 ms single shot timer.
98 *
99 * Run a 100 ms single shot timer and check that it is called once
100 * while waiting for 100 ms periodic timer for two periods.
101 */
102static int execute(void)
103{
104 unsigned long index;
105 efi_status_t ret;
106
107 /* Set 10 ms timer */
108 counter = 0;
109 ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000);
110 if (ret != EFI_SUCCESS) {
111 efi_st_error("Could not set timer\n");
112 return 1;
113 }
114 /* Set 100 ms timer */
115 ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000);
116 if (ret != EFI_SUCCESS) {
117 efi_st_error("Could not set timer\n");
118 return 1;
119 }
120
121 index = 5;
122 ret = boottime->wait_for_event(1, &event_wait, &index);
123 if (ret != EFI_SUCCESS) {
124 efi_st_error("Could not wait for event\n");
125 return 1;
126 }
127 ret = boottime->check_event(event_wait);
128 if (ret != EFI_NOT_READY) {
129 efi_st_error("Signaled state was not cleared.\n");
130 efi_st_printf("ret = %u\n", (unsigned int)ret);
131 return 1;
132 }
133 if (index != 0) {
134 efi_st_error("WaitForEvent returned wrong index\n");
135 return 1;
136 }
137 efi_st_printf("Counter periodic: %u\n", counter);
138 if (counter < 8 || counter > 12) {
139 efi_st_error("Incorrect timing of events\n");
140 return 1;
141 }
142 ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0);
143 if (index != 0) {
144 efi_st_error("Could not cancel timer\n");
145 return 1;
146 }
147 /* Set 10 ms timer */
148 counter = 0;
149 ret = boottime->set_timer(event_notify, EFI_TIMER_RELATIVE, 100000);
150 if (index != 0) {
151 efi_st_error("Could not set timer\n");
152 return 1;
153 }
154 /* Set 100 ms timer */
155 ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000);
156 if (index != 0) {
157 efi_st_error("Could not set timer\n");
158 return 1;
159 }
160 ret = boottime->wait_for_event(1, &event_wait, &index);
161 if (ret != EFI_SUCCESS) {
162 efi_st_error("Could not wait for event\n");
163 return 1;
164 }
165 efi_st_printf("Counter single shot: %u\n", counter);
166 if (counter != 1) {
167 efi_st_error("Single shot timer failed\n");
168 return 1;
169 }
170 ret = boottime->wait_for_event(1, &event_wait, &index);
171 if (ret != EFI_SUCCESS) {
172 efi_st_error("Could not wait for event\n");
173 return 1;
174 }
175 efi_st_printf("Stopped counter: %u\n", counter);
176 if (counter != 1) {
177 efi_st_error("Stopped timer fired\n");
178 return 1;
179 }
180 ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0);
181 if (index != 0) {
182 efi_st_error("Could not cancel timer\n");
183 return 1;
184 }
185
186 return 0;
187}
188
189EFI_UNIT_TEST(events) = {
190 .name = "event services",
191 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
192 .setup = setup,
193 .execute = execute,
194 .teardown = teardown,
195};