aboutsummaryrefslogtreecommitdiff
path: root/lib/efi_selftest/efi_selftest_exitbootservices.c
blob: 59ab15407a00b875b60209f992ad31f76b2d51bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: GPL-2.0+
/*
 * efi_selftest_exitbootservices
 *
 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
 *
 * This unit test checks that the notification function of an
 * EVT_SIGNAL_EXIT_BOOT_SERVICES event is called exactly once.
 */

#include <efi_selftest.h>

static efi_guid_t guid_before_exit_boot_services =
	EFI_GUID(0x8be0e274, 0x3970, 0x4b44, 0x80, 0xc5,
		 0x1a, 0xb9, 0x50, 0x2f, 0x3b, 0xfc);
#define CAPACITY 4

struct notification_record {
	unsigned int count;
	unsigned int type[CAPACITY];
};

struct notification_context {
	struct notification_record *record;
	unsigned int type;
};

static struct efi_boot_services *boottime;
static struct efi_event *event_notify;
struct notification_record record;

struct notification_context context_before = {
	.record = &record,
	.type = 1,
};

struct notification_context context = {
	.record = &record,
	.type = 2,
};

/*
 * Notification function, increments the notification count.
 *
 * @event	notified event
 * @context	pointer to the notification count
 */
static void EFIAPI ebs_notify(struct efi_event *event, void *context)
{
	struct notification_context *ctx = context;

	if (ctx->record->count >= CAPACITY)
		return;

	ctx->record->type[ctx->record->count] = ctx->type;
	ctx->record->count++;
}

/*
 * Setup unit test.
 *
 * Create an EVT_SIGNAL_EXIT_BOOT_SERVICES event.
 *
 * @handle:	handle of the loaded image
 * @systable:	system table
 * @return:	EFI_ST_SUCCESS for success
 */
static int setup(const efi_handle_t handle,
		 const struct efi_system_table *systable)
{
	efi_status_t ret;

	boottime = systable->boottime;

	ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES,
				     TPL_CALLBACK, ebs_notify,
				     &context,
				     &event_notify);
	if (ret != EFI_SUCCESS) {
		efi_st_error("could not create event\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->create_event_ex(0, TPL_CALLBACK, ebs_notify,
					&context_before,
					&guid_before_exit_boot_services,
					&event_notify);
	if (ret != EFI_SUCCESS) {
		efi_st_error("could not create event\n");
		return EFI_ST_FAILURE;
	}

	return EFI_ST_SUCCESS;
}

/*
 * Execute unit test.
 *
 * Check that the notification function of the EVT_SIGNAL_EXIT_BOOT_SERVICES
 * event has been called.
 *
 * Call ExitBootServices again and check that the notification function is
 * not called again.
 *
 * @return:	EFI_ST_SUCCESS for success
 */
static int execute(void)
{
	if (record.count != 2) {
		efi_st_error("Incorrect event count %u\n", record.count);
		return EFI_ST_FAILURE;
	}
	if (record.type[0] != 1) {
		efi_st_error("EFI_GROUP_BEFORE_EXIT_BOOT_SERVICE not notified\n");
		return EFI_ST_FAILURE;
	}
	if (record.type[1] != 2) {
		efi_st_error("EVT_SIGNAL_EXIT_BOOT_SERVICES was not notified\n");
		return EFI_ST_FAILURE;
	}
	efi_st_exit_boot_services();
	if (record.count != 2) {
		efi_st_error("Incorrect event count %u\n", record.count);
		return EFI_ST_FAILURE;
	}
	return EFI_ST_SUCCESS;
}

EFI_UNIT_TEST(exitbootservices) = {
	.name = "ExitBootServices",
	.phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
	.setup = setup,
	.execute = execute,
};