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.

fbcon.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. * Frame buffer console
  27. *
  28. */
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <assert.h>
  32. #include <byteswap.h>
  33. #include <ipxe/ansiesc.h>
  34. #include <ipxe/image.h>
  35. #include <ipxe/pixbuf.h>
  36. #include <ipxe/umalloc.h>
  37. #include <ipxe/console.h>
  38. #include <ipxe/fbcon.h>
  39. /**
  40. * Calculate raw colour value
  41. *
  42. * @v fbcon Frame buffer console
  43. * @v rgb 24-bit RGB value
  44. * @ret raw Raw colour
  45. */
  46. static uint32_t fbcon_colour ( struct fbcon *fbcon, uint32_t rgb ) {
  47. struct fbcon_colour_map *map = fbcon->map;
  48. uint8_t red = ( rgb >> 16 );
  49. uint8_t green = ( rgb >> 8 );
  50. uint8_t blue = ( rgb >> 0 );
  51. uint32_t mapped;
  52. mapped = ( ( ( red >> map->red_scale ) << map->red_lsb ) |
  53. ( ( green >> map->green_scale ) << map->green_lsb ) |
  54. ( ( blue >> map->blue_scale ) << map->blue_lsb ) );
  55. return cpu_to_le32 ( mapped );
  56. }
  57. /**
  58. * Calculate ANSI font colour
  59. *
  60. * @v fbcon Frame buffer console
  61. * @v ansicol ANSI colour value (0-based)
  62. * @ret colour Raw colour
  63. */
  64. static uint32_t fbcon_ansi_colour ( struct fbcon *fbcon,
  65. unsigned int ansicol ) {
  66. uint32_t rgb;
  67. /* Treat ansicol as 3-bit BGR with intensity 0xaa */
  68. rgb = ( ( ( ansicol & ( 1 << 0 ) ) ? 0xaa0000 : 0 ) |
  69. ( ( ansicol & ( 1 << 1 ) ) ? 0x00aa00 : 0 ) |
  70. ( ( ansicol & ( 1 << 2 ) ) ? 0x0000aa : 0 ) );
  71. return fbcon_colour ( fbcon, rgb );
  72. }
  73. /**
  74. * Set default foreground colour
  75. *
  76. * @v fbcon Frame buffer console
  77. */
  78. static void fbcon_set_default_foreground ( struct fbcon *fbcon ) {
  79. /* Default to non-bold white foreground */
  80. fbcon->foreground = fbcon_ansi_colour ( fbcon, 0x7 );
  81. fbcon->bold = 0;
  82. }
  83. /**
  84. * Set default background colour
  85. *
  86. * @v fbcon Frame buffer console
  87. */
  88. static void fbcon_set_default_background ( struct fbcon *fbcon ) {
  89. /* Default to transparent background */
  90. fbcon->background = FBCON_TRANSPARENT;
  91. }
  92. /**
  93. * Clear rows of characters
  94. *
  95. * @v fbcon Frame buffer console
  96. * @v ypos Starting Y position
  97. */
  98. static void fbcon_clear ( struct fbcon *fbcon, unsigned int ypos ) {
  99. struct fbcon_text_cell cell = {
  100. .foreground = fbcon->foreground,
  101. .background = fbcon->background,
  102. .character = ' ',
  103. };
  104. size_t offset;
  105. unsigned int xpos;
  106. /* Clear stored character array */
  107. for ( ; ypos < fbcon->character.height ; ypos++ ) {
  108. offset = ( ypos * fbcon->character.width * sizeof ( cell ) );
  109. for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
  110. copy_to_user ( fbcon->text.start, offset, &cell,
  111. sizeof ( cell ) );
  112. offset += sizeof ( cell );
  113. }
  114. }
  115. }
  116. /**
  117. * Store character at specified position
  118. *
  119. * @v fbcon Frame buffer console
  120. * @v cell Text cell
  121. * @v xpos X position
  122. * @v ypos Y position
  123. */
  124. static void fbcon_store ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
  125. unsigned int xpos, unsigned int ypos ) {
  126. size_t offset;
  127. /* Store cell */
  128. offset = ( ( ( ypos * fbcon->character.width ) + xpos ) *
  129. sizeof ( *cell ) );
  130. copy_to_user ( fbcon->text.start, offset, cell, sizeof ( *cell ) );
  131. }
  132. /**
  133. * Draw character at specified position
  134. *
  135. * @v fbcon Frame buffer console
  136. * @v cell Text cell
  137. * @v xpos X position
  138. * @v ypos Y position
  139. */
  140. static void fbcon_draw ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
  141. unsigned int xpos, unsigned int ypos ) {
  142. uint8_t glyph[fbcon->font->height];
  143. size_t offset;
  144. size_t pixel_len;
  145. size_t skip_len;
  146. unsigned int row;
  147. unsigned int column;
  148. uint8_t bitmask;
  149. int transparent;
  150. void *src;
  151. /* Get font character */
  152. fbcon->font->glyph ( cell->character, glyph );
  153. /* Calculate pixel geometry */
  154. offset = ( fbcon->indent +
  155. ( ypos * fbcon->character.stride ) +
  156. ( xpos * fbcon->character.len ) );
  157. pixel_len = fbcon->pixel->len;
  158. skip_len = ( fbcon->pixel->stride - fbcon->character.len );
  159. /* Check for transparent background colour */
  160. transparent = ( cell->background == FBCON_TRANSPARENT );
  161. /* Draw character rows */
  162. for ( row = 0 ; row < fbcon->font->height ; row++ ) {
  163. /* Draw background picture, if applicable */
  164. if ( transparent ) {
  165. if ( fbcon->picture.start ) {
  166. memcpy_user ( fbcon->start, offset,
  167. fbcon->picture.start, offset,
  168. fbcon->character.len );
  169. } else {
  170. memset_user ( fbcon->start, offset, 0,
  171. fbcon->character.len );
  172. }
  173. }
  174. /* Draw character row */
  175. for ( column = FBCON_CHAR_WIDTH, bitmask = glyph[row] ;
  176. column ; column--, bitmask <<= 1, offset += pixel_len ) {
  177. if ( bitmask & 0x80 ) {
  178. src = &cell->foreground;
  179. } else if ( ! transparent ) {
  180. src = &cell->background;
  181. } else {
  182. continue;
  183. }
  184. copy_to_user ( fbcon->start, offset, src, pixel_len );
  185. }
  186. /* Move to next row */
  187. offset += skip_len;
  188. }
  189. }
  190. /**
  191. * Redraw all characters
  192. *
  193. * @v fbcon Frame buffer console
  194. */
  195. static void fbcon_redraw ( struct fbcon *fbcon ) {
  196. struct fbcon_text_cell cell;
  197. size_t offset = 0;
  198. unsigned int xpos;
  199. unsigned int ypos;
  200. /* Redraw characters */
  201. for ( ypos = 0 ; ypos < fbcon->character.height ; ypos++ ) {
  202. for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
  203. copy_from_user ( &cell, fbcon->text.start, offset,
  204. sizeof ( cell ) );
  205. fbcon_draw ( fbcon, &cell, xpos, ypos );
  206. offset += sizeof ( cell );
  207. }
  208. }
  209. }
  210. /**
  211. * Scroll screen
  212. *
  213. * @v fbcon Frame buffer console
  214. */
  215. static void fbcon_scroll ( struct fbcon *fbcon ) {
  216. size_t row_len;
  217. /* Sanity check */
  218. assert ( fbcon->ypos == fbcon->character.height );
  219. /* Scroll up character array */
  220. row_len = ( fbcon->character.width * sizeof ( struct fbcon_text_cell ));
  221. memmove_user ( fbcon->text.start, 0, fbcon->text.start, row_len,
  222. ( row_len * ( fbcon->character.height - 1 ) ) );
  223. fbcon_clear ( fbcon, ( fbcon->character.height - 1 ) );
  224. /* Update cursor position */
  225. fbcon->ypos--;
  226. /* Redraw all characters */
  227. fbcon_redraw ( fbcon );
  228. }
  229. /**
  230. * Draw character at cursor position
  231. *
  232. * @v fbcon Frame buffer console
  233. * @v show_cursor Show cursor
  234. */
  235. static void fbcon_draw_cursor ( struct fbcon *fbcon, int show_cursor ) {
  236. struct fbcon_text_cell cell;
  237. size_t offset;
  238. offset = ( ( ( fbcon->ypos * fbcon->character.width ) + fbcon->xpos ) *
  239. sizeof ( cell ) );
  240. copy_from_user ( &cell, fbcon->text.start, offset, sizeof ( cell ) );
  241. if ( show_cursor ) {
  242. cell.background = fbcon->foreground;
  243. cell.foreground = ( ( fbcon->background == FBCON_TRANSPARENT ) ?
  244. 0 : fbcon->background );
  245. }
  246. fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
  247. }
  248. /**
  249. * Handle ANSI CUP (cursor position)
  250. *
  251. * @v ctx ANSI escape sequence context
  252. * @v count Parameter count
  253. * @v params[0] Row (1 is top)
  254. * @v params[1] Column (1 is left)
  255. */
  256. static void fbcon_handle_cup ( struct ansiesc_context *ctx,
  257. unsigned int count __unused, int params[] ) {
  258. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  259. int cx = ( params[1] - 1 );
  260. int cy = ( params[0] - 1 );
  261. fbcon_draw_cursor ( fbcon, 0 );
  262. fbcon->xpos = cx;
  263. if ( fbcon->xpos >= fbcon->character.width )
  264. fbcon->xpos = 0;
  265. fbcon->ypos = cy;
  266. if ( fbcon->ypos >= fbcon->character.height )
  267. fbcon->ypos = 0;
  268. fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
  269. }
  270. /**
  271. * Handle ANSI ED (erase in page)
  272. *
  273. * @v ctx ANSI escape sequence context
  274. * @v count Parameter count
  275. * @v params[0] Region to erase
  276. */
  277. static void fbcon_handle_ed ( struct ansiesc_context *ctx,
  278. unsigned int count __unused,
  279. int params[] __unused ) {
  280. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  281. /* We assume that we always clear the whole screen */
  282. assert ( params[0] == ANSIESC_ED_ALL );
  283. /* Clear character array */
  284. fbcon_clear ( fbcon, 0 );
  285. /* Redraw all characters */
  286. fbcon_redraw ( fbcon );
  287. /* Reset cursor position */
  288. fbcon->xpos = 0;
  289. fbcon->ypos = 0;
  290. fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
  291. }
  292. /**
  293. * Handle ANSI SGR (set graphics rendition)
  294. *
  295. * @v ctx ANSI escape sequence context
  296. * @v count Parameter count
  297. * @v params List of graphic rendition aspects
  298. */
  299. static void fbcon_handle_sgr ( struct ansiesc_context *ctx, unsigned int count,
  300. int params[] ) {
  301. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  302. uint32_t *custom = NULL;
  303. uint32_t rgb;
  304. unsigned int end;
  305. unsigned int i;
  306. int aspect;
  307. for ( i = 0 ; i < count ; i++ ) {
  308. /* Process aspect */
  309. aspect = params[i];
  310. if ( aspect == 0 ) {
  311. fbcon_set_default_foreground ( fbcon );
  312. fbcon_set_default_background ( fbcon );
  313. } else if ( aspect == 1 ) {
  314. fbcon->bold = fbcon_colour ( fbcon, FBCON_BOLD );
  315. } else if ( aspect == 22 ) {
  316. fbcon->bold = 0;
  317. } else if ( ( aspect >= 30 ) && ( aspect <= 37 ) ) {
  318. fbcon->foreground =
  319. fbcon_ansi_colour ( fbcon, aspect - 30 );
  320. } else if ( aspect == 38 ) {
  321. custom = &fbcon->foreground;
  322. } else if ( aspect == 39 ) {
  323. fbcon_set_default_foreground ( fbcon );
  324. } else if ( ( aspect >= 40 ) && ( aspect <= 47 ) ) {
  325. fbcon->background =
  326. fbcon_ansi_colour ( fbcon, aspect - 40 );
  327. } else if ( aspect == 48 ) {
  328. custom = &fbcon->background;
  329. } else if ( aspect == 49 ) {
  330. fbcon_set_default_background ( fbcon );
  331. }
  332. /* Process custom RGB colour, if applicable
  333. *
  334. * We support the xterm-compatible
  335. * "<ESC>[38;2;<red>;<green>;<blue>m" and
  336. * "<ESC>[48;2;<red>;<green>;<blue>m" sequences.
  337. */
  338. if ( custom ) {
  339. rgb = 0;
  340. end = ( i + 5 );
  341. for ( ; ( i < count ) && ( i < end ) ; i++ )
  342. rgb = ( ( rgb << 8 ) | params[i] );
  343. *custom = fbcon_colour ( fbcon, rgb );
  344. custom = NULL;
  345. }
  346. }
  347. }
  348. /**
  349. * Handle ANSI DECTCEM set (show cursor)
  350. *
  351. * @v ctx ANSI escape sequence context
  352. * @v count Parameter count
  353. * @v params List of graphic rendition aspects
  354. */
  355. static void fbcon_handle_dectcem_set ( struct ansiesc_context *ctx,
  356. unsigned int count __unused,
  357. int params[] __unused ) {
  358. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  359. fbcon->show_cursor = 1;
  360. fbcon_draw_cursor ( fbcon, 1 );
  361. }
  362. /**
  363. * Handle ANSI DECTCEM reset (hide cursor)
  364. *
  365. * @v ctx ANSI escape sequence context
  366. * @v count Parameter count
  367. * @v params List of graphic rendition aspects
  368. */
  369. static void fbcon_handle_dectcem_reset ( struct ansiesc_context *ctx,
  370. unsigned int count __unused,
  371. int params[] __unused ) {
  372. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  373. fbcon->show_cursor = 0;
  374. fbcon_draw_cursor ( fbcon, 0 );
  375. }
  376. /** ANSI escape sequence handlers */
  377. static struct ansiesc_handler fbcon_ansiesc_handlers[] = {
  378. { ANSIESC_CUP, fbcon_handle_cup },
  379. { ANSIESC_ED, fbcon_handle_ed },
  380. { ANSIESC_SGR, fbcon_handle_sgr },
  381. { ANSIESC_DECTCEM_SET, fbcon_handle_dectcem_set },
  382. { ANSIESC_DECTCEM_RESET, fbcon_handle_dectcem_reset },
  383. { 0, NULL }
  384. };
  385. /**
  386. * Print a character to current cursor position
  387. *
  388. * @v fbcon Frame buffer console
  389. * @v character Character
  390. */
  391. void fbcon_putchar ( struct fbcon *fbcon, int character ) {
  392. struct fbcon_text_cell cell;
  393. /* Intercept ANSI escape sequences */
  394. character = ansiesc_process ( &fbcon->ctx, character );
  395. if ( character < 0 )
  396. return;
  397. /* Handle control characters */
  398. switch ( character ) {
  399. case '\r':
  400. fbcon_draw_cursor ( fbcon, 0 );
  401. fbcon->xpos = 0;
  402. break;
  403. case '\n':
  404. fbcon_draw_cursor ( fbcon, 0 );
  405. fbcon->xpos = 0;
  406. fbcon->ypos++;
  407. break;
  408. case '\b':
  409. fbcon_draw_cursor ( fbcon, 0 );
  410. if ( fbcon->xpos ) {
  411. fbcon->xpos--;
  412. } else if ( fbcon->ypos ) {
  413. fbcon->xpos = ( fbcon->character.width - 1 );
  414. fbcon->ypos--;
  415. }
  416. break;
  417. default:
  418. /* Print character at current cursor position */
  419. cell.foreground = ( fbcon->foreground | fbcon->bold );
  420. cell.background = fbcon->background;
  421. cell.character = character;
  422. fbcon_store ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
  423. fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
  424. /* Advance cursor */
  425. fbcon->xpos++;
  426. if ( fbcon->xpos >= fbcon->character.width ) {
  427. fbcon->xpos = 0;
  428. fbcon->ypos++;
  429. }
  430. break;
  431. }
  432. /* Scroll screen if necessary */
  433. if ( fbcon->ypos >= fbcon->character.height )
  434. fbcon_scroll ( fbcon );
  435. /* Show cursor */
  436. fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
  437. }
  438. /**
  439. * Initialise background picture
  440. *
  441. * @v fbcon Frame buffer console
  442. * @v pixbuf Background picture
  443. * @ret rc Return status code
  444. */
  445. static int fbcon_picture_init ( struct fbcon *fbcon,
  446. struct pixel_buffer *pixbuf ) {
  447. struct fbcon_geometry *pixel = fbcon->pixel;
  448. struct fbcon_picture *picture = &fbcon->picture;
  449. size_t len;
  450. size_t pixbuf_stride;
  451. size_t indent;
  452. size_t pixbuf_indent;
  453. size_t offset;
  454. size_t pixbuf_offset;
  455. uint32_t rgb;
  456. uint32_t raw;
  457. unsigned int x;
  458. unsigned int y;
  459. unsigned int width;
  460. unsigned int height;
  461. int xgap;
  462. int ygap;
  463. int rc;
  464. /* Allocate buffer */
  465. len = ( pixel->height * pixel->stride );
  466. picture->start = umalloc ( len );
  467. if ( ! picture->start ) {
  468. DBGC ( fbcon, "FBCON %p could not allocate %zd bytes for "
  469. "picture\n", fbcon, len );
  470. rc = -ENOMEM;
  471. goto err_umalloc;
  472. }
  473. /* Centre picture on console */
  474. pixbuf_stride = ( pixbuf->width * sizeof ( rgb ) );
  475. xgap = ( ( ( int ) ( pixel->width - pixbuf->width ) ) / 2 );
  476. ygap = ( ( ( int ) ( pixel->height - pixbuf->height ) ) / 2 );
  477. indent = ( ( ( ( ygap >= 0 ) ? ygap : 0 ) * pixel->stride ) +
  478. ( ( ( xgap >= 0 ) ? xgap : 0 ) * pixel->len ) );
  479. pixbuf_indent = ( ( ( ( ygap < 0 ) ? -ygap : 0 ) * pixbuf_stride ) +
  480. ( ( ( xgap < 0 ) ? -xgap : 0 ) * sizeof ( rgb ) ) );
  481. width = pixbuf->width;
  482. if ( width > pixel->width )
  483. width = pixel->width;
  484. height = pixbuf->height;
  485. if ( height > pixel->height )
  486. height = pixel->height;
  487. DBGC ( fbcon, "FBCON %p picture is pixel %dx%d at [%d,%d),[%d,%d)\n",
  488. fbcon, width, height, xgap, ( xgap + pixbuf->width ), ygap,
  489. ( ygap + pixbuf->height ) );
  490. /* Convert to frame buffer raw format */
  491. memset_user ( picture->start, 0, 0, len );
  492. for ( y = 0 ; y < height ; y++ ) {
  493. offset = ( indent + ( y * pixel->stride ) );
  494. pixbuf_offset = ( pixbuf_indent + ( y * pixbuf_stride ) );
  495. for ( x = 0 ; x < width ; x++ ) {
  496. copy_from_user ( &rgb, pixbuf->data, pixbuf_offset,
  497. sizeof ( rgb ) );
  498. raw = fbcon_colour ( fbcon, rgb );
  499. copy_to_user ( picture->start, offset, &raw,
  500. pixel->len );
  501. offset += pixel->len;
  502. pixbuf_offset += sizeof ( rgb );
  503. }
  504. }
  505. return 0;
  506. ufree ( picture->start );
  507. err_umalloc:
  508. return rc;
  509. }
  510. /**
  511. * Initialise frame buffer console
  512. *
  513. * @v fbcon Frame buffer console
  514. * @v start Start address
  515. * @v pixel Pixel geometry
  516. * @v map Colour mapping
  517. * @v font Font definition
  518. * @v config Console configuration
  519. * @ret rc Return status code
  520. */
  521. int fbcon_init ( struct fbcon *fbcon, userptr_t start,
  522. struct fbcon_geometry *pixel,
  523. struct fbcon_colour_map *map,
  524. struct fbcon_font *font,
  525. struct console_configuration *config ) {
  526. int width;
  527. int height;
  528. unsigned int xgap;
  529. unsigned int ygap;
  530. unsigned int left;
  531. unsigned int right;
  532. unsigned int top;
  533. unsigned int bottom;
  534. int rc;
  535. /* Initialise data structure */
  536. memset ( fbcon, 0, sizeof ( *fbcon ) );
  537. fbcon->start = start;
  538. fbcon->pixel = pixel;
  539. assert ( pixel->len <= sizeof ( uint32_t ) );
  540. fbcon->map = map;
  541. fbcon->font = font;
  542. fbcon->ctx.handlers = fbcon_ansiesc_handlers;
  543. fbcon->show_cursor = 1;
  544. /* Derive overall length */
  545. fbcon->len = ( pixel->height * pixel->stride );
  546. DBGC ( fbcon, "FBCON %p at [%08lx,%08lx)\n", fbcon,
  547. user_to_phys ( fbcon->start, 0 ),
  548. user_to_phys ( fbcon->start, fbcon->len ) );
  549. /* Calculate margin. If the actual screen size is larger than
  550. * the requested screen size, then update the margins so that
  551. * the margin remains relative to the requested screen size.
  552. * (As an exception, if a zero margin was specified then treat
  553. * this as meaning "expand to edge of actual screen".)
  554. */
  555. xgap = ( pixel->width - config->width );
  556. ygap = ( pixel->height - config->height );
  557. left = ( xgap / 2 );
  558. right = ( xgap - left );
  559. top = ( ygap / 2 );
  560. bottom = ( ygap - top );
  561. fbcon->margin.left = ( config->left + ( config->left ? left : 0 ) );
  562. fbcon->margin.right = ( config->right + ( config->right ? right : 0 ) );
  563. fbcon->margin.top = ( config->top + ( config->top ? top : 0 ) );
  564. fbcon->margin.bottom =
  565. ( config->bottom + ( config->bottom ? bottom : 0 ) );
  566. /* Expand margin to accommodate whole characters */
  567. width = ( pixel->width - fbcon->margin.left - fbcon->margin.right );
  568. height = ( pixel->height - fbcon->margin.top - fbcon->margin.bottom );
  569. if ( ( width < FBCON_CHAR_WIDTH ) ||
  570. ( height < ( ( int ) font->height ) ) ) {
  571. DBGC ( fbcon, "FBCON %p has unusable character area "
  572. "[%d-%d),[%d-%d)\n", fbcon, fbcon->margin.left,
  573. ( pixel->width - fbcon->margin.right ),
  574. fbcon->margin.top,
  575. ( pixel->height - fbcon->margin.bottom ) );
  576. rc = -EINVAL;
  577. goto err_margin;
  578. }
  579. xgap = ( width % FBCON_CHAR_WIDTH );
  580. ygap = ( height % font->height );
  581. fbcon->margin.left += ( xgap / 2 );
  582. fbcon->margin.top += ( ygap / 2 );
  583. fbcon->margin.right += ( xgap - ( xgap / 2 ) );
  584. fbcon->margin.bottom += ( ygap - ( ygap / 2 ) );
  585. fbcon->indent = ( ( fbcon->margin.top * pixel->stride ) +
  586. ( fbcon->margin.left * pixel->len ) );
  587. /* Derive character geometry from pixel geometry */
  588. fbcon->character.width = ( width / FBCON_CHAR_WIDTH );
  589. fbcon->character.height = ( height / font->height );
  590. fbcon->character.len = ( pixel->len * FBCON_CHAR_WIDTH );
  591. fbcon->character.stride = ( pixel->stride * font->height );
  592. DBGC ( fbcon, "FBCON %p is pixel %dx%d, char %dx%d at "
  593. "[%d-%d),[%d-%d)\n", fbcon, fbcon->pixel->width,
  594. fbcon->pixel->height, fbcon->character.width,
  595. fbcon->character.height, fbcon->margin.left,
  596. ( fbcon->pixel->width - fbcon->margin.right ),
  597. fbcon->margin.top,
  598. ( fbcon->pixel->height - fbcon->margin.bottom ) );
  599. /* Set default colours */
  600. fbcon_set_default_foreground ( fbcon );
  601. fbcon_set_default_background ( fbcon );
  602. /* Allocate and initialise stored character array */
  603. fbcon->text.start = umalloc ( fbcon->character.width *
  604. fbcon->character.height *
  605. sizeof ( struct fbcon_text_cell ) );
  606. if ( ! fbcon->text.start ) {
  607. rc = -ENOMEM;
  608. goto err_text;
  609. }
  610. fbcon_clear ( fbcon, 0 );
  611. /* Set framebuffer to all black (including margins) */
  612. memset_user ( fbcon->start, 0, 0, fbcon->len );
  613. /* Generate pixel buffer from background image, if applicable */
  614. if ( config->pixbuf &&
  615. ( ( rc = fbcon_picture_init ( fbcon, config->pixbuf ) ) != 0 ) )
  616. goto err_picture;
  617. /* Draw background picture (including margins), if applicable */
  618. if ( fbcon->picture.start ) {
  619. memcpy_user ( fbcon->start, 0, fbcon->picture.start, 0,
  620. fbcon->len );
  621. }
  622. /* Update console width and height */
  623. console_set_size ( fbcon->character.width, fbcon->character.height );
  624. return 0;
  625. ufree ( fbcon->picture.start );
  626. err_picture:
  627. ufree ( fbcon->text.start );
  628. err_text:
  629. err_margin:
  630. return rc;
  631. }
  632. /**
  633. * Finalise frame buffer console
  634. *
  635. * @v fbcon Frame buffer console
  636. */
  637. void fbcon_fini ( struct fbcon *fbcon ) {
  638. ufree ( fbcon->text.start );
  639. ufree ( fbcon->picture.start );
  640. }