Firmware uninstall/revert-to-stock for G1s

This commit is contained in:
James Babcock committed 2026-11-08 00:11:13 -08:00
1 parent e103b89510
commit 479308f4a6
17 files changed
+1014 -98

No files matched your search

+158 -27
View File
@@ -1,14 +1,23 @@
import { Frame, Observable } from "@nativescript/core";
import { G1ArmScanner } from "../g1/arm-scanner";
import { EVEN_G1_RESTORE } from "../g1/even-firmware";
import { G1V0_IMAGE } from "../g1/firmware/g1v0-image";
import { buildG1Firmware, type BuiltG1Firmware, type G1BuildProgress } from "../g1/firmware-builder";
import {
buildEvenG1Firmware,
buildG1Firmware,
type BuiltEvenG1Firmware,
type BuiltG1Firmware,
type G1BuildProgress,
} from "../g1/firmware-builder";
import { saveG1Arm, saveG1Pair } from "../g1/addresses";
import {
G1_CASE_DELAY_HINT,
G1_CASE_INSTRUCTIONS,
G1_INSTALL_NOTICE,
G1_SIDES,
G1_UNINSTALL_NOTICE,
G1_UNINSTALL_SIDES,
g1FlashPercent,
type G1FlashResult,
type G1Side,
@@ -23,19 +32,27 @@ import { formatErrorMessage } from "../util/format-error";
import { finishOnboardingNavigation } from "./onboarding-navigation";
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
type Phase = "intro" | "preparing" | "flashing" | "flashed" | "error" | "unavailable";
type Phase = "intro" | "preparing" | "flashing" | "flashed" | "error";
const LOCATE_MS = 45_000;
/** An arm takes about 20 s to copy the image, and a hung boot resets itself within two minutes. */
const RETURN_MS = 240_000;
/** Even's image is larger than g1v0, and MCUboot first installs the network core's. */
const RESTORE_RETURN_MS = 300_000;
/**
* Just after its reset an arm may still be heard on g1v0. Later than this, one heard on g1v0 has booted it again:
* MCUboot didn't take Even's firmware.
*/
const OLD_FIRMWARE_GRACE_MS = 15_000;
const CONNECT_MS = 60_000;
/** A new arm pairs only once it knows it's in the case, which a full case can take a while to tell it. */
const FIRST_PAIRING_MS = 120_000;
/**
* The install page on a G1, in place of OnboardingFlashViewModel. The two arms are separate devices that each take the
* image over MCUmgr (G1ArmFlashFlow), left first. There is nothing on the lens to confirm with before g1v0 is on the arm,
* so the confirmation is on the phone: the page before, or this one's intro.
* The install and uninstall page on a G1, in place of OnboardingFlashViewModel. The two arms are separate devices that
* each take the image over MCUmgr (G1ArmFlashFlow), left first; an uninstall goes right first and puts back both of
* Even's images, the network core's too. There is nothing on the lens to confirm with while an arm runs Even's
* firmware, so the confirmation is on the phone: the page before, or this one's intro.
*
* Every run starts by listening for what each arm runs now, so a retry picks up wherever the last one stopped. An arm's
* new address is saved as soon as it's known.
@@ -43,12 +60,18 @@ const FIRST_PAIRING_MS = 120_000;
export class G1FlashViewModel extends Observable {
private phase: Phase = "intro";
private readonly fromOnboarding: boolean;
private readonly uninstalling: boolean;
private readonly scanner = new G1ArmScanner();
private image: BuiltG1Firmware | null = null;
private evenImage: BuiltEvenG1Firmware | null = null;
private flasher: G1Flasher | null = null;
private generation = 0;
private disposed = false;
private unpairedArm = false;
/** An uninstall's outcome, for the last page: an arm's store erased, a pairing this phone couldn't remove, an arm done. */
private wiped = false;
private pairingLeft = false;
private restoredArm = false;
headline = "Install Faceclaw's Firmware";
status = "";
@@ -59,17 +82,14 @@ export class G1FlashViewModel extends Observable {
constructor(options?: { mode?: "install" | "uninstall"; fromOnboarding?: boolean; autoStart?: boolean }) {
super();
this.fromOnboarding = options?.fromOnboarding ?? true;
if (options?.mode === "uninstall") {
// Restoring Even's firmware has never been run on a real arm, and an arm whose network core is older than the
// image may not come back, so the phone doesn't offer it yet.
this.phase = "unavailable";
this.headline = "Back to Even's Firmware";
this.status = "Faceclaw can't restore Even's firmware on a G1 yet. It takes a computer: see \"Restore Even's firmware\" in " +
"the ScouterKit firmware's README.";
return;
}
this.uninstalling = options?.mode === "uninstall";
suppressAutoReconnect();
this.status = `${G1_CASE_INSTRUCTIONS}\n\n${G1_INSTALL_NOTICE}`;
if (this.uninstalling) {
this.headline = "Back to Even's Firmware";
this.status = `${G1_CASE_INSTRUCTIONS}\n\n${G1_UNINSTALL_NOTICE}`;
} else {
this.status = `${G1_CASE_INSTRUCTIONS}\n\n${G1_INSTALL_NOTICE}`;
}
if (options?.autoStart) void this.run();
}
@@ -90,7 +110,7 @@ export class G1FlashViewModel extends Observable {
get primaryLabel(): string {
switch (this.phase) {
case "intro":
return "Install";
return this.uninstalling ? "Uninstall" : "Install";
case "flashed":
return "Finish";
case "error":
@@ -119,7 +139,7 @@ export class G1FlashViewModel extends Observable {
}
onSecondaryTap(): void {
if (this.phase === "intro" || this.phase === "error" || this.phase === "unavailable") this.leave();
if (this.phase === "intro" || this.phase === "error") this.leave();
}
// --- the install ----------------------------------------------------------
@@ -129,21 +149,30 @@ export class G1FlashViewModel extends Observable {
const live = () => !this.disposed && generation === this.generation;
this.setPhase("preparing");
this.setBusy(true);
this.setText("Preparing Firmware", "Getting the firmware ready...");
this.setText("Preparing Firmware", this.uninstalling ? "Getting Even's firmware ready..." : "Getting the firmware ready...");
try {
await ensureBlePermissions();
if (!live()) return;
if (!this.image) {
this.image = await buildG1Firmware((progress) => {
if (live()) this.setStatus(describeBuild(progress));
});
this.appendLog(`firmware ${this.image.version} prepared and verified (${this.image.bytes.toLocaleString()} bytes)`);
if (this.uninstalling) {
if (!this.evenImage) {
this.evenImage = await buildEvenG1Firmware((progress) => {
if (live()) this.setStatus(describeEvenBuild(progress));
});
this.appendLog(`Even's firmware ${this.evenImage.version} downloaded and verified (${this.evenImage.bytes.toLocaleString()} bytes)`);
}
} else {
if (!this.image) {
this.image = await buildG1Firmware((progress) => {
if (live()) this.setStatus(describeBuild(progress));
});
this.appendLog(`firmware ${this.image.version} prepared and verified (${this.image.bytes.toLocaleString()} bytes)`);
}
await this.prepareFonts(live);
}
await this.prepareFonts(live);
if (!live()) return;
this.setPhase("flashing");
this.setText("Installing Firmware", "Listening for your glasses...");
this.setText(this.uninstalling ? "Restoring Even's Firmware" : "Installing Firmware", "Listening for your glasses...");
const identity = loadPairedGlassesIdentity();
const stored = loadDeviceAddresses();
const pair = await this.scanner.waitForPair({ serial: identity?.serial, left: stored.left, right: stored.right }, LOCATE_MS);
@@ -154,6 +183,14 @@ export class G1FlashViewModel extends Observable {
}
saveG1Pair(pair);
if (this.uninstalling) {
for (const side of G1_UNINSTALL_SIDES) {
await this.restoreArm(side, pair, live);
if (!live()) return;
}
this.restored();
return;
}
this.unpairedArm = false;
for (const side of G1_SIDES) {
await this.installArm(side, pair, live);
@@ -231,6 +268,65 @@ export class G1FlashViewModel extends Observable {
this.appendLog(`${side} arm runs ${running.version}`);
}
/**
* Even's firmware back on one arm, then a wait for it to advertise as Even's. An arm already on Even's firmware is
* left alone: a retry goes on from there.
*/
private async restoreArm(side: G1Side, pair: GlassesPairCandidate, live: () => boolean): Promise<void> {
const arm = pair[side]!;
if (arm.g1Firmware === "stock") {
this.appendLog(`${side} arm already runs Even's firmware`);
this.setProgress(g1FlashPercent(side, 1, 1, G1_UNINSTALL_SIDES));
return;
}
const even = this.evenImage!;
this.setProgress(g1FlashPercent(side, 0, 1, G1_UNINSTALL_SIDES));
this.setStatus(`Connecting to the ${side} arm...`);
const result = await this.flash({
address: arm.address,
side,
firmwarePath: even.appPath,
netCorePath: even.netCorePath,
mode: "restore",
connectWindowMs: CONNECT_MS,
}, (state) => {
if (state === "pairing") this.setStatus(`Pairing with the ${side} arm. If your phone asks, accept it.`);
else if (state === "checking") this.setStatus(`Checking the ${side} arm...`);
else if (state === "wiping") {
this.setStatus(`Erasing Faceclaw's storage on the ${side} arm, where Even's fonts were. This can take several minutes; keep the ` +
"glasses in the case. Nothing else has changed yet.");
} else if (state === "flashing") this.setStatus(`Sending Even's firmware to the ${side} arm. Its firmware hasn't changed yet.`);
else if (state === "committing") this.setStatus(`Installing on the ${side} arm. Keep the glasses in the case.`);
}, (sent, total) => this.setProgress(g1FlashPercent(side, sent, total, G1_UNINSTALL_SIDES)));
if (!live()) return;
if (result.wiped) this.wiped = true;
this.setProgress(g1FlashPercent(side, 1, 1, G1_UNINSTALL_SIDES));
this.setStatus(`The ${side} arm is restarting into Even's firmware. This takes a minute or two; keep it in the case.`);
const resetAt = Date.now();
const identity = loadPairedGlassesIdentity();
const back = await this.scanner.waitForArm(side, identity?.serial ?? pair.serial, RESTORE_RETURN_MS,
(heard) => heard.g1Firmware === "stock" || Date.now() - resetAt >= OLD_FIRMWARE_GRACE_MS);
if (!live()) return;
if (!back) {
throw new Error(`The ${side} arm hasn't come back yet. Leave the glasses in the plugged-in case for a few minutes, with the lid ` +
"open: an interrupted install finishes by itself. Then retry.");
}
if (back.g1Firmware !== "stock") {
throw new Error(`The ${side} arm came back on Faceclaw's firmware: it didn't take Even's. Retry to try again.`);
}
saveG1Arm(side, back);
this.restoredArm = true;
// Even's app makes its own pairing with Even's firmware; the one this phone made with g1v0 would only be in its way.
this.forgetPairing(back.address);
if (back.address !== arm.address) this.forgetPairing(arm.address);
this.appendLog(`${side} arm runs Even's firmware`);
}
private forgetPairing(address: string): void {
if (!forgetG1Pairing(address)) this.pairingLeft = true;
}
private async flash(
options: G1FlasherOptions,
onState?: (state: string) => void,
@@ -271,6 +367,28 @@ export class G1FlashViewModel extends Observable {
: ""));
}
private restored(): void {
// Stays manually disconnected: Faceclaw would only find firmware it can't run.
this.setBusy(false);
this.setProgress(100);
this.setPhase("flashed");
if (!this.restoredArm) {
this.setText("All Done", "Both arms already run Even's firmware.");
return;
}
const steps = [`Both arms run Even's firmware ${EVEN_G1_RESTORE.version}.`];
if (this.pairingLeft) {
steps.push("Before pairing them in Even's app, remove both arms in this phone's Bluetooth settings (\"Forget This Device\"): " +
"Faceclaw's firmware made that pairing, and Even's firmware makes its own.");
} else {
steps.push("To use them, pair them in Even's app.");
}
if (this.wiped) {
steps.push("Even's Chinese, Japanese and Korean text and its demo pictures were erased with Faceclaw's storage, and don't come back.");
}
this.setText("All Done", steps.join("\n\n"));
}
private failed(message: string): void {
this.setBusy(false);
this.setPhase("error");
@@ -278,8 +396,10 @@ export class G1FlashViewModel extends Observable {
}
private finish(): void {
setPreviewOnlyMode(false);
setOnboardingCompleted(true);
if (!this.uninstalling) {
setPreviewOnlyMode(false);
setOnboardingCompleted(true);
}
this.teardown();
finishOnboardingNavigation();
}
@@ -344,6 +464,17 @@ export class G1FlashViewModel extends Observable {
}
}
function describeEvenBuild(progress: G1BuildProgress): string {
switch (progress) {
case "downloading":
return "Downloading Even's firmware from Even's firmware server...";
case "verifying":
return "Verifying the download...";
default:
return "Saving the firmware...";
}
}
function describeBuild(progress: G1BuildProgress): string {
switch (progress) {
case "downloading":