View Single Post
Posts: 2,153 | Thanked: 8,462 times | Joined on May 2010
#17
I think it can be useful also for others with other devices, so here is guide how to properly generate UBI image with UBIFS volume for any MTD device. Both mkfs.ubifs and ubinize tools needs to know geometry of target MTD device (e.g. NAND) during creating of images. The best way how to detect all required parameters is to install mtdinfo utility on target system and run it with -u option for target MTD device. Also optimal value of $MAX_LEB_CNT depends on kernel config option CONFIG_MTD_UBI_BEB_LIMIT.

Here are all commands how to determinate and calculate all required parameters for target MTD device /dev/mtd0:

PEB_SIZE <-- mtdinfo /dev/mtd0 -u | sed -n 's/^Eraseblock size: *\([^ ]*\).*/\1/p'
LEB_SIZE <-- mtdinfo /dev/mtd0 -u | sed -n 's/^Default UBI LEB size: *\([^ ]*\).*/\1/p'
MIN_IO_SIZE <-- mtdinfo /dev/mtd0 -u | sed -n 's/^Minimum input\/output unit size: *\([^ ]*\).*/\1/p'
SUB_PAGE_SIZE <-- mtdinfo /dev/mtd0 -u | sed -n 's/^Sub-page size: *\([^ ]*\).*/\1/p'
EB_CNT <-- mtdinfo /dev/mtd0 -u | sed -n 's/^Amount of eraseblocks: *\([^ ]*\).*/\1/p'
BEB_LIMIT <-- gunzip -c /proc/config.gz | sed -n 's/^CONFIG_MTD_UBI_BEB_LIMIT=//p'
BEB_CNT <-- $BEB_LIMIT * $EB_CNT / 1000
MAX_LEB_CNT <-- (($EB_CNT * $PEB_SIZE) - (($BEB_CNT + 4) * $PEB_SIZE + ($PEB_SIZE - $LEB_SIZE) * ($EB_CNT - $BEB_CNT - 4))) / $LEB_SIZE
VOL_SIZE <-- Size of the generated UBIFS image file ubifs.img (minimal value)
VOL_NAME <-- Name of UBI volume for UBIFS image (e.g. "rootfs")


Calculation of $MAX_LEB_CNT is done according to official information in UBI documentation at:
http://www.linux-mtd.infradead.org/d...tml#L_overhead
Maximal number (but not larger than $MAX_LEB_CNT) for currenly attached UBI volume can be read from file:
/sys/class/ubi/ubi0_0/reserved_ebs. So it can be used for verification that $MAX_LEB_CNT was calculated optimally. Note that bad blocks on device cause lower value.

UBI image ubi.img with UBIFS volume with content of $ROOT_DIR can be generated via following commands:

Code:
/usr/sbin/mkfs.ubifs -m $MIN_IO_SIZE -e $LEB_SIZE -c $MAX_LEB_CNT -r $ROOT_DIR ubifs.img

cat > cfg.ini << EOF
[$VOL_NAME]
mode=ubi
image=ubifs.img
vol_id=0
vol_size=$VOL_SIZE
vol_type=dynamic
vol_name=$VOL_NAME
vol_flags=autoresize
vol_alignment=1
EOF

/usr/sbin/ubinize -o ubi.img -p $PEB_SIZE -m $MIN_IO_SIZE -s $SUB_PAGE_SIZE cfg.ini
Line vol_size= in cfg.ini can be omitted and ubinize will then calculate size from ubifs.img file.

For /dev/mtd5 partition in Nokia N900 with Maemo 2.6.28 kernel are following optimal/correct values:

PEB_SIZE=131072
LEB_SIZE=129024
MIN_IO_SIZE=2048
SUB_PAGE_SIZE=512
MAX_LEB_CNT=1986


As can be seen in examples, mkfs.ubifs was called by Nokia with -c option MAX_LEB_CNT=2047, but maximal value is just 1986. On my N900, kernel reports in /sys/class/ubi/ubi0_0/reserved_ebs value 1985 which proves that calculation is correct (value is smaller probably due to already bad blocks).
 

The Following 7 Users Say Thank You to pali For This Useful Post: