ubifs 提取
nandsim掛載 ?mkfs.ubifs ?create_ubifs.sh
http://blog.csdn.net/hjd03132301/article/details/16804369
制作ubifs文件系統,掛載ubifs?
http://blog.chinaunix.net/uid-28410803-id-3892468.html
http://www.linux-mtd.infradead.org/faq/nand.html
http://www.linux-mtd.infradead.org/faq/ubifs.html
Can I use seek/read/write on /dev/mtdX?
Yes, as long as your program is aware of bad blocks. Make sure to set the desired ECC layout by ioctl (MEMSETOOBSEL). A example for bad block handling and usage of ioctl (MEMOOBSEL) can be found in the nandwrite utility.
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;
###############################################################
create-ubifs.sh腳本,主要是調用mkfs.ubifs和ubinize工具和相關參數來制作ubifs文件系統,內容如下:
#!/bin/bash
##########################################################
# ? ?Script to generate ubifs filesystem image. ? ? #
##########################################################
##### ubinize configuration file
config_file=rootfs_ubinize.cfg
##### Function to check result of the command
check_result() {
if [ $? -ne 0 ]
then
? ? echo "FAILED"
else
? ? echo "SUCCESSFUL"
fi
}
###### ?Function to check whether an application exists
check_program() {
for cmd in "$@"
do
? ? ? ? which ${cmd} > /dev/null 2>&1
? ? ? ? if [ $? -ne 0 ]
? ? ? ? then
? ? ? ? ? ? ? ? echo
? ? ? ? ? ? ? ? echo "Cannot find command /"${cmd}/""
? ? ? ? ? ? ? ? echo
? ? ? ? ? ? ? ? exit 1
? ? ? ? fi
done
}
if [ $# -ne 5 ]
then
? ? echo
? ? echo 'Usage: create-ubifs.sh [page_size_in_bytes] [pages_per_block] [partition_size_in_bytes] [blocks_per_device] [path_to_rootfs]'
? ? echo
? ? exit
fi
page_size_in_bytes=$1
echo "Page size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [$page_size_in_bytes]bytes."
pages_per_block=$2
echo "Pages per block ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [$pages_per_block]"
partition_size_in_bytes=$3
echo "File-system partition size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[$partition_size_in_bytes]bytes."
blocks_per_device=$4
echo "Blocks per device ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [$blocks_per_device]"
path_to_rootfs=$5
# wear_level_reserved_blocks is 1% of total blcoks per device
wear_level_reserved_blocks=`expr $blocks_per_device / 100`
echo "Reserved blocks for wear level ? ? ? ? ? ? ? ? ? ? ? ? ? ?[$wear_level_reserved_blocks]"
#logical_erase_block_size is physical erase block size minus 2 pages for UBI
logical_pages_per_block=`expr $pages_per_block - 2`
logical_erase_block_size=`expr $page_size_in_bytes \* $logical_pages_per_block`
echo "Logical erase block size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[$logical_erase_block_size]bytes."
#Block size = page_size * pages_per_block
block_size=`expr $page_size_in_bytes \* $pages_per_block`
echo "Block size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[$block_size]bytes."
#physical blocks on a partition = partition size / block size
partition_physical_blocks=`expr $partition_size_in_bytes / $block_size`
echo "Physical blocks in a partition ? ? ? ? ? ? ? ? ? ? ? ? ? ?[$partition_physical_blocks]"
#Logical blocks on a partition = physical blocks on a partitiion - reserved for wear level
patition_logical_blocks=`expr $partition_physical_blocks - $wear_level_reserved_blocks`
echo "Logical blocks in a partition ? ? ? ? ? ? ? ? ? ? ? ? ? ? [$patition_logical_blocks]"
#File-system volume = Logical blocks in a partition * Logical erase block size
fs_vol_size=`expr $patition_logical_blocks \* $logical_erase_block_size`
echo "File-system volume ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[$fs_vol_size]bytes."
echo
echo "Generating configuration file..."
echo "[rootfs-volume]" ?> $config_file
echo "mode=ubi" >> $config_file
echo "image=rootfs_ubifs.img" >> $config_file
echo "vol_id=0" >> $config_file
echo "vol_size=$fs_vol_size" >> $config_file
echo "vol_type=dynamic" >> $config_file
echo "vol_name=system" >> $config_file
echo
# Note: Check necessary program for installation
#echo -n "Checking necessary program for installation......"
#check_program mkfs.ubifs ubinize
#echo "Done"
#Generate ubifs image
echo -n "Generating ubifs..."
mkfs.ubifs -x lzo -m $page_size_in_bytes -e $logical_erase_block_size -c $patition_logical_blocks -o rootfs_ubifs.img -d $path_to_rootfs
check_result
echo -n "Generating ubi image out of the ubifs..."
ubinize -o ubi.img -m $page_size_in_bytes -p $block_size -s $page_size_in_bytes $config_file -v
check_result
rm -f rootfs_ubifs.img
rm -f $config_file
將mkfs.ubifs和ubinize以及create-ubifs.sh放置在同一目錄下,然后調用create-ubifs.sh即可創建ubifs文件系統,create-ubifs.sh用法如下:
create-ubifs.sh ?page_size_in_bytes(頁大小) pages_per_block(每個扇區的頁數量) partition_size_in_bytes(分區大小) blocks_per_device(扇區數量) path_to_rootfs(文件系統路徑)
舉例如下:
./create-ubifs.sh 2048 64 83886080 4096 ./rootfs
上面命令的意思是調用create-ubifs.sh將當前目錄下的rootfs文件夾的內容制作成ubifs文件系統,nand flash的頁大小為2k,每個扇區有64頁,
總共有4096個扇區,要制作的文件系統的大小為83886080字節。
create-ubifs.sh 2048 64 83886080 2048 /etc
256MiB, 2048 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa third_id_byte=0x00 fourth_id_byte=0x15
dd if=ubi.img of=/dev/mtd0 bs=2048
#dd if=mtdblock2.bin of=/dev/mtd0 bs=2048
modprobe ubi
ubiattach /dev/ubi_ctrl -m 0 -O 2048
-m指定掛在在mtd0上
-O參數用來指定VID header offset,默認是512。
mkdir /ubimnt
mount -t ubifs ubi0_0 /ubimnt
umount /ubimnt
ubidetach /dev/ubi_ctrl -m 0
rmmod nandsim
每次dd寫入mtd0之前都需要重新modprobe nandsim才可以
否則mount的時候提示錯誤ubifs_read_master: bad leb_cnt on master node
在ubiattach編程器固件時出現錯誤
UBI error: vtbl_check: bad CRC at record 11: 0x2acc7fc4, not 0xf116c36b
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4160135&page=1
1.準備bin文件
編程器讀出的文件命名為src.raw,與getnanddata.exe放在一起
雙擊getnanddata.exe,運行結束后生成src.bin
winhex截取src.bin:跳轉到0x1000000位置,設置開始,跳轉到0xE000000-1,設置為結束,復制
創建新文件1byte,粘貼,刪掉第一個00 byte,保存為新文件rootfs.bin
rootfs.bin放入虛擬機(CentOS 6)
2.虛擬機運行
a)準備nand虛擬設備(256MB)
modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa third_id_byte=0x00 fourth_id_byte=0x15
b)復制nand數據
nandwrite /dev/mtd0 rootfs.bin
c)掛載ubifs文件系統
modprobe ubi
ubiattach /dev/ubi_ctrl -m 0 -O 2048
? -m指定掛在在mtd0上
? -O參數用來指定VID header offset,默認是512。
mkdir /ubimnt
mount -t ubifs ubi0_0 /ubimnt
d)打包文件系統
tar czvf src.tar.gz /ubimnt
e)卸載文件系統
umount /ubimnt
ubidetach /dev/ubi_ctrl -m 0
rmmod nandsim
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#define NAND_RAW_SIZE? ?(264*1024*1024)
#define NAND_SIZE? ? ? ?(256*1024*1024)
#define PAGE_SIZE? ? ? ?(2112)
#define PAGE_COUNT? ? ? (NAND_RAW_SIZE / PAGE_SIZE)
int main()
{
? ? FILE *fi = fopen("187B-", "rb");
? ? FILE *fo = fopen("187b.bin", "wb");
? ? char buf[2048];
? ? int ret;
? ? unsigned int page;
? ? if(!fi || !fo)
? ? ? ? return 0;
? ? //read page0
? ? ret = fread(buf, 1, 12, fi);
? ? if(ret != 12)
? ? ? ? return 0;
? ? ret = fread(buf, 1, 2048, fi);
? ? if(ret != 2048)
? ? ? ? return 0;
? ? fwrite(buf, 1, 2048, fo);
? ? ret = fread(buf, 1, 52, fi);
? ? if(ret != 52)
? ? ? ? return 0;
? ? for(page = 1; page < PAGE_COUNT; page++)
? ? {
? ? ? ? char bbi;
? ? ? ? ret = fread(buf, 1, 10, fi);? ? //metadata
? ? ? ? if(ret != 10)
? ? ? ? ? ? break;
? ? ? ? bbi = buf[0];? ?//4th block(block 3) data area backup
? ? ? ? //block0
? ? ? ? ret = fread(buf, 1, 512, fi);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fwrite(buf, 1, 512, fo);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fread(buf, 1, 13, fi);? ? //ecc
? ? ? ? if(ret != 13)
? ? ? ? ? ? break;
? ? ? ? //block1
? ? ? ? ret = fread(buf, 1, 512, fi);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fwrite(buf, 1, 512, fo);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fread(buf, 1, 13, fi);? ? //ecc
? ? ? ? if(ret != 13)
? ? ? ? ? ? break;
? ? ? ? //block2
? ? ? ? ret = fread(buf, 1, 512, fi);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fwrite(buf, 1, 512, fo);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fread(buf, 1, 13, fi);? ? //ecc
? ? ? ? if(ret != 13)
? ? ? ? ? ? break;
? ? ? ? //block3
? ? ? ? ret = fread(buf, 1, 512, fi);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? buf[0x1cf] = bbi;? ?//restore bbi to data byte
? ? ? ? ret = fwrite(buf, 1, 512, fo);?
? ? ? ? if(ret != 512)
? ? ? ? ? ? break;
? ? ? ? ret = fread(buf, 1, 15, fi);? ? //ecc
? ? ? ? if(ret != 15)
? ? ? ? ? ? break;
? ? }
? ? fclose(fi);
? ? fclose(fo);
}
總結
- 上一篇: mtd-utils编译
- 下一篇: 雄迈400万TF卡内核补丁