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

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