bootcon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 "syscall.h"
  20. #include "vga.h"
  21. #define CELL_W 8
  22. #define CELL_H 16
  23. #define MARGIN 8
  24. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  25. static unsigned int fb_base;
  26. static volatile unsigned char *fb_lfb;
  27. static int fb_swap;
  28. static int fb_ok;
  29. static int enabled;
  30. static unsigned int con_x, con_y; /* console pixel origin */
  31. static unsigned int con_cols, con_rows;
  32. static unsigned int cur_col, cur_row;
  33. /* Text log kept independently of the framebuffer: bootcon is disabled when
  34. * the WM attaches, but the e1000e probe and later driver messages still go
  35. * through bootcon_puts(). The buffer is allocated from the frame allocator
  36. * (see bootcon_log_init) so it does not move the kernel's _end past the low
  37. * rootfs pack at 0x60000. */
  38. static char *log_buf;
  39. static unsigned int log_cap;
  40. static unsigned int log_len;
  41. static int log_truncated;
  42. /* Always-on COM1 log (the `seriallog` source and the bootscan Serial tab).
  43. * `serlog_total` counts every captured byte; the buffer holds the newest
  44. * `serlog_len` of them, so absolute offsets survive the drops. */
  45. static char *serlog_buf;
  46. static unsigned int serlog_cap;
  47. static unsigned int serlog_len;
  48. static unsigned int serlog_total;
  49. static int serlog_truncated;
  50. /* serial_putc() runs before the page allocator exists; keep those first
  51. * characters in BSS and replay them into the ring at log_init. The window
  52. * before mm_init is just the version line, the start/gdt traces and the
  53. * allocator's bitmap line, so 256 bytes is enough to seed a complete log. */
  54. static char serlog_early[256];
  55. static unsigned int serlog_early_len;
  56. /* Serial output of the bootscan kernel thread (bootcon_scan_begin). */
  57. static char *scan_buf;
  58. static unsigned int scan_cap;
  59. static unsigned int scan_total;
  60. static unsigned int scan_start;
  61. static unsigned int scan_pid;
  62. static int scan_active;
  63. static void fb_px(unsigned int x, unsigned int y, unsigned int r,
  64. unsigned int g, unsigned int b)
  65. {
  66. volatile unsigned char *p;
  67. if (!fb_ok || x >= fb_w || y >= fb_h)
  68. return;
  69. p = fb_lfb + (unsigned long)y * fb_pitch +
  70. (unsigned long)x * (fb_bpp / 8);
  71. if (fb_bpp == 16) {
  72. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  73. p[0] = (unsigned char)(v & 0xFF);
  74. p[1] = (unsigned char)(v >> 8);
  75. } else {
  76. p[0] = (unsigned char)(fb_swap ? r : b);
  77. p[1] = (unsigned char)g;
  78. p[2] = (unsigned char)(fb_swap ? b : r);
  79. if (fb_bpp == 32)
  80. p[3] = 0;
  81. }
  82. }
  83. /* Clear the band the current row will be drawn into, so a reused line
  84. * cannot leave parts of an older message behind. */
  85. static void clear_line(void)
  86. {
  87. unsigned int x, y;
  88. unsigned int y0 = con_y + cur_row * CELL_H;
  89. for (y = 0; y < CELL_H; y++)
  90. for (x = 0; x < con_cols * CELL_W; x++)
  91. fb_px(con_x + x, y0 + y, 0, 0, 0);
  92. }
  93. static void draw_char(unsigned int x, unsigned int y, unsigned char c)
  94. {
  95. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  96. unsigned int row, col;
  97. for (row = 0; row < FONT_8X16_ROWS; row++) {
  98. unsigned char bits = g[row];
  99. for (col = 0; col < CELL_W; col++)
  100. if (bits & (0x80u >> col))
  101. fb_px(x + col, y + row, 255, 255, 255);
  102. }
  103. }
  104. /* Move the console up one text row: every row takes the pixels of the row
  105. * below it and the last row is cleared. Only used when the cursor is on the
  106. * last row, so a normal boot never pays for it.
  107. *
  108. * Every supported bpp puts a whole number of dwords on a scanline for CELL_W
  109. * pixels, and the console origin is a multiple of 4 pixels, so no partial
  110. * dword is left behind. Plain word loads/stores are used rather than
  111. * `rep movsl`: the source is the LFB, not RAM. */
  112. static void scroll_up(void)
  113. {
  114. unsigned int pb = fb_bpp / 8;
  115. unsigned int dwords = (con_cols * CELL_W * pb) / 4;
  116. unsigned char *base = (unsigned char *)fb_lfb +
  117. (unsigned long)con_y * fb_pitch + con_x * pb;
  118. unsigned int line, i;
  119. unsigned int lines = con_rows * CELL_H;
  120. /* Move every scanline below the first text row up by one cell. */
  121. for (line = 0; line < lines - CELL_H; line++) {
  122. unsigned int *dst = (unsigned int *)(base +
  123. (unsigned long)line * fb_pitch);
  124. const unsigned int *src = (const unsigned int *)(base +
  125. (unsigned long)(line + CELL_H) * fb_pitch);
  126. for (i = 0; i < dwords; i++)
  127. dst[i] = src[i];
  128. }
  129. /* Clear the bottom cell. */
  130. for (line = lines - CELL_H; line < lines; line++) {
  131. unsigned int *dst = (unsigned int *)(base +
  132. (unsigned long)line * fb_pitch);
  133. for (i = 0; i < dwords; i++)
  134. dst[i] = 0;
  135. }
  136. }
  137. void bootcon_init(void)
  138. {
  139. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  140. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  141. __asm__ volatile ("" : "+r"(fb));
  142. fb_ok = 0;
  143. enabled = 0;
  144. if (!fb->valid || !fb->lfb_base)
  145. return;
  146. fb_w = fb->width;
  147. fb_h = fb->height;
  148. fb_pitch = fb->pitch;
  149. fb_bpp = fb->bpp;
  150. fb_base = fb->lfb_base;
  151. if (fb_bpp != 16 && fb_bpp != 24 && fb_bpp != 32)
  152. return;
  153. /* Some BIOSes report a pitch in pixels or too small; enforce a sane
  154. * minimum exactly like vga_cache_boot_fb() does. */
  155. if (fb_pitch < fb_w * (fb_bpp / 8))
  156. fb_pitch = fb_w * (fb_bpp / 8);
  157. fb_lfb = (volatile unsigned char *)(unsigned long)fb->lfb_base;
  158. fb_swap = bootinfo_rgbx();
  159. if (!fb_w || !fb_h || !fb_pitch || !fb_lfb)
  160. return;
  161. con_x = MARGIN;
  162. con_y = MARGIN;
  163. if (fb_w <= con_x + CELL_W || fb_h <= con_y + CELL_H)
  164. return;
  165. con_cols = (fb_w - 2 * MARGIN) / CELL_W;
  166. con_rows = (fb_h - 2 * MARGIN) / CELL_H;
  167. if (!con_cols || !con_rows)
  168. return;
  169. fb_ok = 1;
  170. enabled = 1;
  171. cur_col = 0;
  172. cur_row = 0;
  173. clear_line();
  174. }
  175. int bootcon_ok(void)
  176. {
  177. return fb_ok;
  178. }
  179. void bootcon_set_enabled(int on)
  180. {
  181. enabled = on && fb_ok;
  182. if (enabled)
  183. clear_line();
  184. }
  185. static void newline(void)
  186. {
  187. cur_col = 0;
  188. if (cur_row + 1 >= con_rows) {
  189. scroll_up();
  190. cur_row = con_rows - 1;
  191. } else {
  192. cur_row++;
  193. }
  194. clear_line();
  195. }
  196. void bootcon_log_init(void)
  197. {
  198. unsigned int mem, i;
  199. if (!serlog_buf) {
  200. mem = mm_alloc_pages(BOOTCON_SERLOG_PAGES);
  201. if (mem) {
  202. serlog_buf = (char *)mem;
  203. serlog_cap = BOOTCON_SERLOG_PAGES * PAGE_SIZE;
  204. serlog_len = 0;
  205. serlog_total = 0;
  206. serlog_truncated = 0;
  207. /* Replay the characters serial_putc() captured
  208. * before the page allocator was up. */
  209. for (i = 0; i < serlog_early_len; i++)
  210. bootcon_serlog_char(serlog_early[i]);
  211. serlog_early_len = 0;
  212. }
  213. }
  214. if (log_buf)
  215. return;
  216. mem = mm_alloc_pages(BOOTCON_LOG_PAGES);
  217. if (!mem)
  218. return;
  219. log_buf = (char *)mem;
  220. log_cap = BOOTCON_LOG_PAGES * PAGE_SIZE;
  221. log_len = 0;
  222. log_truncated = 0;
  223. log_buf[0] = '\0';
  224. }
  225. void bootcon_puts(const char *s)
  226. {
  227. while (*s) {
  228. unsigned char c = (unsigned char)*s++;
  229. /* Keep the text even after bootcon_set_enabled(0) has hidden
  230. * the framebuffer console. */
  231. if (log_buf) {
  232. if (log_len + 1 < log_cap) {
  233. log_buf[log_len++] = (char)c;
  234. log_buf[log_len] = '\0';
  235. } else {
  236. log_truncated = 1;
  237. }
  238. }
  239. if (!fb_ok || !enabled)
  240. continue;
  241. if (c == '\r') {
  242. cur_col = 0;
  243. continue;
  244. }
  245. if (c == '\n') {
  246. newline();
  247. continue;
  248. }
  249. if (cur_col >= con_cols)
  250. newline();
  251. draw_char(con_x + cur_col * CELL_W,
  252. con_y + cur_row * CELL_H, c);
  253. cur_col++;
  254. }
  255. }
  256. const char *bootcon_log(unsigned int *len)
  257. {
  258. *len = log_len;
  259. return log_buf ? log_buf : "";
  260. }
  261. int bootcon_log_truncated(void)
  262. {
  263. return log_truncated;
  264. }
  265. /* Every COM1 character, independent of the bootscan thread and of whether
  266. * the UART itself answered. `seriallog save` writes this to a file on the
  267. * USB ESP so a serial-less board can be debugged from Linux. CRs are
  268. * dropped (serial_puts emits them before every LF), so the buffer is a plain
  269. * LF-only text stream. When full, the older half is dropped to make room,
  270. * which keeps the buffer contiguous and the capture live. */
  271. void bootcon_serlog_char(char c)
  272. {
  273. if (c == '\r')
  274. return;
  275. if (!serlog_buf || !serlog_cap) {
  276. if (serlog_early_len < sizeof(serlog_early))
  277. serlog_early[serlog_early_len++] = c;
  278. return;
  279. }
  280. if (serlog_len >= serlog_cap) {
  281. unsigned int drop = serlog_cap / 2;
  282. unsigned int i;
  283. /* Keep whole lines where possible. */
  284. while (drop < serlog_len && serlog_buf[drop] != '\n')
  285. drop++;
  286. if (drop < serlog_len)
  287. drop++;
  288. for (i = 0; i + drop < serlog_len; i++)
  289. serlog_buf[i] = serlog_buf[i + drop];
  290. serlog_len -= drop;
  291. serlog_truncated = 1;
  292. }
  293. serlog_buf[serlog_len++] = c;
  294. serlog_total++;
  295. }
  296. const char *bootcon_serlog(unsigned int *len)
  297. {
  298. *len = serlog_len;
  299. return serlog_buf ? serlog_buf : "";
  300. }
  301. int bootcon_serlog_truncated(void)
  302. {
  303. return serlog_truncated;
  304. }
  305. /* Unified source view for SYS_BOOTLOG (the bootscan window). See bootcon.h. */
  306. int bootcon_log_stat(int src, unsigned int *total, unsigned int *start,
  307. unsigned int *cap, int *active)
  308. {
  309. switch (src) {
  310. case SYS_BOOTLOG_BOOT:
  311. *total = log_len;
  312. *start = 0;
  313. *cap = log_cap;
  314. *active = log_buf && log_len < log_cap;
  315. return 0;
  316. case SYS_BOOTLOG_SERIAL:
  317. *total = serlog_total;
  318. *start = serlog_total - serlog_len;
  319. *cap = serlog_cap;
  320. *active = serlog_buf != 0;
  321. return 0;
  322. case SYS_BOOTLOG_SCAN:
  323. *total = scan_total;
  324. *start = scan_start;
  325. *cap = scan_cap;
  326. *active = scan_active;
  327. return 0;
  328. default:
  329. return -1;
  330. }
  331. }
  332. unsigned int bootcon_log_read(int src, unsigned int off, char *dst,
  333. unsigned int max)
  334. {
  335. unsigned int i, n;
  336. switch (src) {
  337. case SYS_BOOTLOG_BOOT:
  338. if (!log_buf || off >= log_len)
  339. return 0;
  340. n = log_len - off;
  341. if (n > max)
  342. n = max;
  343. for (i = 0; i < n; i++)
  344. dst[i] = log_buf[off + i];
  345. return n;
  346. case SYS_BOOTLOG_SERIAL: {
  347. unsigned int base = serlog_total - serlog_len;
  348. if (!serlog_buf || off < base || off >= serlog_total)
  349. return 0;
  350. n = serlog_total - off;
  351. if (n > max)
  352. n = max;
  353. for (i = 0; i < n; i++)
  354. dst[i] = serlog_buf[off + i - base];
  355. return n;
  356. }
  357. case SYS_BOOTLOG_SCAN:
  358. return bootcon_scan_read(off, dst, max);
  359. default:
  360. return 0;
  361. }
  362. }
  363. /* ---- bootscan thread serial capture ---------------------------------- */
  364. void bootcon_scan_begin(void)
  365. {
  366. unsigned int mem;
  367. if (!scan_buf) {
  368. mem = mm_alloc_pages(BOOTCON_SCAN_PAGES);
  369. if (!mem) {
  370. scan_cap = 0;
  371. scan_active = 0;
  372. return;
  373. }
  374. scan_buf = (char *)mem;
  375. scan_cap = BOOTCON_SCAN_PAGES * PAGE_SIZE;
  376. }
  377. scan_pid = current_proc() ? current_proc()->pid : 0;
  378. scan_total = 0;
  379. scan_start = 0;
  380. scan_active = 1;
  381. }
  382. void bootcon_scan_end(void)
  383. {
  384. scan_active = 0;
  385. }
  386. void bootcon_scan_char(char c)
  387. {
  388. if (!scan_active || !scan_cap)
  389. return;
  390. if (!current_proc() || current_proc()->pid != scan_pid)
  391. return;
  392. if (c == '\r')
  393. return; /* serial_puts emits CR before every LF */
  394. if (scan_total - scan_start >= scan_cap)
  395. scan_start = scan_total - scan_cap + 1;
  396. scan_buf[scan_total % scan_cap] = c;
  397. scan_total++;
  398. }
  399. unsigned int bootcon_scan_read(unsigned int off, char *dst, unsigned int max)
  400. {
  401. unsigned int n = 0;
  402. if (!scan_buf || !scan_cap)
  403. return 0;
  404. if (off < scan_start)
  405. off = scan_start;
  406. while (off < scan_total && n < max)
  407. dst[n++] = scan_buf[off++ % scan_cap];
  408. return n;
  409. }
  410. void bootcon_dec(unsigned int v)
  411. {
  412. char buf[12];
  413. unsigned int i = 0;
  414. if (v == 0) {
  415. bootcon_puts("0");
  416. return;
  417. }
  418. while (v) {
  419. buf[i++] = (char)('0' + v % 10);
  420. v /= 10;
  421. }
  422. while (i) {
  423. char one[2];
  424. one[0] = buf[--i];
  425. one[1] = '\0';
  426. bootcon_puts(one);
  427. }
  428. }
  429. void bootcon_hex(unsigned int v)
  430. {
  431. static const char hx[] = "0123456789ABCDEF";
  432. char buf[9];
  433. unsigned int i;
  434. for (i = 0; i < 8; i++)
  435. buf[i] = hx[(v >> (4 * (7 - i))) & 0xF];
  436. buf[8] = '\0';
  437. bootcon_puts(buf);
  438. }
  439. void bootcon_fb_info(void)
  440. {
  441. bootcon_puts("fb ");
  442. bootcon_dec(fb_w);
  443. bootcon_puts("x");
  444. bootcon_dec(fb_h);
  445. bootcon_puts("x");
  446. bootcon_dec(fb_bpp);
  447. bootcon_puts(" pitch ");
  448. bootcon_dec(fb_pitch);
  449. bootcon_puts(" lfb 0x");
  450. bootcon_hex(fb_base);
  451. bootcon_puts("\n");
  452. /* Mirror to COM1 (and the netlog console) so a machine without a
  453. * serial port can report the VBE mode before the WM repaints. */
  454. serial_puts(SERIAL_COM1, "fb ");
  455. serial_putdec(SERIAL_COM1, fb_w);
  456. serial_puts(SERIAL_COM1, "x");
  457. serial_putdec(SERIAL_COM1, fb_h);
  458. serial_puts(SERIAL_COM1, "x");
  459. serial_putdec(SERIAL_COM1, fb_bpp);
  460. serial_puts(SERIAL_COM1, " pitch ");
  461. serial_putdec(SERIAL_COM1, fb_pitch);
  462. serial_puts(SERIAL_COM1, " lfb ");
  463. serial_puthex(SERIAL_COM1, fb_base);
  464. serial_puts(SERIAL_COM1, "\n");
  465. }
  466. void bootcon_set_lfb(void *va)
  467. {
  468. if (va)
  469. fb_lfb = (volatile unsigned char *)va;
  470. }