LINUX 内核空间中写mtd设备
内核中读mtd分区中指定长度字节的内容,并存放到字符数组中实现:
for(i=0;i<MAX_MTD_DEVICES;i++)
{
mymtd=get_mtd_device(NULL,i);
if(mymtd && !strcmp(mymtd->name,"Backup"))
{
printk("Get the MTD %s!n", mymtd->name);
break;
}
}
if( i >= MAX_MTD_DEVICES)
{
printk("Can't find MTD Conf!n");
goto err_mtd;
}
if (!mymtd | !mymtd->read)
{
printk("Can't read MTD!n");
goto err_mtd;
}
buf = kmalloc(MTD_DATA_SIZE, GFP_KERNEL);
mymtd->read(mymtd, 0, MTD_DATA_SIZE, &retlen, buf);
if(retlen != MTD_DATA_SIZE){
printk("Read BAK MTD fails!n");
kfree(buf);
goto err_mtd;
}
但内核中写入将字符数组写入mtd分区的指定位置一直不成功:
if (!mymtd || !mymtd->erase)
{
printk("Can't erase BAK MTD!n");
kfree(buf);
goto err_mtd;
}
memset(&mymtd_erase_info, 0, sizeof(struct erase_info *));
mymtd_erase_info.mtd = mymtd;
mymtd_erase_info.addr = 0;
mymtd_erase_info.callback = mtd_erase_callback;
mymtd_erase_info.len = MTD_DATA_SIZE & ~(mymtd->erasesize - 1);
mymtd->erase(mymtd, &mymtd_erase_info);
if (!mymtd->write)
{
printk("Can't write BAK MTD!n");
kfree(buf);
goto err_mtd;
}
strncpy(buf + MTD_OPS_OFF, value, length);
mymtd->write(mymtd, 0, MTD_DATA_SIZE, &retlen, buf);
if(retlen != MTD_DATA_SIZE){
printk("Write BAK MTD fails!n");
kfree(buf);
goto err_mtd;
}
上面的erase总是出错,哪位帮忙分析一下,最好有write的过程的code example!谢谢了!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有头有尾啊!
后来用的内核MTD CORE的erase_write函数去掉static定义