/* Direct-to-LFB boot console (see bootcon.h). * * This deliberately does not use vga_fb_width()/vga_fb_*(): those call * vga_cache_boot_fb(), which probes the GPU and maps BARs, and this code runs * before mm_init/paging_init exist. It reads the boot framebuffer info block * at 0x600 directly, exactly like paging.c does. * * The console uses the same 8x16 Terminus face and cell grid as the ring-3 * terminal, and starts at the top-left like the BIOS text console, so the * loader -> kernel -> WM handoff does not change the text size or jump around * the screen. A full screen scrolls instead of wrapping back to the top. */ #include "bootcon.h" #include "bootinfo.h" #include "font_8x16.h" #include "mm.h" #include "process.h" #include "serial.h" #include "syscall.h" #include "vga.h" #define CELL_W 8 #define CELL_H 16 #define MARGIN 8 static unsigned int fb_w, fb_h, fb_pitch, fb_bpp; static unsigned int fb_base; static volatile unsigned char *fb_lfb; static int fb_swap; static int fb_ok; static int enabled; static unsigned int con_x, con_y; /* console pixel origin */ static unsigned int con_cols, con_rows; static unsigned int cur_col, cur_row; /* Text log kept independently of the framebuffer: bootcon is disabled when * the WM attaches, but the e1000e probe and later driver messages still go * through bootcon_puts(). The buffer is allocated from the frame allocator * (see bootcon_log_init) so it does not move the kernel's _end past the low * rootfs pack at 0x60000. */ static char *log_buf; static unsigned int log_cap; static unsigned int log_len; static int log_truncated; /* Always-on COM1 log (the `seriallog` source and the bootscan Serial tab). * `serlog_total` counts every captured byte; the buffer holds the newest * `serlog_len` of them, so absolute offsets survive the drops. */ static char *serlog_buf; static unsigned int serlog_cap; static unsigned int serlog_len; static unsigned int serlog_total; static int serlog_truncated; /* serial_putc() runs before the page allocator exists; keep those first * characters in BSS and replay them into the ring at log_init. The window * before mm_init is just the version line, the start/gdt traces and the * allocator's bitmap line, so 256 bytes is enough to seed a complete log. */ static char serlog_early[256]; static unsigned int serlog_early_len; /* Serial output of the bootscan kernel thread (bootcon_scan_begin). */ static char *scan_buf; static unsigned int scan_cap; static unsigned int scan_total; static unsigned int scan_start; static unsigned int scan_pid; static int scan_active; static void fb_px(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b) { volatile unsigned char *p; if (!fb_ok || x >= fb_w || y >= fb_h) return; p = fb_lfb + (unsigned long)y * fb_pitch + (unsigned long)x * (fb_bpp / 8); if (fb_bpp == 16) { unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); p[0] = (unsigned char)(v & 0xFF); p[1] = (unsigned char)(v >> 8); } else { p[0] = (unsigned char)(fb_swap ? r : b); p[1] = (unsigned char)g; p[2] = (unsigned char)(fb_swap ? b : r); if (fb_bpp == 32) p[3] = 0; } } /* Clear the band the current row will be drawn into, so a reused line * cannot leave parts of an older message behind. */ static void clear_line(void) { unsigned int x, y; unsigned int y0 = con_y + cur_row * CELL_H; for (y = 0; y < CELL_H; y++) for (x = 0; x < con_cols * CELL_W; x++) fb_px(con_x + x, y0 + y, 0, 0, 0); } static void draw_char(unsigned int x, unsigned int y, unsigned char c) { const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS]; unsigned int row, col; for (row = 0; row < FONT_8X16_ROWS; row++) { unsigned char bits = g[row]; for (col = 0; col < CELL_W; col++) if (bits & (0x80u >> col)) fb_px(x + col, y + row, 255, 255, 255); } } /* Move the console up one text row: every row takes the pixels of the row * below it and the last row is cleared. Only used when the cursor is on the * last row, so a normal boot never pays for it. * * Every supported bpp puts a whole number of dwords on a scanline for CELL_W * pixels, and the console origin is a multiple of 4 pixels, so no partial * dword is left behind. Plain word loads/stores are used rather than * `rep movsl`: the source is the LFB, not RAM. */ static void scroll_up(void) { unsigned int pb = fb_bpp / 8; unsigned int dwords = (con_cols * CELL_W * pb) / 4; unsigned char *base = (unsigned char *)fb_lfb + (unsigned long)con_y * fb_pitch + con_x * pb; unsigned int line, i; unsigned int lines = con_rows * CELL_H; /* Move every scanline below the first text row up by one cell. */ for (line = 0; line < lines - CELL_H; line++) { unsigned int *dst = (unsigned int *)(base + (unsigned long)line * fb_pitch); const unsigned int *src = (const unsigned int *)(base + (unsigned long)(line + CELL_H) * fb_pitch); for (i = 0; i < dwords; i++) dst[i] = src[i]; } /* Clear the bottom cell. */ for (line = lines - CELL_H; line < lines; line++) { unsigned int *dst = (unsigned int *)(base + (unsigned long)line * fb_pitch); for (i = 0; i < dwords; i++) dst[i] = 0; } } void bootcon_init(void) { const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB; /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */ __asm__ volatile ("" : "+r"(fb)); fb_ok = 0; enabled = 0; if (!fb->valid || !fb->lfb_base) return; fb_w = fb->width; fb_h = fb->height; fb_pitch = fb->pitch; fb_bpp = fb->bpp; fb_base = fb->lfb_base; if (fb_bpp != 16 && fb_bpp != 24 && fb_bpp != 32) return; /* Some BIOSes report a pitch in pixels or too small; enforce a sane * minimum exactly like vga_cache_boot_fb() does. */ if (fb_pitch < fb_w * (fb_bpp / 8)) fb_pitch = fb_w * (fb_bpp / 8); fb_lfb = (volatile unsigned char *)(unsigned long)fb->lfb_base; fb_swap = bootinfo_rgbx(); if (!fb_w || !fb_h || !fb_pitch || !fb_lfb) return; con_x = MARGIN; con_y = MARGIN; if (fb_w <= con_x + CELL_W || fb_h <= con_y + CELL_H) return; con_cols = (fb_w - 2 * MARGIN) / CELL_W; con_rows = (fb_h - 2 * MARGIN) / CELL_H; if (!con_cols || !con_rows) return; fb_ok = 1; enabled = 1; cur_col = 0; cur_row = 0; clear_line(); } int bootcon_ok(void) { return fb_ok; } void bootcon_set_enabled(int on) { enabled = on && fb_ok; if (enabled) clear_line(); } static void newline(void) { cur_col = 0; if (cur_row + 1 >= con_rows) { scroll_up(); cur_row = con_rows - 1; } else { cur_row++; } clear_line(); } void bootcon_log_init(void) { unsigned int mem, i; if (!serlog_buf) { mem = mm_alloc_pages(BOOTCON_SERLOG_PAGES); if (mem) { serlog_buf = (char *)mem; serlog_cap = BOOTCON_SERLOG_PAGES * PAGE_SIZE; serlog_len = 0; serlog_total = 0; serlog_truncated = 0; /* Replay the characters serial_putc() captured * before the page allocator was up. */ for (i = 0; i < serlog_early_len; i++) bootcon_serlog_char(serlog_early[i]); serlog_early_len = 0; } } if (log_buf) return; mem = mm_alloc_pages(BOOTCON_LOG_PAGES); if (!mem) return; log_buf = (char *)mem; log_cap = BOOTCON_LOG_PAGES * PAGE_SIZE; log_len = 0; log_truncated = 0; log_buf[0] = '\0'; } void bootcon_puts(const char *s) { while (*s) { unsigned char c = (unsigned char)*s++; /* Keep the text even after bootcon_set_enabled(0) has hidden * the framebuffer console. */ if (log_buf) { if (log_len + 1 < log_cap) { log_buf[log_len++] = (char)c; log_buf[log_len] = '\0'; } else { log_truncated = 1; } } if (!fb_ok || !enabled) continue; if (c == '\r') { cur_col = 0; continue; } if (c == '\n') { newline(); continue; } if (cur_col >= con_cols) newline(); draw_char(con_x + cur_col * CELL_W, con_y + cur_row * CELL_H, c); cur_col++; } } const char *bootcon_log(unsigned int *len) { *len = log_len; return log_buf ? log_buf : ""; } int bootcon_log_truncated(void) { return log_truncated; } /* Every COM1 character, independent of the bootscan thread and of whether * the UART itself answered. `seriallog save` writes this to a file on the * USB ESP so a serial-less board can be debugged from Linux. CRs are * dropped (serial_puts emits them before every LF), so the buffer is a plain * LF-only text stream. When full, the older half is dropped to make room, * which keeps the buffer contiguous and the capture live. */ void bootcon_serlog_char(char c) { if (c == '\r') return; if (!serlog_buf || !serlog_cap) { if (serlog_early_len < sizeof(serlog_early)) serlog_early[serlog_early_len++] = c; return; } if (serlog_len >= serlog_cap) { unsigned int drop = serlog_cap / 2; unsigned int i; /* Keep whole lines where possible. */ while (drop < serlog_len && serlog_buf[drop] != '\n') drop++; if (drop < serlog_len) drop++; for (i = 0; i + drop < serlog_len; i++) serlog_buf[i] = serlog_buf[i + drop]; serlog_len -= drop; serlog_truncated = 1; } serlog_buf[serlog_len++] = c; serlog_total++; } const char *bootcon_serlog(unsigned int *len) { *len = serlog_len; return serlog_buf ? serlog_buf : ""; } int bootcon_serlog_truncated(void) { return serlog_truncated; } /* Unified source view for SYS_BOOTLOG (the bootscan window). See bootcon.h. */ int bootcon_log_stat(int src, unsigned int *total, unsigned int *start, unsigned int *cap, int *active) { switch (src) { case SYS_BOOTLOG_BOOT: *total = log_len; *start = 0; *cap = log_cap; *active = log_buf && log_len < log_cap; return 0; case SYS_BOOTLOG_SERIAL: *total = serlog_total; *start = serlog_total - serlog_len; *cap = serlog_cap; *active = serlog_buf != 0; return 0; case SYS_BOOTLOG_SCAN: *total = scan_total; *start = scan_start; *cap = scan_cap; *active = scan_active; return 0; default: return -1; } } unsigned int bootcon_log_read(int src, unsigned int off, char *dst, unsigned int max) { unsigned int i, n; switch (src) { case SYS_BOOTLOG_BOOT: if (!log_buf || off >= log_len) return 0; n = log_len - off; if (n > max) n = max; for (i = 0; i < n; i++) dst[i] = log_buf[off + i]; return n; case SYS_BOOTLOG_SERIAL: { unsigned int base = serlog_total - serlog_len; if (!serlog_buf || off < base || off >= serlog_total) return 0; n = serlog_total - off; if (n > max) n = max; for (i = 0; i < n; i++) dst[i] = serlog_buf[off + i - base]; return n; } case SYS_BOOTLOG_SCAN: return bootcon_scan_read(off, dst, max); default: return 0; } } /* ---- bootscan thread serial capture ---------------------------------- */ void bootcon_scan_begin(void) { unsigned int mem; if (!scan_buf) { mem = mm_alloc_pages(BOOTCON_SCAN_PAGES); if (!mem) { scan_cap = 0; scan_active = 0; return; } scan_buf = (char *)mem; scan_cap = BOOTCON_SCAN_PAGES * PAGE_SIZE; } scan_pid = current_proc() ? current_proc()->pid : 0; scan_total = 0; scan_start = 0; scan_active = 1; } void bootcon_scan_end(void) { scan_active = 0; } void bootcon_scan_char(char c) { if (!scan_active || !scan_cap) return; if (!current_proc() || current_proc()->pid != scan_pid) return; if (c == '\r') return; /* serial_puts emits CR before every LF */ if (scan_total - scan_start >= scan_cap) scan_start = scan_total - scan_cap + 1; scan_buf[scan_total % scan_cap] = c; scan_total++; } unsigned int bootcon_scan_read(unsigned int off, char *dst, unsigned int max) { unsigned int n = 0; if (!scan_buf || !scan_cap) return 0; if (off < scan_start) off = scan_start; while (off < scan_total && n < max) dst[n++] = scan_buf[off++ % scan_cap]; return n; } void bootcon_dec(unsigned int v) { char buf[12]; unsigned int i = 0; if (v == 0) { bootcon_puts("0"); return; } while (v) { buf[i++] = (char)('0' + v % 10); v /= 10; } while (i) { char one[2]; one[0] = buf[--i]; one[1] = '\0'; bootcon_puts(one); } } void bootcon_hex(unsigned int v) { static const char hx[] = "0123456789ABCDEF"; char buf[9]; unsigned int i; for (i = 0; i < 8; i++) buf[i] = hx[(v >> (4 * (7 - i))) & 0xF]; buf[8] = '\0'; bootcon_puts(buf); } void bootcon_fb_info(void) { bootcon_puts("fb "); bootcon_dec(fb_w); bootcon_puts("x"); bootcon_dec(fb_h); bootcon_puts("x"); bootcon_dec(fb_bpp); bootcon_puts(" pitch "); bootcon_dec(fb_pitch); bootcon_puts(" lfb 0x"); bootcon_hex(fb_base); bootcon_puts("\n"); /* Mirror to COM1 (and the netlog console) so a machine without a * serial port can report the VBE mode before the WM repaints. */ serial_puts(SERIAL_COM1, "fb "); serial_putdec(SERIAL_COM1, fb_w); serial_puts(SERIAL_COM1, "x"); serial_putdec(SERIAL_COM1, fb_h); serial_puts(SERIAL_COM1, "x"); serial_putdec(SERIAL_COM1, fb_bpp); serial_puts(SERIAL_COM1, " pitch "); serial_putdec(SERIAL_COM1, fb_pitch); serial_puts(SERIAL_COM1, " lfb "); serial_puthex(SERIAL_COM1, fb_base); serial_puts(SERIAL_COM1, "\n"); } void bootcon_set_lfb(void *va) { if (va) fb_lfb = (volatile unsigned char *)va; }