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.7KB

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