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

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