Get EvenHub font from firmware

Extracts the font used for EvenHub from the firmware (which is
downloaded from Even)
This commit is contained in:
James Babcock committed 2026-08-16 21:01:27 -07:00
1 parent 6a9743d27c
commit e9438f29f8
8 files changed
+612 -24

No files matched your search

+61 -3
View File
@@ -1,17 +1,23 @@
/**
* Builds the Faceclaw custom firmware on-device: downloads the stock Even
* Realities G2 2.2.6.10 image from Even's CDN, verifies its SHA-256, applies
* the committed byte-patch set (cfw-patches.ts), verifies the patched SHA-256,
* and writes the result to app storage.
* the committed byte-patch set (cfw-patches.ts), extracts the stock EvenHub
* fonts for phone-side rendering, verifies the patched SHA-256, and writes the
* result to app storage.
*
* This mirrors g2flash/build_cfw.sh + patches/apply_patches.py exactly (same
* URL, same pinned hashes, same offset/old/new patch semantics), so a
* successful run reproduces the reviewed image byte-for-byte. It does NOT flash
* anything — producing and verifying the image is the whole job here.
*/
import { knownFolders } from "@nativescript/core";
import { File, knownFolders } from "@nativescript/core";
import { CFW_PATCH_SET, FirmwarePatchOp } from "./firmware/cfw-patches";
import {
EVENHUB_RUNTIME_FONT_FILENAME,
extractEvenHubFontAsset,
isEvenHubFontAsset,
} from "./firmware-fonts";
declare const com: any;
@@ -22,6 +28,7 @@ const STOCK_OUTPUT_FILENAME = "g2_2.2.6.10.bin";
export type FirmwareProgress =
| { phase: "downloading" }
| { phase: "verifying-base" }
| { phase: "extracting-fonts" }
| { phase: "patching"; applied: number; total: number }
| { phase: "verifying-output" }
| { phase: "writing" }
@@ -35,6 +42,36 @@ export type BuiltFirmware = {
export class FirmwareBuildError extends Error {}
/** True only when a complete, parseable runtime extraction is present. */
export function hasExtractedEvenHubFonts(): boolean {
const path = `${knownFolders.documents().path}/${EVENHUB_RUNTIME_FONT_FILENAME}`;
if (!File.exists(path)) return false;
try {
return isEvenHubFontAsset(JSON.parse(File.fromPath(path).readTextSync()));
} catch {
return false;
}
}
/**
* Download and verify the pinned stock image, then extract its EvenHub fonts.
* Used to repair an existing custom-firmware install without reflashing it.
*/
export async function downloadAndExtractEvenHubFonts(
onProgress?: (progress: FirmwareProgress) => void,
): Promise<void> {
const report = (progress: FirmwareProgress) => {
try {
onProgress?.(progress);
} catch {
// progress reporting must never break extraction
}
};
const base = await downloadAndVerifyBase(report);
report({ phase: "extracting-fonts" });
persistEvenHubFonts(base);
}
/**
* Download → verify → patch → verify → persist. Rejects with a
* FirmwareBuildError carrying a human-readable message if any hash check or the
@@ -53,6 +90,9 @@ export async function buildCustomFirmware(
const base = await downloadAndVerifyBase(report);
report({ phase: "extracting-fonts" });
persistEvenHubFonts(base);
const patched = applyPatches(new Uint8Array(base), CFW_PATCH_SET.patches, (applied, total) =>
report({ phase: "patching", applied, total }),
);
@@ -127,6 +167,24 @@ async function downloadFirmware(): Promise<ArrayBuffer> {
return response.arrayBuffer();
}
function persistEvenHubFonts(base: ArrayBuffer): void {
try {
const asset = extractEvenHubFontAsset(base);
let writeError: unknown;
knownFolders.documents().getFile(EVENHUB_RUNTIME_FONT_FILENAME).writeTextSync(
JSON.stringify(asset),
(error) => {
writeError = error;
},
);
if (writeError) throw writeError;
} catch (error) {
throw new FirmwareBuildError(
`The firmware was verified, but its EvenHub fonts could not be extracted: ${(error as Error)?.message ?? error}`,
);
}
}
/**
* Replay the committed patch set onto `base`, returning the patched bytes. Each
* op writes `new` at `offset` after checking the bytes there equal `old`; an op
+304
View File
@@ -0,0 +1,304 @@
/**
* Extract the G2's proprietary 20 px EvenHub fallback fonts from a verified
* stock EVENOTA image. The resulting compact JSON contains only glyph boxes
* and bitmap bytes; text advances and kerning continue to come from pretext.
*
* These fonts are deliberately generated at install time rather than bundled
* with Faceclaw because their redistribution terms are unclear.
*/
export const EVENHUB_RUNTIME_FONT_FILENAME = "evenhub-firmware-20.json";
const MAIN_APP_NAME = "ota/s200_firmware_ota.bin";
const MAIN_APP_LOAD_ADDRESS = 0x00438000;
const MAIN_APP_PREAMBLE_SIZE = 0x20;
const COMPONENT_HEADER_SIZE = 128;
type FontSpec = {
descriptor: number;
lineHeight: number;
baseLine: number;
};
// Priority order is the stock EvenHub fallback chain (before the CJK font).
const FONT_SPECS: FontSpec[] = [
{ descriptor: 0x0077f414, lineHeight: 27, baseLine: 5 },
{ descriptor: 0x0077f428, lineHeight: 26, baseLine: 5 },
{ descriptor: 0x0077f374, lineHeight: 20, baseLine: 2 },
];
type ExtractedGlyph = {
boxW: number;
boxH: number;
ofsX: number;
ofsY: number;
bytes: Uint8Array;
};
export type EvenHubFontAsset = {
lineHeight: number;
baseline: number;
glyphs: Record<string, [number, number, number, number, number, number]>;
bitmapBase64: string;
};
/** Reject missing, truncated, or otherwise unusable persisted font assets. */
export function isEvenHubFontAsset(value: unknown): value is EvenHubFontAsset {
const asset = value as Partial<EvenHubFontAsset> | null;
return !!asset && Number.isFinite(asset.lineHeight) && Number.isFinite(asset.baseline) &&
!!asset.glyphs && typeof asset.glyphs === "object" && Object.keys(asset.glyphs).length >= 700 &&
typeof asset.bitmapBase64 === "string" && asset.bitmapBase64.length > 0;
}
/** Extract and serialize the fonts used for EvenHub background text. */
export function extractEvenHubFontAsset(firmware: ArrayBuffer): EvenHubFontAsset {
const image = new FirmwareImage(firmware);
const mainApp = image.findComponent(MAIN_APP_NAME);
if (mainApp.payloadSize <= MAIN_APP_PREAMBLE_SIZE) {
throw new Error("the main firmware component is missing its application payload");
}
const app = new AddressedImage(
image,
mainApp.payloadOffset + MAIN_APP_PREAMBLE_SIZE,
mainApp.payloadSize - MAIN_APP_PREAMBLE_SIZE,
MAIN_APP_LOAD_ADDRESS,
);
const merged = new Map<number, ExtractedGlyph>();
for (const spec of FONT_SPECS) extractFont(app, spec, merged);
if (merged.size < 700) {
throw new Error(`firmware font extraction produced only ${merged.size} glyphs`);
}
const records: EvenHubFontAsset["glyphs"] = {};
const chunks: Uint8Array[] = [];
let bitmapSize = 0;
for (const [codePoint, glyph] of [...merged].sort((a, b) => a[0] - b[0])) {
records[String(codePoint)] = [
glyph.boxW,
glyph.boxH,
glyph.ofsX,
glyph.ofsY,
bitmapSize,
glyph.bytes.length,
];
chunks.push(glyph.bytes);
bitmapSize += glyph.bytes.length;
}
const bitmap = new Uint8Array(bitmapSize);
let offset = 0;
for (const chunk of chunks) {
bitmap.set(chunk, offset);
offset += chunk.length;
}
const layout = FONT_SPECS[0]!;
return {
lineHeight: layout.lineHeight,
baseline: layout.lineHeight - layout.baseLine,
glyphs: records,
bitmapBase64: base64Encode(bitmap),
};
}
function extractFont(app: AddressedImage, spec: FontSpec, merged: Map<number, ExtractedGlyph>): void {
const bitmapAddress = app.u32(spec.descriptor);
const glyphDescriptors = app.u32(spec.descriptor + 4);
const cmapAddress = app.u32(spec.descriptor + 8);
const packed = app.u16(spec.descriptor + 18);
const cmapCount = packed & 0x1ff;
const bitsPerPixel = (packed >> 9) & 0xf;
const bitmapFormat = (packed >> 14) & 0x3;
if (bitsPerPixel !== 4 || bitmapFormat !== 3 || cmapCount === 0) {
throw new Error(
`unsupported firmware font at 0x${spec.descriptor.toString(16)} ` +
`(bpp=${bitsPerPixel}, format=${bitmapFormat}, cmaps=${cmapCount})`,
);
}
const mapping = new Map<number, number>();
for (let index = 0; index < cmapCount; index++) {
resolveCmap(app, cmapAddress + index * 20, mapping);
}
for (const [codePoint, glyphId] of mapping) {
if (glyphId <= 0 || merged.has(codePoint)) continue;
const descriptor = glyphDescriptors + glyphId * 16;
const bitmapIndex = app.u32(descriptor);
const boxW = app.u16(descriptor + 8);
const boxH = app.u16(descriptor + 10);
const ofsX = app.i16(descriptor + 12);
const ofsY = app.i16(descriptor + 14);
const length = boxW > 0 && boxH > 0 ? rowStride(boxW) * boxH : 0;
merged.set(codePoint, {
boxW,
boxH,
ofsX,
ofsY,
bytes: app.bytes(bitmapAddress + bitmapIndex, length),
});
}
}
function resolveCmap(app: AddressedImage, address: number, mapping: Map<number, number>): void {
const rangeStart = app.u32(address);
const rangeLength = app.u16(address + 4);
const glyphIdStart = app.u16(address + 6);
const unicodeList = app.u32(address + 8);
const glyphIdOffsets = app.u32(address + 12);
const listLength = app.u16(address + 16);
const type = app.u8(address + 18);
if (type === 2) {
for (let relative = 0; relative < rangeLength; relative++) {
mapping.set(rangeStart + relative, glyphIdStart + relative);
}
return;
}
if (type === 0) {
for (let relative = 0; relative < rangeLength; relative++) {
const offset = app.u8(glyphIdOffsets + relative);
if (relative !== 0 && offset === 0) continue;
mapping.set(rangeStart + relative, glyphIdStart + offset);
}
return;
}
if (type === 3 || type === 1) {
for (let index = 0; index < listLength; index++) {
const relative = app.u16(unicodeList + index * 2);
let offset = index;
if (glyphIdOffsets) {
offset = type === 1 ? app.u16(glyphIdOffsets + index * 2) : app.u8(glyphIdOffsets + index);
if (index !== 0 && offset === 0) continue;
}
mapping.set(rangeStart + relative, glyphIdStart + offset);
}
return;
}
throw new Error(`unknown LVGL font cmap type ${type} at 0x${address.toString(16)}`);
}
function rowStride(boxWidth: number): number {
// Even's LVGL pipeline aligns every row to a whole byte and adds a padding
// byte for even widths.
return Math.floor(boxWidth / 2) + 1;
}
class FirmwareImage {
private readonly view: DataView;
private readonly data: Uint8Array;
constructor(buffer: ArrayBuffer) {
this.view = new DataView(buffer);
this.data = new Uint8Array(buffer);
if (this.data.length < 0x40 || ascii(this.data, 0, 8) !== "EVENOTA\0") {
throw new Error("downloaded image is not an EVENOTA firmware container");
}
}
findComponent(name: string): { payloadOffset: number; payloadSize: number } {
const count = this.u32(8);
if (count <= 0 || count > 64) throw new Error(`invalid firmware component count ${count}`);
for (let index = 0; index < count; index++) {
const toc = 0x40 + index * 16;
const componentOffset = this.u32(toc + 4);
this.check(componentOffset, COMPONENT_HEADER_SIZE);
const payloadSize = this.u32(componentOffset + 8);
const filename = nulTerminatedAscii(this.data, componentOffset + 48, 80);
const payloadOffset = componentOffset + COMPONENT_HEADER_SIZE;
this.check(payloadOffset, payloadSize);
if (filename === name) return { payloadOffset, payloadSize };
}
throw new Error(`firmware component ${name} was not found`);
}
u32(offset: number): number {
this.check(offset, 4);
return this.view.getUint32(offset, true);
}
bytes(offset: number, length: number): Uint8Array {
this.check(offset, length);
return this.data.slice(offset, offset + length);
}
check(offset: number, length: number): void {
if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || offset < 0 || length < 0 || offset + length > this.data.length) {
throw new Error(`firmware range 0x${offset.toString(16)} + ${length} is out of bounds`);
}
}
}
class AddressedImage {
constructor(
private readonly firmware: FirmwareImage,
private readonly fileOffset: number,
private readonly length: number,
private readonly loadAddress: number,
) {}
u8(address: number): number {
return this.bytes(address, 1)[0]!;
}
u16(address: number): number {
const bytes = this.bytes(address, 2);
return bytes[0]! | (bytes[1]! << 8);
}
i16(address: number): number {
const value = this.u16(address);
return value & 0x8000 ? value - 0x10000 : value;
}
u32(address: number): number {
const bytes = this.bytes(address, 4);
return (bytes[0]! | (bytes[1]! << 8) | (bytes[2]! << 16) | (bytes[3]! << 24)) >>> 0;
}
bytes(address: number, length: number): Uint8Array {
const relative = address - this.loadAddress;
if (relative < 0 || relative + length > this.length) {
throw new Error(`firmware address 0x${address.toString(16)} + ${length} is outside the main application`);
}
return this.firmware.bytes(this.fileOffset + relative, length);
}
}
function ascii(bytes: Uint8Array, offset: number, length: number): string {
let value = "";
for (let i = 0; i < length; i++) value += String.fromCharCode(bytes[offset + i] ?? 0);
return value;
}
function nulTerminatedAscii(bytes: Uint8Array, offset: number, maxLength: number): string {
let value = "";
for (let i = 0; i < maxLength; i++) {
const byte = bytes[offset + i] ?? 0;
if (byte === 0) break;
value += String.fromCharCode(byte);
}
return value;
}
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64Encode(bytes: Uint8Array): string {
let result = "";
for (let i = 0; i < bytes.length; i += 3) {
const a = bytes[i] ?? 0;
const b = bytes[i + 1] ?? 0;
const c = bytes[i + 2] ?? 0;
const packed = (a << 16) | (b << 8) | c;
result += BASE64_ALPHABET[(packed >> 18) & 63];
result += BASE64_ALPHABET[(packed >> 12) & 63];
result += i + 1 < bytes.length ? BASE64_ALPHABET[(packed >> 6) & 63] : "=";
result += i + 2 < bytes.length ? BASE64_ALPHABET[packed & 63] : "=";
}
return result;
}