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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 <biosint.h>
  28. #include <ipxe/console.h>
  29. #include <ipxe/ansiesc.h>
  30. #include <ipxe/keys.h>
  31. #include <ipxe/keymap.h>
  32. #include <ipxe/init.h>
  33. #include <config/console.h>
  34. #define ATTR_BOLD 0x08
  35. #define ATTR_FCOL_MASK 0x07
  36. #define ATTR_FCOL_BLACK 0x00
  37. #define ATTR_FCOL_BLUE 0x01
  38. #define ATTR_FCOL_GREEN 0x02
  39. #define ATTR_FCOL_CYAN 0x03
  40. #define ATTR_FCOL_RED 0x04
  41. #define ATTR_FCOL_MAGENTA 0x05
  42. #define ATTR_FCOL_YELLOW 0x06
  43. #define ATTR_FCOL_WHITE 0x07
  44. #define ATTR_BLINK 0x80
  45. #define ATTR_BCOL_MASK 0x70
  46. #define ATTR_BCOL_BLACK 0x00
  47. #define ATTR_BCOL_BLUE 0x10
  48. #define ATTR_BCOL_GREEN 0x20
  49. #define ATTR_BCOL_CYAN 0x30
  50. #define ATTR_BCOL_RED 0x40
  51. #define ATTR_BCOL_MAGENTA 0x50
  52. #define ATTR_BCOL_YELLOW 0x60
  53. #define ATTR_BCOL_WHITE 0x70
  54. #define ATTR_DEFAULT ATTR_FCOL_WHITE
  55. /* Set default console usage if applicable */
  56. #if ! ( defined ( CONSOLE_PCBIOS ) && CONSOLE_EXPLICIT ( CONSOLE_PCBIOS ) )
  57. #undef CONSOLE_PCBIOS
  58. #define CONSOLE_PCBIOS ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
  59. #endif
  60. /** Current character attribute */
  61. static unsigned int bios_attr = ATTR_DEFAULT;
  62. /** Keypress injection lock */
  63. static uint8_t __text16 ( bios_inject_lock );
  64. #define bios_inject_lock __use_text16 ( bios_inject_lock )
  65. /** Vector for chaining to other INT 16 handlers */
  66. static struct segoff __text16 ( int16_vector );
  67. #define int16_vector __use_text16 ( int16_vector )
  68. /** Assembly wrapper */
  69. extern void int16_wrapper ( void );
  70. /**
  71. * Handle ANSI CUP (cursor position)
  72. *
  73. * @v ctx ANSI escape sequence context
  74. * @v count Parameter count
  75. * @v params[0] Row (1 is top)
  76. * @v params[1] Column (1 is left)
  77. */
  78. static void bios_handle_cup ( struct ansiesc_context *ctx __unused,
  79. unsigned int count __unused, int params[] ) {
  80. int cx = ( params[1] - 1 );
  81. int cy = ( params[0] - 1 );
  82. if ( cx < 0 )
  83. cx = 0;
  84. if ( cy < 0 )
  85. cy = 0;
  86. __asm__ __volatile__ ( REAL_CODE ( "int $0x10\n\t" )
  87. : : "a" ( 0x0200 ), "b" ( 1 ),
  88. "d" ( ( cy << 8 ) | cx ) );
  89. }
  90. /**
  91. * Handle ANSI ED (erase in page)
  92. *
  93. * @v ctx ANSI escape sequence context
  94. * @v count Parameter count
  95. * @v params[0] Region to erase
  96. */
  97. static void bios_handle_ed ( struct ansiesc_context *ctx __unused,
  98. unsigned int count __unused,
  99. int params[] __unused ) {
  100. /* We assume that we always clear the whole screen */
  101. assert ( params[0] == ANSIESC_ED_ALL );
  102. __asm__ __volatile__ ( REAL_CODE ( "int $0x10\n\t" )
  103. : : "a" ( 0x0600 ), "b" ( bios_attr << 8 ),
  104. "c" ( 0 ),
  105. "d" ( ( ( console_height - 1 ) << 8 ) |
  106. ( console_width - 1 ) ) );
  107. }
  108. /**
  109. * Handle ANSI SGR (set graphics rendition)
  110. *
  111. * @v ctx ANSI escape sequence context
  112. * @v count Parameter count
  113. * @v params List of graphic rendition aspects
  114. */
  115. static void bios_handle_sgr ( struct ansiesc_context *ctx __unused,
  116. unsigned int count, int params[] ) {
  117. static const uint8_t bios_attr_fcols[10] = {
  118. ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
  119. ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
  120. ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
  121. ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
  122. };
  123. static const uint8_t bios_attr_bcols[10] = {
  124. ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
  125. ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
  126. ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
  127. ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
  128. };
  129. unsigned int i;
  130. int aspect;
  131. for ( i = 0 ; i < count ; i++ ) {
  132. aspect = params[i];
  133. if ( aspect == 0 ) {
  134. bios_attr = ATTR_DEFAULT;
  135. } else if ( aspect == 1 ) {
  136. bios_attr |= ATTR_BOLD;
  137. } else if ( aspect == 5 ) {
  138. bios_attr |= ATTR_BLINK;
  139. } else if ( aspect == 22 ) {
  140. bios_attr &= ~ATTR_BOLD;
  141. } else if ( aspect == 25 ) {
  142. bios_attr &= ~ATTR_BLINK;
  143. } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
  144. bios_attr &= ~ATTR_FCOL_MASK;
  145. bios_attr |= bios_attr_fcols[ aspect - 30 ];
  146. } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
  147. bios_attr &= ~ATTR_BCOL_MASK;
  148. bios_attr |= bios_attr_bcols[ aspect - 40 ];
  149. }
  150. }
  151. }
  152. /**
  153. * Handle ANSI DECTCEM set (show cursor)
  154. *
  155. * @v ctx ANSI escape sequence context
  156. * @v count Parameter count
  157. * @v params List of graphic rendition aspects
  158. */
  159. static void bios_handle_dectcem_set ( struct ansiesc_context *ctx __unused,
  160. unsigned int count __unused,
  161. int params[] __unused ) {
  162. uint8_t height;
  163. /* Get character height */
  164. get_real ( height, BDA_SEG, BDA_CHAR_HEIGHT );
  165. __asm__ __volatile__ ( REAL_CODE ( "int $0x10\n\t" )
  166. : : "a" ( 0x0100 ),
  167. "c" ( ( ( height - 2 ) << 8 ) |
  168. ( height - 1 ) ) );
  169. }
  170. /**
  171. * Handle ANSI DECTCEM reset (hide cursor)
  172. *
  173. * @v ctx ANSI escape sequence context
  174. * @v count Parameter count
  175. * @v params List of graphic rendition aspects
  176. */
  177. static void bios_handle_dectcem_reset ( struct ansiesc_context *ctx __unused,
  178. unsigned int count __unused,
  179. int params[] __unused ) {
  180. __asm__ __volatile__ ( REAL_CODE ( "int $0x10\n\t" )
  181. : : "a" ( 0x0100 ), "c" ( 0x2000 ) );
  182. }
  183. /** BIOS console ANSI escape sequence handlers */
  184. static struct ansiesc_handler bios_ansiesc_handlers[] = {
  185. { ANSIESC_CUP, bios_handle_cup },
  186. { ANSIESC_ED, bios_handle_ed },
  187. { ANSIESC_SGR, bios_handle_sgr },
  188. { ANSIESC_DECTCEM_SET, bios_handle_dectcem_set },
  189. { ANSIESC_DECTCEM_RESET, bios_handle_dectcem_reset },
  190. { 0, NULL }
  191. };
  192. /** BIOS console ANSI escape sequence context */
  193. static struct ansiesc_context bios_ansiesc_ctx = {
  194. .handlers = bios_ansiesc_handlers,
  195. };
  196. /**
  197. * Print a character to BIOS console
  198. *
  199. * @v character Character to be printed
  200. */
  201. static void bios_putchar ( int character ) {
  202. int discard_a, discard_b, discard_c;
  203. /* Intercept ANSI escape sequences */
  204. character = ansiesc_process ( &bios_ansiesc_ctx, character );
  205. if ( character < 0 )
  206. return;
  207. /* Print character with attribute */
  208. __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
  209. /* Skip non-printable characters */
  210. "cmpb $0x20, %%al\n\t"
  211. "jb 1f\n\t"
  212. /* Read attribute */
  213. "movb %%al, %%cl\n\t"
  214. "movb $0x08, %%ah\n\t"
  215. "int $0x10\n\t"
  216. "xchgb %%al, %%cl\n\t"
  217. /* Skip if attribute matches */
  218. "cmpb %%ah, %%bl\n\t"
  219. "je 1f\n\t"
  220. /* Set attribute */
  221. "movw $0x0001, %%cx\n\t"
  222. "movb $0x09, %%ah\n\t"
  223. "int $0x10\n\t"
  224. "\n1:\n\t"
  225. /* Print character */
  226. "xorw %%bx, %%bx\n\t"
  227. "movb $0x0e, %%ah\n\t"
  228. "int $0x10\n\t"
  229. "popl %%ebp\n\t" /* gcc bug */ )
  230. : "=a" ( discard_a ), "=b" ( discard_b ),
  231. "=c" ( discard_c )
  232. : "a" ( character ), "b" ( bios_attr ) );
  233. }
  234. /**
  235. * Pointer to current ANSI output sequence
  236. *
  237. * While we are in the middle of returning an ANSI sequence for a
  238. * special key, this will point to the next character to return. When
  239. * not in the middle of such a sequence, this will point to a NUL
  240. * (note: not "will be NULL").
  241. */
  242. static const char *bios_ansi_input = "";
  243. /** A BIOS key */
  244. struct bios_key {
  245. /** Scancode */
  246. uint8_t scancode;
  247. /** Key code */
  248. uint16_t key;
  249. } __attribute__ (( packed ));
  250. /** Mapping from BIOS scan codes to iPXE key codes */
  251. static const struct bios_key bios_keys[] = {
  252. { 0x53, KEY_DC },
  253. { 0x48, KEY_UP },
  254. { 0x50, KEY_DOWN },
  255. { 0x4b, KEY_LEFT },
  256. { 0x4d, KEY_RIGHT },
  257. { 0x47, KEY_HOME },
  258. { 0x4f, KEY_END },
  259. { 0x49, KEY_PPAGE },
  260. { 0x51, KEY_NPAGE },
  261. { 0x3f, KEY_F5 },
  262. { 0x40, KEY_F6 },
  263. { 0x41, KEY_F7 },
  264. { 0x42, KEY_F8 },
  265. { 0x43, KEY_F9 },
  266. { 0x44, KEY_F10 },
  267. { 0x85, KEY_F11 },
  268. { 0x86, KEY_F12 },
  269. };
  270. /**
  271. * Get ANSI escape sequence corresponding to BIOS scancode
  272. *
  273. * @v scancode BIOS scancode
  274. * @ret ansi_seq ANSI escape sequence, if any, otherwise NULL
  275. */
  276. static const char * bios_ansi_seq ( unsigned int scancode ) {
  277. static char buf[ 5 /* "[" + two digits + terminator + NUL */ ];
  278. unsigned int key;
  279. unsigned int terminator;
  280. unsigned int n;
  281. unsigned int i;
  282. char *tmp = buf;
  283. /* Construct ANSI escape sequence for scancode, if known */
  284. for ( i = 0 ; i < ( sizeof ( bios_keys ) /
  285. sizeof ( bios_keys[0] ) ) ; i++ ) {
  286. /* Look for matching scancode */
  287. if ( bios_keys[i].scancode != scancode )
  288. continue;
  289. /* Construct escape sequence */
  290. key = bios_keys[i].key;
  291. n = KEY_ANSI_N ( key );
  292. terminator = KEY_ANSI_TERMINATOR ( key );
  293. *(tmp++) = '[';
  294. if ( n )
  295. tmp += sprintf ( tmp, "%d", n );
  296. *(tmp++) = terminator;
  297. *(tmp++) = '\0';
  298. assert ( tmp <= &buf[ sizeof ( buf ) ] );
  299. return buf;
  300. }
  301. DBG ( "Unrecognised BIOS scancode %02x\n", scancode );
  302. return NULL;
  303. }
  304. /**
  305. * Map a key
  306. *
  307. * @v character Character read from console
  308. * @ret character Mapped character
  309. */
  310. static int bios_keymap ( unsigned int character ) {
  311. struct key_mapping *mapping;
  312. for_each_table_entry ( mapping, KEYMAP ) {
  313. if ( mapping->from == character )
  314. return mapping->to;
  315. }
  316. return character;
  317. }
  318. /**
  319. * Get character from BIOS console
  320. *
  321. * @ret character Character read from console
  322. */
  323. static int bios_getchar ( void ) {
  324. uint16_t keypress;
  325. unsigned int character;
  326. const char *ansi_seq;
  327. /* If we are mid-sequence, pass out the next byte */
  328. if ( ( character = *bios_ansi_input ) ) {
  329. bios_ansi_input++;
  330. return character;
  331. }
  332. /* Do nothing if injection is in progress */
  333. if ( bios_inject_lock )
  334. return 0;
  335. /* Read character from real BIOS console */
  336. bios_inject_lock++;
  337. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  338. "int $0x16\n\t"
  339. "cli\n\t" )
  340. : "=a" ( keypress )
  341. : "a" ( 0x1000 ), "m" ( bios_inject_lock ) );
  342. bios_inject_lock--;
  343. character = ( keypress & 0xff );
  344. /* If it's a normal character, just map and return it */
  345. if ( character && ( character < 0x80 ) )
  346. return bios_keymap ( character );
  347. /* Otherwise, check for a special key that we know about */
  348. if ( ( ansi_seq = bios_ansi_seq ( keypress >> 8 ) ) ) {
  349. /* Start of escape sequence: return ESC (0x1b) */
  350. bios_ansi_input = ansi_seq;
  351. return 0x1b;
  352. }
  353. return 0;
  354. }
  355. /**
  356. * Check for character ready to read from BIOS console
  357. *
  358. * @ret True Character available to read
  359. * @ret False No character available to read
  360. */
  361. static int bios_iskey ( void ) {
  362. unsigned int discard_a;
  363. unsigned int flags;
  364. /* If we are mid-sequence, we are always ready */
  365. if ( *bios_ansi_input )
  366. return 1;
  367. /* Do nothing if injection is in progress */
  368. if ( bios_inject_lock )
  369. return 0;
  370. /* Otherwise check the real BIOS console */
  371. bios_inject_lock++;
  372. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  373. "int $0x16\n\t"
  374. "pushfw\n\t"
  375. "popw %w0\n\t"
  376. "cli\n\t" )
  377. : "=R" ( flags ), "=a" ( discard_a )
  378. : "a" ( 0x1100 ), "m" ( bios_inject_lock ) );
  379. bios_inject_lock--;
  380. return ( ! ( flags & ZF ) );
  381. }
  382. /** BIOS console */
  383. struct console_driver bios_console __console_driver = {
  384. .putchar = bios_putchar,
  385. .getchar = bios_getchar,
  386. .iskey = bios_iskey,
  387. .usage = CONSOLE_PCBIOS,
  388. };
  389. /**
  390. * Inject keypresses
  391. *
  392. * @v ix86 Registers as passed to INT 16
  393. */
  394. static __asmcall void bios_inject ( struct i386_all_regs *ix86 ) {
  395. unsigned int discard_a;
  396. unsigned int scancode;
  397. unsigned int i;
  398. uint16_t keypress;
  399. int key;
  400. /* If this is a blocking call, then loop until the
  401. * non-blocking variant of the call indicates that a keypress
  402. * is available. Do this without acquiring the injection
  403. * lock, so that injection may take place.
  404. */
  405. if ( ( ix86->regs.ah & ~0x10 ) == 0x00 ) {
  406. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  407. "\n1:\n\t"
  408. "pushw %%ax\n\t"
  409. "int $0x16\n\t"
  410. "popw %%ax\n\t"
  411. "jc 2f\n\t"
  412. "jz 1b\n\t"
  413. "\n2:\n\t"
  414. "cli\n\t" )
  415. : "=a" ( discard_a )
  416. : "a" ( ix86->regs.eax | 0x0100 ),
  417. "m" ( bios_inject_lock ) );
  418. }
  419. /* Acquire injection lock */
  420. bios_inject_lock++;
  421. /* Check for keypresses */
  422. if ( iskey() ) {
  423. /* Get key */
  424. key = getkey ( 0 );
  425. /* Reverse internal CR->LF mapping */
  426. if ( key == '\n' )
  427. key = '\r';
  428. /* Convert to keypress */
  429. keypress = ( ( key << 8 ) | key );
  430. /* Handle special keys */
  431. if ( key >= KEY_MIN ) {
  432. for ( i = 0 ; i < ( sizeof ( bios_keys ) /
  433. sizeof ( bios_keys[0] ) ) ; i++ ) {
  434. if ( bios_keys[i].key == key ) {
  435. scancode = bios_keys[i].scancode;
  436. keypress = ( scancode << 8 );
  437. break;
  438. }
  439. }
  440. }
  441. /* Inject keypress */
  442. DBGC ( &bios_console, "BIOS injecting keypress %04x\n",
  443. keypress );
  444. __asm__ __volatile__ ( REAL_CODE ( "int $0x16\n\t" )
  445. : "=a" ( discard_a )
  446. : "a" ( 0x0500 ), "c" ( keypress ),
  447. "m" ( bios_inject_lock ) );
  448. }
  449. /* Release injection lock */
  450. bios_inject_lock--;
  451. }
  452. /**
  453. * Start up keypress injection
  454. *
  455. */
  456. static void bios_inject_startup ( void ) {
  457. /* Assembly wrapper to call bios_inject() */
  458. __asm__ __volatile__ (
  459. TEXT16_CODE ( "\nint16_wrapper:\n\t"
  460. "pushfw\n\t"
  461. "cmpb $0, %%cs:bios_inject_lock\n\t"
  462. "jnz 1f\n\t"
  463. VIRT_CALL ( bios_inject )
  464. "\n1:\n\t"
  465. "popfw\n\t"
  466. "ljmp *%%cs:int16_vector\n\t" ) : );
  467. /* Hook INT 16 */
  468. hook_bios_interrupt ( 0x16, ( ( intptr_t ) int16_wrapper ),
  469. &int16_vector );
  470. }
  471. /**
  472. * Shut down keypress injection
  473. *
  474. * @v booting System is shutting down for OS boot
  475. */
  476. static void bios_inject_shutdown ( int booting __unused ) {
  477. /* Unhook INT 16 */
  478. unhook_bios_interrupt ( 0x16, ( ( intptr_t ) int16_wrapper ),
  479. &int16_vector );
  480. }
  481. /** Keypress injection startup function */
  482. struct startup_fn bios_inject_startup_fn __startup_fn ( STARTUP_NORMAL ) = {
  483. .name = "bios_inject",
  484. .startup = bios_inject_startup,
  485. .shutdown = bios_inject_shutdown,
  486. };