来自Jack's Lab
跳转到: 导航, 搜索


1 Overview

MTD --- Memory Technology Devices

目的在于为 Flash ROM, EEPROM 等设备提供一个统一的接口,亦可将一段RAM虚拟成一个MTD 设备

MTD 设备与传统设备的区别:

HARD drives MTD device Consists of sectors Consists of eraseblocks Sectors are small (512, 1024 bytes) Eraseblocks are larger (32KB, 128KB) Maintains 2 main operations: read sector and write sector Maintains 3 main operations: read from eraseblock, write to eraseblock, and erase eraseblock Bad sectors are re-mapped and hidden by hardware, at least at modern LBA hard drives Bad eraseblocks are not hidden and should be dealt with in software HDD sectors are devoid of the wear-out property Eraseblocks get worn-out (i.e., bad and unusable) after about 104-105 erase cycles.

2 Architecture


2.1 MTD hardware device drivers

  • Common Flash Interface (CFI) onboard NOR flash
  • Onboard NAND flash
  • On-board memory
  • PCMCIA flash (not CompactFlash but real flash)
  • M-Systems' DiskOnChip 2000 and Millennium

所有这些设备有一些基本的访问接口: CFI, JEDEC, MAP_ROM, MAP_RAM 最为常见的是 NAND 和 NOR

2.2 MTD User modules

MTD 提供多种访问方式:

  • Raw character access
  • Raw block access
  • Flash Translation Layer (FTL) (将MTD虚拟成一个块设备,则可像块设备一样在上建立文件系统)
  • NFTL
  • Journalling Flash File System, v2 (JFFS2)

JFFS2 provides a filesystem directly on the flash, rather than emulating a block device

NOTE: USB sticks, CompactFlash cards and other removable flash media are not MTD devices. They are block devices. They do contain flash chip inside, but they also contain some translation layer above which emulates block device. This translation layer is implemented in hardware. So for outside world these devices look exactly as hard drives, not like MTD devices.


NOR 一般实现 CFI 接口,让用户读取其参数。读写擦除数据可以像操作内存一样,其往往映射到一个固定的地址空间,比如: 0x1e00 0000

NAND 的读写则需要通过控制寄存器来操作

NOR Flash is connected to a address / data bus direct like other memory devices as SRAM etc.

NAND Flash uses a multiplexed I/O Interface with some additional control pins.

NOR flash is a random access device appropriate for code storage application.

NAND flash is a sequential access device appropriate for mass storage applications

NOR Flash can be used for code storage and code execution.

Code stored on NAND Flash can't be executed frome there. It must be loaded into RAM memory and executed from there.

4 MTD 上的文件系统

  • JFFS2 and YAFFS for bare NAND Flash and SmartMediaCards
  • NTFL for DiskOnChip devices
  • TRUEFFS from M-Systems for DiskOnChip devices
  • SmartMedia DOS-FAT as defined by the SSFDC Forum

5 Examples

root@nec:/root> xxd /dev/mtd4 | head -4
0000000: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000010: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000020: ffff ffff ffff ffff ffff ffff ffff ffff ................
0000030: ffff ffff ffff ffff ffff ffff ffff ffff ................

root@nec:/root> date > /dev/mtd4

root@nec:/root> xxd /dev/mtd4 | head -10
0000000: 5361 7420 4a61 6e20 2031 2030 303a 3137 Sat Jan 1 00:17
0000010: 3a33 3720 474d 5420 3230 3030 0aff ffff :37 GMT 2000....
0000020: ffff ffff ffff ffff ffff ffff ffff ffff ................

Can use the flash_eraseall (mtd-tools) commands to erase a whole MTD partition:
root@nec:/root> flash_eraseall /dev/mtd4

The mkfs.jffs2 tool is used to create a JFFS2 filesystem image; it populates the image with files from a given directory. For instance, to create a JFFS2 image for a flash partition of 3 MB total size and to populate it with the files from the /tmp/flashtools directory you would use:

# mkfs.jffs2 --pad=3145728 --eraseblock=262144 --root=/tmp/flashtools/ --output image.jffs2

# eraseall /dev/mtd4
Erased 3072 Kibyte @ 0 -- 100% complete.

# dd if=image.jffs2 of=/dev/mtd4 bs=256k
12+0 records in
12+0 records out

# mount -t jffs2 /dev/mtdblock4 /mnt

# df /mnt
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/mtdblock4            3072      2488       584 81% /mnt