Skip to content

Commit

Permalink
OvmfPkg/VirtioRngDxe: check if device is ready
Browse files Browse the repository at this point in the history
Add a 'Ready' boolean to the driver state struct, use it to track
whenever the device is ready to be used.  In case it is not ready
throw an EFI_DEVICE_ERROR instead of sending a request which will
never receive an answer.

Signed-off-by: Gerd Hoffmann <[email protected]>
  • Loading branch information
kraxel authored and ardbiesheuvel committed Jun 1, 2024
1 parent 3b36aa9 commit 7339bfe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions OvmfPkg/VirtioRngDxe/VirtioRng.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ VirtioRngGetRNG (
}

Dev = VIRTIO_ENTROPY_SOURCE_FROM_RNG (This);
if (!Dev->Ready) {
DEBUG ((DEBUG_INFO, "%a: not ready\n", __func__));
return EFI_DEVICE_ERROR;
}

//
// Map Buffer's system physical address to device address
//
Expand Down Expand Up @@ -382,6 +387,7 @@ VirtioRngInit (
//
Dev->Rng.GetInfo = VirtioRngGetInfo;
Dev->Rng.GetRNG = VirtioRngGetRNG;
Dev->Ready = TRUE;

return EFI_SUCCESS;

Expand Down Expand Up @@ -414,8 +420,8 @@ VirtioRngUninit (
// VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from
// the old comms area.
//
Dev->Ready = FALSE;
Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);

Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap);

VirtioRingUninit (Dev->VirtIo, &Dev->Ring);
Expand All @@ -435,15 +441,16 @@ VirtioRngExitBoot (
{
VIRTIO_RNG_DEV *Dev;

DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __func__, Context));
DEBUG ((DEBUG_INFO, "%a: Context=0x%p\n", __func__, Context));
//
// Reset the device. This causes the hypervisor to forget about the virtio
// ring.
//
// We allocated said ring in EfiBootServicesData type memory, and code
// executing after ExitBootServices() is permitted to overwrite it.
//
Dev = Context;
Dev = Context;
Dev->Ready = FALSE;
Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
}

Expand Down
1 change: 1 addition & 0 deletions OvmfPkg/VirtioRngDxe/VirtioRng.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct {
VRING Ring; // VirtioRingInit 2
EFI_RNG_PROTOCOL Rng; // VirtioRngInit 1
VOID *RingMap; // VirtioRingMap 2
BOOLEAN Ready;
} VIRTIO_RNG_DEV;

#define VIRTIO_ENTROPY_SOURCE_FROM_RNG(RngPointer) \
Expand Down

0 comments on commit 7339bfe

Please sign in to comment.