From patchwork Tue Feb 20 17:49:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 128939 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp4897966ljc; Tue, 20 Feb 2018 09:50:00 -0800 (PST) X-Google-Smtp-Source: AH8x227852of0UdchA8AwJCS/HVuyPLKajkutBxPDgPBSdb/ZJPYB4tF8MXuLV6Sj7rQEReyyYQx X-Received: by 10.99.123.74 with SMTP id k10mr327436pgn.217.1519149000572; Tue, 20 Feb 2018 09:50:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519149000; cv=none; d=google.com; s=arc-20160816; b=jYkxC95rZ+aAzBwW/Z5mYvh2v+4qGC68E3eYAXLer3DfnYpKfva2MVv+S76VD4XJrj 7t1hvFughIUeHSVWF5aQEFx4fMm2CEw9JgObUa1uOImIxIDlaDUoTOH7YGRWm3bTQR8M Pm8ojLU7MdMbiRshrTtH4tgl5y/adSNnD+V9fIGrPDNQjuP6hk9ZJu/PX/1YvqhN2byv 1oJYbpPhFQmGPRixy6QRLwaI1zKuKdUJhfdx3BTvXbFsAVr1VNRCmgmZKMHaOdksAQs1 BasrPjYnxZIOLLBRWsuPElqPPzHALcdmZz9EwyQ7Wqn7/lO0mjzlOuaZWzP8Y84YV433 F2lA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=CQ26Z9f37S8q2lha7y+6CQ+28TGHIy9J1FhmJxJDP54=; b=U4DzjbFJFEVK5r8lD41GtPKOrpQBhihCUKsHivPOLN9jH8i7raGAGhP54eP2j7MmjY cuxxn8kvGa9H9JJx4XGZieFiwMvf37FEzszTsnfaRdFhRhbaYV7HUQR2K24n4tR1ILS2 w9cRZtW5EmvrwgFwNIUJNPQ6goGDDVBcT7Wcejxi4k5fYs/tV6Ke0m4reUMqSzDM13nY 0FZejvd+AI8QckRHdnraTnBMPyEo/jGF6REQFbpsEBziG21lZ0L1cgXo3fUrzTKbhkt+ qWmzGsNVouJat2i200dezfZ2Y8jxp71u1yWZKjmGwx7kUVxyI8y52zaGumtHK6s3YwQt hq7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=fgRMlN7c; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id x62si173299pgd.611.2018.02.20.09.50.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 09:50:00 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=fgRMlN7c; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 691E421CF1D02; Tue, 20 Feb 2018 09:44:01 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c0c::235; helo=mail-wr0-x235.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr0-x235.google.com (mail-wr0-x235.google.com [IPv6:2a00:1450:400c:c0c::235]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 1F74720954CBD for ; Tue, 20 Feb 2018 09:44:00 -0800 (PST) Received: by mail-wr0-x235.google.com with SMTP id z12so10668747wrg.4 for ; Tue, 20 Feb 2018 09:49:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YJm5gg2aHMaqQhWHlhGdY72O/GkwU9RINUTIaF1vlAA=; b=fgRMlN7crsbbt3G+zsiEu9gkTOMvBVC1AHqh5dnAHrwV2jwXB0Rmj6Dg2wXjGGmjDo 0nM4Cjpd1/9n/yH47xcyQUYEY45Av2gJGEIYg/U0qew6+ipbzW91ktIOr4LsatS0kQvw mNf5at0+mf8Yn+zkUEF9Wgqy8hsm/k6NDAkEs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YJm5gg2aHMaqQhWHlhGdY72O/GkwU9RINUTIaF1vlAA=; b=YGGdsbGcjxENZn6jKpmvgTbs7tkh40d1yOoE4cb7NvTUsqI41zZi/SdqzOk0LWoM+a kdUVAzznZqv8Am403hdDEbJyHc5EToAT8rr8qnhVF7vc7CjLoUQWHt1dORZVteJK4SCb ULi192B9ClcBbxbt4ao5QM6E77/lgGBccKo2NOdFPhE55fqVRh5JNSxuGVtDPDXWoY8c faremNDOBgzvcKpWtdqIOoo45pnb2oY6s+Iod6UWNEdxF/TRihV5kjy1M+pdA9pEp3jJ DxxgsJz5JkSkybSxHXrS+UfdIXGKwDkmEcf2iNJlc14mtXfRU5N7aZattAdDxAoihwi7 uBLg== X-Gm-Message-State: APf1xPAAl5Z95wfJiQ9GCMGSYDg4tFgDRF0aZtOA+GRl4l7lzM0aUGSg tTKsVSSGdJu6/ryMP52W4/QQ4nGXVHk= X-Received: by 10.223.195.97 with SMTP id e30mr400755wrg.181.1519148996514; Tue, 20 Feb 2018 09:49:56 -0800 (PST) Received: from localhost.localdomain ([105.149.187.179]) by smtp.gmail.com with ESMTPSA id u63sm18282208wrc.26.2018.02.20.09.49.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 09:49:55 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Tue, 20 Feb 2018 17:49:38 +0000 Message-Id: <20180220174944.525-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180220174944.525-1-ard.biesheuvel@linaro.org> References: <20180220174944.525-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH edk2-platforms v2 1/7] Silicon/Atmel: add support for AtSha204a RNG X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: leif.lindholm@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" This adds support for using the random number generator in the Atmel AtSha204a over I2C. Other functionality of the chip is currently unsupported. Note that the the I2C support in this device essentially violates the protocol layering, by requiring that the device is woken up by driving SDA low for a certain amount of time, which is something that cannot be expressed in terms of an I2C packet sent to the device's slave address. Instead, the slave address 0x0 is added to the device's address array, and the wake is sent by sending a dummy write to address 0x0, and ignoring the subsequent error. This requires the I2C bus to be clocked at 100 kHz. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- Silicon/Atmel/AtSha204a/AtSha204a.dec | 22 ++ Silicon/Atmel/AtSha204a/AtSha204aDriver.c | 309 ++++++++++++++++++++ Silicon/Atmel/AtSha204a/AtSha204aDriver.h | 81 +++++ Silicon/Atmel/AtSha204a/AtSha204aDxe.inf | 52 ++++ Silicon/Atmel/AtSha204a/ComponentName.c | 186 ++++++++++++ Silicon/Atmel/AtSha204a/DriverBinding.c | 242 +++++++++++++++ 6 files changed, 892 insertions(+) -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Reviewed-by: Leif Lindholm diff --git a/Silicon/Atmel/AtSha204a/AtSha204a.dec b/Silicon/Atmel/AtSha204a/AtSha204a.dec new file mode 100644 index 000000000000..f1fdea59843d --- /dev/null +++ b/Silicon/Atmel/AtSha204a/AtSha204a.dec @@ -0,0 +1,22 @@ +## @file +# +# Copyright (c) 2018, 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. +# +## + +[Defines] + DEC_SPECIFICATION = 0x0001001A + PACKAGE_NAME = AtSha204a + PACKAGE_GUID = 86085a5b-355b-4e72-92ab-fc3e1d71c9ad + PACKAGE_VERSION = 0.1 + +[Guids] + gAtSha204aI2cDeviceGuid = { 0x52e9b64b, 0x4ec1, 0x4bd6, { 0x9e, 0x1c, 0x6d, 0xac, 0xef, 0x35, 0x18, 0x21 } } diff --git a/Silicon/Atmel/AtSha204a/AtSha204aDriver.c b/Silicon/Atmel/AtSha204a/AtSha204aDriver.c new file mode 100644 index 000000000000..5db2de21a731 --- /dev/null +++ b/Silicon/Atmel/AtSha204a/AtSha204aDriver.c @@ -0,0 +1,309 @@ +/** @file + Device driver for the Atmel ATSHA204A random number generator. + + Copyright (c) 2018, 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 "AtSha204aDriver.h" + +#include +#include +#include + +#define MAX_RETRIES 5 + +// Don't bother calculating the CRC for the immutable RANDOM opcode packet +#define OPCODE_COMMAND_PACKET_CRC 0xcd24 + +/** + 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 +AtSha240aGetInfo ( + IN EFI_RNG_PROTOCOL *This, + IN OUT UINTN *AlgorithmListSize, + OUT EFI_RNG_ALGORITHM *AlgorithmList +) +{ + UINTN Size; + + // + // We only implement the raw algorithm + // + Size = sizeof (EFI_GUID); + + if (*AlgorithmListSize < Size) { + *AlgorithmListSize = Size; + return EFI_BUFFER_TOO_SMALL; + } + + CopyGuid (AlgorithmList, &gEfiRngAlgorithmRaw); + *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 +AtSha240aGetRNG ( + IN EFI_RNG_PROTOCOL *This, + IN EFI_RNG_ALGORITHM *Algorithm OPTIONAL, + IN UINTN ValueLength, + OUT UINT8 *Value +) +{ + EFI_STATUS Status; + ATSHA204A_DEV *AtSha204a; + ATSHA204A_I2C_RNG_COMMAND Command; + ATSHA204A_I2C_RNG_RESULT Result; + I2C_RNG_REQUEST Request; + I2C_RNG_REQUEST Response; + UINTN Retries; + + if (Algorithm != NULL && !CompareGuid (Algorithm, &gEfiRngAlgorithmRaw)) { + return EFI_UNSUPPORTED; + } + + AtSha204a = ATSHA204A_DEV_FROM_THIS (This); + + Request.OperationCount = 1; + Request.Operation.Flags = 0; + + Command.Command = ATSHA204A_COMMAND; + Command.Count = sizeof (Command) - 1; + Command.Opcode = ATSHA204A_OPCODE_RANDOM; + Command.Param1 = 0; + Command.Param2 = 0; + Command.Crc = OPCODE_COMMAND_PACKET_CRC; + + Response.OperationCount = 1; + Response.Operation.Flags = I2C_FLAG_READ; + Response.Operation.LengthInBytes = sizeof (Result); + Response.Operation.Buffer = (VOID *)&Result; + + Retries = 0; + while (ValueLength > 0) { + // + // The AtSha204a will go back to sleep right in the middle of a transaction + // if it does not complete in ~1.3 seconds. So send the wake sequence for + // each iteration, which consists of a dummy write to slave address 0x0. + // + Request.Operation.LengthInBytes = 0; + Status = AtSha204a->I2cIo->QueueRequest (AtSha204a->I2cIo, 1, NULL, + (VOID *)&Request, NULL); + DEBUG ((DEBUG_INFO, "%a: wake AtSha204a: I2cIo->QueueRequest() - %r\n", + __FUNCTION__, Status)); + + gBS->Stall (2500); // wait 2.5 ms for wake to complete + + Request.Operation.LengthInBytes = sizeof (Command); + Request.Operation.Buffer = (VOID *)&Command; + Status = AtSha204a->I2cIo->QueueRequest (AtSha204a->I2cIo, 0, NULL, + (VOID *)&Request, NULL); + if (EFI_ERROR (Status)) { + if (++Retries <= MAX_RETRIES) { + continue; + } + DEBUG ((DEBUG_ERROR, "%a: I2C request transfer failed, Status == %r\n", + __FUNCTION__, Status)); + return EFI_DEVICE_ERROR; + } + + gBS->Stall (50 * 1000); // 50 ms max execution time for RANDOM opcode + + Status = AtSha204a->I2cIo->QueueRequest (AtSha204a->I2cIo, 0, NULL, + (VOID *)&Response, NULL); + if (EFI_ERROR (Status)) { + if (++Retries <= MAX_RETRIES) { + continue; + } + DEBUG ((DEBUG_ERROR, "%a: I2C response transfer failed, Status == %r\n", + __FUNCTION__, Status)); + return EFI_DEVICE_ERROR; + } + + if (Result.Count < sizeof (Result)) { + // + // Incomplete packet received, most likely due to an error. Retry. + // + if (++Retries <= MAX_RETRIES) { + continue; + } + DEBUG ((DEBUG_WARN, "%a: incomplete packet received\n", __FUNCTION__)); + return EFI_DEVICE_ERROR; + } + + gBS->CopyMem (Value, Result.Result, MIN (ValueLength, + ATSHA204A_OUTPUT_SIZE)); + if (ValueLength < ATSHA204A_OUTPUT_SIZE) { + break; + } + + Value += ATSHA204A_OUTPUT_SIZE; + ValueLength -= ATSHA204A_OUTPUT_SIZE; + Retries = 0; + } + return EFI_SUCCESS; +} + +EFI_STATUS +AtSha204aInit ( + IN EFI_HANDLE DriverBindingHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_STATUS Status; + ATSHA204A_DEV *AtSha204a; + + Status = gBS->AllocatePool (EfiBootServicesData, sizeof (ATSHA204A_DEV), + (VOID **) &AtSha204a); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + + AtSha204a->Signature = ATSHA204A_DEV_SIGNATURE; + AtSha204a->Rng.GetInfo = AtSha240aGetInfo; + AtSha204a->Rng.GetRNG = AtSha240aGetRNG; + + // + // Open I2C I/O Protocol + // + Status = gBS->OpenProtocol (ControllerHandle, + &gEfiI2cIoProtocolGuid, + (VOID **)&AtSha204a->I2cIo, + DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER); + if (EFI_ERROR (Status)) { + goto ErrorFreeDev; + } + + Status = gBS->InstallProtocolInterface (&ControllerHandle, + &gEfiRngProtocolGuid, + EFI_NATIVE_INTERFACE, + &AtSha204a->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, &gEfiI2cIoProtocolGuid, + DriverBindingHandle, ControllerHandle); + +ErrorFreeDev: + gBS->FreePool (AtSha204a); + + return Status; +} + +EFI_STATUS +AtSha204aRelease ( + IN EFI_HANDLE DriverBindingHandle, + IN EFI_HANDLE ControllerHandle + ) +{ + EFI_RNG_PROTOCOL *Rng; + ATSHA204A_DEV *AtSha204a; + EFI_STATUS Status; + + Status = gBS->HandleProtocol (ControllerHandle, + &gEfiRngProtocolGuid, + (VOID **)&Rng); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + return Status; + } + + AtSha204a = ATSHA204A_DEV_FROM_THIS (Rng); + + Status = gBS->UninstallProtocolInterface (ControllerHandle, + &gEfiRngProtocolGuid, + Rng); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = gBS->CloseProtocol (ControllerHandle, + &gEfiI2cIoProtocolGuid, + DriverBindingHandle, + ControllerHandle); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + return Status; + } + + gBS->FreePool (AtSha204a); + + return EFI_SUCCESS; +} diff --git a/Silicon/Atmel/AtSha204a/AtSha204aDriver.h b/Silicon/Atmel/AtSha204a/AtSha204aDriver.h new file mode 100644 index 000000000000..315a450d34f2 --- /dev/null +++ b/Silicon/Atmel/AtSha204a/AtSha204aDriver.h @@ -0,0 +1,81 @@ +/** @file + Device driver for the Atmel ATSHA204A random number generator. + + Copyright (c) 2018, 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. + +**/ + +#ifndef _ATSHA204A_I2C_HWRNG_DRIVER_H_ +#define _ATSHA204A_I2C_HWRNG_DRIVER_H_ + +#include +#include +#include +#include +#include + +#include +#include + +#define ATSHA204A_OUTPUT_SIZE 32 + +#define ATSHA204A_DEV_SIGNATURE SIGNATURE_32('a','t','s','h') + +typedef struct { + UINT32 Signature; + EFI_I2C_IO_PROTOCOL *I2cIo; + EFI_RNG_PROTOCOL Rng; +} ATSHA204A_DEV; + +#define ATSHA204A_DEV_FROM_THIS(a) \ + CR(a, ATSHA204A_DEV, Rng, ATSHA204A_DEV_SIGNATURE) + +#pragma pack(1) +typedef struct { + UINT8 Command; + UINT8 Count; + UINT8 Opcode; + UINT8 Param1; + UINT16 Param2; + UINT16 Crc; +} ATSHA204A_I2C_RNG_COMMAND; + +typedef struct { + UINT8 Count; + UINT8 Result[ATSHA204A_OUTPUT_SIZE]; + UINT16 Crc; +} ATSHA204A_I2C_RNG_RESULT; +#pragma pack() + +typedef struct { + UINTN OperationCount; + EFI_I2C_OPERATION Operation; +} I2C_RNG_REQUEST; + +#define ATSHA204A_COMMAND 0x3 + +#define ATSHA204A_OPCODE_RANDOM 0x1b + +extern EFI_COMPONENT_NAME2_PROTOCOL gAtSha204aDriverComponentName2; + +EFI_STATUS +AtSha204aInit ( + IN EFI_HANDLE DriverBindingHandle, + IN EFI_HANDLE ControllerHandle + ); + +EFI_STATUS +AtSha204aRelease ( + IN EFI_HANDLE DriverBindingHandle, + IN EFI_HANDLE ControllerHandle + ); + +#endif // _ATSHA204A_I2C_HWRNG_DRIVER_H_ diff --git a/Silicon/Atmel/AtSha204a/AtSha204aDxe.inf b/Silicon/Atmel/AtSha204a/AtSha204aDxe.inf new file mode 100644 index 000000000000..fe90cc538114 --- /dev/null +++ b/Silicon/Atmel/AtSha204a/AtSha204aDxe.inf @@ -0,0 +1,52 @@ +## @file +# Device driver for the Atmel ATSHA204A random number generator. +# +# Copyright (c) 2018, 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. +# +## + +[Defines] + INF_VERSION = 0x0001001A + BASE_NAME = AtSha204aDxe + FILE_GUID = 8b8f683b-f376-4ba0-b8d7-b4bbd30319cc + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = EntryPoint + UNLOAD_IMAGE = UnloadImage + +# +# VALID_ARCHITECTURES = AARCH64 ARM EBC IA32 IPF X64 +# + +[Sources] + AtSha204aDriver.c + AtSha204aDriver.h + ComponentName.c + DriverBinding.c + +[Packages] + MdePkg/MdePkg.dec + Silicon/Atmel/AtSha204a/AtSha204a.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiLib + +[Protocols] + gEfiRngProtocolGuid # PROTOCOL BY_START + gEfiI2cIoProtocolGuid # PROTOCOL TO_START + +[Guids] + gAtSha204aI2cDeviceGuid + gEfiRngAlgorithmRaw diff --git a/Silicon/Atmel/AtSha204a/ComponentName.c b/Silicon/Atmel/AtSha204a/ComponentName.c new file mode 100644 index 000000000000..9893e7c4c2bf --- /dev/null +++ b/Silicon/Atmel/AtSha204a/ComponentName.c @@ -0,0 +1,186 @@ +/** @file + UEFI Component Name(2) protocol implementation for AtSha204a driver. + + Copyright (c) 2018, 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 "AtSha204aDriver.h" + +STATIC EFI_UNICODE_STRING_TABLE mAtSha204aDriverNameTable[] = { + { + "en", + (CHAR16 *)L"AtSha204a RNG I2C driver" + }, + { } +}; + +STATIC EFI_UNICODE_STRING_TABLE mAtSha204aControllerNameTable[] = { + { + "en", + (CHAR16 *)L"AtSha204a Random Number Generator (I2C)" + }, + { } +}; + +/** + 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 +AtSha204aGetDriverName ( + IN EFI_COMPONENT_NAME2_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 (Language, + This->SupportedLanguages, + mAtSha204aDriverNameTable, + DriverName, + FALSE); +} + +/** + 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 +AtSha204aGetControllerName ( + IN EFI_COMPONENT_NAME2_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, + mAtSha204aControllerNameTable, + ControllerName, + FALSE); +} + +// +// EFI Component Name 2 Protocol +// +EFI_COMPONENT_NAME2_PROTOCOL gAtSha204aDriverComponentName2 = { + AtSha204aGetDriverName, + AtSha204aGetControllerName, + "en" +}; diff --git a/Silicon/Atmel/AtSha204a/DriverBinding.c b/Silicon/Atmel/AtSha204a/DriverBinding.c new file mode 100644 index 000000000000..17e61f96e8c5 --- /dev/null +++ b/Silicon/Atmel/AtSha204a/DriverBinding.c @@ -0,0 +1,242 @@ +/** @file + Device driver for the Atmel ATSHA204A random number generator. + + Copyright (c) 2018, 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 + +#include "AtSha204aDriver.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 +I2cHwrngDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_I2C_IO_PROTOCOL *I2cIo; + EFI_STATUS Status; + + // + // Connect to the I2C stack + // + Status = gBS->OpenProtocol (ControllerHandle, + &gEfiI2cIoProtocolGuid, + (VOID **) &I2cIo, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER); + if (EFI_ERROR (Status)) { + return Status; + } + + if (CompareGuid (I2cIo->DeviceGuid, &gAtSha204aI2cDeviceGuid)) { + DEBUG ((DEBUG_INIT | DEBUG_INFO, "Detected AtSha204a RNG device\n")); + Status = EFI_SUCCESS; + } else { + Status = EFI_UNSUPPORTED; + } + + // + // Clean up. + // + gBS->CloseProtocol (ControllerHandle, + &gEfiI2cIoProtocolGuid, + 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 +I2cHwrngDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + return AtSha204aInit (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 +I2cHwrngDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer OPTIONAL + ) +{ + return AtSha204aRelease (This->DriverBindingHandle, ControllerHandle); +} + + +STATIC +EFI_DRIVER_BINDING_PROTOCOL gI2cHwrngDriverBinding = { + I2cHwrngDriverBindingSupported, + I2cHwrngDriverBindingStart, + I2cHwrngDriverBindingStop, + 0xa, + NULL, + NULL +}; + + +/** + The entry point of AtSha204a 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, &gI2cHwrngDriverBinding, ImageHandle, + NULL, &gAtSha204aDriverComponentName2); + ASSERT_EFI_ERROR (Status); + + DEBUG ((DEBUG_INIT | DEBUG_INFO, "*** Installed AtSha204a driver! ***\n")); + + return EFI_SUCCESS; +} + + +/** + Unload function for the AtSha204a 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 I2C I/O handles in the handle database + // + Status = gBS->LocateHandleBuffer (ByProtocol, + &gEfiI2cIoProtocolGuid, + 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, + &gI2cHwrngDriverBinding, + NULL + ); + + return EFI_SUCCESS; +}