Posts Tagged ‘read cpld chip protected software’

PostHeaderIcon Read CPLD JED: Inside the Format Everyone Uses and Nobody Reads

The JEDEC fuse map format is one of the most widely used and least examined file formats in electronics. Every programmable logic programmer reads it, every vendor tool writes it, and almost nobody who handles the files knows what the fields mean. That is usually fine, because tools handle the details. It stops being fine when a read has to be verified, a file has to be repaired, or a mismatch has to be explained.

This page is about the container, not about the design inside it. Understanding the container is what makes verification possible, and verification is what separates a read from a guess.

The six fields that matter

A JED file is a sequence of fields, each introduced by a single letter, terminated by an asterisk. Between the start marker and the end marker there is nothing but these fields.

Field Meaning Typical content
STX Start of transmission A control character
QF Fuse count Total programmable elements
F Default fuse state 0 or 1
L Fuse list Address, then a run of 0s and 1s
C Checksum Four hex digits
ETX End of transmission A control character

The F field is the one that surprises people. It declares the state of every fuse not mentioned in an L field, which means a file can be complete while listing only the fuses that differ from the default. A file that lists nothing and declares F1 describes a device with every fuse set. Whether that is a valid design depends entirely on whether the device’s unprogrammed state is all ones.

The L field and its addressing

Each L field begins with a decimal address, followed by a run of bits. The address is a fuse index, not a byte offset, and the bits that follow are consecutive fuses starting at that index. The run ends at the next L, C, or ETX.

Field fragment Reading
L0 followed by 64 bits Fuses 0 through 63 set to the listed values
L64 followed by 8 bits Fuses 64 through 71
L1000 followed by 1 bit Fuse 1000 only

Because the address is a fuse index rather than a byte address, the number of bits in a run has no relationship to any word boundary. A run can be any length, and a file can describe the array in any number of runs. Tools generally write one run per line for readability, but the format does not require it.

The checksum, and why file-level checks fail

The C field is a four-digit hexadecimal value computed over the fuse bits, not over the characters in the file. This is the detail that causes the most confusion, because a SHA or CRC computed over the file bytes will not match it, and the mismatch looks like corruption.

The algorithm is a 16-bit sum. Every fuse that is set contributes its index plus one, with the index counted from one rather than zero, and the sum is taken modulo 65536. Fuses that are clear contribute nothing.

Fuse index (0-based) Set? Contribution
0 Yes 1
1 No 0
2 Yes 3
3 Yes 4
Total — 8

Two properties follow, and both are useful. First, the checksum is order-independent, so reordering the L fields does not change it. Second, it is weak: a fuse set at index n and a fuse set at index n-1 can produce the same sum in some combinations, so a matching checksum is evidence of consistency rather than proof of correctness.

What a checksum mismatch actually means

Mismatch pattern Likely cause
Computed value differs by a small amount A few fuses differ from what the checksum was computed over
Computed value differs wildly Wrong fuse count, or the F default was misread
Checksum absent Tool wrote a file without one; verification impossible
Checksum matches but the device misbehaves The file is consistent with itself but not the right design

The second row catches a specific bug that appears in home-made parsers: if F is not honoured, every fuse not explicitly listed is treated as zero, and the reconstruction silently differs from the file’s intent. The error scales with how much of the array the file omits, which is why sparse files fail hardest.

Reading a JED back into a device state

Reconstructing the full array means allocating a vector of QF bits, filling it with the F value, then overwriting the positions named by each L field. The result is the complete configuration, and it should then be checked against C.

Step Failure mode if skipped
Read QF Array length wrong; all later offsets wrong
Initialise from F Sparse files reconstruct as mostly empty
Apply L fields at fuse indices Bit order and position wrong
Compute checksum over fuse bits Cannot distinguish a good read from a truncated one

The third step has a subtlety worth naming: the bits in an L run are in the order they appear, and the first bit after the address corresponds to the address fuse. Reversing the run produces a mirrored design that decodes into equations and behaves wrongly, and the error is easy to make in a parser written from scratch.

Where the container fits in the workflow

The JED file is the transport for a fuse map, and the fuse map is what reading Lattice CPLD embedded firmware produces. Decoding the map into equations is the next step and is covered in reversing Lattice CPLD source code. On the older PAL and GAL parts the container is the same and the array is smaller, which is treated in reading PLD binary source code.

Services at this boundary

Work covers JED parsing and validation, checksum recomputation over fuse bits, repair of files with missing or inconsistent headers, conversion between container variants, and explanation of mismatches. It does not cover making a truncated file complete, because the missing bits are not recoverable from the container.

The practical takeaway is that a JED file carries its own consistency check, and using it costs nothing. A read that was not checked against its own checksum is a read whose quality is unknown.