2024年2月26日发(作者:)

UBI文件系统简介

UBI文件系统简介

作者:刘洪涛,华清远见嵌入式学院金牌讲师。

在linux-2.6.27以前,谈到Flash文件系统,大家很多时候多会想到cramfs、jffs2、yaffs2等文件系统。它们也都是基于文件系统+mtd+flash设备的架构。linux-2.6.27后,内核加入了一种新型的flash文件系统

UBI(Unsorted Block Images)。这里简单介绍下UBI文件系统加入的原因,及使用方法。我也是刚接触到这个文件系统,可能有理解不对的地方,也请指正。

一、产生的背景

FLASH具有的“先擦除再写入”、坏块、“有限的读写次数”等特性,目前管理FLASH的方法主要有:

1、采用MTD+FTL/NFTL(flash 转换层/nand flash转换层)+传统文件系统,如:FAT、ext2等。FTL /NFTL的使用就是针对FLASH的特有属性,通过软件的方式来实现日志管理、坏块管理、损益均衡等技术。但实践证明,由于知识产权、效率等各方面因素导致本方案有一定的局限性。

2、采用硬件翻译层+传统文件系统的方案。这种方法被很多存储卡产品采用,如:SD卡、U盘等。这种方案对于一些产品来说,成本较高。

3、采用MTD+ FLASH专用文件系统,如JFFS1/2,YAFFS1/2等。它们大大提高了FLASH的管理能力,并被广泛应用。

JFFS2、YAFFS2等专用文件系统也存在着一些技术瓶颈,如:内存消耗大,对FLASH容量、文件系统大小、内容、访问模式等的线性依赖,损益均衡能力差或过渡损益等。在此背景下内核加入了UBI文件系统的支持。

二、用法

环境:omap3530处理器、(128MByte 16 位NAND Flash) 、

linnux-2.6.28内核

1、配置内核支持UBIFS

Device Drivers --->Memory Technology Device (MTD)

support --->UBI - Unsorted block

images --->Enable UBI

配置mtd支持UBI接口

File systems --->Miscellaneous filesystems --->UBIFS file

system support

配置内核支持UBIFS文件系统

2、将一个MTD分区4挂载为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文件系统

在制作UBI镜像时,需要首先确定以下几个参数:

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.//逻辑擦除块大小

参数可以由几种方式得到

1)如果使用的是2.6.30以后的内核,这些信息可以通过工具从内核获得,如:mtdinfo –u。

2)之前的内核可以通过以下方法:

● MTD partition size:从内核的分区表或cat /proc/mtd获得

● flash physical eras eblock 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:对于有子页的NAND FLASH来说,等于“物理擦除块大小-1页的大小” 3)也可以通过ubi和mtd连接时的产生的信息获取,如:

#modprobe ubi mtd=4 //ubi作为模块加载

#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

更d

# -r rootfs -m 2048 -e 129024 -c 812 -o

#ubinize -o -m 2048 -p 128KiB -s 512

/home/lht/omap3530/tools/

-r:制定文件内容的位置

-m:页面大小

-e:逻辑擦除块大小

-p:物理擦除块大小

详细的解释参见/doc/,/doc/#L_overhea

-c:最大的逻辑擦除块数量

对我们这种情况,文件系统最多可以访问卷上的129024*812=100M空间 -s:最小的硬件输入输出页面大小,如:k9f1208为256(上下半页访问)

其中,的内容为:

[ubifs]

mode=ubi

image=

vol_id=0

vol_size=100MiB

vol_type=dynamic

vol_name=rootfs

vol_flags=autoresize

4、利用uboot烧写、启动UBIFS镜像

1)烧写UBIFS镜像

OMAP3 DevKit8000 # mmcinit

OMAP3 DevKit8000 # fatload mmc 0:1 81000000

reading

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

烧写过程和烧写内核镜像的过程一致,所以UBI文件系统应该不像yaffs文件系统那样用到了nand的OOB 区域。

2)设置UBIFS文件系统作为根文件系统启动的参数

OMAP3 DevKit8000 # setenv bootargs

console=ttyS2,115200n8 =4 root=ubi0:rootfs

rootfstype=ubifs video=omapfb:mode:4.3inch_LCD

OMAP3 DevKit8000 # setenv bootcmd nand read.i 80300000

280000 200000;bootm 80300000

根文件系统的位置在MTD4上

系统启动时会打印出如下和UBI相关的信息:

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

imx515 uboot UBIFS移植及android UBIFS文件系统烧写

分类:BootLoad 2010-09-13 17:20 2963人阅读评论(1) 收藏举报作者:longfeey

1.1 Uboot UBI的移植

关于uboot的UBI的移植几乎没有说明介绍,移植首先要保证你的flash驱动能够跑起来,我是在nand flash 上跑的UBI。刚开始的时候我也没有什么头绪,只能够从uboot的readme开始查找一些蛛丝马迹。

- MTD Support (mtdparts command, UBI support)

CONFIG_MTD_DEVICE

Adds the MTD device infrastructure from the Linux kernel.

Needed for mtdparts command support.

CONFIG_MTD_PARTITIONS

Adds the MTD partitioning infrastructure from the Linux

kernel. Needed for UBI support.

因此,要UBI支持首先得要MTD支持,因此在配置文件中要添加以上两项的定义。

要移植UBI还要添加:

#define CONFIG_CMD_UBIFS

#define CONFIG_CMD_UBI

总的关于UBI的部分是以下几个宏:

#define CONFIG_CMD_UBI

#define CONFIG_CMD_UBIFS

#define CONFIG_CMD_MTDPARTS

#define CONFIG_MTD_DEVICE

#define CONFIG_MTD_PARTITIONS

#define CONFIG_RBTREE

#define CONFIG_LZO

同时要给NAND建立个默认的分区,方便以后操作。分区如下:

#define MTDIDS_DEFAULT "nand0=nand0"

#define MTDPARTS_DEFAULT

"mtdparts=nand0:0x100000@0x0(u-boot),0x300000@0x120000(kernel),0x7b0

0000@0x420000(rootfs),-(reserved)"

#define MTD_ACTIVE_PART "nand0,2"

以上的配置都在uboot_imx/include/configs/mx51_vdphone.h文件中进行配置。

需要注意的是增加UBI的支持之后uboot会增大到300多KB,在NAND中启动,需要修改以下文件uboot-imx/cpu/arm_cortexa8/mx51/mxc_nand_load.S

add r6, r0, #0x1E00

ldr r5, =_end /* Try get right image size */

add r5, r2, #0x00060000 /* Fixme to get actual image size */

这里使用0x60000(384K)大小,已经足够,如果实际有变化,可以进行相应调节。如果uboot传给Copy_Good_Blk 拷贝的uboot的大小小于uboot的长度的话,uboot跑不起来,移植的时候被这个问题必须注意。

这个时候就可以make 了,执行以下命令:

make clean

make mx51_vdphone_config

make all

如果正常的话会编译出在根目录下。

1.2u-boot 下ubi的使用

1.2.1配置u-boot nand 分区

通过mtdpart命令配置u-boot下的nand 分区,本项目已经在配置头文件里面设置了默认nand 分区,

#define MTDPARTS_DEFAULT

"mtdparts=nand0:0x100000@0x0(u-boot),0x300000@0x120000(kernel),0x7b0

0000@0x420000(rootfs),-(reserved)"

如果需要修改,可以通过修改默认分区列表,也可以通过命令mtdpart进行重新分区。这里使用默认分区,通过以下命令使默认分区生效:

mtdpart default //设置默认分区

saveevn //保存分区信息

1.2.2nand u-boot 烧写

通过以上的配置编译,如果成功生成,那就可以通过SD卡启动,直接烧写到nand flash了。操作步骤如下:

1)下载 到内存

tftp 0x90800000 /tftpboot/mx51/

2)擦除u-boot分区

nand erase u-boot

3)烧写u-boot到nand flash分区

nand write 0x90800000 u-boot 0x60000

1.2.3内核的烧写

内核的烧写和平常烧写方式一样,只需用nand 命令写入nand 即可,操作步骤如下:

1)擦除kernel分区

nand erase kernel

2)下载kernel到内存

tftp 0x90800000 /tftpboot/mx51/uImage 将内核通过tftp下

载到内存中

3)烧写kernel 到nand kernel分区

nand write 0x90800000 kernel 0x300000

1.2.4UBI文件系统的烧写

本项目使用的文件系统将根文件系统和system文件系统整合在一起。所以,只需要烧写整合后的文件系统即可。如果要使用ubifs文件系统作为根文件系统,在烧写之前必须通过工具将做好的文件系统制作镜像文件。 工具是通过编译mtd-utils工具下的目录即可生成的PC端UBIFS文件系统镜像制作工具。操作步骤如下:

1)制作根文件系统

-r root/ -m 2048 -e 129024 -c 2364 -o

root目录为整合android root和system文件系统后的目录,应当能够通过NFS系统的

2)擦除root分区

nand erase root

3)激活root 分区为UBI格式

ubi part root

4) 创建root分区

ubi create root

5)将文件系统下载到内存

tftp 0x90800000

6)将文件系统烧写到rootfs 中

ubi write 0x90800000 rootfs 0x339600//0x339600为tftp 下载到的镜像大小,

1.2.5设置启动参数

设置bootargs:

setenv bootargs =2 root=ubi0:rootfs

rootfstype=ubifs

console=ttymxc0,115200 wvga calibration init=/init rw

启动拨码开关5,8位置设置为ON,上电重新启动,即可从Nand

flash 启动。

1.3 android FLASH UBI文件系统的制作和烧写

将android编译为UBI文件系统格式,生成的,,就可以直接在u-boot中通过ubi write 命令烧写,前提条件是uboot已经支持或完成ubi和UBIFS

的移植工作,并且linux kernel也要支持UBIFS文件系统。

1.3.1 设置mtdpart分区

1) U-Boot中配置默认分区参数,路径如下:

# 板级相关的配置文件include/configs/mx51/xxxx.h

mtdparts:

mtdparts=nand0:0x100000@0x0(u-boot),0x300000@0x120000(kernel),0x100000@0x42

0000(ramdisk),0x4B00000@0x520000(system),0x1E00000@0x5020000(userdata),0xD0 0000@0x6E20000(cache),-(reserved)

2) 第一次烧写完boot后,设置mtdpart分区:

BBG U-Boot > mtdparts default # 加载默认分区配置

BBG U-Boot > save # 保存配置

BBG U-Boot > mtdpart # 查看分区配置

device nand0 , # parts = 7

#: name size offset mask_flags

0: u-boot 0x00100000 0x00000000 0

1: kernel 0x00300000 0x00120000 0

2: ramdisk 0x00100000 0x00420000 0

3: system 0x04b00000 0x00520000 0

4: userdata 0x01e00000 0x05020000 0

5: cache 0x00d00000 0x06e20000 0

6: reserved 0x004e0000 0x07b20000 0

active partition: nand0,0 - (u-boot) 0x00100000 @

0x00000000

defaults:

mtdids : nand0=nand0

3) 烧写U-Boot到FLASH

BBG U-Boot > tftp 0x90800000 # 获取U-Boot到内存

BBG U-Boot > nand erase u-boot # 格式化u-boot分区

BBG U-Boot > nand write 0x90800000 u-boot 0x100000 # 烧写u-boot到对应分区

4) 烧写Linux内核到FLASH

BBG U-Boot > tftp 0x90800000 uImage # 获取内核到内存

BBG U-Boot > nand erase kernel # 格式化内存分区

BBG U-Boot > nand write 0x90800000 kernel 0x300000 # 烧写内核到对应分区

5) 烧写Ramdisk到FLASH

BBG U-Boot > tftp 0x90800000 # 获取uramdisk到内存

BBG U-Boot > nand erase ramdisk # 格式化uramdisk分区

BBG U-Boot > nand write 0x90800000 ramdisk 0x100000 #

烧写uramdisk到对应分区

6) 烧写System到FLASH

BBG U-Boot > nand erase system # 擦除system分区

BBG U-Boot > tftp 0x90800000 # 获取system到内存

BBG U-Boot > ubi part system # 激活system分区为ubi格式

Creating 1 MTD partitions on "nand0":

0x000097855f98-0x : ""

UBI: attaching mtd1 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 mtd1 to ubi0

UBI: MTD device name: "mtd=3"

UBI: MTD device size: 78643200 MiB

UBI: number of good PEBs: 600

UBI: number of bad PEBs: 0

UBI: max. allowed volumes: 128

UBI: wear-leveling threshold: 4096

UBI: number of internal volumes: 1

UBI: number of user volumes: 0

UBI: available PEBs: 590

UBI: total number of reserved PEBs: 10

UBI: number of PEBs reserved for bad PEB handling: 6

UBI: max/mean erase counter: 1/1

BBG U-Boot > ubi create system # 创建system分区

Creating dynamic volume system of size 76124160

# 烧写sytem分区,大小为tftp下载完成后提示的大小

BBG U-Boot > ubi write 0x90800000 system 0x3ca9800

Volume "system" found at volume id 0

7) 烧写userdata到FLASH

BBG U-Boot > nand erase userdata # 擦除userdata分区

BBG U-Boot > ubi part userdata # 激活userdata分区为ubi格式UBI: mtd1 is detached from ubi0

Creating 1 MTD partitions on "nand0":

0x000097855f98-0x : ""

UBI: attaching mtd1 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: empty MTD device detected

UBI: create volume table (copy #1)

UBI: create volume table (copy #2)

UBI: attached mtd1 to ubi0

UBI: MTD device name: "mtd=4"

UBI: MTD device size: 31457280 MiB

UBI: number of good PEBs: 240

UBI: number of bad PEBs: 0

UBI: max. allowed volumes: 128

UBI: wear-leveling threshold: 4096

UBI: number of internal volumes: 1

UBI: number of user volumes: 0

UBI: available PEBs: 234

UBI: total number of reserved PEBs: 6

UBI: number of PEBs reserved for bad PEB handling: 2

UBI: max/mean erase counter: 0/0

BBG U-Boot > ubi create userdata # 创建userdata分区

Creating dynamic volume userdata of size 30191616

BBG U-Boot > tftp 0x90800000 # 获取userdata到内存

# 烧写userdata分区,大小为tftp下载完成后提示的大小

BBG U-Boot > ubi write 0x90800000 userdata 0x979800

Volume "userdata" found at volume id 0

8) 初始化Cache分区

BBG U-Boot > ubi part cache # 激活cache分区为ubi格式

UBI: mtd1 is detached from ubi0

Creating 1 MTD partitions on "nand0":

0x000097855f98-0x000006e20000 : ""

UBI: attaching mtd1 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: empty MTD device detected

UBI: create volume table (copy #1)

UBI: create volume table (copy #2)

UBI: attached mtd1 to ubi0

UBI: MTD device name: "mtd=5"

UBI: MTD device size: 13631488 MiB

UBI: number of good PEBs: 104

UBI: number of bad PEBs: 0

UBI: max. allowed volumes: 128

UBI: wear-leveling threshold: 4096

UBI: number of internal volumes: 1

UBI: number of user volumes: 0

UBI: available PEBs: 98

UBI: total number of reserved PEBs: 6

UBI: number of PEBs reserved for bad PEB handling: 2 UBI:

max/mean erase counter: 0/0

BBG U-Boot > ubi create cache # 创建cache分区

9) FLASH上Android的加载与启动

设置启动参数

setenv bootcmd_nand 'run bootargs_nand;nand read

${loadaddr} kernel; nand read ${rd_loadaddr} ramdisk; bootm

${loadaddr} ${rd_loadaddr}'

setenv bootargs_nand 'setenv bootargs =3

=4 =5

console=ttymxc0,115200

wvga calibration init=/init

bootcmd_nand'

saveenv

重启即可从nand flash 启动烧写的ubi文件系统

Uboot UBI 的移植

分类:BootLoad 2010-09-15 21:56 2235人阅读评论(2) 收藏举报作者:longfeey

1.1 Uboot UBI 的移植

关于uboot 的UBI 的移植几乎没有说明介绍,移植首先要保证你的flash 驱动能够跑起来,我是在nand flash 上跑的UBI 。刚开始的时候我也没有什么头绪,只能够从uboot 的readme 开始查找一些蛛丝马迹。

- MTD Support (mtdparts command, UBI support)

CONFIG_MTD_DEVICE

Adds the MTD device infrastructure from the Linux kernel.

Needed for mtdparts command support.

CONFIG_MTD_PARTITIONS

Adds the MTD partitioning infrastructure from the Linux

kernel. Needed for UBI support.

因此,要UBI 支持首先得要MTD 支持,因此在配置文件中要添加以上两项的定义。

要移植UBI 还要添加:

#define CONFIG_CMD_UBIFS

#define CONFIG_CMD_UBI

总的关于UBI 的部分是以下几个宏:

#define CONFIG_CMD_UBI

e=ttymxc0

rw' setenv bootcmd 'run

#define CONFIG_CMD_UBIFS

#define CONFIG_CMD_MTDPARTS

#define CONFIG_MTD_DEVICE

#define CONFIG_MTD_PARTITIONS

#define CONFIG_RBTREE

#define CONFIG_LZO

同时要给NAND 建立个默认的分区,方便以后操作。分区如下:

#define MTDIDS_DEFAULT "nand0=nand0"

#define MTDPARTS_DEFAULT

"mtdparts=nand0:0x100000@0x0(u-boot),0x300000@0x120000(kernel),0x7b0

0000@0x420000(rootfs),-(reserved)"

#define MTD_ACTIVE_PART "nand0,2"

以上的配置都在uboot_imx/include/configs/mx51_vdphone.h文件中进行配置。需要注意的是增加UBI 的支持之后uboot 会增大到300 多KB ,在NAND 中启动,需要修改以下文件

uboot-imx/cpu/arm_cortexa8/mx51/mxc_nand_load.S

add r6, r0, #0x1E00

ldr r5, =_end /* Try get right image size */

add r5, r2, #0x00060000 /* Fixme to get actual image size */

这里使用0x60000(384K)大小,已经足够,如果实际有变化,可以进行相应调节。如果uboot 传给Copy_Good_Blk 拷贝的uboot 的大小小于uboot 的长度的话,uboot 跑不起来,移植的时候被这个问题必须注意。

这个时候就可以make 了,执行以下命令:

make clean

make mx51_vdphone_config

make all

如果正常的话会编译出 在根目录下。

1.2 u-boot 下ubi的使用

1.2.1配置u-boot nand 分区

通过mtdpart命令配置u-boot下的nand 分区,本项目已经在配置头文件里面设置了默认nand 分区,

#define MTDPARTS_DEFAULT

"mtdparts=nand0:0x100000@0x0(u-boot),0x300000@0x120000(kernel),0x7b0

0000@0x420000(rootfs),-(reserved)"

如果需要修改,可以通过修改默认分区列表,也可以通过命令mtdpart进行重新分区。这里使用默认分区,通过以下命令使默认分区生效:

mtdpart default //设置默认分区

saveevn //保存分区信息

1.2.2nand u-boot 烧写

通过以上的配置编译,如果成功生成 ,那就可以通过SD 卡启动,直接烧写 到nand flash 了。操作步骤如下:

1)下载 到内存

tftp 0x90800000 /tftpboot/mx51/

2)擦除u-boot 分区

nand erase u-boot

3)烧写u-boot 到nand flash 分区

nand write 0x90800000 u-boot 0x60000

1.2.3内核的烧写

内核的烧写和平常烧写方式一样,只需用nand 命令写入nand 即可, 操作步骤如下:

1)擦除kernel 分区

nand erase kernel

2)下载kernel 到内存

tftp 0x90800000 /tftpboot/mx51/uImage 将内核通过tftp 下载到内存中

3)烧写kernel 到nand kernel 分区

nand write 0x90800000 kernel 0x300000

1.2.4UBI文件系统的烧写

本项目使用的文件系统将根文件系统和system 文件系统整合在一起。所以,只需要烧写整合后的文件系统即可。如果要使用ubifs 文件系统作为根文件系统,在烧写之前必须通过 工具将做好的文件系统制作镜像文件。 工具是通过编译mtd-utils 工具下的 目录即可生成的PC 端UBIFS 文件系统镜像制作工具。操作步骤如下:

1)制作根文件系统

-r root/ -m 2048 -e 129024 -c 2364 -o

root 目录为整合android root 和system 文件系统后的目录,应当能够通过NFS 系统的

2)擦除root 分区