繁体   English   中英

如何访问物理 memory 的值并通过在 linux kernel 中创建文件来存储该值

[英]how to access value of physical memory and store the value via making file in linux kernel

我是 kernel 新手,英语不好。

我有一块STM32板。 STM32 开发板有一个唯一的 Id。

我想访问存储唯一 Id 并获取地址值的物理 memory 地址。

然后我想将值存储到缓冲区,因为文件由唯一 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);
}

物理 memory 的读数效果很好。

但是将值存储到缓冲区失败。

请给我建议。

谢谢你。

尽管缺少buf_uniqueId的定义,但存储到至少包含 12 个元素的有效数组应该可以正常工作。 考虑readb()可能返回零。 而不是调用strlen()并期望一个以 NULL 结尾的字符串,写入大小应该是sizeof()之一或固定为 12,具体取决于buf_uniqueId分配。 您可以将每个 12 替换为#define符号。

建议删除get_fs()set_fs()行,同时调用 kernel_write() 替换vfs_write() kernel_write()并检查结果; 还要注意kernel_write()中的old_fs恢复

编译器应该抱怨比较指针和 integer: (unique == -1) ; filp_open()可以返回各种错误代码; 按照filp_open()的源代码查看各种与错误相关的宏,例如IS_ERR()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM