Posts Tagged ‘reverse engineering microcontroller locked file’

PostHeaderIcon 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:

  1. Signature and fuses. Confirms the die and whether the interface is even enabled in the build.
  2. Lock byte. Determines whether internal flash is readable at all.
  3. Internal array, complete, with page-level verification.
  4. Vector table and the first two hundred instructions. Establishes the shape: single-stage or loader.
  5. 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

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.

PostHeaderIcon ATtiny461: Relative Jumps Stop Reaching at Four Kilobytes

Instruction encoding is a budget. A relative jump on this architecture spends twelve bits on the offset, which reaches about two thousand words in each direction — four kilobytes of address space in total. That is fine for a small program and insufficient for one that grows past it.

The consequence is that a C compiler, once the image crosses the boundary, stops emitting relative jumps and starts emitting absolute ones. The generated code changes shape, and a disassembly read with a uniform assumption will look wrong in exactly the region where the program got big.

What the two forms look like

A relative jump is a single word: opcode plus offset. It is cheap, it is position-independent, and it cannot leave its window.

An absolute jump is two words: the opcode plus the target’s low bits, followed by the target’s high bits. It reaches anywhere in the address space and costs twice the space.

The distinction is visible in a dump without a symbol table, which is what makes it useful. A region full of single-word branches is a small, self-contained function. A region with two-word jumps is either a large function or a table of dispatch targets.

Form Size Reach Where it appears
Relative jump one word ±2K words Most branches, loops, small functions
Absolute jump two words full space Long branches, vector table, dispatch
Relative call one word ±2K words Ordinary function calls
Absolute call two words full space Calls across the boundary

Why this misleads a disassembly

Start reading at the wrong offset and everything after is nonsense — that is true everywhere. The subtler problem is starting at the right offset and misjudging instruction length.

If a two-word jump is decoded as a one-word branch, the second word is read as the next instruction. It usually decodes as something plausible, because most 16-bit values decode as some instruction on a dense architecture. The reader then follows a control flow that does not exist, and the reconstruction drifts.

The cure is to check the opcode, not to guess from context. Two-word forms are identifiable from the first word alone; there is no ambiguity at the instruction level, only in a reader who is pattern-matching.

What the boundary means for structure

A 4 KB image that uses absolute jumps throughout is telling you something about how it was built: the compiler targeted a larger part, or the linker was configured for one. That is not a defect, and it does mean the code is larger than the source would suggest.

Conversely, a 4 KB image full of relative jumps is dense and probably hand-optimised or compiled with size optimisation. The two produce very different disassembly experiences, and knowing which you have sets expectations for how much of the image will be library code.

Where the vector table fits

Vectors are jumps, and on this part the reset vector is an absolute jump because it must reach the start of the application regardless of where that is. The interrupt vectors follow at fixed spacing, and each is either a jump to a handler or a placeholder.

Because an absolute jump occupies two words, the vectors consume more space than a reader expecting one-word entries would assume. The spacing is fixed and documented, so the table is a reliable anchor: count entries at the documented interval and they will line up.

Reading the image

  1. Read signature, fuses, lock byte before the array. A part configured for a missing clock will not run.
  2. Read the array twice, page by page, comparing.
  3. Disassemble from address zero and confirm the reset vector decodes as an absolute jump.
  4. Walk the vector table at the documented spacing and confirm each entry lands inside the application region.
  5. Only then follow code, checking instruction width at every branch.

Step 5 is where patience pays. Every two-word instruction decoded as one introduces an error that propagates, and there is no checksum downstream to catch it.

Related notes

FAQ

Can a relative jump reach the whole 4 KB image? Not from every position. The range is ±2K words, which covers 4 KB only from the middle.

Does the compiler always switch to absolute jumps past the boundary? It does when the target is out of range. Short functions can still use relative forms.

Is a two-word jump easy to confuse with two instructions? Only if the opcode is not checked. It is unambiguous at the instruction level.

Does this matter for a 2 KB part? Less. The whole image fits inside one relative window, so the mixed encoding rarely appears.

Conclusion

Instruction width is a structural property of an image, and on a part at the boundary between small and medium it varies within the same program. Checking the opcode rather than assuming a width is what keeps a reconstruction from drifting, and the vector table is the anchor that confirms the reading started in the right place.