Posts Tagged ‘extract atmel microcontroller embedded firmware’

PostHeaderIcon Read Lattice CPLD Embedded Firmware: Where the Bits Actually Live

Asking to read the firmware from a Lattice CPLD borrows a vocabulary that was built for microcontrollers, and the borrowing causes real confusion. A microcontroller stores instructions at addresses and executes them in sequence. A complex programmable logic device stores a configuration that defines how logic cells connect. There is no program counter, no instruction stream, and no execution order that a disassembler could recover.

What is stored is a pattern of connections and truth-table contents. The stored form is a sequence of bits with an ordering defined by the device’s architecture, and the file that carries those bits is a fuse map. Reading it is straightforward once access is granted. Understanding it is a different task, and the first step is knowing what is actually on the die.

Two storage families that behave differently

Lattice’s catalogue spans both major CPLD technologies, and the difference is not cosmetic. EEPROM-based devices store configuration as charge on floating gates, programmed and erased electrically, non-volatile by construction. Flash-based devices, in the more recent families, store configuration in flash cells and typically add a security bit whose behaviour is defined by a configuration block rather than a fuse.

Family type Configuration storage Reprogrammable in system Security mechanism
EEPROM-based Floating gate array Yes, via ISP Read inhibit; no separate security block
Flash-based Flash array Yes, via JTAG or ISP Security bit plus read inhibit
Older PAL/GAL EEPROM or fuse Sometimes, with Vpp Readback disable, often defeated by design

The third row is worth keeping in mind because a large fraction of legacy boards carry GAL-type parts, and their protection is the weakest of the three. It is also the row where the meaning of “firmware” is most obviously wrong: a 16V8 has no firmware, only a set of programmable AND-OR connections.

The array is a two-dimensional structure

A CPLD is organised as logic blocks, each containing macrocells, each macrocell containing an OR term and a flip-flop, fed by a programmable AND array. The bits being read specify, for each product term, which inputs are connected. They also specify how the macrocell’s output is routed and whether the flip-flop is registered or combinational.

Field in the stored pattern What it configures
Product term inputs Which signals are ANDed together
OR term selection Which product terms feed the sum
Macrocell mode Registered or combinatorial output
Polarity Whether the output is inverted
Feedback path Whether the cell’s output returns into the array
Pin assignment Which physical pin the cell drives

This is why reading the pattern produces something that looks nothing like a program. A byte in the pattern is not an opcode; it is a fragment of a connection list. The natural intermediate representation is a set of equations, and getting there requires knowing the device’s architecture in detail — which macrocell maps to which bits, in which order.

The UES, and the one part that is already text

Most Lattice devices include a user electronic signature: a small number of bytes in the same array that the designer can program freely. It is intended for version strings, board identifiers, or a checksum the designer computed. It is not protected differently from the rest of the array, so a successful read returns it along with everything else.

UES content What it tells you
Version string Which build this is, sometimes with a date
Board or product code Which product the design belongs to
Designer checksum Whether a candidate reconstruction is likely correct
Empty Nothing, and this is common

The third row is the most valuable when present. A designer who stored a checksum of their own source tree gives a free verification target: reconstruct the design, compile it, and see whether the checksum matches. It is rare, but it converts an open-ended interpretation problem into a checkable one.

What a read actually returns, byte for byte

The output of a read is a fuse map in a standard format, and the format is not the same as the file a designer would produce from the vendor tool. The two are related but the mapping involves device-specific ordering, and the number of bits in the file is the number of programmable elements, not a multiple of any convenient word size.

Property Value
Content One bit per programmable element, packed
Ordering Device architecture dependent
Header Device identifier and fuse count
Checksum Defined over the fuse bits, not over the file bytes
Padding To a defined length, with a defined fill value

The fourth row causes the most trouble in practice. A checksum computed over the file bytes rather than the fuse bits will disagree with the device’s own checksum even when the file is correct, and the disagreement gets blamed on a failed read.

Protection, and what it does to a read

The read inhibit on these devices does not encrypt anything. It suppresses the readback path, so the programming interface returns a fixed pattern or refuses the command. The array still contains the design. The consequence is that a protected device is a physical project, not a session project.

That said, the physical route on CPLDs is unusually favourable. The arrays are small, the geometry is relaxed compared with contemporary microcontrollers, and the configuration storage is a regular structure that images well. Extracting FPGA configuration describes the same three-route logic for the larger cousins of these parts, and the reasoning transfers. The classic method description for defeating a logic device’s protection is collected under CPLD crack.

From bits to something usable

Once the pattern is in hand, the work becomes interpretation, which is covered in reversing Lattice CPLD source code. The handoff point is worth stating precisely: reading produces a fuse map whose every bit is known and whose meaning requires architecture knowledge. Reading the JED format covers the container itself, including the fields that make a fuse map verifiable.

Services at this boundary

Work covers session readout where the inhibit is absent or removable, physical readout where it is not, UES extraction and interpretation, fuse map validation against the device checksum, and delivery of the pattern in a documented container. It does not cover producing original HDL from a fuse map, because that is a separate and inherently lossy interpretation step.

The expectation to set is that a read returns a configuration, not a design. Everything a customer wants after that is interpretation, and interpretation should be scoped separately.