mtd-util,即mtd的utilities,是mtd相关的很多工具的总称,包括常用的mtdinfo,flash_erase, flash_eraseall, nanddump, nandwrite等,每一个工具,基本上都对应着一个同文件名的C文件。
mtd-util,由mtd官方维护更新,开发这一套工具,目的是为了Linux的MTD层提供一系列工具,方便管理维护mtd分区。
mtd工具对应的源码,叫做mtd-utils,随着时间更新,发布了很多版本。
我之前用到的版本是mtd-utils-1.3.1,截止2011-05-01,最新版本到了v1.4.1。
mtd-util源码的下载地址,请去MTD源码的官网
另外多说一句,MTD的官网,资料很丰富,感兴趣的自己去看:
![]() | linux的mtd要和mtd-util中的一致 |
|---|---|
不过,对于之前的版本的Linux的kernel来说,使用mtd-util的话,一定要配套,主要是后来新的linux的版本,开始支持mtd的大小,即nand的大小,大于4GB,对应的linux内核中的mtd层的有些变量,就必须从u32升级成u64,才可以支持。 对应的mtd的util中一些变量,也是要和你当前linux版本的mtd匹配。 简单说就是,无论你用哪个版本的Linux内核,如果要去用mtd-util的话,那么两者的版本要一直,即查看linux内核中的mtd的一些头文件,主要是include\mtd\mtd-abi.h和你的mtd-util中的include\mtd\mtd-abi.h,两个要一致。 否则,就会出现我之前遇到的问题,当然linux内核是u64版本的,支持nand flash大于4GB的,而用的mtd-util中的变量的定义,却还是u32,所以肯定会出错的。 为了同一套mtd-util工具即支持u32又支持u64,我定义了一个宏来切换,下面贴出来,供需要的人参考: 加了宏以支持u32和u64的mtd-abi.h文件 mtd-util中的include\mtd\mtd-abi.h:
/*
* Portions of MTD ABI definition which are shared by kernel and user space
*/
#ifndef __MTD_ABI_H__
#define __MTD_ABI_H__
#include <linux/types.h>
/* from u32 to u64 to support >4GB */
#define U64_VERSION 1
struct erase_info_user {
#if U64_VERSION
__u64 start;
__u64 length;
#else
__u32 start;
__u32 length;
#endif
};
struct mtd_oob_buf {
#if U64_VERSION
__u64 start;
#else
__u32 start;
#endif
__u32 length;
unsigned char __user *ptr;
};
#define MTD_ABSENT 0
#define MTD_RAM 1
#define MTD_ROM 2
#define MTD_NORFLASH 3
#define MTD_NANDFLASH 4
#define MTD_DATAFLASH 6
#define MTD_UBIVOLUME 7
#define MTD_WRITEABLE 0x400 /* Device is writeable */
#define MTD_BIT_WRITEABLE 0x800 /* Single bits can be flipped */
#define MTD_NO_ERASE 0x1000 /* No erase necessary */
#define MTD_STUPID_LOCK 0x2000 /* Always locked after reset */
// Some common devices / combinations of capabilities
#define MTD_CAP_ROM 0
#define MTD_CAP_RAM (MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE)
#define MTD_CAP_NORFLASH (MTD_WRITEABLE | MTD_BIT_WRITEABLE)
#define MTD_CAP_NANDFLASH (MTD_WRITEABLE)
/* ECC byte placement */
#define MTD_NANDECC_OFF 0 // Switch off ECC (Not recommended)
#define MTD_NANDECC_PLACE 1 // Use the given placement in the structure (YAFFS1 legacy mode)
#define MTD_NANDECC_AUTOPLACE 2 // Use the default placement scheme
#define MTD_NANDECC_PLACEONLY 3 // Use the given placement in the structure (Do not store ecc result on read)
#define MTD_NANDECC_AUTOPL_USR 4 // Use the given autoplacement scheme rather than using the default
#define MTD_MAX_OOBFREE_ENTRIES 8
/* This constant declares the max. oobsize / page, which
* is supported now. If you add a chip with bigger oobsize/page
* adjust this accordingly.
*/
#define MTD_NAND_MAX_PAGESIZE 8192
/*
* for special chip, page/oob is 4K/218,
* so here alloc more than 256+256 for 8192 pagesize for future special chip like that
*/
#define MTD_NAND_MAX_OOBSIZE (256 + 256)
/* OTP mode selection */
#define MTD_OTP_OFF 0
#define MTD_OTP_FACTORY 1
#define MTD_OTP_USER 2
struct mtd_info_user {
__u8 type;
__u32 flags;
#if U64_VERSION
__u64 size; // Total size of the MTD
#else
__u32 size; // Total size of the MTD
#endif
__u32 erasesize;
__u32 writesize;
__u32 oobsize; // Amount of OOB data per block (e.g. 16)
/* The below two fields are obsolete and broken, do not use them
* (TODO: remove at some point) */
__u32 ecctype;
__u32 eccsize;
};
struct region_info_user {
#if U64_VERSION
__u64 offset; /* At which this region starts,
* from the beginning of the MTD */
#else
__u32 offset; /* At which this region starts,
* from the beginning of the MTD */
#endif
__u32 erasesize; /* For this region */
__u32 numblocks; /* Number of blocks in this region */
__u32 regionindex;
};
struct otp_info {
__u32 start;
__u32 length;
__u32 locked;
};
#define MEMGETINFO _IOR('M', 1, struct mtd_info_user)
#define MEMERASE _IOW('M', 2, struct erase_info_user)
#define MEMWRITEOOB _IOWR('M', 3, struct mtd_oob_buf)
#define MEMREADOOB _IOWR('M', 4, struct mtd_oob_buf)
#define MEMLOCK _IOW('M', 5, struct erase_info_user)
#define MEMUNLOCK _IOW('M', 6, struct erase_info_user)
#define MEMGETREGIONCOUNT _IOR('M', 7, int)
#define MEMGETREGIONINFO _IOWR('M', 8, struct region_info_user)
#define MEMSETOOBSEL _IOW('M', 9, struct nand_oobinfo)
#define MEMGETOOBSEL _IOR('M', 10, struct nand_oobinfo)
#define MEMGETBADBLOCK _IOW('M', 11, __kernel_loff_t)
#define MEMSETBADBLOCK _IOW('M', 12, __kernel_loff_t)
#define OTPSELECT _IOR('M', 13, int)
#define OTPGETREGIONCOUNT _IOW('M', 14, int)
#define OTPGETREGIONINFO _IOW('M', 15, struct otp_info)
#define OTPLOCK _IOR('M', 16, struct otp_info)
#define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout)
#define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)
#define MTDFILEMODE _IO('M', 19)
/*
* set/clear prepare oob support
* usage:
* 1. set prep_oob_support
* 2. call write_oob will only prepare, not actually write
* 3. clear prep_oob_support
* 4. write_page will use the previously prepared oob buffer, then clear it automatically
*/
#define SETPREPAREOOB _IOWR('M', 20, int)
#define CLEARPREPAREOOB _IOWR('M', 21, int)
/*
* Obsolete legacy interface. Keep it in order not to break userspace
* interfaces
*/
struct nand_oobinfo {
__u32 useecc;
__u32 eccbytes;
__u32 oobfree[MTD_MAX_OOBFREE_ENTRIES][2];
__u32 eccpos[MTD_NAND_MAX_OOBSIZE];
};
struct nand_oobfree {
__u32 offset;
__u32 length;
};
/*
* ECC layout control structure. Exported to userspace for
* diagnosis and to allow creation of raw images
*/
struct nand_ecclayout {
__u32 eccbytes;
__u32 eccpos[MTD_NAND_MAX_OOBSIZE];
__u32 oobavail;
struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES];
};
/**
* struct mtd_ecc_stats - error correction stats
*
* @corrected: number of corrected bits
* @failed: number of uncorrectable errors
* @badblocks: number of bad blocks in this partition
* @bbtblocks: number of blocks reserved for bad block tables
*/
struct mtd_ecc_stats {
__u32 corrected;
__u32 failed;
__u32 badblocks;
__u32 bbtblocks;
};
/*
* Read/write file modes for access to MTD
*/
enum mtd_file_modes {
MTD_MODE_NORMAL = MTD_OTP_OFF,
MTD_MODE_OTP_FACTORY = MTD_OTP_FACTORY,
MTD_MODE_OTP_USER = MTD_OTP_USER,
MTD_MODE_RAW,
};
#endif /* __MTD_ABI_H__ */
|




![[注意]](https://www.crifan.com/files/res/docbook/images/note.png)

