http://daemons.net/linux/storage/mtd.html

MTD

The Memory Technology Devices (MTD) subsystem provides a common interface to many types of raw flashstorage devices. It includes facilities for partioning flash, erasing flash, and managing bad blocks.

Devices and partitions

The mtdinfo command, provided as part of the mtd-utils distribution, displays all available MTD devices and partitions:

$ mtdinfo
Count of MTD devices: 5
Present MTD devices: mtd0, mtd1, mtd2, mtd3, mtd4
Sysfs interface supported: yes

Given a device argument (or --all), mtdinfo prints detailed device information:

$ mtdinfo /dev/mtd0
mtd0
Name: NAND 128MiB 1,8V 8-bit
Type: nand
Eraseblock size: 16384 bytes, 16.0 KiB
Amount of eraseblocks: 8192 (134217728 bytes, 128.0 MiB)
Minimum input/output unit size: 512 bytes
Sub-page size: 256 bytes
OOB size: 16 bytes
Character device major/minor: 90:0
Bad blocks are allowed: true
Device is writable: true

Like many such userspace tools, mtdinfo is a thin wrapper around the MTD sysfs interface in /sys/class/mtd.

Devices

MTD provides a pair of character devices for each flash device and partition:

  • /dev/mtdX (read-write)
  • /dev/mtdXro (read-only)

Most mtd-utils commands take a /dev/mtdX device node as an argument.

You can read/write directly from/to /dev/mtdX devices, but there are complications:

  • reads can be any size, but writes must be a multiple of the device's minimum write size (as reported by /sys/class/mtd/mtdX/writesize)
  • blocks must be erased before they can be written
  • on NAND devices, reading from or writing to a bad block may produce unexpected results

For these reasons, the [mtd-utils utilities](#MTD Utilities) are often used for flash I/O.

Partitions

MTD allows flash devices to be carved into partitions. Several on-flash partition table formats are supported, as well as partitions defined on the kernel command line or in the device tree.

NAND simulator

MTD includes a NAND simulator module (nandsim) that attaches a virtual MTD device:

$ modprobe nandsim

$ mtdinfo -a
Count of MTD devices: 1
Present MTD devices: mtd0
Sysfs interface supported: yes mtd0
Name: NAND simulator partition 0
Type: nand
Eraseblock size: 16384 bytes, 16.0 KiB
Amount of eraseblocks: 8192 (134217728 bytes, 128.0 MiB)
Minimum input/output unit size: 512 bytes
Sub-page size: 256 bytes
OOB size: 16 bytes
Character device major/minor: 90:0
Bad blocks are allowed: true
Device is writable: true

MTD Utilities

MTD includes a suite of userspace tools, distributed as mtd-utils.

flash_erase

The flash_erase utility erases the data and out-of-band areas of a device, partition, or range of blocks:

$ flash_erase /dev/mtd0 0 0
Erasing 16 Kibyte @ 7ffc000 -- 100 % complete

Reading an erased flash returns all-ones:

$ dd if=/dev/mtd0 bs=512 count=1 | hd
00000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
*
00000200

nanddump

The nanddump utility dumps the contents of an MTD device, skipping any bad blocks that it encounters:

$ nanddump /dev/mtd0 -f nand.img
ECC failed: 0
ECC corrected: 0
Number of bad blocks: 0
Number of bbt blocks: 0
Block size 16384, page size 512, OOB size 16
Dumping data starting at 0x00000000 and ending at 0x08000000...

It can optionally dump out-of-band data and bad blocks.

nandwrite

The nandwrite utility copies a source file to an MTD device, skipping any bad blocks that it encounters:

$ sudo nandwrite /dev/mtd0 nand.img
Writing data to block 0 at offset 0x0
Writing data to block 1 at offset 0x4000
Writing data to block 2 at offset 0x8000
Writing data to block 3 at offset 0xc000
...

mtdpart

The mtdpart utility adds and deletes MTD partitions (but does not update the on-flash partition table):

$ mtdinfo
Count of MTD devices: 2
Present MTD devices: mtd0, mtd1
Sysfs interface supported: yes $ sudo mtdpart del /dev/mtd0 1 $ mtdinfo
Count of MTD devices: 1
Present MTD devices: mtd0
Sysfs interface supported: yes $ sudo mtdpart add /dev/mtd0 ubi 0 1048576 $ mtdinfo
Count of MTD devices: 2
Present MTD devices: mtd0, mtd1
Sysfs interface supported: yes $ mtdinfo /dev/mtd1
mtd1
Name: ubi
Type: nand
Eraseblock size: 16384 bytes, 16.0 KiB
Amount of eraseblocks: 64 (1048576 bytes, 1024.0 KiB)
Minimum input/output unit size: 512 bytes
Sub-page size: 256 bytes
OOB size: 16 bytes
Character device major/minor: 90:2
Bad blocks are allowed: true
Device is writable: true

Programming Interfaces

ioctl ABI

Behind the scenes, the mtd-utils commands interact with the MTD subsystem via an ioctl interface:

/* Get basic MTD characteristics info (better to use sysfs) */
#define MEMGETINFO _IOR('M', 1, struct mtd_info_user) /* Erase segment of MTD */
#define MEMERASE _IOW('M', 2, struct erase_info_user) /* Write out-of-band data from MTD */
#define MEMWRITEOOB _IOWR('M', 3, struct mtd_oob_buf) /* Read out-of-band data from MTD */
#define MEMREADOOB _IOWR('M', 4, struct mtd_oob_buf) /* Lock a chip (for MTD that supports it) */
#define MEMLOCK _IOW('M', 5, struct erase_info_user) /* Unlock a chip (for MTD that supports it) */
#define MEMUNLOCK _IOW('M', 6, struct erase_info_user) /* Check if chip is locked (for MTD that supports it) */
#define MEMISLOCKED _IOR('M', 23, struct erase_info_user) /* Check if an eraseblock is bad */
#define MEMGETBADBLOCK _IOW('M', 11, __kernel_loff_t) /* Mark an eraseblock as bad */
#define MEMSETBADBLOCK _IOW('M', 12, __kernel_loff_t) /*
* Most generic write interface; can write in-band and/or out-of-band in various
* modes (see "struct mtd_write_req")
*/
#define MEMWRITE _IOWR('M', 24, struct mtd_write_req)

libmtd

The mtd-utils source builds a static library (libmtd.a) that wraps the MTD ioctls in a tidy API, e.g., mtd_mark_bad():

/**
* mtd_mark_bad - mark an eraseblock as bad.
* @mtd: MTD device description object
* @fd: MTD device node file descriptor
* @eb: eraseblock to mark as bad
*
* This function marks eraseblock @eb as bad. Returns %0 in case of success and
* %-1 in case of failure.
*/
int mtd_mark_bad(const struct mtd_dev_info *mtd, int fd, int eb);

Sadly, libmtd.a is not included in the mtd-utils binary distribution.

Kernel API

Higher layer kernel subsystems (flash filesytems, UBI) interact with raw flash via the MTD API defined in include/linux/mtd/mtd.h, including:

int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);

Device drivers register MTD devices with the MTD core via mtd_device_register().

mtd工具的更多相关文章

  1. 利用mtd工具实现嵌入式设备在线升级

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   主要思路是:通过web post方式将升级文件交给CGI程序处理,然后通过mtd工具实现设备在线升级. 1.页面部分 & ...

  2. MTD应用学习:mtd和mtdblock的区别

    http://my.oschina.net/shelllife/blog/123482 http://www.cnblogs.com/hnrainll/archive/2011/06/09/20760 ...

  3. MTD应用学习札记【转】

    转自:https://blog.csdn.net/lh2016rocky/article/details/70885421 今天做升级方案用到了mtd-utils中的flash_eraseall和fl ...

  4. Linux系统中/dev/mtd与/dev/mtdblock的区别,即MTD字符设备和块设备的区别

    转:http://www.crifan.com/linux_system_in__dev__mtd_and__dev__mtdblock_distinction_character_devices_a ...

  5. mtd交叉编译mkfs命令

    下载 mtd:ftp://ftp.infradead.org/pub/mtd-utils/ zlib:http://www.zlib.net/ lzo:http://bouchez.info/lzo. ...

  6. Android源码目录结构详解(转载)

    转自:http://blog.csdn.net/xiangjai/article/details/9012387 在学习Android的过程中,学习写应用还好,一开始不用管太多代码,直接调用函数就可以 ...

  7. android源码的目录结构

    android源码的目录结构 [以下网络摘抄] |-- Makefile ! l/ a5 n% S% @- `0 d# z# a$ P4 V3 o7 R|-- bionic              ...

  8. Android 4.0 源代码结构

    Android源码的第一级目录结构   Android/abi (abi相关代码.ABI:application binary interface,应用程序二进制接口)   Android/bioni ...

  9. Android源码-学习随笔

    在线代码网站1:http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/ 书籍: ...

随机推荐

  1. Word批量删除所有书签

    Word中的书签功能可快速.准确定位文档中特定的位置,经常用于模板定制.文档产出等. 可一直以来,书签功能存在一个不便的操作,即无法批量删除,只能单个删除,操作极不友好. 解决方案 我用代码暂时还改变 ...

  2. MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Max, Min, Count , DistinctCount 以及其它 TopCount, Generate

    MDX 中最大值和最小值 MDX 中最大值和最小值函数的语法和之前看到的 Sum 以及 Aggregate 等聚合函数基本上是一样的: Max( {Set} [, Expression]) Min( ...

  3. 2D游戏新手引导点光源和类迷雾实现

    一.新手引导须要的遮罩效果 一般做新手引导的时候,会把游戏画面变的半黑,然后须要玩家点击的地方就亮起来.经常使用的做法是採用遮罩来实现,可是仅仅能实现方形的,不能不规则图形.以及是全然挖空.做不到渐变 ...

  4. 创建py模板

    创建模板之后,每次新建py文件,已初始定义的代码段将会自动出现在py文件中.

  5. IntelliJ IDEA for Mac(Java 语言开发的集成环境)破解版安装

    1.软件简介    IntelliJ IDEA 是 macOS 系统上一款 java 语言开发的集成环境,IntelliJ 在业界被公认为最好的 java 开发工具之一,尤其在智能代码助手.代码自动提 ...

  6. C#访问MySQL数据库的方法

    C#访问MySQL数据库的方法 (1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序 下载地址为: http://dev.mysql.com/downloads/connector/ne ...

  7. nginx跨域

    在 conf文件server块里面加上: add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header ' ...

  8. 深入研究 Runloop 与线程保活

    深入研究 Runloop 与线程保活 在讨论 runloop 相关的文章,以及分析 AFNetworking(2.x) 源码的文章中,我们经常会看到关于利用 runloop 进行线程保活的分析,但如果 ...

  9. linux每日命令(31):tar命令

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...

  10. 实现A星算法

    [更新] 稍微将A*算法进行修正,使用BFS(按F值对open表排序),另外,新增评估函数,用来测量当前点到终点的线段上的随机某一点是否是墙或已访问结点,是的话返回1,否则返回0. function ...