You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_console.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <assert.h>
  20. #include <gpxe/efi/efi.h>
  21. #include <gpxe/ansiesc.h>
  22. #include <console.h>
  23. #define ATTR_BOLD 0x08
  24. #define ATTR_FCOL_MASK 0x07
  25. #define ATTR_FCOL_BLACK 0x00
  26. #define ATTR_FCOL_BLUE 0x01
  27. #define ATTR_FCOL_GREEN 0x02
  28. #define ATTR_FCOL_CYAN 0x03
  29. #define ATTR_FCOL_RED 0x04
  30. #define ATTR_FCOL_MAGENTA 0x05
  31. #define ATTR_FCOL_YELLOW 0x06
  32. #define ATTR_FCOL_WHITE 0x07
  33. #define ATTR_BCOL_MASK 0x70
  34. #define ATTR_BCOL_BLACK 0x00
  35. #define ATTR_BCOL_BLUE 0x10
  36. #define ATTR_BCOL_GREEN 0x20
  37. #define ATTR_BCOL_CYAN 0x30
  38. #define ATTR_BCOL_RED 0x40
  39. #define ATTR_BCOL_MAGENTA 0x50
  40. #define ATTR_BCOL_YELLOW 0x60
  41. #define ATTR_BCOL_WHITE 0x70
  42. #define ATTR_DEFAULT ATTR_FCOL_WHITE
  43. /** Current character attribute */
  44. static unsigned int efi_attr = ATTR_DEFAULT;
  45. /**
  46. * Handle ANSI CUP (cursor position)
  47. *
  48. * @v count Parameter count
  49. * @v params[0] Row (1 is top)
  50. * @v params[1] Column (1 is left)
  51. */
  52. static void efi_handle_cup ( unsigned int count __unused, int params[] ) {
  53. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
  54. int cx = ( params[1] - 1 );
  55. int cy = ( params[0] - 1 );
  56. if ( cx < 0 )
  57. cx = 0;
  58. if ( cy < 0 )
  59. cy = 0;
  60. conout->SetCursorPosition ( conout, cx, cy );
  61. }
  62. /**
  63. * Handle ANSI ED (erase in page)
  64. *
  65. * @v count Parameter count
  66. * @v params[0] Region to erase
  67. */
  68. static void efi_handle_ed ( unsigned int count __unused,
  69. int params[] __unused ) {
  70. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
  71. /* We assume that we always clear the whole screen */
  72. assert ( params[0] == ANSIESC_ED_ALL );
  73. conout->ClearScreen ( conout );
  74. }
  75. /**
  76. * Handle ANSI SGR (set graphics rendition)
  77. *
  78. * @v count Parameter count
  79. * @v params List of graphic rendition aspects
  80. */
  81. static void efi_handle_sgr ( unsigned int count, int params[] ) {
  82. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
  83. static const uint8_t efi_attr_fcols[10] = {
  84. ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
  85. ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
  86. ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
  87. ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
  88. };
  89. static const uint8_t efi_attr_bcols[10] = {
  90. ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
  91. ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
  92. ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
  93. ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
  94. };
  95. unsigned int i;
  96. int aspect;
  97. for ( i = 0 ; i < count ; i++ ) {
  98. aspect = params[i];
  99. if ( aspect == 0 ) {
  100. efi_attr = ATTR_DEFAULT;
  101. } else if ( aspect == 1 ) {
  102. efi_attr |= ATTR_BOLD;
  103. } else if ( aspect == 22 ) {
  104. efi_attr &= ~ATTR_BOLD;
  105. } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
  106. efi_attr &= ~ATTR_FCOL_MASK;
  107. efi_attr |= efi_attr_fcols[ aspect - 30 ];
  108. } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
  109. efi_attr &= ~ATTR_BCOL_MASK;
  110. efi_attr |= efi_attr_bcols[ aspect - 40 ];
  111. }
  112. }
  113. conout->SetAttribute ( conout, efi_attr );
  114. }
  115. /** EFI console ANSI escape sequence handlers */
  116. static struct ansiesc_handler efi_ansiesc_handlers[] = {
  117. { ANSIESC_CUP, efi_handle_cup },
  118. { ANSIESC_ED, efi_handle_ed },
  119. { ANSIESC_SGR, efi_handle_sgr },
  120. { 0, NULL }
  121. };
  122. /** EFI console ANSI escape sequence context */
  123. static struct ansiesc_context efi_ansiesc_ctx = {
  124. .handlers = efi_ansiesc_handlers,
  125. };
  126. /**
  127. * Print a character to EFI console
  128. *
  129. * @v character Character to be printed
  130. */
  131. static void efi_putchar ( int character ) {
  132. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
  133. wchar_t wstr[] = { character, 0 };
  134. /* Intercept ANSI escape sequences */
  135. character = ansiesc_process ( &efi_ansiesc_ctx, character );
  136. if ( character < 0 )
  137. return;
  138. conout->OutputString ( conout, wstr );
  139. }
  140. /**
  141. * Pointer to current ANSI output sequence
  142. *
  143. * While we are in the middle of returning an ANSI sequence for a
  144. * special key, this will point to the next character to return. When
  145. * not in the middle of such a sequence, this will point to a NUL
  146. * (note: not "will be NULL").
  147. */
  148. static const char *ansi_input = "";
  149. /** Mapping from EFI scan codes to ANSI escape sequences */
  150. static const char *ansi_sequences[] = {
  151. [SCAN_UP] = "[A",
  152. [SCAN_DOWN] = "[B",
  153. [SCAN_RIGHT] = "[C",
  154. [SCAN_LEFT] = "[D",
  155. [SCAN_HOME] = "[H",
  156. [SCAN_END] = "[F",
  157. [SCAN_INSERT] = "[2~",
  158. /* EFI translates an incoming backspace via the serial console
  159. * into a SCAN_DELETE. There's not much we can do about this.
  160. */
  161. [SCAN_DELETE] = "[3~",
  162. [SCAN_PAGE_UP] = "[5~",
  163. [SCAN_PAGE_DOWN] = "[6~",
  164. /* EFI translates some (but not all) incoming escape sequences
  165. * via the serial console into equivalent scancodes. When it
  166. * doesn't recognise a sequence, it helpfully(!) translates
  167. * the initial ESC and passes the remainder through verbatim.
  168. * Treating SCAN_ESC as equivalent to an empty escape sequence
  169. * works around this bug.
  170. */
  171. [SCAN_ESC] = "",
  172. };
  173. /**
  174. * Get ANSI escape sequence corresponding to EFI scancode
  175. *
  176. * @v scancode EFI scancode
  177. * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
  178. */
  179. static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
  180. if ( scancode < ( sizeof ( ansi_sequences ) /
  181. sizeof ( ansi_sequences[0] ) ) ) {
  182. return ansi_sequences[scancode];
  183. }
  184. return NULL;
  185. }
  186. /**
  187. * Get character from EFI console
  188. *
  189. * @ret character Character read from console
  190. */
  191. static int efi_getchar ( void ) {
  192. EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
  193. const char *ansi_seq;
  194. EFI_INPUT_KEY key;
  195. EFI_STATUS efirc;
  196. /* If we are mid-sequence, pass out the next byte */
  197. if ( *ansi_input )
  198. return *(ansi_input++);
  199. /* Read key from real EFI console */
  200. if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
  201. DBG ( "EFI could not read keystroke: %s\n",
  202. efi_strerror ( efirc ) );
  203. return 0;
  204. }
  205. DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
  206. key.UnicodeChar, key.ScanCode );
  207. /* If key has a Unicode representation, return it */
  208. if ( key.UnicodeChar )
  209. return key.UnicodeChar;
  210. /* Otherwise, check for a special key that we know about */
  211. if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
  212. /* Start of escape sequence: return ESC (0x1b) */
  213. ansi_input = ansi_seq;
  214. return 0x1b;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * Check for character ready to read from EFI console
  220. *
  221. * @ret True Character available to read
  222. * @ret False No character available to read
  223. */
  224. static int efi_iskey ( void ) {
  225. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  226. EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
  227. EFI_STATUS efirc;
  228. /* If we are mid-sequence, we are always ready */
  229. if ( *ansi_input )
  230. return 1;
  231. /* Check to see if the WaitForKey event has fired */
  232. if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
  233. return 1;
  234. return 0;
  235. }
  236. struct console_driver efi_console __console_driver = {
  237. .putchar = efi_putchar,
  238. .getchar = efi_getchar,
  239. .iskey = efi_iskey,
  240. };