Posts Tagged ‘recover chip embedded program’
ATmega261A: Two Kilobytes Leaves No Room to Hide
Memory size shapes code the way plot size shapes a drawing. Given two kilobytes, a compiler cannot afford abstraction: no layered driver stack, no generic serial framework, no floating-point library unless it is used. What remains is direct register access and inline logic.
That is a gift to anyone reading a recovered image, because a small program has few enough constructs that every one of them can be accounted for.
What disappears at this size
Three things that dominate larger images are absent or minimal:
Library code. A serial implementation becomes a loop that shifts bits, not a driver with a buffer and a state machine. A delay becomes a counted loop, not a call into a timebase.
Indirection. Function pointers exist but are rare, because a dispatch table costs bytes and the compiler can usually resolve calls directly.
Floating point. Fixed-point arithmetic replaces it almost always, which means the constants in the image are scaled integers and the scaling factor is a design decision stored in the code.
That third point is worth dwelling on. A fixed-point design has a scale factor, and it appears as a shift or a multiply by a round number. Finding it is usually the key to interpreting every constant in the program.
What the image looks like
| Region | Size at this scale | Character |
|---|---|---|
| Vector table | small, fixed | Jumps, mostly to a common handler |
| Initialisation | tens of instructions | Port directions, clock, peripherals |
| Main loop | the bulk | Register access, comparisons, delays |
| Constants | a handful of words | Scaled integers, thresholds |
The main loop is where the product is. On a device this small there is usually one loop, and its structure is the design: read an input, compare, act, wait.
Why the entry point is decisive
With so little code, the initialisation sequence is proportionally large, and it states the design’s assumptions: which pins are outputs, which have pull-ups, whether the watchdog is enabled, which clock source is selected.
Reading it first gives a complete description of the hardware interface before any logic is examined. That ordering matters, because the logic is meaningless without knowing what it is talking to.
Reading order
- Signature, fuses, lock byte. Clock and interface configuration first.
- Array read, verified page by page.
- Decode the reset vector, then read initialisation in full.
- Map the port configuration: direction, pull-ups, initial output states.
- Read the main loop, noting every register write as a hardware action.
Step 4 is the description a customer usually wants, and it is obtainable in minutes at this size. Step 5 explains the behaviour that goes with it.
The protection, unchanged
Two lock bits, four states, no boot section. The array is readable or it is not; the only operation that changes the answer erases it.
At two kilobytes the cost of that erasure is small in absolute terms and total in practical ones, because there is no redundancy: nothing else holds the program. That is worth stating plainly before any erase is considered.
Related notes
- ATmega261 and differential comparison — reading two images against each other.
- ATmega861 and code composition — the same family at four times the size.
- ATtiny88A and the entry point — the same first-instruction discipline.
FAQ
Is two kilobytes enough for a real product? For a single-function device, yes. These parts ship in volume.
Why is there no floating point? Because the library would consume most of the array. Fixed-point is the standard choice.
Can the whole image be read exhaustively? Yes, and at this size that is the normal approach rather than an aspiration.
Does the small array affect the lock bits? No. The mechanism is identical to larger members.
Conclusion
Two kilobytes is a constraint that produces legible code. Library layers vanish, indirection is rare, and constants are scaled integers with a visible factor. The result is an image that can be accounted for in full — which makes the conclusions drawn from it certain rather than probable.