gpt4 book ai didi

c - 尝试写入 mtd 设备时获取 EINVAL

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:40 27 4
gpt4 key购买 nike

我指的是这个 answer 中的代码.不过我添加了错误处理。open() 删除和 read() 都执行无误,我读取的 20 个字节都是 0xff。但是,当尝试从 data[] 数组写入 20 个字节时,我从 write() 函数中得到了一个 EINVAL 错误代码。问题的原因可能是什么?我确实在尝试写入之前删除内存...

最佳答案

我看到了你的original post .我最近遇到了同样的问题,我发现写入大小很重要。

mtd_info_t(struct mtd_info_user) 有一个名为 writesize 的变量(引用: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

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

希望对你有帮助

关于c - 尝试写入 mtd 设备时获取 EINVAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com