Posts Tagged ‘reverse engineering microcontroller locked bin’
ATmega640: When Part of the Program Is Not On the Chip
Most microcontrollers keep everything inside the package. This one does not. The external memory interface adds a bus that maps an address window to a device outside the chip — an SRAM, a parallel flash, a latch — and code can execute from that window.
That single feature breaks the assumption that a dump of the internal array is a dump of the program. It can be a complete program. It can be the first stage of a two-stage design. It can be a loader that copies everything else out of an external chip at boot and never returns to internal flash again.
What the interface actually adds
The bus exposes the usual signals: address lines, data lines, read and write strobes, and chip-select outputs. Access is controlled by registers that set the wait states for each of three regions, and the regions can be configured independently — one for external RAM, one for external peripherals, one for lower addresses where code may reside.
Because the regions are configurable, the same physical chip can be wired for data only, or for both code and data. The configuration lives in registers written at start-up, which means it is not a fuse setting and not visible from outside. Only the firmware knows what it configured.
That is the crux of the examination problem: the mapping that tells you where the program is, is itself inside the program.
Three shapes a device can take
Single-stage. All code in internal flash, external memory used purely as data RAM. A dump of internal flash is the complete program. This is the common case, and it is also the case where the interface has no effect on the recovery question at all.
Two-stage with an external code region. A small internal loader initialises the bus, then jumps into the external window. The internal image is real code and worth reading, but it is a fraction of the product’s logic — typically a bootloader plus a copy routine, and often the copy routine is the most informative thing on the chip because it reveals the external device’s geometry.
Data-only with structure. All code internal, but the external RAM holds tables that the code expects to find at known addresses. Recovering flash alone gives you an algorithm with missing constants; the device will not run and the disassembly will contain references to addresses that were never in the image.
| Shape | Internal image contains | External device holds |
|---|---|---|
| Single-stage | the whole program | data only |
| Two-stage | loader plus copy routine | the real code |
| Data-only | all code, missing constants | tables and buffers |
Distinguishing the three requires looking at the vector table and the start-up path. A reset vector that branches immediately to an address outside the internal range is the signature of the second shape. A start-up that configures bus registers before doing anything else is a hint at the third.
Reading the bus configuration out of the image
The configuration registers are written early, before any peripheral initialisation, because the stack may live in external RAM. In a disassembly that means the first few dozen instructions after the vector table are the highest-value region on the whole chip: they name the wait states, the region boundaries, and the chip-select assignment.
From that short block you can reconstruct the wiring without touching the board — which window the external device occupies, how many wait states were needed, and therefore roughly how fast the external part is. That is a lot of information from twenty instructions.
Why a partial image still has value
If the internal array holds only a loader, the honest description of the result is “a loader, not a program”. That sounds like a failure and usually is not, because the loader describes the rest of the system:
- The geometry of the external device: page size, address pins, command sequence.
- The verification method: whether a checksum or a signature is checked before the jump.
- The failure path: what happens when the external part does not respond.
Those three answer most of the questions a customer actually has — what protects the design, whether the external part is a standard component, and whether the product can be made to run a modified image.
What the protection bits do not cover
Lock bits protect the internal flash array. The external memory chip is a separate component with its own access rules — usually none at all, since parallel SRAM and flash have no notion of protection.
That asymmetry is worth stating plainly: on a two-stage design, the strong protection may be on the small internal loader while the bulk of the intellectual property sits on the bus in an unprotected part. Whether that is true for a given product is a board-level question, answered by looking at what is soldered next to the microcontroller.
Reading order
The sequence that produces a usable answer rather than a large file:
- Signature and fuses. Confirms the die and whether the interface is even enabled in the build.
- Lock byte. Determines whether internal flash is readable at all.
- Internal array, complete, with page-level verification.
- Vector table and the first two hundred instructions. Establishes the shape: single-stage or loader.
- If loader-shaped, extract the bus configuration and the external geometry from that block.
Steps 4 and 5 are cheap and they determine whether the remaining work is a disassembly job or a board-level job. Doing them before committing to either saves the more expensive mistake.
Related notes
- ATmega1280PV and the three-byte program counter — how a larger part changes what addresses mean.
- ATmega2561V and EESAVE — the variant with no external bus at all.
- ATmega644 and page buffers — the flash mechanics this part shares.
FAQ
Does the external interface have to be used? No. It is optional and many builds ignore it. The interface being present on the die says nothing about whether it is wired on the board.
Can lock bits hide the external memory? No. They apply to the internal array. The external component is a separate device with its own, usually absent, protection.
Is the bus configuration stored in a fuse? No. It is register state written by firmware at start-up, so it is only recoverable from the image or by observing the bus.
Does a partial internal image mean the read failed? No. It means the design is two-stage. The read succeeded; the program is simply distributed.
Conclusion
On a part with an external memory interface, “the program” is a property of the board, not only of the die. The internal image may be the whole thing, a loader, or a fragment that expects constants elsewhere. The vector table and the first block of start-up code tell you which, and they are the first thing worth reading after the lock byte.