efi_selftest_exitbootservices.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * efi_selftest_exitbootservices
  3. *
  4. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * This unit test checks that the notification function of an
  9. * EVT_SIGNAL_EXIT_BOOT_SERVICES event is called exactly once.
  10. */
  11. #include <efi_selftest.h>
  12. static struct efi_boot_services *boottime;
  13. static struct efi_event *event_notify;
  14. static unsigned int notification_count;
  15. /*
  16. * Notification function, increments the notification count.
  17. *
  18. * @event notified event
  19. * @context pointer to the notification count
  20. */
  21. static void EFIAPI notify(struct efi_event *event, void *context)
  22. {
  23. unsigned int *count = context;
  24. ++*count;
  25. }
  26. /*
  27. * Setup unit test.
  28. *
  29. * Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event.
  30. *
  31. * @handle: handle of the loaded image
  32. * @systable: system table
  33. * @return: EFI_ST_SUCCESS for success
  34. */
  35. static int setup(const efi_handle_t handle,
  36. const struct efi_system_table *systable)
  37. {
  38. efi_status_t ret;
  39. boottime = systable->boottime;
  40. notification_count = 0;
  41. ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES,
  42. TPL_CALLBACK, notify,
  43. (void *)&notification_count,
  44. &event_notify);
  45. if (ret != EFI_SUCCESS) {
  46. efi_st_error("could not create event\n");
  47. return EFI_ST_FAILURE;
  48. }
  49. return EFI_ST_SUCCESS;
  50. }
  51. /*
  52. * Tear down unit test.
  53. *
  54. * Close the event created in setup.
  55. *
  56. * @return: EFI_ST_SUCCESS for success
  57. */
  58. static int teardown(void)
  59. {
  60. efi_status_t ret;
  61. if (event_notify) {
  62. ret = boottime->close_event(event_notify);
  63. event_notify = NULL;
  64. if (ret != EFI_SUCCESS) {
  65. efi_st_error("could not close event\n");
  66. return EFI_ST_FAILURE;
  67. }
  68. }
  69. return EFI_ST_SUCCESS;
  70. }
  71. /*
  72. * Execute unit test.
  73. *
  74. * Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES
  75. * event has been called.
  76. *
  77. * Call ExitBootServices again and check that the notification function is
  78. * not called again.
  79. *
  80. * @return: EFI_ST_SUCCESS for success
  81. */
  82. static int execute(void)
  83. {
  84. if (notification_count != 1) {
  85. efi_st_error("ExitBootServices was not notified\n");
  86. return EFI_ST_FAILURE;
  87. }
  88. efi_st_exit_boot_services();
  89. if (notification_count != 1) {
  90. efi_st_error("ExitBootServices was notified twice\n");
  91. return EFI_ST_FAILURE;
  92. }
  93. return EFI_ST_SUCCESS;
  94. }
  95. EFI_UNIT_TEST(exitbootservices) = {
  96. .name = "ExitBootServices",
  97. .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  98. .setup = setup,
  99. .execute = execute,
  100. .teardown = teardown,
  101. };