写MTD块设备上 [英] Write on a mtd block device

查看:197
本文介绍了写MTD块设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用MTD块设备NAND快闪存储器写,但我不明白的一切。

当我读<一个href=\"http://etutorials.org/Linux+systems/embedded+linux+systems/Chapter+7.+Storage+Device+Manipulation/7.1+MTD-Supported+Devices/\"相对=nofollow>这里


  • mtdblockN 是只读块元件N

  • mtdN 是读/写字符元件N

  • mtdNro 是只读的char元件N

不过,我想用一个简单的直接写入字节为分区 C和我不明白它是如何工作(我读somewhre,我第一次必须抹去我想写上)部门。

我应该使用哪种设备以及如何在此设备上写的?


解决方案

读写自/至内存技术设备是不是真的比任何其他类型的IO的不同,不同之处在于你写之前,你需要擦除扇区(擦除块)

为了让事情对自己简单,你总是可以只使用MTD-utils的(如 flash_erase nanddump nandwrite ,擦除,读取,而不需要写入code分别写)。

不过,如果你想以务实的态度去做,这里是一个例子,请务必阅读所有的意见,因为我把所有的细节有:

 的#include&LT;&stdio.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&; SYS / ioctl.h&GT;
#包括LT&; MTD / MTD-user.h&GT;诠释的main()
{
    mtd_info_t mtd_info; // MTD的结构
    erase_info_t EI; //擦除块结构
    INT I;    unsigned char型数据[20] = {写0xDE,写入0xAD,0xBE,0xEF,//我们的数据写
                               写0xDE,写入0xAD,0xBE,0xEF,
                               写0xDE,写入0xAD,0xBE,0xEF,
                               写0xDE,写入0xAD,0xBE,0xEF,
                               写0xDE,写入0xAD,0xBE,0xEF};
    unsigned char型read_buf [20] = {0×00}; //空数组阅读    INT FD =打开(/开发/ mtd0,O_RDWR); //打开MTD设备进行读取和
                                        // 写作。注意:您要mtd0不mtdblock0
                                        //还您可能需要打开权限
                                        //为开发(须藤搭配chmod 777的/ dev / mtd0)    的ioctl(FD,MEMGETINFO,&安培; mtd_info); //获取设备信息    //倾倒了全面的检查,应符合什么是在/ proc / MTD
    的printf(MTD类型:%X \\ nMTD总大小:%X字节\\ nMTD抹掉大小:%X字节\\ n
         mtd_info.type,mtd_info.size,mtd_info.erasesize);    ei.length = mtd_info.erasesize; //设置擦除块大小
    对于(ei.start = 0; ei.start&LT; mtd_info.size; ei.start + = ei.length)
    {
        的ioctl(FD,MEMUNLOCK,&安培; EI);
        //输出(Eraseing程序段%#x的\\ n,ei.start); //显示擦除块
                                                  //警告,这种打印了很多!
        的ioctl(FD,MEMERASE,&安培; EI);
    }    lseek的(FD,0,SEEK_SET); //去的第一个块
    阅读(FD,read_buf,sizeof的(read_buf)); //读取20个字节    //完整性检查,应全部为0xFF如果擦除工作
    对于(i = 0; I&LT; 20;我++)
        的printf(BUF [%d个=为0x%02X \\ n,我,(无符号整数)read_buf [I]);    lseek的(FD,0,SEEK_SET); //返回第一个块的开始
    写(FD,数据的sizeof(数据)); //写我们的消息    lseek的(FD,0,SEEK_SET); //返回第一个块的开始
    读(FD,read_buf,sizeof的(read_buf)); //读取的数据    //完整性检查,现在你看我们写的留言!
    对于(i = 0; I&LT; 20;我++)
         的printf(BUF [%d个=为0x%02X \\ n,我,(无符号整数)read_buf [I]);
    关闭(FD);
    返回0;
}

关于这样做的好处是,因为当你从其他设备你可以使用标准utils的,它可以很容易地理解的 的write() ,的 的open() 和的 阅读() 做的,从他们的期望。

例如,如果在使用的write()你有 EINVAL 的值,这可能意味着:


  

    

fd被连接到一个对象,它是不适合于书写;或者该文件是用O_DIRECT标志打开,并在BUF指定了地址,偏移量不宜对准计中指定的值,或当前文件。


  

I'm trying to write on a NAND flash memory using MTD block device but I don't understand everything.

As I read here

  • mtdblockN is the read only block device N
  • mtdN is the read/write char device N
  • mtdNro is the read only char device N

But I'd like to directly write bytes to the partition using a simple write in C and I don't understand how it works (I read somewhre that I first must erase the sectors I want to write on).

Which device should I use and how to write on this device?

解决方案

Reading and writing from/to memory technology devices isn't really all that different than any other type of IO, with the exception that before you write you need to erase the sector (erase block)

To make things simple on yourself you can always just use the mtd-utils (such as flash_erase, nanddump and nandwrite, for erasing, read, and writing respectively) without the need for writing code.

However if you do want to do it pragmatically, here's an example, make sure to read all the comments as I put all the details in there:

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

int main()
{
    mtd_info_t mtd_info;           // the MTD structure
    erase_info_t ei;               // the erase block structure
    int i;

    unsigned char data[20] = { 0xDE, 0xAD, 0xBE, 0xEF,  // our data to write
                               0xDE, 0xAD, 0xBE, 0xEF,
                               0xDE, 0xAD, 0xBE, 0xEF,
                               0xDE, 0xAD, 0xBE, 0xEF,
                               0xDE, 0xAD, 0xBE, 0xEF};
    unsigned char read_buf[20] = {0x00};                // empty array for reading

    int fd = open("/dev/mtd0", O_RDWR); // open the mtd device for reading and 
                                        // writing. Note you want mtd0 not mtdblock0
                                        // also you probably need to open permissions
                                        // to the dev (sudo chmod 777 /dev/mtd0)

    ioctl(fd, MEMGETINFO, &mtd_info);   // get the device info

    // dump it for a sanity check, should match what's in /proc/mtd
    printf("MTD Type: %x\nMTD total size: %x bytes\nMTD erase size: %x bytes\n",
         mtd_info.type, mtd_info.size, mtd_info.erasesize);

    ei.length = mtd_info.erasesize;   //set the erase block size
    for(ei.start = 0; ei.start < mtd_info.size; ei.start += ei.length)
    {
        ioctl(fd, MEMUNLOCK, &ei);
        // printf("Eraseing Block %#x\n", ei.start); // show the blocks erasing
                                                  // warning, this prints a lot!
        ioctl(fd, MEMERASE, &ei);
    }    

    lseek(fd, 0, SEEK_SET);               // go to the first block
    read(fd, read_buf, sizeof(read_buf)); // read 20 bytes

    // sanity check, should be all 0xFF if erase worked
    for(i = 0; i<20; i++)
        printf("buf[%d] = 0x%02x\n", i, (unsigned int)read_buf[i]);

    lseek(fd, 0, SEEK_SET);        // go back to first block's start
    write(fd, data, sizeof(data)); // write our message

    lseek(fd, 0, SEEK_SET);              // go back to first block's start
    read(fd, read_buf, sizeof(read_buf));// read the data

    // sanity check, now you see the message we wrote!    
    for(i = 0; i<20; i++)
         printf("buf[%d] = 0x%02x\n", i, (unsigned int)read_buf[i]);


    close(fd);
    return 0;
}

The nice thing about this is since you can use the standards utils as you do from other devices, it makes it easy to understand what write(), open(), and read() do and what to expect from them.

For example if while using write() you got a value of EINVAL it could mean:

fd is attached to an object which is unsuitable for writing; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned.

这篇关于写MTD块设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆