ubifs 提取

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);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值