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

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