bootcon.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. * The console uses the same 8x16 Terminus face and cell grid as the ring-3
  9. * terminal, and starts at the top-left like the BIOS text console, so the
  10. * loader -> kernel -> WM handoff does not change the text size or jump around
  11. * the screen. A full screen scrolls instead of wrapping back to the top.
  12. */
  13. #include "bootcon.h"
  14. #include "bootinfo.h"
  15. #include "font_8x16.h"
  16. #include "mm.h"
  17. #include "serial.h"
  18. #include "vga.h"
  19. #define CELL_W 8
  20. #define CELL_H 16
  21. #define MARGIN 8
  22. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  23. static unsigned int fb_base;
  24. static volatile unsigned char *fb_lfb;
  25. static int fb_swap;
  26. static int fb_ok;
  27. static int enabled;
  28. static unsigned int con_x, con_y; /* console pixel origin */
  29. static unsigned int con_cols, con_rows;
  30. static unsigned int cur_col, cur_row;
  31. /* Text log kept independently of the framebuffer: bootcon is disabled when
  32. * the WM attaches, but the e1000e probe and later driver messages still go
  33. * through bootcon_puts(). The buffer is allocated from the frame allocator
  34. * (see bootcon_log_init) so it does not move the kernel's _end past the low
  35. * rootfs pack at 0x60000. */
  36. static char *log_buf;
  37. static unsigned int log_cap;
  38. static unsigned int log_len;
  39. static int log_truncated;
  40. static void fb_px(unsigned int x, unsigned int y, unsigned int r,
  41. unsigned int g, unsigned int b)
  42. {
  43. volatile unsigned char *p;
  44. if (!fb_ok || x >= fb_w || y >= fb_h)
  45. return;
  46. p = fb_lfb + (unsigned long)y * fb_pitch +
  47. (unsigned long)x * (fb_bpp / 8);
  48. if (fb_bpp == 16) {
  49. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  50. p[0] = (unsigned char)(v & 0xFF);
  51. p[1] = (unsigned char)(v >> 8);
  52. } else {
  53. p[0] = (unsigned char)(fb_swap ? r : b);
  54. p[1] = (unsigned char)g;
  55. p[2] = (unsigned char)(fb_swap ? b : r);
  56. if (fb_bpp == 32)
  57. p[3] = 0;
  58. }
  59. }
  60. /* Clear the band the current row will be drawn into, so a reused line
  61. * cannot leave parts of an older message behind. */
  62. static void clear_line(void)
  63. {
  64. unsigned int x, y;
  65. unsigned int y0 = con_y + cur_row * CELL_H;
  66. for (y = 0; y < CELL_H; y++)
  67. for (x = 0; x < con_cols * CELL_W; x++)
  68. fb_px(con_x + x, y0 + y, 0, 0, 0);
  69. }
  70. static void draw_char(unsigned int x, unsigned int y, unsigned char c)
  71. {
  72. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  73. unsigned int row, col;
  74. for (row = 0; row < FONT_8X16_ROWS; row++) {
  75. unsigned char bits = g[row];
  76. for (col = 0; col < CELL_W; col++)
  77. if (bits & (0x80u >> col))
  78. fb_px(x + col, y + row, 255, 255, 255);
  79. }
  80. }
  81. /* Move the console up one text row: every row takes the pixels of the row
  82. * below it and the last row is cleared. Only used when the cursor is on the
  83. * last row, so a normal boot never pays for it.
  84. *
  85. * Every supported bpp puts a whole number of dwords on a scanline for CELL_W
  86. * pixels, and the console origin is a multiple of 4 pixels, so no partial
  87. * dword is left behind. Plain word loads/stores are used rather than
  88. * `rep movsl`: the source is the LFB, not RAM. */
  89. static void scroll_up(void)
  90. {
  91. unsigned int pb = fb_bpp / 8;
  92. unsigned int dwords = (con_cols * CELL_W * pb) / 4;
  93. unsigned char *base = (unsigned char *)fb_lfb +
  94. (unsigned long)con_y * fb_pitch + con_x * pb;
  95. unsigned int line, i;
  96. unsigned int lines = con_rows * CELL_H;
  97. /* Move every scanline below the first text row up by one cell. */
  98. for (line = 0; line < lines - CELL_H; line++) {
  99. unsigned int *dst = (unsigned int *)(base +
  100. (unsigned long)line * fb_pitch);
  101. const unsigned int *src = (const unsigned int *)(base +
  102. (unsigned long)(line + CELL_H) * fb_pitch);
  103. for (i = 0; i < dwords; i++)
  104. dst[i] = src[i];
  105. }
  106. /* Clear the bottom cell. */
  107. for (line = lines - CELL_H; line < lines; line++) {
  108. unsigned int *dst = (unsigned int *)(base +
  109. (unsigned long)line * fb_pitch);
  110. for (i = 0; i < dwords; i++)
  111. dst[i] = 0;
  112. }
  113. }
  114. void bootcon_init(void)
  115. {
  116. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  117. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  118. __asm__ volatile ("" : "+r"(fb));
  119. fb_ok = 0;
  120. enabled = 0;
  121. if (!fb->valid || !fb->lfb_base)
  122. return;
  123. fb_w = fb->width;
  124. fb_h = fb->height;
  125. fb_pitch = fb->pitch;
  126. fb_bpp = fb->bpp;
  127. fb_base = fb->lfb_base;
  128. if (fb_bpp != 16 && fb_bpp != 24 && fb_bpp != 32)
  129. return;
  130. /* Some BIOSes report a pitch in pixels or too small; enforce a sane
  131. * minimum exactly like vga_cache_boot_fb() does. */
  132. if (fb_pitch < fb_w * (fb_bpp / 8))
  133. fb_pitch = fb_w * (fb_bpp / 8);
  134. fb_lfb = (volatile unsigned char *)(unsigned long)fb->lfb_base;
  135. fb_swap = bootinfo_rgbx();
  136. if (!fb_w || !fb_h || !fb_pitch || !fb_lfb)
  137. return;
  138. con_x = MARGIN;
  139. con_y = MARGIN;
  140. if (fb_w <= con_x + CELL_W || fb_h <= con_y + CELL_H)
  141. return;
  142. con_cols = (fb_w - 2 * MARGIN) / CELL_W;
  143. con_rows = (fb_h - 2 * MARGIN) / CELL_H;
  144. if (!con_cols || !con_rows)
  145. return;
  146. fb_ok = 1;
  147. enabled = 1;
  148. cur_col = 0;
  149. cur_row = 0;
  150. clear_line();
  151. }
  152. int bootcon_ok(void)
  153. {
  154. return fb_ok;
  155. }
  156. void bootcon_set_enabled(int on)
  157. {
  158. enabled = on && fb_ok;
  159. if (enabled)
  160. clear_line();
  161. }
  162. static void newline(void)
  163. {
  164. cur_col = 0;
  165. if (cur_row + 1 >= con_rows) {
  166. scroll_up();
  167. cur_row = con_rows - 1;
  168. } else {
  169. cur_row++;
  170. }
  171. clear_line();
  172. }
  173. void bootcon_log_init(void)
  174. {
  175. unsigned int mem;
  176. if (log_buf)
  177. return;
  178. mem = mm_alloc_pages(BOOTCON_LOG_PAGES);
  179. if (!mem)
  180. return;
  181. log_buf = (char *)mem;
  182. log_cap = BOOTCON_LOG_PAGES * PAGE_SIZE;
  183. log_len = 0;
  184. log_truncated = 0;
  185. log_buf[0] = '\0';
  186. }
  187. void bootcon_puts(const char *s)
  188. {
  189. while (*s) {
  190. unsigned char c = (unsigned char)*s++;
  191. /* Keep the text even after bootcon_set_enabled(0) has hidden
  192. * the framebuffer console. */
  193. if (log_buf) {
  194. if (log_len + 1 < log_cap) {
  195. log_buf[log_len++] = (char)c;
  196. log_buf[log_len] = '\0';
  197. } else {
  198. log_truncated = 1;
  199. }
  200. }
  201. if (!fb_ok || !enabled)
  202. continue;
  203. if (c == '\r') {
  204. cur_col = 0;
  205. continue;
  206. }
  207. if (c == '\n') {
  208. newline();
  209. continue;
  210. }
  211. if (cur_col >= con_cols)
  212. newline();
  213. draw_char(con_x + cur_col * CELL_W,
  214. con_y + cur_row * CELL_H, c);
  215. cur_col++;
  216. }
  217. }
  218. const char *bootcon_log(unsigned int *len)
  219. {
  220. *len = log_len;
  221. return log_buf ? log_buf : "";
  222. }
  223. int bootcon_log_truncated(void)
  224. {
  225. return log_truncated;
  226. }
  227. void bootcon_dec(unsigned int v)
  228. {
  229. char buf[12];
  230. unsigned int i = 0;
  231. if (v == 0) {
  232. bootcon_puts("0");
  233. return;
  234. }
  235. while (v) {
  236. buf[i++] = (char)('0' + v % 10);
  237. v /= 10;
  238. }
  239. while (i) {
  240. char one[2];
  241. one[0] = buf[--i];
  242. one[1] = '\0';
  243. bootcon_puts(one);
  244. }
  245. }
  246. void bootcon_hex(unsigned int v)
  247. {
  248. static const char hx[] = "0123456789ABCDEF";
  249. char buf[9];
  250. unsigned int i;
  251. for (i = 0; i < 8; i++)
  252. buf[i] = hx[(v >> (4 * (7 - i))) & 0xF];
  253. buf[8] = '\0';
  254. bootcon_puts(buf);
  255. }
  256. void bootcon_fb_info(void)
  257. {
  258. bootcon_puts("fb ");
  259. bootcon_dec(fb_w);
  260. bootcon_puts("x");
  261. bootcon_dec(fb_h);
  262. bootcon_puts("x");
  263. bootcon_dec(fb_bpp);
  264. bootcon_puts(" pitch ");
  265. bootcon_dec(fb_pitch);
  266. bootcon_puts(" lfb 0x");
  267. bootcon_hex(fb_base);
  268. bootcon_puts("\n");
  269. /* Mirror to COM1 (and the netlog console) so a machine without a
  270. * serial port can report the VBE mode before the WM repaints. */
  271. serial_puts(SERIAL_COM1, "fb ");
  272. serial_putdec(SERIAL_COM1, fb_w);
  273. serial_puts(SERIAL_COM1, "x");
  274. serial_putdec(SERIAL_COM1, fb_h);
  275. serial_puts(SERIAL_COM1, "x");
  276. serial_putdec(SERIAL_COM1, fb_bpp);
  277. serial_puts(SERIAL_COM1, " pitch ");
  278. serial_putdec(SERIAL_COM1, fb_pitch);
  279. serial_puts(SERIAL_COM1, " lfb ");
  280. serial_puthex(SERIAL_COM1, fb_base);
  281. serial_puts(SERIAL_COM1, "\n");
  282. }