Blame SOURCES/ovmf-ArmVirtPkg-PlatformBootManagerLib-connect-Virtio-RNG.patch

bdb79c
From 1d33f4bb28e1aa2c4d62979596140c22677a2e9f Mon Sep 17 00:00:00 2001
bdb79c
From: Laszlo Ersek <lersek@redhat.com>
bdb79c
Date: Fri, 18 May 2018 21:40:23 +0200
bdb79c
Subject: [PATCH 4/5] ArmVirtPkg/PlatformBootManagerLib: connect Virtio RNG
bdb79c
 devices again
bdb79c
bdb79c
Message-id: <20180518194024.30614-2-lersek@redhat.com>
bdb79c
Patchwork-id: 80426
bdb79c
O-Subject:  [RHEL-7.6 ovmf PATCH 1/2] ArmVirtPkg/PlatformBootManagerLib: connect
bdb79c
	Virtio RNG devices again
bdb79c
Bugzilla: 1579518
bdb79c
Acked-by: Thomas Huth <thuth@redhat.com>
bdb79c
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
bdb79c
bdb79c
Virtio RNG devices are never boot devices, so in commit ff1d0fbfbaec we
bdb79c
stopped connecting them. This is a problem because an OS boot loader may
bdb79c
depend on EFI_RNG_PROTOCOL to seed the OS's RNG.
bdb79c
bdb79c
Connect Virtio RNG devices again. And, while commit ff1d0fbfbaec removed
bdb79c
that from PlatformBootManagerAfterConsole(), reintroduce it now to
bdb79c
PlatformBootManagerBeforeConsole() -- this way Driver#### options launched
bdb79c
between both functions may access EFI_RNG_PROTOCOL too.
bdb79c
bdb79c
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
bdb79c
Fixes: ff1d0fbfbaec55038ccf888759588fa4e21516f4
bdb79c
Ref: https://bugzilla.redhat.com/show_bug.cgi?id=1579518
bdb79c
Contributed-under: TianoCore Contribution Agreement 1.1
bdb79c
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
bdb79c
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
bdb79c
(cherry picked from commit c4add6b6e971e0bb3f276ed3636a083e782e96cc)
bdb79c
---
bdb79c
 .../Library/PlatformBootManagerLib/PlatformBm.c    | 129 +++++++++++++++++++++
bdb79c
 .../PlatformBootManagerLib.inf                     |   1 +
bdb79c
 2 files changed, 130 insertions(+)
bdb79c
bdb79c
diff --git a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
bdb79c
index 5d5e51d..62cce6a 100644
bdb79c
--- a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
bdb79c
+++ b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
bdb79c
@@ -16,6 +16,7 @@
bdb79c
 **/
bdb79c
 
bdb79c
 #include <IndustryStandard/Pci22.h>
bdb79c
+#include <IndustryStandard/Virtio095.h>
bdb79c
 #include <Library/BootLogoLib.h>
bdb79c
 #include <Library/DevicePathLib.h>
bdb79c
 #include <Library/PcdLib.h>
bdb79c
@@ -27,6 +28,7 @@
bdb79c
 #include <Protocol/LoadedImage.h>
bdb79c
 #include <Protocol/PciIo.h>
bdb79c
 #include <Protocol/PciRootBridgeIo.h>
bdb79c
+#include <Protocol/VirtioDevice.h>
bdb79c
 #include <Guid/EventGroup.h>
bdb79c
 #include <Guid/RootBridgesConnectedEventGroup.h>
bdb79c
 
bdb79c
@@ -261,6 +263,121 @@ IsPciDisplay (
bdb79c
 
bdb79c
 
bdb79c
 /**
bdb79c
+  This FILTER_FUNCTION checks if a handle corresponds to a Virtio RNG device at
bdb79c
+  the VIRTIO_DEVICE_PROTOCOL level.
bdb79c
+**/
bdb79c
+STATIC
bdb79c
+BOOLEAN
bdb79c
+EFIAPI
bdb79c
+IsVirtioRng (
bdb79c
+  IN EFI_HANDLE   Handle,
bdb79c
+  IN CONST CHAR16 *ReportText
bdb79c
+  )
bdb79c
+{
bdb79c
+  EFI_STATUS             Status;
bdb79c
+  VIRTIO_DEVICE_PROTOCOL *VirtIo;
bdb79c
+
bdb79c
+  Status = gBS->HandleProtocol (Handle, &gVirtioDeviceProtocolGuid,
bdb79c
+                  (VOID**)&VirtIo);
bdb79c
+  if (EFI_ERROR (Status)) {
bdb79c
+    return FALSE;
bdb79c
+  }
bdb79c
+  return (BOOLEAN)(VirtIo->SubSystemDeviceId ==
bdb79c
+                   VIRTIO_SUBSYSTEM_ENTROPY_SOURCE);
bdb79c
+}
bdb79c
+
bdb79c
+
bdb79c
+/**
bdb79c
+  This FILTER_FUNCTION checks if a handle corresponds to a Virtio RNG device at
bdb79c
+  the EFI_PCI_IO_PROTOCOL level.
bdb79c
+**/
bdb79c
+STATIC
bdb79c
+BOOLEAN
bdb79c
+EFIAPI
bdb79c
+IsVirtioPciRng (
bdb79c
+  IN EFI_HANDLE   Handle,
bdb79c
+  IN CONST CHAR16 *ReportText
bdb79c
+  )
bdb79c
+{
bdb79c
+  EFI_STATUS          Status;
bdb79c
+  EFI_PCI_IO_PROTOCOL *PciIo;
bdb79c
+  UINT16              VendorId;
bdb79c
+  UINT16              DeviceId;
bdb79c
+  UINT8               RevisionId;
bdb79c
+  BOOLEAN             Virtio10;
bdb79c
+  UINT16              SubsystemId;
bdb79c
+
bdb79c
+  Status = gBS->HandleProtocol (Handle, &gEfiPciIoProtocolGuid,
bdb79c
+                  (VOID**)&PciIo);
bdb79c
+  if (EFI_ERROR (Status)) {
bdb79c
+    return FALSE;
bdb79c
+  }
bdb79c
+
bdb79c
+  //
bdb79c
+  // Read and check VendorId.
bdb79c
+  //
bdb79c
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_VENDOR_ID_OFFSET,
bdb79c
+                        1, &VendorId);
bdb79c
+  if (EFI_ERROR (Status)) {
bdb79c
+    goto PciError;
bdb79c
+  }
bdb79c
+  if (VendorId != VIRTIO_VENDOR_ID) {
bdb79c
+    return FALSE;
bdb79c
+  }
bdb79c
+
bdb79c
+  //
bdb79c
+  // Read DeviceId and RevisionId.
bdb79c
+  //
bdb79c
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_DEVICE_ID_OFFSET,
bdb79c
+                        1, &DeviceId);
bdb79c
+  if (EFI_ERROR (Status)) {
bdb79c
+    goto PciError;
bdb79c
+  }
bdb79c
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, PCI_REVISION_ID_OFFSET,
bdb79c
+                        1, &RevisionId);
bdb79c
+  if (EFI_ERROR (Status)) {
bdb79c
+    goto PciError;
bdb79c
+  }
bdb79c
+
bdb79c
+  //
bdb79c
+  // From DeviceId and RevisionId, determine whether the device is a
bdb79c
+  // modern-only Virtio 1.0 device. In case of Virtio 1.0, DeviceId can
bdb79c
+  // immediately be restricted to VIRTIO_SUBSYSTEM_ENTROPY_SOURCE, and
bdb79c
+  // SubsystemId will only play a sanity-check role. Otherwise, DeviceId can
bdb79c
+  // only be sanity-checked, and SubsystemId will decide.
bdb79c
+  //
bdb79c
+  if (DeviceId == 0x1040 + VIRTIO_SUBSYSTEM_ENTROPY_SOURCE &&
bdb79c
+      RevisionId >= 0x01) {
bdb79c
+    Virtio10 = TRUE;
bdb79c
+  } else if (DeviceId >= 0x1000 && DeviceId <= 0x103F && RevisionId == 0x00) {
bdb79c
+    Virtio10 = FALSE;
bdb79c
+  } else {
bdb79c
+    return FALSE;
bdb79c
+  }
bdb79c
+
bdb79c
+  //
bdb79c
+  // Read and check SubsystemId as dictated by Virtio10.
bdb79c
+  //
bdb79c
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16,
bdb79c
+                        PCI_SUBSYSTEM_ID_OFFSET, 1, &SubsystemId);
bdb79c
+  if (EFI_ERROR (Status)) {
bdb79c
+    goto PciError;
bdb79c
+  }
bdb79c
+  if (Virtio10 && SubsystemId >= 0x40) {
bdb79c
+    return TRUE;
bdb79c
+  }
bdb79c
+  if (!Virtio10 && SubsystemId == VIRTIO_SUBSYSTEM_ENTROPY_SOURCE) {
bdb79c
+    return TRUE;
bdb79c
+  }
bdb79c
+  return FALSE;
bdb79c
+
bdb79c
+PciError:
bdb79c
+  DEBUG ((DEBUG_ERROR, "%a: %s: %r\n", __FUNCTION__, ReportText, Status));
bdb79c
+  return FALSE;
bdb79c
+}
bdb79c
+
bdb79c
+
bdb79c
+/**
bdb79c
   This CALLBACK_FUNCTION attempts to connect a handle non-recursively, asking
bdb79c
   the matching driver to produce all first-level child handles.
bdb79c
 **/
bdb79c
@@ -644,6 +761,18 @@ PlatformBootManagerBeforeConsole (
bdb79c
   // Register platform-specific boot options and keyboard shortcuts.
bdb79c
   //
bdb79c
   PlatformRegisterOptionsAndKeys ();
bdb79c
+
bdb79c
+  //
bdb79c
+  // At this point, VIRTIO_DEVICE_PROTOCOL instances exist only for Virtio MMIO
bdb79c
+  // transports. Install EFI_RNG_PROTOCOL instances on Virtio MMIO RNG devices.
bdb79c
+  //
bdb79c
+  FilterAndProcess (&gVirtioDeviceProtocolGuid, IsVirtioRng, Connect);
bdb79c
+
bdb79c
+  //
bdb79c
+  // Install both VIRTIO_DEVICE_PROTOCOL and (dependent) EFI_RNG_PROTOCOL
bdb79c
+  // instances on Virtio PCI RNG devices.
bdb79c
+  //
bdb79c
+  FilterAndProcess (&gEfiPciIoProtocolGuid, IsVirtioPciRng, Connect);
bdb79c
 }
bdb79c
 
bdb79c
 /**
bdb79c
diff --git a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
bdb79c
index 1e22f8b..d6c1ef9 100644
bdb79c
--- a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
bdb79c
+++ b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
bdb79c
@@ -83,3 +83,4 @@
bdb79c
   gEfiLoadedImageProtocolGuid
bdb79c
   gEfiPciRootBridgeIoProtocolGuid
bdb79c
   gEfiSimpleFileSystemProtocolGuid
bdb79c
+  gVirtioDeviceProtocolGuid
bdb79c
-- 
bdb79c
1.8.3.1
bdb79c