Browse Source

Add top-bar network menu for netdev and WiFi selection

The WM draws a network status icon to the left of the clock: a cable for
a wired netdev, signal bars for an associated WiFi link, a USB mark for
RNDIS tethering, or a red X when nothing is up.  Clicking it opens one
anchored netmenu window.

netmenu lists every present driver through the new SYS_NET_DEVLIST (111),
selects one through SYS_NET_SELECT (112) and runs DHCP.  It also scans
WiFi networks and asks for the WPA2 passphrase before starting the
existing wifi supplicant.  The passphrase is staged in
/share/netmenu.wifi and read by `wifi connectfile`, because SYS_SPAWN
arguments are limited to VFS_PATH_MAX (64) bytes.

SYS_NET_INFO now reports the active netdev's link state, and its first
probe releases the BKL/interrupts so driver init does not hang the WM.
The WM polls the icon at 1 Hz and anchors the menu under it with
u_wm_set_geom.  ngui gains ngui_input_value() so a password field can
return the real value instead of its '*' mask.

Build and QEMU checks: make all; wired selection/DHCP and the true
no-NIC X/menu paths.  The WiFi connect path is not hardware-tested.
Docs and AGENTS.md updated.
Johannes Zellner 1 tuần trước cách đây
mục cha
commit
3d539bf660
20 tập tin đã thay đổi với 1611 bổ sung và 14 xóa
  1. 27 3
      AGENTS.md
  2. 10 2
      Makefile
  3. 8 1
      docs/graphics.html
  4. 1 1
      docs/index.html
  5. 18 0
      docs/network.html
  6. 4 1
      docs/userland.html
  7. 26 0
      netdev.c
  8. 13 0
      netdev.h
  9. 21 2
      ngui.c
  10. 3 0
      ngui.h
  11. 96 1
      sys_net.c
  12. 2 0
      sys_net.h
  13. 26 0
      syscall.c
  14. 20 0
      syscall.h
  15. 26 0
      ucmd_lib.h
  16. 3 0
      ucmd_netinfo.c
  17. 974 0
      ucmd_netmenu.c
  18. 67 1
      ucmd_wifi.c
  19. 244 2
      ucmd_wm.c
  20. 22 0
      wifi_cfg.h

+ 27 - 3
AGENTS.md

@@ -320,6 +320,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   The right-aligned date/time text is itself a click target: `draw_bar()`
   records its rectangle, the pointer highlights it, and a click spawns
   `/ramfs/bin/settime` (the `Clock` launcher still opens the analog clock).
+  A network status icon sits to its left (before the temperature/AC/battery
+  status): a cable for a wired netdev, signal bars for an associated WiFi
+  link, a USB mark for RNDIS tethering, and a red X otherwise.  The WM polls
+  `SYS_NET_INFO` (and `SYS_WIFI_STATUS` only while WiFi is active) at 1 Hz;
+  clicking the icon spawns one `/ramfs/bin/netmenu` window and anchors it
+  under the icon once its window exists (`u_wm_set_geom`).
   The last launcher, `Chat`, spawns `/ramfs/bin/chat`; `WM_BTN_END_X`
   (`wm_abi.h`) is the launchers' right edge, and the bar notice and the
   right-aligned status never draw left of it.
@@ -553,7 +559,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `netdev [name]` is a kernel command that shows the active driver or
   switches it (`netdev rndis`, `netdev asix`, `netdev e1000e`, ...); it calls
   the selected driver's probe and leaves the current driver untouched on
-  failure.  `wifi.c` (the Intel 7265D/3165 netdev, see the WiFi bullet) is
+  failure.  The top-bar network icon opens `ucmd_netmenu.c`, which lists every
+  present driver through `SYS_NET_DEVLIST` (111), selects one through
+  `SYS_NET_SELECT` (112) and runs DHCP; the same window lists scanned WiFi
+  networks and asks for the WPA2 passphrase.  `wifi.c` (the Intel
+  7265D/3165 netdev, see the WiFi bullet) is
   last in the table; `struct netdev` has an optional `link_up()` hook, and
   the boot DHCP is skipped while it returns 0.  `net.c` — ARP + ICMP echo, IPv4/UDP
   send + parse helpers,
@@ -624,7 +634,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   The firmware is loaded on the first `wifi` command, not at boot.
   `netdev_shutdown()` always calls `wifi_shutdown()` before a kexec (the RX
   ring would keep writing into reused pages).  The ring-3 side is
-  `ucmd_wifi.c` (`wifi`, `wifi up|scan|connect <ssid> [pass]|disconnect`),
+  `ucmd_wifi.c` (`wifi`, `wifi up|scan|connect <ssid> [pass]|disconnect`,
+  `wifi connectfile <path>` for the network menu's staged SSID/pass/BSSID
+  record),
   `wpa.c/h` (4-way + group handshake, key descriptor v2) and
   `wpa_crypto.c/h` (SHA-1, HMAC, PBKDF2, PRF, AES-128, RFC 3394 unwrap; host
   test `make test-wpa`).  After the handshake `wifi connect` installs the
@@ -1169,7 +1181,7 @@ filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 `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`,
 `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
-`netreset`, `websrv`, `curl`, `wifi`, `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
 commands spawned as processes, or builtins (`cd`, `path`, `help`, `clear`).
 
@@ -1954,3 +1966,15 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     sources without hardware in the loop; expect bring-up debugging on the
     Chromebook.  The kernel budget is tight: WiFi added ~20 KB, `_end` is
     ~0x89710 (27 KB below the `0x90000` wall).
+
+36. Network menu limits (`ucmd_netmenu.c`): building the device list probes
+    every driver, so opening the menu on a machine with an unconfigured USB
+    NIC can take a moment (USB control transfers) and may initialize a device
+    that was not the boot-time winner.  The list shows at most six device
+    rows, one menu instance is kept open by the WM, and WiFi scanning is
+    refused while associated (use Disconnect first).  The staged
+    `/share/netmenu.wifi` record contains the WPA2 passphrase until the
+    `wifi` helper reads and unlinks it; the path is fixed, so two concurrent
+    netmenu instances (e.g. one launched from a terminal) would overwrite
+    each other.  The `wifi` helper is asynchronous: closing the menu does not
+    cancel a connection attempt.

+ 10 - 2
Makefile

@@ -619,7 +619,7 @@ UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
         ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_net100 ucmd_netmac ucmd_curl \
         ucmd_files ucmd_view ucmd_sensors ucmd_powerinfo ucmd_mines ucmd_solitaire \
         ucmd_settime ucmd_date ucmd_gallery ucmd_websrv ucmd_browser ucmd_chat \
-        ucmd_wifi
+        ucmd_wifi ucmd_netmenu
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
@@ -888,12 +888,20 @@ wpa.o: wpa.c wpa.h wpa_crypto.h
 wpa_crypto.o: wpa_crypto.c wpa_crypto.h
 	$(CC) $(CFLAGS) -O2 $(SECTION_CFLAGS) -o $@ $<
 
-ucmd_wifi.o: ucmd_wifi.c ucmd_lib.h syscall.h wpa.h tls_rng.h
+ucmd_wifi.o: ucmd_wifi.c ucmd_lib.h syscall.h wpa.h tls_rng.h wifi_cfg.h
 
 ucmd_wifi.elf: ucmd_wifi.o $(WPA_OBJS) tls_rng.o tls_sha256.o ucmd.ld libc.a
 	$(LD) -T ucmd.ld -z max-page-size=0x100 --gc-sections -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
 	$(OBJCOPY) --strip-all $@ $@
 
+# Top-bar network menu: NIC list, WiFi scan and the WPA2 passphrase prompt.
+ucmd_netmenu.o: ucmd_netmenu.c ucmd_lib.h syscall.h ngui.h wifi_cfg.h
+	$(CC) $(CFLAGS) -Ilibc/include -o $@ $<
+
+ucmd_netmenu.elf: ucmd_netmenu.o ngui.o font_8x16.o $(UFONT_OBJS) ucmd.ld libc.a
+	$(LD) -T ucmd.ld -z max-page-size=0x100 --gc-sections -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
+	$(OBJCOPY) --strip-all $@ $@
+
 wpa_test: wpa_test.c wpa.c wpa_crypto.c wpa.h wpa_crypto.h
 	gcc -O2 -Wall -Wextra -I. -o $@ wpa_test.c wpa.c wpa_crypto.c
 

+ 8 - 1
docs/graphics.html

@@ -145,7 +145,13 @@
 			Click-to-focus, drag-by-title-bar and close are routed through the
 			same pointer path. The right-aligned date/time text is also a
 			target: it highlights on hover and opens the Date &amp; Time editor
-			(<code>bin/settime</code>) on a click.
+			(<code>bin/settime</code>) on a click. To its left, a network status
+			icon shows a cable for wired Ethernet, signal bars for a connected
+			WiFi link, a USB mark for RNDIS tethering, or a red X when no link
+			is up. Clicking it opens the anchored <code>bin/netmenu</code>
+			window: present NICs can be selected (then DHCP runs), and scanned
+			WiFi networks can be joined, asking for the WPA2 passphrase first
+			when the AP requires one.
 		</p>
 		<p>
 			The WM is the ring-3 process loaded from
@@ -378,6 +384,7 @@
 			<li><code>cpu.c</code> — SSE2 probing</li>
 			<li><code>wm.c</code>, <code>wm.h</code>, <code>wm_abi.h</code> — kernel window server and shared ABI</li>
 			<li><code>ucmd_wm.c</code>, <code>uwm_lib.c</code> — ring-3 WM</li>
+			<li><code>ucmd_netmenu.c</code> — top-bar network menu (NIC selection, DHCP, WiFi scan/passphrase)</li>
 			<li><code>ucmd_settime.c</code>, <code>rtc.c</code>, <code>ntp.c</code> — Date &amp; Time editor, RTC access and SNTP sync</li>
 			<li><code>gfx.c</code>, <code>gfx.h</code> — RGB565 window canvases</li>
 			<li><code>sys_wm.c</code>, <code>sys_wm.h</code> — WM/window ABI</li>

+ 1 - 1
docs/index.html

@@ -217,7 +217,7 @@
 			</a>
 			<a class="card" href="network.html">
 				<h3>Networking</h3>
-				<p>NIC registry, ARP/ICMP/UDP, DHCP, DNS, NTP, TCP client and listener, TLS 1.3, USB tethering, WiFi, netconsole and netkexec.</p>
+				<p>NIC registry, ARP/ICMP/UDP, DHCP, DNS, NTP, TCP client and listener, TLS 1.3, USB tethering, WiFi, the top-bar network menu, netconsole and netkexec.</p>
 				<span class="card-tag">network.html</span>
 			</a>
 			<a class="card" href="drivers.html#wifi">

+ 18 - 0
docs/network.html

@@ -143,6 +143,17 @@
 			<code>netdev asix</code> switches to a named one, leaving the current
 			driver untouched if the probe fails.
 		</p>
+		<p>
+			The top-bar network icon opens <span class="file">ucmd_netmenu.c</span>.
+			It asks the kernel for every present driver through
+			<code>SYS_NET_DEVLIST</code> (111), so an RNDIS phone or a USB
+			Ethernet dongle that was not the boot-time winner still appears.
+			Selecting one calls <code>SYS_NET_SELECT</code> (112) and then runs
+			DHCP. The same window lists scanned WiFi networks; selecting a WPA2
+			network asks for the passphrase and starts the existing
+			<code>wifi</code> supplicant, which opens the data port and runs DHCP
+			itself.
+		</p>
 		<p>
 			<span class="file">rndis.c</span> is the USB tethering path, not a
 			WiFi driver: an Android phone (or QEMU's <code>usb-net</code>) is
@@ -168,6 +179,12 @@
 			Data frames are converted between Ethernet and non-QoS 802.11 +
 			LLC/SNAP; EAPOL (ethertype <code>0x888e</code>) never reaches
 			<code>net_pump()</code> and is queued for the supplicant instead.
+			The top-bar menu starts the same supplicant through
+			<code>wifi connectfile &lt;path&gt;</code>: it stages the SSID,
+			passphrase and BSSID in a small <code>/share/netmenu.wifi</code>
+			record because a <code>SYS_SPAWN</code> argument is limited to
+			<code>VFS_PATH_MAX</code> (64) bytes. The helper removes the file
+			after reading it.
 		</p>
 		<div class="table-wrap">
 			<table>
@@ -432,6 +449,7 @@
 			<li><code>e1000e.c</code>, <code>r8168.c</code>, <code>ne2000.c</code>, <code>asix.c</code>, <code>rndis.c</code></li>
 			<li><code>wifi.c</code>, <code>iwl_mvm.c</code>, <code>iwl_trans.c</code> — Intel 7265D/3165 WiFi</li>
 			<li><code>wpa.c</code>, <code>wpa_crypto.c</code>, <code>ucmd_wifi.c</code> — WPA2-PSK supplicant and the <code>wifi</code> command (<code>make test-wpa</code>)</li>
+			<li><code>ucmd_netmenu.c</code>, <code>wifi_cfg.h</code> — top-bar network menu and its staged WiFi connect request</li>
 			<li><code>net.c</code>, <code>net.h</code> — ARP cache/ICMP/UDP/IP</li>
 			<li><code>netcore.c</code>, <code>netcore.h</code> — shared receive pump and IRQ helpers</li>
 			<li><code>dhcp.c</code>, <code>dns.c</code>, <code>ntp.c</code></li>

+ 4 - 1
docs/userland.html

@@ -227,6 +227,7 @@
 					<tr><td>Process &amp; memory</td><td>73–79, 81, 88–90, 108</td><td>TTY, spawn, getpid, alive, kill, chdir, scheduler info, heap alloc/grow/free</td></tr>
 					<tr><td>TCP server</td><td>93–97, 99–101</td><td>listen, accept, server recv/send/close, peer, speed, MAC set</td></tr>
 					<tr><td>WiFi</td><td>110</td><td><code>SYS_WIFI(op, a2, a3)</code>: up, status, scan/results, connect/disconnect, EAPOL RX/TX, set key, authorize, set/get PTK for the rekey helper (<span class="file">wifi.c</span>)</td></tr>
+					<tr><td>NIC menu</td><td>111–112</td><td><code>SYS_NET_DEVLIST</code> (present drivers with kind/active/link) and <code>SYS_NET_SELECT</code> (make a named netdev active)</td></tr>
 				</tbody>
 			</table>
 		</div>
@@ -312,7 +313,7 @@
 			</div>
 			<div class="card">
 				<h3>Network</h3>
-				<p><code>netinfo</code> <code>netip</code> <code>netdns</code> <code>host</code> <code>dhcp</code> <code>ping</code> <code>pong</code> <code>netsend</code> <code>netrecv</code> <code>netlog</code> <code>netconsole</code> <code>netregs</code> <code>netloop</code> <code>netreset</code> <code>net100</code> <code>netmac</code> <code>curl</code> <code>websrv</code> <code>wifi</code></p>
+				<p><code>netinfo</code> <code>netip</code> <code>netdns</code> <code>host</code> <code>dhcp</code> <code>ping</code> <code>pong</code> <code>netsend</code> <code>netrecv</code> <code>netlog</code> <code>netconsole</code> <code>netregs</code> <code>netloop</code> <code>netreset</code> <code>net100</code> <code>netmac</code> <code>curl</code> <code>websrv</code> <code>wifi</code> <code>netmenu</code></p>
 			</div>
 			<div class="card">
 				<h3>GUI</h3>
@@ -341,6 +342,7 @@
 			<li><strong>edit</strong> — text editor that draws its own text with the 8x16 font.</li>
 			<li><strong>clock</strong> / <strong>colors</strong> — small window canvas demos.</li>
 			<li><strong>settime</strong> — Date &amp; Time editor opened by clicking the top-bar clock. It reads and writes the CMOS RTC through <code>SYS_RTC_GET</code>/<code>SYS_RTC_SET</code>; the <em>Sync NTP</em> button asks the kernel's SNTP client (<code>SYS_NTP_SYNC</code>, 106) for UTC and writes the result to the RTC. The two-digit RTC year is expanded over 1970–2069 so dead-battery defaults such as 1980/1990 can be corrected.</li>
+			<li><strong>netmenu</strong> — network menu opened by the top-bar status icon. Lists the present NICs from <code>SYS_NET_DEVLIST</code> (Ethernet, USB Ethernet, RNDIS tethering), selects one with <code>SYS_NET_SELECT</code> and runs DHCP. Its WiFi section scans through <code>SYS_WIFI_SCAN</code>/<code>RESULTS</code>, asks for a WPA2 passphrase when needed and starts the <code>wifi</code> helper through a <code>/share/netmenu.wifi</code> request, because the 64-byte <code>SYS_SPAWN</code> argument cannot hold a long SSID plus passphrase. The helper's terminal output is shown while it connects.</li>
 			<li><strong>wm</strong> / <strong>term</strong> — the ring-3 window manager and terminal launcher used by the desktop.</li>
 		</ul>
 		<p>
@@ -419,6 +421,7 @@
 			<li><code>browser.c</code>, <code>browser_html.c</code>, <code>js_browser.c/h</code>, <code>third_party/mujs/</code>, <code>gallery.c</code></li>
 			<li><code>ucmd_chat.c</code>, <code>chat_login.c</code>, <code>ujson.c</code>, <code>uhttp_cookie.c</code> — chat client</li>
 			<li><code>ucmd_wifi.c</code>, <code>wpa.c</code>, <code>wpa_crypto.c</code> — <code>wifi</code> command and the WPA2-PSK supplicant</li>
+			<li><code>ucmd_netmenu.c</code>, <code>wifi_cfg.h</code> — top-bar network menu and its staged WiFi connect record</li>
 		</ul>
 	</section>
 </main>

+ 26 - 0
netdev.c

@@ -97,6 +97,32 @@ const struct netdev *netdev_active(void)
 	return active;
 }
 
+unsigned int netdev_count(void)
+{
+	return sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
+}
+
+const struct netdev *netdev_at(unsigned int index)
+{
+	if (index >= netdev_count())
+		return 0;
+	return netdev_drivers[index];
+}
+
+int netdev_probed(void)
+{
+	return probed;
+}
+
+int netdev_link(const struct netdev *nd)
+{
+	if (!nd)
+		return 0;
+	if (!nd->link_up)
+		return 1;
+	return nd->link_up() ? 1 : 0;
+}
+
 /* The ASIX dongle is the direct management link when present.  Keeping
  * netlog/netkexec here lets the active driver be switched to e1000e for
  * testing without losing the netmon console or `make net-dev`. */

+ 13 - 0
netdev.h

@@ -39,6 +39,19 @@ struct netdev {
  * NULL when no NIC is present. */
 const struct netdev *netdev_active(void);
 
+/* Driver table access for the ring-3 network menu: count and indexed entry.
+ * The menu probes each driver's init() to build its "available" list. */
+unsigned int netdev_count(void);
+const struct netdev *netdev_at(unsigned int index);
+
+/* Whether the boot-time probe has run (netdev_active() is then a cached
+ * lookup and can be called without releasing the BKL). */
+int netdev_probed(void);
+
+/* Effective link state of a driver (drivers without a link_up hook are
+ * treated as always up). */
+int netdev_link(const struct netdev *nd);
+
 /* A USB network device appeared after boot (hotplug probe succeeded).  Makes
  * it the active NIC when the boot probe found none; never overrides an
  * already-selected driver. */

+ 21 - 2
ngui.c

@@ -32,6 +32,12 @@ static unsigned int slen(const char *s)
 	return n;
 }
 
+/* Secret fields are measured and painted from a '*' mask: the real text is
+ * swapped out for everything except the edit loop, so the caret, selection
+ * and click positions all follow the masked width. */
+static char secret_save[NGUI_INPUT_MAX];
+static struct ngui_input *secret_owner;
+
 static void scopy(char *dst, const char *src, unsigned int cap)
 {
 	unsigned int i = 0;
@@ -1170,6 +1176,20 @@ void ngui_input_set(struct ngui_input *in, const char *s)
 	in->last_click_ms = 0;
 }
 
+void ngui_input_value(const struct ngui_input *in, char *out,
+		      unsigned int max)
+{
+	const char *src;
+	unsigned int i;
+
+	if (!in || !out || !max)
+		return;
+	src = (in->secret && in == secret_owner) ? secret_save : in->text;
+	for (i = 0; src[i] && i + 1 < max; i++)
+		out[i] = src[i];
+	out[i] = '\0';
+}
+
 static int input_word_char(char c)
 {
 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
@@ -1379,12 +1399,11 @@ static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
 /* Secret fields are measured and painted from a '*' mask: the real text is
  * swapped out for everything except the edit loop, so the caret, selection
  * and click positions all follow the masked width. */
-static char secret_save[NGUI_INPUT_MAX];
-
 static void secret_mask(struct ngui_input *in)
 {
 	unsigned int i;
 
+	secret_owner = in;
 	for (i = 0; i <= in->len && i < NGUI_INPUT_MAX; i++) {
 		secret_save[i] = in->text[i];
 		in->text[i] = i < in->len ? '*' : '\0';

+ 3 - 0
ngui.h

@@ -236,6 +236,9 @@ int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		    struct ngui_input *in);
 void ngui_input_set(struct ngui_input *in, const char *s);
+/* Copy a field's real value out (a secret field's '*' mask is bypassed). */
+void ngui_input_value(const struct ngui_input *in, char *out,
+		      unsigned int max);
 
 /* Scrolling pane: call begin, draw the content with scroll->offset, end. */
 void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,

+ 96 - 1
sys_net.c

@@ -57,6 +57,35 @@ static unsigned int caller_pid(void)
 	return current_proc()->pid;
 }
 
+static int nd_name_eq(const char *a, const char *b)
+{
+	while (*a && *b) {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
+static void nd_copy_name(char *dst, unsigned int max, const char *src)
+{
+	unsigned int i;
+
+	for (i = 0; src && src[i] && i + 1 < max; i++)
+		dst[i] = src[i];
+	dst[i] = '\0';
+}
+
+static unsigned int nd_kind(const struct netdev *nd)
+{
+	if (nd_name_eq(nd->name, "wifi"))
+		return SYS_NETDEV_WIFI;
+	if (nd_name_eq(nd->name, "rndis"))
+		return SYS_NETDEV_USB;
+	return SYS_NETDEV_WIRED;
+}
+
 /* Called from the scheduler's reap loop for every dead process. */
 void sys_net_reap(unsigned int pid)
 {
@@ -72,13 +101,25 @@ void sys_net_reap(unsigned int pid)
 
 int sys_net_info(struct sys_netinfo *out)
 {
-	const struct netdev *nd = netdev_active();
+	const struct netdev *nd;
 	unsigned int i;
 
 	for (i = 0; i < sizeof(*out); i++)
 		((unsigned char *)out)[i] = 0;
 	out->mirror_on = netlog_get_enabled() ? 1 : 0;
 	netlog_get_host(out->mirror_host);
+
+	/* The first SYS_NET_INFO may be the first NIC probe (the WM's top-bar
+	 * poll runs before the boot DHCP on some boots).  Driver init waits on
+	 * the PHY/USB with tick-based delays, so run it with interrupts on and
+	 * the BKL released.  Once probed, this is just a cached lookup. */
+	if (!netdev_probed()) {
+		net_enter();
+		nd = netdev_active();
+		net_exit();
+	} else {
+		nd = netdev_active();
+	}
 	if (!nd)
 		return 0;
 
@@ -93,9 +134,63 @@ int sys_net_info(struct sys_netinfo *out)
 		out->name[i] = nd->name[i];
 	for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
 		out->chip[i] = nd->chip[i];
+	out->link = (unsigned int)netdev_link(nd);
 	return 1;
 }
 
+/* Probe every registered driver once and return the ones that are present.
+ * The menu calls this when it opens, so a plain USB dongle or phone that was
+ * not the boot-time active NIC still shows up.  Driver init() can wait on the
+ * PHY/USB, so it runs with interrupts on and the BKL released. */
+int sys_net_devlist(struct sys_netdev *out, unsigned int max)
+{
+	const struct netdev *act;
+	unsigned int i, n = 0;
+
+	if (!out || !max)
+		return 0;
+	net_enter();
+	act = netdev_active();
+	for (i = 0; i < netdev_count() && n < max; i++) {
+		const struct netdev *nd = netdev_at(i);
+		unsigned int j;
+		int present;
+
+		if (!nd)
+			continue;
+		present = (nd == act);
+		if (!present && nd->init)
+			present = nd->init() == 0;
+		if (!present)
+			continue;
+
+		for (j = 0; j < sizeof(out[n]); j++)
+			((unsigned char *)&out[n])[j] = 0;
+		nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
+		nd_copy_name(out[n].chip, sizeof(out[n].chip), nd->chip);
+		out[n].kind = nd_kind(nd);
+		out[n].active = (nd == act);
+		out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :
+					     (nd->link_up ? (nd->link_up() ? 1 : 0)
+							  : 1));
+		n++;
+	}
+	net_exit();
+	return (int)n;
+}
+
+int sys_net_select(const char *name)
+{
+	int r;
+
+	if (!name || !*name)
+		return -1;
+	net_enter();
+	r = netdev_select(name);
+	net_exit();
+	return r;
+}
+
 int sys_net_print_info(void)
 {
 	const struct netdev *nd = netdev_active();

+ 2 - 0
sys_net.h

@@ -8,6 +8,8 @@
 void sys_net_reap(unsigned int pid);
 
 int sys_net_info(struct sys_netinfo *out);
+int sys_net_devlist(struct sys_netdev *out, unsigned int max);
+int sys_net_select(const char *name);
 int sys_net_print_info(void);
 int sys_net_acquire(void);
 int sys_net_release(void);

+ 26 - 0
syscall.c

@@ -410,6 +410,32 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return (int)current_proc()->pid;
 	case SYS_WIFI:
 		return sys_wifi(a1, a2, a3);
+	case SYS_NET_DEVLIST: {
+		struct sys_netdev list[8];
+		unsigned int max = (unsigned int)a2, i;
+		int n;
+
+		if (max > 8)
+			max = 8;
+		if (!max || !paging_user_range_ok(current_proc()->cr3,
+						  (unsigned int)a1,
+						  max * sizeof(list[0]), 1))
+			return -1;
+		n = sys_net_devlist(list, max);
+		if (n < 0)
+			return -1;
+		for (i = 0; i < (unsigned int)n; i++)
+			((struct sys_netdev *)a1)[i] = list[i];
+		return n;
+	}
+	case SYS_NET_SELECT: {
+		char name[16];
+
+		if (copy_user_string(name, sizeof(name),
+				     (const char *)a1) != 0)
+			return -1;
+		return sys_net_select(name);
+	}
 	case SYS_ALIVE:
 		return process_is_alive((unsigned int)a1);
 	case SYS_KILL:

+ 20 - 0
syscall.h

@@ -186,6 +186,16 @@
 #define SYS_WIFI_SEC_PSK     1  /* RSN, PSK AKM, CCMP pairwise + group */
 #define SYS_WIFI_SEC_OTHER   2  /* WEP/WPA1/TKIP/802.1X/SAE: unsupported */
 
+/* NIC list and selection for the ring-3 network menu.  The list probes every
+ * registered driver, so only devices that are actually present are returned;
+ * `kind` picks the menu icon (wired, WiFi, USB tethering). */
+#define SYS_NET_DEVLIST  111  /* (struct sys_netdev *out, max) -> count */
+#define SYS_NET_SELECT   112  /* (const char *name) -> 0/-1 */
+
+#define SYS_NETDEV_WIRED 0
+#define SYS_NETDEV_WIFI  1
+#define SYS_NETDEV_USB   2
+
 #define SYS_WIFI_ST_DOWN     0
 #define SYS_WIFI_ST_IDLE     1  /* firmware up, not associated */
 #define SYS_WIFI_ST_ASSOC    2  /* associated, port closed */
@@ -223,6 +233,16 @@ struct sys_netinfo {
 	unsigned char mirror_host[4];
 	char name[8];
 	char chip[16];
+	unsigned int link;       /* active netdev reports a usable link */
+};
+
+/* One selectable NIC (see SYS_NET_DEVLIST). */
+struct sys_netdev {
+	char name[8];
+	char chip[24];
+	unsigned int kind;       /* SYS_NETDEV_* */
+	unsigned int active;
+	unsigned int link;
 };
 
 /* Error codes returned by the file syscalls. */

+ 26 - 0
ucmd_lib.h

@@ -488,4 +488,30 @@ static inline void u_fat_name(const char *raw, char *out, unsigned int cap)
 	out[n] = '\0';
 }
 
+/* Network state and device selection (the kernel owns the stack/NIC). */
+static inline int u_net_info(struct sys_netinfo *out)
+{
+	return u_syscall1(SYS_NET_INFO, (int)out);
+}
+
+static inline int u_net_devlist(struct sys_netdev *out, unsigned int max)
+{
+	return u_syscall2(SYS_NET_DEVLIST, (int)out, (int)max);
+}
+
+static inline int u_net_select(const char *name)
+{
+	return u_syscall1(SYS_NET_SELECT, (int)name);
+}
+
+static inline int u_dhcp_run(unsigned int timeout_ms)
+{
+	return u_syscall1(SYS_DHCP_RUN, (int)timeout_ms);
+}
+
+static inline int u_wifi(int op, int a2, int a3)
+{
+	return u_syscall3(SYS_WIFI, op, a2, a3);
+}
+
 #endif

+ 3 - 0
ucmd_netinfo.c

@@ -19,6 +19,9 @@ void _start(const char *arg)
 	u_puts(" (");
 	u_puts(n.chip);
 	u_puts(")\n");
+	u_puts("link:   ");
+	u_puts(n.link ? "up" : "down");
+	u_puts("\n");
 	u_syscall0(SYS_NET_PRINT_INFO);
 
 	u_puts("ip=");

+ 974 - 0
ucmd_netmenu.c

@@ -0,0 +1,974 @@
+/* netmenu: the top-bar network menu.
+ *
+ * Lists the NICs the kernel reports as present (Ethernet, USB Ethernet,
+ * RNDIS tethering) and the WiFi networks from a scan.  Selecting a wired or
+ * USB device makes it the active netdev and runs DHCP; selecting a WiFi
+ * network asks for the WPA2 passphrase when needed, then starts the existing
+ * `wifi` supplicant with a staged /share/netmenu.wifi request (the SYS_SPAWN
+ * argument limit is too small for an SSID plus a long passphrase).  The
+ * supplicant itself runs DHCP once the data port opens.
+ *
+ * A small ngui canvas window; the WM places it under the network icon and
+ * opens only one instance at a time. */
+
+#include "ucmd_lib.h"
+#include "ngui.h"
+#include "wifi_cfg.h"
+
+#define MAX_DEVS 8
+#define MAX_BSS  32
+#define ROW_H    24
+#define LOG_MAX  2048
+
+#define ID_REFRESH       1
+#define ID_SCAN          2
+#define ID_DISCONNECT    3
+#define ID_PASS_OK       4
+#define ID_PASS_CANCEL   5
+#define ID_CONN_CANCEL   6
+#define ID_PASS          7
+#define ID_DEV_BASE      100
+#define ID_BSS_BASE      200
+
+#define ACT_REFRESH    (1u << 0)
+#define ACT_SCAN       (1u << 1)
+#define ACT_DISCONNECT (1u << 2)
+#define ACT_BACK       (1u << 3)
+#define ACT_CONNECT    (1u << 4)
+
+enum { VIEW_MAIN, VIEW_PASS, VIEW_CONNECT };
+
+static struct ngui ui;
+static struct ngui_input pass_in;
+static struct ngui_scroll wifi_scroll;
+
+static struct sys_netdev devs[MAX_DEVS];
+static int ndev;
+static struct sys_wifi_bss bss[MAX_BSS];
+static int nbss;
+static struct sys_wifi_status wst;
+static struct sys_netinfo net;
+static int net_present;
+static int wifi_checked;
+static int wifi_present;
+
+static int view;
+static int sel_bss = -1;
+static char sel_ssid[33];
+static int scanning;
+static unsigned int scan_start;
+
+static int connect_pid = -1;
+static int connect_tty;
+static unsigned int connect_start;
+static char conn_log[LOG_MAX];
+static unsigned int conn_log_len;
+
+static char status[160];
+static int status_error;
+static unsigned int status_until;
+
+static unsigned int actions;
+static int action_dev = -1;
+static int action_bss = -1;
+
+static void draw(void);
+static void paint_frame(void);
+
+static unsigned int slen(const char *s)
+{
+	unsigned int n = 0;
+
+	while (s && s[n])
+		n++;
+	return n;
+}
+
+static int seq(const char *a, const char *b)
+{
+	while (*a && *b) {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
+static char *sput(char *p, const char *s)
+{
+	while (s && *s)
+		*p++ = *s++;
+	return p;
+}
+
+static char *put_dec(char *p, unsigned int v)
+{
+	char b[10];
+	unsigned int n = 0;
+
+	do {
+		b[n++] = (char)('0' + v % 10);
+		v /= 10;
+	} while (v);
+	while (n)
+		*p++ = b[--n];
+	return p;
+}
+
+static char *put_signed(char *p, int v)
+{
+	if (v < 0) {
+		*p++ = '-';
+		v = -v;
+	}
+	return put_dec(p, (unsigned int)v);
+}
+
+static char *put_ip(char *p, const unsigned char ip[4])
+{
+	p = put_dec(p, ip[0]); *p++ = '.';
+	p = put_dec(p, ip[1]); *p++ = '.';
+	p = put_dec(p, ip[2]); *p++ = '.';
+	p = put_dec(p, ip[3]);
+	return p;
+}
+
+static void ssid_name(const struct sys_wifi_bss *b, char out[33])
+{
+	unsigned int i, n = b->ssid_len;
+
+	if (n > 32)
+		n = 32;
+	for (i = 0; i < n; i++)
+		out[i] = (b->ssid[i] >= 32 && b->ssid[i] < 127) ?
+			 (char)b->ssid[i] : '?';
+	out[n] = '\0';
+	if (!n) {
+		out[0] = '(';
+		out[1] = 'h'; out[2] = 'i'; out[3] = 'd';
+		out[4] = 'd'; out[5] = 'e'; out[6] = 'n';
+		out[7] = ')';
+		out[8] = '\0';
+	}
+}
+
+static const char *security_name(unsigned int s)
+{
+	return s == SYS_WIFI_SEC_OPEN ? "open" :
+	       s == SYS_WIFI_SEC_PSK ? "WPA2" : "unsupported";
+}
+
+static int rssi_bars(int rssi)
+{
+	if (rssi >= -55)
+		return 4;
+	if (rssi >= -65)
+		return 3;
+	if (rssi >= -75)
+		return 2;
+	return 1;
+}
+
+static int bss_is_active(const struct sys_wifi_bss *b)
+{
+	unsigned int i;
+
+	if (wst.state < SYS_WIFI_ST_ASSOC)
+		return 0;
+	for (i = 0; i < 6; i++)
+		if (wst.bssid[i] != b->bssid[i])
+			return 0;
+	return 1;
+}
+
+static void set_status(const char *msg, int err)
+{
+	unsigned int i;
+
+	for (i = 0; msg && msg[i] && i + 1 < sizeof(status); i++)
+		status[i] = msg[i];
+	status[i] = '\0';
+	status_error = err;
+	status_until = msg && msg[0] ? u_ticks_ms() + 6000 : 0;
+	ngui_invalidate(&ui);
+}
+
+static int dev_active(const struct sys_netdev *d)
+{
+	return net_present && seq(net.name, d->name);
+}
+
+static void load_status(void)
+{
+	int i;
+
+	net_present = (u_net_info(&net) == 1 && net.present);
+	if (!wifi_checked || wifi_present) {
+		if (u_wifi(SYS_WIFI_STATUS, (int)&wst, 0) == 0) {
+			wifi_checked = 1;
+			wifi_present = wst.present != 0;
+		} else {
+			/* Do not repeatedly probe a machine without a card. */
+			wifi_checked = 1;
+			wifi_present = 0;
+		}
+	}
+	for (i = 0; i < ndev; i++) {
+		devs[i].active = dev_active(&devs[i]) ? 1u : 0u;
+		if (devs[i].active)
+			devs[i].link = net.link;
+	}
+}
+
+static void load_devices(void)
+{
+	int n = u_net_devlist(devs, MAX_DEVS);
+
+	ndev = n > 0 ? n : 0;
+	load_status();
+}
+
+static void drain_tty(void)
+{
+	char buf[256];
+	int n;
+
+	if (connect_tty <= 0)
+		return;
+	while ((n = u_tty_read(connect_tty, buf, sizeof(buf))) > 0) {
+		int i;
+
+		for (i = 0; i < n; i++) {
+			if (conn_log_len + 1 >= sizeof(conn_log)) {
+				unsigned int half = sizeof(conn_log) / 2;
+				unsigned int j;
+
+				for (j = 0; j < half; j++)
+					conn_log[j] = conn_log[conn_log_len -
+								 half + j];
+				conn_log_len = half;
+			}
+			conn_log[conn_log_len++] = buf[i];
+		}
+		conn_log[conn_log_len] = '\0';
+		ngui_invalidate(&ui);
+	}
+}
+
+static void sort_bss(void)
+{
+	int i, j;
+
+	for (i = 1; i < nbss; i++)
+		for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
+			struct sys_wifi_bss t = bss[j];
+
+			bss[j] = bss[j - 1];
+			bss[j - 1] = t;
+		}
+}
+
+static void start_scan(void)
+{
+	int r;
+
+	if (scanning || !wifi_present)
+		return;
+	set_status("Scanning for WiFi networks...", 0);
+	paint_frame();
+	/* SYS_WIFI_SCAN loads the firmware itself if it is still down. */
+	r = u_wifi(SYS_WIFI_SCAN, 0, 0);
+	if (r < 0) {
+		char m[48], *p;
+
+		u_wifi(SYS_WIFI_STATUS, (int)&wst, 0);
+		p = sput(m, "WiFi: ");
+		p = sput(p, wst.detail[0] ? wst.detail : "scan failed");
+		*p = '\0';
+		set_status(m, 1);
+		return;
+	}
+	scanning = 1;
+	scan_start = u_ticks_ms();
+	nbss = 0;
+	wifi_scroll.offset = 0;
+	ngui_invalidate(&ui);
+}
+
+static void poll_scan(void)
+{
+	int n;
+
+	if (!scanning)
+		return;
+	n = u_wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS);
+	if (n == -2) {
+		if (u_ticks_ms() - scan_start > 15000) {
+			scanning = 0;
+			set_status("WiFi scan timed out", 1);
+		}
+		return;
+	}
+	scanning = 0;
+	if (n < 0) {
+		set_status("WiFi scan failed", 1);
+		return;
+	}
+	nbss = n;
+	sort_bss();
+	if (nbss == 0)
+		set_status("No WiFi networks found", 1);
+	else
+		set_status("WiFi scan complete", 0);
+	ngui_invalidate(&ui);
+}
+
+static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
+{
+	struct wifi_cfg cfg;
+	unsigned int i, n = slen(pass);
+	int r;
+
+	for (i = 0; i < sizeof(cfg); i++)
+		((unsigned char *)&cfg)[i] = 0;
+	cfg.magic = WIFI_CFG_MAGIC;
+	cfg.ssid_len = b->ssid_len;
+	cfg.pass_len = (unsigned char)(n > 64 ? 64 : n);
+	for (i = 0; i < 6; i++)
+		cfg.bssid[i] = b->bssid[i];
+	for (i = 0; i < b->ssid_len && i < 32; i++)
+		cfg.ssid[i] = b->ssid[i];
+	for (i = 0; i < cfg.pass_len; i++)
+		cfg.pass[i] = pass[i];
+	r = u_syscall3(SYS_WRITE_FILE, (int)"/share/netmenu.wifi",
+		       (int)&cfg, (int)sizeof(cfg));
+	for (i = 0; i < sizeof(cfg); i++)
+		((unsigned char *)&cfg)[i] = 0;
+	return r;
+}
+
+static void start_connect(const char *pass)
+{
+	struct sys_wifi_bss *b;
+	char msg[80], *p;
+	int pid;
+
+	if (sel_bss < 0 || sel_bss >= nbss)
+		return;
+	if (connect_pid > 0 && u_alive((unsigned int)connect_pid)) {
+		set_status("A WiFi connection is already in progress", 1);
+		return;
+	}
+	b = &bss[sel_bss];
+	ssid_name(b, sel_ssid);
+	if (write_wifi_cfg(b, pass) != 0) {
+		set_status("Cannot stage the WiFi request in /share", 1);
+		return;
+	}
+	if (connect_tty <= 0) {
+		connect_tty = u_tty_create();
+		if (connect_tty < 0)
+			connect_tty = 0;
+	}
+	conn_log_len = 0;
+	conn_log[0] = '\0';
+	connect_start = u_ticks_ms();
+
+	pid = u_spawn("/ramfs/bin/wifi", "connectfile /share/netmenu.wifi",
+		      connect_tty, "/");
+	if (pid < 0) {
+		u_syscall1(SYS_UNLINK, (int)"/share/netmenu.wifi");
+		set_status("Cannot start the WiFi helper", 1);
+		return;
+	}
+	connect_pid = pid;
+	view = VIEW_CONNECT;
+	p = sput(msg, "Connecting to \"");
+	p = sput(p, sel_ssid);
+	p = sput(p, "\"...");
+	*p = '\0';
+	set_status(msg, 0);
+}
+
+static void cancel_connect(void)
+{
+	if (connect_pid > 0 && u_alive((unsigned int)connect_pid))
+		u_kill((unsigned int)connect_pid);
+	connect_pid = -1;
+	u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
+	view = VIEW_MAIN;
+	set_status("WiFi connection cancelled", 0);
+	load_status();
+}
+
+static void poll_connect(void)
+{
+	int i;
+
+	if (connect_pid <= 0)
+		return;
+
+	if (!u_alive((unsigned int)connect_pid)) {
+		connect_pid = -1;
+		load_status();
+		if (wst.state >= SYS_WIFI_ST_LINK) {
+			int have_ip = 0;
+
+			for (i = 0; i < 4; i++)
+				if (net.ip[i])
+					have_ip = 1;
+			view = VIEW_MAIN;
+			if (net_present && seq(net.name, "wifi")) {
+				if (have_ip)
+					set_status("WiFi connected and DHCP is up", 0);
+				else
+					set_status("WiFi connected, but DHCP got no lease", 1);
+			} else {
+				set_status("WiFi associated, but the netdev is busy", 1);
+			}
+		} else {
+			/* Keep the log on screen so the failure reason is visible;
+			 * the Cancel button becomes Back. */
+			set_status("WiFi connection failed", 1);
+		}
+		ngui_invalidate(&ui);
+		return;
+	}
+	if (u_ticks_ms() - connect_start > 45000) {
+		u_kill((unsigned int)connect_pid);
+		connect_pid = -1;
+		u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
+		set_status("WiFi connection timed out", 1);
+	}
+}
+
+static void select_device(int i)
+{
+	char msg[64], *p;
+
+	if (i < 0 || i >= ndev)
+		return;
+	p = sput(msg, "Selecting ");
+	p = sput(p, devs[i].name);
+	*p = '\0';
+	set_status(msg, 0);
+	paint_frame();
+
+	if (u_net_select(devs[i].name) != 0) {
+		set_status("Cannot select that netdev (busy or unavailable)", 1);
+		return;
+	}
+	set_status("Running DHCP...", 0);
+	paint_frame();
+	if (u_dhcp_run(6000) == 0)
+		set_status("Device selected; DHCP lease acquired", 0);
+	else
+		set_status("Device selected, but DHCP got no lease", 1);
+	load_devices();
+	ngui_invalidate(&ui);
+}
+
+static void select_bss(int i)
+{
+	if (i < 0 || i >= nbss)
+		return;
+	if (bss[i].ssid_len == 0) {
+		set_status("Hidden network: use `wifi connect` from a terminal", 1);
+		return;
+	}
+	sel_bss = i;
+	ssid_name(&bss[i], sel_ssid);
+	if (bss[i].security == SYS_WIFI_SEC_OTHER) {
+		set_status("Unsupported security (only open and WPA2-PSK)", 1);
+		return;
+	}
+	if (bss[i].security == SYS_WIFI_SEC_PSK) {
+		ngui_input_set(&pass_in, "");
+		pass_in.secret = 1;
+		pass_in.placeholder = "WPA2 passphrase";
+		view = VIEW_PASS;
+		set_status("", 0);
+		ngui_set_focus(&ui, ID_PASS);
+		ngui_invalidate(&ui);
+		return;
+	}
+	start_connect("");
+}
+
+static void handle_actions(void)
+{
+	unsigned int a = actions;
+	int di = action_dev, bi = action_bss;
+
+	actions = 0;
+	action_dev = -1;
+	action_bss = -1;
+
+	if (a & ACT_BACK) {
+		if (view == VIEW_CONNECT)
+			cancel_connect();
+		else {
+			view = VIEW_MAIN;
+			set_status("", 0);
+		}
+		return;
+	}
+	if (a & ACT_CONNECT) {
+		char pass[65];
+		unsigned int i, n;
+
+		ngui_input_value(&pass_in, pass, sizeof(pass));
+		n = slen(pass);
+		if (n == 0) {
+			set_status("Enter the WPA2 passphrase", 1);
+		} else if (n < 8 || n > 63) {
+			/* A 64-character hex string is a raw PSK. */
+			int hex = n == 64;
+
+			if (hex) {
+				unsigned int j;
+				for (j = 0; j < n; j++) {
+					char c = pass[j];
+					if (!((c >= '0' && c <= '9') ||
+					      (c >= 'a' && c <= 'f') ||
+					      (c >= 'A' && c <= 'F'))) {
+						hex = 0;
+						break;
+					}
+				}
+			}
+			if (hex)
+				start_connect(pass);
+			else
+				set_status("Use 8-63 characters or 64 hex digits", 1);
+		} else {
+			start_connect(pass);
+		}
+		for (i = 0; i < sizeof(pass); i++)
+			((volatile char *)pass)[i] = 0;
+		return;
+	}
+	if (a & ACT_REFRESH) {
+		set_status("Refreshing network devices...", 0);
+		paint_frame();
+		load_devices();
+		set_status("Device list refreshed", 0);
+		ngui_invalidate(&ui);
+	} else if (a & ACT_SCAN) {
+		start_scan();
+	} else if (a & ACT_DISCONNECT) {
+		u_wifi(SYS_WIFI_DISCONNECT, 0, 0);
+		load_status();
+		set_status("WiFi disconnected", 0);
+		ngui_invalidate(&ui);
+	} else if (di >= 0) {
+		select_device(di);
+	} else if (bi >= 0) {
+		select_bss(bi);
+	}
+}
+
+static void draw_bars(int x, int y, int bars, unsigned short color)
+{
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		int h = 3 + i * 3;
+		struct ngui_rect r;
+
+		r.x = x + i * 5;
+		r.y = y + 12 - h;
+		r.w = 3;
+		r.h = h;
+		ngui_fill_rect(&ui, r, i < bars ? color : ui.theme.border);
+	}
+}
+
+static void draw_status(void)
+{
+	if (status[0] && status_until &&
+	    (int)(u_ticks_ms() - status_until) < 0)
+		ngui_text(&ui, 10, ui.h - 22, status,
+			  status_error ? ui.theme.error : ui.theme.accent);
+}
+
+static void draw_wifi_list(struct ngui_rect area)
+{
+	char label[40], shortcut[20];
+	int i;
+
+	if (!wifi_present) {
+		ngui_text(&ui, area.x + 2, area.y + 4,
+			  "No WiFi card detected", ui.theme.text_dim);
+		return;
+	}
+	if (scanning) {
+		ngui_text(&ui, area.x + 2, area.y + 4,
+			  "Scanning...", ui.theme.text_dim);
+		return;
+	}
+	if (nbss == 0) {
+		ngui_text(&ui, area.x + 2, area.y + 4,
+			  wst.state >= SYS_WIFI_ST_ASSOC ?
+			  "Connected - Disconnect to scan for other networks" :
+			  "Press Scan to list WiFi networks",
+			  ui.theme.text_dim);
+		return;
+	}
+
+	wifi_scroll.content_h = nbss * ROW_H;
+	ngui_scroll_begin(&ui, area, &wifi_scroll);
+	ngui_scroll_wheel(&ui, area, &wifi_scroll);
+	ngui_scroll_keys(&ui, &wifi_scroll);
+	for (i = 0; i < nbss; i++) {
+		struct ngui_rect rr;
+		struct sys_wifi_bss *b = &bss[i];
+		char ssid[33];
+		int sw, bx, clicked;
+		unsigned short signal;
+
+		rr.x = area.x;
+		rr.y = area.y - wifi_scroll.offset + i * ROW_H;
+		rr.w = area.w - 16;
+		rr.h = ROW_H - 2;
+		if (rr.y + rr.h <= area.y || rr.y >= area.y + area.h)
+			continue;
+
+		ssid_name(b, ssid);
+		sput(label, ssid);
+		sput(shortcut, security_name(b->security));
+		clicked = ngui_menu_item(&ui, ID_BSS_BASE + i, rr, label,
+					 shortcut, bss_is_active(b));
+
+		sw = (int)ngui_text_width_ui(&ui, shortcut);
+		bx = rr.x + rr.w - 8 - sw - 30;
+		if (b->security == SYS_WIFI_SEC_OTHER)
+			signal = ui.theme.error;
+		else
+			signal = rssi_bars(b->rssi) >= 3 ? ui.theme.accent :
+				 ui.theme.text_dim;
+		draw_bars(bx, rr.y + 3, rssi_bars(b->rssi), signal);
+		if (clicked) {
+			action_bss = i;
+		}
+	}
+	ngui_scroll_end(&ui, area, &wifi_scroll);
+}
+
+static void draw_main(void)
+{
+	struct ngui_rect body = { 10, 8, ui.w - 20, ui.h - 34 };
+	struct ngui_rect r, rb;
+	char line[128], *p;
+	int i, shown;
+
+	/* Active connection summary. */
+	r = ngui_cut_top(&body, 20);
+	p = line;
+	if (!net_present) {
+		p = sput(p, "No active network device");
+	} else {
+		p = sput(p, "Active: ");
+		p = sput(p, net.name);
+		p = sput(p, " (");
+		p = sput(p, net.chip);
+		p = sput(p, ")");
+		if (seq(net.name, "wifi") && wst.state >= SYS_WIFI_ST_ASSOC) {
+			p = sput(p, "  ");
+			for (i = 0; i < wst.ssid_len && i < 32; i++)
+				*p++ = (wst.ssid[i] >= 32 && wst.ssid[i] < 127) ?
+				       (char)wst.ssid[i] : '?';
+			p = sput(p, " ");
+			p = put_signed(p, wst.rssi);
+			p = sput(p, " dBm");
+		}
+	}
+	*p = '\0';
+	ngui_text_ellipsis(&ui, r, r.x, r.y, line, ui.theme.text);
+
+	r = ngui_cut_top(&body, 20);
+	p = line;
+	if (net_present) {
+		p = sput(p, "IP ");
+		if (net.ip[0] || net.ip[1] || net.ip[2] || net.ip[3])
+			p = put_ip(p, net.ip);
+		else
+			p = sput(p, "not configured");
+		if (seq(net.name, "wifi"))
+			p = sput(p, wst.state >= SYS_WIFI_ST_LINK ?
+				 "  link up" : "  link down");
+	} else if (wifi_present) {
+		p = sput(p, "WiFi card present - scan to join a network");
+	} else {
+		p = sput(p, "Plug in Ethernet or USB tethering, then Refresh");
+	}
+	*p = '\0';
+	ngui_text_ellipsis(&ui, r, r.x, r.y, line, ui.theme.text_dim);
+	body.y += 4;
+	body.h -= 4;
+
+	/* Devices. */
+	r = ngui_cut_top(&body, 26);
+	ngui_text(&ui, r.x, r.y + 3, "Network devices", ui.theme.text_dim);
+	rb = r;
+	rb.x = r.x + r.w - 78;
+	rb.w = 78;
+	rb.h = 22;
+	if (ngui_button(&ui, ID_REFRESH, rb, "Refresh"))
+		actions |= ACT_REFRESH;
+
+	shown = 0;
+	for (i = 0; i < ndev && shown < 6; i++) {
+		if (devs[i].kind == SYS_NETDEV_WIFI)
+			continue;
+		p = line;
+		p = sput(p, devs[i].name);
+		p = sput(p, "  ");
+		p = sput(p, devs[i].chip);
+		*p = '\0';
+		r = ngui_cut_top(&body, ROW_H);
+		if (ngui_menu_item(&ui, ID_DEV_BASE + i, r, line,
+				   devs[i].active ? "active" :
+				   (devs[i].link ? "link" : ""),
+				   devs[i].active))
+			action_dev = i;
+		shown++;
+	}
+	if (!shown) {
+		r = ngui_cut_top(&body, ROW_H);
+		ngui_text(&ui, r.x + 2, r.y + 3,
+			  wifi_present ?
+			  "No wired or USB device - use the WiFi list" :
+			  "None found - Refresh after plugging a device in",
+			  ui.theme.text_dim);
+	}
+	body.y += 6;
+	body.h -= 6;
+
+	/* WiFi networks. */
+	r = ngui_cut_top(&body, 26);
+	ngui_text(&ui, r.x, r.y + 3, "WiFi networks", ui.theme.text_dim);
+	rb = r;
+	rb.x = r.x + r.w - 96;
+	rb.w = 96;
+	rb.h = 22;
+	if (wifi_present) {
+		if (wst.state >= SYS_WIFI_ST_ASSOC) {
+			if (ngui_button(&ui, ID_DISCONNECT, rb, "Disconnect"))
+				actions |= ACT_DISCONNECT;
+		} else if (scanning) {
+			ngui_text(&ui, rb.x + 10, rb.y + 3, "Scanning...",
+				  ui.theme.text_dim);
+		} else {
+			if (ngui_button(&ui, ID_SCAN, rb, "Scan"))
+				actions |= ACT_SCAN;
+		}
+	}
+	draw_wifi_list(body);
+}
+
+static void draw_pass(void)
+{
+	struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
+	struct ngui_rect r, b1, b2;
+	char line[80], *p;
+	int ev;
+
+	r = ngui_cut_top(&body, 24);
+	p = sput(line, "Join WiFi network \"");
+	p = sput(p, sel_ssid);
+	p = sput(p, "\"");
+	*p = '\0';
+	ngui_text_ellipsis(&ui, r, r.x, r.y, line, ui.theme.text);
+
+	r = ngui_cut_top(&body, 22);
+	ngui_text(&ui, r.x, r.y, "WPA2-PSK passphrase", ui.theme.text_dim);
+
+	r = ngui_cut_top(&body, 30);
+	ev = ngui_text_input(&ui, ID_PASS, r, &pass_in);
+	if (ev & NGUI_INPUT_SUBMIT)
+		actions |= ACT_CONNECT;
+	if (ev & NGUI_INPUT_CANCEL)
+		actions |= ACT_BACK;
+	body.y += 6;
+	body.h -= 6;
+
+	r = ngui_cut_top(&body, 30);
+	b1 = r;
+	b1.w = 100;
+	b2 = r;
+	b2.x = r.x + 110;
+	b2.w = 100;
+	if (ngui_button(&ui, ID_PASS_OK, b1, "Connect"))
+		actions |= ACT_CONNECT;
+	if (ngui_button(&ui, ID_PASS_CANCEL, b2, "Cancel"))
+		actions |= ACT_BACK;
+
+	r = ngui_cut_top(&body, 22);
+	ngui_text(&ui, r.x, r.y,
+		  "8-63 characters, or 64 hex digits for a raw PSK.",
+		  ui.theme.text_dim);
+	draw_status();
+}
+
+static void draw_connect(void)
+{
+	struct ngui_rect body = { 10, 10, ui.w - 20, ui.h - 40 };
+	struct ngui_rect r, b;
+	char line[96], *p;
+	unsigned int start = 0, lines = 0, i;
+	int y, line_y;
+
+	r = ngui_cut_top(&body, 24);
+	p = line;
+	if (connect_pid > 0) {
+		p = sput(p, "Connecting to \"");
+		p = sput(p, sel_ssid);
+		p = sput(p, "\"...");
+	} else {
+		p = sput(p, "WiFi connection \"");
+		p = sput(p, sel_ssid);
+		p = sput(p, "\"");
+	}
+	*p = '\0';
+	ngui_text_ellipsis(&ui, r, r.x, r.y, line, ui.theme.text);
+
+	r = ngui_cut_top(&body, 22);
+	if (status[0])
+		ngui_text(&ui, r.x, r.y, status,
+			  status_error ? ui.theme.error : ui.theme.accent);
+	body.y += 4;
+	body.h -= 4;
+
+	/* Last ~12 lines of the helper's terminal output. */
+	for (i = conn_log_len; i > 0; i--) {
+		if (conn_log[i - 1] == '\n' && ++lines > 12) {
+			start = i;
+			break;
+		}
+	}
+	y = body.y;
+	line_y = 0;
+	for (i = start; i < conn_log_len && y + 16 <= body.y + body.h - 34; i++) {
+		char c = conn_log[i];
+
+		if (c == '\r')
+			continue;
+		if (c == '\n') {
+			line[line_y] = '\0';
+			ngui_text(&ui, body.x, y, line, ui.theme.text_dim);
+			y += 16;
+			line_y = 0;
+			continue;
+		}
+		if ((unsigned int)line_y + 1 < sizeof(line))
+			line[line_y++] = (c >= 32 && c < 127) ? c : '.';
+	}
+	if (line_y) {
+		line[line_y] = '\0';
+		ngui_text(&ui, body.x, y, line, ui.theme.text_dim);
+	}
+
+	/* Cancel at the bottom. */
+	b.x = body.x;
+	b.y = body.y + body.h - 28;
+	b.w = 100;
+	b.h = 24;
+	if (ngui_button(&ui, ID_CONN_CANCEL, b,
+			connect_pid > 0 ? "Cancel" : "Back"))
+		actions |= ACT_BACK;
+}
+
+static void draw(void)
+{
+	ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
+		       ui.theme.bg);
+	if (view == VIEW_PASS)
+		draw_pass();
+	else if (view == VIEW_CONNECT)
+		draw_connect();
+	else
+		draw_main();
+	if (view != VIEW_PASS && view != VIEW_CONNECT)
+		draw_status();
+}
+
+static void paint_frame(void)
+{
+	if (ngui_begin(&ui)) {
+		draw();
+		ngui_end(&ui);
+	}
+	actions = 0;
+	action_dev = -1;
+	action_bss = -1;
+}
+
+void _start(const char *arg)
+{
+	struct win_info wi;
+	unsigned int last_status = 0;
+
+	(void)arg;
+	if (u_win_create(460, 500, "Network", &wi) != 0 &&
+	    u_win_create(400, 420, "Network", &wi) != 0 &&
+	    u_win_create(340, 360, "Network", &wi) != 0) {
+		u_exit(1);
+	}
+	ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
+	ngui_font_attach(&ui);
+	pass_in.secret = 1;
+	pass_in.placeholder = "WPA2 passphrase";
+	view = VIEW_MAIN;
+
+	load_devices();
+	load_status();
+	if (wifi_present && wst.state != SYS_WIFI_ST_DOWN &&
+	    wst.state < SYS_WIFI_ST_ASSOC) {
+		start_scan();
+		paint_frame();
+	}
+
+	for (;;) {
+		unsigned int now = u_ticks_ms();
+
+		drain_tty();
+		poll_scan();
+		poll_connect();
+		if (now - last_status >= 1000) {
+			last_status = now;
+			load_status();
+			ngui_invalidate(&ui);
+		}
+		if (status_until && (int)(now - status_until) >= 0) {
+			status[0] = '\0';
+			status_until = 0;
+			ngui_invalidate(&ui);
+		}
+
+		if (ngui_begin(&ui)) {
+			draw();
+			ngui_end(&ui);
+			handle_actions();
+		}
+
+		/* The password field owns Esc while focused. */
+		if (ngui_key_peek(&ui) == NGUI_KEY_ESC) {
+			ngui_key(&ui);
+			if (view == VIEW_PASS) {
+				view = VIEW_MAIN;
+				set_status("", 0);
+			} else if (view == VIEW_CONNECT) {
+				cancel_connect();
+			} else if (ui.focus == 0) {
+				break;
+			}
+		}
+
+		u_sleep_ms(25);
+	}
+	ngui_font_detach(&ui);
+	u_win_close();
+	u_exit(0);
+}

+ 67 - 1
ucmd_wifi.c

@@ -7,6 +7,9 @@
  *                                join an open or WPA2-PSK (CCMP) network,
  *                                run DHCP; quote an SSID with spaces; a
  *                                BSSID picks one of several APs
+ *   wifi connectfile <path>      join using an SSID/passphrase/BSSID record
+ *                                staged by the network menu (SYS_SPAWN's
+ *                                argument buffer is only 64 bytes)
  *   wifi disconnect
  *
  * The WPA2 handshake (wpa.c) runs here; a small background copy of this
@@ -15,6 +18,7 @@
 
 #include "ucmd_lib.h"
 #include "tls_rng.h"
+#include "wifi_cfg.h"
 #include "wpa.h"
 
 #define MAX_BSS 32
@@ -70,6 +74,8 @@ static const char *word(const char *s, char *out, unsigned int max)
 		s++;
 	}
 	while (*s && *s != end) {
+		if (end == '"' && *s == '\\' && s[1])
+			s++;
 		if (n + 1 < max)
 			out[n++] = *s;
 		s++;
@@ -460,6 +466,64 @@ static int cmd_connect(const char *args)
 	return 0;
 }
 
+/* Quote a raw byte string for the connect argument parser (which understands
+ * backslash escapes inside double quotes). */
+static char *put_qbytes(char *p, const unsigned char *s, unsigned int n)
+{
+	unsigned int i;
+
+	*p++ = '"';
+	for (i = 0; i < n; i++) {
+		if (s[i] == '"' || s[i] == '\\')
+			*p++ = '\\';
+		*p++ = (char)s[i];
+	}
+	*p++ = '"';
+	return p;
+}
+
+/* `wifi connectfile <path>`: the network menu stages an SSID + passphrase +
+ * BSSID record in /share because SYS_SPAWN's argument buffer is only
+ * VFS_PATH_MAX (64) bytes.  The file is removed after it has been read. */
+static int cmd_connect_file(const char *args)
+{
+	static const char hx[] = "0123456789abcdef";
+	struct wifi_cfg cfg;
+	char path[80], line[320], *p;
+	unsigned int i, n;
+	int r;
+
+	args = word(args, path, sizeof(path));
+	for (i = 0; i < sizeof(cfg); i++)
+		((unsigned char *)&cfg)[i] = 0;
+	r = u_syscall3(SYS_READ_FILE, (int)path, (int)&cfg, (int)sizeof(cfg));
+	u_syscall1(SYS_UNLINK, (int)path);
+	if (r < 0 || (unsigned int)r < sizeof(cfg) ||
+	    cfg.magic != WIFI_CFG_MAGIC) {
+		u_puts("wifi: no valid staged connect request\n");
+		return 1;
+	}
+	if (cfg.ssid_len == 0 || cfg.ssid_len > 32 || cfg.pass_len > 64) {
+		u_puts("wifi: staged connect request is malformed\n");
+		return 1;
+	}
+	p = line;
+	for (n = 0; "connect "[n]; n++)
+		*p++ = "connect "[n];
+	p = put_qbytes(p, cfg.ssid, cfg.ssid_len);
+	*p++ = ' ';
+	p = put_qbytes(p, (const unsigned char *)cfg.pass, cfg.pass_len);
+	*p++ = ' ';
+	for (i = 0; i < 6; i++) {
+		*p++ = hx[cfg.bssid[i] >> 4];
+		*p++ = hx[cfg.bssid[i] & 15];
+	}
+	*p = '\0';
+	for (i = 0; i < sizeof(cfg); i++)
+		((unsigned char *)&cfg)[i] = 0;
+	return cmd_connect(line);
+}
+
 /* Background: answer group-key rekeys until the connection changes. */
 static int keeper(const char *args)
 {
@@ -537,11 +601,13 @@ void _start(const char *arg)
 		r = cmd_scan();
 	else if (starts(a, "connect "))
 		r = cmd_connect(a + 8);
+	else if (starts(a, "connectfile "))
+		r = cmd_connect_file(a + 12);
 	else if (starts(a, "disconnect")) {
 		wifi(SYS_WIFI_DISCONNECT, 0, 0);
 		u_puts("wifi: disconnected\n");
 	} else {
-		u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|disconnect]\n");
+		u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|disconnect]\n");
 		r = 1;
 	}
 	u_exit(r);

+ 244 - 2
ucmd_wm.c

@@ -61,6 +61,22 @@ static unsigned int bar_clock_w;
 static int bar_clock_hot;
 static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
 
+/* Network status icon (right side of the bar). */
+#define BAR_NET_W 24
+#define BAR_NET_Y 6
+#define BAR_NET_NONE  0
+#define BAR_NET_WIRED 1
+#define BAR_NET_WIFI  2
+#define BAR_NET_USB   3
+static int bar_net_x = -1;
+static unsigned int bar_net_w;
+static int bar_net_hot;
+static int bar_net_kind;
+static int bar_net_bars;
+static unsigned int bar_net_color = 0xFF5050u;
+static int netmenu_pid;
+static int netmenu_placed;
+
 static void bar_notice(const char *msg)
 {
 	unsigned int i;
@@ -132,6 +148,126 @@ static void bar_button(unsigned int x, unsigned int y, unsigned int w,
 		   label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
 }
 
+static int name_is(const char *a, const char *b)
+{
+	while (*a && *b) {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
+static int net_rssi_bars(int rssi)
+{
+	if (rssi >= -55)
+		return 4;
+	if (rssi >= -65)
+		return 3;
+	if (rssi >= -75)
+		return 2;
+	return 1;
+}
+
+/* Small top-bar pictograms, drawn directly into the shadow. */
+static void bar_icon_cable(int x, int y, unsigned int c)
+{
+	usurf_fill(&screen, x + 1, y + 6, 9, 8, c);
+	usurf_fill(&screen, x + 3, y + 8, 2, 2, COL_BG);
+	usurf_fill(&screen, x + 6, y + 8, 2, 2, COL_BG);
+	usurf_fill(&screen, x + 10, y + 9, 7, 2, c);
+	usurf_fill(&screen, x + 17, y + 5, 5, 10, c);
+	usurf_fill(&screen, x + 19, y + 7, 2, 3, COL_BG);
+}
+
+static void bar_icon_wifi(int x, int y, unsigned int c, int bars)
+{
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		int h = 3 + i * 3;
+
+		usurf_fill(&screen, x + 3 + i * 5, y + 16 - h, 3, h,
+			   i < bars ? c : 0x4A4A5Au);
+	}
+}
+
+static void bar_icon_x(int x, int y, unsigned int c)
+{
+	int i;
+
+	for (i = 0; i < 12; i++) {
+		usurf_fill(&screen, x + 6 + i, y + 4 + i, 2, 2, c);
+		usurf_fill(&screen, x + 17 - i, y + 4 + i, 2, 2, c);
+	}
+}
+
+static void bar_icon_usb(int x, int y, unsigned int c)
+{
+	usurf_text(&screen, x + 1, y + 1, "USB", c, 0, 0, fbw, WM_BAR_H);
+}
+
+static void bar_draw_net_icon(int x, int y)
+{
+	switch (bar_net_kind) {
+	case BAR_NET_WIRED:
+		bar_icon_cable(x, y, bar_net_color);
+		break;
+	case BAR_NET_WIFI:
+		bar_icon_wifi(x, y, bar_net_color, bar_net_bars);
+		break;
+	case BAR_NET_USB:
+		bar_icon_usb(x, y, bar_net_color);
+		break;
+	default:
+		bar_icon_x(x, y, bar_net_color);
+		break;
+	}
+}
+
+/* 1 Hz poll: cached state -> the bar's network icon.  The WiFi status call is
+ * only made when WiFi is the active netdev, so a machine without a card does
+ * not run a PCI scan on every tick. */
+static int poll_net_status(void)
+{
+	struct sys_netinfo ni;
+	int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
+	unsigned int color = 0xFF5050u;
+	int changed;
+	int i;
+
+	if (u_net_info(&ni) == 1 && ni.present) {
+		for (i = 0; i < 4; i++)
+			if (ni.ip[i])
+				have_ip = 1;
+		if (name_is(ni.name, "wifi")) {
+			struct sys_wifi_status ws;
+
+			if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
+			    ws.present && ws.state >= SYS_WIFI_ST_ASSOC) {
+				kind = BAR_NET_WIFI;
+				bars = net_rssi_bars(ws.rssi);
+				color = (ws.state >= SYS_WIFI_ST_LINK && have_ip) ?
+					0x55FF55u : 0xFFD250u;
+			}
+		} else if (name_is(ni.name, "rndis")) {
+			kind = BAR_NET_USB;
+			color = have_ip ? 0x55FF55u : 0xFFD250u;
+		} else {
+			kind = BAR_NET_WIRED;
+			color = have_ip ? 0x55FF55u : 0xFFD250u;
+		}
+	}
+
+	changed = kind != bar_net_kind || bars != bar_net_bars ||
+		  color != bar_net_color;
+	bar_net_kind = kind;
+	bar_net_bars = bars;
+	bar_net_color = color;
+	return changed;
+}
+
 static unsigned int temp_color(int c)
 {
 	if (c >= 80)
@@ -261,6 +397,8 @@ static void draw_bar(void)
 		bar_clock_x = -1;
 		bar_clock_w = 0;
 		bar_clock_hot = 0;
+		bar_net_x = -1;
+		bar_net_w = 0;
 		n = 0;
 		while (msg[n])
 			dt[n] = msg[n], n++;
@@ -278,6 +416,8 @@ static void draw_bar(void)
 
 		bar_clock_x = -1;
 		bar_clock_w = 0;
+		bar_net_x = -1;
+		bar_net_w = 0;
 		if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
 			int tw = (int)usurf_text_width(&screen, dt);
 			int tx = right - tw;
@@ -334,10 +474,25 @@ static void draw_bar(void)
 			(void)ac_text(ab, bar_ac_online);
 			ax = right - (int)usurf_text_width(&screen, ab);
 
-			if (ax >= min_x)
+			if (ax >= min_x) {
 				usurf_text(&screen, ax, 5, ab,
 					   ac_color(bar_ac_online), 0, 0,
 					   fbw, WM_BAR_H);
+				right = ax - 8;
+			}
+		}
+		{
+			int nx = right - BAR_NET_W;
+
+			if (nx >= min_x) {
+				if (bar_net_hot)
+					usurf_fill(&screen, nx - 3, 2,
+						   BAR_NET_W + 6,
+						   WM_BAR_H - 4, 0x3A3A55u);
+				bar_draw_net_icon(nx, BAR_NET_Y);
+				bar_net_x = nx;
+				bar_net_w = BAR_NET_W;
+			}
 		}
 	}
 }
@@ -762,6 +917,13 @@ static int bar_clock_hit(unsigned int mx, unsigned int my)
 	       (int)mx < bar_clock_x + (int)bar_clock_w + 5;
 }
 
+static int bar_net_hit(unsigned int mx, unsigned int my)
+{
+	return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
+	       (int)mx >= bar_net_x - 3 &&
+	       (int)mx < bar_net_x + (int)bar_net_w + 3;
+}
+
 static void do_update(struct wm_status *st)
 {
 	struct wm_present_info present;
@@ -811,6 +973,14 @@ static void do_update(struct wm_status *st)
 			bar_dirty = 1;
 		}
 	}
+	{
+		int hot = bar_net_hit(st->mouse.x, st->mouse.y);
+
+		if (hot != bar_net_hot) {
+			bar_net_hot = hot;
+			bar_dirty = 1;
+		}
+	}
 
 	/* Refresh cached window state and mappings before opening a frame:
 	 * MAP may unmap/remap content. */
@@ -844,6 +1014,44 @@ static void do_update(struct wm_status *st)
 			}
 		}
 	}
+	/* Anchor the network menu under its bar icon once it has created its
+	 * window.  The WM owns the geometry syscall, so the app itself does
+	 * not need a special placement ABI. */
+	if (netmenu_pid > 0) {
+		int found = 0;
+
+		for (i = 0; i < WM_MAX_WINDOWS; i++) {
+			struct wm_window_info ni;
+
+			if (!wins[i].info.used ||
+			    wins[i].info.pid != (unsigned int)netmenu_pid)
+				continue;
+			found = 1;
+			if (!netmenu_placed) {
+				int nx = (bar_net_x >= 0 ?
+					  bar_net_x + (int)bar_net_w :
+					  (int)fbw) - (int)wins[i].info.w;
+				int ny = WM_BAR_H + 2;
+
+				if (nx < 4)
+					nx = 4;
+				if (nx + (int)wins[i].info.w > (int)fbw - 4)
+					nx = (int)fbw - 4 -
+					     (int)wins[i].info.w;
+				if (u_wm_set_geom(i, nx, ny) == 0 &&
+				    u_wm_info(i, &ni) == 1) {
+					wins[i].info = ni;
+					full_redraw = 1;
+				}
+				netmenu_placed = 1;
+			}
+			break;
+		}
+		if (!found && !u_alive((unsigned int)netmenu_pid)) {
+			netmenu_pid = 0;
+			netmenu_placed = 0;
+		}
+	}
 	/* The RTC is slow (CMOS polling) and the temperature/battery state only
 	 * changes once a second; query them at 1 Hz instead of every 10 ms. */
 	{
@@ -904,6 +1112,8 @@ static void do_update(struct wm_status *st)
 					}
 				}
 			}
+			if (poll_net_status())
+				bar_dirty = 1;
 		}
 	}
 	if (full_redraw) {
@@ -1004,6 +1214,36 @@ static void spawn_app_checked(const char *path)
 	}
 }
 
+/* The network icon opens one menu at a time; a second click focuses it. */
+static void open_netmenu(void)
+{
+	unsigned int i;
+
+	if (netmenu_pid > 0 && u_alive((unsigned int)netmenu_pid)) {
+		for (i = 0; i < WM_MAX_WINDOWS; i++)
+			if (wins[i].info.used &&
+			    wins[i].info.pid == (unsigned int)netmenu_pid) {
+				u_wm_focus(i);
+				full_redraw = 1;
+				return;
+			}
+	}
+	if (!free_window_slot()) {
+		bar_notice("window limit");
+		full_redraw = 1;
+		return;
+	}
+	netmenu_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
+	netmenu_placed = 0;
+	if (netmenu_pid < 0) {
+		netmenu_pid = 0;
+		bar_notice("spawn failed");
+		full_redraw = 1;
+	} else {
+		full_redraw = 1;
+	}
+}
+
 static void handle_mouse(struct wm_status *st)
 {
 	unsigned int buttons = st->mouse.buttons;
@@ -1016,7 +1256,9 @@ static void handle_mouse(struct wm_status *st)
 		unsigned int now = u_ticks_ms();
 
 		last_title_id = -1;
-		if (bar_clock_hit(mx, my)) {
+		if (bar_net_hit(mx, my)) {
+			open_netmenu();
+		} else if (bar_clock_hit(mx, my)) {
 			spawn_app_checked("/ramfs/bin/settime");
 		} else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
 		    mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {

+ 22 - 0
wifi_cfg.h

@@ -0,0 +1,22 @@
+/* Staged WiFi connect request, written by the network menu and read by
+ * `wifi connectfile <path>`.
+ *
+ * The normal `wifi connect <ssid> <pass>` command arguments are limited by
+ * SYS_SPAWN's VFS_PATH_MAX buffer (64 bytes), which is too small for a long
+ * SSID plus a 63-character WPA2 passphrase.  The menu therefore writes this
+ * fixed-size record to a /share file and spawns a helper with a short path. */
+#ifndef WIFI_CFG_H
+#define WIFI_CFG_H
+
+#define WIFI_CFG_MAGIC 0x4E425746u   /* "NBWF" */
+
+struct wifi_cfg {
+	unsigned int magic;
+	unsigned char ssid_len;          /* 1..32 */
+	unsigned char pass_len;          /* 0..64 (64 = raw hex PSK) */
+	unsigned char bssid[6];
+	unsigned char ssid[32];
+	char pass[65];                   /* NUL terminated, pass_len used */
+};
+
+#endif