1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * efi_selftest_exitbootservices
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
7 * 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
13 static efi_guid_t guid_before_exit_boot_services =
14 EFI_GUID(0x8be0e274, 0x3970, 0x4b44, 0x80, 0xc5,
15 0x1a, 0xb9, 0x50, 0x2f, 0x3b, 0xfc);
16 #define CAPACITY 4
17
18 struct notification_record {
19 unsigned int count;
20 unsigned int type[CAPACITY];
21 };
22
23 struct notification_context {
24 struct notification_record *record;
25 unsigned int type;
26 };
27
28 static struct efi_boot_services *boottime;
29 static struct efi_event *efi_st_event_notify;
30 static struct notification_record record;
31
32 struct notification_context context_before = {
33 .record = &record,
34 .type = 1,
35 };
36
37 struct notification_context context = {
38 .record = &record,
39 .type = 2,
40 };
41
42 /*
43 * Notification function, increments the notification count.
44 *
45 * @event notified event
46 * @context pointer to the notification count
47 */
ebs_notify(struct efi_event * event,void * context)48 static void EFIAPI ebs_notify(struct efi_event *event, void *context)
49 {
50 struct notification_context *ctx = context;
51
52 if (ctx->record->count >= CAPACITY)
53 return;
54
55 ctx->record->type[ctx->record->count] = ctx->type;
56 ctx->record->count++;
57 }
58
59 /*
60 * Setup unit test.
61 *
62 * Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event.
63 *
64 * @handle: handle of the loaded image
65 * @systable: system table
66 * Return: EFI_ST_SUCCESS for success
67 */
setup(const efi_handle_t handle,const struct efi_system_table * systable)68 static int setup(const efi_handle_t handle,
69 const struct efi_system_table *systable)
70 {
71 efi_status_t ret;
72
73 boottime = systable->boottime;
74
75 ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES,
76 TPL_CALLBACK, ebs_notify,
77 &context,
78 &efi_st_event_notify);
79 if (ret != EFI_SUCCESS) {
80 efi_st_error("could not create event\n");
81 return EFI_ST_FAILURE;
82 }
83 ret = boottime->create_event_ex(0, TPL_CALLBACK, ebs_notify,
84 &context_before,
85 &guid_before_exit_boot_services,
86 &efi_st_event_notify);
87 if (ret != EFI_SUCCESS) {
88 efi_st_error("could not create event\n");
89 return EFI_ST_FAILURE;
90 }
91
92 return EFI_ST_SUCCESS;
93 }
94
95 /*
96 * Execute unit test.
97 *
98 * Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES
99 * event has been called.
100 *
101 * Call ExitBootServices again and check that the notification function is
102 * not called again.
103 *
104 * Return: EFI_ST_SUCCESS for success
105 */
execute(void)106 static int execute(void)
107 {
108 if (record.count != 2) {
109 efi_st_error("Incorrect event count %u\n", record.count);
110 return EFI_ST_FAILURE;
111 }
112 if (record.type[0] != 1) {
113 efi_st_error("EFI_GROUP_BEFORE_EXIT_BOOT_SERVICE not notified\n");
114 return EFI_ST_FAILURE;
115 }
116 if (record.type[1] != 2) {
117 efi_st_error("EVT_SIGNAL_EXIT_BOOT_SERVICES was not notified\n");
118 return EFI_ST_FAILURE;
119 }
120 efi_st_exit_boot_services();
121 if (record.count != 2) {
122 efi_st_error("Incorrect event count %u\n", record.count);
123 return EFI_ST_FAILURE;
124 }
125 return EFI_ST_SUCCESS;
126 }
127
128 EFI_UNIT_TEST(exitbootservices) = {
129 .name = "ExitBootServices",
130 .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
131 .setup = setup,
132 .execute = execute,
133 };
134