从uboot到kernel来使用ubifs文件系统
从uboot到kernel来使用ubifs文件系统
开发板:mini2440 (64m+128m)
交叉编译器gcc版本:gcc version 6.5.0 (crosstool-NG 1.24.0)
uboot版本:U-Boot 2020.04
kernel版本:Linux version 4.19.187
uboot配置
#define CONFIG_CMD_UBI
#define CONFIG_CMD_UBIFS
#define CONFIG_CMD_MTDPARTS
#define CONFIG_MTD_DEVICE
#define CONFIG_MTD_PARTITIONS
#define MTDIDS_DEFAULT "nand0=smdk2440-0"
/* Size must be a multiple of Nand erase size (524288 b) */
#define MTDPARTS_DEFAULT "mtdparts=smdk2440-0:2m(uboot)," \
"128k(u-boot-env)," \
"4m(kernel),-(rootfs)"
kernel配置
配置linux内核
配置的时候选上
1)Device Drivers —>Memory Technology Device (MTD) support —>UBI - Unsorted blockimages —>Enable UBI
2)File systems —>Miscellaneous filesystems —>UBIFS file system support
这样我们的内核就支持UBIFS文件系统了
ubifs镜像制作
mtd-utils工具包 下载mtd-utils工具包源码编译后获得mkfs.ubifs和ubinize工具
或者 sudo apt-get install mtd-utils
mkfs.ubifs -m 2048 -c 2048 -e 129024 -r rootfs -o rootfs.ubifs
mkfs.ubifs工具需要明确的参数:
m:页大小,如上:2K
x: 镜像压缩格式,可选 LZO
e:逻辑可擦除块大小
c: 最多逻辑可擦除数目
r(或d):指定根文件目录树
o:指定生产文件名
ubinize -o rootfs.ubi -m 2048 -p 131072 -s 512 ubifs.cfg
ubinize工具需要明确的参数:
m: 页大小,2k
p: 物理块擦除大小
s:最小的硬件输入输出页面大小,如:k9f1208为256(上下半页访问)
v:头部大小,一般是512
o: 指定输出文件
ubifs.cfg配置文件,格式如下:
[ubifs-volume]
mode=ubi
image=filename //文件名称,即mkfs.ubifs文件制作生产文件
vol_id=0
vol_size= filesize //文件大小,按实际情况来定
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize
vol_alignment=1
uboot中挂载ubifs文件系统
使用uboot挂载ubifs文件系统可以检验ubifs镜像文件制作有没有问题。
使用ubi part命令连接mtd分区, 例如链接rootfs分区, 激活当前分区会将已经激活的分区取消链接
ubi part rootfs
SMDK2440 # ubi part rootfs
ubi0: attaching mtd4
ubi0: scanning is finished
ubi0: attached mtd4 (name “rootfs”, size 121 MiB)
ubi0: PEB size: 131072 bytes (128 KiB), LEB size: 129024 bytes
ubi0: min./max. I/O unit sizes: 2048/2048, sub-page size 512
ubi0: VID header offset: 512 (aligned 512), data offset: 2048
ubi0: good PEBs: 975, bad PEBs: 0, corrupted PEBs: 0
ubi0: user volume: 1, internal volumes: 1, max. volumes count: 128
ubi0: max/mean erase counter: 1/0, WL threshold: 4096, image sequence number: 789925165
ubi0: available PEBs: 0, total reserved PEBs: 975, PEBs reserved for bad PEB handling: 20
使用ubi info 查看当前分区信息, 注意其中的PEB代表可擦除的物理页个数
每个物理页大小128K
SMDK2440 # ubi info
UBI: MTD device name: “rootfs”
UBI: MTD device size: 121 MiB
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: number of good PEBs: 975
UBI: number of bad PEBs: 0
UBI: smallest flash I/O unit: 2048
UBI: VID header offset: 512 (aligned 512)
UBI: data offset: 2048
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: 975
UBI: number of PEBs reserved for bad PEB handling: 20
UBI: max/mean erase counter: 1/0
挂载ubi0:rootfs
SMDK2440 # ubifs mount ubi0:rootfs
UBIFS: mounted UBI device 0, volume 0, name “rootfs”
UBIFS: mounted read-only
UBIFS: file system size: 32378880 bytes (31620 KiB, 30 MiB, 255 LEBs)
UBIFS: journal size: 9023488 bytes (8812 KiB, 8 MiB, 72 LEBs)
UBIFS: media format: w4/r0 (latest is w4/r0)
UBIFS: default compressor: LZO
UBIFS: reserved for root: 0 bytes (0 KiB)
查看文件系统内容:
ubifsls
DIR> 4576 Wed Jan 03 06:43:17 2018 bin
DIR> 224 Fri Sep 08 04:17:51 2017 dev
DIR> 2224 Wed Jan 03 06:43:18 2018 etc
DIR> 2960 Wed Jan 03 06:43:18 2018 lib
DIR> 232 Fri Sep 08 04:17:51 2017 mnt
DIR> 232 Fri Sep 08 04:17:51 2017 opt
DIR> 304 Wed Jan 03 06:43:18 2018 tmp
DIR> 232 Fri Sep 08 04:17:51 2017 sys
DIR> 608 Fri Sep 22 09:34:23 2017 var
DIR> 608 Wed Jan 03 06:43:18 2018 usr
DIR> 232 Fri Sep 08 04:17:51 2017 proc
DIR> 4000 Wed Jan 03 06:43:27 2018 sbin
DIR> 232 Fri Sep 22 09:34:23 2017 root
LNK> 11 Fri Sep 08 04:17:51 2017 linuxrc
LNK> 3 Fri Sep 08 04:17:51 2017 lib32
DIR> 232 Fri Sep 08 04:17:51 2017 media
DIR> 600 Wed Jan 03 06:43:58 2018 testsuite
DIR> 760 Wed Jan 03 06:43:18 2018 firmware
遇到的问题
在kernel加载ubifs文件系统的时候,出现
ubi0: attaching mtd3
ubi0 warning: ubi_io_read: error -74 (ECC error) while reading 64 bytes from PEB 0:0, read only 64 bytes, retry
ubi0 warning: ubi_io_read: error -74 (ECC error) while reading 64 bytes from PEB 0:0, read only 64 bytes, retry
ubi0 warning: ubi_io_read: error -74 (ECC error) while reading 64 bytes from PEB 0:0, read only 64 bytes, retry
ubi0 error: ubi_io_read: error -74 (ECC error) while reading 64 bytes from PEB 0:0, read 64 bytes
CPU: 0 PID: 1 Comm: swapper Not tainted 4.19.187 #6
Hardware name: MINI2440
[<c000fc88>] (unwind_backtrace) from [<c000d9c8>] (show_stack+0x10/0x18)
[<c000d9c8>] (show_stack) from [<c0583034>] (dump_stack+0x18/0x24)
[<c0583034>] (dump_stack) from [<c0375b34>] (ubi_io_read+0x128/0x31c)
[<c0375b34>] (ubi_io_read) from [<c0375f58>] (ubi_io_read_ec_hdr+0x44/0x250)
[<c0375f58>] (ubi_io_read_ec_hdr) from [<c037af98>] (ubi_attach+0x170/0x15d8)
[<c037af98>] (ubi_attach) from [<c036f7ac>] (ubi_attach_mtd_dev+0x3f4/0xb90)
[<c036f7ac>] (ubi_attach_mtd_dev) from [<c06fc8e8>] (ubi_init+0x160/0x214)
[<c06fc8e8>] (ubi_init) from [<c000a5d4>] (do_one_initcall+0x4c/0x1b0)
[<c000a5d4>] (do_one_initcall) from [<c06e4df8>] (kernel_init_freeable+0x10c/0x1d0)
[<c06e4df8>] (kernel_init_freeable) from [<c05830f0>] (kernel_init+0x8/0xf4)
[<c05830f0>] (kernel_init) from [<c00090e0>] (ret_from_fork+0x14/0x34)
Exception stack(0xc3821fb0 to 0xc3821ff8)
1fa0: 00000000 00000000 00000000 00000000
1fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1fe0: 00000000 00000000 00000000 00000000 00000013 00000000
在linux代码中去掉 hardware ECC ,换成soft ECC就没问题了