From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (EUR05-DB8-obe.outbound.protection.outlook.com [40.107.20.81]) by mx.groups.io with SMTP id smtpd.web12.13749.1593005727472941712 for ; Wed, 24 Jun 2020 06:35:27 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@armh.onmicrosoft.com header.s=selector2-armh-onmicrosoft-com header.b=rO3qkuPy; spf=pass (domain: arm.com, ip: 40.107.20.81, mailfrom: sami.mujawar@arm.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mr+Y29oHMUG37ys6t5rmwAklm/JM1twWDpsEXIZqD+k=; b=rO3qkuPyLl2yDlFG1FL/xm6u0tkzx8bw0E/bBIcyEMOgC0YmeNfH829MGRBLZYdVnK9X/u5lWVZHUSvZVVlOFWkB/9Rcip9CAJr8sEt8j/MDX/jewkcIUn0LBcGVEh3MAJL8VjT73f4vPhu4S2mTv9+L2HtgwIVJ6e0IxPlhsl8= Received: from DB6PR0801CA0049.eurprd08.prod.outlook.com (2603:10a6:4:2b::17) by DB6PR0802MB2293.eurprd08.prod.outlook.com (2603:10a6:4:86::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.23; Wed, 24 Jun 2020 13:35:25 +0000 Received: from DB5EUR03FT059.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:2b:cafe::f4) by DB6PR0801CA0049.outlook.office365.com (2603:10a6:4:2b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.23 via Frontend Transport; Wed, 24 Jun 2020 13:35:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; edk2.groups.io; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;edk2.groups.io; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DB5EUR03FT059.mail.protection.outlook.com (10.152.21.175) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.20 via Frontend Transport; Wed, 24 Jun 2020 13:35:24 +0000 Received: ("Tessian outbound 217a52b9caed:v59"); Wed, 24 Jun 2020 13:35:24 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: c6f6e816997b3ea3 X-CR-MTA-TID: 64aa7808 Received: from b323b984d1a5.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 634BE867-E932-4D66-A8CC-D6E3D0231E6C.1; Wed, 24 Jun 2020 13:35:19 +0000 Received: from EUR02-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id b323b984d1a5.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 24 Jun 2020 13:35:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PVcltADKf5MdTE5m9czbBNc4vhro3ZCShw9Lj30RHfYOJGAZoiNNv0uM7KfF4UYZ9dsRJR/BjImugGFdy5Afec2GbocpPIwzDL4j51TUPWy4A9TIxGoYv9iXKGTcNwdfktwbKgOEfC55pYOt/NI+hg0yvmhMWUMxqQJxo59dtQjsQsAd4L+v9sakPPIuQfDa70Hjk0J5J/+G97UGq4cS9FiuFhJrND0ZTaGsmb5RgIHbfRQSDfF1w7/gMdsyx1L07doBaiPUxz6FWMqYBtfLNqilEGDPTE5VnI9O8IgdgNZ+F4GOU8HsEvufeexjtJs2EyZhdzRcaQGnU65aXlOMKQ== 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-SenderADCheck; bh=mr+Y29oHMUG37ys6t5rmwAklm/JM1twWDpsEXIZqD+k=; b=W80UciNKCO+XUDLI4Qb+89WnEETp+VwusTDQjGigMavkVtkQzNX3m/Ylfw41hmI5c29L1J0heTyZ+zZBKky61CLeS4HPzRR2wstKbMKG/r13HHJIqDQ8CX4caV468WgyOBARQphQWUgzjHVJ8DGh+3NvkIdYz2rb2tvXmzEl6W37iY5DkqLDY/1OQmsqe1G16usepiOh1AEWTUEz0Rich70MkqRfQUFRhH/kymv5zYkCWQ00LRtJqwTgDbWvkFToEoM20Gpf29f/K4pKx6i10SAuOAEcFxFTbyH5GQkMDBRbHYSTjjmNeBziJvA/qRvJv7Rvzlewtrs9Sytie16qSQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=arm.com; dmarc=bestguesspass action=none header.from=arm.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mr+Y29oHMUG37ys6t5rmwAklm/JM1twWDpsEXIZqD+k=; b=rO3qkuPyLl2yDlFG1FL/xm6u0tkzx8bw0E/bBIcyEMOgC0YmeNfH829MGRBLZYdVnK9X/u5lWVZHUSvZVVlOFWkB/9Rcip9CAJr8sEt8j/MDX/jewkcIUn0LBcGVEh3MAJL8VjT73f4vPhu4S2mTv9+L2HtgwIVJ6e0IxPlhsl8= Received: from DB6PR0601CA0019.eurprd06.prod.outlook.com (2603:10a6:4:7b::29) by AM0PR08MB5538.eurprd08.prod.outlook.com (2603:10a6:208:13b::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.22; Wed, 24 Jun 2020 13:35:15 +0000 Received: from DB5EUR03FT014.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:7b:cafe::3a) by DB6PR0601CA0019.outlook.office365.com (2603:10a6:4:7b::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3131.20 via Frontend Transport; Wed, 24 Jun 2020 13:35:15 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; Received: from nebula.arm.com (40.67.248.234) by DB5EUR03FT014.mail.protection.outlook.com (10.152.20.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.3131.20 via Frontend Transport; Wed, 24 Jun 2020 13:35:15 +0000 Received: from AZ-NEU-EX01.Emea.Arm.com (10.251.26.4) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.1415.2; Wed, 24 Jun 2020 13:35:08 +0000 Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX01.Emea.Arm.com (10.251.26.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1779.2; Wed, 24 Jun 2020 13:35:07 +0000 Received: from E107187.Arm.com (10.57.22.181) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.1415.2 via Frontend Transport; Wed, 24 Jun 2020 13:35:05 +0000 From: "Sami Mujawar" To: CC: Sami Mujawar , , , , , , , , , Subject: [PATCH v3 06/15] ArmVirtPkg: Add Kvmtool NOR flash lib Date: Wed, 24 Jun 2020 14:34:49 +0100 Message-ID: <20200624133458.61920-7-sami.mujawar@arm.com> X-Mailer: git-send-email 2.11.0.windows.3 In-Reply-To: <20200624133458.61920-1-sami.mujawar@arm.com> References: <20200624133458.61920-1-sami.mujawar@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:nebula.arm.com;PTR:InfoDomainNonexistent;CAT:NONE;SFTY:;SFS:(4636009)(396003)(136003)(39860400002)(376002)(346002)(46966005)(8936002)(30864003)(5660300002)(26005)(82310400002)(8676002)(70206006)(186003)(81166007)(2906002)(86362001)(1076003)(356005)(70586007)(6916009)(47076004)(36756003)(44832011)(82740400003)(2616005)(6666004)(4326008)(336012)(426003)(478600001)(83380400001)(966005)(54906003)(7696005)(316002);DIR:OUT;SFP:1101; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 99b072c2-4069-4922-478d-08d818437357 X-MS-TrafficTypeDiagnostic: AM0PR08MB5538:|DB6PR0802MB2293: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:9508;OLM:9508; X-Forefront-PRVS: 0444EB1997 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 0/0Jptk/VkDUwyCM9SKukGx8t87F4ieX1Yrjui1tfmDVq4l/9CbUBNf7u69yDNNIi4o1+Bejpd5K4R1Lbgp+oSE9af74d1aeobv2ZFAmSXH+FS5hAyTaHL5TXxTB7MnjaflDz9LLWUwH8zMsHs+RvlTGXADLldekN2ROETzVRwPYkGlXcV2Vd8VvVsA1/RIdI1sMlWyc6kXMQrpv76v9zbQG6c1X8lbSedN3oEIsTWPhR8P0dleho2PZfyOf6FcQqfy8iZyr5HlzKV55+hiCyDauEHFjPqu5/Sdune7rVEhcq2H9q4D4//tw+hZr4WM0L4w5exoxC0f/xhqaFkDojL4XtB9A8ARGDqGsAgzvW2XMU1FgR7AtYQyReMcGhqmJCtEKJDcRn3Z8pe/VgYcQmnpzq2Dcx2FxQHDM6blzPwsF8iPass87rvpBiO7lDxSfEgG4C/Kz03qDe1xJXAmtaj2R4zYCbfln56rEfNYaKUw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB5538 Return-Path: Sami.Mujawar@arm.com X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT059.eop-EUR03.prod.protection.outlook.com X-Forefront-Antispam-Report: CIP:63.35.35.123;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:64aa7808-outbound-1.mta.getcheckrecipient.com;PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com;CAT:NONE;SFTY:;SFS:(4636009)(376002)(136003)(39860400002)(346002)(396003)(46966005)(316002)(336012)(426003)(1076003)(6916009)(8936002)(44832011)(2906002)(82310400002)(30864003)(82740400003)(478600001)(7696005)(83380400001)(5660300002)(47076004)(6666004)(966005)(70586007)(86362001)(2616005)(81166007)(70206006)(54906003)(186003)(26005)(36756003)(4326008)(8676002);DIR:OUT;SFP:1101; X-MS-Office365-Filtering-Correlation-Id-Prvs: 954ef3b1-e35c-4b0c-545e-08d818436d7f X-Forefront-PRVS: 0444EB1997 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YbLCJoXtmM3W8gGMP6qOAL09L7Hh/vOdlDnVSqWfGLkWqVc6g6YbFkUi9jQBwrMWMyXDaAZ0g3ihmSKpC86ZiGyp+QAYTqjmbmw5VqNHPQJE3StHWBxFeXXUKgYcHSPjxMMmeuzYnN7U7SFahpDFDyNJJR92JOJhJGyNqpbTu3tKDGgF2TLFbaXVoskFD8DngJuJ1hWLcAdFhYnRg6pDZrnR3ZbRoeIIOOdiEbGrrIFOPF2aPJk7827ffm1bU5/MpfWUY8OU0BjGL5cKh3qTbZaVQlJ13IrLpQEeFjqjdv7jrXNFy9/iVdM0mGeDiIR3+MBbxBZVhna00NzuMZDjnzauasGyDfUKZn5jCXFxAV7Ia4xRbUeE6PW5sEFXbIYuHl93XFtMGwq8wr6tjJ4xdhxrO4KFkUtw3ge0//qKCXyoThp8M4XZ9Wt68X8H+VJyqteCecUfgdOCJk9ZhzNIUGWFJ/AplkDeccfdrIsUFs4= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2020 13:35:24.9820 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 99b072c2-4069-4922-478d-08d818437357 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0802MB2293 Content-Type: text/plain Kvmtool places the base address of the CFI flash in the device tree it passes to UEFI. This library parses the kvmtool device tree to read the CFI base address and initialise the PCDs use by the NOR flash driver and the variable storage. UEFI takes ownership of the CFI flash hardware, and exposes its functionality through the UEFI Runtime Variable Service. Therefore, disable the device tree node for the CFI flash used for storing the UEFI variables, to prevent the OS from attaching its device driver as well. Signed-off-by: Sami Mujawar Acked-by: Laszlo Ersek --- Notes: v3: - ASSERT is sufficient to test Locating [Ard] gFdtClientProtocolGuid as DEPEX ensures that this is guaranteed to succeed. - Removed additional error handling based on review [Sami] feedback. - Fix confusion caused by use of macro MAX_FLASH_BANKS. [Philippe] - Renamed MAX_FLASH_BANKS to MAX_FLASH_DEVICES. [Sami] - Use macro to define block size for flash. [Philippe] - Defined macro KVMTOOL_NOR_BLOCK_SIZE and also configured [Sami] to reflect the correct block size 64KB. - Disable the DT flash node used for UEFI variable storage [Sami] as UEFI takes ownership of the flash device. Ref: https://edk2.groups.io/g/devel/topic/74200914#60341 v2: - Library to read CFI flash base address from DT and initialise [Sami] PCDs used for NOR flash variables. ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c | 330 ++++++++++++++++++++ ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf | 49 +++ 2 files changed, 379 insertions(+) diff --git a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c new file mode 100644 index 0000000000000000000000000000000000000000..8e9dcf31691b4b12b9c7bac1ad4ba8d3a534a1d8 --- /dev/null +++ b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c @@ -0,0 +1,330 @@ +/** @file + An instance of the NorFlashPlatformLib for Kvmtool platform. + + Copyright (c) 2020, ARM Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + **/ + +#include +#include +#include +#include +#include + +/** Macro defining the NOR block size configured in Kvmtool. +*/ +#define KVMTOOL_NOR_BLOCK_SIZE SIZE_64KB + +/** Macro defining the maximum number of Flash devices. +*/ +#define MAX_FLASH_DEVICES 4 + +/** Macro defining the cfi-flash label describing the UEFI variable store. +*/ +#define LABEL_UEFI_VAR_STORE "System-firmware" + +STATIC NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_DEVICES]; +STATIC UINTN mNorFlashDeviceCount = 0; +STATIC INT32 mUefiVarStoreNode = MAX_INT32; +STATIC FDT_CLIENT_PROTOCOL *mFdtClient; + +/** This function performs platform specific actions to initialise + the NOR flash, if required. + + @retval EFI_SUCCESS Success. +**/ +EFI_STATUS +NorFlashPlatformInitialization ( + VOID + ) +{ + EFI_STATUS Status; + + DEBUG ((DEBUG_INFO, "NorFlashPlatformInitialization\n")); + + if ((mNorFlashDeviceCount > 0) && (mUefiVarStoreNode != MAX_INT32)) { + // + // UEFI takes ownership of the cfi-flash hardware, and exposes its + // functionality through the UEFI Runtime Variable Service. This means we + // need to disable it in the device tree to prevent the OS from attaching + // its device driver as well. + // Note: This library is loaded by the FaultTolerantWriteDxe to setup the + // Ftw PCDs and later by the NorFlashDxe to provide the NorFlashPlatformLib + // interfaces. Therefore the FDT node used for UEFI storage variable is + // disabled here. + // + Status = mFdtClient->SetNodeProperty ( + mFdtClient, + mUefiVarStoreNode, + "status", + "disabled", + sizeof ("disabled") + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_WARN, "Failed to set cfi-flash status to 'disabled'\n")); + } + } else { + Status = EFI_NOT_FOUND; + DEBUG ((DEBUG_ERROR, "Flash device for UEFI variable storage not found\n")); + } + + return Status; +} + +/** Initialise Non volatile Flash storage variables. + + @param [in] FlashDevice Pointer to the NOR Flash device. + + @retval EFI_SUCCESS Success. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Insufficient flash storage space. +**/ +EFI_STATUS +SetupVariableStore ( + IN NOR_FLASH_DESCRIPTION * FlashDevice + ) +{ + UINTN FlashRegion; + UINTN FlashNvStorageVariableBase; + UINTN FlashNvStorageFtwWorkingBase; + UINTN FlashNvStorageFtwSpareBase; + UINTN FlashNvStorageVariableSize; + UINTN FlashNvStorageFtwWorkingSize; + UINTN FlashNvStorageFtwSpareSize; + + FlashNvStorageVariableSize = PcdGet32 (PcdFlashNvStorageVariableSize); + FlashNvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize); + FlashNvStorageFtwSpareSize = PcdGet32 (PcdFlashNvStorageFtwSpareSize); + + if ((FlashNvStorageVariableSize == 0) || + (FlashNvStorageFtwWorkingSize == 0) || + (FlashNvStorageFtwSpareSize == 0)) { + DEBUG ((DEBUG_ERROR, "FlashNvStorage size not defined\n")); + return EFI_INVALID_PARAMETER; + } + + // Setup the variable store + FlashRegion = FlashDevice->DeviceBaseAddress; + + FlashNvStorageVariableBase = FlashRegion; + FlashRegion += PcdGet32 (PcdFlashNvStorageVariableSize); + + FlashNvStorageFtwWorkingBase = FlashRegion; + FlashRegion += PcdGet32 (PcdFlashNvStorageFtwWorkingSize); + + FlashNvStorageFtwSpareBase = FlashRegion; + FlashRegion += PcdGet32 (PcdFlashNvStorageFtwSpareSize); + + if (FlashRegion > (FlashDevice->DeviceBaseAddress + FlashDevice->Size)) { + DEBUG ((DEBUG_ERROR, "Insufficient flash storage size\n")); + return EFI_OUT_OF_RESOURCES; + } + + PcdSet32S ( + PcdFlashNvStorageVariableBase, + FlashNvStorageVariableBase + ); + + PcdSet32S ( + PcdFlashNvStorageFtwWorkingBase, + FlashNvStorageFtwWorkingBase + ); + + PcdSet32S ( + PcdFlashNvStorageFtwSpareBase, + FlashNvStorageFtwSpareBase + ); + + DEBUG (( + DEBUG_INFO, + "PcdFlashNvStorageVariableBase = 0x%x\n", + FlashNvStorageVariableBase + )); + DEBUG (( + DEBUG_INFO, + "PcdFlashNvStorageVariableSize = 0x%x\n", + FlashNvStorageVariableSize + )); + DEBUG (( + DEBUG_INFO, + "PcdFlashNvStorageFtwWorkingBase = 0x%x\n", + FlashNvStorageFtwWorkingBase + )); + DEBUG (( + DEBUG_INFO, + "PcdFlashNvStorageFtwWorkingSize = 0x%x\n", + FlashNvStorageFtwWorkingSize + )); + DEBUG (( + DEBUG_INFO, + "PcdFlashNvStorageFtwSpareBase = 0x%x\n", + FlashNvStorageFtwSpareBase + )); + DEBUG (( + DEBUG_INFO, + "PcdFlashNvStorageFtwSpareSize = 0x%x\n", + FlashNvStorageFtwSpareSize + )); + + return EFI_SUCCESS; +} + +/** Return the Flash devices on the platform. + + @param [out] NorFlashDescriptions Pointer to the Flash device description. + @param [out] Count Number of Flash devices. + + @retval EFI_SUCCESS Success. + @retval EFI_NOT_FOUND Flash device not found. +**/ +EFI_STATUS +NorFlashPlatformGetDevices ( + OUT NOR_FLASH_DESCRIPTION **NorFlashDescriptions, + OUT UINT32 *Count + ) +{ + if (mNorFlashDeviceCount > 0) { + *NorFlashDescriptions = mNorFlashDevices; + *Count = mNorFlashDeviceCount; + return EFI_SUCCESS; + } + return EFI_NOT_FOUND; +} + +/** Entrypoint for NorFlashPlatformLib. + + @param [in] ImageHandle The handle to the image. + @param [in] SystemTable Pointer to the System Table. + + @retval EFI_SUCCESS Success. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + @retval EFI_NOT_FOUND Flash device not found. +**/ +EFI_STATUS +EFIAPI +NorFlashPlatformLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE * SystemTable + ) +{ + INT32 Node; + EFI_STATUS Status; + EFI_STATUS FindNodeStatus; + CONST UINT32 *Reg; + UINT32 PropSize; + UINT64 Base; + UINT64 Size; + UINTN UefiVarStoreIndex; + CONST CHAR8 *Label; + UINT32 LabelLen; + + if (mNorFlashDeviceCount != 0) { + return EFI_SUCCESS; + } + + Status = gBS->LocateProtocol ( + &gFdtClientProtocolGuid, + NULL, + (VOID **)&mFdtClient + ); + ASSERT_EFI_ERROR (Status); + + UefiVarStoreIndex = MAX_UINTN; + for (FindNodeStatus = mFdtClient->FindCompatibleNode ( + mFdtClient, + "cfi-flash", + &Node + ); + !EFI_ERROR (FindNodeStatus) && + (mNorFlashDeviceCount < MAX_FLASH_DEVICES); + FindNodeStatus = mFdtClient->FindNextCompatibleNode ( + mFdtClient, + "cfi-flash", + Node, + &Node + )) { + Status = mFdtClient->GetNodeProperty ( + mFdtClient, + Node, + "label", + (CONST VOID **)&Label, + &LabelLen + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "%a: GetNodeProperty ('label') failed (Status == %r)\n", + __FUNCTION__, + Status + )); + } else if (AsciiStrCmp (Label, LABEL_UEFI_VAR_STORE) == 0) { + UefiVarStoreIndex = mNorFlashDeviceCount; + mUefiVarStoreNode = Node; + } + + Status = mFdtClient->GetNodeProperty ( + mFdtClient, + Node, + "reg", + (CONST VOID **)&Reg, + &PropSize + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: GetNodeProperty () failed (Status == %r)\n", + __FUNCTION__, Status)); + continue; + } + + ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0); + + while ((PropSize >= (4 * sizeof (UINT32))) && + (mNorFlashDeviceCount < MAX_FLASH_DEVICES)) { + Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0])); + Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2])); + Reg += 4; + + PropSize -= 4 * sizeof (UINT32); + + // + // Disregard any flash devices that overlap with the primary FV. + // The firmware is not updatable from inside the guest anyway. + // + if ((PcdGet64 (PcdFvBaseAddress) + PcdGet32 (PcdFvSize) > Base) && + (Base + Size) > PcdGet64 (PcdFvBaseAddress)) { + continue; + } + + DEBUG (( + DEBUG_INFO, + "NOR%d : Base = 0x%lx, Size = 0x%lx\n", + mNorFlashDeviceCount, + Base, + Size + )); + + mNorFlashDevices[mNorFlashDeviceCount].DeviceBaseAddress = (UINTN)Base; + mNorFlashDevices[mNorFlashDeviceCount].RegionBaseAddress = (UINTN)Base; + mNorFlashDevices[mNorFlashDeviceCount].Size = (UINTN)Size; + mNorFlashDevices[mNorFlashDeviceCount].BlockSize = KVMTOOL_NOR_BLOCK_SIZE; + mNorFlashDeviceCount++; + } + } // for + + // Setup the variable store in the last device + if (mNorFlashDeviceCount > 0) { + if (UefiVarStoreIndex == MAX_UINTN) { + // We did not find a label matching the UEFI Variable store. Default to + // using the last cfi-flash device as the variable store. + UefiVarStoreIndex = mNorFlashDeviceCount - 1; + mUefiVarStoreNode = Node; + } + if (mNorFlashDevices[UefiVarStoreIndex].DeviceBaseAddress != 0) { + return SetupVariableStore (&mNorFlashDevices[UefiVarStoreIndex]); + } + } + + return EFI_NOT_FOUND; +} + diff --git a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf new file mode 100644 index 0000000000000000000000000000000000000000..a3230e4b2be668904322103825b93e867503984e --- /dev/null +++ b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf @@ -0,0 +1,49 @@ +#/** @file +# +# Copyright (c) 2020, ARM Ltd. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +#**/ + +[Defines] + INF_VERSION = 0x0001001B + BASE_NAME = NorFlashKvmtoolLib + FILE_GUID = E75F07A1-B160-4893-BDD4-09E32FF847DC + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = NorFlashPlatformLib + CONSTRUCTOR = NorFlashPlatformLibConstructor + +[Sources.common] + NorFlashKvmtool.c + +[Packages] + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + ArmVirtPkg/ArmVirtPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + PcdLib + UefiBootServicesTableLib + +[Protocols] + gFdtClientProtocolGuid ## CONSUMES + +[Pcd] + gArmTokenSpaceGuid.PcdFvBaseAddress + gArmTokenSpaceGuid.PcdFvSize + + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize + +[Depex] + gFdtClientProtocolGuid + -- 'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'