Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

bios_console.c 15KB

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