efi_selftest_event_groups.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * efi_selftest_event_groups
  3. *
  4. * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * This test checks the notification of group events and the
  9. * following services:
  10. * CreateEventEx, CloseEvent, SignalEvent, CheckEvent.
  11. */
  12. #include <efi_selftest.h>
  13. #define GROUP_SIZE 16
  14. static struct efi_boot_services *boottime;
  15. static efi_guid_t event_group =
  16. EFI_GUID(0x2335905b, 0xc3b9, 0x4221, 0xa3, 0x71,
  17. 0x0e, 0x5b, 0x45, 0xc0, 0x56, 0x91);
  18. /*
  19. * Notification function, increments the notfication count if parameter
  20. * context is provided.
  21. *
  22. * @event notified event
  23. * @context pointer to the notification count
  24. */
  25. static void EFIAPI notify(struct efi_event *event, void *context)
  26. {
  27. unsigned int *count = context;
  28. if (count)
  29. ++*count;
  30. }
  31. /*
  32. * Setup unit test.
  33. *
  34. * @handle: handle of the loaded image
  35. * @systable: system table
  36. * @return: EFI_ST_SUCCESS for success
  37. */
  38. static int setup(const efi_handle_t handle,
  39. const struct efi_system_table *systable)
  40. {
  41. boottime = systable->boottime;
  42. return EFI_ST_SUCCESS;
  43. }
  44. /*
  45. * Execute unit test.
  46. *
  47. * Create multiple events in an event group. Signal each event once and check
  48. * that all events are notified once in each round.
  49. *
  50. * @return: EFI_ST_SUCCESS for success
  51. */
  52. static int execute(void)
  53. {
  54. unsigned int counter[GROUP_SIZE] = {0};
  55. struct efi_event *events[GROUP_SIZE];
  56. size_t i, j;
  57. efi_status_t ret;
  58. for (i = 0; i < GROUP_SIZE; ++i) {
  59. ret = boottime->create_event_ex(0, TPL_NOTIFY,
  60. notify, (void *)&counter[i],
  61. &event_group, &events[i]);
  62. if (ret != EFI_SUCCESS) {
  63. efi_st_error("Failed to create event\n");
  64. return EFI_ST_FAILURE;
  65. }
  66. }
  67. for (i = 0; i < GROUP_SIZE; ++i) {
  68. ret = boottime->signal_event(events[i]);
  69. if (ret != EFI_SUCCESS) {
  70. efi_st_error("Failed to signal event\n");
  71. return EFI_ST_FAILURE;
  72. }
  73. for (j = 0; j < GROUP_SIZE; ++j) {
  74. if (counter[j] != i) {
  75. efi_st_printf("i %u, j %u, count %u\n",
  76. (unsigned int)i, (unsigned int)j,
  77. (unsigned int)counter[j]);
  78. efi_st_error(
  79. "Notification function was called\n");
  80. return EFI_ST_FAILURE;
  81. }
  82. /* Clear signaled state */
  83. ret = boottime->check_event(events[j]);
  84. if (ret != EFI_SUCCESS) {
  85. efi_st_error("Event was not signaled\n");
  86. return EFI_ST_FAILURE;
  87. }
  88. if (counter[j] != i) {
  89. efi_st_printf("i %u, j %u, count %u\n",
  90. (unsigned int)i, (unsigned int)j,
  91. (unsigned int)counter[j]);
  92. efi_st_error(
  93. "Notification function was called\n");
  94. return EFI_ST_FAILURE;
  95. }
  96. /* Call notification function */
  97. ret = boottime->check_event(events[j]);
  98. if (ret != EFI_NOT_READY) {
  99. efi_st_error(
  100. "Signaled state not cleared\n");
  101. return EFI_ST_FAILURE;
  102. }
  103. if (counter[j] != i + 1) {
  104. efi_st_printf("i %u, j %u, count %u\n",
  105. (unsigned int)i, (unsigned int)j,
  106. (unsigned int)counter[j]);
  107. efi_st_error(
  108. "Nofification function not called\n");
  109. return EFI_ST_FAILURE;
  110. }
  111. }
  112. }
  113. for (i = 0; i < GROUP_SIZE; ++i) {
  114. ret = boottime->close_event(events[i]);
  115. if (ret != EFI_SUCCESS) {
  116. efi_st_error("Failed to close event\n");
  117. return EFI_ST_FAILURE;
  118. }
  119. }
  120. return EFI_ST_SUCCESS;
  121. }
  122. EFI_UNIT_TEST(eventgoups) = {
  123. .name = "event groups",
  124. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  125. .setup = setup,
  126. .execute = execute,
  127. };