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.

vesafb.c 14KB

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