mtd设备操作、jffs2

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

mtd交叉编译:https://blog.csdn.net/zhangxuechao_/article/details/52124424

系统flash操作命令

# 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
# mtd_debug read /dev/mtd2 0 100 file.dest

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

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

uboot flash按分区操作

> 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-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\n", erase.length, erase.start);

         fflush(stdout);

         if(unlock != 0)
         {
            //unlock the sector first
            printf("\rPerforming Flash unlock at offset 0x%x\n", erase.start);

            if(ioctl(fd, MEMUNLOCK, &erase) != 0)
            {
               perror("MTD Unlock failure");
               close(fd);
               return -1;
            }
         }

         if(ioctl(fd, MEMERASE, &erase) != 0)
         {
            perror("MTD 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;
   struct mtd_info_user info;

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

   struct stat st;

   //check is a char device
   ret = fstat(fd, &st);

   if(ret < 0)
   {
      printf("fstat %s failed!\n", FLASH_DEV_NAME);
      close(fd);
      return -1;
   }

   if(!S_ISCHR(st.st_mode))
   {
      printf("%s: not a char device\n", FLASH_DEV_NAME);
      close(fd);
      return -1;
   }

#if 0

   ret = non_region_erase(fd, 0, 1, 0);

   if(ret < 0)
   {
      return -1;
   }

#endif

   char *pReadBuf;
   int nReadBytes;

   nReadBytes = info.erasesize;
   pReadBuf = malloc(nReadBytes);
   memset(pReadBuf, 0, nReadBytes);
   ret = read(fd, pReadBuf, 256);
   printf("%s--read %d bytes from mtd\n", __func__, ret);

   for(i = 0; i < ret; i++)
   {
      printf("%02x,", *(pReadBuf + i));
   }

   printf("\n");

   free(pReadBuf);
}

详细操作,参考mtd源码mtd_debug.c

posted @ 2016-08-08 23:45  thomas_blog  阅读(1000)  评论(0编辑  收藏  举报