efi_selftest_textinput.c 3.4 KB

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