efi_selftest_textinput.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. static struct efi_boot_services *boottime;
  16. /*
  17. * Setup unit test.
  18. *
  19. * @handle: handle of the loaded image
  20. * @systable: system table
  21. * @return: EFI_ST_SUCCESS for success
  22. */
  23. static int setup(const efi_handle_t handle,
  24. const struct efi_system_table *systable)
  25. {
  26. boottime = systable->boottime;
  27. return EFI_ST_SUCCESS;
  28. }
  29. /*
  30. * Execute unit test.
  31. *
  32. * @return: EFI_ST_SUCCESS for success
  33. */
  34. static int execute(void)
  35. {
  36. struct efi_input_key input_key = {0};
  37. efi_status_t ret;
  38. efi_uintn_t index;
  39. /* Drain the console input */
  40. ret = con_in->reset(con_in, true);
  41. if (ret != EFI_SUCCESS) {
  42. efi_st_error("Reset failed\n");
  43. return EFI_ST_FAILURE;
  44. }
  45. ret = con_in->read_key_stroke(con_in, &input_key);
  46. if (ret != EFI_NOT_READY) {
  47. efi_st_error("Empty buffer not reported\n");
  48. return EFI_ST_FAILURE;
  49. }
  50. efi_st_printf("Waiting for your input\n");
  51. efi_st_printf("To terminate type 'x'\n");
  52. for (;;) {
  53. /* Wait for next key */
  54. ret = boottime->wait_for_event(1, &con_in->wait_for_key,
  55. &index);
  56. if (ret != EFI_ST_SUCCESS) {
  57. efi_st_error("WaitForEvent failed\n");
  58. return EFI_ST_FAILURE;
  59. }
  60. ret = con_in->read_key_stroke(con_in, &input_key);
  61. if (ret != EFI_SUCCESS) {
  62. efi_st_error("ReadKeyStroke failed\n");
  63. return EFI_ST_FAILURE;
  64. }
  65. /* Allow 5 minutes until time out */
  66. boottime->set_watchdog_timer(300, 0, 0, NULL);
  67. efi_st_printf("Unicode char %u (%ps), scan code %u (%ps)\n",
  68. (unsigned int)input_key.unicode_char,
  69. efi_st_translate_char(input_key.unicode_char),
  70. (unsigned int)input_key.scan_code,
  71. efi_st_translate_code(input_key.scan_code));
  72. switch (input_key.unicode_char) {
  73. case 'x':
  74. case 'X':
  75. return EFI_ST_SUCCESS;
  76. }
  77. }
  78. }
  79. EFI_UNIT_TEST(textinput) = {
  80. .name = "text input",
  81. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  82. .setup = setup,
  83. .execute = execute,
  84. .on_request = true,
  85. };