在linux-2.6.27以前,談到Flash文(wen)件(jian)(jian)系(xi)(xi)(xi)統(tong),大(da)家很多時候多會想到cramfs、jffs2、yaffs2等(deng)文(wen)件(jian)(jian)系(xi)(xi)(xi)統(tong)。它(ta)們也(ye)都(dou)是(shi)基于文(wen)件(jian)(jian)系(xi)(xi)(xi)統(tong)+mtd+flash設備(bei)的(de)架構。linux-2.6.27后,內核(he)加入(ru)了一種新(xin)型的(de)flash文(wen)件(jian)(jian)系(xi)(xi)(xi)統(tong)UBI(Unsorted Block Images)。這(zhe)里簡單介紹下(xia)UBI文(wen)件(jian)(jian)系(xi)(xi)(xi)統(tong)加入(ru)的(de)原(yuan)因,及使用方法。我(wo)也(ye)是(shi)剛(gang)接觸(chu)到這(zhe)個(ge)文(wen)件(jian)(jian)系(xi)(xi)(xi)統(tong),可能有理解不對的(de)地方,也(ye)請指正。
一、產(chan)生的(de)背景
FLASH具(ju)有(you)的“先(xian)擦除再(zai)寫入”、壞塊(kuai)、“有(you)限(xian)的讀(du)寫次(ci)數”等(deng)特性,目前(qian)管理FLASH的方法主要有(you):
1、采用(yong)MTD+FTL/NFTL(flash 轉換層/nand flash轉換層)+ 傳統(tong)文件(jian)系(xi)統(tong),如(ru):FAT、ext2等。FTL/NFTL的(de)使用(yong)就是(shi)針對(dui)FLASH的(de)特有屬性,通(tong)過軟(ruan)件(jian)的(de)方(fang)(fang)式來(lai)實現(xian)日志管(guan)理(li)、壞塊(kuai)管(guan)理(li)、損益均(jun)衡(heng)等技術。但(dan)實踐證明,由(you)于知識產權、效率等各(ge)方(fang)(fang)面因素(su)導致本方(fang)(fang)案有一定(ding)的(de)局限性。
2、采用硬(ying)件翻譯層+傳(chuan)統文(wen)件系統的方案。這種方法(fa)被很多存儲卡產品采用,如:SD卡、U盤等。這種方案對于一些產品來說,成(cheng)本較高。
3、采用MTD+ FLASH專用文(wen)件系統,如JFFS1/2,YAFFS1/2等。它(ta)們大大提高了FLASH的管理能力,并(bing)被廣泛應用。
JFFS2、YAFFS2等專用文件(jian)(jian)系統也存(cun)在著一些技術瓶頸,如:內(nei)存(cun)消(xiao)耗大,對FLASH容量、文件(jian)(jian)系統大小、內(nei)容、訪問(wen)模式(shi)等的線性依賴(lai),損益均衡能力差或過(guo)渡損益等。在此背景下(xia)內(nei)核加入了UBI文件(jian)(jian)系統的支(zhi)持。
二、用法
環境:omap3530處理器(qi)、 (128MByte 16 位(wei)NAND Flash) 、linnux-2.6.28內核
1、配置內(nei)核(he)支持(chi)UBIFS
Device Drivers --->Memory Technology Device (MTD) support --->UBI - Unsorted block images --->Enable UBI
配置mtd支持UBI接口
File systems --->Miscellaneous filesystems --->UBIFS file system support
配置內核(he)支持UBIFS文件系統
2、將一個MTD分區4掛載(zai)為UBIFS格式
● flash_eraseall /dev/mtd4 //擦除mtd4
● ubiattach /dev/ubi_ctrl -m 4 //和mtd4關聯
● ubimkvol /dev/ubi0 -N rootfs -s 100MiB //設定volume 大小(不是固定值,可以用工具改變)及名稱
● mount -t ubifs ubi0_0 /mnt/ubi或mount -t ubifs ubi0:rootfs /mnt/ubi
3、制作UBIFS文件系(xi)統
在(zai)制(zhi)作(zuo)UBI鏡像時(shi),需要首先確(que)定以下幾個參數:
MTD partition size; //對應的FLASH分區大小
flash physical eraseblock size; // FLASH物理擦除塊大小
minimum flash input/output unit size; //小的FLASH輸入輸出單元大小
for NAND flashes - sub-page size; //對于nand flash來說,子頁大小
logical eraseblock size.//邏(luo)輯擦除塊大小
參數可以由幾種方式得到
1)如果(guo)使用的是(shi)2.6.30以(yi)(yi)后的內核,這些信息可以(yi)(yi)通過工具從內核獲得,如:mtdinfo –u。
2)之前的內核可以(yi)通過以(yi)下(xia)方法:
● MTD partition size:從內核的分區表或cat /proc/mtd獲得
● flash physical eraseblock size:從flash芯片手冊中可以得到FLASH物理擦除塊大小,或cat /proc/mtd
● minimum flash input/output unit size:
1)nor flash:通常是1個字節
2)nand falsh:一個頁面
● sub-page size:通過flash手冊獲得
● logical eraseblock size:對(dui)于有(you)子(zi)頁(ye)(ye)的NAND FLASH來(lai)說,等于“物理(li)擦除塊(kuai)大小(xiao)-1頁(ye)(ye)的大小(xiao)”
3)也(ye)可以通過(guo)ubi和(he)mtd連接時(shi)的產生的信息獲取(qu),如:
#modprobe ubi mtd=4 //ubi作(zuo)為模塊加載
或
#ubiattach /dev/ubi_ctrl -m 4 //通過ubiattach關聯MTD
UBI: attaching mtd4 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 512 (aligned 512)
UBI: data offset: 2048
UBI: attached mtd4 to ubi0
更詳細(xi)的解釋(shi)參見//www.linux-mtd.infradead.org/doc/ubi.html#L_overhead
#mkfs.ubifs -r rootfs -m 2048 -e 129024 -c 812 -o ubifs.img
#ubinize -o ubi.img -m 2048 -p 128KiB -s 512 /home/lht/omap3530/tools/ubinize.cfg
-r:制定文件內容的位置
-m:頁面大小
-e:邏輯擦除塊大小
-p:物理擦除塊大小
-c:大的邏輯擦除塊數量
對我們這種情況,文件系統多可以訪問卷上的129024*812=100M空間
-s:小(xiao)(xiao)的硬件輸(shu)(shu)入(ru)輸(shu)(shu)出(chu)頁面大小(xiao)(xiao),如:k9f1208為256(上下半頁訪問)
其中(zhong),ubinize.cfg的內容為:
[ubifs]
mode=ubi
image=ubifs.img
vol_id=0
vol_size=100MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize
4、利(li)用uboot燒寫、啟(qi)動UBIFS鏡像
1)燒寫UBIFS鏡像
OMAP3 DevKit8000 # mmcinit
OMAP3 DevKit8000 # fatload mmc 0:1 81000000 ubi.img
reading ubi.img
12845056 bytes read
OMAP3 DevKit8000 # nand unlock
device 0 whole chip
nand_unlock: start: 00000000, length: 268435456!
NAND flash successfully unlocked
OMAP3 DevKit8000 # nand ecc sw
OMAP3 DevKit8000 # nand erase 680000 7980000
NAND erase: device 0 offset 0x680000, size 0x7980000
Erasing at 0x7fe0000 -- 100% complete.
OK
OMAP3 DevKit8000 # nand write.i 81000000 680000 $(filesize)
NAND write: device 0 offset 0x680000, size 0xc40000
Writing data at 0x12bf800 -- 100% complete.
12845056 bytes written: OK
燒寫過程(cheng)和燒寫內核鏡(jing)像的(de)過程(cheng)一致,所以UBI文件系(xi)統應該不像yaffs文件系(xi)統那樣用(yong)到了nand的(de)OOB區域。
2)設(she)置UBIFS文件(jian)系統(tong)作為根(gen)文件(jian)系統(tong)啟動(dong)的參數
OMAP3 DevKit8000 # setenv bootargs console=ttyS2,115200n8 ubi.mtd=4 root=ubi0:rootfs
rootfstype=ubifs video=omapfb:mode:4.3inch_LCD
OMAP3 DevKit8000 # setenv bootcmd nand read.i 80300000 280000 200000\;bootm 80300000
根文件系統的位(wei)置在MTD4上
系統啟動時(shi)會打印(yin)出如下和UBI相關的(de)信息:
Creating 5 MTD partitions on "omap2-nand":
0x00000000-0x00080000 : "X-Loader"
0x00080000-0x00260000 : "U-Boot"
0x00260000-0x00280000 : "U-Boot Env"
0x00280000-0x00680000 : "Kernel"
0x00680000-0x08000000 : "File System"
UBI: attaching mtd4 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 512 (aligned 512)
UBI: data offset: 2048
UBI: attached mtd4 to ubi0
UBI: MTD device name: "File System"
UBI: MTD device size: 121 MiB
UBI: number of good PEBs: 970
UBI: number of bad PEBs: 2
UBI: max. allowed volumes: 128
UBI: wear-leveling threshold: 4096
UBI: number of internal volumes: 1
UBI: number of user volumes: 1
UBI: available PEBs: 0
UBI: total number of reserved PEBs: 970
UBI: number of PEBs reserved for bad PEB handling: 9
UBI: max/mean erase counter: 2/0