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

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