mtd命令及制作ubi镜像做根文件系统

时间:2022-11-19 10:04:12
在linux2.6.28后才加入对ubifs的支持

1 查看nand分区

root@ubuntu:~# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00020000 00020000 "U-Boot-min"
mtd1: 00240000 00020000 "U-Boot"
mtd2: 00020000 00020000 "U-Boot Env"
mtd3: 00440000 00020000 "Kernel"
mtd4: 1f400000 00020000 "File System"
mtd5: 00540000 00020000 "Reserved"

root@ubuntu:~# cat /proc/partitions
major minor  #blocks  name

  31        0        128 mtdblock0
  31        1       2304 mtdblock1
  31        2        128 mtdblock2
  31        3       4352 mtdblock3
  31        4     512000 mtdblock4
  31        5       5376 mtdblock5
root@ubuntu:~#

2、查看mtd4的信息
root@ubuntu:~# mtdinfo -m 4 -u
mtd4
Name:                           File System
Type:                           nand
Eraseblock size:                131072 bytes, 128.0 KiB
Amount of eraseblocks:          4000 (524288000 bytes, 500.0 MiB)
Minimum input/output unit size: 2048 bytes
Sub-page size:                  512 bytes
OOB size:                       64 bytes
Character device major/minor:   90:8
Bad blocks are allowed:         true
Device is writable:             true
Default UBI VID header offset:  512
Default UBI data offset:        2048
Default UBI LEB size:           129024 bytes, 126.0 KiB
Maximum UBI volumes count:      128

root@ubuntu:~# mtdinfo -m 2 -u

root@ubuntu:~# mtdinfo /dev/mtd4
mtd2
Name:                           U-Boot Env
Type:                           nand
Eraseblock size:                131072 bytes, 128.0 KiB     // FLASH物理擦除块大小
Amount of eraseblocks:          1 (131072 bytes, 128.0 KiB)
Minimum input/output unit size: 2048 bytes     1)nor flash:通常是1个字节   2)nand falsh:一个页面 
Sub-page size:                  512 bytes   //对于nand flash来说,子页大小 
OOB size:                       64 bytes
Character device major/minor:   90:4
Bad blocks are allowed:         true
Device is writable:             true
Default UBI VID header offset:  512
Default UBI data offset:        2048
Default UBI LEB size:           129024 bytes, 126.0 KiB   //逻辑擦除块大小
Maximum UBI volumes count:      128

mtd4大小为500MiB,擦除单元大小(一般即为块大小)为128KiB,名字是"NAND simulator partition 0"。 NandFlash

擦除是以块(block)为单位,读写是以页(page)为单位。

3 root@ubuntu:~# ls -lah /dev/mtd*
crw------- 1 root root 90,  0 Jan  1 00:00 /dev/mtd0 //字符设备
crw------- 1 root root 90,  1 Jan  1 00:00 /dev/mtd0ro
crw------- 1 root root 90,  2 Jan  1 00:00 /dev/mtd1
crw------- 1 root root 90,  3 Jan  1 00:00 /dev/mtd1ro
crw------- 1 root root 90,  4 Jan  1 00:00 /dev/mtd2
crw------- 1 root root 90,  5 Jan  1 00:00 /dev/mtd2ro
crw------- 1 root root 90,  6 Jan  1 00:00 /dev/mtd3
crw------- 1 root root 90,  7 Jan  1 00:00 /dev/mtd3ro
crw------- 1 root root 90,  8 Jan  1 00:00 /dev/mtd4
crw------- 1 root root 90,  9 Jan  1 00:00 /dev/mtd4ro
crw------- 1 root root 90, 10 Jan  1 00:00 /dev/mtd5
crw------- 1 root root 90, 11 Jan  1 00:00 /dev/mtd5ro
brw-rw---- 1 root disk 31,  0 Jan  1 00:00 /dev/mtdblock0  //块设备,与mtd0对应
brw-rw---- 1 root disk 31,  1 Jan  1 00:00 /dev/mtdblock1
brw-rw---- 1 root disk 31,  2 Jan  1 00:00 /dev/mtdblock2
brw-rw---- 1 root disk 31,  3 Jan  1 00:00 /dev/mtdblock3
brw-rw---- 1 root disk 31,  4 Jan  1 00:00 /dev/mtdblock4
brw-rw---- 1 root disk 31,  5 Jan  1 00:00 /dev/mtdblock5
root@ubuntu:~#


4.
关于mtd工具集的安装
    sudo apt-get install mtd-utils
UBI文件系统镜像文件的制作
@ubuntu:~$ sudo mkfs.ubifs -r targetfs -m 2048 -e 129024 -c 3900 -o ubifs.img
@ubuntu:~$ sudo ubinize -o ubi.img -m 2048 -p 128KiB -s 512 ubinize.cfg

关于mkfs.ubifs参数的算法
  -m minimum I/O unit size
  -e, --leb-size=SIZE      logical erase block size
  -c maximum logical erase block count
  -x compression type - "lzo", "favor_lzo", "zlib" or "none" (default: "lzo")
  -p size of the physical eraseblock of the flash this UBI image is created for in bytes

wear_level_reserved_blocks is 1% of total blcoks per device
*logical_erase_block_size* is physical erase block size minus 2 pages for UBI
Block size = page_size * pages_per_block
physical blocks on a partition = partition size / block size
Logical blocks on a partition = physical blocks on a partitiion - reserved for wear level
File-system volume = Logical blocks in a partition * Logical erase block size

关于参数可以参考attach的命令输出:

root@ubuntu:~# ubiattach  /dev/ubi_ctrl -m 4 -d 0
UBI device number 0, total 4000 LEBs (516096000 bytes, 492.2 MiB), available 0 LEBs (0 bytes), LEB

size 129024 bytes (126.0 KiB)
root@ubuntu:~#

ubinize.cfg文件

[ubifs]
mode=ubi
image=ubifs.img
vol_id=0
vol_size=450MiB
vol_type=dynamic
vol_alignment=1
vol_name=rootfs
vol_flags=autoresize


5. UBI文件系统镜像在Linux下的烧写
flash_eraseall /dev/mtd4
ubiformat /dev/mtd4 -s 512 -f /xxx/ubi.img

6、 UBI文件系统镜像在U-BOOT下的烧写
//load ubi image to RAM
tftp ubi.img
//erase MTD4 nand space
nand erase 0x6c0000 0xc820000
//write image to nand
nand write.i 0x81000000 0x6c0000 0xxxxx(image size)

7. UBI文件系统镜像在Linux下的挂载和卸载

挂载
ubiattach /dev/ubi_ctrl -m 4 -d 0
mount -t ubifs ubi0_0 /mnt/ubi


卸载
umount /mnt/ubi
ubidetach -d 0

8、使用ubi做根文件系统
需要在bootargs中设置如下信息:

  root=ubi0:rootfs ubi.mtd=4 rootfstype=ubifs


 配置linux内核
           配置的时候选上
          1)Device Drivers  --->Memory Technology Device (MTD) support  --->UBI - Unsorted block images  --->Enable UBI
          2)File systems  --->Miscellaneous filesystems  --->UBIFS file system support
          这样我们的内核就支持UBIFS文件系统了