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

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