linux UBI文件系统制作与升级【转】

转自:https://blog.csdn.net/lichengjun238/article/details/88821225

第1章 UBI文件系统制作
前期准备工具源码

制作好的普通根文件系统rootfs
制作工具
交叉编译工具: arm-linux-gcc-4.4.3-20100728.tar.gz
下载 zlib-1.2.5.tar.gz :http://www.zlib.net/
下载 lzo-2.06.tar.gz :http://www.oberhumer.com/opensource/lzo/download/
下载e2fsprogs-1.41.14.tar.gz: http://e2fsprogs.sourceforge.net/
下载 mtd-utils-1.4.6.tar.bz2: http://debian.mirror.inra.fr/debian/pool/main/m/mtd-utils/
平台工具

linux平台: VMware Workstation Pro + Ubuntu12
目标平台: imx.6
编译工具链: linux-ARM交叉工具链
注意事项
2. 制作文件系统时,存放的文件系统及相关文件,要放在ubuntu的家目录‘~’下,不能放在共享目录下;
3. 通过shell命令来完成文件系统制作

1.1 编译开发板上使用的UBI工具命令
部分编译参数:

–host:编译主机
–target:目标平台
–prefix:安装目录
CC:交叉工具链路径
相关文件的目录位置

根文件系统rootfs: /home/filesystem/armLinux/rootfs
工具链: /home/filesystem/FriendlyARM
工具源码:/home/filesystem/mtd
(1) 编译安装 e2fsprogs
tar xzvf e2fsprogs-1.41.14.tar.gz
cd e2fsprogs-1.41.14
./configure --host=i686-linux --target=arm-linux CC=/home/filesystem/FriendlyARM/toolschain/4.4.3/bin/arm-linux-gcc --prefix=/home/filesystem/FriendlyARM/toolschain/4.4.3/ arm-none-linux-gnueabi
make
sudo make install-libs ( 只需要 uuid 库, 所以不需要完全安装)
cd ..

结果:可 以 在 工 具 链 目 录/home/filesystem/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi,看到uuid/uuid.h 文件已经安装在include,libuuid.a已经安装在lib目录。

(2) 编译安装 zlib
tar xzvf zlib-1.2.5.tar.gz
cd zlib-1.2.5
./configure --host=i686-linux --target=arm-linux CC=/home/filesystem/FriendlyARM/toolschain/4.4.3/bin/arm-linux-gcc --prefix=/home/filesystem/FriendlyARM/toolschain/4.4.3/ arm-none-linux-gnueabi
make
sudo make install
cd ..

结果:zconf.h 和 libz.a 都安装到了工具链目录 include 和 lib。

(3) 编译安装 lzo
ar xzvf lzo-2.06.tar.gz
cd lzo-2.06
./configure --host=i686-linux --target=arm-linux --prefix=/home/filesystem/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi CC=/home/filesystem/FriendlyARM/toolschain/4.4.3/ bin/arm-linux-gcc
make
sudo make install
cd ..

结果: liblzo2.a 已经拷贝到工具链的 lib 目录。

(4) 编译安装 mtd-utils
tar xzvf mtd-utils-1.4.6.tar.gz
cd mtd-utils-1.4.6
export CFLAGS="-static -O2 -g"
export CROSS=/home/filesystem/FriendlyARM/toolschain/4.4.3/bin/arm-linux-
export DESTDIR=/home/install (这样声明不行,生成的文件在当前文件下的home目录下)
make WITHOUT_XATTR=1
sudo make install

安装出现错误:

LD ftl_check
CC mkfs.jffs2.o
mkfs.jffs2.c:71:21: 致命错误: sys/acl.h:没有那个文件或目录编译中断。
make:***[/home/filesystem/FriendlyARM/ArmLinux/source/mtd-utils-v1.4.8-d37fcc0/mkfs.jffs2.o] 错误 1。

解决方案:

sudo apt-get install uuid-dev libacl1-dev liblzo2-dev

或者不用管这个错误,我们需要的工具已经生成了。ubi-utils 子目录下生成我们需要的 ubiformat、ubiattach、 ubimkvol 等文件(请确保是交叉编译所得), 将需要的工具复制添加到目标板的根文件系统中即可:

cd ./home/filesystem/FriendlyARM/toolschain/4.4.3/bin/arm-linux
cp * /home/filesystem/armLinux/rootfs/usr/sbin
cd ..
cp share/man/man1 /home/filesystem/armLinux/rootfs/usr/share/man/man1

1.2 编译制作UBI镜像工具命令(ubuntu)
1.2.1 安装UBI编译依赖工具库
在ubuntu中建立home/filesystem/mtd/install 目录,将zlib-1.2.5.tar.gz、tar xvf lzo-2.06.tar.gz、e2fsprogs-1.42.tar.gz这3 个 source code 包拷贝到~/mtd目录下。

(1) zlib
tar xvf zlib-1.2.5.tar.gz
cd zlib-1.2.5/
./configure --prefix=~/mtd/install
make
make install
cd ..

(2) lzo
tar xvf lzo-2.06.tar.gz
cd lzo-2.06/
./configure --prefix=~/mtd/install
make
make install
cd ..

(3) e2fsprogs
tar xvf e2fsprogs-1.42.tar.gz
cd e2fsprogs-1.42/
./configure --prefix=~/mtd/install
make
make install
cd lib/uuid/
make install
cd ../../../

1.2.2 编译MTD Utilite
在编译 mtd-utils 之前,先修改 makfile 文件:

$ vi Makefile
PREFIX = ~/mtd/install
ZLIBCPPFLAGS = -I$(PREFIX)/include
LZOCPPFLAGS = -I$(PREFIX)/include
ZLIBLDFLAGS = -L$(PREFIX)/lib
LZOLDFLAGS = -L$(PREFIX)/lib
LDFLAGS += $(ZLIBLDFLAGS) $(LZOLDFLAGS)
CFLAGS ?= -O2 -g $(ZLIBCPPFLAGS) $(LZOCPPFLAGS)
$ vi common.mk
#PREFIX=/usr

然后依照如下指令编译:

$ WITHOUT_XATTR=1 make
make install DESTDIR=~/mtd/install
cd ..

制作 ubi 文件系统所用的工具在如下目录下:

~/mtd/install/home/<username>/mtd/install/sbin/mkfs.ubifs
~/mtd/install/home/<username>/mtd/install/sbin/ubinize

1.3 UBI文件系统镜像制作
UBI系统的特性参数必须与NAND FLASH分区I/O单元空间大小、页大小、块逻辑大小、块物理大小、总块数等参数特性相匹配。所以在制作UBI镜像之前,必须找到和计算出这些参数。

(1)NAND FLASH特性
我们可以NAND FLASH手册获取其特性,也可以从内核启动打印信息获取。正常启动顶盖时,观察终端打印信息,可以占到挂着根文件系统为MTD分区的mtd4,NAND FLASH特性如下:

GPMI NAND driver registered.(IMX)
UBI: attaching mtd4 to ubi0
UBI: physical eraseblock size: 262144 bytes (256 KiB)
UBI: logical eraseblock size: 253952 bytes
UBI: smallest flash I/O unit: 4096
UBI: VID header offset: 4096 (aligned 4096)
UBI: data offset: 8192
UBI: max. sequence number: 11

UBI: MTD device size: 480 MiB
UBI: number of good PEBs: 1920
UBI: number of bad PEBs: 0
UBI: number of corrupted PEBs: 0
UBI: max. allowed volumes: 128

可以得出mtd4分区大小480MiB、I/O单元大小4096Bytes、块物理大小为256KiB,块逻辑大小253952Bytes、总块数1920块,这样可以计算出制作UBI文件系统参数值:
(2)UBI镜像制作

mkfs.ubifs -F -r /home/filesystem/armLinux/rootfs/ -m 4096 -e 253952 -c 1919 -o ubifs.img
ubinize -o ubi.img -m 4096 -p 256KiB -s 4096 ubinize.cfg

ubinize.cfg内容如下:
[ubifs]

mode=ubi
image=ubifs.img
vol_id=0
vol_size=464MiB
vol_type=dynamic
vol_alignment=1
vol_name=ubifs0
vol_flags=autoresize

根据使用的NAND FLASH特性,这里使用一下参数计算如下:
-m: 4096
-s: 4096
-e: 253952 = 64(页数一般固定)4096 - 2
-c: 1919 = 1920 - 1
制作的UBI文件系统空间大小:464MiB = 1919253952/1024/1024

参数介绍:
-F:使能自动填补空白空间"white-space-fixup",通过uboot或者ubiformat/nandwrite工具烧写必须使能此项,不管哪种方式最好都加上。
-r:制作UBI的源文件系统目录
-m:NAND FLASH 的最小读写单元,一般为 page size
-e:LEB size,对于 AM335x 的 NAND driver,为 block size-2x(page size)
-c:文件系统所占用的最大 block 数,一般小于等于 block count -1,文 件 系 统 最 多 可 以 访 问 卷 上 的256KiB*1919=464M空间

第2章 A9 UBI文件系统升级
2.1 u-boot方式烧写
使用ubinize命令制作的带有卷标的UBIFS文件系统镜像可以直接使用nand flash的烧写命令烧写到NAND FLASH上,具体命令为:

tftp 0x10800000 ubi.img
nand erase 0x2000000 0x1E000000
nand write 0x10800000 0x2000000 0x92c0000
1
2
3
0x10800000:下载到的内存地址
0x2000000:A9中文件系统起始地址
0x1E000000:A9 NAND FLASH分区大小(480MiB)
0x8d00000:ubi.img的大小,根据其实际大小来定

结果:烧写成功,重启开发板正常运行,更换不同镜像也成功,经过多次试验都能成功。

2.2 挂载U盘方式烧写
前期准备:以下工具命令、镜像拷贝到U盘目录sbin/armtool/(随意更改)下。

UBI文件系统镜像:ubi.img

制作出来的工具

ubiformat
(flash_erase)
(nandwrite)
reboot

2.2.1 ubiformat工具烧写UBI镜像
(1) 直接执行命令
./ubiformat -q /dev/mtd4 -f ubi.img

结果:失败,/dev/mtd4 busy

ubiformat: error!: please, first detach mtd4 (/dev/mtd4) from ubi0
$./ubidetach –p /dev/mtd4
ubidetach: error!: cannot detach "/dev/mtd4"
error 16 (Device or resource busy)

结果:失败,出错,命令执行不成功。

(2) 在线升级前先解除掉占用A9文件系统的程序和挂载
根据第一次的错误ubiformat: error!: please, first detach mtd4 (/dev/mtd4) from ubi0,所以先要解除ubi0关联和占用文件系统的程序,即kill掉占用文件系统的应用、umount掉mtd4分区(mtd4分区挂载UBI文件系统)。

killall WorkStationApp //kill掉占用文件系统的应用
sleep 3

cd /mnt/udisk/sda1/sbin/armtool/ //这个只是我U盘放镜像和命令的目录

umount /mnt/rwfs/ //umount挂载的文件系统
umount /tmp/
umount /dev/shm/
umount /dev/pts/
umount /sys/
umount /
sleep 1

./ubiformat -s 4096 /dev/mtd4 -f ./ubi.img //擦除和烧写UBI文件系统镜像
sleep 2

./reboot

防止烧写失败脚本
=====================================================
#!/bin/sh

echo "## start update ubi image ###"

killall WorkStationApp
if [ $? -ne 0 ]
then
echo "kill WorkStationApp failed!"
exit 1
fi

sleep 5
cd /mnt/udisk/sda1/updateA9FileSystem/

umount /mnt/rwfs/
if test $? -ne 0
then
echo "1 failed!"
./reboot
fi

umount /tmp/
if test $? -ne 0
then
echo "2 failed!"
./reboot
fi

umount /dev/shm/
if test $? -ne 0
then
echo "3 filed!"
./reboot
fi

umount /dev/pts/
if test $? -ne 0
then
echo "4 failed!"
./reboot
fi

umount /sys/
if test $? -ne 0
then
echo "5 failed!"
./reboot
fi

umount /
if test $? -ne 0
then
echo "6 failed!"
./reboot
fi

sleep 1
./ubiformat -s 4096 /dev/mtd4 -f ./ubi.img

sleep 1

./reboot

==========================================================================
结果:烧写成功,重启开发板正常运行,更换不同镜像也成功,经过多次试验基本都没问题。

2.2.2 flah_erase&nandwrite工具烧写UBI镜像
killall WorkStationApp
sleep 3

umount /sys/
umount /

sleep 1
cd /mnt/udisk/sda1/sbin/armtool/

./flash_erse /dev/mtd4 0x0 0x0 //擦除mtd4整个分区
./nandwrite –a /dev/mtd4 ./ubi.img

sleep 2
./reboot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
结果:烧写成功,重启开发板正常运行,经过多次试验基本都没问题。
说明:还需继续测试,调整执行命令的参数

第3章遇到的问题
(1)UBI烧写错误问题
升级UBI文件系统不成功,基本上是由于以下两原因导致的:
其一,制作UBI文件系统与NAND FLASH参数不匹配。对应1.3小节中UBI镜像制作。
其二,UBI文件系统镜像没有被完全写入或者写入错误,其实这是由于A9中的文件系统被系统的其他程序占用了,烧写没能正确写入的原因。

ubi卷(volume)没有创建: 原因其一

UBI error: ubi_read_volume_table: the layout volume was not found
UBI error: ubi_init: cannot attach mtd4
1
2
校验出错:原因其一

UBI error: ubi_io_read: error -74 (ECC error) while reading 253952 bytes from PEB 3:8192, read 253952 bytes
UBI error: ubi_io_read: error -74 (ECC error) while reading 253952 bytes from PEB 3:8192, read 253952 bytes
1
2
读取ubifs节点出错:原因可能是其一,也可能是其二

UBIFS error (pid 1): ubifs_read_node: bad node type (255 but expected 0)
UBIFS error (pid 1): ubifs_read_node:bad node at LEB 552:57664,LEB mapping status1
UBIFS error (pid 1): ubifs_iget: failed to read inode 6066, error -22
UBIFS error (pid 1): ubifs_lookup: dead directory entry 'utmp', error -22

挂载ubifs失败:原因可能是其一,也可能是其二

UBI error: vtbl_check: volume table check failed: record 0, error 9
UBI error: ubi_init: cannot attach mtd4
No filesystem could mount root, tried: ubifs
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

(2)其他问题
执行命令时,MTD设备报资源忙/已关联错

ubiformat: error!: please, first detach mtd4 (/dev/mtd4) from ubi0
$./ubidetach –p /dev/mtd4
ubidetach: error!: cannot detach "/dev/mtd4"
error 16 (Device or resource busy)
1
2
3
4
解决:
1、 kill应用程序
2、 umount掉该MTD分区挂载操作:
(1)mount查看挂载的文件系统

root@freescale /WorkStationApp$ mount
rootfs on / type rootfs (rw)
ubi0:rootfs on / type ubifs (rw,relatime)
proc on /proc type proc (rw,relatime)
sys on /sys type sysfs (rw,relatime)
tmpfs on /dev type tmpfs (rw,relatime,mode=755)
/dev/sda1 on /mnt/udisk/sda1 type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp936,iocharset=utf8,shortname=mixed,errors=remount-ro)
devpts on /dev/pts type devpts (rw,relatime,mode=600)
shm on /dev/shm type tmpfs (rw,relatime)
rwfs on /mnt/rwfs type tmpfs (rw,relatime,size=512k)
tmpfs on /tmp type tmpfs (rw,relatime,size=65536k)

(2)kill掉应用,解除挂载

killall WorkStationApp
sleep 3

umount /mnt/rwfs/ //umount挂载的文件系统
umount /tmp/
umount /dev/shm/
umount /dev/pts/
umount /sys/
umount /

命令执行
1
2
3
4
5
6
7
8
9
10
11
第4章 参考博客
http://blog.chinaunix.net/uid-15706699-id-2646284.html
http://www.cnblogs.com/leaven/archive/2011/04/19/2021388.html
https://blog.csdn.net/fulinus/article/details/8844127
————————————————
版权声明:本文为CSDN博主「Bill Jenkins」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lichengjun238/article/details/88821225

posted @ 2022-07-01 15:33  Sky&Zhang  阅读(1649)  评论(0编辑  收藏  举报