nandsim是在PC机上模拟nand flash设备的一个小模块,本文使用这个工具,在pc机器上模拟一块nand flash.
然后在虚拟的nand flash上对UBI相关的操作进行测试。这样操作的好处就是不需要搭建开发板的环境,在PC
机环境下,快速测试UBI模块。
1、载入相关模块
PC机上不一定有mtd和mtdblock模块,要进行整个实验必须先保证PC机上已经安装了这两个模块,并使用下面的
命令载入这两个模块:
- modprobe mtd
- modprobe mtdblock
- andy@andy-desktop:~/test$ sudo modprobe nandsim
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$ mtdinfo /dev/mtd0
- mtd0
- Name: NAND simulator partition 0
- Type: nand
- Eraseblock size: 16384 bytes, 16.0 KiB
- Amount of eraseblocks: 8192 (134217728 bytes, 128.0 MiB)
- Minimum input/output unit size: 512 bytes
- Sub-page size: 256 bytes
- OOB size: 16 bytes
- Character device major/minor: 90:0
- Bad blocks are allowed: true
- Device is writable: true
-
- andy@andy-desktop:~/test$ ls /dev/mtd
- mtd0 mtd0ro mtdblock0
- andy@andy-desktop:~/test$ ls /dev/mtd
3、载入ubi和gluebi两个模块
这里需要说明一下gluebi这个模块实际上是ubi系统下面的一个子系统,它的作用就是对ubi系统下面建立起来的
每一个卷(volume)再建立一个块设备,块设备名字一般为mtdblockx.这样ubi中的每一个卷就可以当成一个块
设备被上层的软件(主要是文件系统)来操作了
- andy@andy-desktop:~/test$ sudo modprobe ubi mtd=0
- andy@andy-desktop:~/test$ sudo modprobe gluebi
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$ ls /dev/mtd*
- /dev/mtd0 /dev/mtd0ro /dev/mtdblock0
- andy@andy-desktop:~/test$ ls /dev/ubi*
- /dev/ubi0 /dev/ubi_ctrl
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$ sudo ubimkvol /dev/ubi0 -N ubi-vol0 -s 60MiB
- Volume ID 0, size 3964 LEBs (62916608 bytes, 60.0 MiB), LEB size 15872 bytes (15.5 KiB), dynamic, name "ubi-vol0", alignment 1
- andy@andy-desktop:~/test$ ls /dev/mtd*
- /dev/mtd0 /dev/mtd1 /dev/mtdblock0
- /dev/mtd0ro /dev/mtd1ro /dev/mtdblock1
- andy@andy-desktop:~/test$ ls /dev/ubi*
- /dev/ubi0 /dev/ubi0_0 /dev/ubi_ctrl
- andy@andy-desktop:~/test$
- andy@andy-desktop:~/test$ sudo mkfs.vfat /dev/mtdblock1
- mkfs.vfat 3.0.7 (24 Dec 2009)
- unable to get drive geometry, using default 255/63
- andy@andy-desktop:~/test$ sudo mount -t vfat /dev/mtdblock1 ./ubimount
- andy@andy-desktop:~/test$ cd ./ubimount/
- andy@andy-desktop:~/test/ubimount$ ls
- andy@andy-desktop:~/test/ubimount$ cp ../
- hello/ hello.txt jffs.img ubimount/
- andy@andy-desktop:~/test/ubimount$ cp ../hello
- hello/ hello.txt
- andy@andy-desktop:~/test/ubimount$ cp ../hello/hello.txt ./hello
- cp: 无法创建普通文件"./hello": 权限不够
- andy@andy-desktop:~/test/ubimount$ sudo cp ../hello/hello.txt ./hello
- andy@andy-desktop:~/test/ubimount$ ls
- hello
- andy@andy-desktop:~/test/ubimount$ cat hello
- hello
- andy@andy-desktop:~/test/ubimount$
首先查看一下虚拟出来的ubi卷的信息,ubi卷的mtd设备是mtd1,mtd0是nandsim虚拟的mtd设备
- andy@andy-desktop:~/test$ mtdinfo /dev/mtd1
- mtd1
- Name: ubi-vol0
- Type: ubi
- Eraseblock size: 15872 bytes, 15.5 KiB
- Amount of eraseblocks: 3964 (62916608 bytes, 60.0 MiB)
- Minimum input/output unit size: 512 bytes
- Sub-page size: 512 bytes
- Character device major/minor: 90:2
- Bad blocks are allowed: false
- Device is writable: true
- andy@andy-desktop:~/test$ sudo mkfs.jffs2 -e 0x3e00 -s 0x200 -p 0x500000 -d ./hello
- hello/ hello.txt
- andy@andy-desktop:~/test$ sudo mkfs.jffs2 -e 0x3e00 -s 0x200 -p 0x500000 -d ./hello -o jffs.img
- andy@andy-desktop:~/test$ ls
- hello hello.txt jffs_bak.img jffs.img ubimount
- andy@andy-desktop:~/test$ dd if=jffs.img of=/dev/mtdblock1
- dd: 正在打开"/dev/mtdblock1": 权限不够
- andy@andy-desktop:~/test$ sudo dd if=jffs.img of=/dev/mtdblock1
- 记录了31+0 的读入
- 记录了31+0 的写出
- 15872字节(16 kB)已复制,0.0078479 秒,2.0 MB/秒
- andy@andy-desktop:~/test$ sudo mount -t jffs2 /dev/mtdblock1 ./ubimount/
- andy@andy-desktop:~/test$ cd ./ubimount/
- andy@andy-desktop:~/test/ubimount$ ls
- hello.txt
- andy@andy-desktop:~/test/ubimount$ cat hello.txt
- hello
- andy@andy-desktop:~/test/ubimount$