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

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