2018年9月9日 星期日

Mount UBI with nandsim


UBI/UBIFS無法使用loop-back方式mount, 不過我們可以使用NAND simulator將UBI燒入到nandsim中.
root@vista:~# mkfs.ubifs -m 2KiB -e 129024 -c 2048 -r ubifs-root -x zlib ubifs.img
root@vista:~# cat ubi.ini
[ubi_rfs]
mode=ubi
image=ubifs.img
vol_id=0
vol_type=dynamic
vol_name=ubi_rfs
vol_alignment=1
vol_flags=autoresize

root@vista:~# ubinize -o my.ubi -p 128KiB -m 2KiB -O 512 ubi.ini
ubinize: volume size was not specified in section "ubi_rfs", assume minimum to fit image "ubifs.img"1806336 bytes (1.7 MiB)
root@vista:~# modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa third_id_byte=0x00 fourth_id_byte=0x15
root@vista:~# ubiformat /dev/mtd0 -f my.ubi
ubiformat: mtd0 (nand), size 268435456 bytes (256.0 MiB), 2048 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 2047 -- 100 % complete
ubiformat: 2048 eraseblocks have valid erase counter, mean value is 3
ubiformat: flashing eraseblock 15 -- 100 % complete
ubiformat: formatting eraseblock 2047 -- 100 % complete
ubiformat: formatting eraseblock 2047 -- 100 % complete
root@vista:~# ubiattach -m 0
UBI device number 0, total 2048 LEBs (264241152 bytes, 252.0 MiB), available 0 LEBs (0 bytes), LEB size 129024 bytes (126.0 KiB)
root@vista:~# ubinfo -a
UBI version:                    1
Count of UBI devices:           1
UBI control device major/minor: 10:55
Present UBI devices:            ubi0

ubi0
Volumes count:                           1
Logical eraseblock size:                 129024 bytes, 126.0 KiB
Total amount of logical eraseblocks:     2048 (264241152 bytes, 252.0 MiB)
Amount of available logical eraseblocks: 0 (0 bytes)
Maximum count of volumes                 128
Count of bad physical eraseblocks:       0
Count of reserved physical eraseblocks:  40
Current maximum erase counter value:     9
Minimum input/output unit size:          2048 bytes
Character device major/minor:            245:0
Present volumes:                         0

Volume ID:   0 (on ubi0)
Type:        dynamic
Alignment:   1
Size:        2002 LEBs (258306048 bytes, 246.3 MiB)
State:       OK
Name:        ubi_rfs
Character device major/minor: 245:1

root@vista:~# mount -t ubifs /dev/ubi0_0 www

Usage: mkfs.ubifs [OPTIONS] target
Make a UBIFS file system image from an existing directory tree
Options:
-r, -d, --root=DIR       build file system from directory DIR
-m, --min-io-size=SIZE   minimum I/O unit size
-e, --leb-size=SIZE      logical erase block size
-c, --max-leb-cnt=COUNT  maximum logical erase block count
-x, --compr=TYPE         compression type - "lzo", "favor_lzo", "zlib" or
                         "none" (default: "lzo")

# mkfs.ubifs -F -r </path/to/your/rootfs/tree> -m <min io size> -e <LEB size> -c 
        <Erase Blocks count> -o </path/to/output/Image.ubifs>
<min io size> Equals the page-size of the used NAND-Flash
<LEB size> Logical Erase Block size
<Erase Blocks count> maximum logical erase block count
mkfs.ubifs -m 2KiB -e 129024 -c 2048 -r ubifs-root -x zlib ubifs.img這些相關參數是先用ubiformat確認的, 不過-e是在mount failed的時候看dmesg的


Usage: ubinize [-o filename] [-p ] [-m ] [-s ] [-O ] [-e ]
                [-x ] [-Q ] [-v] [-h] [-V] [--output=] [--peb-size=]
                [--min-io-size=] [--sub-page-size=] [--vid-hdr-offset=]
                [--erase-counter=] [--ubi-ver=] [--image-seq=] [--verbose] [--help]
                [--version] ini-file
-p, --peb-size=       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=    minimum input/output unit size of the flash
                             in bytes
-O, --vid-hdr-offset=   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)

# ubinize -vv -o <output image> -m <min io size> -p <PEB size>KiB 
        -s <subpage-size> -O <VID-hdr-offset> <configuration file>
<min io size> Equals the page-size of the used NAND-Flash
<PEB size> Physical Erase Block size (in KiB) - Equals the block size of
        the NAND-Flash
<Erase Blocks count> Count of the available Erase Blocks
<subpage-size> Subpage size in bytes. Default value is the minimum 
        input/output size (page-size)
<VID-hdr-offset> offset if the VID header from start of the PEB 
        (default is the next min I/O unit or sub-page after the EC header)
<configuration file> Path to a configuration file.

ubinize -o my.ubi -p 128KiB -m 2KiB -O 512 ubi.ini這些參數也是在ubiformat修正的, 如-O 512


    參考資料:
  • http://www.linux-mtd.infradead.org/faq/ubifs.html#L_loop_mount, Can UBIFS mount loop-back devices?
  • https://elinux.org/UBIFS, UBIFS
  • http://www.linux-mtd.infradead.org/faq/nand.html#L_nand_nandsim, How do I use NAND simulator?




3 則留言:

  1. http://www.linux-mtd.infradead.org/faq/nand.html#L_nand_nandsim

    modprobe nandsim first_id_byte=0x20 second_id_byte=0x33 - 16MiB, 512 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0x35 - 32MiB, 512 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0x36 - 64MiB, 512 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0x78 - 128MiB, 512 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0x71 - 256MiB, 512 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0xa2 third_id_byte=0x00 fourth_id_byte=0x15 - 64MiB, 2048 bytes page;
    modprobe nandsim first_id_byte=0xec second_id_byte=0xa1 third_id_byte=0x00 fourth_id_byte=0x15 - 128MiB, 2048 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa third_id_byte=0x00 fourth_id_byte=0x15 - 256MiB, 2048 bytes page;
    modprobe nandsim first_id_byte=0x20 second_id_byte=0xac third_id_byte=0x00 fourth_id_byte=0x15 - 512MiB, 2048 bytes page;
    modprobe nandsim first_id_byte=0xec second_id_byte=0xd3 third_id_byte=0x51 fourth_id_byte=0x95 - 1GiB, 2048 bytes page;

    回覆刪除
  2. http://www.linux-mtd.infradead.org/doc/ubi.html#L_subpage
    UBI utilizes sub-pages to lessen flash space overhead. The overhead is less if NAND flash supports sub-pages (see here). Indeed, let's consider a NAND flash with 128KiB eraseblocks and 2048-byte pages. If it does not have sub-pages, UBI puts the the VID header at physical offset 2048, so LEB size becomes 124KiB (128KiB minus one NAND page which stores the EC header and minus another NAND page which stores the VID header. In opposite, if the NAND flash does have sub-pages, UBI puts the VID header at physical offset 512 (the second sub-page), so LEB size becomes 126KiB (128KiB minus one NAND page which is used for storing both UBI headers). See this section for more information about where the UBI headers are stored.

    So, mkfs.ubifs 的 -e 129024 是eraseblock(128K) - pagesize(2k) = 126K=129024

    回覆刪除
  3. http://www.linux-mtd.infradead.org/faq/ubifs.html#L_loop_mount

    Unfortunately not, because loop-back devices are block devices (backed by regular files), while UBIFS works on top of UBI devices (see here).

    However, there is an unusual way to make UBIFS work with a file-backed image using NAND simulator (see here). If your image is not very big, then you can create a RAM-backed nandsim MTD device, then copy your image to that emulated MTD device. If the image is large and you do not have that much RAM, you can create a file-backed nandsim MTD device using the cache_file nandsim module option. Below is an example:

    # Create a 1GiB emulated MTD device backed by regular file "my_image"
    $ modprobe nandsim cache_file=my_image first_id_byte=0xec second_id_byte=0xd3 \
    third_id_byte=0x51 fourth_id_byte=0x95
    See here for more instructions about using UBIFS with nandsim.

    回覆刪除

熱門文章