From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from EUR01-HE1-obe.outbound.protection.outlook.com (EUR01-HE1-obe.outbound.protection.outlook.com [40.107.13.50]) by mx.groups.io with SMTP id smtpd.web11.5747.1601673278880268090 for ; Fri, 02 Oct 2020 14:14:39 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@armh.onmicrosoft.com header.s=selector2-armh-onmicrosoft-com header.b=JmPu1VAk; spf=pass (domain: arm.com, ip: 40.107.13.50, 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=oH1uPeKwL8/ssWRCBOglGvX3hm7pWs//XxiIffAHHGY=; b=JmPu1VAk1xi6U+DVEBXPWnhLRM+9yyPl/sw+zJzLRSfBH9hFt6XZYRTqy7yA6HZCGXs51n3MSZuSL0mnPWowB4fzNS1P4KN29MSLYb2zuWr8ODE4UY138+E8D3ahUFRKw8GEptoCL6GAzULDAe2KTdflf5XR700JZvHn8opEBKM= Received: from DB6PR0301CA0087.eurprd03.prod.outlook.com (2603:10a6:6:30::34) by VI1PR08MB4285.eurprd08.prod.outlook.com (2603:10a6:803:f8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3433.35; Fri, 2 Oct 2020 21:14:33 +0000 Received: from DB5EUR03FT057.eop-EUR03.prod.protection.outlook.com (2603:10a6:6:30:cafe::1e) by DB6PR0301CA0087.outlook.office365.com (2603:10a6:6:30::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3433.36 via Frontend Transport; Fri, 2 Oct 2020 21:14:33 +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=pass 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 DB5EUR03FT057.mail.protection.outlook.com (10.152.20.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3433.34 via Frontend Transport; Fri, 2 Oct 2020 21:14:32 +0000 Received: ("Tessian outbound e8cdb8c6f386:v64"); Fri, 02 Oct 2020 21:14:31 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: ddec25d1a734a1bd X-CR-MTA-TID: 64aa7808 Received: from 9959e02feae2.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 46A191D8-1CC0-44B4-B1F3-437726B8BBE7.1; Fri, 02 Oct 2020 21:14:26 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 9959e02feae2.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 02 Oct 2020 21:14:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FcIR6si+hnSiY+sqrcl5g68Gu6vBE9Dva9HsCN38vtjgTYgsP6nM/xvQkJD5G/kEusdZ++z5Xl8zZioajWYpEJQaACvquK0hgeoHdCJkbGTr0AJAm0zwUl9GkComBHhtfzDfIh1Hg85Thsv/vGsr/IFwrCBdJI2Tznv2/qp4MbZCdHHF9ZAp4CKWSQxW1JR2gT+OADugXGyVBlUmHTzF6r7hdKBOR6sHEdzgluqVrltBFyiUG2YEGZahZaHe8Qv1Lqph8RIcaeuI+xTGletw4ae52VLNc/Qna74SQ7nS2Nsfx3rKHuYno+s2xtLaNN9zUY/rTVNGHKOnDmacCPfpXA== 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=oH1uPeKwL8/ssWRCBOglGvX3hm7pWs//XxiIffAHHGY=; b=Qaba2rQONNaygYW3h1FdDoZbVIbNJx+8CsUfdcKtzkxIQMkUvNwOLvwtWalAo9tXjisGgQpesTCLqoIetyR6QDDQ0Sxv9zTPTXMLHYIGijP5K0lUu4Df5SFb29FfVPmIsz6Mx/JTy551ivEXDlwZOu3vGAM5RDrWQ+re/n+qf6qdzMR5cvdPzVxA803TipKeYN/bhv+wLAsy8asCTwkBUd6Ag7VGdhsAJBUsuRvjasuupLQKdukLk0UnRkKnb/Jjew2TBeFGbOUxrUhAorW4qtMSUcyd/iQGSQm2Oq0Epb/f4EV5zmL4fKWUcJsbemAaFS+5GfTuOlLQfBgLkasH8g== 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=pass (p=none sp=none pct=100) 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=oH1uPeKwL8/ssWRCBOglGvX3hm7pWs//XxiIffAHHGY=; b=JmPu1VAk1xi6U+DVEBXPWnhLRM+9yyPl/sw+zJzLRSfBH9hFt6XZYRTqy7yA6HZCGXs51n3MSZuSL0mnPWowB4fzNS1P4KN29MSLYb2zuWr8ODE4UY138+E8D3ahUFRKw8GEptoCL6GAzULDAe2KTdflf5XR700JZvHn8opEBKM= Received: from MR2P264CA0093.FRAP264.PROD.OUTLOOK.COM (2603:10a6:500:32::33) by VE1PR08MB5119.eurprd08.prod.outlook.com (2603:10a6:803:114::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3433.35; Fri, 2 Oct 2020 21:14:24 +0000 Received: from VE1EUR03FT046.eop-EUR03.prod.protection.outlook.com (2603:10a6:500:32:cafe::7d) by MR2P264CA0093.outlook.office365.com (2603:10a6:500:32::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3433.35 via Frontend Transport; Fri, 2 Oct 2020 21:14:23 +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=pass 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 VE1EUR03FT046.mail.protection.outlook.com (10.152.19.226) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.3433.34 via Frontend Transport; Fri, 2 Oct 2020 21:14:23 +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.2044.4; Fri, 2 Oct 2020 21:14:17 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) 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; Fri, 2 Oct 2020 21:14:16 +0000 Received: from E107187.Arm.com (10.57.53.44) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server id 15.1.2044.4 via Frontend Transport; Fri, 2 Oct 2020 21:14:15 +0000 From: "Sami Mujawar" To: CC: Sami Mujawar , , , , , , , , , Subject: [PATCH v5 06/15] ArmVirtPkg: Add Kvmtool NOR flash lib Date: Fri, 2 Oct 2020 22:14:00 +0100 Message-ID: <20201002211409.43888-7-sami.mujawar@arm.com> X-Mailer: git-send-email 2.11.0.windows.3 In-Reply-To: <20201002211409.43888-1-sami.mujawar@arm.com> References: <20201002211409.43888-1-sami.mujawar@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-Office365-Filtering-HT: Tenant X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: af3a1fce-e8b0-44cd-9aee-08d867182879 X-MS-TrafficTypeDiagnostic: VE1PR08MB5119:|VI1PR08MB4285: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:10000;OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: IL6LqIO3rE54DW+w7aMd96x0waAh345xgN/ywG0INXuxdR7zijnfSM8HNwGy+Q99Jz9+R2s26Hcrx7HQOvhPujgtxR+vXKDuQLeb39RgbQV7aKdxPSzJtKBzj0s6baH694k8q1GO95yBe7BWY/Z7q04gpbgtWSu05HXzY2QmUsmNTnb+EQwquUH4vPWO/+lFkMPe0c/N2WdYJxDKmdOo6murq2f0rOlbP71llS4OWZo1yEo8QqpvTGsAVmwMJCuf9hjb190+R9S0N7oNKnDSfCA4uycGpzQG2TAzR3kHqwfE6uaj4FlYSA2rtpMgQHy0akDcIliF4QwIfO2SOS3tw/9vPl149nIQAo36YETqrmfkc9VAbgLoKvuORJekNaAXKt9/X9K+OIansjgW3+qKHP3MtEDkFY6vT9FjULE4/dD91D6rLu8yiJz4djqeJXHG6ObzD/8bNstuTB9K6IfgvHjiXAz+VbM0egjKJA0v3Jw= 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;SFS:(4636009)(396003)(39850400004)(136003)(346002)(376002)(46966005)(356005)(26005)(54906003)(36756003)(186003)(83380400001)(2616005)(44832011)(7696005)(966005)(5660300002)(426003)(478600001)(30864003)(82310400003)(81166007)(70206006)(336012)(2906002)(8936002)(6916009)(70586007)(1076003)(47076004)(316002)(8676002)(82740400003)(6666004)(86362001)(4326008)(83080400001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1PR08MB5119 Return-Path: Sami.Mujawar@arm.com X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT057.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 60dacbb1-2476-4977-e91e-08d8671822ef X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rZNP8AALWytyTp1wh8WoHb2K3AK893lz6vlo22O7M54k7VyZbZirh7sTdL+qTUtBdcZLsJAWAA3LqWEcDyYjJX35u3RtNyU4+vTNNEJZBSKb30pozOJNfr4h+4wrmEAE+ARuB3vCSQhKs3QCGVpWcjZSlad6+wlh05SAOS3LE/At7Izv0DxfK54uric4mlccQcZez0QKki5bKuT45KI3lH4UqgBTOuAAOv7j+We1Z/Yr0XtUO7180jFmqJthSYt8O1teN0nLtly/ug7vaaQVoHRU4IL/QmmSBpOCLiri15JfKhGbtmQ55J3zZzmqYgjKRLjMPj3QBiMj7Ye5TrrgO9P89U2qCS2L/J1j8QOo62ejXdDyYzUuv6yzbFLi5ipZkmBRsGoxmkLl1W952JuKXgzO+Ez9jHb5hZl8QSNjLZofIAXre35pzlwOSEfYPO6rAUjDO8wDxJfXId4RRavOOOHLJHihBcXDrOVj6wgMO84= 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;SFS:(4636009)(39860400002)(136003)(396003)(376002)(346002)(46966005)(83380400001)(2906002)(5660300002)(6666004)(1076003)(30864003)(47076004)(26005)(966005)(2616005)(70586007)(70206006)(478600001)(81166007)(82740400003)(7696005)(4326008)(426003)(6916009)(186003)(36756003)(8936002)(8676002)(316002)(44832011)(54906003)(82310400003)(336012)(86362001)(83080400001);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Oct 2020 21:14:32.8739 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: af3a1fce-e8b0-44cd-9aee-08d867182879 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-AuthSource: DB5EUR03FT057.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB4285 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 Reviewed-by: Ard Biesheuvel --- Notes: v5: - Fixed minor ECC reported issues in file header. [Sami] Ref: https://edk2.groups.io/g/devel/message/62160 v4: - Added STATIC to local functions and updated comments [Sami] to explain why DT node deferred to be disabled in NorFlashPlatformInitialization() - Use STATIC for local functions and explain why DT [Ard] node for flash is disabled in NorFlashPlatformInitialization()? Ref: https://edk2.groups.io/g/devel/topic/75081477 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 | 336 ++++++++++++++++++++ ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf | 50 +++ 2 files changed, 386 insertions(+) diff --git a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c new file mode 100644 index 0000000000000000000000000000000000000000..dce585b779a87dabde280cf4bbca5b828ceccda4 --- /dev/null +++ b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c @@ -0,0 +1,336 @@ +/** @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 twice. First by FaultTolerantWriteDxe to + // setup the PcdFlashNvStorageFtw* and later by NorFlashDxe to provide the + // NorFlashPlatformLib interfaces. If the node is disabled when the library + // is first loaded, then during the subsequent loading of the library the + // call to FindNextCompatibleNode() from the library constructor skips the + // FDT node used for UEFI storage variable. Due to this we cannot setup the + // NOR flash device description i.e. mNorFlashDevices[]. + // Since NorFlashPlatformInitialization() is called only by NorFlashDxe, + // we know it is safe to disable the node 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. +**/ +STATIC +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..b811e99156e2d55a95b4941e927a1006eb18a610 --- /dev/null +++ b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf @@ -0,0 +1,50 @@ +## @file +# Nor Flash library for Kvmtool. +# +# 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)'