efi_selftest_exitbootservices.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 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 counter;
  15. /*
  16. * Notification function, increments a counter.
  17. *
  18. * @event notified event
  19. * @context pointer to the counter
  20. */
  21. static void EFIAPI notify(struct efi_event *event, void *context)
  22. {
  23. if (!context)
  24. return;
  25. ++*(unsigned int *)context;
  26. }
  27. /*
  28. * Setup unit test.
  29. *
  30. * Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event.
  31. *
  32. * @handle: handle of the loaded image
  33. * @systable: system table
  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. counter = 0;
  41. ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES,
  42. TPL_CALLBACK, notify, (void *)&counter,
  43. &event_notify);
  44. if (ret != EFI_SUCCESS) {
  45. efi_st_error("could not create event\n");
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. /*
  51. * Tear down unit test.
  52. *
  53. * Close the event created in setup.
  54. */
  55. static int teardown(void)
  56. {
  57. efi_status_t ret;
  58. if (event_notify) {
  59. ret = boottime->close_event(event_notify);
  60. event_notify = NULL;
  61. if (ret != EFI_SUCCESS) {
  62. efi_st_error("could not close event\n");
  63. return 1;
  64. }
  65. }
  66. return 0;
  67. }
  68. /*
  69. * Execute unit test.
  70. *
  71. * Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES
  72. * event has been called.
  73. *
  74. * Call ExitBootServices again and check that the notification function is
  75. * not called again.
  76. */
  77. static int execute(void)
  78. {
  79. if (counter != 1) {
  80. efi_st_error("ExitBootServices was not notified");
  81. return 1;
  82. }
  83. efi_st_exit_boot_services();
  84. if (counter != 1) {
  85. efi_st_error("ExitBootServices was notified twice");
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. EFI_UNIT_TEST(exitbootservices) = {
  91. .name = "ExitBootServices",
  92. .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  93. .setup = setup,
  94. .execute = execute,
  95. .teardown = teardown,
  96. };