Introduction

Are you stuck because a UBI attach failed? Or do you want to know when a UBI attach can fail?

When an attach fails, ubiattach may print the following error:

ubiattach: error!: cannot attach "/dev/mtd1" error 22 (Invalid argument) 

The kernel log shows this line:

ubi0 error: ubi_attach_mtd_dev [ubi]: failed to attach mtd1, error -22 

error 22 is EINVAL (Invalid argument). But this only tells you that the kernel's attach returned -22. It does not necessarily mean that the arguments you gave ubiattach are wrong.

The cause is written in the kernel log, before this line. In this article, I reproduce three situations in which attach fails with error -22, and show which log lines appear before it in each case.

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, and an erase block (PEB) is 128 KiB.

Where to Look

Look at the kernel log, not at the output of ubiattach.

dmesg 

Go up from the failed to attach mtd1, error -22 line, and look for lines that contain ubi0 error:.

Case 1: bad image sequence number

ubi0: attaching mtd1 ubi0 error: ubi_attach [ubi]: bad image sequence number 1311134312 in PEB 205, expected 597359688 

What is happening

Each time ubiformat formats the flash, it chooses one number, image_seq, and writes the same value to the EC header of every PEB. At attach time, if UBI finds a PEB whose value (other than 0) differs from the value of the PEB read first, it gives up attaching.

The log above means that the image_seq of PEB 205 (1311134312) differs from the value of the PEB read earlier (597359688).

How I caused it

I stopped the ubiformat process in the middle of reformatting. ubiformat rewrites the PEBs in order from the start, with the new image_seq. If it stops partway, the PEBs not yet rewritten keep the value of the previous format. In this example, PEB 205 onward still had the old value.

Possible causes on real hardware

  • The power was cut while ubiformat was reformatting the flash
  • When a new image was written, PEBs of the old image were left unerased

Case 2: both volume tables are corrupted

ubi0 error: vtbl_check [ubi]: bad CRC at record 0: 0x85d7b601, not 0xa6fac6cc ubi0 error: vtbl_check [ubi]: bad CRC at record 0: 0x85d7b601, not 0xa6fac6cc ubi0 error: ubi_read_volume_table [ubi]: both volume tables are corrupted 

What is happening

UBI records which volumes exist (such as their names and sizes) in the volume table. It keeps two identical copies of the table, in LEB 0 and LEB 1 of a dedicated volume.

bad CRC at record 0 means that the CRC of record 0 of the table does not match. It appears twice because it did not match in either LEB 0 or LEB 1. If only one copy is corrupted, UBI restores it from the other copy and continues attaching (the log shows volume table was restored). If both copies are corrupted, UBI cannot tell which volumes exist, so it gives up attaching.

How I caused it

In both PEBs that hold the volume table, I changed one byte of the volume name.

Case 3: the layout volume was not found

ubi0 error: ubi_attach [ubi]: PEB 257 contains corrupted VID header, and the data does not contain all 0xFF ubi0 error: ubi_attach [ubi]: this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection ubi0 error: ubi_attach [ubi]: PEB 258 contains corrupted VID header, and the data does not contain all 0xFF ubi0 error: ubi_attach [ubi]: 2 PEBs are corrupted and preserved Corrupted PEBs are: 258 257 ubi0 error: ubi_read_volume_table [ubi]: the layout volume was not found 

What is happening

A PEB assigned to a volume has a VID header after the EC header at its start (in this environment, the VID header starts at byte 512). The VID header records which LEB of which volume the PEB is assigned to. PEB 257 contains corrupted VID header means that the VID header of PEB 257 is corrupted.

When the VID header is corrupted, UBI cannot tell what the PEB is. Something is written in the data area (it is not all 0xFF), so UBI keeps this PEB without erasing it (preserved), so that a person can look into its contents.

In this example, the two corrupted PEBs were the PEBs of the dedicated volume that holds the volume table (the layout volume). UBI finds no PEB of the table, so it gives up attaching with the layout volume was not found.

How I caused it

In both PEBs that hold the volume table, I changed one byte of the VID header.

Summary

Line before failed to attach What is happening
bad image sequence number … in PEB …, expected … PEBs from different formats are mixed together
both volume tables are corrupted Both copies of the volume table are corrupted
the layout volume was not found None of the volume table's PEBs are found (their VID headers are corrupted)

error 22 (Invalid argument) from ubiattach alone does not tell you the cause. In the kernel log, look at the ubi0 error: lines that appear before failed to attach.

When you investigate a returned product or similar, take a dump of the flash, including the OOB (nanddump -o --bb=dumpbad), before you format or attach it. Reformatting makes attach possible again, but neither the contents of the volumes nor the way the PEBs were corrupted remain. Even attaching alone can make UBI write to the flash: it may rewrite a corrupted volume table or move data to another PEB. By default, nanddump skips bad blocks when it dumps, so the PEBs after a bad block shift to earlier positions in the dump.

Want to Learn More?

This article collects the attach failures from Chapters 7 and 8 of my book, Hands-On Embedded Linux UBI with QEMU: Stop Writing Off Flash Corruption as "Cause Unknown", and rewrites them.

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

An appendix lists the UBI and NAND logs that appear in the book, along with when they appear.

Hands-On Embedded Linux UBI with QEMU on Amazon