From nobody Mon Sep 16 20:08:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+114586+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1706274741315648.7463082295442; Fri, 26 Jan 2024 05:12:21 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=QHVAF1P+pahLeJdfwR0AqjNOjpPSCX99yhCMx3ndz+8=; 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=1706274740; v=1; b=Ghprpk+AuNvhmcsgXrvfi4lvtgX+2d9g9pXTYNJtm4kehRWyco9JnZnsxQCa4HCBljTtd4+K 8DNuCfwwHiYbxmddzjlI1At81Nv1F/Txu7DYuXbDNQq3Eqt4ubXJbCP8kEogQdhIRp0MKZoB9Sy dogfMSiY6eQLWIb0in1JMuo8= X-Received: by 127.0.0.2 with SMTP id URCOYY1788612xQVxmFTgW1i; Fri, 26 Jan 2024 05:12:20 -0800 X-Received: from NAM04-MW2-obe.outbound.protection.outlook.com (NAM04-MW2-obe.outbound.protection.outlook.com [40.107.101.86]) by mx.groups.io with SMTP id smtpd.web11.15441.1706274740204479922 for ; Fri, 26 Jan 2024 05:12:20 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lvptm+0XmZtHkLAmMbkt+aykf9KvB7mL7tg6bzku6CdfSCQvbVR8nsEDUhf0WiGzgWxnnUQqCf1WuPlsKM3jDCVn3VzXXwLNiN7dIggS/E1L8gbTlzgG39m2jVRPlg2X+k8FAGmu76GBeDBIWVMsFgoFjkhGnl1ZiKWAPJgE9021V+znyY6vAJaVT9FXgBubj7Onk60Dc/z/Q3MSwGRvOiY7XsM8cL3tQbCdUb+41qVsrvrsYXNbpthrrYyjuawapq1EIsCuNbmc2/m36AQcb3KZRuCHqoTTcNzXBIwvpchKIUATx+Q+PA/NCfYibmJAUbygnjHufqjC9pQToFdK4g== 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=zpyaxnJOFicLY05FCZwqqFOxaxyjJlWJSsHTI/Hxplc=; b=Rku2jPuVN8N1gV4dxGw1DqGzyZ5uoOTLpIs9Y6Lk+VLI28Tri+zAa02to0TPJ9CTqIpybOfmWAGJvJTjY3xS/bdTQ/CFfKcBwkaWn9B149ZvlThJbolSp0o+fUIJBQz5ObV5UTPZpUtfwbuja+dnA17cmlHs15CmigcUR2rCpwPBz/sp63aCCtnVFaScOVJ7OGG6WU512YyZMrhBmrDk+WQcaxTPW2nArt9QdhfETke3wr1bYw2GJMSNiqMXC7kyf45UCYG9jZjwwop0OX82aKqRMXKXwTsrRTKFx+X9ywygPdAplpcjWs1uS/zJRZxw663W5APbDttj8d6u1lMPKQ== 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 CYXPR02CA0096.namprd02.prod.outlook.com (2603:10b6:930:ce::7) by LV3PR12MB9355.namprd12.prod.outlook.com (2603:10b6:408:216::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27; Fri, 26 Jan 2024 13:12:16 +0000 X-Received: from CY4PEPF0000E9D1.namprd03.prod.outlook.com (2603:10b6:930:ce:cafe::4b) by CYXPR02CA0096.outlook.office365.com (2603:10b6:930:ce::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 13:12:15 +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 (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+114586+1787277+3901457@groups.io; helo=mail02.groups.io; 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 CY4PEPF0000E9D1.mail.protection.outlook.com (10.167.241.144) 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 13:12:15 +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 07:12:13 -0600 From: "Zhai, MingXin (Duke) via groups.io" To: CC: Eric Xing , Ken Yao , Igniculus Fu , Abner Chang Subject: [edk2-devel] [PATCH V3 10/32] AMD/VanGoghBoard: Check in FlashUpdate Date: Fri, 26 Jan 2024 21:11:03 +0800 Message-ID: <20240126131125.1881-11-duke.zhai@amd.com> In-Reply-To: <20240126131125.1881-1-duke.zhai@amd.com> References: <20240126131125.1881-1-duke.zhai@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D1:EE_|LV3PR12MB9355:EE_ X-MS-Office365-Filtering-Correlation-Id: 493d3fef-bbc2-4207-57de-08dc1e706ae3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: MZlZkO71PWSz6d3fxpeoz9bqqamSIzZj1pXZwNuvKtw3QuVWBX4mY+m5eUBV0j1rNldM8xgDQ2QFRFvswsLonccgaYwvuXK0ge3kXn+GgxWLadElT4vJSgaaYOW9ptou6IemJ7AUCu12htcGgr9lbR8zWi0v4S3bgknru396kq1jzI5tO1EBreaWl9VWGYTU3b7vmpm4LvRuUsJWaQ5mQyhPn3dOfLoFfjpX6zHAEtZGcs6/LXHdJuU7cHbBdEE0/1vOqtkXE/omDNuSMrp6MpBee1VTQqUi4T4D8mkGT1spdNaWlGng8tkdSXP+TmVvYY/KW4/EHWZJmdTPgZAZ8IhLpLUtaxmAqfDaejgxxo3rXvOIdybTsGf3i7ZorhOzlSwQxOgX9p5MMnALxIn+6YR5bFYWSib6apzCWzguFoU2m+2QyZWke2maMQ9aA9QMgISRMtXkCy44UOG21drXFBuhtj/PUD0hFfMYFSHx6ZXBiM4bAJnEkanBJhAZR+CIYrSGObeLy9FtUOYsnu8XmKOV/6EjAypKUqWInVnwvHSnK7ZL+YAZetXxJhfwDKAvjjPcxcXuJiSlP7TV9dx5HCCQHryG4F4XeEeOIKCE2wWm8pWK38jYtVYiamrwk3MlvM1JNhocksG18Qbsz8YF3Q7Htk901pw3UKdj2U3sF5TtwwZQJR223Kvg2Pyi/7LFJm30IkPc0elxtxpZbsIz1cpHzScljj6NwE6zT6asSr2CpFxQvtW40pGL4RcfDFxd6Mf2gCaZq4kEpZ885sXSxQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 13:12:15.7127 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 493d3fef-bbc2-4207-57de-08dc1e706ae3 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: CY4PEPF0000E9D1.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR12MB9355 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: 72WE0xL17GlUoPJmobtAnE4Hx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1706274742088100029 Content-Type: text/plain; charset="utf-8" From: Duke Zhai BZ #:4640 In V3: Improve coding style follow edk2 C coding standard. 1.Remove macro definition extra underscores. 2.Putting some AMD copyright in the right place. 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..77967f1f26 --- /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 device + 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, // PrefixOpcodeIndex + 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..6019e635c5 --- /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..2354eaa2b7 --- /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 initialization +// +#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 Enabled + UINT8 Pie : 1; // 0 - Periodic INT disabled 1 - Periodic INT Ena= bled + UINT8 Set : 1; // 0 - Normal operation. 1 - Updates inhibited +} 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 format + 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 --=20 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 (#114586): https://edk2.groups.io/g/devel/message/114586 Mute This Topic: https://groups.io/mt/103975450/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-