fix(firmware): preserve intact NVS values, copied blob chunks, unknown page versions and -0

This commit is contained in:
liziye committed 2026-11-28 02:02:52 +08:00
1 parent 057fb21362
commit a2e8fc1a70
4 files changed
+187 -13

No files matched your search

+139
View File
@@ -815,4 +815,143 @@ describe("parseEspNvsPartition", () => {
badReserved.writeUInt32LE(espIdfCrc32(badReserved.subarray(4, 28)), 28);
expect(hasEspNvsHeader(badReserved, 4096)).toBe(false);
});
it("keeps intact earlier settings active when a newer rewrite is damaged, skips unsupported page versions, handles copied BLOB_DATA chunks, and preserves -0 floats", async () => {
// Page 0 (seq 1): namespace "cfg"=1, intact string "host"="good", float "-0" and "+0", and BLOB_DATA chunk 0 copy
const p0 = initActivePage(1, 0xfe);
writeNamespaceSlot(p0, 0, "cfg", 1);
const spanGood = writeVarSlot({
page: p0,
slotIdx: 1,
nsId: 1,
typeCode: 0x21,
chunkIndex: 0xff,
key: "host",
payload: Buffer.from("good\0", "utf8"),
});
// Float -0.0 (0x80000000) and Double -0.0
const fNegSlotOff = 64 + (1 + spanGood) * 32;
const fNeg = p0.subarray(fNegSlotOff, fNegSlotOff + 32);
fNeg.fill(0xff);
fNeg[0] = 1;
fNeg[1] = 0x24; // FLOAT
fNeg[2] = 1;
fNeg[3] = 0xff;
writeKey(fNeg, "f_neg_zero");
fNeg.writeFloatLE(-0, 24);
fNeg.writeUInt32LE(computeNvsItemHeaderCrc(fNeg), 4);
setBitmapWritten(p0, 1 + spanGood, 1);
const dNegSlotOff = fNegSlotOff + 32;
const dNeg = p0.subarray(dNegSlotOff, dNegSlotOff + 32);
dNeg.fill(0xff);
dNeg[0] = 1;
dNeg[1] = 0x28; // DOUBLE
dNeg[2] = 1;
dNeg[3] = 0xff;
writeKey(dNeg, "d_neg_zero");
dNeg.writeDoubleLE(-0, 24);
dNeg.writeUInt32LE(computeNvsItemHeaderCrc(dNeg), 4);
setBitmapWritten(p0, 2 + spanGood, 1);
const blobChunkPayload = Buffer.from("copied-chunk-bytes", "utf8");
writeVarSlot({
page: p0,
slotIdx: 3 + spanGood,
nsId: 1,
typeCode: 0x42, // BLOB_DATA (older page-move copy)
chunkIndex: 0,
key: "moved_blob",
payload: blobChunkPayload,
});
// Page 1 (seq 2): copied BLOB_DATA chunk 0 + matching BLOB_IDX (complete page move before freeing p0)
const p1 = initActivePage(2, 0xfe);
const cSpan = writeVarSlot({
page: p1,
slotIdx: 0,
nsId: 1,
typeCode: 0x42,
chunkIndex: 0,
key: "moved_blob",
payload: blobChunkPayload,
});
const idxOff = 64 + cSpan * 32;
const idxSlot = p1.subarray(idxOff, idxOff + 32);
idxSlot.fill(0xff);
idxSlot[0] = 1;
idxSlot[1] = 0x48; // BLOB_IDX
idxSlot[2] = 1;
idxSlot[3] = 0xff;
writeKey(idxSlot, "moved_blob");
idxSlot.writeUInt32LE(blobChunkPayload.length, 24);
idxSlot[28] = 1; // chunkCount
idxSlot[29] = 0; // chunkStart
idxSlot.writeUInt32LE(computeNvsItemHeaderCrc(idxSlot), 4);
setBitmapWritten(p1, cSpan, 1);
// Check 1: [p0, p1] alone has complete coverage and zero orphan chunks despite duplicate BLOB_DATA
const cleanMove = await parseEspNvsPartition({
artifact: { path: "/tmp/move.bin", sha256: "a".repeat(64), bytes: 8192 },
partitionOffset: 0,
sliceBytes: Buffer.concat([p0, p1]),
});
expect(cleanMove.ok).toBe(true);
if (!cleanMove.ok) return;
expect(cleanMove.value.coverage.status).toBe("complete");
expect(
cleanMove.value.entries.filter((e) => e.data_type === "blob_data_orphan"),
).toHaveLength(0);
expect(
cleanMove.value.entries.find((e) => e.key === "f_neg_zero")?.value_float,
).toBe("-0");
expect(
cleanMove.value.entries.find((e) => e.key === "d_neg_zero")?.value_float,
).toBe("-0");
// Page 2 (seq 3): damaged string rewrite of "host" (corrupted payload byte -> crc_mismatch)
const p2 = initActivePage(3, 0xfe);
writeVarSlot({
page: p2,
slotIdx: 0,
nsId: 1,
typeCode: 0x21,
chunkIndex: 0xff,
key: "host",
payload: Buffer.from("corrupt\0", "utf8"),
});
p2[64 + 32] = p2[64 + 32]! ^ 0xff; // corrupt string payload byte
// Page 3 (seq 4): unsupported formatVersion 0xfd with valid header CRC
const p3 = initActivePage(4, 0xfd);
const full = await parseEspNvsPartition({
artifact: {
path: "/tmp/mixed.bin",
sha256: "b".repeat(64),
bytes: 16384,
},
partitionOffset: 0,
sliceBytes: Buffer.concat([p0, p1, p2, p3]),
});
expect(full.ok).toBe(true);
if (!full.ok) return;
expect(espNvsReportSchema.safeParse(full.value).success).toBe(true);
expect(full.value.coverage.status).toBe("partial");
expect(full.value.pages[3]?.format_version).toBe("unknown");
expect(full.value.pages[3]?.entries_decoded).toBe(false);
expect(full.value.undecoded_page_count).toBe(1);
const hostEntries = full.value.entries.filter((e) => e.key === "host");
expect(hostEntries).toHaveLength(2);
expect(hostEntries[0]).toMatchObject({
value_text: "good",
completeness: "complete",
superseded: false,
});
expect(hostEntries[1]).toMatchObject({
completeness: "crc_mismatch",
superseded: false,
});
});
});
+20 -3
View File
@@ -68,7 +68,21 @@ export const reassembleNvsV2Blobs = async (
sortedByNsKey.set(nsIndex, sortedByKey);
}
const usedChunkRefs = new Set<RawBlobChunk>();
const claimedChunks = new Map<number, Map<string, Set<number>>>();
const markChunkClaimed = (ch: RawBlobChunk): void => {
let byKey = claimedChunks.get(ch.nsIndex);
if (byKey === undefined) {
byKey = new Map<string, Set<number>>();
claimedChunks.set(ch.nsIndex, byKey);
}
let indicesSet = byKey.get(ch.keyId);
if (indicesSet === undefined) {
indicesSet = new Set<number>([ch.chunkIndex]);
byKey.set(ch.keyId, indicesSet);
} else {
indicesSet.add(ch.chunkIndex);
}
};
const reassemblyCache = new Map<
number,
Map<string, Map<number, ReassembledBlobSummary>>
@@ -118,7 +132,7 @@ export const reassembleNvsV2Blobs = async (
for (const ch of seriesChunks) {
if (ch.chunkIndex < idx.chunkStart) continue;
if (ch.chunkIndex >= endChunk) break;
usedChunkRefs.add(ch);
markChunkClaimed(ch);
matchedCount += 1;
if (!ch.crcValid) chunksCrcValid = false;
joinedLength += ch.payload.length;
@@ -175,7 +189,10 @@ export const reassembleNvsV2Blobs = async (
onCheckpoint?.();
}
const ch = chunks[i]!;
if (usedChunkRefs.has(ch)) continue;
if (
claimedChunks.get(ch.nsIndex)?.get(ch.keyId)?.has(ch.chunkIndex) === true
)
continue;
out.push({
page_index: ch.pageIndex,
entry_index: ch.entryIndex,
+4 -1
View File
@@ -136,6 +136,9 @@ const integer = (
integerText: String(read(reader, offset)),
}),
});
const formatFloat = (value: number): string =>
Object.is(value, -0) ? "-0" : String(value);
const float = (
dataType: EspNvsDataType,
read: (reader: BinaryReader, offset: number) => number,
@@ -143,7 +146,7 @@ const float = (
dataType,
read: (reader: BinaryReader, offset: number): PrimitiveValue => ({
dataType,
floatText: String(read(reader, offset)),
floatText: formatFloat(read(reader, offset)),
}),
});
+24 -9
View File
@@ -1,4 +1,5 @@
import { setImmediate } from "node:timers/promises";
import { byteToHex } from "../../kernel/byteToHex.js";
import { BinaryReader, readUint32At } from "../binaryContainer.js";
import { CoverageAccumulator } from "../coverageAccumulator.js";
import { malformedInput, type ParseFailureError } from "../parseFailure.js";
@@ -96,9 +97,12 @@ interface PageWalkAccumulator {
const undecodedReason = (
state: EspNvsPageState,
headerCrcValid: boolean,
versionByte: number,
): string => {
if (!headerCrcValid)
return "its header failed CRC32, which whole-flash encryption or damage produces; NVS encryption leaves page headers in plaintext";
if (READABLE_PAGE_STATES.has(state))
return `its format version byte 0x${byteToHex(versionByte)} is not ESP-IDF v1 (0xff) or v2 (0xfe)`;
if (state === "corrupt") return "firmware marked it CORRUPT (0xfffffff0)";
if (state === "invalid") return "its state is INVALID (0)";
return "its state is not an ESP-IDF page state";
@@ -204,7 +208,10 @@ const decodeSinglePage = (
readUint32At(pageBytes, 28, true);
const formatVersion: EspNvsPageSummary["format_version"] =
versionByte === 0xfe ? "v2" : versionByte === 0xff ? "v1" : "unknown";
const entriesDecoded = headerCrcValid && READABLE_PAGE_STATES.has(state);
const entriesDecoded =
headerCrcValid &&
READABLE_PAGE_STATES.has(state) &&
formatVersion !== "unknown";
if (tallies.illegal > 0)
acc.anomalies.push(
@@ -213,7 +220,7 @@ const decodeSinglePage = (
if (!entriesDecoded) {
acc.undecodedPageCount += 1;
acc.anomalies.push(
`Page ${String(pageIndex)} at offset 0x${pageFileOffset.toString(16)} (state ${hex32(rawState)}) was not decoded: ${undecodedReason(state, headerCrcValid)}.`,
`Page ${String(pageIndex)} at offset 0x${pageFileOffset.toString(16)} (state ${hex32(rawState)}) was not decoded: ${undecodedReason(state, headerCrcValid, versionByte)}.`,
);
}
@@ -272,7 +279,11 @@ const markSuperseded = (
const identity = (entry: PendingNvsEntry): string =>
`${String(entry.namespace_id)}\u0000${entry.keyId}\u0000${lookupClass(entry)}`;
for (const entry of entries) {
if (entry.data_type === "blob_data_orphan") continue;
if (
entry.data_type === "blob_data_orphan" ||
entry.completeness !== "complete"
)
continue;
const id = identity(entry);
const current = newest.get(id);
if (current === undefined) {
@@ -284,12 +295,16 @@ const markSuperseded = (
if (seq > currentSeq || (seq === currentSeq && slot > currentSlot))
newest.set(id, entry);
}
return entries.map((entry) =>
entry.data_type === "blob_data_orphan" ||
newest.get(identity(entry)) === entry
? entry
: { ...entry, superseded: true },
);
return entries.map((entry) => {
if (entry.data_type === "blob_data_orphan") return entry;
const winner = newest.get(identity(entry));
if (winner === undefined || winner === entry) return entry;
const [seq, slot] = order(entry);
const [winnerSeq, winnerSlot] = order(winner);
return winnerSeq > seq || (winnerSeq === seq && winnerSlot > slot)
? { ...entry, superseded: true }
: entry;
});
};
export interface ParseEspNvsOptions {