1
0

7 コミット c5f21a2ab9 ... 4996d78353

作者 SHA1 メッセージ 日付
  Johannes Zellner 4996d78353 browser: draw ::before/::after icon boxes and data: images 4 日 前
  Johannes Zellner 312f63a340 disks: keep scroll pane content clear of the scrollbar 4 日 前
  Johannes Zellner 06a7aa59be wm: add a Disks launcher to the top bar 4 日 前
  Johannes Zellner d097c55b1a browser: show the hand cursor over URL-bar suggestions 4 日 前
  Johannes Zellner c6eac494e7 ngui: drop the checkmark on selected list rows 4 日 前
  Johannes Zellner a258507718 Add the disks storage manager with mount policies 4 日 前
  Johannes Zellner f3081de286 Render docs.cloudron.io: cascade layers, calc folding, scoped variables 4 日 前

+ 97 - 17
AGENTS.md

@@ -454,7 +454,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   window uses the 1 px border plus title bar).  An outside press dismisses the
   window uses the 1 px border plus title bar).  An outside press dismisses the
   popover and then continues with the normal click handling; a second click
   popover and then continues with the normal click handling; a second click
   on a status target toggles it.
   on a status target toggles it.
-  The last launcher, `Chat`, spawns `/ramfs/bin/chat`; `WM_BTN_END_X`
+  `Chat` spawns `/ramfs/bin/chat` and the last launcher, `Disks`, spawns
+  `/ramfs/bin/disks` (the storage manager); `WM_BTN_END_X`
   (`wm_abi.h`) is the launchers' right edge, and the bar notice and the
   (`wm_abi.h`) is the launchers' right edge, and the bar notice and the
   right-aligned status never draw left of it.
   right-aligned status never draw left of it.
   `terminal.c` keeps the privileged kernel command
   `terminal.c` keeps the privileged kernel command
@@ -501,7 +502,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   primitives, buttons, text input, the Adwaita symbolic icons and scrolling panes
   primitives, buttons, text input, the Adwaita symbolic icons and scrolling panes
   (`ngui_scroll`; `hide_bar` keeps wheel/key scrolling without drawing a
   (`ngui_scroll`; `hide_bar` keeps wheel/key scrolling without drawing a
   scrollbar).
   scrollbar).
-  `files` (`ucmd_files.c`) is its first user; `gallery` (`gallery.c`) is the
+  `files` (`ucmd_files.c`) is its first user (its toolbar's `Disks`
+  button opens the storage manager); `gallery` (`gallery.c`) is the
   live showcase of every control and primitive the toolkit exports.  Buttons
   live showcase of every control and primitive the toolkit exports.  Buttons
   fire on the release edge and keep `active` until `ui->released`: inferring
   fire on the release edge and keep `active` until `ui->released`: inferring
   release from the sampled `ui->buttons` races the WM event queue and drops
   release from the sampled `ui->buttons` races the WM event queue and drops
@@ -710,6 +712,29 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   it when absent.  It refuses a raw runtime pack (`cmds-dev`/`net-cmds`/
   it when absent.  It refuses a raw runtime pack (`cmds-dev`/`net-cmds`/
   `kernel-dev`); use `net-install` for that.  `target=auto` picks the only
   `kernel-dev`); use `net-install` for that.  `target=auto` picks the only
   non-USB NebOS disk.
   non-USB NebOS disk.
+- **Storage manager / mount policies**: `ucmd_disks.c` (`disks`, the
+  top-bar `Disks` launcher and the `Disks` button in the `files` toolbar) is the ngui view of storage; the
+  `storage` command stays the text view.  It lists the present block
+  devices (empty IDE/eMMC slots are hidden), reads sector 0 and each
+  partition's first sector with `SYS_BLK_READ` (119, one 512-byte sector,
+  under `sys_fs_enter`, copied through a kernel buffer) to show the MBR,
+  NebOS boot partition, rootfs, FAT type and label, and lists every mount
+  with its source volume (`struct sys_mount` now carries `dev` and `key`).
+  Floppies are never read raw.  `SYS_MOUNT_CTL` (120) sets/gets a
+  `struct sys_mount_policy` keyed by the volume's default mount name
+  (`ide0p1`, or the device name for a whole-device volume): a mount name,
+  `SYS_MOUNT_RO` and `SYS_MOUNT_NOAUTO`.  `vfs.c` keeps up to 16 policies in
+  RAM; `mount_one()` applies them on every scan (a policy name loses to a
+  name already in use) and a set remounts that device at once.  Writes,
+  unlink, mkdir and rmdir on a mount with `ro` set return `SYS_EROFS`
+  before the backend (`sys_fs_begin_w`).  The app saves the table as
+  `MOUNTS.CFG` (`"NBMNT1\n\0"` + records) on the `NEBOSDATA` volume; the
+  `bootscan` thread calls `sys_mount_load_config()` after `vfs_mount_all()`
+  and applies `/data/MOUNTS.CFG`.  The data volume itself is not editable
+  from the app (it stores the file).  Format and install run the kernel
+  commands with `-y` (which skips their raw-keyboard y/n prompt; the app
+  confirms in its own dialog) through `SYS_SPAWN_KCMD` on a private tty and
+  show the output; success is read from the `done` line.
 - **`/share`**: `share.c/h` — writable, page-backed in-memory filesystem for
 - **`/share`**: `share.c/h` — writable, page-backed in-memory filesystem for
   network uploads (the netbooted board may have no writable disk).  Entries
   network uploads (the netbooted board may have no writable disk).  Entries
   are full relative paths (like the pack ramfs) plus explicit directory
   are full relative paths (like the pack ramfs) plus explicit directory
@@ -1387,7 +1412,16 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   pseudo-classes such as `:hover` never match and their rules are dropped
   pseudo-classes such as `:hover` never match and their rules are dropped
   at parse time), `::before`/`::after` generated text (`content` strings,
   at parse time), `::before`/`::after` generated text (`content` strings,
   `attr()`, quotes; cascaded as an anonymous inline box per element by
   `attr()`, quotes; cascaded as an anonymous inline box per element by
-  `br_pseudo()` and emitted as literal runs, so scripts never see it),
+  `br_pseudo()` and emitted as literal runs, so scripts never see it;
+  an empty `content: ""` box with a size and a background -- an icon --
+  is one `RUN_PBOX` run that fills the background colour and draws a
+  `data:` background image at its `background-size`/`-position`, clipped
+  to the box and rotated by `transform`; a flex row reserves its first/
+  last column for such boxes (`margin-left: auto` pushes one to the right
+  edge), `intrinsic_width()` counts them, and an empty `<button>` whose
+  icon is such a box is laid out as an element),
+  `data:` URLs (percent-encoded or base64; `data_url_decode()`) for
+  background images, `<img>` sources and those icon boxes,
   colours/`rgba()`, legacy `bgcolor` attributes, solid backgrounds and
   colours/`rgba()`, legacy `bgcolor` attributes, solid backgrounds and
   `background-image` with
   `background-image` with
   `repeat`/`position`/`size` (`auto` keeps the legacy stretch, `cover` and
   `repeat`/`position`/`size` (`auto` keeps the legacy stretch, `cover` and
@@ -1420,8 +1454,29 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   120-byte cap silently dropped every responsive variant past it.  A
   120-byte cap silently dropped every responsive variant past it.  A
   stylesheet whose helper fetch fails is retried once synchronously
   stylesheet whose helper fetch fails is retried once synchronously
   (`[css] retry` on the log) instead of leaving the page unstyled.
   (`[css] retry` on the log) instead of leaving the page unstyled.
-  `@media (min-width/max-width)` blocks are evaluated against the browser
-  canvas width when the page is parsed; `@import`/`@keyframes` are skipped.
+  `@media` blocks (`min-width`/`max-width` and the `(width >= 997px)` range
+  form) are evaluated against the browser canvas width when the page is
+  parsed, and `window.matchMedia()` gives scripts the same answers
+  (`br_media_matches()`).  `@layer` blocks are parsed and cascade in layer
+  order (`br_rule.layer`, ranked by first appearance including `@layer a,
+  b;` statements): precedence is a band per layer above raw specificity
+  (`cascade_rank()`), unlayered rules above every layer, the `style`
+  attribute above all, and `!important` reverses the layer order.
+  Docusaurus/Infima ships its whole sheet in layers.  `@supports` blocks
+  apply unless negated or asking for `selector()`; `@import`/`@keyframes`
+  are skipped.  After `var()` expansion every `calc()` in a declaration
+  block is folded (`css_fold_calc()`: `*`, `/`, nested parentheses, %,
+  px, rem/em at the root size, vw, unitless) into a plain length or the
+  `calc(P% + Npx)` form the property parsers read; `var()` substitution
+  keeps token boundaries, so minified `font:var(--w)var(--size)` cannot
+  glue into one token.  A rule that uses or defines a custom property some
+  non-root rule also defines (`BR_VAR_SCOPED`) keeps its raw block and is
+  re-expanded per element in `br_style_for()` against the definitions of
+  the element's own matched rules (`css_env_build()`; Infima's `.col--3`
+  sets `--ifm-col-width`, `.col[class*=col--]` uses it).  The flex shorthand
+  and `flex-basis` map a definite basis onto the item width, and flex items
+  are clamped by `min-width`/`max-width` (a capped growing item leaves its
+  space to the others).
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   (the browser fetches remote faces before layout).  The compatibility
   (the browser fetches remote faces before layout).  The compatibility
   ladder is `testdata/browser/compat/`
   ladder is `testdata/browser/compat/`
@@ -1735,7 +1790,24 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   is ~3.7 KB.  Inline `<style>` text is collected up to 256 KB
   is ~3.7 KB.  Inline `<style>` text is collected up to 256 KB
   (`BR_CSS_SIZE`; google.com inlines ~94 KB).  Media features with a known
   (`BR_CSS_SIZE`; google.com inlines ~94 KB).  Media features with a known
   answer are evaluated (`forced-colors: none`, normal contrast,
   answer are evaluated (`forced-colors: none`, normal contrast,
-  `hover: hover`, `pointer: fine`); unknown ones stay true.  Flex/grid,
+  `hover: hover`, `pointer: fine`, `prefers-reduced-motion: no-preference`,
+  `display-mode: browser`); unknown ones stay true.  When the page starts
+  with `<html>` (no whitespace after the doctype) that element is node 0:
+  layout inherits from its cascade (`root_style()`), and selector
+  combinators, `style_for_node()` and the preloader's `node_hidden()` treat
+  node 0 as its children's parent (`sel_parent()`).  An unquoted attribute
+  value ending in `/` (`<a href=/x/>`) does not self-close the tag, and
+  `/>` is ignored on non-void HTML elements outside SVG.  `<svg>` is inline
+  by default (an inline-block holding only an `<svg>` icon stays in the
+  text line); an empty `<button>` has no label.  `font-weight` 500 picks
+  the regular face (CSS matching with only 400/700 faces), 600+ the bold
+  one.  Alpha without a leading zero (`rgba(0,0,0,.05)`) is parsed.  A
+  float's `width` is its content box (margins, and padding/border unless
+  `border-box`, are added to the column).  A fixed box's deferred
+  background fill covers only its own columns, and a positioned box pushed
+  past its containing block (`left: 100%`) keeps a one-line width instead
+  of collapsing to 8 px.  Percentage `<img>` heights resolve against the
+  containing block's definite height.  Flex/grid,
   basic tables, inline SVG and CSS
   basic tables, inline SVG and CSS
   variables are supported (see known issue 22 for the remaining CSS and DOM
   variables are supported (see known issue 22 for the remaining CSS and DOM
   gaps).  `SYS_SPAWN`
   gaps).  `SYS_SPAWN`
@@ -1827,7 +1899,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   files in the browser (an SVG body is parsed into a detached subtree of
   files in the browser (an SVG body is parsed into a detached subtree of
   the page and drawn by the inline-SVG renderer into an RGB565 + alpha
   the page and drawn by the inline-SVG renderer into an RGB565 + alpha
   bitmap: intrinsic size from width/height/viewBox, drawn at up to 4x for
   bitmap: intrinsic size from width/height/viewBox, drawn at up to 4x for
-  small icons, `currentColor` black, no `<text>`) and
+  small icons (without a viewBox the user units are scaled to that
+  raster), `currentColor` black, no `<text>`) and
   animated GIF frames are supported as well; arithmetic JPEG/CMYK are not.
   animated GIF frames are supported as well; arithmetic JPEG/CMYK are not.
   The viewer, `ucmd_view.c`, owns a canvas window and reads its file through
   The viewer, `ucmd_view.c`, owns a canvas window and reads its file through
   `SYS_READ_FILE`; it uses fit/1:1/zoom/pan, shows format/dimensions/size,
   `SYS_READ_FILE`; it uses fit/1:1/zoom/pan, shows format/dimensions/size,
@@ -1897,7 +1970,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>`,
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
-`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `meminfo`, `top`, `sched`, `format <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk), `install <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 [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `disks` (the ngui storage manager), `meminfo`, `top`, `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]`, `backlight [0-100|up|down]` (also `probe`), `keytest` (raw scancode tap), `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
 `ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `backlight [0-100|up|down]` (also `probe`), `keytest` (raw scancode tap), `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
 
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
@@ -1929,7 +2002,7 @@ nested). `/` is the read-only VFS root listing the mounted filesystems (the
 filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 
 
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`, `top`, `spin`,
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`, `top`, `spin`,
-`clock`, `colors`, `settime`, `date`, `files`, `gallery`, `chat`, `view`, `sensors`, `powerinfo`, `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
+`clock`, `colors`, `settime`, `date`, `files`, `disks`, `gallery`, `chat`, `view`, `sensors`, `powerinfo`, `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
 `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
 `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
 `netreset`, `websrv`, `curl`, `wifi`, `netmenu`, `solitaire` and `hello` are files in `/ramfs/bin`
 `netreset`, `websrv`, `curl`, `wifi`, `netmenu`, `solitaire` and `hello` are files in `/ramfs/bin`
 (packed by the Makefile), found through the shell's PATH; the rest are kernel
 (packed by the Makefile), found through the shell's PATH; the rest are kernel
@@ -2539,9 +2612,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
     pack is out of the kernel now; after the WiFi/netmenu/install code and the
     pack is out of the kernel now; after the WiFi/netmenu/install code and the
     per-connection TCP queues moved to the frame allocator, `_end` is
     per-connection TCP queues moved to the frame allocator, `_end` is
-    ~0x8eb20 (with the 6200/6300 DVM layer, the RTL8821AU driver, the
+    ~0x8ef20 (with the 6200/6300 DVM layer, the RTL8821AU driver, the
     8×16 boot-console font, the IOAPIC/AMD-thermal additions and the safe
     8×16 boot-console font, the IOAPIC/AMD-thermal additions and the safe
-    AMD MTRR path and the 32x32 cursor save buffer), ~5 KB below the `0x90000` wall, and the remaining limit
+    AMD MTRR path, the 32x32 cursor save buffer and the mount-policy table), ~4 KB below the `0x90000` wall, and the remaining limit
     is kernel code/buffers only.
     is kernel code/buffers only.
 
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
 22. Browser limits: JavaScript runs inline and external classic scripts on
@@ -2573,8 +2646,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     `document.implementation.createDocument`, canvas/2D contexts, user-state
     `document.implementation.createDocument`, canvas/2D contexts, user-state
     pseudo-classes (`:hover`, `:focus`, ...), `:nth-child(... of S)` and
     pseudo-classes (`:hover`, `:focus`, ...), `:nth-child(... of S)` and
     pseudo-elements other than `::before`/`::after`; generated content is
     pseudo-elements other than `::before`/`::after`; generated content is
-    text only (no box, background, counters or `url()` images, and
-    pseudo-elements inside flex/grid containers are not emitted).
+    text, or an empty icon box with a background colour and a `data:`
+    background image (no borders/padding, counters, `content: url()` or
+    remote background images on pseudo-elements; text pseudo-elements
+    inside flex/grid containers are not emitted).
     `getComputedStyle` returns the engine's
     `getComputedStyle` returns the engine's
     known properties (display, box metrics, colours, fonts, borders,
     known properties (display, box metrics, colours, fonts, borders,
     background and transform state), and `style`/`dataset` cover inline state rather
     background and transform state), and `style`/`dataset` cover inline state rather
@@ -2592,7 +2667,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     win, a component-level definition applies only while no other rule
     win, a component-level definition applies only while no other rule
     gives the name a different value, otherwise `var()` uses its fallback
     gives the name a different value, otherwise `var()` uses its fallback
     -- Font Awesome's competing `--fa-width` values made every icon 100%
     -- Font Awesome's competing `--fa-width` values made every icon 100%
-    wide), percentage heights (against the containing block's definite
+    wide; rules touching such names are re-expanded per element against
+    that element's own matched rules, but values are not inherited from
+    ancestors' rules), percentage heights (against the containing block's definite
     height, `lay.cb_h`, with the viewport for `html`/`body`; used as a
     height, `lay.cb_h`, with the viewport for `html`/`body`; used as a
     minimum since there are no scrollable inner boxes, and auto when the
     minimum since there are no scrollable inner boxes, and auto when the
     containing height is indefinite -- they used to resolve against the
     containing height is indefinite -- they used to resolve against the
@@ -2670,8 +2747,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     caps.  Video/audio elements are placeholders.  Bare `&` characters are
     caps.  Video/audio elements are placeholders.  Bare `&` characters are
     consumed by the entity decoder instead of looping forever.  A page with
     consumed by the entity decoder instead of looping forever.  A page with
-    no `html`/`body` background still gets the dark default canvas, so dark
-    text can be hard to read; pages with their own background are unaffected.
+    no `html`/`body` background gets the dark default canvas unless its
+    computed text colour is dark, in which case the canvas is white like a
+    normal browser's.  `@layer` ranks nested layers flat by first
+    appearance (a parent layer's own rules lose to its sublayers, the spec
+    says the reverse).
     TLS is phase A (no certificate verification); `https` is shown as
     TLS is phase A (no certificate verification); `https` is shown as
     unverified.  The disk app
     unverified.  The disk app
     is loaded from FAT at spawn time, so a cold floppy start takes a few
     is loaded from FAT at spawn time, so a cold floppy start takes a few
@@ -2944,4 +3024,4 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     drivers and only Cirrus and the software cursor are tested in QEMU.
     drivers and only Cirrus and the software cursor are tested in QEMU.
     The cursor shape follows the WM poll, so it can lag the pointer by
     The cursor shape follows the WM poll, so it can lag the pointer by
     one WM loop.  The 4 KB save buffer and the cursor code (~8 KB in all) leave `_end` at
     one WM loop.  The 4 KB save buffer and the cursor code (~8 KB in all) leave `_end` at
-    ~0x8eb20, ~5 KB below the `0x90000` wall.
+    ~0x8ef20, ~4 KB below the `0x90000` wall.

+ 5 - 1
Makefile

@@ -386,7 +386,7 @@ UCMDS = cpuinfo ls cat save rm mkdir rmdir \
         netconsole netregs netloop netreset net100 netmac curl \
         netconsole netregs netloop netreset net100 netmac curl \
         files view sensors powerinfo mines solitaire \
         files view sensors powerinfo mines solitaire \
         settime date gallery websrv browser chat \
         settime date gallery websrv browser chat \
-        wifi netmenu
+        wifi netmenu disks
 
 
 app = $(U)/apps/$(1)/ucmd_$(1)
 app = $(U)/apps/$(1)/ucmd_$(1)
 UCMD_ELFS = $(foreach a,$(UCMDS),$(call app,$(a)).elf)
 UCMD_ELFS = $(foreach a,$(UCMDS),$(call app,$(a)).elf)
@@ -454,6 +454,10 @@ $(call app,wifi).elf: $(call app,wifi).o $(WIFI_STORE) $(WPA_OBJS) $(UL)/tls/tls
 $(call app,netmenu).elf: $(call app,netmenu).o $(WIFI_STORE) $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
 $(call app,netmenu).elf: $(call app,netmenu).o $(WIFI_STORE) $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
 	$(ULINK)
 	$(ULINK)
 
 
+# Storage manager: devices, partitions, mount policies, format/install.
+$(call app,disks).elf: $(call app,disks).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
+	$(ULINK)
+
 # chat: native Cloudron Chat client (ngui + uhttp/TLS + cookie jar + JSON +
 # chat: native Cloudron Chat client (ngui + uhttp/TLS + cookie jar + JSON +
 # the native OIDC login driver).
 # the native OIDC login driver).
 CHAT_LOGIN = $(U)/apps/chat/chat_login.o
 CHAT_LOGIN = $(U)/apps/chat/chat_login.o

+ 29 - 2
docs/filesystems.html

@@ -59,6 +59,7 @@
 			<li><a href="#ramfs">ramfs &amp; the command pack</a></li>
 			<li><a href="#ramfs">ramfs &amp; the command pack</a></li>
 			<li><a href="#share">/share</a></li>
 			<li><a href="#share">/share</a></li>
 			<li><a href="#data">/data</a></li>
 			<li><a href="#data">/data</a></li>
+			<li><a href="#disks">Storage manager &amp; mount policies</a></li>
 			<li><a href="#sysfs">File service ABI</a></li>
 			<li><a href="#sysfs">File service ABI</a></li>
 			<li><a href="#webdav">WebDAV</a></li>
 			<li><a href="#webdav">WebDAV</a></li>
 			<li><a href="#capacity">Capacity chart</a></li>
 			<li><a href="#capacity">Capacity chart</a></li>
@@ -315,10 +316,32 @@
 			<li><strong>WiFi credentials.</strong> A successful WiFi connect adds the network to a most-recently-used list of up to 16 SSID/passphrase records in <code>WIFI.CFG</code>; the boot-time <code>wifi autoconnect</code> helper scans and joins the strongest saved network it finds. <code>wifi saved</code> lists them and <code>wifi forget [ssid]</code> removes one. The file is plaintext and 8.3, like the rest of the volume.</li>
 			<li><strong>WiFi credentials.</strong> A successful WiFi connect adds the network to a most-recently-used list of up to 16 SSID/passphrase records in <code>WIFI.CFG</code>; the boot-time <code>wifi autoconnect</code> helper scans and joins the strongest saved network it finds. <code>wifi saved</code> lists them and <code>wifi forget [ssid]</code> removes one. The file is plaintext and 8.3, like the rest of the volume.</li>
 			<li><strong>Chat session.</strong> The Chat client stores its server and session cookie in <code>CHAT.CFG</code> when this volume is mounted; without it the client falls back to the RAM-only <code>/share/chat.cfg</code>. The cookie is a bearer credential, and the password is never stored.</li>
 			<li><strong>Chat session.</strong> The Chat client stores its server and session cookie in <code>CHAT.CFG</code> when this volume is mounted; without it the client falls back to the RAM-only <code>/share/chat.cfg</code>. The cookie is a bearer credential, and the password is never stored.</li>
 			<li><strong>Fixed start.</strong> P4 starts at LBA 34816. A BIOS-only install leaves the space between P2 and P4 unpartitioned, and a later disk clone does not resize P4.</li>
 			<li><strong>Fixed start.</strong> P4 starts at LBA 34816. A BIOS-only install leaves the space between P2 and P4 unpartitioned, and a later disk clone does not resize P4.</li>
+			<li><strong>Mount settings.</strong> The <code>disks</code> app saves the mount policies in <code>MOUNTS.CFG</code>; the boot scan applies them (see below).</li>
 			<li><strong>No fsck.</strong> Like the other FAT mounts, the volume has no journal, repair tool or permissions; it is writable by every process.</li>
 			<li><strong>No fsck.</strong> Like the other FAT mounts, the volume has no journal, repair tool or permissions; it is writable by every process.</li>
 		</ul>
 		</ul>
 	</section>
 	</section>
 
 
+	<section id="disks">
+		<h2>Storage manager &amp; mount policies</h2>
+		<p>
+			<code>disks</code> (<span class="file">ucmd_disks.c</span>, also the
+			top-bar <em>Disks</em> launcher and the <em>Disks</em> button in
+			<code>files</code>) is the ngui front end to
+			storage; <code>storage</code> stays the text view. It lists every
+			present block device (<code>SYS_BLKDEV</code>), reads sector 0 and
+			each partition's first sector through <code>SYS_BLK_READ</code>
+			(119) to show the MBR, the NebOS boot partition, the rootfs, FAT
+			types and labels, and shows all mounts with their source volume.
+			Floppies are never read raw (no media means seconds of retries).
+		</p>
+		<ul>
+			<li><strong>Mount policies.</strong> <code>SYS_MOUNT_CTL</code> (120) sets or reads a <code>struct sys_mount_policy</code>: a mount name, <code>SYS_MOUNT_RO</code> and <code>SYS_MOUNT_NOAUTO</code>, keyed by the volume's default name (<code>ide0p1</code>, or <code>usb0</code>/<code>floppy1</code> for a whole-device volume). <span class="file">vfs.c</span> keeps up to 16 in RAM; <code>mount_one()</code> honours them on every scan and a set remounts the device at once. Names are 1–15 of <code>[a-z0-9_-]</code>; <code>ramfs</code>/<code>share</code> are refused and a name already in use falls back to the default.</li>
+			<li><strong>Read-only.</strong> Write, unlink, mkdir and rmdir on a mount with <code>ro</code> set return <code>SYS_EROFS</code> before reaching the backend (this also covers the ramfs).</li>
+			<li><strong>Persistence.</strong> The app writes the table to <code>MOUNTS.CFG</code> on the <code>NEBOSDATA</code> volume (<code>"NBMNT1\n\0"</code> then the records); the <code>bootscan</code> thread reads <code>/data/MOUNTS.CFG</code> after <code>vfs_mount_all()</code> and applies it. Without <code>/data</code> the settings last until reboot. The data volume itself cannot be edited, since it stores the file.</li>
+			<li><strong>Format and install.</strong> The app confirms in its own dialog, then runs <code>format -y &lt;dev&gt; [size]</code> or <code>install -y &lt;dev&gt; [self]</code> through <code>SYS_SPAWN_KCMD</code> on a private tty and shows the output; <code>-y</code> skips the commands' raw-keyboard y/n prompt. An installed disk offers <em>Update</em> (<code>self</code>) and <em>Reinstall</em> (clone), a blank one <em>Install</em>.</li>
+		</ul>
+	</section>
+
 	<section id="sysfs">
 	<section id="sysfs">
 		<h2>File service ABI</h2>
 		<h2>File service ABI</h2>
 		<p>
 		<p>
@@ -326,7 +349,10 @@
 			<code>SYS_WRITE_FILE</code>, <code>SYS_LIST_DIR</code>,
 			<code>SYS_WRITE_FILE</code>, <code>SYS_LIST_DIR</code>,
 			<code>SYS_LIST_MOUNTS</code>, <code>SYS_UNLINK</code>,
 			<code>SYS_LIST_MOUNTS</code>, <code>SYS_UNLINK</code>,
 			<code>SYS_MKDIR</code>, <code>SYS_RMDIR</code>,
 			<code>SYS_MKDIR</code>, <code>SYS_RMDIR</code>,
-			<code>SYS_FS_STAT</code> and <code>SYS_GETCWD</code>.
+			<code>SYS_FS_STAT</code> and <code>SYS_GETCWD</code>; the storage
+			manager adds <code>SYS_BLK_READ</code> and
+			<code>SYS_MOUNT_CTL</code>. <code>struct sys_mount</code> carries the
+			source device and the volume key.
 			<span class="file">sys_fs.c</span> validates the path, resolves it
 			<span class="file">sys_fs.c</span> validates the path, resolves it
 			against the caller's cwd, calls the VFS and maps backend errors to
 			against the caller's cwd, calls the VFS and maps backend errors to
 			the stable <code>SYS_E*</code> values.
 			the stable <code>SYS_E*</code> values.
@@ -420,7 +446,8 @@
 			<li><code>kernel/fat.c</code>, <code>kernel/fat.h</code> — FAT12/16/32</li>
 			<li><code>kernel/fat.c</code>, <code>kernel/fat.h</code> — FAT12/16/32</li>
 			<li><code>kernel/ramfs.c</code>, <code>kernel/ramfs.h</code> — pack inflate</li>
 			<li><code>kernel/ramfs.c</code>, <code>kernel/ramfs.h</code> — pack inflate</li>
 			<li><code>kernel/share.c</code>, <code>kernel/share.h</code> — /share</li>
 			<li><code>kernel/share.c</code>, <code>kernel/share.h</code> — /share</li>
-			<li><code>kernel/sys_fs.c</code> — ring-3 file service</li>
+			<li><code>kernel/sys_fs.c</code> — ring-3 file service, raw sector read, mount policies and <code>MOUNTS.CFG</code> loader</li>
+			<li><code>user/apps/disks/ucmd_disks.c</code> — storage manager app</li>
 			<li><code>tools/mkpack.c</code>, <code>tools/mkrootfs.c</code>, <code>include/rootfs.h</code></li>
 			<li><code>tools/mkpack.c</code>, <code>tools/mkrootfs.c</code>, <code>include/rootfs.h</code></li>
 			<li><code>common/inflate.c</code> — DEFLATE/gzip</li>
 			<li><code>common/inflate.c</code> — DEFLATE/gzip</li>
 			<li><code>user/apps/websrv/ucmd_websrv.c</code> — HTTP/WebDAV</li>
 			<li><code>user/apps/websrv/ucmd_websrv.c</code> — HTTP/WebDAV</li>

+ 4 - 0
docs/graphics.html

@@ -406,6 +406,10 @@
 				<h3>chat</h3>
 				<h3>chat</h3>
 				<p>Cloudron Chat client: native OIDC login, channel/DM sidebar with unread badges and presence dots, a wrapped timeline with avatars, day headers and reply snippets, a composer and browse/new-DM pickers. Media downloads use a warm <code>curl __serve</code> helper; message images are also decoded there (480&times;320 bounds), while avatars decode at 32&times;32 on the chat thread, so the window keeps drawing. Saved sessions resume from <code>/data</code>. The top-bar <em>Chat</em> button launches it.</p>
 				<p>Cloudron Chat client: native OIDC login, channel/DM sidebar with unread badges and presence dots, a wrapped timeline with avatars, day headers and reply snippets, a composer and browse/new-DM pickers. Media downloads use a warm <code>curl __serve</code> helper; message images are also decoded there (480&times;320 bounds), while avatars decode at 32&times;32 on the chat thread, so the window keeps drawing. Saved sessions resume from <code>/data</code>. The top-bar <em>Chat</em> button launches it.</p>
 			</div>
 			</div>
+			<div class="card">
+				<h3>disks</h3>
+				<p>Storage manager: devices, MBR partitions and mounts, per-volume mount name, read-only and automount, and Format / Install NebOS dialogs that show the kernel command's output. The top-bar <em>Disks</em> button and the <em>Disks</em> button in <code>files</code> launch it.</p>
+			</div>
 			<div class="card">
 			<div class="card">
 				<h3>solitaire</h3>
 				<h3>solitaire</h3>
 				<p>Klondike with drag-and-drop, draw-one/three, scoring, animation and pixel-art cards on the canvas service.</p>
 				<p>Klondike with drag-and-drop, draw-one/three, scoring, animation and pixel-art cards on the canvas service.</p>

+ 1 - 1
docs/index.html

@@ -207,7 +207,7 @@
 			</a>
 			</a>
 			<a class="card" href="filesystems.html">
 			<a class="card" href="filesystems.html">
 				<h3>Storage &amp; filesystems</h3>
 				<h3>Storage &amp; filesystems</h3>
-				<p>VFS vtable, FAT12/16/32, the ramfs pack pipeline, persistent <code>/data</code>, <code>/share</code> and the WebDAV view of the VFS.</p>
+				<p>VFS vtable, FAT12/16/32, the ramfs pack pipeline, persistent <code>/data</code>, <code>/share</code>, the <code>disks</code> manager with its mount policies and the WebDAV view of the VFS.</p>
 				<span class="card-tag">filesystems.html</span>
 				<span class="card-tag">filesystems.html</span>
 			</a>
 			</a>
 			<a class="card" href="graphics.html">
 			<a class="card" href="graphics.html">

ファイルの差分が大きいため隠しています
+ 5 - 1
docs/userland.html


+ 31 - 0
include/syscall.h

@@ -121,6 +121,17 @@
 				  cursor (enum cursor_shape, cursor_abi.h) */
 				  cursor (enum cursor_shape, cursor_abi.h) */
 #define SYS_WM_SET_CURSOR 118 /* (int shape | CURSOR_NO_OVERRIDE) -> 0/-1;
 #define SYS_WM_SET_CURSOR 118 /* (int shape | CURSOR_NO_OVERRIDE) -> 0/-1;
 				  WM only: cursor for its chrome/drags */
 				  WM only: cursor for its chrome/drags */
+
+/* Storage management (see sys_fs.c, the `disks` app).  SYS_BLK_READ reads one
+ * raw 512-byte sector of block device `index` (SYS_BLKDEV order) so ring 3
+ * can show the partition table; it waits for a format/rescan like a file
+ * operation.  SYS_MOUNT_CTL sets the per-volume mount policy (mount point
+ * name, read-only, not mounted) and remounts the device at once.  Policies
+ * live in RAM; the boot scan loads SYS_MOUNT_CFG_PATH from /data. */
+#define SYS_BLK_READ      119 /* (index, lba, void *buf512) -> 0/<0 */
+#define SYS_MOUNT_CTL     120 /* (op, a2, a3) */
+#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 */
 #define SYS_BACKLIGHT     113 /* (int op, int value) -> percent / -1.
 #define SYS_BACKLIGHT     113 /* (int op, int value) -> percent / -1.
 				  op 0 = get, 1 = set (5..100), 2 = step */
 				  op 0 = get, 1 = set (5..100), 2 = step */
 #define SYS_BACKLIGHT_GET  0
 #define SYS_BACKLIGHT_GET  0
@@ -298,8 +309,28 @@ struct sys_mount {
 	unsigned int total_sectors;
 	unsigned int total_sectors;
 	char label[12];
 	char label[12];
 	unsigned int ro;
 	unsigned int ro;
+	char dev[16];           /* source block device ("" for ramfs/share) */
+	char key[16];           /* default name of the volume ("ide0p1"); the
+				   mount-policy key */
+};
+
+/* Mount policy of one volume, keyed by its default mount name: "ide0p1" for
+ * MBR partition 1 of ide0, "floppy0"/"usb0" for a whole-device volume.  A
+ * policy with no name and no flags is removed. */
+#define SYS_MOUNT_NOAUTO 0x1    /* do not mount this volume */
+#define SYS_MOUNT_RO     0x2    /* mount read-only */
+#define SYS_MOUNT_POLICY_MAX 16
+struct sys_mount_policy {
+	char key[16];
+	char name[16];          /* mount point override; "" = default */
+	unsigned int flags;     /* SYS_MOUNT_* */
 };
 };
 
 
+/* Saved policies: an 8-byte "NBMNT1\n\0" header and up to
+ * SYS_MOUNT_POLICY_MAX struct sys_mount_policy records. */
+#define SYS_MOUNT_CFG_PATH  "/data/MOUNTS.CFG"
+#define SYS_MOUNT_CFG_MAGIC "NBMNT1\n"
+
 /* Result of SYS_FS_STAT. */
 /* Result of SYS_FS_STAT. */
 struct sys_stat {
 struct sys_stat {
 	unsigned int size;
 	unsigned int size;

+ 5 - 1
include/wm_abi.h

@@ -46,9 +46,13 @@
 #define WM_BTN_CHAT_Y WM_BTN_Y
 #define WM_BTN_CHAT_Y WM_BTN_Y
 #define WM_BTN_CHAT_W 56
 #define WM_BTN_CHAT_W 56
 #define WM_BTN_CHAT_H WM_BTN_H
 #define WM_BTN_CHAT_H WM_BTN_H
+#define WM_BTN_DISKS_X (WM_BTN_CHAT_X + WM_BTN_CHAT_W + WM_BTN_GAP)
+#define WM_BTN_DISKS_Y WM_BTN_Y
+#define WM_BTN_DISKS_W 64
+#define WM_BTN_DISKS_H WM_BTN_H
 /* Right edge of the launcher buttons: the notice text and the right-aligned
 /* Right edge of the launcher buttons: the notice text and the right-aligned
  * status (clock, sensors, power) never draw left of it. */
  * status (clock, sensors, power) never draw left of it. */
-#define WM_BTN_END_X (WM_BTN_CHAT_X + WM_BTN_CHAT_W)
+#define WM_BTN_END_X (WM_BTN_DISKS_X + WM_BTN_DISKS_W)
 #define WM_TITLE_H 22
 #define WM_TITLE_H 22
 #define WM_CLOSE_W 16
 #define WM_CLOSE_W 16
 
 

+ 15 - 1
kernel/format.c

@@ -106,7 +106,7 @@ void program_format_main(const char *arg)
 	char name[16];
 	char name[16];
 	const char *p;
 	const char *p;
 	unsigned int i = 0, j, limit = 0;
 	unsigned int i = 0, j, limit = 0;
-	int k, r;
+	int k, r, yes = 0;
 
 
 	if (format_busy && process_is_alive(format_owner_pid)) {
 	if (format_busy && process_is_alive(format_owner_pid)) {
 		io_putstr("format: another format is in progress\n");
 		io_putstr("format: another format is in progress\n");
@@ -121,6 +121,15 @@ void program_format_main(const char *arg)
 
 
 	while (arg[i] == ' ')
 	while (arg[i] == ' ')
 		i++;
 		i++;
+	/* `-y` skips the prompt: the `disks` app confirms in its own dialog
+	 * and runs the command on a private tty with no keyboard. */
+	if (arg[i] == '-' && arg[i + 1] == 'y' &&
+	    (arg[i + 2] == ' ' || !arg[i + 2])) {
+		yes = 1;
+		i += 2;
+		while (arg[i] == ' ')
+			i++;
+	}
 	j = 0;
 	j = 0;
 	while (arg[i] && arg[i] != ' ' && j < sizeof(name) - 1)
 	while (arg[i] && arg[i] != ' ' && j < sizeof(name) - 1)
 		name[j++] = arg[i++];
 		name[j++] = arg[i++];
@@ -201,6 +210,10 @@ void program_format_main(const char *arg)
 		io_putstr(" (whole disk)");
 		io_putstr(" (whole disk)");
 	}
 	}
 	io_putstr("? this destroys all data (y/n) ");
 	io_putstr("? this destroys all data (y/n) ");
+	if (yes) {
+		io_putstr("y\n");
+		goto confirmed;
+	}
 	vga_fb_flush();
 	vga_fb_flush();
 
 
 	/* Keep the WM from draining the keyboard ring while we poll for the
 	/* Keep the WM from draining the keyboard ring while we poll for the
@@ -224,6 +237,7 @@ void program_format_main(const char *arg)
 	io_set_raw_keyboard(0);
 	io_set_raw_keyboard(0);
 	io_putchar('\n');
 	io_putchar('\n');
 
 
+confirmed:
 	io_putstr("formatting ");
 	io_putstr("formatting ");
 	io_putstr(d->name);
 	io_putstr(d->name);
 	io_putstr("...\n");
 	io_putstr("...\n");

+ 14 - 0
kernel/install.c

@@ -426,6 +426,9 @@ static blk_dev_t *resolve_target(const char *name)
 	return found;
 	return found;
 }
 }
 
 
+/* Set by `install -y ...` (the `disks` app confirms in its own dialog). */
+static int install_yes;
+
 static int ask_continue(blk_dev_t *target, int have_data)
 static int ask_continue(blk_dev_t *target, int have_data)
 {
 {
 	int k;
 	int k;
@@ -439,6 +442,10 @@ static int ask_continue(blk_dev_t *target, int have_data)
 		io_putstr(target->name);
 		io_putstr(target->name);
 		io_putstr("? (y/n) ");
 		io_putstr("? (y/n) ");
 	}
 	}
+	if (install_yes) {
+		io_putstr("y\n");
+		return 1;
+	}
 	vga_fb_flush();
 	vga_fb_flush();
 
 
 	/* The confirmation loop yields so the kworker can poll the USB HID
 	/* The confirmation loop yields so the kworker can poll the USB HID
@@ -839,6 +846,13 @@ void program_install_main(const char *arg)
 	i = 0;
 	i = 0;
 	while (arg[i] == ' ')
 	while (arg[i] == ' ')
 		i++;
 		i++;
+	install_yes = 0;
+	if (arg[i] == '-' && arg[i + 1] == 'y' && arg[i + 2] == ' ') {
+		install_yes = 1;
+		i += 3;
+		while (arg[i] == ' ')
+			i++;
+	}
 	j = 0;
 	j = 0;
 	while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
 	while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
 		target_name[j++] = arg[i++];
 		target_name[j++] = arg[i++];

+ 2 - 0
kernel/kernel.c

@@ -26,6 +26,7 @@
 #include "rndis.h"
 #include "rndis.h"
 #include "rtl8821au.h"
 #include "rtl8821au.h"
 #include "sched.h"
 #include "sched.h"
+#include "sys_fs.h"
 #include "sys_wm.h"
 #include "sys_wm.h"
 #include "thermal.h"
 #include "thermal.h"
 #include "timer.h"
 #include "timer.h"
@@ -407,6 +408,7 @@ static volatile int boot_dhcp_done;
 static void boot_storage_main(void)
 static void boot_storage_main(void)
 {
 {
 	vfs_mount_all();
 	vfs_mount_all();
+	sys_mount_load_config();
 	boot_trace("storage");
 	boot_trace("storage");
 	/* As before, WiFi only starts after the boot DHCP attempt on the
 	/* As before, WiFi only starts after the boot DHCP attempt on the
 	 * wired NIC, so two DHCP clients never overlap. */
 	 * wired NIC, so two DHCP clients never overlap. */

+ 104 - 4
kernel/sys_fs.c

@@ -19,6 +19,8 @@
 
 
 #include "syscall.h"
 #include "syscall.h"
 #include "fatformat.h"
 #include "fatformat.h"
+#include "kmalloc.h"
+#include "paging.h"
 #include "process.h"
 #include "process.h"
 #include "vfs.h"
 #include "vfs.h"
 
 
@@ -169,6 +171,20 @@ static int sys_fs_begin(const char *path, struct vfs_mount **m,
 	return 0;
 	return 0;
 }
 }
 
 
+/* sys_fs_begin() for a modifying operation: a read-only mount (the ramfs,
+ * or a volume with the SYS_MOUNT_RO policy) refuses it up front. */
+static int sys_fs_begin_w(const char *path, struct vfs_mount **m,
+			  char *rel, unsigned int rel_size)
+{
+	int r = sys_fs_begin(path, m, rel, rel_size);
+
+	if (r == 0 && (*m)->ro) {
+		sys_fs_exit();
+		return SYS_EROFS;
+	}
+	return r;
+}
+
 int sys_read_file(const char *path, void *buf, unsigned int max)
 int sys_read_file(const char *path, void *buf, unsigned int max)
 {
 {
 	struct vfs_mount *m;
 	struct vfs_mount *m;
@@ -188,7 +204,7 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
 {
 {
 	struct vfs_mount *m;
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
 	char rel[VFS_PATH_MAX];
-	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
+	int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
 
 
 	if (r != 0)
 	if (r != 0)
 		return r;
 		return r;
@@ -203,7 +219,7 @@ int sys_unlink(const char *path)
 {
 {
 	struct vfs_mount *m;
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
 	char rel[VFS_PATH_MAX];
-	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
+	int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
 
 
 	if (r != 0)
 	if (r != 0)
 		return r;
 		return r;
@@ -218,7 +234,7 @@ int sys_mkdir(const char *path)
 {
 {
 	struct vfs_mount *m;
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
 	char rel[VFS_PATH_MAX];
-	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
+	int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
 
 
 	if (r != 0)
 	if (r != 0)
 		return r;
 		return r;
@@ -233,7 +249,7 @@ int sys_rmdir(const char *path)
 {
 {
 	struct vfs_mount *m;
 	struct vfs_mount *m;
 	char rel[VFS_PATH_MAX];
 	char rel[VFS_PATH_MAX];
-	int r = sys_fs_begin(path, &m, rel, sizeof(rel));
+	int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
 
 
 	if (r != 0)
 	if (r != 0)
 		return r;
 		return r;
@@ -292,9 +308,93 @@ int sys_list_mounts(unsigned int index, struct sys_mount *out)
 	out->total_sectors = vfs_mounts[index].total_sectors;
 	out->total_sectors = vfs_mounts[index].total_sectors;
 	copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
 	copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
 	out->ro = (unsigned int)vfs_mounts[index].ro;
 	out->ro = (unsigned int)vfs_mounts[index].ro;
+	copy_bounded(out->dev, vfs_mounts[index].dev, sizeof(out->dev));
+	copy_bounded(out->key, vfs_mounts[index].key, sizeof(out->key));
 	return 1;
 	return 1;
 }
 }
 
 
+/* One raw sector of a block device for the `disks` partition view.  Read
+ * into a kernel buffer (some drivers DMA through their own bounce buffers,
+ * others write the destination directly), then copy out. */
+int sys_blk_read(unsigned int index, unsigned int lba, void *buf)
+{
+	unsigned char sec[512];
+	blk_dev_t *d;
+	unsigned int i;
+	int r;
+
+	if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)buf,
+				  sizeof(sec), 1))
+		return SYS_EINVAL;
+	sys_fs_enter();
+	if (index >= blk_dev_count) {
+		sys_fs_exit();
+		return SYS_ENODEV;
+	}
+	d = &blk_devs[index];
+	r = SYS_EIO;
+	if (d->present && lba < d->total_sectors && blk_read(d, lba, sec, 1) == 0)
+		r = 0;
+	sys_fs_exit();
+	if (r == 0)
+		for (i = 0; i < sizeof(sec); i++)
+			((unsigned char *)buf)[i] = sec[i];
+	return r;
+}
+
+int sys_mount_ctl(int op, int a2, int a3)
+{
+	struct sys_mount_policy p;
+	int r;
+
+	if (op == SYS_MOUNT_POLICY_GET) {
+		if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a3,
+					  sizeof(p), 1))
+			return SYS_EINVAL;
+		return vfs_policy_get((unsigned int)a2,
+				      (struct sys_mount_policy *)a3);
+	}
+	if (op != SYS_MOUNT_POLICY_SET ||
+	    !paging_user_range_ok(current_proc()->cr3, (unsigned int)a2,
+				  sizeof(p), 0))
+		return SYS_EINVAL;
+	p = *(const struct sys_mount_policy *)a2;
+	sys_fs_enter();
+	r = vfs_policy_set(&p) == 0 ? 0 : SYS_EINVAL;
+	sys_fs_exit();
+	return r;
+}
+
+/* Boot: apply the policies saved by the `disks` app on /data (the boot
+ * scan's kernel thread, after vfs_mount_all()).  A missing file is normal. */
+struct mount_cfg {
+	char magic[8];
+	struct sys_mount_policy p[SYS_MOUNT_POLICY_MAX];
+	unsigned char slack[512];   /* a FAT read may fill a whole last sector */
+};
+
+void sys_mount_load_config(void)
+{
+	struct mount_cfg *cfg = (struct mount_cfg *)kmalloc(sizeof(*cfg));
+	int n, i;
+
+	if (!cfg)
+		return;
+	n = sys_read_file(SYS_MOUNT_CFG_PATH, cfg,
+			  sizeof(*cfg) - sizeof(cfg->slack));
+	for (i = 0; n >= 8 && i < 8; i++)
+		if (cfg->magic[i] != SYS_MOUNT_CFG_MAGIC[i])
+			n = 0;
+	n = n >= 8 ? (n - 8) / (int)sizeof(cfg->p[0]) : 0;
+	for (i = 0; i < n; i++) {
+		cfg->p[i].key[sizeof(cfg->p[i].key) - 1] = '\0';
+		sys_fs_enter();
+		vfs_policy_set(&cfg->p[i]);
+		sys_fs_exit();
+	}
+	kfree(cfg);
+}
+
 /* Stat a path.  Unlike read_file this never touches the disk beyond the FAT
 /* Stat a path.  Unlike read_file this never touches the disk beyond the FAT
  * directory walk, so the WebDAV server can probe a collection cheaply. */
  * directory walk, so the WebDAV server can probe a collection cheaply. */
 int sys_stat_path(const char *path, struct sys_stat *out)
 int sys_stat_path(const char *path, struct sys_stat *out)

+ 3 - 0
kernel/sys_fs.h

@@ -19,5 +19,8 @@ int sys_rmdir(const char *path);
 int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out);
 int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out);
 int sys_list_mounts(unsigned int index, struct sys_mount *out);
 int sys_list_mounts(unsigned int index, struct sys_mount *out);
 int sys_stat_path(const char *path, struct sys_stat *out);
 int sys_stat_path(const char *path, struct sys_stat *out);
+int sys_blk_read(unsigned int index, unsigned int lba, void *buf);
+int sys_mount_ctl(int op, int a2, int a3);
+void sys_mount_load_config(void);
 
 
 #endif
 #endif

+ 4 - 0
kernel/syscall.c

@@ -76,6 +76,10 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_storage_info((struct sys_storage_info *)a1);
 		return sys_storage_info((struct sys_storage_info *)a1);
 	case SYS_BLKDEV:
 	case SYS_BLKDEV:
 		return sys_blkdev((unsigned int)a1, (struct sys_blkdev *)a2);
 		return sys_blkdev((unsigned int)a1, (struct sys_blkdev *)a2);
+	case SYS_BLK_READ:
+		return sys_blk_read((unsigned int)a1, (unsigned int)a2, (void *)a3);
+	case SYS_MOUNT_CTL:
+		return sys_mount_ctl(a1, a2, a3);
 	case SYS_NET_INFO:
 	case SYS_NET_INFO:
 		return sys_net_info((struct sys_netinfo *)a1);
 		return sys_net_info((struct sys_netinfo *)a1);
 	case SYS_NET_PRINT_INFO:
 	case SYS_NET_PRINT_INFO:

+ 108 - 4
kernel/vfs.c

@@ -12,6 +12,7 @@
 #include "rootfs.h"
 #include "rootfs.h"
 #include "serial.h"
 #include "serial.h"
 #include "share.h"
 #include "share.h"
+#include "syscall.h"
 #include "timer.h"
 #include "timer.h"
 
 
 vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
@@ -591,6 +592,7 @@ static vfs_mount_t *mount_slot(const char *name, const char *dev)
 				m->dev[i] = dev[i];
 				m->dev[i] = dev[i];
 			m->dev[i] = '\0';
 			m->dev[i] = '\0';
 		}
 		}
+		m->key[0] = '\0';
 		m->mounted = 1;
 		m->mounted = 1;
 		return m;
 		return m;
 	}
 	}
@@ -628,13 +630,106 @@ static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
 	return i == ROOTFS_LBA;
 	return i == ROOTFS_LBA;
 }
 }
 
 
+/* ---- mount policies ---- */
+
+static struct sys_mount_policy vfs_policies[SYS_MOUNT_POLICY_MAX];
+static unsigned int vfs_policy_count;
+
+static struct sys_mount_policy *policy_find(const char *key)
+{
+	unsigned int i;
+
+	for (i = 0; i < vfs_policy_count; i++)
+		if (name_eq(vfs_policies[i].key, key))
+			return &vfs_policies[i];
+	return 0;
+}
+
+/* A mount name or key: 1..15 of [a-z0-9_-]. */
+static int policy_name_ok(const char *s)
+{
+	unsigned int i;
+
+	for (i = 0; s[i]; i++) {
+		char c = s[i];
+
+		if (i >= 15 || !((c >= 'a' && c <= 'z') ||
+				 (c >= '0' && c <= '9') || c == '_' || c == '-'))
+			return 0;
+	}
+	return i > 0;
+}
+
+/* The block device a policy key names: "<dev>" or "<dev>p<1-4>". */
+static blk_dev_t *policy_device(const char *key)
+{
+	unsigned int i, n;
+
+	for (i = 0; i < blk_dev_count; i++) {
+		const char *d = blk_devs[i].name;
+
+		for (n = 0; d[n] && key[n] == d[n]; n++)
+			;
+		if (d[n])
+			continue;
+		if (key[n] == '\0' ||
+		    (key[n] == 'p' && key[n + 1] >= '1' && key[n + 1] <= '4' &&
+		     key[n + 2] == '\0'))
+			return &blk_devs[i];
+	}
+	return 0;
+}
+
+static void remount(blk_dev_t *d);
+
+int vfs_policy_set(const struct sys_mount_policy *p)
+{
+	struct sys_mount_policy *e;
+	blk_dev_t *d;
+
+	if (!policy_name_ok(p->key) || p->name[sizeof(p->name) - 1] ||
+	    (p->name[0] && (!policy_name_ok(p->name) ||
+			    name_eq(p->name, "ramfs") ||
+			    name_eq(p->name, "share"))) ||
+	    (p->flags & ~(SYS_MOUNT_NOAUTO | SYS_MOUNT_RO)))
+		return -1;
+	d = policy_device(p->key);
+	if (!d)
+		return -1;
+	e = policy_find(p->key);
+	if (!p->name[0] && !p->flags) {
+		if (e)
+			*e = vfs_policies[--vfs_policy_count];
+	} else {
+		if (!e) {
+			if (vfs_policy_count >= SYS_MOUNT_POLICY_MAX)
+				return -1;
+			e = &vfs_policies[vfs_policy_count++];
+		}
+		*e = *p;
+	}
+	remount(d);
+	return 0;
+}
+
+int vfs_policy_get(unsigned int index, struct sys_mount_policy *out)
+{
+	if (index >= vfs_policy_count)
+		return 0;
+	*out = vfs_policies[index];
+	return 1;
+}
+
 static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 {
 {
 	fat_fs_t fs;
 	fat_fs_t fs;
 	vfs_mount_t *m;
 	vfs_mount_t *m;
+	const struct sys_mount_policy *pol = policy_find(name);
 	const char *use_name = name;
 	const char *use_name = name;
 	int r;
 	int r;
 
 
+	if (pol && (pol->flags & SYS_MOUNT_NOAUTO))
+		return;
 	fat_cache_clear();
 	fat_cache_clear();
 
 
 	r = fat_mount(dev, part_lba, &fs);
 	r = fat_mount(dev, part_lba, &fs);
@@ -648,12 +743,16 @@ static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 	 * device name.  Only the first NEBOSDATA volume becomes /data. */
 	 * device name.  Only the first NEBOSDATA volume becomes /data. */
 	if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data"))
 	if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data"))
 		use_name = "data";
 		use_name = "data";
+	/* A policy name wins unless another volume already holds it. */
+	if (pol && pol->name[0] && !vfs_find(pol->name))
+		use_name = pol->name;
 	m = mount_slot(use_name, dev->name);
 	m = mount_slot(use_name, dev->name);
 	if (!m)
 	if (!m)
 		return;
 		return;
+	copy_bounded(m->key, name, sizeof(m->key));
 	m->ops = &fat_ops;
 	m->ops = &fat_ops;
 	m->fs = fs;
 	m->fs = fs;
-	m->ro = 0;
+	m->ro = (pol && (pol->flags & SYS_MOUNT_RO)) ? 1 : 0;
 	m->total_sectors = fs.total_sectors;
 	m->total_sectors = fs.total_sectors;
 	copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
 	copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
 	copy_bounded(m->label, fs.label, sizeof(m->label));
 	copy_bounded(m->label, fs.label, sizeof(m->label));
@@ -825,15 +924,20 @@ static int device_is_mounted(const char *dev)
 	return 0;
 	return 0;
 }
 }
 
 
-void vfs_remount_device(blk_dev_t *d)
+static void remount(blk_dev_t *d)
 {
 {
 	unsigned char buf[512];
 	unsigned char buf[512];
 
 
-	if (!d)
-		return;
 	fat_cache_clear();
 	fat_cache_clear();
 	unmount_device(d->name);
 	unmount_device(d->name);
 	mount_device(d, buf);
 	mount_device(d, buf);
+}
+
+void vfs_remount_device(blk_dev_t *d)
+{
+	if (!d)
+		return;
+	remount(d);
 	if (!device_is_mounted(d->name)) {
 	if (!device_is_mounted(d->name)) {
 		io_putstr("vfs: remount of ");
 		io_putstr("vfs: remount of ");
 		io_putstr(d->name);
 		io_putstr(d->name);

+ 11 - 0
kernel/vfs.h

@@ -61,6 +61,7 @@ struct fs_ops {
 typedef struct vfs_mount {
 typedef struct vfs_mount {
 	char name[16];
 	char name[16];
 	char dev[16];                /* block device name for pN mounts */
 	char dev[16];                /* block device name for pN mounts */
+	char key[16];                /* default name (mount-policy key) */
 	const struct fs_ops *ops;
 	const struct fs_ops *ops;
 	int mounted;
 	int mounted;
 	int ro;
 	int ro;
@@ -82,6 +83,16 @@ void vfs_mount_all(void);
 /* Re-mount only one block device after it was formatted. */
 /* Re-mount only one block device after it was formatted. */
 void vfs_remount_device(blk_dev_t *d);
 void vfs_remount_device(blk_dev_t *d);
 
 
+/* Mount policies (SYS_MOUNT_CTL): per-volume mount point name, read-only and
+ * "do not mount", keyed by the default mount name ("ide0p1").  mount_one()
+ * applies them on every (re)scan.  vfs_policy_set() validates and stores
+ * one (an empty policy deletes it) and remounts its device; -1 on a bad
+ * policy, an unknown device or a full table.  The caller holds the fs
+ * section (sys_fs_enter).  vfs_policy_get() copies entry `index`. */
+struct sys_mount_policy;
+int vfs_policy_set(const struct sys_mount_policy *p);
+int vfs_policy_get(unsigned int index, struct sys_mount_policy *out);
+
 /* Set while vfs_mount_all() owns the mount table and the FAT sector buffer.
 /* Set while vfs_mount_all() owns the mount table and the FAT sector buffer.
  * sys_fs.c waits for it; the owner (the calling `storage` syscall) is exempt. */
  * sys_fs.c waits for it; the owner (the calling `storage` syscall) is exempt. */
 extern volatile int vfs_scan_busy;
 extern volatile int vfs_scan_busy;

+ 663 - 35
user/apps/browser/browser.c

@@ -253,6 +253,12 @@ struct lay_run {
 	unsigned int form;          /* node + 1, 0 = not a control */
 	unsigned int form;          /* node + 1, 0 = not a control */
 	unsigned char form_kind;
 	unsigned char form_kind;
 	unsigned char form_disabled;
 	unsigned char form_disabled;
+	/* RUN_PBOX: background colour, background image and its placement
+	 * inside the iw x ih box (relative offsets, drawn size). */
+	unsigned char pb_has_bg;
+	unsigned short pb_bg;
+	short pb_img;
+	short pb_x, pb_y, pb_w, pb_h;
 };
 };
 
 
 struct lay {
 struct lay {
@@ -466,6 +472,21 @@ static int button_has_elements(unsigned int node)
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next)
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next)
 		if (doc.nodes[c].type == BR_NODE_ELEM)
 		if (doc.nodes[c].type == BR_NODE_ELEM)
 			return 1;
 			return 1;
+	/* An empty button whose icon is a ::before/::after box (Docusaurus'
+	 * sidebar caret) is laid out as an element so the box is drawn. */
+	if (!doc.nodes[node].first && doc.npe_rules) {
+		struct br_style st, ps;
+		char buf[8];
+		int pe;
+
+		style_for_node(node, &st);
+		for (pe = 1; pe <= 2; pe++)
+			if (!br_pseudo(&doc, node, pe, &st, &ps, buf,
+				       sizeof(buf)) &&
+			    ps.content == BR_CONTENT_EMPTY &&
+			    ps.display != BR_DISPLAY_NONE)
+				return 1;
+	}
 	return 0;
 	return 0;
 }
 }
 
 
@@ -992,7 +1013,9 @@ static int js_host_computed(unsigned int node, const char *property,
 	struct br_style st;
 	struct br_style st;
 	char name[40];
 	char name[40];
 
 
-	if (!buf || cap <= 0 || node >= doc.nnode || node == 0)
+	/* Node 0 is a real element when the page starts with <html>. */
+	if (!buf || cap <= 0 || node >= doc.nnode ||
+	    (node == 0 && doc.nodes[0].type != BR_NODE_ELEM))
 		return 0;
 		return 0;
 	buf[0] = '\0';
 	buf[0] = '\0';
 	css_computed_name(property, name, sizeof(name));
 	css_computed_name(property, name, sizeof(name));
@@ -2339,6 +2362,12 @@ static void run_page_scripts(void)
 
 
 /* ---- images ---- */
 /* ---- images ---- */
 
 
+/* data: images that failed to decode (key = data_img_key()), so a layout
+ * pass does not retry them on every frame. */
+#define DATA_IMG_FAIL_MAX 32
+static unsigned int data_img_fail[DATA_IMG_FAIL_MAX];
+static int data_img_nfail;
+
 static void image_reset(void)
 static void image_reset(void)
 {
 {
 	int i;
 	int i;
@@ -2354,6 +2383,7 @@ static void image_reset(void)
 		images[i].disp_last = 0;
 		images[i].disp_last = 0;
 	}
 	}
 	image_count = 0;
 	image_count = 0;
+	data_img_nfail = 0;
 	image_pixels = 0;
 	image_pixels = 0;
 	image_disp_pixels = 0;
 	image_disp_pixels = 0;
 	image_disp_clock = 0;
 	image_disp_clock = 0;
@@ -4566,6 +4596,8 @@ static void preload_begin(void)
  * menu/carousel are never visited and keep img_top == 0.  Without this check
  * menu/carousel are never visited and keep img_top == 0.  Without this check
  * the preloader walks the DOM in node order and spends the whole image
  * the preloader walks the DOM in node order and spends the whole image
  * budget on logos and off-canvas thumbnails the reader cannot see. */
  * budget on logos and off-canvas thumbnails the reader cannot see. */
+static struct br_style base_style(void);
+
 static int node_hidden(unsigned int node)
 static int node_hidden(unsigned int node)
 {
 {
 	unsigned int path[32], n = 0, p = node, i;
 	unsigned int path[32], n = 0, p = node, i;
@@ -4575,9 +4607,17 @@ static int node_hidden(unsigned int node)
 		path[n++] = p;
 		path[n++] = p;
 		p = doc.nodes[p].parent;
 		p = doc.nodes[p].parent;
 	}
 	}
-	parent = (struct br_style){0};
-	parent.font_px = 16;
-	parent.display = BR_DISPLAY_BLOCK;
+	/* Node 0 is the <html> element when the page starts with one: it
+	 * is an ancestor too.  Styles are cached per node, so the chain
+	 * must start from the same base as style_for_node(); seeding it
+	 * with a zeroed style left <body> cached with black text and no
+	 * line-height once an image job ran (docs.cloudron.io). */
+	if (!p && n && n < sizeof(path) / sizeof(path[0]) &&
+	    doc.nodes[0].type == BR_NODE_ELEM &&
+	    doc.nodes[0].tag == BR_TAG_HTML &&
+	    !(doc.nodes[path[n - 1]].flags & BR_NODE_DETACHED))
+		path[n++] = 0;
+	parent = base_style();
 	for (i = n; i > 0; i--) {
 	for (i = n; i > 0; i--) {
 		br_style_for(&doc, path[i - 1], &parent, &st);
 		br_style_for(&doc, path[i - 1], &parent, &st);
 		if (st.display == BR_DISPLAY_NONE || st.vis_hidden ||
 		if (st.display == BR_DISPLAY_NONE || st.vis_hidden ||
@@ -4597,7 +4637,12 @@ static void style_for_node(unsigned int node, struct br_style *out)
 	unsigned int p = doc.nodes[node].parent;
 	unsigned int p = doc.nodes[node].parent;
 	struct br_style parent;
 	struct br_style parent;
 
 
-	if (p) {
+	/* Parent index 0 is ambiguous: it is the <html> element when the page
+	 * starts with one (see root_style()), but also the parent field of a
+	 * detached subtree root. */
+	if (p || (node != 0 && doc.nodes[0].type == BR_NODE_ELEM &&
+		  doc.nodes[0].tag == BR_TAG_HTML &&
+		  !(doc.nodes[node].flags & BR_NODE_DETACHED))) {
 		style_for_node(p, &parent);
 		style_for_node(p, &parent);
 		br_style_for(&doc, node, &parent, out);
 		br_style_for(&doc, node, &parent, out);
 	} else {
 	} else {
@@ -4820,6 +4865,130 @@ static void preload_decode(unsigned int node, int bg,
 		image_repaint();
 		image_repaint();
 }
 }
 
 
+
+/* ---- data: URLs ----
+ *
+ * `data:[<type>][;base64],<payload>` decoded to bytes from the page heap
+ * (the caller frees them).  The payload is percent-decoded, then base64-
+ * decoded when the header says so.  Infima's menu chevron and many icon
+ * fonts' fallbacks are inline SVGs written this way. */
+static int b64_val(int c)
+{
+	if (c >= 'A' && c <= 'Z') return c - 'A';
+	if (c >= 'a' && c <= 'z') return c - 'a' + 26;
+	if (c >= '0' && c <= '9') return c - '0' + 52;
+	if (c == '+' || c == '-') return 62;
+	if (c == '/' || c == '_') return 63;
+	return -1;
+}
+
+static int hex_val(int c)
+{
+	if (c >= '0' && c <= '9') return c - '0';
+	c |= 32;
+	if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+	return -1;
+}
+
+static int data_url_decode(const char *url, unsigned char **out,
+			   unsigned int *out_len)
+{
+	const char *comma, *p;
+	unsigned int n = 0, len, i;
+	unsigned char *buf;
+	int base64 = 0;
+
+	*out = 0;
+	*out_len = 0;
+	if (!starts_with(url, "data:"))
+		return -1;
+	comma = url + 5;
+	while (*comma && *comma != ',')
+		comma++;
+	if (!*comma)
+		return -1;
+	for (p = url + 5; p + 7 <= comma; p++)
+		if (!strncmp(p, ";base64", 7))
+			base64 = 1;
+	len = s_len(comma + 1);
+	buf = (unsigned char *)malloc(len + 1);
+	if (!buf)
+		return -1;
+	for (i = 0, p = comma + 1; i < len; i++) {
+		int c = (unsigned char)p[i];
+
+		if (c == '%' && i + 2 < len &&
+		    hex_val(p[i + 1]) >= 0 && hex_val(p[i + 2]) >= 0) {
+			c = hex_val(p[i + 1]) * 16 + hex_val(p[i + 2]);
+			i += 2;
+		}
+		buf[n++] = (unsigned char)c;
+	}
+	if (base64) {
+		unsigned int acc = 0, o = 0;
+		int bits = 0;
+
+		for (i = 0; i < n; i++) {
+			int v = b64_val(buf[i]);
+
+			if (v < 0)
+				continue;       /* padding, whitespace */
+			acc = (acc << 6) | (unsigned int)v;
+			bits += 6;
+			if (bits >= 8) {
+				bits -= 8;
+				buf[o++] = (unsigned char)(acc >> bits);
+			}
+		}
+		n = o;
+	}
+	buf[n] = 0;
+	*out = buf;
+	*out_len = n;
+	return 0;
+}
+
+static unsigned int data_img_key(const char *url, int w, int h)
+{
+	return (script_cache_hash(url) ^ ((unsigned int)w * 2654435761u) ^
+		((unsigned int)h * 40503u)) | 1u;
+}
+
+/* The image index of a data: URL decoded for a w x h box (an SVG is
+ * rasterized at that size), decoding it on first use; -1 on failure.
+ * Decoded bitmaps are found again through url_hash, which is distinct
+ * from the <img> sharing hash because it mixes in the size. */
+static int data_url_image(const char *url, int w, int h)
+{
+	unsigned int key, len;
+	unsigned char *data;
+	int i;
+
+	if (!starts_with(url, "data:") || w <= 0 || h <= 0)
+		return -1;
+	key = data_img_key(url, w, h);
+	for (i = 0; i < image_count; i++)
+		if (images[i].state == 1 && images[i].url_hash == key)
+			return i;
+	for (i = 0; i < data_img_nfail; i++)
+		if (data_img_fail[i] == key)
+			return -1;
+	if (image_count < BR_MAX_IMAGES &&
+	    data_url_decode(url, &data, &len) == 0) {
+		int r = decode_image_data(data, len, &images[image_count],
+					  w, h, w, h);
+
+		free(data);
+		if (r == 0) {
+			images[image_count].url_hash = key;
+			return image_count++;
+		}
+	}
+	if (data_img_nfail < DATA_IMG_FAIL_MAX)
+		data_img_fail[data_img_nfail++] = key;
+	return -1;
+}
+
 /* A local file image bypasses the helpers: count its decode so a page opened
 /* A local file image bypasses the helpers: count its decode so a page opened
  * from /share or /data still produces the same timing breakdown. */
  * from /share or /data still produces the same timing breakdown. */
 static void img_stat_local(int before, unsigned int bytes, unsigned int io_ms)
 static void img_stat_local(int before, unsigned int bytes, unsigned int io_ms)
@@ -5064,8 +5233,24 @@ static int preload_fill_jobs(void)
 						preload_node++;
 						preload_node++;
 						continue;
 						continue;
 					}
 					}
+					if (starts_with(bsrc, "data:")) {
+						/* Inline image: decode now,
+						 * no fetch helper. */
+						unsigned char *data;
+						unsigned int len;
+
+						preload_node = i + 1;
+						if (data_url_decode(bsrc, &data,
+								    &len) == 0) {
+							preload_decode(i, 1, data,
+								       len, 0);
+							free(data);
+						} else {
+							nd->bg_img_index = -2;
+						}
+						continue;
+					}
 					if (bsrc[0] &&
 					if (bsrc[0] &&
-					    !starts_with(bsrc, "data:") &&
 					    resolve_link(page_url, bsrc, url,
 					    resolve_link(page_url, bsrc, url,
 							 sizeof(url)) == 0) {
 							 sizeof(url)) == 0) {
 						if (image_count +
 						if (image_count +
@@ -5133,7 +5318,20 @@ static int preload_fill_jobs(void)
 			continue;
 			continue;
 		}
 		}
 		src = br_src(&doc, i);
 		src = br_src(&doc, i);
-		if (!src[0] || starts_with(src, "data:")) {
+		if (starts_with(src, "data:")) {
+			unsigned char *data;
+			unsigned int len;
+
+			preload_node = i + 1;
+			if (data_url_decode(src, &data, &len) == 0) {
+				preload_decode(i, 0, data, len, 0);
+				free(data);
+			} else {
+				doc.nodes[i].img_index = -2;
+			}
+			continue;
+		}
+		if (!src[0]) {
 			preload_node++;
 			preload_node++;
 			continue;
 			continue;
 		}
 		}
@@ -5930,6 +6128,43 @@ static struct br_style base_style(void)
 	return s;
 	return s;
 }
 }
 
 
+/* The style node 0's children inherit.  Node 0 is the first parsed node:
+ * a minified page that starts `<!doctype html><html ...>` makes it the
+ * <html> element itself, and the layout walks only its children, so its
+ * own cascade (html { color; font } -- Docusaurus/Infima put the page
+ * colour and font there) used to be skipped and the theme defaults showed
+ * through.  Only inherited properties are taken: the root's box (margins,
+ * display, background) is not a container style for its children here. */
+static struct br_style root_style(void)
+{
+	struct br_style base = base_style(), st;
+
+	if (!doc.nnode || doc.nodes[doc.root].type != BR_NODE_ELEM ||
+	    doc.nodes[doc.root].tag != BR_TAG_HTML)
+		return base;
+	br_style_for(&doc, doc.root, &base, &st);
+	base.color = st.color;
+	base.font_px = st.font_px;
+	base.bold = st.bold;
+	base.italic = st.italic;
+	base.mono = st.mono;
+	base.font_face = st.font_face;
+	base.align = st.align;
+	base.line_h_pct = st.line_h_pct;
+	base.line_h_px = st.line_h_px;
+	base.nowrap = st.nowrap;
+	base.uppercase = st.uppercase;
+	base.cursor = st.cursor;
+	base.vis_hidden = st.vis_hidden;
+	base.fill_mode = st.fill_mode;
+	base.fill = st.fill;
+	base.stroke_mode = st.stroke_mode;
+	base.stroke = st.stroke;
+	base.stroke_w = st.stroke_w;
+	base.fill_evenodd = st.fill_evenodd;
+	return base;
+}
+
 /* Resolve a margin/padding raw value.  Percentages use the containing
 /* Resolve a margin/padding raw value.  Percentages use the containing
  * block width for all four sides, as CSS specifies. */
  * block width for all four sides, as CSS specifies. */
 static int css_mp_px(const struct br_style *st, int v, int unit, int base)
 static int css_mp_px(const struct br_style *st, int v, int unit, int base)
@@ -6430,14 +6665,16 @@ static void svg_paint_box(struct ngui *ui, unsigned int node,
 			  int h);
 			  int h);
 
 
 /* Run kinds besides text: img >= 0 an <img> bitmap, -2 a button label,
 /* Run kinds besides text: img >= 0 an <img> bitmap, -2 a button label,
- * RUN_SVG an inline <svg> icon (its node in off). */
+ * RUN_SVG an inline <svg> icon (its node in off), RUN_PBOX an empty
+ * ::before/::after box with a background (Infima's menu chevrons). */
 #define RUN_SVG (-3)
 #define RUN_SVG (-3)
+#define RUN_PBOX (-4)
 
 
 static int run_width(const struct lay_run *r)
 static int run_width(const struct lay_run *r)
 {
 {
 	if (r->img >= 0)
 	if (r->img >= 0)
 		return r->iw;
 		return r->iw;
-	if (r->img == -2 || r->img == RUN_SVG)
+	if (r->img == -2 || r->img == RUN_SVG || r->img == RUN_PBOX)
 		return r->iw;
 		return r->iw;
 	return (int)r->w;
 	return (int)r->w;
 }
 }
@@ -6636,6 +6873,29 @@ static void flush_line(struct lay *l)
 							     r->font_px, 0, 0, 0);
 							     r->font_px, 0, 0, 0);
 					}
 					}
 				}
 				}
+			} else if (r->img == RUN_PBOX) {
+				struct ngui_rect bx = { x, ry, w, r->ih };
+
+				ngui_push_clip(l->ui, bx);
+				if (r->pb_has_bg)
+					ngui_fill_rect(l->ui, bx, r->pb_bg);
+				if (r->pb_img >= 0 && r->pb_img < image_count &&
+				    images[r->pb_img].state == 1) {
+					/* Rotate about the box centre: the
+					 * image is centred in it here. */
+					draw_image_transformed(l->ui,
+						&images[r->pb_img].img,
+						x + r->pb_x, ry + r->pb_y,
+						r->pb_w, r->pb_h,
+						r->transform_scale ?
+						r->scale_x : 100,
+						r->transform_scale ?
+						r->scale_y : 100,
+						r->transform_scale ?
+						r->rotate_x100 : 0,
+						50, 50, 1, 1);
+				}
+				ngui_pop_clip(l->ui);
 			} else if (r->img == RUN_SVG) {
 			} else if (r->img == RUN_SVG) {
 				struct br_style sst;
 				struct br_style sst;
 
 
@@ -6920,14 +7180,20 @@ static void add_image_run(struct lay *l, unsigned int node,
 					maxw * (int)st->width / 100 :
 					maxw * (int)st->width / 100 :
 					(int)st->width;
 					(int)st->width;
 		}
 		}
-		if (st->height_kind) {
+		/* Percentage heights resolve against the containing block's
+		 * definite height (Docusaurus' logo: `.navbar__logo { height:
+		 * 2rem }` holding an `img { height: 100% }`), and are auto
+		 * when it has none; they used to use the width. */
+		if (st->height_kind == 1 ||
+		    ((st->height_kind == 2 || st->height_kind == 3) &&
+		     l->cb_h > 0)) {
 			css_h = 1;
 			css_h = 1;
 			if (st->height_kind == 3)
 			if (st->height_kind == 3)
-				dh = maxw * st->height_calc_pct / 100 +
+				dh = l->cb_h * st->height_calc_pct / 100 +
 				     st->height_calc_px;
 				     st->height_calc_px;
 			else
 			else
 				dh = st->height_kind == 2 ?
 				dh = st->height_kind == 2 ?
-					maxw * (int)st->height / 100 :
+					l->cb_h * (int)st->height / 100 :
 					(int)st->height;
 					(int)st->height;
 		}
 		}
 		if (css_w && !css_h)
 		if (css_w && !css_h)
@@ -7355,6 +7621,12 @@ static void add_form_run(struct lay *l, unsigned int node,
 		   kind == FORM_RESET) {
 		   kind == FORM_RESET) {
 		if (fs->len)
 		if (fs->len)
 			text = fs->value;
 			text = fs->value;
+		else if (doc.nodes[node].tag == BR_TAG_BUTTON)
+			/* A <button>'s label is its content: an empty one
+			 * (Docusaurus' sidebar caret, drawn by a CSS
+			 * ::before icon) shows none.  Only <input> buttons
+			 * get the UA's default label. */
+			text = "";
 		else
 		else
 			text = kind == FORM_SUBMIT ? "Submit" :
 			text = kind == FORM_SUBMIT ? "Submit" :
 			       kind == FORM_RESET ? "Reset" : "Button";
 			       kind == FORM_RESET ? "Reset" : "Button";
@@ -7571,6 +7843,10 @@ static void layout_children(struct lay *l, unsigned int parent,
 static void layout_flex_children(struct lay *l, unsigned int parent,
 static void layout_flex_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 const struct br_style *pst, int depth,
 				 unsigned short link);
 				 unsigned short link);
+static int pseudo_box_w(unsigned int node, const struct br_style *st, int pe,
+			struct br_style *ps, int base);
+static void emit_pseudo_box(struct lay *l, const struct br_style *parent,
+			    const struct br_style *ps, unsigned short link);
 static void layout_grid_children(struct lay *l, unsigned int parent,
 static void layout_grid_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 const struct br_style *pst, int depth,
 				 unsigned short link);
 				 unsigned short link);
@@ -7611,7 +7887,7 @@ static int flex_wants_row(unsigned int parent, const struct br_style *pst)
 		    cs.width_kind || cs.has_bg ||
 		    cs.width_kind || cs.has_bg ||
 		    cs.border_w[0] || cs.border_w[1] ||
 		    cs.border_w[0] || cs.border_w[1] ||
 		    cs.border_w[2] || cs.border_w[3] ||
 		    cs.border_w[2] || cs.border_w[3] ||
-		    nd->tag == BR_TAG_IMG)
+		    nd->tag == BR_TAG_IMG || nd->tag == BR_TAG_SVG)
 			return 1;
 			return 1;
 	}
 	}
 	return 0;
 	return 0;
@@ -8398,8 +8674,16 @@ static int svg_decode_image(const unsigned char *data, unsigned int len,
 	base.color = 0x0000;
 	base.color = 0x0000;
 	br_style_for(&doc, svg, &base, &st);
 	br_style_for(&doc, svg, &base, &st);
 	svg_root_matrix(svg, w, h, m);
 	svg_root_matrix(svg, w, h, m);
-	svg_vp_w = have_vb ? vb[2] : w;
-	svg_vp_h = have_vb ? vb[3] : h;
+	if (!have_vb && nat_w > 0 && nat_h > 0) {
+		/* No viewBox: user units are CSS pixels of the natural size,
+		 * so the up-to-4x raster (and a box size) must scale them.
+		 * A 40x20 icon used to fill only the top-left quarter of its
+		 * 160x80 bitmap and was drawn at a quarter of its size. */
+		m[0] = (double)w / nat_w;
+		m[3] = (double)h / nat_h;
+	}
+	svg_vp_w = have_vb ? vb[2] : nat_w;
+	svg_vp_h = have_vb ? vb[3] : nat_h;
 	svg_draw_children(&L, svg, m, &st, 255, 0);
 	svg_draw_children(&L, svg, m, &st, 255, 0);
 	bi->native_w = nat_w;
 	bi->native_w = nat_w;
 	bi->native_h = nat_h;
 	bi->native_h = nat_h;
@@ -8681,6 +8965,19 @@ static void layout_block(struct lay *l, unsigned int node,
 				l->y = flow_y + block_h[parent] - st->bottom -
 				l->y = flow_y + block_h[parent] - st->bottom -
 				      style_height_px(st, old_right - old_left);
 				      style_height_px(st, old_right - old_left);
 		}
 		}
+		/* `left: 100%` parks a box (Docusaurus' "Skip to main
+		 * content" link) past the containing block's right edge
+		 * until it is focused.  An auto width then has no room: it
+		 * used to collapse to 8 px and wrap one word per line into a
+		 * tall column painted over the page.  Lay it out at the
+		 * containing block's width so it stays one line and
+		 * overflows (off-screen) like in a real browser. */
+		if (def_w < 0 && l->right - l->left < 8) {
+			int avail = st->pos == 2 ? page_rect.w - 12 :
+				    flow_right - flow_left;
+
+			l->right = l->left + (avail > 8 ? avail : 8);
+		}
 		old_left = l->left;
 		old_left = l->left;
 		old_right = l->right;
 		old_right = l->right;
 	} else if (relative) {
 	} else if (relative) {
@@ -9187,6 +9484,14 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 
 
 	if (depth > 32)
 	if (depth > 32)
 		return 0;
 		return 0;
+	if (doc.npe_rules) {
+		struct br_style ps;
+
+		/* Icon boxes (::before/::after with a size) are inline
+		 * content of the element. */
+		line_w += pseudo_box_w(node, pst, 1, &ps, base) +
+			  pseudo_box_w(node, pst, 2, &ps, base);
+	}
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
 		struct br_node *nd = &doc.nodes[c];
 		struct br_node *nd = &doc.nodes[c];
 		struct br_style cs;
 		struct br_style cs;
@@ -9258,7 +9563,15 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			} else if (nd->height_attr && iw && ih) {
 			} else if (nd->height_attr && iw && ih) {
 				line_w += iw * (int)nd->height_attr / ih;
 				line_w += iw * (int)nd->height_attr / ih;
 			} else if (cs.height_kind && iw && ih) {
 			} else if (cs.height_kind && iw && ih) {
-				int hh = style_height_px(&cs, base);
+				/* A percentage height needs the parent's
+				 * definite height (the logo's 2rem box), as in
+				 * add_image_run(); otherwise it is auto. */
+				int hh = cs.height_kind == 1 ?
+					 style_height_px(&cs, base) :
+					 cs.height_kind == 2 &&
+					 pst->height_kind == 1 ?
+					 (int)pst->height * (int)cs.height / 100 :
+					 0;
 
 
 				line_w += hh > 0 ? iw * hh / ih : iw;
 				line_w += hh > 0 ? iw * hh / ih : iw;
 			} else if (iw) {
 			} else if (iw) {
@@ -9386,10 +9699,23 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 		if (nd->tag == BR_TAG_A && nd->href_off)
 		if (nd->tag == BR_TAG_A && nd->href_off)
 			lk = (unsigned short)n;
 			lk = (unsigned short)n;
 		right = cs.float_left == 2;
 		right = cs.float_left == 2;
-		if (cs.width_kind == 2 || cs.width_kind == 3) {
-			colw = style_width_px(&cs, row_right - row_left);
-		} else if (cs.width_kind == 1) {
-			colw = (int)cs.width;
+		if (cs.width_kind == 1 || cs.width_kind == 2 ||
+		    cs.width_kind == 3) {
+			/* The column is the margin box: `width` is the
+			 * content (or, with border-box, the border) box,
+			 * and layout_block() takes margins, padding and
+			 * borders back out of the column.  A 200px float
+			 * with margin-right:20px used to get 180px of
+			 * content. */
+			int base = row_right - row_left;
+
+			colw = cs.width_kind == 1 ? (int)cs.width :
+			       style_width_px(&cs, base);
+			colw += mp_px(&cs, 3, base) + mp_px(&cs, 1, base);
+			if (!cs.box_sizing)
+				colw += mp_px(&cs, 7, base) +
+					mp_px(&cs, 5, base) +
+					cs.border_w[1] + cs.border_w[3];
 		} else {
 		} else {
 			int base = row_right - row_left;
 			int base = row_right - row_left;
 
 
@@ -9611,8 +9937,19 @@ static int inline_block_flows(unsigned int node, const struct br_style *cs,
 
 
 		if (nd->type != BR_NODE_ELEM)
 		if (nd->type != BR_NODE_ELEM)
 			continue;
 			continue;
-		if (nd->tag == BR_TAG_IMG || nd->tag == BR_TAG_SVG ||
-		    br_tag_is_block(nd->tag))
+		if (nd->tag == BR_TAG_SVG) {
+			/* An icon box (Docusaurus' admonition heading:
+			 * `<span inline-block><svg/></span>Note`): the SVG
+			 * is an inline replaced run, so the box can stay in
+			 * the text line. */
+			br_style_for(&doc, c, cs, &ch);
+			if (ch.display == BR_DISPLAY_NONE ||
+			    ch.display == BR_DISPLAY_INLINE ||
+			    ch.display == BR_DISPLAY_INLINE_BLOCK)
+				continue;
+			return 0;
+		}
+		if (nd->tag == BR_TAG_IMG || br_tag_is_block(nd->tag))
 			return 0;
 			return 0;
 		br_style_for(&doc, c, cs, &ch);
 		br_style_for(&doc, c, cs, &ch);
 		if (ch.display == BR_DISPLAY_NONE)
 		if (ch.display == BR_DISPLAY_NONE)
@@ -9722,6 +10059,43 @@ struct flex_item {
 	int w;
 	int w;
 };
 };
 
 
+/* An item's min-width/max-width as outer (margin-box) widths in the flex
+ * row, or -1 when unset.  Percentages and calc() resolve against the
+ * container width. */
+static int flex_limit_w(const struct br_style *cs, int which_max, int avail)
+{
+	unsigned char kind = which_max ? cs->max_width_kind :
+			     cs->min_width_kind;
+	int w;
+
+	if (kind == 3)
+		w = which_max ?
+		    avail * cs->max_width_calc_pct / 100 +
+		    cs->max_width_calc_px :
+		    avail * cs->min_width_calc_pct / 100 +
+		    cs->min_width_calc_px;
+	else
+		w = css_size(kind, which_max ? cs->max_width : cs->min_width,
+			     avail);
+	if (w < 0)
+		return -1;
+	if (!cs->box_sizing)
+		w += mp_px(cs, 7, avail) + mp_px(cs, 5, avail) +
+		     cs->border_w[1] + cs->border_w[3];
+	return w + mp_px(cs, 3, avail) + mp_px(cs, 1, avail);
+}
+
+static int flex_clamp_w(const struct br_style *cs, int w, int avail)
+{
+	int mx = flex_limit_w(cs, 1, avail), mn = flex_limit_w(cs, 0, avail);
+
+	if (mx >= 0 && w > mx)
+		w = mx;
+	if (mn >= 0 && w < mn)
+		w = mn;
+	return w;
+}
+
 static void layout_flex_children(struct lay *l, unsigned int parent,
 static void layout_flex_children(struct lay *l, unsigned int parent,
 				 const struct br_style *pst, int depth,
 				 const struct br_style *pst, int depth,
 				 unsigned short link)
 				 unsigned short link)
@@ -9729,10 +10103,14 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 	struct flex_item it[32];
 	struct flex_item it[32];
 	unsigned int c;
 	unsigned int c;
 	int nitem = 0, grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
 	int nitem = 0, grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
+	int grow_need = 0;      /* growing items at their content width */
 	int row_left = l->left, row_right = l->right;
 	int row_left = l->left, row_right = l->right;
 	int avail = row_right - row_left;
 	int avail = row_right - row_left;
 	int idx, row_top;
 	int idx, row_top;
 	unsigned char row_align = l->align;
 	unsigned char row_align = l->align;
+	int box_left = l->left, box_right = l->right, row_top0 = l->y;
+	int bw = 0, aw = 0, first_h = -1;
+	struct br_style bps, aps;
 
 
 	if (avail < 8 || depth > 48) {
 	if (avail < 8 || depth > 48) {
 		layout_children(l, parent, pst, depth, link);
 		layout_children(l, parent, pst, depth, link);
@@ -9750,6 +10128,20 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 		}
 		}
 	}
 	}
 
 
+	/* ::before/::after icon boxes are the row's first and last items
+	 * (Docusaurus' sidebar chevron): reserve their columns. */
+	if (doc.npe_rules) {
+		bw = pseudo_box_w(parent, pst, 1, &bps, avail);
+		aw = pseudo_box_w(parent, pst, 2, &aps, avail);
+		if (bw + aw > avail - 8) {
+			bw = 0;
+			aw = 0;
+		}
+		row_left += bw;
+		row_right -= aw;
+		avail = row_right - row_left;
+	}
+
 	for (c = doc.nodes[parent].first;
 	for (c = doc.nodes[parent].first;
 	     c && nitem < (int)(sizeof(it) / sizeof(it[0]));
 	     c && nitem < (int)(sizeof(it) / sizeof(it[0]));
 	     c = doc.nodes[c].next) {
 	     c = doc.nodes[c].next) {
@@ -9765,31 +10157,74 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 		it[nitem].link = (nd->tag == BR_TAG_A && nd->href_off) ?
 		it[nitem].link = (nd->tag == BR_TAG_A && nd->href_off) ?
 				 (unsigned short)c : link;
 				 (unsigned short)c : link;
 		it[nitem].w = 0;
 		it[nitem].w = 0;
-		if (cs.flex_grow && !cs.width_kind)
+		if (cs.flex_grow && !cs.width_kind) {
 			grow_count++;
 			grow_count++;
-		else
-			fixed += flex_item_width(l, c, &cs, avail);
+			grow_need += flex_item_width(l, c, &cs, avail);
+		} else
+			fixed += flex_clamp_w(&cs,
+					      flex_item_width(l, c, &cs, avail),
+					      avail);
 		nitem++;
 		nitem++;
 	}
 	}
 	/* A narrow container (an icon row sized to its content, Google's
 	/* A narrow container (an icon row sized to its content, Google's
 	 * mic + lens buttons) is still a row when its items fit; only a
 	 * mic + lens buttons) is still a row when its items fit; only a
 	 * narrow one that would overflow or squeeze growing items falls
 	 * narrow one that would overflow or squeeze growing items falls
 	 * back to block flow. */
 	 * back to block flow. */
-	if (avail < 160 && (grow_count || fixed > avail)) {
+	if (avail < 160 && fixed + grow_need > avail) {
 		layout_children(l, parent, pst, depth, link);
 		layout_children(l, parent, pst, depth, link);
 		return;
 		return;
 	}
 	}
 	grow_w = grow_count ? (avail - fixed) / grow_count : 0;
 	grow_w = grow_count ? (avail - fixed) / grow_count : 0;
-	if (grow_w < 96)
+	/* A growing item capped by max-width (Docusaurus' doc column,
+	 * `max-width: 75%`; its main container, `calc(100% - 300px)`)
+	 * keeps its cap and leaves the rest of the free space to the other
+	 * growing items. */
+	if (grow_count) {
+		int pass, capped[32] = { 0 };
+
+		for (pass = 0; pass < nitem; pass++) {
+			int changed = 0;
+
+			for (idx = 0; idx < nitem; idx++) {
+				struct br_style cs;
+				int lim;
+
+				if (capped[idx])
+					continue;
+				br_style_for(&doc, it[idx].node, pst, &cs);
+				if (!cs.flex_grow || cs.width_kind)
+					continue;
+				lim = flex_clamp_w(&cs, grow_w, avail);
+				if (lim == grow_w)
+					continue;
+				capped[idx] = lim + 1;
+				fixed += lim;
+				grow_count--;
+				changed = 1;
+			}
+			if (!changed || !grow_count)
+				break;
+			grow_w = (avail - fixed) / grow_count;
+		}
+		for (idx = 0; idx < nitem; idx++)
+			it[idx].w = capped[idx] ? capped[idx] - 1 : 0;
+	}
+	if (grow_w < 96 && avail >= 160)
 		grow_w = 96;
 		grow_w = 96;
 	for (idx = 0; idx < nitem; idx++) {
 	for (idx = 0; idx < nitem; idx++) {
 		struct br_style cs;
 		struct br_style cs;
 
 
+		if (it[idx].w)
+			continue;       /* capped growing item */
 		br_style_for(&doc, it[idx].node, pst, &cs);
 		br_style_for(&doc, it[idx].node, pst, &cs);
 		if (cs.flex_grow && !cs.width_kind)
 		if (cs.flex_grow && !cs.width_kind)
 			it[idx].w = grow_w;
 			it[idx].w = grow_w;
 		else
 		else
-			it[idx].w = flex_item_width(l, it[idx].node, &cs, avail);
+			it[idx].w = flex_clamp_w(&cs,
+						 flex_item_width(l,
+								 it[idx].node,
+								 &cs, avail),
+						 avail);
 		if (it[idx].w > avail)
 		if (it[idx].w > avail)
 			it[idx].w = avail;
 			it[idx].w = avail;
 		if (it[idx].w < 8)
 		if (it[idx].w < 8)
@@ -9892,10 +10327,41 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 			if (k + 1 < count)
 			if (k + 1 < count)
 				x += gap + extra;
 				x += gap + extra;
 		}
 		}
+		if (first_h < 0)
+			first_h = row_h;
 		row_top += row_h;
 		row_top += row_h;
 	}
 	}
-	l->left = row_left;
-	l->right = row_right;
+	if (bw || aw) {
+		int pass;
+
+		for (pass = 0; pass < 2; pass++) {
+			const struct br_style *ps = pass ? &aps : &bps;
+			int a = pass ? row_right : box_left;
+			int b = pass ? box_right : row_left;
+			int h = ps->height_kind ?
+				style_height_px(ps, box_right - box_left) : 0;
+
+			if (!(pass ? aw : bw))
+				continue;
+			l->left = l->line_left = l->line_x = l->run_x = a;
+			l->right = l->line_right = b;
+			l->y = row_top0;
+			if (pst->align_items == 1 && first_h > h)
+				l->y += (first_h - h) / 2;
+			l->line_y = l->y;
+			l->line_h = 0;
+			l->nruns = 0;
+			l->space_pending = 0;
+			emit_pseudo_box(l, pst, ps, link);
+			flush_line(l);
+			/* The box sits inside the first row; it only makes
+			 * the row taller when it is taller itself. */
+			if (row_top0 + h > row_top)
+				row_top = row_top0 + h;
+		}
+	}
+	l->left = box_left;
+	l->right = box_right;
 	l->align = row_align;
 	l->align = row_align;
 	l->y = row_top;
 	l->y = row_top;
 	l->line_x = l->left;
 	l->line_x = l->left;
@@ -10864,6 +11330,119 @@ struct br_deferred {
 static struct br_deferred deferred[BR_MAX_DEFERRED];
 static struct br_deferred deferred[BR_MAX_DEFERRED];
 static int ndeferred;
 static int ndeferred;
 
 
+/* Outer width of an element's empty ::before (pe 1) / ::after (pe 2) icon
+ * box, 0 when it has none; *ps receives the pseudo-element's style. */
+static int pseudo_box_w(unsigned int node, const struct br_style *st, int pe,
+			struct br_style *ps, int base)
+{
+	char buf[8];
+	int w, h;
+
+	if (!doc.npe_rules ||
+	    br_pseudo(&doc, node, pe, st, ps, buf, sizeof(buf)) ||
+	    ps->content != BR_CONTENT_EMPTY || ps->display == BR_DISPLAY_NONE)
+		return 0;
+	w = ps->width_kind ? style_width_px(ps, base) : 0;
+	h = ps->height_kind ? style_height_px(ps, base) : 0;
+	if (ps->min_width_kind == 1 && w < (int)ps->min_width)
+		w = (int)ps->min_width;
+	if (w <= 0)
+		w = h;
+	if (w <= 0 || w > 512)
+		return 0;
+	return w + (ps->ml_auto ? 0 : mp_px(ps, 3, base)) + mp_px(ps, 1, base);
+}
+
+/* An empty ::before/::after with a size and a background: one run the
+ * size of the box.  Only data: background images are drawn (decoded at
+ * the background-size); `margin-left: auto` inside a flex parent pushes
+ * the box to the line's right edge like the flex row would. */
+static void emit_pseudo_box(struct lay *l, const struct br_style *parent,
+			    const struct br_style *ps, unsigned short link)
+{
+	struct lay_run *r;
+	int lw = l->line_right - l->line_left, w, h, ml, mr, tw, th;
+	const char *url = ps->bg_image ? br_bg_image_url(ps->bg_image - 1) : "";
+
+	if (lw < 8)
+		lw = 8;
+	w = ps->width_kind ? style_width_px(ps, lw) : 0;
+	h = ps->height_kind ? style_height_px(ps, lw) : 0;
+	if (ps->min_width_kind == 1 && w < (int)ps->min_width)
+		w = (int)ps->min_width;
+	if (w <= 0)
+		w = h;
+	if (h <= 0)
+		h = w;
+	if (w <= 0 || w > 512 || h > 512)
+		return;
+	ml = ps->ml_auto ? 0 : mp_px(ps, 3, lw);
+	mr = mp_px(ps, 1, lw);
+	if (ml < 0)
+		ml = 0;
+	if (mr < 0)
+		mr = 0;
+	if (l->line_x + ml + w > l->line_right &&
+	    (l->nruns > 0 || l->line_x > l->line_left))
+		flush_line(l);
+	if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
+		flush_line(l);
+	if (l->nruns == 0) {
+		l->line_y = l->y;
+		l->line_h = 0;
+		start_line(l, ps);
+	}
+	if (ps->ml_auto && parent->display == BR_DISPLAY_FLEX &&
+	    l->line_right - w - mr > l->line_x)
+		l->line_x = l->line_right - w - mr;
+	else
+		l->line_x += ml;
+	r = &l->runs[l->nruns++];
+	memset(r, 0, sizeof(*r));
+	r->gap = (unsigned short)(l->line_x > l->run_x ?
+				  l->line_x - l->run_x : 0);
+	r->img = RUN_PBOX;
+	r->href = link;
+	r->iw = (unsigned short)w;
+	r->ih = (unsigned short)h;
+	r->w = (unsigned short)w;
+	r->font_px = ps->font_px;
+	r->pb_has_bg = ps->has_bg;
+	r->pb_bg = ps->bg;
+	r->pb_img = -1;
+	if (ps->transform_set) {
+		r->transform_scale = 1;
+		r->scale_x = ps->scale_x;
+		r->scale_y = ps->scale_y;
+		r->rotate_x100 = ps->rotate_x100;
+	}
+	/* Background size and position, as draw_bg_image() resolves them. */
+	tw = w;
+	th = h;
+	if (ps->bg_size_kind == 1 || ps->bg_size_kind == 2) {
+		int bw = ps->bg_size_w, bh = ps->bg_size_h;
+
+		if (ps->bg_size_kind == 2) {
+			bw = bw ? w * bw / 100 : 0;
+			bh = bh ? h * bh / 100 : 0;
+		}
+		tw = bw ? bw : (bh ? bh : w);
+		th = bh ? bh : tw;
+	}
+	if (tw > 0 && th > 0 && tw <= 512 && th <= 512 && url[0])
+		r->pb_img = (short)data_url_image(url, tw, th);
+	r->pb_w = (short)tw;
+	r->pb_h = (short)th;
+	r->pb_x = (short)(ps->bg_pos_x_kind == 1 ?
+			  (w - tw) * ps->bg_pos_x / 100 : ps->bg_pos_x);
+	r->pb_y = (short)(ps->bg_pos_y_kind == 1 ?
+			  (h - th) * ps->bg_pos_y / 100 : ps->bg_pos_y);
+	l->line_x += w + mr;
+	l->run_x = l->line_x;
+	if (l->line_h < h)
+		l->line_h = h;
+}
+
 /* ::before/::after generated text (br_pseudo()) as literal runs in the
 /* ::before/::after generated text (br_pseudo()) as literal runs in the
  * element's line.  A block-level pseudo-element gets its own line; its box
  * element's line.  A block-level pseudo-element gets its own line; its box
  * (margins, padding, background) is not drawn. */
  * (margins, padding, background) is not drawn. */
@@ -10876,6 +11455,11 @@ static void emit_pseudo(struct lay *l, unsigned int node,
 	unsigned int i = 0;
 	unsigned int i = 0;
 	int block, space_w;
 	int block, space_w;
 
 
+	if (!n && ps.content == BR_CONTENT_EMPTY &&
+	    ps.display != BR_DISPLAY_NONE) {
+		emit_pseudo_box(l, st, &ps, link);
+		return;
+	}
 	if (!n)
 	if (!n)
 		return;
 		return;
 	block = ps.display == BR_DISPLAY_BLOCK || ps.display == BR_DISPLAY_FLEX;
 	block = ps.display == BR_DISPLAY_BLOCK || ps.display == BR_DISPLAY_FLEX;
@@ -11150,6 +11734,7 @@ static int measure_page(int left, int right)
 	l->depth = 0;
 	l->depth = 0;
 	l->nruns = 0;
 	l->nruns = 0;
 	l->space_pending = 0;
 	l->space_pending = 0;
+	base = root_style();
 	layout_children(l, doc.root, &base, 0, 0);
 	layout_children(l, doc.root, &base, 0, 0);
 	return l->y + 8;
 	return l->y + 8;
 }
 }
@@ -11210,6 +11795,7 @@ static void render_page(int left, int right, int y0)
 	l->space_pending = 0;
 	l->space_pending = 0;
 	nhits = 0;
 	nhits = 0;
 	ndeferred = 0;
 	ndeferred = 0;
+	base = root_style();
 	layout_children(l, doc.root, &base, 0, 0);
 	layout_children(l, doc.root, &base, 0, 0);
 	sort_deferred();
 	sort_deferred();
 	{
 	{
@@ -11236,10 +11822,32 @@ static void render_page(int left, int right, int y0)
 			if (st.pos == 2 && block_h[deferred[i].node]) {
 			if (st.pos == 2 && block_h[deferred[i].node]) {
 				struct ngui_rect clip;
 				struct ngui_rect clip;
 				int ch = block_h[deferred[i].node] + 24;
 				int ch = block_h[deferred[i].node] + 24;
-
-				clip.x = page_rect.x;
+				int pr = page_rect.x + page_rect.w - 12;
+				int x0 = page_rect.x, x1 = pr;
+
+				/* The background pre-fill spans the box's own
+				 * columns, not the whole page: Docusaurus parks
+				 * a white "Skip to main content" link at
+				 * `left: 100%` with a z-index above the navbar,
+				 * and a full-width fill painted over the bar. */
+				if (st.left_set)
+					x0 = page_rect.x + st.left;
+				if (st.width_kind == 1) {
+					if (st.right_set && !st.left_set)
+						x0 = pr - st.right - st.width;
+					x1 = x0 + st.width;
+				} else if (st.right_set) {
+					x1 = pr - st.right;
+				}
+				if (x0 >= pr || x1 <= page_rect.x)
+					continue;       /* entirely off-screen */
+				if (x0 < page_rect.x)
+					x0 = page_rect.x;
+				if (x1 > pr)
+					x1 = pr;
+				clip.x = x0;
 				clip.y = page_rect.y;
 				clip.y = page_rect.y;
-				clip.w = page_rect.w - 12;
+				clip.w = x1 - x0;
 				if (ch > page_rect.h)
 				if (ch > page_rect.h)
 					ch = page_rect.h;
 					ch = page_rect.h;
 				clip.h = ch;
 				clip.h = ch;
@@ -11584,6 +12192,7 @@ static void draw_suggest(struct ngui_rect page)
 	sug_rect = r;
 	sug_rect = r;
 	if (ui.inside && ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y)) {
 	if (ui.inside && ngui_rect_contains(&r, ui.mouse_x, ui.mouse_y)) {
 		hover = (ui.mouse_y - r.y - 1) / SUG_ROW_H;
 		hover = (ui.mouse_y - r.y - 1) / SUG_ROW_H;
+		ngui_set_cursor(&ui, CURSOR_POINTER);
 	}
 	}
 	ngui_fill_rect(&ui, r, ui.theme.border);
 	ngui_fill_rect(&ui, r, ui.theme.border);
 	for (i = 0; i < rows; i++) {
 	for (i = 0; i < rows; i++) {
@@ -11708,16 +12317,35 @@ static void draw_page(struct ngui_rect page)
 		unsigned int body = doc.nnode > 1 ?
 		unsigned int body = doc.nnode > 1 ?
 			br_find_tag(&doc, BR_TAG_BODY) : 0;
 			br_find_tag(&doc, BR_TAG_BODY) : 0;
 		struct br_style bs;
 		struct br_style bs;
+		int bg_found = 0;
 
 
 		if (root) {
 		if (root) {
 			style_for_node(root - 1, &bs);
 			style_for_node(root - 1, &bs);
-			if (bs.has_bg)
+			if (bs.has_bg) {
 				page_bg = bs.bg;
 				page_bg = bs.bg;
+				bg_found = 1;
+			}
 		}
 		}
 		if (body) {
 		if (body) {
 			style_for_node(body - 1, &bs);
 			style_for_node(body - 1, &bs);
-			if (bs.has_bg)
+			if (bs.has_bg) {
 				page_bg = bs.bg;
 				page_bg = bs.bg;
+				bg_found = 1;
+			}
+		}
+		/* No canvas colour, but the author made the text dark
+		 * (Infima's light theme: `--ifm-background-color:
+		 * transparent` on html, #1c1e21 text): the page expects the
+		 * white canvas of a normal browser, and dark text on the
+		 * dark default canvas is unreadable. */
+		if (!bg_found && (body || root) && bs.color != ui.theme.text) {
+			unsigned int c = bs.color;
+			unsigned int lum = ((c >> 11) & 31) * 8 * 299 +
+					   ((c >> 5) & 63) * 4 * 587 +
+					   (c & 31) * 8 * 114;
+
+			if (lum < 100 * 1000)
+				page_bg = 0xFFFF;
 		}
 		}
 	}
 	}
 	if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&
 	if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&

ファイルの差分が大きいため隠しています
+ 720 - 40
user/apps/browser/browser_html.c


+ 22 - 1
user/apps/browser/browser_html.h

@@ -20,6 +20,11 @@
 #define BR_FACE_FAMILY   40
 #define BR_FACE_FAMILY   40
 #define BR_FACE_URL      192
 #define BR_FACE_URL      192
 #define BR_MAX_VARS      512
 #define BR_MAX_VARS      512
+#define BR_MAX_LAYERS    48
+/* br_style.content for `content: ""`: a box without generated text. */
+#define BR_CONTENT_EMPTY 0x7FFF
+#define BR_RULE_USES_SCOPED    1
+#define BR_RULE_DEFINES_SCOPED 2
 #define BR_MAX_CONTENT   512
 #define BR_MAX_CONTENT   512
 #define BR_TAG_MAX       128
 #define BR_TAG_MAX       128
 #define BR_RULE_HASH     256
 #define BR_RULE_HASH     256
@@ -272,6 +277,13 @@ struct br_rule {
 	unsigned char nd;
 	unsigned char nd;
 	unsigned char single;       /* one compound, no ancestor walk needed */
 	unsigned char single;       /* one compound, no ancestor walk needed */
 	unsigned char pe;           /* 0 element, 1 ::before, 2 ::after */
 	unsigned char pe;           /* 0 element, 1 ::before, 2 ::after */
+	unsigned char layer;        /* 0 unlayered, else @layer order (1-based) */
+	/* BR_RULE_USES_SCOPED / BR_RULE_DEFINES_SCOPED: the block uses or
+	 * defines a custom property whose value depends on the element (see
+	 * br_style_for); raw_off/raw_len keep the unexpanded block. */
+	unsigned char var_scope;
+	unsigned short raw_len;
+	unsigned int raw_off;
 	struct br_decl d[BR_DECLS];
 	struct br_decl d[BR_DECLS];
 };
 };
 
 
@@ -308,6 +320,10 @@ struct br_doc {
 	unsigned char var_flags[BR_MAX_VARS];
 	unsigned char var_flags[BR_MAX_VARS];
 	unsigned short var_slot[1024];
 	unsigned short var_slot[1024];
 	unsigned int nvars;
 	unsigned int nvars;
+	/* @layer names (hash of the full dotted name) in order of first
+	 * appearance; a rule's `layer` indexes this table + 1. */
+	unsigned int layer_hash[BR_MAX_LAYERS];
+	unsigned int nlayers;
 	/* Rule index: rightmost class/id/tag -> linked list of rule indices. */
 	/* Rule index: rightmost class/id/tag -> linked list of rule indices. */
 	unsigned short rule_next[BR_MAX_RULES];
 	unsigned short rule_next[BR_MAX_RULES];
 	unsigned short rule_bucket[BR_RULE_HASH];
 	unsigned short rule_bucket[BR_RULE_HASH];
@@ -330,7 +346,9 @@ void br_doc_init(struct br_doc *d);
 /* ::before (pe 1) or ::after (pe 2) of an element: the pseudo-element's
 /* ::before (pe 1) or ::after (pe 2) of an element: the pseudo-element's
  * style (inheriting from the element's style `elem`) and its generated
  * style (inheriting from the element's style `elem`) and its generated
  * text.  Returns the text length; 0 when there is no content, or when the
  * text.  Returns the text length; 0 when there is no content, or when the
- * pseudo-element is display:none or absolutely positioned. */
+ * pseudo-element is display:none or absolutely positioned.  `content: ""`
+ * returns 0 with out->content == BR_CONTENT_EMPTY and a visible display
+ * when the box has a size and a background (an icon to draw). */
 unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
 unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
 		       const struct br_style *elem, struct br_style *out,
 		       const struct br_style *elem, struct br_style *out,
 		       char *buf, unsigned int cap);
 		       char *buf, unsigned int cap);
@@ -343,6 +361,9 @@ unsigned int br_parse_image_doc(struct br_doc *d, const char *src);
 /* External CSS is parsed by the browser after br_parse(); parse the inline
 /* External CSS is parsed by the browser after br_parse(); parse the inline
  * <style> blocks last so the cascade order matches the document. */
  * <style> blocks last so the cascade order matches the document. */
 void br_set_viewport(unsigned int width);
 void br_set_viewport(unsigned int width);
+/* A media query list (`screen and (min-width: 600px)`) against the
+ * viewport set above; window.matchMedia() uses the same answers as CSS. */
+int  br_media_matches(const char *query);
 void br_set_theme(unsigned short text, unsigned short bg,
 void br_set_theme(unsigned short text, unsigned short bg,
 		  unsigned short panel, unsigned short link);
 		  unsigned short panel, unsigned short link);
 void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);
 void br_parse_css_text(struct br_doc *d, const char *css, unsigned int len);

+ 1 - 1
user/apps/browser/js_browser.c

@@ -3151,7 +3151,7 @@ static void win_matchmedia(js_State *state)
 	const char *query = js_tostring(state, 1);
 	const char *query = js_tostring(state, 1);
 
 
 	js_newobject(state);
 	js_newobject(state);
-	js_pushboolean(state, 0);
+	js_pushboolean(state, br_media_matches(query));
 	js_setproperty(state, -2, "matches");
 	js_setproperty(state, -2, "matches");
 	js_pushstring(state, query);
 	js_pushstring(state, query);
 	js_setproperty(state, -2, "media");
 	js_setproperty(state, -2, "media");

+ 1322 - 0
user/apps/disks/ucmd_disks.c

@@ -0,0 +1,1322 @@
+/* disks: the ngui storage manager.
+ *
+ * Lists every block device (SYS_BLKDEV) with its MBR partitions, read as raw
+ * sectors through SYS_BLK_READ, and every mounted filesystem.  A FAT volume
+ * can be mounted/unmounted, renamed and made read-only (SYS_MOUNT_CTL; the
+ * policies are saved to /data/MOUNTS.CFG and applied again by the boot
+ * scan).  A device can be formatted or have NebOS installed on it: those run
+ * the privileged `format -y` / `install -y` kernel commands on a private tty
+ * and show their output, after this window asked for confirmation.  The
+ * `storage` command remains the text view. */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "ngui.h"
+#include "ucmd_lib.h"
+
+#define MAX_DISKS   16
+#define MAX_MOUNTS  24                  /* VFS_MAX_MOUNTS */
+#define MAX_VOLS    5
+
+#define TOOLBAR_H   40
+#define LIST_W      230
+#define SECTION_H   28
+#define ROW_H       42
+#define GAP         10
+
+#define KIND_FLOPPY 0
+
+enum {
+	ID_RESCAN = 1,
+	ID_OVERVIEW,
+	ID_FORMAT,
+	ID_INSTALL,
+	ID_MOUNT_TOGGLE,
+	ID_RO,
+	ID_AUTO,
+	ID_NAME,
+	ID_APPLY,
+	ID_RESET,
+	ID_OPEN,
+	ID_DLG_SIZE,
+	ID_DLG_CANCEL,
+	ID_DLG_OK,
+	ID_DLG_OK2,
+	ID_DLG_CLOSE,
+	ID_DISK_BASE = 100,
+	ID_PART_BASE = 200,
+	ID_MOUNT_BASE = 300,
+};
+
+/* One MBR partition (or the whole device for a floppy/superfloppy). */
+struct part {
+	int slot;                       /* MBR slot 0..3, -1 = whole device */
+	unsigned char type, active;
+	unsigned int start, sectors;
+	int fat;                        /* has a FAT boot sector */
+	int nebos_boot;                 /* the NebOS boot sector + kernel */
+	char fs[8];
+	char label[12];
+	char key[16];                   /* default mount name */
+};
+
+struct disk {
+	struct sys_blkdev bd;
+	unsigned int index;             /* SYS_BLKDEV / SYS_BLK_READ index */
+	int read_ok;                    /* sector 0 was readable */
+	int mbr;                        /* a valid partition table */
+	int nparts;
+	struct part parts[4];
+	int nebos;                      /* boot partition + rootfs partition */
+	int has_data;                   /* a NEBOSDATA partition */
+};
+
+enum dialog { DLG_NONE, DLG_FORMAT, DLG_INSTALL, DLG_JOB };
+
+static struct ngui ui;
+static struct disk disks[MAX_DISKS];
+static int ndisks;
+static struct sys_mount mounts[MAX_MOUNTS];
+static int nmounts;
+static struct sys_mount_policy pols[SYS_MOUNT_POLICY_MAX];
+static int npols;
+
+static int sel_disk = -1;               /* -1 = mount overview */
+static char sel_key[16];                /* selected volume */
+static char edit_key[16];               /* volume the editor was loaded for */
+static struct ngui_input name_in;
+static int edit_ro, edit_auto;
+static struct ngui_scroll detail_scroll, list_scroll;
+
+static char status[128];
+static int status_err;
+static unsigned int status_until;
+
+static enum dialog dialog;
+static int rescan_pending;
+static struct ngui_input size_in;
+static int dlg_disk;
+
+/* The running format/install. */
+static int job_tty = -1, job_pid = -1;
+static int job_done, job_ok;
+static char job_title[64];
+static char job_log[4096];
+static unsigned int job_len;
+
+/* ---- helpers ---- */
+
+static void set_status(const char *msg, int err)
+{
+	snprintf(status, sizeof(status), "%s", msg);
+	status_err = err;
+	status_until = u_ticks_ms() + 8000;
+	ngui_invalidate(&ui);
+}
+
+static unsigned int rd32(const unsigned char *p)
+{
+	return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+static void fmt_size(char *out, unsigned int n, unsigned int sectors)
+{
+	if (sectors >= 2u * 1024u * 1024u)
+		snprintf(out, n, "%u.%u GB", sectors >> 21,
+			 ((sectors & 0x1FFFFFu) * 10u) >> 21);
+	else if (sectors >= 2048u)
+		snprintf(out, n, "%u.%u MB", sectors >> 11,
+			 ((sectors & 0x7FFu) * 10u) >> 11);
+	else
+		snprintf(out, n, "%u KB", sectors / 2u);
+}
+
+static const char *kind_name(unsigned int kind)
+{
+	static const char *names[] = { "Floppy", "IDE", "SATA", "eMMC", "USB" };
+
+	return kind < 5 ? names[kind] : "Disk";
+}
+
+static const char *type_name(unsigned char t)
+{
+	switch (t) {
+	case 0x01: return "FAT12";
+	case 0x04: case 0x06: case 0x0E: return "FAT16";
+	case 0x0B: case 0x0C: return "FAT32";
+	case 0x05: case 0x0F: return "Extended";
+	case 0x07: return "NTFS/exFAT";
+	case 0x7F: return "NebOS rootfs";
+	case 0x82: return "Linux swap";
+	case 0x83: return "Linux";
+	case 0xEE: return "GPT";
+	case 0xEF: return "EFI System";
+	default:   return "Unknown";
+	}
+}
+
+/* Copy a space-padded BPB field and trim it. */
+static void bpb_str(char *out, const unsigned char *p, unsigned int n)
+{
+	unsigned int i;
+
+	for (i = 0; i < n; i++)
+		out[i] = (p[i] >= 0x20 && p[i] < 0x7F) ? (char)p[i] : ' ';
+	while (i > 0 && out[i - 1] == ' ')
+		i--;
+	out[i] = '\0';
+}
+
+/* Classify a partition's (or a superfloppy's) first sector. */
+static void probe_boot_sector(struct part *p, const unsigned char *s)
+{
+	unsigned int bps = s[11] | (s[12] << 8);
+
+	p->fat = 0;
+	p->nebos_boot = 0;
+	p->fs[0] = p->label[0] = '\0';
+	if (s[510] != 0x55 || s[511] != 0xAA)
+		return;
+	if (s[3] == 'N' && s[4] == 'E' && s[5] == 'B' && s[6] == 'O' &&
+	    s[7] == 'S') {
+		p->nebos_boot = 1;
+		snprintf(p->fs, sizeof(p->fs), "NebOS");
+		return;
+	}
+	if (bps != 512 || (s[0] != 0xEB && s[0] != 0xE9) || s[13] == 0)
+		return;
+	p->fat = 1;
+	if ((s[0x16] | (s[0x17] << 8)) == 0) {          /* FAT32 BPB */
+		snprintf(p->fs, sizeof(p->fs), "FAT32");
+		bpb_str(p->label, s + 0x47, 11);
+	} else {
+		bpb_str(p->fs, s + 0x36, 5);
+		if (strncmp(p->fs, "FAT", 3) != 0)
+			snprintf(p->fs, sizeof(p->fs), "FAT");
+		bpb_str(p->label, s + 0x2B, 11);
+	}
+	if (!strcmp(p->label, "NO NAME"))
+		p->label[0] = '\0';
+}
+
+static int blk_read(unsigned int index, unsigned int lba, unsigned char *buf)
+{
+	return u_syscall3(SYS_BLK_READ, (int)index, (int)lba, (int)buf);
+}
+
+/* Read the partition table and the first sector of every partition.  A
+ * floppy is never read here: without a disk the controller retries for
+ * seconds, and it has no partitions anyway. */
+static void probe_disk(struct disk *d)
+{
+	static unsigned char s[512];
+	unsigned int i;
+
+	d->read_ok = d->mbr = d->nparts = d->nebos = d->has_data = 0;
+	if (!d->bd.present)
+		return;
+	if (d->bd.kind == KIND_FLOPPY) {
+		struct part *p = &d->parts[d->nparts++];
+
+		memset(p, 0, sizeof(*p));
+		p->slot = -1;
+		p->sectors = d->bd.total_sectors;
+		p->fat = 1;
+		snprintf(p->key, sizeof(p->key), "%s", d->bd.name);
+		return;
+	}
+	if (blk_read(d->index, 0, s) != 0)
+		return;
+	d->read_ok = 1;
+	if (s[510] == 0x55 && s[511] == 0xAA) {
+		int ok = 1, n = 0;
+
+		for (i = 0; i < 4; i++) {
+			const unsigned char *e = s + 0x1BE + i * 16;
+			unsigned int start = rd32(e + 8), cnt = rd32(e + 12);
+
+			if (e[4] == 0)
+				continue;
+			if ((e[0] != 0 && e[0] != 0x80) || start == 0 ||
+			    cnt == 0 || start > d->bd.total_sectors ||
+			    cnt > d->bd.total_sectors - start)
+				ok = 0;
+			n++;
+		}
+		d->mbr = ok && n > 0;
+	}
+	if (!d->mbr) {
+		struct part *p = &d->parts[0];
+
+		memset(p, 0, sizeof(*p));
+		p->slot = -1;
+		p->sectors = d->bd.total_sectors;
+		probe_boot_sector(p, s);
+		snprintf(p->key, sizeof(p->key), "%s", d->bd.name);
+		if (p->fat)
+			d->nparts = 1;
+		return;
+	}
+	for (i = 0; i < 4; i++) {
+		const unsigned char *e = s + 0x1BE + i * 16;
+		struct part *p;
+
+		if (e[4] == 0)
+			continue;
+		p = &d->parts[d->nparts++];
+		memset(p, 0, sizeof(*p));
+		p->slot = (int)i;
+		p->type = e[4];
+		p->active = e[0] == 0x80;
+		p->start = rd32(e + 8);
+		p->sectors = rd32(e + 12);
+		snprintf(p->key, sizeof(p->key), "%sp%u", d->bd.name, i + 1);
+	}
+	for (i = 0; i < (unsigned int)d->nparts; i++) {
+		struct part *p = &d->parts[i];
+
+		if (p->type == 0x05 || p->type == 0x0F || p->type == 0xEE ||
+		    p->type == 0x7F)
+			continue;
+		if (blk_read(d->index, p->start, s) == 0)
+			probe_boot_sector(p, s);
+		if (p->nebos_boot && p->slot == 0)
+			d->nebos |= 1;
+		if (p->fat && !strcmp(p->label, "NEBOSDATA"))
+			d->has_data = 1;
+	}
+	for (i = 0; i < (unsigned int)d->nparts; i++)
+		if (d->parts[i].type == 0x7F)
+			d->nebos |= 2;
+	d->nebos = d->nebos == 3;
+}
+
+static void load_mounts(void)
+{
+	int i;
+
+	nmounts = 0;
+	for (i = 0; i < MAX_MOUNTS; i++) {
+		if (u_syscall2(SYS_LIST_MOUNTS, i, (int)&mounts[nmounts]) != 1)
+			break;
+		nmounts++;
+	}
+	npols = 0;
+	for (i = 0; i < SYS_MOUNT_POLICY_MAX; i++) {
+		if (u_syscall3(SYS_MOUNT_CTL, SYS_MOUNT_POLICY_GET, i,
+			       (int)&pols[npols]) != 1)
+			break;
+		npols++;
+	}
+}
+
+/* USB INQUIRY strings are space-padded vendor/product fields. */
+static void squeeze(char *s)
+{
+	char *o = s;
+	const char *p;
+
+	for (p = s; *p; p++)
+		if (*p != ' ' || (o > s && o[-1] != ' '))
+			*o++ = *p;
+	if (o > s && o[-1] == ' ')
+		o--;
+	*o = '\0';
+}
+
+static void load_all(void)
+{
+	int i;
+
+	ndisks = 0;
+	for (i = 0; i < MAX_DISKS; i++) {
+		struct disk *d = &disks[ndisks];
+
+		if (u_syscall2(SYS_BLKDEV, i, (int)&d->bd) != 1)
+			break;
+		d->index = (unsigned int)i;
+		squeeze(d->bd.model);
+		probe_disk(d);
+		ndisks++;
+	}
+	if (sel_disk >= ndisks)
+		sel_disk = -1;
+	load_mounts();
+	edit_key[0] = '\0';         /* reload the editor from the new state */
+}
+
+static const struct sys_mount *mount_by_key(const char *key)
+{
+	int i;
+
+	for (i = 0; i < nmounts; i++)
+		if (mounts[i].dev[0] && !strcmp(mounts[i].key, key))
+			return &mounts[i];
+	return 0;
+}
+
+static const struct sys_mount *mount_by_label(const char *label)
+{
+	int i;
+
+	for (i = 0; i < nmounts; i++)
+		if (mounts[i].dev[0] && !strcmp(mounts[i].label, label))
+			return &mounts[i];
+	return 0;
+}
+
+static const struct sys_mount_policy *policy_by_key(const char *key)
+{
+	int i;
+
+	for (i = 0; i < npols; i++)
+		if (!strcmp(pols[i].key, key))
+			return &pols[i];
+	return 0;
+}
+
+/* The volume carrying the saved policies: never edited from here, since an
+ * unmounted, renamed or read-only /data could not store the change. */
+static int is_data_volume(const struct part *p)
+{
+	const struct sys_mount *m = mount_by_key(p->key);
+
+	return !strcmp(p->label, "NEBOSDATA") ||
+	       (m && !strcmp(m->name, "data"));
+}
+
+/* ---- mount policies ---- */
+
+static int save_policies(void)
+{
+	static struct {
+		char magic[8];
+		struct sys_mount_policy p[SYS_MOUNT_POLICY_MAX];
+	} cfg;
+	const struct sys_mount *data = mount_by_label("NEBOSDATA");
+	char path[48];
+
+	if (!data)
+		return 0;
+	memcpy(cfg.magic, SYS_MOUNT_CFG_MAGIC, 8);
+	memcpy(cfg.p, pols, sizeof(pols[0]) * (unsigned int)npols);
+	snprintf(path, sizeof(path), "/%s/MOUNTS.CFG", data->name);
+	return u_syscall3(SYS_WRITE_FILE, (int)path, (int)&cfg,
+			  8 + (int)sizeof(pols[0]) * npols) == 0 ? 1 : -1;
+}
+
+static int name_valid(const char *s)
+{
+	unsigned int i;
+
+	for (i = 0; s[i]; i++)
+		if (i >= 15 || !((s[i] >= 'a' && s[i] <= 'z') ||
+				 (s[i] >= '0' && s[i] <= '9') ||
+				 s[i] == '_' || s[i] == '-'))
+			return 0;
+	return 1;
+}
+
+static void apply_policy(const char *key, const char *name, unsigned int flags)
+{
+	struct sys_mount_policy p;
+	const struct sys_mount *cur = mount_by_key(key);
+	char msg[96];
+	int i, saved;
+
+	if (!name_valid(name)) {
+		set_status("Mount name: up to 15 of a-z, 0-9, _ and -", 1);
+		return;
+	}
+	if (!strcmp(name, "ramfs") || !strcmp(name, "share") ||
+	    !strcmp(name, "data")) {
+		set_status("That mount name is reserved", 1);
+		return;
+	}
+	for (i = 0; name[0] && i < nmounts; i++)
+		if (!strcmp(mounts[i].name, name) && &mounts[i] != cur) {
+			set_status("Another volume is mounted with that name", 1);
+			return;
+		}
+	memset(&p, 0, sizeof(p));
+	snprintf(p.key, sizeof(p.key), "%s", key);
+	if (strcmp(name, key) != 0)
+		snprintf(p.name, sizeof(p.name), "%s", name);
+	p.flags = flags;
+	if (u_syscall3(SYS_MOUNT_CTL, SYS_MOUNT_POLICY_SET, (int)&p, 0) != 0) {
+		set_status("The kernel refused the mount settings", 1);
+		return;
+	}
+	load_mounts();
+	edit_key[0] = '\0';
+	saved = save_policies();
+	cur = mount_by_key(key);
+	if (flags & SYS_MOUNT_NOAUTO)
+		snprintf(msg, sizeof(msg), "%s is unmounted", key);
+	else if (cur)
+		snprintf(msg, sizeof(msg), "%s is mounted at /%s%s", key,
+			 cur->name, cur->ro ? " (read-only)" : "");
+	else
+		snprintf(msg, sizeof(msg), "%s has no readable filesystem", key);
+	if (saved == 0)
+		snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), "; not saved (no /data)");
+	else if (saved < 0)
+		snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), "; saving failed");
+	set_status(msg, !cur && !(flags & SYS_MOUNT_NOAUTO));
+	/* Applied, but only until the next boot: a warning, not an error. */
+	if (saved == 0 && (cur || (flags & SYS_MOUNT_NOAUTO)))
+		status_err = 2;
+}
+
+/* ---- format / install jobs ---- */
+
+static void start_job(const char *cmd, const char *arg, const char *title)
+{
+	if (job_tty < 0)
+		job_tty = u_tty_create();
+	if (job_tty < 0) {
+		set_status("No terminal for the command", 1);
+		return;
+	}
+	job_len = 0;
+	job_log[0] = '\0';
+	job_done = job_ok = 0;
+	snprintf(job_title, sizeof(job_title), "%s", title);
+	job_pid = u_spawn_kcmd(cmd, arg, job_tty);
+	if (job_pid < 0) {
+		set_status("Could not start the command", 1);
+		dialog = DLG_NONE;
+		return;
+	}
+	dialog = DLG_JOB;
+	ngui_invalidate(&ui);
+}
+
+/* The log is drawn in a 13 px monospace face; longer lines are wrapped
+ * here so the dialog never clips them. */
+#define JOB_LOG_COLS 66
+
+static void job_append(const char *s, int n)
+{
+	static unsigned int col;
+	int i;
+
+	for (i = 0; i < n; i++) {
+		if (s[i] == '\r')
+			continue;
+		if (s[i] != '\n' && col >= JOB_LOG_COLS) {
+			job_append("\n", 1);
+		}
+		col = s[i] == '\n' ? 0 : col + 1;
+		if (job_len + 1 >= sizeof(job_log)) {
+			/* Keep the newest half of the log. */
+			unsigned int keep = sizeof(job_log) / 2;
+
+			memmove(job_log, job_log + job_len - keep, keep);
+			job_len = keep;
+		}
+		job_log[job_len++] = s[i];
+	}
+	job_log[job_len] = '\0';
+}
+
+static void job_poll(void)
+{
+	char buf[256];
+	int n, any = 0;
+
+	if (job_tty < 0)
+		return;
+	while ((n = u_tty_read(job_tty, buf, sizeof(buf))) > 0) {
+		if (dialog == DLG_JOB && !job_done)
+			job_append(buf, n);
+		any = 1;
+	}
+	if (dialog == DLG_JOB && !job_done && job_pid > 0 &&
+	    !u_alive((unsigned int)job_pid)) {
+		while ((n = u_tty_read(job_tty, buf, sizeof(buf))) > 0)
+			job_append(buf, n);
+		job_done = 1;
+		job_pid = -1;
+		job_ok = strstr(job_log, "done") && !strstr(job_log, "failed") &&
+			 !strstr(job_log, "aborted");
+		load_all();
+		any = 1;
+	}
+	if (any)
+		ngui_invalidate(&ui);
+}
+
+/* ---- drawing ---- */
+
+/* Widgets drawn under a dialog must not react: hide the pointer edges. */
+struct input_save {
+	unsigned int pressed, released;
+	int inside, scroll_y;
+};
+
+static void input_mute(struct input_save *s)
+{
+	s->pressed = ui.pressed;
+	s->released = ui.released;
+	s->inside = ui.inside;
+	s->scroll_y = ui.scroll_y;
+	ui.pressed = ui.released = 0;
+	ui.inside = 0;
+	ui.scroll_y = 0;
+}
+
+static void input_restore(const struct input_save *s)
+{
+	ui.pressed = s->pressed;
+	ui.released = s->released;
+	ui.inside = s->inside;
+	ui.scroll_y = s->scroll_y;
+}
+
+static int visible(struct ngui_rect r, struct ngui_rect area)
+{
+	return r.y + r.h > area.y && r.y < area.y + area.h;
+}
+
+static void text_dim(int x, int y, const char *s)
+{
+	ngui_text(&ui, x, y, s, ui.theme.text_dim);
+}
+
+static void draw_toolbar(struct ngui_rect r)
+{
+	struct ngui_rect b;
+	unsigned short c;
+
+	ngui_fill_rect(&ui, r, ui.theme.panel);
+	ngui_inset(&r, 6);
+	b = ngui_cut_left(&r, 92);
+	if (ngui_button(&ui, ID_RESCAN, b, "Rescan")) {
+		set_status("Scanning devices...", 0);
+		rescan_pending = 1;
+	}
+	ngui_cut_left(&r, 12);
+	if (status[0]) {
+		c = status_err == 2 ? ui.theme.warning :
+		    status_err ? ui.theme.error : ui.theme.success;
+		ngui_text_ellipsis(&ui, r, r.x, r.y + (r.h - 16) / 2, status, c);
+	} else {
+		ngui_text_ellipsis(&ui, r, r.x, r.y + (r.h - 16) / 2,
+				   "Storage devices, volumes and mounts",
+				   ui.theme.text_dim);
+	}
+}
+
+/* ngui's scroll pane paints its bar over the right edge of the pane, so the
+ * content must leave that strip free while the pane scrolls. */
+#define SCROLLBAR_ROOM 20
+
+static struct ngui_rect scroll_content(struct ngui_rect area,
+				       const struct ngui_scroll *s)
+{
+	if (!s->hide_bar && s->content_h > s->view_h)
+		area.w -= SCROLLBAR_ROOM;
+	return area;
+}
+
+/* Empty IDE channels and absent eMMC/USB slots are registered as devices
+ * but have nothing to show; a floppy drive is listed without media too. */
+static int listed(const struct disk *d)
+{
+	return d->bd.present || d->bd.kind == KIND_FLOPPY;
+}
+
+static void draw_device_list(struct ngui_rect area)
+{
+	struct ngui_row row;
+	struct ngui_rect r, col;
+	char sub[64], size[16];
+	int i, y;
+
+	ngui_fill_rect(&ui, area, ui.theme.panel);
+	ngui_inset(&area, 6);
+	for (i = 0, y = 0; i < ndisks; i++)
+		y += listed(&disks[i]);
+	list_scroll.content_h = (y + 1) * ROW_H + 2 * SECTION_H;
+	ngui_scroll_begin(&ui, area, &list_scroll);
+	ngui_scroll_wheel(&ui, area, &list_scroll);
+	col = scroll_content(area, &list_scroll);
+	y = area.y - list_scroll.offset;
+
+	r = (struct ngui_rect){ col.x, y, col.w, SECTION_H };
+	ngui_section(&ui, r, "Overview", 0, 0);
+	y += SECTION_H;
+	r = (struct ngui_rect){ col.x, y, col.w, ROW_H };
+	if (visible(r, area)) {
+		snprintf(sub, sizeof(sub), "%d filesystem%s", nmounts,
+			 nmounts == 1 ? "" : "s");
+		row.title = "All mounts";
+		row.subtitle = sub;
+		row.trailing = 0;
+		row.bars = -1;
+		row.selected = sel_disk < 0;
+		if (ngui_list_row(&ui, ID_OVERVIEW, r, &row)) {
+			sel_disk = -1;
+			detail_scroll.offset = 0;
+			ngui_invalidate(&ui);
+		}
+	}
+	y += ROW_H;
+
+	r = (struct ngui_rect){ col.x, y, col.w, SECTION_H };
+	ngui_section(&ui, r, "Devices", 0, 0);
+	y += SECTION_H;
+	for (i = 0; i < ndisks; i++) {
+		const struct disk *d = &disks[i];
+
+		if (!listed(d))
+			continue;
+		r = (struct ngui_rect){ col.x, y, col.w, ROW_H };
+		y += ROW_H;
+		if (!visible(r, area))
+			continue;
+		fmt_size(size, sizeof(size), d->bd.total_sectors);
+		if (!d->bd.present)
+			snprintf(sub, sizeof(sub), "%s, not present",
+				 kind_name(d->bd.kind));
+		else
+			snprintf(sub, sizeof(sub), "%s %s",
+				 kind_name(d->bd.kind),
+				 d->bd.kind == KIND_FLOPPY ? "" : size);
+		row.title = d->bd.name;
+		row.subtitle = sub;
+		row.trailing = d->nebos ? "NebOS" : 0;
+		row.bars = -1;
+		row.selected = sel_disk == i;
+		if (ngui_list_row(&ui, ID_DISK_BASE + i, r, &row) &&
+		    sel_disk != i) {
+			sel_disk = i;
+			sel_key[0] = '\0';
+			detail_scroll.offset = 0;
+			ngui_invalidate(&ui);
+		}
+	}
+	ngui_scroll_end(&ui, area, &list_scroll);
+}
+
+/* The overview: one line per mount. */
+static int draw_overview(struct ngui_rect area, int y)
+{
+	static const int col[] = { 0, 96, 156, 230, 330, 404 };
+	static const char *hdr[] = { "Mount", "Type", "Size", "Label",
+				     "Device", "Mode" };
+	char size[16];
+	int i, j;
+
+	ngui_text_px(&ui, area.x, y, "All mounts", 10, ui.theme.text, 18, 1, 0, 0);
+	y += 32;
+	for (j = 0; j < 6; j++)
+		text_dim(area.x + col[j], y, hdr[j]);
+	y += 22;
+	ngui_fill_rect(&ui, (struct ngui_rect){ area.x, y - 4, area.w, 1 },
+		       ui.theme.border);
+	for (i = 0; i < nmounts; i++) {
+		const struct sys_mount *m = &mounts[i];
+		struct ngui_rect r = { area.x, y, area.w, 26 };
+		char name[20];
+
+		if (visible(r, area)) {
+			snprintf(name, sizeof(name), "/%s", m->name);
+			fmt_size(size, sizeof(size), m->total_sectors);
+			ngui_text(&ui, area.x + col[0], y + 4, name, ui.theme.text);
+			ngui_text(&ui, area.x + col[1], y + 4, m->type, ui.theme.text);
+			ngui_text(&ui, area.x + col[2], y + 4, size, ui.theme.text);
+			ngui_text(&ui, area.x + col[3], y + 4, m->label, ui.theme.text);
+			ngui_text(&ui, area.x + col[4], y + 4,
+				  m->dev[0] ? m->key : "RAM", ui.theme.text_dim);
+			ngui_text(&ui, area.x + col[5], y + 4,
+				  m->ro ? "read-only" : "read-write",
+				  m->ro ? ui.theme.warning : ui.theme.text_dim);
+			if (m->dev[0] && ngui_button(&ui, ID_MOUNT_BASE + i,
+				(struct ngui_rect){ area.x + area.w - 60, y + 1,
+						    60, 24 }, "Show")) {
+				for (j = 0; j < ndisks; j++)
+					if (!strcmp(disks[j].bd.name, m->dev))
+						sel_disk = j;
+				snprintf(sel_key, sizeof(sel_key), "%s", m->key);
+				detail_scroll.offset = 0;
+				ngui_invalidate(&ui);
+			}
+		}
+		y += 28;
+	}
+	y += GAP;
+	text_dim(area.x, y,
+		 "Select a device to format it, install NebOS or change how");
+	y += 18;
+	text_dim(area.x, y, "its volumes are mounted.");
+	return y + 24;
+}
+
+static void load_editor(const struct part *p)
+{
+	const struct sys_mount_policy *pol = policy_by_key(p->key);
+	const struct sys_mount *m = mount_by_key(p->key);
+
+	snprintf(edit_key, sizeof(edit_key), "%s", p->key);
+	if (pol && pol->name[0])
+		ngui_input_set(&name_in, pol->name);
+	else
+		ngui_input_set(&name_in, m ? m->name : p->key);
+	edit_ro = pol && (pol->flags & SYS_MOUNT_RO);
+	edit_auto = !(pol && (pol->flags & SYS_MOUNT_NOAUTO));
+}
+
+static int draw_mount_editor(struct ngui_rect area, int y, const struct part *p)
+{
+	const struct sys_mount *m = mount_by_key(p->key);
+	struct ngui_rect r, b;
+	char line[96], name[16];
+	unsigned int flags;
+
+	if (strcmp(edit_key, p->key) != 0)
+		load_editor(p);
+
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
+	snprintf(line, sizeof(line), "Volume %s", p->key);
+	ngui_section(&ui, r, line, 0, 0);
+	y += SECTION_H;
+
+	if (m)
+		snprintf(line, sizeof(line), "Mounted at /%s, %s, %s", m->name,
+			 m->type, m->ro ? "read-only" : "read-write");
+	else
+		snprintf(line, sizeof(line), "Not mounted");
+	ngui_text(&ui, area.x, y + 2, line, m ? ui.theme.success : ui.theme.text_dim);
+	y += 24;
+
+	if (is_data_volume(p)) {
+		text_dim(area.x, y,
+			 "This is the NebOS data volume (/data).  It holds the");
+		y += 18;
+		text_dim(area.x, y,
+			 "saved settings, so it is always mounted read-write.");
+		y += 26;
+		if (m && ngui_button(&ui, ID_OPEN,
+				     (struct ngui_rect){ area.x, y, 130, 28 },
+				     "Open in Files"))
+			goto open_files;
+		return y + 38;
+	}
+
+	text_dim(area.x, y + 6, "Mount name");
+	r = (struct ngui_rect){ area.x + 110, y, 180, 28 };
+	if (visible(r, area))
+		ngui_text_input(&ui, ID_NAME, r, &name_in);
+	y += 34;
+
+	r = (struct ngui_rect){ area.x, y, 290, 26 };
+	if (visible(r, area) &&
+	    ngui_menu_item(&ui, ID_AUTO, r, "Mount automatically", 0, edit_auto))
+		edit_auto = !edit_auto;
+	y += 28;
+	r = (struct ngui_rect){ area.x, y, 290, 26 };
+	if (visible(r, area) &&
+	    ngui_menu_item(&ui, ID_RO, r, "Read-only", 0, edit_ro))
+		edit_ro = !edit_ro;
+	y += 34;
+
+	ngui_input_value(&name_in, name, sizeof(name));
+	flags = (edit_ro ? SYS_MOUNT_RO : 0) | (edit_auto ? 0 : SYS_MOUNT_NOAUTO);
+
+	b = (struct ngui_rect){ area.x, y, 90, 28 };
+	if (visible(b, area) && ngui_button(&ui, ID_APPLY, b, "Apply"))
+		apply_policy(p->key, name, flags);
+	b.x += 98;
+	b.w = 100;
+	if (visible(b, area) &&
+	    ngui_button(&ui, ID_MOUNT_TOGGLE, b, m ? "Unmount" : "Mount")) {
+		if (m)
+			apply_policy(p->key, name, flags | SYS_MOUNT_NOAUTO);
+		else
+			apply_policy(p->key, name, flags & ~SYS_MOUNT_NOAUTO);
+	}
+	b.x += 108;
+	b.w = 90;
+	if (visible(b, area) && ngui_button(&ui, ID_RESET, b, "Defaults"))
+		apply_policy(p->key, "", 0);
+	b.x += 98;
+	b.w = 130;
+	if (m && visible(b, area) && ngui_button(&ui, ID_OPEN, b, "Open in Files"))
+		goto open_files;
+	y += 38;
+	text_dim(area.x, y,
+		 "Settings apply at once and are saved to /data/MOUNTS.CFG,");
+	y += 18;
+	text_dim(area.x, y, "so the boot scan uses them too.");
+	return y + 26;
+
+open_files:
+	{
+		char cwd[24];
+
+		snprintf(cwd, sizeof(cwd), "/%s", m->name);
+		if (u_spawn("/ramfs/bin/files", "", 0, cwd) < 0)
+			set_status("Could not start files", 1);
+	}
+	return y + 38;
+}
+
+static int draw_disk(struct ngui_rect area, int y, struct disk *d)
+{
+	struct ngui_row row;
+	struct ngui_rect r, b;
+	char line[128], size[16], title[48], sub[80];
+	const struct part *sel = 0;
+	int i;
+
+	snprintf(title, sizeof(title), "%s", d->bd.name);
+	ngui_text_px(&ui, area.x, y, title, (unsigned int)strlen(title),
+		     ui.theme.text, 18, 1, 0, 0);
+	y += 30;
+	fmt_size(size, sizeof(size), d->bd.total_sectors);
+	if (d->bd.kind == KIND_FLOPPY)
+		snprintf(line, sizeof(line), "%s drive", kind_name(d->bd.kind));
+	else
+		snprintf(line, sizeof(line), "%s%s%s, %s (%u sectors)",
+			 kind_name(d->bd.kind), d->bd.model[0] ? " " : "",
+			 d->bd.model, size, d->bd.total_sectors);
+	text_dim(area.x, y, line);
+	y += 20;
+	if (!d->bd.present) {
+		ngui_text(&ui, area.x, y, "Not present", ui.theme.warning);
+		return y + 30;
+	}
+	if (d->nebos) {
+		ngui_text(&ui, area.x, y, d->has_data ?
+			  "NebOS installed, with a /data partition" :
+			  "NebOS installed", ui.theme.success);
+		y += 20;
+	}
+	y += GAP;
+
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
+	ngui_section(&ui, r, d->mbr ? "Partitions" : "Volume", 0, 0);
+	y += SECTION_H;
+	if (d->bd.kind != KIND_FLOPPY && !d->read_ok) {
+		ngui_text(&ui, area.x, y + 4, "The device could not be read",
+			  ui.theme.error);
+		y += 30;
+	} else if (!d->nparts) {
+		text_dim(area.x, y + 4,
+			 "No partition table and no FAT filesystem");
+		y += 30;
+	}
+	for (i = 0; i < d->nparts; i++) {
+		const struct part *p = &d->parts[i];
+		const struct sys_mount *m = mount_by_key(p->key);
+		const struct sys_mount_policy *pol = policy_by_key(p->key);
+		const char *what;
+		char trail[24];
+		int selectable = p->fat && !p->nebos_boot;
+
+		r = (struct ngui_rect){ area.x, y, area.w, ROW_H };
+		y += ROW_H;
+		if (selectable && !sel_key[0])
+			snprintf(sel_key, sizeof(sel_key), "%s", p->key);
+		if (selectable && !strcmp(sel_key, p->key))
+			sel = p;
+		if (!visible(r, area))
+			continue;
+		if (p->nebos_boot)
+			what = "NebOS boot + kernel";
+		else if (p->fat)
+			what = p->fs;
+		else
+			what = type_name(p->type);
+		fmt_size(size, sizeof(size), p->sectors);
+		if (p->slot >= 0)
+			snprintf(title, sizeof(title), "%d   %s%s%s", p->slot + 1,
+				 what, p->label[0] ? "  " : "", p->label);
+		else
+			/* A floppy is never read here; its mount has the label. */
+			snprintf(title, sizeof(title), "%s%s%s",
+				 d->bd.kind == KIND_FLOPPY ? "Floppy disk" : what,
+				 (p->label[0] || (m && m->label[0])) ? "  " : "",
+				 p->label[0] ? p->label : (m ? m->label : ""));
+		if (p->slot >= 0)
+			snprintf(sub, sizeof(sub), "%s, type 0x%02X, LBA %u%s",
+				 size, p->type, p->start,
+				 p->active ? ", active" : "");
+		else
+			snprintf(sub, sizeof(sub), "%s, whole device",
+				 d->bd.kind == KIND_FLOPPY ? "1.4 MB" : size);
+		if (m)
+			snprintf(trail, sizeof(trail), "/%s%s", m->name,
+				 m->ro ? " (ro)" : "");
+		else if (pol && (pol->flags & SYS_MOUNT_NOAUTO))
+			snprintf(trail, sizeof(trail), "unmounted");
+		else
+			snprintf(trail, sizeof(trail), "%s",
+				 selectable ? "not mounted" : "");
+		row.title = title;
+		row.subtitle = sub;
+		row.trailing = trail[0] ? trail : 0;
+		row.bars = -1;
+		row.selected = sel == p;
+		if (ngui_list_row(&ui, ID_PART_BASE + d->index * 4 +
+				  (unsigned int)(p->slot < 0 ? 0 : p->slot),
+				  r, &row) && selectable) {
+			snprintf(sel_key, sizeof(sel_key), "%s", p->key);
+			ngui_invalidate(&ui);
+		}
+	}
+	y += GAP;
+	if (sel)
+		y = draw_mount_editor(area, y, sel) + GAP;
+
+	r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
+	ngui_section(&ui, r, "Device", 0, 0);
+	y += SECTION_H;
+	b = (struct ngui_rect){ area.x, y, 120, 28 };
+	if (visible(b, area) && ngui_button(&ui, ID_FORMAT, b, "Format...")) {
+		dialog = DLG_FORMAT;
+		dlg_disk = sel_disk;
+		ngui_input_set(&size_in, "");
+		ngui_set_focus(&ui, ID_DLG_SIZE);
+	}
+	b.x += 128;
+	b.w = 150;
+	if (d->bd.kind != KIND_FLOPPY && visible(b, area) &&
+	    ngui_button(&ui, ID_INSTALL, b, "Install NebOS...")) {
+		dialog = DLG_INSTALL;
+		dlg_disk = sel_disk;
+	}
+	y += 36;
+	text_dim(area.x, y, d->bd.kind == KIND_FLOPPY ?
+		 "Format writes a fresh FAT12 filesystem." :
+		 "Format writes one FAT partition (MBR) over the whole disk.");
+	return y + 26;
+}
+
+static void draw_details(struct ngui_rect area)
+{
+	struct ngui_rect col;
+	int y, top, had_bar;
+
+	ngui_inset(&area, 14);
+	ngui_scroll_begin(&ui, area, &detail_scroll);
+	ngui_scroll_wheel(&ui, area, &detail_scroll);
+	if (!ui.focus)
+		ngui_scroll_keys(&ui, &detail_scroll);
+	/* The height is only known after drawing, so the bar decision uses
+	 * the previous frame's; redraw once when it flips. */
+	col = scroll_content(area, &detail_scroll);
+	had_bar = col.w != area.w;
+	top = area.y - detail_scroll.offset;
+	if (sel_disk < 0)
+		y = draw_overview(col, top);
+	else
+		y = draw_disk(col, top, &disks[sel_disk]);
+	detail_scroll.content_h = y - top;
+	ngui_scroll_end(&ui, area, &detail_scroll);
+	if (had_bar != (scroll_content(area, &detail_scroll).w != area.w))
+		ngui_invalidate(&ui);
+}
+
+/* ---- dialogs ---- */
+
+static struct ngui_rect dialog_rect(int w, int h)
+{
+	struct ngui_rect r;
+
+	r.w = w < ui.w - 20 ? w : ui.w - 20;
+	r.h = h < ui.h - 20 ? h : ui.h - 20;
+	r.x = (ui.w - r.w) / 2;
+	r.y = (ui.h - r.h) / 2;
+	ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x - 1, r.y - 1,
+				  r.w + 2, r.h + 2 }, 9, 9, 9, 9,
+			     ui.theme.border);
+	ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel);
+	return r;
+}
+
+static void dialog_title(struct ngui_rect *r, const char *s)
+{
+	struct ngui_rect t = ngui_cut_top(r, 34);
+
+	ngui_text_px(&ui, t.x, t.y + 6, s, (unsigned int)strlen(s),
+		     ui.theme.text, 16, 1, 0, 0);
+}
+
+static void dialog_line(struct ngui_rect *r, const char *s, unsigned short c)
+{
+	struct ngui_rect t = ngui_cut_top(r, 20);
+
+	ngui_text_ellipsis(&ui, t, t.x, t.y, s, c);
+}
+
+static int key_pressed(int key)
+{
+	if (ngui_key_peek(&ui) == key) {
+		ngui_key(&ui);
+		return 1;
+	}
+	return 0;
+}
+
+static void draw_format_dialog(void)
+{
+	const struct disk *d = &disks[dlg_disk];
+	struct ngui_rect r = dialog_rect(460, 260), b;
+	char line[96], size[16], arg[48], val[24];
+	int floppy = d->bd.kind == KIND_FLOPPY, go = 0, i;
+
+	ngui_inset(&r, 18);
+	snprintf(line, sizeof(line), "Format %s", d->bd.name);
+	dialog_title(&r, line);
+	fmt_size(size, sizeof(size), d->bd.total_sectors);
+	snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind),
+		 d->bd.model[0] ? " " : "", d->bd.model,
+		 floppy ? "1.4 MB" : size);
+	dialog_line(&r, line, ui.theme.text_dim);
+	dialog_line(&r, "Every file and partition on this device is erased.",
+		    ui.theme.warning);
+	if (d->nebos)
+		dialog_line(&r, "This removes the NebOS installation as well.",
+			    ui.theme.warning);
+	ngui_cut_top(&r, 8);
+	if (!floppy) {
+		b = ngui_cut_top(&r, 30);
+		text_dim(b.x, b.y + 6, "Size");
+		b.x += 60;
+		b.w = 140;
+		size_in.placeholder = "whole disk";
+		if (ngui_text_input(&ui, ID_DLG_SIZE, b, &size_in) &
+		    NGUI_INPUT_SUBMIT)
+			go = 1;
+		text_dim(b.x + b.w + 10, b.y + 6, "e.g. 100mb, 2gb");
+	}
+	b = ngui_cut_bottom(&r, 30);
+	b.x = r.x + r.w - 110;
+	b.w = 110;
+	if (ngui_button(&ui, ID_DLG_OK, b, "Erase disk"))
+		go = 1;
+	b.x -= 100;
+	b.w = 92;
+	if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") ||
+	    key_pressed(NGUI_KEY_ESC)) {
+		dialog = DLG_NONE;
+		return;
+	}
+	if (!go)
+		return;
+	ngui_input_value(&size_in, val, sizeof(val));
+	for (i = 0; val[i]; i++)
+		if (val[i] == ' ' || (val[i] >= 'A' && val[i] <= 'Z'))
+			val[i] = val[i] == ' ' ? '\0' : (char)(val[i] + 32);
+	snprintf(arg, sizeof(arg), "-y %s%s%s", d->bd.name,
+		 (!floppy && val[0]) ? " " : "", floppy ? "" : val);
+	snprintf(line, sizeof(line), "Formatting %s", d->bd.name);
+	ngui_set_focus(&ui, 0);
+	start_job("format", arg, line);
+}
+
+static void draw_install_dialog(void)
+{
+	const struct disk *d = &disks[dlg_disk];
+	struct ngui_rect r = dialog_rect(520, d->nebos ? 290 : 250), b;
+	char line[96], arg[48], size[16];
+	int mode = 0;
+
+	ngui_inset(&r, 18);
+	snprintf(line, sizeof(line), "Install NebOS on %s", d->bd.name);
+	dialog_title(&r, line);
+	fmt_size(size, sizeof(size), d->bd.total_sectors);
+	snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind),
+		 d->bd.model[0] ? " " : "", d->bd.model, size);
+	dialog_line(&r, line, ui.theme.text_dim);
+	if (d->nebos) {
+		dialog_line(&r, "NebOS is already installed here.", ui.theme.text);
+		dialog_line(&r, "Update writes the running kernel and rootfs to "
+			    "partitions 1-2.", ui.theme.text_dim);
+		dialog_line(&r, "Reinstall clones the live image (partitions 1-3).",
+			    ui.theme.text_dim);
+		dialog_line(&r, "Both keep the /data partition.",
+			    ui.theme.success);
+	} else {
+		dialog_line(&r, "Clones the live NebOS image (boot, rootfs, EFI) and",
+			    ui.theme.text_dim);
+		dialog_line(&r, "creates a /data partition on the rest of the disk.",
+			    ui.theme.text_dim);
+		dialog_line(&r, "Everything else on this device is erased.",
+			    ui.theme.warning);
+	}
+	b = ngui_cut_bottom(&r, 30);
+	b.x = r.x + r.w - 110;
+	b.w = 110;
+	if (ngui_button(&ui, ID_DLG_OK, b, d->nebos ? "Update" : "Install"))
+		mode = d->nebos ? 2 : 1;
+	if (d->nebos) {
+		b.x -= 118;
+		b.w = 110;
+		if (ngui_button(&ui, ID_DLG_OK2, b, "Reinstall"))
+			mode = 1;
+	}
+	b.x -= 100;
+	b.w = 92;
+	if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") ||
+	    key_pressed(NGUI_KEY_ESC)) {
+		dialog = DLG_NONE;
+		return;
+	}
+	if (!mode)
+		return;
+	snprintf(arg, sizeof(arg), "-y %s%s", d->bd.name,
+		 mode == 2 ? " self" : "");
+	snprintf(line, sizeof(line), "%s NebOS on %s",
+		 mode == 2 ? "Updating" : "Installing", d->bd.name);
+	start_job("install", arg, line);
+}
+
+static void draw_job_dialog(void)
+{
+	struct ngui_rect r = dialog_rect(600, 380), b, log;
+	int lh = 16, lines, n, i, start;
+	const char *p;
+
+	ngui_inset(&r, 18);
+	dialog_title(&r, job_title);
+	if (!job_done)
+		dialog_line(&r, "Running... keep the devices connected.",
+			    ui.theme.warning);
+	else
+		dialog_line(&r, job_ok ? "Finished." : "Failed; see the output below.",
+			    job_ok ? ui.theme.success : ui.theme.error);
+	ngui_cut_top(&r, 6);
+	b = ngui_cut_bottom(&r, 30);
+	ngui_cut_bottom(&r, 10);
+	log = r;
+	ngui_fill_rect(&ui, log, ui.theme.bg);
+	ngui_inset(&log, 6);
+	ngui_push_clip(&ui, log);
+	/* Show the newest lines that fit. */
+	lines = log.h / lh;
+	n = 0;
+	for (p = job_log; *p; p++)
+		if (*p == '\n')
+			n++;
+	if (job_len && job_log[job_len - 1] != '\n')
+		n++;
+	start = n > lines ? n - lines : 0;
+	p = job_log;
+	for (i = 0; *p && i < start; p++)
+		if (*p == '\n')
+			i++;
+	for (i = 0; *p && i < lines; i++) {
+		const char *e = strchr(p, '\n');
+		unsigned int len = e ? (unsigned int)(e - p) : (unsigned int)strlen(p);
+
+		ngui_text_px(&ui, log.x, log.y + i * lh, p, len, ui.theme.text,
+			     13, 0, 0, 1);
+		p += len;
+		if (*p == '\n')
+			p++;
+	}
+	ngui_pop_clip(&ui);
+	if (job_done) {
+		b.x = r.x + r.w - 92;
+		b.w = 92;
+		if (ngui_button(&ui, ID_DLG_CLOSE, b, "Close") ||
+		    key_pressed(NGUI_KEY_ESC) || key_pressed('\n'))
+			dialog = DLG_NONE;
+	}
+}
+
+static void draw(void)
+{
+	struct ngui_rect all = { 0, 0, ui.w, ui.h }, body, list;
+	struct input_save save;
+
+	int muted = dialog != DLG_NONE;
+
+	if (muted)
+		input_mute(&save);
+	ngui_fill_rect(&ui, all, ui.theme.bg);
+	body = all;
+	draw_toolbar(ngui_cut_top(&body, TOOLBAR_H));
+	list = ngui_cut_left(&body, LIST_W);
+	draw_device_list(list);
+	ngui_fill_rect(&ui, ngui_cut_left(&body, 1), ui.theme.border);
+	draw_details(body);
+	if (dialog == DLG_NONE)
+		return;
+	if (muted)
+		input_restore(&save);
+	if (dialog == DLG_FORMAT)
+		draw_format_dialog();
+	else if (dialog == DLG_INSTALL)
+		draw_install_dialog();
+	else if (dialog == DLG_JOB)
+		draw_job_dialog();
+}
+
+void _start(const char *arg)
+{
+	struct win_info wi;
+	unsigned int cw, ch;
+	int quit = 0;
+
+	(void)arg;
+	if (u_win_create(800, 560, "Disks", &wi) != 0) {
+		u_puts("disks: no graphics window\n");
+		u_exit(1);
+	}
+	cw = wi.width;
+	ch = wi.height;
+	ngui_init(&ui, (unsigned short *)wi.fb, (int)cw, (int)ch);
+	ngui_font_attach(&ui);
+	name_in.placeholder = "mount name";
+	load_all();
+
+	for (;;) {
+		struct win_info cur;
+
+		if (u_win_info(&cur) == 0 &&
+		    (cur.width != cw || cur.height != ch) &&
+		    u_win_ack_resize() == 0) {
+			cw = cur.width;
+			ch = cur.height;
+			ui.px = (unsigned short *)cur.fb;
+			ui.w = (int)cw;
+			ui.h = (int)ch;
+			ngui_invalidate(&ui);
+		}
+		job_poll();
+		if (status_until && (int)(u_ticks_ms() - status_until) >= 0) {
+			status[0] = '\0';
+			status_until = 0;
+			ngui_invalidate(&ui);
+		}
+		if (!ngui_begin(&ui)) {
+			u_sleep_ms(dialog == DLG_JOB ? 20 : 10);
+			continue;
+		}
+		draw();
+		if (dialog == DLG_NONE && !ui.focus && key_pressed(NGUI_KEY_ESC))
+			quit = 1;
+		ngui_end(&ui);
+		if (quit)
+			break;
+		if (rescan_pending) {
+			/* The frame above showed "Scanning..."; the rescan
+			 * blocks this window while it probes the disks. */
+			struct sys_storage_info si;
+
+			rescan_pending = 0;
+			u_syscall1(SYS_STORAGE_INFO, (int)&si);
+			load_all();
+			set_status("Devices rescanned", 0);
+		}
+		u_sleep_ms(10);
+	}
+	u_win_close();
+	u_exit(0);
+}

+ 15 - 0
user/apps/files/ucmd_files.c

@@ -30,6 +30,7 @@ enum {
 	ID_NEW_INPUT,
 	ID_NEW_INPUT,
 	ID_CREATE,
 	ID_CREATE,
 	ID_CANCEL,
 	ID_CANCEL,
+	ID_DISKS,
 };
 };
 
 
 #define ACT_UP       (1u << 0)
 #define ACT_UP       (1u << 0)
@@ -43,6 +44,7 @@ enum {
 #define ACT_CREATE   (1u << 8)
 #define ACT_CREATE   (1u << 8)
 #define ACT_CANCEL   (1u << 9)
 #define ACT_CANCEL   (1u << 9)
 #define ACT_CLOSE    (1u << 10)
 #define ACT_CLOSE    (1u << 10)
+#define ACT_DISKS    (1u << 11)
 
 
 struct fm_entry {
 struct fm_entry {
 	char name[64];
 	char name[64];
@@ -611,6 +613,9 @@ static void do_action(unsigned int action)
 		refresh();
 		refresh();
 	if (action & ACT_NEW)
 	if (action & ACT_NEW)
 		new_folder();
 		new_folder();
+	if ((action & ACT_DISKS) &&
+	    u_spawn("/ramfs/bin/disks", "", 0, "/") < 0)
+		set_status("Cannot start disks", 1);
 	if (action & ACT_OPEN)
 	if (action & ACT_OPEN)
 		open_selected();
 		open_selected();
 	if (action & ACT_CREATE) {
 	if (action & ACT_CREATE) {
@@ -658,6 +663,11 @@ static void draw_toolbar(struct ngui *g, struct ngui_rect r, unsigned int *actio
 				     (struct ngui_rect){x, y, 30, h},
 				     (struct ngui_rect){x, y, 30, h},
 				     NGUI_ICON_PLUS))
 				     NGUI_ICON_PLUS))
 			*action |= ACT_NEW;
 			*action |= ACT_NEW;
+		/* Storage manager, right-aligned. */
+		if (ngui_icon_button(g, ID_DISKS,
+				     (struct ngui_rect){r.x + r.w - 36, y, 30, h},
+				     NGUI_ICON_DISK))
+			*action |= ACT_DISKS;
 	} else {
 	} else {
 		int w;
 		int w;
 
 
@@ -679,6 +689,11 @@ static void draw_toolbar(struct ngui *g, struct ngui_rect r, unsigned int *actio
 		if (ngui_button(g, ID_NEW, (struct ngui_rect){x, y, w, h},
 		if (ngui_button(g, ID_NEW, (struct ngui_rect){x, y, w, h},
 				"New Folder"))
 				"New Folder"))
 			*action |= ACT_NEW;
 			*action |= ACT_NEW;
+		w = (int)ngui_text_width_ui(g, "Disks") + 18;
+		if (ngui_button(g, ID_DISKS,
+				(struct ngui_rect){r.x + r.w - 6 - w, y, w, h},
+				"Disks"))
+			*action |= ACT_DISKS;
 	}
 	}
 }
 }
 
 

+ 8 - 2
user/apps/wm/ucmd_wm.c

@@ -452,6 +452,8 @@ static void draw_bar(void)
 		   WM_BTN_GALLERY_H, "Gallery");
 		   WM_BTN_GALLERY_H, "Gallery");
 	bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
 	bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
 		   WM_BTN_CHAT_H, "Chat");
 		   WM_BTN_CHAT_H, "Chat");
+	bar_button(WM_BTN_DISKS_X, WM_BTN_DISKS_Y, WM_BTN_DISKS_W,
+		   WM_BTN_DISKS_H, "Disks");
 	if (bar_msg_until &&
 	if (bar_msg_until &&
 	    (int)(u_ticks_ms() - bar_msg_until) < 0)
 	    (int)(u_ticks_ms() - bar_msg_until) < 0)
 		usurf_text(&screen, (int)(WM_BTN_END_X + 8),
 		usurf_text(&screen, (int)(WM_BTN_END_X + 8),
@@ -1731,12 +1733,12 @@ static int bar_launcher_hit(unsigned int mx, unsigned int my)
 	static const int x0[] = {
 	static const int x0[] = {
 		WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
 		WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
 		WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
 		WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
-		WM_BTN_CHAT_X
+		WM_BTN_CHAT_X, WM_BTN_DISKS_X
 	};
 	};
 	static const int w[] = {
 	static const int w[] = {
 		WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
 		WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
 		WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
 		WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
-		WM_BTN_CHAT_W
+		WM_BTN_CHAT_W, WM_BTN_DISKS_W
 	};
 	};
 	unsigned int i;
 	unsigned int i;
 
 
@@ -1836,6 +1838,10 @@ static void handle_mouse(struct wm_status *st)
 			   mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
 			   mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
 			   my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
 			   my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
 			spawn_app_checked("/ramfs/bin/chat");
 			spawn_app_checked("/ramfs/bin/chat");
+		} else if (mx >= WM_BTN_DISKS_X && my >= WM_BTN_DISKS_Y &&
+			   mx < WM_BTN_DISKS_X + WM_BTN_DISKS_W &&
+			   my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
+			spawn_app_checked("/ramfs/bin/disks");
 		} else {
 		} else {
 			int idx = window_at((int)mx, (int)my);
 			int idx = window_at((int)mx, (int)my);
 
 

+ 0 - 8
user/lib/ngui/ngui.c

@@ -1552,14 +1552,6 @@ static int ngui_list_row_common(struct ngui *ui, unsigned int id,
 	sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
 	sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
 	trail_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
 	trail_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
 
 
-	if (row->selected) {
-		int cy = r.y + r.h / 2;
-
-		ngui_line(ui, left, cy, left + 3, cy + 4, fg);
-		ngui_line(ui, left + 3, cy + 4, left + 11, cy - 5, fg);
-		left += 17;
-	}
-
 	if (row->trailing && row->trailing[0]) {
 	if (row->trailing && row->trailing[0]) {
 		int tw = (int)ngui_text_width_ui(ui, row->trailing);
 		int tw = (int)ngui_text_width_ui(ui, row->trailing);
 
 

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません