forked from Blendman974/faceclaw
Detect custom firmware during onboarding; add uninstall option
This commit is contained in:
1 parent
cb78841a9d
commit
ea80d05607
20 files changed
+917
-46
No files matched your search
+44
-12
@@ -16,7 +16,8 @@ import { CFW_PATCH_SET, FirmwarePatchOp } from "./firmware/cfw-patches";
|
||||
declare const com: any;
|
||||
|
||||
const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/a6966d807634cc97aec641a0dcca358b.bin";
|
||||
const OUTPUT_FILENAME = "g2_2.2.4.34_cfw.bin";
|
||||
const CFW_OUTPUT_FILENAME = "g2_2.2.4.34_cfw.bin";
|
||||
const STOCK_OUTPUT_FILENAME = "g2_2.2.4.34.bin";
|
||||
|
||||
export type FirmwareProgress =
|
||||
| { phase: "downloading" }
|
||||
@@ -50,16 +51,7 @@ export async function buildCustomFirmware(
|
||||
}
|
||||
};
|
||||
|
||||
report({ phase: "downloading" });
|
||||
const base = await downloadFirmware();
|
||||
|
||||
report({ phase: "verifying-base" });
|
||||
const baseSha = sha256Hex(base);
|
||||
if (baseSha !== CFW_PATCH_SET.baseSha256) {
|
||||
throw new FirmwareBuildError(
|
||||
`Downloaded stock firmware failed verification.\nexpected ${CFW_PATCH_SET.baseSha256}\ngot ${baseSha}`,
|
||||
);
|
||||
}
|
||||
const base = await downloadAndVerifyBase(report);
|
||||
|
||||
const patched = applyPatches(new Uint8Array(base), CFW_PATCH_SET.patches, (applied, total) =>
|
||||
report({ phase: "patching", applied, total }),
|
||||
@@ -75,13 +67,53 @@ export async function buildCustomFirmware(
|
||||
}
|
||||
|
||||
report({ phase: "writing" });
|
||||
const path = `${knownFolders.documents().path}/${OUTPUT_FILENAME}`;
|
||||
const path = `${knownFolders.documents().path}/${CFW_OUTPUT_FILENAME}`;
|
||||
writeFile(path, outBuffer);
|
||||
|
||||
report({ phase: "done", path, bytes: patched.length });
|
||||
return { path, bytes: patched.length, sha256: outSha };
|
||||
}
|
||||
|
||||
/**
|
||||
* Download and verify the unmodified stock firmware (no patches applied) and
|
||||
* persist it. Used to un-install the custom firmware by reflashing the original
|
||||
* image the CFW was built from.
|
||||
*/
|
||||
export async function buildStockFirmware(
|
||||
onProgress?: (progress: FirmwareProgress) => void,
|
||||
): Promise<BuiltFirmware> {
|
||||
const report = (progress: FirmwareProgress) => {
|
||||
try {
|
||||
onProgress?.(progress);
|
||||
} catch {
|
||||
// progress reporting must never break the build
|
||||
}
|
||||
};
|
||||
|
||||
const base = await downloadAndVerifyBase(report);
|
||||
|
||||
report({ phase: "writing" });
|
||||
const path = `${knownFolders.documents().path}/${STOCK_OUTPUT_FILENAME}`;
|
||||
writeFile(path, base);
|
||||
|
||||
report({ phase: "done", path, bytes: base.byteLength });
|
||||
return { path, bytes: base.byteLength, sha256: CFW_PATCH_SET.baseSha256 };
|
||||
}
|
||||
|
||||
async function downloadAndVerifyBase(report: (progress: FirmwareProgress) => void): Promise<ArrayBuffer> {
|
||||
report({ phase: "downloading" });
|
||||
const base = await downloadFirmware();
|
||||
|
||||
report({ phase: "verifying-base" });
|
||||
const baseSha = sha256Hex(base);
|
||||
if (baseSha !== CFW_PATCH_SET.baseSha256) {
|
||||
throw new FirmwareBuildError(
|
||||
`Downloaded stock firmware failed verification.\nexpected ${CFW_PATCH_SET.baseSha256}\ngot ${baseSha}`,
|
||||
);
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
async function downloadFirmware(): Promise<ArrayBuffer> {
|
||||
let response: Response;
|
||||
try {
|
||||
|
||||
@@ -10,6 +10,12 @@ import { type FirmwareInfo } from "../native/faceclaw-communicator";
|
||||
const MIN_FIRMWARE_VERSION = [2, 2, 4, 34];
|
||||
const REQUIRED_FIRMWARE_EXTENSION = "imgz";
|
||||
|
||||
// The stock firmware release Faceclaw's custom image is built from. Stock at or
|
||||
// below this can be flashed with our patched image; a newer stock version is
|
||||
// unrecognized (its layout may differ from what our patch set targets).
|
||||
export const FLASHABLE_STOCK_VERSION = [2, 2, 4, 34];
|
||||
export const FLASHABLE_STOCK_VERSION_TEXT = FLASHABLE_STOCK_VERSION.join(".");
|
||||
|
||||
function parseDottedVersion(version: string): number[] {
|
||||
return version
|
||||
.trim()
|
||||
@@ -60,3 +66,41 @@ export function firmwareIncompatibilityMessage(info: FirmwareInfo): string | nul
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** True when the glasses advertise Faceclaw's custom-firmware image extension. */
|
||||
export function hasCustomFirmware(info: FirmwareInfo): boolean {
|
||||
return info.capabilities.trim().split(/\s+/).includes(REQUIRED_FIRMWARE_EXTENSION);
|
||||
}
|
||||
|
||||
/** The higher of the two arms' reported versions, or "" if none reported. */
|
||||
export function reportedFirmwareVersion(info: FirmwareInfo): string {
|
||||
const versions = [info.leftVersion, info.rightVersion].map((v) => v.trim()).filter(Boolean);
|
||||
if (versions.length === 0) return "";
|
||||
return versions.reduce((highest, current) =>
|
||||
compareVersions(parseDottedVersion(current), parseDottedVersion(highest)) > 0 ? current : highest,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* How the pre-flash firmware check should treat the connected glasses:
|
||||
* - "custom": Faceclaw's firmware is already installed — nothing to flash.
|
||||
* - "flashable-stock": stock firmware at or below the version we build from.
|
||||
* - "newer-stock": stock firmware newer than we recognize — flash only on override.
|
||||
* - "unknown": no version could be read (treated as a probe/connection failure).
|
||||
*/
|
||||
export type OnboardingFirmwareKind = "custom" | "flashable-stock" | "newer-stock" | "unknown";
|
||||
|
||||
export function classifyOnboardingFirmware(info: FirmwareInfo): {
|
||||
kind: OnboardingFirmwareKind;
|
||||
version: string;
|
||||
} {
|
||||
if (hasCustomFirmware(info)) {
|
||||
return { kind: "custom", version: reportedFirmwareVersion(info) };
|
||||
}
|
||||
const version = reportedFirmwareVersion(info);
|
||||
if (!version) {
|
||||
return { kind: "unknown", version: "" };
|
||||
}
|
||||
const comparison = compareVersions(parseDottedVersion(version), FLASHABLE_STOCK_VERSION);
|
||||
return { kind: comparison <= 0 ? "flashable-stock" : "newer-stock", version };
|
||||
}
|
||||
Reference in new issue
Block a user