问题描述
我是内核新手,英语不好。
我要访问一个物理内存地址,该地址存储唯一的ID,并采用该地址的值。
然后我想将值存储到缓冲区,因为使文件包含唯一的ID。
下面是我的代码。
u8 buf_uniqueId[12];
void __iomem *Unique_Id = ioremap(0x5C005234,12);
if(Unique_Id == NULL){
printk(KERN_INFO "count not found UniqueId\n");
return 1;
}
printk(KERN_INFO "This is UniqueId\n");
struct file *unique = filp_open("/Unique_Id",O_WRONLY|O_CREAT,0644);
if(unique == -1){
printk("[!] Can't crate the dstfile");
return 2;
}
else{
fs = get_fs();
set_fs(get_ds());
for(i=0;i<12;i++){
buf_uniqueId[i]=readb(Unique_Id+i);
printk(KERN_DEBUG "value of buffer is %02x\n",buf_uniqueId[i]);
printk("%02x ",readb(Unique_Id+i));
}
vfs_write(unique,buf_uniqueId,strlen(buf_uniqueId),&unique->f_pos);
}
物理内存的读取值效果很好。
但是将值存储到缓冲区失败。
请给我建议。
谢谢。
解决方法
尽管缺少buf_uniqueId
的定义,但将其存储到具有至少12个元素的有效数组应该可以。考虑readb()
可能返回零。根据{{1}}的分配,写入大小应为strlen()
之一或固定为12,而不是调用sizeof()
并期望以NULL结尾的字符串。您可以使用buf_uniqueId
符号将每12个替换。
建议删除#define
和get_fs()
行,并用对set_fs()
的调用替换vfs_write()
并检查结果;还请注意kernel_write()
内的old_fs
恢复
编译器应该抱怨比较指针和整数:kernel_write()
; (unique == -1)
可能返回各种错误代码;按照filp_open()
的源代码查看各种与错误相关的宏,例如filp_open()