| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- /* 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 "serial.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;
- 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;
- 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;
- }
- 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");
- }
|