尝试写入mtd设备时获取EINVAL [英] Getting EINVAL when trying to write to mtd device

查看:90
本文介绍了尝试写入mtd设备时获取EINVAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是此 answer 中的代码。
虽然我添加了错误处理。
open()擦除和 read()的执行均无错误,我执行的20个字节读取的全部为 0xff
但是,当尝试从 data [] 数组写入20个字节时,我得到了 EINVAL write()函数中的错误代码。
可能是什么原因造成的?在尝试写之前,我确实擦除了内存...

I am referring to the code in this answer. I added error handling though. The open() erase and the read() all perform without error and the 20 bytes that I read all are 0xff. However, when trying to write the 20 bytes from the data[] array, I get an EINVAL errorcode from the write() function. What could be the cause of the problem? I did erase the memory before trying to write...

推荐答案

我已经看到了您的原始帖子
我最近遇到了同样的问题,发现写入大小很重要。

I have seen your original post. I have the same problem recently, and I found that the write size is important.

mtd_info_t(struct mtd_info_user)有一个名为writesize
的变量(参考: https:// elixir .bootlin.com / linux / v3.2 / source / include / mtd / mtd-abi.h#L125

mtd_info_t(struct mtd_info_user) have a variable named writesize (reference: https://elixir.bootlin.com/linux/v3.2/source/include/mtd/mtd-abi.h#L125)

struct mtd_info_user {
    __u8 type;
    __u32 flags;
    __u32 size; /* Total size of the MTD */
    __u32 erasesize;
    __u32 writesize;
    __u32 oobsize;  /* Amount of OOB data per block (e.g. 16) */
    __u64 padding;  /* Old obsolete field; do not use */
};

写入mtd时,应注意writesize

when write to the mtd, should notice writesize

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

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

    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(hex) bytes\nMTD erase size: %x(hex) bytes\nMTD write size: %x(hex) bytes\n",
        mtd_info.type, mtd_info.size, mtd_info.erasesize, mtd_info.writesize);

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

    /**********************************************************
     *   important part!                                      *
     *   notice the size of data array is mtd_info.writesize  *
     **********************************************************/
    uint32_t write_size = mtd_info.writesize;
    unsigned char data[write_size];//write 0
    bzero(data, write_size);

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

希望它可以提供帮助

这篇关于尝试写入mtd设备时获取EINVAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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