Преглед изворни кода

Add emoji fallback text and persistent WiFi credentials

Extend the ring-3 font stack with UTF-8 text and a per-codepoint emoji
fallback.  ufont now parses TTF cmap format 12 and looks up codepoints
above the BMP; ngui gains UTF-8 text, width, ellipsis and menu-item
variants that try the selected primary face, then a lazily loaded
monochrome Noto Emoji subset.  The new fonts/emoji.ttf is a curated
382-glyph subset (including U+1F349) limited to glyphs that fit the
existing 256-point/16-contour rasterizer; ZWJ sequences, flags and color
glyphs fall back to tofu.  netmenu keeps raw SSID bytes for connecting
but renders names through the UTF-8 path, so an emoji SSID is no longer
shown as "????".

A successful WiFi connect from the menu or CLI now saves the SSID and
passphrase to /data/WIFI.CFG (8.3 FAT, plaintext, BSSID zeroed so a
mesh/repeater AP still works).  When that file exists, kernel_main
spawns `wifi autoconnect` after the boot DHCP attempt; it skips a wired
link that got a DHCP lease and leaves an existing WiFi link alone.
`wifi forget` removes the saved record.  The credential reader/writer
keeps 512 bytes of slack because the FAT path may transfer a final
partial sector past the requested length; without it, autoconnect
faulted at the user stack top.

Verified with make all from the staged tree, make test-font and
test-font-asan, a host ngui render of an emoji menu row, and a QEMU boot
with a NEBOSDATA disk (mount + autoconnect spawn, no fault).
Johannes Zellner пре 1 недеља
родитељ
комит
9d0bd469aa
21 измењених фајлова са 719 додато и 68 уклоњено
  1. 35 13
      AGENTS.md
  2. 4 4
      Makefile
  3. 16 5
      docs/graphics.html
  4. 12 0
      docs/network.html
  5. 6 3
      docs/userland.html
  6. 2 0
      fonts/LICENSE-OFL
  7. 18 2
      fonts/README
  8. 0 0
      fonts/emoji-unicodes.txt
  9. BIN
      fonts/emoji.ttf
  10. 32 0
      kernel.c
  11. 211 7
      ngui.c
  12. 13 0
      ngui.h
  13. 11 0
      tools/subset_fonts.sh
  14. 15 15
      ucmd_netmenu.c
  15. 180 3
      ucmd_wifi.c
  16. 19 0
      ufont.c
  17. 10 0
      ufont.h
  18. 3 0
      ufont_internal.h
  19. 50 0
      ufont_test.c
  20. 80 16
      ufont_ttf.c
  21. 2 0
      wifi_cfg.h

+ 35 - 13
AGENTS.md

@@ -445,11 +445,17 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   subset.
 - **Fonts**: `ufont.c/h`, `ufont_ttf.c`, `ufont_draw.c`, `ufont_file.c` — a
   fixed-point (26.6, no FPU) TrueType parser and antialiased rasterizer for
-  ring-3 text.  Latin-1 only for now; the faces are the Arimo regular/bold/
-  italic/bold-italic and Liberation Mono subsets in `fonts/`, packed into
-  `/ramfs/fonts`.  `ufont` owns one 512 KB `malloc` arena for face data and
-  glyph masks; the five built-in faces plus up to 11 `@font-face` faces share
-  it.  `ngui_font_attach/detach` (ngui) and `usurf_font_attach`
+  ring-3 text.  The byte/Latin-1 path remains the default; cmap format 4 and
+  format 12 are supported, and the new UTF-8 `ngui_text_utf8*`/
+  `ngui_menu_item_utf8` APIs fall back per codepoint to the monochrome
+  `fonts/emoji.ttf` subset (`ngui_font_attach` loads it lazily).  The
+  existing faces are the Arimo regular/bold/italic/bold-italic and Liberation
+  Mono subsets plus Noto Emoji in `fonts/`, packed into `/ramfs/fonts`.  The
+  emoji subset is limited to common single-codepoint glyphs that fit the
+  rasterizer (256 points / 16 contours); ZWJ sequences, flags, color glyphs
+  and shaping are not supported.  `ufont` owns one 512 KB `malloc` arena for
+  face data and glyph masks; the built-in faces plus up to 11 `@font-face`
+  faces share it.  `ngui_font_attach/detach` (ngui) and `usurf_font_attach`
   (`uwm_lib`) load faces and leave the 8×16 bitmap fallback when they are
   absent.  The **browser calls `ngui_font_detach` before `heap_reset()` and
   reattaches after `heap_start()`**, because its page heap is reset on every
@@ -639,12 +645,21 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   ring would keep writing into reused pages).  The ring-3 side is
   `ucmd_wifi.c` (`wifi`, `wifi up|scan|connect <ssid> [pass]|disconnect`,
   `wifi connectfile <path>` for the network menu's staged SSID/pass/BSSID
-  record),
+  record, `wifi autoconnect` for the boot helper and `wifi forget`),
   `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
   TK/GTK, calls `SYS_WIFI_AUTHORIZE` (data port open + `netdev_select
   ("wifi")`), runs DHCP and leaves `wifi __keep` running for group rekeys.
+  A successful menu/CLI connect also writes the SSID and passphrase to
+  `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
+  after the boot DHCP attempt `kernel_main` spawns
+  `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect skips a
+  wired link that got a DHCP lease and leaves an already-linked WiFi card
+  alone; `/data` is the persistent FAT volume from the installer.
+  `wifi forget` removes the saved record.  A FAT read may write a final
+  partial sector past the requested length, so the credential reader keeps
+  512 bytes of slack on the stack.
   The helper's PTK and EAPOL replay counter are kept in the kernel
   (`SYS_WIFI_SET_PTK`/`SYS_WIFI_GET_PTK`) and fetched by connection ID, so
   the 64-byte `SYS_SPAWN` argument limit cannot truncate the rekey state.
@@ -1842,12 +1857,17 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     pulled into commands that reference them; the legacy pack still needs
     every added byte checked against `CMDS_GZ_MAX`.
 
-26. Fonts: `ufont` is a Latin-1-only, unhinted fixed-point TrueType subset;
-    there is no shaping/bidi/kerning, CFF or variable-font support, and no
-    full-Unicode fallback.  Small sizes (below ~12 px) are softer than the
-    bitmap font.  The 512 KB arena is shared by five built-in faces, up to
-    11 `@font-face` faces and glyph masks; a very text-heavy page can exhaust
-    it and fall back to re-rasterizing evicted glyphs.  The browser must detach/re-attach its font arena around
+26. Fonts: `ufont` remains a fixed-point TrueType implementation with no
+    shaping/bidi/kerning, CFF or variable-font support.  UTF-8 rendering is
+    opt-in through `ngui_text_utf8*`; the browser/terminal and other byte
+    paths stay Latin-1.  The emoji face is a curated monochrome Noto Emoji
+    subset and only covers single-codepoint glyphs that fit the 256-point /
+    16-contour rasterizer: ZWJ sequences, flags, skin-tone sequences and
+    color glyphs fall back to a tofu/`?`.  Small sizes (below ~12 px) are
+    softer than the bitmap font.  The 512 KB arena is shared by the built-in
+    faces (including emoji), up to 11 `@font-face` faces and glyph masks; a
+    very text-heavy page can exhaust it and fall back to re-rasterizing
+    evicted glyphs.  The browser must detach/re-attach its font arena around
     `heap_reset()`; terminal/editor cell grids stay on the 8×16 bitmap font,
     while ngui text fields use the proportional face.
 
@@ -1980,4 +2000,6 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     `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.
+    cancel a connection attempt.  Saved credentials live in plaintext
+    `/data/WIFI.CFG` (8.3), hold one network, and are retried at boot; use
+    `wifi forget` to remove them.  There is no menu button for forgetting yet.

+ 4 - 4
Makefile

@@ -73,7 +73,7 @@ all: floppy.img usb.img final.bin root.img image.bin
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.o: kernel.c acpi.h cpu.h cros_ec.h idle.h io.h lapic.h mouse.h panic.h phys.h process.h sched.h smp.h terminal.h timer.h serial.h tss.h usbhid.h version.h vga.h
+kernel.o: kernel.c acpi.h cpu.h cros_ec.h idle.h io.h lapic.h mouse.h panic.h phys.h process.h sched.h smp.h terminal.h timer.h serial.h tss.h usbhid.h version.h vga.h wifi_cfg.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 screen.o: screen.c screen.h
@@ -649,7 +649,7 @@ ufont_file.o: ufont_file.c ufont.h ucmd_lib.h syscall.h $(LIBC_HEADERS)
 	$(CC) $(CFLAGS) -Os $(SECTION_CFLAGS) -Ilibc/include -o $@ $<
 
 UFONT_TEST_SRCS = ufont_test.c ufont.c ufont_ttf.c ufont_draw.c
-ufont_test: $(UFONT_TEST_SRCS) ufont.h ufont_internal.h
+ufont_test: $(UFONT_TEST_SRCS) ufont.h ufont_internal.h fonts/emoji.ttf
 	gcc -O2 -Wall -Wextra -I. -o $@ $(UFONT_TEST_SRCS)
 
 test-font: ufont_test
@@ -661,7 +661,7 @@ test-touchpad: touchpad_gesture_test
 touchpad_gesture_test: touchpad_gesture_test.c touchpad_gesture.c touchpad_gesture.h
 	gcc -O2 -Wall -Wextra -I. -o $@ touchpad_gesture_test.c touchpad_gesture.c
 
-test-font-asan: $(UFONT_TEST_SRCS) ufont.h ufont_internal.h
+test-font-asan: $(UFONT_TEST_SRCS) ufont.h ufont_internal.h fonts/emoji.ttf
 	gcc -O1 -g -fsanitize=address,undefined -I. -o ufont_test_asan $(UFONT_TEST_SRCS)
 	./ufont_test_asan
 
@@ -997,7 +997,7 @@ mkpack: mkpack.c
 # browser/gallery apps, packed into bin/<name>.  See mkpack.c, rootfs.h and
 # ramfs.h.
 FONT_FILES = fonts/sans.ttf fonts/sans-b.ttf fonts/sans-i.ttf \
-             fonts/sans-bi.ttf fonts/mono.ttf
+             fonts/sans-bi.ttf fonts/mono.ttf fonts/emoji.ttf
 
 PACK_FILES = $(foreach f,$(UCMDS),bin/$(patsubst ucmd_%,%,$(f))=$(f).elf) \
              bin/hello=hello.elf \

+ 16 - 5
docs/graphics.html

@@ -303,16 +303,27 @@
 		<p>
 			Proportional text comes from <span class="file">ufont.c</span>: a
 			fixed-point, integer-only TrueType parser and antialiased rasterizer.
-			The pack carries Latin-1 Arimo regular/bold/italic/bold-italic and
-			Liberation Mono subsets under <code>/ramfs/fonts</code>. The browser,
-			ngui apps and the WM chrome use real pixel sizes from 8 to 48 px;
-			the browser maps CSS <code>font-size</code>, <code>font-weight</code>,
+			The pack carries Latin-1 Arimo regular/bold/italic/bold-italic,
+			Liberation Mono and a curated monochrome Noto Emoji subset under
+			<code>/ramfs/fonts</code>. The browser, ngui apps and the WM chrome
+			use real pixel sizes from 8 to 48 px; the browser maps CSS
+			<code>font-size</code>, <code>font-weight</code>,
 			<code>font-style</code> and <code>font-family: monospace</code> to
 			the faces. Missing fonts fall back to the 8×16 bitmap. The browser
 			detaches the font arena before <code>heap_reset()</code> and
 			reattaches after <code>heap_start()</code>; terminal content and
 			the editor keep the bitmap cells.
 		</p>
+		<p>
+			UTF-8 text is available through <code>ngui_text_utf8*</code> and
+			<code>ngui_menu_item_utf8()</code>. Per codepoint, the selected
+			primary face is tried first, then the lazily loaded emoji face;
+			cmap format 12 covers codepoints above the BMP. The emoji set is
+			deliberately limited to common single-codepoint glyphs that fit the
+			rasterizer's 256-point/16-contour limits. ZWJ sequences, flags,
+			skin tones and color glyphs fall back to a tofu or
+			<code>?</code>; the browser keeps its Latin-1 byte path.
+		</p>
 	</section>
 
 	<section id="apps">
@@ -391,7 +402,7 @@
 			<li><code>browser.c</code>, <code>browser_html.c</code> — HTML/CSS renderer and inline SVG</li>
 			<li><code>ngui.c</code>, <code>ngui.h</code> — toolkit</li>
 			<li><code>ufont.c</code>, <code>ufont_ttf.c</code>, <code>ufont_draw.c</code> — fixed-point TrueType text</li>
-			<li><code>fonts/*.ttf</code> — Arimo / Liberation Mono subsets in the pack</li>
+			<li><code>fonts/*.ttf</code> — Arimo / Liberation Mono / Noto Emoji subsets in the pack</li>
 			<li><code>ucmd_files.c</code>, <code>gallery.c</code>, <code>ucmd_solitaire.c</code>, <code>ucmd_chat.c</code></li>
 			<li><code>img*.c</code> — image codecs</li>
 			<li><code>ati.c</code>, <code>nvidia.c</code>, <code>intel.c</code>, <code>nv50.c</code></li>

+ 12 - 0
docs/network.html

@@ -188,6 +188,17 @@
 			<code>VFS_PATH_MAX</code> (64) bytes. The helper removes the file
 			after reading it.
 		</p>
+		<p>
+			A successful <code>wifi connect</code> (from the CLI or the menu) also saves
+			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
+			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
+			attempt <code>kernel_main</code> spawns
+			<code>wifi autoconnect</code> when that file exists; it skips a
+			wired link that already has a DHCP lease and leaves an existing
+			WiFi link alone. <code>wifi forget</code> removes the saved record.
+			The file is a plaintext 8.3 FAT file with the BSSID zeroed so the
+			strongest AP advertising that SSID is used.
+		</p>
 		<div class="table-wrap">
 			<table>
 				<thead>
@@ -436,6 +447,7 @@
 					<tr><td>WebDAV</td><td>No authentication</td><td>Anyone on the LAN can read mounts and write <code>/share</code>.</td></tr>
 					<tr><td>Chat session</td><td>Cookie saved in <code>/share/chat.cfg</code></td><td>A bearer credential in RAM until sign-out; the password is sent over unverified TLS and never stored.</td></tr>
 					<tr><td>WiFi WPA2</td><td>PSK/CCMP, handshake in ring 3</td><td>The SNonce comes from the weak TLS DRBG. The PTK/replay are kept in the kernel (<code>SYS_WIFI_SET_PTK</code>/<code>SYS_WIFI_GET_PTK</code>) and fetched by the <code>wifi __keep</code> helper by connection ID for group rekeys; no WPA3/PMF.</td></tr>
+					<tr><td>Saved WiFi credentials</td><td>Plaintext <code>/data/WIFI.CFG</code></td><td>The SSID and passphrase are stored in an 8.3 FAT file so <code>wifi autoconnect</code> can reconnect at boot; anyone who can read the disk can read the passphrase. <code>wifi forget</code> removes it.</td></tr>
 					<tr><td>NTP</td><td>Unauthenticated SNTP</td><td>Anyone who can answer for the server name can set the clock.</td></tr>
 					<tr><td>NIC ownership</td><td>Raw lease for packet commands; shared TCP client table</td><td>Raw commands are mutually exclusive; up to twelve processes can hold one TCP connection each.  A killed owner is force-released.</td></tr>
 					<tr><td>Kernel/user isolation</td><td>None (identity map)</td><td>A ring-3 bug can reach any mapped kernel page.</td></tr>

+ 6 - 3
docs/userland.html

@@ -342,7 +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. Duplicate BSSIDs for one SSID are collapsed to the strongest entry (or the active one), and hidden networks are omitted.</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. On success the helper saves the SSID/passphrase to <code>/data/WIFI.CFG</code>; the next boot's <code>wifi autoconnect</code> helper reconnects from it. Duplicate BSSIDs for one SSID are collapsed to the strongest entry (or the active one), and hidden networks are omitted.</li>
 			<li><strong>wm</strong> / <strong>term</strong> — the ring-3 window manager and terminal launcher used by the desktop.</li>
 		</ul>
 		<p>
@@ -350,7 +350,10 @@
 			TrueType engine in <span class="file">ufont.c</span> with the Arimo
 			and Liberation Mono subsets in the pack. CSS and ngui can select
 			regular/bold/italic/mono faces at 8–48 px; missing faces fall back to
-			the 8×16 bitmap. Terminal contents and the editor stay bitmap;
+			the 8×16 bitmap. UTF-8 text APIs additionally fall back to the
+			monochrome Noto Emoji subset for common emoji (format-12 cmap);
+			the browser still uses the Latin-1 byte path. Terminal contents and
+			the editor stay bitmap;
 			ngui text fields use the proportional face for text, caret,
 			selection and horizontal scrolling. The browser detaches the font
 			arena before <code>heap_reset()</code> and reattaches after the page
@@ -417,7 +420,7 @@
 			<li><code>Makefile</code> — <code>UCMDS</code> and pack rules</li>
 			<li><code>ucmd_*.c</code> — commands</li>
 			<li><code>ufont.c</code>, <code>ufont_ttf.c</code> — fixed-point TrueType text</li>
-			<li><code>ngui.c</code> — GUI toolkit; <code>fonts/*.ttf</code> — UI subsets</li>
+			<li><code>ngui.c</code> — GUI toolkit; <code>fonts/*.ttf</code> — UI and emoji subsets</li>
 			<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>

+ 2 - 0
fonts/LICENSE-OFL

@@ -2,6 +2,8 @@ Digitized data copyright (c) 2010 Google Corporation
 	with Reserved Font Arimo, Tinos and Cousine.
 Copyright (c) 2012 Red Hat, Inc.
 	with Reserved Font Name Liberation.
+Noto Emoji Copyright (c) 2013 Google Inc., licensed under the SIL Open
+Font License, Version 1.1.
 
 This Font Software is licensed under the SIL Open Font License,
 Version 1.1.

+ 18 - 2
fonts/README

@@ -14,13 +14,23 @@ Files
   sans-i.ttf    Arimo Italic
   sans-bi.ttf   Arimo BoldItalic
   mono.ttf      Liberation Mono Regular (browser <pre>/<code>)
+  emoji.ttf     Noto Emoji monochrome subset (common emoji, cmap 12)
+  emoji-unicodes.txt
+                the curated codepoints used to generate emoji.ttf
 
 The Arimo faces are Copyright 2020 The Arimo Project Authors and are
 distributed under the Apache License 2.0 (see LICENSE-Apache-2.0).  Liberation
 Mono is copyright (c) 2012 Red Hat, Inc. with Reserved Font Name Liberation
 and is distributed under the SIL Open Font License 1.1 (see LICENSE-OFL).
-The subsetting removes unused glyphs and OpenType layout features and keeps
-the files small enough for the rootfs pack.
+Noto Emoji is Copyright 2013 Google Inc. and is distributed under the SIL
+Open Font License 1.1; emoji.ttf contains a curated subset of the monochrome
+Noto Emoji face.  The subsets remove unused glyphs and OpenType layout
+features and keep the files small enough for the rootfs pack.
+
+The emoji subset is limited to glyphs that fit the fixed-point rasterizer
+(256 points / 16 contours per glyph) and to common single-codepoint emoji;
+ZWJ sequences, flags and color glyphs are not supported.  The list is in
+fonts/emoji-unicodes.txt.
 
 Regenerating
 ------------
@@ -35,6 +45,12 @@ or manually:
     --unicodes='U+0020-00FF,U+20AC,U+2013-2014,U+2018-2019,U+201C-201D,U+2022,U+2026,U+00A0' \
     --layout-features='' --output-file=fonts/sans.ttf
 
+The emoji subset is regenerated from a monochrome Noto Emoji TTF (for
+example the Google Fonts `Noto Emoji` regular face) with:
+
+  pyftsubset NotoEmoji.ttf --unicodes-file=fonts/emoji-unicodes.txt \
+    --layout-features='' --output-file=fonts/emoji.ttf
+
 The checked-in files are the canonical build inputs; a normal `make` does not
 need fonttools or the original system fonts.  Do not edit the generated TTFs
 by hand.

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
fonts/emoji-unicodes.txt



+ 32 - 0
kernel.c

@@ -35,6 +35,7 @@
 #include "version.h"
 #include "vga.h"
 #include "vfs.h"
+#include "wifi_cfg.h"
 
 static inline void outb(unsigned short port, unsigned char v)
 {
@@ -127,12 +128,20 @@ void exc_handler(struct exc_state *st)
 	/* A fault from ring 3 is a bug in a user command (or its ELF): kill that
 	 * process and keep the kernel running.  Kernel faults still halt. */
 	if ((st->cs & 3) == 3) {
+		struct process *p = current_proc();
+
 		serial_puts(SERIAL_COM1, "\n*** user fault vec ");
 		serial_puthex(SERIAL_COM1, vector);
 		serial_puts(SERIAL_COM1, " eip=");
 		serial_puthex(SERIAL_COM1, st->eip);
 		serial_puts(SERIAL_COM1, " cr2=");
 		serial_puthex(SERIAL_COM1, cr2);
+		if (p) {
+			serial_puts(SERIAL_COM1, " pid=");
+			serial_putdec(SERIAL_COM1, p->pid);
+			serial_puts(SERIAL_COM1, " ");
+			serial_puts(SERIAL_COM1, p->name);
+		}
 		serial_puts(SERIAL_COM1, " -- process killed\n");
 		io_putstr("\n*** fault: ");
 		io_putstr(vector < 20 ? names[vector] : "unknown vector");
@@ -550,6 +559,29 @@ void kernel_main(void)
 		dhcp_run(4000);
 	boot_trace(net_kept ? "dhcp skipped" : "dhcp");
 
+	/* If the writable /data volume has saved WiFi credentials, let the
+	 * normal supplicant reconnect in the background.  It skips a working
+	 * wired link, and a missing /data or wifi helper is harmless. */
+	{
+		unsigned int sz = 0;
+		int is_dir = 0;
+
+		if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
+		    !is_dir && sz > 0) {
+			struct vfs_file f;
+			struct vfs_mount *m = 0;
+
+			if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
+				if (process_create_user("wifi", f.data, f.size,
+							"autoconnect", "/") > 0)
+					serial_puts(SERIAL_COM1,
+						    "wifi: autoconnect started\n");
+				vfs_release_file(m, &f);
+			}
+		}
+	}
+	boot_trace("wifi auto");
+
 	/* Dump the active NIC's counters after the boot DHCP attempt.  With
 	 * the ASIX as the management link this appears in netmon even when
 	 * the active e1000e cannot pass traffic. */

+ 211 - 7
ngui.c

@@ -130,9 +130,25 @@ void ngui_font_detach(struct ngui *ui)
 	ui->font_italic = 0;
 	ui->font_bold_italic = 0;
 	ui->font_mono = 0;
+	ui->font_emoji = 0;
+	ui->emoji_loaded = 0;
 	ufont_reset();
 }
 
+/* Emoji fallback is opened on first use so apps that never draw UTF-8 text
+ * do not pay for it.  ufont_face_open_path() may clear the glyph cache, which
+ * is safe here: callers never keep a glyph pointer across this call. */
+static struct ufont_face *ngui_emoji_face(struct ngui *ui)
+{
+	if (!ui)
+		return 0;
+	if (!ui->emoji_loaded) {
+		ui->emoji_loaded = 1;
+		ui->font_emoji = ufont_face_open_path("/ramfs/fonts/emoji.ttf");
+	}
+	return ui->font_emoji;
+}
+
 void ngui_invalidate(struct ngui *ui)
 {
 	ui->dirty = 1;
@@ -845,6 +861,172 @@ void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
 	ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
 }
 
+/* ---- UTF-8 text with an emoji fallback face --------------------------- */
+
+static int utf8_skip_cp(unsigned int cp)
+{
+	return cp == 0x200B || cp == 0x200C || cp == 0x200D ||
+	       cp == 0xFE0E || cp == 0xFE0F;
+}
+
+/* Advance of one codepoint in 26.6 pixels, choosing the primary face, then
+ * the lazily loaded emoji face, then '?'. */
+static int ngui_utf8_advance(struct ngui *ui, struct ufont_face *f,
+			     int px, unsigned int cp)
+{
+	struct ufont_face *emoji;
+
+	if (utf8_skip_cp(cp) || !f)
+		return 0;
+	if (ufont_has_glyph(f, cp))
+		return ufont_advance_cp(f, px, cp);
+	emoji = ngui_emoji_face(ui);
+	if (emoji && ufont_has_glyph(emoji, cp))
+		return ufont_advance_cp(emoji, px, cp);
+	return ufont_advance_cp(f, px, '?');
+}
+
+static unsigned int ngui_text_width_face_utf8(struct ngui *ui,
+					      struct ufont_face *face,
+					      const char *s, unsigned int n,
+					      int px, int bold, int italic,
+					      int mono)
+{
+	struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
+	int i = 0, total = 0;
+
+	if (!f)
+		return n * 8u * (px >= 24 ? 2u : 1u);
+	while (i < (int)n) {
+		unsigned int cp = ufont_utf8_next(s, &i, (int)n);
+
+		total += ngui_utf8_advance(ui, f, px, cp);
+	}
+	return (unsigned int)((total + 32) >> 6);
+}
+
+static const struct ufont_glyph *ngui_utf8_glyph(struct ngui *ui,
+						 struct ufont_face *f,
+						 int px, unsigned int cp,
+						 struct ufont_face **used)
+{
+	struct ufont_face *emoji;
+	const struct ufont_glyph *g;
+
+	*used = f;
+	if (utf8_skip_cp(cp) || !f)
+		return 0;
+	if (ufont_has_glyph(f, cp)) {
+		g = ufont_glyph(f, px, cp);
+		if (g)
+			return g;
+	}
+	emoji = ngui_emoji_face(ui);
+	if (emoji && ufont_has_glyph(emoji, cp)) {
+		g = ufont_glyph(emoji, px, cp);
+		if (g) {
+			*used = emoji;
+			return g;
+		}
+	}
+	*used = f;
+	return ufont_glyph(f, px, '?');
+}
+
+static void ngui_text_px_utf8_common(struct ngui *ui, struct ufont_face *face,
+				     int x, int y, const char *s,
+				     unsigned int n, unsigned short color,
+				     int px, int bold, int italic, int mono)
+{
+	struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
+	int i = 0, pen = 0, ascent;
+	int cx, cy, cw, ch;
+
+	if (!ui || !ui->px || !s)
+		return;
+	if (!f) {
+		ngui_text_px_common(ui, face, x, y, s, n, color, px, bold,
+				    italic, mono);
+		return;
+	}
+	ascent = ngui_text_ascent_face(ui, face, px, mono);
+	ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
+	while (i < (int)n) {
+		unsigned int cp = ufont_utf8_next(s, &i, (int)n);
+		struct ufont_face *used;
+		const struct ufont_glyph *g;
+
+		if (utf8_skip_cp(cp))
+			continue;
+		g = ngui_utf8_glyph(ui, f, px, cp, &used);
+		if (!g) {
+			pen += 8 << 6;
+			continue;
+		}
+		ufont_blit_rgb565(ui->px, ui->w, ui->h,
+				  x + (pen >> 6), y + ascent, g, color,
+				  cx, cy, cw, ch);
+		pen += g->advance;
+	}
+}
+
+void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
+		      unsigned int n, unsigned short color)
+{
+	ngui_text_px_utf8_common(ui, 0, x, y, s, n, color,
+				 ui ? ui->font_px : 14, 0, 0, 0);
+}
+
+void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
+		    unsigned short color)
+{
+	ngui_text_n_utf8(ui, x, y, s, s ? slen(s) : 0, color);
+}
+
+unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s)
+{
+	return ngui_text_width_face_utf8(ui, 0, s, s ? slen(s) : 0,
+					 ui ? ui->font_px : 14, 0, 0, 0);
+}
+
+void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
+			     int x, int y, const char *s,
+			     unsigned short color)
+{
+	unsigned int n = s ? slen(s) : 0;
+	unsigned int end = 0, i = 0;
+	int avail = r.x + r.w - x;
+	int dots, width;
+
+	if (!ui || !s)
+		return;
+	if (avail <= 0)
+		return;
+	width = (int)ngui_text_width_utf8(ui, s);
+	if (width <= avail) {
+		ngui_text_utf8(ui, x, y, s, color);
+		return;
+	}
+	dots = (int)ngui_text_width_utf8(ui, "...");
+	if (dots >= avail)
+		return;
+	while (i < n) {
+		int next = i;
+
+		(void)ufont_utf8_next(s, &next, (int)n);
+		if ((int)ngui_text_width_face_utf8(ui, 0, s, (unsigned int)next,
+						   ui->font_px, 0, 0, 0) +
+		    dots > avail)
+			break;
+		i = (unsigned int)next;
+		end = i;
+	}
+	ngui_text_n_utf8(ui, x, y, s, end, color);
+	ngui_text_utf8(ui, x + (int)ngui_text_width_face_utf8(
+			       ui, 0, s, end, ui->font_px, 0, 0, 0),
+		       y, "...", color);
+}
+
 void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
 		       int y, const char *s, unsigned int n,
 		       unsigned short color, int px, int bold, int italic,
@@ -1078,8 +1260,9 @@ int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 /* One row of a drop-down menu: left-aligned label, optional check mark and
  * right-aligned shortcut.  Uses the same press/release/active semantics as
  * ngui_button so a menu and its caller never disagree about a click. */
-int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
-		   const char *label, const char *shortcut, int checked)
+static int ngui_menu_item_common(struct ngui *ui, unsigned int id,
+				 struct ngui_rect r, const char *label,
+				 const char *shortcut, int checked, int utf8)
 {
 	int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
 	int press_hot = ui->pressed &&
@@ -1113,12 +1296,21 @@ int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
 		ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
 	}
-	if (label)
-		ngui_text(ui, r.x + 22, ty, label, fg);
+	if (label) {
+		if (utf8)
+			ngui_text_utf8(ui, r.x + 22, ty, label, fg);
+		else
+			ngui_text(ui, r.x + 22, ty, label, fg);
+	}
 	if (shortcut) {
-		int sw = (int)ngui_text_width_ui(ui, shortcut);
-
-		ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
+		int sw = (int)(utf8 ? ngui_text_width_utf8(ui, shortcut) :
+			       ngui_text_width_ui(ui, shortcut));
+
+		if (utf8)
+			ngui_text_utf8(ui, r.x + r.w - 8 - sw, ty, shortcut,
+				       fg);
+		else
+			ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
 	}
 	ngui_pop_clip(ui);
 	if (hot)
@@ -1126,6 +1318,18 @@ int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	return fired;
 }
 
+int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		   const char *label, const char *shortcut, int checked)
+{
+	return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 0);
+}
+
+int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
+			const char *label, const char *shortcut, int checked)
+{
+	return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 1);
+}
+
 int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		     enum ngui_icon icon){
 	int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);

+ 13 - 0
ngui.h

@@ -111,6 +111,8 @@ struct ngui {
 	struct ufont_face *font_italic;
 	struct ufont_face *font_bold_italic;
 	struct ufont_face *font_mono;
+	struct ufont_face *font_emoji;      /* lazy fallback face */
+	int emoji_loaded;
 	int font_px;
 
 	/* Current pointer state, content-relative. */
@@ -192,6 +194,15 @@ void ngui_text(struct ngui *ui, int x, int y, const char *s,
 	       unsigned short color);
 void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
 		 unsigned int n, unsigned short color);
+/* UTF-8 variants with the primary face -> emoji fallback stack. */
+void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
+		    unsigned short color);
+void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
+		      unsigned int n, unsigned short color);
+unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s);
+void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
+			     int x, int y, const char *s,
+			     unsigned short color);
 void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
 		      unsigned int n, unsigned short color, int scale, int bold);
 /* Browser-oriented style: arbitrary pixel size, optional italic and mono. */
@@ -231,6 +242,8 @@ int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		const char *label);
 int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		   const char *label, const char *shortcut, int checked);
+int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
+			const char *label, const char *shortcut, int checked);
 int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		     enum ngui_icon icon);
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,

+ 11 - 0
tools/subset_fonts.sh

@@ -17,3 +17,14 @@ SUB=${PYFTSUBSET:-pyftsubset}
 	--layout-features='' --output-file=fonts/sans-bi.ttf
 "$SUB" /usr/share/fonts/liberation/LiberationMono-Regular.ttf --unicodes="$U" \
 	--layout-features='' --output-file=fonts/mono.ttf
+
+# Monochrome Noto Emoji subset for ngui's UTF-8 fallback face.  The checked-in
+# emoji-unicodes.txt is curated to glyphs that fit ufont's 256-point / 16-
+# contour rasterizer.  Point NOTO_EMOJI_TTF at a monochrome Noto Emoji TTF.
+EMOJI=${NOTO_EMOJI_TTF:-}
+if [ -n "$EMOJI" ] && [ -f "$EMOJI" ]; then
+	"$SUB" "$EMOJI" --unicodes-file=fonts/emoji-unicodes.txt \
+		--layout-features='' --output-file=fonts/emoji.ttf
+else
+	echo "note: set NOTO_EMOJI_TTF to regenerate fonts/emoji.ttf"
+fi

+ 15 - 15
ucmd_netmenu.c

@@ -141,9 +141,10 @@ static void ssid_name(const struct sys_wifi_bss *b, char out[33])
 
 	if (n > 32)
 		n = 32;
+	/* Keep the raw UTF-8 bytes; ngui's UTF-8 renderer handles them and the
+	 * connection path needs the exact SSID. */
 	for (i = 0; i < n; i++)
-		out[i] = (b->ssid[i] >= 32 && b->ssid[i] < 127) ?
-			 (char)b->ssid[i] : '?';
+		out[i] = (char)b->ssid[i];
 	out[n] = '\0';
 	if (!n) {
 		out[0] = '(';
@@ -385,7 +386,7 @@ static int write_wifi_cfg(const struct sys_wifi_bss *b, const char *pass)
 		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",
+	r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CFG_STAGED,
 		       (int)&cfg, (int)sizeof(cfg));
 	for (i = 0; i < sizeof(cfg); i++)
 		((unsigned char *)&cfg)[i] = 0;
@@ -633,7 +634,7 @@ 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,
+		ngui_text_utf8(&ui, 10, ui.h - 22, status,
 			  status_error ? ui.theme.error : ui.theme.accent);
 }
 
@@ -682,7 +683,7 @@ static void draw_wifi_list(struct ngui_rect area)
 		ssid_name(b, ssid);
 		sput(label, ssid);
 		sput(shortcut, security_name(b->security));
-		clicked = ngui_menu_item(&ui, ID_BSS_BASE + i, rr, label,
+		clicked = ngui_menu_item_utf8(&ui, ID_BSS_BASE + i, rr, label,
 					 shortcut, bss_is_active(b));
 
 		sw = (int)ngui_text_width_ui(&ui, shortcut);
@@ -721,15 +722,14 @@ static void draw_main(void)
 		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++ = (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);
+	ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text);
 
 	r = ngui_cut_top(&body, 20);
 	p = line;
@@ -748,7 +748,7 @@ static void draw_main(void)
 		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);
+	ngui_text_ellipsis_utf8(&ui, r, r.x, r.y, line, ui.theme.text_dim);
 	body.y += 4;
 	body.h -= 4;
 
@@ -824,7 +824,7 @@ static void draw_pass(void)
 	p = sput(p, sel_ssid);
 	p = sput(p, "\"");
 	*p = '\0';
-	ngui_text_ellipsis(&ui, r, r.x, r.y, line, ui.theme.text);
+	ngui_text_ellipsis_utf8(&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);
@@ -876,11 +876,11 @@ static void draw_connect(void)
 		p = sput(p, "\"");
 	}
 	*p = '\0';
-	ngui_text_ellipsis(&ui, r, r.x, r.y, line, ui.theme.text);
+	ngui_text_ellipsis_utf8(&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,
+		ngui_text_utf8(&ui, r.x, r.y, status,
 			  status_error ? ui.theme.error : ui.theme.accent);
 	body.y += 4;
 	body.h -= 4;
@@ -901,17 +901,17 @@ static void draw_connect(void)
 			continue;
 		if (c == '\n') {
 			line[line_y] = '\0';
-			ngui_text(&ui, body.x, y, line, ui.theme.text_dim);
+			ngui_text_utf8(&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 : '.';
+			line[line_y++] = c;
 	}
 	if (line_y) {
 		line[line_y] = '\0';
-		ngui_text(&ui, body.x, y, line, ui.theme.text_dim);
+		ngui_text_utf8(&ui, body.x, y, line, ui.theme.text_dim);
 	}
 
 	/* Cancel at the bottom. */

+ 180 - 3
ucmd_wifi.c

@@ -10,6 +10,8 @@
  *   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 autoconnect            reconnect from /data/WIFI.CFG (boot helper)
+ *   wifi forget                 remove the saved credentials
  *   wifi disconnect
  *
  * The WPA2 handshake (wpa.c) runs here; a small background copy of this
@@ -30,6 +32,97 @@ static int wifi(int op, int a2, int a3)
 	return u_syscall3(SYS_WIFI, op, a2, a3);
 }
 
+static int name_eq(const char *a, const char *b)
+{
+	while (*a && *b) {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
+static int bssid_zero(const unsigned char b[6])
+{
+	unsigned int i;
+
+	for (i = 0; i < 6; i++)
+		if (b[i])
+			return 0;
+	return 1;
+}
+
+static void wipe_cfg(struct wifi_cfg *cfg)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(*cfg); i++)
+		((volatile unsigned char *)cfg)[i] = 0;
+}
+
+/* Save the network for the next boot.  The BSSID is deliberately left zero
+ * so a mesh/repeater AP with the same SSID still works; /data is a plain FAT
+ * partition, so WIFI.CFG is 8.3 and the password is stored in clear. */
+static int save_wifi_cred(const unsigned char *ssid, unsigned int ssid_len,
+			  const char *pass, unsigned int pass_len)
+{
+	/* FAT writes may read a final partial sector past the source length;
+	 * keep slack after the record as well. */
+	unsigned char buf[sizeof(struct wifi_cfg) + 512];
+	struct wifi_cfg *cfg = (struct wifi_cfg *)buf;
+	unsigned int i;
+	int r;
+
+	if (ssid_len > 32)
+		ssid_len = 32;
+	if (pass_len > 64)
+		pass_len = 64;
+	for (i = 0; i < sizeof(buf); i++)
+		buf[i] = 0;
+	cfg->magic = WIFI_CFG_MAGIC;
+	cfg->ssid_len = (unsigned char)ssid_len;
+	cfg->pass_len = (unsigned char)pass_len;
+	for (i = 0; i < ssid_len; i++)
+		cfg->ssid[i] = ssid[i];
+	for (i = 0; i < pass_len; i++)
+		cfg->pass[i] = pass[i];
+	r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CRED_PATH,
+		       (int)cfg, (int)sizeof(*cfg));
+	for (i = 0; i < sizeof(buf); i++)
+		buf[i] = 0;
+	return r;
+}
+
+static int load_wifi_cred(struct wifi_cfg *cfg)
+{
+	/* FAT reads can write a final partial sector past the requested
+	 * length; keep 512 bytes of slack so the stack top is never touched. */
+	unsigned char buf[sizeof(struct wifi_cfg) + 512];
+	unsigned int i;
+	int r;
+
+	wipe_cfg(cfg);
+	r = u_syscall3(SYS_READ_FILE, (int)WIFI_CRED_PATH,
+		       (int)buf, (int)sizeof(*cfg));
+	if (r < (int)sizeof(*cfg) || ((struct wifi_cfg *)buf)->magic !=
+	    WIFI_CFG_MAGIC) {
+		for (i = 0; i < sizeof(buf); i++)
+			buf[i] = 0;
+		return -1;
+	}
+	for (i = 0; i < sizeof(*cfg); i++)
+		((unsigned char *)cfg)[i] = buf[i];
+	for (i = 0; i < sizeof(buf); i++)
+		buf[i] = 0;
+	if (cfg->ssid_len == 0 || cfg->ssid_len > 32 ||
+	    cfg->pass_len > 64) {
+		wipe_cfg(cfg);
+		return -1;
+	}
+	return 0;
+}
+
 static void put_ssid(const unsigned char *s, unsigned int n)
 {
 	unsigned int i;
@@ -453,7 +546,7 @@ static int connect_bss(const char *ssid, const char *pass,
 		spawn_keeper(&sm, st.conn_id);
 	if (r == 0 && u_syscall1(SYS_DHCP_RUN, 6000) != 0)
 		u_puts("wifi: DHCP failed; set an address with netip\n");
-	return 0;
+	return r == 0 ? 0 : 1;
 }
 
 static int cmd_connect(const char *args)
@@ -461,6 +554,7 @@ static int cmd_connect(const char *args)
 	char ssid[33], pass[80], bssid_s[13];
 	unsigned char want_bssid[6];
 	int have_bssid = 0;
+	int r;
 
 	args = word(args, ssid, sizeof(ssid));
 	args = word(args, pass, sizeof(pass));
@@ -473,7 +567,16 @@ static int cmd_connect(const char *args)
 		}
 		have_bssid = 1;
 	}
-	return connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
+	r = connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
+	if (r == 0) {
+		if (save_wifi_cred((const unsigned char *)ssid,
+				   (unsigned int)slen(ssid), pass,
+				   (unsigned int)slen(pass)) == 0)
+			u_puts("wifi: saved for auto-connect\n");
+		else
+			u_puts("wifi: note: could not save credentials to /data\n");
+	}
+	return r;
 }
 
 /* `wifi connectfile <path>`: the network menu stages an SSID + passphrase +
@@ -507,6 +610,67 @@ static int cmd_connect_file(const char *args)
 		pass[i] = cfg.pass[i];
 	pass[cfg.pass_len] = '\0';
 	r = connect_bss(ssid, pass, cfg.bssid);
+	if (r == 0) {
+		if (save_wifi_cred(cfg.ssid, cfg.ssid_len, cfg.pass,
+				   cfg.pass_len) == 0)
+			u_puts("wifi: saved for auto-connect\n");
+		else
+			u_puts("wifi: note: credentials not saved (no writable /data)\n");
+	}
+	for (i = 0; i < sizeof(cfg); i++)
+		((unsigned char *)&cfg)[i] = 0;
+	for (i = 0; i < sizeof(pass); i++)
+		((volatile char *)pass)[i] = 0;
+	return r;
+}
+
+/* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG.  A
+ * wired link that already has an address wins; an existing WiFi link is left
+ * alone. */
+static int cmd_autoconnect(void)
+{
+	struct wifi_cfg cfg;
+	struct sys_wifi_status ws;
+	struct sys_netinfo ni;
+	char ssid[33], pass[65];
+	unsigned int i;
+	int attempt, r = 1;
+
+	if (load_wifi_cred(&cfg) != 0)
+		return 0;
+	if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present) {
+		wipe_cfg(&cfg);
+		return 0;
+	}
+	if (ws.state >= SYS_WIFI_ST_LINK) {
+		wipe_cfg(&cfg);
+		return 0;
+	}
+	if (u_net_info(&ni) == 1 && ni.present && !name_eq(ni.name, "wifi")) {
+		unsigned char srv[4];
+		unsigned int lease = 0;
+
+		/* Only a real wired DHCP lease keeps the boot-time wired link;
+		 * the static fallback address does not mean the cable is up. */
+		if (u_syscall2(SYS_DHCP_INFO, (int)srv, (int)&lease) == 1) {
+			wipe_cfg(&cfg);
+			return 0;
+		}
+	}
+	for (i = 0; i < cfg.ssid_len; i++)
+		ssid[i] = (char)cfg.ssid[i];
+	ssid[cfg.ssid_len] = '\0';
+	for (i = 0; i < cfg.pass_len; i++)
+		pass[i] = cfg.pass[i];
+	pass[cfg.pass_len] = '\0';
+	for (attempt = 0; attempt < 2; attempt++) {
+		r = connect_bss(ssid, pass,
+				bssid_zero(cfg.bssid) ? 0 : cfg.bssid);
+		if (r == 0)
+			break;
+		if (attempt == 0)
+			u_sleep_ms(3000);
+	}
 	for (i = 0; i < sizeof(cfg); i++)
 		((unsigned char *)&cfg)[i] = 0;
 	for (i = 0; i < sizeof(pass); i++)
@@ -514,6 +678,15 @@ static int cmd_connect_file(const char *args)
 	return r;
 }
 
+static int cmd_forget(void)
+{
+	if (u_syscall1(SYS_UNLINK, (int)WIFI_CRED_PATH) == 0)
+		u_puts("wifi: saved credentials removed\n");
+	else
+		u_puts("wifi: no saved credentials\n");
+	return 0;
+}
+
 /* Background: answer group-key rekeys until the connection changes. */
 static int keeper(const char *args)
 {
@@ -593,11 +766,15 @@ void _start(const char *arg)
 		r = cmd_connect(a + 8);
 	else if (starts(a, "connectfile "))
 		r = cmd_connect_file(a + 12);
+	else if (starts(a, "autoconnect"))
+		r = cmd_autoconnect();
+	else if (starts(a, "forget"))
+		r = cmd_forget();
 	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]|connectfile <path>|disconnect]\n");
+		u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|autoconnect|forget|disconnect]\n");
 		r = 1;
 	}
 	u_exit(r);

+ 19 - 0
ufont.c

@@ -210,6 +210,25 @@ int ufont_text_width(const struct ufont_face *f, int px,
 	return (total + 32) >> 6;
 }
 
+int ufont_has_glyph(const struct ufont_face *f, unsigned int cp)
+{
+	return f && ufont_ttf_glyph_index(f, cp) != 0;
+}
+
+int ufont_advance_cp(const struct ufont_face *f, int px, unsigned int cp)
+{
+	if (!f)
+		return 0;
+	return ufont_ttf_advance(f, ufont_ttf_glyph_index(f, cp), px);
+}
+
+unsigned int ufont_utf8_next(const char *s, int *i, int n)
+{
+	if (!s || !i)
+		return 0xFFFD;
+	return next_cp(s, i, n, 1);
+}
+
 static struct ufont_cache_ent *cache_lookup(const struct ufont_face *f,
 					    int px, unsigned int cp)
 {

+ 10 - 0
ufont.h

@@ -51,6 +51,16 @@ int ufont_text_width(const struct ufont_face *face, int px,
 const struct ufont_glyph *ufont_glyph(const struct ufont_face *face, int px,
 				      unsigned int cp);
 
+/* Whether the face has a real glyph for the codepoint, and its advance.
+ * Used by the ngui fallback stack before opening/rasterizing a mask. */
+int ufont_has_glyph(const struct ufont_face *face, unsigned int cp);
+int ufont_advance_cp(const struct ufont_face *face, int px,
+		     unsigned int cp);
+
+/* Decode one UTF-8 codepoint.  Advances *i; returns U+FFFD for invalid
+ * sequences.  n < 0 means NUL-terminated. */
+unsigned int ufont_utf8_next(const char *s, int *i, int n);
+
 /* Alpha blend one glyph.  The mask is positioned at
  * (x + g->xoff, baseline + g->yoff).  Returns the number of touched pixels. */
 int ufont_blit_rgb565(unsigned short *px, int w, int h,

+ 3 - 0
ufont_internal.h

@@ -27,6 +27,9 @@ struct ufont_face {
 	const unsigned char *hmtx;
 	const unsigned char *cmap4;
 	unsigned int cmap4_len;
+	const unsigned char *cmap12;
+	unsigned int cmap12_len;
+	unsigned int cmap12_groups;
 };
 
 /* Parse the sfnt directory and fill the face; 0 on success. */

+ 50 - 0
ufont_test.c

@@ -51,11 +51,31 @@ static int draw_text(unsigned short *fb, int w, int h, int x, int baseline,
 	return (pen + 32) >> 6;
 }
 
+static int draw_utf8(unsigned short *fb, int w, int h, int x, int baseline,
+		     const struct ufont_face *face, int px, const char *s)
+{
+	int i = 0, pen = 0, n = (int)strlen(s);
+	unsigned short color = 0xFFFF;
+
+	while (i < n) {
+		unsigned int cp = ufont_utf8_next(s, &i, n);
+		const struct ufont_glyph *g = ufont_glyph(face, px, cp);
+
+		if (g) {
+			ufont_blit_rgb565(fb, w, h, x + (pen >> 6), baseline,
+					  g, color, 0, 0, w, h);
+			pen += g->advance;
+		}
+	}
+	return (pen + 32) >> 6;
+}
+
 int main(void)
 {
 	unsigned char *data;
 	unsigned int len = 0;
 	struct ufont_face *face;
+	struct ufont_face *eface;
 	struct ufont_metrics m;
 	int sizes[] = { 12, 16, 24, 32 };
 	unsigned int si;
@@ -79,6 +99,33 @@ int main(void)
 		fprintf(stderr, "cannot parse font\n");
 		return 1;
 	}
+	free(data);
+	data = 0;
+	{
+		unsigned char *edata = read_file("fonts/emoji.ttf", &len);
+		struct ufont_face *ef;
+
+		if (!edata) {
+			fprintf(stderr, "cannot read fonts/emoji.ttf\n");
+			return 1;
+		}
+		eface = ufont_face_open_mem(edata, len);
+		free(edata);
+		if (!eface) {
+			fprintf(stderr, "cannot parse emoji font\n");
+			return 1;
+		}
+		if (!ufont_has_glyph(eface, 0x1F349)) {
+			fprintf(stderr, "emoji font lacks U+1F349\n");
+			return 1;
+		}
+		ef = eface;
+		printf("emoji U+1F349 width 16=%d\n",
+		       ufont_text_width(ef, 16, "\xF0\x9F\x8D\x89", -1, 1));
+		printf("emoji cmap12 U+1F349 advance 16=%d, U+1F600 advance=%d\n",
+		       ufont_advance_cp(ef, 16, 0x1F349),
+		       ufont_advance_cp(ef, 16, 0x1F600));
+	}
 	ufont_metrics(face, 16, &m);
 	printf("16px: ascent=%d descent=%d gap=%d line=%d\n",
 	       m.ascent, m.descent, m.line_gap, m.line_height);
@@ -101,6 +148,9 @@ int main(void)
 			  "The quick brown fox jumps over 1234567890");
 		draw_text(fb, W, H, 8, y + 34, face, px,
 			  "\xC4rger \xE9 \xFC \xDF \xA9 \xAE \xB0 \xB1");
+		if (si == 1)
+			draw_utf8(fb, W, H, 8, y + 68, eface, px,
+				  "\xF0\x9F\x8D\x89 \xE2\x9A\xA1 \xE2\x9D\xA4");
 	}
 	out = fopen("/tmp/opencode/ufont-test.ppm", "wb");
 	if (!out)

+ 80 - 16
ufont_ttf.c

@@ -96,7 +96,7 @@ int ufont_ttf_load(struct ufont_face *f, const unsigned char *data,
 	const unsigned char *head, *maxp, *hhea, *hmtx, *loca, *glyf, *cmap;
 	unsigned int head_len, maxp_len, hhea_len, hmtx_len, loca_len;
 	unsigned int glyf_len, cmap_len, i, nrec;
-	unsigned int cmap4_off = 0;
+	unsigned int cmap4_off = 0, cmap12_off = 0;
 
 	if (len < 12)
 		return -1;
@@ -139,7 +139,8 @@ int ufont_ttf_load(struct ufont_face *f, const unsigned char *data,
 	if (hmtx_len < (unsigned int)f->num_hmetrics * 4u)
 		return -1;
 
-	/* Pick a format 4 Unicode cmap. */
+	/* Pick a format 4 Unicode cmap and/or a format 12 cmap for emoji and
+	 * other supplementary-plane codepoints. */
 	if (cmap_len < 4)
 		return -1;
 	nrec = be16(cmap + 2);
@@ -150,38 +151,59 @@ int ufont_ttf_load(struct ufont_face *f, const unsigned char *data,
 		unsigned int off = be32(r + 4);
 		unsigned int plat = be16(r);
 		unsigned int enc = be16(r + 2);
+		unsigned int fmt;
 
 		if (off + 2 > cmap_len)
 			continue;
-		if (be16(cmap + off) != 4)
-			continue;
-		if (plat == 3 && (enc == 0 || enc == 1)) {
-			cmap4_off = off;
-			break;
+		fmt = be16(cmap + off);
+		if (fmt == 4) {
+			if (!cmap4_off ||
+			    (plat == 3 && (enc == 0 || enc == 1)))
+				cmap4_off = off;
+		} else if (fmt == 12) {
+			if (!cmap12_off ||
+			    (plat == 3 && enc == 10) ||
+			    (plat == 0 && enc == 4))
+				cmap12_off = off;
 		}
-		if (plat == 0 && enc == 3 && !cmap4_off)
-			cmap4_off = off;
 	}
-	if (!cmap4_off || cmap4_off + 16 > cmap_len)
-		return -1;
-	if (be16(cmap + cmap4_off + 2) > cmap_len - cmap4_off)
+	if (!cmap4_off && !cmap12_off)
 		return -1;
+	if (cmap4_off) {
+		if (cmap4_off + 16 > cmap_len ||
+		    be16(cmap + cmap4_off + 2) > cmap_len - cmap4_off)
+			return -1;
+	}
+	if (cmap12_off) {
+		unsigned int groups;
+
+		if (cmap12_off + 16 > cmap_len ||
+		    be32(cmap + cmap12_off + 4) > cmap_len - cmap12_off)
+			return -1;
+		groups = be32(cmap + cmap12_off + 12);
+		if (groups > 65535u ||
+		    16u + groups * 12u > cmap_len - cmap12_off)
+			return -1;
+		f->cmap12 = cmap + cmap12_off;
+		f->cmap12_len = be32(cmap + cmap12_off + 4);
+		f->cmap12_groups = groups;
+	}
 
 	f->loca = loca;
 	f->glyf = glyf;
 	f->glyf_len = glyf_len;
 	f->hmtx = hmtx;
-	f->cmap4 = cmap + cmap4_off;
-	f->cmap4_len = be16(cmap + cmap4_off + 2);
+	f->cmap4 = cmap4_off ? cmap + cmap4_off : 0;
+	f->cmap4_len = cmap4_off ? be16(cmap + cmap4_off + 2) : 0;
 	return 0;
 }
 
-int ufont_ttf_glyph_index(const struct ufont_face *f, unsigned int cp)
+static int cmap4_lookup(const struct ufont_face *f, unsigned int cp)
 {
 	const unsigned char *c = f->cmap4;
 	unsigned int seg, i, end, start, delta, range;
 
-	if (cp > 0xFFFFu || !c || f->cmap4_len < 16)
+	if (!c || f->cmap4_len < 16 || cp > 0xFFFFu)
 		return 0;
 	seg = be16(c + 6) / 2u;
 	if (seg == 0 || 16u + seg * 8u > f->cmap4_len)
@@ -213,6 +235,48 @@ int ufont_ttf_glyph_index(const struct ufont_face *f, unsigned int cp)
 	return 0;
 }
 
+static int cmap12_lookup(const struct ufont_face *f, unsigned int cp)
+{
+	const unsigned char *c = f->cmap12;
+	unsigned int lo = 0, hi;
+
+	if (!c || f->cmap12_len < 16)
+		return 0;
+	hi = f->cmap12_groups;
+	while (lo < hi) {
+		unsigned int mid = lo + (hi - lo) / 2;
+		const unsigned char *g = c + 16u + mid * 12u;
+		unsigned int start = be32(g);
+		unsigned int end = be32(g + 4);
+		unsigned int glyph = be32(g + 8);
+
+		if (cp < start) {
+			hi = mid;
+		} else if (cp > end) {
+			lo = mid + 1;
+		} else {
+			unsigned int id = glyph + (cp - start);
+
+			return id <= 0xFFFFu ? (int)id : 0;
+		}
+	}
+	return 0;
+}
+
+int ufont_ttf_glyph_index(const struct ufont_face *f, unsigned int cp)
+{
+	int g;
+
+	if (!f)
+		return 0;
+	if (cp <= 0xFFFFu) {
+		g = cmap4_lookup(f, cp);
+		if (g)
+			return g;
+	}
+	return cmap12_lookup(f, cp);
+}
+
 static const unsigned char *glyph_data(const struct ufont_face *f, int glyph,
 				       unsigned int *size)
 {

+ 2 - 0
wifi_cfg.h

@@ -9,6 +9,8 @@
 #define WIFI_CFG_H
 
 #define WIFI_CFG_MAGIC 0x4E425746u   /* "NBWF" */
+#define WIFI_CFG_STAGED "/share/netmenu.wifi"
+#define WIFI_CRED_PATH  "/data/WIFI.CFG"
 
 struct wifi_cfg {
 	unsigned int magic;

Неке датотеке нису приказане због велике количине промена