在Linux内核模式下读写EFI变量

3

我正在处理Linux UEFI技术。 我想通过我的驱动程序代码访问efi变量。 目前,我正在查看类似efi.get_variable()的linux/efi.h API。 但是,我不知道如何从我的驱动程序代码中调用这些API。

    struct efi  efi1;
efi_init();         
    efi_char16_t *name = (efi_char16_t *)"Boot001";
    efi_guid_t *vendor = (efi_guid_t *)"8be4df61-93ca-11d2-aa0d-00e098032b8c";
    u32 *attr = (u32 *)0x7;
    unsigned long data_size = 1024;
    void *data = NULL;

    printk("\n Showing efi info \n");
    stat = efi1.get_variable(name,vendor,attr,&data_size,data);

使用此代码时,我得到了数据的NULL值。您能建议我应该怎么做吗?或者有什么修改建议吗?


你确定可以直接将 char * 强制转换为 efi_guid_t * 吗?而且 u32 *attr = (u32 *)0x7; 看起来非常、非常不对。你还在 efi1 初始化之前使用它。 - tangrs
此外,我认为您不需要调用 efi_init - 这是在启动时完成的。 - tangrs
@tangrs,我对如何初始化efi1并访问这些变量感到困惑,你能提出任何修改建议吗? - aksh
1个回答

2

试着将代码改写成类似以下的形式(注意,未经测试):

efi_char16_t name[] = L"Boot0001";
efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
u32 attr;
unsigned long data_size = 0;
u8 *data = NULL;
efi_status_t status;

/* Get real size of UEFI variable */
status = efi.get_variable(name,&guid,&attr,&data_size,data);
if (status == EFI_BUFFER_TOO_SMALL) {
   /* Allocate data buffer of data_size bytes */
   data = (u8*)vmalloc(data_size);
   if (!data) {
       /* Your handling here */
   }

   /* Get variable contents into buffer */
   status = efi.get_variable(name,&guid,&attr,&data_size,data);
   if (status != EFI_SUCCESS) {
       /* Your handling here */
   }
   else {
       /* Variable is now in data */
   }   
} 
else if (status == EFI_NOT_FOUND) {
   /* There is no Boot0001 variable. Try Boot0000 maybe? */
} 
else {
   /* Your handling here */
}

这非常有帮助。非常感谢你。@CodeRush - siyuan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接