开发环境:Fedora15

开发工具:mtd-utils-1.4.6

注意:在linux2.6.28后才加入对ubifs的支持,开发环境主机要求至少是在linux2.6.28后的内核,且已经有nandsim,ubi等相关模块。

步骤:

1.虚拟出NANDFLASH

#modprobe mtd

#modprobe mtdblock

#modprobe nandsim first_id_byte=0xec second_id_byte=0xa1 third_id_byte=0x00 fourth_id_byte=0x15

#chmod 660 /dev/mtd0*

#ls -lah /dev/mtd*

crw-------. 1 root root 90, 0 Oct 18 15:07 /dev/mtd0         mtd字符设备

crw-------. 1 root root 90, 1 Oct 18 15:07 /dev/mtd0ro

brw-rw----. 1 root disk 31, 0 Oct 18 15:07 /dev/mtdblock0    mtd块设备,与mtd0对应

# cat /proc/mtd

dev:    size   erasesize  name

mtd0: 08000000 00020000 "NAND simulator partition 0"

大小为128MiB,擦除单元大小(一般即为块大小)128KiB,名字是"NAND simulator partition 0"

NandFlash擦除是以块(block)为单位,读写是以页(page)为单位。

查看nand的基本信息

# ./mtd-utils/ubi-utils/mtdinfo /dev/mtd0

mtd0

Name:                           NAND simulator partition 0

Type:                           nand

Eraseblock size:                131072 bytes, 128.0 KiB      擦除单元大小

Amount of eraseblocks:          1024 (134217728 bytes, 128.0 MiB)  块数*块大小=总容量

Minimum input/output unit size: 2048 bytes    最小输入输出单元

Sub-page size:                  512 bytes  子页大小

OOB size:                       64 bytes  Out-of-bank

Character device major/minor:   90:0        主,次设备号

Bad blocks are allowed:         true

Device is writable:             true

现在的nandflash一般页大小为(512+16)Bytes,(2048+64)Bytes

大小为512Bytes的可用页对应有16BytesOOB信息

大小为2048Byte的可用页对应有64BytesOOB信息

 

 

 

2.制作ubi文件系统

我是参照google到的一个创建ubi.img的脚本来生成ubi image的。

./create_ubifs.sh 页大小 每块的页数 分区大小 分区包含的块数 文件夹

:

#./create_ubifs.sh 2048 64 134217728 1024 ./rootfs

这个是与上面加载的nandsim对应的.

 

3.写入镜像到nandflash

#dd if=ubi.img of=/dev/mtd0 bs=2048

 

4.加载文件系统并attach MTD设备

#modprobe ubi

#./ubiattach /dev/ubi_ctrl -m 0 -O 2048

-m指定挂在在mtd0

-O参数用来指定VID header offset,默认是512

 

5.mount文件系统

#mount -t ubifs ubi0_0 ./mntdir

即可将文件系统挂上.

 

错误处理

mount时出错:

mount: wrong fs type, bad option, bad superblock on ubi0_0,

       missing codepage or helper program, or other error

       (for several filesystems (e.g. nfs, cifs) you might

       need a /sbin/mount. helper program)

       In some cases useful info is found in syslog - try

       dmesg | tail  or so

#dmesg

[40278.359982] UBIFS error (pid 11094): validate_sb: LEB size mismatch: 520192 in superblock, 126976 real

[40278.359987] UBIFS error (pid 11094): validate_sb: bad superblock, error 1

 

很明显能看出是由于LEB size不对造成的,而LED size是我们在mkfs.ubifs时指定的。

重新修改脚本对应命令:

./create_ubifs.sh 2048 64 134217728 1024 ./rootfs

我之前用的命令是

./create_ubifs.sh 2048 256 134217728 1024 ./rootfs

生成新的ubi.img再重新写入nandflash.

注意此时需要重新挂载mtdblock,nandsim模块。

 

:创建分卷命令

ubimkvol /dev/ubi0 -N ubi-vol0 -s 32MiB



linux 内核nandsim的使用

 

If you wish to use the mtd nandsim device as a simulator for testing
yaffs, you can follow these steps:

1) Make sure you have a recent 2.6 kernel with a recent MTD tree. (If
you ask the MTD folk, they'll tell you to run the mtd patchin script
from the latest cvs version of MTD.)

2) Configure your kernel to enable the mtd nand support and nandsim
driver. Make sure that the following are set in .config:

CONFIG_MTD=m
CONFIG_MTD_CHAR=m
CONFIG_MTD_BLOCK=m
CONFIG_MTD_NAND=m
CONFIG_MTD_NAND_NANDSIM=m

You can set them to 'y' rather than 'm' if you don't want the modules
form.

3) make and install your kernel and modules in the usual way, then boot.

4) To use the nandsim, you need to load some MTD modules, unless you've
configured MTD into the kernel:

modprobe mtdblock
modprobe mtdchar
modprobe nandsim

5) Verify that the nandsim device has loaded properly:

cat /proc/mtd

You will see something like

dev: size erasesize name
mtd0: 00800000 00002000 "NAND simulator partition"

If everything went well.

6) what the nand device is called will depend on your Linux distro, and
whether you are using devfs or udev, but the '0', or whatever mtd
reports as the device associated with the simulator, in the above will
show up in the name. On my ubuntu system, if /proc/mtd reports 'mtd0'
then the block device is /dev/mtdblock0 and the char device is
/dev/mtd0.

7) You can mount yaffs on the nandsim in the usual way. I have
/mnt/nand as the directory I mount on, so I would mount using

mount -t yaffs /dev/mtdblock0 /mnt/nand
-------------------------------------


[root@urbetter /]# cd /lib/modules/2.6.28.6/extra/
[root@urbetter extra]# ls
chr_dev.ko  nandsim.ko  test.ko     text        usr_app
[root@urbetter extra]# insmod nandsim.ko
NAND device: Manufacturer ID: 0x98, Chip ID: 0x39 (Toshiba NAND 128MiB 1,8V 8-bit)
flash size: 128 MiB
page size: 512 bytes
OOB area size: 16 bytes
sector size: 16 KiB
pages number: 262144
pages per sector: 32
bus width: 8
bits in sector size: 14
bits in page size: 9
bits in OOB size: 4
flash size with OOB: 135168 KiB
page address bytes: 4
sector address bytes: 3
options: 0x62
Scanning device for bad blocks
Creating 1 MTD partitions on "NAND 128MiB 1,8V 8-bit":
0x00000000-0x08000000 : "NAND simulator partition 0"
[root@urbetter extra]# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00040000 00020000 "Bootloader"
mtd1: 003c0000 00020000 "Kernel"
mtd2: 05000000 00020000 "Rootfs"
mtd3: 0ac00000 00020000 "File System"
mtd4: 08000000 00004000 "NAND simulator partition 0"
[root@urbetter extra]# ls /dev/mtd
/dev/mtd0       /dev/mtd1ro     /dev/mtd3       /dev/mtd4ro     /dev/mtdblock2
/dev/mtd0ro     /dev/mtd2       /dev/mtd3ro     /dev/mtdblock0  /dev/mtdblock3
/dev/mtd1       /dev/mtd2ro     /dev/mtd4       /dev/mtdblock1  /dev/mtdblock4
[root@urbetter extra]# ls /dev/mtd4 /dev/mtdblock4 -l
crw-rw----    1 root     root      90,   8 Jan  2 06:39 /dev/mtd4
brw-rw----    1 root     root      31,   4 Jan  2 06:39 /dev/mtdblock4

[root@urbetter extra]# ls
chr_dev.ko  nandsim.ko  test.ko     text        usr_app
[root@urbetter extra]# mkdir test

[root@urbetter extra]# mount /dev/mtdblock4 test
yaffs: dev is 32505860 name is "mtdblock4"
yaffs: passed flags ""
yaffs: Attempting MTD mount on 31.4, "mtdblock4"
yaffs_read_super: isCheckpointed 0

[root@urbetter extra]# cd test
[root@urbetter test]# ls
lost+found
[root@urbetter test]# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 9.4G      6.6G      2.3G  74% /
tmpfs                    34.8M         0     34.8M   0% /dev/shm
/dev/mtdblock3          172.0M      1.1M    170.9M   1% /mnt/disk
/dev/mtdblock4          128.0M     96.0K    127.9M   0% /lib/modules/2.6.28.6/extra/test

Logo

更多推荐