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.

vesafb.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (C) 2013 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. /** @file
  25. *
  26. * VESA frame buffer console
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #include <limits.h>
  32. #include <realmode.h>
  33. #include <ipxe/console.h>
  34. #include <ipxe/io.h>
  35. #include <ipxe/ansicol.h>
  36. #include <ipxe/fbcon.h>
  37. #include <ipxe/vesafb.h>
  38. #include <config/console.h>
  39. /* Avoid dragging in BIOS console if not otherwise used */
  40. extern struct console_driver bios_console;
  41. struct console_driver bios_console __attribute__ (( weak ));
  42. /* Disambiguate the various error causes */
  43. #define EIO_FAILED __einfo_error ( EINFO_EIO_FAILED )
  44. #define EINFO_EIO_FAILED \
  45. __einfo_uniqify ( EINFO_EIO, 0x01, \
  46. "Function call failed" )
  47. #define EIO_HARDWARE __einfo_error ( EINFO_EIO_HARDWARE )
  48. #define EINFO_EIO_HARDWARE \
  49. __einfo_uniqify ( EINFO_EIO, 0x02, \
  50. "Not supported in current configuration" )
  51. #define EIO_MODE __einfo_error ( EINFO_EIO_MODE )
  52. #define EINFO_EIO_MODE \
  53. __einfo_uniqify ( EINFO_EIO, 0x03, \
  54. "Invalid in current video mode" )
  55. #define EIO_VBE( code ) \
  56. EUNIQ ( EINFO_EIO, (code), EIO_FAILED, EIO_HARDWARE, EIO_MODE )
  57. /* Set default console usage if applicable
  58. *
  59. * We accept either CONSOLE_FRAMEBUFFER or CONSOLE_VESAFB.
  60. */
  61. #if ( defined ( CONSOLE_FRAMEBUFFER ) && ! defined ( CONSOLE_VESAFB ) )
  62. #define CONSOLE_VESAFB CONSOLE_FRAMEBUFFER
  63. #endif
  64. #if ! ( defined ( CONSOLE_VESAFB ) && CONSOLE_EXPLICIT ( CONSOLE_VESAFB ) )
  65. #undef CONSOLE_VESAFB
  66. #define CONSOLE_VESAFB ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
  67. #endif
  68. /** Character height */
  69. #define VESAFB_CHAR_HEIGHT 16
  70. /** Font corresponding to selected character width and height */
  71. #define VESAFB_FONT VBE_FONT_8x16
  72. /* Forward declaration */
  73. struct console_driver vesafb_console __console_driver;
  74. /** A VESA frame buffer */
  75. struct vesafb {
  76. /** Frame buffer console */
  77. struct fbcon fbcon;
  78. /** Physical start address */
  79. physaddr_t start;
  80. /** Pixel geometry */
  81. struct fbcon_geometry pixel;
  82. /** Colour mapping */
  83. struct fbcon_colour_map map;
  84. /** Font definition */
  85. struct fbcon_font font;
  86. /** Character glyphs */
  87. struct segoff glyphs;
  88. /** Saved VGA mode */
  89. uint8_t saved_mode;
  90. };
  91. /** The VESA frame buffer */
  92. static struct vesafb vesafb;
  93. /** Base memory buffer used for VBE calls */
  94. union vbe_buffer {
  95. /** VBE controller information block */
  96. struct vbe_controller_info controller;
  97. /** VBE mode information block */
  98. struct vbe_mode_info mode;
  99. };
  100. static union vbe_buffer __bss16 ( vbe_buf );
  101. #define vbe_buf __use_data16 ( vbe_buf )
  102. /**
  103. * Convert VBE status code to iPXE status code
  104. *
  105. * @v status VBE status code
  106. * @ret rc Return status code
  107. */
  108. static int vesafb_rc ( unsigned int status ) {
  109. unsigned int code;
  110. if ( ( status & 0xff ) != 0x4f )
  111. return -ENOTSUP;
  112. code = ( ( status >> 8 ) & 0xff );
  113. return ( code ? -EIO_VBE ( code ) : 0 );
  114. }
  115. /**
  116. * Get character glyph
  117. *
  118. * @v character Character
  119. * @v glyph Character glyph to fill in
  120. */
  121. static void vesafb_glyph ( unsigned int character, uint8_t *glyph ) {
  122. size_t offset = ( character * VESAFB_CHAR_HEIGHT );
  123. copy_from_real ( glyph, vesafb.glyphs.segment,
  124. ( vesafb.glyphs.offset + offset ), VESAFB_CHAR_HEIGHT);
  125. }
  126. /**
  127. * Get font definition
  128. *
  129. */
  130. static void vesafb_font ( void ) {
  131. /* Get font information
  132. *
  133. * Working around gcc bugs is icky here. The value we want is
  134. * returned in %ebp, but there's no way to specify %ebp in an
  135. * output constraint. We can't put %ebp in the clobber list,
  136. * because this tends to cause random build failures on some
  137. * gcc versions. We can't manually push/pop %ebp and return
  138. * the value via a generic register output constraint, because
  139. * gcc might choose to use %ebp to satisfy that constraint
  140. * (and we have no way to prevent it from so doing).
  141. *
  142. * Work around this hideous mess by using %ecx and %edx as the
  143. * output registers, since they get clobbered anyway.
  144. */
  145. __asm__ __volatile__ ( REAL_CODE ( "pushw %%bp\n\t" /* gcc bug */
  146. "int $0x10\n\t"
  147. "movw %%es, %%cx\n\t"
  148. "movw %%bp, %%dx\n\t"
  149. "popw %%bp\n\t" /* gcc bug */ )
  150. : "=c" ( vesafb.glyphs.segment ),
  151. "=d" ( vesafb.glyphs.offset )
  152. : "a" ( VBE_GET_FONT ),
  153. "b" ( VESAFB_FONT ) );
  154. DBGC ( &vbe_buf, "VESAFB has font %04x at %04x:%04x\n",
  155. VESAFB_FONT, vesafb.glyphs.segment, vesafb.glyphs.offset );
  156. vesafb.font.height = VESAFB_CHAR_HEIGHT;
  157. vesafb.font.glyph = vesafb_glyph;
  158. }
  159. /**
  160. * Get VBE mode list
  161. *
  162. * @ret mode_numbers Mode number list (terminated with VBE_MODE_END)
  163. * @ret rc Return status code
  164. *
  165. * The caller is responsible for eventually freeing the mode list.
  166. */
  167. static int vesafb_mode_list ( uint16_t **mode_numbers ) {
  168. struct vbe_controller_info *controller = &vbe_buf.controller;
  169. userptr_t video_mode_ptr;
  170. uint16_t mode_number;
  171. uint16_t status;
  172. size_t len;
  173. int rc;
  174. /* Avoid returning uninitialised data on error */
  175. *mode_numbers = NULL;
  176. /* Get controller information block */
  177. controller->vbe_signature = 0;
  178. __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
  179. : "=a" ( status )
  180. : "a" ( VBE_CONTROLLER_INFO ),
  181. "D" ( __from_data16 ( controller ) )
  182. : "memory", "ebx", "edx" );
  183. if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
  184. DBGC ( &vbe_buf, "VESAFB could not get controller information: "
  185. "[%04x] %s\n", status, strerror ( rc ) );
  186. return rc;
  187. }
  188. if ( controller->vbe_signature != VBE_CONTROLLER_SIGNATURE ) {
  189. DBGC ( &vbe_buf, "VESAFB invalid controller signature "
  190. "\"%c%c%c%c\"\n", ( controller->vbe_signature >> 0 ),
  191. ( controller->vbe_signature >> 8 ),
  192. ( controller->vbe_signature >> 16 ),
  193. ( controller->vbe_signature >> 24 ) );
  194. DBGC_HDA ( &vbe_buf, 0, controller, sizeof ( *controller ) );
  195. return -EINVAL;
  196. }
  197. DBGC ( &vbe_buf, "VESAFB found VBE version %d.%d with mode list at "
  198. "%04x:%04x\n", controller->vbe_major_version,
  199. controller->vbe_minor_version,
  200. controller->video_mode_ptr.segment,
  201. controller->video_mode_ptr.offset );
  202. /* Calculate length of mode list */
  203. video_mode_ptr = real_to_user ( controller->video_mode_ptr.segment,
  204. controller->video_mode_ptr.offset );
  205. len = 0;
  206. do {
  207. copy_from_user ( &mode_number, video_mode_ptr, len,
  208. sizeof ( mode_number ) );
  209. len += sizeof ( mode_number );
  210. } while ( mode_number != VBE_MODE_END );
  211. /* Allocate and fill mode list */
  212. *mode_numbers = malloc ( len );
  213. if ( ! *mode_numbers )
  214. return -ENOMEM;
  215. copy_from_user ( *mode_numbers, video_mode_ptr, 0, len );
  216. return 0;
  217. }
  218. /**
  219. * Get video mode information
  220. *
  221. * @v mode_number Mode number
  222. * @ret rc Return status code
  223. */
  224. static int vesafb_mode_info ( unsigned int mode_number ) {
  225. struct vbe_mode_info *mode = &vbe_buf.mode;
  226. uint16_t status;
  227. int rc;
  228. /* Get mode information */
  229. __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
  230. : "=a" ( status )
  231. : "a" ( VBE_MODE_INFO ),
  232. "c" ( mode_number ),
  233. "D" ( __from_data16 ( mode ) )
  234. : "memory" );
  235. if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
  236. DBGC ( &vbe_buf, "VESAFB could not get mode %04x information: "
  237. "[%04x] %s\n", mode_number, status, strerror ( rc ) );
  238. return rc;
  239. }
  240. DBGC ( &vbe_buf, "VESAFB mode %04x %dx%d %dbpp(%d:%d:%d:%d) model "
  241. "%02x [x%d]%s%s%s%s%s\n", mode_number, mode->x_resolution,
  242. mode->y_resolution, mode->bits_per_pixel, mode->rsvd_mask_size,
  243. mode->red_mask_size, mode->green_mask_size, mode->blue_mask_size,
  244. mode->memory_model, ( mode->number_of_image_pages + 1 ),
  245. ( ( mode->mode_attributes & VBE_MODE_ATTR_SUPPORTED ) ?
  246. "" : " [unsupported]" ),
  247. ( ( mode->mode_attributes & VBE_MODE_ATTR_TTY ) ?
  248. " [tty]" : "" ),
  249. ( ( mode->mode_attributes & VBE_MODE_ATTR_GRAPHICS ) ?
  250. "" : " [text]" ),
  251. ( ( mode->mode_attributes & VBE_MODE_ATTR_LINEAR ) ?
  252. "" : " [nonlinear]" ),
  253. ( ( mode->mode_attributes & VBE_MODE_ATTR_TRIPLE_BUF ) ?
  254. " [buf]" : "" ) );
  255. return 0;
  256. }
  257. /**
  258. * Set video mode
  259. *
  260. * @v mode_number Mode number
  261. * @ret rc Return status code
  262. */
  263. static int vesafb_set_mode ( unsigned int mode_number ) {
  264. struct vbe_mode_info *mode = &vbe_buf.mode;
  265. uint16_t status;
  266. int rc;
  267. /* Get mode information */
  268. if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
  269. return rc;
  270. /* Record mode parameters */
  271. vesafb.start = mode->phys_base_ptr;
  272. vesafb.pixel.width = mode->x_resolution;
  273. vesafb.pixel.height = mode->y_resolution;
  274. vesafb.pixel.len = ( ( mode->bits_per_pixel + 7 ) / 8 );
  275. vesafb.pixel.stride = mode->bytes_per_scan_line;
  276. DBGC ( &vbe_buf, "VESAFB mode %04x has frame buffer at %08x\n",
  277. mode_number, mode->phys_base_ptr );
  278. /* Initialise font colours */
  279. vesafb.map.red_scale = ( 8 - mode->red_mask_size );
  280. vesafb.map.green_scale = ( 8 - mode->green_mask_size );
  281. vesafb.map.blue_scale = ( 8 - mode->blue_mask_size );
  282. vesafb.map.red_lsb = mode->red_field_position;
  283. vesafb.map.green_lsb = mode->green_field_position;
  284. vesafb.map.blue_lsb = mode->blue_field_position;
  285. /* Select this mode */
  286. __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
  287. : "=a" ( status )
  288. : "a" ( VBE_SET_MODE ),
  289. "b" ( mode_number ) );
  290. if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
  291. DBGC ( &vbe_buf, "VESAFB could not set mode %04x: [%04x] %s\n",
  292. mode_number, status, strerror ( rc ) );
  293. return rc;
  294. }
  295. return 0;
  296. }
  297. /**
  298. * Select video mode
  299. *
  300. * @v mode_numbers Mode number list (terminated with VBE_MODE_END)
  301. * @v min_width Minimum required width (in pixels)
  302. * @v min_height Minimum required height (in pixels)
  303. * @v min_bpp Minimum required colour depth (in bits per pixel)
  304. * @ret mode_number Mode number, or negative error
  305. */
  306. static int vesafb_select_mode ( const uint16_t *mode_numbers,
  307. unsigned int min_width, unsigned int min_height,
  308. unsigned int min_bpp ) {
  309. struct vbe_mode_info *mode = &vbe_buf.mode;
  310. int best_mode_number = -ENOENT;
  311. unsigned int best_score = INT_MAX;
  312. unsigned int score;
  313. uint16_t mode_number;
  314. int rc;
  315. /* Find the first suitable mode */
  316. while ( ( mode_number = *(mode_numbers++) ) != VBE_MODE_END ) {
  317. /* Force linear mode variant */
  318. mode_number |= VBE_MODE_LINEAR;
  319. /* Get mode information */
  320. if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
  321. continue;
  322. /* Skip unusable modes */
  323. if ( ( mode->mode_attributes & ( VBE_MODE_ATTR_SUPPORTED |
  324. VBE_MODE_ATTR_GRAPHICS |
  325. VBE_MODE_ATTR_LINEAR ) ) !=
  326. ( VBE_MODE_ATTR_SUPPORTED | VBE_MODE_ATTR_GRAPHICS |
  327. VBE_MODE_ATTR_LINEAR ) ) {
  328. continue;
  329. }
  330. if ( mode->memory_model != VBE_MODE_MODEL_DIRECT_COLOUR )
  331. continue;
  332. /* Skip modes not meeting the requirements */
  333. if ( ( mode->x_resolution < min_width ) ||
  334. ( mode->y_resolution < min_height ) ||
  335. ( mode->bits_per_pixel < min_bpp ) ) {
  336. continue;
  337. }
  338. /* Select this mode if it has the best (i.e. lowest)
  339. * score. We choose the scoring system to favour
  340. * modes close to the specified width and height;
  341. * within modes of the same width and height we prefer
  342. * a higher colour depth.
  343. */
  344. score = ( ( mode->x_resolution * mode->y_resolution ) -
  345. mode->bits_per_pixel );
  346. if ( score < best_score ) {
  347. best_mode_number = mode_number;
  348. best_score = score;
  349. }
  350. }
  351. if ( best_mode_number >= 0 ) {
  352. DBGC ( &vbe_buf, "VESAFB selected mode %04x\n",
  353. best_mode_number );
  354. } else {
  355. DBGC ( &vbe_buf, "VESAFB found no suitable mode\n" );
  356. }
  357. return best_mode_number;
  358. }
  359. /**
  360. * Restore video mode
  361. *
  362. */
  363. static void vesafb_restore ( void ) {
  364. uint32_t discard_a;
  365. /* Restore saved VGA mode */
  366. __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
  367. : "=a" ( discard_a )
  368. : "a" ( VBE_SET_VGA_MODE | vesafb.saved_mode ) );
  369. DBGC ( &vbe_buf, "VESAFB restored VGA mode %#02x\n",
  370. vesafb.saved_mode );
  371. }
  372. /**
  373. * Initialise VESA frame buffer
  374. *
  375. * @v config Console configuration, or NULL to reset
  376. * @ret rc Return status code
  377. */
  378. static int vesafb_init ( struct console_configuration *config ) {
  379. uint32_t discard_b;
  380. uint16_t *mode_numbers;
  381. int mode_number;
  382. int rc;
  383. /* Record current VGA mode */
  384. __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
  385. : "=a" ( vesafb.saved_mode ), "=b" ( discard_b )
  386. : "a" ( VBE_GET_VGA_MODE ) );
  387. DBGC ( &vbe_buf, "VESAFB saved VGA mode %#02x\n", vesafb.saved_mode );
  388. /* Get VESA mode list */
  389. if ( ( rc = vesafb_mode_list ( &mode_numbers ) ) != 0 )
  390. goto err_mode_list;
  391. /* Select mode */
  392. if ( ( mode_number = vesafb_select_mode ( mode_numbers, config->width,
  393. config->height,
  394. config->depth ) ) < 0 ) {
  395. rc = mode_number;
  396. goto err_select_mode;
  397. }
  398. /* Set mode */
  399. if ( ( rc = vesafb_set_mode ( mode_number ) ) != 0 )
  400. goto err_set_mode;
  401. /* Get font data */
  402. vesafb_font();
  403. /* Initialise frame buffer console */
  404. if ( ( rc = fbcon_init ( &vesafb.fbcon, phys_to_user ( vesafb.start ),
  405. &vesafb.pixel, &vesafb.map, &vesafb.font,
  406. config ) ) != 0 )
  407. goto err_fbcon_init;
  408. free ( mode_numbers );
  409. return 0;
  410. fbcon_fini ( &vesafb.fbcon );
  411. err_fbcon_init:
  412. err_set_mode:
  413. vesafb_restore();
  414. err_select_mode:
  415. free ( mode_numbers );
  416. err_mode_list:
  417. return rc;
  418. }
  419. /**
  420. * Finalise VESA frame buffer
  421. *
  422. */
  423. static void vesafb_fini ( void ) {
  424. /* Finalise frame buffer console */
  425. fbcon_fini ( &vesafb.fbcon );
  426. /* Restore saved VGA mode */
  427. vesafb_restore();
  428. }
  429. /**
  430. * Print a character to current cursor position
  431. *
  432. * @v character Character
  433. */
  434. static void vesafb_putchar ( int character ) {
  435. fbcon_putchar ( &vesafb.fbcon, character );
  436. }
  437. /**
  438. * Configure console
  439. *
  440. * @v config Console configuration, or NULL to reset
  441. * @ret rc Return status code
  442. */
  443. static int vesafb_configure ( struct console_configuration *config ) {
  444. int rc;
  445. /* Reset console, if applicable */
  446. if ( ! vesafb_console.disabled ) {
  447. vesafb_fini();
  448. bios_console.disabled &= ~CONSOLE_DISABLED_OUTPUT;
  449. ansicol_reset_magic();
  450. }
  451. vesafb_console.disabled = CONSOLE_DISABLED;
  452. /* Do nothing more unless we have a usable configuration */
  453. if ( ( config == NULL ) ||
  454. ( config->width == 0 ) || ( config->height == 0 ) ) {
  455. return 0;
  456. }
  457. /* Initialise VESA frame buffer */
  458. if ( ( rc = vesafb_init ( config ) ) != 0 )
  459. return rc;
  460. /* Mark console as enabled */
  461. vesafb_console.disabled = 0;
  462. bios_console.disabled |= CONSOLE_DISABLED_OUTPUT;
  463. /* Set magic colour to transparent if we have a background picture */
  464. if ( config->pixbuf )
  465. ansicol_set_magic_transparent();
  466. return 0;
  467. }
  468. /** VESA frame buffer console driver */
  469. struct console_driver vesafb_console __console_driver = {
  470. .usage = CONSOLE_VESAFB,
  471. .putchar = vesafb_putchar,
  472. .configure = vesafb_configure,
  473. .disabled = CONSOLE_DISABLED,
  474. };