blob: f5e0d9da89bb58ff7fadb57d81fbd7d973fdd438 [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
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010013static 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
18struct notification_record {
19 unsigned int count;
20 unsigned int type[CAPACITY];
21};
22
23struct notification_context {
24 struct notification_record *record;
25 unsigned int type;
26};
27
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020028static struct efi_boot_services *boottime;
29static struct efi_event *event_notify;
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010030struct notification_record record;
31
32struct notification_context context_before = {
33 .record = &record,
34 .type = 1,
35};
36
37struct notification_context context = {
38 .record = &record,
39 .type = 2,
40};
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020041
42/*
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020043 * Notification function, increments the notification count.
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020044 *
45 * @event notified event
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020046 * @context pointer to the notification count
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020047 */
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010048static void EFIAPI ebs_notify(struct efi_event *event, void *context)
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020049{
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010050 struct notification_context *ctx = context;
51
52 if (ctx->record->count >= CAPACITY)
53 return;
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020054
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010055 ctx->record->type[ctx->record->count] = ctx->type;
56 ctx->record->count++;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020057}
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
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +010066 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020067 */
68static 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
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020075 ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES,
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010076 TPL_CALLBACK, ebs_notify,
77 &context,
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020078 &event_notify);
79 if (ret != EFI_SUCCESS) {
80 efi_st_error("could not create event\n");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020081 return EFI_ST_FAILURE;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020082 }
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +010083 ret = boottime->create_event_ex(0, TPL_CALLBACK, ebs_notify,
84 &context_before,
85 &guid_before_exit_boot_services,
86 &event_notify);
87 if (ret != EFI_SUCCESS) {
88 efi_st_error("could not create event\n");
89 return EFI_ST_FAILURE;
90 }
91
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020092 return EFI_ST_SUCCESS;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020093}
94
95/*
Heinrich Schuchardt630d1342017-09-15 10:06:19 +020096 * 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.
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200103 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100104 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt630d1342017-09-15 10:06:19 +0200105 */
106static int execute(void)
107{
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +0100108 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");
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200118 return EFI_ST_FAILURE;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +0200119 }
120 efi_st_exit_boot_services();
Heinrich Schuchardt4419c1d2021-11-17 18:52:35 +0100121 if (record.count != 2) {
122 efi_st_error("Incorrect event count %u\n", record.count);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200123 return EFI_ST_FAILURE;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +0200124 }
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200125 return EFI_ST_SUCCESS;
Heinrich Schuchardt630d1342017-09-15 10:06:19 +0200126}
127
128EFI_UNIT_TEST(exitbootservices) = {
129 .name = "ExitBootServices",
130 .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
131 .setup = setup,
132 .execute = execute,
Heinrich Schuchardt630d1342017-09-15 10:06:19 +0200133};