Kaynağa Gözat

bootscan: add boot/serial tabs to the live log window

Turn the startup bootscan window into a three-tab live log viewer: Boot is
the direct-LFB boot console text, Serial is the always-on COM1 capture from
the first kernel line (including the deferred USB mounts and later driver
output) and Scan is the original storage-scan trail.  Each tab keeps its own
scrollback and follows its newest line; hidden tabs with new output get a
dot, and clicking a tab or pressing 1/2/3/Tab switches.

SYS_BOOTLOG now selects a source with its op (SYS_BOOTLOG_OP(src, STAT|READ)).
The serial capture is a 128 KB window of the newest bytes that drops the
older half at a line boundary, so it stays contiguous for `seriallog save`
and can keep tailing; a 256-byte BSS buffer seeds it with output from before
the page allocator exists.  bootcon.o builds with -Os to keep the 4 KB-aligned
BSS from crossing a page.

Update AGENTS.md and the architecture pages.
Johannes Zellner 1 saat önce
ebeveyn
işleme
7880ae01ed

+ 42 - 26
AGENTS.md

@@ -1911,24 +1911,34 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   via `make gallery-dev`), then the pack copy, then the `GALLERY` disk
   fallback on `root.img`/FAT mounts.  Like the browser launcher it spawns
   the real app parented and waits for it.
-- **Boot scan window**: `ucmd_bootscan.c` is the `bootscan` window, a live
-  tail of the `bootscan` kernel thread's own COM1 output.  `boot_storage_main()`
-  calls `bootcon_scan_begin()`/`bootcon_scan_end()`; while active,
-  `serial_putc()` passes every COM1 character through `bootcon_scan_char()`,
-  which keeps only the owning thread's output (CRs dropped) in a 16 KB
-  frame-allocated ring.  The ring survives the thread, so a later manual
-  `bootscan` launch still shows the completed scan.  The deferred USB mount
-  pass runs after `bootcon_scan_end()`, so its lines go to COM1/netmon but
-  not into the window; the window's scan completes before the slow USB
-  probe.  `SYS_BOOTLOG` (122, STAT
-  fills `struct sys_bootlog` with total/start/capacity/active, READ copies
-  one 1 KB chunk from an absolute offset) is the ring-3 view.  The WM spawns
+- **Boot log window**: `ucmd_bootscan.c` is the `bootscan` window, a live
+  tail with three tabs: *Boot* (`bootcon_puts()`'s text log), *Serial* (the
+  always-on COM1 capture) and *Scan* (`boot_storage_main()`'s own trail).
+  `SYS_BOOTLOG` (122) selects one of the three sources with its op
+  (`SYS_BOOTLOG_OP(src, STAT|READ)`): STAT fills `struct sys_bootlog` with
+  total/start/capacity/active, READ copies one 1 KB chunk from an absolute
+  offset and fails when that byte was already dropped, so the client re-reads
+  the counters.  The Boot source is the linear 32 KB `bootcon_log()` buffer;
+  the Serial source is the newest 128 KB of COM1 output (on overflow it
+  drops the older half at a line boundary, which keeps the buffer contiguous
+  for `seriallog save` and lets the window tail forever), seeded with the
+  handful of characters `serial_putc()` captured before `mm_init` from a
+  256-byte BSS buffer; the Scan source is the same 16 KB frame-allocated ring
+  as before.  `boot_storage_main()` calls `bootcon_scan_begin()`/
+  `bootcon_scan_end()`; while active, `serial_putc()` passes every COM1
+  character through `bootcon_scan_char()`, which keeps only the owning
+  thread's output (CRs dropped).  The ring survives the thread, so a later
+  manual `bootscan` launch still shows the completed scan.  The deferred USB
+  mount pass runs after `bootcon_scan_end()`, so its lines are in the Serial
+  tab (and COM1/netmon) but not in the Scan tab; the Scan tab completes
+  before the slow USB probe.  The WM spawns
   the window before the terminal, waits (2 s bound) for it to appear and
   places it flush right under the top bar, so the terminal is created last
-  and owns the focus.  The app follows the tail while the scan runs, keeps
-  the scrollback afterwards (wheel/PgUp/PgDn/Up/Down/Home/End, scrolling up
-  pauses the follow), colours `vfs:`/`blk:`/`[boot]` lines, and closes with
-  Esc or the title-bar X.
+  and owns the focus.  Each tab keeps its own scrollback and follows its tail
+  while at the bottom (wheel/PgUp/PgDn/Up/Down/Home/End, scrolling up pauses
+  the follow); a dot marks a hidden tab that received new output.  Click a
+  tab, press 1/2/3 or Tab to switch.  `vfs:`/`blk:`/`[boot]` lines are
+  coloured, and Esc or the title-bar X closes the window.
 - **Process monitor**: `ucmd_process.c` — the graphical task manager (`process`
   or the start menu's `Tasks` entry), the ngui counterpart of `top`.  It
   samples once per second and keeps 60 s
@@ -2716,7 +2726,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `acpidump` (writes the mapped
 DSDT to `/share/DSDT.BIN`), `reboot`, `poweroff`,
-`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [probe] [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional] [invert on|off]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `disks` (the ngui storage manager), `meminfo`, `top`, `process` (the ngui task manager), `bootscan` (the live boot storage-scan window), `seriallog save <path>|show` (write the always-on COM1 capture to a file, or print the whole capture on screen), `sched`, `format [-y] <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk; `-y` skips the prompt), `install [-y] <target> [source|self]` (clone the live image, or write the running image; auto-detects the source, preferring `usb0`; `auto` targets the only non-USB NebOS disk),
+`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [probe] [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional] [invert on|off]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `disks` (the ngui storage manager), `meminfo`, `top`, `process` (the ngui task manager), `bootscan` (the live boot-log window with Boot/Serial/Scan tabs), `bootlog [save <path>]` (the boot console text), `seriallog save <path>|show` (the newest COM1 capture), `sched`, `format [-y] <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk; `-y` skips the prompt), `install [-y] <target> [source|self]` (clone the live image, or write the running image; auto-detects the source, preferring `usb0`; `auto` targets the only non-USB NebOS disk),
 `ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `membench` (ring-3 speed check: TSC, ALU, heap/BSS memcpy, syscall rates), `backlight [0-100|up|down]` (also `probe`), `keytest` (raw scancode tap), `help`, `clear`, `serial test`, `kexec`, `dbg`, `service [list|status <name>|start|stop|restart|kill <name>|logs <name>|logtail <name>|--gui]` (init's client; `websrv` is the first service and holds the raw NIC receiver while running), `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `textedit [file]` (Notepad-like plain-text editor; start menu/dock *Textedit*), `browser [url|file]` (disk app launcher; waits for the app so a terminal launch is foreground), and `chat [config-path]` (Cloudron Chat client).
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
@@ -3246,9 +3256,14 @@ QEMU gotchas that bit once:
 
 ### Serial-less boards: save the COM1 capture to a file
 
-`serial_putc()` feeds an always-on 64 KB COM1 ring (`bootcon_serlog_char()`,
+`serial_putc()` feeds an always-on 128 KB COM1 capture (`bootcon_serlog_char()`,
 allocated from the frame allocator by `bootcon_log_init()`), independent of
-the `bootscan` thread.  `seriallog save <path>` writes it to a file; on a
+the `bootscan` thread.  The first characters are captured in a 256-byte BSS
+buffer before the allocator exists and replayed into the capture once it is
+up, so the log starts at the very first kernel line.  The capture keeps the
+newest bytes: when full it drops the older half at a line boundary, which
+keeps the buffer contiguous for the file write and lets the bootscan Serial
+tab keep tailing.  `seriallog save <path>` writes it to a file; on a
 laptop with no legacy COM1 (the Yoga 720), write it to the USB ESP and read
 it from Linux after a reboot:
 
@@ -3476,13 +3491,14 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     ~0x8bf20 once the frame bitmap moved out of BSS; with the ioremap
     window, the EHCI handoff fix, the boot-scan log ring, the init bootstrap,
     parent-pid tree kill, the tty control syscalls and `TTY_MAX` 16, the
-    early BSP LAPIC/EOI, the boot-stage markers and the Synaptics touchpad it
-    is ~0x8f260, ~3.5 KB below the `0x90000` wall.  The WM and `sys_wm.o`
-    build with `-Os` because their extra settings code otherwise pushed the
-    `.data` end across a page boundary and moved the 4 KB-aligned BSS by a
-    whole page; the
-    remaining limit
-    is kernel code/buffers only.
+    early BSP LAPIC/EOI, the boot-stage markers, the Synaptics touchpad and
+    the three-source boot log (Boot/Serial/Scan) it is ~0x8fd60, ~0.7 KB
+    below the `0x90000` wall.  The WM and `sys_wm.o` build with `-Os` because
+    their extra settings code otherwise pushed the `.data` end across a page
+    boundary and moved the 4 KB-aligned BSS by a whole page; `bootcon.o`
+    builds with `-Os` for the same reason (its unified log reader at `-O2`
+    crossed that boundary).  The remaining limit is kernel code/buffers
+    only.
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
     QuickJS (ES2025) with the DOM surface described above (creation/mutation/selectors/

+ 4 - 0
Makefile

@@ -132,6 +132,10 @@ KERNEL_OS = terminal disk fat nvidia nv50 uhci ohci ehci xhci asix rndis \
 	peek ne2000 r8168 blkdev fatdetect fatformat format install usbmsc dbg vfs \
 	wm sys_wm ideapad i2c touchpad usbhid
 $(addprefix $(B)/kernel/,$(addsuffix .o,$(KERNEL_OS))): OPT = -Os
+# bootcon.c carries the unified boot/serial/scan log reader; at -O2 its extra
+# text moved the .data end across a page boundary and shifted the 4 KB-aligned
+# BSS by a whole page, which the 0x90000 wall cannot afford.
+$(B)/kernel/bootcon.o: OPT = -Os
 $(B)/kernel/e1000e.o: OPT = -Oz
 # The ACPI table/S5 parser and the poweroff path are cold boot/shutdown code
 # and the kernel is close to the low-memory wall; squeeze them hard.

+ 3 - 3
docs/boot.html

@@ -406,7 +406,7 @@
 			<li>Bring up COM1 and the network-console ring buffer.</li>
 			<li>Install the GDT/TSS and per-CPU GS base.</li>
 			<li>Parse E820, build the frame bitmap and initialise <code>kmalloc</code>.</li>
-			<li>Enable paging; keep a RAM copy of the boot log; enable SSE state if present.</li>
+			<li>Keep RAM copies of the boot console text and the COM1 capture as soon as the frame allocator is up (the first serial bytes were held in a small BSS buffer until then); enable paging; enable SSE state if present.</li>
 			<li>Parse ACPI/MADT; initialise I/O, power, idle, thermal and board sensors.</li>
 			<li>Create the process table, then start the PIT and verify it ticks.</li>
 			<li>Start APs through INIT-SIPI-SIPI (they idle until scheduling is enabled).</li>
@@ -414,7 +414,7 @@
 			<li>Inflate the command pack into <code>/ramfs</code> (table-driven whole-buffer inflate), initialize the kernel window server and start <code>/ramfs/bin/init</code> as PID 1. Init spawns <code>/ramfs/bin/wm</code>; the kernel never starts a WM and the boot console stays if init is absent.</li>
 			<li>Publish <code>vfs_scan_busy</code> and call <code>wm_server_boot_ready()</code>; the WM spawns the <code>bootscan</code> log window first and the first terminal after it, then idle (1 s cap) until the terminal presents.</li>
 			<li>Bring up USB (controllers, HID, USB NICs/WiFi/disks) non-preemptibly, as before the WM existed; probe the NIC, restore retained network config, start the kworker.</li>
-			<li>Start the <code>bootscan</code> kernel thread (non-USB storage probe + FAT mounts, then the deferred USB mounts after the scan window closes, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done); its COM1 output is captured into the <code>SYS_BOOTLOG</code> ring, which the <code>bootscan</code> window tails live. The deferred USB pass can wait seconds for a slow card reader without holding up the desktop, the scan window or the other volumes. Run DHCP if needed.</li>
+			<li>Start the <code>bootscan</code> kernel thread (non-USB storage probe + FAT mounts, then the deferred USB mounts after the scan window closes, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done); its COM1 output is captured into the <code>Scan</code> source of <code>SYS_BOOTLOG</code>. The <code>bootscan</code> window also tabs the boot console text and the complete COM1 capture (which starts at the first kernel line and keeps tailing after the scan). The deferred USB pass can wait seconds for a slow card reader without holding up the desktop, the scan window or the other volumes. Run DHCP if needed.</li>
 			<li>Enable scheduling on the APs and enter the kernel idle loop.</li>
 		</ol>
 		<p>
@@ -472,7 +472,7 @@
 			<li><code>kernel/netkexec.c</code> — network reload</li>
 			<li><code>kernel/kernel.c</code> — init order</li>
 			<li><code>kernel/init.c</code>, <code>kernel/init.h</code> — PID-1 init bootstrap and restart</li>
-			<li><code>kernel/bootcon.c</code>, <code>common/font_8x16.c</code> — direct-to-LFB boot console and the <code>bootscan</code> thread's serial ring (<code>SYS_BOOTLOG</code>)</li>
+			<li><code>kernel/bootcon.c</code>, <code>common/font_8x16.c</code> — direct-to-LFB boot console, its text log, the always-on COM1 capture and the <code>bootscan</code> thread's serial ring (the three <code>SYS_BOOTLOG</code> sources)</li>
 			<li><code>kernel/install.c</code>, <code>kernel/install.h</code> — clone, self-update and net-install writer</li>
 			<li><code>include/rootfs.h</code> — installed image geometry</li>
 			<li><code>kernel/kernel.ld</code> — image limits</li>

+ 1 - 1
docs/development.html

@@ -365,7 +365,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 		<ul>
 			<li><strong>Serial log.</strong> <code>make monitor</code> shows the boot trace and printf-style driver output. The <code>NebOS v… booted</code> line is written before the framebuffer is ready.</li>
 			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime; the <code>bootscan</code> window presents just before it and is not counted).  On QEMU the USB image reaches it about 0.5 s after the kernel starts; USB, the NIC and the disks are probed after that (<code>[boot] usb/net/storage</code> lines).</li>
-			<li><strong>No serial port.</strong> The boot console is the early log. After the desktop, <code>bootlog</code> prints the captured text and <code>bootlog save /usb0p3/BOOTLOG.TXT</code> writes it to the live stick's FAT ESP (mount that partition on the host to read it).  <code>seriallog show</code> prints the whole always-on COM1 capture on screen, which works even when no USB volume is mounted; <code>seriallog save /usb0p3/USBLOG.TXT</code> writes it to a file instead.  The capture includes the xHCI init/Address Device failure lines for both controllers and the touchpad probe's LPSS/PMECTRL/pad diagnostics; run <code>touchpad probe</code> first.  Once DHCP works, <code>make netmon</code> mirrors serial and terminal output to UDP 19290, so it becomes the serial console.</li>
+			<li><strong>No serial port.</strong> The boot console is the early log. After the desktop, <code>bootlog</code> prints the captured text and <code>bootlog save /usb0p3/BOOTLOG.TXT</code> writes it to the live stick's FAT ESP (mount that partition on the host to read it).  <code>seriallog show</code> prints the newest 128 KB of the always-on COM1 capture on screen, which works even when no USB volume is mounted; <code>seriallog save /usb0p3/USBLOG.TXT</code> writes it to a file instead.  The bootscan window's <em>Serial</em> tab tails the same capture live from the first kernel line and keeps the boot console text and the storage-scan trail in its <em>Boot</em>/<em>Scan</em> tabs.  The capture includes the xHCI init/Address Device failure lines for both controllers and the touchpad probe's LPSS/PMECTRL/pad diagnostics; run <code>touchpad probe</code> first.  Once DHCP works, <code>make netmon</code> mirrors serial and terminal output to UDP 19290, so it becomes the serial console.</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
 			<li><strong>Keys.</strong> <code>keytest</code> drains the raw scancode tap in <span class="file">io.c</span> and prints every byte with its E0-prefix state, make/break kind and decoded <code>IO_KEY_*</code>/ASCII value; Ctrl+C stops it.  It sees releases and keys the decoder ignores, so an unknown action-row key is easy to identify.</li>

+ 6 - 4
docs/drivers.html

@@ -686,10 +686,12 @@
 			and netlog mirrors are still fed before the dead check.
 			<code>serial_puthex</code> and <code>serial_putdec</code> are the
 			structured dump helpers used by drivers.  Every COM1 character is
-			also appended to an always-on 64 KB ring (even when the UART is
-			absent); <code>seriallog save &lt;path&gt;</code> writes it to a
-			file.  On the Yoga, save it on the USB ESP and read the xHCI/USB
-			trace from Linux.
+			also appended to an always-on 128 KB capture (even when the UART is
+			absent; the first bytes are held in a small BSS buffer before the
+			allocator exists, and the capture drops its older half at a line
+			boundary when full); <code>seriallog save &lt;path&gt;</code> writes it to a
+			file, and the <code>bootscan</code> Serial tab tails it live.  On the
+			Yoga, save it on the USB ESP and read the xHCI/USB trace from Linux.
 		</p>
 	</section>
 

+ 4 - 4
docs/userland.html

@@ -327,7 +327,7 @@
 					<tr><td>NIC menu</td><td>111–112</td><td><code>SYS_NET_DEVLIST</code> (present drivers with kind/active/link) and <code>SYS_NET_SELECT</code> (make a named netdev active)</td></tr>
 					<tr><td>Hardware settings</td><td>113–114</td><td><code>SYS_BACKLIGHT(op, value)</code> get/set/step the panel PWM, <code>SYS_CPUFREQ(mode)</code> get/set the P-state mode (Settings sliders/tiles)</td></tr>
 					<tr><td>Popover layout</td><td>115–116</td><td><code>SYS_WIN_RESIZE_SELF(w, h)</code> resizes the caller's canvas, <code>SYS_SCREEN_INFO(out)</code> returns the framebuffer dimensions (apps size their windows from it)</td></tr>
-					<tr><td>Boot log</td><td>122</td><td><code>SYS_BOOTLOG(STAT|READ, ...)</code> returns the ring counters or one 1 KB chunk of the <code>bootscan</code> thread's captured COM1 output (the <code>bootscan</code> window)</td></tr>
+					<tr><td>Boot logs</td><td>122</td><td><code>SYS_BOOTLOG(SYS_BOOTLOG_OP(src, STAT|READ), ...)</code> returns one source's counters or a 1 KB chunk at an absolute offset; sources are the boot console text, the newest 128 KB of COM1 output and the <code>bootscan</code> thread's ring (the window's three tabs)</td></tr>
 					<tr><td>Storage</td><td>119–120</td><td><code>SYS_BLK_READ(index, lba, buf)</code> reads one raw sector, <code>SYS_MOUNT_CTL(op, ...)</code> sets/gets a mount policy (name, read-only, no automount); used by <code>disks</code></td></tr>
 					<tr><td>Mouse cursor</td><td>117–118, 121</td><td><code>SYS_WIN_SET_CURSOR(shape)</code> sets the caller's content cursor, <code>SYS_WM_SET_CURSOR(shape | -1)</code> is the WM's override for its chrome, and <code>SYS_WM_MOUSE_CAPTURE(on)</code> takes/releases the pointer while the WM drags or resizes a window (shapes in <span class="file">cursor_abi.h</span>)</td></tr>
 					<tr><td>Wallpaper</td><td>123</td><td><code>SYS_WM_WALLPAPER(1)</code> bumps <code>wm_status.wallpaper_seq</code> after a client writes the RGB565 wallpaper file; the WM reloads it and repaints the desktop</td></tr>
@@ -359,7 +359,7 @@
 		</p>
 		<ul>
 			<li><strong>Dispatch order.</strong> Builtins → PATH file → kernel command. A PATH hit loads the ELF through the VFS and spawns it as ring 3.</li>
-			<li><strong>Builtins.</strong> <code>cd</code>, <code>path</code>, <code>help</code>, <code>clear</code>; the ring-3 shell also handles <code>ps</code> and <code>kill</code> inline. <code>bootlog</code> is a kernel command that dumps the early boot console; <code>bootscan</code> is the ring-3 live scan window.</li>
+			<li><strong>Builtins.</strong> <code>cd</code>, <code>path</code>, <code>help</code>, <code>clear</code>; the ring-3 shell also handles <code>ps</code> and <code>kill</code> inline. <code>bootlog</code> is a kernel command that dumps the early boot console; <code>bootscan</code> is the ring-3 live boot-log window (Boot/Serial/Scan tabs).</li>
 			<li><strong>Arguments.</strong> The raw remainder of the line is passed verbatim; commands parse their own flags.</li>
 			<li><strong>Working directory.</strong> The prompt shows <code>[dir] &gt;</code>; <code>cd</code> changes only the current terminal's cwd, and spawned commands inherit it.</li>
 			<li><strong>Windows.</strong> A window receives keys through its window key queue (<code>SYS_WIN_KEY_WAIT</code>) or the generic <code>SYS_KEY_POLL</code>; closing the window kills its owner process.</li>
@@ -453,7 +453,7 @@
 		<h2>GUI applications</h2>
 		<ul>
 			<li><strong>files</strong> — browser for the VFS; opens text in <code>textedit</code>, images in <code>view</code>, HTML in <code>browser</code>. First user of ngui.</li>
-			<li><strong>bootscan</strong> — live tail of the boot storage-scan kernel thread's output (the WM opens it before the terminal, flush right under the top bar). The kernel captures the thread's COM1 trail (mounts, probes, boot traces) into a ring exposed by <code>SYS_BOOTLOG</code>; the window follows the tail while the scan runs, keeps the scrollback afterwards, pauses the follow when scrolled up, and closes with Esc. The deferred USB mount pass runs after the window's ring closes, so its lines appear on COM1/netmon but not in the bootscan scrollback. Also startable from the shell.</li>
+			<li><strong>bootscan</strong> — live tail of the kernel boot logs (the WM opens it before the terminal, flush right under the top bar). <code>SYS_BOOTLOG</code> exposes three sources, one tab each: <em>Boot</em> is the direct-LFB boot console's text, <em>Serial</em> is the always-on COM1 capture (from the first kernel line, including the deferred USB pass and later driver output) and <em>Scan</em> is the boot storage-scan thread's own trail. Each tab keeps its scrollback and follows its newest line while at the bottom; scrolling up pauses the follow, and a dot marks a hidden tab that received output. Click a tab, press 1/2/3 or Tab to switch; Esc closes the window. Also startable from the shell.</li>
 			<li><strong>process</strong> — graphical task manager (start menu <em>Tasks</em> entry or <code>process</code>), the ngui counterpart of <code>top</code>. It samples once per second into 60 s histories of CPU busy (<code>SYS_SCHEDINFO</code> idle ticks summed over all CPUs), physical memory in use (<code>SYS_MEMINFO</code>) and NIC throughput (<code>SYS_NET_INFO.rx_bytes</code>/<code>tx_bytes</code> from the netdev frame counters), then draws the three graphs with ngui rectangles and lines. The table is a process tree: it lists PID, state, CPU%, memory, total CPU time and name, indents children under their <code>parent_pid</code>, and still sorts sibling groups by PID, state, CPU, memory, time or name when a header is clicked; PgUp/PgDn or the wheel scrolls. Selecting a row shows its pid/ppid and details; <em>End task</em> asks for a second click and then calls <code>SYS_KILL</code> (which kills the subtree), while kernel threads (<code>kernel_thread</code> in the process snapshot), the monitor itself and init are protected. Esc closes it.</li>
 			<li><strong>disks</strong> — storage manager (start menu <em>Disks</em> entry, or the <em>Disks</em> button in <code>files</code>): devices, MBR partitions and mounts; per-volume mount name, read-only and automount saved to <code>/data/MOUNTS.CFG</code>; Format and Install NebOS dialogs that run <code>format -y</code>/<code>install -y</code> on a private tty and show the log. See <a href="filesystems.html#disks">Filesystems → Storage manager</a>.</li>
 			<li><strong>gallery</strong> — live widget and primitive showcase; the real app ships in the pack.</li>
@@ -566,7 +566,7 @@
 			<li><code>user/apps/disks/ucmd_disks.c</code> — storage manager</li>
 			<li><code>user/apps/textedit/ucmd_textedit.c</code> — Notepad-like text editor and its ngui text-area user</li>
 			<li><code>user/apps/process/ucmd_process.c</code> — graphical task manager and its history graphs</li>
-			<li><code>user/apps/bootscan/ucmd_bootscan.c</code> — live boot-scan log window (<code>SYS_BOOTLOG</code>, <code>kernel/bootcon.c</code>)</li>
+			<li><code>user/apps/bootscan/ucmd_bootscan.c</code> — live boot-log window with Boot/Serial/Scan tabs (<code>SYS_BOOTLOG</code>, <code>kernel/bootcon.c</code>)</li>
 			<li><code>user/apps/netmenu/ucmd_netmenu.c</code> — top-bar quick-settings popover</li>
 			<li><code>user/apps/settings/ucmd_settings.c</code>, <code>user/apps/wifi/ucmd_wifi.c</code>, <code>include/wifi_cfg.h</code>, <code>user/lib/wifi/wifi_store.c/h</code> — Settings window, WiFi supplicant, staged WiFi connect record and saved-network list</li>
 		</ul>

+ 20 - 11
include/syscall.h

@@ -135,21 +135,30 @@
 #define SYS_BLK_READ      119 /* (index, lba, void *buf512) -> 0/<0 */
 #define SYS_MOUNT_CTL     120 /* (op, a2, a3) */
 
-/* Boot storage-scan log (the `bootscan` kernel thread's serial output,
- * captured while it runs).  STAT fills struct sys_bootlog, READ copies from
- * absolute byte offset a2 into the caller's SYS_BOOTLOG_CHUNK buffer at a3
- * and returns the number of bytes (0 at the end, <0 on a bad range).  The
- * producer is a ring: `start` is the oldest byte still available. */
-#define SYS_BOOTLOG       122
-#define SYS_BOOTLOG_STAT  0
-#define SYS_BOOTLOG_READ  1
-#define SYS_BOOTLOG_CHUNK 1024
+/* Kernel boot logs (the `bootscan` window's tabs).  a1 is
+ * SYS_BOOTLOG_OP(source, kind):
+ *   STAT fills struct sys_bootlog, READ copies from absolute byte offset a2
+ *   into the caller's SYS_BOOTLOG_CHUNK buffer at a3 and returns the number
+ *   of bytes (0 at the end, <0 when the range fell out of the source).
+ * Sources: the direct-LFB boot console text, the always-on COM1 capture
+ * (newest bytes kept) and the boot storage-scan kernel thread's own trail.
+ * `start` is the absolute offset of the oldest byte still available; a
+ * source drops old bytes when its buffer fills. */
+#define SYS_BOOTLOG           122
+#define SYS_BOOTLOG_BOOT      0
+#define SYS_BOOTLOG_SERIAL    1
+#define SYS_BOOTLOG_SCAN      2
+#define SYS_BOOTLOG_SRC_COUNT 3
+#define SYS_BOOTLOG_STAT      0
+#define SYS_BOOTLOG_READ      1
+#define SYS_BOOTLOG_OP(src, kind) ((src) * 2 + (kind))
+#define SYS_BOOTLOG_CHUNK     1024
 
 struct sys_bootlog {
 	unsigned int total;      /* bytes ever written */
 	unsigned int start;      /* absolute offset of the oldest available byte */
-	unsigned int capacity;   /* ring size in bytes (0: no log) */
-	unsigned int active;     /* the scan thread is still producing */
+	unsigned int capacity;   /* buffer size in bytes (0: no log) */
+	unsigned int active;     /* the producer can still append */
 };
 #define SYS_MOUNT_POLICY_SET 0 /* (const struct sys_mount_policy *) -> 0/<0 */
 #define SYS_MOUNT_POLICY_GET 1 /* (index, struct sys_mount_policy *out) -> 1/0 */

+ 104 - 29
kernel/bootcon.c

@@ -16,6 +16,7 @@
 #include "mm.h"
 #include "process.h"
 #include "serial.h"
+#include "syscall.h"
 #include "vga.h"
 
 #define CELL_W  8
@@ -42,11 +43,20 @@ static unsigned int log_cap;
 static unsigned int log_len;
 static int log_truncated;
 
-/* Always-on COM1 log (the `seriallog save` source). */
+/* 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;
@@ -214,7 +224,7 @@ static void newline(void)
 
 void bootcon_log_init(void)
 {
-	unsigned int mem;
+	unsigned int mem, i;
 
 	if (!serlog_buf) {
 		mem = mm_alloc_pages(BOOTCON_SERLOG_PAGES);
@@ -222,8 +232,13 @@ void bootcon_log_init(void)
 			serlog_buf = (char *)mem;
 			serlog_cap = BOOTCON_SERLOG_PAGES * PAGE_SIZE;
 			serlog_len = 0;
+			serlog_total = 0;
 			serlog_truncated = 0;
-			serlog_buf[0] = '\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;
 		}
 	}
 
@@ -286,17 +301,35 @@ int bootcon_log_truncated(void)
 
 /* 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. */
+ * 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 (!serlog_buf)
+	if (c == '\r')
 		return;
-	if (serlog_len + 1 < serlog_cap) {
-		serlog_buf[serlog_len++] = c;
-		serlog_buf[serlog_len] = '\0';
-	} else {
+	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)
@@ -310,6 +343,68 @@ 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)
@@ -351,26 +446,6 @@ void bootcon_scan_char(char c)
 	scan_total++;
 }
 
-unsigned int bootcon_scan_total(void)
-{
-	return scan_total;
-}
-
-unsigned int bootcon_scan_start(void)
-{
-	return scan_start;
-}
-
-unsigned int bootcon_scan_capacity(void)
-{
-	return scan_cap;
-}
-
-int bootcon_scan_running(void)
-{
-	return scan_active;
-}
-
 unsigned int bootcon_scan_read(unsigned int off, char *dst, unsigned int max)
 {
 	unsigned int n = 0;

+ 19 - 10
kernel/bootcon.h

@@ -16,9 +16,11 @@
 /* Ring for the storage scan thread's own serial output (see bootcon.c).  It
  * is allocated on demand from the frame allocator, not BSS. */
 #define BOOTCON_SCAN_PAGES 4
-/* Always-on COM1 capture used by `seriallog save` on serial-less boards.
- * The xHCI/USB/touchpad debug lines go here even though the UART is absent. */
-#define BOOTCON_SERLOG_PAGES 16
+/* Always-on COM1 capture used by `seriallog save` and the bootscan window.
+ * The xHCI/USB/touchpad debug lines go here even though the UART is absent.
+ * The buffer keeps the newest bytes (it drops the older half when full), so
+ * a live window can keep tailing a long uptime. */
+#define BOOTCON_SERLOG_PAGES 32
 
 void bootcon_init(void);
 int  bootcon_ok(void);
@@ -41,6 +43,16 @@ void bootcon_log_init(void);
 const char *bootcon_log(unsigned int *len);
 int bootcon_log_truncated(void);
 
+/* Unified view for the `bootscan` window (SYS_BOOTLOG).  `src` is
+ * SYS_BOOTLOG_BOOT/SERIAL/SCAN from syscall.h.  stat fills the absolute
+ * counters; `start` is the oldest byte still available (0 for the linear boot
+ * console log).  read copies from absolute offset off and returns the byte
+ * count (0 when there is nothing left). */
+int bootcon_log_stat(int src, unsigned int *total, unsigned int *start,
+		     unsigned int *cap, int *active);
+unsigned int bootcon_log_read(int src, unsigned int off, char *dst,
+			      unsigned int max);
+
 /* Capture the bootscan kernel thread's serial output.  begin() is called by
  * the thread itself (it allocates the ring and records the thread pid), end()
  * stops the capture when the thread returns.  serial_putc() feeds every COM1
@@ -50,17 +62,14 @@ int bootcon_log_truncated(void);
 void bootcon_scan_begin(void);
 void bootcon_scan_end(void);
 void bootcon_scan_char(char c);
-unsigned int bootcon_scan_total(void);
-unsigned int bootcon_scan_start(void);
-unsigned int bootcon_scan_capacity(void);
-int bootcon_scan_running(void);
 /* Copy up to max bytes from absolute offset off; off is clamped to the
  * oldest byte still in the ring.  Returns the byte count. */
 unsigned int bootcon_scan_read(unsigned int off, char *dst, unsigned int max);
 
-/* Always-on COM1 log (independent of the bootscan thread).  It starts at
- * bootcon_log_init() and keeps the first BOOTCON_SERLOG_PAGES pages of
- * serial output; later output is dropped after the buffer fills. */
+/* Always-on COM1 log (independent of the bootscan thread).  It is seeded with
+ * the characters captured before the page allocator was up and then keeps
+ * the newest bytes in a BOOTCON_SERLOG_PAGES ring: when full, the older half
+ * is dropped (the returned buffer stays contiguous for `seriallog save`). */
 void bootcon_serlog_char(char c);
 const char *bootcon_serlog(unsigned int *len);
 int bootcon_serlog_truncated(void);

+ 5 - 3
kernel/kernel.c

@@ -484,6 +484,11 @@ void kernel_main(void)
 	boot_trace("gdt");
 
 	mm_init();
+	/* Keep a RAM copy of the boot console text and the COM1 trail as soon
+	 * as the frame allocator can give us pages; `bootlog`/`seriallog` and
+	 * the bootscan window read them.  Everything before this point is
+	 * still in the small BSS early-serial buffer. */
+	bootcon_log_init();
 	mm_dump();
 	boot_trace("mm");
 	boot_val("mm free KB", mm_free_pages() * 4);
@@ -493,9 +498,6 @@ void kernel_main(void)
 	bootcon_fb_info();
 	paging_init();
 	boot_trace("paging");
-	/* Keep a RAM copy of the boot console text; the WM hides the
-	 * framebuffer console later and this is what `bootlog` prints. */
-	bootcon_log_init();
 	/* paging_init only ORs in PSE; make sure the BSP's SSE state is on
 	 * before any drawing path can select an SSE2 helper. */
 	cpu_enable_sse();

+ 23 - 11
kernel/syscall.c

@@ -516,27 +516,37 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return process_is_alive((unsigned int)a1);
 	case SYS_KILL:
 		return process_kill((unsigned int)a1);
-	case SYS_BOOTLOG:
-		if (a1 == SYS_BOOTLOG_STAT) {
+	case SYS_BOOTLOG: {
+		unsigned int src, op;
+		unsigned int total, start, cap;
+		int active;
+
+		if (a1 < 0 || a1 >= SYS_BOOTLOG_SRC_COUNT * 2)
+			return -1;
+		src = (unsigned int)a1 >> 1;
+		op = (unsigned int)a1 & 1u;
+		if (bootcon_log_stat((int)src, &total, &start, &cap,
+				     &active) != 0)
+			return -1;
+		if (op == SYS_BOOTLOG_STAT) {
 			struct sys_bootlog *out = (struct sys_bootlog *)a2;
 
 			if (!paging_user_range_ok(current_proc()->cr3,
 						  (unsigned int)out,
 						  sizeof(*out), 1))
 				return -1;
-			out->total = bootcon_scan_total();
-			out->start = bootcon_scan_start();
-			out->capacity = bootcon_scan_capacity();
-			out->active = (unsigned int)bootcon_scan_running();
+			out->total = total;
+			out->start = start;
+			out->capacity = cap;
+			out->active = (unsigned int)active;
 			return 0;
 		}
-		if (a1 == SYS_BOOTLOG_READ) {
+		if (op == SYS_BOOTLOG_READ) {
 			unsigned int off = (unsigned int)a2;
-			unsigned int total = bootcon_scan_total();
 			unsigned int n;
 
-			if (off < bootcon_scan_start())
-				return -1;      /* ring wrapped; re-read the info */
+			if (off < start)
+				return -1;      /* dropped; re-read the info */
 			if (off >= total)
 				return 0;
 			n = total - off;
@@ -545,9 +555,11 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 			if (!paging_user_range_ok(current_proc()->cr3,
 						  (unsigned int)a3, n, 1))
 				return -1;
-			return (int)bootcon_scan_read(off, (char *)a3, n);
+			return (int)bootcon_log_read((int)src, off,
+						     (char *)a3, n);
 		}
 		return -1;
+	}
 	case SYS_CHDIR: {
 		char arg[VFS_PATH_MAX], abs[VFS_PATH_MAX];
 		unsigned int size;

+ 6 - 6
kernel/terminal.c

@@ -216,9 +216,9 @@ static int cmd_bootlog(struct term *t, const char *args)
 	return 0;
 }
 
-/* Save the always-on COM1 capture to a file, or print the whole capture on
- * screen.  On a serial-less board, write it to the USB ESP and read it from
- * Linux after a reboot. */
+/* Save the always-on COM1 capture to a file, or print the newest window of
+ * it on screen.  On a serial-less board, write it to the USB ESP and read it
+ * from Linux after a reboot. */
 static int cmd_seriallog(struct term *t, const char *args)
 {
 	const char *p;
@@ -232,6 +232,8 @@ static int cmd_seriallog(struct term *t, const char *args)
 	    args[3] == 'w' && (args[4] == ' ' || args[4] == '\0')) {
 		unsigned int i = 0;
 
+		if (bootcon_serlog_truncated())
+			io_putstr("seriallog: [older bytes dropped]\n");
 		while (i < len) {
 			unsigned int j = i, k;
 
@@ -243,8 +245,6 @@ static int cmd_seriallog(struct term *t, const char *args)
 			io_putchar('\n');
 			i = j < len ? j + 1 : j;
 		}
-		if (bootcon_serlog_truncated())
-			io_putstr("seriallog: [truncated]\n");
 		return 0;
 	}
 	if (!(args[0] == 's' && args[1] == 'a' && args[2] == 'v' &&
@@ -272,7 +272,7 @@ static int cmd_seriallog(struct term *t, const char *args)
 		}
 	}
 	if (bootcon_serlog_truncated())
-		io_putstr("seriallog: [truncated]\n");
+		io_putstr("seriallog: [older bytes dropped]\n");
 	return 0;
 }
 

+ 296 - 88
user/apps/bootscan/ucmd_bootscan.c

@@ -1,83 +1,189 @@
-/* bootscan: live tail of the boot storage-scan kernel thread's output.
+/* bootscan: live tail of the kernel's boot logs.
  *
- * 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.
+ * SYS_BOOTLOG exposes three sources: the direct-LFB boot console text (the
+ * `bootlog` command), the always-on COM1 capture (`seriallog`) and the boot
+ * storage-scan kernel thread's own trail.  Each tab keeps its own scrollback;
+ * the Serial tab is the complete boot trail (it starts with the first kernel
+ * line and keeps growing for later driver output, the deferred USB mounts and
+ * the WiFi helper).
  *
- * 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.
+ * The WM spawns this window before the first terminal, so the terminal is
+ * created last and keeps the keyboard focus.
+ *
+ * Every tab follows the newest output while it is at the bottom; wheel,
+ * PgUp/PgDn, Up/Down, Home and End scroll, and scrolling up stops the follow
+ * until the bottom is reached again.  Click a tab, press 1/2/3 or Tab to
+ * switch; Esc closes the window.
  */
 
 #include "ucmd_lib.h"
 #include "ngui.h"
 
-#define TEXT_MAX (32 * 1024)
 #define POLL_MS  100
 #define LINE_PX  12
 #define PAD      8
+#define TAB_H    24
 #define FOOT_H   20
 
+enum { TAB_BOOT, TAB_SERIAL, TAB_SCAN, TAB_COUNT };
+
+struct logsrc {
+	const char *label;
+	int kind;
+	char *text;
+	unsigned int cap;      /* text[] size (at least the kernel window) */
+	unsigned int len;      /* bytes held in text[] */
+	unsigned int base;     /* absolute offset of text[0] */
+	unsigned int seen;     /* absolute offset fetched up to */
+	unsigned int total;    /* source counters from the last poll */
+	unsigned int rstart;
+	unsigned int rcap;
+	int active;
+	int have_info;
+	int fresh;             /* new output arrived while the tab was hidden */
+	int trim_lines;        /* lines dropped from the front since the draw */
+};
+
+/* At least the kernel's window for each source (BOOTCON_LOG_PAGES,
+ * BOOTCON_SERLOG_PAGES, BOOTCON_SCAN_PAGES in kernel/bootcon.h). */
+static char boot_text[32 * 1024];
+static char serial_text[128 * 1024];
+static char scan_text[16 * 1024];
+
+static struct logsrc src[TAB_COUNT] = {
+	{ .label = "Boot", .kind = SYS_BOOTLOG_BOOT,
+	  .text = boot_text, .cap = sizeof(boot_text) },
+	{ .label = "Serial", .kind = SYS_BOOTLOG_SERIAL,
+	  .text = serial_text, .cap = sizeof(serial_text) },
+	{ .label = "Scan", .kind = SYS_BOOTLOG_SCAN,
+	  .text = scan_text, .cap = sizeof(scan_text) },
+};
+
 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 cur = TAB_SERIAL;
 static int follow = 1;
 static int pending_tail;
-static int have_info;
-static int scan_active;
-static unsigned int scan_cap;
+static int pressed_tab = -1;
 static int quit;
 
-static void append_text(const char *p, unsigned int n)
+static unsigned int count_lines(const struct logsrc *s)
 {
-	unsigned int i;
+	unsigned int i, n = 0;
 
-	for (i = 0; i < n && text_len + 1 < TEXT_MAX; i++)
-		text[text_len++] = p[i];
+	if (!s->len)
+		return 0;
+	for (i = 0; i < s->len; i++)
+		if (s->text[i] == '\n')
+			n++;
+	if (s->text[s->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;
+}
+
+/* Drop the oldest `drop` bytes, remembering how many text lines vanished so
+ * an unfollowed view can keep its place. */
+static void trim_front(struct logsrc *s, unsigned int drop)
+{
+	unsigned int i, lines = 0;
+
+	if (drop > s->len)
+		drop = s->len;
+	for (i = 0; i < drop; i++)
+		if (s->text[i] == '\n')
+			lines++;
+	for (i = 0; i + drop < s->len; i++)
+		s->text[i] = s->text[i + drop];
+	s->len -= drop;
+	s->base += drop;
+	s->trim_lines += (int)lines;
+}
+
+static void append_text(struct logsrc *s, const char *p, unsigned int n)
+{
+	unsigned int i, need;
+
+	if (n > s->cap) {
+		unsigned int skip = n - s->cap;
+
+		/* Not reachable with SYS_BOOTLOG_CHUNK-sized reads, but keep
+		 * the absolute mapping consistent if a buffer ever shrinks. */
+		p += skip;
+		s->base += skip;
+		n = s->cap;
+	}
+	if (s->len + n > s->cap) {
+		need = s->len + n - s->cap;
+		/* Drop in quarter-buffer steps instead of copying the whole
+		 * buffer for every chunk once it is full. */
+		if (need < s->cap / 4)
+			need = s->cap / 4;
+		trim_front(s, need);
+	}
+	for (i = 0; i < n; i++)
+		s->text[s->len++] = p[i];
 }
 
 /* Fetch every byte that appeared since the last poll.  Returns 1 when the
  * view changed. */
-static int pull(void)
+static int pull(struct logsrc *s)
 {
 	struct sys_bootlog info;
 	int changed = 0;
 	int passes = 0;
 
-	if (u_bootlog_stat(&info) != 0)
+	if (u_bootlog_stat(s->kind, &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;
+	if (!s->have_info || s->total != info.total ||
+	    s->rstart != info.start || s->rcap != info.capacity ||
+	    s->active != (int)info.active) {
+		s->have_info = 1;
+		s->total = info.total;
+		s->rstart = info.start;
+		s->rcap = info.capacity;
+		s->active = (int)info.active;
 		changed = 1;
 	}
 	if (!info.capacity)
 		return changed;
-	if (seen < info.start || seen > info.total)
-		seen = info.start;
-	while (seen < info.total && passes++ < 64) {
+
+	/* The source dropped bytes we still hold (the Serial window keeps the
+	 * newest bytes, the Scan ring overwrites). */
+	if (info.start > s->base)
+		trim_front(s, info.start - s->base);
+	if (info.start > s->base) {
+		s->base = info.start;
+		s->len = 0;
+	}
+	s->seen = s->base + s->len;
+
+	while (s->seen < info.total && passes++ < 64) {
 		char chunk[SYS_BOOTLOG_CHUNK];
-		int n = u_bootlog_read(seen, chunk);
+		int n = u_bootlog_read(s->kind, s->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;
+			/* The source wrapped between the two calls. */
+			s->have_info = 0;
+			break;
 		}
 		if (n == 0)
 			break;
-		append_text(chunk, (unsigned int)n);
-		seen += (unsigned int)n;
+		append_text(s, chunk, (unsigned int)n);
+		s->seen += (unsigned int)n;
 		changed = 1;
 		if ((unsigned int)n < SYS_BOOTLOG_CHUNK)
 			break;
@@ -85,52 +191,126 @@ static int pull(void)
 	return changed;
 }
 
-static unsigned int count_lines(void)
+static struct ngui_rect tab_rect(int i)
 {
-	unsigned int i, n = 0;
+	struct ngui_rect r;
+	int x = PAD, j;
+
+	r.y = 3;
+	r.h = TAB_H - 6;
+	for (j = 0; j < i; j++)
+		x += (int)ngui_text_width_ui(&ui, src[j].label) + 32;
+	r.x = x;
+	r.w = (int)ngui_text_width_ui(&ui, src[i].label) + 30;
+	return r;
+}
 
-	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 void draw_tabs(void)
+{
+	struct ngui_rect hr = { 0, 0, ui.w, TAB_H };
+	struct ngui_rect br = { 0, TAB_H - 1, ui.w, 1 };
+	int i;
+
+	ngui_fill_rect(&ui, hr, ui.theme.panel);
+	ngui_fill_rect(&ui, br, ui.theme.border);
+	for (i = 0; i < TAB_COUNT; i++) {
+		struct ngui_rect r = tab_rect(i);
+		int hot = ui.inside &&
+			ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y);
+		int lh = ngui_text_line_height(&ui, LINE_PX, 0);
+		int ty = r.y + (r.h - lh) / 2;
+		unsigned short color;
+
+		if (i == cur)
+			ngui_fill_rect(&ui, r, ui.theme.panel_alt);
+		else if (hot)
+			ngui_fill_rect(&ui, r, ui.theme.button);
+		color = (i == cur || hot) ? ui.theme.text : ui.theme.text_dim;
+		if (ty < r.y)
+			ty = r.y;
+		ngui_text(&ui, r.x + 10, ty, src[i].label, color);
+		if (i == cur)
+			ngui_fill_rect(&ui, (struct ngui_rect){ r.x, TAB_H - 3,
+							       r.w, 2 },
+				       ui.theme.accent);
+		if (src[i].fresh && i != cur)
+			ngui_fill_round_rect(&ui, (struct ngui_rect){
+						     r.x + r.w - 12,
+						     r.y + r.h / 2 - 2, 4, 4 },
+					     2, 2, 2, 2, ui.theme.accent);
+		if (hot)
+			ngui_set_cursor(&ui, CURSOR_DEFAULT);
+	}
 }
 
-static unsigned short line_color(const char *s, unsigned int n)
+static int tab_hit(int x, int y)
 {
-	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;
+	int i;
+
+	for (i = 0; i < TAB_COUNT; i++) {
+		struct ngui_rect r = tab_rect(i);
+
+		if (ngui_rect_contains(&r, x, y))
+			return i;
+	}
+	return -1;
+}
+
+static void select_tab(int i)
+{
+	if (i < 0 || i >= TAB_COUNT || i == cur)
+		return;
+	cur = i;
+	src[i].fresh = 0;
+	src[i].trim_lines = 0;
+	sc.offset = 0;
+	follow = 1;
+	pending_tail = 1;
+	ngui_invalidate(&ui);
+}
+
+static void tab_input(void)
+{
+	int i;
+
+	if (ui.pressed)
+		pressed_tab = tab_hit(ui.press_x, ui.press_y);
+	if (ui.released) {
+		i = tab_hit(ui.release_x, ui.release_y);
+		if (i >= 0 && i == pressed_tab)
+			select_tab(i);
+		pressed_tab = -1;
+	}
 }
 
 static void draw_footer(struct ngui_rect r)
 {
 	const char *status;
-	const char *hint = "PgUp/PgDn scroll  |  Esc close";
+	const char *hint = "1/2/3 tabs  |  PgUp/PgDn  |  Esc";
 	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;
+	struct logsrc *s = &src[cur];
+
+	if (!s->have_info) {
+		status = "waiting for the kernel log...";
+		color = ui.theme.text_dim;
+	} else if (!s->rcap) {
+		status = s->kind == SYS_BOOTLOG_SCAN ?
+			"boot scan log unavailable" : "log unavailable";
+		color = ui.theme.warning;
+	} else if (s->kind == SYS_BOOTLOG_BOOT) {
+		status = s->active ? "boot console: live" :
+			"boot console: full";
+		color = s->active ? ui.theme.success : ui.theme.text_dim;
+	} else if (s->kind == SYS_BOOTLOG_SERIAL) {
+		status = s->rstart ? "serial log: live, oldest dropped" :
+			"serial log: live";
+		color = ui.theme.success;
+	} else {
+		status = s->active ? "live: boot scan running" :
+			"boot scan finished";
+		color = s->active ? ui.theme.success : ui.theme.text_dim;
+	}
 
 	ngui_fill_rect(&ui, r, ui.theme.panel);
 	ngui_text(&ui, r.x + 2, r.y + 3, status, color);
@@ -143,18 +323,23 @@ static void draw_footer(struct ngui_rect r)
 static void draw(void)
 {
 	struct ngui_rect body, foot;
+	struct logsrc *s = &src[cur];
 	int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
-	unsigned int lines = count_lines();
+	unsigned int lines = count_lines(s);
 	unsigned int pos = 0, idx = 0;
 	int max, y;
 
 	if (line_h < 10)
 		line_h = 10;
 
+	ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
+		       ui.theme.bg);
+	draw_tabs();
+
 	body.x = PAD;
-	body.y = PAD;
+	body.y = TAB_H + 2;
 	body.w = ui.w - 2 * PAD;
-	body.h = ui.h - 2 * PAD - FOOT_H;
+	body.h = ui.h - body.y - FOOT_H - 2;
 	if (body.w < 20)
 		body.w = 20;
 	if (body.h < 20)
@@ -165,10 +350,15 @@ static void draw(void)
 	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;
+	if (s->trim_lines) {
+		if (!follow) {
+			int off = sc.offset - s->trim_lines * line_h;
+
+			sc.offset = off > 0 ? off : 0;
+		}
+		s->trim_lines = 0;
+	}
 	ngui_scroll_begin(&ui, body, &sc);
 	ngui_scroll_wheel(&ui, body, &sc);
 
@@ -180,21 +370,21 @@ static void draw(void)
 	pending_tail = 0;
 	follow = sc.offset >= max - 1;
 
-	while (pos < text_len) {
+	while (pos < s->len) {
 		unsigned int start = pos, end;
 
-		while (pos < text_len && text[pos] != '\n')
+		while (pos < s->len && s->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),
+			ngui_text_px(&ui, body.x + 2, y + 1, s->text + start,
+				     end - start,
+				     line_color(s->text + start, end - start),
 				     LINE_PX, 0, 0, 1);
 		}
 		idx++;
-		if (pos < text_len && text[pos] == '\n')
+		if (pos < s->len && s->text[pos] == '\n')
 			pos++;
 	}
 	ngui_scroll_end(&ui, body, &sc);
@@ -218,6 +408,10 @@ static void scroll_keys(void)
 
 		if (k == NGUI_KEY_ESC)
 			quit = 1;
+		else if (k >= '1' && k <= '3')
+			select_tab(k - '1');
+		else if (k == NGUI_KEY_TAB)
+			select_tab((cur + 1) % TAB_COUNT);
 		else if (k == NGUI_KEY_PAGE_UP)
 			ngui_scroll_to(&sc, sc.offset - page);
 		else if (k == NGUI_KEY_PAGE_DOWN)
@@ -264,11 +458,24 @@ void _start(const char *arg)
 
 	for (;;) {
 		unsigned int now = u_ticks_ms();
+		int changed = 0;
+		int active_changed = 0;
+		int i;
 
 		if ((int)(now - (last_poll + POLL_MS)) >= 0) {
 			last_poll = now;
-			if (pull()) {
-				pending_tail = 1;
+			for (i = 0; i < TAB_COUNT; i++) {
+				if (!pull(&src[i]))
+					continue;
+				changed = 1;
+				if (i == cur)
+					active_changed = 1;
+				else
+					src[i].fresh = 1;
+			}
+			if (changed) {
+				if (active_changed)
+					pending_tail = 1;
 				ngui_invalidate(&ui);
 			}
 		}
@@ -277,6 +484,7 @@ void _start(const char *arg)
 			u_sleep_ms(POLL_MS);
 			continue;
 		}
+		tab_input();
 		scroll_keys();
 		if (quit)
 			break;

+ 10 - 6
user/ucmd_lib.h

@@ -168,16 +168,20 @@ static inline unsigned int u_ticks_ms(void)
 	return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);
 }
 
-/* Boot storage-scan log (the `bootscan` window).  STAT fills the ring
- * counters; READ copies one SYS_BOOTLOG_CHUNK chunk at absolute offset. */
-static inline int u_bootlog_stat(struct sys_bootlog *out)
+/* Kernel boot logs (the `bootscan` window's tabs).  STAT fills the source's
+ * counters; READ copies one SYS_BOOTLOG_CHUNK chunk at an absolute offset. */
+static inline int u_bootlog_stat(int source, struct sys_bootlog *out)
 {
-	return u_syscall2(SYS_BOOTLOG, SYS_BOOTLOG_STAT, (int)out);
+	return u_syscall2(SYS_BOOTLOG,
+			  SYS_BOOTLOG_OP(source, SYS_BOOTLOG_STAT),
+			  (int)out);
 }
 
-static inline int u_bootlog_read(unsigned int off, char *buf)
+static inline int u_bootlog_read(int source, unsigned int off, char *buf)
 {
-	return u_syscall3(SYS_BOOTLOG, SYS_BOOTLOG_READ, (int)off, (int)buf);
+	return u_syscall3(SYS_BOOTLOG,
+			  SYS_BOOTLOG_OP(source, SYS_BOOTLOG_READ),
+			  (int)off, (int)buf);
 }
 
 /* Cursor position of this process's terminal screen (linear cell index);