nandlog v1.1

On-Flash Format

The bytes nandlog writes to the part and sends over the wire, and how a boot finds its place in them. Read this page if you are writing a parser, a host tool, or a recovery utility.

nandlog writes three kinds of structure to the part and defines a fourth for the wire. Every multi-byte field is little-endian, every structure is packed, and every one carries a CRC-32 with the IEEE 802.3 polynomial, so a host can check it with Python's zlib.crc32.

Within an epoch, a page's sequence number is its position, so a reader can locate any page without an index, and every gap has a sequence number a host can ask for again. At boot, the log finds the newest slot in the ring and then the write head, the position the next page goes to, by reading alone. tools/nandlog_parse.py reads everything on this page, independently of the library.

If you are writing a parser, read Layout through Parser. The last two sections, Newest epoch and Write head, describe how a boot recovers and what it reads.

Layout

The array is divided into three regions. The log never writes to the reserve, which belongs to the chip driver.

RegionBlocksContents
Metadata ringThe first 8Metadata slots, written in turn
Log regionEverything betweenData pages, written in sequence
ReserveThe top 16 (Alliance) or 40 (Winbond)The Alliance driver's bad-block table and on-device test scratch, or the Winbond part's spare blocks for remapping
metadata ring log region reserve 8 blocks epoch slots data pages, written in sequence the write head sweeps forward and wraps 16 or 40 blocks the driver's own block 0 last block write head wraps, erasing each block just before it needs it
Figure 1. The three regions. An earlier epoch is not erased when a new one begins. The head erases its blocks one at a time as it sweeps back around to them.

Positions. An epoch's positions are the pages of the log region's good blocks, numbered from 0 at the block the epoch began in, and wrapping from the end of the region to its start. A retired block has no positions. A page's sequence number is its position, so the page that holds a given sequence number is found by arithmetic over the list of retired blocks. The driver persists which blocks are retired, so the numbering is the same after every reboot. In an earlier epoch, a block retired since then can shift the positions, so a reader of an old epoch goes by the seq in each header.

Checksums. Every CRC on this page is the reflected IEEE 802.3 CRC-32 (polynomial 0xEDB88320, initial value and final XOR 0xFFFFFFFF), identical to zlib.crc32. The CRC of zero bytes is 0.

Every data page begins with a 32-byte header, declared in nandlog.h as nandlog_page_header_t. The header has its own CRC, so a reader can trust it before reading the payload. That is why the log can find the write head and resolve time bounds from 32-byte reads.

OffsetSizeFieldMeaning
04magic0x33505454 ('TTP3'): opaque records. 0x34505454 ('TTP4'): framed records
44epochThe epoch this page belongs to
84seqSequence number within the epoch, from 0
124first_timestampSmallest record timestamp in the page, or 0xFFFFFFFF if it has no records
164last_timestampLargest record timestamp in the page
202payload_lengthPayload bytes after the header, 0 only in a tombstone
222record_countComplete records in the payload
244payload_crcCRC-32 over the payload
284header_crcCRC-32 over bytes 0–27

The payload starts at offset 32, and the rest of the page reads FFh.

32-byte header payload offset 0 32 page size magic epoch seq first_ts last_ts len cnt pld_crc hdr_crc 4 4 4 4 4 2 2 4 4 covered by hdr_crc two checksums, deliberately
Figure 2. A data page. Because the header has its own CRC, a reader can trust seq, payload_length, and the timestamps before it reads the payload, so a 32-byte read is enough to place a page.

Validity. A page is valid when its magic is one of the two values, its header CRC matches, and its payload_length is no more than the page size less 32. A valid page whose payload CRC fails, or which the part cannot correct, is a gap. The library serves a gap as a zero-length page that carries its sequence number, and a gap is never the end of the log.

Timestamps. The two time fields are the smallest and the largest timestamps of the page's records, in the caller's units, which the log never interprets. They need not be those of the first and last records, because a record may carry an earlier time than the one before it, and keeps it. A record that steps back by more than NANDLOG_TIMESTAMP_TOLERANCE_MS (250 by default) from the page's largest timestamp starts a new page instead. A record stamped 0xFFFFFFFF keeps that stamp, but counts as 0xFFFFFFFE in its page's bounds, so 0xFFFFFFFF in a header always means that the page has no records. Time-bounded reads select pages by these two fields.

Tombstones

A page is only ever committed with records in it, so a valid page with payload_length 0 is a tombstone. A tombstone keeps a position occupied when its page cannot be kept, so the position reads as a gap and can never be taken for the write head. The log writes one in two cases:

A tombstone carries the build's magic, the epoch, the position's sequence number, 0xFFFFFFFF for both timestamps, 0 for the length and the record count, and 0 for payload_crc, which is the CRC of no bytes.

Record framing

A page's magic says how its records are laid out, so a log that spans a firmware change parses correctly page by page. Records are never split across pages, and nandlog_store_record() refuses a record that cannot fit in an empty page with NANDLOG_ERR_TOO_LARGE.

Framed ('TTP4', the default). With NANDLOG_RECORD_FRAMING on, each record is:

[data length: 2][record type: 1][timestamp: 4][data: data length]

The length is little-endian and counts the data only. A reader steps from record to record by the length alone, so it can walk a page, and skip a record type it does not recognize, without knowing the application's types. nandlog_framed_next_record() does this on the device, and stops at a record that would run past the payload.

Opaque ('TTP3'). With framing off, each record is:

[record type: 1][timestamp: 4][data]

Only the application that wrote the records knows where each ends, so a generic reader hands the payload back whole. A framed record takes 7 bytes more than its data and an opaque one 5, so on a 4 KB page a single framed record holds at most 4,057 bytes of data.

Metadata slot

A metadata slot records an epoch: where it began, the caller's details for it, and what the log has learned about it since, such as positions it passed over or a page that failed. The log writes a slot when an epoch begins, and writes the current epoch's slot again whenever that record changes or a boot finds it unsettled. Each slot goes to both pages of a pair, an even page of a ring block and the odd page after it, so one unreadable page never loses it. nandlog.h declares bytes 0–31 as nandlog_meta_header_t and bytes 32–63 as nandlog_meta_extension_t.

OffsetSizeFieldMeaning
04magic0x334D5454 ('TTM3')
44epochThe epoch this slot describes
84log_start_pageFirst page of the block the epoch began in
124created_timestampAlways 0, since the log reads no clock
162details_lengthBytes of caller-defined details
182format_versionNANDLOG_FORMAT_VERSION: 2
204details_crcCRC-32 over the details
244header_crcCRC-32 over bytes 0–23
284reserved0xFFFFFFFF, not covered by header_crc
324sequenceOne more than the slot written before it, anywhere in the ring
364head_failed_page0xFFFFFFFF, or a write-head page whose program failed or did not read back
404retired_blocksRetired blocks in the ring and log region when the slot was written
444span_retiredFingerprint of which of the epoch's blocks are retired
482span_blocksBlocks from the epoch's first through the one holding its newest page, or 0
501zero0
511sealed0xFF, or 0 once the epoch is sealed
522passed_over_countRuns in the passed-over list
542reserved0xFFFF
564passed_over_crcCRC-32 over the passed-over list
604extension_crcCRC-32 over bytes 0–59
64details_lengthdetailsThe caller's blob
64 + details4 per runpassed-over listRuns of positions, below

The rest of the page reads FFh.

Validity. A copy is valid when its magic, header CRC, and extension CRC hold, its log_start_page begins a block of the log region, and its details leave room for at least 64 runs of the list. It is whole when its details CRC and list CRC hold too. The details are limited by what the page holds, not by the reading build's NANDLOG_MAX_EPOCH_DETAILS_BYTES, so a slot written under a larger limit is still found.

Passed-over list. A passed-over position is one the log will never program, because a boot found that its page would not read whole. A torn page, one a power cut interrupted, is the usual case. The list holds ascending runs, one 32-bit word each: the run's first position in bits 0–23 and its length less one in bits 24–31. A listed position counts as written, whatever its page reads later. The list has room for (page size − 64 − NANDLOG_MAX_EPOCH_DETAILS_BYTES) / 4 runs, at most 1,024: 880 on a 4 KB page and 368 on a 2 KB page, with the default 512 bytes of details.

Failed head. head_failed_page names a write-head page whose program failed or did not read back. The log records it before it does anything else about the failure, and carries it into every later slot of the epoch. Such a page may hold charge that no read can see, so a boot whose head is still in that block, unretired, moves the block's pages before it programs anything.

Layout record. Positions are laid out over the blocks the driver calls good, so retired_blocks, span_blocks, and span_retired record the layout the slot was written under. span_retired is the 32-bit sum, over each retired block among the epoch's first span_blocks blocks, of the CRC-32 of that block's 32-bit offset from the epoch's first block. A boot whose table names fewer retired blocks, or other ones under the epoch, would lay the pages out differently, so it seals the epoch and keeps the slot's original record. The one exception is the block of a recorded failed head, whose retirement completes a relocation the slot recorded, once the head stands at or past the failed page.

Seal. A sealed epoch takes no more pages. Every later boot reads the seal, refuses records with NANDLOG_ERR_SEALED, and calls nandlog_port_unwritable(), until a new epoch begins, but the epoch's pages can still be read. A boot seals the epoch, in its slot, in four cases:

Bad-block table

Which blocks are retired, and so which pages have positions, is the chip driver's business rather than part of this format. A parser of a raw dump still needs it, to tell the live copy of a relocated page from the stale original. This section describes the Alliance driver's table. The Winbond part remaps a retired block in a lookup table inside the chip, so a Winbond dump carries no table (see Durability).

Each version of the Alliance table is one page:

OffsetSizeFieldMeaning
04magicThe ASCII bytes BBT3
44crcCRC-32 over byte 8 through the last entry
84versionOne more than the version it replaces
124countEntries that follow, at most 256
164 per entryentriesThe first page of each retired block

The rest of the page reads FFh. The rules a reader needs:

A reader of a raw dump that has no table recognizes the stale copies a relocation leaves in a retired block by keeping one copy of each (epoch, sequence number).

Wire format

The offload stream is how a device sends an epoch to a host. nandlog.h defines its two structures, nandlog_stream_header_t and nandlog_wire_page_t, and the device's firmware writes them from what nandlog_read_span() and nandlog_retrieve_next_page() report. A stream is a 16-byte header, the caller's details, then a 20-byte frame and its payload for each page.

OffsetSizeStream header fieldMeaning
04magic0x31535454 ('TTS1')
42record_format1 opaque, 2 framed
62details_lengthBytes of details that follow the header
84total_pagesPages that will be sent, gaps included
124total_payload_bytesSum of their payload lengths
OffsetSizePage frame fieldMeaning
04seqThe page's sequence number
44first_timestampAs in the page header
84last_timestampAs in the page header
122payload_lengthPayload bytes that follow, or 0 if the device could not read this page
142record_countAs in the page header
164payload_crcCRC-32 over the payload that follows
… stream hdr details frame 0 payload 0 frame 1 (none) frame 2 'TTS1' a gap payload_length = 0 the device could not read this page, but its seq is still reported, so the host can ask again
Figure 3. An offload stream, with a gap at frame 1.

A device sends a page it cannot read, and a tombstone, as a frame with zero payload, so the host sees an explicit gap at a known sequence number rather than silently missing data. The host checks payload_crc itself, which also catches corruption in transit.

A frame carries no magic, so the header's record_format applies to every page of the stream. An epoch's pages share one format unless NANDLOG_RECORD_FRAMING changed while it was being written, so begin a new epoch along with a firmware update that changes it. Alternatively, a device can send each page's magic, which nandlog_retrieve_next_page() returns in the header it fills in, alongside the frame.

Sequence numbers are contiguous within an epoch. A host can therefore identify exactly which pages are missing, including pages lost to a truncated transfer, which have no frame at all, and ask for them again (see Retransmission).

Reading an image or a stream

tools/nandlog_parse.py is a second implementation of this page, written independently of the library, so a disagreement between the two shows that one of them is wrong. It reads a raw dump of the part, main and spare area page by page, or an offload stream.

./tools/nandlog_parse.py image  dump.bin --page-size 4096 --spare-size 256
./tools/nandlog_parse.py image  dump.bin --json
./tools/nandlog_parse.py image  dump.bin --expect library_view.txt
./tools/nandlog_parse.py stream offload.bin

The parser exits with status 1 if any live page fails verification or, with --expect, if any page disagrees. make in sim/ runs the --expect comparison on every image the suite dumps, a Winbond image among them.

Format versions

MacroMeaning
NANDLOG_FORMAT_VERSIONThe on-flash format a build writes: 2. Every metadata slot records it, but a reader goes by the magics, which each format has its own of.
NANDLOG_RECORD_FORMAT_THIS_BUILDThe record format a build writes: 1 opaque, 2 framed. It follows NANDLOG_RECORD_FRAMING. A page states it in its magic, a stream in its header.
NANDLOG_VERSION_MAJOR / _MINORThe library API: 1.1. It is independent of the formats.

The digits in the magics (TTP3, TTP4, TTM3, TTS1, BBT3, and the OTP marker BBT2) are part of the tags, not version numbers. Each on-flash format has its own page and slot magics, and the stream, table, and OTP tags change only when their own layouts do.

Parts written in format 1. Format 1, which every release up to 1.0 wrote, tagged its pages TTP1 and TTP2, and its slots TTM1 or TTM2. This library does not read those slots, so it finds no epoch on such a part. It does recognize those pages, whose header is this format's, and treats each as belonging to no epoch: a boot settles it with one header read, as it would another epoch's page, and never adopts it or counts it as the current epoch's. Format 1 pages stay on the part until the log erases their blocks, and are not readable through the API.

The first epoch on a part with no slot. With nothing in the ring to number from, as on a fresh part or one whose whole ring was lost, nandlog_begin_epoch() reads a header from each block of the log region and numbers the epoch above every epoch this format's pages name. Without that, a part whose ring was lost would begin again at epoch 1 among its own earlier pages.

Finding the newest epoch

A boot first finds the newest slot it may trust, because that slot names the current epoch, where it began, and what the log knows about it. The boot only reads at this stage, and judges each copy from a single read.

How the ring is written. The ring's eight blocks are used in turn. The log enters a block by erasing it, fills it strictly in order, and leaves it at the first pair that is not blank or will not take a program, so each block's written pages are a prefix of it. The second copy of a pair is programmed only after the first has read back with margin and its CRC agrees. If no pair in the ring takes both copies, a single verified copy stands as the slot.

What a boot reads.

  1. The first readable slot of each ring block, usually its first page: 8 reads of 64 bytes. The block whose first slot has the highest sequence is the newest.
  2. A binary search of that block for the end of what was written: 6 header reads on a 64-page block.
  3. Both copies of the last pair, each read once, as a whole page, into RAM. Each image is judged by every checksum it carries: whole, valid with its list but not its details, valid without its list, or not a slot. That image, never a second read, is what the boot goes by and what every slot it writes is built from.
  4. A header read of the page after the pair and of the first page of the next ring block, each followed by a raw read (the whole page and spare, ECC off) if its header reads erased, to check that nothing was written after the slot.

That is 18 to 20 reads, at most 2 of them raw, and ten of them in the newest ring block. If that block has no usable pair, the boot repeats steps 1 to 3 for the block entered before it, at most once per ring block.

Choosing the slot. The last pair counts only if a copy is whole, because a power cut may have torn it before anything acted on it. An earlier pair counts if either copy is valid with its list, since it was verified twice when it was written, and acted on. If neither copy of that pair still holds its list, going back to an older slot would undo what it recorded, so the boot keeps the slot's header and seals the epoch. If only the details are lost, the epoch goes on without them. As a result, one unreadable page never costs a slot.

Settling. A slot is settled when both its copies are whole and read with margin, and nothing was written after it, in its block or in the first page of the next. Otherwise a power cut may have torn it, and a torn page can read valid on one boot and not the next. So before the boot erases or programs anything for the write head, it writes the slot again from its image, as the first pair of the next ring block, numbered past anything the torn pages could claim. Every later boot then goes by the same slot.

Worn copies. A copy that the part corrects only at the limit of its ECC (NANDLOG_READ_AT_ECC_LIMIT) still counts as whole, but it leaves the slot unsettled, so the boot writes the slot again into a fresh block. A copy is never written to a page that reads back at the limit, so a worn page costs one erase and two programs once, not at every boot. Both copies share a block, and every boot reads ten of its pages. Durability covers that wear.

A newer epoch the ring lost. An epoch begins only once its slot is on the part twice, so pages of an epoch newer than the ring names mean that every copy of its slot became unreadable. After finding the write head of the epoch the slot names, and before acting on that slot, the boot reads the first page header of the head's block (or of the next good block, if the head is part-way into one) and of the blocks after it, NANDLOG_ERASE_AHEAD_BLOCKS + 2 good blocks in all. A block whose first header neither checks nor reads erased is read on, page by page. A page of a newer epoch with sequence number s at offset o in its block shows that the epoch began (s − o) / P good blocks earlier, where P is the pages per block. The boot carries on with that epoch, finds its write head, and writes it a new slot without the caller's details. If s − o is not a whole number of blocks, the log stops writing. The check repeats after each epoch it adopts, at most once per page of the ring.

Finding the write head

The boot finds the write head by binary search over the epoch's positions, reading only. The search is correct because the write paths keep three rules true at every instant a power cut can land. With h the write head:

  1. Every position below h is passed over or not blank, and any valid header there is this epoch's, carrying the position as its sequence number.
  2. Position h is blank and not passed over, unless it begins a block the epoch has not entered yet.
  3. No position at or above h is passed over, or holds this epoch's valid header for that position.

Classes. The search sorts each position it probes into one of three classes:

The rules make "inside" exactly "below the head". Only a blank page that is not passed over counts as evidence of being past the head, so retired blocks, unreadable pages, and torn pages below the head do not mislead the search. A torn page that reads whole cannot be told from a committed one, and is taken as committed. The comment that opens the Write-Head Recovery section of nandlog.c states the rules.

What the search reads.

Below the head. The boot then checks the answer from below. It reads each position below the head once, as a whole page, until one reads whole: this epoch's header for that position, a payload whose CRC holds, and ECC that corrects. Every position on the way is passed over, whatever the search made of it, because a page below the answer that reads blank or as another epoch's contradicts the search, as the remains of an interrupted erase can. The boot lists those positions in the slot before it commits anything after them. Usually this costs one whole-page read, of the page just below the head.

When that run covers everything below the head in its block, the block cannot be trusted as it stands:

The head's own page. A build with NANDLOG_PASS_OVER_HEAD_AT_BOOT also passes over the head's own page whenever the head is part-way into a block in which no program has failed and which the boot is not rewriting, and moves the head past it. A power cut during a commit can leave that page partly programmed, and this keeps the log from programming it a second time. Getting Started covers when to choose it.

Then. If the slot is unsettled, or the boot passed over new positions, adopted an epoch, or counts a different number of retired blocks, it writes the slot. Only then does it prepare the head: it relocates the head's block if a program failed there before the reboot or the head's page is not blank, rewrites a distrusted block, and erases the head's block, if the epoch has not entered it, and the NANDLOG_ERASE_AHEAD_BLOCKS blocks after it. Those blocks are erased again at every boot because an interrupted erase can leave a block that reads erased but programs unreliably.