Posts Tagged ‘recover mcu encrypted binary’
ATmega168PV: The Calibration Byte You Need Before Reset
Imagine a recovered image that is byte-perfect and still useless. It happens, and the usual cause is not the flash at all — it is a single byte stored somewhere else, written by the factory, that the firmware reads at start-up to trim its own clock.
On the ATmega168PV, as across the megaAVR line, the internal RC oscillator is the default clock source. It is cheap, needs no external components, and is accurate enough for a UART only because each die is individually calibrated at test and the result is stored in the signature row. Lose that byte and the oscillator reverts to its untrimmed centre frequency, which is several percent off — plenty to break a serial link and enough to break anything that measures time.
Where the byte lives
The signature area is a small separate address space, not part of the flash array. It holds three things of interest:
| Item | Purpose | Readable when locked |
|---|---|---|
| Device signature, three bytes | Identifies vendor, family, model | yes |
| RC calibration byte | Trims the internal oscillator | usually yes |
| Lock and fuse readback | Reports current protection state | yes |
The second row is the one that matters here. Because the signature area is not the flash, the lock bits that disable flash reads do not disable these reads — the part still has to identify itself to a production tester, and the calibration value has to be available to any tool that reprograms a blank device.
The practical consequence is unusually favourable: on an otherwise unreadable ATmega168PV, you can still recover the calibration byte, the signature, and the fuse state. Three reads, no erase, no invasion.
Why the firmware cannot just hard-code a value
A recovered image that ran on one unit is not automatically correct on another. Each die’s calibration value is measured individually; two parts from the same wafer do not share it. If the firmware reads the calibration byte at run time — which is what the vendor’s own start-up code does — then the image carries no frequency constant of its own and will behave correctly on any part, provided the signature row on the new part still holds its factory value.
The problem appears in the other direction. Some recovered images contain a hard-coded trim value, because a developer measured one unit and baked the number in. Such an image works on the original die and drifts on every other. When a rebuilt device shows a baud rate that is close but not exact, this is the first thing to check.
What else the start-up code reads
The calibration byte is not alone. A typical start-up sequence on this family reads, before main gets control:
- The reset cause, to distinguish power-on from watchdog from brown-out.
- The MCU control register state left by the bootloader, if there is one.
- On parts with a boot section, whether the reset vector was redirected.
None of that is stored in flash in any obvious place. It is register state, and it is rebuilt at every reset. It does not need to be recovered — but it does need to be understood, because an image that looks complete can behave differently depending on which reset path it came through.
The voltage grade and the oscillator
The V in ATmega168PV marks a low-voltage grade. The oscillator’s accuracy is specified over a supply range, and that range is narrower than the range on the non-V part. Two practical consequences follow.
First, calibration performed at a different supply voltage than the part will run at is not necessarily transferable. The factory calibrates under specified conditions; a bench measurement taken at a different voltage can differ, and chasing that difference leads nowhere useful.
Second, the trim value is temperature-sensitive in a way the byte cannot capture. The single stored byte compensates at one operating point. Firmware that needs better than that either uses an external crystal or re-trims against a reference at run time. If a recovered product uses the internal oscillator for anything with a strict tolerance, there is probably a software trim loop somewhere in the image — and finding it is often the point of reading the code at all.
Reading the byte reliably
The signature row is read with the load-program-memory instruction pointed at the signature address space, over whichever programming interface is open. Two cautions apply.
The read is not affected by the flash lock bits, but it is affected by the interface being disabled. If SPIEN is unprogrammed or RSTDISBL is set, there is no serial path to the signature row, and the parallel interface is the only way in. That is a wiring problem, not a protection problem, and it is worth establishing before concluding anything about the device.
The read is also affected by the target clock. The interface samples on the target’s clock edges; a target running from an untrimmed RC at a low voltage answers slowly. Reading at a slow SCK is the reliable choice and costs seconds.
Why this matters for a rebuild
When a device is rebuilt around a recovered image, the new blank part has its own factory calibration byte. The image reads it at run time and adapts. That is the clean case.
The unclean case is an image that was pulled from a locked part by way of an erase, then restored from a source that had already lost the calibration data. The symptom is subtle: the device boots, runs, and communicates at the wrong rate, or its timekeeping drifts. Nothing in the flash looks wrong. The bug is in a byte that was never in the flash.
Related notes
- ATmega8A and the four lock states — which states still answer a signature read.
- ATmega324V and the low-voltage grade — the same V suffix from the supply side.
- ATmega162 and paged program memory — what “complete image” means on a larger part.
FAQ
Can the calibration byte be read from a locked ATmega168PV? In most cases yes. It sits in the signature area, which is outside the flash array and outside the lock bits’ scope. It is still gated by whichever programming interface is enabled.
Does a recovered image need the original calibration value? Only if the firmware hard-codes it. If the firmware reads the signature row at start-up, the value travels with the new part and the image needs nothing extra.
Is the byte the same across a production batch? No. It is measured per die. Parts from the same batch differ, usually by a small amount and occasionally by enough to matter.
Can a wrong calibration byte damage the part? No. It changes the clock frequency, which affects timing behaviour, not the silicon.
Conclusion
The distinction between “the flash” and “the device” is where most recovery assumptions break. The ATmega168PV stores its identity and its oscillator trim outside the flash, in an area the lock bits do not cover, and the firmware depends on both at every reset. Reading three items that cost nothing — signature, calibration, fuses — frequently answers the question before the flash is even a topic.