bootcon.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Direct-to-LFB boot console (see bootcon.h).
  2. *
  3. * This deliberately does not use vga_fb_width()/vga_fb_*(): those call
  4. * vga_cache_boot_fb(), which probes the GPU and maps BARs, and this code runs
  5. * before mm_init/paging_init exist. It reads the boot framebuffer info block
  6. * at 0x600 directly, exactly like paging.c does.
  7. */
  8. #include "bootcon.h"
  9. #include "bootinfo.h"
  10. #include "font_8x8.h"
  11. #include "vga.h"
  12. #define SCALE 2
  13. #define LINE_H (8 * SCALE + 2)
  14. #define LEFT 8
  15. #define TOP 8
  16. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  17. static unsigned int fb_base;
  18. static volatile unsigned char *fb_lfb;
  19. static int fb_swap;
  20. static int fb_ok;
  21. static int enabled;
  22. static unsigned int cur_x, cur_y;
  23. static unsigned int home_x, home_y;
  24. static void fb_px(unsigned int x, unsigned int y, unsigned int r,
  25. unsigned int g, unsigned int b)
  26. {
  27. volatile unsigned char *p;
  28. if (!fb_ok || x >= fb_w || y >= fb_h)
  29. return;
  30. p = fb_lfb + (unsigned long)y * fb_pitch +
  31. (unsigned long)x * (fb_bpp / 8);
  32. if (fb_bpp == 16) {
  33. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  34. p[0] = (unsigned char)(v & 0xFF);
  35. p[1] = (unsigned char)(v >> 8);
  36. } else {
  37. p[0] = (unsigned char)(fb_swap ? r : b);
  38. p[1] = (unsigned char)g;
  39. p[2] = (unsigned char)(fb_swap ? b : r);
  40. if (fb_bpp == 32)
  41. p[3] = 0;
  42. }
  43. }
  44. /* Clear the band the next line will be drawn into, so a reused line (after
  45. * wrapping at the bottom) cannot leave parts of an older message behind. */
  46. static void clear_line(void)
  47. {
  48. unsigned int x, y;
  49. for (y = 0; y < LINE_H; y++)
  50. for (x = 0; x < fb_w; x++)
  51. fb_px(x, cur_y + y, 0, 0, 0);
  52. }
  53. static void draw_char(unsigned int x, unsigned int y, unsigned char c)
  54. {
  55. const unsigned char *g = &font_8x8[(unsigned int)c * 8];
  56. unsigned int row, col;
  57. for (row = 0; row < 8; row++) {
  58. unsigned char bits = g[row];
  59. for (col = 0; col < 8; col++) {
  60. if (bits & (0x80u >> col)) {
  61. unsigned int i, j;
  62. for (i = 0; i < SCALE; i++)
  63. for (j = 0; j < SCALE; j++)
  64. fb_px(x + col * SCALE + i,
  65. y + row * SCALE + j,
  66. 255, 255, 255);
  67. }
  68. }
  69. }
  70. }
  71. void bootcon_init(void)
  72. {
  73. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  74. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  75. __asm__ volatile ("" : "+r"(fb));
  76. fb_ok = 0;
  77. enabled = 0;
  78. cur_x = LEFT;
  79. cur_y = TOP;
  80. if (!fb->valid || !fb->lfb_base)
  81. return;
  82. fb_w = fb->width;
  83. fb_h = fb->height;
  84. fb_pitch = fb->pitch;
  85. fb_bpp = fb->bpp;
  86. fb_base = fb->lfb_base;
  87. if (fb_bpp != 16 && fb_bpp != 24 && fb_bpp != 32)
  88. return;
  89. /* Some BIOSes report a pitch in pixels or too small; enforce a sane
  90. * minimum exactly like vga_cache_boot_fb() does. */
  91. if (fb_pitch < fb_w * (fb_bpp / 8))
  92. fb_pitch = fb_w * (fb_bpp / 8);
  93. fb_lfb = (volatile unsigned char *)(unsigned long)fb->lfb_base;
  94. fb_swap = bootinfo_rgbx();
  95. if (!fb_w || !fb_h || !fb_pitch || !fb_lfb)
  96. return;
  97. fb_ok = 1;
  98. enabled = 1;
  99. /* Start away from the very top-left: some VBIOSes centre a mode that
  100. * does not match the panel (e.g. 1280x1024 on a 1280x800 LVDS), which
  101. * crops the outer rows/columns. Starting at 1/8,1/4 keeps the log
  102. * inside the visible area in both the centred and the scaled case. */
  103. home_x = fb_w / 8;
  104. home_y = fb_h / 4;
  105. if (home_x < 8)
  106. home_x = 8;
  107. if (home_y < 8)
  108. home_y = 8;
  109. cur_x = home_x;
  110. cur_y = home_y;
  111. clear_line();
  112. }
  113. int bootcon_ok(void)
  114. {
  115. return fb_ok;
  116. }
  117. void bootcon_set_enabled(int on)
  118. {
  119. enabled = on && fb_ok;
  120. if (enabled)
  121. clear_line();
  122. }
  123. static void newline(void)
  124. {
  125. cur_x = home_x;
  126. cur_y += LINE_H;
  127. if (cur_y + 8 * SCALE > fb_h)
  128. cur_y = home_y;
  129. clear_line();
  130. }
  131. void bootcon_puts(const char *s)
  132. {
  133. if (!fb_ok || !enabled)
  134. return;
  135. while (*s) {
  136. unsigned char c = (unsigned char)*s++;
  137. if (c == '\r') {
  138. cur_x = LEFT;
  139. continue;
  140. }
  141. if (c == '\n') {
  142. newline();
  143. continue;
  144. }
  145. if (cur_x + 8 * SCALE > fb_w)
  146. newline();
  147. draw_char(cur_x, cur_y, c);
  148. cur_x += 8 * SCALE;
  149. }
  150. }
  151. void bootcon_dec(unsigned int v)
  152. {
  153. char buf[12];
  154. unsigned int i = 0;
  155. if (v == 0) {
  156. bootcon_puts("0");
  157. return;
  158. }
  159. while (v) {
  160. buf[i++] = (char)('0' + v % 10);
  161. v /= 10;
  162. }
  163. while (i) {
  164. char one[2];
  165. one[0] = buf[--i];
  166. one[1] = '\0';
  167. bootcon_puts(one);
  168. }
  169. }
  170. void bootcon_hex(unsigned int v)
  171. {
  172. static const char hx[] = "0123456789ABCDEF";
  173. char buf[9];
  174. unsigned int i;
  175. for (i = 0; i < 8; i++)
  176. buf[i] = hx[(v >> (4 * (7 - i))) & 0xF];
  177. buf[8] = '\0';
  178. bootcon_puts(buf);
  179. }
  180. void bootcon_fb_info(void)
  181. {
  182. bootcon_puts("fb ");
  183. bootcon_dec(fb_w);
  184. bootcon_puts("x");
  185. bootcon_dec(fb_h);
  186. bootcon_puts("x");
  187. bootcon_dec(fb_bpp);
  188. bootcon_puts(" pitch ");
  189. bootcon_dec(fb_pitch);
  190. bootcon_puts(" lfb 0x");
  191. bootcon_hex(fb_base);
  192. bootcon_puts("\n");
  193. }