转:http://www.crifan.com/linux_system_in__dev__mtd_and__dev__mtdblock_distinction_character_devices_and_block_devices_mtd_difference/
1. /dev/mtdN 是Linux 中的MTD架构中,系统自己实现的mtd分区所对应的字符设备,其里面添加了一些ioctl,支持很多命令,如MEMGETINFO,MEMERASE等。
而mtd-util中的flash_eraseall等工具,就是以这些ioctl为基础而实现的工具,实现一些关于Flash的操作。比如,mtd 工具中的 flash_eraseall中的:
if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
fprintf(stderr, "%s: %s: unable to get MTD device infon", exe_name, mtd_device);
return 1;
}
其中,MEMGETINFO,就是Linux MTD中的drivers/mtd/nand/mtdchar.c中的:
static int mtd_ioctl(struct inode *inode, struct file *file,
u_int cmd, u_long arg)
{
。。。。。
case MEMGETINFO:
info.type = mtd->type;
info.flags = mtd->flags;
info.size = mtd->size;
info.erasesize = mtd->erasesize;
info.writesize = mtd->writesize;
info.oobsize = mtd->oobsize;
/* The below fields are obsolete */
info.ecctype = -1;
info.eccsize = 0;
if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
return -EFAULT;
break;
。。。
}
而/dev/mtdblockN,是Nand Flash驱动中,驱动在用add_mtd_partitions()添加MTD设备分区,而生成的对应的块设备。
根据以上内容,也就更加明白,为什么不能用nandwrite,flash_eraseall,flash_erase等工具去对/dev/mtdblockN去操作了。因为/dev/mtdblock中不包含对应的ioctl,不支持你这么操作。
2. mtd char 设备的主设备号是90,而mtd block设备的主设备号是31:
# ls /dev/mtd? -l
crw-r—– 1 root root 90, 0 May 30 2007 /dev/mtd0
crw-r—– 1 root root 90, 2 May 30 2007 /dev/mtd1
crw-r—– 1 root root 90, 4 Jul 17 2009 /dev/mtd2
crw-r—– 1 root root 90, 6 May 30 2007 /dev/mtd3
crwxrwxrwx 1 root root 90, 8 May 30 2007 /dev/mtd4
crwxrwxrwx 1 root root 90, 10 May 30 2007 /dev/mtd5
crwxrwxrwx 1 root root 90, 12 May 30 2007 /dev/mtd6
crwxrwxrwx 1 root root 90, 14 May 30 2007 /dev/mtd7
crwxrwxrwx 1 root root 90, 16 May 30 2007 /dev/mtd8
crwxrwxrwx 1 root root 90, 18 May 30 2007 /dev/mtd9
# ls /dev/mtdblock? -l
brw-r—– 1 root root 31, 0 May 30 2007 /dev/mtdblock0
brw-r—– 1 root root 31, 1 May 30 2007 /dev/mtdblock1
brw-r—– 1 root root 31, 2 May 30 2007 /dev/mtdblock2
brw-r—– 1 root root 31, 3 May 30 2007 /dev/mtdblock3
brwxrwxrwx 1 root root 31, 4 May 30 2007 /dev/mtdblock4
brwxrwxrwx 1 root root 31, 5 May 30 2007 /dev/mtdblock5
brwxrwxrwx 1 root root 31, 6 May 30 2007 /dev/mtdblock6
brwxrwxrwx 1 root root 31, 7 May 30 2007 /dev/mtdblock7
brwxrwxrwx 1 root root 31, 8 May 30 2007 /dev/mtdblock8
brwxrwxrwx 1 root root 31, 9 May 30 2007 /dev/mtdblock9
此设备号,定义在/include/linux/mtd/mtd.h中 :
#define MTD_CHAR_MAJOR 90
#define MTD_BLOCK_MAJOR 31
3. 其中,mtd的块设备的大小,可以通过查看分区信息获得:
# cat /proc/partitions
major minor #blocks name
31 0 1024 mtdblock0
31 1 8192 mtdblock1
31 2 204800 mtdblock2
31 3 65536 mtdblock3
31 4 225280 mtdblock4
上面中显示的块设备大小,是block的数目,每个block是1KB。
而每个字符设备,其实就是对应着上面的每个块设备。即/dev/mtd0对应/dev/mtdblock0,其他以此类推。换句话说,mtdblockN的一些属性,也就是mtdN的属性,比如大小。
4。对每个mtd字符设备的操作,比如利用nandwrite去对/dev/mtd0写数据,实际就是操作/dev/mtdblock0。
而这些操作里面涉及到的偏移量offset,都指的是此mtd 分区内的偏移。比如向/dev/mtd1的offset为0的位置写入数据,实际操作的是物理偏移offset=/dev/mtd0的大小=1MB=0×100000。
5.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 |
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. |
上表相关内容,请参考附录2.
【附录】
1.最新的mtd-utils-1.3.0.tar.bz2 下载地址:ftp://ftp.infradead.org/pub/mtd-utils/mtd-utils-1.3.0.tar.bz2
2.7.1 MTD-Supported Devices