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

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