X系列通用Uboot下读写ubi 方法

U-Boot 下读写 UBI 方法

1. 依赖的环境变量

U-Boot 的 mtdparts 命令依赖两个环境变量:

1. mtdids:U-Boot 设备名 <-> Linux MTD 名的映射

2. mtdparts:分区表定义

注意:

• mtdids=nand0:nand 不是这里使用的格式

• mtdparts=mtdparts=nand:... 可用

2. 操作步骤

2.1 修正 mtdids

setenv mtdids nand0=nand
printenv mtdids
# 期望看到:mtdids=nand0=nand

2.2 设置 mtdparts(你的板子分区)

setenv mtdparts 'mtdparts=nand:1M(boot),8M(kernel),40M(rootfs),-(data)'
printenv mtdparts
# 期望看到:mtdparts=mtdparts=nand:1M(boot),8M(kernel),40M(rootfs),-(data)

2.3 解析并展示分区表

mtdparts

预期:看到 nand0 下 4 个分区:boot / kernel / rootfs / data。

2.4 清空 data 分区并初始化为 UBI

危险:会擦除 data 分区

nand erase.part data
ubi part data
ubi info

2.5 创建卷并做读写验证

ubi create datavol 0x1400000 d
ubi info l
ubi write 0x82000000 datavol 0x100000
ubi read  0x83000000 datavol 0x100000
md.b 0x83000000 0x40

2.6 持久化(确认没问题后再保存)

saveenv

3. 快速自测(固定字节)

# 往 0x82000000 填充 0x5A,共 0x1000 字节
mw.b 0x82000000 0x5A 0x1000

# 写入卷
ubi write 0x82000000 datavol 0x1000

# 读回到 0x83000000
ubi read 0x83000000 datavol 0x1000

# 检查前 64 字节应基本都是 5A
md.b 0x83000000 0x40

4. 实测日志

  x2000_halley5# printenv mtdids
  mtdids=nand0:nand
  x2000_halley5# printenv mtdparts
  mtdparts=mtdparts=nand:1M(boot),8M(kernel),40M(rootfs),-(data)
  x2000_halley5#
  setenv mtdids nand0=nand
  x2000_halley5# setenv mtdparts 'mtdparts=nand:1M(boot),8M(kernel),40M(rootfs),-(data)'
  x2000_halley5# 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                0x04f00000      0x03100000      0

  active partition: nand0,0 - (boot) 0x00100000 @ 0x00000000

  defaults:
  mtdids  : nand0:nand
  mtdparts: mtdparts=nand:1M(boot),8M(kernel),40M(rootfs),-(data)
  x2000_halley5#  nand erase.part data

  NAND erase.part: device 0 offset 0x3100000, size 0x4f00000
  Erasing at 0x7fe0000 -- 100% complete.
  OK
  x2000_halley5#
  x2000_halley5# ubi part data
  UBI: attaching mtd1 to ubi0
  UBI: physical eraseblock size:   131072 bytes (128 KiB)
  UBI: logical eraseblock size:    126976 bytes
  UBI: smallest flash I/O unit:    2048
  UBI: VID header offset:          2048 (aligned 2048)
  UBI: data offset:                4096
  UBI: empty MTD device detected
  UBI: create volume table (copy #1)
  UBI: create volume table (copy #2)
  UBI: attached mtd1 to ubi0
  UBI: MTD device name:            "mtd=3"
  UBI: MTD device size:            79 MiB
  UBI: number of good PEBs:        632
  UBI: number of bad PEBs:         0
  UBI: max. allowed volumes:       128
  UBI: wear-leveling threshold:    4096
  UBI: number of internal volumes: 1
  UBI: number of user volumes:     0
  UBI: available PEBs:             616
  UBI: total number of reserved PEBs: 16
  UBI: number of PEBs reserved for bad PEB handling: 12
  UBI: max/mean erase counter: 1/0
  x2000_halley5# ubi info
  UBI: MTD device name:            "mtd=3"
  UBI: MTD device size:            79 MiB
  UBI: physical eraseblock size:   131072 bytes (128 KiB)
  UBI: logical eraseblock size:    126976 bytes
  UBI: number of good PEBs:        632
  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:     0
  UBI: available PEBs:             616
  UBI: total number of reserved PEBs: 16
  UBI: number of PEBs reserved for bad PEB handling: 12
  UBI: max/mean erase counter: 1/0
  x2000_halley5#
  x2000_halley5#  ubi create datavol 0x1400000 d
  Creating dynamic volume datavol of size 20971520
  x2000_halley5# ubi info l
  UBI: volume information dump:
  UBI: vol_id          0
  UBI: reserved_pebs   166
  UBI: alignment       1
  UBI: data_pad        0
  UBI: vol_type        3
  UBI: name_len        7
  UBI: usable_leb_size 126976
  UBI: used_ebs        166
  UBI: used_bytes      21078016
  UBI: last_eb_bytes   126976
  UBI: corrupted       0
  UBI: upd_marker      0
  UBI: name            datavol

  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

  x2000_halley5# ubi write 0x82000000 datavol 0x100000
  1048576 bytes written to volume datavol
  x2000_halley5# ubi read 0x83000000 datavol 0x100000
  Read 1048576 bytes from volume datavol to 83000000
  x2000_halley5# md.b 0x83000000 0x40
  83000000: 55 42 49 23 01 00 00 00 00 00 00 00 00 00 00 04    UBI#............
  83000010: 00 00 08 00 00 00 10 00 00 00 00 00 00 00 00 00    ................
  83000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
  83000030: 00 00 00 00 00 00 00 00 00 00 00 00 3f 05 d0 a6    ............?...

x2000_halley5# ubi write 0x82000000 datavol 0x100000
1048576 bytes written to volume datavol
x2000_halley5# ubi read 0x83000000 datavol 0x100000
Read 1048576 bytes from volume datavol to 83000000
x2000_halley5# md.b 0x83000000 0x40
83000000: 55 42 49 23 01 00 00 00 00 00 00 00 00 00 00 04    UBI#............
83000010: 00 00 08 00 00 00 10 00 00 00 00 00 00 00 00 00    ................
83000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
83000030: 00 00 00 00 00 00 00 00 00 00 00 00 3f 05 d0 a6    ............?...
x2000_halley5#
x2000_halley5# mw.b 0x82000000 0x5A 0x1000
x2000_halley5# ubi write 0x82000000 datavol 0x1000
4096 bytes written to volume datavol
x2000_halley5#  ubi read 0x83000000 datavol 0x1000
Read 4096 bytes from volume datavol to 83000000
x2000_halley5# md.b 0x83000000 0x40
83000000: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a    ZZZZZZZZZZZZZZZZ
83000010: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a    ZZZZZZZZZZZZZZZZ
83000020: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a    ZZZZZZZZZZZZZZZZ
83000030: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a    ZZZZZZZZZZZZZZZZ
x2000_halley5#


评论交流 (共 0 条)