mtd设备操作、jffs2

安装
手动安装mtd-utils,根据系统自行选择

命令

# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00080000 00020000 "boot"
mtd1: 00100000 00020000 "kernel"
mtd2: 00580000 00020000 "roofs"
mtd3: 00900000 00020000 "app"
# flash_erase device [start] [cnt] 
# flash_erase /dev/mtd0 0×40000 5

start:起始位置。必须为0x20000(128K)的整数倍
cnt:块数

# flash_eraseall [OPTION] device 
# flash_eraseall -j /dev/mtd0

-j,jffs2:jffs2格式化分区
-q,quiet:不显示打印信息

# flash_eraseall -j -q /dev/mtd0 = flash_erase /dev/mtd0 0 0

新版擦除整个设备,已建议使用flash_erase

# mount -t jffs2 /dev/mtdblock0 /mnt

挂载mtd设备。mtdblock只用于挂载,/dev/mtd操作实际就是操作/dev/mtdblock

# flashcp <filename> <device>
# flashcp fs.jffs2 /dev/mtd0
# mtd_debug info <device>
# mtd_debug info /dev/mtd0
mtd.type = MTD_NORFLASH
mtd.flags = 
mtd.size = 12582912 (12M)
mtd.erasesize = 131072 (128K)
mtd.oobblock = 1 
mtd.oobsize = 0 
mtd.ecctype = (unknown ECC type - new MTD API maybe?)
regions = 0

注意
对flash对应mtd分区擦除时。擦除要擦除整个设备大小;写要写jffs2文件系统大小。不然数据可能会有问题,可能原因是jffs2格式写入位置

# nand erase 0x4100000 0x200000 //擦除整个mtd分区2M
# nand write 0x6000000 0x4100000 0x20000 //写入128K

整块操作

> mtd

device nand0 <nandflash0>, # parts = 4
 #: name                        size            offset          mask_flags
 0: bootloader          0x00040000      0x00000000      0
 1: params              0x00020000      0x00040000      0
 2: kernel              0x00200000      0x00060000      0
 3: root                0x0fda0000      0x00260000      0

> nand erase root

NAND erase: device 0 offset 0x260000, size 0xfda0000                                         
Erasing at 0xffe0000 -- 100% complete.
OK

> nand write.jffs2 0x3000000 root

NAND write: device 0 offset 0x260000, size 0xfda0000
 0xfda0000 bytes written: OK

filesize文件大小

> nand write.jffs2 0x3000000 0x260000 $(filesize)

NAND write: device 0 offset 0x260000, size 0x15787a8

Writing data at 0x17f8000 -- 100% complete.
 22513576 bytes written: OK

举例

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <mtd/mtd-user.h>

int non_region_erase(int Fd,int start,int count,int unlock)
{
    mtd_info_t meminfo;

    if(ioctl(Fd, MEMGETINFO, &meminfo) == 0)
    {   
        erase_info_t erase;
        erase.start = start;
        erase.length = meminfo.erasesize;

        for(; count > 0; count--)
        {   
            printf("\rPerforming Flash Erase of length %u at offset 0x%x", erase.length, erase.start);

            fflush(stdout);

            if(unlock != 0)
            {   
                //unlock the sector first
                printf("\rPerforming Flash unlock at offset 0x%x", erase.start);
                if(ioctl(Fd, MEMUNLOCK, &erase) != 0)
                {   
                    perror("\nMTD Unlock failure");
                    close(Fd);
                    return -1; 
                }   
            }   

            if (ioctl(Fd, MEMERASE, &erase) != 0)
            {   
                perror("\nMTD Erase failure");
                close(Fd);
                return -1; 
            }   
            erase.start += meminfo.erasesize;
        }   
        printf("done!\n");
    }
    return 0;
}

int main(int argc, char *argv[])
{
    int fd,ret, i, write_cnt, writelen, regcount; 
    struct mtd_info_user info;

    // Open the device
    if ((fd = open(argv[2], O_RDWR)) < 0)
    {
        fprintf(stderr,"File open error\n");
        return -1;
    }
    else
    {
        ioctl(fd, MEMGETINFO, &info);
        printf("info.size=%d\n info.erasesize=%d\n info.writesize=%d\n info.oobsize=%d \n", info.size, info.erasesize, info.writesize, info.oobsize);
    }

    if (ioctl(fd, MEMGETREGIONCOUNT, &regcount) == 0)
    {
        printf("regcount=%d\n", regcount);
    }

    //write
    writelen = info.writesize;
    char bufss[] = "welcome to china";

    if((write_cnt = write(fd, bufss, writelen)) != writelen)
    {
        printf("write mtd device error!");
        return -1;
    }

    //read
    char *pReadBuf;
    int nReadBytes;

    nReadBytes = info.erasesize;
    pReadBuf = malloc(nReadBytes);
    memset(pReadBuf, 0, nReadBytes);
    ret = read(fd, pReadBuf, 256);
    printf("%s\n", pReadBuf);
    printf("%s--read %d bytes from mtd\n", __FUNCTION__, ret);
    for(i = 0; i < ret; i++)
    {
        printf("%02x,", *(pReadBuf + i));
    }
    printf("\n");

    free(pReadBuf);
} 

猜你喜欢

转载自blog.csdn.net/zhuyong006/article/details/89249773
今日推荐