blob: 4fecd1b41519830bef31fc466344f91bb244d092 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt630d1342017-09-15 10:06:19 +02002/*
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +02003 * efi_selftest_exitbootservices
Heinrich Schuchardt630d1342017-09-15 10:06:19 +02004 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardt630d1342017-09-15 10:06:19 +02007 * This unit test checks that the notification function of an
8 * EVT_SIGNAL_EXIT_BOOT_SERVICES event is called exactly once.
9 */
10
11#include <efi_selftest.h>
12
13static struct efi_boot_services *boottime;
14static struct efi_event *event_notify;
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020015static unsigned int notification_count;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020016
17/*
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020018 * Notification function, increments the notification count.
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020019 *
20 * @event notified event
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020021 * @context pointer to the notification count
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020022 */
23static void EFIAPI notify(struct efi_event *event, void *context)
24{
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020025 unsigned int *count = context;
26
27 ++*count;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020028}
29
30/*
31 * Setup unit test.
32 *
33 * Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event.
34 *
35 * @handle: handle of the loaded image
36 * @systable: system table
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020037 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020038 */
39static int setup(const efi_handle_t handle,
40 const struct efi_system_table *systable)
41{
42 efi_status_t ret;
43
44 boottime = systable->boottime;
45
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020046 notification_count = 0;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020047 ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES,
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020048 TPL_CALLBACK, notify,
49 (void *)&notification_count,
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020050 &event_notify);
51 if (ret != EFI_SUCCESS) {
52 efi_st_error("could not create event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020053 return EFI_ST_FAILURE;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020054 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020055 return EFI_ST_SUCCESS;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020056}
57
58/*
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020059 * Execute unit test.
60 *
61 * Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES
62 * event has been called.
63 *
64 * Call ExitBootServices again and check that the notification function is
65 * not called again.
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020066 *
67 * @return: EFI_ST_SUCCESS for success
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020068 */
69static int execute(void)
70{
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020071 if (notification_count != 1) {
72 efi_st_error("ExitBootServices was not notified\n");
73 return EFI_ST_FAILURE;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020074 }
75 efi_st_exit_boot_services();
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020076 if (notification_count != 1) {
77 efi_st_error("ExitBootServices was notified twice\n");
78 return EFI_ST_FAILURE;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020079 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020080 return EFI_ST_SUCCESS;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020081}
82
83EFI_UNIT_TEST(exitbootservices) = {
84 .name = "ExitBootServices",
85 .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
86 .setup = setup,
87 .execute = execute,
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020088};