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.

bios_console.c 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2006 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 <assert.h>
  20. #include <realmode.h>
  21. #include <ipxe/console.h>
  22. #include <ipxe/ansiesc.h>
  23. #include <ipxe/keymap.h>
  24. #include <config/console.h>
  25. #define ATTR_BOLD 0x08
  26. #define ATTR_FCOL_MASK 0x07
  27. #define ATTR_FCOL_BLACK 0x00
  28. #define ATTR_FCOL_BLUE 0x01
  29. #define ATTR_FCOL_GREEN 0x02
  30. #define ATTR_FCOL_CYAN 0x03
  31. #define ATTR_FCOL_RED 0x04
  32. #define ATTR_FCOL_MAGENTA 0x05
  33. #define ATTR_FCOL_YELLOW 0x06
  34. #define ATTR_FCOL_WHITE 0x07
  35. #define ATTR_BCOL_MASK 0x70
  36. #define ATTR_BCOL_BLACK 0x00
  37. #define ATTR_BCOL_BLUE 0x10
  38. #define ATTR_BCOL_GREEN 0x20
  39. #define ATTR_BCOL_CYAN 0x30
  40. #define ATTR_BCOL_RED 0x40
  41. #define ATTR_BCOL_MAGENTA 0x50
  42. #define ATTR_BCOL_YELLOW 0x60
  43. #define ATTR_BCOL_WHITE 0x70
  44. #define ATTR_DEFAULT ATTR_FCOL_WHITE
  45. /* Set default console usage if applicable */
  46. #if ! ( defined ( CONSOLE_PCBIOS ) && CONSOLE_EXPLICIT ( CONSOLE_PCBIOS ) )
  47. #undef CONSOLE_PCBIOS
  48. #define CONSOLE_PCBIOS CONSOLE_USAGE_ALL
  49. #endif
  50. /** Current character attribute */
  51. static unsigned int bios_attr = ATTR_DEFAULT;
  52. /**
  53. * Handle ANSI CUP (cursor position)
  54. *
  55. * @v count Parameter count
  56. * @v params[0] Row (1 is top)
  57. * @v params[1] Column (1 is left)
  58. */
  59. static void bios_handle_cup ( unsigned int count __unused, int params[] ) {
  60. int cx = ( params[1] - 1 );
  61. int cy = ( params[0] - 1 );
  62. if ( cx < 0 )
  63. cx = 0;
  64. if ( cy < 0 )
  65. cy = 0;
  66. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  67. "int $0x10\n\t"
  68. "cli\n\t" )
  69. : : "a" ( 0x0200 ), "b" ( 1 ),
  70. "d" ( ( cy << 8 ) | cx ) );
  71. }
  72. /**
  73. * Handle ANSI ED (erase in page)
  74. *
  75. * @v count Parameter count
  76. * @v params[0] Region to erase
  77. */
  78. static void bios_handle_ed ( unsigned int count __unused,
  79. int params[] __unused ) {
  80. /* We assume that we always clear the whole screen */
  81. assert ( params[0] == ANSIESC_ED_ALL );
  82. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  83. "int $0x10\n\t"
  84. "cli\n\t" )
  85. : : "a" ( 0x0600 ), "b" ( bios_attr << 8 ),
  86. "c" ( 0 ), "d" ( 0xffff ) );
  87. }
  88. /**
  89. * Handle ANSI SGR (set graphics rendition)
  90. *
  91. * @v count Parameter count
  92. * @v params List of graphic rendition aspects
  93. */
  94. static void bios_handle_sgr ( unsigned int count, int params[] ) {
  95. static const uint8_t bios_attr_fcols[10] = {
  96. ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
  97. ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
  98. ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
  99. ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
  100. };
  101. static const uint8_t bios_attr_bcols[10] = {
  102. ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
  103. ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
  104. ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
  105. ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
  106. };
  107. unsigned int i;
  108. int aspect;
  109. for ( i = 0 ; i < count ; i++ ) {
  110. aspect = params[i];
  111. if ( aspect == 0 ) {
  112. bios_attr = ATTR_DEFAULT;
  113. } else if ( aspect == 1 ) {
  114. bios_attr |= ATTR_BOLD;
  115. } else if ( aspect == 22 ) {
  116. bios_attr &= ~ATTR_BOLD;
  117. } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
  118. bios_attr &= ~ATTR_FCOL_MASK;
  119. bios_attr |= bios_attr_fcols[ aspect - 30 ];
  120. } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
  121. bios_attr &= ~ATTR_BCOL_MASK;
  122. bios_attr |= bios_attr_bcols[ aspect - 40 ];
  123. }
  124. }
  125. }
  126. /** BIOS console ANSI escape sequence handlers */
  127. static struct ansiesc_handler bios_ansiesc_handlers[] = {
  128. { ANSIESC_CUP, bios_handle_cup },
  129. { ANSIESC_ED, bios_handle_ed },
  130. { ANSIESC_SGR, bios_handle_sgr },
  131. { 0, NULL }
  132. };
  133. /** BIOS console ANSI escape sequence context */
  134. static struct ansiesc_context bios_ansiesc_ctx = {
  135. .handlers = bios_ansiesc_handlers,
  136. };
  137. /**
  138. * Print a character to BIOS console
  139. *
  140. * @v character Character to be printed
  141. */
  142. static void bios_putchar ( int character ) {
  143. int discard_a, discard_b, discard_c;
  144. /* Intercept ANSI escape sequences */
  145. character = ansiesc_process ( &bios_ansiesc_ctx, character );
  146. if ( character < 0 )
  147. return;
  148. /* Print character with attribute */
  149. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  150. /* Skip non-printable characters */
  151. "cmpb $0x20, %%al\n\t"
  152. "jb 1f\n\t"
  153. /* Read attribute */
  154. "movb %%al, %%cl\n\t"
  155. "movb $0x08, %%ah\n\t"
  156. "int $0x10\n\t"
  157. "xchgb %%al, %%cl\n\t"
  158. /* Skip if attribute matches */
  159. "cmpb %%ah, %%bl\n\t"
  160. "je 1f\n\t"
  161. /* Set attribute */
  162. "movw $0x0001, %%cx\n\t"
  163. "movb $0x09, %%ah\n\t"
  164. "int $0x10\n\t"
  165. "\n1:\n\t"
  166. /* Print character */
  167. "xorw %%bx, %%bx\n\t"
  168. "movb $0x0e, %%ah\n\t"
  169. "int $0x10\n\t"
  170. "cli\n\t" )
  171. : "=a" ( discard_a ), "=b" ( discard_b ),
  172. "=c" ( discard_c )
  173. : "a" ( character ), "b" ( bios_attr )
  174. : "ebp" );
  175. }
  176. /**
  177. * Pointer to current ANSI output sequence
  178. *
  179. * While we are in the middle of returning an ANSI sequence for a
  180. * special key, this will point to the next character to return. When
  181. * not in the middle of such a sequence, this will point to a NUL
  182. * (note: not "will be NULL").
  183. */
  184. static const char *ansi_input = "";
  185. /** A mapping from a BIOS scan code to an ANSI escape sequence */
  186. #define BIOS_KEY( key, ansi ) key ansi "\0"
  187. /** Mapping from BIOS scan codes to ANSI escape sequences */
  188. static const char ansi_sequences[] = {
  189. BIOS_KEY ( "\x53", "[3~" ) /* Delete */
  190. BIOS_KEY ( "\x48", "[A" ) /* Up arrow */
  191. BIOS_KEY ( "\x50", "[B" ) /* Down arrow */
  192. BIOS_KEY ( "\x4b", "[D" ) /* Left arrow */
  193. BIOS_KEY ( "\x4d", "[C" ) /* Right arrow */
  194. BIOS_KEY ( "\x47", "[H" ) /* Home */
  195. BIOS_KEY ( "\x4f", "[F" ) /* End */
  196. BIOS_KEY ( "\x3f", "[15~" ) /* F5 */
  197. BIOS_KEY ( "\x40", "[17~" ) /* F6 */
  198. BIOS_KEY ( "\x41", "[18~" ) /* F7 */
  199. BIOS_KEY ( "\x42", "[19~" ) /* F8 (required for PXE) */
  200. BIOS_KEY ( "\x43", "[20~" ) /* F9 */
  201. BIOS_KEY ( "\x44", "[21~" ) /* F10 */
  202. BIOS_KEY ( "\x85", "[23~" ) /* F11 */
  203. BIOS_KEY ( "\x86", "[24~" ) /* F12 */
  204. };
  205. /**
  206. * Get ANSI escape sequence corresponding to BIOS scancode
  207. *
  208. * @v scancode BIOS scancode
  209. * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
  210. */
  211. static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
  212. const char *seq = ansi_sequences;
  213. while ( *seq ) {
  214. if ( *(seq++) == ( ( char ) scancode ) )
  215. return seq;
  216. seq += ( strlen ( seq ) + 1 );
  217. }
  218. DBG ( "Unrecognised BIOS scancode %02x\n", scancode );
  219. return NULL;
  220. }
  221. /**
  222. * Map a key
  223. *
  224. * @v character Character read from console
  225. * @ret character Mapped character
  226. */
  227. static int bios_keymap ( unsigned int character ) {
  228. struct key_mapping *mapping;
  229. for_each_table_entry ( mapping, KEYMAP ) {
  230. if ( mapping->from == character )
  231. return mapping->to;
  232. }
  233. return character;
  234. }
  235. /**
  236. * Get character from BIOS console
  237. *
  238. * @ret character Character read from console
  239. */
  240. static int bios_getchar ( void ) {
  241. uint16_t keypress;
  242. unsigned int character;
  243. const char *ansi_seq;
  244. /* If we are mid-sequence, pass out the next byte */
  245. if ( ( character = *ansi_input ) ) {
  246. ansi_input++;
  247. return character;
  248. }
  249. /* Read character from real BIOS console */
  250. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  251. "int $0x16\n\t"
  252. "cli\n\t" )
  253. : "=a" ( keypress ) : "a" ( 0x1000 ) );
  254. character = ( keypress & 0xff );
  255. /* If it's a normal character, just map and return it */
  256. if ( character && ( character < 0x80 ) )
  257. return bios_keymap ( character );
  258. /* Otherwise, check for a special key that we know about */
  259. if ( ( ansi_seq = scancode_to_ansi_seq ( keypress >> 8 ) ) ) {
  260. /* Start of escape sequence: return ESC (0x1b) */
  261. ansi_input = ansi_seq;
  262. return 0x1b;
  263. }
  264. return 0;
  265. }
  266. /**
  267. * Check for character ready to read from BIOS console
  268. *
  269. * @ret True Character available to read
  270. * @ret False No character available to read
  271. */
  272. static int bios_iskey ( void ) {
  273. unsigned int discard_a;
  274. unsigned int flags;
  275. /* If we are mid-sequence, we are always ready */
  276. if ( *ansi_input )
  277. return 1;
  278. /* Otherwise check the real BIOS console */
  279. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  280. "int $0x16\n\t"
  281. "pushfw\n\t"
  282. "popw %w0\n\t"
  283. "cli\n\t" )
  284. : "=r" ( flags ), "=a" ( discard_a )
  285. : "a" ( 0x1100 ) );
  286. return ( ! ( flags & ZF ) );
  287. }
  288. struct console_driver bios_console __console_driver = {
  289. .putchar = bios_putchar,
  290. .getchar = bios_getchar,
  291. .iskey = bios_iskey,
  292. .usage = CONSOLE_PCBIOS,
  293. };