bootcon.c 10 KB

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