Posts Tagged ‘recover mcu protected content’

PostHeaderIcon ATmega644PA: When RSTDISBL Removes Your Way In

A device that ignores the programmer is usually diagnosed as dead or locked. Both conclusions are premature. The most common cause on an AVR is a fuse that removed the pin the programmer needs, and the chip is behaving exactly as configured.

The fuse is RSTDISBL, reset disable. When it is programmed, the reset pin stops being a reset pin and becomes a general-purpose I/O. The serial programming interface enters the chip through that pin, holding it low to select programming mode. With the pin reassigned, there is nothing to hold and no mode to enter.

Why a designer would do that

Because pins are scarce. A 40-pin part with a dozen peripherals competing for I/O will trade a reset line for one more usable pin, especially in a product that never needs field reflashing. It is a legitimate, common optimisation — and it is invisible from the outside until someone tries to program the part.

The same logic applies to two neighbouring fuses. The debug-wire enable fuse assigns the reset pin to the on-chip debug interface, which also excludes ISP. The self-programming enable fuse, in the opposite direction, allows the boot section to write flash — which is a different question, but shows up in the same fuse byte.

Fuse What it does Effect on serial programming
RSTDISBL programmed Reset pin becomes I/O ISP cannot be entered
DWEN enabled Reset pin owned by debug interface ISP cannot be entered
SPIEN unprogrammed Serial programming disabled ISP cannot be entered
EESAVE programmed EEPROM preserved through chip erase no effect on entry

Three different fuses, one identical symptom: the programmer reports no device. Separating them requires reading the fuse bytes, and reading the fuse bytes requires an interface — which is the circular problem the fuse created.

The way back in

There is one: high-voltage parallel programming. That mode does not use the reset pin as a selection line in the same way; it applies twelve volts to the reset pin and drives the part through a parallel command interface. It works regardless of RSTDISBL, which is precisely why the fuse is called “disable reset” and not “disable access”.

So a part with RSTDISBL set is not closed. It is closed to serial access. The parallel interface still reads the signature, still reads the fuses, still reads the lock byte — and still respects the lock bits, which is the important caveat.

Two practical consequences:

  • Recovering the part means a different adapter, a different socket, and a twelve-volt supply. The procedure is documented and the hardware exists, but it is a distinct workflow, not a setting change.
  • The parallel interface does not bypass protection. If the lock bits are in mode 2 or 3, entering in parallel mode still does not permit a flash read. It permits a chip erase, which clears the locks and the contents together.

That combination — RSTDISBL plus locked flash — is the genuinely hard case, and it is the configuration a careful designer would choose deliberately.

Why “no device” is not a diagnosis

A programmer that cannot enter programming mode reports the same error for four different conditions: no power, wrong pinout, RSTDISBL or DWEN, and SPIEN disabled. The error message does not distinguish them, and the natural next step — assume the part is dead — closes the investigation too early.

The distinguishing test is voltage. Measure the supply at the socket, then measure the reset pin while the programmer asserts it. A part in ISP mode holds that pin near ground when the programmer drives it; if the pin does not follow, either the fuse reassigned it or the die is not responding. That single measurement separates a configuration problem from a hardware failure, and it takes less time than a retry.

Reading a device that will only answer in parallel

Once in parallel mode, the working order is the same as anywhere else: signature, fuses, lock byte, then memory. The difference is that each of those is now a parallel command rather than a serial one, and the timing is different — the parallel interface is much faster, which is convenient for a full read and unforgiving of a badly made adapter.

A useful habit: read the fuse bytes twice, before and after any operation that might have changed them. Fuses do not change spontaneously, so a difference means the read was wrong, not that the part changed. That check catches marginal contact on the socket, which is by far the most common source of nonsense from a parallel adapter.

What the design choice tells you

A product with RSTDISBL set and the general lock bits clear is a product designed by someone who did not think about protection at all — they were optimising pin count. Those are the easy ones: parallel mode in, fuses read, memory read, done.

A product with RSTDISBL set and the lock bits in mode 2 is a different proposition. Someone thought about it. The configuration says: no serial access, no readback, and the only way to change anything is to erase the whole thing. Recovering anything from that part requires a route around the lock bits themselves, not around the fuse, and the fuse is a distraction from the real obstacle.

Knowing which of the two you are facing changes the estimate by an order of magnitude, and it is established by one fuse read.

Related notes

FAQ

Can RSTDISBL be undone over ISP? No. ISP is what the fuse disables. The fuse can only be changed through the parallel interface.

Does RSTDISBL erase the flash? No. It changes a pin’s function. The program memory is untouched and keeps running.

Is a part with RSTDISBL set unusable? No. It is unusable over serial. The parallel interface still works and still reads the fuse and lock bytes.

Can the parallel interface read locked flash? No. It respects the lock bits exactly as the serial interface does. It can always erase, and erasing clears the lock and the contents together.

Conclusion

RSTDISBL is a pin-count decision that reads as a hardware failure. It removes serial access without touching the memory, and it is diagnosed by one voltage measurement and one fuse read rather than by retrying the programmer. The important question is not whether the fuse is set, but what the lock byte says next to it — because that determines whether the part is merely inconvenient or genuinely closed.

PostHeaderIcon ATmega644: Page Buffers, RWW, and Why Dumps Come Out Ragged

Flash memory does not accept single bytes. It is erased in blocks and written in pages, and between the two sits a temporary buffer that holds the page while it is being assembled. Every AVR in this family works this way, and the arrangement leaves fingerprints in real device dumps that are routinely mistaken for corruption.

Knowing the mechanism makes those fingerprints readable. It also explains a class of failure that has nothing to do with protection: a device that was interrupted mid-update, and still runs, but from a program that is no longer what the vendor shipped.

The write sequence, in order

A page write on this family is a four-step procedure executed by SPM from the boot section:

  1. Erase the page. The whole page becomes FF.
  2. Fill the page buffer, one word at a time, by executing SPM with the buffered-write bit set.
  3. Commit: execute SPM with the write-page bit set, which transfers the buffer to the array.
  4. Wait for the operation to complete, polling or by interrupt.

Two properties follow from that order. The buffer is volatile — if power is lost before step 3, the buffer contents are gone and the page stays blank. And the erase in step 1 precedes the fill, so a commit that happens with a partially filled buffer writes a page that is part real data and part FF.

That second case is exactly what an interrupted firmware update produces. It is not corruption in the sense of damaged cells; it is a page that was committed before it was fully assembled.

Why half the flash is read-while-write and half is not

The array is split into two sections. The read-while-write section can be read by the CPU while a page in it is being written. The no-read-while-write section cannot — while a page there is being erased or written, the CPU stalls.

Section During a page write Typical contents
RWW CPU keeps executing from it Application code
NRWW CPU halted until the operation finishes Bootloader, interrupt vectors

The split exists so that a bootloader living in the NRWW section can service the interface while it writes the application — but the bootloader itself cannot run from the section it is writing. Anyone who has watched an AVR update stall for tens of milliseconds per page has seen the NRWW boundary in action.

For a read attempt, the practical consequence is simpler: reading is a separate operation from writing, and a page write in progress does not block a read through the programming interface. There is no need to wait for an erase to finish before reading a different page, though it is polite to.

Where the ragged dumps come from

Three distinct patterns show up in real dumps and each has a different cause.

A page of FF in the middle of code. The page was erased and never committed, or committed empty. In a device that has been through a failed update, this is a normal artifact.

A page that is half code and half FF. The commit happened with a partially filled buffer. The boundary within the page tells you how many words were buffered before the interruption — which is, incidentally, a fairly precise indicator of how far the update got.

A single word that differs from its neighbours in a repeating pattern. Usually not flash at all, but a read taken at the wrong voltage or too fast, sampled at the point where the sense amplifiers are marginal. Reading the page again at a lower clock rate either reproduces it exactly or makes it vanish.

The third pattern is the one worth checking first, because it costs nothing to re-read and because acting on a misread image wastes far more time than the re-read.

Interrupts and the read-while-write trap

On a part running from flash, an interrupt vector fetch during an NRWW write stalls the core. Firmware that writes the application section from the boot section therefore has to either disable interrupts or accept the stall. Both are common, and both leave a trace: a device that drops characters or misses a deadline during a field update is showing the stall.

This is not a recovery problem, but it is a diagnostic one. If a customer reports that a device “sometimes bricks during update”, the mechanism above is a better explanation than flash wear, and the fix is in the bootloader, not the memory.

Reading order that respects the mechanism

Because page boundaries are the unit of everything, a read is best planned page by page rather than as one long burst:

  • Read the page, then read it again. Two identical reads establish that the interface is stable; a difference means voltage or clock, not memory.
  • Record the number of non-FF words per page. Pages that are entirely FF are candidates for “never written” or “erased”; pages with a partial tail are candidates for “interrupted”.
  • Compare the pattern against the vector table. The first page holds the reset vector and the interrupt table; if it is FF, the device cannot be running the code you think it is.

That last check resolves a surprising number of contradictions. A part that appears to hold an application but power-cycles into the bootloader is usually a part whose application start is elsewhere in the address space, with the vector table redirected by a fuse rather than by the code.

Related notes

FAQ

Can a single byte be changed in flash? Not without erasing and rewriting its whole page. The buffer supports partial fill, but the array operation is per page.

Does a stalled CPU mean the flash is busy? It means the CPU is fetching from the NRWW section while that section is being written. The array is busy; the reading interface is not.

Is a page of FF a sign of damage? Usually not. It is a sign of an erased page that was never committed, which is what a normal erase without a following write leaves behind.

Can the page size be changed? No. It is fixed by the die and documented in the programming specification. It is the same for every member of the family with the same flash size.

Conclusion

Page granularity is the organising fact of AVR flash. Erase-then-fill-then-commit explains the partial pages, the FF blocks, and the update stalls, and the read-while-write split explains why a bootloader can update the application but not itself. Once those two mechanisms are clear, a ragged dump stops looking like damage and starts reading like a history.