Posts Tagged ‘mcu flash copy’

PostHeaderIcon NAND and NOR Copy Differently: ECC and Bad Block Tables

Copying a flash device sounds like the simplest operation in this field. Read one, write another. It works exactly as expected on NOR and fails in a way that looks like a hardware fault on NAND, and the difference is not in the reader but in what the two technologies store besides the data.

The distinction is worth understanding before the copy, because a NAND image made the wrong way is not obviously wrong. It is the right size and it contains the right bytes in most places.

NOR: a copy is a copy

NOR flash is byte-addressable and reads like memory. A command places a read address, the device returns the byte at that address, and the interface supports random access at full speed. Programming is per word or per buffer, erasing is per sector, and there is no hidden structure.

A copy is therefore a literal copy. Read all addresses from zero to the end, write the result to an identical part, and the device behaves the same.

Property NOR Consequence for a copy
Addressing Byte or word Random access, no structure
Read Direct Any reader works
Erase Sector, typically 4 to 64 KB Uniform
Hidden metadata Lock bits, OTP region Must be handled separately
Error correction Usually none No syndrome to preserve

Two exceptions interrupt the simplicity. The first is the OTP region, a small area that can be programmed once and cannot be erased, which holds a serial number or a key on some parts. It sits at a vendor-defined address outside the main array and is not part of a normal read, so a copy silently omits it.

The second is the lock bits. They live in a configuration register, they are not data, and a copy performed with a normal reader returns the array and nothing else. A device cloned from such a copy arrives unlocked, which is usually an improvement and occasionally a surprise.

NAND: pages, blocks, and a spare area

NAND is not byte-addressable in the useful sense. Reads happen a page at a time, typically two or four kilobytes, and erases happen a block at a time, typically sixty-four to two hundred fifty-six pages.

Each page also has a spare area, called the out-of-band region or OOB, of sixty-four to two hundred twenty-four bytes. That area is not user data. It holds the error-correction syndromes for the page, a bad block marker, and on some controllers a sequence number and a small amount of file system metadata.

Region Size Contents
Main area 2 KB or 4 KB per page User data
Spare area 64 to 224 bytes per page ECC, bad block marker, sequence
Block 64 to 256 pages Smallest erasable unit
Page Smallest readable unit Smallest programmable unit

A copy that transfers only the main area is missing the syndromes. That is the raw copy problem, and it produces an image that is the right size and cannot be corrected.

What the ECC does to a raw dump

NAND cells are less reliable than NOR cells by design, because the process optimises density over retention. A page read without error correction has a small but non-zero chance of a bit error, and on a device that has been through many cycles the chance is not small at all.

The controller reads the main area and the spare area together, computes the syndrome, and corrects up to a fixed number of bit errors per page. The number is a property of the controller rather than the chip, which matters: the same chip used with two different controllers has two different correction strengths.

Dump method Contains syndromes Correctable Detects errors
Raw with OOB Yes By a matching controller Yes
Main area only No No No
ECC-corrected with OOB stored Yes, as corrected Already applied Only uncorrectable

The second row is the failure mode that wastes the most time. An image without syndromes looks fine, boots partway, and fails at a random point that moves when the image is rewritten. The fault appears to be in the device being programmed, and it is actually in the copy.

The bad block table

NAND ships with defects. A small percentage of blocks are bad from the factory, and more become bad with use. The factory marks them by writing a specific value into the first byte of the spare area of the first page of the block.

The controller cannot consult the chip for the list, because reading a bad block is exactly what it must avoid. So it maintains a bad block table of its own, stored in flash, usually in the last few blocks, with two or more copies and a version counter.

Structure Location Purpose
Factory marker Spare area, first page of block Manufacturer’s defect list
Primary table A reserved block, often near the end Controller’s working list
Mirror table A second reserved block Redundancy if the primary is erased
Version counter Inside each table copy Selects the newer

The consequence for copying is severe. If the copy does not carry the table, the target controller scans for factory markers and builds a new table from scratch — which is correct for factory defects and wrong for grown ones. If the copy carries the table but places it at a different block, the controller will not find it.

Three strategies, in order of fidelity

The highest-fidelity copy preserves everything: main area, spare area, and the reserved table blocks at their original addresses. The image is then a byte-exact replica of the chip, including the geometry, and it works on an identical part with an identical controller.

The middle strategy copies with correction applied and rebuilds the table. It produces an image that is more portable across controller generations, because the syndromes are recomputed rather than preserved, but it loses any information that only existed in the original syndromes.

Strategy Preserves Portable Best for
Full raw with OOB Everything Only to identical parts Forensic copies, exact clones
Corrected plus rebuilt table Data Across controllers Repair with a different board
Data only, no OOB User data Widely Extracting files, not cloning

The third strategy is legitimate when the goal is data extraction rather than a working device. A file system parsed out of the main area does not need syndromes, because the parser reads at a higher level and can often recover from an uncorrected page.

Why a copied NAND often does not boot

Five causes account for nearly every failure, and all five are metadata rather than data.

The first is a missing or misplaced bad block table, discussed above. The second is a syndrome layout that does not match the target controller, which happens when the copy came from a device with different correction strength. The third is scrambling, described below.

The fourth is the sequence number in the spare area, which some controllers use to resolve which of two copies of a block is newer after an interrupted write. A copy that renumbers pages in order can invert that resolution.

Failure Symptom Fix
Table missing Boot loader reports no valid blocks Restore the table blocks verbatim
Syndrome mismatch Random single-bit failures Recompute, or match the controller
Scrambling Everything looks encrypted Apply the controller’s pattern
Sequence numbers reset Old data wins over new Preserve the spare area
OOB truncated Size mismatch on write Read with the full page length

The fifth is a plain truncation. A reader configured for the main area returns two kilobytes per page, and a device that expects two kilobytes plus sixty-four bytes in the spare area will reject every page as short.

Scrambling, the variable nobody documents

Several controllers XOR the data with an address-derived pattern before writing, to spread wear and to make the bus traffic less informative. The pattern is not a secret in the cryptographic sense and is often a linear feedback shift register seeded by the block address.

The effect on a copy is that the array looks like noise in a hex view and looks like nothing after a chip-off read. The pattern is recoverable — it repeats with the address, so a block of known plaintext gives it away — but the recovery is a separate step and it must happen before any layout analysis.

What the service does

We characterise the device before copying, because the strategy depends on facts that are not in the part number.

Deliverable Content
Geometry Page size, block size, spare area width
Correction strength Bits per page, as configured by the controller
Table location Where the bad block table lives, and how many copies
Scrambling status Whether an address-derived pattern is applied
Copy strategy Raw, corrected, or data-only, with the reason

Where a device will be cloned rather than repaired, the copy is only half the work; the per-unit data that a copy cannot legitimately transfer is covered in three sources for rebuilding flash, and the read reliability questions that a raw copy depends on are in floating gate margins. Where the copy is of a program image rather than an array, the equivalent constraints are in copy IC program.