您的位置:首页 > 运维架构 > Linux

MTD系列 - 关于linux MTD的一些理解

2014-09-11 12:03 218 查看
Memory Technology Device (MTD) Subsystem for Linux
一、MTD 的概念和层次
MTD(memory technology device 存储 技术设备 ) 是用于访问 memory 设备( ROM 、 flash )的 Linux 的子系统。 MTD 的主要目的是为了使新的 memory 设备的驱动更加简单,为此它在硬件和上层之间提供了一个抽象的接口。 MTD 的所有源代码在 /drivers/mtd 子目录下 。[1]
传统上, UNIX 只认识块设备和字符设备。字符设备是类似键盘或者鼠标的这类设备,你必须从它读取当前数据,但是不可以定位也没有大小。块设备有固定的大小并且可以定位,它们恰好组织成许多字节的块,通常为 512 字节。
闪存既不满足块设备描述也不满足字符设备的描述。它们表现的类似块设备,但又有所不同。比如,块设备不区分写和擦除操作。因此,一种符合闪存特性的特殊设备类型诞生了,就是 MTD 设备。所以MTD既不是块设备,也不是字符设备 。 [2]
关于 MTD 的层次,网络上有一张流传盛广的图片,如下所示,但是最初我看了这幅图根本是一点概念都没有的,不过通过看代码和网上查阅资料,知道了详细一点的分层结构,也纠正了一些前期对这张图的误解。

( 以下这部分纯属个人理解,如果有误,请高人拍砖!)
为了方便理解,先声明两点:
1. xxx 层 (MTD 原始设备层, MTD 块设备层 ) ,实现封装的代码。
2. xxx 设备 (MTD 原始设备, MTD 块设备 ) ,是 xxx 层向下封装后呈现给上层的表象就是一个 xxx 设备。
Flash 硬件驱动层:该层的基于特定处理器和特定 flash 芯片,这里以 pxa935 和Hynix NAND 512MB 1.8V 16-bit 为例。使用类型为 nand_chip, pxa3xx_nand_info, dfc_context, pxa3xx_bbm 这几个结构体来实现硬 件驱动。代码位于 drivers/mtd/nand 目录下。
MTD 原始设备层: 用类型 mtd_info 的结构体来描述 MTD 原始设备,该结构体中有一 个域会指向 Flash 硬件驱动层中所有使用的结构体 ( 串联形式,另外 pxa3xx_bbm 结构体是只在底层驱动中使用 ) 。
NAND flash 在嵌入式系统中通常需要划分多个分区,系统没有运行起来的时候分区表现为 mtd_partition 类型的结构体数组,该数组由 工程师自己决定。在系统初始化,确切的说是在 nand 的驱动加载时 执行相应的 prob 函数时,会将上述数组中的每一个分区用类型为 mtd_part 的结构体来描述。因为 mtd_part 结构体中内嵌了一个 mtd_info 的结构体,所以每一个分区在系统看来都是一个 MTD 原始 设备,另外 mtd_part 种还有一个
master 指针,指向描述整片 flash 原 始设备的 mtd_info 结构体,所以这个描述整片 nand 的 mtd_info 结构 体也被叫做主分区。
在 MTD 原始设备层和其上层 MTD 块设备层 (FTL) 活跃着一个牛 X 的指针数组 mtd_table ,定义于文件 mtdcore.c 中,该数组就是所有 MTD 原始设备的指针列表 ( 当然有数量限制,这里限制在 32 范围内 ) 。 不过上面所说的主分区没有在 mtd_table 之列。
如果你系统中有 2 片 nand flash ,每个有 8 个分区,那么系统中总 共存在有 18 个 mtd_info 结构体对象, mtd_table 数组中有 16 个指针 已经有归属。
本层和其上层 FTL之间就全靠mtd_table数组和
mtd_notifiers 链表来
联系了 ,至于如何联系的,下文再详细解释。
MTD 块设备层: 该层也叫flash 翻译层 (FTL) 。以前为了在 MTD 设备上使用某种传统 的文件系统, linux 系统中存在一个叫做 flash 翻译层 (FTL) ,该 FTL 是在 MTD 原始设备的基础上模拟出块设备,所以 FTL 以下的所有内 容呈现给上层的就是一个块设备。这样可以使用通用的块设备的接口 了。
这里也存在一个著名的结构体指针数组,定义于 mtdblock.c 文件中, 其中的每一个指针均指向一个 struct mtdblk_dev 的类型的对象,每一 个 struct mtdblk_dev 类型的对象都是一个 MTD 块设备。
网上流传着说使用该 FTL 如何不好,这种观点其实是基于使用传统文件系统存在的问题,现在有专门针对 nand flash 的 yaffs 日志型文 件系统了。所以那种掉电丢失数据的风险降低到了很小很小。
通用磁盘层: 再上层就是通用磁盘层了,其实每个分区在最后都是向通用磁盘层注 册成了一个 disk 来使用的,后面分析代码会看到这部分。当然对于 block 层的分析不在本文之中讨论。
下图是我自己画的草图, 表示向下封装,表示向上呈现:

或许你读了上面的内容还是感触不深,不要紧,接下来和我一起分析代码吧!
我一直在纠结该怎么来安排代码分析的顺序,如果直接从硬件驱动开始,那细节的东西太多,不足以体现出 MTD 机制来,看来还是先来分析一下 MTD 块设备层 (FTL) 这一承上启下的一层吧。
下面是打印出来的模块初始化调用顺序,供大家娱乐一下:
...
[ 3.513214] lizgo:calling init_mtd+0x0/0x44 @ 1
[ 3.517883] lizgo:calling cmdline_parser_init+0x0/0x1c @ 1
[ 3.523468] lizgo:calling init_mtdchar+0x0/0x98 @ 1
[ 3.528594] lizgo:calling init_mtdblock+0x0/0x1c @ 1
[ 3.533813] lizgo:calling nand_base_init+0x0/0x14 @ 1
[ 3.538909] lizgo:calling pxa3xx_nand_init+0x0/0x30 @ 1
...
二、英文资料原文及翻译 [3] :
MTD User modules

These are the modules which provide interfaces that can be used directly from userspace. The user modules currently planned include:
(MTD 子系统向上层提供的几种用户空间可直接使用的接口)
Raw character access
( 原始字符设备 )
A character device which allows direct access to the underlying memory. Useful for creating filesystems on the devices, before using some of the translation drivers below, or for raw storage on infrequently-changed flash, or RAM devices.
Raw block access
( 原始块设备 )
A block device driver which allows you to pretend(假设) that the flash is a normal device with sensible( 合理的 ) sector size. It actually works by caching
a whole flash erase block in RAM, modifying it as requested, then erasing the whole block and writing back the modified data.
( 块设备驱动允许你假设flash有合理的扇区大小。它实际上是依靠缓存整个擦除块到RAM中,修改之后,擦除整个块,然后将数据回写到flash上。)

This allows you to use normal filesystems on flash parts. Obviously(显然地) it's not particularly(格外地)
robust(健壮) when you are writing to it - you lose a whole erase block's worth of data if your read/modify/erase/rewrite cycle actually goes read/modify/erase/poweroff.
But for development, and for setting up filesystems which are actually going to be mounted read-only in production units, it should be fine.
( 这样就会允许你在某个分区上使用传统的文件系统,但是当你写数据的时候就会明显地降低了它的健壮性- 当你的read/modify/erase/rewrite正常周期突然变成了read/modify/erase/power
off的话,那么你就会丢失正常的数据。如果你将存在于其上的文件系统mount成只读文件系统,这是没有问题的。)
There is also a read-only version of this driver which doesn't have the capacity to do the caching and erase/writeback, mainly for use with uCLinux where the extra RAM requirement was considered too large.
( 存在一个只读版本的原始块设备驱动,该驱动没有缓存和擦除回写的能力,主要使用在uCLinux上)

Flash Translation Layer (FTL)
NFTL
Block device drivers which implement an FTL/NFTL filesystem on the underlying memory device. FTL is fully functional. NFTL is currently working for both reading and writing, but could probably do with some more
field testing before being used on production systems.
Journalling Flash File System, v2
This provides a filesystem directly on the flash , rather than emulating( 模拟 ) a block device. For more information, seesources.redhat.com.
MTD hardware device drivers
These provide physical access(
物理访问 ) to memory devices, and are not used directly - they are accessed through the user modules above(他们是通过上层的用户模块来访问的) .
On-board memory
Many PC chipsets(
芯片组 ) are incapable of correctly(不能正确地)caching system memory above 64M or 512M. A driver exists which allows you to use this
memory with the linux-mtd system.(有些PC
芯片组不能正确缓存高于 64M或者512M
的系统内存,那么就可以通过linux
的 mtd
来使用这些内存)
PCMCIA devices
PCMCIA flash (not CompactFlash but real flash) cards are now supported by the pcmciamtd driver in CVS.(PCMCIA闪存卡- 不是CF卡但是是真实的
flash)
Common Flash Interface (CFI) onboard NOR flash
This is a common solution and is well-tested and supported, most often using JFFS2 or cramfs file systems.
Onboard NAND flash
NAND flash is rapidly overtaking NOR flash due to its larger size and lower cost; JFFS2 support for NAND flash is approaching production quality.(NAND因其大容量和低成本正在飞速超越NOR)
M-Systems' DiskOnChip 2000 and Millennium
The DiskOnChip 2000, Millennium and Millennium Plus devices should be fully supported, using their native NFTL and INFTL 'translation layers'. Support for JFFS2 on DiskOnChip 2000 and Millennium is also operational although lacking proper support
for bad block handling.
CompactFlash - http://www.compactflash.org/ CompactFlash emulates an IDE disk, either through the PCMCIA-ATA standard, or by connecting directly to an IDE interface.
As such, it has no business being on this page, as to the best of my knowledge it doesn't have any alternative method of accessing the flash - you have to use the IDE emulation - I mention it here for completeness.
读了上面这些,下面的这张图应该比较容易理解吧!

下面是 MTD 设备节点的命名规则,这个使用什么样的用户访问接口有关系。
Table 7-1. MTD /dev / entries, corresponding ( 对应的 ) MTD user modules, and relevant ( 相应的 ) device major numbers
/dev entry
Accessible MTD user module
Device type
Major number
mtdN
char device
char
90
mtdrN
read-only char device
char
90
mtdblockN
Block device, read-only block
device, JFFS, and JFFS2
block
31
nftlLN
NFTL
block
93
ftlLN
FTL
block
44
Table 7-2. MTD /dev / entries, minor numbers, and naming schemes( 方案 )
/dev entry
Minor number range
Naming scheme
mtdN
0 to 32 per increments of 2
N = minor / 2 偶数
mtdrN
1 to 33 per increments of 2
N = (minor - 1) / 2 奇数
mtdblockN
0 to 16 per increments of 1
N = minor
nftlLN
0 to 255 per sets of 16
L = set;[2]N = minor
- (set - 1) x 16;N is not appended to entry name if its value is zero.
ftlLN
0 to 255 per sets of 16
Same as NFTL.
有了上面的这些预备知识,以后的代码分析或许会更加容易理解些!
/article/2889949.html

------------------------------------------------------------------------------------------------------------------------------------------------------------------
MTD(Memory Technology Device,内存技术设备)
作用:
1、建立 Flash针对Linux的统一抽象的接口。
2、文件系统与底层Flash存储器进行了隔离。
3、无需关注是字符设备或者块设备。
组成:
由四层组成。
A、设备节点
通过mknod在/dev目录下建立MTD字符设备节点.
字符设备节点主设备号90;块设备节点主设备号31.
B、MTD设备层
注册一系列file_operation函数。
C、MTD原始设备层
原始设备通用代码和各个特定flash的数据(例如分区)
D、硬件驱动层
Flash硬件的读、写、擦除。
norflash: /driver/mtd/chips
nand: /driver/mtd/nand
引入MTD后,驱动工程师直接与MTD原始设备层打交道,利用其提供的接口注册设备和分区。
重要结构mtd_info,定义了MTD的数据和操作函数。每一个分区也被认为是一个mtd_info,3个分区就有三个mtd_info结构体,这些指针被存放在mtd_table中。

http://blog.chinaunix.net/uid-25087291-id-3450265.html

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

mtd层次分析

MTD(memory technology device内存技术设备)是用于访问memory设备(ROM、flash)的Linux的子系统。MTD的主要目的是为了使新的memory设备的驱 动更加简单,为此它在硬件和上层之间提供了一个抽象的接口。MTD的所有源代码在/drivers/mtd子目录下。我将CFI接口的MTD设备分为四层
(从设备节点直到底层硬件驱动),这四层从上到下依次是:设备节点、MTD设备层、MTD原始设备层和硬件驱动层。

  一、Flash硬件驱动层:硬件驱动层负责在init时驱动Flash硬件,Linux MTD设备的NOR Flash芯片驱动遵循CFI接口标准,其驱动程序位于drivers/mtd/chips子目录下。NAND型Flash的驱动程 序则位于/drivers/mtd/nand子目录下。

  二、MTD原始设备:原始设备层有两部分组成,一部分是MTD原始设备的通用代码,另一部分是各个特定的Flash的数据,例如分区。 用于描述MTD原始设备的数据结构是mtd_info,这其中定义了大量的关于MTD的数据和操作函数。mtd_table(mtdcore.c)则是所 有MTD原始设备的列表,mtd_part(mtd_part.c)是用于表示MTD原始设备分区的结构,其中包含了mtd_info,因为每一个分区都
是被看成一个MTD原始设备加在mtd_table中的,mtd_part.mtd_info中的大部分数据都从该分区的主分区 mtd_part->master中获得。 在drivers/mtd/maps/子目录下存放的是特定的flash的数据,每一个文件都描述了一块板子上的flash。其中调用 add_mtd_device()、del_mtd_device()建立/删除mtd_info结构并将其加入/删除mtd_table(或者调用 add_mtd_partition()、del_mtd_partition()(mtdpart.c)建立/删除mtd_part结构并将
mtd_part.mtd_info加入/删除mtd_table 中)。

  三、MTD设备层:基于MTD原始设备,linux系统可以定义出MTD的块设备(主设备号31)和字符设备(设备号90)。MTD字符设备的定义在 mtdchar.c中实现,通过注册一系列file operation函数(lseek、open、close、read、write)。MTD块设备则是定义了一个描述MTD块设备的结构
mtdblk_dev,并声明了一个名为mtdblks的指针数组,这数组中的每一个mtdblk_dev和mtd_table中的每一个 mtd_info一一对应。

  四、设备节点:通过mknod在/dev子目录下建立MTD字符设备节点(主设备号为90)和MTD块设备节点(主设备号为31),通过访问此设备节点即可访问MTD字符设备和块设备。

  五、根文件系统:在Bootloader中将JFFS(或JFFS2)的文件系统映像jffs.image(或jffs2.img)烧到flash的 某一个分区中,在/arch/arm/mach-your/arch.c文件的your_fixup函数中将该分区作为根文件系统挂载。

  六、文件系统:内核启动后,通过mount 命令可以将flash中的其余分区作为文件系统挂载到mountpoint上。

/article/9013379.html

ls -l /dev/mtd*
cat /proc/mtd

cat /proc/partition
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: