xref: /openbmc/u-boot/lib/efi_selftest/efi_selftest_textinput.c (revision 7bcdf19572c8f64552233d04b309903b297d5b63)
1 /*
2  * efi_selftest_textinput
3  *
4  * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  *
8  * Provides a unit test for the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
9  * The unicode character and the scan code are printed for text
10  * input. To run the test:
11  *
12  *	setenv efi_selftest text input
13  *	bootefi selftest
14  */
15 
16 #include <efi_selftest.h>
17 
18 struct translate {
19 	u16 code;
20 	u16 *text;
21 };
22 
23 static struct efi_boot_services *boottime;
24 
25 static struct translate control_characters[] = {
26 	{0, L"Null"},
27 	{8, L"BS"},
28 	{9, L"TAB"},
29 	{10, L"LF"},
30 	{13, L"CR"},
31 	{0, NULL},
32 };
33 
34 static u16 ch[] = L"' '";
35 static u16 unknown[] = L"unknown";
36 
37 static struct translate scan_codes[] = {
38 	{0x00, L"Null"},
39 	{0x01, L"Up"},
40 	{0x02, L"Down"},
41 	{0x03, L"Right"},
42 	{0x04, L"Left"},
43 	{0x05, L"Home"},
44 	{0x06, L"End"},
45 	{0x07, L"Insert"},
46 	{0x08, L"Delete"},
47 	{0x09, L"Page Up"},
48 	{0x0a, L"Page Down"},
49 	{0x0b, L"FN 1"},
50 	{0x0c, L"FN 2"},
51 	{0x0d, L"FN 3"},
52 	{0x0e, L"FN 4"},
53 	{0x0f, L"FN 5"},
54 	{0x10, L"FN 6"},
55 	{0x11, L"FN 7"},
56 	{0x12, L"FN 8"},
57 	{0x13, L"FN 9"},
58 	{0x14, L"FN 10"},
59 	{0x15, L"FN 11"},
60 	{0x16, L"FN 12"},
61 	{0x17, L"Escape"},
62 	{0x68, L"FN 13"},
63 	{0x69, L"FN 14"},
64 	{0x6a, L"FN 15"},
65 	{0x6b, L"FN 16"},
66 	{0x6c, L"FN 17"},
67 	{0x6d, L"FN 18"},
68 	{0x6e, L"FN 19"},
69 	{0x6f, L"FN 20"},
70 	{0x70, L"FN 21"},
71 	{0x71, L"FN 22"},
72 	{0x72, L"FN 23"},
73 	{0x73, L"FN 24"},
74 	{0x7f, L"Mute"},
75 	{0x80, L"Volume Up"},
76 	{0x81, L"Volume Down"},
77 	{0x100, L"Brightness Up"},
78 	{0x101, L"Brightness Down"},
79 	{0x102, L"Suspend"},
80 	{0x103, L"Hibernate"},
81 	{0x104, L"Toggle Display"},
82 	{0x105, L"Recovery"},
83 	{0x106, L"Reject"},
84 	{0x0, NULL},
85 };
86 
87 /*
88  * Translate a unicode character to a string.
89  *
90  * @code	unicode character
91  * @return	string
92  */
93 static u16 *translate_char(u16 code)
94 {
95 	struct translate *tr;
96 
97 	if (code >= ' ') {
98 		ch[1] = code;
99 		return ch;
100 	}
101 	for (tr = control_characters; tr->text; ++tr) {
102 		if (tr->code == code)
103 			return tr->text;
104 	}
105 	return unknown;
106 }
107 
108 /*
109  * Translate a scan code to a human readable string.
110  *
111  * @code	unicode character
112  * @return	string
113  */
114 static u16 *translate_code(u16 code)
115 {
116 	struct translate *tr;
117 
118 	for (tr = scan_codes; tr->text; ++tr) {
119 		if (tr->code == code)
120 			return tr->text;
121 	}
122 	return unknown;
123 }
124 
125 /*
126  * Setup unit test.
127  *
128  * @handle:	handle of the loaded image
129  * @systable:	system table
130  * @return:	EFI_ST_SUCCESS for success
131  */
132 static int setup(const efi_handle_t handle,
133 		 const struct efi_system_table *systable)
134 {
135 	boottime = systable->boottime;
136 
137 	return EFI_ST_SUCCESS;
138 }
139 
140 /*
141  * Execute unit test.
142  *
143  * @return:	EFI_ST_SUCCESS for success
144  */
145 static int execute(void)
146 {
147 	struct efi_input_key input_key = {0};
148 	efi_status_t ret;
149 
150 	efi_st_printf("Waiting for your input\n");
151 	efi_st_printf("To terminate type 'x'\n");
152 
153 	for (;;) {
154 		/* Wait for next key */
155 		do {
156 			ret = con_in->read_key_stroke(con_in, &input_key);
157 		} while (ret == EFI_NOT_READY);
158 
159 		/* Allow 5 minutes until time out */
160 		boottime->set_watchdog_timer(300, 0, 0, NULL);
161 
162 		efi_st_printf("Unicode char %u (%ps), scan code %u (%ps)\n",
163 			      (unsigned int)input_key.unicode_char,
164 			      translate_char(input_key.unicode_char),
165 			      (unsigned int)input_key.scan_code,
166 			      translate_code(input_key.scan_code));
167 
168 		switch (input_key.unicode_char) {
169 		case 'x':
170 		case 'X':
171 			return EFI_ST_SUCCESS;
172 		}
173 	}
174 }
175 
176 EFI_UNIT_TEST(textinput) = {
177 	.name = "text input",
178 	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
179 	.setup = setup,
180 	.execute = execute,
181 	.on_request = true,
182 };
183