ubi命令用法(linux)

原文网址:ubi命令用法(linux)_IT利刃出鞘的博客-CSDN博客_ubi命令

简介

内核启动后,会自动创建/dev/ubi_ctrl、/sys/class/misc/ubi_ctrl、/sys/class/ubi/version。

本部分命令所在软件包:ubi-utils

ubinfo

作用

提供从系统中找到的UBI设备、UBI卷的相关信息

格式

参数

-d, --devn=<UBI device number>

UBI device number to get information about     

-n, --vol_id=<volume ID>       

ID of UBI volume to print information about    

-N, --name=<volume name>       

name of UBI volume to print information about  

-a, --all                      

print information about all devices and volumes,or
about all volumes if the UBI device was       

specified                                        

-h, --help     

print help message   

-V, --version  

print program version

示例

ubinfo                        - (no arguments) print general UBI information

ubinfo -d 1                - print information about UBI device number 1

ubinfo /dev/ubi0 -a    - print information about all volumes of UBI device /dev/ubi0

ubinfo /dev/ubi1_0    - print information about UBI volume /dev/ubi1_0

ubinfo -a                   - print all information

ubiformat

作用

格式化MTD设备,擦除Flash,保存擦除计数,写入UBI镜像到Flash;

flash_erase命令有类似的效果。

详细操作:

1.从/sys/class/mtd/mtdN下的节点中读数据
2. 确保节点没有attach

3. 扫描所有块:

    检查坏块、读出EC header、检查是否为ubi块、记录擦除次数
4.擦除对应分区的所有块,并且写入所有块的EC header
5.写布局卷(layout volume)
    创建空的volume table(除了mtd的crc外,其他全是0)
    设置第0块和第1块的EC header和VID header写到其对应位置(EC header在第0页,
VID header在第1页)
    把创建的空的vtbl(volume table)写到第0块和第1块的data_offset处。(data_offset
是EC header的成员)

EC header是在一个擦除块的开头,而VID header的位置有几个情况: 

若是nor flash有1字节操作单元,通常VID header的位置在偏移64字节处
若nand不支持sub-pages,那么VID header在下一个nand page处
若nand支持sub-pages,那么VID header就在下一个sub-pages处
我们这里是使用4KB的pagesize,所以在0x1000的偏移处。

格式

ubiformat <MTD device node file name> [-s <bytes>] [-O <offs>] [-n]

         [-Q <num>] [-f <file>] [-S <bytes>] [-e <value>] [-x <num>] [-y]
         [-q] [-v] [-h] [--sub-page-size=<bytes>] 

         [--vid-hdr-offset=<offs>]

         [--no-volume-table][--flash-image=<file>]
          [--image-size=<bytes>] [--erase-counter=<value>]

          [--image-seq=<num>] [--ubi-ver=<num>] [--yes] [--quiet]

          [--verbose] [--help] [--version]

参数

-s, --sub-page-size=<bytes>                  

                            

minimum input/output unit used for UBI headers, e.g. sub-page size in case of NAND flash (equivalent to the minimum input/output unit size by default)                   

-O, --vid-hdr-offset=<offs>

offset if the VID header from start of the physical eraseblock (default is the next minimum I/O unit or sub-page after the EC header

-n, --no-volume-table    

only erase all eraseblock and preserve erase counters, do not write empty volume table

-f, --flash-image=<file>    

flash image file, or '-' for stdin            

-S, --image-size=<bytes>  

bytes in input, if not reading from file      

-e, --erase-counter=<value> 

use <value> as the erase counter value for all eraseblocks                                 

-x, --ubi-ver=<num>  

UBI version number to put to EC headers (default is 1)                         

-Q, --image-seq=<num>                

32-bit UBI image sequence number to use       

(by default a random number is picked)        

-y, --yes    

assume the answer is "yes" for all question this program would otherwise ask              

-q, --quiet                 

suppress progress percentage information      

-v, --verbose               

be verbose                                    

-h, -?, --help

print help message                            

-V, --version

print program version                         

示例

ubiformat /dev/mtd0 -y          - format MTD device number 0 and do not ask questions.

ubiformat /dev/mtd0 -q -e 0   - format MTD device number 0, be quiet and force erase counter value 0.

ubiformat /dev/mtd8 -s 2048 -O 2048

ubiformat: mtd8 (nand), size 41943040 bytes (40.0 MiB), 320 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 319 -- 100 % complete
ubiformat: 320 eraseblocks have valid erase counter, mean value is 17
ubiformat: formatting eraseblock 319 -- 100 % complete
 

nanddump -p -c -s 0 -l 4096 /dev/mtd8
ECC failed: 0
ECC corrected: 0
Number of bad blocks: 0
Number of bbt blocks: 0
Block size 131072, page size 2048, OOB size 128
Dumping data starting at 0x00000000 and ending at 0x00002000...
0x00000000: 55 42 49 23 01 00 00 00 00 00 00 00 00 00 00 06          |UBI#............|
0x00000010: 00 00 10 00 00 00 20 00 45 b7 30 77 00 00 00 00          |...... .E.0w....|
0x00000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00          |................|
0x00000030: 00 00 00 00 00 00 00 00 00 00 00 00 6d 77 90 bc          |............mw..|
0x00000040: ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff    |................|
0x00000050: ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff    |................|
...................        all 0xff        ....................                                                     |................|
0x000007f0: ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff      |................|
0x00000800: 55 42 49 21 01 01 00 05 7f ff   ef ff   00 00 00 00           |UBI!............|
0x00000810: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00          |................|
0x00000820: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00          |................|
0x00000830: 00 00 00 00 00 00 00 00 00 00 00 00 b8 25 64 a8          |.............%d.|
0x00000840: ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff    |................|
...................        all 0xff        ....................                                                     |................|
0x00000fe0: ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff    |................|
0x00000ff0:  ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff   ff    |................|
 

ubiattach

作用

链接MTD设备(MTD设备描述原始Flash设备)到UBI并且创建相应的UBI设备。

attach通过ioctl系统调用,执行内核的UBI_IOCATT操作,内核中执行attach的主要函数是ubi_attach_mtd_dev。

详细操作:
1.获取mtd设备信息
2.读取每个块的header信息,包括EC header和VID header,检查一些参数
3.ubi_wl_init
4.ubi_eba_init

5. 创建一个ubi设备(struct ubi_device),设备节点为/dev/ubiN
6.创建磨损均衡线程

会创建/dev/ubiN节点 以及 /sys/class/ubi/ubiN目录
/sys/class/ubi/ubiN/下有以下文件,可以直接cat
avail_eraseblocks  eraseblock_size    mtd_num            subsystem
bad_peb_count      max_ec             power              total_eraseblocks
bgt_enabled        max_vol_count      reserved_for_bad   uevent
dev                min_io_size        ro_mode            volumes_count

格式

ubiattach [<UBI control device node file name>]

        [-m <MTD device number>]

        [-d <UBI device number>]

        [-p <path to device>]

        [--mtdn=<MTD device number>]

        [--devn=<UBI device number>]

        [--dev-path=<path to device>]

        [--max-beb-per1024=<maximum bad block number per 1024 blocks>]

UBI control device defaults to /dev/ubi_ctrl if not supplied.

参数

-d, --devn=<number>   

the number to assign to the newly created UBI device

(assigned automatically if this is not specified)   

-p, --dev-path=<path>

path to MTD device node to attach

-m, --mtdn=<number>

此选项是必须的   

MTD device number to attach (alternative method, e.g  if the character device node does not exist) 。    
注意:是从0开始的。                                         

-O, --vid-hdr-offset  

VID header offset (do not specify this unless you really know what you are doing,

the default should be optimal)  

-b, --max-beb-per1024

maximum expected bad block number per 1024 eraseblock.   
The default value is correct for most NAND devices.      

Allowed range is 0-768, 0 means the default kernel value.

-h, --help    

print help message   

-V, --version

print program version

示例

ubiattach -p /dev/mtd0 - attach /dev/mtd0 to UBI      //等价于  ubiattach -m 0

ubiattach -m 0              - attach MTD device 0 (mtd0) to UBI

ubiattach -m 0 -d 3      - attach MTD device 0 (mtd0) to UBI and create UBI device number 3 (ubi3)

ubiattach -m 1 -b 25     - attach /dev/mtd1 to UBI and reserve

                                       25*C/1024 eraseblocks for bad block handling, where C is the flash

                                       is total flash chip eraseblocks count, that is flash chip size in

                                        eraseblocks (including bad eraseblocks). E.g., if the flash chip

                                       has 4096 PEBs, 100 will be reserved.

实例:

ubiattach -m 8 -d 1

执行结果:

[  366.634630] ubi1: attaching mtd8
[  369.956645] ubi1: scanning is finished
[  370.071529] ubi1: attached mtd8 (name "app0", size 40 MiB)
[  370.080197] ubi1: PEB size: 131072 bytes (128 KiB), LEB size: 126976 bytes
[  370.090677] ubi1: min./max. I/O unit sizes: 2048/2048, sub-page size 2048
[  370.100384] ubi1: VID header offset: 2048 (aligned 2048), data offset: 4096
[  370.110694] ubi1: good PEBs: 320, bad PEBs: 0, corrupted PEBs: 0
[  370.119283] ubi1: user volume: 0, internal volumes: 1, max. volumes count: 128
[  370.129493] ubi1: max/mean erase counter: 15/13, WL threshold: 4096, image sequence number: 668018020
[  370.142359] ubi1: available PEBs: 296, total reserved PEBs: 24, PEBs reserved for bad PEB handling: 20
[  370.155725] ubi1: background thread "ubi_bgt1d" started, PID 279
UBI device number 1, total 320 LEBs (40632320 bytes, 38.8 MiB), available 296 LEBs (37584896 bytes, 35.8 MiB), LEB size 126976 bytes (124.0 KiB)

flash physical eraseblock size:从flash芯片手册中可以得到FLASH物理擦除块大小,或cat /proc/mtd

sub-page size:                       通过flash手册获得

logical eraseblock size:          对于没有子页的NAND FLASH来说:等于peb(物理擦除块大小)

                                                 对于有子页的NAND FLASH来说,等于“物理擦除块大小-1页的大小”

ubidetach

作用

ubiattach相反的操作,将MTD设备从UBI设备上去链接。

注意,在使用此命令之前,先要umount掉。

格式

ubidetach [<UBI control device node file name>]

        [-d <UBI device number>]

        [-m <MTD device number>]

        [-p <path to device>]

        [--devn=<UBI device number>]

        [--mtdn=<MTD device number>]

        [--dev-path=<path to device>]

UBI control device defaults to /dev/ubi_ctrl if not supplied.

参数

-d, --devn=<number>   

the number to assign to the newly created UBI device

(assigned automatically if this is not specified)   

-p, --dev-path=<path>

path to MTD device node to attach

-m, --mtdn=<number>   

MTD device number to attach (alternative method, e.g  

if the character device node does not exist)      
注意:是从0开始的。                                        

-O, --vid-hdr-offset  

VID header offset (do not specify this unless you really

know what you are doing, the default should be optimal)  

-b, --max-beb-per1024

maximum expected bad block number per 1024 eraseblock.   

The default value is correct for most NAND devices.      

Allowed range is 0-768, 0 means the default kernel value.

-h, --help    

print help message   

-V, --version

print program version

示例

ubidetach -p /dev/mtd0 - detach MTD device /dev/mtd0

ubidetach -d 2              - delete UBI device 2 (ubi2)

ubidetach -m 0             - detach MTD device 0 (mtd0)

ubimkvol

作用

从UBI设备上创建UBI卷。

详细操作:
1.检查各种参数名称等等
2.注册字符设备
3.修改volume table记录

        其实ubiattach要分两种情况:mtd设备已经有制作卷了,或者是没有制作卷。判断依据就是VIDheader里面的卷ID。
        如果是没有制作的,需要进行卷制作,也就是ubimkvlo。一个ubi设备可以有很多个volume,最多几个呢?要看volume table能有几个records。一个blocksize的大小能容纳多少volume table record就有多少个卷。 

格式

ubimkvol <UBI device node file name>

        [-h] [-a <alignment>] [-n <volume ID>] [-N <name>]

        [-s <bytes>] [-S <LEBs>] [-t <static|dynamic>] [-V] [-m]

        [--alignment=<alignment>][--vol_id=<volume ID>]
        [--name=<name>] [--size=<bytes>] [--lebs=<LEBs>]
        [--type=<static|dynamic>] [--help]

        [--version] [--maxavsize]

参数

-a, --alignment=<alignment>  

volume alignment (default is 1)                    

-n, --vol_id=<volume ID>                      

UBI volume ID, if not specified, the volume ID     

will be assigned automatically                     

-N, --name=<name>         

volume name                                        

-s, --size=<bytes>  

volume size volume size in bytes, kilobytes (KiB) or megabytes (MiB) 

-S, --lebs=<LEBs count> 

alternative way to give volume size in logical  eraseblocks 

-m, --maxavsize              

set volume size to maximum available size          

-t, --type=<static|dynamic>  

volume type (dynamic, static), default is dynamic  

-h, -?, --help               

print help message                                 

-V, --version                

print program version                              

示例

ubimkvol /dev/ubi0 -s 20MiB -N config_data      - create a 20 Megabytes volume named "config_data" on UBI device /dev/ubi0.

ubimkvol /dev/ubi1 -N app_pri -m

Set volume size to 37584896
Volume ID 0, size 296 LEBs (37584896 bytes, 35.8 MiB), LEB size 126976 bytes (124.0 KiB), dynamic, name "app_pri", alignment 1

ubirmvol

作用

从UBI设备上删除UBI卷

格式

 ubirmvol <UBI device node file name> [-n <volume id>]
                 [--vol_id=<volume id>] [-N <volume name>]
                 [--name=<volume name>] [-h] [--help]

参数

-a, --alignment=<alignment>

volume alignment (default is 1)

-m, --maxavsize

set volume size to maximum available size

-n, --vol_id=<volume id>

volume ID to remove  

-N, --name=<volume name>

volume name to remove

-h, -?, --help           

print help message   

-s, --size=<bytes>

volume size volume size in bytes, kilobytes (KiB) or megabytes (MiB)

-S, --lebs=<LEBs count>

alternative way to give volume size in logical eraseblocks

-t, --type=<static|dynamic>

volume type (dynamic, static), default is dynamic

-h, -?, --help

print help message

-V, --version            

print program version

示例

ubirmvol /dev/ubi0 -n 1              - remove UBI volume 1 from UBI device corresponding to /dev/ubi0

ubirmvol /dev/ubi0 -N my_vol     - remove UBI named "my_vol" from UBI device corresponding to    /dev/ubi0

ubinize

作用

制作UBI镜像

格式

ubinize [-o filename] [-p <bytes>] [-m <bytes>]
              [-s <bytes>]  [-O <num>]

              [-e <num>] [-x <num>] [-Q <num>] [-v] [-h] [-V]

              [--output=<filename>] [--peb-size=<bytes>]

              [--min-io-size=<bytes>] [--sub-page-size=<bytes>]

              [--vid-hdr-offset=<num>] [--erase-counter=<num>]

              [--ubi-ver=<num>] [--image-seq=<num>] [--verbose]

              [--help] [--version]

              ini-file

参数

-o, --output=<file name>   

output file name                              

-p, --peb-size=<bytes>     

             

size of the physical eraseblock of the flash this UBI image is created for in bytes,   kilobytes (KiB), or megabytes (MiB) (mandatory parameter) 

-m, --min-io-size=<bytes>

minimum input/output unit size of the flash in bytes      

-s, --sub-page-size=<bytes>

minimum input/output unit used for UBI headers, e.g. sub-page size in case of NAND flash (equivalent to the minimum input/output unit size by default)  

-O, --vid-hdr-offset=<num>

offset if the VID header from start of the physical eraseblock (default is the next  minimum I/O unit or sub-page after the EC header) 

-e, --erase-counter=<num>

the erase counter value to put to EC headers (default is 0)                                

-x, --ubi-ver=<num>               

UBI version number to put to EC headers (default is 1)                                

-Q, --image-seq=<num>      

32-bit UBI image sequence number to use  (by default a random number is picked)   

-v, --verbose              

be verbose                                    

-h, --help                 

print help message                            

-V, --version              

print program version                         

示例

ubinize -o ubi.img -p 16KiB -m 512 -s 256 cfg.ini

- create UBI image 'ubi.img' as described by configuration file 'cfg.ini'

ubiblock

作用

管理UBI卷上的block(创建/删除)

格式

ubiblock [-c,-r] <UBI volume node file name>

参数

-c, --create

create block on top of a volume

-r, --remove

remove block from volume       

-h, --help   

print help message             

-V, --version

print program version          

示例

ubiblock --create /dev/ubi0_0

ubirename

作用

更改ubi某个olume的名字,或者完成两个volume的互换

格式

ubirename <UBI device node file name> [<old name> <new name>|...]

示例

1. 将ubi2上名字为my_vol_a的volume名字改为my_vol_newname

      ubirename /dev/ubi2 my_vol_a my_vol_newname

2. 将ubi2上名字为my_vol_b和my_vol_c的两个volume互换,

       ubirename /dev/ubi2 my_vol_b my_vol_c my_vol_c my_vol_b

3. 把volume A的名字改为B,把volume C的名字改为D

       ubirename/dev/ubi0 A B C D

ubiupdatevol

作用

向UBI volume里边写数据

格式

ubiupdatevol <UBI volume node file name> [-t] [-s <size>] [-h] [-V]

[--truncate] [--size=<size>] [--help] [--version] <image file>

参数

-t, --truncate    

truncate volume (wipe it out)    

-s, --size=<bytes>

bytes to read from input         

--skip=<bytes>

leading bytes to skip from input

-h, --help        

print help message               

-V, --version     

print program version            

示例

1. write file "fs.img" to UBI volume /dev/ubi0_1

     ubiupdatevol /dev/ubi0_1 fs.img

2. wipe out UBI volume /dev/ubi0_1

     ubiupdatevol /dev/ubi0_1 -t

操作示例

第一次操作

ubiformat /dev/mtd8 -s 0x800 -O 0x800
ubiattach -m 8 -d 1 /dev/ubi_ctrl              //会出现/dev/ubi1节点
ubimkvol /dev/ubi1 -N app_pri -m           //会出现/dev/ubi1_0节点
mount -t ubifs /dev/ubi1_0 /mnt

//注意:ubiattach 生成/dev/ubiM和ubimkvol生成/dev/ubiM_N需要时间,要等待其生成节点之后再mount
示例等待命令(shell):
/usr/sbin/ubiattach -m 8 -d 1 /dev/ubi_ctrl
if [ $? == 0 ]; then
    loop=0
    while [ "$((loop++))" -lt "6" ]
    do
        echo "waiting for /dev/ubi1."
        if [ -e /dev/mtd1 ]; then
            echo "/dev/ubi1 appear"
            break
        fi
        /bin/sleep 1
    done
else
    echo "ubiattach -m 8 -d 1failed!"
    exit 1
fi

非第一次操作

// umount /dav
// ubidetach /dev/ubi_ctrl -d 1
ubiattach -m 8 -d 1 /dev/ubi_ctrl              //会出现/dev/ubi1节点
mount -t ubifs /dev/ubi1_0 /mnt

mount打印:

# mount -t ubifs /dev/ubi1_0 /mnt/
[   52.943452] UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 274
[   53.387663] UBIFS (ubi1:0): recovery needed
[   54.160605] UBIFS (ubi1:0): recovery completed
[   54.169441] UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "app_pri"
[   54.181077] UBIFS (ubi1:0): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[   54.195172] UBIFS (ubi1:0): FS size: 36315136 bytes (34 MiB, 286 LEBs), journal size 1777664 bytes (1 MiB, 14 LEBs)
[   54.209642] UBIFS (ubi1:0): reserved for root: 1715252 bytes (1675 KiB)
[   54.219050] UBIFS (ubi1:0): media format: w4/r0 (latest is w4/r0), UUID 0C991C90-92CF-4740-A810-7114851BC829, small LPT model
 


  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT利刃出鞘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值