摘自:http://blog.chinaunix.net/uid-30016330-id-4611629.html
1.制作ubi.fs的脚本
echo "Step1: mkfs.ubifs"
#echo "sub-page support";mkfs.ubifs -F -r arago-base-tisdk-image-am335x-evm/ -m 2048 -e 129024 -c 512 -o rootfs.ubi
echo "no sub-page";mkfs.ubifs -F -r arago-base-tisdk-image-am335x-evm/ -m 2048 -e 126976 -c 512 -o rootfs.ubi
echo "Step2: ubinize"
ubinize -vv -o ubi.img -m 2048 -p 128KiB ubinize.cfg
#ubinize -vv -o ubi.img -m 2048 -p 128KiB -s 512 -O 2048 ubinize.cfg
echo "Step3: cp tftproot, rm ..."
cp ubi.img ~/tftproot/
rm ubi.img rootfs.ubi
echo "End!"
[ubifs]
mode=ubi
image=rootfs.ubi
vol_id=5
vol_size=60MiB
vol_type=dynamic
vol_name=rootfs
vol_alignment=1
vol_flags=autoresize
chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);//-QWB140514-add
chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);//-QWB140514-add
chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);//-QWB140514-add
chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);//-QWB140514-add
if (byte_pos < BCH8_ECC_BYTES) //-QWB140514-add
Creating 1 MTD partitions on "nand0":
0x000000500000-0x000004500000 : "mtd=1"
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: sub-page size: 512
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=1"
UBI: MTD device size: 64 MiB
UBI: number of good PEBs: 512
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: 503
UBI: total number of reserved PEBs: 9
UBI: number of PEBs reserved for bad PEB handling: 5
UBI: max/mean erase counter: 1/0
U-Boot#
[rootfs_volume] mode=ubi image=rootfs.ubifs vol_id=1 vol_type=static vol_name=rootfs vol_alignment=1 [rwdata_volume] mode=ubi image=data.ubifs vol_id=2 vol_type=dynamic vol_name=data vol_alignment=1 vol_flags=autoresize
# ubinize -vv -o -m -p KiB
$make
- the eraseblocks (PEB) ;
- the pages (or “minimum input/output size”) ;
- the subpages (it may be the same as the min i/o size) ;
- 逻辑eraseblocks=PEB-(int((Subpage_size+Page_size)/Page_size))*Page_size:128*1024-(2048+2048)/2048*2048=126976
- 其中 subpage+page truncated to page size
# mkfs.ubifs -r -m -e -c -o
u-boot#> setenv bootargs 'console=ttyO0,115200n8 root=ubi0:rootfs rw ubi.mtd=,2048 noinitrd rootfstype=ubifs mem=256M rootwait=1'
The value of PARTITION_ID depends on MTD device which holds the root filesystem. The below example assumes UBIFS file system is flashed on MTD partition 7.
One may instead pass in the name of the partition as set in the partition table for the device.
u-boot> setenv bootargs 'console=ttyO0,115200n8 root=ubi0:rootfs rw ubi.mtd=7,2048 noinitrd rootfstype=ubifs mem=256M rootwait=1'
How to enable UBI/UBIFS support in Linux Kernel ?
To enable UBIFS support, start the Linux Kernel Configuration tool:
- Enabling UBI support on MTD devices.
Device Drivers ---> Memory Technology Device (MTD) support ---> Enable UBI - Unsorted block images --->
- Enabling UBIFS file-system support.
File systems ---> Miscellaneous filesystems ---> UBIFS file system support
Note: Above configurations are enabled default in omap2plus_defconfig
How to enable UBI/UBIFS support in u-boot ?
The following macros need to be defined in the board config for enabling UBI/UBIFS in uboot
#define CONFIG_CMD_NAND #define CONFIG_CMD_UBI #define CONFIG_CMD_UBIFS #define CONFIG_RBTREE #define CONFIG_MTD_DEVICE #define CONFIG_MTD_PARTITIONS #define CONFIG_CMD_MTDPARTS #define CONFIG_LZO
Note: Details of UBI command available in u-boot are given in $U-BOOT/doc/README.ubi
U-Boot# nand erase 0x00000000 0x00500000 U-Boot# nand write 0x82000000 0x0000000 0x500000
U-Boot# nand erase 0x00500000 0xFB00000 U-Boot# nand write 0x81000000 0x00500000 0x02000000
[ 1.447937] 0x000000500000-0x000010000000 : "File System"
Usage:
nand info - show available NAND devices
nand device [dev] - show or set current device
nand read - addr off|partition size
nand write - addr off|partition size
read/write 'size' bytes starting at offset 'off'
to/from memory address 'addr', skipping bad blocks.
nand read.raw - addr off|partition [count]
nand write.raw - addr off|partition [count]
Use read.raw/write.raw to avoid ECC and access the flash as-is.
nand erase[.spread] [clean] off size - erase 'size' bytes from offset 'off'
With '.spread', erase enough for given file size, otherwise,
'size' includes skipped bad blocks.
nand erase.part [clean] partition - erase entire mtd partition'
nand erase.chip [clean] - erase entire chip'
nand bad - show bad blocks
nand dump[.oob] off - dump page
nand scrub [-y] off size | scrub.part partition | scrub.chip
really clean NAND erasing bad blocks (UNSAFE)
nand markbad off [...] - mark bad block(s) at offset (UNSAFE)
nand biterr off - make a bit error at offset (UNSAFE)
U-Boot# tftp 0x82000000
Now flash the kernel image to NAND at the appropriate offset (refer to NAND layout section for the offsets)
U-Boot# nand erase 0x00280000 0x00500000 U-Boot# nand write 0x82000000 0x00280000 0x500000
*Image_size should be aligned to page size of 2048 (0x800) bytes
UBIFS file system flashing
In AM335X, UBIFS file system is used in NAND flash as it is next generation flash file system.
1. Creating and Flashing of UBIFS file system image is described over here
In case of AM335x, file system partition is starting from 0x780000. So flashing offset for file system in U-Boot is 0x780000 and from Linux MTD partition number 7 should used for flashing file file system.
Creating 1 MTD partitions on "nand0":
0x000000500000-0x000004500000 : "mtd=1"
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: sub-page size: 512
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=1"
UBI: MTD device size: 64 MiB
UBI: number of good PEBs: 512
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: 503
UBI: total number of reserved PEBs: 9
UBI: number of PEBs reserved for bad PEB handling: 5
UBI: max/mean erase counter: 1/0
$ mkfs.ubifs -q -r root-fs -m 2048 -e 129024 -c 2047 -o ubifs.img $ ubinize -o ubi.img -m 2048 -p 128KiB -s 512 ubinize.cfg
nandroot=ubi0:rootfs rw ubi.mtd=5,2048
"ubi.mtd=3 root=ubi0:rootfs rootfstype=ubifs console=ttySAC0 init=/linuxrc rw "
设置启动命令:bootcmd,(bootcmd 是uboot启动的命令,输入命令:
U-Boot# loady 0x82000000
U-Boot# nand erase 0x0 0x20000 U-Boot# nand write 0x82000000 0x0 0x20000
U-Boot# loady 0x82000000
U-Boot# nand erase 0x80000 0x40000 U-Boot# nand write 0x82000000 0x80000 0x40000