PostHeaderIcon ATtiny2313: Self-Programming and the Half-Written Page It Leaves Behind

Most images have one author: whatever programmer wrote them at the factory or on the line. Some images have two, because the firmware can rewrite its own flash, and when that is the case the array is not a snapshot of a build — it is the current state of a process that may have stopped in the middle.

On a twenty-pin part with 2 KB of flash that distinction is unusually tractable. The array is only thirty-two pages. An interrupted self-update leaves evidence in one of them, and finding which one is a matter of arithmetic rather than archaeology.

The mechanism, in the order it happens

Self-programming on AVR is not a store instruction. It is a sequence of writes to a control register, each one triggering a hardware operation that runs to completion after the instruction retires:

  1. Erase the target page. The whole page goes to 0xFF, not just the bytes about to be written.
  2. Fill the page buffer, one word at a time, through a temporary register pair.
  3. Commit the buffer to the page in a single write.

Two properties follow from that sequence and both matter during a recovery. The first is that a page is the unit of destruction: rewriting four bytes costs the other sixty. The second is that the sequence has intermediate states, and a power loss between any two of them leaves a page that is neither old nor new.

interruption point page state what it looks like in the image
before the erase old contents intact nothing to see
after the erase, before the commit all 0xFF a hole in the middle of otherwise dense code
during the buffer fill, before the commit still all 0xFF same as above; the buffer is volatile
after the commit, before verification new contents, unverified valid code that may be truncated

The middle two rows are the reason a hole of erased bytes inside a region of dense code is worth investigating rather than dismissing. On a part that was programmed once and never updated, erased bytes appear only at the top of the array. Anywhere else, they are a scar.

Why 2 KB makes this cheap to analyse

Thirty-two pages of sixty-four bytes means the page map fits on one line. Walk the array page by page and classify each one as dense, sparse or blank, and the layout of the firmware becomes visible without reading a single instruction:

  • the vector table occupies the first page, and possibly part of the second;
  • initialisation and the main loop follow in dense pages;
  • string and table data tend to occupy pages with a regular repeating structure;
  • blank pages appear at the top, and anywhere else they are candidates for an interrupted write.

That classification takes minutes and it answers a question that otherwise gets answered badly: whether the image is a clean build or the survivor of a failed field update. The difference matters commercially, because the second case usually means the customer has a second unit that was updated successfully, and that unit is a better source than the damaged one.

The vector table as a fixed cost

Every interrupt source needs an entry, and on this family each entry is a two-word relative jump. The count of entries is set by the peripheral set: two timers, a USART with its separate receive, transmit and data-register-empty sources, an analogue comparator, an external interrupt pair, a pin-change group, a watchdog, and the EEPROM-ready source.

That works out to a table of roughly thirty to forty words, which is between one and two percent of a 2 KB array. Small in absolute terms, and large enough that it constrains what fits: a 2 KB part running a USART protocol stack has very little left for application code, and that is why images on this part are usually written in assembly with attention to every byte.

It also gives a verification tool. The table is at a known address, its entries are a known instruction form, and any deviation is either an unused entry left as a jump-to-self or the mark of a build that disabled an interrupt. Both are readable, and a table that does not decode at all means the image starts at the wrong base address.

The USART register writes as a chronology

Because the part is normally chosen for its serial port, almost every image contains a USART initialisation block: baud rate registers, frame format, enable bits. That block is a timestamp of sorts. The baud divisor is computed from the clock frequency, so the pair of constants written to the baud registers states the clock the build assumed, and comparing it against the board’s actual oscillator resolves the most common «restored image runs at the wrong speed» complaint.

The frame format register says something else: whether the design used the port for a simple debug channel, in which case the configuration is minimal and the transmit path is a blocking loop, or for a real protocol, in which case there are receive interrupts, buffers and state machines. The second kind of image is longer, denser and usually the one worth reconstructing in full.

What is delivered

The return set for a code-level recovery on this part is the flash image as Intel HEX, the EEPROM as a separate file, the fuse and lock state as read, a disassembly with the SPM sequence and the USART configuration annotated, and a page map marking any region that looks like an interrupted write.

The page map is the part of the deliverable that customers did not ask for and consistently use. It says whether the image in hand is the original build or the result of an update that stopped, and those two answers lead to different next steps.

Where the same package carries twice the flash, ATtiny4313 code recovery covers what changes when code is moved between them. The variant with revised silicon is discussed in ATtiny2313A heximal recovery, and the wider vector table of the next part up in ATtiny261 code recovery.

FAQ

Can self-programming be detected from the image alone? Yes. The control-register sequence is distinctive and appears in a small number of routines that are then callable from the update path.

Does an erased page always mean a failed update? No. It can also be an unused page inside a build, but on this part the linker packs from the bottom, so a hole in the middle deserves a second look.

Is the page buffer readable after a power loss? No. It is volatile, which is why an interrupted fill leaves the page erased rather than partially written.

Conclusion

A part that can rewrite its own flash produces images that record their own history. The page is the unit of destruction, the sequence has three steps with a gap between each, and a 2 KB array is small enough to classify page by page in a few minutes. That classification is the difference between reporting a build and reporting the state of an update that failed.

Comments are closed.