forked from Blendman974/faceclaw
Firmware uninstall/revert-to-stock for G1s
This commit is contained in:
1 parent
e103b89510
commit
479308f4a6
17 files changed
+1014
-98
No files matched your search
+13
-2
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Finding a G1's arms by listening for them. That's how an install knows what each arm runs before connecting to it,
|
||||
* and how it finds an arm again afterwards: an arm coming off Even's firmware has a new address.
|
||||
* and how it finds an arm again afterwards, when it advertises what it runs now (and, coming from g1v0 before 0.5.66, at
|
||||
* a new address).
|
||||
*/
|
||||
import { classifyAdvertisement, type EvenAdvertisement } from "../g2/even-advertisement";
|
||||
import { DiscoveryAggregator, type GlassesPairCandidate } from "../g2/pairing-candidates";
|
||||
@@ -27,11 +28,21 @@ export class G1ArmScanner {
|
||||
|
||||
/** Resolves when an arm of this side on g1v0 is heard, or null after [timeoutMs]. */
|
||||
waitForG1v0Arm(side: G1Side, serial: string | null | undefined, timeoutMs: number): Promise<EvenAdvertisement | null> {
|
||||
return this.waitForArm(side, serial, timeoutMs, (arm) => arm.g1Firmware !== "stock");
|
||||
}
|
||||
|
||||
/** Resolves when an arm of this side is heard that [accept] takes, on any firmware, or null after [timeoutMs]. */
|
||||
waitForArm(
|
||||
side: G1Side,
|
||||
serial: string | null | undefined,
|
||||
timeoutMs: number,
|
||||
accept: (arm: EvenAdvertisement) => boolean,
|
||||
): Promise<EvenAdvertisement | null> {
|
||||
const startedAt = Date.now();
|
||||
const others = new Map<string, EvenAdvertisement>();
|
||||
return this.listen<EvenAdvertisement>(timeoutMs, (raw, done) => {
|
||||
const arm = classifyAdvertisement(raw);
|
||||
if (!arm || arm.role !== side || !arm.g1Firmware || arm.g1Firmware === "stock") return;
|
||||
if (!arm || arm.role !== side || !arm.g1Firmware || !accept(arm)) return;
|
||||
if (!serial || sameG1Serial(arm.serial, serial)) {
|
||||
done(arm);
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Even's official G1 firmware, which an uninstall puts back: both images of one public release, the application core's
|
||||
* and the network core's (the Bluetooth controller), as Even's own update installs them, so the two always match.
|
||||
* From Even's CDN, checked against these SHA-256 sums (scouter_kit g1_firmware/stock_images.txt). Every public image
|
||||
* says 0.0.0 in its MCUboot header, so the hashes are the only way to tell releases apart.
|
||||
*/
|
||||
export type EvenG1Release = {
|
||||
version: string;
|
||||
zipUrl: string;
|
||||
zipSha256: string;
|
||||
appSha256: string;
|
||||
netCoreSha256: string;
|
||||
};
|
||||
|
||||
export const EVEN_G1_RESTORE: EvenG1Release = {
|
||||
version: "1.6.6",
|
||||
zipUrl: "https://cdn.evenreal.co/firmware/dcfa510c090e476c10a6f6c348a22415.zip",
|
||||
zipSha256: "748836f2d9d092b0783cbb7672fdccec4f730051cad27f095bbb156ac3e5d316",
|
||||
appSha256: "ae032838e27037156da98e19de90df78f5406715751f3b38adf50498db3010c1",
|
||||
netCoreSha256: "c277749be97a2763742db399afeb10ca6916059abd60d41bbc05c8892c002c77",
|
||||
};
|
||||
@@ -1,29 +1,34 @@
|
||||
/**
|
||||
* Prepares the g1v0 image on the phone, as app/g2/firmware-builder.ts does for a G2: downloads Even's public firmware,
|
||||
* checks it, and puts the parts this firmware embeds back into what the app ships. The result is byte for byte the
|
||||
* image that was built, tested and signed, and it is checked to be. It does not touch the glasses.
|
||||
* image that was built, tested and signed, and it is checked to be. Also prepares Even's own images for an uninstall.
|
||||
* It does not touch the glasses.
|
||||
*/
|
||||
import { knownFolders } from "@nativescript/core";
|
||||
|
||||
import { firmwareSha256, writeFirmwareFile } from "../native/firmware-files";
|
||||
import { decodeBase64 } from "../util/base64";
|
||||
import { fetchWithUserAgent } from "../util/http";
|
||||
import { EVEN_G1_RESTORE } from "./even-firmware";
|
||||
import { G1V0_IMAGE } from "./firmware/g1v0-image";
|
||||
import { G1V0_OWN_BYTES_BASE64 } from "./firmware/g1v0-own-bytes";
|
||||
import { assembleG1Image, decodeG1OwnBytes, G1FirmwareError, readZipEntry } from "./firmware-image";
|
||||
import { assembleG1Image, decodeG1OwnBytes, extractEvenG1Images, G1FirmwareError, readZipEntry } from "./firmware-image";
|
||||
|
||||
export type G1BuildProgress = "downloading" | "verifying" | "assembling" | "writing";
|
||||
|
||||
export type BuiltG1Firmware = { path: string; bytes: number; version: string };
|
||||
|
||||
/** Even's release for an uninstall: the application core's image and the network core's. */
|
||||
export type BuiltEvenG1Firmware = { appPath: string; netCorePath: string; bytes: number; version: string };
|
||||
|
||||
function sha256(bytes: Uint8Array): string {
|
||||
return firmwareSha256(bytes.slice().buffer);
|
||||
}
|
||||
|
||||
async function downloadStockZip(): Promise<Uint8Array> {
|
||||
async function downloadStockZip(url: string): Promise<Uint8Array> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetchWithUserAgent(G1V0_IMAGE.stockZipUrl);
|
||||
response = await fetchWithUserAgent(url);
|
||||
} catch (error) {
|
||||
throw new G1FirmwareError(`Could not reach Even's firmware server: ${(error as Error)?.message ?? error}`);
|
||||
}
|
||||
@@ -33,7 +38,7 @@ async function downloadStockZip(): Promise<Uint8Array> {
|
||||
|
||||
export async function buildG1Firmware(onProgress?: (progress: G1BuildProgress) => void): Promise<BuiltG1Firmware> {
|
||||
onProgress?.("downloading");
|
||||
const zip = await downloadStockZip();
|
||||
const zip = await downloadStockZip(G1V0_IMAGE.stockZipUrl);
|
||||
|
||||
onProgress?.("verifying");
|
||||
const zipHash = sha256(zip);
|
||||
@@ -50,3 +55,19 @@ export async function buildG1Firmware(onProgress?: (progress: G1BuildProgress) =
|
||||
writeFirmwareFile(path, image.slice().buffer);
|
||||
return { path, bytes: image.length, version: G1V0_IMAGE.version };
|
||||
}
|
||||
|
||||
export async function buildEvenG1Firmware(onProgress?: (progress: G1BuildProgress) => void): Promise<BuiltEvenG1Firmware> {
|
||||
onProgress?.("downloading");
|
||||
const zip = await downloadStockZip(EVEN_G1_RESTORE.zipUrl);
|
||||
|
||||
onProgress?.("verifying");
|
||||
const { app, netCore } = extractEvenG1Images(zip, EVEN_G1_RESTORE, sha256);
|
||||
|
||||
onProgress?.("writing");
|
||||
const folder = knownFolders.documents().path;
|
||||
const appPath = `${folder}/even_g1_${EVEN_G1_RESTORE.version}_app.bin`;
|
||||
const netCorePath = `${folder}/even_g1_${EVEN_G1_RESTORE.version}_net.bin`;
|
||||
writeFirmwareFile(appPath, app.slice().buffer);
|
||||
writeFirmwareFile(netCorePath, netCore.slice().buffer);
|
||||
return { appPath, netCorePath, bytes: app.length + netCore.length, version: EVEN_G1_RESTORE.version };
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
import { inflate, inflateRaw } from "pako";
|
||||
|
||||
import type { EvenG1Release } from "./even-firmware";
|
||||
import type { G1FirmwareImage } from "./firmware/g1v0-image";
|
||||
|
||||
export class G1FirmwareError extends Error {}
|
||||
@@ -41,6 +42,31 @@ export function readZipEntry(zip: Uint8Array, name: string): Uint8Array {
|
||||
throw new G1FirmwareError(`The firmware download has no ${name}.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* The two images of Even's release from its download, each checked by its own SHA-256 as well as the zip's, so what
|
||||
* goes to the arms is byte for byte what Even published.
|
||||
*/
|
||||
export function extractEvenG1Images(
|
||||
zip: Uint8Array,
|
||||
release: EvenG1Release,
|
||||
sha256: (bytes: Uint8Array) => string,
|
||||
): { app: Uint8Array; netCore: Uint8Array } {
|
||||
const zipHash = sha256(zip);
|
||||
if (zipHash !== release.zipSha256) {
|
||||
throw new G1FirmwareError(`Even's firmware download failed verification.\nexpected ${release.zipSha256}\ngot ${zipHash}`);
|
||||
}
|
||||
const checked = (name: string, want: string) => {
|
||||
const bytes = readZipEntry(zip, name);
|
||||
const got = sha256(bytes);
|
||||
if (got !== want) throw new G1FirmwareError(`${name} in Even's download failed verification.\nexpected ${want}\ngot ${got}`);
|
||||
return bytes;
|
||||
};
|
||||
return {
|
||||
app: checked("app_update.bin", release.appSha256),
|
||||
netCore: checked("net_core_app_update.bin", release.netCoreSha256),
|
||||
};
|
||||
}
|
||||
|
||||
export function decodeG1OwnBytes(base64: string, decode: (text: string) => Uint8Array): Uint8Array {
|
||||
return inflate(decode(base64));
|
||||
}
|
||||
|
||||
+16
-5
@@ -1,11 +1,15 @@
|
||||
/** What installing firmware on a G1 has to decide. No I/O. */
|
||||
import type { GlassesPairCandidate } from "../g2/pairing-candidates";
|
||||
import { EVEN_G1_RESTORE } from "./even-firmware";
|
||||
|
||||
export type G1Side = "left" | "right";
|
||||
|
||||
/** Left first: a right arm on g1v0 next to a left arm on Even's firmware would leave the left eye dark. */
|
||||
export const G1_SIDES: readonly G1Side[] = ["left", "right"];
|
||||
|
||||
/** Going back, right first, for the same reason: halfway, the pair is as halfway through an install. */
|
||||
export const G1_UNINSTALL_SIDES: readonly G1Side[] = ["right", "left"];
|
||||
|
||||
export const G1_CASE_INSTRUCTIONS =
|
||||
"Put the glasses in their charging case with the lid open, and plug the case in. Stay nearby, and leave Faceclaw open.";
|
||||
|
||||
@@ -14,13 +18,20 @@ export const G1_CASE_DELAY_HINT =
|
||||
"A fully charged arm can take up to 5 minutes to notice it's in the case, so this may take a while.";
|
||||
|
||||
export const G1_INSTALL_NOTICE =
|
||||
"This replaces the firmware on both arms, about two minutes each. Going back to Even's firmware later takes a computer, and " +
|
||||
"may not be the version you have now. Installing custom firmware is not covered by warranty.";
|
||||
"This replaces the firmware on both arms, about two minutes each. Faceclaw can put Even's firmware back later, but as version " +
|
||||
`${EVEN_G1_RESTORE.version}, which may not be the version you have now. Installing custom firmware is not covered by warranty.`;
|
||||
|
||||
export const G1_UNINSTALL_NOTICE =
|
||||
`This puts Even's official firmware ${EVEN_G1_RESTORE.version} back on both arms, Bluetooth controller included, a few minutes ` +
|
||||
"each. It may not be the version they came with. Afterwards they work with Even's app again, and not with Faceclaw until you " +
|
||||
"install its firmware again.";
|
||||
|
||||
/** What the native flow says when it finishes (G1ArmFlashFlow). */
|
||||
export type G1FlashResult = {
|
||||
flashed?: boolean;
|
||||
firmware?: "g1v0" | "stock";
|
||||
/** A restore erased g1v0's asset store, where Even's fonts and pictures were. */
|
||||
wiped?: boolean;
|
||||
version?: string;
|
||||
safeMode?: boolean;
|
||||
inCase?: boolean;
|
||||
@@ -102,8 +113,8 @@ export function g1StateFromAdvertising(
|
||||
return { state: compareG1Versions(lowest, shipped) < 0 ? "older" : "current", version: lowest };
|
||||
}
|
||||
|
||||
/** Each arm counts for half of the bar. */
|
||||
export function g1FlashPercent(side: G1Side, bytesSent: number, bytesTotal: number): number {
|
||||
/** Each arm counts for half of the bar, in the order they're done. */
|
||||
export function g1FlashPercent(side: G1Side, bytesSent: number, bytesTotal: number, order: readonly G1Side[] = G1_SIDES): number {
|
||||
const within = bytesTotal > 0 ? Math.min(1, bytesSent / bytesTotal) : 0;
|
||||
return ((side === "right" ? 1 + within : within) / 2) * 100;
|
||||
return ((order.indexOf(side) + within) / 2) * 100;
|
||||
}
|
||||
Reference in new issue
Block a user