您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

efi_console.c 8.9KB

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