efi_selftest: refactor text input test
[oweals/u-boot.git] / lib / efi_selftest / efi_selftest_textinput.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_textinput
4  *
5  * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * Provides a unit test for the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
8  * The unicode character and the scan code are printed for text
9  * input. To run the test:
10  *
11  *      setenv efi_selftest text input
12  *      bootefi selftest
13  */
14
15 #include <efi_selftest.h>
16
17 static struct efi_boot_services *boottime;
18
19 /*
20  * Setup unit test.
21  *
22  * @handle:     handle of the loaded image
23  * @systable:   system table
24  * @return:     EFI_ST_SUCCESS for success
25  */
26 static int setup(const efi_handle_t handle,
27                  const struct efi_system_table *systable)
28 {
29         boottime = systable->boottime;
30
31         return EFI_ST_SUCCESS;
32 }
33
34 /*
35  * Execute unit test.
36  *
37  * @return:     EFI_ST_SUCCESS for success
38  */
39 static int execute(void)
40 {
41         struct efi_input_key input_key = {0};
42         efi_status_t ret;
43
44         efi_st_printf("Waiting for your input\n");
45         efi_st_printf("To terminate type 'x'\n");
46
47         for (;;) {
48                 /* Wait for next key */
49                 do {
50                         ret = con_in->read_key_stroke(con_in, &input_key);
51                 } while (ret == EFI_NOT_READY);
52
53                 /* Allow 5 minutes until time out */
54                 boottime->set_watchdog_timer(300, 0, 0, NULL);
55
56                 efi_st_printf("Unicode char %u (%ps), scan code %u (%ps)\n",
57                               (unsigned int)input_key.unicode_char,
58                               efi_st_translate_char(input_key.unicode_char),
59                               (unsigned int)input_key.scan_code,
60                               efi_st_translate_code(input_key.scan_code));
61
62                 switch (input_key.unicode_char) {
63                 case 'x':
64                 case 'X':
65                         return EFI_ST_SUCCESS;
66                 }
67         }
68 }
69
70 EFI_UNIT_TEST(textinput) = {
71         .name = "text input",
72         .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
73         .setup = setup,
74         .execute = execute,
75         .on_request = true,
76 };