Posts Tagged ‘smartcard flash recovery’

PostHeaderIcon Interrupted Erase: Why Half-Erased Is Usually Recoverable

A device that lost power during a firmware update is the most common recovery case we receive, and the most commonly misdiagnosed. The owner reports a dead unit and assumes the flash is empty. In most cases the flash is not empty; it is a mixture, and the mixture is what makes recovery possible.

The reason is that erase is a physical process with duration, not a command with an instantaneous effect.

What an erase does over time

Erasing a block means removing charge from every floating gate in it, and the mechanism is a high field applied across the tunnel oxide for a period measured in milliseconds. During that period the threshold voltages of the cells fall together from the programmed distribution toward the erased distribution.

The fall is not instantaneous and it is not simultaneous across the block. Cells differ slightly in geometry and coupling, so the threshold distribution widens before it moves. If the field is removed halfway through, the block is left with a distribution that is neither programmed nor erased, and the width of that distribution is the reason the block is still readable in principle.

Elapsed fraction Cell state Readable
0 Programmed, intact Yes, normally
0.3 Distribution widened, means shifted Yes, with margin
0.6 Means between states Ambiguous per cell
0.9 Near erased, residual charge Usually reads as erased
1.0 Erased Nothing left

Why block granularity helps

The more important protection is that erase commands operate on blocks, and a firmware update erases many blocks in sequence.

The erase pointer moves monotonically through the address space. Blocks below the interruption point are fully erased and hold nothing. Blocks above it were never touched and hold everything, bit for bit, exactly as they were before the update started. Only the block being erased at the moment of the interruption is in the intermediate state.

Region State Contents
Below the erase pointer Erased Gone
The block at the pointer Partial Ambiguous, often partly readable
Above the erase pointer Untouched Intact

This is why the recoverable fraction is usually large. A boot loader that erases the application region from the bottom upward and loses power at thirty percent has erased thirty percent of the application and destroyed nothing else. The boot loader itself, the parameter block, and the file system above the application are all intact.

Reading a partially erased block

The block at the pointer needs a different read strategy from the rest of the array. A normal read compares each cell’s threshold to a fixed reference and returns a bit. Applied to a partially erased block, that comparison is a coin toss for cells sitting between the two distributions.

The useful technique is to sweep the reference. Read the block at the nominal reference, then at references offset above and below it, and compare the three results. Cells whose value is stable across the sweep are decided; cells that flip are the ambiguous ones, and their count is a measure of how far the erase progressed.

Sweep result Interpretation Action
Stable at all references Cell is decided Accept the value
Flips at one reference Marginal cell Flag, use context
Flips at every reference Deeply ambiguous Treat as unknown

The third category is usually small. Even a sixty-percent-complete erase leaves most cells clearly on one side, and error-correcting codes elsewhere in the image absorb the remainder, provided the block has any.

The failures that are not recoverable

Three scenarios destroy the data, and they are worth separating from the recoverable ones.

The first is interruption during the erase of a metadata block that the rest of the image depends on. If the block holding the file system superblock is erased, the data blocks above it are intact but unaddressed, and reconstruction becomes a forensic exercise rather than a read.

The second is a boot loader that retries the erase on the next power-up. The device comes back, sees an update in progress, and erases again from the beginning, this time over blocks that were previously untouched. Each retry destroys more, and a device that is power-cycled repeatedly during a failed update can end up genuinely empty.

Scenario Recoverable fraction Method
Single interruption, no retry Most of the array Straight read plus sweep
Repeated power cycles Boot region and above only Region-wise read
Interrupted metadata erase Data present, structure lost Reconstruction
Erase completed, then programming failed Nothing Rebuild from another source

The third is a programming failure rather than an erase failure, and it is common on devices that lost power during the write phase. Here the blocks are erased and partially rewritten, which means the image is a mixture of new data in the first blocks and 0xFF in the rest. Nothing can be recovered from the erased part, and the case moves to three sources for rebuilding flash.

The order of operations

Recovery has a fixed order, and reversing it costs data.

The first action is to read the whole array and keep the raw dump. Not a parsed image, not a partial read of the interesting region — the entire array, byte for byte, with the read parameters recorded. Every later step works from that dump and never from the device, because the device can be lost and the dump cannot.

The second action is to identify the layout, which determines what the fragments belong to. The method is the one in five flash layouts, and it applies to a damaged image exactly as it applies to an intact one, because the fingerprints survive a partial erase.

The third action is the reference sweep on the boundary block, performed on a copy of the dump rather than the dump itself, so that the original measurement remains available for a second interpretation.

When recovery is not the goal

A frequent mismatch in expectations is a customer who asks for recovery when what they need is a working device. Those are different objectives with different costs.

If the goal is a working device, the fastest route is usually to erase the remainder and program a known image, provided one exists. If the goal is the data — calibration values, a production log, a key that only exists on this unit — then recovery is the only route and the effort is justified.

Goal Route Data outcome
Working device Erase and reprogram Replaced
Per-unit data Extract the parameter block Preserved
Code for analysis Read the untouched regions Partial
Both device and data Extract, then reprogram Preserved, then extended

The parameter block is the region most often worth recovering on its own. It is small, it survives interruption more often than the code does because it usually sits at the top of the array, and it cannot be obtained from any other unit.

What the service does

We read the array first and report what is actually there, before quoting any recovery work.

Deliverable Content
Raw dump Full array, with read parameters recorded
Region map Which regions are intact, erased, or partial
Recoverable estimate Percentage and which parts
Sweep analysis Decided versus ambiguous cells in the boundary block

The estimate is delivered before the work rather than after, because a device that is eighty percent intact and a device that is five percent intact look identical from the outside, and the customer deserves to know which one they have. Where the answer is that the untouched regions are enough, the remaining work overlaps with extraction margins on aged cells, since a device that has just survived an interrupted erase is not a device in its first cycle.