<PAGE>
<VAR match="VAR_ORIGIN" replace="../" />
<VAR match="VAR_CVSID" replace="Last updated: 13 Jan 2009, dedekind"/>
<INCLUDE file="../inc/header.tmpl" />

<VAR match="VAR_SEL_FAQ" replace="selected" />
<VAR match="VAR_SEL_UBI" replace="selected" />
<PARSE file="../menu1.xml" />

<INCLUDE file="../inc/content.tmpl" />

<h1>UBI FAQ and HOWTO</h1>

<h2>Table of contents</h2>
<ol>
	<li><a href="ubi.html#L_enable_ubi">How do I enable UBI?</a></li>
	<li><a href="ubi.html#L_attachmtd">How do I attach an MTD device?</a></li>
	<li><a href="ubi.html#L_mkvol">How do I create/delete UBI volumes?</a></li>
	<li><a href="ubi.html#L_run_jffs2">How do I run JFFS2 on top of an UBI volume?</a></li>
	<li><a href="ubi.html#L_ext2_over_ubi">Can I run ext2 on top of UBI?</a></li>
	<li><a href="ubi.html#L_squashfs_over_ubi">Can I run squashfs on top of UBI?</a></li>
	<li><a href="ubi.html#L_format_mtd">Do I have to format my empty flash before running UBI on top of it?</a></li>
	<li><a href="ubi.html#L_ubierase">How do I erase flash and preserve erase counters?</a></li>
	<li><a href="ubi.html#L_ubi_mkimg">How do I create UBI images?</a></li>
	<li><a href="ubi.html#L_find_min_io_size">How do I find out min. I/O unit size, sub-page size, and LEB size?</a></li>
	<li><a href="ubi.html#L_flash_img">How do I flash UBI images and preserve erase counters?</a></li>
	<li><a href="ubi.html#L_restrict">Can UBI logical eraseblocks be written randomly?</a></li>
	<li><a href="ubi.html#L_why_no_oob">Why UBI does not use OOB area of NAND flashes?</a></li>
	<li><a href="ubi.html#L_crash_safe">Is UBI tolerant of power failures?</a></li>
	<li><a href="ubi.html#L_bad_blocks_exceeded">What happens when the PEBs reserved for bad block handling run out?</a></li>
	<li><a href="ubi.html#L_mlc">May UBI be used on MLC flash?</a></li>
	<li><a href="ubi.html#L_vid_offset_mismatch">Why does ubiattach on a freshly formatted device fail with "Invalid argument"?</a></li>
	<li><a href="ubi.html#L_subpage">What is a sub-page?</a></li>
	<li><a href="ubi.html#L_subpage_verify_fail">I get "ubi_io_write: error -5 while writing 512 bytes to PEB 5:512"</a></li>
	<li><a href="ubi.html#L_no_vid_header">I get "no VID header found at PEB 7923, only 0xFF bytes"</a></li>
	<li><a href="ubi.html#L_ecc_error">I get: "ubi_io_read: error -74 (ECC error) while reading 126976 bytes from PEB 47:4096, read 126976 bytes"</a></li>
	<li><a href="ubi.html#L_force_no_subpage">How do I force UBI to ignore sub-pages?</a></li>
	<li><a href="ubi.html#L_flasher">How do I implement UBI flasher?</a></li>
	<li><a href="ubi.html#L_bgt_thread">What does the "ubi_bgt0d" thread do?</a></li>
	<li><a href="ubi.html#L_attach_faster">How do I speed up UBI initialization</a></li>
	<li><a href="ubi.html#L_dyn_faster">Why a dynamic volume is faster to access than a static volume of the
same size?</a></li>
	<li><a href="ubi.html#L_how_debug">How do I debug UBI?</a></li>
	<li><a href="ubi.html#L_how_send_bugreport">How do I send an UBI bug report?</a></li>
</ol>



<h2><a name="L_enable_ubi">How do I enable UBI?</a></h2>

<p>In the Linux configuration menu, go to "<b>Device Drivers</b>" ->
"<b>Memory Technology Devices (MTD)</b>" -> "<b>UBI - Unsorted block images</b>",
and mark the "<b>Enable UBI</b>" check-box. UBI may be either compiled into the
kernel or be built as a kernel module.</p>



<h2><a name="L_attachmtd">How do I attach an MTD device?</a></h2>

<p>Each MTD device has a name and a number, which you may find out by
examining the <code>/proc/mtd</code> file. The preferable way to attach
MTD devices is to attach them by name, not by number, because MTD device
numbers may change if you change the layout of your flash, while the
names will supposedly stay the same.</p>

<p>If UBI is compiled as a kernel module, it is enough to specify the MTD
device name or number to attach in the module arguments, e.g.</p>

<pre>
$ modprobe ubi mtd=rootfs
$ modprobe ubi mtd=0
</pre>


<p>loads the UBI kernel module and attaches MTD device named "rootfs" or with
number 3 (<code>mtd3</code>). And</p>

<pre>
$ modprobe ubi mtd=config mtd=rootfs
$ modprobe ubi mtd=3 mtd=5
</pre>

<p>command loads UBI kernel module and attaches MTD devices named "config"
and "rootfs", or <code>mtd3</code> and <code>mtd5</code>.</p>

<p>If UBI is compiled into the kernel, the mtd device to attach may be
specified in the <code>ubi.mtd=</code>kernel boot parameter, e.g.,</p>

<pre>
ubi.mtd=rootfs
ubi.mtd=0
</pre>

<p>command makes UBI attach MTD device named "rootfs" or <code>mtd3</code> when
the kernel is booting. And</p>

<pre>
ubi.mtd=config ubi.mtd=rootfs
ubi.mtd=3 ubi.mtd=5
</pre>

<p>command makes UBI attach MTD devices named "config" and "rootfs", or
<code>mtd3</code> and <code>mtd5</code>.</p>

<p>And finally, MTD devices may be attached or detached at any time with the
<code>ubiattach</code> and <code>ubidetach</code>
<a href="../doc/ubi.html#L_usptools">utilities</a>; For example,</p>

<pre>
$ ubiattach /dev/ubi_ctrl -m 3
</pre>

<p>attaches <code>mtd3</code>. But these utilities will work with kernel
versions starting from version <code>2.6.25</code>. And someone should
update them and teach to accept MTD device names, not only MTD device
numbers.</p>



<h2><a name="L_mkvol">How do I create/delete UBI volumes?</a></h2>

<p>Use the <code>ubimkvol</code> and <code>ubirmvol</code>
<a href="../doc/ubi.html#L_usptools">utilities</a>. For example, the below command
creates an 128MiB volume on UBI device 0:</p>

<pre>
$ ubimkvol /dev/ubi0 -N rootfs -s 128MiB
</pre>

<p>and the following command removes it:</p>

<pre>
$ ubirmvol /dev/ubi0 -n 0
</pre>

<p>For additional information, use <code>ubimkvol -h</code> and
<code>ubirmvol -h</code>.</p>



<h2><a name="L_run_jffs2">How do I run JFFS2 on top of an UBI volume?</a></h2>

<p>There is an additional driver called <code>gluebi</code> which can emulate
fake MTD devices for each UBI volume and JFFS2 can be used with these emulated
MTD devices. Enable <code>gluebi</code> in the UBI configuration menu (the
"<i>MTD devices emulation driver (gluebi)</i>" option).</p>

<p>Note, pre-<code>2.6.31</code> kernels did not have a separate
<code>gluebi</code> driver and it was built into the UBI driver. The
corresponding UBI configuration menu option was
"<i>Emulate MTD devices</i>"</p>



<h2><a name="L_ext2_over_ubi">Can I run ext2 on top of UBI?</a></h2>

<p>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 <a href="../doc/ubi.html#L_rednote">big red note</a>
and <a href="../doc/ubi.html#L_overview">overview</a> documentation sections to
realize why.</p>

<p>However, given UBI takes care of many flash complexities, it provides a
bad-block-free block device access on top of UBI volumes. This feature is useful to
mount read-only file systems.</p>

<h2><a name="L_squashfs_over_ubi">Can I run squashfs on top of UBI?</a></h2>

<p>Yes. UBI allows to create a read-only block device on top of a UBI volume
which is suitable for read-only, block-oriented file systems, such as squashfs.
See the <a href="../doc/ubi.html#L_block">UBI block device</a> section for more details.</p>

<h2><a name="L_format_mtd">
	Do I have to format my empty flash before running UBI on top of it?
</a></h2>

<p>Ideally, yes, but not necessarily.</p>

<p>Roughly speaking, "UBI-formatted MTD device" has each PEB erased and
containing a valid <a href="..doc//ubi.html#L_ubi_headers">EC header</a>.
However, it is not always possible because many a lot of existing software is
not UBI-aware and cannot preserve erase counters. Often all it can do is to wipe
out the flash. This is why UBI can to deal with empty flash perfectly well - it
just automatically formats it.</p>

<p>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. The formatting process takes time, though.</p>

<p>But keep in mind that every time you erase the flash, you lose erase
counters, so you lose 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. Try to use UBI-aware utilities and
flashing programs.</p>



<h2><a name="L_ubierase">
	How do I erase flash and preserve erase counters?
</a></h2>

<p>Use the <a href="../doc/ubi.html#L_usptools"><code>ubiformat</code></a>
utility. Example:</p>

<pre>
$ 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
</pre>

<p><b>Note!</b> If you use an old kernel, which is older than 2.6.30 and where
MTD does not support sysfs, you have to pass "<code>-s</code>" argument to
<code>ubiformat</code>.</p>

<p><a href="../doc/ubi.html#L_flasher_algo">This</a> section has some hints
for those who implement a custom UBI flasher.</p>



<h2><a name="L_ubi_mkimg">How do I create UBI images?</a></h2>

<p>UBI images may be created using the
<a href="../doc/ubi.html#L_usptools"><code>ubinize</code></a> utility. This
utility takes the configuration file on input and generates an UBI image on
output. The input configuration file describes all UBI volumes which the
resulting UBI image has to contain. The configuration file has the
<code>ini</code>-file syntax. Here is an example:</p>

<pre>
$ cat config.ini
[configuration-data-volume]
mode=ubi
image=config_data.img
vol_id=0
vol_size=512KiB
vol_type=static
vol_name=configuration

[rootfs-volume]
mode=ubi
image=rootfs.img
vol_id=1
vol_size=220MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize

$ ./ubinize -o ubi.img -p 128KiB -m 512 -s 256 config.ini
</pre>

<p>The <code>config.ini</code> file tells UBIFS to create 2 volumes:</p>
<ul>
	<li>static configuration volume of 512KiB in size, assign it ID 0 and
	name "configuration"; the contents of the volume should be taken from
	the <code>config_data.img</code> file;</li>
	<li>dynamic root file-system volume of 220MiB in size, assign it ID 1
	and name "rootfs"; the contents of the volume should be taken from
	the <code>rootfs.img</code> file; this volume should also have
	"auto-resize" flag which means the size of this volume will be amended
	when UBI runs for the first time; namely, UBI will make this volume
	larger by giving available eraseblocks; this may be very useful in case
	of NAND flash (see <a href="../doc/ubi.html#L_autoresize">here</a> for
	more details).</li>
</ul>

<p>So in the above example, <code>ubinize</code> basically reads 3 input
files:</p>
<ul>
	<li>The <code>config.ini</code> file which describes how many volumes
	should the resulting <code>ubi.img</code> file contain, their sizes,
	names, and so on; it also refers the files containing the data which
	should be put to the volumes; note, if the volume is supposed to be
	empty, just do not specify the image file;</li>
	<li>the <code>config_data.img</code> image file for the first
	volume;</li>
	<li>the <code>rootfs.img</code> image file for the second volume.</li>
</ul>

<p>Users often wonder why <code>ubinize</code> needs a configuration file. The
answer is that one UBI image may contain many UBI volumes with different
characteristics and it is difficult to invent a nice command-line interface for
specifying all those characteristics. Thus a configuration file is used. But
feel free sending a patch which teaches <code>ubinize</code> working without
the configuration file if there is only one volume.</p>

<p>Note, UBI reserves <i>physical</i> flash space for volumes. Namely, UBI
reserves a physical eraseblock for each logical eraseblock. The size if LEB
130560 bytes in our example (found out by running <code>ubinize</code>
with <code>-v</code> option), which means that the configuration volume will
have 5 LEBs ([512 * 1024] / 130560 rounded up) and the root file-system
volume will have at least 1767 LEBs. This means that the MTD device the
<code>ubi.img</code> is going to be flashed to has to have at least 1772
physical eraseblocks, which is about 221MiB. But because of the UBI overhead
(see <a href="../doc/ubi.html#L_overhead">this</a> section), the MTD device
actually has to be at least 225MiB in size. Of course it may be larger,
in which case the "rootfs" volume will be re-sized and take the rest of the
flash space (because of the auto-resize flag).</p>

<p>
The implications of the above paragraph are important. The
<code>vol_size</code> option effectively represents the minimum size of the
flash where the volume will be installed. If you are working with multiple
devices (i.e. you are producing an image to be flashed on various devices,
even when 'identical'), the amount of usable flash <em>will</em> vary because
some devices have more bad blocks than others. Excluding the
<code>vol_size</code> option will cause vol_size to be automatically
calculated based on the size of the input image, and this will produce
maximum robustness in the face of varying numbers of bad blocks on target
devices. You can combine this with the autoresize functionality so that the
maximum amount of free space is made available upon first mount.
</p>

<p>Also, the <code>config_data.img</code> and <code>rootfs.img</code> input
files do not have to be 512KiB and 220MiB respectively, but may be smaller if
they contain less data. In this case the resulting <code>ubi.img</code> file
will also be smaller than 221MiB. All the <code>ubinize</code> utility does is
it takes the image files, splits them to LEB-sized chunks, forms PEB data by
adding <a href="../doc/ubi.html#L_ubi_headers">UBI headers</a> to these LEB
chunks, and writes the result to the output file. It also writes the
<a href="../doc/ubi.html#L_vol_table">volume table</a> (2 physical
eraseblocks). Thus, <code>ubi.img</code> file size will be small if the input
volume images are small. And <code>ubinize</code> does not do any further
padding.</p>

<p>Please, use the Use the
<a href="../doc/ubi.html#L_usptools"><code>ubiformat</code></a> utility to
flash the resulting UBI image. Also, you can find detailed description of how
UBI-aware flashing programs should work in
<a href="../doc/ubi.html#L_flasher_algo">this</a> section.</p>



<h2><a name="L_find_min_io_size">
	How do I find out min. I/O unit size, sub-page size, and LEB size?
</a></h2>

<p>If you run kernel version <code>2.6.30</code> or later, the easiest
way to find out all these parameters is to run the
<a href="../doc/ubi.html#L_usptools"><code>mtdinfo</code></a>
tool, which reports them. Of course, the tool has to be run on the
target.</p>

<p>Otherwise, first find out the physical eraseblock (PEB) size from
the flash manual, or from the <code>/proc/mtd</code> file on the target.</p>

<p>Refer <a href="../doc/ubi.html#L_min_io_unit">this</a> and
<a href="../doc/ubi.html#L_subpage">this</a> sections for some hints about
what the min. I/O unit and sub-page sizes may be for you.</p>

<p>The LEB size is defined by:</p>
<ul>
	<li>physical eraseblock size;</li>
	<li>minimum input/output unit size;</li>
	<li>sub-page size;</li>
</ul>

<p>Please, read <a href="../doc/ubi.html#L_overhead">this</a> section for
information about how LEB size is calculated (it is denoted by <i>O</i>
there).</p>



<h2><a name="L_flash_img">
	How do I flash UBI images and preserve erase counters?
</a></h2>

<p>Use the <a href="../doc/ubi.html#L_usptools"><code>ubiformat</code></a>
utility. Example:</p>

<pre>
$ 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
</pre>

<p>Note, <a href="../doc/ubi.html#L_flasher_algo">this</a> section has
describes how UBI-aware flasher program has to work.</p>

<p><b>Important!</b>: even if you do not care about erasecounters, do not use
<code>dd</code> for flashing UBI images to NAND flashes, because
<code>dd</code> does handle bad eraseblocks.</p>

<p><b>Important!</b>: even if you do not care about erasecounters, do not use
<code>nandwrite</code> for flashing UBI images containing UBIFS file-system to
NAND flashes, read <a href="ubifs.html#L_why_ubiformat">here</a> for
more information.</p>



<h2><a name="L_restrict">
	Can UBI logical eraseblocks be written randomly?
</a></h2>

<p>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. The important flash restrictions are:</p>

<ol>
	<li>all writes have to be aligned to the
	<a href="../doc/ubi.html#L_min_io_unit">min. I/O unit</a> size and be
	multiple of min. I/O unit size;</li>

	<li>sometimes it is prohibited to write more then once to the same PEB
	offset (e.g., in case of NAND flash);</li>

	<li>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 first write to offset 2048, then to offset 0; once offset
	2048 has been written to, it is possible to write only to further
	offsets.</li>
</ol>

<p>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 because UBI calculates data CRC when moving logical
eraseblocks to other physical eraseblocks (see
<a href="../doc/ubi.html#L_lebchange_det">here</a> for more information),
so a write an offset which is less than the furthest written data offset causes
CRC errors;</p>



<h2><a name="L_why_no_oob">
	Why UBI does not use OOB area of NAND flashes?
</a></h2>

<p>Because many flashes (e.g., NOR) do not have OOB and UBI was designed to be
<i>generic</i>. Also, modern MLC NAND flashes use whole OOB area for the ECC
checksum, so there is no room for application data.</p>

<p>But of course, things could be optimized for SLC NAND flashes if UBI used
the space available in the OOB area. This is not implemented, but one could
probably do this.</p>



<h2><a name="L_crash_safe">Is UBI tolerant of power failures?</a></h2>

<p>Yes, UBI is designed to be tolerant of power failures and unclean
reboots.</p>



<h2><a name="L_bad_blocks_exceeded">What happens when the PEBs reserved for bad block handling run out?</a></h2>

<p>By default, about 2% of the whole chip size (20/1024 PEB) are reserved for
<a href="ubi.html#L_max_beb">bad blocks handling.</a>
If the number of blocks that turn bad exceeds that allocation, an error
message will be printed and UBI will switch to read-only mode.</p>
<p>Note: If at attach time, there's already more bad blocks than reserved PEBs,
UBI will stay in read-write mode. The switching to read-only mode only occurs
when a new bad block appears.</p>



<h2><a name="L_mlc">May UBI be used on MLC flash?</a></h2>

<p>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
<a href="ubi.html#L_restrict">here</a>). 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.</p>

<p>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 <a href="ubi.html#L_run_jffs2">this</a>
section) to improve wear-leveling.</p>



<h2><a name="L_vid_offset_mismatch">
Why does ubiattach on a freshly formatted device fail with "Invalid argument"?
</a></h2>

<p>On NAND devices that support sub-page accesses, <code>ubiformat</code>
may choose a different location for the
<a href="../doc/ubi.html#L_ubi_headers">VID header</a> to the kernel UBI driver.
This can result in the following error when attaching to a UBI device:</p>

<pre>
$ ubiformat /dev/mtd0
ubiformat: mtd0 (NAND), size 260046848 bytes (248.0 MiB), 131072 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
[...]
$ ubiattach /dev/ubi_ctrl -m 0
ubiattach: error!: cannot attach mtd0
           error 22 (Invalid argument)
</pre>

<p>and in <code>dmesg</code> you will see:</p>

<pre>
UBI error: validate_ec_hdr: bad VID header offset 2048, expected 512
UBI error: validate_ec_hdr: bad EC header
UBI error: ubi_io_read_ec_hdr: validation failed for PEB 0
</pre>

<p>This happens because <code>ubiformat</code> assumes the flash does not
support sub-pages, because the kernel does not expose sub-page information
to user-space (which should be fixed when sysfs support is added to MTD).
However, the kernel UBI driver assumes sub-pages are supported and sub-page
size is 512 bytes in our example. To fix this, you should override the default
sub-page size that <code>ubiformat</code> uses to what the kernel expects using
the <code>-s</code> option of <code>ubiformat</code>. For example, if you see
the error above in dmesg, you can tell <code>ubiformat</code> to assume
512-byte sub-page by executing:</p>

<pre>
$ ubiformat /dev/mtd0 -s 512
</pre>

<p>Or you may pass "<code>-O 512</code>" would have the same effect as
"<code>-s 512</code>" - the VID header would be put at offset 512.</p>

<p>Alternately, you may wish to actually attach to the UBI device by forcing
VID header offset to be 2048 bytes. In other words, you may ask UBI to avoid
using sub-pages. This is not recommended since this will require more storage
overhead, but may be useful if your NAND driver incorrectly reports that it can
handle sub-page accesses when it should not. To do this with
<code>ubiattach</code>, use:</p>

<pre>
$ ubiattach /dev/ubi_ctrl -m 0 -O 2048
</pre>

<p>or on the kernel command-line, pass:</p>

<pre>
ubi.mtd=0,2048
</pre>



<h2><a name="L_subpage">What is a sub-page?</a></h2>

<p>Please, refer to <a href="../doc/ubi.html#L_subpage">this</a> section.</p>



<h2><a name="L_subpage_verify_fail">I get "ubi_io_write: error -5 while writing 512 bytes to PEB 5:512"</a></h2>

<p>If you have a 2048 bytes per NAND page device, and have
<code>CONFIG_MTD_NAND_VERIFY_WRITE</code> enabled in your kernel, you will need
to turn it off. The code did not currently (as of 2.6.26) perform verification
of sub-page writes correctly. As UBI is one of the few users of sub-page
writes, not much else seemed to be affected by this bug. In Linux 3.7,
<code>CONFIG_MTD_NAND_VERIFY_WRITE</code> was removed:

<pre>
commit 657f28f8811c92724db10d18bbbec70d540147d6
Date:   Tue Aug 14 22:38:45 2012 -0400

    mtd: kill MTD_NAND_VERIFY_WRITE

    "Both UBI and JFFS2 are able to read verify what they wrote already.
    There are also MTD tests which do this verification. So I think there
    is no reason to keep this in the NAND layer, let alone wasting RAM in
    the driver to support this feature. Besides, it does not work for sub-pages
    and many drivers have it broken. It hurts more than it provides benefits."

    So kill MTD_NAND_VERIFY_WRITE entirely.
</pre>

</p>



<h2><a name="L_no_vid_header">I get "no VID header found at PEB 7923, only 0xFF bytes"</a></h2>

<p>The messages mean that UBI could not find
<a href="../doc/ubi.html#L_ubi_headers">VID header</a> in the eraseblock, but
the header supposed to be there. This probably means some corruption.</p>

<p>However, if you have UBI "build" debugging messages enabled,
(<code>CONFIG_MTD_UBI_DEBUG_MSG_BLD=y</code>), you may see a lot of these
messages and they are harmless. They are just debugging messages in this
case.</p>



<h2><a name="L_ecc_error">
I see this UBI error: "ubi_io_read: error -74 (ECC error) while reading 126976 bytes from PEB 47:4096, read 126976 bytes"
</a></h2>

<p>The <code>-74</code> error code is <code>-EBADMSG</code> and means an ECC
error. In other words, UBI tried to read some data from the flash, but the
flash driver found that there is an uncorrectable ECC error, and returned
<code>-EBADMSG</code>.</p>

<p>There may be many reasons for this. It may be because your NAND driver is
buggy, or you HW is buggy. We recommend you to validate the driver using the
<a href="../doc/general.xml#L_mtd_tests">MTD tests</a>.</p>

<p>The other possibility is that you failed to flash your UBI/UBIFS image
properly. Try to erase your flash, then attach it to UBI/UBIFS without writing
any image, and check if you still have these errors.</p>

<p>If you do not have errors when you mount empty flash, it is probably indeed
related to how you flash the UBI/UBIFS images. One typical problem is related
to ECC calculation algorithm - read
<a href="ubifs.html#L_why_ubiformat">here</a> for more information. Make sure
that you use <a href="../doc/ubifs.html#L_usptools">ubiformat</a>, or make sure
either that your flashing program skips <code>0xFF</code> properly (see
<a href="../doc/ubi.html#L_flasher_algo">here</a>) or that your UBIFS image
was generated with the "free space fixup" flag set (see
<a href="ubifs.html#L_free_space_fixup">here</a>).</p>

<p>Another possibility is that your flash reports that it supports
<a href="../doc/ubi.html#L_subpage">sub-pages</a>, but does not actually support
them properly.</p>



<h2><a name="L_force_no_subpage">How do I force UBI to ignore sub-pages?"</a></h2>

<p>If your NAND flash supports
<a href="../doc/ubi.html#L_subpage">sub-pages</a>, UBI will use them. But
sometimes you may want to ask UBI to ignore sub-pages, for example if you have
an UBI image which was made for a similar NAND flash, but without sub-pages, or
if you just want to disable sub-pages for testing purposes. You may do this
by forcing UBI to read/write the VID header from/to the beginning of the second
page, instead of the second sub-page (which is the default behavior). And UBI
uses sub-pages <i>only</i> for VID headers, so this is enough.</p>

<p>If you attach MTD devices using the
<a href="../doc/ubi.html#L_usptools"><code>ubiattach</code></a> tool - just
use the <code>--vid-hdr-offset</code> option and specify the second page
offset. For example, if you have 2048-byte NAND pages, use
<code>--vid-hdr-offset 2048</code>. All the below examples also assume
2048-byte NAND pages.</p>

<p>If you do not use <code>ubiattach</code> and instead specify the MTD device
to attach using kernel boot parameters (see
<a href="ubi.html#L_attachmtd">here</a>), then you may put VID header offset
after the MTD device name or number. For example:</p>

<pre>
$ ubi.mtd=rootfs,2048
$ ubi.mtd=3,2048
</pre>

<p>will attach MTD device named "rootfs" (or MTD device number 3) and force
UBI to read/write VID headers from/to offset 2048, which is the second NAND
page in the eraseblock.</p>

<p>Similarly, if you use module load parameters, try</p>

<pre>
$ modprobe ubi mtd=rootfs,2048
$ modprobe ubi mtd=3,2048
</pre>

<p>And of course, if you have an MTD partition which is already formatted
so that sub-pages are used, which means all VID headers sit at offsets 512
(second sub-page of a 2048-byte NAND page), you cannot force UBI to stop using
sub-pages (it will fail). You may do this only if you are attaching an empty
MTD partition, or if the flash is already formatted so that all VID headers
are at offset 2048. This means that if you use or
<a href="../doc/ubi.html#L_usptools"><code>ubinize</code></a>, you should
avoid using the <code>-s</code> parameter.</p>



<h2><a name="L_flasher">How do I implement UBI flasher?</a></h2>

<p>Please, read <a href="../doc/ubi.html#L_flasher_algo">here</a></p>



<h2><a name="L_bgt_thread">What does the "ubi_bgt0d" thread do?</a></h2>

<p>The UBI back-ground thread is a per-UBI device thread which has
"<i>ubi_bgtXd</i>" name, where "<i>X</i>" is the UBI device number. For
example, "<code>ubi_bgt0d</code>" is a background thread corresponding to UBI
device 0.</p>

<p>The UBI background thread is doing background physical eraseblock erasure.
This is an important optimization which greatly improves UBI I/O throughput
(applications do not have to wait for erasure completion). For example,
UBI <a href="../doc/ubi.html#L_unmap">unmap</a> operation schedules physical
eraseblocks for erasure.</p>

<p>The background thread also
<a href="../doc/ubi.html#L_torturing">tortures</a> faulty physical
eraseblocks.</p>

<p>The UBI background thread also moves data from more worn-out physical
eraseblocks to less worn out, i.e., performs wear-leveling. It also moves data
from physical eraseblocks which have bit-flips.  See the
<a href="../doc/ubi.html#L_unmap">UBI overview</a> section for some more
information.</p>

<p>Note, UBI may work without the background thread, so the thread is just an
optimization, although a very important one.</p>



<h2><a name="L_attach_faster">How do I speed up UBI initialization</a></h2>

<p>As <a href="../doc/ubi.html#L_scalability">this</a> section explains, UBI
has scalability limitations imposed by its design, so there is no much you
can do about it but without re-designing. Here are some ideas</p>

<ul>
	<li>When UBI attaches an MTD device, it reads a lot of data from the
	media. If you manage to improve read speed on the driver level, you
	will also speed up UBI initialization.</li>

	<li>The MTD layer supplies upper layers (including UBI) with information
	about bad PEBs. It keeps so-called bad block table in RAM, which is
	usually 1 bit per PEB. When the driver initializes, it has to build
	this table by scanning whole flash media, which normally includes
	reading OOB area of 1st NAND page of each PEB. This takes time and may
	be improved by using on-flash BBT (bad block table). In this case the
	bad PEB map is stored on flash and MTD does not have to do any
	scanning. See the <code>NAND_USE_FLASH_BBT</code> constant in the Linux
	source codes. But note, bad PEB scanning is usually minor comparing to
	the UBI scan time, so on-flash BBT is not probably going to give
	much.</li>

	<li>UBI scan time linearly depends on the number of PEB to read, which
	means that you may speed it up by merging two or more PEB and treating
	them as one composite PEB. Then from UBI POV PEB size will become
	larger, and the amount of PEBs to Scan will become smaller. The best
	way to do this is probably to teach UBI merging PEBs. It does not have
        to merge adjacent PEB. Instead, UBI can store the paired PEB number in
	the EC header. If one of the PEBs in the pair becomes bad, the pair may
	be "re-constructed" by moving the good PEB somewhere else and
	substituting the bad PEB by a good one from the pool of reserved
	PEBs.</li>

	<li>Design and implement UBI2. You may teach UBI to store erase
	counters and mapping information in a separate internal volume, instead
	of storing it in each PEB. Then you could implement the "anchor" idea
	to quickly find that internal volume (see
	<a href="../doc/JFFS3design.pdf">here</a>). This would solve slow UBI
	attach problem, but not linear memory consumption problem. Later we
	could have UBI3 which could solve more issues. This plan was suggested
	<a href="http://lists.infradead.org/pipermail/linux-mtd/2009-February/024437.html">
	here</a>. Indeed, it is easier to solve one problem at a time.</li>
</ul>


<h2><a name="L_dyn_faster">
Why a dynamic volume is faster to access than a static volume of the same size?
</a></h2>

<p>Static UBI volumes were originally designed to store blobs of data like
configuration files. Static volumes provide CRC-32 protection for the stored
data, and static volumes know how much data they store. E.g., if you have a
static volume <code>/dev/ubi0_1</code> of 254KiB in size consisting of 2
127KiB LEBs, and you store a 200KiB file there, and you read whole
<code>/dev/ubi0_1</code> , you'll get exactly 200KiB. If
<code>/dev/ubi0_1</code>  was a dynamic volume, you'd read 254KiB. So static
volumes make it easier to store blobs of data.</p>

<p>Thus, static volumes are slower to access (comparing to dynamic
volumes) because UBI has to check the data CRC-32 checksum. And note, it is not
a good idea to use static volumes with R/O UBIFS because UBIFS also protects
all the information it stores in the UBI volume with CRC-32 checksums, and
using static volumes with UBIFS would only slow down UBIFS mount speed.</p>


<h2><a name="L_how_debug">How do I debug UBI?</a></h2>

<h3><a name="L_how_debug_fake_mtd">Use fake MTD device</a></h3>

<p>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.</p>

<p>There are 3 MTD device emulators in Linux kernel available:</p>
<ul>
	<li><b>mtdram</b> which simulates NOR flash in RAM;</li>
	<li><b>nandsim</b> which simulates NAND flash in RAM;</li>
	<li><b>block2mtd</b> which simulates NOR flash on top of a block
	device;</li>
</ul>

<p>For example, to get a 32MiB fake NOR flash, run</p>

<pre>
$ modprobe mtdram total_size=32768
</pre>

<p>or to get a 64MiB fake NAND flash, run</p>

<pre>
$ modprobe nandsim second_id_byte=0x36
</pre>

<p>See <a href="nand.html#L_nand_nandsim">here</a> for more information about
the NAND simulator.</p>

<p>To ensure that you have fake MTD devices, run "<code>cat /proc/mtd</code>".
It should print something like</p>

<pre>
dev:    size   erasesize  name
mtd0: 02000000 00020000 "mtdram test device"
mtd1: 04000000 00004000 "NAND simulator partition"
</pre>

<p>The fake MTD devices may further be attached to UBI (see
<a href="ubi.html#L_attachmtd">here</a>).</p>


<h3>Debugging messages</h3>

<p>Sometimes it is necessary to make UBI print about what it is doing. You may
enable various UBI debugging messages using the "dynamic debug" Linux kernel
infrastructure. Please, refer to the corresponding
<a href="ubifs.html#L_debug_msgs">UBIFS section</a> for more information. Below
is a similar table for UBI.</p>

<table border="2" cellpadding="4" cellspacing="0">
<tr>
	<td><b>Message Type</b></td>
	<td><b>Magic string</b></td>
</tr>
<tr>
	<td>All messages (very noisy)</td>
	<td>'format "UBI DBG" +pf'</td>
</tr>
<tr>
	<td>General messages</td>
	<td>'format "UBI DBG gen" +pf'</td>
</tr>
<tr>
	<td>EBA messages</td>
	<td>'format "UBI DBG eba" +pf'</td>
</tr>
<tr>
	<td>Wear-levelling messages</td>
	<td>'format "UBI DBG wl" +pf'</td>
</tr>
<tr>
	<td>Input/output messages</td>
	<td>'format "UBI DBG io" +pf'</td>
</tr>
<tr>
	<td>Attach messages</td>
	<td>'format "UBI DBG bld" +pf'</td>
</tr>
</table>


<h3>Extra self-checks</h3>

<p>UBI contains various internal self-check functions which are often
very useful for debugging or testing. Please, refer to the corresponding
<a href="ubifs.html#L_how_debug_self_checks">UBIFS self-checks</a>
section for more information, because UBI extra self-checks are very
similar, just a bit simpler. Here is a similar table for UBI.</p>

<table border="2" cellpadding="4" cellspacing="0">
<tr>
	<td><b>Check type</b></td>
	<td><b>File name</b></td>
</tr>
<tr>
	<td>General checks.</td>
	<td><code>chk_gen</code></td>
</tr>
<tr>
	<td>I/O checks. Does things like verifying that eraseblocks contain
	only 0xFF bytes after erasure, verifies that all writes go to an empty
	flash area, verifies each writes by reading the data back and comparing
	to the original.</td>
	<td><code>chk_io</code></td>
</tr>
</table>

<h3><a name="L_how_debug_recovery">Power-cut recovery testing</a></h3>

<p>UBI suppors power-cut emulation for testing which emulates power-cuts after
a random number of writes. When a power-cut is emulated, UBI switches to
read-only mode and disallows any further write to the UBI volume, thus
emulating a power cut. The main idea of this mode is to emulate power cuts in
interesting places, e.g. when writing the vid header.</p>

<p>Use the <code>tst_emulate_power_cut</code> debugfs file to set flags for
when to enable power-cut emulation. Possible flags for power-cut testing
can be seen in the table below.</p>

<p>Use the files <code>tst_emulate_power_cut_min</code> and
<code>tst_emulate_power_cut_max</code> to set the minimum and maximum number
of successful writes, respectively, before a power-cut is emulated.</p>

<p>Note that UBIFS has its own power-cut emulation functions. Please, refere
to the corresponding
<a href="ubifs.html#L_how_debug_recovery">Power-cut recovery testing</a>
section.</p>

<table border="2" cellpadding="4" cellspacing="0">
<tr>
	<td><b>Emulation type</b></td>
	<td><b>Flag value</b></td>
</tr>
<tr>
	<td>Allow power-cut to be emulated during EC header write</td>
	<td>1</td>
</tr>
<tr>
	<td>Allow power-cut to be emulated during VID header write</td>
	<td>2</td>
</tr>
</table>

<h2><a name="L_how_send_bugreport">How do I send an UBI bug report?</a></h2>

<p>Basically the same way as an
<a href="ubifs.html#L_how_send_bugreport">UBIFS bug report</a></p>.


<INCLUDE file="../inc/footer.tmpl" />
</PAGE>
