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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <console.h>
  22. #include <ipxe/ansiesc.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 bios_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 bios_handle_cup ( unsigned int count __unused, int params[] ) {
  53. int cx = ( params[1] - 1 );
  54. int cy = ( params[0] - 1 );
  55. if ( cx < 0 )
  56. cx = 0;
  57. if ( cy < 0 )
  58. cy = 0;
  59. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  60. "int $0x10\n\t"
  61. "cli\n\t" )
  62. : : "a" ( 0x0200 ), "b" ( 1 ),
  63. "d" ( ( cy << 8 ) | cx ) );
  64. }
  65. /**
  66. * Handle ANSI ED (erase in page)
  67. *
  68. * @v count Parameter count
  69. * @v params[0] Region to erase
  70. */
  71. static void bios_handle_ed ( unsigned int count __unused,
  72. int params[] __unused ) {
  73. /* We assume that we always clear the whole screen */
  74. assert ( params[0] == ANSIESC_ED_ALL );
  75. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  76. "int $0x10\n\t"
  77. "cli\n\t" )
  78. : : "a" ( 0x0600 ), "b" ( bios_attr << 8 ),
  79. "c" ( 0 ), "d" ( 0xffff ) );
  80. }
  81. /**
  82. * Handle ANSI SGR (set graphics rendition)
  83. *
  84. * @v count Parameter count
  85. * @v params List of graphic rendition aspects
  86. */
  87. static void bios_handle_sgr ( unsigned int count, int params[] ) {
  88. static const uint8_t bios_attr_fcols[10] = {
  89. ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
  90. ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
  91. ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
  92. ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
  93. };
  94. static const uint8_t bios_attr_bcols[10] = {
  95. ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
  96. ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
  97. ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
  98. ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
  99. };
  100. unsigned int i;
  101. int aspect;
  102. for ( i = 0 ; i < count ; i++ ) {
  103. aspect = params[i];
  104. if ( aspect == 0 ) {
  105. bios_attr = ATTR_DEFAULT;
  106. } else if ( aspect == 1 ) {
  107. bios_attr |= ATTR_BOLD;
  108. } else if ( aspect == 22 ) {
  109. bios_attr &= ~ATTR_BOLD;
  110. } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
  111. bios_attr &= ~ATTR_FCOL_MASK;
  112. bios_attr |= bios_attr_fcols[ aspect - 30 ];
  113. } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
  114. bios_attr &= ~ATTR_BCOL_MASK;
  115. bios_attr |= bios_attr_bcols[ aspect - 40 ];
  116. }
  117. }
  118. }
  119. /** BIOS console ANSI escape sequence handlers */
  120. static struct ansiesc_handler bios_ansiesc_handlers[] = {
  121. { ANSIESC_CUP, bios_handle_cup },
  122. { ANSIESC_ED, bios_handle_ed },
  123. { ANSIESC_SGR, bios_handle_sgr },
  124. { 0, NULL }
  125. };
  126. /** BIOS console ANSI escape sequence context */
  127. static struct ansiesc_context bios_ansiesc_ctx = {
  128. .handlers = bios_ansiesc_handlers,
  129. };
  130. /**
  131. * Print a character to BIOS console
  132. *
  133. * @v character Character to be printed
  134. */
  135. static void bios_putchar ( int character ) {
  136. int discard_a, discard_b, discard_c;
  137. /* Intercept ANSI escape sequences */
  138. character = ansiesc_process ( &bios_ansiesc_ctx, character );
  139. if ( character < 0 )
  140. return;
  141. /* Print character with attribute */
  142. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  143. /* Skip non-printable characters */
  144. "cmpb $0x20, %%al\n\t"
  145. "jb 1f\n\t"
  146. /* Set attribute */
  147. "movw $0x0001, %%cx\n\t"
  148. "movb $0x09, %%ah\n\t"
  149. "int $0x10\n\t"
  150. "\n1:\n\t"
  151. /* Print character */
  152. "xorw %%bx, %%bx\n\t"
  153. "movb $0x0e, %%ah\n\t"
  154. "int $0x10\n\t"
  155. "cli\n\t" )
  156. : "=a" ( discard_a ), "=b" ( discard_b ),
  157. "=c" ( discard_c )
  158. : "a" ( character ), "b" ( bios_attr )
  159. : "ebp" );
  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. /**
  171. * Lowest BIOS scancode of interest
  172. *
  173. * Most of the BIOS key scancodes that we are interested in are in a
  174. * dense range, so subtracting a constant and treating them as offsets
  175. * into an array works efficiently.
  176. */
  177. #define BIOS_KEY_MIN 0x42
  178. /** Offset into list of interesting BIOS scancodes */
  179. #define BIOS_KEY(scancode) ( (scancode) - BIOS_KEY_MIN )
  180. /** Mapping from BIOS scan codes to ANSI escape sequences */
  181. static const char *ansi_sequences[] = {
  182. [ BIOS_KEY ( 0x42 ) ] = "[19~", /* F8 (required for PXE) */
  183. [ BIOS_KEY ( 0x47 ) ] = "[H", /* Home */
  184. [ BIOS_KEY ( 0x48 ) ] = "[A", /* Up arrow */
  185. [ BIOS_KEY ( 0x4b ) ] = "[D", /* Left arrow */
  186. [ BIOS_KEY ( 0x4d ) ] = "[C", /* Right arrow */
  187. [ BIOS_KEY ( 0x4f ) ] = "[F", /* End */
  188. [ BIOS_KEY ( 0x50 ) ] = "[B", /* Down arrow */
  189. [ BIOS_KEY ( 0x53 ) ] = "[3~", /* Delete */
  190. };
  191. /**
  192. * Get ANSI escape sequence corresponding to BIOS scancode
  193. *
  194. * @v scancode BIOS scancode
  195. * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
  196. */
  197. static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
  198. unsigned int bios_key = BIOS_KEY ( scancode );
  199. if ( bios_key < ( sizeof ( ansi_sequences ) /
  200. sizeof ( ansi_sequences[0] ) ) ) {
  201. return ansi_sequences[bios_key];
  202. }
  203. DBG ( "Unrecognised BIOS scancode %02x\n", scancode );
  204. return NULL;
  205. }
  206. /**
  207. * Get character from BIOS console
  208. *
  209. * @ret character Character read from console
  210. */
  211. static int bios_getchar ( void ) {
  212. uint16_t keypress;
  213. unsigned int character;
  214. const char *ansi_seq;
  215. /* If we are mid-sequence, pass out the next byte */
  216. if ( ( character = *ansi_input ) ) {
  217. ansi_input++;
  218. return character;
  219. }
  220. /* Read character from real BIOS console */
  221. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  222. "int $0x16\n\t"
  223. "cli\n\t" )
  224. : "=a" ( keypress ) : "a" ( 0x1000 ) );
  225. character = ( keypress & 0xff );
  226. /* If it's a normal character, just return it */
  227. if ( character && ( character < 0x80 ) )
  228. return character;
  229. /* Otherwise, check for a special key that we know about */
  230. if ( ( ansi_seq = scancode_to_ansi_seq ( keypress >> 8 ) ) ) {
  231. /* Start of escape sequence: return ESC (0x1b) */
  232. ansi_input = ansi_seq;
  233. return 0x1b;
  234. }
  235. return 0;
  236. }
  237. /**
  238. * Check for character ready to read from BIOS console
  239. *
  240. * @ret True Character available to read
  241. * @ret False No character available to read
  242. */
  243. static int bios_iskey ( void ) {
  244. unsigned int discard_a;
  245. unsigned int flags;
  246. /* If we are mid-sequence, we are always ready */
  247. if ( *ansi_input )
  248. return 1;
  249. /* Otherwise check the real BIOS console */
  250. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  251. "int $0x16\n\t"
  252. "pushfw\n\t"
  253. "popw %w0\n\t"
  254. "cli\n\t" )
  255. : "=r" ( flags ), "=a" ( discard_a )
  256. : "a" ( 0x0100 ) );
  257. return ( ! ( flags & ZF ) );
  258. }
  259. struct console_driver bios_console __console_driver = {
  260. .putchar = bios_putchar,
  261. .getchar = bios_getchar,
  262. .iskey = bios_iskey,
  263. };