From nobody Mon Sep 16 19:41:32 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+114501+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 1706248942290649.4271512662735; Thu, 25 Jan 2024 22:02:22 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=eIV/Tbf1WKuEU362/b+nYYyznWio+Pa+0ZeNN8af5zo=; 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=1706248942; v=1; b=ObFdshfPlZuZrJLBV5vCP8wDaOwGZe1OmSziPTAiZGnrdsFfBbRZPd8ZXBDUiWpnWW8VyV30 yJEIdPrKqNMYaVgBOr1uCJ63LA+xX1rCUYoNMEDwr2LaCT5B14odn3zTW+RjpKUZLUFI1tjZO8V aYfzANl+1vvJI1Aj2xdxM1OQ= X-Received: by 127.0.0.2 with SMTP id GKvbYY1788612xv7gkA0mMiY; Thu, 25 Jan 2024 22:02:21 -0800 X-Received: from NAM04-DM6-obe.outbound.protection.outlook.com (NAM04-DM6-obe.outbound.protection.outlook.com [40.107.102.87]) by mx.groups.io with SMTP id smtpd.web11.9680.1706248941306689447 for ; Thu, 25 Jan 2024 22:02:21 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SDvmsNRXVR4ZJd8qXEE+mMxeVi+tlwEv/vYuKuSsbpJmrjo8GOYk3HEUcQQKm+qAG3d0qLwzdla5J8fc0uJln8fmj0Fo7YebQMPpKf5Dt03JH0q8DJL3TCA6fj++ET/8Ivjqvz/e4hlkSwgTkS0jmEuneXu1I4uMv9njFxHvQmUAMAjFQRtFfp1skNio2GXXjVtPVs5neGlpwBYSBJGdVaMBAD8J7jgrcvFSOUch8tLs53lEtEO6PLQLBZ7Rl6UXNuSdywFa5DlNdejazbh7zrKUkmZKXoBTUzWZEdVrv0HmQtswo5xbI/UAKg8bKDVfo9rtAlKHeXdIaA/LJb7yew== 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=w+RIyfmc9obaQB6GUhOPIRGjYlC9Zk9xIscG/D33qrk=; b=O3uZa3tMhIfWfftKGXjBgRGg35G8xogtuNEg/44Ma0tdujArSAfvagsoX9xXia0fpOPgXFWN6HNnCM4p2g11t5uaUPffwiZaoHsH62Hnpd1NqDp1xTA6x7DBepa4nKbcveYP5NpVV3ckCog+ahlL8a/MQDHhvGe5YU9aD9pTeHGez9LIcDNNZCBIy7U8DC0rUAH3IdX72JZiqIqsqJ9FT3d0Ka6uCVLJvynGkvrregsMtE7NuL+7rSpJmO2yt2EpP2ZEakANcbHz9ESeXkWNXMqjSuBA/UE2RUou0iVMH1SpX6jU5ED/0aQi42t35QjuHnZZBgOKw3vjooOHybgE1w== 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 DS7PR03CA0343.namprd03.prod.outlook.com (2603:10b6:8:55::17) by DM6PR12MB4862.namprd12.prod.outlook.com (2603:10b6:5:1b7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.27; Fri, 26 Jan 2024 06:02:16 +0000 X-Received: from DS2PEPF00003440.namprd02.prod.outlook.com (2603:10b6:8:55:cafe::b0) by DS7PR03CA0343.outlook.office365.com (2603:10b6:8:55::17) 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 06:02:16 +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+114501+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 DS2PEPF00003440.mail.protection.outlook.com (10.167.18.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 06:02:16 +0000 X-Received: from SHA-LX-MINGXZHA.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 26 Jan 2024 00:02:14 -0600 From: "Zhai, MingXin (Duke) via groups.io" To: CC: Eric Xing , Ken Yao , Igniculus Fu , Abner Chang Subject: [edk2-devel] [PATCH V2 08/32] AMD/VanGoghBoard: Check in UDKFlashUpdate Date: Fri, 26 Jan 2024 14:00:26 +0800 Message-ID: <20240126060050.1725-9-duke.zhai@amd.com> In-Reply-To: <20240126060050.1725-1-duke.zhai@amd.com> References: <20240126060050.1725-1-duke.zhai@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF00003440:EE_|DM6PR12MB4862:EE_ X-MS-Office365-Filtering-Correlation-Id: a67c3a97-c1fd-451f-4d51-08dc1e345947 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: oHG0NIDwmzgDTRrQLFm5CPf7gIyG7SCW6oO5Tq82Rt4gtmbF925BeF83X9vwxf1ddS6Zc+SshKeMaVodTcyhR6M9k3mGYVbU0tnFNiy9wXpi4I585cgF72KwcGngz6PjAve7zk1PALtOyHbmdymyiXZxdkgsCsan9nK854J9FrHsoxhjqEbMUlKLQp7rXPTPTfdzBrznhc6qcYPWUigIxKwaYNqaAT3p/bU6IhoEJVgudo9XuEiKN+uOrKsl0V8VB7O/Usym6pPCqDxifJZQ0nvRkv3X45ozev4ylmbbURa0DuQwIesRXT7pqrWQGAfRjDJo9NS4qdAqfa1HcFxz/YcMJ6jYcOdnsdpRlDk1l0iwhUwcUH0uJgFhOPaDum7uwRM0SL3mxqxKbDniutRF4OPQkUGiwV5LIRhCF7kFlyPpb6GE5H99zfZiGZRDuAfhcjq3QM5i5++GV8lEyioMm9yFlto/H0ir5AQvhldpE2zbLWnMCShdZ9f+gENhByOqOQxH7nwWausZJRzw+ZRmgLqvW8/6Gq/gO/PR2/8bN+Zgc369rPgs40V2O/tKKP5+EQpBx1QUeNHYS2/FBclHU/AiCg/5hyLdf0YbdH0g7jxrMcxeiB0UsvPMOlNJuEYI/tZTWRfBaGGYHtIsdIHe8VK9plV9162rv013AKBq0acUsdAdGvbaoRfWoqdYEqwuvGwUA8YKM1gV2Mir/p2b/+rMBdZ2OV/JtkAXr5R0f/k8WgRT2ehatWBj6PXNfEy77cIJF7BAjx1ojGLn2F5wNA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 06:02:16.3964 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a67c3a97-c1fd-451f-4d51-08dc1e345947 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: DS2PEPF00003440.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4862 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: 5BNJxSrnmy4fVfsPWWfT5N3ix1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1706248943140100004 Content-Type: text/plain; charset="utf-8" From: Duke Zhai BZ #:4640 In V2: Improve coding style. 1.Remove the leading underscore and use double underscore at trailing in = C header files. 2.Remove old tianocore licenses and redundant license description. 3.Improve coding style. For example: remove space between @param. In V1: UDKFlashUpdate is a uefi tool for BIOS binary updating. It depends on EDK= 2's flash access protocol. UDKFlashUpdate needs to run under EDK2 BIOS. Signed-off-by: Duke Zhai Cc: Eric Xing Cc: Ken Yao Cc: Igniculus Fu Cc: Abner Chang --- .../UDKFlashUpdate/SpiFlashDevice.c | 37 + .../UDKFlashUpdate/SpiFlashDevice.h | 62 ++ .../UDKFlashUpdate/UDKFlashUpdate.c | 671 ++++++++++++++++++ .../UDKFlashUpdate/UDKFlashUpdate.h | 48 ++ .../UDKFlashUpdate/UDKFlashUpdate.inf | 51 ++ 5 files changed, 869 insertions(+) create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/= UDKFlashUpdate/SpiFlashDevice.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/= UDKFlashUpdate/SpiFlashDevice.h create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/= UDKFlashUpdate/UDKFlashUpdate.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/= UDKFlashUpdate/UDKFlashUpdate.h create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/= UDKFlashUpdate/UDKFlashUpdate.inf diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlas= hUpdate/SpiFlashDevice.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Appli= cation/UDKFlashUpdate/SpiFlashDevice.c new file mode 100644 index 0000000000..d4f5b12f41 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlashUpdate= /SpiFlashDevice.c @@ -0,0 +1,37 @@ +/** @file + Implements SpiFlashDevice.c + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "SpiFlashDevice.h" + +SPI_INIT_TABLE mSpiInitTable[] =3D { + { // W25Q256JW/W74M25JW + SF_VENDOR_ID_WINBOND, + SF_DEVICE_ID0_W25Q256JW, + SF_DEVICE_ID1_W25Q256JW, + { + SPI_COMMAND_WRITE_ENABLE, + SPI_COMMAND_WRITE_S_EN + }, + { + { EnumSpiOpcodeReadNoAddr,SPI_COMMAND_JEDEC_ID, EnumSpiOper= ationJedecId }, + { EnumSpiOpcodeWriteNoAddr,SPI_COMMAND_WRITE_S, EnumSpiOpe= rationWriteStatus }, + { EnumSpiOpcodeWrite, SPI_COMMAND_WRITE, EnumSpiOpera= tionProgramData_1_Byte }, + { EnumSpiOpcodeRead, SPI_COMMAND_READ, EnumSpiOpera= tionReadData }, + { EnumSpiOpcodeWrite, SPI_COMMAND_ERASE, EnumSpiOpera= tionErase_4K_Byte }, + { EnumSpiOpcodeReadNoAddr,SPI_COMMAND_READ_S, EnumSpiOper= ationReadStatus }, + { EnumSpiOpcodeWriteNoAddr,SPI_COMMAND_CHIP_ERASE, EnumSpiOpe= rationFullChipErase }, + { EnumSpiOpcodeRead, SPI_COMMAND_READ_SFDP, EnumSpiOpera= tionReadData }, + { EnumSpiOpcodeWriteNoAddr,SPI_COMMAND_RPMC_OP1, EnumSpiOpe= rationOther }, + { EnumSpiOpcodeReadNoAddr,SPI_COMMAND_RPMC_OP2, EnumSpiOper= ationReadData }, + { EnumSpiOpcodeReadNoAddr,SPI_COMMAND_Enter_4Byte_Addr, EnumSpiOper= ationOther }, + { EnumSpiOpcodeReadNoAddr,SPI_COMMAND_Exit_4Byte_Addr, EnumSpiOper= ationOther } + }, + 0, + 0x2000000 // BIOS image size in flash + } +}; diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlas= hUpdate/SpiFlashDevice.h b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Appli= cation/UDKFlashUpdate/SpiFlashDevice.h new file mode 100644 index 0000000000..fe4d99e82c --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlashUpdate= /SpiFlashDevice.h @@ -0,0 +1,62 @@ +/** @file + Implements SpiFlashDevice.h + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef SPI_FLASH_DEVICE_H__ +#define SPI_FLASH_DEVICE_H__ + +#include +#include +#include + +// +// Supported SPI Flash Devices +// +typedef enum { + EnumSpiFlashW25Q256JW, + EnumSpiFlashMax +} SPI_FLASH_TYPES_SUPPORTED; + +// Flash Device commands +// +// If a supported device uses a command different from the list below, a d= evice specific command +// will be defined just below it's JEDEC id section. +// +#define SPI_COMMAND_WRITE 0x02 +#define SPI_COMMAND_WRITE_AAI 0xAD +#define SPI_COMMAND_READ 0x03 +#define SPI_COMMAND_ERASE 0x20 +#define SPI_COMMAND_WRITE_DISABLE 0x04 +#define SPI_COMMAND_READ_S 0x05 +#define SPI_COMMAND_WRITE_ENABLE 0x06 +#define SPI_COMMAND_READ_ID 0xAB +#define SPI_COMMAND_JEDEC_ID 0x9F +#define SPI_COMMAND_WRITE_S_EN 0x50 +#define SPI_COMMAND_WRITE_S 0x01 +#define SPI_COMMAND_CHIP_ERASE 0xC7 +#define SPI_COMMAND_BLOCK_ERASE 0xD8 +#define SPI_COMMAND_READ_SFDP 0x5A +#define SPI_COMMAND_RPMC_OP1 0x9B +#define SPI_COMMAND_RPMC_OP2 0x96 +#define SPI_COMMAND_Enter_4Byte_Addr 0xB7 +#define SPI_COMMAND_Exit_4Byte_Addr 0xE9 + +// +// Winbond 256Mbit parts +// +#define SF_VENDOR_ID_WINBOND 0xEF +#define SF_DEVICE_ID1_W25Q256JW 0x19 // Capacity 256Mbit +#define SF_DEVICE_ID0_W25Q256JW 0x60 + +// +// index for prefix opcodes +// +#define SPI_WREN_INDEX 0 // Prefix Opcode 0: SPI_COMM= AND_WRITE_ENABLE +#define SPI_EWSR_INDEX 1 // Prefix Opcode 1: SPI_COMM= AND_WRITE_S_EN +#define BIOS_CTRL 0xDC + +#endif diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlas= hUpdate/UDKFlashUpdate.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Appli= cation/UDKFlashUpdate/UDKFlashUpdate.c new file mode 100644 index 0000000000..0497509d03 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlashUpdate= /UDKFlashUpdate.c @@ -0,0 +1,671 @@ +/** @file + Platform Flash Access library. + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "UDKFlashUpdate.h" + +EFI_SPI_PROTOCOL *mSpiProtocol =3D NULL; +UINT32 mFlashAreaBaseAddress; +UINTN mBiosSize; +UINTN mBlockSize; + +/** + Input the BeginTimeValue and EndTimeValue, return the spent time(seconds= ). + + @param[in] BeginTimeValue The begin time value read by AsmReadTsc(). + @param[in] EndTimeValue The end time value read by AsmReadTsc(). + + @retval -1 An error occurred. + @retval other The seconds value. + +**/ +STATIC +INT64 +EFIAPI +GetSpentTime ( + IN UINT64 EndTimeValue, + IN UINT64 BeginTimeValue + ) +{ + if (EndTimeValue >=3D BeginTimeValue) { + return (DivU64x32 (GetTimeInNanoSecond (EndTimeValue - BeginTimeValue)= , 1000000000)); + } else { + Print (L"!!!ERROR: Wrong time\n"); + return (-1); + } +} + +/** + Read 'ReadAddress|NumBytes' of the flash chip, and saved into 'ReadFlash= .bin' + + @param[in] ReadAddress Read address base in flash chip. + @param[in] NumBytes Read number of bytes. + + @retval 0 Flash read exited normally. + @retval Other An error occurred. + +**/ +UINTN +EFIAPI +FlashFdRead ( + IN UINTN ReadAddress, + IN UINTN NumBytes, + IN CHAR16 *FileName + ) +{ + EFI_STATUS Status; + VOID *Buffer; + SHELL_FILE_HANDLE FileHandle; + + Print (L"\nRead flash chip and saved into %s ...\n", FileName); + + Buffer =3D AllocateZeroPool (NumBytes); + if (NULL =3D=3D Buffer) { + Print (L"!!!ERROR: Allocate pool fail ...\n"); + return (1); + } + + Status =3D ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_REA= D|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Open file %s %r\n", FileName, Status); + FreePool (Buffer); + return (1); + } + + CopyMem ((UINT8 *)Buffer, (UINT8 *)(ReadAddress + mFlashAreaBaseAddress)= , NumBytes); + Status =3D ShellWriteFile (FileHandle, &NumBytes, Buffer); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Write file %s %r\n", FileName, Status); + FreePool (Buffer); + ShellCloseFile (&FileHandle); + return (1); + } + + FreePool (Buffer); + ShellCloseFile (&FileHandle); + + return (0); +} + +/** + Erase 'EraseAddress|NumBytes' in flash chip, and skip the block all '0xF= F'. + + @param[in] EraseAddress Erase address base. + @param[in] NumBytes Erase number of bytes. + + @retval 0 Flas= h erase exited normally. + @retval Other An err= or occurred. + +**/ +UINTN +EFIAPI +FlashFd64KErase ( + IN UINTN EraseAddress, + IN UINTN NumBytes + ) +{ + EFI_STATUS Status; + // UINTN Index; + UINT8 *Buffer; + + Print (L"\nErase flash chip "); + + Buffer =3D AllocateZeroPool (NumBytes); + if (NULL =3D=3D Buffer) { + Print (L"!!!ERROR: Allocate fail ...\n"); + return (1); + } + + CopyMem (Buffer, (UINT8 *)(EraseAddress + mFlashAreaBaseAddress), NumByt= es); + + for ( ; EraseAddress < NumBytes; EraseAddress +=3D mBlockSize) { + Status =3D mSpiProtocol->Execute ( + mSpiProtocol, + SPI_OPCODE_ERASE_INDEX, // Opcod= eIndex + 0, // Prefi= xOpcodeIndex + FALSE, // DataC= ycle + TRUE, // Atomic + TRUE, // Shift= Out + EraseAddress, // Addre= ss + 0, // Data = Number + NULL, + EnumSpiRegionBios // SPI_R= EGION_TYPE + ); + Print (L"Erase address =3D 0x%x, Erase %r\n", EraseAddress, Status); + if (EFI_ERROR (Status)) { + FreePool (Buffer); + Print (L"!!!ERROR: Erase flash %r\n", Status); + return (1); + } + } + + FreePool (Buffer); + AsmWbinvd (); + + return (0); +} + +/** + Write 'WriteAddress|NumBytes' in flash chip and skip the block all '0xFF= '. + + @param[in] WriteAddress Write address base in flash chip. + @param[in] NumBytes Write number of bytes. + @param[in] Buffer Point to contents going to write int= o flash chip. + + @retval 0 Flash write exited normally. + @retval Other An error occurred. + +**/ +UINTN +EFIAPI +FlashFdWrite ( + IN UINTN WriteAddress, + IN UINTN NumBytes, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + UINTN Index; + + Print (L"\nWrite flash chip "); + + for ( ; WriteAddress < NumBytes; WriteAddress +=3D mBlockSize) { + for (Index =3D 0; Index < mBlockSize; Index++) { + if (0xFF !=3D *(Buffer + Index)) { + Print (L"FlashFdWrite WriteAddress=3D 0x%x\n", WriteAddress); + Status =3D mSpiProtocol->Execute ( + mSpiProtocol, + SPI_OPCODE_WRITE_INDEX, // OpcodeIndex + 0, // PrefixOpcod= eIndex + TRUE, // DataCycle + TRUE, // Atomic + TRUE, // ShiftOut + WriteAddress, // Address + (UINT32)mBlockSize, // Data Number + Buffer, + EnumSpiRegionBios + ); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Write flash %r\n", Status); + return (1); + } else { + Print (L"."); + } + + break; + } + } + + Buffer +=3D mBlockSize; + } + + Print (L"\nWrite flash chip success\n"); + AsmWbinvd (); + + return (0); +} + +/** + Verify the binary in flash chip and the source binary use checksum. + + @param[in] BaseAddress Write address base in memory. + @param[in] NumBytes Write total number of bytes. + @param[in] Sourcefile Point to contents writed into flash = chip. + + @retval 0 Flash verify exited normally. + @retval Other An error occurred. + +**/ +UINTN +EFIAPI +FlashFdVerify ( + IN UINTN BaseAddress, + IN UINTN NumBytes, + IN VOID *Sourcefile + ) +{ + UINT8 *Buffer; + UINT32 Index; + UINT32 ChecksumSourceFile; + UINT32 ChecksumFlash; + + Print (L"\n"); + + ChecksumSourceFile =3D 0; + ChecksumFlash =3D 0; + + Buffer =3D AllocateZeroPool (NumBytes); + if (NULL =3D=3D Buffer) { + Print (L"!!!ERROR: Allocate fail ...\n"); + return (1); + } + + CopyMem (Buffer, (UINT8 *)(BaseAddress + mFlashAreaBaseAddress), NumByte= s); + for (Index =3D 0; Index < NumBytes; Index++) { + ChecksumFlash +=3D *(UINT8 *)(Buffer + Index); + ChecksumSourceFile +=3D *((UINT8 *)Sourcefile + Index); + } + + Print (L"Flash checksum: 0x%x, Source File checksum: 0x%x\n", ChecksumFl= ash, ChecksumSourceFile); + + if (ChecksumSourceFile =3D=3D ChecksumFlash) { + Print (L"Verify success\n"); + } else { + Print (L"!!!ERROR: Verify fail\n"); + FreePool (Buffer); + return (1); + } + + FreePool (Buffer); + + return (0); +} + +/** + Initialize. + + @retval 0 Flash erase exited normally. + @retval Other An error occurred. + +**/ +UINTN +EFIAPI +Initialize ( + IN UINT8 *Index + ) +{ + EFI_STATUS Status; + UINT8 FlashIndex; + UINT8 FlashID[3]; + SPI_INSTANCE *SpiInstance; + + mSpiProtocol =3D NULL; + + Status =3D gBS->LocateProtocol (&gEfiSpiProtocolGuid, NULL, (VOID **)&mS= piProtocol); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Locate SpiProtocol %r\n", Status); + FreePool (mSpiProtocol); + return (1); + } + + // + // attempt to identify flash part and initialize spi table + // + for (FlashIndex =3D 0; FlashIndex < EnumSpiFlashMax; FlashIndex++) { + Status =3D mSpiProtocol->Init ( + mSpiProtocol + ); + if (!EFI_ERROR (Status)) { + // + // read vendor/device IDs to check if flash device is supported + // + Status =3D mSpiProtocol->Execute ( + mSpiProtocol, + SPI_OPCODE_JEDEC_ID_INDEX, + SPI_WREN_INDEX, + TRUE, + FALSE, + FALSE, + 0, + 3, + FlashID, + EnumSpiRegionAll + ); + if (EFI_ERROR (Status)) { + return (1); + } else { + if ((FlashID[0] =3D=3D mSpiInitTable[FlashIndex].VendorId) && + (FlashID[1] =3D=3D mSpiInitTable[FlashIndex].DeviceId0) && + (FlashID[2] =3D=3D mSpiInitTable[FlashIndex].DeviceId1)) + { + Print ( + L"Supported SPI Flash device found, Vendor Id: 0x%02x, Device = ID: 0x%02x%02x\n", + FlashID[0], + FlashID[1], + FlashID[2] + ); + *Index =3D FlashIndex; + break; + } + } + } + } + + SpiInstance =3D SPI_INSTANCE_FROM_SPIPROTOCOL (mSpiProtocol); + mBiosSize =3D SpiInstance->SpiInitTable.BiosSize; + mFlashAreaBaseAddress =3D (UINT32)(0x100000000 - mBiosSize); + mBlockSize =3D SpiInstance->SpiInitTable.OpcodeMenu[SPI_OPCOD= E_ERASE_INDEX].Operation; + Print (L"BiosSize :0x%x, FlashAreaBaseAddress: 0x%x, Blocksize :0x%x\n",= mBiosSize, mFlashAreaBaseAddress, mBlockSize); + + return (0); +} + +/** + Print out help information. + +**/ +STATIC +VOID +PrintHelpInfo ( + VOID + ) +{ + Print (L"Application to update flash chip. Depends on SpiProtocol.\nSupp= ort flash chip: W25Q64FV/JV, W25Q64FW, MX25U6435F, MX25U12835F.\n\n"); + Print (L"Usage: FLASHUPDATE option [filename]\n\n"); + Print (L"Option:\n"); + Print (L" -help -h This help message\n"); + Print (L" Specifies the name of the file to write in= to flash chip\n"); + Print (L" -v Display version information\n"); + Print (L" -r Read flash chip and saved into file\n\n"); + Print (L"Filename:\n"); + Print (L" Specifies the name of the file to save the= contents read\n \ +from flash chip, just need when read flash chip.\n\n"); +} + +/** + Parse command in shell. + + @param[in] Num The number of items in Str. + @param[in] Str Array of pointers to strings. + + @retval 0 The application exited normally. + @retval 2 Read flash chip and save into file. + @retval Other An error occurred. + +**/ +STATIC +UINTN +EFIAPI +ShellCommandParse ( + IN UINTN Num, + IN CHAR16 **Str + ) +{ + EFI_STATUS Status; + + if (Num < 2) { + Print (L"FlashUpdate: Too few argument\n\n"); + PrintHelpInfo (); + return (1); + } else if (2 =3D=3D Num) { + if (StrLen (Str[1]) =3D=3D 0) { + Print (L"FlashUpdate: Too few argument\n\n"); + PrintHelpInfo (); + return (1); + } + + if ((Str[1])[0] =3D=3D L'-') { + // + // Parse the arguments. + // + if (StrCmp (Str[1], L"-v") =3D=3D 0) { + Print (L"FlashUpdate: Version 20230527\n\n"); + return (1); + } + + if (StrCmp (Str[1], L"-r") =3D=3D 0) { + Print (L"FlashUpdate: Too few argument\n\n"); + PrintHelpInfo (); + return (1); + } + + if ((StrCmp (Str[1], L"-help") =3D=3D 0) || (StrCmp (Str[1], L"-h") = =3D=3D 0)) { + PrintHelpInfo (); + return (1); + } else { + Print (L"FlashUpdate: Illegal option: '%s'\n\n", Str[1]); + PrintHelpInfo (); + return (1); + } + } + + Status =3D ShellIsFile (Str[1]); + if (EFI_ERROR (Status)) { + Print (L"FlashUpdate: %s is not a file\n\n", Str[1]); + PrintHelpInfo (); + return (1); + } + } else if (3 =3D=3D Num) { + if ((Str[1])[0] =3D=3D L'-') { + if (StrCmp (Str[1], L"-r") =3D=3D 0) { + Print (L"Read flash chip\n"); + return (2); + } + } + + Print (L"FlashUpdate: Illegal argument: '%s %s'\n\n", Str[1], Str[2]); + PrintHelpInfo (); + return (1); + } else if (Num > 3) { + Print (L"FlashUpdate: Too many argument\n\n"); + PrintHelpInfo (); + return (1); + } + + return (0); +} + +/** + UEFI application entry point which has an interface similar to a + standard C main function. + + The ShellCEntryLib library instance wrappers the actual UEFI application + entry point and calls this ShellAppMain function. + + @param[in] Argc The number of items in Argv. + @param[in] Argv Array of pointers to strings. + + @retval 0 The application exited normally. + @retval Other An error occurred. + +**/ +INTN +EFIAPI +ShellAppMain ( + IN UINTN Argc, + IN CHAR16 **Argv + ) +{ + VOID *Buffer; + EFI_STATUS Status; + SHELL_FILE_HANDLE SourceHandle; + UINTN SourceFileSize; + UINTN BeginTimeValue; + UINTN InitTimeValue; + UINTN EraseTimeValue; + UINTN WriteTimeValue; + UINTN VerifyTimeValue; + UINTN BaseAddress; + UINTN NumBytes; + UINT32 Index; + UINT8 FlashIndex; + + BeginTimeValue =3D AsmReadTsc (); + SourceHandle =3D NULL; + Buffer =3D NULL; + + Status =3D Initialize (&FlashIndex); + if (0 !=3D Status) { + Print (L"!!!ERROR: Initialize fail\n"); + return (1); + } + + BaseAddress =3D 0; + NumBytes =3D mBiosSize; // Assign after mBiosSize init in Initialize + + // + // Parse the command line. + // + Status =3D ShellCommandParse (Argc, Argv); + if (1 =3D=3D Status) { + return (1); + } else if (2 =3D=3D Status) { + Status =3D FlashFdRead (BaseAddress, NumBytes, Argv[2]); + if (0 !=3D Status) { + Print (L"!!!ERROR: Read flash chip fail"); + return (1); + } + + Print (L"Read flash chip and saved into %s success\n", Argv[2]); + return (0); + } + + // + // open source file + // + Status =3D ShellOpenFileByName (Argv[1], &SourceHandle, EFI_FILE_MODE_RE= AD, 0); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Open file %s %r\n", Argv[1], Status); + return (1); + } + + // + // get file size of source file + // + Status =3D ShellGetFileSize (SourceHandle, &SourceFileSize); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Read file %s size %r\n", Argv[1], Status); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + return (1); + } + + Buffer =3D AllocateZeroPool (SourceFileSize); + if (NULL =3D=3D Buffer) { + Print (L"!!!ERROR: Allocate pool fail ...\n"); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + return (1); + } + + Status =3D ShellReadFile (SourceHandle, &SourceFileSize, Buffer); + if (EFI_ERROR (Status)) { + Print (L"!!!ERROR: Read file %s %r\n", Argv[1], Status); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + return (1); + } + + Print ( + L"Supported SPI Flash device found, Vendor Id: 0x%02x, Device ID: 0x%0= 2x%02x\n", + mSpiInitTable[FlashIndex].VendorId, + mSpiInitTable[FlashIndex].DeviceId0, + mSpiInitTable[FlashIndex].DeviceId1 + ); + + InitTimeValue =3D AsmReadTsc (); + Print (L"Init spent time: %d seconds\n", GetSpentTime (InitTimeValue, Be= ginTimeValue)); + + Print (L"Size of %s: 0x%x bytes, Flash size: 0x%x bytes\n", Argv[1], Sou= rceFileSize, mBiosSize); + if (mBiosSize !=3D SourceFileSize) { + Print (L"!!!ERROR: Bios size is not correct\n"); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + return (1); + } + + Status =3D FlashFd64KErase (BaseAddress, NumBytes); + if (0 !=3D Status) { + Print (L"!!!ERROR: Erase falsh chip fail\n"); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + return (1); + } + + if (0 !=3D Status) { + Print (L"!!!ERROR: Erase falsh chip fail\n"); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + return (1); + } + + EraseTimeValue =3D AsmReadTsc (); + Print (L"Erase spent time: %d seconds\n", GetSpentTime (EraseTimeValue, = InitTimeValue)); + + Status =3D FlashFdWrite (BaseAddress, NumBytes, Buffer); + if (0 !=3D Status) { + Print (L"!!!ERROR: Write falsh chip fail\n"); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + return (1); + } + + WriteTimeValue =3D AsmReadTsc (); + Print (L"\nWrite spent time: %d seconds\n", GetSpentTime (WriteTimeValue= , EraseTimeValue)); + + Status =3D FlashFdVerify (BaseAddress, NumBytes, (UINT8 *)Buffer); + if (0 !=3D Status) { + Print (L"!!!ERROR: Verify falsh chip fail\n"); + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + return (1); + } + + VerifyTimeValue =3D AsmReadTsc (); + Print (L"\nWrite flash chip success!\n"); + Print (L"--------------------------------------------------\n"); + Print (L"Total spent time: %d seconds\n", GetSpentTime (VerifyTimeValue,= BeginTimeValue)); + + if (NULL !=3D SourceHandle) { + ShellCloseFile (&SourceHandle); + } + + if (NULL !=3D Buffer) { + FreePool (Buffer); + } + + Print (L"\nReady to restart "); + for (Index =3D 0; Index < 4; Index++) { + MicroSecondDelay (1000000); // delay 1 second + Print (L"."); + } + + gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL); + + return (0); +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlas= hUpdate/UDKFlashUpdate.h b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Appli= cation/UDKFlashUpdate/UDKFlashUpdate.h new file mode 100644 index 0000000000..ed15a4489a --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlashUpdate= /UDKFlashUpdate.h @@ -0,0 +1,48 @@ +/** @file + Implements UDKFlashUpdate.h + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef UDK_FLASH_UPDATE_H_ +#define UDK_FLASH_UPDATE_H_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "SpiFlashDevice.h" + +#define SPI_OPCODE_JEDEC_ID_INDEX 0 +#define SPI_OPCODE_WRITE_S_INDEX 1 +#define SPI_OPCODE_WRITE_INDEX 2 +#define SPI_OPCODE_READ_INDEX 3 +#define SPI_OPCODE_ERASE_INDEX 4 +#define SPI_OPCODE_READ_S_INDEX 5 +#define SPI_OPCODE_CHIP_ERASE_INDEX 6 +#define SPI_OPCODE_READ_SFDP_INDEX 7 +#define SPI_COMMAND_RPMC_OP1_INDEX 8 +#define SPI_COMMAND_RPMC_OP2_INDEX 9 +#define SPI_COMMAND_Enter_4Byte_Addr_INDEX 10 +#define SPI_COMMAND_Exit_4Byte_Addr_INDEX 11 + +extern SPI_INIT_TABLE mSpiInitTable[]; +extern EFI_RUNTIME_SERVICES *gRT; + +#endif diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlas= hUpdate/UDKFlashUpdate.inf b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/App= lication/UDKFlashUpdate/UDKFlashUpdate.inf new file mode 100644 index 0000000000..8f055f3925 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Application/UDKFlashUpdate= /UDKFlashUpdate.inf @@ -0,0 +1,51 @@ +## @file +# UDK Flash update +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010006 + BASE_NAME =3D UDKFlashUpdate + FILE_GUID =3D a912f198-7f0e-4803-b908-b757b806ec83 + MODULE_TYPE =3D UEFI_APPLICATION + VERSION_STRING =3D 0.1 + ENTRY_POINT =3D ShellCEntryLib + +# +# VALID_ARCHITECTURES =3D IA32 X64 IPF +# + +[Sources] + UDKFlashUpdate.c + UDKFlashUpdate.h + SpiFlashDevice.c + SpiFlashDevice.h + +[Packages] + MdePkg/MdePkg.dec + ShellPkg/ShellPkg.dec + VanGoghCommonPkg/AmdCommonPkg.dec + ChachaniBoardPkg/Project.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + UefiLib + ShellCEntryLib + MemoryAllocationLib + ShellLib + UefiBootServicesTableLib + PcdLib + BaseMemoryLib + BaseLib + TimerLib + +[Protocols] + gEfiSpiProtocolGuid # CONSUME + +[FixedPcd] + gPlatformPkgTokenSpaceGuid.PcdFlashAreaSize + gPlatformPkgTokenSpaceGuid.PcdFlashAreaBaseAddress + -- 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 (#114501): https://edk2.groups.io/g/devel/message/114501 Mute This Topic: https://groups.io/mt/103971398/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-