一 编写源代码
DataSize = sizeof (CSDNEnable);
Status = gRT->SetVariable (
L"CSDNEnable",
&gEfiCsdnEnableGuid,
EFI_VARIABLE_NON_VOLATILE |
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS,
DataSize,
&CSDNEnable
);
ASSERT(Status);
DEBUG ((EFI_D_ERROR," [CSDN]: Setvariable CSDNEnable 0x%x.\n", CSDNEnable));
CSDNEnable = 0;
DataSize = sizeof (CSDNEnable);
Status = gRT->GetVariable (
L"CSDNEnable",
&gEfiCsdnEnableGuid,
NULL,
&DataSize,
&CSDNEnable
);
ASSERT(Status);
if (CSDNEnable) {
DEBUG ((EFI_D_ERROR, "[CSDN] CSDNEnable\n"));
}
二、 编译
生成EFI文件 & 运行
UEFI APP MyVariable.efi
还可以通过Shell下的命令setvar
,dmpstore
,来查看新建Variable的值。
UEFI Varible 可以用作不同driver之间的通信, 是一种非常好的模块共享机制。Variable 的服务由BS,RT, SMM提供,在不同的phase应该使用对对应的服务。如在SMM handler下就应该使用
Status = gSmst->SmmLocateProtocol ( &gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mSmmVariable)
mSmmVariable->SmmSetVariable(...)
mSmmVariable->SmmGetVariable(...)
否则会fail。
PEI Phase 使用如下(注意PEI阶段没有setvariable 操作
)
pPeiServices = (EFI_PEI_SERVICES**)(UINTN)Host->var.common.oemVariable;
//Read the EFI variable that stores TME Key
Status = (*pPeiServices)->LocatePpi (
pPeiServices,
&gEfiPeiReadOnlyVariable2PpiGuid,
0,
NULL,
(void **)&pPeiVariable
);
Status = pPeiVariable->GetVariable (pPeiVariable, L"CSDNEnable", &gEfiCsdnEnableGuid, NULL, &Size, (void *)&CSDNEnable);
Linux write variable cmd:
chattr-i/sys/firmware/efi/efivars/CSDNEnable-dc1254bf-b8bd-4dec-8878-acbf1da226c23>/dev/null;
printf"\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00">/sys/firmware/efi/efivars/CSDNEnable-dc1254bf-b8bd-4dec-8878-bccf1da226c3;
Linux dumpvariable cmd:
hexdump /sys/firmware/efi/efivars/CSDNEnable-dc1254bf-b8bd-4dec-8878-bccf1da226c3
Variable DEMO源码