/* bootscan: live tail of the boot storage-scan kernel thread's output. * * The kernel captures every COM1 character boot_storage_main() writes (the * vfs/blk mount trail, the driver probes and the final boot traces) into a * ring exposed by SYS_BOOTLOG. The WM spawns this window before the first * terminal, so the terminal is created last and keeps the keyboard focus. * * The view follows the tail while the scan runs and keeps the scrollback * afterwards. Wheel, PgUp/PgDn, Up/Down, Home and End scroll; scrolling up * stops the follow until the bottom is reached again. Esc closes it. */ #include "ucmd_lib.h" #include "ngui.h" #define TEXT_MAX (32 * 1024) #define POLL_MS 100 #define LINE_PX 12 #define PAD 8 #define FOOT_H 20 static struct ngui ui; static struct ngui_scroll sc; static char text[TEXT_MAX]; static unsigned int text_len; static unsigned int seen; static int follow = 1; static int pending_tail; static int have_info; static int scan_active; static unsigned int scan_cap; static int quit; static void append_text(const char *p, unsigned int n) { unsigned int i; for (i = 0; i < n && text_len + 1 < TEXT_MAX; i++) text[text_len++] = p[i]; } /* Fetch every byte that appeared since the last poll. Returns 1 when the * view changed. */ static int pull(void) { struct sys_bootlog info; int changed = 0; int passes = 0; if (u_bootlog_stat(&info) != 0) return 0; if (!have_info || info.active != (unsigned int)scan_active || info.capacity != scan_cap) { have_info = 1; scan_active = (int)info.active; scan_cap = info.capacity; changed = 1; } if (!info.capacity) return changed; if (seen < info.start || seen > info.total) seen = info.start; while (seen < info.total && passes++ < 64) { char chunk[SYS_BOOTLOG_CHUNK]; int n = u_bootlog_read(seen, chunk); if (n < 0) { /* The ring wrapped between the two calls. */ if (u_bootlog_stat(&info) != 0) break; if (seen < info.start || seen > info.total) seen = info.start; if (seen >= info.total) break; continue; } if (n == 0) break; append_text(chunk, (unsigned int)n); seen += (unsigned int)n; changed = 1; if ((unsigned int)n < SYS_BOOTLOG_CHUNK) break; } return changed; } static unsigned int count_lines(void) { unsigned int i, n = 0; if (!text_len) return 0; for (i = 0; i < text_len; i++) if (text[i] == '\n') n++; if (text[text_len - 1] != '\n') n++; return n; } static unsigned short line_color(const char *s, unsigned int n) { if (n >= 4 && s[0] == 'v' && s[1] == 'f' && s[2] == 's' && s[3] == ':') return ui.theme.accent; if (n >= 4 && s[0] == 'b' && s[1] == 'l' && s[2] == 'k' && s[3] == ':') return ui.theme.text_dim; if (n >= 6 && s[0] == '[' && s[1] == 'b' && s[2] == 'o' && s[3] == 'o' && s[4] == 't' && s[5] == ']') return ui.theme.text_dim; return ui.theme.text; } static void draw_footer(struct ngui_rect r) { const char *status; const char *hint = "PgUp/PgDn scroll | Esc close"; unsigned short color; int hint_w; if (!have_info || (!scan_cap && !scan_active)) status = "waiting for the boot scan..."; else if (!scan_cap) status = "boot scan log unavailable"; else if (scan_active) status = "live: boot scan running"; else status = "boot scan finished"; color = scan_active ? (scan_cap ? ui.theme.success : ui.theme.warning) : ui.theme.text_dim; ngui_fill_rect(&ui, r, ui.theme.panel); ngui_text(&ui, r.x + 2, r.y + 3, status, color); hint_w = (int)ngui_text_width_ui(&ui, hint); if (hint_w + 120 < r.w) ngui_text(&ui, r.x + r.w - hint_w - 2, r.y + 3, hint, ui.theme.text_dim); } static void draw(void) { struct ngui_rect body, foot; int line_h = ngui_text_line_height(&ui, LINE_PX, 1); unsigned int lines = count_lines(); unsigned int pos = 0, idx = 0; int max, y; if (line_h < 10) line_h = 10; body.x = PAD; body.y = PAD; body.w = ui.w - 2 * PAD; body.h = ui.h - 2 * PAD - FOOT_H; if (body.w < 20) body.w = 20; if (body.h < 20) body.h = 20; foot.x = 0; foot.y = ui.h - FOOT_H; foot.w = ui.w; foot.h = FOOT_H; ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h }, ui.theme.bg); sc.content_h = (int)lines * line_h; ngui_scroll_begin(&ui, body, &sc); ngui_scroll_wheel(&ui, body, &sc); max = sc.content_h - sc.view_h; if (max < 0) max = 0; if (pending_tail && follow) ngui_scroll_to(&sc, max); pending_tail = 0; follow = sc.offset >= max - 1; while (pos < text_len) { unsigned int start = pos, end; while (pos < text_len && text[pos] != '\n') pos++; end = pos; y = body.y + (int)idx * line_h - sc.offset; if (y + line_h > body.y && y < body.y + body.h) { ngui_text_px(&ui, body.x + 2, y + 1, text + start, end - start, line_color(text + start, end - start), LINE_PX, 0, 0, 1); } idx++; if (pos < text_len && text[pos] == '\n') pos++; } ngui_scroll_end(&ui, body, &sc); draw_footer(foot); } static void scroll_keys(void) { int k; int max = sc.content_h - sc.view_h; int page = sc.view_h - 22; if (page < 22) page = 22; if (max < 0) max = 0; while ((k = ngui_key(&ui)) != 0) { int old = sc.offset; if (k == NGUI_KEY_ESC) quit = 1; else if (k == NGUI_KEY_PAGE_UP) ngui_scroll_to(&sc, sc.offset - page); else if (k == NGUI_KEY_PAGE_DOWN) ngui_scroll_to(&sc, sc.offset + page); else if (k == NGUI_KEY_UP) ngui_scroll_to(&sc, sc.offset - 20); else if (k == NGUI_KEY_DOWN) ngui_scroll_to(&sc, sc.offset + 20); else if (k == NGUI_KEY_HOME) ngui_scroll_to(&sc, 0); else if (k == NGUI_KEY_END) ngui_scroll_to(&sc, max); if (sc.offset != old) ngui_invalidate(&ui); } } void _start(const char *arg) { struct win_info wi; unsigned int sw = 0, sh = 0; unsigned int w = 440, h = 300; unsigned int last_poll; (void)arg; if (u_screen_size(&sw, &sh) == 0) { if (sw > 80 && w > sw - 40) w = sw - 40; if (sh > 200 && h > sh - 120) h = sh - 120; } if (w < 260) w = 260; if (h < 180) h = 180; if (u_win_create(w, h, "Boot scan", &wi) != 0) { u_puts("bootscan: no graphics window\n"); u_exit(1); } ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height); ngui_font_attach(&ui); sc.hide_bar = 0; last_poll = u_ticks_ms() - POLL_MS; for (;;) { unsigned int now = u_ticks_ms(); if ((int)(now - (last_poll + POLL_MS)) >= 0) { last_poll = now; if (pull()) { pending_tail = 1; ngui_invalidate(&ui); } } if (!ngui_begin(&ui)) { ngui_end_ex(&ui, 0); u_sleep_ms(POLL_MS); continue; } scroll_keys(); if (quit) break; draw(); ngui_end(&ui); u_sleep_ms(POLL_MS); } u_win_close(); u_exit(0); }