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

來源:互聯網
上載者:User

移植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 > ${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 > ${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 ] <filename> <device>
       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 <device>
       mtd_debug read <device> <offset> <len> <dest-filename>
       mtd_debug write <device> <offset> <len> <source-filename>
       mtd_debug erase <device> <offset> <len>

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.