Posts Tagged ‘reverse engineering chip memory’
PIC12C508 Code: The Calibration Word That Lives in the Last Address
A PIC12C508 holds 512 twelve-bit words and 511 of them are program. The last one is a measurement. Microchip trimmed each die’s internal oscillator at final test and stored the result at address 0x1FF, where the reset code is expected to fetch it and write it into OSCCAL. That word is why a code read from this part is not finished when the last instruction has been decoded.

What actually sits at the top of the array
The array is one-time programmable, organised as 512 words of twelve bits, and the programmer’s address counter runs to 0x1FF like any other location. Nothing marks the top word as special in the silicon. What makes it special is the convention: the assembler template places a movlw of that value at the reset vector, and the first few instructions of every build move it into the OSCCAL register before anything else happens. Skip the word and the code still assembles, still links at the right address, and still runs. It simply runs at whatever frequency the erased or default trim produces, which on this family is a spread wide enough to break any timing that matters.
Why a dump without it still looks complete
A read that stops at 0x1FE produces a file of the right length for 511 words and a checksum that is internally consistent, because the checksum was computed over the same shortened range. This is the trap: nothing in the file complains. The failure appears later, on the second device, as a serial port that returns framing errors at one baud rate and works at another, or as a watchdog timeout that fires a few percent early. Engineers then spend a day on the UART configuration, which is where the original firmware spent its own time, and never look at the top of the array.
What the protection bit does to that one word
The code-protect bit is in the configuration word, which on a twelve-bit core is not reachable by the program counter and is therefore addressed by a separate programming command. When it is set, reads of the array return zero, which decodes as a no-operation, across the whole range including 0x1FF. There is no exception carved out for the calibration word. That is worth stating plainly because customers often assume the trim value is “factory data” and therefore readable; on this part it is ordinary array content and it disappears with everything else.
Three routes, and what each returns
| Route | What it reaches | What it costs |
|---|---|---|
| In-circuit read, protection clear | Full array including 0x1FF | Minutes, no risk |
| In-circuit read, protection set | 0x000 everywhere | Nothing useful |
| Optical read of the array | Every cell, including the trim word | Decapsulation, days |
The third row is the one that answers the question, and it is also the one that recovers the trim value rather than inferring it. Where the device is still alive and the protection is clear, the first row is enough and the whole job is a file transfer. The middle row is what most enquiries are actually sitting on.
When the word cannot be recovered
If the die is damaged, or the top word was never programmed because the design used an external crystal, there is a fallback that does not require the original value: calibrate the replacement. Program the image with any legal trim, run the device against a reference clock, and sweep OSCCAL until the output frequency matches. It takes an afternoon and a frequency counter, and it produces a unit that behaves correctly without pretending to be a bit copy. The distinction between a bit copy and a calibrated rebuild is the same one that separates the four objects in copying a microcontroller.
The question to ask before quoting
Ask what the second unit has to do. If it has to talk to the same bus at the same rate, the trim word is part of the deliverable and the route has to reach address 0x1FF. If it only has to demonstrate the algorithm on a bench, a default trim will do and the job is smaller. The sibling part PIC12C509 doubles the array and keeps the same convention at its own top address, and the low-voltage variant covered in PIC12LC508 moves the trim spread wider still. Format and verification of the resulting image are handled as described in extracting MCU code.
Duplicate Locked GD32F305VGT6 Program: 1 MB Copy
The GD32F305VGT6 is the 1 MB flash member of the line in the LQFP100 body, and the parts we are asked to duplicate locked GD32F305VGT6 program from are typically end products that must be copied exactly — same behaviour, same lock state, no interest in the source. Duplication is the simplest of the recovery family: read the whole array, write it to a fresh part, confirm it runs. The difficulty is only in doing the read on a locked 1 MB device without loss.

Reading the full 1 MB image
A duplication begins with the same fault-injection opening used across the family, applied to the largest array in the range. The capture is broken into many checksummed blocks so a single dropped word is caught and re-read; at 1 MB the discipline is identical to the smaller parts, only applied more often. The image that comes off the bench is whole or it is not shipped, because a duplicate with a corrupted block is a duplicate that fails in the field. Because the VGT6 is the top of the flash range, the capture also exercises the addressing past the 512 KB boundary that the smaller parts never reach, which is one more reason the block checksums matter more, not less, at this size. The memory attack that opens the VET6 is the same opening scaled to this array.
Carrying the option bytes
The most overlooked part of a faithful duplicate is the option bytes, which hold the SPC level and the boot settings. If those are captured with the array, the image written to a blank VGT6 boots and locks exactly as the original did. If they are dropped, the copy may run but will not carry the original protection, which defeats a duplication that was supposed to be exact. We return the option bytes explicitly and write them with the array so the clone is bit-for-bit the same device.
Writing to a blank part
A blank VGT6 takes the captured array through the same programming path any firmware uses, with the option bytes written last so the lock engages only after the code is in place. Writing the option bytes last is not a detail: if the SPC level were set before the array was complete the part would lock with a partial image and the duplicate would be useless, so the sequence is that the lock engages only on a whole, verified array. The 1 MB write is long but routine; what is not routine is the verification that follows. The copy is read back and compared block by block against the original capture, and only when they agree is the duplicate accepted. A clone rebuilt from source is judged by whether it runs; a duplicate is judged by whether it matches.
Why a duplicate is not a clone
A duplicate and a clone are often confused, and the difference decides the method. A clone rebuilds the source and recompiles, which is the route when the goal is to understand or modify the firmware; a duplicate copies the image bit for bit, which is the route when the goal is simply to have another part that behaves identically. The VGT6 duplicate asks for neither understanding nor modification, only fidelity, and that is why its verification is a comparison rather than a build. The rebuilt clone of the 1 MB RGT6 is the other end of that choice.
Why the VGT6 size matters
Room for an RTOS, a USB stack, an application and a data region means the duplicate must carry all of them verbatim, and a data region — calibration, fonts, a parameter set — must not be mistaken for code and altered. The larger array also means the verification pass is longer, but it is the same pass that proves the smaller parts. The 256 KB VCT6 decapsulation is the route when this part will not open electrically; once it does, duplication is the easy half.
| Duplicate step | Action | Proof |
|---|---|---|
| Read | block-checksummed 1 MB | whole capture |
| Write | array then option bytes | lock engages last |
| Verify | read back vs original | bit-for-bit match |
| Boot | run on blank part | same behaviour |
How the duplicate is returned
The deliverable is the working duplicate part plus the original binary and option bytes, so you hold both the copy and the record of what it contains. The image itself, if you prefer to program the copies yourself, is the same file used for the flash retrieval of the smaller parts — only the array is larger.