ubi 命令
ubinfo // (a tool to print UBI information.) 查資訊
ubidetach // (tool to remove UBI devices (detach MTD devices from UBI)) 卸載
ubiattach // (a tool to attach MTD device to UBI.) 加載
ubimkvol // (a tool to create UBI volumes.) 建立卷
ubirmvol // (a tool to remove UBI volumes.) 刪除卷
ubirsvol // (a tool to resize UBI volumes.) 變更卷的大小
ubiformat // (a tool to format MTD devices and flash UBI images) 重建整個ubi
ubiblock // (a tool to create/remove block device interface from UBI volumes.)
ubiupdatevol // (a tool to write data to UBI volumes.) 更新卷
ubinize // (a tool to generate UBI images.) 製作 ubi 分區檔案
ubicrc32 //
ubirename // 變更卷的名稱
掛載 ubifs
mount -t ubifs ubi0_0 /mnt/ubi // ubi0_0, 第一個0是第幾個加載的ubi流水號, 第二個0是建立ubi檔案時的vol_id
mount -t ubifs ubi0:rootfs /mnt/ubi // ubi0:rootfs, 0是第幾個加載的ubi流水號, rootfs是建立ubi檔案時的vol_name
mtd 在 kernel 的訊息範例
[ 2.893251] nand: device found, Manufacturer ID: 0xc2, Chip ID: 0xaa
[ 2.896277] nand: ONFI 10-Compliant Macronix MX30UF2G18AC
[ 2.902682] nand: 256 MiB, SLC, erase size: 128 KiB, page size: 2048, OOB size: 64
[ 2.915469] 16 cmdlinepart partitions found on MTD device qcom_nand.0
[ 2.918829] Creating 16 MTD partitions on "qcom_nand.0":
[ 2.925254] 0x000000000000-0x000000180000 : "0:SBL1"
[ 2.932702] 0x000000180000-0x000000280000 : "0:MIBIB"
[ 2.937205] 0x000000280000-0x000000300000 : "0:BOOTCONFIG"
[ 2.941754] 0x000000300000-0x000000380000 : "0:BOOTCONFIG1"
[ 2.947094] 0x000000380000-0x000000700000 : "0:QSEE"
[ 2.954870] 0x000000700000-0x000000780000 : "0:DEVCFG"
[ 2.957800] 0x000000780000-0x000000800000 : "0:RPM"
[ 2.962746] 0x000000800000-0x000000880000 : "0:CDT"
[ 2.967500] 0x000000880000-0x000000900000 : "0:APPSBLENV"
[ 2.972404] 0x000000900000-0x000000a80000 : "0:APPSBL"
[ 2.978693] 0x000000a80000-0x000000b00000 : "0:ART"
[ 2.982961] 0x000000b00000-0x000000c00000 : "hwconfig"
[ 2.988119] 0x000000c00000-0x000000d00000 : "swconfig"
[ 2.993344] 0x000000d00000-0x000007700000 : "rootfs"
[ 3.077346] mtd: device 13 (rootfs) set to be root filesystem
[ 3.077635] mtdsplit: no squashfs found in "rootfs"
[ 3.082091] 0x000007700000-0x000007780000 : "0:ETHPHYFW"
[ 3.087948] 0x000007780000-0x00000b780000 : "image"
使用的 Image.cfg
[kernel]
# Volume mode (other option is static)
mode=ubi
# Source image
image=arch/arm64/boot/Image.itb
vol_size=10MiB
# Volume ID in UBI image
vol_id=0
# Allow for dynamic resize
vol_type=dynamic
# Volume name
vol_name=kernel
[rootfs]
# Volume mode (other option is static)
mode=ubi
# Source image
image=rootfs.squashfs
# Volume ID in UBI image
vol_id=1
# Allow for dynamic resize
vol_type=dynamic
# Volume name
vol_name=ubi_rootfs
[config_data]
mode=ubi
# this is arbitrary, since autoresize below will use up the remaining
# free space on the mtd volume
vol_size=1KiB
vol_id=2
vol_type=dynamic
vol_name=config_data
vol_flags=autoresize
/proc/mtd 的內容
# cat /proc/mtd
dev: size erasesize name
mtd0: 00180000 00020000 "0:SBL1"
mtd1: 00100000 00020000 "0:MIBIB"
mtd2: 00080000 00020000 "0:BOOTCONFIG"
mtd3: 00080000 00020000 "0:BOOTCONFIG1"
mtd4: 00380000 00020000 "0:QSEE"
mtd5: 00080000 00020000 "0:DEVCFG"
mtd6: 00080000 00020000 "0:RPM"
mtd7: 00080000 00020000 "0:CDT"
mtd8: 00080000 00020000 "0:APPSBLENV"
mtd9: 00180000 00020000 "0:APPSBL"
mtd10: 00080000 00020000 "0:ART"
mtd11: 00100000 00020000 "hwconfig"
mtd12: 00100000 00020000 "swconfig"
mtd13: 06a00000 00020000 "rootfs"
mtd14: 00080000 00020000 "0:ETHPHYFW"
mtd15: 04000000 00020000 "image"
mtd16: 00a0d000 0001f000 "kernel"
mtd17: 03013000 0001f000 "ubi_rootfs"
mtd18: 0273c000 0001f000 "config_data"
mtd19: 0386e000 0001f000 "rootfs_data"
手動重建ubi範例
ubidetach -f -p /dev/mtd15 // 卸載ubi
ubiformat /dev/mtd15 -y // 格式化ubi
ubiattach -p /dev/mtd15 // 加載ubi
ubimkvol /dev/ubi1 -m -N rootfs_data // 建立ubi卷
mount -t ubifs ubi1:rootfs_data /mnt/data // mount ubifs
更新ubi卷 (需要先 umount)
ubiupdatevol /dev/ubi1_0 /tmp/image.data // 把 /tmp/image.data 寫入到第2個加載的ubi且voi_id為0的卷
ubiupdatevol /dev/ubi1_0 -t // 清除卷
更新整個ubi
ubiformat /dev/mtd15 -y -f /tmp/image.ubi // 把 /tmp/image.ubi 寫入到 mtd15
ubinfo 範例
# ubinfo -d 0
ubi0
Volumes count: 3
Logical eraseblock size: 126976 bytes, 124.0 KiB
Total amount of logical eraseblocks: 848 (107675648 bytes, 102.7 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: 38
Current maximum erase counter value: 2
Minimum input/output unit size: 2048 bytes
Character device major/minor: 242:0
Present volumes: 0, 1, 2
# ubinfo -d 0 -n 0
Volume ID: 0 (on ubi0)
Type: dynamic
Alignment: 1
Size: 83 LEBs (10539008 bytes, 10.1 MiB)
State: OK
Name: kernel
Character device major/minor: 242:1
# ubinfo -d 0 -n 1
Volume ID: 1 (on ubi0)
Type: dynamic
Alignment: 1
Size: 397 LEBs (50409472 bytes, 48.1 MiB)
State: OK
Name: ubi_rootfs
Character device major/minor: 242:2
# ubinfo -d 0 -n 2
Volume ID: 2 (on ubi0)
Type: dynamic
Alignment: 1
Size: 324 LEBs (41140224 bytes, 39.2 MiB)
State: OK
Name: config_data
Character device major/minor: 242:3
※ 如果 u-boot 有自己mtd的切法,不要試著在 kernel 裡改變它。
否則當你使用 ubiupdatevol 後,會無法開機。
附件一: Image.itb 的製作方法
// ipq6018-cp01-c3.its 檔案內容
/dts-v1/;
/ {
description = "ARM64 OpenWrt FIT (Flattened Image Tree)";
#address-cells = <1>;
images {
kernel@1 {
description = "ARM64 OpenWrt Linux-4.4.60";
data = /incbin/("Image.gz");
type = "kernel";
arch = "arm64";
os = "linux";
compression = "gzip";
load = <0x41080000>;
entry = <0x41080000>;
hash@1 {
algo = "crc32";
};
hash@2 {
algo = "sha1";
};
};
fdt@1 {
description = "ARM64 OpenWrt qcom-ipq6018-cp01-c3 device tree blob";
data = /incbin/("dts/qcom/qcom-ipq6018-cp01-c3.dtb");
type = "flat_dt";
arch = "arm64";
compression = "none";
hash@1 {
algo = "crc32";
};
hash@2 {
algo = "sha1";
};
};
};
configurations {
default = "config@cp01-c3";
config@cp01-c3 {
description = "OpenWrt";
kernel = "kernel@1";
fdt = "fdt@1";
};
};
};
mkimage -f ipq6018-cp01-c3.its Image.itb
(要先有正確的dtb、kernel 編譯出 Image.gz)
64位元的位址是:0x41080000
32位元的位址是:0x41008000
// dts & dtb 互轉
dtc -I dts -O dtb -o output.dtb ipq6018-cp01-c3.dts // dts -> dtb
dtc -I dtb -O dts -o output.dts ipq6018-cp01-c3.dtb // dtb -> dts
dumpimage -l Image.itb // 列出列表
dumpimage Image.itb -T flat_dt -p 0 -o output.1 // 從 .itb 匯出列表第一個檔案 (通常是 kernel)
dumpimage Image.itb -T flat_dt -p 1 -o output.2 // 從 .itb 匯出列表第二個檔案 (通常是 dtb)
附件二: rootfs.squashfs 的製作方法
mksquashfs4 src_dir dst_file -nopad -noappend -root-owned -comp xz -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2 -b 256k -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1' -processors 1
padjffs2 dst_file 4 8 16 64 128 256
dd if=dst_file of=root.squashfs bs=2k conv=sync
讀取 src_dir 目錄,建立 root.squashfs (暫存檔案為 dst_file)
附件三:image.ubi 的製作方法
ubinize -m 2048 -p 128KiB -o Image.ubi Image.cfg
附件四:image.data 的製作方法
mkfs.ubifs -x lzo -m 2048 -e 126976 -c 466 -r ./src_dir -o dst_file
-e 的值可查詢 ubinfo -d 0 的 "Logical eraseblock size"
-c 的值可查詢 ubinfo -d 0 -n 0 的 "Size"
註:增加 -F 參數,可解決 ECC ERROR 的問題 (kernel 需要 3.0 以上)。