nandlog
Append-only, CRC-validated logging on raw SPI NAND, built to keep every committed record across a power cut. No flash translation layer, no filesystem, no heap.
nandlog is a C99 library for devices that collect data for weeks and hand it over in one go, such as a battery-powered sensor, a tag, or a logger. It assumes that writes are small and frequent, reads are rare and bulk, and power can fail at any instant without warning.
The log assembles records into a page in RAM and commits each page to the next position on the part, with one CRC-32 over the page header and another over the payload. Each page is programmed once, read back, and never rewritten. A metadata ring describes each epoch, one generation of the log, and a boot finds where the next page goes, the write head, by a binary search.
The library is four layers with three interfaces (Figure 1). You write the port, thirteen small functions over your SPI peripheral, and use one of the two supplied chip drivers or write one for your part.
If you are deciding whether to use nandlog, read What it provides and What survives a power cut. If you are integrating it, start with Getting Started.
Getting Started →
Install, configure, and write your first record.
API Reference →
Every function, grouped by what it does.
Porting →
The thirteen functions you write, with a worked example.
Chip Drivers →
Adding support for a part that is not already included.
Durability →
What survives a power cut, and where the guarantee ends.
What it provides
| Capability | Detail |
|---|---|
| Static allocation | About 16 KB of RAM on a 4 KB-page part, fixed at compile time. No malloc. |
| Durability | Once the call that commits a page returns, its records survive a power cut at any later instant. See What survives a power cut. |
| Corruption detection | Separate CRC-32s over each page header and payload, and every commit read back and compared byte for byte. |
| Bad-block handling | A block that refuses a program or an erase is retired, and its pages are moved and verified. The bad-block table survives a power cut at any instant (details). |
| Fast recovery | A warm boot of the 8 Gbit Alliance part takes 82–89 ms with 35 blocks in its bad-block table. The search for the write head stays correct with retired blocks, unreadable pages, and torn writes in the epoch. |
| Timestamped records | Every record carries a 32-bit timestamp you supply, in your own units. The log reads no clock, and reads select pages by time window (details). |
| Bulk offload | Time-bounded reads, a framed wire format with a CRC on every page, and per-page retransmission. |
| Multiple generations | Epochs. An earlier epoch stays readable until the write head sweeps over it. |
| Bounded work | Every search loop has a stated bound, so a failing part cannot stall the calling task indefinitely. |
| Nothing dropped silently | nandlog_store_record() says why it refused a record, and running counters say how many records were refused or lost. |
What survives a power cut
Once the call that commits a page returns, every record in that page survives a power cut at any later instant, and a page that a power cut tore reads back as a gap. Records still in RAM are lost, as at any reset, and the guarantee has stated limits, chiefly a page left erased in every bit and epochs that are sealed when their metadata can no longer be verified. The simulator checks the contract at every program and erase, and an unattended soak on the Alliance part has passed 2,059 RESET-interrupted operations. A soak on the Winbond part found one residual second program in 2,017 cuts, and tests that remove power are still to come. Durability has the full contract, its limits, and the evidence.
What it does not do
- Interpret records. A record's contents and length are the application's business, and the log stores opaque bytes.
- Read a clock. The caller supplies timestamps, which the log compares and never interprets.
- Allocate. Everything is static and sized at compile time.
- Wear-level. The write head sweeps forward and wraps. Anything more would need a mapping table.
- Update in place. A page is written once and never revised.
A minimal example
#include "nandlog.h"
if (!nandlog_init())
return; /* the port would not open, or no part answered */
nandlog_begin_session();
nandlog_begin_epoch(&details, sizeof(details));
nandlog_end_session();
if (nandlog_store_record(RECORD_SAMPLE, milliseconds, &sample, sizeof(sample)) != NANDLOG_OK)
; /* not stored, and the status says why */
nandlog_flush(true);
Getting Started has the complete picture, including how to read data back.
Supported parts
| Driver | Part | Page | Block | Array |
|---|---|---|---|---|
nandlog_chip_AS5F18G04SND.c | Alliance Memory, 8 Gbit | 4096 + 256 B | 64 pages | 4096 blocks |
nandlog_chip_W25N01GWZEIG.c | Winbond, 1 Gbit | 2048 + 64 B | 64 pages | 1024 blocks |
Any other part needs a driver, which is one .c file. See
Chip Drivers.
Requirements
- A C99 compiler. Build with
-fno-strict-aliasing. - A little-endian target, because the on-flash structures are native integers. A compiler that reports a big-endian target fails the build, and
nandlog_init()checks a target whose compiler does not say. - A SPI NAND part with a power-of-two number of pages per block.
- About 16 KB of RAM for a 4 KB-page part, and less for a smaller one.
- No RTOS, heap, or filesystem. A mutex is needed only if more than one thread calls the library.
Project layout
| Path | Contents |
|---|---|
nandlog.c, nandlog.h | The log core and its public API. |
nandlog_conf.h | Configuration, the one file you edit rather than write. |
nandlog_chip.h | What the log requires of a part. |
nandlog_port.h, nandlog_port.c | What the log requires of a platform, and a template to fill in. |
chips/ | One self-contained driver per supported part. |
sim/ | Host build: the real log and chip drivers over a RAM-backed emulated part. |
tests/ | Applications that run on real hardware. |
tools/nandlog_parse.py | Reference reader for raw images and offload streams. |
License
MIT. See LICENSE.