---
title: "移植mtd-utils工具包--使用教程"
author: "shangjava"
date: "2011-12-07T21:42:00Z"
tags: []
slug: "mtd-utils"
draft: false
lastmod: "2012-01-05T09:22:31Z"
id: 1335666
url: "https://shangjava.iteye.com/blog/1335666"
---

移植mtd-utils工具包--使用教程

关于mtd-utils的移植，在Ubuntu Server 10.04平台上交叉编译，移植到嵌入式Linux，经过实践，以下是交叉编译的正确步骤，不保证在其他环境中能正确运行，以下是编译环境：

build: Ubuntu Server 10.04  
host: Linux version 2.6.32.15-15-sigma  
cross compiler:mips-linux-gnu-gcc version:4.3.2  
cpu: mips

交叉编译mtd-utils，依赖(zlib,lzo)；

Download Open Sources:

mtd-utils-1.0.0.tar.gz (Address:ftp://ftp.infradead.org/pub/mtd-utils/)  
zlib-1.2.3.tar.gz  
lzo-2.03.tar.gz

Building zlib-1.2.3.tar.gz:

tar zxvf zlib-1.2.3.tar.gz  
cd zlib-1.2.3/  
CC="mips-linux-gnu-gcc -EL" ./configure --shared --prefix=/home/gzshun/zlib/ (动态库\*.so)  
CC="mips-linux-gnu-gcc -EL" ./configure --prefix=/home/gzshun/zlib/ (静态库\*.a)  
make  
make install

Building lzo-2.03.tar.gz:

tar zxvf lzo-2.03.tar.gz  
cd lzo-2.03/  
./configure --host=mips-linux-gnu CC="mips-linux-gnu-gcc -EL" --prefix=/home/gzshun/lzo/  
make  
make install

将上述2个开源包的安装目录include里面的头文件拷贝到mips交叉编译器相应的include目录下。  
本人的环境：cp -a /home/gzshun/zlib/include/* /home/gzshun/lzo/include/* /usr/local/src/mips-4.3/mips-linux-gnu/libc/usr/include/

Building mtd-utils-1.0.0.tar.gz:

tar zxvf mtd-utils-1.0.0.tar.gz  
cd mtd-utils-1.0.0/  
动态编译：  
vi Makefile  
修改内容如下：  
9 CROSS=mips-linux-gnu-  
10 CC := $(CROSS)gcc -EL  
42 mkfs.jffs2: crc32.o compr\_rtime.o mkfs.jffs2.o compr\_zlib.o compr.o  
43 $(CC) $(LDFLAGS) -o $@ $^ /home/gzshun/zlib/lib/libz.so #-lz  
48 jffs2reader: jffs2reader.o  
49 $(CC) $(LDFLAGS) -o $@ $^ /home/gzshun/zlib/lib/libz.so #-lz  
57 INSTALL\_DIR=/home/gzshun/mtd-utils  
58  
59 install: ${TARGETS}  
60 mkdir -p ${INSTALL\_DIR}/sbin  
61 install -m0755 ${TARGETS} ${INSTALL\_DIR}/sbin  
62 mkdir -p ${INSTALL\_DIR}/man/man1  
63 gzip -c mkfs.jffs2.1 &gt; ${INSTALL\_DIR}/man/man1/mkfs.jffs2.1.gz

保存退出

静态编译：  
vi Makefile  
修改内容如下：  
9 CROSS=mips-linux-gnu-  
10 CC := $(CROSS)gcc -EL  
12 LDFLAGS := -static  
25 $(CC) $(LDFLAGS) -g -o $@ $^  
43 mkfs.jffs2: crc32.o compr\_rtime.o mkfs.jffs2.o compr\_zlib.o compr.o  
44 $(CC) $(LDFLAGS) -o $@ $^ /home/gzshun/zlib/lib/libz.a #-lz  
45  
46 flash\_eraseall: crc32.o flash\_eraseall.o  
47 $(CC) $(LDFLAGS) -o $@ $^  
48  
49 jffs2reader: jffs2reader.o  
50 $(CC) $(LDFLAGS) -o $@ $^ /home/gzshun/zlib/lib/libz.a #-lz  
58 INSTALL\_DIR=/home/gzshun/others/mtd-utils  
59  
60 install: ${TARGETS}  
61 mkdir -p ${INSTALL\_DIR}/sbin  
62 install -m0755 ${TARGETS} ${INSTALL\_DIR}/sbin  
63 mkdir -p ${INSTALL\_DIR}/man/man1  
64 gzip -c mkfs.jffs2.1 &gt; ${INSTALL\_DIR}/man/man1/mkfs.jffs2.1.gz

保存退出

make  
make install

mtd-utils命令安装到/home/gzshun/mtd-utils/sbin/目录下。

由于交叉编译开源软件包，不涉及到具体难题，没有逐一解释，只提供编译步骤。

=======================================  
下面文章经过网上转载，如有侵犯版权问题，请通知。本人立即删除

mtd-utils工具包的使用(转载)：

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 ] &lt;filename&gt; &lt;device&gt;  
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 /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 &lt;device&gt;  
mtd\_debug read &lt;device&gt; &lt;offset&gt; &lt;len&gt; &lt;dest-filename&gt;  
mtd\_debug write &lt;device&gt; &lt;offset&gt; &lt;len&gt; &lt;source-filename&gt;  
mtd\_debug erase &lt;device&gt; &lt;offset&gt; &lt;len&gt;

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).

七、参考文章  
http://blog.csdn.net/yinkaizhong/archive/2008/12/25/3604794.aspx  
http://hi.baidu.com/qwetiop/blog/item/f2acb50f03e800eaab64577a.html  
http://blog.chinaunix.net/u1/53103/showart\_1101011.html