summaryrefslogtreecommitdiff
path: root/lib/efi_selftest/efi_selftest_textinputex.c
blob: de44224ce1c08800ef45275989dfe54f7df666b4 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: GPL-2.0+
/*
 * efi_selftest_textinput
 *
 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
 *
 * Provides a unit test for the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
 * The unicode character and the scan code are printed for text
 * input. To run the test:
 *
 *	setenv efi_selftest extended text input
 *	bootefi selftest
 */

#include <efi_selftest.h>

static const efi_guid_t text_input_ex_protocol_guid =
		EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;

static struct efi_simple_text_input_ex_protocol *con_in_ex;

static struct efi_boot_services *boottime;

static void *efi_key_notify_handle;
static bool efi_running;

/**
 * efi_key_notify_function() - key notification function
 *
 * This function is called when the registered key is hit.
 *
 * @key_data:		next key
 * Return:		status code
 */
static efi_status_t EFIAPI efi_key_notify_function
				(struct efi_key_data *key_data)
{
	efi_running = false;

	return EFI_SUCCESS;
}

/*
 * Setup unit test.
 *
 * @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;
	struct efi_key_data key_data = {
		.key = {
			.scan_code = 0,
			.unicode_char = 0x18
		},
		.key_state = {
			.key_shift_state = EFI_SHIFT_STATE_VALID |
					   EFI_LEFT_CONTROL_PRESSED,
			.key_toggle_state = EFI_TOGGLE_STATE_INVALID,
		},
	};

	boottime = systable->boottime;

	ret = boottime->locate_protocol(&text_input_ex_protocol_guid, NULL,
					(void **)&con_in_ex);
	if (ret != EFI_SUCCESS) {
		con_in_ex = NULL;
		efi_st_error
			("Extended text input protocol is not available.\n");
		return EFI_ST_FAILURE;
	}

	ret = con_in_ex->register_key_notify(con_in_ex, &key_data,
					     efi_key_notify_function,
					     &efi_key_notify_handle);
	if (ret != EFI_SUCCESS) {
		efi_key_notify_handle = NULL;
		efi_st_error
			("Notify function could not be registered.\n");
		return EFI_ST_FAILURE;
	}
	efi_running = true;

	return EFI_ST_SUCCESS;
}

/*
 * Tear down unit test.
 *
 * Unregister notify function.
 *
 * @return:	EFI_ST_SUCCESS for success
 */
static int teardown(void)
{
	efi_status_t ret;

	ret = con_in_ex->unregister_key_notify
			(con_in_ex, efi_key_notify_handle);
	if (ret != EFI_SUCCESS) {
		efi_st_error
			("Notify function could not be registered.\n");
		return EFI_ST_FAILURE;
	}

	return EFI_ST_SUCCESS;
}
/*
 * Execute unit test.
 *
 * @return:	EFI_ST_SUCCESS for success
 */
static int execute(void)
{
	struct efi_key_data input_key = { {0, 0}, {0, 0} };
	efi_status_t ret;
	efi_uintn_t index;

	if (!con_in_ex) {
		efi_st_printf("Setup failed\n");
		return EFI_ST_FAILURE;
	}

	/* Drain the console input */
	ret = con_in_ex->reset(con_in_ex, true);
	if (ret != EFI_SUCCESS) {
		efi_st_error("Reset failed\n");
		return EFI_ST_FAILURE;
	}
	ret = con_in_ex->read_key_stroke_ex(con_in_ex, &input_key);
	if (ret != EFI_NOT_READY) {
		efi_st_error("Empty buffer not reported\n");
		return EFI_ST_FAILURE;
	}

	efi_st_printf("Waiting for your input\n");
	efi_st_printf("To terminate type 'CTRL+x'\n");

	while (efi_running) {
		/* Wait for next key */
		ret = boottime->wait_for_event(1, &con_in_ex->wait_for_key_ex,
					       &index);
		if (ret != EFI_ST_SUCCESS) {
			efi_st_error("WaitForEvent failed\n");
			return EFI_ST_FAILURE;
		}
		ret = con_in_ex->read_key_stroke_ex(con_in_ex, &input_key);
		if (ret != EFI_SUCCESS) {
			efi_st_error("ReadKeyStroke failed\n");
			return EFI_ST_FAILURE;
		}

		/* Allow 5 minutes until time out */
		boottime->set_watchdog_timer(300, 0, 0, NULL);

		efi_st_printf("Unicode char %u (%ps), scan code %u (",
			      (unsigned int)input_key.key.unicode_char,
			      efi_st_translate_char(input_key.key.unicode_char),
			      (unsigned int)input_key.key.scan_code);
		if (input_key.key_state.key_shift_state &
		    EFI_SHIFT_STATE_VALID) {
			if (input_key.key_state.key_shift_state &
			    (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED))
				efi_st_printf("SHIFT+");
			if (input_key.key_state.key_shift_state &
			    (EFI_LEFT_ALT_PRESSED | EFI_RIGHT_ALT_PRESSED))
				efi_st_printf("ALT+");
			if (input_key.key_state.key_shift_state &
			    (EFI_LEFT_CONTROL_PRESSED |
			     EFI_RIGHT_CONTROL_PRESSED))
				efi_st_printf("CTRL+");
			if (input_key.key_state.key_shift_state &
			    (EFI_LEFT_LOGO_PRESSED | EFI_RIGHT_LOGO_PRESSED))
				efi_st_printf("META+");
			if (input_key.key_state.key_shift_state ==
			    EFI_SHIFT_STATE_VALID)
				efi_st_printf("+");
		}

		efi_st_printf("%ps)\n",
			      efi_st_translate_code(input_key.key.scan_code));

	}
	return EFI_ST_SUCCESS;
}

EFI_UNIT_TEST(textinputex) = {
	.name = "extended text input",
	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
	.setup = setup,
	.execute = execute,
	.teardown = teardown,
	.on_request = true,
};