Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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