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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. struct fbcon_font_glyph glyph;
  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. copy_from_user ( &glyph, fbcon->font->start,
  153. ( cell->character * sizeof ( glyph ) ),
  154. sizeof ( glyph ) );
  155. /* Calculate pixel geometry */
  156. offset = ( fbcon->indent +
  157. ( ypos * fbcon->character.stride ) +
  158. ( xpos * fbcon->character.len ) );
  159. pixel_len = fbcon->pixel->len;
  160. skip_len = ( fbcon->pixel->stride - fbcon->character.len );
  161. /* Check for transparent background colour */
  162. transparent = ( cell->background == FBCON_TRANSPARENT );
  163. /* Draw character rows */
  164. for ( row = 0 ; row < FBCON_CHAR_HEIGHT ; row++ ) {
  165. /* Draw background picture, if applicable */
  166. if ( transparent ) {
  167. if ( fbcon->picture.start ) {
  168. memcpy_user ( fbcon->start, offset,
  169. fbcon->picture.start, offset,
  170. fbcon->character.len );
  171. } else {
  172. memset_user ( fbcon->start, offset, 0,
  173. fbcon->character.len );
  174. }
  175. }
  176. /* Draw character row */
  177. for ( column = FBCON_CHAR_WIDTH, bitmask = glyph.bitmask[row] ;
  178. column ; column--, bitmask <<= 1, offset += pixel_len ) {
  179. if ( bitmask & 0x80 ) {
  180. src = &cell->foreground;
  181. } else if ( ! transparent ) {
  182. src = &cell->background;
  183. } else {
  184. continue;
  185. }
  186. copy_to_user ( fbcon->start, offset, src, pixel_len );
  187. }
  188. /* Move to next row */
  189. offset += skip_len;
  190. }
  191. }
  192. /**
  193. * Redraw all characters
  194. *
  195. * @v fbcon Frame buffer console
  196. */
  197. static void fbcon_redraw ( struct fbcon *fbcon ) {
  198. struct fbcon_text_cell cell;
  199. size_t offset = 0;
  200. unsigned int xpos;
  201. unsigned int ypos;
  202. /* Redraw characters */
  203. for ( ypos = 0 ; ypos < fbcon->character.height ; ypos++ ) {
  204. for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
  205. copy_from_user ( &cell, fbcon->text.start, offset,
  206. sizeof ( cell ) );
  207. fbcon_draw ( fbcon, &cell, xpos, ypos );
  208. offset += sizeof ( cell );
  209. }
  210. }
  211. }
  212. /**
  213. * Scroll screen
  214. *
  215. * @v fbcon Frame buffer console
  216. */
  217. static void fbcon_scroll ( struct fbcon *fbcon ) {
  218. size_t row_len;
  219. /* Sanity check */
  220. assert ( fbcon->ypos == fbcon->character.height );
  221. /* Scroll up character array */
  222. row_len = ( fbcon->character.width * sizeof ( struct fbcon_text_cell ));
  223. memmove_user ( fbcon->text.start, 0, fbcon->text.start, row_len,
  224. ( row_len * ( fbcon->character.height - 1 ) ) );
  225. fbcon_clear ( fbcon, ( fbcon->character.height - 1 ) );
  226. /* Update cursor position */
  227. fbcon->ypos--;
  228. /* Redraw all characters */
  229. fbcon_redraw ( fbcon );
  230. }
  231. /**
  232. * Draw character at cursor position
  233. *
  234. * @v fbcon Frame buffer console
  235. * @v show_cursor Show cursor
  236. */
  237. static void fbcon_draw_cursor ( struct fbcon *fbcon, int show_cursor ) {
  238. struct fbcon_text_cell cell;
  239. size_t offset;
  240. offset = ( ( ( fbcon->ypos * fbcon->character.width ) + fbcon->xpos ) *
  241. sizeof ( cell ) );
  242. copy_from_user ( &cell, fbcon->text.start, offset, sizeof ( cell ) );
  243. if ( show_cursor ) {
  244. cell.background = fbcon->foreground;
  245. cell.foreground = ( ( fbcon->background == FBCON_TRANSPARENT ) ?
  246. 0 : fbcon->background );
  247. }
  248. fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
  249. }
  250. /**
  251. * Handle ANSI CUP (cursor position)
  252. *
  253. * @v ctx ANSI escape sequence context
  254. * @v count Parameter count
  255. * @v params[0] Row (1 is top)
  256. * @v params[1] Column (1 is left)
  257. */
  258. static void fbcon_handle_cup ( struct ansiesc_context *ctx,
  259. unsigned int count __unused, int params[] ) {
  260. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  261. int cx = ( params[1] - 1 );
  262. int cy = ( params[0] - 1 );
  263. fbcon_draw_cursor ( fbcon, 0 );
  264. fbcon->xpos = cx;
  265. if ( fbcon->xpos >= fbcon->character.width )
  266. fbcon->xpos = 0;
  267. fbcon->ypos = cy;
  268. if ( fbcon->ypos >= fbcon->character.height )
  269. fbcon->ypos = 0;
  270. fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
  271. }
  272. /**
  273. * Handle ANSI ED (erase in page)
  274. *
  275. * @v ctx ANSI escape sequence context
  276. * @v count Parameter count
  277. * @v params[0] Region to erase
  278. */
  279. static void fbcon_handle_ed ( struct ansiesc_context *ctx,
  280. unsigned int count __unused,
  281. int params[] __unused ) {
  282. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  283. /* We assume that we always clear the whole screen */
  284. assert ( params[0] == ANSIESC_ED_ALL );
  285. /* Clear character array */
  286. fbcon_clear ( fbcon, 0 );
  287. /* Redraw all characters */
  288. fbcon_redraw ( fbcon );
  289. /* Reset cursor position */
  290. fbcon->xpos = 0;
  291. fbcon->ypos = 0;
  292. fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
  293. }
  294. /**
  295. * Handle ANSI SGR (set graphics rendition)
  296. *
  297. * @v ctx ANSI escape sequence context
  298. * @v count Parameter count
  299. * @v params List of graphic rendition aspects
  300. */
  301. static void fbcon_handle_sgr ( struct ansiesc_context *ctx, unsigned int count,
  302. int params[] ) {
  303. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  304. uint32_t *custom = NULL;
  305. uint32_t rgb;
  306. unsigned int end;
  307. unsigned int i;
  308. int aspect;
  309. for ( i = 0 ; i < count ; i++ ) {
  310. /* Process aspect */
  311. aspect = params[i];
  312. if ( aspect == 0 ) {
  313. fbcon_set_default_foreground ( fbcon );
  314. fbcon_set_default_background ( fbcon );
  315. } else if ( aspect == 1 ) {
  316. fbcon->bold = fbcon_colour ( fbcon, FBCON_BOLD );
  317. } else if ( aspect == 22 ) {
  318. fbcon->bold = 0;
  319. } else if ( ( aspect >= 30 ) && ( aspect <= 37 ) ) {
  320. fbcon->foreground =
  321. fbcon_ansi_colour ( fbcon, aspect - 30 );
  322. } else if ( aspect == 38 ) {
  323. custom = &fbcon->foreground;
  324. } else if ( aspect == 39 ) {
  325. fbcon_set_default_foreground ( fbcon );
  326. } else if ( ( aspect >= 40 ) && ( aspect <= 47 ) ) {
  327. fbcon->background =
  328. fbcon_ansi_colour ( fbcon, aspect - 40 );
  329. } else if ( aspect == 48 ) {
  330. custom = &fbcon->background;
  331. } else if ( aspect == 49 ) {
  332. fbcon_set_default_background ( fbcon );
  333. }
  334. /* Process custom RGB colour, if applicable
  335. *
  336. * We support the xterm-compatible
  337. * "<ESC>[38;2;<red>;<green>;<blue>m" and
  338. * "<ESC>[48;2;<red>;<green>;<blue>m" sequences.
  339. */
  340. if ( custom ) {
  341. rgb = 0;
  342. end = ( i + 5 );
  343. for ( ; ( i < count ) && ( i < end ) ; i++ )
  344. rgb = ( ( rgb << 8 ) | params[i] );
  345. *custom = fbcon_colour ( fbcon, rgb );
  346. custom = NULL;
  347. }
  348. }
  349. }
  350. /**
  351. * Handle ANSI DECTCEM set (show cursor)
  352. *
  353. * @v ctx ANSI escape sequence context
  354. * @v count Parameter count
  355. * @v params List of graphic rendition aspects
  356. */
  357. static void fbcon_handle_dectcem_set ( struct ansiesc_context *ctx,
  358. unsigned int count __unused,
  359. int params[] __unused ) {
  360. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  361. fbcon->show_cursor = 1;
  362. fbcon_draw_cursor ( fbcon, 1 );
  363. }
  364. /**
  365. * Handle ANSI DECTCEM reset (hide cursor)
  366. *
  367. * @v ctx ANSI escape sequence context
  368. * @v count Parameter count
  369. * @v params List of graphic rendition aspects
  370. */
  371. static void fbcon_handle_dectcem_reset ( struct ansiesc_context *ctx,
  372. unsigned int count __unused,
  373. int params[] __unused ) {
  374. struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
  375. fbcon->show_cursor = 0;
  376. fbcon_draw_cursor ( fbcon, 0 );
  377. }
  378. /** ANSI escape sequence handlers */
  379. static struct ansiesc_handler fbcon_ansiesc_handlers[] = {
  380. { ANSIESC_CUP, fbcon_handle_cup },
  381. { ANSIESC_ED, fbcon_handle_ed },
  382. { ANSIESC_SGR, fbcon_handle_sgr },
  383. { ANSIESC_DECTCEM_SET, fbcon_handle_dectcem_set },
  384. { ANSIESC_DECTCEM_RESET, fbcon_handle_dectcem_reset },
  385. { 0, NULL }
  386. };
  387. /**
  388. * Print a character to current cursor position
  389. *
  390. * @v fbcon Frame buffer console
  391. * @v character Character
  392. */
  393. void fbcon_putchar ( struct fbcon *fbcon, int character ) {
  394. struct fbcon_text_cell cell;
  395. /* Intercept ANSI escape sequences */
  396. character = ansiesc_process ( &fbcon->ctx, character );
  397. if ( character < 0 )
  398. return;
  399. /* Handle control characters */
  400. switch ( character ) {
  401. case '\r':
  402. fbcon_draw_cursor ( fbcon, 0 );
  403. fbcon->xpos = 0;
  404. break;
  405. case '\n':
  406. fbcon_draw_cursor ( fbcon, 0 );
  407. fbcon->xpos = 0;
  408. fbcon->ypos++;
  409. break;
  410. case '\b':
  411. fbcon_draw_cursor ( fbcon, 0 );
  412. if ( fbcon->xpos ) {
  413. fbcon->xpos--;
  414. } else if ( fbcon->ypos ) {
  415. fbcon->xpos = ( fbcon->character.width - 1 );
  416. fbcon->ypos--;
  417. }
  418. break;
  419. default:
  420. /* Print character at current cursor position */
  421. cell.foreground = ( fbcon->foreground | fbcon->bold );
  422. cell.background = fbcon->background;
  423. cell.character = character;
  424. fbcon_store ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
  425. fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
  426. /* Advance cursor */
  427. fbcon->xpos++;
  428. if ( fbcon->xpos >= fbcon->character.width ) {
  429. fbcon->xpos = 0;
  430. fbcon->ypos++;
  431. }
  432. break;
  433. }
  434. /* Scroll screen if necessary */
  435. if ( fbcon->ypos >= fbcon->character.height )
  436. fbcon_scroll ( fbcon );
  437. /* Show cursor */
  438. fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
  439. }
  440. /**
  441. * Initialise background picture
  442. *
  443. * @v fbcon Frame buffer console
  444. * @v pixbuf Background picture
  445. * @ret rc Return status code
  446. */
  447. static int fbcon_picture_init ( struct fbcon *fbcon,
  448. struct pixel_buffer *pixbuf ) {
  449. struct fbcon_geometry *pixel = fbcon->pixel;
  450. struct fbcon_picture *picture = &fbcon->picture;
  451. size_t len;
  452. size_t pixbuf_stride;
  453. size_t indent;
  454. size_t pixbuf_indent;
  455. size_t offset;
  456. size_t pixbuf_offset;
  457. uint32_t rgb;
  458. uint32_t raw;
  459. unsigned int x;
  460. unsigned int y;
  461. unsigned int width;
  462. unsigned int height;
  463. int xgap;
  464. int ygap;
  465. int rc;
  466. /* Allocate buffer */
  467. len = ( pixel->height * pixel->stride );
  468. picture->start = umalloc ( len );
  469. if ( ! picture->start ) {
  470. DBGC ( fbcon, "FBCON %p could not allocate %zd bytes for "
  471. "picture\n", fbcon, len );
  472. rc = -ENOMEM;
  473. goto err_umalloc;
  474. }
  475. /* Centre picture on console */
  476. pixbuf_stride = ( pixbuf->width * sizeof ( rgb ) );
  477. xgap = ( ( ( int ) ( pixel->width - pixbuf->width ) ) / 2 );
  478. ygap = ( ( ( int ) ( pixel->height - pixbuf->height ) ) / 2 );
  479. indent = ( ( ( ( ygap >= 0 ) ? ygap : 0 ) * pixel->stride ) +
  480. ( ( ( xgap >= 0 ) ? xgap : 0 ) * pixel->len ) );
  481. pixbuf_indent = ( ( ( ( ygap < 0 ) ? -ygap : 0 ) * pixbuf_stride ) +
  482. ( ( ( xgap < 0 ) ? -xgap : 0 ) * sizeof ( rgb ) ) );
  483. width = pixbuf->width;
  484. if ( width > pixel->width )
  485. width = pixel->width;
  486. height = pixbuf->height;
  487. if ( height > pixel->height )
  488. height = pixel->height;
  489. DBGC ( fbcon, "FBCON %p picture is pixel %dx%d at [%d,%d),[%d,%d)\n",
  490. fbcon, width, height, xgap, ( xgap + pixbuf->width ), ygap,
  491. ( ygap + pixbuf->height ) );
  492. /* Convert to frame buffer raw format */
  493. memset_user ( picture->start, 0, 0, len );
  494. for ( y = 0 ; y < height ; y++ ) {
  495. offset = ( indent + ( y * pixel->stride ) );
  496. pixbuf_offset = ( pixbuf_indent + ( y * pixbuf_stride ) );
  497. for ( x = 0 ; x < width ; x++ ) {
  498. copy_from_user ( &rgb, pixbuf->data, pixbuf_offset,
  499. sizeof ( rgb ) );
  500. raw = fbcon_colour ( fbcon, rgb );
  501. copy_to_user ( picture->start, offset, &raw,
  502. pixel->len );
  503. offset += pixel->len;
  504. pixbuf_offset += sizeof ( rgb );
  505. }
  506. }
  507. return 0;
  508. ufree ( picture->start );
  509. err_umalloc:
  510. return rc;
  511. }
  512. /**
  513. * Initialise frame buffer console
  514. *
  515. * @v fbcon Frame buffer console
  516. * @v start Start address
  517. * @v pixel Pixel geometry
  518. * @v margin Minimum margin
  519. * @v map Colour mapping
  520. * @v font Font definition
  521. * @v pixbuf Background picture (if any)
  522. * @ret rc Return status code
  523. */
  524. int fbcon_init ( struct fbcon *fbcon, userptr_t start,
  525. struct fbcon_geometry *pixel,
  526. struct fbcon_margin *margin,
  527. struct fbcon_colour_map *map,
  528. struct fbcon_font *font,
  529. struct pixel_buffer *pixbuf ) {
  530. int width;
  531. int height;
  532. unsigned int xgap;
  533. unsigned int ygap;
  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. /* Expand margin to accommodate whole characters */
  550. width = ( pixel->width - margin->left - margin->right );
  551. height = ( pixel->height - margin->top - margin->bottom );
  552. if ( ( width < FBCON_CHAR_WIDTH ) || ( height < FBCON_CHAR_HEIGHT ) ) {
  553. DBGC ( fbcon, "FBCON %p has unusable character area "
  554. "[%d-%d),[%d-%d)\n", fbcon,
  555. margin->left, ( pixel->width - margin->right ),
  556. margin->top, ( pixel->height - margin->bottom ) );
  557. rc = -EINVAL;
  558. goto err_margin;
  559. }
  560. xgap = ( width % FBCON_CHAR_WIDTH );
  561. ygap = ( height % FBCON_CHAR_HEIGHT );
  562. fbcon->margin.left = ( margin->left + ( xgap / 2 ) );
  563. fbcon->margin.top = ( margin->top + ( ygap / 2 ) );
  564. fbcon->margin.right = ( margin->right + ( xgap - ( xgap / 2 ) ) );
  565. fbcon->margin.bottom = ( margin->bottom + ( ygap - ( ygap / 2 ) ) );
  566. fbcon->indent = ( ( fbcon->margin.top * pixel->stride ) +
  567. ( fbcon->margin.left * pixel->len ) );
  568. /* Derive character geometry from pixel geometry */
  569. fbcon->character.width = ( width / FBCON_CHAR_WIDTH );
  570. fbcon->character.height = ( height / FBCON_CHAR_HEIGHT );
  571. fbcon->character.len = ( pixel->len * FBCON_CHAR_WIDTH );
  572. fbcon->character.stride = ( pixel->stride * FBCON_CHAR_HEIGHT );
  573. DBGC ( fbcon, "FBCON %p is pixel %dx%d, char %dx%d at "
  574. "[%d-%d),[%d-%d)\n", fbcon, fbcon->pixel->width,
  575. fbcon->pixel->height, fbcon->character.width,
  576. fbcon->character.height, fbcon->margin.left,
  577. ( fbcon->pixel->width - fbcon->margin.right ),
  578. fbcon->margin.top,
  579. ( fbcon->pixel->height - fbcon->margin.bottom ) );
  580. /* Set default colours */
  581. fbcon_set_default_foreground ( fbcon );
  582. fbcon_set_default_background ( fbcon );
  583. /* Allocate and initialise stored character array */
  584. fbcon->text.start = umalloc ( fbcon->character.width *
  585. fbcon->character.height *
  586. sizeof ( struct fbcon_text_cell ) );
  587. if ( ! fbcon->text.start ) {
  588. rc = -ENOMEM;
  589. goto err_text;
  590. }
  591. fbcon_clear ( fbcon, 0 );
  592. /* Set framebuffer to all black (including margins) */
  593. memset_user ( fbcon->start, 0, 0, fbcon->len );
  594. /* Generate pixel buffer from background image, if applicable */
  595. if ( pixbuf && ( ( rc = fbcon_picture_init ( fbcon, pixbuf ) ) != 0 ) )
  596. goto err_picture;
  597. /* Draw background picture (including margins), if applicable */
  598. if ( fbcon->picture.start ) {
  599. memcpy_user ( fbcon->start, 0, fbcon->picture.start, 0,
  600. fbcon->len );
  601. }
  602. /* Update console width and height */
  603. console_set_size ( fbcon->character.width, fbcon->character.height );
  604. return 0;
  605. ufree ( fbcon->picture.start );
  606. err_picture:
  607. ufree ( fbcon->text.start );
  608. err_text:
  609. err_margin:
  610. return rc;
  611. }
  612. /**
  613. * Finalise frame buffer console
  614. *
  615. * @v fbcon Frame buffer console
  616. */
  617. void fbcon_fini ( struct fbcon *fbcon ) {
  618. ufree ( fbcon->text.start );
  619. ufree ( fbcon->picture.start );
  620. }