From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smarthost01a.ixn.mail.zen.net.uk (smarthost01a.ixn.mail.zen.net.uk [212.23.1.20]) by mx.groups.io with SMTP id smtpd.web12.3155.1645120294167313975 for ; Thu, 17 Feb 2022 09:51:34 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=permerror, err=parse error for token &{10 18 sdn.klaviyomail.com}: permanent DNS error (domain: starlabs.systems, ip: 212.23.1.20, mailfrom: sean@starlabs.systems) Received: from [217.155.46.38] (helo=sean-StarBook.lan) by smarthost01a.ixn.mail.zen.net.uk with esmtp (Exim 4.90_1) (envelope-from ) id 1nKkwF-0004pV-EX; Thu, 17 Feb 2022 17:51:31 +0000 From: "Sean Rhodes" To: devel@edk2.groups.io Cc: guo.dong@intel.com, Patrick Rudolph , Ray Ni , Maurice Ma , Benjamin You Subject: [PATCH 2/2] UefipayloadPkg: Add SmbusConfigLoaderDxe Date: Thu, 17 Feb 2022 17:51:27 +0000 Message-Id: <1a141c15226d8ffbecfcdbe5b51d34a1f4b8887d.1645120287.git.sean@starlabs.systems> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 X-Originating-smarthost01a-IP: [217.155.46.38] Feedback-ID: 217.155.46.38 Content-Transfer-Encoding: quoted-printable From: Patrick Rudolph Reads board specific configuration values from an EEPROM that is written to by the BMC. Cc: Guo Dong Cc: Ray Ni Cc: Maurice Ma Cc: Benjamin You Signed-off-by: Patrick Rudolph --- .../Include/Guid/BoardSettingsGuid.h | 36 +++ .../SmbusConfigLoaderDxe/SMBusConfigLoader.c | 269 ++++++++++++++++++ .../SmbusConfigLoaderDxe/SMBusConfigLoader.h | 23 ++ .../SMBusConfigLoader.inf | 54 ++++ UefiPayloadPkg/UefiPayloadPkg.dec | 1 + UefiPayloadPkg/UefiPayloadPkg.dsc | 1 + UefiPayloadPkg/UefiPayloadPkg.fdf | 11 +- 7 files changed, 390 insertions(+), 5 deletions(-) create mode 100644 UefiPayloadPkg/Include/Guid/BoardSettingsGuid.h create mode 100644 UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.c create mode 100644 UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.h create mode 100644 UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.i= nf diff --git a/UefiPayloadPkg/Include/Guid/BoardSettingsGuid.h b/UefiPayloadP= kg/Include/Guid/BoardSettingsGuid.h new file mode 100644 index 0000000000..415b49dcde --- /dev/null +++ b/UefiPayloadPkg/Include/Guid/BoardSettingsGuid.h @@ -0,0 +1,36 @@ +/** @file + This file defines the hob structure for board settings + + Copyright (c) 2020, 9elements GmbH. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __BOARD_SETTINGS_GUID_H__ +#define __BOARD_SETTINGS_GUID_H__ + +/// +/// Board information GUID +/// +extern EFI_GUID gEfiBoardSettingsVariableGuid; + +#pragma pack(1) + +typedef struct { + UINT32 Signature; + UINT8 SecureBoot; + UINT8 PrimaryVideo; + UINT8 DeepSx; + UINT8 WakeOnUSB; + UINT8 USBPowerinS5; + UINT8 PowerStateAfterG3; + UINT8 BlueRearPort; + UINT8 InternalAudioConnection; + UINT8 PxeBootCapability; +} BOARD_SETTINGS; + +#pragma pack() + +#define BOARD_SETTINGS_NAME L"BoardSettings" + +#endif // __BOARD_SETTINGS_GUID_H__ diff --git a/UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.c b/Uefi= PayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.c new file mode 100644 index 0000000000..c2edbaa74f --- /dev/null +++ b/UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.c @@ -0,0 +1,269 @@ +/** @file=0D + Implementation for a generic i801 SMBus driver.=0D +=0D +Copyright (c) 2016, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +=0D +**/=0D +=0D +#include "SMBusConfigLoader.h"=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D + GetPciConfigSpaceAddress=0D +=0D + Return the PCI Config Space Address if found, zero otherwise.=0D +=0D + @retval 0 Can not find any SMBusController=0D + @retval UINT32 PCI Config Space Address=0D +**/=0D +STATIC UINT32=0D +EFIAPI=0D +GetPciConfigSpaceAddress (=0D + )=0D +{=0D + UINT8 Device;=0D + UINT8 Function;=0D + UINT8 BaseClass;=0D + UINT8 SubClass;=0D +=0D + //=0D + // Search for SMBus Controller within PCI Devices on root bus=0D + //=0D + for (Device =3D 0; Device <=3D PCI_MAX_DEVICE; Device++) {=0D + for (Function =3D 0; Function <=3D PCI_MAX_FUNC; Function++) {=0D + if (PciRead16 (PCI_LIB_ADDRESS (0, Device, Function, 0x00)) !=3D 0x8= 086) {=0D + continue;=0D + }=0D +=0D + BaseClass =3D PciRead8 (PCI_LIB_ADDRESS (0, Device, Function, 0x0B))= ;=0D +=0D + if (BaseClass =3D=3D PCI_CLASS_SERIAL) {=0D + SubClass =3D PciRead8 (PCI_LIB_ADDRESS (0, Device, Function, 0xA))= ;=0D +=0D + if (SubClass =3D=3D PCI_CLASS_SERIAL_SMB) {=0D + return PCI_LIB_ADDRESS (0, Device, Function, 0x00);=0D + }=0D + }=0D + }=0D + }=0D +=0D + return 0;=0D +}=0D +=0D +/**=0D + ReadBoardOptionFromEEPROM=0D +=0D + Reads the Board options like Primary Video from the EEPROM=0D +=0D + @param Buffer Pointer to the Buffer Array=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +ReadBoardOptionFromEEPROM (=0D + IN OUT UINT8 *Buffer,=0D + IN UINT32 Size=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINT16 Index;=0D + UINT16 Value;=0D +=0D + for (Index =3D BOARD_SETTINGS_OFFSET; Index < BOARD_SETTINGS_OFFSET + Si= ze; Index +=3D 2) {=0D + Value =3D SmBusProcessCall (SMBUS_LIB_ADDRESS (0x57, 0, 0, 0), ((Index= & 0xff) << 8) | ((Index & 0xff00) >> 8), &Status);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "Failed to read SMBUS byte at offset 0x%x\n", I= ndex));=0D + return Status;=0D + }=0D +=0D + DEBUG ((EFI_D_ERROR, "Read %x\n", Value));=0D + CopyMem (&Buffer[Index-BOARD_SETTINGS_OFFSET], &Value, sizeof (Value))= ;=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +STATIC=0D +UINT32=0D +EFIAPI=0D +crc32_byte (=0D + UINT32 prev_crc,=0D + UINT8 data=0D + )=0D +{=0D + prev_crc ^=3D (UINT32)data << 24;=0D +=0D + for (int i =3D 0; i < 8; i++) {=0D + if ((prev_crc & 0x80000000UL) !=3D 0) {=0D + prev_crc =3D ((prev_crc << 1) ^ 0x04C11DB7UL);=0D + } else {=0D + prev_crc <<=3D 1;=0D + }=0D + }=0D +=0D + return prev_crc;=0D +}=0D +=0D +/**=0D + Computes and returns a 32-bit CRC for a data buffer.=0D + CRC32 value bases on ITU-T V.42.=0D +=0D + If Buffer is NULL, then ASSERT().=0D + If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().=0D +=0D + @param[in] Buffer A pointer to the buffer on which the 32-bit CRC= is to be computed.=0D + @param[in] Length The number of bytes in the buffer Data.=0D +=0D + @retval Crc32 The 32-bit CRC was computed for the data buffer= .=0D +=0D +**/=0D +STATIC=0D +UINT32=0D +EFIAPI=0D +CalculateCrc32 (=0D + IN VOID *Buffer,=0D + IN UINTN Length=0D + )=0D +{=0D + int i;=0D + UINT32 crc =3D 0x0;=0D +=0D + for ( i =3D 0; i < Length; i++ ) {=0D + crc =3D crc32_byte (crc, ((UINT8 *)Buffer)[i]);=0D + }=0D +=0D + return crc;=0D +}=0D +=0D +/**=0D + The Entry Point for SMBUS driver.=0D +=0D + It installs DriverBinding.=0D +=0D + @retval EFI_SUCCESS The entry point is executed successfully.=0D + @retval other Some error occurs when executing this entry po= int.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +InstallSMBusConfigLoader (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D + BOARD_SETTINGS BoardSettings;=0D + UINT32 BaseAddress;=0D + UINT32 CRC32Array;=0D + UINT32 HostCBackup;=0D + UINT8 Array[sizeof (BOARD_SETTINGS)];=0D +=0D + DEBUG ((DEBUG_INFO, "SMBusConfigLoader: InstallSMBusConfigLoader\n"));=0D +=0D + BaseAddress =3D GetPciConfigSpaceAddress ();=0D + if (BaseAddress =3D=3D 0) {=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + ZeroMem (&BoardSettings, sizeof (BOARD_SETTINGS));=0D +=0D + // Set I2C_EN Bit=0D + HostCBackup =3D PciRead32 (BaseAddress + HOSTC);=0D + PciWrite32 (BaseAddress + HOSTC, HostCBackup | I2C_EN_HOSTC);=0D +=0D + Status =3D ReadBoardOptionFromEEPROM (Array, sizeof (Array));=0D +=0D + CRC32Array =3D 0;=0D + if (!EFI_ERROR (Status)) {=0D + CopyMem (&BoardSettings, Array, sizeof (BOARD_SETTINGS));=0D +=0D + DEBUG ((DEBUG_INFO, "SMBusConfigLoader: Board Settings:\n"));=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "SMBusConfigLoader: CRC: %08x - SecureBoot: %02x - PrimaryVideo: %02= x\n",=0D + BoardSettings.Signature,=0D + BoardSettings.SecureBoot,=0D + BoardSettings.PrimaryVideo=0D + ));=0D +=0D + CRC32Array =3D CalculateCrc32 (&Array[sizeof (UINT32)], sizeof (BOARD_= SETTINGS) - sizeof (UINT32));=0D + if (CRC32Array !=3D BoardSettings.Signature) {=0D + DEBUG ((DEBUG_ERROR, "SMBusConfigLoader: Checksum invalid. Should be= %04X - is: %04x.\nReseting to defaults.\n", CRC32Array, BoardSettings.Sign= ature));=0D + }=0D + }=0D +=0D + if (EFI_ERROR (Status) || (CRC32Array !=3D BoardSettings.Signature)) {=0D + BoardSettings.PrimaryVideo =3D 0;=0D + BoardSettings.SecureBoot =3D 1;=0D + }=0D +=0D + // Set SecureBootEnable. Only affects SecureBootSetupDxe.=0D + Status =3D gRT->SetVariable (=0D + EFI_SECURE_BOOT_ENABLE_NAME,=0D + &gEfiSecureBootEnableDisableGuid,=0D + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACC= ESS,=0D + sizeof BoardSettings.SecureBoot,=0D + &BoardSettings.SecureBoot=0D + );=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "SMBusConfigLoader: Failed to set SecureBootEnabl= e: %x\n", Status));=0D + }=0D +=0D + // Set SecureBoot. Only affects code outside of SecureBootSetupDxe.=0D + Status =3D gRT->SetVariable (=0D + EFI_SECURE_BOOT_MODE_NAME,=0D + &gEfiGlobalVariableGuid,=0D + EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_A= CCESS,=0D + sizeof BoardSettings.SecureBoot,=0D + &BoardSettings.SecureBoot=0D + );=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "SMBusConfigLoader: Failed to set SecureBoot: %x\= n", Status));=0D + }=0D +=0D + if (BoardSettings.SecureBoot =3D=3D 0) {=0D + UINT8 SetupMode;=0D + // Hide the UI if secureboot is disabled=0D + SetupMode =3D 1;=0D + Status =3D gRT->SetVariable (=0D + EFI_SETUP_MODE_NAME,=0D + &gEfiGlobalVariableGuid,=0D + EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERV= ICE_ACCESS,=0D + sizeof SetupMode,=0D + &SetupMode=0D + );=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "SMBusConfigLoader: Failed to set SetupMode: %x= \n", Status));=0D + }=0D + }=0D +=0D + // Restore I2C_EN Bit=0D + PciWrite32 (BaseAddress + HOSTC, HostCBackup);=0D +=0D + // Save data into UEFI Variable for later use=0D + Status =3D gRT->SetVariable (=0D + BOARD_SETTINGS_NAME, = // Variable Name=0D + &gEfiBoardSettingsVariableGuid, = // Variable Guid=0D + (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_AC= CESS), // Variable Attributes=0D + sizeof (BOARD_SETTINGS),=0D + &BoardSettings=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "SMBusConfigLoader: Failed to save BoardSettings = %x\n", Status));=0D + return Status;=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D diff --git a/UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.h b/Uefi= PayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.h new file mode 100644 index 0000000000..5e589cefff --- /dev/null +++ b/UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.h @@ -0,0 +1,23 @@ +/** @file=0D + Header file for a generic i801 SMBUS driver.=0D +=0D +Copyright (c) 2016, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +=0D +**/=0D +#ifndef _PCI_PLATFORM_DXE_H_=0D +#define _PCI_PLATFORM_DXE_H_=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#define BOARD_SETTINGS_OFFSET 0x1f00=0D +=0D +#define HOSTC 0x40=0D +#define I2C_EN_HOSTC (1 << 2)=0D +=0D +#endif=0D diff --git a/UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.inf b/Ue= fiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.inf new file mode 100644 index 0000000000..a2c2656a4d --- /dev/null +++ b/UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.inf @@ -0,0 +1,54 @@ +## @file=0D +# This driver produces gEfiSmbusHcProtocolGuid protocol to load access SMB= US devices=0D +#=0D +# Copyright (c) 2020, 9elements Agency GmbH=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D SMBusConfigLoader=0D + FILE_GUID =3D FCBA64FC-BC6B-4B8B-A0E2-D1EC78B8C01A= =0D + MODULE_TYPE =3D DXE_DRIVER=0D + VERSION_STRING =3D 1.0=0D + ENTRY_POINT =3D InstallSMBusConfigLoader=0D +=0D +#=0D +# The following information is for reference only and not required by the = build tools.=0D +#=0D +# VALID_ARCHITECTURES =3D IA32 X64=0D +#=0D +=0D +[Sources.common]=0D + SMBusConfigLoader.h=0D + SMBusConfigLoader.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + UefiPayloadPkg/UefiPayloadPkg.dec=0D + SecurityPkg/SecurityPkg.dec=0D +=0D +[Guids]=0D + gEfiBoardSettingsVariableGuid=0D + gEfiSecureBootEnableDisableGuid=0D + gEfiGlobalVariableGuid=0D +=0D +[LibraryClasses]=0D + UefiDriverEntryPoint=0D + DebugLib=0D + DevicePathLib=0D + SmbusLib=0D + UefiRuntimeServicesTableLib=0D + PciLib=0D +=0D +[Depex]=0D + gEfiSmbusHcProtocolGuid AND=0D + gEfiVariableArchProtocolGuid=0D +=0D +[Protocols]=0D + gEfiSmbusHcProtocolGuid ## CONSUMES=0D + gEfiVariableArchProtocolGuid ## CONSUMES=0D diff --git a/UefiPayloadPkg/UefiPayloadPkg.dec b/UefiPayloadPkg/UefiPayload= Pkg.dec index 551f0a4915..e902180349 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dec +++ b/UefiPayloadPkg/UefiPayloadPkg.dec @@ -19,6 +19,7 @@ =0D =0D [Guids]=0D + gEfiBoardSettingsVariableGuid =3D { 0xae2029d1, 0x11f2, 0x483d, { 0xbf, = 0xbc, 0xad, 0x63, 0x5d, 0xe3, 0x32, 0x26 }} # ae2029d1-11f2-483d-bfbc-ad635= de33226=0D #=0D ## Defines the token space for the UEFI Payload Package PCDs.=0D #=0D diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 54543b7623..9321fa958c 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -663,6 +663,7 @@ # SMBUS Support=0D #=0D UefiPayloadPkg/SmbusDxe/SMBusi801Dxe.inf=0D + UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.inf=0D =0D #=0D # Console Support=0D diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayload= Pkg.fdf index 8861d29162..b241613507 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.fdf +++ b/UefiPayloadPkg/UefiPayloadPkg.fdf @@ -158,6 +158,12 @@ INF MdeModulePkg/Universal/EbcDxe/EbcDxe.inf INF UefiPayloadPkg/BlSupportDxe/BlSupportDxe.inf=0D =0D INF MdeModulePkg/Universal/SmbiosDxe/SmbiosDxe.inf=0D +#=0D +# SMBUS Support=0D +#=0D +INF UefiPayloadPkg/SmbusDxe/SMBusi801Dxe.inf=0D +INF UefiPayloadPkg/SmbusConfigLoaderDxe/SMBusConfigLoader.inf=0D +=0D #=0D # PCI Support=0D #=0D @@ -178,11 +184,6 @@ INF MdeModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2KeyboardDxe= .inf !endif=0D INF MdeModulePkg/Bus/Isa/Ps2MouseDxe/Ps2MouseDxe.inf=0D =0D -#=0D -# SMBUS Support=0D -#=0D -INF UefiPayloadPkg/SmbusDxe/SMBusi801Dxe.inf=0D -=0D #=0D # Console Support=0D #=0D --=20 2.32.0