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

cc9195
From 402cfd944f9a6764daec96aef9eec4d2393c3a90 Mon Sep 17 00:00:00 2001
cc9195
From: Laszlo Ersek <lersek@redhat.com>
cc9195
Date: Fri, 18 May 2018 21:40:23 +0200
cc9195
Subject: ArmVirtPkg/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-2-lersek@redhat.com>
cc9195
Patchwork-id: 80426
cc9195
O-Subject:  [RHEL-7.6 ovmf PATCH 1/2] ArmVirtPkg/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 ff1d0fbfbaec 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 ff1d0fbfbaec 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
Fixes: ff1d0fbfbaec55038ccf888759588fa4e21516f4
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 c4add6b6e971e0bb3f276ed3636a083e782e96cc)
cc9195
(cherry picked from commit 1d33f4bb28e1aa2c4d62979596140c22677a2e9f)
cc9195
---
cc9195
 .../Library/PlatformBootManagerLib/PlatformBm.c    | 129 +++++++++++++++++++++
cc9195
 .../PlatformBootManagerLib.inf                     |   1 +
cc9195
 2 files changed, 130 insertions(+)
cc9195
cc9195
diff --git a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
cc9195
index 5d5e51d..62cce6a 100644
cc9195
--- a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
cc9195
+++ b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
cc9195
@@ -16,6 +16,7 @@
cc9195
 **/
cc9195
 
cc9195
 #include <IndustryStandard/Pci22.h>
cc9195
+#include <IndustryStandard/Virtio095.h>
cc9195
 #include <Library/BootLogoLib.h>
cc9195
 #include <Library/DevicePathLib.h>
cc9195
 #include <Library/PcdLib.h>
cc9195
@@ -27,6 +28,7 @@
cc9195
 #include <Protocol/LoadedImage.h>
cc9195
 #include <Protocol/PciIo.h>
cc9195
 #include <Protocol/PciRootBridgeIo.h>
cc9195
+#include <Protocol/VirtioDevice.h>
cc9195
 #include <Guid/EventGroup.h>
cc9195
 #include <Guid/RootBridgesConnectedEventGroup.h>
cc9195
 
cc9195
@@ -261,6 +263,121 @@ IsPciDisplay (
cc9195
 
cc9195
 
cc9195
 /**
cc9195
+  This FILTER_FUNCTION checks if a handle corresponds to a Virtio RNG device at
cc9195
+  the VIRTIO_DEVICE_PROTOCOL level.
cc9195
+**/
cc9195
+STATIC
cc9195
+BOOLEAN
cc9195
+EFIAPI
cc9195
+IsVirtioRng (
cc9195
+  IN EFI_HANDLE   Handle,
cc9195
+  IN CONST CHAR16 *ReportText
cc9195
+  )
cc9195
+{
cc9195
+  EFI_STATUS             Status;
cc9195
+  VIRTIO_DEVICE_PROTOCOL *VirtIo;
cc9195
+
cc9195
+  Status = gBS->HandleProtocol (Handle, &gVirtioDeviceProtocolGuid,
cc9195
+                  (VOID**)&VirtIo);
cc9195
+  if (EFI_ERROR (Status)) {
cc9195
+    return FALSE;
cc9195
+  }
cc9195
+  return (BOOLEAN)(VirtIo->SubSystemDeviceId ==
cc9195
+                   VIRTIO_SUBSYSTEM_ENTROPY_SOURCE);
cc9195
+}
cc9195
+
cc9195
+
cc9195
+/**
cc9195
+  This FILTER_FUNCTION checks if a handle corresponds to a Virtio RNG device at
cc9195
+  the EFI_PCI_IO_PROTOCOL level.
cc9195
+**/
cc9195
+STATIC
cc9195
+BOOLEAN
cc9195
+EFIAPI
cc9195
+IsVirtioPciRng (
cc9195
+  IN EFI_HANDLE   Handle,
cc9195
+  IN CONST CHAR16 *ReportText
cc9195
+  )
cc9195
+{
cc9195
+  EFI_STATUS          Status;
cc9195
+  EFI_PCI_IO_PROTOCOL *PciIo;
cc9195
+  UINT16              VendorId;
cc9195
+  UINT16              DeviceId;
cc9195
+  UINT8               RevisionId;
cc9195
+  BOOLEAN             Virtio10;
cc9195
+  UINT16              SubsystemId;
cc9195
+
cc9195
+  Status = gBS->HandleProtocol (Handle, &gEfiPciIoProtocolGuid,
cc9195
+                  (VOID**)&PciIo);
cc9195
+  if (EFI_ERROR (Status)) {
cc9195
+    return FALSE;
cc9195
+  }
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 PciError;
cc9195
+  }
cc9195
+  if (VendorId != VIRTIO_VENDOR_ID) {
cc9195
+    return FALSE;
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 PciError;
cc9195
+  }
cc9195
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, PCI_REVISION_ID_OFFSET,
cc9195
+                        1, &RevisionId);
cc9195
+  if (EFI_ERROR (Status)) {
cc9195
+    goto PciError;
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 FALSE;
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 PciError;
cc9195
+  }
cc9195
+  if (Virtio10 && SubsystemId >= 0x40) {
cc9195
+    return TRUE;
cc9195
+  }
cc9195
+  if (!Virtio10 && SubsystemId == VIRTIO_SUBSYSTEM_ENTROPY_SOURCE) {
cc9195
+    return TRUE;
cc9195
+  }
cc9195
+  return FALSE;
cc9195
+
cc9195
+PciError:
cc9195
+  DEBUG ((DEBUG_ERROR, "%a: %s: %r\n", __FUNCTION__, ReportText, Status));
cc9195
+  return FALSE;
cc9195
+}
cc9195
+
cc9195
+
cc9195
+/**
cc9195
   This CALLBACK_FUNCTION attempts to connect a handle non-recursively, asking
cc9195
   the matching driver to produce all first-level child handles.
cc9195
 **/
cc9195
@@ -644,6 +761,18 @@ PlatformBootManagerBeforeConsole (
cc9195
   // Register platform-specific boot options and keyboard shortcuts.
cc9195
   //
cc9195
   PlatformRegisterOptionsAndKeys ();
cc9195
+
cc9195
+  //
cc9195
+  // At this point, VIRTIO_DEVICE_PROTOCOL instances exist only for Virtio MMIO
cc9195
+  // transports. Install EFI_RNG_PROTOCOL instances on Virtio MMIO RNG devices.
cc9195
+  //
cc9195
+  FilterAndProcess (&gVirtioDeviceProtocolGuid, IsVirtioRng, Connect);
cc9195
+
cc9195
+  //
cc9195
+  // Install both VIRTIO_DEVICE_PROTOCOL and (dependent) EFI_RNG_PROTOCOL
cc9195
+  // instances on Virtio PCI RNG devices.
cc9195
+  //
cc9195
+  FilterAndProcess (&gEfiPciIoProtocolGuid, IsVirtioPciRng, Connect);
cc9195
 }
cc9195
 
cc9195
 /**
cc9195
diff --git a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
cc9195
index 1e22f8b..d6c1ef9 100644
cc9195
--- a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
cc9195
+++ b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
cc9195
@@ -83,3 +83,4 @@
cc9195
   gEfiLoadedImageProtocolGuid
cc9195
   gEfiPciRootBridgeIoProtocolGuid
cc9195
   gEfiSimpleFileSystemProtocolGuid
cc9195
+  gVirtioDeviceProtocolGuid
cc9195
-- 
cc9195
1.8.3.1
cc9195