efi_selftest_events.c 5.1 KB

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