I want to make a DXE_RUNTIME_DRIVER with EDK2 that hooks gRT->SetVariable so I can write stuff to memory by setting NVRAM variables through Windows (NtSetSystemEnvironmentValueEx). It always gives me a bluescreen though.
This is my code:
EFI_STATUS EFIAPI SetVariableHook(IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, IN UINT32 Attributes, IN UINTN DataSize, IN VOID *Data) {
if (CompareGuid(VendorGuid, &gEfiSmrkVariableGuid) && StrCmp(VariableName, gSmrkVariableName) == 0) {
EFI_PHYSICAL_ADDRESS SMRK_PHYSICAL_ADDRESS = 0x1112323223;
EFI_STATUS Status = gRT->ConvertPointer(0, reinterpret_cast<void**>(&SMRK_PHYSICAL_ADDRESS));
if (EFI_ERROR(Status)) {
return Status;
}
EFI_VIRTUAL_ADDRESS SMRK_ADDRESS = SMRK_PHYSICAL_ADDRESS;
SMRK_MEMORY_TABLE *Smrk = reinterpret_cast<SMRK_MEMORY_TABLE *>(SMRK_ADDRESS);
if (Smrk) {
Smrk->Signature[0] = 0x53; // S
Smrk->Signature[1] = 0x4D; // M
Smrk->Signature[2] = 0x52; // R
Smrk->Signature[3] = 0x4B; // K
return EFI_SUCCESS;
}
return EFI_ABORTED;
}
return gOrigSetVariable(VariableName, VendorGuid, Attributes, DataSize, Data);
}
What do I do wrong? The error code is “UNEXPECTED KERNEL MODE TRAP”.