From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web11.10621.1590080352745462960 for ; Thu, 21 May 2020 09:59:13 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: ard.biesheuvel@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 333B430E; Thu, 21 May 2020 09:59:11 -0700 (PDT) Received: from e123331-lin.nice.arm.com (unknown [10.37.8.250]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 47B6F3F68F; Thu, 21 May 2020 09:59:10 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: leif@nuviainc.com, Ard Biesheuvel Subject: [PATCH edk2-platforms 1/1] Silicon/ChaosKeyDxe: don't rely on connect all controllers Date: Thu, 21 May 2020 18:59:06 +0200 Message-Id: <20200521165906.23205-1-ard.biesheuvel@arm.com> X-Mailer: git-send-email 2.17.1 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 --- 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 #include +#include #include #include +#include #include #include 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