当前位置: > Linux教程 > Linux学习 >

mtd-utils 工具的使用

时间:2017-06-14 10:20来源:linux.it.net.cn 作者:IT
 

mtd_debug



[root@xmos /root]# mtd_debug
usage: mtd_debug info <device>
       mtd_debug read <device> <offset> <len> <dest-filename>
       mtd_debug write <device> <offset> <len> <source-filename>
       mtd_debug erase <device> <offset> <len>



mtd_debug info

可以查看flash类型,块大小,mtd分区总大小,大小以10进制显示,分区大小就是len

mtd_debug write

把文件写入mtd,offset和len可以用十进制也可以用16进制,16进制要以0x作前缀

len要小于等于文件长度和mtd分区大小

写入之前要erase mtd分区

mtd_debug read

把文件写入mtd,offset和len可以用十进制也可以用16进制,16进制要以0x作前缀

len要小于等于mtd分区大小

mtd_debug erase

擦除mtd分区

offset和len可以用十进制也可以用16进制,16进制要以0x作前缀

len要小于等于mtd分区大小



例如

mtd_debug read /dev/mtd2 0x0 0x00900000 config.img

flashcp

用来把flash镜像写入mtd分区

flashcp -v rootfs.image /dev/mtd1

该命令把erase,write,校验一块完成了



mtd_debug和flashcp执行的时候cpu占用比较高,会导致系统响应缓慢甚至死机,执行完就好了

dd的时候不要使用mtdblock作为设备名,mtdblock是ftl层,ftl转换的效率很低,而且不安全,操作ftl不需要erase操作,完全像block设备一样




在mtd-utils/ubi-utils中生成的工具是针对UBIFS的,包含:

(1)mtdinfo: 输出指定的mtd分区的信息, 该命令只对可读的设备分区有效。

用法: ./mtdinfo /dev/mtd1



(2)ubinfo:  输出指定的ubi设备的信息,不带参数时,输出系统所有的ubi设备以及ubi控制设备信息。ubi控制设备即/dev/ubi_ctrl是一个字符设备,在后面还将用到。

用法:./ubinfo

      ./ubinfo /dev/ubi0



(3)ubiformat:格式化指定的mtd分区, 参数使用的是mtd的字符设备

用法:./ubiformat /dev/mtd1



(4)ubiattach:将指定的mtd分区关联到ubi上

用法:./ubiattach /dev/ubi_ctrl -m 1, 将mtd1关连ubi



(5)ubidetach:解除mtd与ubi的关联

用法:./ubidetach /dev/ubi_ctrl -m 1



(6)ubimkvol:创建一个voluem,volume才是最终用户挂载文件系统的地方。

用法:在ubi2上创建4个olume,每个大小是20M, 名字分别为my_vol_a, my_vol_b, my_vol_c, my_vol_d.

./ubimkvol /dev/ubi2 -s 20Mib -N my_vol_a

./ubimkvol /dev/ubi2 -s 20Mib -N my_vol_b

./ubimkvol /dev/ubi2 -s 20Mib -N my_vol_c

./ubimkvol /dev/ubi2 -s 20Mib -N my_vol_d



我用上面的方法试了试,不行。下面是我的做法:

如果要建的volume大小是100MiB,100*1024*1024=104857600

ubimkvol  /dev/ubi0 -N peng -s 104857600

mount -t ubifs ubi0:peng /mnt 

注意:我用mount -t ubifs /dev/uib0_1 /mnt 出现错误。





(7)ubirename: 更改ubi某个olume的名字,或者完成两个volume的互换

用法:

(1)将ubi2上名字为my_vol_a的volume名字改为my_vol_newname

./ubirename /dev/ubi2 my_vol_a my_vol_newname

(2)将ubi2上名字为my_vol_b和my_vol_c的两个volume互换,

./ubirename /dev/ubi2 my_vol_b my_vol_c my_vol_c my_vol_b



(8)ubirmvol: 删除某个volume,可以通过名字或id指定具体的volume

用法: ./ubirmvol /dev/ubi2 -n 1
或./ubirmvol /dev/ubi2 -N my_vol_d



(9)ubinize: 创建ubi image。

用法: ./ubinize -o myubi.img -p 126976 -m 2048 my.ini

其中, -p, 指定的是目标flash的physical eraseblock的大小

       -s, 指定的是目标flash的minimum input/output unit 的大小, 这两个参数的值可以通过“cat /sys/class/ubi/ubi2/*”得到

       my.ini是配置文件,其中指定了原文件,目标volume的大小、名字等。其格式如小:

[jffs2-volume]
mode=ubi
image=../jffs2.img
vol_id=1
vol_size=30MiB
vol_type=dynamic
vol_name=jffs2_volume
vol_flags=autoresize
vol_alignment=1

(10)ubiupdatevol: 向指定的volume上写数据

用法:./ubiupdatevol /dev/ubi2_0 myubi.img



mtd-utils工具命令的使用
1)
使用命令前用cat /proc/mtd 查看一下mtdchar字符设备;或者用ls -l /dev/mtd*
#cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00c00000 00020000 "ROOTFS"
mtd1: 00200000 00020000 "BOOTLOADER"
mtd2: 00200000 00020000 "KERNEL"
mtd3: 03200000 00020000 "NAND ROOTFS partition"
mtd4: 04b00000 00020000 "NAND DATAFS partition"
为了更详细了解分区信息用mtd_debug命令
#mtd_debug info /dev/mtdX (不能使用mtdblockX, mtdblockX 只是提供用來 mount 而已)
mtd.type = MTD_NORFLASH
mtd.flags =
mtd.size = 12582912 (12M)
mtd.erasesize = 131072 (128K)
mtd.oobblock = 1
mtd.oobsize = 0
mtd.ecctype = (unknown ECC type - new MTD API maybe?)
regions = 0


2)
命令:flash_erase
作用:擦出指定范围内flash的内容,如果不指定,默认擦出起始位置的第一块,使相应flash变为全1
用法:
flash_erase MTD-device [start] [cnt (# erase blocks)] [lock]
MTD-device:待擦出的分区,如/dev/mtd0
start:起始位置设置,这里必须设置为0x20000(128K)的整数倍
cnt: 从start开始计算,要擦出的块数
lock: 写保护                            
eg:  ./flash_erase /dev/mtd0 0x40000 5   //擦出mtd0分区上从0x40000开始的5块数据 ,128K/块

命令:flash_eraseall
作用:擦出整个分区的数据,同时也会作坏块检测
用法:
flash_eraseall [OPTION] MTD_DEVICE
-q, --quiet    不显示打印信息
-j, --jffs2    一jffs2 格式化分区
eg: ./flash_eraseall -j /dev/mtd0

命令:flashcp
作用:copy 数据到 flash 中
用法:
usage: flashcp [ -v | --verbose ] 
       flashcp -h | --help
filename:待写入的数据
device: 写入的分区,如/dev/mtd0
eg: 
filename制作:mkfs.jffs2 -e 0x20000 -d cq8401 -o cq8401.img  -n  //这里的-e 0x20000 必须更你芯片的erasesize 相等

./flashcp cq8401.img /dev/mtd0  // copy cq8401.img文件系统到  /dev/mtd0分区中
当然这个命令的功能跟 dd if=/tmp/fs.img of=/dev/mtd0差不多

命令:nandwrite
作用:向nand flash中写数据
用法:
nandwrite [OPTION] MTD_DEVICE INPUTFILE
  -a, --autoplace       Use auto oob layout
  -j, --jffs2           force jffs2 oob layout (legacy support)
  -y, --yaffs           force yaffs oob layout (legacy support)
  -f, --forcelegacy     force legacy support on autoplacement enabled mtd device
  -n, --noecc           write without ecc
  -o, --oob             image contains oob data
  -s addr, --start=addr set start address (default is 0)
  -p, --pad             pad to page size
  -b, --blockalign=1|2|4 set multiple of eraseblocks to align to
  -q, --quiet           don't display progress messages
      --help            display this help and exit
      --version         output version information and exit
   
eg: ./nandwrite -p /dev/mtd0  /tmp/rootfs.jffs2

命令:nanddump
作用:dump出nand flash一些信息,如:block size,erasesize,oobblock 大小,oob data ,page data等;同时也会作坏块检测
用法:
nanddump [OPTIONS] MTD-device
           --help               display this help and exit
           --version            output version information and exit
-f file    --file=file          dump to file
-i         --ignoreerrors       ignore errors
-l length  --length=length      length
-o         --omitoob            omit oob data
-b         --omitbad            omit bad blocks from the dump
-p         --prettyprint        print nice (hexdump)
-s addr    --startaddress=addr  start address
eg:./nanddump -p -f nandinfo.txt /dev/mtd0  //dump出nand flash /dev/mtd0数据并保存到 nandinfo.txt

命令:mtd_debug
作用: 对mtd 调试作用
用法:
usage: mtd_debug info
       mtd_debug read   
       mtd_debug write   
       mtd_debug erase  
eg:
#./mtd_debug info /dev/mtd0  // 输出/dev/mtd0上的一些信息,这里必须用mtdx
#./mtd_debug erase /dev/mtd0 0x0 0x40000  // 擦出/dev/mtd0 分区上 从0x0开始的  , 128K*2 大小的数据
#./mtd_debug write /dev/mtdblock0 ox0 0x360810 cq8401.img //向mtdblock0分区,写入 3.6M 大小的文件系统cq8401.img,这里最好用mtdblockx
#./mtd_debug read  /dev/mtdblock0 ox0 0x360810 read.img  //从mtdblock0中读出 3.6M 数据保存到read.img
# cmp -l cq8401.img read.img  // 验证write to flash 和 read from flash 中的数据是否一致;也可以使用diff命令来比较
另外针对nand flash,mtd_debug这个工具来测试mtd驱动也不是很好,用nandwrite和nanddump这两个工具或许更好点。然后可以用cmp这个命令来比较一下nanddump出来的数据和nandwrite写入的数据是否一致。

命令:ftl_format
解 释:In order to use one of conventional file systems (Ext2, ext3, XFS, JFS, FAT) over an MTD device, you need a software layer which emulates a block device over the MTD device. These layers are often called Flash Translation Layers (FTLs).

例一:如何测试nor flash 驱动
step1:
#./mtd_debug info /dev/mtd0 // 输出/dev/mtd0上的一些信息,这里必须用mtdx
step2:
#./mtd_debug erase /dev/mtd0 0x0 0x40000 // 擦出/dev/mtd0 分区上 从0x0开始的 , 128K*2 大小的数据
step3:

#./mtd_debug write /dev/mtdblock0 ox0 0x360810 cq8401.img //向mtdblock0分区,写入 3.6M 大小的文件系统cq8401.img,这里最好用mtdblockx

step4:
#./mtd_debug read /dev/mtdblock0 ox0 0x360810 read.img //从mtdblock0中读出 3.6M 数据保存到read.img,当然这里的长度应该相等

step5:
# cmp -l cq8401.img read.img // 验证write to flash 和 read from flash 中的数据是否一致;也可以使用diff命令来比较



例二:如何测试nand flash 驱动

其实nand flash 驱动同样可以用例一的方法测试,但既然有nandwrite,nanddump命令,为何不用呢!


step1:
#./flash_eraseall -j /dev/mtd1        //用jffs2格式化该分区

step2:
#./nanddump -p /dev/mtd1 //dump出nand flash /dev/mtd1数据,可以看到现在的数据全是ff

step3:
#./nandwrite -p   /dev/mtd1 cq8401.img // 将cq8401.img文件系统写入mtd0分区

step4:
#./nanddump -p /dev/mtd1 //dump出nand flash /dev/mtd1数据,可以看到现在的数据不再是全ff


例三:如何用mtd-util 工具向nand flash写入文件系统jffs2.img,并修改启动参数,使文件系统从nand flash 启动;假设已分好区,mtd0为文件系统分区

方式一:
step1:
NFS起文件系统
#./flash_eraseall -j /dev/mtd0        //用jffs2格式化该分区

#./nandwrite -j -f -p -q /dev/mtd0 jffs2.img // 将jffs2.img文件系统写入mtd0分区

step2:
然后再看看我们新写入的JFFS2文件系统能不能mount上.

#mount -t jffs2 /dev/mtdblock0 /mnt
#ls /mnt

setp3:
重启开发板,在U-BOOT里 设置启动参数
#setenv bootargs 'mem=64M console=ttyS0,115200n8 ip=192.168.4.201:::::eth0:off root=/dev/mtdblock0 rootfstype=jffs2 rw'
#reset


方式二:


NAND 起内核,NAND起文件系统
1. 网起文件系统
nerase 0 55 && nprog 0 192.168.4.200 n-boot.bin.hg && nprog 128 192.168.4.200 zImage-6pci && reset
2.进入网起的文件系统
cat /proc/mtd

3. 制作JIFFS的文件系统
mkfs.jffs2 -e 0x20000 -d root-vw -o dvr20000.img -n

4.
cp dvr20000.img /dev/mtdblock1

5.修改NAND BOOT启动参数 include/cq8401_board.h
修改NAND BOOT
setenv bootargs 'mem=64M console=ttyS0,115200n8 ip=192.168.4.201:::::eth0:off root=/dev/mtdblock1 rootfstype=jffs2 rw'

6. 从新烧写
nerase 0 55 && nprog 0 192.168.4.200 n-boot.bin.local && nprog 128 192.168.4.200 zImage-6pci && reset

例四:
如何将一个 .tar.gz文件系统 写到 nor 或者 nand flash中
   target$ mkdir /mnt/flash    
   target$ mount -t jffs2 /dev/mtdblock0 /mnt/flash (mtdblockx只是用来挂载的)
   target$ cd /mnt/flash
   target$ tar zxvf rootfs.tar.gz (责任编辑:IT)
------分隔线----------------------------