uboot升级ubifs文件系统

1 ubifs

1.1 背景

在 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文件系统的支持。

1.2 UBIFS 简介

由IBM、nokia工程师Thomas Gleixner,Artem Bityutskiy等人于2006年发起,致力于开发性能卓越、扩展性高FLASH专用文件系统,以解决当前嵌入式环境下以FLASH作为MTD设备使用时的技术瓶颈。

UBI:一种类似于LVM的逻辑卷管理层。主要实现损益均衡,逻辑擦除块、卷管理,坏块管理等。
UBIFS:基于UBI的FLASH日志文件系统。

1.3 内核支持UBIFS

1)> Device Drivers > Memory Technology Device (MTD) support > Enable UBI - Unsorted block images

2)> File systems > Miscellaneous filesystems > UBIFS file system support

这样我们的内核就支持UBIFS文件系统了

2 ubifs制作

2.1 mtd-utils工具

mtd-utils工具中提供了对UBIFS的支持,所以我们需要下载和编译这些工具,下载以下几个文件

zlib-1.2.13.tar.gz

lzo-2.10.tar.gz

mtd-utils-2.1.5.tar.gz

Linux主机安装mtd-utils

# 编译安装zlib
./configure
make
sudo make install

# 编译安装lzo
./configure
make
sudo make install

# 编译mtd-utils
./autogen.sh
./configure --without-xattr --without-zstd UUID_LIBS=/usr/lib/x86_64-linux-gnu/libuuid.so
make -j8
sudo make install

交叉编译mtd-utils

# 编译安装zlib
export CC=arm-none-linux-gnueabi-gcc
./configure --prefix=/opt/imx6ull_toolchain
make
sudo make install

# 编译安装lzo
./configure --prefix=/opt/imx6ull_toolchain --host=arm-linux CC=arm-none-linux-gnueabi-gcc --enable-shared
make
sudo make install

# 编译mtd-utils
./configure --prefix=/opt/imx6ull_toolchain --host=arm-linux CC=arm-none-linux-gnueabi-gcc --without-xattr --without-zstd
make
sudo make install

2.2 ubifs文件系统镜像制作

mkfs.ubifs:创建UBIFS镜像

mkfs.ubifs -r rootfs -m 2048 -e 126976 -c 1823 -o rootfs.ubifs.img

-r:文件系统路径
-m:闪存的最大输入/输出单元大小
-e:逻辑擦除块大小
-c:最大的逻辑擦除块数量(maximum logical erase block count)

ubinize:从UBIFS镜像创建UBI镜像

ubinize -o rootfs.ubi.img -m 2048 -p 128KiB -s 2048 ubinize.cfg

-m:页面大小
-p:指定的是目标 flash 的 physical eraseblock 的大小
-s:子页面大小

1)以上命令的参数可从ubifs挂载信息中提取

=> ubi info
UBI: MTD device name:            "mtd=4"
UBI: MTD device size:            234 MiB
UBI: physical eraseblock size:   131072 bytes (128 KiB)
UBI: logical eraseblock size:    126976 bytes
UBI: number of good PEBs:        1879
UBI: number of bad PEBs:         0
UBI: smallest flash I/O unit:    2048
UBI: VID header offset:          2048 (aligned 2048)
UBI: data offset:                4096
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: 1879
UBI: number of PEBs reserved for bad PEB handling: 40
UBI: max/mean erase counter: 1/0

2)配置文件ubinize.cfg的内容为:

[ubifs]
mode=ubi
image=rootfs.ubifs.img
vol_id=0
vol_size=234MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize

3 uboot烧写ubifs文件系统

3.1 uboot支持ubifs配置

/* mtd分区支持 */
#define CONFIG_CMD_MTDPARTS
#define CONFIG_MTD_DEVICE
#define CONFIG_MTD_PARTITIONS
#define MTDIDS_DEFAULT		"nand0=gpmi-nand"
#define MTDPARTS_DEFAULT	"mtdparts=gpmi-nand:4m(boot),128k(env),1m(dtb),16m(kernel),-(rootfs)"

/* UBI及UBIFS支持 */
#define CONFIG_CMD_UBI
#define CONFIG_CMD_UBIFS
#define CONFIG_RBTREE
#define CONFIG_LZO

3.2 烧写UBI镜像(失败)

tftp 80800000 rootfs.ubi.img
nand erase 0x01520000 $filesize
nand write 80800000 0x01520000 $filesize

3.3 烧写UBIFS镜像

# 加载默认分区表
=> mtdparts default

# 查看分区信息
=> mtdparts

device nand0 <gpmi-nand>, # parts = 5
 #: name                size            offset          mask_flags
 0: boot                0x00400000      0x00000000      0
 1: env                 0x00020000      0x00400000      0
 2: dtb                 0x00100000      0x00420000      0
 3: kernel              0x01000000      0x00520000      0
 4: rootfs              0x0eae0000      0x01520000      0

active partition: nand0,0 - (boot) 0x00400000 @ 0x00000000

defaults:
mtdids  : nand0=gpmi-nand
mtdparts: mtdparts=gpmi-nand:4m(boot),128k(env),1m(dtb),16m(kernel),-(rootfs)

# 擦除rootfs分区
=> nand erase.part rootfs

NAND erase.part: device 0 offset 0x1520000, size 0xeae0000
Erasing at 0xffe0000 -- 100% complete.
OK

# 激活rootfs分区
ubi part rootfs
ubi0: attaching mtd1
ubi0: scanning is finished
ubi0: empty MTD device detected
ubi0: attached mtd1 (name "mtd=4", size 234 MiB)
ubi0: PEB size: 131072 bytes (128 KiB), LEB size: 126976 bytes
ubi0: min./max. I/O unit sizes: 2048/2048, sub-page size 2048
ubi0: VID header offset: 2048 (aligned 2048), data offset: 4096
ubi0: good PEBs: 1879, bad PEBs: 0, corrupted PEBs: 0
ubi0: user volume: 0, internal volumes: 1, max. volumes count: 128
ubi0: max/mean erase counter: 1/0, WL threshold: 4096, image sequence number: 0
ubi0: available PEBs: 1835, total reserved PEBs: 44, PEBs reserved for bad PEB handling: 40

# 创建UBI volume
=> ubi create rootfs 
No size specified -> Using max size (233000960)
Creating dynamic volume rootfs of size 233000960

# 下载ubifs镜像(上节制作的 rootfs.ubifs.img)
=> tftp 80800000 rootfs.ubifs.img
FEC0 Waiting for PHY auto negotiation to complete... done
Using FEC0 device
TFTP from server 10.99.20.250; our IP address is 10.99.20.222
Filename 'rootfs.ubifs.img'.
Load address: 0x80800000
Loading: #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         ##########################################
         1 MiB/s
done
Bytes transferred = 10158080 (9b0000 hex)

# 写ubifs镜像到 rootfs volume
=> ubi write 80800000 rootfs $filesize
10158080 bytes written to volume rootfs

3.4 设置UBIFS启动参数

bootargs=console=ttymxc0,115200 ubi.mtd=4 root=ubi0:rootfs rw rootfstype=ubifs

3.5 uboot升级命令及测试

#define CMD_UPKERNEL \
	"tftp 80800000 zImage;" \
	"nand erase 0x520000 $filesize;" \
	"nand write 80800000 0x520000 $filesize;" \
	"boot;\0"

#define CMD_UPDTB \
	"tftp 80800000 hybrid-v2.dtb;" \
	"nand erase 0x420000 $filesize;" \
	"nand write 80800000 0x420000 $filesize;" \
	"boot;\0"

#define CMD_UPROOTFS \
	"mtdparts default;" \
	"nand erase.part rootfs;" \
	"ubi part rootfs;" \
	"ubi create rootfs;" \
	"tftp 80800000 rootfs.ubifs.img;" \
	"ubi write 80800000 rootfs $filesize;" \
	"boot;\0"

升级文件系统指令测试

=> uprootfs

NAND erase.part: device 0 offset 0x1520000, size 0xeae0000
Erasing at 0xffe0000 -- 100% complete.
OK
ubi0: attaching mtd1
ubi0: scanning is finished
ubi0: empty MTD device detected
ubi0: attached mtd1 (name "mtd=4", size 234 MiB)
ubi0: PEB size: 131072 bytes (128 KiB), LEB size: 126976 bytes
ubi0: min./max. I/O unit sizes: 2048/2048, sub-page size 2048
ubi0: VID header offset: 2048 (aligned 2048), data offset: 4096
ubi0: good PEBs: 1879, bad PEBs: 0, corrupted PEBs: 0
ubi0: user volume: 0, internal volumes: 1, max. volumes count: 128
ubi0: max/mean erase counter: 1/0, WL threshold: 4096, image sequence number: 0
ubi0: available PEBs: 1835, total reserved PEBs: 44, PEBs reserved for bad PEB handling: 40
No size specified -> Using max size (233000960)
Creating dynamic volume rootfs of size 233000960
FEC0 Waiting for PHY auto negotiation to complete... done
Using FEC0 device
TFTP from server 10.99.20.250; our IP address is 10.99.20.222
Filename 'rootfs.ubifs.img'.
Load address: 0x80800000
Loading: #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         ##########################################
         947.3 KiB/s
done
Bytes transferred = 10158080 (9b0000 hex)
10158080 bytes written to volume rootfs

NAND read: device 0 offset 0x520000, size 0x600000
 6291456 bytes read: OK

NAND read: device 0 offset 0x420000, size 0xc800
 51200 bytes read: OK
Kernel image @ 0x80800000 [ 0x000000 - 0x4dca50 ]
## Flattened Device Tree blob at 83000000
   Booting using the fdt blob at 0x83000000
   Using Device Tree in place at 83000000, end 8300b325

Starting kernel ...
posted @ 2025-06-20 11:12  silencehuan  阅读(174)  评论(0)    收藏  举报