public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Nhi Pham" <nhi@os.amperecomputing.com>
To: devel@edk2.groups.io
Cc: Vu Nguyen <vunguyen@os.amperecomputing.com>
Subject: [edk2-platforms][PATCH 17/34] Silicon/Ampere: Add Random Number Generator Support
Date: Wed,  9 Dec 2020 16:25:14 +0700	[thread overview]
Message-ID: <20201209092531.30867-18-nhi@os.amperecomputing.com> (raw)
In-Reply-To: <20201209092531.30867-1-nhi@os.amperecomputing.com>

From: Vu Nguyen <vunguyen@os.amperecomputing.com>

This change adds support for gEfiRngProtocolGuid.

Signed-off-by: Vu Nguyen <vunguyen@os.amperecomputing.com>
---
 Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc |   5 +
 Platform/Ampere/JadePkg/Jade.fdf              |   4 +
 Silicon/Ampere/Drivers/RngDxe/RngDxe.inf      |  57 +++++
 Silicon/Ampere/Drivers/RngDxe/RngDxe.c        | 246 ++++++++++++++++++++
 Silicon/Ampere/Drivers/RngDxe/RngDxe.uni      |  10 +
 Silicon/Ampere/Drivers/RngDxe/RngDxeExtra.uni |   9 +
 6 files changed, 331 insertions(+)

diff --git a/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc b/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc
index fab2d3fa800f..cb03f7fc076f 100755
--- a/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc
+++ b/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc
@@ -665,6 +665,11 @@ [Components.common]
   MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
   Silicon/Ampere/AmpereAltraPkg/Bus/Pci/PciHostBridgeDxe/PciHostBridgeDxe.inf
 
+  #
+  # Random Number Generator Support
+  #
+  Silicon/Ampere/Drivers/RngDxe/RngDxe.inf
+
   #
   # Bds
   #
diff --git a/Platform/Ampere/JadePkg/Jade.fdf b/Platform/Ampere/JadePkg/Jade.fdf
index bd005ccb09d3..f826b14d67d0 100755
--- a/Platform/Ampere/JadePkg/Jade.fdf
+++ b/Platform/Ampere/JadePkg/Jade.fdf
@@ -313,6 +313,10 @@ [FV.FvMain]
   #
   INF Drivers/ASpeed/ASpeedGopBinPkg/ASpeedAst2500GopDxe.inf
 
+  # Random Number Generator Support
+  #
+  INF Silicon/Ampere/Drivers/RngDxe/RngDxe.inf
+
   #
   # UEFI application (Shell Embedded Boot Loader)
   #
diff --git a/Silicon/Ampere/Drivers/RngDxe/RngDxe.inf b/Silicon/Ampere/Drivers/RngDxe/RngDxe.inf
new file mode 100644
index 000000000000..f8f90a4580d4
--- /dev/null
+++ b/Silicon/Ampere/Drivers/RngDxe/RngDxe.inf
@@ -0,0 +1,57 @@
+## @file
+#
+# Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = RngDxe
+  FILE_GUID                      = 67D6517B-7279-4F8E-B273-BDF3F7998016
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = RngDriverEntry
+  MODULE_UNI_FILE                = RngDxe.uni
+
+[Sources.common]
+  RngDxe.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  ArmPkg/ArmPkg.dec
+  ArmPlatformPkg/ArmPlatformPkg.dec
+  Silicon/Ampere/AmperePkg.dec
+  Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dec
+
+[LibraryClasses]
+  UefiLib
+  UefiBootServicesTableLib
+  BaseLib
+  DebugLib
+  UefiDriverEntryPoint
+  SMProLib
+
+[Guids]
+  gEfiRngAlgorithmRaw                 ## SOMETIMES_PRODUCES    ## GUID        # Unique ID of the algorithm for RNG
+
+[Protocols]
+  gEfiRngProtocolGuid                ## PRODUCES
+
+[Depex]
+  TRUE
+
+[UserExtensions.TianoCore."ExtraFiles"]
+  RngDxeExtra.uni
+
+[FixedPcd]
+  gArmPlatformTokenSpaceGuid.PcdCoreCount
+  gArmPlatformTokenSpaceGuid.PcdClusterCount
+
+  gAmpereTokenSpaceGuid.PcdTurboDefaultFreq
+  gAmpereTokenSpaceGuid.PcdSmproDb
+  gAmpereTokenSpaceGuid.PcdSmproDbBaseReg
+  gAmpereTokenSpaceGuid.PcdSmproEfuseShadow0
+  gAmpereTokenSpaceGuid.PcdSmproI2cBmcBusAddr
+  gAmpereTokenSpaceGuid.PcdSmproNsMailboxIndex
diff --git a/Silicon/Ampere/Drivers/RngDxe/RngDxe.c b/Silicon/Ampere/Drivers/RngDxe/RngDxe.c
new file mode 100644
index 000000000000..baac9e8196b0
--- /dev/null
+++ b/Silicon/Ampere/Drivers/RngDxe/RngDxe.c
@@ -0,0 +1,246 @@
+/** @file
+
+  Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Library/BaseMemoryLib.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/SMProLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/SMProInterface.h>
+#include <Protocol/Rng.h>
+#include <Platform/Ac01.h>
+
+/*
+ * Supported RNG Algorithms list by this driver.
+ */
+EFI_RNG_ALGORITHM mSupportedRngAlgorithms[] = {
+  EFI_RNG_ALGORITHM_RAW
+};
+
+enum {
+  SCP_RNG_GET_TRNG = 1
+};
+
+EFI_STATUS
+SMProRNGRead (
+  IN UINT8 *Data,
+  IN UINTN DataLen
+  )
+{
+  UINT32      Msg[3];
+  EFI_STATUS  Status;
+
+  /* SMPro only supports 64bits at a time */
+  if ((DataLen != sizeof (UINT64)) || (Data == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Msg[0] = SMPRO_RNG_ENCODE_MSG (SCP_RNG_GET_TRNG, 0);
+  Msg[1] = 0;
+  Msg[2] = 0;
+
+  Status = SMProDBWr (
+             SMPRO_NS_RNG_MAILBOX_INDEX,
+             Msg[0],
+             Msg[1],
+             Msg[2],
+             SMPRO_DB_BASE_REG
+             );
+  if (EFI_ERROR (Status)) {
+    return EFI_DEVICE_ERROR;
+  }
+
+  Status = SMProDBRd (
+             SMPRO_NS_RNG_MAILBOX_INDEX,
+             &Msg[0],
+             &Msg[1],
+             &Msg[2],
+             SMPRO_DB_BASE_REG
+             );
+  if (EFI_ERROR (Status)) {
+    return EFI_DEVICE_ERROR;
+  }
+
+  CopyMem (Data, &Msg[1], sizeof (Msg[1]));
+  CopyMem (Data + sizeof (Msg[1]), &Msg[2], sizeof (Msg[2]));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Returns information about the random number generation implementation.
+
+  @param[in]     This                 A pointer to the EFI_RNG_PROTOCOL instance.
+  @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
+                                      On output with a return code of EFI_SUCCESS, the size
+                                      in bytes of the data returned in RNGAlgorithmList. On output
+                                      with a return code of EFI_BUFFER_TOO_SMALL,
+                                      the size of RNGAlgorithmList required to obtain the list.
+  @param[out] RNGAlgorithmList        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.
+
+**/
+EFI_STATUS
+EFIAPI
+RngGetInfo (
+  IN EFI_RNG_PROTOCOL             *This,
+  IN OUT UINTN                    *RNGAlgorithmListSize,
+  OUT EFI_RNG_ALGORITHM           *RNGAlgorithmList
+  )
+{
+  EFI_STATUS    Status;
+  UINTN         RequiredSize;
+
+  if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  RequiredSize = sizeof (mSupportedRngAlgorithms);
+  if (*RNGAlgorithmListSize < RequiredSize) {
+    Status = EFI_BUFFER_TOO_SMALL;
+  } else {
+    if (RNGAlgorithmList != NULL) {
+      CopyMem (RNGAlgorithmList, mSupportedRngAlgorithms, RequiredSize);
+      Status = EFI_SUCCESS;
+    } else {
+      Status = EFI_INVALID_PARAMETER;
+    }
+  }
+  *RNGAlgorithmListSize = RequiredSize;
+
+  return Status;
+}
+
+/**
+  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]  RNGAlgorithm            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]  RNGValueLength          The length in bytes of the memory buffer pointed to by
+                                      RNGValue. The driver shall return exactly this numbers of bytes.
+  @param[out] RNGValue                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.
+
+**/
+EFI_STATUS
+EFIAPI
+RngGetRNG (
+  IN EFI_RNG_PROTOCOL            *This,
+  IN EFI_RNG_ALGORITHM           *RNGAlgorithm, OPTIONAL
+  IN UINTN                       RNGValueLength,
+  OUT UINT8                      *RNGValue
+  )
+{
+  UINTN         Count, Length;
+  UINT64        TmpVal64;
+  EFI_STATUS    Status = EFI_UNSUPPORTED;
+
+  if ((RNGValueLength == 0) || (RNGValue == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (RNGAlgorithm == NULL) {
+    /*
+     * Use the default RNG algorithm if RNGAlgorithm is NULL.
+     */
+    RNGAlgorithm = &gEfiRngAlgorithmRaw;
+  }
+
+  if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
+    /* FIXME: Linux kernel only passes 64bits which violate UEFI Spec */
+    /*
+     * When a DRBG is used on the output of a entropy source,
+     * its security level must be at least 256 bits according to UEFI Spec.
+     */
+    /*if (RNGValueLength < 32) {
+      return EFI_INVALID_PARAMETER;
+    }*/
+
+    for (Count = 0; Count < (RNGValueLength / sizeof (TmpVal64)) + 1; Count++) {
+      Length = sizeof (TmpVal64);
+      if (Count == (RNGValueLength / sizeof (TmpVal64))) {
+        Length = RNGValueLength % sizeof (TmpVal64);
+      }
+
+      if (Length != 0) {
+        Status = SMProRNGRead ((UINT8 *)&TmpVal64, sizeof (TmpVal64));
+        if (EFI_ERROR (Status)) {
+          return EFI_NOT_READY;
+        }
+        CopyMem (RNGValue + Count * sizeof (TmpVal64), &TmpVal64, Length);
+      }
+    }
+    Status = EFI_SUCCESS;
+  }
+
+  return Status;
+}
+
+/*
+ * The Random Number Generator (RNG) protocol
+ */
+EFI_RNG_PROTOCOL mRng = {
+  RngGetInfo,
+  RngGetRNG
+};
+
+/**
+  The user Entry Point for the Random Number Generator (RNG) driver.
+
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in] SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+  @retval EFI_NOT_SUPPORTED Platform does not support RNG.
+  @retval Other             Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+RngDriverEntry (
+  IN EFI_HANDLE          ImageHandle,
+  IN EFI_SYSTEM_TABLE    *SystemTable
+  )
+{
+  EFI_STATUS             Status;
+  EFI_HANDLE             Handle;
+
+  //
+  // Install UEFI RNG (Random Number Generator) Protocol
+  //
+  Handle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (
+                  &Handle,
+                  &gEfiRngProtocolGuid,
+                  &mRng,
+                  NULL
+                  );
+
+  return Status;
+}
diff --git a/Silicon/Ampere/Drivers/RngDxe/RngDxe.uni b/Silicon/Ampere/Drivers/RngDxe/RngDxe.uni
new file mode 100644
index 000000000000..3f3b48f2036a
--- /dev/null
+++ b/Silicon/Ampere/Drivers/RngDxe/RngDxe.uni
@@ -0,0 +1,10 @@
+//
+// Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "Produces UEFI Random Number Generator protocol"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "This module will produce UEFI Random Number Generator protocol."
diff --git a/Silicon/Ampere/Drivers/RngDxe/RngDxeExtra.uni b/Silicon/Ampere/Drivers/RngDxe/RngDxeExtra.uni
new file mode 100644
index 000000000000..f629e4b65e75
--- /dev/null
+++ b/Silicon/Ampere/Drivers/RngDxe/RngDxeExtra.uni
@@ -0,0 +1,9 @@
+//
+// Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+
+#string STR_PROPERTIES_MODULE_NAME
+#language en-US
+"Ampere UEFI Random Number Generator DXE"
-- 
2.17.1


  parent reply	other threads:[~2020-12-09  9:24 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-09  9:24 [edk2-platforms][PATCH 00/34] Add new Ampere Mt. Jade platform Nhi Pham
2020-12-09  9:24 ` [edk2-platforms][PATCH 01/34] Initial support for Ampere Altra and " Nhi Pham
2021-01-07 23:57   ` [edk2-devel] " Leif Lindholm
2021-01-15  4:59     ` Vu Nguyen
2020-12-09  9:24 ` [edk2-platforms][PATCH 02/34] Platform/Ampere: Implement FailSafe library Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 03/34] Platform/Ampere: Add FailSafe and WDT support Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 04/34] AmpereAltraPkg: Implement GpioLib and I2cLib modules Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 05/34] JadePkg: Implement RealTimeClockLib for PCF85063 Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 06/34] Platform/Ampere: Add AcpiPccLib to support ACPI PCCT Table Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 07/34] Platform/Ampere: Add AcpiHelperLib to update ACPI DSDT table Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 08/34] JadePkg: Initial support for static ACPI tables Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 09/34] JadePkg: Install some ACPI tables at runtime Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 10/34] Silicon/Ampere: Support Non Volatile storage for Variable service Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 11/34] Silicon/Ampere: Support PlatformManagerUiLib Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 12/34] AmpereAltraPkg: Add PcieCore Library Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 13/34] AmpereAltraPkg: Add PciHostBridge driver Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 14/34] JadePkg: Add implementation for PcieBoardLib Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 15/34] JadePkg: Enable PCIe support Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 16/34] JadePkg: Add ASpeed GOP driver Nhi Pham
2020-12-09  9:25 ` Nhi Pham [this message]
2020-12-09  9:25 ` [edk2-platforms][PATCH 18/34] Silicon/Ampere: Fixup runtime memory attribute Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 19/34] JadePkg: Add SMBIOS tables support Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 20/34] AmpereAltraPkg: Add DebugInfoPei module Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 21/34] Silicon/Ampere: Add platform info screen Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 22/34] Silicon/Ampere: Add Memory " Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 23/34] AmpereAltraPkg: Add CPU Configuration for SubNUMA Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 24/34] AmpereAltraPkg: Add ACPI configuration screen Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 25/34] AmpereAltraPkg: Implement PlatformFlashAccessLib instance Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 26/34] JadePkg: Add implementation for UEFI Capsule Update Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 27/34] JadePkg: Add Capsule Update support Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 28/34] Silicon/Ampere: Implement PlatformBootManagerLib for LinuxBoot Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 29/34] Platform/Ampere: Add LinuxBoot image Nhi Pham
2020-12-10 12:40   ` [edk2-devel] " Leif Lindholm
2020-12-11  2:38     ` Nhi Pham
2020-12-11 11:15       ` Leif Lindholm
2020-12-09  9:25 ` [edk2-platforms][PATCH 30/34] JadePkg: Support LinuxBoot DSC/FDF build for Jade platform Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 31/34] AmpereAltraPkg: Add BootProgress support Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 32/34] JadePkg: Add ACPI/APEI tables Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 33/34] Platform/Ampere: Add AcpiApeiLib Nhi Pham
2020-12-09  9:25 ` [edk2-platforms][PATCH 34/34] AmpereAltraPkg, JadePkg: Add RAS setting screen Nhi Pham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201209092531.30867-18-nhi@os.amperecomputing.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox