From cce17e841ba152a3fbadcad3b0427e277fe01265 Mon Sep 17 00:00:00 2001 From: vit9696 Date: Sat, 9 Mar 2019 01:57:11 +0300 Subject: [PATCH] Start working on OcAcpiLib --- Include/Library/OcAcpiLib.h | 140 +++++-- Include/Library/OcSmbiosLib.h | 4 - Library/OcAcpiLib/AcpiFindLegacyRsdPtr.c | 71 ---- Library/OcAcpiLib/AcpiFindRsdPtr.c | 62 ---- Library/OcAcpiLib/AcpiLocateTable.c | 88 ----- Library/OcAcpiLib/OcAcpiLib.c | 446 +++++++++++++++++++++++ Library/OcAcpiLib/OcAcpiLib.inf | 8 +- Library/OcSmbiosLib/SmbiosPatch.c | 12 +- OcSupportPkg.dsc | 2 + Tests/AcpiTest/AcpiTest.c | 116 ++++++ Tests/AcpiTest/AcpiTest.inf | 66 ++++ Tests/AcpiTest/AcpiTestApp.inf | 63 ++++ Tests/SmbiosTest/SmbiosTest.c | 1 - TestsUser/Smbios/Smbios.c | 1 - 14 files changed, 813 insertions(+), 267 deletions(-) delete mode 100755 Library/OcAcpiLib/AcpiFindLegacyRsdPtr.c delete mode 100755 Library/OcAcpiLib/AcpiFindRsdPtr.c delete mode 100755 Library/OcAcpiLib/AcpiLocateTable.c create mode 100755 Library/OcAcpiLib/OcAcpiLib.c create mode 100644 Tests/AcpiTest/AcpiTest.c create mode 100644 Tests/AcpiTest/AcpiTest.inf create mode 100644 Tests/AcpiTest/AcpiTestApp.inf diff --git a/Include/Library/OcAcpiLib.h b/Include/Library/OcAcpiLib.h index ad8a67a4..1cc582d0 100755 --- a/Include/Library/OcAcpiLib.h +++ b/Include/Library/OcAcpiLib.h @@ -12,51 +12,121 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ -#ifndef OC_ACPI_LIB_H_ -#define OC_ACPI_LIB_H_ +#ifndef OC_ACPI_LIB_H +#define OC_ACPI_LIB_H -// TODO: remove this nasty temporary workaround +#include -#include +// +// RSDP and XSDT table definitions not provided by EDK2 due to no +// flexible array support. +// -// AcpiFindLegacyRsdPtr -/** Find RSD_PTR Table In Legacy Area +#pragma pack(push, 1) - @retval EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER -**/ -EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER * -AcpiFindLegacyRsdPtr ( - VOID - ); +typedef struct { + EFI_ACPI_DESCRIPTION_HEADER Header; + UINT32 Tables[]; +} OC_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_TABLE; -// AcpiFindRsdPtr -/** Find RSD_PTR Table From System Configuration Tables +typedef struct { + EFI_ACPI_DESCRIPTION_HEADER Header; + UINT64 Tables[]; +} OC_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE; - @retval EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER -**/ -EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER * -AcpiFindRsdPtr ( - VOID - ); +#pragma pack(pop) -// AcpiLocateTable -/** Locate an existing ACPI table. +typedef struct { + // + // Pointer to original RSDP table. + // + EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp; + // + // Pointer to active RSDT table. + // + OC_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_TABLE *Rsdt; + // + // Pointer to active XSDT table. + // + OC_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE *Xsdt; + // + // Current list of tables allocated from heap. + // + EFI_ACPI_COMMON_HEADER **Tables; + // + // Number of tables. + // + UINT32 NumberOfTables; + // + // Number of allocated table slots. + // + UINT32 AllocatedTables; +} OC_ACPI_CONTEXT; - @param[in] Signature - @param[in, out] Table - @param[in, out] Handle - @param[in, out] Version +/** Find ACPI System Tables for later table configuration. - @retval EFI_SUCCESS - @retval EFI_NOT_FOUND - @retval EFI_INVALID_PARAMETER + @param Context ACPI library context. + + @retval EFI_SUCCESS when Rsdp and Xsdt or Rsdt are found. **/ EFI_STATUS -AcpiLocateTable ( - IN UINT32 Signature, - IN OUT EFI_ACPI_DESCRIPTION_HEADER **Table, - IN OUT UINTN *Handle, - IN OUT EFI_ACPI_TABLE_VERSION *Version +AcpiInitContext ( + IN OUT OC_ACPI_CONTEXT *Context ); -#endif // OC_ACPI_LIB_H_ +/** Free ACPI context dynamic resources. + + @param Context ACPI library context. +**/ +VOID +AcpiFreeContext ( + IN OUT OC_ACPI_CONTEXT *Context + ); + +/** Apply ACPI context to this system. + + @param Context ACPI library context. +**/ +EFI_STATUS +AcpiApplyContext ( + IN OUT OC_ACPI_CONTEXT *Context + ); + +/** Drop one ACPI table. + + @param Context ACPI library context + @param Signature Table signature or 0. + @param Length Table length or 0. + @param OemTableId Table Id or 0. +**/ +EFI_STATUS +AcpiDropTable ( + IN OUT OC_ACPI_CONTEXT *Context, + IN UINT32 Signature, + IN UINT32 Length, + IN UINT64 OemTableId + ); + +/** Install one ACPI table. For DSDT this performs table replacement. + + @param Context ACPI library context + @param Data Table data. + @param Length Table length. +**/ +EFI_STATUS +AcpiInsertTable ( + IN OUT OC_ACPI_CONTEXT *Context, + IN CONST UINT8 *Data, + IN UINT32 Length + ); + +/** Normalise ACPI headers to contain 7-bit ASCII. + + @param Context ACPI library context +**/ +VOID +AcpiNormalizeHeaders ( + IN OUT OC_ACPI_CONTEXT *Context + ); + +#endif // OC_ACPI_LIB_H diff --git a/Include/Library/OcSmbiosLib.h b/Include/Library/OcSmbiosLib.h index 0bf8e199..e88e968c 100644 --- a/Include/Library/OcSmbiosLib.h +++ b/Include/Library/OcSmbiosLib.h @@ -83,10 +83,6 @@ typedef struct OC_SMBIOS_DATA_ { // UINT16 ProcessorType; // - // Type 132 - // - UINT16 ProcessorBusSpeed; - // // Type 133 // UINT32 PlatformFeature; diff --git a/Library/OcAcpiLib/AcpiFindLegacyRsdPtr.c b/Library/OcAcpiLib/AcpiFindLegacyRsdPtr.c deleted file mode 100755 index e163fc74..00000000 --- a/Library/OcAcpiLib/AcpiFindLegacyRsdPtr.c +++ /dev/null @@ -1,71 +0,0 @@ -/** @file - Copyright (C) 2016, The HermitCrabs Lab. All rights reserved. - - All rights reserved. - - This program and the accompanying materials - are licensed and made available under the terms and conditions of the BSD License - which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -**/ -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -// AcpiFindLegacyRsdPtr -/** Find RSD_PTR Table In Legacy Area - - @retval EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER -**/ -EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER * -AcpiFindLegacyRsdPtr ( - VOID - ) -{ - EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER *RsdPtr; - - UINTN Address; - UINTN Index; - - // First Search 0x0E0000 - 0x0FFFFF for RSD_PTR - - RsdPtr = NULL; - - for (Address = 0x0E0000; Address < 0x0FFFFF; Address += 16) { - if (*(UINT64 *)Address == EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) { - RsdPtr = (EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)Address; - } - } - - // Then Search EBDA 0x40E - 0x800 - - if (RsdPtr == NULL) { - Address = ((*(UINT16 *)(UINTN)0x040E) << 4); - - for (Index = 0; Index < 0x0400; Index += 16) { - if (*(UINT64 *)(Address + Index) == EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) { - RsdPtr = (EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)Address; - } - } - } - - return RsdPtr; -} diff --git a/Library/OcAcpiLib/AcpiFindRsdPtr.c b/Library/OcAcpiLib/AcpiFindRsdPtr.c deleted file mode 100755 index 976e1708..00000000 --- a/Library/OcAcpiLib/AcpiFindRsdPtr.c +++ /dev/null @@ -1,62 +0,0 @@ -/** @file - Copyright (C) 2016, The HermitCrabs Lab. All rights reserved. - - All rights reserved. - - This program and the accompanying materials - are licensed and made available under the terms and conditions of the BSD License - which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -**/ -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -// AcpiFindRsdPtr -/** Find RSD_PTR Table From System Configuration Tables - - @retval EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER -**/ -EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER * -AcpiFindRsdPtr ( - VOID - ) -{ - EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER *RsdPtr; - - UINTN Index; - - RsdPtr = NULL; - - // Find ACPI table RSD_PTR from system table - - for (Index = 0; Index < gST->NumberOfTableEntries; ++Index) { - if (CompareGuid (&(gST->ConfigurationTable[Index].VendorGuid), &gEfiAcpi20TableGuid) - || CompareGuid (&(gST->ConfigurationTable[Index].VendorGuid), &gEfiAcpi10TableGuid) - || CompareGuid (&(gST->ConfigurationTable[Index].VendorGuid), &gEfiAcpiTableGuid)) { - RsdPtr = gST->ConfigurationTable[Index].VendorTable; - - break; - } - } - - return RsdPtr; -} diff --git a/Library/OcAcpiLib/AcpiLocateTable.c b/Library/OcAcpiLib/AcpiLocateTable.c deleted file mode 100755 index d63f3955..00000000 --- a/Library/OcAcpiLib/AcpiLocateTable.c +++ /dev/null @@ -1,88 +0,0 @@ -/** @file - Copyright (C) 2016, The HermitCrabs Lab. All rights reserved. - - All rights reserved. - - This program and the accompanying materials - are licensed and made available under the terms and conditions of the BSD License - which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -**/ -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include - -#include - -#include - -// AcpiLocateTable -/** - - @param[in] Signature - @param[in, out] Table - @param[in, out] Handle - @param[in, out] Version - - @retval EFI_SUCCESS - @retval EFI_NOT_FOUND - @retval EFI_INVALID_PARAMETER -**/ -EFI_STATUS -AcpiLocateTable ( - IN UINT32 Signature, - IN OUT EFI_ACPI_DESCRIPTION_HEADER **Table, - IN OUT UINTN *Handle, - IN OUT EFI_ACPI_TABLE_VERSION *Version - ) -{ - EFI_ACPI_SUPPORT_PROTOCOL *AcpiSupport; - - EFI_STATUS Status; - INTN Index; - - AcpiSupport = NULL; - Status = gBS->LocateProtocol ( - &gEfiAcpiSupportProtocolGuid, - NULL, - (VOID **)&AcpiSupport - ); - - if (!EFI_ERROR (Status)) { - Index = 0; - - // Iterate The Tables To Find Matching Table - - do { - Status = AcpiSupport->GetAcpiTable ( - AcpiSupport, - Index, - (VOID **)Table, - Version, - Handle - ); - - if (Status == EFI_NOT_FOUND) { - break; - } - - ++Index; - } while ((*Table)->Signature != Signature); - } - - return Status; -} diff --git a/Library/OcAcpiLib/OcAcpiLib.c b/Library/OcAcpiLib/OcAcpiLib.c new file mode 100755 index 00000000..9deea730 --- /dev/null +++ b/Library/OcAcpiLib/OcAcpiLib.c @@ -0,0 +1,446 @@ +/** @file + Copyright (C) 2016, The HermitCrabs Lab. All rights reserved. + + All rights reserved. + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +**/ +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include + + +/** Find RSD_PTR Table In Legacy Area + + @retval EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER +**/ +STATIC +EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER * +AcpiFindLegacyRsdp ( + VOID + ) +{ + EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp; + + UINTN Address; + UINTN Index; + + // + // First Search 0x0E0000 - 0x0FFFFF for RSD_PTR + // + + Rsdp = NULL; + + for (Address = 0x0E0000; Address < 0x0FFFFF; Address += 16) { + if (*(UINT64 *) Address == EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) { + Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *) Address; + } + } + + // + // Then Search EBDA 0x40E - 0x800 + // + + if (Rsdp == NULL) { + Address = ((*(UINT16 *) 0x040E) << 4); + + for (Index = 0; Index < 0x0400; Index += 16) { + if (*(UINT64 *) (Address + Index) == EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) { + Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *) Address; + } + } + } + + return Rsdp; +} + +/** Find RSD_PTR Table From System Configuration Tables + + @retval EFI_ACPI_6_0_ROOT_SYSTEM_DESCRIPTION_POINTER +**/ +STATIC +EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER * +AcpiFindRsdp ( + VOID + ) +{ + EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp; + UINTN Index; + + Rsdp = NULL; + + // + // Find ACPI table RSD_PTR from system table + // + + for (Index = 0; Index < gST->NumberOfTableEntries; ++Index) { + // + // Prefer ACPI 2.0 + // + if (CompareGuid (&gST->ConfigurationTable[Index].VendorGuid, &gEfiAcpi20TableGuid)) { + Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *) gST->ConfigurationTable[Index].VendorTable; + DEBUG ((DEBUG_VERBOSE, "Found ACPI 2.0 RSDP table %p\n", Rsdp)); + break; + } + + // + // Otherwise use ACPI 1.0, but do search for ACPI 2.0. + // + if (CompareGuid (&gST->ConfigurationTable[Index].VendorGuid, &gEfiAcpi10TableGuid)) { + Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *) gST->ConfigurationTable[Index].VendorTable; + DEBUG ((DEBUG_VERBOSE, "Found ACPI 1.0 RSDP table %p\n", Rsdp)); + } + } + + // + // Try to use legacy search as a last resort. + // + if (Rsdp == NULL) { + Rsdp = AcpiFindLegacyRsdp (); + if (Rsdp != NULL) { + DEBUG ((DEBUG_VERBOSE, "Found ACPI legacy RSDP table %p\n", Rsdp)); + } + } + + if (Rsdp == NULL) { + DEBUG ((DEBUG_WARN, "Failed to find ACPI RSDP table\n")); + } + + return Rsdp; +} + +STATIC +EFI_STATUS +AcpiReplaceDsdt ( + IN OUT OC_ACPI_CONTEXT *Context, + IN CONST UINT8 *Data, + IN UINT32 Length + ) +{ + DEBUG ((DEBUG_WARN, "Patching DSDT is not yet supported\n")); + return EFI_UNSUPPORTED; +} + +EFI_STATUS +AcpiInitContext ( + IN OUT OC_ACPI_CONTEXT *Context + ) +{ + UINT32 Index; + UINT32 DstIndex; + + ZeroMem (Context, sizeof (*Context)); + + Context->Rsdp = AcpiFindRsdp (); + + if (Context->Rsdp == NULL) { + return EFI_NOT_FOUND; + } + + // + // Support RSDT on ACPI 1.0 and newer. + // + Context->Rsdt = (OC_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_TABLE *)(UINTN) Context->Rsdp->RsdtAddress; + DEBUG ((DEBUG_VERBOSE, "Found ACPI RSDT table %p", Context->Rsdt)); + + // + // ACPI 2.0 and newer have XSDT as well. + // + if (Context->Rsdp->Revision > 0) { + Context->Xsdt = (OC_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE *)(UINTN) Context->Rsdp->XsdtAddress; + DEBUG ((DEBUG_VERBOSE, "Found ACPI XSDT table %p", Context->Xsdt)); + } + + if (Context->Rsdt == NULL && Context->Xsdt == NULL) { + DEBUG ((DEBUG_WARN, "Failed to find ACPI RSDT or XSDT tables\n")); + return EFI_NOT_FOUND; + } + + if (Context->Xsdt != NULL) { + Context->NumberOfTables = (Context->Xsdt->Header.Length - sizeof (Context->Xsdt->Header)) + / sizeof (Context->Xsdt->Tables[0]); + } else { + Context->NumberOfTables = (Context->Rsdt->Header.Length - sizeof (Context->Rsdt->Header)) + / sizeof (Context->Rsdt->Tables[0]); + } + + DEBUG ((DEBUG_INFO, "Found %u ACPI tables\n", Context->NumberOfTables)); + + if (Context->NumberOfTables == 0) { + DEBUG ((DEBUG_WARN, "No ACPI tables are available\n")); + return EFI_INVALID_PARAMETER; + } + + Context->Tables = AllocatePool (Context->NumberOfTables * sizeof (Context->Tables[0])); + if (Context->Tables == NULL) { + DEBUG ((DEBUG_WARN, "Cannot allocate space for %u ACPI tables\n", Context->NumberOfTables)); + return EFI_OUT_OF_RESOURCES; + } + + Context->AllocatedTables = Context->NumberOfTables; + + for (DstIndex = 0, Index = 0; Index < Context->NumberOfTables; ++Index) { + Context->Tables[DstIndex] = (EFI_ACPI_COMMON_HEADER *)(Context->Xsdt != NULL + ? Context->Xsdt->Tables[Index] : (UINT64) Context->Rsdt->Tables[Index]); + + // + // Skip NULL table entries if any. + // + if (Context->Tables[DstIndex] == NULL) { + continue; + } + + DEBUG (( + DEBUG_INFO, + "Detected table %08x of %u bytes at index %u\n", + Context->Tables[DstIndex]->Signature, + Context->Tables[DstIndex]->Length, + Index + )); + + ++DstIndex; + } + + if (Context->NumberOfTables != DstIndex) { + DEBUG ((DEBUG_WARN, "Only %u ACPI tables out of %u were valid\n", DstIndex, Context->NumberOfTables)); + Context->NumberOfTables = DstIndex; + } + + return EFI_SUCCESS; +} + +VOID +AcpiFreeContext ( + IN OUT OC_ACPI_CONTEXT *Context + ) +{ + if (Context->Tables != NULL) { + FreePool (Context->Tables); + Context->Tables = NULL; + } +} + +EFI_STATUS +AcpiApplyContext ( + IN OUT OC_ACPI_CONTEXT *Context + ) +{ + EFI_STATUS Status; + UINT32 XsdtSize; + UINT32 RsdtSize; + UINT32 Index; + UINT32 Size; + EFI_PHYSICAL_ADDRESS Table; + + XsdtSize = Context->Xsdt == NULL ? 0 : sizeof (*Context->Xsdt) + sizeof (Context->Xsdt->Tables[0]) * Context->NumberOfTables; + RsdtSize = Context->Rsdt == NULL ? 0 : sizeof (*Context->Rsdt) + sizeof (Context->Rsdt->Tables[0]) * Context->NumberOfTables; + Size = ALIGN_VALUE (XsdtSize, sizeof (UINT64)) + ALIGN_VALUE (RsdtSize, sizeof (UINT64)); + + Table = BASE_4GB - 1; + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiACPIMemoryNVS, + EFI_SIZE_TO_PAGES (Size), + &Table + ); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_WARN, "Failed to allocate %u bytes for ACPI system tables\n", Size)); + return Status; + } + + if (Context->Xsdt != NULL) { + CopyMem ((VOID *) Table, Context->Xsdt, sizeof (*Context->Xsdt)); + Context->Xsdt = (OC_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE *) Table; + Context->Xsdt->Header.Length = XsdtSize; + + for (Index = 0; Index < Context->NumberOfTables; ++Index) { + Context->Xsdt->Tables[Index] = (UINT64)(UINTN) Context->Tables[Index]; + } + + Context->Xsdt->Header.Checksum = 0; + Context->Xsdt->Header.Checksum = CalculateCheckSum8 ( + (UINT8 *) Context->Xsdt, + Context->Xsdt->Header.Length + ); + + Context->Rsdp->XsdtAddress = (UINT64) Table; + Table += ALIGN_VALUE (XsdtSize, sizeof (UINT64)); + } + + if (Context->Rsdt != NULL) { + CopyMem ((VOID *) Table, Context->Rsdt, sizeof (*Context->Rsdt)); + Context->Rsdt = (OC_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_TABLE *) Table; + Context->Rsdt->Header.Length = RsdtSize; + + for (Index = 0; Index < Context->NumberOfTables; ++Index) { + Context->Rsdt->Tables[Index] = (UINT32)(UINTN) Context->Tables[Index]; + } + + Context->Rsdt->Header.Checksum = 0; + Context->Rsdt->Header.Checksum = CalculateCheckSum8 ( + (UINT8 *) Context->Rsdt, + Context->Rsdt->Header.Length + ); + + Table += ALIGN_VALUE (RsdtSize, sizeof (UINT64)); + } + + Context->Rsdp->Checksum = 0; + Context->Rsdp->Checksum = CalculateCheckSum8 ( + (UINT8 *) Context->Rsdp, + Context->Xsdt != NULL ? Context->Rsdp->Length : 20 + ); + + return EFI_UNSUPPORTED; +} + +EFI_STATUS +AcpiDropTable ( + IN OUT OC_ACPI_CONTEXT *Context, + IN UINT32 Signature, + IN UINT32 Length, + IN UINT64 OemTableId + ) +{ + UINT32 Index; + UINT64 CurrOemTableId; + + for (Index = 0; Index < Context->NumberOfTables; ++Index) { + if ((Signature == 0 || Context->Tables[Index]->Signature == Signature) + && (Length == 0 || Context->Tables[Index]->Length == Length)) { + + if (Context->Tables[Index]->Length >= sizeof (EFI_ACPI_DESCRIPTION_HEADER)) { + CurrOemTableId = ((EFI_ACPI_DESCRIPTION_HEADER *) Context->Tables[Index])->OemTableId; + } else { + CurrOemTableId = 0; + } + + if (OemTableId != 0 && CurrOemTableId != OemTableId) { + continue; + } + + DEBUG (( + DEBUG_INFO, + "Dropping table %08x of %u bytes with %016Lx ID at index %u\n", + Context->Tables[Index]->Signature, + Context->Tables[Index]->Length, + CurrOemTableId, + Index + )); + + CopyMem ( + &Context->Tables[Index], + &Context->Tables[Index+1], + Context->NumberOfTables - Index - 1 + ); + --Context->NumberOfTables; + return EFI_SUCCESS; + } + } + + return EFI_NOT_FOUND; +} + +EFI_STATUS +AcpiInsertTable ( + IN OUT OC_ACPI_CONTEXT *Context, + IN CONST UINT8 *Data, + IN UINT32 Length + ) +{ + EFI_ACPI_COMMON_HEADER *Common; + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Table; + EFI_ACPI_COMMON_HEADER **NewTables; + + if (Length < sizeof (EFI_ACPI_COMMON_HEADER)) { + DEBUG ((DEBUG_WARN, "Inserted ACPI table is only %u bytes, ignoring\n", Length)); + return EFI_INVALID_PARAMETER; + } + + Common = (EFI_ACPI_COMMON_HEADER *) Data; + if (Common->Length != Length) { + DEBUG ((DEBUG_WARN, "Inserted ACPI table has length mismatch %u vs %u, ignoring\n", Length, Common->Length)); + return EFI_INVALID_PARAMETER; + } + + if (Common->Signature == EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) { + return AcpiReplaceDsdt (Context, Data, Length); + } + + if (Context->NumberOfTables == Context->AllocatedTables) { + NewTables = AllocatePool ((Context->NumberOfTables + 2) * sizeof (Context->Tables[0])); + if (NewTables == NULL) { + DEBUG ((DEBUG_WARN, "Cannot allocate space for new %u ACPI tables\n", Context->NumberOfTables+2)); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (NewTables, Context->Tables, Context->NumberOfTables * sizeof (Context->Tables[0])); + FreePool (Context->Tables); + + Context->Tables = NewTables; + Context->AllocatedTables += 2; + } + + Table = BASE_4GB - 1; + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiACPIMemoryNVS, + EFI_SIZE_TO_PAGES (Length), + &Table + ); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_WARN, "Failed to allocate %u bytes for inserted ACPI table\n", Length)); + return Status; + } + + CopyMem ((UINT8 *) Table, Data, Length); + ZeroMem ((UINT8 *) Table + Length, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Length)) - Length); + + DEBUG (( + DEBUG_INFO, + "Inserted table %08x of %u bytes into ACPI at index %u\n", + Common->Signature, + Common->Length, + Context->NumberOfTables + )); + + Context->Tables[Context->NumberOfTables] = (EFI_ACPI_COMMON_HEADER *) Table; + ++Context->NumberOfTables; + + return EFI_UNSUPPORTED; +} + +VOID +AcpiNormalizeHeaders ( + IN OUT OC_ACPI_CONTEXT *Context + ) +{ + UINT32 Index; + + for (Index = 0; Index < Context->NumberOfTables; ++Index) { + if (Context->Tables[Index]->Length >= sizeof (EFI_ACPI_COMMON_HEADER)) { + // TODO: normalize headers (https://alextjam.es/debugging-appleacpiplatform/) + } + } +} diff --git a/Library/OcAcpiLib/OcAcpiLib.inf b/Library/OcAcpiLib/OcAcpiLib.inf index cccb66ca..8df22417 100755 --- a/Library/OcAcpiLib/OcAcpiLib.inf +++ b/Library/OcAcpiLib/OcAcpiLib.inf @@ -36,7 +36,9 @@ [LibraryClasses] BaseLib +[Guids] + gEfiAcpi10TableGuid + gEfiAcpi20TableGuid + [Sources] - AcpiLocateTable.c - AcpiFindLegacyRsdPtr.c - AcpiFindRsdPtr.c \ No newline at end of file + OcAcpiLib.c diff --git a/Library/OcSmbiosLib/SmbiosPatch.c b/Library/OcSmbiosLib/SmbiosPatch.c index 46469347..57ac97a3 100755 --- a/Library/OcSmbiosLib/SmbiosPatch.c +++ b/Library/OcSmbiosLib/SmbiosPatch.c @@ -697,7 +697,11 @@ PatchMemoryDevice ( SMBIOS_OVERRIDE_V (Table, Standard.Type17->TotalWidth, Original, NULL, NULL); SMBIOS_OVERRIDE_V (Table, Standard.Type17->DataWidth, Original, NULL, NULL); SMBIOS_OVERRIDE_V (Table, Standard.Type17->Size, Original, NULL, NULL); - Table->CurrentPtr.Standard.Type17->FormFactor = Data->MemoryFormFactor; + if (Data->MemoryFormFactor != 0) { + Table->CurrentPtr.Standard.Type17->FormFactor = Data->MemoryFormFactor; + } else { + Table->CurrentPtr.Standard.Type17->FormFactor = MemoryFormFactorSodimm; + } SMBIOS_OVERRIDE_V (Table, Standard.Type17->DeviceSet, Original, NULL, NULL); SMBIOS_OVERRIDE_S (Table, Standard.Type17->DeviceLocator, Original, NULL, &StringIndex, NULL); SMBIOS_OVERRIDE_S (Table, Standard.Type17->BankLocator, Original, NULL, &StringIndex, NULL); @@ -996,7 +1000,11 @@ CreateAppleProcessorType ( return; } - Table->CurrentPtr.Type131->ProcessorType.Type = CpuInfo->AppleProcessorType; + if (Data->ProcessorType != AppleProcessorTypeUnknown) { + Table->CurrentPtr.Type131->ProcessorType.Type = Data->ProcessorType; + } else { + Table->CurrentPtr.Type131->ProcessorType.Type = CpuInfo->AppleProcessorType; + } SmbiosFinaliseStruct (Table); } diff --git a/OcSupportPkg.dsc b/OcSupportPkg.dsc index 7df5ad13..72d0006c 100644 --- a/OcSupportPkg.dsc +++ b/OcSupportPkg.dsc @@ -102,6 +102,8 @@ OcSupportPkg/Tests/PropertyTest/PropertyTestApp.inf OcSupportPkg/Tests/SmbiosTest/SmbiosTest.inf OcSupportPkg/Tests/SmbiosTest/SmbiosTestApp.inf + OcSupportPkg/Tests/AcpiTest/AcpiTest.inf + OcSupportPkg/Tests/AcpiTest/AcpiTestApp.inf [PcdsFixedAtBuild] gEfiMdePkgTokenSpaceGuid.PcdMaximumAsciiStringLength|0 diff --git a/Tests/AcpiTest/AcpiTest.c b/Tests/AcpiTest/AcpiTest.c new file mode 100644 index 00000000..c85f7365 --- /dev/null +++ b/Tests/AcpiTest/AcpiTest.c @@ -0,0 +1,116 @@ +/** @file + Test acpi support. + +Copyright (c) 2018, vit9696. All rights reserved.
+This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +UINT8 PatchedSsdt8[] = { + 0x53, 0x53, 0x44, 0x54, 0x25, 0x01, 0x00, 0x00, 0x02, 0x60, 0x41, 0x50, 0x50, 0x4C, 0x45, 0x20, + 0x53, 0x73, 0x64, 0x74, 0x45, 0x43, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, + 0x10, 0x12, 0x16, 0x20, 0xA0, 0x1C, 0x00, 0x15, 0x5C, 0x2F, 0x03, 0x5F, 0x53, 0x42, 0x5F, 0x50, + 0x43, 0x49, 0x30, 0x4C, 0x50, 0x43, 0x42, 0x06, 0x00, 0x15, 0x5C, 0x55, 0x4D, 0x41, 0x50, 0x01, + 0x00, 0x10, 0x43, 0x0E, 0x5C, 0x5F, 0x53, 0x42, 0x5F, 0x5B, 0x82, 0x4F, 0x08, 0x55, 0x53, 0x42, + 0x58, 0x08, 0x5F, 0x41, 0x44, 0x52, 0x00, 0x14, 0x42, 0x08, 0x5F, 0x44, 0x53, 0x4D, 0x04, 0xA0, + 0x09, 0x93, 0x6A, 0x00, 0xA4, 0x11, 0x03, 0x01, 0x03, 0xA4, 0x12, 0x4F, 0x06, 0x08, 0x0D, 0x6B, + 0x55, 0x53, 0x42, 0x53, 0x6C, 0x65, 0x65, 0x70, 0x50, 0x6F, 0x77, 0x65, 0x72, 0x53, 0x75, 0x70, + 0x70, 0x6C, 0x79, 0x00, 0x0B, 0xEC, 0x13, 0x0D, 0x6B, 0x55, 0x53, 0x42, 0x53, 0x6C, 0x65, 0x65, + 0x70, 0x50, 0x6F, 0x72, 0x74, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6E, 0x74, 0x4C, 0x69, 0x6D, 0x69, + 0x74, 0x00, 0x0B, 0x34, 0x08, 0x0D, 0x6B, 0x55, 0x53, 0x42, 0x57, 0x61, 0x6B, 0x65, 0x50, 0x6F, + 0x77, 0x65, 0x72, 0x53, 0x75, 0x70, 0x70, 0x6C, 0x79, 0x00, 0x0B, 0xEC, 0x13, 0x0D, 0x6B, 0x55, + 0x53, 0x42, 0x57, 0x61, 0x6B, 0x65, 0x50, 0x6F, 0x72, 0x74, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6E, + 0x74, 0x4C, 0x69, 0x6D, 0x69, 0x74, 0x00, 0x0B, 0x34, 0x08, 0x10, 0x4A, 0x04, 0x5C, 0x2F, 0x03, + 0x5F, 0x53, 0x42, 0x5F, 0x50, 0x43, 0x49, 0x30, 0x4C, 0x50, 0x43, 0x42, 0x5B, 0x82, 0x37, 0x45, + 0x43, 0x5F, 0x5F, 0x08, 0x5F, 0x48, 0x49, 0x44, 0x0C, 0x41, 0xD0, 0x0C, 0x09, 0x08, 0x5F, 0x55, + 0x49, 0x44, 0x00, 0x08, 0x5F, 0x43, 0x52, 0x53, 0x11, 0x15, 0x0A, 0x12, 0x47, 0x01, 0x62, 0x00, + 0x62, 0x00, 0x00, 0x01, 0x47, 0x01, 0x66, 0x00, 0x66, 0x00, 0x00, 0x01, 0x79, 0x00, 0x08, 0x5F, + 0x47, 0x50, 0x45, 0x0A, 0x17 +}; + + +EFI_STATUS +EFIAPI +TestAcpi ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + OC_ACPI_CONTEXT Context; + + Status = AcpiInitContext (&Context); + + if (!EFI_ERROR (Status)) { + AcpiDropTable (&Context, EFI_ACPI_6_2_DMA_REMAPPING_TABLE_SIGNATURE, 0, 0); + AcpiDropTable (&Context, EFI_ACPI_6_2_WINDOWS_SMM_SECURITY_MITIGATION_TABLE_SIGNATURE, 0, 0); + AcpiDropTable (&Context, EFI_ACPI_6_2_WINDOWS_ACPI_EMULATED_DEVICES_TABLE_SIGNATURE, 0, 0); + + AcpiInsertTable (&Context, PatchedSsdt8, sizeof (PatchedSsdt8)); + + AcpiApplyContext (&Context); + + AcpiFreeContext (&Context); + } + + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +UefiMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + UINTN Index; + + WaitForKeyPress (L"Press any key..."); + + for (Index = 0; Index < SystemTable->NumberOfTableEntries; ++Index) { + Print (L"Table %u is %g\n", (UINT32) Index, &SystemTable->ConfigurationTable[Index].VendorGuid); + } + + Print (L"This is test app...\n"); + + TestAcpi (ImageHandle, SystemTable); + + return EFI_SUCCESS; +} diff --git a/Tests/AcpiTest/AcpiTest.inf b/Tests/AcpiTest/AcpiTest.inf new file mode 100644 index 00000000..bb2ad66f --- /dev/null +++ b/Tests/AcpiTest/AcpiTest.inf @@ -0,0 +1,66 @@ +## @file +# General purpose test application. +# +# Copyright (c) 2019, vit9696. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = AcpiTest + FILE_GUID = 469FA667-C311-4ED3-BDA5-97789B837AE3 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + INF_VERSION = 0x00010005 + EDK_RELEASE_VERSION = 0x00020000 + EFI_SPECIFICATION_VERSION = 0x00010000 + ENTRY_POINT = TestAcpi + +# +# This flag specifies whether HII resource section is generated into PE image. +# + UEFI_HII_RESOURCE_SECTION = TRUE + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 IPF EBC +# + +[Sources] + AcpiTest.c + +[Packages] + OcSupportPkg/OcSupportPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + EfiPkg/EfiPkg.dec + +[Protocols] + gEfiMpServiceProtocolGuid ## CONSUMES + +[LibraryClasses] + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + UefiBootServicesTableLib + UefiLib + PcdLib + IoLib + PrintLib + OcGuardLib + DevicePathLib + OcDevicePropertyLib + OcMiscLib + OcProtocolLib + OcAppleBootPolicyLib + OcSmbiosLib + OcVariableLib + OcAcpiLib diff --git a/Tests/AcpiTest/AcpiTestApp.inf b/Tests/AcpiTest/AcpiTestApp.inf new file mode 100644 index 00000000..bf72a323 --- /dev/null +++ b/Tests/AcpiTest/AcpiTestApp.inf @@ -0,0 +1,63 @@ +## @file +# General purpose test application. +# +# Copyright (c) 2019, vit9696. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = AcpiTestApp + FILE_GUID = C4BE955C-47DD-427C-A38A-FE18DFF4F8FD + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = UefiMain + +# +# This flag specifies whether HII resource section is generated into PE image. +# + UEFI_HII_RESOURCE_SECTION = TRUE + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 IPF EBC +# + +[Sources] + AcpiTest.c + +[Packages] + OcSupportPkg/OcSupportPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + EfiPkg/EfiPkg.dec + +[Protocols] + gEfiMpServiceProtocolGuid ## CONSUMES + +[LibraryClasses] + UefiApplicationEntryPoint + UefiRuntimeServicesTableLib + UefiBootServicesTableLib + UefiLib + PcdLib + IoLib + PrintLib + OcGuardLib + DevicePathLib + OcDevicePropertyLib + OcMiscLib + OcProtocolLib + OcAppleBootPolicyLib + OcSmbiosLib + OcVariableLib + OcAcpiLib diff --git a/Tests/SmbiosTest/SmbiosTest.c b/Tests/SmbiosTest/SmbiosTest.c index ef323ff6..59dff560 100644 --- a/Tests/SmbiosTest/SmbiosTest.c +++ b/Tests/SmbiosTest/SmbiosTest.c @@ -72,7 +72,6 @@ static OC_SMBIOS_DATA Data = { .ExtendedFirmwareFeatures = 0xE00FE137, .ExtendedFirmwareFeaturesMask = 0xFF1FFF3F, .ProcessorType = 0, // Will be calculated automatically - .ProcessorBusSpeed = 0, // unused .PlatformFeature = 1 }; diff --git a/TestsUser/Smbios/Smbios.c b/TestsUser/Smbios/Smbios.c index 9f6b81c9..94e7eddd 100644 --- a/TestsUser/Smbios/Smbios.c +++ b/TestsUser/Smbios/Smbios.c @@ -84,7 +84,6 @@ static OC_SMBIOS_DATA SmbiosData = { .ExtendedFirmwareFeatures = 0xE00FE137, .ExtendedFirmwareFeaturesMask = 0xFF1FFF3F, .ProcessorType = 0, // Will be calculated automatically - .ProcessorBusSpeed = 0, // unused .PlatformFeature = 1 };