MTD Application Learning: The difference between mtd and mtdblock today we use the flash_eraseall and flash_cp tools in mtd-utils for the upgrade solution. During solution verification, we encountered various puzzles and doubts, because I am not familiar with the MTD principle, I can only try it multiple times. Although I have finished the solution, I think the difference between MTD and mtdblock in mtd is worth summing up and learning. Here we will explain the problem first, and then explain the difference Principle in detail. MTD-utils tool for mtd and mtdblock partition device difference processing 1/$ flash_eraseall/dev/mtdblock/22flash_eraseall: /dev/mtdblock/2: unable to get MTD device info3/$ flash_eraseall/dev/mtdblock/24flash_eraseall:/dev/mtdblock/2: unable to get MTD device info5/$ flash_eraseall/dev/mtd/26 Erasing 128 Kibyte @ 8e0000 -- 98% complete.7/$ ls1/$ flashcp rootfs_version/dev/mtdblock22This doesn't seem O be a valid MTD flash device! 3/$ flashcp rootfs_version/dev/mtdblock/24 This doesn't seem to be a valid MTD flash device! 5/$ flashcp rootfs_version/dev/mtd26/$ differences between lsmtd and mtdblock partition device mount 1/$ mount-t jffs2/dev/mtd/2 qqzm/2 mount: mounting/dev/mtd/2 on qqzm/failed: Invalid argument3/$ mount-t jffs2/dev/mtd2 qqzm/4 mount: Mounting/dev/mtd2 on qqzm/failed: invalid argument5/$ mount-t jffs2/dev/mtdblock/2 qqzm/6/$ lsmtdblock mounted successfully, failed to unmount after a single erasure 01/$ flash_eraseall/dev/mtd/2 <span> </span> Erasing 128 Kibyte @ 8e00 00 -- 98% complete.02/qqzm $ mount03/dev/root on/type jffs2 (rw, noatime) 04 proc on/proc type proc (rw, nodiratime) 05 sysfs on/sys type sysfs (rw) 06 devfs on/dev type devfs (rw) 07 devpts on/dev/pts type devpts (rw) 08/dev/mmcblk0p1 on/mnt/sd type vfat (rw, nodiratime, fmask = 0022, dmask = 0022, codepage = cp437, iocharset = iso8859-1) 09/dev/mtdblock/2 on/qqzm type jffs2 (rw, noatime) 10 none on/qqzm/www/cgi-B In/tmp type ramfs (rw) 11/qqzm $ cd .. 12/$ umount/volume: Couldn't umount/qqzm: Inappropriate ioctl for device14/$ umount/dev/mtdblock/215 umount: Couldn't umount/dev/mtdblock/2: inappropriate ioctl for device16/$ through the above continuous attempts and error feedback, I have passed the basic verification of the solution, but the principle is not clear: why does mtd and mtdblock perform different operations in the same device partition? Can only Block devices be mounted through the mount command? What does Inappropriate ioctl for device mean when you detach an mtdblock device? What does unable to get MTD device info mean? The basic principle of MTD technology MTD (memory technology device) is a Linux subsystem used to access memory devices (ROM, flash. The main purpose of MTD is to make the drive of the new memory device simpler. Therefore, MTD provides an abstract interface between the hardware and the upper layer and implements a hierarchical division, the device file, MTD device layer, MTD raw device layer, and hardware driver layer are roughly listed from top to bottom. All source code of MTD is in the/drivers/mtd subdirectory. MTD device files in the system 01 ~ $ Ls/dev/mtd *-l02crw-rw ---- 1 root 90, 0 Jan 1/dev/mtd003crw-rw ---- 1 root 90, 1 Jan 1/dev/mtd0ro04crw-rw ---- 1 root 90, 2 Jan 1/dev/mtd105crw-rw ---- 1 root 90, 3 Jan 1/dev/mtd1ro06crw-rw ---- 1 root 90, 4 Jan 1/dev/mtd207crw-rw ---- 1 root 90, 5 Jan 1/dev/mtd2ro08crw-rw ---- 1 root 90, 6 Jan 1/dev/mtd309crw-rw ---- 1 root 90, 7 Jan 1/dev/mtd3ro10brw-rw ---- 1 root 31, 0 Jan 1/dev/mtdblock011brw-rw ---- 1 root 31, 1 Jan 1/dev/mtdblock112brw-rw ---- 1 root 31, 2 Jan 1/dev/mtdblock213brw-rw ---- 1 root 31, 3 Jan 1/dev/mtdblock314 15/dev/mtd: 16crw-rw-rw-1 root 90, 0 Jan 1 017cr -- r -- 1 root 90, 1 Jan 1 00:00 0ro18crw-rw-rw-1 root 90, 2 Jan 1 00:00 119cr -- r -- 1 root 90, 3 Jan 1 00:00 1ro20crw-rw-rw-1 root 90, 4 Jan 1 221cr -- r -- 1 root 90, 5 Jan 1 2ro22crw-rw-rw-1 root 90, 6 Jan 1 323cr -- r -- 1 root 90, 7 Jan 1 3ro24 25/dev/mtdblock: 26brw ------- 1 root 31, 0 Jan 1 00:00 027brw ------- 1 root 31, 1 Jan 1 00:00 128brw ------- 1 root 31, 2 Jan 1 00:00 229brw- ------ 1 root 31, 3 Jan 1 330 ~ $ MtdN and the corresponding/dev/mtd/N, mtdblockN, and the corresponding/dev/mtdblock/n mtd devices are displayed, which are character devices, primary device No. 90 and block device, primary device No. 31. Among them,/dev/mtd0 and/dev/mtd/0 are completely equivalent, and/dev/mtdblock0 and/dev/mtdblock/0 are completely equivalent, the/dev/mtd0 and/dev/mtdblock0 are two different application descriptions of the same MTD partition, which have different operations. /Dev/mtdN device/dev/mtdN is the character device corresponding to the MTD partition implemented in the mtd architecture (mtd devices are divided into multiple zones and each zone is a character device ), some ioctl is added to it, supporting many commands, such as MEMGETINFO and MEMERASE. Tools such as flash_eraseall in mtd-utils are implemented based on these ioctl to implement some Flash operations. For example, in flash_eraseall in mtd: 1if (ioctl (fd, MEMGETINFO, & meminfo )! = 0) 2 {3 fprintf (stderr, "% s: unable to get MTD device info \ n", exe_name, mtd_device); 4 return 1; 5} MEMGETINFO is drivers/MTD/mtdchar in Linux mtd. the ioctl command in c. To use the mtd character device, the mtdchar kernel module needs to be loaded. This Code explains the first phenomenon above. The/dev/mtdblockN device/dev/mtdblockN is the block device generated when the MTD device partition is added with add_mtd_partitions () in the Flash drive. The MTD block device driver can disguise flash devices as block devices. In fact, it places the entire erase block in ram for access and then updates it to flash, you can create a common file system on this block device. For MTD Block devices, the MTD device layer does not provide ioctl implementation methods, so there will be no corresponding MEMGETINFO commands and so on. Therefore, nandwrite, flash_eraseall, flash_erase and other tools to operate on/dev/mtdblockN; otherwise, the above symptom 1 will appear, and also explains symptom 3-after the partition is erased using mtd2, using mtdblock2 for umount can cause confusion. The size of mtd Block devices can be viewed through the proc file system: 01 ~ $ Cat/proc/mongominor # blocks name03 04 31 0 512 mtdblock005 31 1 1024 mtdblock106 31 2 5632 mtdblock207 31 3 9216 mtdblock308 254 0 30760960 mmcblk0 09 254 1 30756864 mmcblk0p110 ~ $ The two following are the partition size of the SD block device. The size of each block is 1 kb. MTD device partition and summary view mtd device partition information through the proc file system: 1 ~ $ Cat/proc/mtd2dev: size erasesize name3mtd0: 00080000 00020000 "boot" 4mtd1: 00100000 00020000 "kernel" 5mtd2: 00580000 00020000 "roofs70" 6mtd3: 00900000 00020000 "app" 7 ~ $ We can find that mtdN and mtdblockN are described in the same MTD partition and correspond to the same hardware Partition. the sizes of the two are the same, however, the MTD device layer provides different views for the upper layer, and provides two operation views: character and block device. For the convenience and needs of upper layer usage, such as the requirement of the mount command, you can only mount Block devices (with file systems), but not character devices. Otherwise, the above phenomenon 2 is invalid. Here is a brief summary of the Use Cases of mtd and mtdblock devices: the mtd-utils tool can only be used with the/dev/mtdN MTD character device mount, umount command only for the/dev/mtdblockN MTD block device/dev/mtdN and/dev/mtdblockN is the same partition of the same MTD device (N is the same)