efi_selftest_events.c 5.2 KB

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