efi_selftest_textinput.c 3.4 KB

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