Introduction

When you take a dump of NAND flash that has UBI on it, it is not obvious what is written where.

What UBI writes to the flash is defined as structures in a kernel source header, drivers/mtd/ubi/ubi-media.h. In this article, you read the two headers that UBI writes at the start of an erase block (PEB): the EC header, which every PEB has, and the VID header, which only PEBs assigned to an LEB have. You compare each one in a dump with its structure.

The test environment is Linux 6.6 running on QEMU's virt machine (ARM64), with nandsim, a kernel module that imitates a NAND flash chip. The NAND is 128 MiB, a PEB is 128 KiB (131072 bytes), and a page is 2048 bytes.

The Layout Inside a PEB

In this environment, both headers are in the first page of a PEB. The numbers on the right are byte offsets from the start of the PEB.

┌──────────────────────────────┐ 0 │ EC header (64 bytes) │ in every PEB ├──────────────────────────────┤ 64 │ (unused) │ ├──────────────────────────────┤ 512 │ VID header (64 bytes) │ only in PEBs assigned to an LEB ├──────────────────────────────┤ 576 │ (unused) │ ├──────────────────────────────┤ 2048 │ data (129024 bytes) │ the LEB's contents └──────────────────────────────┘ 131072 

The position of the VID header (512) and the position of the data (2048) are the values for this NAND. By default, UBI puts the VID header at the next unit the NAND can write after the EC header. This NAND can write in 512-byte subpages, so the VID header is at 512. On a NAND that cannot, the VID header is at the start of the second page, and the data starts at the third page. On any device, the actual positions are written in vid_hdr_offset and data_offset of the EC header.

The numeric fields of the headers are big-endian. The structures use the types __be32 and __be64. ARM64 runs little-endian here, but UBI writes its headers in big-endian whatever the CPU is.

The EC Header: How Many Times the PEB Was Erased

EC stands for Erase Counter. The main job of the EC header is to record, for each PEB, the number of times it was erased.

Dump the first page of PEB 0.

nanddump -s 0 -l 2048 /dev/mtd1 | hexdump -C 
00000000 55 42 49 23 01 00 00 00 00 00 00 00 00 00 00 01 |UBI#............| 00000010 00 00 02 00 00 00 08 00 01 51 da fa 00 00 00 00 |.........Q......| 00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000030 00 00 00 00 00 00 00 00 00 00 00 00 f6 59 ca 6f |.............Y.o| 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000200 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................| * 00000800 

The structure is as follows:

/* drivers/mtd/ubi/ubi-media.h */ struct ubi_ec_hdr { __be32 magic; __u8 version; __u8 padding1[3]; __be64 ec; /* Warning: the current limit is 31-bit anyway! */ __be32 vid_hdr_offset; __be32 data_offset; __be32 image_seq; __u8 padding2[32]; __be32 hdr_crc; } __packed; 

Compared with the dump, it reads as follows:

Offset Field Value Meaning
0 magic 55 42 49 23 (UBI#) Marks an EC header
4 version 01 The version of the UBI format
8 ec 00 … 01 (1) The number of times this PEB was erased
16 vid_hdr_offset 00 00 02 00 (512) The position of the VID header
20 data_offset 00 00 08 00 (2048) The position of the data
24 image_seq 01 51 da fa A number chosen at each format. The same value is written to every PEB
60 hdr_crc f6 59 ca 6f The CRC of the 60 bytes up to here

From byte 512 on, it is still 0xFF. PEB 0 has no VID header. It is a free PEB, not yet assigned to any LEB.

Neither the NAND chip nor MTD counts erases. UBI keeps the count by writing it in this EC header. So if you erase a NAND that has UBI on it with flash_erase, the count is lost along with the EC header. ubiformat reads the count before erasing, adds 1, and writes it back.

image_seq is used at attach time. If a PEB has a value (other than 0) that differs from the value in the first PEB read, attach stops with the error bad image sequence number. This is to detect PEBs from an old image left behind after a new image was written.

The VID Header: Which LEB of Which Volume

VID stands for Volume Identifier. The main job of the VID header is to record which LEB (logical erase block) of which volume the PEB is assigned to.

The VID header starts with UBI! (55 42 49 21). Here you read PEB 257, which has a VID header. PEB 257 starts at byte 257 × 131072 = 0x2020000.

nanddump -s 0x2020000 -l 2048 /dev/mtd1 | hexdump -C 
00000000 55 42 49 23 01 00 00 00 00 00 00 00 00 00 00 00 |UBI#............| 00000010 00 00 02 00 00 00 08 00 01 51 da fa 00 00 00 00 |.........Q......| 00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000030 00 00 00 00 00 00 00 00 00 00 00 00 55 cf e2 23 |............U..#| 00000040 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................| * 00000200 55 42 49 21 01 01 01 05 7f ff ef ff 00 00 00 00 |UBI!............| 00000210 00 00 00 00 00 00 58 00 00 00 00 00 00 00 00 00 |......X.........| 00000220 3d 26 c9 e1 00 00 00 00 00 00 00 00 00 00 00 01 |=&..............| 00000230 00 00 00 00 00 00 00 00 00 00 00 00 dd 89 12 1c |................| 00000240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000400 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................| * 00000800 

The EC header is at byte 0, and the VID header is at byte 512 (00000200). The structure is as follows:

/* drivers/mtd/ubi/ubi-media.h */ struct ubi_vid_hdr { __be32 magic; __u8 version; __u8 vol_type; __u8 copy_flag; __u8 compat; __be32 vol_id; __be32 lnum; __u8 padding1[4]; __be32 data_size; __be32 used_ebs; __be32 data_pad; __be32 data_crc; __u8 padding2[4]; __be64 sqnum; __u8 padding3[12]; __be32 hdr_crc; } __packed; 

The main fields compared with the dump are as follows. The offsets are byte offsets from the start of the VID header. For example, the VID header starts at 0x200 and vol_id is 8 bytes into it, so vol_id is the four bytes at 0x208 of the dump.

Offset Field Value Meaning
0 magic 55 42 49 21 (UBI!) Marks a VID header
6 copy_flag 01 UBI sets it when it replaces a whole LEB at once or copies an LEB to another PEB. Here, the volume table was rewritten when the volume was created. Used at attach time (below)
8 vol_id 7f ff ef ff Which volume's LEB this is
12 lnum 00 00 00 00 (0) Which LEB of that volume
40 sqnum 00 … 01 (1) A number that increases each time a VID header is written
60 hdr_crc dd 89 12 1c The CRC of the 60 bytes up to here

Volumes you create are numbered from 0 in vol_id. 0x7FFFEFFF is the number of a dedicated volume that holds the list of volumes (the volume table). PEB 257 holds LEB 0 of this volume.

What UBI Does at Attach Time

At attach time, UBI reads the EC header and the VID header of every PEB and builds a table of LEB-to-PEB mappings in memory.

When UBI rewrites data, it does not overwrite the same PEB. It writes to another PEB and then erases the original one. If the power is cut before the erase, the same LEB remains in two PEBs. In that case, UBI uses the one with the larger (newer) sqnum. However, if copy_flag is set in the newer one, UBI also checks the CRC of its data against data_crc in the VID header, and if they do not match, it uses the older one (ubi_compare_lebs() in drivers/mtd/ubi/attach.c).

Getting a Volume's Data Out of a Dump

With what you have read so far, you can get a volume's data out of a dump.

  1. Collect the PEBs of the volume you want by vol_id in the VID header
  2. Put them in lnum order
  3. If two PEBs have the same lnum, choose one the same way UBI does at attach time (above)
  4. Join their data parts (from data_offset in the EC header on)

An LEB that was never written has no PEB. In a dynamic volume, UBI reads such an LEB as 0xFF bytes, so put one LEB's worth of 0xFF (129024 bytes here) in its place. If you just join the PEBs you found, the LEBs after a gap shift forward.

When you investigate the flash of a returned product, take a dump including the OOB (nanddump -o) before you format or attach. Even attaching alone can make UBI write to the flash: it may rewrite a corrupted volume table or move data to another PEB.

Summary

Header Location Marker Main fields
EC header Byte 0 of every PEB UBI# ec (erase count), vid_hdr_offset and data_offset, image_seq
VID header At vid_hdr_offset of an assigned PEB UBI! vol_id and lnum (which LEB of which volume), sqnum

Look for UBI# and UBI! in a dump, and you can see where each PEB starts and which PEB is used for what. From there, you can read the rest by comparing it with the structures in ubi-media.h.

Want to Learn More?

This article is based on Chapter 6 of my book, Hands-On Embedded Linux UBI with QEMU: Stop Writing Off Flash Corruption as "Cause Unknown".

In the book, with no real hardware, using just one PC, you load nandsim into Linux running on QEMU and put UBI on top of it. You dump the contents of the flash and compare them with the structures in the kernel source. Then you deliberately cause failures that could happen at any time on real hardware, and check in the logs and the contents of the flash what UBI does.

  • Read the volume table in a dump, one field at a time
  • See why rewriting data moves it to another erase block
  • Corrupt the volume table and the headers, and see how UBI deals with it
  • Power cuts in the middle of a rewrite, an update, and a format
  • Bit flips, erase and write failures, and bad blocks increasing until no erase block is left to replace them

Hands-On Embedded Linux UBI with QEMU on Amazon