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