diff mbox

[edk2,edk2-platforms] Silicon/Openmoko: add driver for ChaosKey RNG USB device

Message ID 20170823131206.25008-1-ard.biesheuvel@linaro.org
State Superseded
Headers show

Commit Message

Ard Biesheuvel Aug. 23, 2017, 1:12 p.m. UTC
This is a continuation of the work carried out by Leif Lindholm to
implement a driver for the ChaosKey USB device. This driver uses the
UEFI driver model, which is a slightly awkward fit, due to the fact
that a UEFI implementation may legally only instantiate those protocols
that are needed to access the device path that the active Boot####
options refers to.

However, it is expected that UEFI implementations typically instantiate
all USB I/O protocols and connect them as well, as those are required
for a USB keyboard to be able to control the boot sequence. This should
result in this driver being connected and given the opportunity to
produce the EFI_RNG_PROTOCOL.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c | 346 ++++++++++++++++++++
 Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h |  61 ++++
 Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf  |  48 +++
 Silicon/Openmoko/ChaosKeyDxe/ComponentName.c  | 205 ++++++++++++
 Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c  | 256 +++++++++++++++
 5 files changed, 916 insertions(+)

-- 
2.11.0

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Comments

Leif Lindholm Aug. 23, 2017, 11:48 p.m. UTC | #1
On Wed, Aug 23, 2017 at 02:12:06PM +0100, Ard Biesheuvel wrote:
> This is a continuation of the work carried out by Leif Lindholm to

> implement a driver for the ChaosKey USB device. This driver uses the

> UEFI driver model, which is a slightly awkward fit, due to the fact

> that a UEFI implementation may legally only instantiate those protocols

> that are needed to access the device path that the active Boot####

> options refers to.

> 

> However, it is expected that UEFI implementations typically instantiate

> all USB I/O protocols and connect them as well, as those are required

> for a USB keyboard to be able to control the boot sequence. This should

> result in this driver being connected and given the opportunity to

> produce the EFI_RNG_PROTOCOL.

> 

> Contributed-under: TianoCore Contribution Agreement 1.1

> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

> ---

>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c | 346 ++++++++++++++++++++

>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h |  61 ++++

>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf  |  48 +++

>  Silicon/Openmoko/ChaosKeyDxe/ComponentName.c  | 205 ++++++++++++

>  Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c  | 256 +++++++++++++++

>  5 files changed, 916 insertions(+)


This is all a bit new territory, so I don't know what the best way of
dealing with this is, but I think we could do with a .dsc somewhere to
enable building this driver standalone. My gut feel is that an
Openmoko/Openmoko.dsc would be a reasonable candidate.

> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c

> new file mode 100644

> index 000000000000..1870080d2c70

> --- /dev/null

> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c

> @@ -0,0 +1,346 @@

> +/** @file

> +  Device driver for the ChaosKey hardware random number generator.

> +

> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

> +

> +  This program and the accompanying materials

> +  are licensed and made available under the terms and conditions of the BSD

> +  License which accompanies this distribution. The full text of the license may

> +  be found at  http://opensource.org/licenses/bsd-license.php.

> +

> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> +

> +**/

> +

> +#include "ChaosKeyDriver.h"

> +

> +#include <Library/BaseMemoryLib.h>

> +#include <Library/DebugLib.h>

> +#include <Library/MemoryAllocationLib.h>

> +

> +STATIC

> +BOOLEAN

> +IsBulkInEndpoint (

> +  IN  EFI_USB_ENDPOINT_DESCRIPTOR     *Endpoint

> +  )

> +{

> +  if ((Endpoint->Attributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_BULK) {

> +    if (Endpoint->EndpointAddress & USB_ENDPOINT_DIR_IN) {

> +      return TRUE;

> +    }

> +  }

> +  return FALSE;

> +}

> +

> +

> +STATIC

> +EFI_STATUS

> +FindEndpoint (

> +  IN  CHAOSKEY_DEV *ChaosKey

> +  )

> +{

> +  EFI_USB_IO_PROTOCOL             *UsbIo;

> +  EFI_STATUS                      Status;

> +  UINTN                           Index;

> +  EFI_USB_INTERFACE_DESCRIPTOR    InterfaceDescriptor;

> +

> +  UsbIo = ChaosKey->UsbIo;

> +

> +  //

> +  // Get interface & endpoint descriptor

> +  //

> +  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);

> +  if (EFI_ERROR (Status)) {

> +    return Status;

> +  }

> +

> +  //

> +  // The ChaosKey provides two endpoints:

> +  // - The first one is the 'cooked' one, to be used as random data input

> +  // - The second one is the raw bitstream from the generator, higher

> +  //   throughput, but lower randomness.

> +  // So locate the first bulk IN endpoint and save it for later use.

> +  //

> +  for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {

> +    EFI_USB_ENDPOINT_DESCRIPTOR  Endpoint;

> +

> +    Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &Endpoint);

> +    if (EFI_ERROR (Status)) {

> +      DEBUG ((DEBUG_ERROR, "UsbGetEndPointDescriptor(%d) failed!\n", Index));

> +      return Status;

> +    }

> +

> +    if (IsBulkInEndpoint(&Endpoint)) {

> +      ChaosKey->EndpointAddress = Endpoint.EndpointAddress;

> +      ChaosKey->EndpointSize = Endpoint.MaxPacketSize;

> +      return EFI_SUCCESS;

> +    }

> +  }

> +

> +  DEBUG ((DEBUG_ERROR, "Failed to locate suitable BULK IN USB endpoint!\n"));

> +  return EFI_DEVICE_ERROR;

> +}

> +

> +

> +/**

> +  Returns information about the random number generation implementation.

> +

> +  @param[in]     This               A pointer to the EFI_RNG_PROTOCOL instance.

> +  @param[in,out] AlgorithmListSize  On input, the size in bytes of AlgorithmList

> +                                    On output with a return code of EFI_SUCCESS,

> +                                    the size in bytes of the data returned in

> +                                    AlgorithmList. On output with a return

> +                                    code of EFI_BUFFER_TOO_SMALL, the size of

> +                                    AlgorithmList required to obtain the list.

> +  @param[out] AlgorithmList         A caller-allocated memory buffer filled by

> +                                    the driver with one EFI_RNG_ALGORITHM

> +                                    element for each supported RNG algorithm.

> +                                    The list must not change across multiple

> +                                    calls to the same driver. The first

> +                                    algorithm in the list is the default

> +                                    algorithm for the driver.

> +

> +  @retval EFI_SUCCESS               The RNG algorithm list was returned

> +                                    successfully.

> +  @retval EFI_UNSUPPORTED           The services is not supported by this driver

> +  @retval EFI_DEVICE_ERROR          The list of algorithms could not be

> +                                    retrieved due to a hardware or firmware

> +                                    error.

> +  @retval EFI_INVALID_PARAMETER     One or more of the parameters are incorrect.

> +  @retval EFI_BUFFER_TOO_SMALL      The buffer RNGAlgorithmList is too small to

> +                                    hold the result.

> +

> +**/

> +STATIC

> +EFI_STATUS

> +EFIAPI

> +GetInfo (

> +  IN      EFI_RNG_PROTOCOL    *This,

> +  IN  OUT UINTN               *AlgorithmListSize,

> +  OUT     EFI_RNG_ALGORITHM   *AlgorithmList

> +)

> +{

> +  UINTN Size;

> +

> +  //

> +  // We only implement the raw algorithm

> +  //

> +  Size = sizeof gEfiRngAlgorithmRaw;

> +

> +  if (*AlgorithmListSize < Size) {

> +    *AlgorithmListSize = Size;

> +    return EFI_BUFFER_TOO_SMALL;

> +  }

> +

> +  gBS->CopyMem (AlgorithmList, &gEfiRngAlgorithmRaw, Size);

> +  *AlgorithmListSize = Size;

> +

> +  return EFI_SUCCESS;

> +}

> +

> +

> +/**

> +  Produces and returns an RNG value using either the default or specified RNG

> +  algorithm.

> +

> +  @param[in]  This                A pointer to the EFI_RNG_PROTOCOL instance.

> +  @param[in]  Algorithm           A pointer to the EFI_RNG_ALGORITHM that

> +                                  identifies the RNG algorithm to use. May be

> +                                  NULL in which case the function will use its

> +                                  default RNG algorithm.

> +  @param[in]  ValueLength         The length in bytes of the memory buffer

> +                                  pointed to by RNGValue. The driver shall

> +                                  return exactly this numbers of bytes.

> +  @param[out] Value               A caller-allocated memory buffer filled by the

> +                                  driver with the resulting RNG value.

> +

> +  @retval EFI_SUCCESS             The RNG value was returned successfully.

> +  @retval EFI_UNSUPPORTED         The algorithm specified by RNGAlgorithm is not

> +                                  supported by this driver.

> +  @retval EFI_DEVICE_ERROR        An RNG value could not be retrieved due to a

> +                                  hardware or firmware error.

> +  @retval EFI_NOT_READY           There is not enough random data available to

> +                                  satisfy the length requested by

> +                                  RNGValueLength.

> +  @retval EFI_INVALID_PARAMETER   RNGValue is NULL or RNGValueLength is zero.

> +

> +**/

> +STATIC

> +EFI_STATUS

> +EFIAPI

> +GetRNG (

> +  IN EFI_RNG_PROTOCOL   *This,

> +  IN EFI_RNG_ALGORITHM  *Algorithm OPTIONAL,

> +  IN UINTN              ValueLength,

> +  OUT UINT8             *Value

> +)

> +{

> +  EFI_STATUS        Status;

> +  CHAOSKEY_DEV      *ChaosKey;

> +  EFI_TPL           Tpl;

> +  UINT8             Buffer[CHAOSKEY_MAX_EP_SIZE];

> +  UINT8             *OutPointer;

> +  UINTN             OutSize;

> +  UINT32            Result;

> +

> +  if (Algorithm != NULL && !CompareGuid (Algorithm, &gEfiRngAlgorithmRaw)) {

> +    return EFI_UNSUPPORTED;

> +  }

> +

> +  ChaosKey = CHAOSKEY_DEV_FROM_THIS (This);

> +

> +  while (ValueLength > 0) {

> +    //

> +    // If more data is requested than the endpoint can deliver in a single

> +    // transfer, put it straight into the caller's buffer.

> +    //

> +    if (ValueLength >= ChaosKey->EndpointSize) {

> +      OutPointer = Value;

> +    } else {

> +      OutPointer = Buffer;

> +    }

> +    OutSize = ChaosKey->EndpointSize;

> +

> +    Tpl = gBS->RaiseTPL (TPL_NOTIFY);


You mentioned to me on IRC that this should not be necessary, and that
UsbIo should adjust tpl as necessary.

It is quite likely I misinterpreted behaviour I encountered when using
USB passthrough in QEMU for testing (and managed to break the host
system's driver).

> +

> +    Status = ChaosKey->UsbIo->UsbBulkTransfer (ChaosKey->UsbIo,

> +                                               ChaosKey->EndpointAddress,

> +                                               OutPointer,

> +                                               &OutSize,

> +                                               CHAOSKEY_TIMEOUT,

> +                                               &Result);

> +

> +    gBS->RestoreTPL (Tpl);

> +

> +    if (Status == EFI_TIMEOUT) {

> +      DEBUG ((DEBUG_ERROR, "Bulk transfer timed out, USB status == %d\n",

> +        Result));

> +      return EFI_NOT_READY;

> +    } else if (EFI_ERROR (Status)) {

> +      DEBUG ((DEBUG_ERROR,

> +        "Bulk transfer failed, Status == %r, USB status == %d\n",

> +        Status, Result));

> +      return EFI_DEVICE_ERROR;

> +    }

> +

> +    OutSize = MIN (OutSize, ValueLength);

> +

> +    if (Value != Buffer) {

> +      gBS->CopyMem (Value, Buffer, OutSize);

> +    }

> +    Value += OutSize;

> +    ValueLength -= OutSize;

> +  }

> +  return EFI_SUCCESS;

> +}

> +

> +

> +EFI_STATUS

> +ChaosKeyInit (

> +  IN      EFI_HANDLE        DriverBindingHandle,

> +  IN      EFI_HANDLE        ControllerHandle

> +  )

> +{

> +  EFI_STATUS                Status;

> +  CHAOSKEY_DEV              *ChaosKey;

> +

> +  Status  = gBS->AllocatePool (EfiBootServicesData,

> +                               sizeof (CHAOSKEY_DEV),

> +                               (VOID **) &ChaosKey);

> +  if (EFI_ERROR (Status)) {

> +    return EFI_OUT_OF_RESOURCES;

> +  }

> +

> +  ChaosKey->Signature         = CHAOSKEY_DEV_SIGNATURE;

> +  ChaosKey->Rng.GetInfo       = GetInfo;

> +  ChaosKey->Rng.GetRNG        = GetRNG;

> +

> +  //

> +  // Open USB I/O Protocol

> +  //

> +  Status = gBS->OpenProtocol (ControllerHandle,

> +                              &gEfiUsbIoProtocolGuid,

> +                              (VOID **)&ChaosKey->UsbIo,

> +                              DriverBindingHandle,

> +                              ControllerHandle,

> +                              EFI_OPEN_PROTOCOL_BY_DRIVER);

> +  if (EFI_ERROR (Status)) {

> +    goto ErrorFreeDev;

> +  }

> +

> +  Status = FindEndpoint (ChaosKey);

> +  if (EFI_ERROR (Status)) {

> +    goto ErrorCloseProtocol;

> +  }

> +

> +  //

> +  // The following can only occur if the Chaoskey is suddenly reissued

> +  // as a high speed or super speed device under the same VID/PID.

> +  //

> +  ASSERT (ChaosKey->EndpointSize <= CHAOSKEY_MAX_EP_SIZE);

> +

> +  Status = gBS->InstallProtocolInterface (&ControllerHandle,

> +                                          &gEfiRngProtocolGuid,

> +                                          EFI_NATIVE_INTERFACE,

> +                                          &ChaosKey->Rng);

> +  if (EFI_ERROR (Status)) {

> +    DEBUG ((DEBUG_ERROR,

> +      "Failed to install RNG protocol interface (Status == %r)\n",

> +    Status));

> +    goto ErrorCloseProtocol;

> +  }

> +

> +  return EFI_SUCCESS;

> +

> +ErrorCloseProtocol:

> +  gBS->CloseProtocol (ControllerHandle, &gEfiUsbIoProtocolGuid,

> +         DriverBindingHandle, ControllerHandle);

> +

> +ErrorFreeDev:

> +  gBS->FreePool (ChaosKey);

> +

> +  return Status;

> +}

> +

> +EFI_STATUS

> +ChaosKeyRelease (

> +  IN  EFI_HANDLE        DriverBindingHandle,

> +  IN  EFI_HANDLE        ControllerHandle

> +  )

> +{

> +  EFI_RNG_PROTOCOL    *Rng;

> +  CHAOSKEY_DEV        *ChaosKey;

> +  EFI_STATUS          Status;

> +

> +  Status = gBS->HandleProtocol (ControllerHandle,

> +                                &gEfiRngProtocolGuid,

> +                                (VOID **)&Rng);

> +  ASSERT_EFI_ERROR (Status);

> +  if (EFI_ERROR (Status)) {

> +    return Status;

> +  }

> +

> +  ChaosKey = CHAOSKEY_DEV_FROM_THIS (Rng);

> +

> +  Status = gBS->UninstallProtocolInterface (ControllerHandle,

> +                                            &gEfiRngProtocolGuid,

> +                                            Rng);

> +  ASSERT_EFI_ERROR (Status);

> +  if (EFI_ERROR (Status)) {

> +    return Status;

> +  }

> +

> +  Status = gBS->CloseProtocol (ControllerHandle,

> +                               &gEfiUsbIoProtocolGuid,

> +                               DriverBindingHandle,

> +                               ControllerHandle);

> +  ASSERT_EFI_ERROR (Status);

> +  if (EFI_ERROR (Status)) {

> +    return Status;

> +  }

> +

> +  gBS->FreePool (ChaosKey);

> +

> +  return EFI_SUCCESS;

> +}

> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h

> new file mode 100644

> index 000000000000..153deb4edb1c

> --- /dev/null

> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h

> @@ -0,0 +1,61 @@

> +/** @file

> +  Header file for the ChaosKey hardware random number generator.

> +

> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

> +

> +  This program and the accompanying materials

> +  are licensed and made available under the terms and conditions of the BSD

> +  License which accompanies this distribution. The full text of the license may

> +  be found at  http://opensource.org/licenses/bsd-license.php.

> +

> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> +

> +**/

> +

> +#ifndef _CHAOSKEY_USB_HWRNG_DRIVER_H_

> +#define _CHAOSKEY_USB_HWRNG_DRIVER_H_

> +

> +#include <Uefi.h>

> +#include <Library/DebugLib.h>

> +#include <Library/UefiLib.h>

> +#include <Library/UefiBootServicesTableLib.h>


B before L.

> +

> +#include <Protocol/Rng.h>

> +#include <Protocol/UsbIo.h>

> +

> +#define CHAOSKEY_VENDOR_ID      0x1d50  /* OpenMoko */

> +#define CHAOSKEY_PRODUCT_ID     0x60c6  /* ChaosKey */

> +

> +#define CHAOSKEY_TIMEOUT        10 // ms

> +#define CHAOSKEY_MAX_EP_SIZE    64 // max EP size for full-speed devices

> +

> +#define CHAOSKEY_DEV_SIGNATURE  SIGNATURE_32('c','h','k','e')

> +

> +typedef struct {

> +  UINT32                        Signature;

> +  UINT16                        EndpointAddress;

> +  UINT16                        EndpointSize;

> +  EFI_USB_IO_PROTOCOL           *UsbIo;

> +  EFI_RNG_PROTOCOL              Rng;

> +} CHAOSKEY_DEV;

> +

> +#define CHAOSKEY_DEV_FROM_THIS(a) \

> +  CR(a, CHAOSKEY_DEV, Rng, CHAOSKEY_DEV_SIGNATURE)

> +

> +extern EFI_COMPONENT_NAME_PROTOCOL gChaosKeyDriverComponentName;

> +extern EFI_COMPONENT_NAME2_PROTOCOL gChaosKeyDriverComponentName2;

> +

> +EFI_STATUS

> +ChaosKeyInit (

> +  IN  EFI_HANDLE        DriverBindingHandle,

> +  IN  EFI_HANDLE        ControllerHandle

> +  );

> +

> +EFI_STATUS

> +ChaosKeyRelease (

> +  IN  EFI_HANDLE        DriverBindingHandle,

> +  IN  EFI_HANDLE        ControllerHandle

> +  );

> +

> +#endif // _CHAOSKEY_USB_HWRNG_DRIVER_H_

> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf

> new file mode 100644

> index 000000000000..2ff84956ca72

> --- /dev/null

> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf

> @@ -0,0 +1,48 @@

> +## @file

> +#  Device driver for the ChaosKey hardware random number generator.

> +#

> +#  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

> +#

> +#  This program and the accompanying materials

> +#  are licensed and made available under the terms and conditions of the BSD

> +#  License which accompanies this distribution. The full text of the license may

> +#  be found at  http://opensource.org/licenses/bsd-license.php.

> +#

> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> +#

> +##

> +

> +[Defines]

> +  INF_VERSION                    = 0x00010019

> +  BASE_NAME                      = ChaosKeyDxe

> +  FILE_GUID                      = 9A54122B-F5E4-40D8-AE61-A71E406ED449

> +  MODULE_TYPE                    = UEFI_DRIVER

> +  VERSION_STRING                 = 1.0

> +  ENTRY_POINT                    = EntryPoint

> +  UNLOAD_IMAGE                   = UnloadImage

> +

> +#

> +#  VALID_ARCHITECTURES           = AARCH64 ARM EBC IA32 IPF X64

> +#

> +

> +[Sources]

> +  ChaosKeyDriver.c

> +  ChaosKeyDriver.h

> +  ComponentName.c

> +  DriverBinding.c

> +

> +[Packages]

> +  MdePkg/MdePkg.dec

> +

> +[LibraryClasses]

> +  UefiBootServicesTableLib

> +  UefiDriverEntryPoint

> +  UefiLib

> +

> +[Protocols]

> +  gEfiRngProtocolGuid                 # PROTOCOL BY_START

> +  gEfiUsbIoProtocolGuid               # PROTOCOL TO_START

> +

> +[Guids]

> +  gEfiRngAlgorithmRaw

> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c b/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c

> new file mode 100644

> index 000000000000..81f2130bcd9e

> --- /dev/null

> +++ b/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c


You mentioned on IRC that there's an accidental incomplete refactoring
in this file (and indeed the build fails as it stands).

> @@ -0,0 +1,205 @@

> +/** @file

> +  UEFI Component Name(2) protocol implementation for ChaosKey driver.

> +

> +  Copyright (c) 2017, Linaro Ltd. All rights reserved.

> +

> +  This program and the accompanying materials

> +  are licensed and made available under the terms and conditions of the BSD License

> +  which accompanies this distribution.  The full text of the license may be found at

> +  http://opensource.org/licenses/bsd-license.php

> +

> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> +

> +**/

> +

> +#include "ChaosKeyDriver.h"

> +

> +STATIC EFI_UNICODE_STRING_TABLE mChaosKeyDriverNameTable[] = {

> +  {

> +    "eng;en",

> +    (CHAR16 *)L"ChaosKey RNG USB driver"

> +  },

> +  {

> +    NULL,

> +    NULL

> +  }

> +};

> +

> +STATIC EFI_UNICODE_STRING_TABLE mChaosKeyControllerNameTable[] = {

> +  {

> +    "eng;en",

> +    (CHAR16 *)L"ChaosKey Random Number Generator (USB)"

> +  },

> +  {

> +    NULL,

> +    NULL

> +  }

> +};

> +

> +/**

> +  Retrieves a Unicode string that is the user readable name of the driver.

> +

> +  This function retrieves the user readable name of a driver in the form of a

> +  Unicode string. If the driver specified by This has a user readable name in

> +  the language specified by Language, then a pointer to the driver name is

> +  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified

> +  by This does not support the language specified by Language,

> +  then EFI_UNSUPPORTED is returned.

> +

> +  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or

> +                                EFI_COMPONENT_NAME_PROTOCOL instance.

> +

> +  @param  Language[in]          A pointer to a Null-terminated ASCII string

> +                                array indicating the language. This is the

> +                                language of the driver name that the caller is

> +                                requesting, and it must match one of the

> +                                languages specified in SupportedLanguages. The

> +                                number of languages supported by a driver is up

> +                                to the driver writer. Language is specified

> +                                in RFC 4646 or ISO 639-2 language code format.

> +

> +  @param  DriverName[out]       A pointer to the Unicode string to return.

> +                                This Unicode string is the name of the

> +                                driver specified by This in the language

> +                                specified by Language.

> +

> +  @retval EFI_SUCCESS           The Unicode string for the Driver specified by

> +                                This and the language specified by Language was

> +                                returned in DriverName.

> +

> +  @retval EFI_INVALID_PARAMETER Language is NULL.

> +

> +  @retval EFI_INVALID_PARAMETER DriverName is NULL.

> +

> +  @retval EFI_UNSUPPORTED       The driver specified by This does not support

> +                                the language specified by Language.

> +

> +**/

> +STATIC

> +EFI_STATUS

> +EFIAPI

> +ChaosKeyDriverName (

> +  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,

> +  IN  CHAR8                        *Language,

> +  OUT CHAR16                       **DriverName

> +  )

> +{

> +  return LookupUnicodeString2 (

> +           Language,

> +           This->SupportedLanguages,

> +           mChaosKeyDriverNameTable,

> +           DriverName,

> +           (BOOLEAN)(This == &gChaosKeyDriverComponentName)

> +           );

> +}

> +

> +/**

> +  Retrieves a Unicode string that is the user readable name of the controller

> +  that is being managed by a driver.

> +

> +  This function retrieves the user readable name of the controller specified by

> +  ControllerHandle and ChildHandle in the form of a Unicode string. If the

> +  driver specified by This has a user readable name in the language specified by

> +  Language, then a pointer to the controller name is returned in ControllerName,

> +  and EFI_SUCCESS is returned.  If the driver specified by This is not currently

> +  managing the controller specified by ControllerHandle and ChildHandle,

> +  then EFI_UNSUPPORTED is returned.  If the driver specified by This does not

> +  support the language specified by Language, then EFI_UNSUPPORTED is returned.

> +

> +  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or

> +                                EFI_COMPONENT_NAME_PROTOCOL instance.

> +

> +  @param  ControllerHandle[in]  The handle of a controller that the driver

> +                                specified by This is managing.  This handle

> +                                specifies the controller whose name is to be

> +                                returned.

> +

> +  @param  ChildHandle[in]       The handle of the child controller to retrieve

> +                                the name of.  This is an optional parameter that

> +                                may be NULL.  It will be NULL for device

> +                                drivers.  It will also be NULL for a bus drivers

> +                                that wish to retrieve the name of the bus

> +                                controller.  It will not be NULL for a bus

> +                                driver that wishes to retrieve the name of a

> +                                child controller.

> +

> +  @param  Language[in]          A pointer to a Null-terminated ASCII string

> +                                array indicating the language.  This is the

> +                                language of the driver name that the caller is

> +                                requesting, and it must match one of the

> +                                languages specified in SupportedLanguages. The

> +                                number of languages supported by a driver is up

> +                                to the driver writer. Language is specified in

> +                                RFC 4646 or ISO 639-2 language code format.

> +

> +  @param  ControllerName[out]   A pointer to the Unicode string to return.

> +                                This Unicode string is the name of the

> +                                controller specified by ControllerHandle and

> +                                ChildHandle in the language specified by

> +                                Language from the point of view of the driver

> +                                specified by This.

> +

> +  @retval EFI_SUCCESS           The Unicode string for the user readable name in

> +                                the language specified by Language for the

> +                                driver specified by This was returned in

> +                                DriverName.

> +

> +  @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.

> +

> +  @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid

> +                                EFI_HANDLE.

> +

> +  @retval EFI_INVALID_PARAMETER Language is NULL.

> +

> +  @retval EFI_INVALID_PARAMETER ControllerName is NULL.

> +

> +  @retval EFI_UNSUPPORTED       The driver specified by This is not currently

> +                                managing the controller specified by

> +                                ControllerHandle and ChildHandle.

> +

> +  @retval EFI_UNSUPPORTED       The driver specified by This does not support

> +                                the language specified by Language.

> +

> +**/

> +STATIC

> +EFI_STATUS

> +EFIAPI

> +ChaosKeyGetControllerName (

> +  IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,

> +  IN  EFI_HANDLE                                      ControllerHandle,

> +  IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,

> +  IN  CHAR8                                           *Language,

> +  OUT CHAR16                                          **ControllerName

> +  )

> +{

> +  if (ChildHandle != NULL) {

> +    return EFI_UNSUPPORTED;

> +  }

> +

> +  return LookupUnicodeString2 (

> +           Language,

> +           This->SupportedLanguages,

> +           mChaosKeyControllerNameTable,

> +           ControllerName,

> +           (BOOLEAN)(This == &gChaosKeyDriverComponentName)

> +           );

> +}

> +

> +//

> +// EFI Component Name Protocol

> +//

> +EFI_COMPONENT_NAME_PROTOCOL gChaosKeyDriverComponentName = {

> +  ChaosKeyDriverName,

> +  ChaosKeyControllerName,

> +  "eng"

> +};

> +

> +//

> +// EFI Component Name 2 Protocol

> +//

> +EFI_COMPONENT_NAME2_PROTOCOL gChaosKeyDriverComponentName2 = {

> +  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) ChaosKeyGetDriverName,

> +  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) ChaosKeyGetControllerName,

> +  "en"

> +};

> diff --git a/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c

> new file mode 100644

> index 000000000000..a1a5a796d38d

> --- /dev/null

> +++ b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c

> @@ -0,0 +1,256 @@

> +/** @file

> +  Device driver for the ChaosKey hardware random number generator.

> +

> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

> +

> +  This program and the accompanying materials

> +  are licensed and made available under the terms and conditions of the BSD

> +  License which accompanies this distribution. The full text of the license may

> +  be found at  http://opensource.org/licenses/bsd-license.php.

> +

> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> +

> +**/

> +

> +#include <Library/UefiDriverEntryPoint.h>

> +

> +#include "ChaosKeyDriver.h"

> +

> +/**

> +  Tests to see if this driver supports a given controller.

> +

> +  @param  This[in]                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL

> +                                   instance.

> +  @param  ControllerHandle[in]     The handle of the controller to test.

> +  @param  RemainingDevicePath[in]  The remaining device path.

> +                                   (Ignored - this is not a bus driver.)

> +

> +  @retval EFI_SUCCESS              The driver supports this controller.

> +  @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle is

> +                                   already being managed by the driver specified

> +                                   by This.

> +  @retval EFI_UNSUPPORTED          The device specified by ControllerHandle is

> +                                   not supported by the driver specified by This.

> +

> +**/

> +EFI_STATUS

> +EFIAPI

> +UsbHwrngDriverBindingSupported (

> +  IN EFI_DRIVER_BINDING_PROTOCOL  *This,

> +  IN EFI_HANDLE                   ControllerHandle,

> +  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath

> +  )

> +{

> +  EFI_USB_DEVICE_DESCRIPTOR  Device;

> +  EFI_USB_IO_PROTOCOL        *UsbIo;

> +  EFI_STATUS                 Status;

> +

> +  //

> +  //  Connect to the USB stack

> +  //

> +  Status = gBS->OpenProtocol (ControllerHandle,

> +                              &gEfiUsbIoProtocolGuid,

> +                              (VOID **) &UsbIo,

> +                              This->DriverBindingHandle,

> +                              ControllerHandle,

> +                              EFI_OPEN_PROTOCOL_BY_DRIVER);

> +  if (EFI_ERROR (Status)) {

> +    return Status;

> +  }

> +

> +  //

> +  //  Get the interface descriptor to check the USB class and find a transport

> +  //  protocol handler.

> +  //

> +  Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &Device);

> +  if (!EFI_ERROR (Status)) {

> +    //

> +    //  Validate the adapter

> +    //

> +    if ((Device.IdVendor != CHAOSKEY_VENDOR_ID) ||

> +        (Device.IdProduct != CHAOSKEY_PRODUCT_ID)) {

> +      Status = EFI_UNSUPPORTED;

> +    } else {

> +      DEBUG ((DEBUG_INIT | DEBUG_INFO,

> +        "ChaosKey (0x%04x:0x%04x) is my homeboy!\n",

> +        Device.IdVendor, Device.IdProduct));


That was intended as a cute remark in my original blog post. Perhaps a
more descriptive message would be better.
"Detected ChaosKey RNG device." or such.

> +      Status = EFI_SUCCESS;

> +    }

> +  }

> +

> +  //

> +  // Clean up.

> +  //

> +  gBS->CloseProtocol (ControllerHandle,

> +                      &gEfiUsbIoProtocolGuid,

> +                      This->DriverBindingHandle,

> +                      ControllerHandle);

> +

> +  return Status;

> +}

> +

> +

> +/**

> +  Starts a device controller or a bus controller.

> +

> +  @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL

> +                                   instance.

> +  @param[in]  ControllerHandle     The handle of the device to start. This

> +                                   handle must support a protocol interface that

> +                                   supplies an I/O abstraction to the driver.

> +  @param[in]  RemainingDevicePath  The remaining portion of the device path.

> +                                   (Ignored - this is not a bus driver.)

> +

> +  @retval EFI_SUCCESS              The device was started.

> +  @retval EFI_DEVICE_ERROR         The device could not be started due to a

> +                                   device error.

> +  @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a

> +                                   lack of resources.

> +

> +**/

> +EFI_STATUS

> +EFIAPI

> +UsbHwrngDriverBindingStart (

> +  IN EFI_DRIVER_BINDING_PROTOCOL  *This,

> +  IN EFI_HANDLE                   ControllerHandle,

> +  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL

> +  )

> +{

> +  return ChaosKeyInit (This->DriverBindingHandle, ControllerHandle);

> +}

> +

> +

> +/**

> +  Stops a device controller or a bus controller.

> +

> +  @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL

> +                                instance.

> +  @param[in]  ControllerHandle  A handle to the device being stopped. The handle

> +                                must support a bus specific I/O protocol for the

> +                                driver to use to stop the device.

> +  @param[in]  NumberOfChildren  The number of child device handles in

> +                                ChildHandleBuffer.

> +  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be

> +                                NULL if NumberOfChildren is 0.

> +

> +  @retval EFI_SUCCESS           The device was stopped.

> +  @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device

> +                                error.

> +

> +**/

> +EFI_STATUS

> +EFIAPI

> +UsbHwrngDriverBindingStop (

> +  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,

> +  IN  EFI_HANDLE                  ControllerHandle,

> +  IN  UINTN                       NumberOfChildren,

> +  IN  EFI_HANDLE                  *ChildHandleBuffer OPTIONAL

> +  )

> +{

> +  return ChaosKeyRelease (This->DriverBindingHandle, ControllerHandle);

> +}

> +

> +

> +STATIC

> +EFI_DRIVER_BINDING_PROTOCOL  gUsbDriverBinding = {

> +  UsbHwrngDriverBindingSupported,

> +  UsbHwrngDriverBindingStart,

> +  UsbHwrngDriverBindingStop,

> +  0xa,

> +  NULL,

> +  NULL

> +};

> +

> +

> +/**

> +  The entry point of ChaosKey UEFI Driver.

> +

> +  @param  ImageHandle                The image handle of the UEFI Driver.

> +  @param  SystemTable                A pointer to the EFI System Table.

> +

> +  @retval  EFI_SUCCESS               The Driver or UEFI Driver exited normally.

> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than

> +                                     SystemTable->Hdr.Revision.

> +

> +**/

> +EFI_STATUS

> +EFIAPI

> +EntryPoint (

> +  IN  EFI_HANDLE          ImageHandle,

> +  IN  EFI_SYSTEM_TABLE    *SystemTable

> +  )

> +{

> +  EFI_STATUS    Status;

> +

> +  //

> +  //  Add the driver to the list of drivers

> +  //

> +  Status = EfiLibInstallDriverBindingComponentName2 (

> +             ImageHandle, SystemTable, &gUsbDriverBinding, ImageHandle,

> +             &gChaosKeyDriverComponentName, &gChaosKeyDriverComponentName2);


Random question ... do we _want_ to provide
EFI_COMPONENT_NAME_PROTOCOL, in addition to
EFI_COMPONENT_NAME2_PROTOCOL, for new drivers?

I should also point out that I don't actually see a DRIVER NAME when
executing the "drivers" command from UEFI Shell.

/
    Leif

> +  ASSERT_EFI_ERROR (Status);

> +

> +  DEBUG ((DEBUG_INIT | DEBUG_INFO, "*** Installed ChaosKey driver! ***\n"));

> +

> +  return EFI_SUCCESS;

> +}

> +

> +

> +/**

> +  Unload function for the ChaosKey Driver.

> +

> +  @param  ImageHandle[in]        The allocated handle for the EFI image

> +

> +  @retval EFI_SUCCESS            The driver was unloaded successfully

> +  @retval EFI_INVALID_PARAMETER  ImageHandle is not a valid image handle.

> +

> +**/

> +EFI_STATUS

> +EFIAPI

> +UnloadImage (

> +  IN EFI_HANDLE  ImageHandle

> +  )

> +{

> +  EFI_STATUS  Status;

> +  EFI_HANDLE  *HandleBuffer;

> +  UINTN       HandleCount;

> +  UINTN       Index;

> +

> +  //

> +  // Retrieve all USB I/O handles in the handle database

> +  //

> +  Status = gBS->LocateHandleBuffer (ByProtocol,

> +                                    &gEfiUsbIoProtocolGuid,

> +                                    NULL,

> +                                    &HandleCount,

> +                                    &HandleBuffer);

> +  if (EFI_ERROR (Status)) {

> +    return Status;

> +  }

> +

> +  //

> +  // Disconnect the driver from the handles in the handle database

> +  //

> +  for (Index = 0; Index < HandleCount; Index++) {

> +    Status = gBS->DisconnectController (HandleBuffer[Index],

> +                                        gImageHandle,

> +                                        NULL);

> +  }

> +

> +  //

> +  // Free the handle array

> +  //

> +  gBS->FreePool (HandleBuffer);

> +

> +  //

> +  // Uninstall protocols installed by the driver in its entrypoint

> +  //

> +  Status = gBS->UninstallMultipleProtocolInterfaces (ImageHandle,

> +                  &gEfiDriverBindingProtocolGuid,

> +                  &gUsbDriverBinding,

> +                  NULL

> +                  );

> +

> +  return EFI_SUCCESS;

> +}

> -- 

> 2.11.0

> 

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Ard Biesheuvel Aug. 24, 2017, 9:44 a.m. UTC | #2
On 24 August 2017 at 00:48, Leif Lindholm <leif.lindholm@linaro.org> wrote:
> On Wed, Aug 23, 2017 at 02:12:06PM +0100, Ard Biesheuvel wrote:

>> This is a continuation of the work carried out by Leif Lindholm to

>> implement a driver for the ChaosKey USB device. This driver uses the

>> UEFI driver model, which is a slightly awkward fit, due to the fact

>> that a UEFI implementation may legally only instantiate those protocols

>> that are needed to access the device path that the active Boot####

>> options refers to.

>>

>> However, it is expected that UEFI implementations typically instantiate

>> all USB I/O protocols and connect them as well, as those are required

>> for a USB keyboard to be able to control the boot sequence. This should

>> result in this driver being connected and given the opportunity to

>> produce the EFI_RNG_PROTOCOL.

>>

>> Contributed-under: TianoCore Contribution Agreement 1.1

>> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

>> ---

>>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c | 346 ++++++++++++++++++++

>>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h |  61 ++++

>>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf  |  48 +++

>>  Silicon/Openmoko/ChaosKeyDxe/ComponentName.c  | 205 ++++++++++++

>>  Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c  | 256 +++++++++++++++

>>  5 files changed, 916 insertions(+)

>

> This is all a bit new territory, so I don't know what the best way of

> dealing with this is, but I think we could do with a .dsc somewhere to

> enable building this driver standalone. My gut feel is that an

> Openmoko/Openmoko.dsc would be a reasonable candidate.

>


Yeah, I can add one. Also useful for building this driver as an EBC binary.

>> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c

>> new file mode 100644

>> index 000000000000..1870080d2c70

>> --- /dev/null

>> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c

>> @@ -0,0 +1,346 @@

>> +/** @file

>> +  Device driver for the ChaosKey hardware random number generator.

>> +

>> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

>> +

>> +  This program and the accompanying materials

>> +  are licensed and made available under the terms and conditions of the BSD

>> +  License which accompanies this distribution. The full text of the license may

>> +  be found at  http://opensource.org/licenses/bsd-license.php.

>> +

>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

>> +

>> +**/

>> +

>> +#include "ChaosKeyDriver.h"

>> +

>> +#include <Library/BaseMemoryLib.h>

>> +#include <Library/DebugLib.h>

>> +#include <Library/MemoryAllocationLib.h>

>> +

>> +STATIC

>> +BOOLEAN

>> +IsBulkInEndpoint (

>> +  IN  EFI_USB_ENDPOINT_DESCRIPTOR     *Endpoint

>> +  )

>> +{

>> +  if ((Endpoint->Attributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_BULK) {

>> +    if (Endpoint->EndpointAddress & USB_ENDPOINT_DIR_IN) {

>> +      return TRUE;

>> +    }

>> +  }

>> +  return FALSE;

>> +}

>> +

>> +

>> +STATIC

>> +EFI_STATUS

>> +FindEndpoint (

>> +  IN  CHAOSKEY_DEV *ChaosKey

>> +  )

>> +{

>> +  EFI_USB_IO_PROTOCOL             *UsbIo;

>> +  EFI_STATUS                      Status;

>> +  UINTN                           Index;

>> +  EFI_USB_INTERFACE_DESCRIPTOR    InterfaceDescriptor;

>> +

>> +  UsbIo = ChaosKey->UsbIo;

>> +

>> +  //

>> +  // Get interface & endpoint descriptor

>> +  //

>> +  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);

>> +  if (EFI_ERROR (Status)) {

>> +    return Status;

>> +  }

>> +

>> +  //

>> +  // The ChaosKey provides two endpoints:

>> +  // - The first one is the 'cooked' one, to be used as random data input

>> +  // - The second one is the raw bitstream from the generator, higher

>> +  //   throughput, but lower randomness.

>> +  // So locate the first bulk IN endpoint and save it for later use.

>> +  //

>> +  for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {

>> +    EFI_USB_ENDPOINT_DESCRIPTOR  Endpoint;

>> +

>> +    Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &Endpoint);

>> +    if (EFI_ERROR (Status)) {

>> +      DEBUG ((DEBUG_ERROR, "UsbGetEndPointDescriptor(%d) failed!\n", Index));

>> +      return Status;

>> +    }

>> +

>> +    if (IsBulkInEndpoint(&Endpoint)) {

>> +      ChaosKey->EndpointAddress = Endpoint.EndpointAddress;

>> +      ChaosKey->EndpointSize = Endpoint.MaxPacketSize;

>> +      return EFI_SUCCESS;

>> +    }

>> +  }

>> +

>> +  DEBUG ((DEBUG_ERROR, "Failed to locate suitable BULK IN USB endpoint!\n"));

>> +  return EFI_DEVICE_ERROR;

>> +}

>> +

>> +

>> +/**

>> +  Returns information about the random number generation implementation.

>> +

>> +  @param[in]     This               A pointer to the EFI_RNG_PROTOCOL instance.

>> +  @param[in,out] AlgorithmListSize  On input, the size in bytes of AlgorithmList

>> +                                    On output with a return code of EFI_SUCCESS,

>> +                                    the size in bytes of the data returned in

>> +                                    AlgorithmList. On output with a return

>> +                                    code of EFI_BUFFER_TOO_SMALL, the size of

>> +                                    AlgorithmList required to obtain the list.

>> +  @param[out] AlgorithmList         A caller-allocated memory buffer filled by

>> +                                    the driver with one EFI_RNG_ALGORITHM

>> +                                    element for each supported RNG algorithm.

>> +                                    The list must not change across multiple

>> +                                    calls to the same driver. The first

>> +                                    algorithm in the list is the default

>> +                                    algorithm for the driver.

>> +

>> +  @retval EFI_SUCCESS               The RNG algorithm list was returned

>> +                                    successfully.

>> +  @retval EFI_UNSUPPORTED           The services is not supported by this driver

>> +  @retval EFI_DEVICE_ERROR          The list of algorithms could not be

>> +                                    retrieved due to a hardware or firmware

>> +                                    error.

>> +  @retval EFI_INVALID_PARAMETER     One or more of the parameters are incorrect.

>> +  @retval EFI_BUFFER_TOO_SMALL      The buffer RNGAlgorithmList is too small to

>> +                                    hold the result.

>> +

>> +**/

>> +STATIC

>> +EFI_STATUS

>> +EFIAPI

>> +GetInfo (

>> +  IN      EFI_RNG_PROTOCOL    *This,

>> +  IN  OUT UINTN               *AlgorithmListSize,

>> +  OUT     EFI_RNG_ALGORITHM   *AlgorithmList

>> +)

>> +{

>> +  UINTN Size;

>> +

>> +  //

>> +  // We only implement the raw algorithm

>> +  //

>> +  Size = sizeof gEfiRngAlgorithmRaw;

>> +

>> +  if (*AlgorithmListSize < Size) {

>> +    *AlgorithmListSize = Size;

>> +    return EFI_BUFFER_TOO_SMALL;

>> +  }

>> +

>> +  gBS->CopyMem (AlgorithmList, &gEfiRngAlgorithmRaw, Size);

>> +  *AlgorithmListSize = Size;

>> +

>> +  return EFI_SUCCESS;

>> +}

>> +

>> +

>> +/**

>> +  Produces and returns an RNG value using either the default or specified RNG

>> +  algorithm.

>> +

>> +  @param[in]  This                A pointer to the EFI_RNG_PROTOCOL instance.

>> +  @param[in]  Algorithm           A pointer to the EFI_RNG_ALGORITHM that

>> +                                  identifies the RNG algorithm to use. May be

>> +                                  NULL in which case the function will use its

>> +                                  default RNG algorithm.

>> +  @param[in]  ValueLength         The length in bytes of the memory buffer

>> +                                  pointed to by RNGValue. The driver shall

>> +                                  return exactly this numbers of bytes.

>> +  @param[out] Value               A caller-allocated memory buffer filled by the

>> +                                  driver with the resulting RNG value.

>> +

>> +  @retval EFI_SUCCESS             The RNG value was returned successfully.

>> +  @retval EFI_UNSUPPORTED         The algorithm specified by RNGAlgorithm is not

>> +                                  supported by this driver.

>> +  @retval EFI_DEVICE_ERROR        An RNG value could not be retrieved due to a

>> +                                  hardware or firmware error.

>> +  @retval EFI_NOT_READY           There is not enough random data available to

>> +                                  satisfy the length requested by

>> +                                  RNGValueLength.

>> +  @retval EFI_INVALID_PARAMETER   RNGValue is NULL or RNGValueLength is zero.

>> +

>> +**/

>> +STATIC

>> +EFI_STATUS

>> +EFIAPI

>> +GetRNG (

>> +  IN EFI_RNG_PROTOCOL   *This,

>> +  IN EFI_RNG_ALGORITHM  *Algorithm OPTIONAL,

>> +  IN UINTN              ValueLength,

>> +  OUT UINT8             *Value

>> +)

>> +{

>> +  EFI_STATUS        Status;

>> +  CHAOSKEY_DEV      *ChaosKey;

>> +  EFI_TPL           Tpl;

>> +  UINT8             Buffer[CHAOSKEY_MAX_EP_SIZE];

>> +  UINT8             *OutPointer;

>> +  UINTN             OutSize;

>> +  UINT32            Result;

>> +

>> +  if (Algorithm != NULL && !CompareGuid (Algorithm, &gEfiRngAlgorithmRaw)) {

>> +    return EFI_UNSUPPORTED;

>> +  }

>> +

>> +  ChaosKey = CHAOSKEY_DEV_FROM_THIS (This);

>> +

>> +  while (ValueLength > 0) {

>> +    //

>> +    // If more data is requested than the endpoint can deliver in a single

>> +    // transfer, put it straight into the caller's buffer.

>> +    //

>> +    if (ValueLength >= ChaosKey->EndpointSize) {

>> +      OutPointer = Value;

>> +    } else {

>> +      OutPointer = Buffer;

>> +    }

>> +    OutSize = ChaosKey->EndpointSize;

>> +

>> +    Tpl = gBS->RaiseTPL (TPL_NOTIFY);

>

> You mentioned to me on IRC that this should not be necessary, and that

> UsbIo should adjust tpl as necessary.

>

> It is quite likely I misinterpreted behaviour I encountered when using

> USB passthrough in QEMU for testing (and managed to break the host

> system's driver).

>


I will remove it. We are dealing with an abstract protocol here, not
hardware, and if completing a single bulk transfer does not tolerate
interruptions, it should be up to the USB I/O protocol implementation
to deal with that.

>> +

>> +    Status = ChaosKey->UsbIo->UsbBulkTransfer (ChaosKey->UsbIo,

>> +                                               ChaosKey->EndpointAddress,

>> +                                               OutPointer,

>> +                                               &OutSize,

>> +                                               CHAOSKEY_TIMEOUT,

>> +                                               &Result);

>> +

>> +    gBS->RestoreTPL (Tpl);

>> +

>> +    if (Status == EFI_TIMEOUT) {

>> +      DEBUG ((DEBUG_ERROR, "Bulk transfer timed out, USB status == %d\n",

>> +        Result));

>> +      return EFI_NOT_READY;

>> +    } else if (EFI_ERROR (Status)) {

>> +      DEBUG ((DEBUG_ERROR,

>> +        "Bulk transfer failed, Status == %r, USB status == %d\n",

>> +        Status, Result));

>> +      return EFI_DEVICE_ERROR;

>> +    }

>> +

>> +    OutSize = MIN (OutSize, ValueLength);

>> +

>> +    if (Value != Buffer) {

>> +      gBS->CopyMem (Value, Buffer, OutSize);

>> +    }

>> +    Value += OutSize;

>> +    ValueLength -= OutSize;

>> +  }

>> +  return EFI_SUCCESS;

>> +}

>> +

>> +

>> +EFI_STATUS

>> +ChaosKeyInit (

>> +  IN      EFI_HANDLE        DriverBindingHandle,

>> +  IN      EFI_HANDLE        ControllerHandle

>> +  )

>> +{

>> +  EFI_STATUS                Status;

>> +  CHAOSKEY_DEV              *ChaosKey;

>> +

>> +  Status  = gBS->AllocatePool (EfiBootServicesData,

>> +                               sizeof (CHAOSKEY_DEV),

>> +                               (VOID **) &ChaosKey);

>> +  if (EFI_ERROR (Status)) {

>> +    return EFI_OUT_OF_RESOURCES;

>> +  }

>> +

>> +  ChaosKey->Signature         = CHAOSKEY_DEV_SIGNATURE;

>> +  ChaosKey->Rng.GetInfo       = GetInfo;

>> +  ChaosKey->Rng.GetRNG        = GetRNG;

>> +

>> +  //

>> +  // Open USB I/O Protocol

>> +  //

>> +  Status = gBS->OpenProtocol (ControllerHandle,

>> +                              &gEfiUsbIoProtocolGuid,

>> +                              (VOID **)&ChaosKey->UsbIo,

>> +                              DriverBindingHandle,

>> +                              ControllerHandle,

>> +                              EFI_OPEN_PROTOCOL_BY_DRIVER);

>> +  if (EFI_ERROR (Status)) {

>> +    goto ErrorFreeDev;

>> +  }

>> +

>> +  Status = FindEndpoint (ChaosKey);

>> +  if (EFI_ERROR (Status)) {

>> +    goto ErrorCloseProtocol;

>> +  }

>> +

>> +  //

>> +  // The following can only occur if the Chaoskey is suddenly reissued

>> +  // as a high speed or super speed device under the same VID/PID.

>> +  //

>> +  ASSERT (ChaosKey->EndpointSize <= CHAOSKEY_MAX_EP_SIZE);

>> +

>> +  Status = gBS->InstallProtocolInterface (&ControllerHandle,

>> +                                          &gEfiRngProtocolGuid,

>> +                                          EFI_NATIVE_INTERFACE,

>> +                                          &ChaosKey->Rng);

>> +  if (EFI_ERROR (Status)) {

>> +    DEBUG ((DEBUG_ERROR,

>> +      "Failed to install RNG protocol interface (Status == %r)\n",

>> +    Status));

>> +    goto ErrorCloseProtocol;

>> +  }

>> +

>> +  return EFI_SUCCESS;

>> +

>> +ErrorCloseProtocol:

>> +  gBS->CloseProtocol (ControllerHandle, &gEfiUsbIoProtocolGuid,

>> +         DriverBindingHandle, ControllerHandle);

>> +

>> +ErrorFreeDev:

>> +  gBS->FreePool (ChaosKey);

>> +

>> +  return Status;

>> +}

>> +

>> +EFI_STATUS

>> +ChaosKeyRelease (

>> +  IN  EFI_HANDLE        DriverBindingHandle,

>> +  IN  EFI_HANDLE        ControllerHandle

>> +  )

>> +{

>> +  EFI_RNG_PROTOCOL    *Rng;

>> +  CHAOSKEY_DEV        *ChaosKey;

>> +  EFI_STATUS          Status;

>> +

>> +  Status = gBS->HandleProtocol (ControllerHandle,

>> +                                &gEfiRngProtocolGuid,

>> +                                (VOID **)&Rng);

>> +  ASSERT_EFI_ERROR (Status);

>> +  if (EFI_ERROR (Status)) {

>> +    return Status;

>> +  }

>> +

>> +  ChaosKey = CHAOSKEY_DEV_FROM_THIS (Rng);

>> +

>> +  Status = gBS->UninstallProtocolInterface (ControllerHandle,

>> +                                            &gEfiRngProtocolGuid,

>> +                                            Rng);

>> +  ASSERT_EFI_ERROR (Status);

>> +  if (EFI_ERROR (Status)) {

>> +    return Status;

>> +  }

>> +

>> +  Status = gBS->CloseProtocol (ControllerHandle,

>> +                               &gEfiUsbIoProtocolGuid,

>> +                               DriverBindingHandle,

>> +                               ControllerHandle);

>> +  ASSERT_EFI_ERROR (Status);

>> +  if (EFI_ERROR (Status)) {

>> +    return Status;

>> +  }

>> +

>> +  gBS->FreePool (ChaosKey);

>> +

>> +  return EFI_SUCCESS;

>> +}

>> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h

>> new file mode 100644

>> index 000000000000..153deb4edb1c

>> --- /dev/null

>> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h

>> @@ -0,0 +1,61 @@

>> +/** @file

>> +  Header file for the ChaosKey hardware random number generator.

>> +

>> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

>> +

>> +  This program and the accompanying materials

>> +  are licensed and made available under the terms and conditions of the BSD

>> +  License which accompanies this distribution. The full text of the license may

>> +  be found at  http://opensource.org/licenses/bsd-license.php.

>> +

>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

>> +

>> +**/

>> +

>> +#ifndef _CHAOSKEY_USB_HWRNG_DRIVER_H_

>> +#define _CHAOSKEY_USB_HWRNG_DRIVER_H_

>> +

>> +#include <Uefi.h>

>> +#include <Library/DebugLib.h>

>> +#include <Library/UefiLib.h>

>> +#include <Library/UefiBootServicesTableLib.h>

>

> B before L.

>


OK

>> +

>> +#include <Protocol/Rng.h>

>> +#include <Protocol/UsbIo.h>

>> +

>> +#define CHAOSKEY_VENDOR_ID      0x1d50  /* OpenMoko */

>> +#define CHAOSKEY_PRODUCT_ID     0x60c6  /* ChaosKey */

>> +

>> +#define CHAOSKEY_TIMEOUT        10 // ms

>> +#define CHAOSKEY_MAX_EP_SIZE    64 // max EP size for full-speed devices

>> +

>> +#define CHAOSKEY_DEV_SIGNATURE  SIGNATURE_32('c','h','k','e')

>> +

>> +typedef struct {

>> +  UINT32                        Signature;

>> +  UINT16                        EndpointAddress;

>> +  UINT16                        EndpointSize;

>> +  EFI_USB_IO_PROTOCOL           *UsbIo;

>> +  EFI_RNG_PROTOCOL              Rng;

>> +} CHAOSKEY_DEV;

>> +

>> +#define CHAOSKEY_DEV_FROM_THIS(a) \

>> +  CR(a, CHAOSKEY_DEV, Rng, CHAOSKEY_DEV_SIGNATURE)

>> +

>> +extern EFI_COMPONENT_NAME_PROTOCOL gChaosKeyDriverComponentName;

>> +extern EFI_COMPONENT_NAME2_PROTOCOL gChaosKeyDriverComponentName2;

>> +

>> +EFI_STATUS

>> +ChaosKeyInit (

>> +  IN  EFI_HANDLE        DriverBindingHandle,

>> +  IN  EFI_HANDLE        ControllerHandle

>> +  );

>> +

>> +EFI_STATUS

>> +ChaosKeyRelease (

>> +  IN  EFI_HANDLE        DriverBindingHandle,

>> +  IN  EFI_HANDLE        ControllerHandle

>> +  );

>> +

>> +#endif // _CHAOSKEY_USB_HWRNG_DRIVER_H_

>> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf

>> new file mode 100644

>> index 000000000000..2ff84956ca72

>> --- /dev/null

>> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf

>> @@ -0,0 +1,48 @@

>> +## @file

>> +#  Device driver for the ChaosKey hardware random number generator.

>> +#

>> +#  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

>> +#

>> +#  This program and the accompanying materials

>> +#  are licensed and made available under the terms and conditions of the BSD

>> +#  License which accompanies this distribution. The full text of the license may

>> +#  be found at  http://opensource.org/licenses/bsd-license.php.

>> +#

>> +#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

>> +#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

>> +#

>> +##

>> +

>> +[Defines]

>> +  INF_VERSION                    = 0x00010019

>> +  BASE_NAME                      = ChaosKeyDxe

>> +  FILE_GUID                      = 9A54122B-F5E4-40D8-AE61-A71E406ED449

>> +  MODULE_TYPE                    = UEFI_DRIVER

>> +  VERSION_STRING                 = 1.0

>> +  ENTRY_POINT                    = EntryPoint

>> +  UNLOAD_IMAGE                   = UnloadImage

>> +

>> +#

>> +#  VALID_ARCHITECTURES           = AARCH64 ARM EBC IA32 IPF X64

>> +#

>> +

>> +[Sources]

>> +  ChaosKeyDriver.c

>> +  ChaosKeyDriver.h

>> +  ComponentName.c

>> +  DriverBinding.c

>> +

>> +[Packages]

>> +  MdePkg/MdePkg.dec

>> +

>> +[LibraryClasses]

>> +  UefiBootServicesTableLib

>> +  UefiDriverEntryPoint

>> +  UefiLib

>> +

>> +[Protocols]

>> +  gEfiRngProtocolGuid                 # PROTOCOL BY_START

>> +  gEfiUsbIoProtocolGuid               # PROTOCOL TO_START

>> +

>> +[Guids]

>> +  gEfiRngAlgorithmRaw

>> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c b/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c

>> new file mode 100644

>> index 000000000000..81f2130bcd9e

>> --- /dev/null

>> +++ b/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c

>

> You mentioned on IRC that there's an accidental incomplete refactoring

> in this file (and indeed the build fails as it stands).

>

>> @@ -0,0 +1,205


Yes.

 @@
>> +/** @file

>> +  UEFI Component Name(2) protocol implementation for ChaosKey driver.

>> +

>> +  Copyright (c) 2017, Linaro Ltd. All rights reserved.

>> +

>> +  This program and the accompanying materials

>> +  are licensed and made available under the terms and conditions of the BSD License

>> +  which accompanies this distribution.  The full text of the license may be found at

>> +  http://opensource.org/licenses/bsd-license.php

>> +

>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

>> +

>> +**/

>> +

>> +#include "ChaosKeyDriver.h"

>> +

>> +STATIC EFI_UNICODE_STRING_TABLE mChaosKeyDriverNameTable[] = {

>> +  {

>> +    "eng;en",

>> +    (CHAR16 *)L"ChaosKey RNG USB driver"

>> +  },

>> +  {

>> +    NULL,

>> +    NULL

>> +  }

>> +};

>> +

>> +STATIC EFI_UNICODE_STRING_TABLE mChaosKeyControllerNameTable[] = {

>> +  {

>> +    "eng;en",

>> +    (CHAR16 *)L"ChaosKey Random Number Generator (USB)"

>> +  },

>> +  {

>> +    NULL,

>> +    NULL

>> +  }

>> +};

>> +

>> +/**

>> +  Retrieves a Unicode string that is the user readable name of the driver.

>> +

>> +  This function retrieves the user readable name of a driver in the form of a

>> +  Unicode string. If the driver specified by This has a user readable name in

>> +  the language specified by Language, then a pointer to the driver name is

>> +  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified

>> +  by This does not support the language specified by Language,

>> +  then EFI_UNSUPPORTED is returned.

>> +

>> +  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or

>> +                                EFI_COMPONENT_NAME_PROTOCOL instance.

>> +

>> +  @param  Language[in]          A pointer to a Null-terminated ASCII string

>> +                                array indicating the language. This is the

>> +                                language of the driver name that the caller is

>> +                                requesting, and it must match one of the

>> +                                languages specified in SupportedLanguages. The

>> +                                number of languages supported by a driver is up

>> +                                to the driver writer. Language is specified

>> +                                in RFC 4646 or ISO 639-2 language code format.

>> +

>> +  @param  DriverName[out]       A pointer to the Unicode string to return.

>> +                                This Unicode string is the name of the

>> +                                driver specified by This in the language

>> +                                specified by Language.

>> +

>> +  @retval EFI_SUCCESS           The Unicode string for the Driver specified by

>> +                                This and the language specified by Language was

>> +                                returned in DriverName.

>> +

>> +  @retval EFI_INVALID_PARAMETER Language is NULL.

>> +

>> +  @retval EFI_INVALID_PARAMETER DriverName is NULL.

>> +

>> +  @retval EFI_UNSUPPORTED       The driver specified by This does not support

>> +                                the language specified by Language.

>> +

>> +**/

>> +STATIC

>> +EFI_STATUS

>> +EFIAPI

>> +ChaosKeyDriverName (

>> +  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,

>> +  IN  CHAR8                        *Language,

>> +  OUT CHAR16                       **DriverName

>> +  )

>> +{

>> +  return LookupUnicodeString2 (

>> +           Language,

>> +           This->SupportedLanguages,

>> +           mChaosKeyDriverNameTable,

>> +           DriverName,

>> +           (BOOLEAN)(This == &gChaosKeyDriverComponentName)

>> +           );

>> +}

>> +

>> +/**

>> +  Retrieves a Unicode string that is the user readable name of the controller

>> +  that is being managed by a driver.

>> +

>> +  This function retrieves the user readable name of the controller specified by

>> +  ControllerHandle and ChildHandle in the form of a Unicode string. If the

>> +  driver specified by This has a user readable name in the language specified by

>> +  Language, then a pointer to the controller name is returned in ControllerName,

>> +  and EFI_SUCCESS is returned.  If the driver specified by This is not currently

>> +  managing the controller specified by ControllerHandle and ChildHandle,

>> +  then EFI_UNSUPPORTED is returned.  If the driver specified by This does not

>> +  support the language specified by Language, then EFI_UNSUPPORTED is returned.

>> +

>> +  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or

>> +                                EFI_COMPONENT_NAME_PROTOCOL instance.

>> +

>> +  @param  ControllerHandle[in]  The handle of a controller that the driver

>> +                                specified by This is managing.  This handle

>> +                                specifies the controller whose name is to be

>> +                                returned.

>> +

>> +  @param  ChildHandle[in]       The handle of the child controller to retrieve

>> +                                the name of.  This is an optional parameter that

>> +                                may be NULL.  It will be NULL for device

>> +                                drivers.  It will also be NULL for a bus drivers

>> +                                that wish to retrieve the name of the bus

>> +                                controller.  It will not be NULL for a bus

>> +                                driver that wishes to retrieve the name of a

>> +                                child controller.

>> +

>> +  @param  Language[in]          A pointer to a Null-terminated ASCII string

>> +                                array indicating the language.  This is the

>> +                                language of the driver name that the caller is

>> +                                requesting, and it must match one of the

>> +                                languages specified in SupportedLanguages. The

>> +                                number of languages supported by a driver is up

>> +                                to the driver writer. Language is specified in

>> +                                RFC 4646 or ISO 639-2 language code format.

>> +

>> +  @param  ControllerName[out]   A pointer to the Unicode string to return.

>> +                                This Unicode string is the name of the

>> +                                controller specified by ControllerHandle and

>> +                                ChildHandle in the language specified by

>> +                                Language from the point of view of the driver

>> +                                specified by This.

>> +

>> +  @retval EFI_SUCCESS           The Unicode string for the user readable name in

>> +                                the language specified by Language for the

>> +                                driver specified by This was returned in

>> +                                DriverName.

>> +

>> +  @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.

>> +

>> +  @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid

>> +                                EFI_HANDLE.

>> +

>> +  @retval EFI_INVALID_PARAMETER Language is NULL.

>> +

>> +  @retval EFI_INVALID_PARAMETER ControllerName is NULL.

>> +

>> +  @retval EFI_UNSUPPORTED       The driver specified by This is not currently

>> +                                managing the controller specified by

>> +                                ControllerHandle and ChildHandle.

>> +

>> +  @retval EFI_UNSUPPORTED       The driver specified by This does not support

>> +                                the language specified by Language.

>> +

>> +**/

>> +STATIC

>> +EFI_STATUS

>> +EFIAPI

>> +ChaosKeyGetControllerName (

>> +  IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,

>> +  IN  EFI_HANDLE                                      ControllerHandle,

>> +  IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,

>> +  IN  CHAR8                                           *Language,

>> +  OUT CHAR16                                          **ControllerName

>> +  )

>> +{

>> +  if (ChildHandle != NULL) {

>> +    return EFI_UNSUPPORTED;

>> +  }

>> +

>> +  return LookupUnicodeString2 (

>> +           Language,

>> +           This->SupportedLanguages,

>> +           mChaosKeyControllerNameTable,

>> +           ControllerName,

>> +           (BOOLEAN)(This == &gChaosKeyDriverComponentName)

>> +           );

>> +}

>> +

>> +//

>> +// EFI Component Name Protocol

>> +//

>> +EFI_COMPONENT_NAME_PROTOCOL gChaosKeyDriverComponentName = {

>> +  ChaosKeyDriverName,

>> +  ChaosKeyControllerName,

>> +  "eng"

>> +};

>> +

>> +//

>> +// EFI Component Name 2 Protocol

>> +//

>> +EFI_COMPONENT_NAME2_PROTOCOL gChaosKeyDriverComponentName2 = {

>> +  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) ChaosKeyGetDriverName,

>> +  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) ChaosKeyGetControllerName,

>> +  "en"

>> +};

>> diff --git a/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c

>> new file mode 100644

>> index 000000000000..a1a5a796d38d

>> --- /dev/null

>> +++ b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c

>> @@ -0,0 +1,256 @@

>> +/** @file

>> +  Device driver for the ChaosKey hardware random number generator.

>> +

>> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

>> +

>> +  This program and the accompanying materials

>> +  are licensed and made available under the terms and conditions of the BSD

>> +  License which accompanies this distribution. The full text of the license may

>> +  be found at  http://opensource.org/licenses/bsd-license.php.

>> +

>> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

>> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

>> +

>> +**/

>> +

>> +#include <Library/UefiDriverEntryPoint.h>

>> +

>> +#include "ChaosKeyDriver.h"

>> +

>> +/**

>> +  Tests to see if this driver supports a given controller.

>> +

>> +  @param  This[in]                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL

>> +                                   instance.

>> +  @param  ControllerHandle[in]     The handle of the controller to test.

>> +  @param  RemainingDevicePath[in]  The remaining device path.

>> +                                   (Ignored - this is not a bus driver.)

>> +

>> +  @retval EFI_SUCCESS              The driver supports this controller.

>> +  @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle is

>> +                                   already being managed by the driver specified

>> +                                   by This.

>> +  @retval EFI_UNSUPPORTED          The device specified by ControllerHandle is

>> +                                   not supported by the driver specified by This.

>> +

>> +**/

>> +EFI_STATUS

>> +EFIAPI

>> +UsbHwrngDriverBindingSupported (

>> +  IN EFI_DRIVER_BINDING_PROTOCOL  *This,

>> +  IN EFI_HANDLE                   ControllerHandle,

>> +  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath

>> +  )

>> +{

>> +  EFI_USB_DEVICE_DESCRIPTOR  Device;

>> +  EFI_USB_IO_PROTOCOL        *UsbIo;

>> +  EFI_STATUS                 Status;

>> +

>> +  //

>> +  //  Connect to the USB stack

>> +  //

>> +  Status = gBS->OpenProtocol (ControllerHandle,

>> +                              &gEfiUsbIoProtocolGuid,

>> +                              (VOID **) &UsbIo,

>> +                              This->DriverBindingHandle,

>> +                              ControllerHandle,

>> +                              EFI_OPEN_PROTOCOL_BY_DRIVER);

>> +  if (EFI_ERROR (Status)) {

>> +    return Status;

>> +  }

>> +

>> +  //

>> +  //  Get the interface descriptor to check the USB class and find a transport

>> +  //  protocol handler.

>> +  //

>> +  Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &Device);

>> +  if (!EFI_ERROR (Status)) {

>> +    //

>> +    //  Validate the adapter

>> +    //

>> +    if ((Device.IdVendor != CHAOSKEY_VENDOR_ID) ||

>> +        (Device.IdProduct != CHAOSKEY_PRODUCT_ID)) {

>> +      Status = EFI_UNSUPPORTED;

>> +    } else {

>> +      DEBUG ((DEBUG_INIT | DEBUG_INFO,

>> +        "ChaosKey (0x%04x:0x%04x) is my homeboy!\n",

>> +        Device.IdVendor, Device.IdProduct));

>

> That was intended as a cute remark in my original blog post. Perhaps a

> more descriptive message would be better.

> "Detected ChaosKey RNG device." or such.

>


OK, will change that.

>> +      Status = EFI_SUCCESS;

>> +    }

>> +  }

>> +

>> +  //

>> +  // Clean up.

>> +  //

>> +  gBS->CloseProtocol (ControllerHandle,

>> +                      &gEfiUsbIoProtocolGuid,

>> +                      This->DriverBindingHandle,

>> +                      ControllerHandle);

>> +

>> +  return Status;

>> +}

>> +

>> +

>> +/**

>> +  Starts a device controller or a bus controller.

>> +

>> +  @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL

>> +                                   instance.

>> +  @param[in]  ControllerHandle     The handle of the device to start. This

>> +                                   handle must support a protocol interface that

>> +                                   supplies an I/O abstraction to the driver.

>> +  @param[in]  RemainingDevicePath  The remaining portion of the device path.

>> +                                   (Ignored - this is not a bus driver.)

>> +

>> +  @retval EFI_SUCCESS              The device was started.

>> +  @retval EFI_DEVICE_ERROR         The device could not be started due to a

>> +                                   device error.

>> +  @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a

>> +                                   lack of resources.

>> +

>> +**/

>> +EFI_STATUS

>> +EFIAPI

>> +UsbHwrngDriverBindingStart (

>> +  IN EFI_DRIVER_BINDING_PROTOCOL  *This,

>> +  IN EFI_HANDLE                   ControllerHandle,

>> +  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL

>> +  )

>> +{

>> +  return ChaosKeyInit (This->DriverBindingHandle, ControllerHandle);

>> +}

>> +

>> +

>> +/**

>> +  Stops a device controller or a bus controller.

>> +

>> +  @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL

>> +                                instance.

>> +  @param[in]  ControllerHandle  A handle to the device being stopped. The handle

>> +                                must support a bus specific I/O protocol for the

>> +                                driver to use to stop the device.

>> +  @param[in]  NumberOfChildren  The number of child device handles in

>> +                                ChildHandleBuffer.

>> +  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be

>> +                                NULL if NumberOfChildren is 0.

>> +

>> +  @retval EFI_SUCCESS           The device was stopped.

>> +  @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device

>> +                                error.

>> +

>> +**/

>> +EFI_STATUS

>> +EFIAPI

>> +UsbHwrngDriverBindingStop (

>> +  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,

>> +  IN  EFI_HANDLE                  ControllerHandle,

>> +  IN  UINTN                       NumberOfChildren,

>> +  IN  EFI_HANDLE                  *ChildHandleBuffer OPTIONAL

>> +  )

>> +{

>> +  return ChaosKeyRelease (This->DriverBindingHandle, ControllerHandle);

>> +}

>> +

>> +

>> +STATIC

>> +EFI_DRIVER_BINDING_PROTOCOL  gUsbDriverBinding = {

>> +  UsbHwrngDriverBindingSupported,

>> +  UsbHwrngDriverBindingStart,

>> +  UsbHwrngDriverBindingStop,

>> +  0xa,

>> +  NULL,

>> +  NULL

>> +};

>> +

>> +

>> +/**

>> +  The entry point of ChaosKey UEFI Driver.

>> +

>> +  @param  ImageHandle                The image handle of the UEFI Driver.

>> +  @param  SystemTable                A pointer to the EFI System Table.

>> +

>> +  @retval  EFI_SUCCESS               The Driver or UEFI Driver exited normally.

>> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than

>> +                                     SystemTable->Hdr.Revision.

>> +

>> +**/

>> +EFI_STATUS

>> +EFIAPI

>> +EntryPoint (

>> +  IN  EFI_HANDLE          ImageHandle,

>> +  IN  EFI_SYSTEM_TABLE    *SystemTable

>> +  )

>> +{

>> +  EFI_STATUS    Status;

>> +

>> +  //

>> +  //  Add the driver to the list of drivers

>> +  //

>> +  Status = EfiLibInstallDriverBindingComponentName2 (

>> +             ImageHandle, SystemTable, &gUsbDriverBinding, ImageHandle,

>> +             &gChaosKeyDriverComponentName, &gChaosKeyDriverComponentName2);

>

> Random question ... do we _want_ to provide

> EFI_COMPONENT_NAME_PROTOCOL, in addition to

> EFI_COMPONENT_NAME2_PROTOCOL, for new drivers?

>


If this is supposed to be a UEFI Driver Model driver, I'd suggest we
keep it for compatibility with older firmwares.

> I should also point out that I don't actually see a DRIVER NAME when

> executing the "drivers" command from UEFI Shell.

>


That worked fine for me, both for 'drivers' and 'devices'. Which
platform? Does it display other driver names correctly?
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Leif Lindholm Aug. 24, 2017, 11:30 a.m. UTC | #3
On Thu, Aug 24, 2017 at 10:44:46AM +0100, Ard Biesheuvel wrote:
> >>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c | 346 ++++++++++++++++++++

> >>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h |  61 ++++

> >>  Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf  |  48 +++

> >>  Silicon/Openmoko/ChaosKeyDxe/ComponentName.c  | 205 ++++++++++++

> >>  Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c  | 256 +++++++++++++++

> >>  5 files changed, 916 insertions(+)

> >

> > This is all a bit new territory, so I don't know what the best way of

> > dealing with this is, but I think we could do with a .dsc somewhere to

> > enable building this driver standalone. My gut feel is that an

> > Openmoko/Openmoko.dsc would be a reasonable candidate.

> 

> Yeah, I can add one. Also useful for building this driver as an EBC binary.


Yes, indeed. Thanks.

> >> diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c

> >> new file mode 100644

> >> index 000000000000..1870080d2c70

> >> --- /dev/null

> >> +++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c

> >> @@ -0,0 +1,346 @@

> >> +/** @file

> >> +  Device driver for the ChaosKey hardware random number generator.

> >> +

> >> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

> >> +

> >> +  This program and the accompanying materials

> >> +  are licensed and made available under the terms and conditions of the BSD

> >> +  License which accompanies this distribution. The full text of the license may

> >> +  be found at  http://opensource.org/licenses/bsd-license.php.

> >> +

> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> >> +

> >> +**/

> >> +

> >> +#include "ChaosKeyDriver.h"

> >> +

> >> +#include <Library/BaseMemoryLib.h>

> >> +#include <Library/DebugLib.h>

> >> +#include <Library/MemoryAllocationLib.h>

> >> +

> >> +STATIC

> >> +BOOLEAN

> >> +IsBulkInEndpoint (

> >> +  IN  EFI_USB_ENDPOINT_DESCRIPTOR     *Endpoint

> >> +  )

> >> +{

> >> +  if ((Endpoint->Attributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_BULK) {

> >> +    if (Endpoint->EndpointAddress & USB_ENDPOINT_DIR_IN) {

> >> +      return TRUE;

> >> +    }

> >> +  }

> >> +  return FALSE;

> >> +}

> >> +

> >> +

> >> +STATIC

> >> +EFI_STATUS

> >> +FindEndpoint (

> >> +  IN  CHAOSKEY_DEV *ChaosKey

> >> +  )

> >> +{

> >> +  EFI_USB_IO_PROTOCOL             *UsbIo;

> >> +  EFI_STATUS                      Status;

> >> +  UINTN                           Index;

> >> +  EFI_USB_INTERFACE_DESCRIPTOR    InterfaceDescriptor;

> >> +

> >> +  UsbIo = ChaosKey->UsbIo;

> >> +

> >> +  //

> >> +  // Get interface & endpoint descriptor

> >> +  //

> >> +  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);

> >> +  if (EFI_ERROR (Status)) {

> >> +    return Status;

> >> +  }

> >> +

> >> +  //

> >> +  // The ChaosKey provides two endpoints:

> >> +  // - The first one is the 'cooked' one, to be used as random data input

> >> +  // - The second one is the raw bitstream from the generator, higher

> >> +  //   throughput, but lower randomness.

> >> +  // So locate the first bulk IN endpoint and save it for later use.

> >> +  //

> >> +  for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {

> >> +    EFI_USB_ENDPOINT_DESCRIPTOR  Endpoint;

> >> +

> >> +    Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &Endpoint);

> >> +    if (EFI_ERROR (Status)) {

> >> +      DEBUG ((DEBUG_ERROR, "UsbGetEndPointDescriptor(%d) failed!\n", Index));

> >> +      return Status;

> >> +    }

> >> +

> >> +    if (IsBulkInEndpoint(&Endpoint)) {

> >> +      ChaosKey->EndpointAddress = Endpoint.EndpointAddress;

> >> +      ChaosKey->EndpointSize = Endpoint.MaxPacketSize;

> >> +      return EFI_SUCCESS;

> >> +    }

> >> +  }

> >> +

> >> +  DEBUG ((DEBUG_ERROR, "Failed to locate suitable BULK IN USB endpoint!\n"));

> >> +  return EFI_DEVICE_ERROR;

> >> +}

> >> +

> >> +

> >> +/**

> >> +  Returns information about the random number generation implementation.

> >> +

> >> +  @param[in]     This               A pointer to the EFI_RNG_PROTOCOL instance.

> >> +  @param[in,out] AlgorithmListSize  On input, the size in bytes of AlgorithmList

> >> +                                    On output with a return code of EFI_SUCCESS,

> >> +                                    the size in bytes of the data returned in

> >> +                                    AlgorithmList. On output with a return

> >> +                                    code of EFI_BUFFER_TOO_SMALL, the size of

> >> +                                    AlgorithmList required to obtain the list.

> >> +  @param[out] AlgorithmList         A caller-allocated memory buffer filled by

> >> +                                    the driver with one EFI_RNG_ALGORITHM

> >> +                                    element for each supported RNG algorithm.

> >> +                                    The list must not change across multiple

> >> +                                    calls to the same driver. The first

> >> +                                    algorithm in the list is the default

> >> +                                    algorithm for the driver.

> >> +

> >> +  @retval EFI_SUCCESS               The RNG algorithm list was returned

> >> +                                    successfully.

> >> +  @retval EFI_UNSUPPORTED           The services is not supported by this driver

> >> +  @retval EFI_DEVICE_ERROR          The list of algorithms could not be

> >> +                                    retrieved due to a hardware or firmware

> >> +                                    error.

> >> +  @retval EFI_INVALID_PARAMETER     One or more of the parameters are incorrect.

> >> +  @retval EFI_BUFFER_TOO_SMALL      The buffer RNGAlgorithmList is too small to

> >> +                                    hold the result.

> >> +

> >> +**/

> >> +STATIC

> >> +EFI_STATUS

> >> +EFIAPI

> >> +GetInfo (

> >> +  IN      EFI_RNG_PROTOCOL    *This,

> >> +  IN  OUT UINTN               *AlgorithmListSize,

> >> +  OUT     EFI_RNG_ALGORITHM   *AlgorithmList

> >> +)

> >> +{

> >> +  UINTN Size;

> >> +

> >> +  //

> >> +  // We only implement the raw algorithm

> >> +  //

> >> +  Size = sizeof gEfiRngAlgorithmRaw;

> >> +

> >> +  if (*AlgorithmListSize < Size) {

> >> +    *AlgorithmListSize = Size;

> >> +    return EFI_BUFFER_TOO_SMALL;

> >> +  }

> >> +

> >> +  gBS->CopyMem (AlgorithmList, &gEfiRngAlgorithmRaw, Size);

> >> +  *AlgorithmListSize = Size;

> >> +

> >> +  return EFI_SUCCESS;

> >> +}

> >> +

> >> +

> >> +/**

> >> +  Produces and returns an RNG value using either the default or specified RNG

> >> +  algorithm.

> >> +

> >> +  @param[in]  This                A pointer to the EFI_RNG_PROTOCOL instance.

> >> +  @param[in]  Algorithm           A pointer to the EFI_RNG_ALGORITHM that

> >> +                                  identifies the RNG algorithm to use. May be

> >> +                                  NULL in which case the function will use its

> >> +                                  default RNG algorithm.

> >> +  @param[in]  ValueLength         The length in bytes of the memory buffer

> >> +                                  pointed to by RNGValue. The driver shall

> >> +                                  return exactly this numbers of bytes.

> >> +  @param[out] Value               A caller-allocated memory buffer filled by the

> >> +                                  driver with the resulting RNG value.

> >> +

> >> +  @retval EFI_SUCCESS             The RNG value was returned successfully.

> >> +  @retval EFI_UNSUPPORTED         The algorithm specified by RNGAlgorithm is not

> >> +                                  supported by this driver.

> >> +  @retval EFI_DEVICE_ERROR        An RNG value could not be retrieved due to a

> >> +                                  hardware or firmware error.

> >> +  @retval EFI_NOT_READY           There is not enough random data available to

> >> +                                  satisfy the length requested by

> >> +                                  RNGValueLength.

> >> +  @retval EFI_INVALID_PARAMETER   RNGValue is NULL or RNGValueLength is zero.

> >> +

> >> +**/

> >> +STATIC

> >> +EFI_STATUS

> >> +EFIAPI

> >> +GetRNG (

> >> +  IN EFI_RNG_PROTOCOL   *This,

> >> +  IN EFI_RNG_ALGORITHM  *Algorithm OPTIONAL,

> >> +  IN UINTN              ValueLength,

> >> +  OUT UINT8             *Value

> >> +)

> >> +{

> >> +  EFI_STATUS        Status;

> >> +  CHAOSKEY_DEV      *ChaosKey;

> >> +  EFI_TPL           Tpl;

> >> +  UINT8             Buffer[CHAOSKEY_MAX_EP_SIZE];

> >> +  UINT8             *OutPointer;

> >> +  UINTN             OutSize;

> >> +  UINT32            Result;

> >> +

> >> +  if (Algorithm != NULL && !CompareGuid (Algorithm, &gEfiRngAlgorithmRaw)) {

> >> +    return EFI_UNSUPPORTED;

> >> +  }

> >> +

> >> +  ChaosKey = CHAOSKEY_DEV_FROM_THIS (This);

> >> +

> >> +  while (ValueLength > 0) {

> >> +    //

> >> +    // If more data is requested than the endpoint can deliver in a single

> >> +    // transfer, put it straight into the caller's buffer.

> >> +    //

> >> +    if (ValueLength >= ChaosKey->EndpointSize) {

> >> +      OutPointer = Value;

> >> +    } else {

> >> +      OutPointer = Buffer;

> >> +    }

> >> +    OutSize = ChaosKey->EndpointSize;

> >> +

> >> +    Tpl = gBS->RaiseTPL (TPL_NOTIFY);

> >

> > You mentioned to me on IRC that this should not be necessary, and that

> > UsbIo should adjust tpl as necessary.

> >

> > It is quite likely I misinterpreted behaviour I encountered when using

> > USB passthrough in QEMU for testing (and managed to break the host

> > system's driver).

> 

> I will remove it. We are dealing with an abstract protocol here, not

> hardware, and if completing a single bulk transfer does not tolerate

> interruptions, it should be up to the USB I/O protocol implementation

> to deal with that.


Yes, sounds good.

> >> diff --git a/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c

> >> new file mode 100644

> >> index 000000000000..a1a5a796d38d

> >> --- /dev/null

> >> +++ b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c

> >> @@ -0,0 +1,256 @@

> >> +/** @file

> >> +  Device driver for the ChaosKey hardware random number generator.

> >> +

> >> +  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>

> >> +

> >> +  This program and the accompanying materials

> >> +  are licensed and made available under the terms and conditions of the BSD

> >> +  License which accompanies this distribution. The full text of the license may

> >> +  be found at  http://opensource.org/licenses/bsd-license.php.

> >> +

> >> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

> >> +  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

> >> +

> >> +**/

> >> +

> >> +#include <Library/UefiDriverEntryPoint.h>

> >> +

> >> +#include "ChaosKeyDriver.h"

> >> +

> >> +/**

> >> +  Tests to see if this driver supports a given controller.

> >> +

> >> +  @param  This[in]                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL

> >> +                                   instance.

> >> +  @param  ControllerHandle[in]     The handle of the controller to test.

> >> +  @param  RemainingDevicePath[in]  The remaining device path.

> >> +                                   (Ignored - this is not a bus driver.)

> >> +

> >> +  @retval EFI_SUCCESS              The driver supports this controller.

> >> +  @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle is

> >> +                                   already being managed by the driver specified

> >> +                                   by This.

> >> +  @retval EFI_UNSUPPORTED          The device specified by ControllerHandle is

> >> +                                   not supported by the driver specified by This.

> >> +

> >> +**/

> >> +EFI_STATUS

> >> +EFIAPI

> >> +UsbHwrngDriverBindingSupported (

> >> +  IN EFI_DRIVER_BINDING_PROTOCOL  *This,

> >> +  IN EFI_HANDLE                   ControllerHandle,

> >> +  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath

> >> +  )

> >> +{

> >> +  EFI_USB_DEVICE_DESCRIPTOR  Device;

> >> +  EFI_USB_IO_PROTOCOL        *UsbIo;

> >> +  EFI_STATUS                 Status;

> >> +

> >> +  //

> >> +  //  Connect to the USB stack

> >> +  //

> >> +  Status = gBS->OpenProtocol (ControllerHandle,

> >> +                              &gEfiUsbIoProtocolGuid,

> >> +                              (VOID **) &UsbIo,

> >> +                              This->DriverBindingHandle,

> >> +                              ControllerHandle,

> >> +                              EFI_OPEN_PROTOCOL_BY_DRIVER);

> >> +  if (EFI_ERROR (Status)) {

> >> +    return Status;

> >> +  }

> >> +

> >> +  //

> >> +  //  Get the interface descriptor to check the USB class and find a transport

> >> +  //  protocol handler.

> >> +  //

> >> +  Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &Device);

> >> +  if (!EFI_ERROR (Status)) {

> >> +    //

> >> +    //  Validate the adapter

> >> +    //

> >> +    if ((Device.IdVendor != CHAOSKEY_VENDOR_ID) ||

> >> +        (Device.IdProduct != CHAOSKEY_PRODUCT_ID)) {

> >> +      Status = EFI_UNSUPPORTED;

> >> +    } else {

> >> +      DEBUG ((DEBUG_INIT | DEBUG_INFO,

> >> +        "ChaosKey (0x%04x:0x%04x) is my homeboy!\n",

> >> +        Device.IdVendor, Device.IdProduct));

> >

> > That was intended as a cute remark in my original blog post. Perhaps a

> > more descriptive message would be better.

> > "Detected ChaosKey RNG device." or such.

> >

> 

> OK, will change that.


Thanks.

> >> +      Status = EFI_SUCCESS;

> >> +    }

> >> +  }

> >> +

> >> +  //

> >> +  // Clean up.

> >> +  //

> >> +  gBS->CloseProtocol (ControllerHandle,

> >> +                      &gEfiUsbIoProtocolGuid,

> >> +                      This->DriverBindingHandle,

> >> +                      ControllerHandle);

> >> +

> >> +  return Status;

> >> +}

> >> +

> >> +

> >> +/**

> >> +  Starts a device controller or a bus controller.

> >> +

> >> +  @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL

> >> +                                   instance.

> >> +  @param[in]  ControllerHandle     The handle of the device to start. This

> >> +                                   handle must support a protocol interface that

> >> +                                   supplies an I/O abstraction to the driver.

> >> +  @param[in]  RemainingDevicePath  The remaining portion of the device path.

> >> +                                   (Ignored - this is not a bus driver.)

> >> +

> >> +  @retval EFI_SUCCESS              The device was started.

> >> +  @retval EFI_DEVICE_ERROR         The device could not be started due to a

> >> +                                   device error.

> >> +  @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a

> >> +                                   lack of resources.

> >> +

> >> +**/

> >> +EFI_STATUS

> >> +EFIAPI

> >> +UsbHwrngDriverBindingStart (

> >> +  IN EFI_DRIVER_BINDING_PROTOCOL  *This,

> >> +  IN EFI_HANDLE                   ControllerHandle,

> >> +  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL

> >> +  )

> >> +{

> >> +  return ChaosKeyInit (This->DriverBindingHandle, ControllerHandle);

> >> +}

> >> +

> >> +

> >> +/**

> >> +  Stops a device controller or a bus controller.

> >> +

> >> +  @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL

> >> +                                instance.

> >> +  @param[in]  ControllerHandle  A handle to the device being stopped. The handle

> >> +                                must support a bus specific I/O protocol for the

> >> +                                driver to use to stop the device.

> >> +  @param[in]  NumberOfChildren  The number of child device handles in

> >> +                                ChildHandleBuffer.

> >> +  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be

> >> +                                NULL if NumberOfChildren is 0.

> >> +

> >> +  @retval EFI_SUCCESS           The device was stopped.

> >> +  @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device

> >> +                                error.

> >> +

> >> +**/

> >> +EFI_STATUS

> >> +EFIAPI

> >> +UsbHwrngDriverBindingStop (

> >> +  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,

> >> +  IN  EFI_HANDLE                  ControllerHandle,

> >> +  IN  UINTN                       NumberOfChildren,

> >> +  IN  EFI_HANDLE                  *ChildHandleBuffer OPTIONAL

> >> +  )

> >> +{

> >> +  return ChaosKeyRelease (This->DriverBindingHandle, ControllerHandle);

> >> +}

> >> +

> >> +

> >> +STATIC

> >> +EFI_DRIVER_BINDING_PROTOCOL  gUsbDriverBinding = {

> >> +  UsbHwrngDriverBindingSupported,

> >> +  UsbHwrngDriverBindingStart,

> >> +  UsbHwrngDriverBindingStop,

> >> +  0xa,

> >> +  NULL,

> >> +  NULL

> >> +};

> >> +

> >> +

> >> +/**

> >> +  The entry point of ChaosKey UEFI Driver.

> >> +

> >> +  @param  ImageHandle                The image handle of the UEFI Driver.

> >> +  @param  SystemTable                A pointer to the EFI System Table.

> >> +

> >> +  @retval  EFI_SUCCESS               The Driver or UEFI Driver exited normally.

> >> +  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than

> >> +                                     SystemTable->Hdr.Revision.

> >> +

> >> +**/

> >> +EFI_STATUS

> >> +EFIAPI

> >> +EntryPoint (

> >> +  IN  EFI_HANDLE          ImageHandle,

> >> +  IN  EFI_SYSTEM_TABLE    *SystemTable

> >> +  )

> >> +{

> >> +  EFI_STATUS    Status;

> >> +

> >> +  //

> >> +  //  Add the driver to the list of drivers

> >> +  //

> >> +  Status = EfiLibInstallDriverBindingComponentName2 (

> >> +             ImageHandle, SystemTable, &gUsbDriverBinding, ImageHandle,

> >> +             &gChaosKeyDriverComponentName, &gChaosKeyDriverComponentName2);

> >

> > Random question ... do we _want_ to provide

> > EFI_COMPONENT_NAME_PROTOCOL, in addition to

> > EFI_COMPONENT_NAME2_PROTOCOL, for new drivers?

> >

> 

> If this is supposed to be a UEFI Driver Model driver, I'd suggest we

> keep it for compatibility with older firmwares.


I am not strongly opposed to it, but I think the following statement
from UEFI 2.7 suggests that it may be worth starting to consider
leaving it out of new drivers:

"To provide backwards compatibility with preexisting EFI 1.10 drivers,
a UEFI platforms may support deprecated protocols which represent
languages in the ISO 639-2 format. This includes the following
protocols:
UNICODE_COLLATION_INTERFACE,
EFI_DRIVER_CONFIGURATION_PROTOCOL,
EFI_DRIVER_DIAGNOSTICS_PROTOCOL,
and EFI_COMPONENT_NAME_PROTOCOL."

The protocol is mentioned in only one other location in the
specification.

> > I should also point out that I don't actually see a DRIVER NAME when

> > executing the "drivers" command from UEFI Shell.

> 

> That worked fine for me, both for 'drivers' and 'devices'. Which

> platform? Does it display other driver names correctly?


*cough*

No.

Platform/ARM/VExpressPkg/ArmVExpress.dsc.inc:
gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|TRUE
Platform/ARM/VExpressPkg/ArmVExpress.dsc.inc:
gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable|TRUE

And this has been cargo-culted across to other platforms.

Or, well, the FAT driver does show up (how!?).
Ah, FatBinPkg.

Ok, the world makes sense again, I will delete these illogical
overrides on all ARM platforms in edk2-platforms.
And probably from BeagleBoardPkh as well - I guess this is where this
originated, together with Ebl. In an effort to reduce image size.
Since BeagleBoardPkg now includes UefiShell, I don't think we need to
worry about that as much.

It is time for me to start deduplicating the common bits of platform
configuration, originally prototyped by Ryan a couple of years back,
in order to avoid this kind of spread in future.

/
    Leif
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
diff mbox

Patch

diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c
new file mode 100644
index 000000000000..1870080d2c70
--- /dev/null
+++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.c
@@ -0,0 +1,346 @@ 
+/** @file
+  Device driver for the ChaosKey hardware random number generator.
+
+  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD
+  License which accompanies this distribution. The full text of the license may
+  be found at  http://opensource.org/licenses/bsd-license.php.
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "ChaosKeyDriver.h"
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+
+STATIC
+BOOLEAN
+IsBulkInEndpoint (
+  IN  EFI_USB_ENDPOINT_DESCRIPTOR     *Endpoint
+  )
+{
+  if ((Endpoint->Attributes & USB_ENDPOINT_TYPE_MASK) == USB_ENDPOINT_BULK) {
+    if (Endpoint->EndpointAddress & USB_ENDPOINT_DIR_IN) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
+
+STATIC
+EFI_STATUS
+FindEndpoint (
+  IN  CHAOSKEY_DEV *ChaosKey
+  )
+{
+  EFI_USB_IO_PROTOCOL             *UsbIo;
+  EFI_STATUS                      Status;
+  UINTN                           Index;
+  EFI_USB_INTERFACE_DESCRIPTOR    InterfaceDescriptor;
+
+  UsbIo = ChaosKey->UsbIo;
+
+  //
+  // Get interface & endpoint descriptor
+  //
+  Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  //
+  // The ChaosKey provides two endpoints:
+  // - The first one is the 'cooked' one, to be used as random data input
+  // - The second one is the raw bitstream from the generator, higher
+  //   throughput, but lower randomness.
+  // So locate the first bulk IN endpoint and save it for later use.
+  //
+  for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {
+    EFI_USB_ENDPOINT_DESCRIPTOR  Endpoint;
+
+    Status = UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &Endpoint);
+    if (EFI_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR, "UsbGetEndPointDescriptor(%d) failed!\n", Index));
+      return Status;
+    }
+
+    if (IsBulkInEndpoint(&Endpoint)) {
+      ChaosKey->EndpointAddress = Endpoint.EndpointAddress;
+      ChaosKey->EndpointSize = Endpoint.MaxPacketSize;
+      return EFI_SUCCESS;
+    }
+  }
+
+  DEBUG ((DEBUG_ERROR, "Failed to locate suitable BULK IN USB endpoint!\n"));
+  return EFI_DEVICE_ERROR;
+}
+
+
+/**
+  Returns information about the random number generation implementation.
+
+  @param[in]     This               A pointer to the EFI_RNG_PROTOCOL instance.
+  @param[in,out] AlgorithmListSize  On input, the size in bytes of AlgorithmList
+                                    On output with a return code of EFI_SUCCESS,
+                                    the size in bytes of the data returned in
+                                    AlgorithmList. On output with a return
+                                    code of EFI_BUFFER_TOO_SMALL, the size of
+                                    AlgorithmList required to obtain the list.
+  @param[out] AlgorithmList         A caller-allocated memory buffer filled by
+                                    the driver with one EFI_RNG_ALGORITHM
+                                    element for each supported RNG algorithm.
+                                    The list must not change across multiple
+                                    calls to the same driver. The first
+                                    algorithm in the list is the default
+                                    algorithm for the driver.
+
+  @retval EFI_SUCCESS               The RNG algorithm list was returned
+                                    successfully.
+  @retval EFI_UNSUPPORTED           The services is not supported by this driver
+  @retval EFI_DEVICE_ERROR          The list of algorithms could not be
+                                    retrieved due to a hardware or firmware
+                                    error.
+  @retval EFI_INVALID_PARAMETER     One or more of the parameters are incorrect.
+  @retval EFI_BUFFER_TOO_SMALL      The buffer RNGAlgorithmList is too small to
+                                    hold the result.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+GetInfo (
+  IN      EFI_RNG_PROTOCOL    *This,
+  IN  OUT UINTN               *AlgorithmListSize,
+  OUT     EFI_RNG_ALGORITHM   *AlgorithmList
+)
+{
+  UINTN Size;
+
+  //
+  // We only implement the raw algorithm
+  //
+  Size = sizeof gEfiRngAlgorithmRaw;
+
+  if (*AlgorithmListSize < Size) {
+    *AlgorithmListSize = Size;
+    return EFI_BUFFER_TOO_SMALL;
+  }
+
+  gBS->CopyMem (AlgorithmList, &gEfiRngAlgorithmRaw, Size);
+  *AlgorithmListSize = Size;
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Produces and returns an RNG value using either the default or specified RNG
+  algorithm.
+
+  @param[in]  This                A pointer to the EFI_RNG_PROTOCOL instance.
+  @param[in]  Algorithm           A pointer to the EFI_RNG_ALGORITHM that
+                                  identifies the RNG algorithm to use. May be
+                                  NULL in which case the function will use its
+                                  default RNG algorithm.
+  @param[in]  ValueLength         The length in bytes of the memory buffer
+                                  pointed to by RNGValue. The driver shall
+                                  return exactly this numbers of bytes.
+  @param[out] Value               A caller-allocated memory buffer filled by the
+                                  driver with the resulting RNG value.
+
+  @retval EFI_SUCCESS             The RNG value was returned successfully.
+  @retval EFI_UNSUPPORTED         The algorithm specified by RNGAlgorithm is not
+                                  supported by this driver.
+  @retval EFI_DEVICE_ERROR        An RNG value could not be retrieved due to a
+                                  hardware or firmware error.
+  @retval EFI_NOT_READY           There is not enough random data available to
+                                  satisfy the length requested by
+                                  RNGValueLength.
+  @retval EFI_INVALID_PARAMETER   RNGValue is NULL or RNGValueLength is zero.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+GetRNG (
+  IN EFI_RNG_PROTOCOL   *This,
+  IN EFI_RNG_ALGORITHM  *Algorithm OPTIONAL,
+  IN UINTN              ValueLength,
+  OUT UINT8             *Value
+)
+{
+  EFI_STATUS        Status;
+  CHAOSKEY_DEV      *ChaosKey;
+  EFI_TPL           Tpl;
+  UINT8             Buffer[CHAOSKEY_MAX_EP_SIZE];
+  UINT8             *OutPointer;
+  UINTN             OutSize;
+  UINT32            Result;
+
+  if (Algorithm != NULL && !CompareGuid (Algorithm, &gEfiRngAlgorithmRaw)) {
+    return EFI_UNSUPPORTED;
+  }
+
+  ChaosKey = CHAOSKEY_DEV_FROM_THIS (This);
+
+  while (ValueLength > 0) {
+    //
+    // If more data is requested than the endpoint can deliver in a single
+    // transfer, put it straight into the caller's buffer.
+    //
+    if (ValueLength >= ChaosKey->EndpointSize) {
+      OutPointer = Value;
+    } else {
+      OutPointer = Buffer;
+    }
+    OutSize = ChaosKey->EndpointSize;
+
+    Tpl = gBS->RaiseTPL (TPL_NOTIFY);
+
+    Status = ChaosKey->UsbIo->UsbBulkTransfer (ChaosKey->UsbIo,
+                                               ChaosKey->EndpointAddress,
+                                               OutPointer,
+                                               &OutSize,
+                                               CHAOSKEY_TIMEOUT,
+                                               &Result);
+
+    gBS->RestoreTPL (Tpl);
+
+    if (Status == EFI_TIMEOUT) {
+      DEBUG ((DEBUG_ERROR, "Bulk transfer timed out, USB status == %d\n",
+        Result));
+      return EFI_NOT_READY;
+    } else if (EFI_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR,
+        "Bulk transfer failed, Status == %r, USB status == %d\n",
+        Status, Result));
+      return EFI_DEVICE_ERROR;
+    }
+
+    OutSize = MIN (OutSize, ValueLength);
+
+    if (Value != Buffer) {
+      gBS->CopyMem (Value, Buffer, OutSize);
+    }
+    Value += OutSize;
+    ValueLength -= OutSize;
+  }
+  return EFI_SUCCESS;
+}
+
+
+EFI_STATUS
+ChaosKeyInit (
+  IN      EFI_HANDLE        DriverBindingHandle,
+  IN      EFI_HANDLE        ControllerHandle
+  )
+{
+  EFI_STATUS                Status;
+  CHAOSKEY_DEV              *ChaosKey;
+
+  Status  = gBS->AllocatePool (EfiBootServicesData,
+                               sizeof (CHAOSKEY_DEV),
+                               (VOID **) &ChaosKey);
+  if (EFI_ERROR (Status)) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  ChaosKey->Signature         = CHAOSKEY_DEV_SIGNATURE;
+  ChaosKey->Rng.GetInfo       = GetInfo;
+  ChaosKey->Rng.GetRNG        = GetRNG;
+
+  //
+  // Open USB I/O Protocol
+  //
+  Status = gBS->OpenProtocol (ControllerHandle,
+                              &gEfiUsbIoProtocolGuid,
+                              (VOID **)&ChaosKey->UsbIo,
+                              DriverBindingHandle,
+                              ControllerHandle,
+                              EFI_OPEN_PROTOCOL_BY_DRIVER);
+  if (EFI_ERROR (Status)) {
+    goto ErrorFreeDev;
+  }
+
+  Status = FindEndpoint (ChaosKey);
+  if (EFI_ERROR (Status)) {
+    goto ErrorCloseProtocol;
+  }
+
+  //
+  // The following can only occur if the Chaoskey is suddenly reissued
+  // as a high speed or super speed device under the same VID/PID.
+  //
+  ASSERT (ChaosKey->EndpointSize <= CHAOSKEY_MAX_EP_SIZE);
+
+  Status = gBS->InstallProtocolInterface (&ControllerHandle,
+                                          &gEfiRngProtocolGuid,
+                                          EFI_NATIVE_INTERFACE,
+                                          &ChaosKey->Rng);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR,
+      "Failed to install RNG protocol interface (Status == %r)\n",
+    Status));
+    goto ErrorCloseProtocol;
+  }
+
+  return EFI_SUCCESS;
+
+ErrorCloseProtocol:
+  gBS->CloseProtocol (ControllerHandle, &gEfiUsbIoProtocolGuid,
+         DriverBindingHandle, ControllerHandle);
+
+ErrorFreeDev:
+  gBS->FreePool (ChaosKey);
+
+  return Status;
+}
+
+EFI_STATUS
+ChaosKeyRelease (
+  IN  EFI_HANDLE        DriverBindingHandle,
+  IN  EFI_HANDLE        ControllerHandle
+  )
+{
+  EFI_RNG_PROTOCOL    *Rng;
+  CHAOSKEY_DEV        *ChaosKey;
+  EFI_STATUS          Status;
+
+  Status = gBS->HandleProtocol (ControllerHandle,
+                                &gEfiRngProtocolGuid,
+                                (VOID **)&Rng);
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  ChaosKey = CHAOSKEY_DEV_FROM_THIS (Rng);
+
+  Status = gBS->UninstallProtocolInterface (ControllerHandle,
+                                            &gEfiRngProtocolGuid,
+                                            Rng);
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = gBS->CloseProtocol (ControllerHandle,
+                               &gEfiUsbIoProtocolGuid,
+                               DriverBindingHandle,
+                               ControllerHandle);
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  gBS->FreePool (ChaosKey);
+
+  return EFI_SUCCESS;
+}
diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h
new file mode 100644
index 000000000000..153deb4edb1c
--- /dev/null
+++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDriver.h
@@ -0,0 +1,61 @@ 
+/** @file
+  Header file for the ChaosKey hardware random number generator.
+
+  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD
+  License which accompanies this distribution. The full text of the license may
+  be found at  http://opensource.org/licenses/bsd-license.php.
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _CHAOSKEY_USB_HWRNG_DRIVER_H_
+#define _CHAOSKEY_USB_HWRNG_DRIVER_H_
+
+#include <Uefi.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/Rng.h>
+#include <Protocol/UsbIo.h>
+
+#define CHAOSKEY_VENDOR_ID      0x1d50  /* OpenMoko */
+#define CHAOSKEY_PRODUCT_ID     0x60c6  /* ChaosKey */
+
+#define CHAOSKEY_TIMEOUT        10 // ms
+#define CHAOSKEY_MAX_EP_SIZE    64 // max EP size for full-speed devices
+
+#define CHAOSKEY_DEV_SIGNATURE  SIGNATURE_32('c','h','k','e')
+
+typedef struct {
+  UINT32                        Signature;
+  UINT16                        EndpointAddress;
+  UINT16                        EndpointSize;
+  EFI_USB_IO_PROTOCOL           *UsbIo;
+  EFI_RNG_PROTOCOL              Rng;
+} CHAOSKEY_DEV;
+
+#define CHAOSKEY_DEV_FROM_THIS(a) \
+  CR(a, CHAOSKEY_DEV, Rng, CHAOSKEY_DEV_SIGNATURE)
+
+extern EFI_COMPONENT_NAME_PROTOCOL gChaosKeyDriverComponentName;
+extern EFI_COMPONENT_NAME2_PROTOCOL gChaosKeyDriverComponentName2;
+
+EFI_STATUS
+ChaosKeyInit (
+  IN  EFI_HANDLE        DriverBindingHandle,
+  IN  EFI_HANDLE        ControllerHandle
+  );
+
+EFI_STATUS
+ChaosKeyRelease (
+  IN  EFI_HANDLE        DriverBindingHandle,
+  IN  EFI_HANDLE        ControllerHandle
+  );
+
+#endif // _CHAOSKEY_USB_HWRNG_DRIVER_H_
diff --git a/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf
new file mode 100644
index 000000000000..2ff84956ca72
--- /dev/null
+++ b/Silicon/Openmoko/ChaosKeyDxe/ChaosKeyDxe.inf
@@ -0,0 +1,48 @@ 
+## @file
+#  Device driver for the ChaosKey hardware random number generator.
+#
+#  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>
+#
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD
+#  License which accompanies this distribution. The full text of the license may
+#  be found at  http://opensource.org/licenses/bsd-license.php.
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010019
+  BASE_NAME                      = ChaosKeyDxe
+  FILE_GUID                      = 9A54122B-F5E4-40D8-AE61-A71E406ED449
+  MODULE_TYPE                    = UEFI_DRIVER
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = EntryPoint
+  UNLOAD_IMAGE                   = UnloadImage
+
+#
+#  VALID_ARCHITECTURES           = AARCH64 ARM EBC IA32 IPF X64
+#
+
+[Sources]
+  ChaosKeyDriver.c
+  ChaosKeyDriver.h
+  ComponentName.c
+  DriverBinding.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  UefiBootServicesTableLib
+  UefiDriverEntryPoint
+  UefiLib
+
+[Protocols]
+  gEfiRngProtocolGuid                 # PROTOCOL BY_START
+  gEfiUsbIoProtocolGuid               # PROTOCOL TO_START
+
+[Guids]
+  gEfiRngAlgorithmRaw
diff --git a/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c b/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c
new file mode 100644
index 000000000000..81f2130bcd9e
--- /dev/null
+++ b/Silicon/Openmoko/ChaosKeyDxe/ComponentName.c
@@ -0,0 +1,205 @@ 
+/** @file
+  UEFI Component Name(2) protocol implementation for ChaosKey driver.
+
+  Copyright (c) 2017, Linaro Ltd. All rights reserved.
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "ChaosKeyDriver.h"
+
+STATIC EFI_UNICODE_STRING_TABLE mChaosKeyDriverNameTable[] = {
+  {
+    "eng;en",
+    (CHAR16 *)L"ChaosKey RNG USB driver"
+  },
+  {
+    NULL,
+    NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mChaosKeyControllerNameTable[] = {
+  {
+    "eng;en",
+    (CHAR16 *)L"ChaosKey Random Number Generator (USB)"
+  },
+  {
+    NULL,
+    NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
+                                EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]          A pointer to a Null-terminated ASCII string
+                                array indicating the language. This is the
+                                language of the driver name that the caller is
+                                requesting, and it must match one of the
+                                languages specified in SupportedLanguages. The
+                                number of languages supported by a driver is up
+                                to the driver writer. Language is specified
+                                in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]       A pointer to the Unicode string to return.
+                                This Unicode string is the name of the
+                                driver specified by This in the language
+                                specified by Language.
+
+  @retval EFI_SUCCESS           The Unicode string for the Driver specified by
+                                This and the language specified by Language was
+                                returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED       The driver specified by This does not support
+                                the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ChaosKeyDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8                        *Language,
+  OUT CHAR16                       **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+           Language,
+           This->SupportedLanguages,
+           mChaosKeyDriverNameTable,
+           DriverName,
+           (BOOLEAN)(This == &gChaosKeyDriverComponentName)
+           );
+}
+
+/**
+  Retrieves a Unicode string that is the user readable name of the controller
+  that is being managed by a driver.
+
+  This function retrieves the user readable name of the controller specified by
+  ControllerHandle and ChildHandle in the form of a Unicode string. If the
+  driver specified by This has a user readable name in the language specified by
+  Language, then a pointer to the controller name is returned in ControllerName,
+  and EFI_SUCCESS is returned.  If the driver specified by This is not currently
+  managing the controller specified by ControllerHandle and ChildHandle,
+  then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
+  support the language specified by Language, then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
+                                EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  ControllerHandle[in]  The handle of a controller that the driver
+                                specified by This is managing.  This handle
+                                specifies the controller whose name is to be
+                                returned.
+
+  @param  ChildHandle[in]       The handle of the child controller to retrieve
+                                the name of.  This is an optional parameter that
+                                may be NULL.  It will be NULL for device
+                                drivers.  It will also be NULL for a bus drivers
+                                that wish to retrieve the name of the bus
+                                controller.  It will not be NULL for a bus
+                                driver that wishes to retrieve the name of a
+                                child controller.
+
+  @param  Language[in]          A pointer to a Null-terminated ASCII string
+                                array indicating the language.  This is the
+                                language of the driver name that the caller is
+                                requesting, and it must match one of the
+                                languages specified in SupportedLanguages. The
+                                number of languages supported by a driver is up
+                                to the driver writer. Language is specified in
+                                RFC 4646 or ISO 639-2 language code format.
+
+  @param  ControllerName[out]   A pointer to the Unicode string to return.
+                                This Unicode string is the name of the
+                                controller specified by ControllerHandle and
+                                ChildHandle in the language specified by
+                                Language from the point of view of the driver
+                                specified by This.
+
+  @retval EFI_SUCCESS           The Unicode string for the user readable name in
+                                the language specified by Language for the
+                                driver specified by This was returned in
+                                DriverName.
+
+  @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
+
+  @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
+                                EFI_HANDLE.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER ControllerName is NULL.
+
+  @retval EFI_UNSUPPORTED       The driver specified by This is not currently
+                                managing the controller specified by
+                                ControllerHandle and ChildHandle.
+
+  @retval EFI_UNSUPPORTED       The driver specified by This does not support
+                                the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ChaosKeyGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
+  IN  EFI_HANDLE                                      ControllerHandle,
+  IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
+  IN  CHAR8                                           *Language,
+  OUT CHAR16                                          **ControllerName
+  )
+{
+  if (ChildHandle != NULL) {
+    return EFI_UNSUPPORTED;
+  }
+
+  return LookupUnicodeString2 (
+           Language,
+           This->SupportedLanguages,
+           mChaosKeyControllerNameTable,
+           ControllerName,
+           (BOOLEAN)(This == &gChaosKeyDriverComponentName)
+           );
+}
+
+//
+// EFI Component Name Protocol
+//
+EFI_COMPONENT_NAME_PROTOCOL gChaosKeyDriverComponentName = {
+  ChaosKeyDriverName,
+  ChaosKeyControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+EFI_COMPONENT_NAME2_PROTOCOL gChaosKeyDriverComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) ChaosKeyGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) ChaosKeyGetControllerName,
+  "en"
+};
diff --git a/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c
new file mode 100644
index 000000000000..a1a5a796d38d
--- /dev/null
+++ b/Silicon/Openmoko/ChaosKeyDxe/DriverBinding.c
@@ -0,0 +1,256 @@ 
+/** @file
+  Device driver for the ChaosKey hardware random number generator.
+
+  Copyright (c) 2016 - 2017, Linaro Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD
+  License which accompanies this distribution. The full text of the license may
+  be found at  http://opensource.org/licenses/bsd-license.php.
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Library/UefiDriverEntryPoint.h>
+
+#include "ChaosKeyDriver.h"
+
+/**
+  Tests to see if this driver supports a given controller.
+
+  @param  This[in]                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL
+                                   instance.
+  @param  ControllerHandle[in]     The handle of the controller to test.
+  @param  RemainingDevicePath[in]  The remaining device path.
+                                   (Ignored - this is not a bus driver.)
+
+  @retval EFI_SUCCESS              The driver supports this controller.
+  @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle is
+                                   already being managed by the driver specified
+                                   by This.
+  @retval EFI_UNSUPPORTED          The device specified by ControllerHandle is
+                                   not supported by the driver specified by This.
+
+**/
+EFI_STATUS
+EFIAPI
+UsbHwrngDriverBindingSupported (
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
+  IN EFI_HANDLE                   ControllerHandle,
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
+  )
+{
+  EFI_USB_DEVICE_DESCRIPTOR  Device;
+  EFI_USB_IO_PROTOCOL        *UsbIo;
+  EFI_STATUS                 Status;
+
+  //
+  //  Connect to the USB stack
+  //
+  Status = gBS->OpenProtocol (ControllerHandle,
+                              &gEfiUsbIoProtocolGuid,
+                              (VOID **) &UsbIo,
+                              This->DriverBindingHandle,
+                              ControllerHandle,
+                              EFI_OPEN_PROTOCOL_BY_DRIVER);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  //
+  //  Get the interface descriptor to check the USB class and find a transport
+  //  protocol handler.
+  //
+  Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &Device);
+  if (!EFI_ERROR (Status)) {
+    //
+    //  Validate the adapter
+    //
+    if ((Device.IdVendor != CHAOSKEY_VENDOR_ID) ||
+        (Device.IdProduct != CHAOSKEY_PRODUCT_ID)) {
+      Status = EFI_UNSUPPORTED;
+    } else {
+      DEBUG ((DEBUG_INIT | DEBUG_INFO,
+        "ChaosKey (0x%04x:0x%04x) is my homeboy!\n",
+        Device.IdVendor, Device.IdProduct));
+      Status = EFI_SUCCESS;
+    }
+  }
+
+  //
+  // Clean up.
+  //
+  gBS->CloseProtocol (ControllerHandle,
+                      &gEfiUsbIoProtocolGuid,
+                      This->DriverBindingHandle,
+                      ControllerHandle);
+
+  return Status;
+}
+
+
+/**
+  Starts a device controller or a bus controller.
+
+  @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL
+                                   instance.
+  @param[in]  ControllerHandle     The handle of the device to start. This
+                                   handle must support a protocol interface that
+                                   supplies an I/O abstraction to the driver.
+  @param[in]  RemainingDevicePath  The remaining portion of the device path.
+                                   (Ignored - this is not a bus driver.)
+
+  @retval EFI_SUCCESS              The device was started.
+  @retval EFI_DEVICE_ERROR         The device could not be started due to a
+                                   device error.
+  @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a
+                                   lack of resources.
+
+**/
+EFI_STATUS
+EFIAPI
+UsbHwrngDriverBindingStart (
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
+  IN EFI_HANDLE                   ControllerHandle,
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL
+  )
+{
+  return ChaosKeyInit (This->DriverBindingHandle, ControllerHandle);
+}
+
+
+/**
+  Stops a device controller or a bus controller.
+
+  @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL
+                                instance.
+  @param[in]  ControllerHandle  A handle to the device being stopped. The handle
+                                must support a bus specific I/O protocol for the
+                                driver to use to stop the device.
+  @param[in]  NumberOfChildren  The number of child device handles in
+                                ChildHandleBuffer.
+  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be
+                                NULL if NumberOfChildren is 0.
+
+  @retval EFI_SUCCESS           The device was stopped.
+  @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device
+                                error.
+
+**/
+EFI_STATUS
+EFIAPI
+UsbHwrngDriverBindingStop (
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
+  IN  EFI_HANDLE                  ControllerHandle,
+  IN  UINTN                       NumberOfChildren,
+  IN  EFI_HANDLE                  *ChildHandleBuffer OPTIONAL
+  )
+{
+  return ChaosKeyRelease (This->DriverBindingHandle, ControllerHandle);
+}
+
+
+STATIC
+EFI_DRIVER_BINDING_PROTOCOL  gUsbDriverBinding = {
+  UsbHwrngDriverBindingSupported,
+  UsbHwrngDriverBindingStart,
+  UsbHwrngDriverBindingStop,
+  0xa,
+  NULL,
+  NULL
+};
+
+
+/**
+  The entry point of ChaosKey UEFI Driver.
+
+  @param  ImageHandle                The image handle of the UEFI Driver.
+  @param  SystemTable                A pointer to the EFI System Table.
+
+  @retval  EFI_SUCCESS               The Driver or UEFI Driver exited normally.
+  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
+                                     SystemTable->Hdr.Revision.
+
+**/
+EFI_STATUS
+EFIAPI
+EntryPoint (
+  IN  EFI_HANDLE          ImageHandle,
+  IN  EFI_SYSTEM_TABLE    *SystemTable
+  )
+{
+  EFI_STATUS    Status;
+
+  //
+  //  Add the driver to the list of drivers
+  //
+  Status = EfiLibInstallDriverBindingComponentName2 (
+             ImageHandle, SystemTable, &gUsbDriverBinding, ImageHandle,
+             &gChaosKeyDriverComponentName, &gChaosKeyDriverComponentName2);
+  ASSERT_EFI_ERROR (Status);
+
+  DEBUG ((DEBUG_INIT | DEBUG_INFO, "*** Installed ChaosKey driver! ***\n"));
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Unload function for the ChaosKey Driver.
+
+  @param  ImageHandle[in]        The allocated handle for the EFI image
+
+  @retval EFI_SUCCESS            The driver was unloaded successfully
+  @retval EFI_INVALID_PARAMETER  ImageHandle is not a valid image handle.
+
+**/
+EFI_STATUS
+EFIAPI
+UnloadImage (
+  IN EFI_HANDLE  ImageHandle
+  )
+{
+  EFI_STATUS  Status;
+  EFI_HANDLE  *HandleBuffer;
+  UINTN       HandleCount;
+  UINTN       Index;
+
+  //
+  // Retrieve all USB I/O handles in the handle database
+  //
+  Status = gBS->LocateHandleBuffer (ByProtocol,
+                                    &gEfiUsbIoProtocolGuid,
+                                    NULL,
+                                    &HandleCount,
+                                    &HandleBuffer);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  //
+  // Disconnect the driver from the handles in the handle database
+  //
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->DisconnectController (HandleBuffer[Index],
+                                        gImageHandle,
+                                        NULL);
+  }
+
+  //
+  // Free the handle array
+  //
+  gBS->FreePool (HandleBuffer);
+
+  //
+  // Uninstall protocols installed by the driver in its entrypoint
+  //
+  Status = gBS->UninstallMultipleProtocolInterfaces (ImageHandle,
+                  &gEfiDriverBindingProtocolGuid,
+                  &gUsbDriverBinding,
+                  NULL
+                  );
+
+  return EFI_SUCCESS;
+}