bootcon.c 9.4 KB

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