Posts Tagged ‘cpld flash reverse engineering’

PostHeaderIcon Five Flash Layouts and Their Fingerprints

A flash dump arrives as a single file with no map. Before any code analysis is possible, someone has to decide where the boot loader ends, where the application begins, whether there is a file system, and where the calibration constants live. That decision is made from the binary, not from a datasheet, because the datasheet describes the silicon and not the product.

Five layouts account for almost everything we see. Each has a fingerprint that is visible in a hex view, an entropy plot, or a search for a handful of byte patterns.

Layout one: a single flat region

The simplest layout is one image occupying the whole array, starting at address zero with a reset vector and continuing without interruption.

The fingerprint is a valid vector table at the very start — a stack pointer value that lies inside RAM, a reset vector that points inside the array, and a run of handler addresses all in the same range. After that the entropy stays roughly constant, with occasional clusters of zeros or 0xFF where constant tables and uninitialised padding sit.

The mistake this layout invites is assuming that a low-entropy region is unused. It is more often a lookup table, and deleting it to save space breaks the device in a way that is hard to trace later.

Layout two: a boot loader plus an application

The second layout splits the array into a small boot region at address zero and a larger application above it. This is what makes field update possible without a debugger.

The fingerprint is two vector tables. The first is at address zero and belongs to the boot loader. The second sits at the application’s base address, which is aligned to a block boundary and usually to a round number such as 0x4000 or 0x8000. Between the two there is a gap of unused space, filled with 0xFF, which is the visual signature of a layout boundary.

Feature Boot region Application region
Vector table At 0x0000 At its base address
Size Small, one to sixteen blocks The rest of the array
Contents UART or USB protocol handler The product
Tell in the binary A jump into higher memory A second SP and reset pair

The jump instruction at the end of the boot loader is the confirmation. It loads the application’s stack pointer from the second table and branches to the second reset vector, and seeing that sequence removes any doubt about where the boundary lies.

Layout three: dual bank

Dual bank exists so that an update can be interrupted without bricking the device. One bank holds the running image, the other receives the new one, and a marker decides which is valid after the next reset.

The fingerprint is symmetry. Two regions of identical size, each starting with a vector table, each ending at a block boundary, and each accompanied by a small header containing a version number, a length, and a checksum. Often one bank is entirely 0xFF because it has never been used.

Observation Interpretation
Two equal regions, one blank Bank B never written
Two regions, different versions Update completed, old bank retained
Two regions, one with a bad checksum Interrupted update
Header at bank start, not at 0x0 Swap mechanism is header-driven

The interrupted-update case is the one worth care. The bank with the bad checksum is not garbage; it is a partially written image, and the intact part of it is often useful for comparison against the running bank.

Layout four: an image plus a file system

Devices that log, store configuration, or hold user assets usually carry a small file system above the code. The common choices are littlefs, SPIFFS, and on larger parts a FAT variant.

The fingerprint is block-structured rather than code-like. Instead of a dense stream of instructions, there are repeating headers at fixed intervals, wear-levelling counters that increment across erase cycles, and a superblock near the start of the region that names the geometry.

File system Tell in a raw dump
littlefs Pairs of blocks with revision counters; a superblock with block size and count
SPIFFS Object headers with page indices; a large proportion of 0xFF at the end
FAT A boot sector with 0x55AA at offset 510, then a FAT table

The counter pattern is the most reliable sign. A region whose first few bytes change every few hundred bytes, incrementing slowly, is a journal, and journals belong to file systems.

Layout five: an image plus a parameter area

The fifth layout adds a small region that is written far more often than the rest. Calibration values, run-hour counters, MAC addresses, and trimmed oscillator settings all live here.

The fingerprint is a small erase-block-sized region, usually the last block or the one immediately after the code, with a short record structure repeated at intervals. Each record starts with a marker and ends with a checksum, and older records are superseded by newer ones rather than overwritten, because a flash cell can be programmed without an erase only if it is still erased.

Signal Meaning
Same structure repeated every 16 to 64 bytes Append-only record log
A monotonic counter field Sequence number for the newest record
A single-byte or two-byte checksum Record integrity
Region under 4 KB Sized to one erase block on purpose

This region is the one most often lost in a careless rebuild, because it looks like padding. It is also the region that a reference device cannot supply, since the values are per-unit.

Fingerprints that do not lie

Three measurements settle most layout questions quickly, and all three can be scripted.

The entropy plot comes first. Code sits in a narrow band, compressed data sits near the top, erased flash sits at zero, and a layout boundary shows as a step rather than a slope.

The second is a scan for vector-table candidates: any offset where two consecutive words both point inside the array and the second one, masked to the instruction-set alignment, lands on another plausible pointer chain. Genuine tables appear two or three times in a well-structured image and nowhere else.

Measurement What it resolves
Entropy per block Where data ends and code begins
Vector table scan Where each bank or image starts
0xFF run lengths Where regions were never programmed
ASCII string map Which region is configuration rather than code

The third is a string map, filtered to printable runs of six characters or more. Version strings, file system names, boot messages, and AT command tables cluster inside the region that owns them, and the clustering is usually enough to assign a region to a role.

Reading the map before reading the code

The order matters because the map decides which tool applies. A code region goes to a disassembler for the right architecture. A file system goes to a parser. A parameter block goes to a record decoder, and the record decoder needs the record layout, which is inferred from the repetition rather than from documentation.

Attempting the disassembler first is the usual failure. A file system region disassembles into plausible-looking instructions, because any byte sequence does on a variable-length instruction set, and the analysis produces pages of meaningless output before anyone notices.

What the service does

We produce a layout map as a separate deliverable, before analysis, because it makes the analysis quotable.

Output Content
Region table Start, end, size, and role for each region
Confidence Which regions are certain and which are inferred
Fingerprint evidence The measurement that supports each assignment
Per-unit data list Regions that a reference device cannot supply

The map is also the input to two downstream questions: which regions an extraction must preserve byte for byte, covered in floating gate margins, and which regions a copy can legitimately rebuild, covered in NAND and NOR copying.

Where the code itself then needs to be understood rather than located, the method is the one in MCU source code reverse engineering. The layout map is what tells that work where to start.