Note-taking-for-Newifi2-D1-MTD
Hardware:Newifi2 D1
Bootloader:Breed written by hackpascal
Definition of MTD(Memory Technology Device)
A type of device file in Linux for interacting with flash memory. The MTD subsystem was created to provide an abstraction layer between the hardware-specific device drivers and higher-level applications.
Differences
Linux traditionally only knew block devices and character devices. Character devices were things like keyboards or mice, that you could read current data from, but couldn’t be seek-ed and didn’t have a size. Block devices had a fixed size and could be seek-ed. They also happened to be organized in blocks of multiple bytes, usually 512.
USB sticks, MMCs, SDs, CompactFlashes and other popular removable devices should not be confused with MTDs. Although they contain flash memory, this is hidden behind a block device interface using a FTL(Flash Translation Layer).
Based on how the flash memory chip is connected with the SoC (i.e. the “host”) OpenWrt distinguish between raw flash or host-managed and FTL (Flash Translation Layer) flash or self-managed: in case the flash memory chip is connected directly with the SoC we call it raw flash or host-managed and in case there is an additional controller chip between the flash memory chip and the SoC, we call it FTL flash or self-managed. Primarily the controller chip does wear-leveling and manages known bad blocks, but it may do other stuff as well. The flash memory cannot be accessed directly, but only through this controller. The controller has to be considered a black box.
The following table describes the differences between block devices and raw flashes. Note, SSD, MMC, eMMC, RS-MMC, SD, mini-SD, micro-SD, USB flash drive, CompactFlash, MemoryStick, MemoryStick Micro, and other FTL devices are block devices, not raw flash devices. For example, if you look inside an USB flash drive, you’ll find there a NAND chip (or several of them), and a micro-controller, which runs FTL firmware. Some USB flash drives are known to have quite powerful ARM processors inside. Similarly, MMC, eMMC, SD, SSD, and other FTL devices have a built-in controller which runs FTL firmware.
Block device | MTD device |
---|---|
Consists of sectors | Consists of eraseblocks |
Sectors are small (512, 1024 bytes) | Eraseblocks are larger (typically 128KiB) |
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 in modern LBA hard drives); in case of FTL devices it is the responsibility of FTL to provide this | Bad eraseblocks are not hidden and should be dealt with in software |
Sectors are devoid of the wear-out property (in FTL devices it is the responsibility of FTL to provide this) | Eraseblocks wear-out and become bad and unusable after about 103 (for MLC NAND) - 105 (NOR, SLC NAND) erase cycles |
Flash doesn’t match the description of either block or character devices. They behave similar to block device, but have differences. For example, block devices don’t distinguish between write and erase operations. Therefore, a special device type to match flash characteristics was created: MTD.
So MTD is neither a block nor a char device. There are translations to use them, as if they were. But those translations are nowhere near the original, just like translated Chinese poems.
When using an MTD, the use of an MTD aware file system such as JFFS2 or YAFFS is recommended. The MTD subsystem exports block devices as well, which allows the use of common filesystem like ext4.
Newifi2 D1 as an example
1 | cat /proc/mtd |
The dts description for Newifi2 D1 also can be found at git.openwrt.org.
ramips: fix support for MX25L25635F flash
1 | m25p80@0 { |
The hierarchy of flash storage partitions:
From the hierarchy diagram, it’s very clear why the following commands are used to write flash.
1 | BACKUP |
!
Please point out if there is a miscalculation or misunderstanding, thanks.
REF
- https://en.wikipedia.org/wiki/Memory_Technology_Device
- http://www.linux-mtd.infradead.org/faq/general.html#L_mtd_vs_hdd
- https://blog.ztjal.info/my-computer-using/hate-software/newifi-2-d1-pandorabox
- https://openwrt.org/docs/techref/flash.layout
- https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=311515&sid=c09401a847e4410acc8ecc0ffd974640
- https://blog.csdn.net/yubing_615/article/details/51860872
- https://opensourceforu.com/2012/01/working-with-mtd-devices/