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

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