Blame SOURCES/ovmf-OvmfPkg-PlatformBootManagerLib-connect-Virtio-RNG-de.patch

6ef48d
From 4c7e315ccb97dd7c3dc7f38e22b84ffbc4df90e3 Mon Sep 17 00:00:00 2001
6ef48d
From: Laszlo Ersek <lersek@redhat.com>
6ef48d
Date: Fri, 18 May 2018 21:40:24 +0200
6ef48d
Subject: [PATCH 5/5] OvmfPkg/PlatformBootManagerLib: connect Virtio RNG
6ef48d
 devices again
6ef48d
6ef48d
Message-id: <20180518194024.30614-3-lersek@redhat.com>
6ef48d
Patchwork-id: 80427
6ef48d
O-Subject:  [RHEL-7.6 ovmf PATCH 2/2] OvmfPkg/PlatformBootManagerLib: connect
6ef48d
	Virtio RNG devices again
6ef48d
Bugzilla: 1579518
6ef48d
Acked-by: Thomas Huth <thuth@redhat.com>
6ef48d
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
6ef48d
6ef48d
Virtio RNG devices are never boot devices, so in commit 245c643cc8b7 we
6ef48d
stopped connecting them. This is a problem because an OS boot loader may
6ef48d
depend on EFI_RNG_PROTOCOL to seed the OS's RNG.
6ef48d
6ef48d
Connect Virtio RNG devices again. And, while commit 245c643cc8b7 removed
6ef48d
that from PlatformBootManagerAfterConsole(), reintroduce it now to
6ef48d
PlatformBootManagerBeforeConsole() -- this way Driver#### options launched
6ef48d
between both functions may access EFI_RNG_PROTOCOL too.
6ef48d
6ef48d
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
6ef48d
Cc: Jordan Justen <jordan.l.justen@intel.com>
6ef48d
Fixes: 245c643cc8b73240c3b88cb55b2911b285a8c10d
6ef48d
Ref: https://bugzilla.redhat.com/show_bug.cgi?id=1579518
6ef48d
Contributed-under: TianoCore Contribution Agreement 1.1
6ef48d
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
6ef48d
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
6ef48d
(cherry picked from commit 7ebad830d6ab61f0395f6f4bae4156664bbd8086)
6ef48d
---
6ef48d
 .../Library/PlatformBootManagerLib/BdsPlatform.c   | 105 +++++++++++++++++++++
6ef48d
 .../Library/PlatformBootManagerLib/BdsPlatform.h   |   1 +
6ef48d
 2 files changed, 106 insertions(+)
6ef48d
6ef48d
diff --git a/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c b/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
6ef48d
index 004b753..5d4d323 100644
6ef48d
--- a/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
6ef48d
+++ b/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
6ef48d
@@ -319,6 +319,15 @@ ConnectRootBridge (
6ef48d
   );
6ef48d
 
6ef48d
 STATIC
6ef48d
+EFI_STATUS
6ef48d
+EFIAPI
6ef48d
+ConnectVirtioPciRng (
6ef48d
+  IN EFI_HANDLE Handle,
6ef48d
+  IN VOID       *Instance,
6ef48d
+  IN VOID       *Context
6ef48d
+  );
6ef48d
+
6ef48d
+STATIC
6ef48d
 VOID
6ef48d
 SaveS3BootScript (
6ef48d
   VOID
6ef48d
@@ -399,6 +408,13 @@ PlatformBootManagerBeforeConsole (
6ef48d
   ASSERT_RETURN_ERROR (PcdStatus);
6ef48d
 
6ef48d
   PlatformRegisterOptionsAndKeys ();
6ef48d
+
6ef48d
+  //
6ef48d
+  // Install both VIRTIO_DEVICE_PROTOCOL and (dependent) EFI_RNG_PROTOCOL
6ef48d
+  // instances on Virtio PCI RNG devices.
6ef48d
+  //
6ef48d
+  VisitAllInstancesOfProtocol (&gEfiPciIoProtocolGuid, ConnectVirtioPciRng,
6ef48d
+    NULL);
6ef48d
 }
6ef48d
 
6ef48d
 
6ef48d
@@ -427,6 +443,95 @@ ConnectRootBridge (
6ef48d
 }
6ef48d
 
6ef48d
 
6ef48d
+STATIC
6ef48d
+EFI_STATUS
6ef48d
+EFIAPI
6ef48d
+ConnectVirtioPciRng (
6ef48d
+  IN EFI_HANDLE Handle,
6ef48d
+  IN VOID       *Instance,
6ef48d
+  IN VOID       *Context
6ef48d
+  )
6ef48d
+{
6ef48d
+  EFI_PCI_IO_PROTOCOL *PciIo;
6ef48d
+  EFI_STATUS          Status;
6ef48d
+  UINT16              VendorId;
6ef48d
+  UINT16              DeviceId;
6ef48d
+  UINT8               RevisionId;
6ef48d
+  BOOLEAN             Virtio10;
6ef48d
+  UINT16              SubsystemId;
6ef48d
+
6ef48d
+  PciIo = Instance;
6ef48d
+
6ef48d
+  //
6ef48d
+  // Read and check VendorId.
6ef48d
+  //
6ef48d
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_VENDOR_ID_OFFSET,
6ef48d
+                        1, &VendorId);
6ef48d
+  if (EFI_ERROR (Status)) {
6ef48d
+    goto Error;
6ef48d
+  }
6ef48d
+  if (VendorId != VIRTIO_VENDOR_ID) {
6ef48d
+    return EFI_SUCCESS;
6ef48d
+  }
6ef48d
+
6ef48d
+  //
6ef48d
+  // Read DeviceId and RevisionId.
6ef48d
+  //
6ef48d
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_DEVICE_ID_OFFSET,
6ef48d
+                        1, &DeviceId);
6ef48d
+  if (EFI_ERROR (Status)) {
6ef48d
+    goto Error;
6ef48d
+  }
6ef48d
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, PCI_REVISION_ID_OFFSET,
6ef48d
+                        1, &RevisionId);
6ef48d
+  if (EFI_ERROR (Status)) {
6ef48d
+    goto Error;
6ef48d
+  }
6ef48d
+
6ef48d
+  //
6ef48d
+  // From DeviceId and RevisionId, determine whether the device is a
6ef48d
+  // modern-only Virtio 1.0 device. In case of Virtio 1.0, DeviceId can
6ef48d
+  // immediately be restricted to VIRTIO_SUBSYSTEM_ENTROPY_SOURCE, and
6ef48d
+  // SubsystemId will only play a sanity-check role. Otherwise, DeviceId can
6ef48d
+  // only be sanity-checked, and SubsystemId will decide.
6ef48d
+  //
6ef48d
+  if (DeviceId == 0x1040 + VIRTIO_SUBSYSTEM_ENTROPY_SOURCE &&
6ef48d
+      RevisionId >= 0x01) {
6ef48d
+    Virtio10 = TRUE;
6ef48d
+  } else if (DeviceId >= 0x1000 && DeviceId <= 0x103F && RevisionId == 0x00) {
6ef48d
+    Virtio10 = FALSE;
6ef48d
+  } else {
6ef48d
+    return EFI_SUCCESS;
6ef48d
+  }
6ef48d
+
6ef48d
+  //
6ef48d
+  // Read and check SubsystemId as dictated by Virtio10.
6ef48d
+  //
6ef48d
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16,
6ef48d
+                        PCI_SUBSYSTEM_ID_OFFSET, 1, &SubsystemId);
6ef48d
+  if (EFI_ERROR (Status)) {
6ef48d
+    goto Error;
6ef48d
+  }
6ef48d
+  if ((Virtio10 && SubsystemId >= 0x40) ||
6ef48d
+      (!Virtio10 && SubsystemId == VIRTIO_SUBSYSTEM_ENTROPY_SOURCE)) {
6ef48d
+    Status = gBS->ConnectController (
6ef48d
+                    Handle, // ControllerHandle
6ef48d
+                    NULL,   // DriverImageHandle -- connect all drivers
6ef48d
+                    NULL,   // RemainingDevicePath -- produce all child handles
6ef48d
+                    FALSE   // Recursive -- don't follow child handles
6ef48d
+                    );
6ef48d
+    if (EFI_ERROR (Status)) {
6ef48d
+      goto Error;
6ef48d
+    }
6ef48d
+  }
6ef48d
+  return EFI_SUCCESS;
6ef48d
+
6ef48d
+Error:
6ef48d
+  DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));
6ef48d
+  return Status;
6ef48d
+}
6ef48d
+
6ef48d
+
6ef48d
 /**
6ef48d
   Add IsaKeyboard to ConIn; add IsaSerial to ConOut, ConIn, ErrOut.
6ef48d
 
6ef48d
diff --git a/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.h b/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.h
6ef48d
index 97ffbb5..4948ca6 100644
6ef48d
--- a/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.h
6ef48d
+++ b/OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.h
6ef48d
@@ -30,6 +30,7 @@ Abstract:
6ef48d
 #include <IndustryStandard/Acpi.h>
6ef48d
 #include <IndustryStandard/SmBios.h>
6ef48d
 #include <IndustryStandard/PeImage.h>
6ef48d
+#include <IndustryStandard/Virtio095.h>
6ef48d
 
6ef48d
 #include <Library/DebugLib.h>
6ef48d
 #include <Library/BaseMemoryLib.h>
6ef48d
-- 
6ef48d
1.8.3.1
6ef48d