本节介绍File System和MTD技术
一 FS
熟知的FS有ext2,3,4.但是这些都是针对磁盘设备的。而ES中一般的存储设备为Flash,由于Flash的特殊性:
所以,针对FLASH的特性,整出了一个Journaling Flash File System(JFFS2,第二版)。它的特点就是:
(apt-get install mtd-tools,下载不少好工具,另外,可见往flash设备写log,可能会导致flash短命喔)
一些伪文件系统:proc/sysfs,systool工具用来分析sysfs。
ES中常用的FS还有ramfs/rootfs/tmpfs,都是基于内存的FS。这个和前面讲的initramfs有什么关系?实际上这些是基于Initramfs的。
这里要解释一下几个比如容易混淆的东西:
下面介绍一下如何利用mount loop创建一个虚拟的基于文件的BLOCK设备。
二 MTD技术
MTD全称是Memory Technology Device,内存技术设备?实际上是一个虚拟设备驱动层,类似Virtual File System。它提供标准API给那些操作Raw Flash的device driver。那么Flash device和普通的Block device的区别是什么呢?
这里要重点指出的是:
SD/MMC卡、CF(Compact Flash)卡、USB Flash等并不是MTD设备,因为这些设备中已经有一个内置的Flash Translation Layer,这个layer处理erase、wear leveling事情了(这个TL应该是固件中支持的)。所以这些设备直接当做普通的Block Device使用
(上面的描述还是没能说清楚MTD到底是怎么用的,以后会结合源码分析一下)
2.1 内核中启用MTD支持
这个很简单,make menuconfig的时候打开就行了,有好几个选项。
图1 LK中MTD支持的配置选项
其中:
MTC_CHAR和MTD_BLOCK用来支持CHAR模式和BLOCK模式读写MTD设备。这个和普通的char以及block设备意思一样
最后两个是在内核中设置一个MTD test驱动。8192K用来设置总大小,第二个128用来设置block size。就是在内核中搞了一个虚拟的Flash设备,用作测试
ES中又如何配置MTD及其相关的东西呢?
为Flash Disk设置分区(也可以整个Device就一个分区。BTW,我一直没彻底搞清楚分区到底是想干什么,这个可能是历史原因啦....)
设置Flash的类型以及location。Flash设备分为NOR和NAND,本节最后会简单介绍下二者的区别。
为Flash芯片选择合适的driver
为LK配置driver
下面先看看分区的设置
可对Flash分区,这里有一些稍微重要的内容:如何把Flash分区的信息传递给LK呢?有两种方法:
将整个device的分区情况存在一个BLock中,这样BootLoader启动的时候,根据这个BLock中的内容建立相应信息等。好像只有Red Boot支持。所以叫RedBoot Partition Table。另外,LK可以识别这种分区,通过CFI(Command Flash Interface)读取这个分区的信息。
Kernel Command Line Partitioning:通过Kernel启动的时候传入参数,不过KL必须配置一下。Command格式如下:
图2
再看看Driver的Mapping,也就是将MTD和对应的Flash Driver配对...
kernel/drivers/mtd/maps......,以后要分析
Flash芯片本身的Driver呢?
kernel/drivers/mtd/chips,目前比较流行的是CFI接口
三 一些参考资料和补充知识
http://www.linux-mtd.infradead.org/
MTD的本意是:
We‘re working on a generic Linux subsystem for memory devices, especially
Flash devices.
The aim of the system is to make it simple to provide a driver
for new hardware, by providing a generic interface between the hardware drivers
and the upper layers of the system.
Hardware drivers need to know nothing
about the storage formats used, such as FTL, FFS2, etc., but will only need to
provide simple routines for read, write and erase. Presentation of the device‘s
contents to the user in an appropriate form will be handled by the upper layers
of the system.
MTD overview
MTD subsystem (stands for Memory Technology Devices) provides
an abstraction layer for raw flash devices. It makes it possible to use the same
API when working with different flash types and technologies, e.g. NAND,
OneNAND, NOR, AG-AND, ECC‘d NOR, etc.
MTD subsystem does not deal with block
devices like MMC, eMMC, SD, CompactFlash, etc. These devices are not raw flashes
but they have a Flash Translation layer inside, which makes them look like block
devices. These devices are the subject of the Linux block subsystem, not MTD.
Please, refer to this FAQ section for a short list of the main differences
between block and MTD devices. And the raw flash vs. FTL devices UBIFS section
discusses this in more details.
MTD subsystem has the following interfaces.
Additionally, MTD supports legacy FTL/NFTL "translation layers", M-Systems‘ DiskOnChip 2000 and Millennium chips, and PCMCIA flashes (pcmciamtd driver). But the corresponding drivers are very old and not maintained very much.
MTD Block Driver:
The mtdblock driver available in the MTD is an archaic tool which emulates
block devices on top of MTD devices. It does not even have bad eraseblock
handling, so it is not really usable with NAND flashes. And it works by caching
a whole flash erase block in RAM, modifying it as requested, then erasing the
whole block and writing back the modified. This means that mtdblock does not try
to do any optimizations, and that you will lose lots of data in case of power
cuts. And last, but not least, mtdblock does not do any wear-leveling.
Often
people consider mtdblock as general FTL layer and try to use block-based file
systems on top of bare flashes using mtdblock. This is wrong in most cases. In
other words, please, do not use mtdblock unless you know exactly what you are
doing.
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
These are the modules which provide interfaces that can be used directly from userspace. The user modules currently planned include:
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.
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. 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.
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, see sources.redhat.com.
这里牵扯到NOR和NAND,那么二者有啥区别呢?
Beside the different silicon cell design, the most important difference between NAND and NOR Flash is the bus interface. 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. NAND flash is a sequential access device appropriate for mass storage applications, while NOR flash is a random access device appropriate for code storage application. NOR Flash can be used for code storage and code execution. Code stored on NAND Flash can‘t be executed from there. It must be loaded into RAM memory and executed from there.
linux kernel系列四:嵌入式系统中的文件系统以及MTD,布布扣,bubuko.com
linux kernel系列四:嵌入式系统中的文件系统以及MTD
原文:http://www.cnblogs.com/xmphoenix/p/3672031.html