1. 程式人生 > >uboot中訪問ubifs檔案系統內容

uboot中訪問ubifs檔案系統內容

有被問到在uboot中訪問ubi檔案系統,之前大概瞭解,但是沒有實際操作,這次還是操作一遍看看。

硬體平臺

  • 君正 x1000 halley2

uboot編譯配置

至少配置以下幾項,其他的驅動是必備的

#define CONFIG_MTD_DEVICE
#define CONFIG_CMD_UBI
#define CONFIG_CMD_UBIFS
#define CONFIG_CMD_MTDPARTS
#define CONFIG_MTD_PARTITIONS

uboot啟動

help之後的列印會看到:

mtdparts- define flash/nand partitions

ubi - ubi commands
ubifsload- load
file from an UBIFS filesystem ubifsls - list files in a directory ubifsmount- mount UBIFS volume ubifsumount- unmount UBIFS volume

uboot下使用ubi命令

首先使用mtdparts命令生成所有的mtd預設分割槽,

halley2-sfcnand# mtdparts default
halley2-sfcnand# mtdparts

device nand0 <nand>, # parts = 4
#: name size offset mask_flags
0: boot 0x00100000 0x00000000 0 1: kernel 0x00800000 0x00100000 0 2: rootfs 0x02800000 0x00900000 0 3: data 0x0cf00000 0x03100000 0 active partition: nand0,0 - (boot) 0x00100000 @ 0x00000000 defaults: mtdids : nand0=nand mtdparts: mtdparts=nand:1M(boot),8M(kernel),40M(rootfs),-(data)

使用ubi part命令連線mtd分割槽, 例如連結rootfs分割槽, 啟用當前分割槽會將已經啟用的分割槽取消連結

halley2-sfcnand# ubi part nand
halley2-sfcnand# ubi part
Device 0: nand0, partition rootfs

使用ubi info 檢視當前分割槽資訊, 注意其中的PEB代表可擦除的物理頁個數
每個物理頁大小128K

halley2-sfcnand# ubi info
UBI: MTD device name: "mtd=2"
UBI: MTD device size: 40 MiB
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 126976 bytes
UBI: number of good PEBs: 320
UBI: number of bad PEBs: 0
UBI: smallest flash I/O unit: 2048
UBI: VID header offset: 2048 (aligned 2048)
UBI: data offset: 4096
UBI: max. allowed volumes: 128
UBI: wear-leveling threshold: 4096
UBI: number of internal volumes: 1
UBI: number of user volumes: 2
UBI: available PEBs: 0
UBI: total number of reserved PEBs: 320
UBI: number of PEBs reserved for bad PEB handling: 3
UBI: max/mean erase counter: 5/1

檢視更詳細的資訊, 其中 test 卷標是使用ubi create test建立的。

halley2-sfcnand# ubi info l
UBI: volume information dump:
UBI: vol_id 0
UBI: reserved_pebs 266
UBI: alignment 1
UBI: data_pad 0
UBI: vol_type 3
UBI: name_len 6
UBI: usable_leb_size 126976
UBI: used_ebs 266
UBI: used_bytes 33775616
UBI: last_eb_bytes 126976
UBI: corrupted 0
UBI: upd_marker 0
UBI: name rootfs

UBI: volume information dump:
UBI: vol_id 1
UBI: reserved_pebs 47
UBI: alignment 1
UBI: data_pad 0
UBI: vol_type 3
UBI: name_len 4
UBI: usable_leb_size 126976
UBI: used_ebs 47
UBI: used_bytes 5967872
UBI: last_eb_bytes 126976
UBI: corrupted 0
UBI: upd_marker 0
UBI: name test

UBI: volume information dump:
UBI: vol_id 2147479551
UBI: reserved_pebs 2
UBI: alignment 1
UBI: data_pad 0
UBI: vol_type 3
UBI: name_len 13
UBI: usable_leb_size 126976
UBI: used_ebs 2
UBI: used_bytes 253952
UBI: last_eb_bytes 2
UBI: corrupted 0
UBI: upd_marker 0
UBI: name layout volume

ubi info Display volume and ubi layout information
ubi createvol Create UBI volume on UBI device
ubi removevol Remove UBI volume from UBI device
ubi read Read data from UBI volume to memory
ubi write Write data from memory to UBI volume

uboot ubifs訪問裝置

ubifsmount
ubifsumount
ubifsls
ubifsload

halley2-sfcnand# help ubifsmount
ubifsmount - mount UBIFS volume

Usage:
ubifsmount <volume-name>
- mount 'volume-name' volume
halley2-sfcnand# help ubifsumount
ubifsumount - unmount UBIFS volume

Usage:
ubifsumount - unmount current volume
halley2-sfcnand# help ubifsload
ubifsload - load file from an UBIFS filesystem

Usage:
ubifsload <addr> <filename> [bytes]
- load file 'filename' to address 'addr'
halley2-sfcnand# help ubifsls
ubifsls - list files in a directory

Usage:
ubifsls [directory]
- list files in a 'directory' (default '/')

要訪問ubifs檔案系統的內容, 首先需要執行ubifsmount操作掛在分割槽:volume, 從上一節中可以直到rootfs檔案掛在到了ubi0, 有卷標rootfs, test, layout volume, 其中test是隻建立了卷標,並沒有資料,所以無法掛在,layout volume是根卷標,沒有內容。 只有rootfs有資料,可以掛載, 如下:

  • 當我們執行, 以下命令時,會看到ubi分割槽為ubi0。
halley2-sfcnand# ubi part rootfs
UBI: mtd1 is detached from ubi0
UBI: attaching mtd1 to ubi0
  • 掛載ubi0:rootfs如下
halley2-sfcnand# ubifsmount ubi0:rootfs
UBIFS: mounted UBI device 0, volume 0, name "rootfs"
UBIFS: mounted read-only
UBIFS: file system size: 32378880 bytes (31620 KiB, 30 MiB, 255 LEBs)
UBIFS: journal size: 9023488 bytes (8812 KiB, 8 MiB, 72 LEBs)
UBIFS: media format: w4/r0 (latest is w4/r0)
UBIFS: default compressor: LZO
UBIFS: reserved for root: 0 bytes (0 KiB)
  • 檢視檔案系統內容:
halley2-sfcnand# ubifsls
<DIR> 4576 Wed Jan 03 06:43:17 2018 bin
<DIR> 224 Fri Sep 08 04:17:51 2017 dev
<DIR> 2224 Wed Jan 03 06:43:18 2018 etc
<DIR> 2960 Wed Jan 03 06:43:18 2018 lib
<DIR> 232 Fri Sep 08 04:17:51 2017 mnt
<DIR> 232 Fri Sep 08 04:17:51 2017 opt
<DIR> 304 Wed Jan 03 06:43:18 2018 tmp
<DIR> 232 Fri Sep 08 04:17:51 2017 sys
<DIR> 608 Fri Sep 22 09:34:23 2017 var
<DIR> 608 Wed Jan 03 06:43:18 2018 usr
<DIR> 232 Fri Sep 08 04:17:51 2017 proc
<DIR> 4000 Wed Jan 03 06:43:27 2018 sbin
<DIR> 232 Fri Sep 22 09:34:23 2017 root
<LNK> 11 Fri Sep 08 04:17:51 2017 linuxrc
<LNK> 3 Fri Sep 08 04:17:51 2017 lib32
<DIR> 232 Fri Sep 08 04:17:51 2017 media
<DIR> 600 Wed Jan 03 06:43:58 2018 testsuite
<DIR> 760 Wed Jan 03 06:43:18 2018 firmware
  • 載入檔案到記憶體:
halley2-sfcnand# ubifsload 0x80600000 etc/fstab 1024
Loading file 'etc/fstab' to addr 0x80600000 with size 405 (0x00000195)...
Done

halley2-sfcnand# md 0x80600000
80600000: 652f2023 662f6374 62617473 7473203a # /etc/fstab: st
80600010: 63697461 6c696620 79732065 6d657473 atic file system
80600020: 666e6920 616d726f 6e6f6974 0a230a2e information..#.
80600030: 663c2023 20656c69 74737973 203e6d65 # <file system>
80600040: 756f6d3c 7020746e 20203e74 3c202020 <mount pt> <
80600050: 65707974 6f3c093e 6f697470 203e736e type>.<options>
80600060: 20202020 20202020 6d75643c 3c203e70 <dump> <
80600070: 73736170 642f0a3e 722f7665 20746f6f pass>./dev/root
80600080: 20202020 202f2020 20202020 20202020 /
80600090: 20202020 74786520 77720932 616f6e2c ext2.rw,noa
806000a0: 206f7475 20202020 20202020 20202030 uto 0
806000b0: 31202020 6f72700a 09090963 6f72702f 1.proc.../pro
806000c0: 20200963 20202020 6f727020 20202063 c. proc
806000d0: 65642020 6c756166 20097374 20093020 defaults. 0.
806000e0: 79730a30 09736673 732f0909 20097379 0.sysfs.../sys.
806000f0: 20202020 79732020 20736673 64202020 sysfs d

總結

  1. mtd預設分割槽建立
  2. ubi part 命令連線mtd到ubi0上
  3. ubi info [l] 可以檢視ubi分割槽(卷標)資訊
  4. ubi read/write 通過ubi管理層讀寫nand
  5. ubifsxx 操作簇執行掛載,顯示,讀取等操作。配合mm,md等命令,進行記憶體資料修改,檢視等。。
  6. uboot中ubifs不支援寫檔案操作,後來看了下ubifs的寫檔案還是挺複雜的。。。。