From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id CCCE47803CF for ; Fri, 26 Jan 2024 06:02:28 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=IGoaNGJViJjcLGIgQgdNsBxt6BM5ZhqVnUKbd9XKpVA=; c=relaxed/simple; d=groups.io; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:Received-SPF:From:To:CC:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1706248947; v=1; b=s4xX+pWJCdJlgGh+JIC59o6sTZR2kanLIkr9OZ7RdcBErIbkmzRccWJxHyhq47rHMT/7+ABN PbwFVNZa1kQ7FBP0k4oA4SbOEIz3Dln0Hxvfc5jpmZbxWVtOZFkmuaQTOTcBDRxoXBvWjL+3j4Y tmhuDxzelCmVbpBV/AMwRS0g= X-Received: by 127.0.0.2 with SMTP id e4PDYY7687511xPPOqoL7Bqk; Thu, 25 Jan 2024 22:02:27 -0800 X-Received: from NAM11-BN8-obe.outbound.protection.outlook.com (NAM11-BN8-obe.outbound.protection.outlook.com [40.107.236.40]) by mx.groups.io with SMTP id smtpd.web10.9826.1706248946736584384 for ; Thu, 25 Jan 2024 22:02:27 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fTwKosc+A2owofUKR37VtgBWANp3n3GE/ObcppXDI+7LOcx3nQPGeDXbJk9YkgwrvVZ5neDuAhc8XoaI/MXMDs3E5ST6SJRUQGSsJqRF4aEPB6JI2ksJ/f0R4ARf1V0ONiyK8Ti9aYV4JgDWEqxDvarvQLeaUwHBWVR2VT7ssmuGTnKpMzrQtVeQlqgjYRi/janCkjNAyCX/jKCVmYVZSofU21fTG+Cpg22t/cb1P3GeaauZgRNhM8FG5BLOFy7ydZFdd+0kgRoArnvLCxpC5cETsqsNsylpnMoK5UOY+e3+O+4003kTehjN6knQLrpkbqztirsD9at7gIDauAD9Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=t0U3/YCUk8OSgjhPiwfnPhy/MHVkSkn3SGAnqVuyrAM=; b=nJwUIz0+9HGlQyUlTnxcjkXZumlxnhC2gEXUqVguDVxsdiFaOdtU2nalDytW3O6nCR+uV0l1eRDpI8Uwrtyu+SnFI6RvLIVZK4VqeYyfqF9PLxPIu+auugZpobBD9OQ0e9YM30mQq/Gen7ZbOU7WzkUrtTGeuC2XFj2Tqkau9A8FFLddPhaBnVfPPtfqPhTHI0xW3Ie3FLFHMrivAxWnOa/OTPQKNH5+AojPaYBtMI2d6k8LGNfb0fwn4pJGaT/j2nGJMm+E0a3mSWoiy3mkhY2p0z+topoDCPyuOvdFOsITnhhJ7ECbH7LEMYBk6Yn7BttpbD1c0EsyP39mkfkQIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) X-Received: from CH2PR12CA0003.namprd12.prod.outlook.com (2603:10b6:610:57::13) by PH8PR12MB7375.namprd12.prod.outlook.com (2603:10b6:510:215::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26; Fri, 26 Jan 2024 06:02:21 +0000 X-Received: from DS2PEPF0000343D.namprd02.prod.outlook.com (2603:10b6:610:57:cafe::55) by CH2PR12CA0003.outlook.office365.com (2603:10b6:610:57::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.26 via Frontend Transport; Fri, 26 Jan 2024 06:02:20 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C X-Received: from SATLEXMB04.amd.com (165.204.84.17) by DS2PEPF0000343D.mail.protection.outlook.com (10.167.18.40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 06:02:20 +0000 X-Received: from SHA-LX-MINGXZHA.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 26 Jan 2024 00:02:18 -0600 From: "Zhai, MingXin (Duke) via groups.io" To: CC: Eric Xing , Ken Yao , Igniculus Fu , Abner Chang Subject: [edk2-devel] [PATCH V2 10/32] AMD/VanGoghBoard: Check in FlashUpdate Date: Fri, 26 Jan 2024 14:00:28 +0800 Message-ID: <20240126060050.1725-11-duke.zhai@amd.com> In-Reply-To: <20240126060050.1725-1-duke.zhai@amd.com> References: <20240126060050.1725-1-duke.zhai@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343D:EE_|PH8PR12MB7375:EE_ X-MS-Office365-Filtering-Correlation-Id: 5d900db3-eaf9-4215-116f-08dc1e345bb7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: 802XP3QtyEpkNQayNjnOz3fLnb5Ci9hNKbcnt65r5giVVk8sJFy3TFUu2lsGPZg96AYn+1NBWuRmXjtt6XpAzAEVzGDzOqTdWW7afwMxkMg35uy/ZTO7ajKSLJyQSk6H9nSoHpMuvz2TDqgjCizJ3Nf5syYPzq6zPraHa9qXr6JU2dP7lyi3i4/g/6ifLTVT0J2vp06oH7Deym1tsc2dGjNUTGIJBWntprxsBX7qmyNDT6RuGIUDpas0sTkcHjjEvX5XtrChCc6qKN5FngVYcR1AWcKbKuuN9C2tT1Mn+UlYSnMpR4bcC2mtlMN5ZmUHTfA0SyvW9Tu6XBYx5qvUxXeIcVHAJAo1mqj0QdkD5Xf/9DCPIF2Hx38sXxMlLERpoaZsS1QrORp10trzf4p+md+52Be8+kwQ7oXujuZd7fcx/fKQPFKGsyLPyk07fBJFP/KhLWlvqzODf7f8XU9AyStpdC7KdkiE4PkGgqWpbhMBZS9N7pDQhna8iT9u0O4r7PzEtHr2xa0xXOSABZx4DOk8a0pXOBI7DVXnCJtia2qGi/Xa1+J7H65lvtt6zHTniIYwhgu2ThOfxJCBuT7Ch2uokwTRThSPkTQbbyF02BGMN2bOIJ5ICEioekYAJiFLi9BVPDycTFQD0cVNiglGRmlWoNnuAoif2aLh6HvjkHxQar7/lYBmp24tfAHpsY3RixOY2ntKhojDi3r8kLyPrsTs73N1SxqeCWmN+3nskaFNQM8C0AHwaqczZt4qKny4rj1tVVC0EUYx3MiCFFmpfA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 06:02:20.4687 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5d900db3-eaf9-4215-116f-08dc1e345bb7 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7375 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,duke.zhai@amd.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 9Q47jaA7awjn28aUYC8e75Kex7686176AA= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=s4xX+pWJ; arc=reject ("signature check failed: fail, {[1] = sig:microsoft.com:reject}"); dmarc=none; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io From: Duke Zhai BZ #:4640 In V2: Improve coding style. 1.Remove the leading underscore and use double underscore at trailing in = C header files. 2.Remove old tianocore licenses and redundant license description. 3.Improve coding style. For example: remove space between @param. In V1: Initial FlashUpdate module for Chachani platform flash IC. It provides mEfiSpiFlashUpdateProtocol for other module to access flash. Signed-off-by: Duke Zhai Cc: Eric Xing Cc: Ken Yao Cc: Igniculus Fu Cc: Abner Chang --- .../FlashUpdate/FlashUpdateCommon.h | 143 +++++ .../FlashUpdate/FlashUpdateSmm.c | 512 ++++++++++++++++++ .../FlashUpdate/FlashUpdateSmm.h | 123 +++++ .../FlashUpdate/FlashUpdateSmm.inf | 59 ++ .../FlashUpdate/FlashUpdateSmmRuntimeDxe.c | 407 ++++++++++++++ .../FlashUpdate/FlashUpdateSmmRuntimeDxe.inf | 48 ++ .../VanGoghCommonPkg/FlashUpdate/PcRtc.h | 375 +++++++++++++ 7 files changed, 1667 insertions(+) create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= FlashUpdateCommon.h create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= FlashUpdateSmm.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= FlashUpdateSmm.h create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= FlashUpdateSmm.inf create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= FlashUpdateSmmRuntimeDxe.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= FlashUpdateSmmRuntimeDxe.inf create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/= PcRtc.h diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUp= dateCommon.h b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/Flash= UpdateCommon.h new file mode 100644 index 0000000000..616035b82d --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpdateCom= mon.h @@ -0,0 +1,143 @@ +/** @file + Implements AMD FlashUpdateCommon.h + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef FLASH_UPDATE_COMMON_H__ +#define FLASH_UPDATE_COMMON_H__ + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include + +#define SPI_SMM_COMM_ID_GET_FLASH_SIZE_BLOCK_SIZE 0x0 // ID for get= flash size and block size +#define SPI_SMM_COMM_ID_READ_FLASH 0x1 // ID for Rea= d Flash +#define SPI_SMM_COMM_ID_WRITE_FALSH 0x2 // ID for Wri= te Flash +#define SPI_SMM_COMM_ID_ERASE_FALSH 0x3 // ID for Era= se Flash + +// +// SMM communication common buffer +// +typedef struct _FLASH_UPDATE_SMM_COMMUNICATION_CMN { + UINT32 id; // Function ID of smm communication bu= ffer +} FLASH_UPDATE_SMM_COMMUNICATION_CMN; + +#pragma pack(1) + +// +// SMM communication common buffer +// +typedef struct _SMM_COMM_RWE_FLASH { + UINT32 id; // ID of smm communication bu= ffer + UINTN FlashAddress; // Flash devicd physical flas= h address + UINTN NumBytes; // Number in byte + EFI_STATUS ReturnStatus; // Return status + UINT8 Buffer[1]; // Buffer start +} SMM_COMM_RWE_FLASH; + +// +// SMM communication common buffer +// +typedef struct _SMM_COMM_GET_FLASH_SIZE_BLOCK_SIZE { + UINT32 id; // ID of smm communication b= uffer + UINTN FlashSize; // Flash size + UINTN BlockSize; // Block size of flash devic= e + EFI_STATUS ReturnStatus; // Return status +} SMM_COMM_GET_FLASH_SIZE_BLOCK_SIZE; + +#pragma pack() + +#define SMM_COMMUNICATE_HEADER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADE= R, Data)) +#define SMM_COMM_RWE_FLASH_SIZE (OFFSET_OF (SMM_COMM_RWE_FLASH, Buffe= r)) + +/** + Read data from flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[out] Buffer Buffer contain the read data. + + @retval EFI_SUCCESS Read successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolFlashFdRead ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + OUT VOID *Buffer + ); + +/** + Erase flash region according to input in a block size. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte, a block size in = flash device. + + @retval EFI_SUCCESS Erase successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolFlashFdErase ( + IN UINTN FlashAddress, + IN UINTN NumBytes + ); + +/** + Write data to flash device. + + Write Buffer(FlashAddress|NumBytes) to flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[in] Buffer Buffer contain the write data. + + @retval EFI_SUCCESS Write successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolFlashFdWrite ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + IN UINT8 *Buffer + ); + +/** + Get flash device size and flash block size. + + @param[out] FlashSize Pointer to the size of flash dev= ice. + @param[out] BlockSize Pointer to the size of block in = flash device. + + @retval EFI_SUCCESS Get successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolGetFlashSizeBlockSize ( + OUT UINTN *FlashSize, + OUT UINTN *BlockSize + ); + +#endif // _FLASH_UPDATE_COMMON_H_ diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUp= dateSmm.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpd= ateSmm.c new file mode 100644 index 0000000000..42991295d7 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpdateSmm= .c @@ -0,0 +1,512 @@ +/** @file + Implements AMD FlashUpdateSmm.c + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "FlashUpdateSmm.h" + +#define PM1_EN_HIGH_BYTE 0x03 +#define RTC_EVENT_ENABLE 0x04 +#define ACPIMMIO16(x) (*(volatile UINT16*)(UINTN)(x)) +#define ACPI_MMIO_BASE 0xFED80000ul +#define ACPI_PM1_EVT_BLK 0x60 +#define ACPI_PM1_CNT_BLK 0x62 +#define PMIO_BASE 0x300 // DWORD +#define SUS_S3 0x0C00U // S3 +#define SUS_S5 0x1400U // S5 +#define SLP_TYPE 0x1C00U // MASK +#define SLP_EN 0x2000U // BIT13 + +EFI_SPI_PROTOCOL *mSmmSpiProtocol =3D NULL; +UINTN mFlashAreaBaseAddress; +UINTN mFlashSize; +UINTN mBlockSize; + +EFI_SMM_SPI_FLASH_UPDATE_PROTOCOL mEfiSmmSpiFlashUpdateProtocol =3D { + FlashUpdateServiceFlashFdRead, + FlashUpdateServiceFlashFdErase, + FlashUpdateServiceFlashFdWrite, + FlashUpdateServiceGetFlashSizeBlockSize, +}; + +/** + Read data from flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[out] Buffer Buffer contain the read data. + + @retval EFI_SUCCESS Read successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceFlashFdRead ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + OUT VOID *Buffer + ) +{ + if ((FlashAddress >=3D mFlashSize) || (NumBytes =3D=3D 0) || (NumBytes >= mFlashSize) || + (Buffer =3D=3D NULL) || (FlashAddress + NumBytes > mFlashSize)) + { + return EFI_INVALID_PARAMETER; + } + + CopyMem (Buffer, (UINT8 *)(FlashAddress + mFlashAreaBaseAddress), NumByt= es); + + return EFI_SUCCESS; +} + +/** + Erase flash region according to input in a block size. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte, a block size in = flash device. + + @retval EFI_SUCCESS Erase successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceFlashFdErase ( + IN UINTN FlashAddress, + IN UINTN NumBytes + ) +{ + EFI_STATUS Status; + + if ((FlashAddress >=3D mFlashSize) || (NumBytes =3D=3D 0) || (NumBytes >= mFlashSize) || + (FlashAddress + NumBytes > mFlashSize)) + { + return EFI_INVALID_PARAMETER; + } + + if (mSmmSpiProtocol =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "mSmmSpiProtocol =3D NULL\n")); + return EFI_NOT_FOUND; + } + + Status =3D mSmmSpiProtocol->Execute ( + mSmmSpiProtocol, + SPI_OPCODE_ERASE_INDEX, // OpcodeIndex + 0, // PrefixOpcodeInde= x + FALSE, // DataCycle + TRUE, // Atomic + TRUE, // ShiftOut + FlashAddress, // Address + 0, // Data Number + NULL, // Buffer + EnumSpiRegionBios // SpiRegionType + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "!!!ERROR: Erase flash %r\n", Status)); + return Status; + } + + AsmWbinvd (); + + return EFI_SUCCESS; +} + +/** + Write data to flash device. + + Write Buffer(FlashAddress|NumBytes) to flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[in] Buffer Buffer contain the write data. + + @retval EFI_SUCCESS Write successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceFlashFdWrite ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + + if ((FlashAddress >=3D mFlashSize) || (NumBytes =3D=3D 0) || (NumBytes >= mFlashSize) || (Buffer =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (FlashAddress + NumBytes > mFlashSize) { + return EFI_INVALID_PARAMETER; + } + + if (mSmmSpiProtocol =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "mSmmSpiProtocol =3D NULL\n")); + return EFI_NOT_FOUND; + } + + Status =3D mSmmSpiProtocol->Execute ( + mSmmSpiProtocol, + SPI_OPCODE_WRITE_INDEX, // OpcodeIndex + 0, // PrefixOpcodeIndex + TRUE, // DataCycle + TRUE, // Atomic + TRUE, // ShiftOut + FlashAddress, // Address + (UINT32)NumBytes, // Data Number + Buffer, // Buffer + EnumSpiRegionBios // Spi Region Type + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "!!!ERROR: Write flash %r\n", Status)); + return Status; + } + + AsmWbinvd (); + + return EFI_SUCCESS; +} + +/** + Get flash device size and flash block size. + + @param[out] FlashSize Pointer to the size of flash dev= ice. + @param[out] BlockSize Pointer to the size of block in = flash device. + + @retval EFI_SUCCESS Get successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceGetFlashSizeBlockSize ( + OUT UINTN *FlashSize, + OUT UINTN *BlockSize + ) +{ + if ((FlashSize =3D=3D 0) || (BlockSize =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + *FlashSize =3D mFlashSize; + *BlockSize =3D mBlockSize; + + return EFI_SUCCESS; +} + +/** + Communication service SMI Handler entry. + + This SMI handler provides services for the update flash routines. + + @param[in] DispatchHandle The unique handle assigned to this handl= er by SmiHandlerRegister(). + @param[in] RegisterContext Points to an optional handler context wh= ich was specified when the + handler was registered. + @param[in, out] CommBuffer A pointer to a collection of data in memo= ry that will + be conveyed from a non-SMM environment in= to an SMM environment. + @param[in, out] CommBufferSize The size of the CommBuffer. + + @retval EFI_SUCCESS The interrupt was handled an= d quiesced. No other handlers + should still be called. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *RegisterContext, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize + ) +{ + EFI_STATUS Status; + FLASH_UPDATE_SMM_COMMUNICATION_CMN *Buffer; + SMM_COMM_RWE_FLASH *RweBuffer; + SMM_COMM_GET_FLASH_SIZE_BLOCK_SIZE *GetFlashSizeBlockSizeBuffer; + + // + // If input is invalid, stop processing this SMI + // + if ((CommBuffer =3D=3D NULL) || (CommBufferSize =3D=3D NULL)) { + DEBUG ((DEBUG_ERROR, "!!!ERROR: FlashUpdateServiceHandler: Invalid par= ameter!\n")); + return EFI_SUCCESS; + } + + if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, *CommBufferSize)) { + DEBUG ((DEBUG_ERROR, "!!!ERROR: FlashUpdateServiceHandler: SMM communi= cation buffer in SMRAM or overflow!\n")); + return EFI_SUCCESS; + } + + GetFlashSizeBlockSizeBuffer =3D NULL; + Buffer =3D (FLASH_UPDATE_SMM_COMMUNICATION_CMN *)Co= mmBuffer; + RweBuffer =3D (SMM_COMM_RWE_FLASH *)CommBuffer; + + switch (Buffer->id) { + case SPI_SMM_COMM_ID_GET_FLASH_SIZE_BLOCK_SIZE: + GetFlashSizeBlockSizeBuffer =3D (SMM_COMM_GET_FLASH_SI= ZE_BLOCK_SIZE *)CommBuffer; + GetFlashSizeBlockSizeBuffer->ReturnStatus =3D EFI_SUCCESS; + GetFlashSizeBlockSizeBuffer->FlashSize =3D mFlashSize; + GetFlashSizeBlockSizeBuffer->BlockSize =3D mBlockSize; + break; + + case SPI_SMM_COMM_ID_READ_FLASH: + Status =3D mEfiSmmSpiFlashUpdateProtocol.Read ( + RweBuffer->FlashAddress, + RweBuffer->NumBytes, + RweBuffer->Buffer + ); + RweBuffer->ReturnStatus =3D Status; + break; + + case SPI_SMM_COMM_ID_WRITE_FALSH: + Status =3D mEfiSmmSpiFlashUpdateProtocol.Write ( + RweBuffer->FlashAddress, + RweBuffer->NumBytes, + RweBuffer->Buffer + ); + RweBuffer->ReturnStatus =3D Status; + break; + + case SPI_SMM_COMM_ID_ERASE_FALSH: + Status =3D mEfiSmmSpiFlashUpdateProtocol.Erase ( + RweBuffer->FlashAddress, + RweBuffer->NumBytes + ); + RweBuffer->ReturnStatus =3D Status; + break; + + default: + ASSERT (FALSE); + } + + return EFI_SUCCESS; +} + +/** + Read RTC through its registers using IO access. + + @param Address Address offset of RTC. It is recommended to use + macros such as RTC_ADDRESS_SECONDS. + @param Data The content you want to write into RTC. + +**/ +STATIC +UINT8 +IoRtcRead ( + IN UINT8 Address + ) +{ + IoWrite8 (0x70, Address); + return IoRead8 (0x71); +} + +/** + Write RTC through its registers using IO access. + + @param Address Address offset of RTC. It is recommended to use + macros such as RTC_ADDRESS_SECONDS. + @param Data The content you want to write into RTC. + +**/ +STATIC +VOID +IoRtcWrite ( + IN UINT8 Address, + IN UINT8 Data + ) +{ + IoWrite8 (0x70, Address); + IoWrite8 (0x71, Data); +} + +/** + Write RTC through its registers using IO access. + + @param Address Address offset of RTC. It is recommended to use + macros such as RTC_ADDRESS_SECONDS. + @param Data The content you want to write into RTC. + +**/ +VOID +EnableRtcWakeup ( + IN UINT16 AcpiBaseAddr, + IN UINT8 WakeAfter + ) +{ + volatile RTC_REGISTER_B RtcRegisterB; + + RtcRegisterB.Data =3D IoRtcRead (RTC_ADDRESS_REGISTER_B); + UINT8 CurrentSecond =3D IoRtcRead (RTC_ADDRESS_SECONDS); + UINT8 CurrentMinute =3D IoRtcRead (RTC_ADDRESS_MINUTES); + UINT8 CurrentHour =3D IoRtcRead (RTC_ADDRESS_HOURS); + + if (!(RtcRegisterB.Bits.Dm)) { + CurrentSecond =3D BcdToDecimal8 (CurrentSecond); + CurrentMinute =3D BcdToDecimal8 (CurrentMinute); + CurrentHour =3D BcdToDecimal8 (CurrentHour); + } + + CurrentSecond +=3D WakeAfter; + CurrentMinute +=3D CurrentSecond/60; + CurrentHour +=3D CurrentMinute/60; + + CurrentSecond %=3D 60; + CurrentMinute %=3D 60; + CurrentHour %=3D 24; + + if (!(RtcRegisterB.Bits.Dm)) { + CurrentSecond =3D DecimalToBcd8 (CurrentSecond); + CurrentMinute =3D DecimalToBcd8 (CurrentMinute); + CurrentHour =3D DecimalToBcd8 (CurrentHour); + } + + IoRtcWrite (RTC_ADDRESS_SECONDS_ALARM, CurrentSecond); + IoRtcWrite (RTC_ADDRESS_MINUTES_ALARM, CurrentMinute); + IoRtcWrite (RTC_ADDRESS_HOURS_ALARM, CurrentHour); + IoRtcRead (RTC_ADDRESS_REGISTER_C); + + RtcRegisterB.Data =3D IoRtcRead (RTC_ADDRESS_REGISTER_B); + RtcRegisterB.Bits.Aie =3D 1; + IoRtcWrite (RTC_ADDRESS_REGISTER_B, RtcRegisterB.Data); + + UINT8 RtcSts =3D IoRead8 (AcpiBaseAddr); + + RtcSts |=3D 0x400; + IoWrite8 (AcpiBaseAddr, RtcSts); + + UINT8 RtcEn =3D IoRead8 (AcpiBaseAddr + PM1_EN_HIGH_BYTE); + + RtcEn |=3D RTC_EVENT_ENABLE; + IoWrite8 (AcpiBaseAddr + PM1_EN_HIGH_BYTE, RtcEn); + return; +} + +/** + Set Capsule S3 Flag SMI Handler. + + This SMI handler provides services for marking capsule update. + + @param[in] DispatchHandle The unique handle assigned to this handle= r by SmiHandlerRegister(). + @param[in] RegisterContext Points to an optional handler context whi= ch was specified when the + handler was registered. + @param[in, out] CommBuffer A pointer to a collection of data in memo= ry that will + be conveyed from a non-SMM environment in= to an SMM environment. + @param[in, out] CommBufferSize The size of the CommBuffer. + + @retval EFI_SUCCESS The interrupt was handled an= d quiesced. No other handlers + should still be called. + +**/ +EFI_STATUS +EFIAPI +SetCapsuleS3FlagHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *RegisterContext, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize + ) +{ + EFI_STATUS Status; + AMD_CAPSULE_SMM_HOOK_PROTOCOL *AmdCapsuleSmmHookProtocol; + + AmdCapsuleSmmHookProtocol =3D NULL; + Status =3D gSmst->SmmLocateProtocol ( + &gAmdCapsuleSmmHookProtocolGuid, + NULL, + (VOID **)&AmdCapsuleSmmHookProtocol + ); + if (!EFI_ERROR (Status)) { + AmdCapsuleSmmHookProtocol->Hook (0); + } + + DEBUG ((DEBUG_INFO, "Entering S3 sleep.\n")); + // Transform system into S3 sleep state + EnableRtcWakeup (ACPIMMIO16 (ACPI_MMIO_BASE + PMIO_BASE + ACPI_PM1_EVT_B= LK), 2); + UINTN AcpiPm1CntBase =3D ACPIMMIO16 (ACPI_MMIO_BASE + PMIO_BASE + ACPI= _PM1_CNT_BLK); + UINT16 PmCntl =3D IoRead16 (AcpiPm1CntBase); + + PmCntl =3D (PmCntl & ~SLP_TYPE) | SUS_S3 | SLP_EN; + IoWrite16 (AcpiPm1CntBase, PmCntl); + return Status; +} + +/** + Smm Flash Update Driver main entry point. + + Install the Smm Flash Update Protocol on a new handle. Register SMM flas= h update + SMI handler. Locate SmmSpiProtocol and init the flash device size and bl= ock size. + + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS Variable service successfully initialized. + @retval others Some error occurs when executing this routine. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceInitialize ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_HANDLE FlashUpdateRegisterHandle; + EFI_HANDLE FlashUpdateInstallHandle; + SPI_INSTANCE *SpiInstance; + EFI_HANDLE SetCapsuleS3FlagHandle; + + SpiInstance =3D NULL; + + // + // Install the Smm Flash Update Protocol on a new handle + // + FlashUpdateInstallHandle =3D NULL; + Status =3D gSmst->SmmInstallProtocolInterface ( + &FlashUpdateInstallHandle, + &gEfiSmmSpiFlashUpdateProtocolGuid, + EFI_NATIVE_INTERFACE, + &mEfiSmmSpiFlashUpdateProtocol + ); + ASSERT_EFI_ERROR (Status); + + // + // Register SMM flash update SMI handler + // + FlashUpdateRegisterHandle =3D NULL; + Status =3D gSmst->SmiHandlerRegister ( + FlashUpdateServiceHandler, + &gEfiSmmSpiFlashUpdateProtocolGuid, + &FlashUpdateRegisterHandle + ); + ASSERT_EFI_ERROR (Status); + + SetCapsuleS3FlagHandle =3D NULL; + Status =3D gSmst->SmiHandlerRegister ( + SetCapsuleS3FlagHandler, + &gAmdSetCapsuleS3FlagGuid, + &SetCapsuleS3FlagHandle + ); + ASSERT_EFI_ERROR (Status); + + Status =3D gSmst->SmmLocateProtocol ( + &gEfiSmmSpiProtocolGuid, + NULL, + (VOID **)&mSmmSpiProtocol + ); + ASSERT_EFI_ERROR (Status); + + SpiInstance =3D SPI_INSTANCE_FROM_SPIPROTOCOL (mSmmSpiProtocol= ); + mFlashSize =3D SpiInstance->SpiInitTable.BiosSize; + mFlashAreaBaseAddress =3D FixedPcdGet32 (PcdFlashAreaBaseAddress); + mBlockSize =3D SpiInstance->SpiInitTable.OpcodeMenu[SPI_OPCOD= E_ERASE_INDEX].Operation; + + return Status; +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUp= dateSmm.h b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpd= ateSmm.h new file mode 100644 index 0000000000..14bd37ed2e --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpdateSmm= .h @@ -0,0 +1,123 @@ +/** @file + Implements AMD FlashUpdateSmm.h + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef FLASH_UPDATE_SMM_H__ +#define FLASH_UPDATE_SMM_H__ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "FlashUpdateCommon.h" +#include "PcRtc.h" + +/** + Read data from flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[out] Buffer Buffer contain the read data. + + @retval EFI_SUCCESS Read successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceFlashFdRead ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + OUT VOID *Buffer + ); + +/** + Erase flash region according to input in a block size. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte, a block size in = flash device. + + @retval EFI_SUCCESS Erase successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceFlashFdErase ( + IN UINTN FlashAddress, + IN UINTN NumBytes + ); + +/** + Write data to flash device. + + Write Buffer(FlashAddress|NumBytes) to flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[in] Buffer Buffer contain the write data. + + @retval EFI_SUCCESS Write successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceFlashFdWrite ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + IN UINT8 *Buffer + ); + +/** + Get flash device size and flash block size. + + @param[out] FlashSize Pointer to the size of flash dev= ice. + @param[out] BlockSize Pointer to the size of block in = flash device. + + @retval EFI_SUCCESS Get successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateServiceGetFlashSizeBlockSize ( + OUT UINTN *FlashSize, + OUT UINTN *BlockSize + ); + +/** + Set AMD Capsule SMM Flag hook + + @param[out] Reserved Not used; Must be 0. + + @retval EFI_SUCCESS Set successfully. + +**/ + +typedef EFI_STATUS (*AMD_CAPSULE_SMM_HOOK) ( + IN UINT32 Reserved + ); + +typedef struct _AMD_CAPSULE_SMM_HOOK_PROTOCOL { + AMD_CAPSULE_SMM_HOOK Hook; +} AMD_CAPSULE_SMM_HOOK_PROTOCOL; + +extern EFI_GUID gAmdSetCapsuleS3FlagGuid; +extern EFI_GUID gAmdCapsuleSmmHookProtocolGuid; + +#endif // _FLASH_UPDATE_SMM_H_ diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUp= dateSmm.inf b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashU= pdateSmm.inf new file mode 100644 index 0000000000..f39e814512 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpdateSmm= .inf @@ -0,0 +1,59 @@ +## @file +# Flash UpdateSmm +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D FlashUpdateSmm + FILE_GUID =3D 42859181-A407-4CF2-A9A7-5848FEAA8958 + MODULE_TYPE =3D DXE_SMM_DRIVER + VERSION_STRING =3D 1.0 + PI_SPECIFICATION_VERSION =3D 0x0001000A + ENTRY_POINT =3D FlashUpdateServiceInitialize + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + FlashUpdateSmm.c + FlashUpdateSmm.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + VanGoghCommonPkg/AmdCommonPkg.dec + AgesaPublic/AgesaPublic.dec + ChachaniBoardPkg/Project.dec + +[LibraryClasses] + UefiDriverEntryPoint + SmmServicesTableLib + DebugLib + BaseLib + MemoryAllocationLib + SmmMemLib + PcdLib + IoLib + +[Protocols] + gEfiSmmSpiProtocolGuid ## CONSUME + + gEfiSmmSpiFlashUpdateProtocolGuid ## PRODUCE + + gAmdCapsuleSmmHookProtocolGuid ## CONSUME + +[Guids] + gAmdSetCapsuleS3FlagGuid + +[Pcd] + gPlatformPkgTokenSpaceGuid.PcdFlashAreaBaseAddress + +[Depex] + gEfiSmmSpiProtocolGuid diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUp= dateSmmRuntimeDxe.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdat= e/FlashUpdateSmmRuntimeDxe.c new file mode 100644 index 0000000000..80e60b30fd --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpdateSmm= RuntimeDxe.c @@ -0,0 +1,407 @@ +/*************************************************************************= **** + * + * Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved. + * SPDX-License-Identifier: BSD-2-Clause-Patent + *************************************************************************= ***** + */ + +#include "FlashUpdateCommon.h" + +EFI_EVENT mVirtualAddressChangeEvent =3D NULL; +UINTN *mFlashSize =3D NULL; +UINTN *mBlockSize =3D NULL; +EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication =3D NULL; +UINT8 *mRweBuffer =3D NULL; +UINTN mRweBufferSize; + +EFI_SPI_FLASH_UPDATE_PROTOCOL mEfiSpiFlashUpdateProtocol =3D { + SfuProtocolFlashFdRead, + SfuProtocolFlashFdErase, + SfuProtocolFlashFdWrite, + SfuProtocolGetFlashSizeBlockSize, +}; + +/** + Initialize the communicate buffer. + + The communicate buffer is: SMM_COMMUNICATE_HEADER + SMM_COMM_RWE_FLASH += Payload. + The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_COMM_RWE_FLAS= H_SIZE + FlashSize. + + @param[out] DataPtr Points to the data in the communicate = buffer. + +**/ +VOID +EFIAPI +InitCommunicateBuffer ( + OUT VOID **DataPtr + ) +{ + EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; + + SmmCommunicateHeader =3D (EFI_SMM_COMMUNICATE_HEADER *)mRweBuffer; + CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmSpiFlashUpdateProto= colGuid); + SmmCommunicateHeader->MessageLength =3D SMM_COMM_RWE_FLASH_SIZE; + + *DataPtr =3D (SMM_COMM_RWE_FLASH *)((EFI_SMM_COMMUNICATE_HEADER *)mRweBu= ffer)->Data; +} + +/** + Read data from flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[out] Buffer Buffer contain the read data. + + @retval EFI_SUCCESS Read successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolFlashFdRead ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + OUT VOID *Buffer + ) +{ + EFI_STATUS Status; + SMM_COMM_RWE_FLASH *RweFlashBuffer; + + if ((FlashAddress >=3D *mFlashSize) || (NumBytes =3D=3D 0) || (Buffer = =3D=3D NULL) || + (FlashAddress + NumBytes > *mFlashSize)) + { + return EFI_INVALID_PARAMETER; + } + + RweFlashBuffer =3D NULL; + + InitCommunicateBuffer ((VOID **)&RweFlashBuffer); + + if (RweFlashBuffer =3D=3D NULL) { + return EFI_BUFFER_TOO_SMALL; + } + + RweFlashBuffer->id =3D SPI_SMM_COMM_ID_READ_FLASH; + RweFlashBuffer->FlashAddress =3D FlashAddress; + RweFlashBuffer->NumBytes =3D NumBytes; + CopyMem (RweFlashBuffer->Buffer, Buffer, NumBytes); + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate ( + mSmmCommunication, + mRweBuffer, + &mRweBufferSize + ); + ASSERT_EFI_ERROR (Status); + + // + // Get data from SMM + // + if (!EFI_ERROR (RweFlashBuffer->ReturnStatus)) { + CopyMem (Buffer, RweFlashBuffer->Buffer, NumBytes); + } + + return RweFlashBuffer->ReturnStatus; +} + +/** + Erase flash region according to input in a block size. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte, a block size in = flash device. + + @retval EFI_SUCCESS Erase successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolFlashFdErase ( + IN UINTN FlashAddress, + IN UINTN NumBytes + ) +{ + EFI_STATUS Status; + SMM_COMM_RWE_FLASH *RweFlashBuffer; + + if ((FlashAddress >=3D *mFlashSize) || (NumBytes =3D=3D 0) || (NumBytes = > *mFlashSize) || + (FlashAddress + NumBytes > *mFlashSize)) + { + return EFI_INVALID_PARAMETER; + } + + RweFlashBuffer =3D NULL; + + InitCommunicateBuffer ((VOID **)&RweFlashBuffer); + + if (RweFlashBuffer =3D=3D NULL) { + return EFI_BUFFER_TOO_SMALL; + } + + RweFlashBuffer->id =3D SPI_SMM_COMM_ID_ERASE_FALSH; + RweFlashBuffer->FlashAddress =3D FlashAddress; + RweFlashBuffer->NumBytes =3D NumBytes; + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate ( + mSmmCommunication, + mRweBuffer, + &mRweBufferSize + ); + ASSERT_EFI_ERROR (Status); + + return RweFlashBuffer->ReturnStatus; +} + +/** + Write data to flash device. + + Write Buffer(FlashAddress|NumBytes) to flash device. + + @param[in] FlashAddress Physical flash address. + @param[in] NumBytes Number in Byte. + @param[in] Buffer Buffer contain the write data. + + @retval EFI_SUCCESS Write successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolFlashFdWrite ( + IN UINTN FlashAddress, + IN UINTN NumBytes, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + SMM_COMM_RWE_FLASH *RweFlashBuffer; + + if ((FlashAddress >=3D *mFlashSize) || (NumBytes =3D=3D 0) || (NumBytes = > *mFlashSize) || (Buffer =3D=3D NULL) || + (FlashAddress + NumBytes > *mFlashSize)) + { + return EFI_INVALID_PARAMETER; + } + + RweFlashBuffer =3D NULL; + + InitCommunicateBuffer ((VOID **)&RweFlashBuffer); + + if (RweFlashBuffer =3D=3D NULL) { + return EFI_BUFFER_TOO_SMALL; + } + + RweFlashBuffer->id =3D SPI_SMM_COMM_ID_WRITE_FALSH; + RweFlashBuffer->FlashAddress =3D FlashAddress; + RweFlashBuffer->NumBytes =3D NumBytes; + CopyMem (RweFlashBuffer->Buffer, Buffer, NumBytes); + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate ( + mSmmCommunication, + mRweBuffer, + &mRweBufferSize + ); + ASSERT_EFI_ERROR (Status); + + return RweFlashBuffer->ReturnStatus; +} + +/** + Get flash device size and flash block size. + + @param[out] FlashSize Pointer to the size of flash dev= ice. + @param[out] BlockSize Pointer to the size of block in = flash device. + + @retval EFI_SUCCESS Get successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +SfuProtocolGetFlashSizeBlockSize ( + OUT UINTN *FlashSize, + OUT UINTN *BlockSize + ) +{ + if ((FlashSize =3D=3D NULL) || (BlockSize =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + *FlashSize =3D *mFlashSize; + *BlockSize =3D *mBlockSize; + + return EFI_SUCCESS; +} + +/** + Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE. + + This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS= _CHANGE event. + It convers pointer to new virtual address. + + @param[in] Event Event whose notification function is being invo= ked. + @param[in] Context Pointer to the notification function's context. + +**/ +VOID +EFIAPI +SpiFlashAddressChangeEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0x0, (VOID **)&mRweBuffer); + EfiConvertPointer (0x0, (VOID **)&mSmmCommunication); + EfiConvertPointer (0x0, (VOID **)&mBlockSize); + EfiConvertPointer (0x0, (VOID **)&mFlashSize); +} + +/** + Get flash device size and flash block size from SMM. + + @param[in] VOID + + @retval EFI_SUCCESS Get successfully. + @retval others Some error occurs when executing= this routine. + +**/ +EFI_STATUS +EFIAPI +GetFlashSizeBlockSize ( + VOID + ) +{ + EFI_STATUS Status; + UINTN CommSize; + UINT8 *CommBuffer; + EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader; + SMM_COMM_GET_FLASH_SIZE_BLOCK_SIZE *SmmGetFlashSizeBlockSize; + + CommBuffer =3D NULL; + SmmCommunicateHeader =3D NULL; + SmmGetFlashSizeBlockSize =3D NULL; + + // + // Init the communicate buffer. The buffer size is: + // SMM_COMMUNICATE_HEADER_SIZE + sizeof (SMM_COMM_GET_FLASH_SIZE_BLOCK_S= IZE) + // + CommSize =3D SMM_COMMUNICATE_HEADER_SIZE + sizeof (SMM_COMM_GET_FLASH_= SIZE_BLOCK_SIZE); + CommBuffer =3D AllocateRuntimePool (CommSize); + if (CommBuffer =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + return Status; + } + + SmmCommunicateHeader =3D (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer; + CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmSpiFlashUpdateProto= colGuid); + SmmCommunicateHeader->MessageLength =3D sizeof (SMM_COMM_GET_FLASH_SIZE_= BLOCK_SIZE); + + SmmGetFlashSizeBlockSize =3D (SMM_COMM_GET_FLASH_SIZE_BLOCK_SIZE *)S= mmCommunicateHeader->Data; + SmmGetFlashSizeBlockSize->id =3D SPI_SMM_COMM_ID_GET_FLASH_SIZE_BLOCK_SI= ZE; + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate (mSmmCommunication, CommBuffer= , &CommSize); + ASSERT_EFI_ERROR (Status); + + Status =3D SmmGetFlashSizeBlockSize->ReturnStatus; + if (EFI_ERROR (Status)) { + if (CommBuffer !=3D NULL) { + FreePool (CommBuffer); + } + + return Status; + } + + // + // Get data from SMM. + // + *mFlashSize =3D SmmGetFlashSizeBlockSize->FlashSize; + *mBlockSize =3D SmmGetFlashSizeBlockSize->BlockSize; + + if (CommBuffer !=3D NULL) { + FreePool (CommBuffer); + } + + return Status; +} + +/** + Update Flash Driver main entry point. + + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS Update Flash service successfully initialized. + +**/ +EFI_STATUS +EFIAPI +FlashUpdateSmmRuntimeInitialize ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + + Status =3D gBS->LocateProtocol ( + &gEfiSmmCommunicationProtocolGuid, + NULL, + (VOID **)&mSmmCommunication + ); + ASSERT_EFI_ERROR (Status); + + // + // Allocate memory for flash device size communicate buffer. + // + mFlashSize =3D AllocateRuntimePool (sizeof (UINTN)); + ASSERT (mFlashSize !=3D NULL); + + // + // Allocate memory for flash device block size communicate buffer. + // + mBlockSize =3D AllocateRuntimePool (sizeof (UINTN)); + ASSERT (mBlockSize !=3D NULL); + + Status =3D GetFlashSizeBlockSize (); + ASSERT_EFI_ERROR (Status); + + // + // Allocate memory for update flash communicate buffer. + // + mRweBufferSize =3D SMM_COMMUNICATE_HEADER_SIZE + SMM_COMM_RWE_FLASH_SIZE= + *mFlashSize; + mRweBuffer =3D AllocateRuntimePool (mRweBufferSize); + ASSERT (mRweBuffer !=3D NULL); + + gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + SpiFlashAddressChangeEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mVirtualAddressChangeEvent + ); + + Handle =3D NULL; + Status =3D gBS->InstallProtocolInterface ( + &Handle, + &gEfiSpiFlashUpdateProtocolGuid, + EFI_NATIVE_INTERFACE, + &mEfiSpiFlashUpdateProtocol + ); + ASSERT_EFI_ERROR (Status); + + return EFI_SUCCESS; +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUp= dateSmmRuntimeDxe.inf b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpd= ate/FlashUpdateSmmRuntimeDxe.inf new file mode 100644 index 0000000000..7f606dd048 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/FlashUpdateSmm= RuntimeDxe.inf @@ -0,0 +1,48 @@ +## @file +# Flash Update SmmRuntimeDxe +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D FlashUpdateSmmRuntimeDxe + FILE_GUID =3D FA08552D-9666-42A0-A327-BF342B03DA2C + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D FlashUpdateSmmRuntimeInitialize + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + FlashUpdateSmmRuntimeDxe.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + VanGoghCommonPkg/AmdCommonPkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + DebugLib + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + UefiRuntimeLib + +[Protocols] + gEfiSmmCommunicationProtocolGuid ## CONSUME + gEfiSmmSpiFlashUpdateProtocolGuid ## CONSUME + gEfiSpiFlashUpdateProtocolGuid ## PRODUCE + +[Guids] + gEfiEventVirtualAddressChangeGuid ## CONSUME ## Event + +[Depex] + gEfiSmmCommunicationProtocolGuid diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/PcRtc.h= b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/PcRtc.h new file mode 100644 index 0000000000..08caf4257b --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/FlashUpdate/PcRtc.h @@ -0,0 +1,375 @@ +/** @file + Implements AMD PcRtc + Header file for real time clock driver. + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef RTC_H__ +#define RTC_H__ + +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + EFI_LOCK RtcLock; + INT16 SavedTimeZone; + UINT8 Daylight; + UINT8 CenturyRtcAddress; +} PC_RTC_MODULE_GLOBALS; + +extern PC_RTC_MODULE_GLOBALS mModuleGlobal; + +#define PCAT_RTC_ADDRESS_REGISTER 0x70 +#define PCAT_RTC_DATA_REGISTER 0x71 + +// +// Dallas DS12C887 Real Time Clock +// +#define RTC_ADDRESS_SECONDS 0 // R/W Range 0..59 +#define RTC_ADDRESS_SECONDS_ALARM 1 // R/W Range 0..59 +#define RTC_ADDRESS_MINUTES 2 // R/W Range 0..59 +#define RTC_ADDRESS_MINUTES_ALARM 3 // R/W Range 0..59 +#define RTC_ADDRESS_HOURS 4 // R/W Range 1..12 or 0..23 Bit= 7 is AM/PM +#define RTC_ADDRESS_HOURS_ALARM 5 // R/W Range 1..12 or 0..23 Bit= 7 is AM/PM +#define RTC_ADDRESS_DAY_OF_THE_WEEK 6 // R/W Range 1..7 +#define RTC_ADDRESS_DAY_OF_THE_MONTH 7 // R/W Range 1..31 +#define RTC_ADDRESS_MONTH 8 // R/W Range 1..12 +#define RTC_ADDRESS_YEAR 9 // R/W Range 0..99 +#define RTC_ADDRESS_REGISTER_A 10 // R/W[0..6] R0[7] +#define RTC_ADDRESS_REGISTER_B 11 // R/W +#define RTC_ADDRESS_REGISTER_C 12 // RO +#define RTC_ADDRESS_REGISTER_D 13 // RO +// +// Date and time initial values. +// They are used if the RTC values are invalid during driver initializatio= n +// +#define RTC_INIT_SECOND 0 +#define RTC_INIT_MINUTE 0 +#define RTC_INIT_HOUR 0 +#define RTC_INIT_DAY 1 +#define RTC_INIT_MONTH 1 + +#pragma pack(1) +// +// Register A +// +typedef struct { + UINT8 Rs : 4; // Rate Selection Bits + UINT8 Dv : 3; // Divisor + UINT8 Uip : 1; // Update in progress +} RTC_REGISTER_A_BITS; + +typedef union { + RTC_REGISTER_A_BITS Bits; + UINT8 Data; +} RTC_REGISTER_A; + +// +// Register B +// +typedef struct { + UINT8 Dse : 1; // 0 - Daylight saving disabled 1 - Daylight savings= enabled + UINT8 Mil : 1; // 0 - 12 hour mode 1 - 24 hour mode + UINT8 Dm : 1; // 0 - BCD Format 1 - Binary Format + UINT8 Sqwe : 1; // 0 - Disable SQWE output 1 - Enable SQWE outp= ut + UINT8 Uie : 1; // 0 - Update INT disabled 1 - Update INT enabl= ed + UINT8 Aie : 1; // 0 - Alarm INT disabled 1 - Alarm INT Enable= d + UINT8 Pie : 1; // 0 - Periodic INT disabled 1 - Periodic INT Ena= bled + UINT8 Set : 1; // 0 - Normal operation. 1 - Updates inhibite= d +} RTC_REGISTER_B_BITS; + +typedef union { + RTC_REGISTER_B_BITS Bits; + UINT8 Data; +} RTC_REGISTER_B; + +// +// Register C +// +typedef struct { + UINT8 Reserved : 4; // Read as zero. Can not be written. + UINT8 Uf : 1; // Update End Interrupt Flag + UINT8 Af : 1; // Alarm Interrupt Flag + UINT8 Pf : 1; // Periodic Interrupt Flag + UINT8 Irqf : 1; // Iterrupt Request Flag =3D PF & PIE | AF & AIE = | UF & UIE +} RTC_REGISTER_C_BITS; + +typedef union { + RTC_REGISTER_C_BITS Bits; + UINT8 Data; +} RTC_REGISTER_C; + +// +// Register D +// +typedef struct { + UINT8 Reserved : 7; // Read as zero. Can not be written. + UINT8 Vrt : 1; // Valid RAM and Time +} RTC_REGISTER_D_BITS; + +typedef union { + RTC_REGISTER_D_BITS Bits; + UINT8 Data; +} RTC_REGISTER_D; + +#pragma pack() + +/** + Initialize RTC. + + @param Global For global use inside this module. + + @retval EFI_DEVICE_ERROR Initialization failed due to device error. + @retval EFI_SUCCESS Initialization successful. + +**/ +EFI_STATUS +PcRtcInit ( + IN PC_RTC_MODULE_GLOBALS *Global + ); + +/** + Sets the current local time and date information. + + @param Time A pointer to the current time. + @param Global For global use inside this module. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The time could not be set due due to hardw= are error. + +**/ +EFI_STATUS +PcRtcSetTime ( + IN EFI_TIME *Time, + IN PC_RTC_MODULE_GLOBALS *Global + ); + +/** + Returns the current time and date information, and the time-keeping capa= bilities + of the hardware platform. + + @param Time A pointer to storage to receive a snapshot of the = current time. + @param Capabilities An optional pointer to a buffer to receive the rea= l time clock + device's capabilities. + @param Global For global use inside this module. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER Time is NULL. + @retval EFI_DEVICE_ERROR The time could not be retrieved due to ha= rdware error. + +**/ +EFI_STATUS +PcRtcGetTime ( + OUT EFI_TIME *Time, + OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL + IN PC_RTC_MODULE_GLOBALS *Global + ); + +/** + Sets the system wakeup alarm clock time. + + @param Enabled Enable or disable the wakeup alarm. + @param Time If Enable is TRUE, the time to set the wakeup alarm for= . + If Enable is FALSE, then this parameter is optional, an= d may be NULL. + @param Global For global use inside this module. + + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm w= as enabled. + If Enable is FALSE, then the wakeup alarm = was disabled. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a = hardware error. + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this pl= atform. + +**/ +EFI_STATUS +PcRtcSetWakeupTime ( + IN BOOLEAN Enable, + IN EFI_TIME *Time, OPTIONAL + IN PC_RTC_MODULE_GLOBALS *Global + ); + +/** + Returns the current wakeup alarm clock setting. + + @param Enabled Indicates if the alarm is currently enabled or disabled= . + @param Pending Indicates if the alarm signal is pending and requires a= cknowledgement. + @param Time The current alarm setting. + @param Global For global use inside this module. + + @retval EFI_SUCCESS The alarm settings were returned. + @retval EFI_INVALID_PARAMETER Enabled is NULL. + @retval EFI_INVALID_PARAMETER Pending is NULL. + @retval EFI_INVALID_PARAMETER Time is NULL. + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due= to a hardware error. + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this pl= atform. + +**/ +EFI_STATUS +PcRtcGetWakeupTime ( + OUT BOOLEAN *Enabled, + OUT BOOLEAN *Pending, + OUT EFI_TIME *Time, + IN PC_RTC_MODULE_GLOBALS *Global + ); + +/** + The user Entry Point for PcRTC module. + + This is the entrhy point for PcRTC module. It installs the UEFI runtime = service + including GetTime(),SetTime(),GetWakeupTime(),and SetWakeupTime(). + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point is executed successfully. + @retval Others Some error occurs when executing this entry point= . + +**/ +EFI_STATUS +EFIAPI +InitializePcRtc ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +/** + See if all fields of a variable of EFI_TIME type is correct. + + @param Time The time to be checked. + + @retval EFI_INVALID_PARAMETER Some fields of Time are not correct. + @retval EFI_SUCCESS Time is a valid EFI_TIME variable. + +**/ +EFI_STATUS +RtcTimeFieldsValid ( + IN EFI_TIME *Time + ); + +/** + Converts time from EFI_TIME format defined by UEFI spec to RTC's. + + This function converts time from EFI_TIME format defined by UEFI spec to= RTC's. + If data mode of RTC is BCD, then converts EFI_TIME to it. + If RTC is in 12-hour format, then converts EFI_TIME to it. + + @param Time On input, the time data read from UEFI to convert + On output, the time converted to RTC format + @param RegisterB Value of Register B of RTC, indicating data mode +**/ +VOID +ConvertEfiTimeToRtcTime ( + IN OUT EFI_TIME *Time, + IN RTC_REGISTER_B RegisterB + ); + +/** + Converts time read from RTC to EFI_TIME format defined by UEFI spec. + + This function converts raw time data read from RTC to the EFI_TIME forma= t + defined by UEFI spec. + If data mode of RTC is BCD, then converts it to decimal, + If RTC is in 12-hour format, then converts it to 24-hour format. + + @param Time On input, the time data read from RTC to convert + On output, the time converted to UEFI format + @param RegisterB Value of Register B of RTC, indicating data mode + and hour format. + + @retval EFI_INVALID_PARAMETER Parameters passed in are invalid. + @retval EFI_SUCCESS Convert RTC time to EFI time successfull= y. + +**/ +EFI_STATUS +ConvertRtcTimeToEfiTime ( + IN OUT EFI_TIME *Time, + IN RTC_REGISTER_B RegisterB + ); + +/** + Wait for a period for the RTC to be ready. + + @param Timeout Tell how long it should take to wait. + + @retval EFI_DEVICE_ERROR RTC device error. + @retval EFI_SUCCESS RTC is updated and ready. +**/ +EFI_STATUS +RtcWaitToUpdate ( + UINTN Timeout + ); + +/** + See if field Day of an EFI_TIME is correct. + + @param Time Its Day field is to be checked. + + @retval TRUE Day field of Time is correct. + @retval FALSE Day field of Time is NOT correct. +**/ +BOOLEAN +DayValid ( + IN EFI_TIME *Time + ); + +/** + Check if it is a leapyear. + + @param Time The time to be checked. + + @retval TRUE It is a leapyear. + @retval FALSE It is NOT a leapyear. +**/ +BOOLEAN +IsLeapYear ( + IN EFI_TIME *Time + ); + +/** + Get the century RTC address from the ACPI FADT table. + + @return The century RTC address or 0 if not found. +**/ +UINT8 +GetCenturyRtcAddress ( + VOID + ); + +/** + Notification function of ACPI Table change. + + This is a notification function registered on ACPI Table change event. + It saves the Century address stored in ACPI FADT table. + + @param Event Event whose notification function is being invoked. + @param Context Pointer to the notification function's context. + +**/ +VOID +EFIAPI +PcRtcAcpiTableChangeCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +#endif -- 2.31.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#114503): https://edk2.groups.io/g/devel/message/114503 Mute This Topic: https://groups.io/mt/103971400/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-