efi_selftest_events.c 4.5 KB

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