UBI FAQ and HOWTO
Table of contents
- How to enable UBI?
- How to attach an MTD device?
- How to create/delete UBI volumes?
- How to run JFFS2 on top of an UBI volume?
- Can I run ext2 on top of UBI?
- Do I have to format my empty flash before running UBI on top of it?
- How to erase flash and preserve erase counters?
- How to flash UBI images and preserve erase counters?
- Can UBI logical eraseblocks be written randomly?
- What for does UBI need headers in physical eraseblocks?
- Why does UBI need two headers, not just one?
- Why UBI does not use OOB area of NAND flashes?
- What is volume table?
- Is UBI tolerant of power failures?
- May UBI be used on MLC flash?
- How to debug UBI?
How to enable UBI?
In the Linux configuration menu, go to "Device Drivers" -> "Memory Technology Devices (MTD)" -> "UBI - Unsorted block images", and mark the "Enable UBI" check-box. UBI may be either compiled into the kernel or built as a kernel module.
How to attach an MTD device?
If UBI is compiled as a kernel module, it is enough to specify the MTD device to attach in the module arguments, e.g.
$ modprobe ubi mtd=3
loads the UBI kernel module and attaches mtd3. And
$ modprobe ubi mtd=3 mtd=5
command loads UBI kernel module and attaches mtd3
and mtd5.
If UBI is compiled into the kernel, the mtd device to attach may be specified in the kernel boot parameters, e.g.,
ubi.mtd=3
command makes UBI attach mtd3 when the kernel is booting,
and
ubi.mtd=3 ubi.mtd=3
command makes UBI attach mtd3 and mtd6.
And finally, MTD devices may be attached or detached at any time with the
ubiattach and ubidetach utilities
(see here); For example,
$ ubiattach /dev/ubi_ctrl -m 3
attaches mtd3. But this "run-time attach" UBI capability was
added recently and it is in the main-line kernels starting from version
2.6.25. Note, it is anyway recommended to back-port UBI patches from the latest
kernel or even better from the UBI git tree.
How to create/delete UBI volumes?
Use ubimkvol and ubirmvol tools (see
here). For example, the below command
creates a 128MiB volume on UBI device 0:
$ ubimkvol /dev/ubi0 -N rootfs -s 128MiB
and the following command removes it:
$ ubirmvol /dev/ubi0 -n 0
For additional information, use ubimkvol -h and
ubirmvol -h.
How to run JFFS2 on top of an UBI volume?
Although it may sound weird, UBI can emulate MTD devices for UBI volumes and JFFS2 can be mounted on these emulated MTD devices. Enable the "Emulate MTD devices" UBI configuration menu check-box to make UBI create one MTD device for each UBI volume. One of the reasons to do this might be using of MLC NAND flash (see this section) or legacy software.
Can I run ext2 on top of UBI?
UBI is not a block device emulation layer, it is not an FTL. Neither ext2 nor other "traditional" file systems can be run on top of an UBI device. Please, read the big red note and overview documentation sections to realize why.
But it is much easier to implement FTL on top of UBI then on top of MTD, because UBI takes care about many flash complexities and makes it possible to concentrate on on upper-level issues rather then solving flash media problems like wear-leveling, bit-flips, bad-blocks, etc.
This e-mail describes an idea of a simple FTL layer on top of UBI.
Do I have to format my empty flash before running UBI on top of it?
Generally speaking, the flash should be formatted into UBI format before using it. This means, each eraseblock should be erased and the erase counter header should be written to it. This is an ideal situation for UBI. However, it is not always possible because most embedded platforms may only wipe out the flash and flash images to it. This is we toughed UBI to deal with empty flash or empty eraseblocks perfectly well - it puts zero EC header to them automatically.
So the answer is no, you do not have to. For example, if you wipe out the flash and try to attach it to UBI - it will work. UBI will just automatically format the flash (which however, will take some time). Or it is perfectly fine if you wipe out your flash, and write an UBI image to it (which will probably be shorter than the flash). In this case UBI will just format the empty physical eraseblocks at the end of the flash.
However, it is not recommended to do this often, because when erasing you
loose erase counters, so you loose wearing information. Doing this over and
over again may wear out some eraseblocks. This is especially dangerous on MLC
NAND flashes which have very low eraseblock life-cycle. The proper way to deal
with flash which is used for UBI is to preserve the erase-counters. Please, use
the ubiformat utility
for these purposes. This utility may wipe out the flash and preserve erase
counters as well as properly write UBI images. Or refer
this section for some hints what
the flasher should do to be UBI-aware.
How to erase flash and preserve erase counters?
Use the ubiformat
utility. Example:
$ ubiformat /dev/mtd0 ubiformat: mtd0 (NAND), size 536870912 bytes (512.0 MiB), 131072 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes libscan: scanning eraseblock 4095 -- 100 % complete ubiformat: 4094 eraseblocks have valid erase counter, mean value is 104 ubiformat: bad eraseblocks: 13, 666 ubiformat: formatting eraseblock 4095 -- 100 % complete
Note, this has some hints for those who implement a flasher program.
How to flash UBI images and preserve erase counters?
Use the ubiformat
utility. Example:
$ ubiformat /dev/mtd0 -f ubi.img ubiformat: mtd0 (NAND), size 536870912 bytes (512.0 MiB), 131072 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes libscan: scanning eraseblock 4095 -- 100 % complete ubiformat: 4094 eraseblocks have valid erase counter, mean value is 105 ubiformat: bad eraseblocks: 13, 666 ubiformat: flashing eraseblock 50 -- 100 % complete ubiformat: formatting eraseblock 4095 -- 100 % complete
Note, this has some hints for those who implement a flasher program.
Can UBI logical eraseblocks be written randomly?
No, the flash chip restrictions have to be taken into account. This is because UBI logical eraseblocks (LEB) are mapped to physical eraseblocks (PEB), and an LEB write operation is essentially a write to the corresponding PEB plus a small offset, because there are erase counter and volume ID headers at the beginning of the PEB (see here for few more details). The important flash restrictions are:
- many flashes have minimal input-output unit size larger then 1 byte, so write offsets and lengths have to be aligned to the minimum I/O unit size; for example, in case of a NAND flash with 2KiB NAND page it is possible to write only 2, 4, 8, etc KiB chunks and only to 0, 2, 4, 8, etc KiB offsets;
- it is prohibited to write more then once to the same PEB offset;
- many NAND flashes (specifically, MLC NAND flashes) require NAND pages to be written sequentially from the beginning of the physical eraseblock, to the end of the physical eraseblock; for example, it is prohibited to write first to offset 2048, then to offset 0; once offset 2048 has been written to, it is possible to write only to further offsets.
Even if the flash chip is devoid of the last restriction, UBI anyway requires logical eraseblocks to be written sequentially from the beginning to the end; this is needed because UBI calculates data CRC when moving logical eraseblocks to other physical eraseblocks (see here to realize why), so a write operation to before the furthest data offset causes a CRC error;
What for does UBI need headers in physical eraseblocks?
The headers are needed to keep track of erase counters and physical-to-logical eraseblock associations. There are two UBI headers stored in each PEB:
- erase counter header (or EC header) which is mostly needed to store the erase counter of the PEB;
- volume identifier header (or VID header) which stores volume ID and LEB number this PEB belongs to.
Note, there are also some other data stored in EC and VID headers, see
ubi-media.h for more details.
Why does UBI need two headers, not just one?
UBI maintains two per-eraseblock headers because it needs to write different information on flash at different moments of time:
- after a PEB is erased, the EC header is written straight away, which minimizes the probability of losing the erase counter due to an unclean reboot;
- when UBI associates a PEB with a LEB, the VID header is written to the PEB.
When the EC header is written to a PEB, UBI does not yet know the volume ID and LEB number this PEB will be associated with. This is why UBI needs to do two separate write operations and to have two separate headers.
Why UBI does not use OOB area of NAND flashes?
Because many flashes (e.g., NOR) do not have OOB and UBI was designed to be a generic wear-leveling layer. Also, modern MLC NAND flashes use whole OOB area for the ECC checksum, so there is no room for application data.
What is volume table?
Volume table is an on-flash internal UBI data structure containing information about each volume on this UBI device (e.g., volume size, name, type, etc.). Each time a volume is created, removed or re-sized, or updated, the volume table is altered. UBI maintains two copies of the volume table for reliability and power-off tolerance reasons.
Is UBI tolerant of power failures?
Yes, UBI is designed to be tolerant of power failures and unclean reboots.
May UBI be used on MLC flash?
Yes, it may, as long as the flash is supported by the MTD layer. UBI does not use OOB and it requires data to be written sequentially (see here). UBI guarantees that the difference between maximum and minimum erase-counters is withing certain threshold, which is 4096 by default. Since MLC flashes have quite low eraseblock life-cycle (about 1000-10000, unlike 100000-1000000 for SLC NAND and NOR flashes), the threshold has to be set to a lower value (e.g., 256). This may be done via the Linux kernel configuration menu.
Note, unlike UBI, JFFS2 uses random wear-leveling algorithm, which is in fact not completely random, because JFFS2 makes it more probable to garbage collect eraseblocks with more dirty data. This means that JFFS2 is not really appropriate for MLC flashes. However, it is possible to use JFFS2 file-system on top of UBI (see this section) to improve wear-leveling.
How to debug UBI?
Use fake MTD device
When debugging UBI one doesn't have to use a real embedded platform with real flash. In many cases, it is easier to use a PC with an MTD device emulator and run UBI on top of this emulated MTD device. In fact, this is how most of the UBI development was done.
There are 3 MTD device emulators in Linux kernel available:
- mtdram which simulates NOR flash in RAM;
- nandsim which simulates NAND flash in RAM;
- block2mtd which simulates NOR flash on top of a block device;
For example, to get a 32MiB fake NOR flash, run
$ modprobe mtdram total_size=32768
or to get a 64MiB fake NAND flash, run
$ modprobe nandsim second_id_byte=0x36
See here for more information about the NAND simulator.
To ensure that you have fake MTD devices, run "cat /prof/mtd".
It should print something like
dev: size erasesize name mtd0: 02000000 00020000 "mtdram test device" mtd1: 04000000 00004000 "NAND simulator partition"
The fake MTD devices may further be attached to UBI (see here).
Enable debugging
Enable UBI debugging support in the configuration menu (the "UBI debugging" check-box). When debugging is enabled, UBI prints more information about errors, and adds extra assertions in the code which may help to catch bugs.
In many cases, it is enough to just enable debugging. But sometimes it is also useful to enable extra-self checks, which make sure internal data structures are consistent and may catch the problem much earlier then it would have been noticed otherwise. But the checks make UBI much slower.
Debugging messages
Sometimes it is necessary to make UBI print about what it is doing. This is where UBI messages may be enabled. When they are enabled, UBI prints a lot and the kernel ring buffer and this makes it slower. This section describes few tricks and techniques which might be useful when debugging using the messages.
The Linux kernel has internal ring buffer where all the debugging prints go.
User-space applications like syslogd usually read data from the
ring buffer, do further processing and the prints usually end up in the system
log file. When the UBI debugging messages are enabled, it prints huge amount of
messages. What happens is that the user-space processes are unable to fetch
them from the ring buffer with this pace so most of the messages are just lost.
Namely, they are just over-written with newer message (the buffer is a "ring").
There are 2 ways to gather all the messages:
- use serial console;
- use very large ring buffer.
The first method is usually appropriate when debugging on an small embedded platform connected to a PC via serial line. What you have to be aware of is that the messages are printed to the serial console synchronously, which means that the system is blocked and waiting for the print operation to be finished. So if there are many prints, the system speed becomes limited to the serial console baud rate. And obviously, it is recommended to use higher baud rates, e.g. 115200.
The UBI debugging messages have "debugging" level 7 and they are usually not
printed to the console. You may use dmesg -n8 command to make all
kernel messages to go to the console. One useful trick is to boot the kernel
with ignore_loglevel option, in which case it will print all
messages to the console unconditionally.
The second method is more appropriate when debugging on a machine with a lot
of RAM, for example on a desktop PC with a flash emulator. Just make your ring
buffer large, e.g. 64MiB by booting the kernel with log_buf_len=64M
option. This will make the ring buffer fit enough messages to identify the
problem. And because the messages are printed to RAM, this is way quicker than
the first method.