簡體   English   中英

寫在mtd塊設備上

[英]Write on a mtd block device

我正在嘗試使用MTD塊設備在NAND閃存上寫,但我不明白一切。

在這里讀到

  • mtdblockN是只讀塊設備N.
  • mtdN是讀/寫字符設備N.
  • mtdNro是只讀字符設備N.

但是我想用C語言簡單write直接將字節寫入分區,我不明白它是如何工作的(我讀過一些我首先必須刪除我要寫的扇區)。

我應該使用哪種設備以及如何在此設備上書寫?

對存儲器技術設備進行讀寫操作並不是與任何其他類型的IO完全不同,除了在編寫之前需要擦除扇區(擦除塊)

為了使事情對自己簡單,你總是可以只使用MTD-utils的(如flash_erasenanddumpnandwrite ,擦除,讀取和寫入分別),而無需編寫代碼。

但是,如果您確實想要務實地執行此操作,這是一個示例,請務必閱讀所有注釋,因為我將所有詳細信息放在其中:

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

關於這一點的好處是因為您可以像使用其他設備那樣使用標准工具,這使得理解write()open()read()功能以及對它們的期望很容易。

例如,如果使用write()時得到的值為EINVAL則可能意味着:

fd附在不適合書寫的物體上; 或者使用O_DIRECT標志打開文件,並且buf中指定的地址,count中指定的值或當前文件偏移量未適當對齊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM