簡體   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