Sunday, January 15, 2012

How to mount an UBI image

UBI and UBIFS do not work on top of block devices. So simple loop-mount will not work.

UBI is an abstraction layer that works on top of MTD raw flash devices. It's worth to note that raw flash is not the typical pen-drive, memory card or SSD but it is a flash chip without a FTL. UBI is usually it's used in embedded devices.

UBI is usually used as a supporting layer for the UBIFS filesystem. It's a successor to JJFS2 which does not use a supporting layer like UBI but works directly on MTD devices. UBIFS mounting works across these layers:

/mnt/fs (mount point) => /dev/ubi0_0 (UBIFS) => /dev/ubi0 (UBI) => /dev/mtd0 (MTD raw flash device)
TODO: make a simple diagram here

UBIFS images are created by mkfs.ubifs. UBI images are created by the ubinize utility taking UBIFS images as input. Note that an UBI image may contain multiple UBIFS filesystems.

For further information about UBI and UBIFS check the documentation and FAQ.

To mount an UBI image an MTD device is needed. If you have it on your system you can use it to flash the UBI image to the device. If you want to do this on a regular computer you need to emulate it. One way to do it is to use the mtdram linux kernel module. You need root to do it like this:

modprobe mtdram total_size=X

X is the size in KB of the emulated MTD device. Make sure it is larger than the image you wish to mount. To check the status of MTD devices you can use:

cat /proc/mtd

You should see a mtd0 device. Now you need to prepare it and flash the UBI image into it:

flash_erase /dev/mtd0 0 0
ubiformat /dev/mtd0 -f image.ubi

Now the MTD device contains the contents of the UBI image. Now you need to tell the UBI module to use it:

modprobe ubi
ubiattach -p /dev/mtd0

This will create UBI device (/dev/ubi0) and  the UBIFS (/dev/ubi0_0) partitions. Now just mount it:

mount -t ubifs /dev/ubi0_0 /mnt/ubifs

You could also use nandsim but then you would have to know the bytes of READ ID response of the particular flash chip and pass these during modprobe.

4 comments:

eidtech said...

Thanks for your post, it is very helpful. I am trying to mount one ubi image with your method, but I have some problems at the first step because my image is 512 MB and I read on the Internet the limit of MTD device is 64 MB.

Do you know if there is another way to do the same for larger images?

Thanks in advance!

Rico70 said...

Worked great up until ubiattach -p /dev/mtd0

# ubiattach -p /dev/mtd0
ubiattach: error!: cannot attach "/dev/mtd0"
error 22 (Invalid argument)


Before that, ubiformat and modprobe ubi worked with no errors.

# ubiformat /dev/mtd0 -f ubi.img
ubiformat: mtd0 (ram), size 72704000 bytes (69.3 MiB), 554 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 1 bytes
libscan: scanning eraseblock 553 -- 100 % complete
ubiformat: 554 eraseblocks are supposedly empty
ubiformat: flashing eraseblock 547 -- 100 % complete
ubiformat: formatting eraseblock 553 -- 100 % complete


The ubiattach version I am using is
ubiattach version 1.5.2 - a tool to attach MTD device to UBI.

Any hints how to pass this blocker?

Eoin D said...

I had the same issue..

mad_cow said...

for me helped using nandsim and the option "-O 2048" for ubiformat and ubiattach. the 2048 have been looked up in dmesg