public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH edk2-platforms 1/1] Silicon/ChaosKeyDxe: don't rely on connect all controllers
@ 2020-05-21 16:59 Ard Biesheuvel
  0 siblings, 0 replies; only message in thread
From: Ard Biesheuvel @ 2020-05-21 16:59 UTC (permalink / raw)
  To: devel; +Cc: leif, Ard Biesheuvel

The ChaosKey driver implements the UEFI driver model, and so it is
not guaranteed that any controllers will be attached to this driver
unless it is connected explicitly. On many platforms today, this is
taken care of by the ConnectAll() call that occurs in the BDS, but
this is not something we should rely on.

So add a protocol notification event that will attempt to connect
the ChaosKey on each USB host controller that is registered, by
connecting it to the short-form USB class device path describing the
ChaosKey hardware by vendor/product ID. This still relies on the USB
host controllers to be connected by the platform, which is typically
the case (given that a USB keyboard is required to interrupt the boot)

On platforms where USB is not connected at all by default, it is really
not up to a third party driver to meddle with this, and so waiting for
the USB host controller is the best we can do. Note that third party
drivers registered via Driver#### can set a 'reconnect all' flag if
needed to mitigate this.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
---
 Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf  |  1 +
 Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h |  2 +
 Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c  | 83 ++++++++++++++++++++
 3 files changed, 86 insertions(+)

diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf
index 420310634d16..1548e0485766 100644
--- a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf
+++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf
@@ -36,6 +36,7 @@ [LibraryClasses]
 
 [Protocols]
   gEfiRngProtocolGuid                 # PROTOCOL BY_START
+  gEfiUsb2HcProtocolGuid              # CONSUMES
   gEfiUsbIoProtocolGuid               # PROTOCOL TO_START
 
 [Guids]
diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h
index 97cfbbb7556e..c84e3ca00cc1 100644
--- a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h
+++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h
@@ -12,9 +12,11 @@
 
 #include <Uefi.h>
 #include <Library/DebugLib.h>
+#include <Library/DevicePathLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/UefiLib.h>
 
+#include <Protocol/DevicePath.h>
 #include <Protocol/Rng.h>
 #include <Protocol/UsbIo.h>
 
diff --git a/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c
index e7d0d3fe563e..6d4993e8de95 100644
--- a/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c
+++ b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c
@@ -11,6 +11,75 @@
 
 #include "ChaosKeyDriver.h"
 
+#pragma pack (1)
+typedef struct {
+  USB_CLASS_DEVICE_PATH    Keyboard;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} CHAOSKEY_USB_DEVICE_PATH;
+#pragma pack ()
+
+STATIC CHAOSKEY_USB_DEVICE_PATH   mChaosKeyDevicePath = {
+  {
+    {
+      MESSAGING_DEVICE_PATH, MSG_USB_CLASS_DP,
+      {
+        (UINT8)sizeof (USB_CLASS_DEVICE_PATH),
+        (UINT8)(sizeof (USB_CLASS_DEVICE_PATH) >> 8)
+      }
+    },
+    CHAOSKEY_VENDOR_ID,   // VendorId
+    CHAOSKEY_PRODUCT_ID,  // ProductId
+    0xff,                 // DeviceClass: any
+    0xff,                 // DeviceSubClass: any
+    0xff,                 // DeviceProtocol: any
+  }, {
+    END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+    {
+      (UINT8)sizeof (EFI_DEVICE_PATH_PROTOCOL),
+      (UINT8)(sizeof (EFI_DEVICE_PATH_PROTOCOL) >> 8)
+    }
+  }
+};
+
+STATIC VOID         *mProtocolNotifyRegistration;
+STATIC EFI_EVENT    mProtocolNotifyRegistrationEvent;
+
+STATIC
+VOID
+EFIAPI
+OnProtocolNotify (
+  IN EFI_EVENT            Event,
+  IN VOID                 *Context
+  )
+{
+  EFI_STATUS              Status;
+  EFI_HANDLE              *Handles;
+  UINTN                   HandleCount;
+  UINTN                   Index;
+
+  Status = gBS->LocateHandleBuffer (ByRegisterNotify, NULL,
+                  mProtocolNotifyRegistration, &HandleCount, &Handles);
+  if (EFI_ERROR (Status)) {
+    if (Status != EFI_NOT_FOUND) {
+      DEBUG ((DEBUG_WARN, "%a: LocateHandleBuffer() failed - %r\n",
+        __FUNCTION__, Status));
+      }
+    return;
+  }
+
+  for (Index = 0; Index < HandleCount; Index++) {
+    //
+    // Attempt to connect the USB device path describing the ChaosKey
+    // hardware via the handle describing a USB host controller.
+    //
+    Status = gBS->ConnectController (Handles[Index], NULL,
+                    (EFI_DEVICE_PATH_PROTOCOL *)&mChaosKeyDevicePath, FALSE);
+    DEBUG ((DEBUG_VERBOSE, "%a: ConnectController () returned %r\n",
+      __FUNCTION__, Status));
+  }
+  gBS->FreePool (Handles);
+}
+
 /**
   Tests to see if this driver supports a given controller.
 
@@ -185,6 +254,18 @@ EntryPoint (
              NULL, &gChaosKeyDriverComponentName2);
   ASSERT_EFI_ERROR (Status);
 
+  //
+  // This driver produces the EFI Random Number Generator protocol on
+  // compatible USB I/O handles, which is not a protocol that can provide
+  // a boot target. This means that it will not get connected on an ordinary
+  // 'fast' boot (which only connects the console and boot entry device paths)
+  // unless we take extra measures.
+  //
+  mProtocolNotifyRegistrationEvent = EfiCreateProtocolNotifyEvent (
+                                       &gEfiUsb2HcProtocolGuid, TPL_CALLBACK,
+                                       OnProtocolNotify, NULL,
+                                       &mProtocolNotifyRegistration);
+
   DEBUG ((DEBUG_INIT | DEBUG_INFO, "*** Installed ChaosKey driver! ***\n"));
 
   return EFI_SUCCESS;
@@ -211,6 +292,8 @@ UnloadImage (
   UINTN       HandleCount;
   UINTN       Index;
 
+  gBS->CloseEvent (mProtocolNotifyRegistrationEvent);
+
   //
   // Retrieve all USB I/O handles in the handle database
   //
-- 
2.17.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-05-21 16:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-05-21 16:59 [PATCH edk2-platforms 1/1] Silicon/ChaosKeyDxe: don't rely on connect all controllers Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox