16#include <Library/BaseLib.h>
17#include <Library/BaseMemoryLib.h>
19#include <Library/MemoryAllocationLib.h>
20#include <Library/UefiLib.h>
21#include <Library/UefiBootServicesTableLib.h>
24#include <Library/PciLib.h>
26#include <IndustryStandard/AcpiAml.h>
27#include <IndustryStandard/Acpi.h>
33#define PCI_VENDOR_NVIDIA 0x10DE
41EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *
46 EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp;
57 for (Address = 0x0E0000; Address < 0x0FFFFF; Address += 16) {
58 if (*(UINT64 *)Address == EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) {
59 Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *)Address;
68 Address = ((UINTN)(*(UINT16 *)0x040E) << 4U);
70 for (Index = 0; Index < 0x0400; Index += 16) {
71 if (*(UINT64 *)(Address + Index) == EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE) {
72 Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *)Address;
86EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *
91 EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp;
100 for (Index = 0; Index <
gST->NumberOfTableEntries; ++Index) {
104 if (
CompareGuid (&
gST->ConfigurationTable[Index].VendorGuid, &gEfiAcpi20TableGuid)) {
105 Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *)
gST->ConfigurationTable[Index].VendorTable;
106 DEBUG ((DEBUG_VERBOSE,
"OCA: Found ACPI 2.0 RSDP table %p\n", Rsdp));
113 if (
CompareGuid (&
gST->ConfigurationTable[Index].VendorGuid, &gEfiAcpi10TableGuid)) {
114 Rsdp = (EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *)
gST->ConfigurationTable[Index].VendorTable;
115 DEBUG ((DEBUG_VERBOSE,
"OCA: Found ACPI 1.0 RSDP table %p\n", Rsdp));
125 DEBUG ((DEBUG_VERBOSE,
"OCA: Found ACPI legacy RSDP table %p\n", Rsdp));
130 DEBUG ((DEBUG_WARN,
"OCA: Failed to find ACPI RSDP table\n"));
146 IN CONST EFI_ACPI_COMMON_HEADER *Common
153 Result = Common->Signature;
155 Walker = (CHAR8 *)&Result;
156 for (Index = 0; Index <
sizeof (Common->Signature); Index++) {
175 IN CONST EFI_ACPI_COMMON_HEADER *Common
178 if (Common->Length <= sizeof (EFI_ACPI_DESCRIPTION_HEADER)) {
182 return ((EFI_ACPI_DESCRIPTION_HEADER *)Common)->OemTableId;
197 IN CONST UINT8 *Data,
199 OUT UINT32 *NameOffset OPTIONAL
208 Off = Data[0] ==
'\\' ? 1 : 0;
211 if ( (Data[Index] <
'/')
212 || ((Data[Index] >
'9') && (Data[Index] <
'A'))
213 || ((Data[Index] >
'Z') && (Data[Index] !=
'_')))
218 Name[Index - Off] = Data[Index];
223 if (NameOffset != NULL) {
242 IN CONST UINT8 *Data,
257 if ( (Data[Index] == AML_NAME_OP)
258 && (Data[Index+1] == Name[0])
259 && (Data[Index+2] == Name[1])
260 && (Data[Index+3] == Name[2])
261 && (Data[Index+4] == Name[3]))
273 IN EFI_ACPI_DESCRIPTION_HEADER *Table
276 UINT32 TablePrintSignature;
280 Table->Checksum = CalculateCheckSum8 ((UINT8 *)Table, Table->Length);
284 "OCA: Refreshed %.4a checksum to %02x\n",
285 (CHAR8 *)&TablePrintSignature,
293 IN EFI_ACPI_COMMON_HEADER *Table
296 UINT32 LengthOriginal;
298 LengthOriginal = Table->Length;
299 (*(
volatile UINT32 *)&Table->Length)++;
301 if (LengthOriginal == *(
volatile UINT32 *)&Table->Length) {
305 Table->Length = LengthOriginal;
312 IN EFI_ACPI_COMMON_HEADER *Table,
313 IN UINT32 NewSize OPTIONAL,
314 OUT EFI_ACPI_COMMON_HEADER **NewTable
318 UINT32 TablePrintSignature;
319 EFI_PHYSICAL_ADDRESS NewTableAddress;
321 if (NewSize < Table->
Length) {
322 NewSize = Table->Length;
327 NewTableAddress = BASE_4GB - 1;
328 Status =
gBS->AllocatePages (
331 EFI_SIZE_TO_PAGES (NewSize),
334 if (EFI_ERROR (Status)) {
337 "OCA: Failed to allocate %u bytes for table %.4a\n",
339 (CHAR8 *)&TablePrintSignature
344 *NewTable = (EFI_ACPI_COMMON_HEADER *)(UINTN)NewTableAddress;
348 "OCA: Allocated new table %.4a at %p\n",
349 (CHAR8 *)&TablePrintSignature,
353 CopyMem (*NewTable, Table, Table->Length);
354 ZeroMem (((UINT8 *)*NewTable) + Table->Length, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (NewSize)) - Table->Length);
355 (*NewTable)->Length = NewSize;
368 EFI_ACPI_COMMON_HEADER *NewFadt;
372 (EFI_ACPI_COMMON_HEADER *)Context->Fadt,
376 if (EFI_ERROR (Status)) {
380 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
384 if (Context->Tables[Index]->Signature == EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
385 Context->Tables[Index] = NewFadt;
389 Context->Fadt = (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE *)NewFadt;
398 IN EFI_ACPI_DESCRIPTION_HEADER *NewDsdt OPTIONAL
402 EFI_ACPI_COMMON_HEADER *AllocatedDsdt;
405 (EFI_ACPI_COMMON_HEADER *)(NewDsdt != NULL ? NewDsdt : Context->Dsdt),
409 if (EFI_ERROR (Status)) {
418 if (EFI_ERROR (Status)) {
419 FreePool (AllocatedDsdt);
424 if (Context->Fadt->Header.Length >= OFFSET_OF (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE, XDsdt) +
sizeof (Context->Fadt->XDsdt)) {
425 Context->Fadt->XDsdt = (UINT64)(UINTN)AllocatedDsdt;
428 Context->Fadt->Dsdt = (UINT32)(UINTN)AllocatedDsdt;
431 Context->Dsdt = (EFI_ACPI_DESCRIPTION_HEADER *)AllocatedDsdt;
448 IN EFI_ACPI_COMMON_HEADER *Table
462 BufferLen = Table->Length;
464 if (BufferLen <
sizeof (EFI_ACPI_DESCRIPTION_HEADER)) {
468 for (Index =
sizeof (EFI_ACPI_DESCRIPTION_HEADER); Index < BufferLen - 0xF; ++Index) {
469 if ( (
Buffer[Index] == AML_EXT_OP)
470 && (
Buffer[Index+1] == AML_EXT_REGION_OP)
484 if ((Index2 > 0) && (Index2 < BufferLen - 0xF)) {
493 for (Index2 = 0; Index2 < Context->NumberOfRegions; ++Index2) {
494 if (Context->Regions[Index2].Address == Address) {
501 if (Context->AllocatedRegions == Context->NumberOfRegions) {
502 NewRegions = AllocatePool ((Context->AllocatedRegions + 2) * sizeof (Context->Regions[0]));
503 if (NewRegions == NULL) {
504 DEBUG ((DEBUG_WARN,
"OCA: Failed to allocate memory for %u regions\n", Context->AllocatedRegions+2));
505 return EFI_OUT_OF_RESOURCES;
508 CopyMem (NewRegions, Context->Regions, Context->NumberOfRegions * sizeof (Context->Regions[0]));
509 FreePool (Context->Regions);
511 Context->Regions = NewRegions;
512 Context->AllocatedRegions += 2;
515 DEBUG ((DEBUG_INFO,
"OCA: Found OperationRegion %a at %08X\n", Name, Address));
516 Context->Regions[Context->NumberOfRegions].Address = Address;
517 CopyMem (&Context->Regions[Context->NumberOfRegions].Name[0], &Name[0], sizeof (Name));
518 ++Context->NumberOfRegions;
536 IN EFI_ACPI_COMMON_HEADER *Table
551 BufferLen = Table->Length;
554 if (BufferLen <
sizeof (EFI_ACPI_DESCRIPTION_HEADER)) {
558 for (Index =
sizeof (EFI_ACPI_DESCRIPTION_HEADER); Index < BufferLen - 0xF; ++Index) {
559 if ( (
Buffer[Index] == AML_EXT_OP)
560 && (
Buffer[Index+1] == AML_EXT_REGION_OP)
567 for (RegionIndex = 0; RegionIndex < Context->NumberOfRegions; ++RegionIndex) {
568 if (AsciiStrCmp (Context->Regions[RegionIndex].Name, Name) == 0) {
580 if ((Index2 > 0) && (Index2 < BufferLen - 0xF)) {
593 if (Modified && (OldAddress != Context->Regions[RegionIndex].Address)) {
596 "OCA: Region %a address relocated from %08X to %08X\n",
597 Context->Regions[RegionIndex].Name,
599 Context->Regions[RegionIndex].Address
613 ((EFI_ACPI_DESCRIPTION_HEADER *)Table)->Checksum = 0;
614 ((EFI_ACPI_DESCRIPTION_HEADER *)Table)->Checksum = CalculateCheckSum8 (
630 IN EFI_ACPI_DESCRIPTION_HEADER *Table
637 if (Table->Length < sizeof (EFI_ACPI_DESCRIPTION_HEADER)) {
643 Walker = (CHAR8 *)&Table->Signature;
644 for (Index = 0; Index <
sizeof (Table->Signature); ++Index) {
651 Walker = (CHAR8 *)&Table->OemId;
652 for (Index = 0; Index <
sizeof (Table->OemId); ++Index) {
659 Walker = (CHAR8 *)&Table->OemTableId;
660 for (Index = 0; Index <
sizeof (Table->OemTableId); ++Index) {
667 Walker = (CHAR8 *)&Table->CreatorId;
668 for (Index = 0; Index <
sizeof (Table->CreatorId); ++Index) {
677 Table->Checksum = CalculateCheckSum8 (
696 IN EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp,
706 Walker = (CHAR8 *)&Rsdp->OemId;
707 for (Index = 0; Index <
sizeof (Rsdp->OemId); ++Index) {
720 Rsdp->Checksum = CalculateCheckSum8 (
726 Rsdp->ExtendedChecksum = 0;
727 Rsdp->ExtendedChecksum = CalculateCheckSum8 (
744 UINT32 TablePrintSignature;
746 ZeroMem (Context,
sizeof (*Context));
750 if (Context->Rsdp == NULL) {
751 return EFI_NOT_FOUND;
758 DEBUG ((DEBUG_VERBOSE,
"OCA: Found ACPI RSDT table %p", Context->Rsdt));
763 if (Context->Rsdp->Revision > 0) {
765 DEBUG ((DEBUG_VERBOSE,
"OCA: Found ACPI XSDT table %p", Context->Xsdt));
768 if ((Context->Rsdt == NULL) && (Context->Xsdt == NULL)) {
769 DEBUG ((DEBUG_WARN,
"OCA: Failed to find ACPI RSDT or XSDT tables\n"));
770 return EFI_NOT_FOUND;
773 if (Context->Xsdt != NULL) {
774 Context->NumberOfTables = (Context->Xsdt->Header.Length -
sizeof (Context->Xsdt->Header))
775 /
sizeof (Context->Xsdt->Tables[0]);
777 Context->NumberOfTables = (Context->Rsdt->Header.Length -
sizeof (Context->Rsdt->Header))
778 /
sizeof (Context->Rsdt->Tables[0]);
781 DEBUG ((DEBUG_INFO,
"OCA: Found %u ACPI tables\n", Context->NumberOfTables));
783 if (Context->NumberOfTables == 0) {
784 DEBUG ((DEBUG_WARN,
"OCA: No ACPI tables are available\n"));
785 return EFI_INVALID_PARAMETER;
788 Context->Tables = AllocatePool (Context->NumberOfTables * sizeof (Context->Tables[0]));
789 if (Context->Tables == NULL) {
790 DEBUG ((DEBUG_WARN,
"OCA: Cannot allocate space for %u ACPI tables\n", Context->NumberOfTables));
791 return EFI_OUT_OF_RESOURCES;
794 Context->AllocatedTables = Context->NumberOfTables;
796 for (DstIndex = 0, Index = 0; Index < Context->NumberOfTables; ++Index) {
797 Context->Tables[DstIndex] = (EFI_ACPI_COMMON_HEADER *)(Context->Xsdt != NULL
798 ? (UINTN)Context->Xsdt->Tables[Index] : (UINTN)Context->Rsdt->Tables[Index]);
803 if ( (Context->Tables[DstIndex] == NULL)
804 || (Context->Tables[DstIndex]->Signature == EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE)
805 || (*(UINT64 *)Context->Tables[DstIndex] == EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE))
814 "OCA: Detected table %.4a (%08x) (OEM %016Lx) at %p of %u bytes at index %u\n",
815 (CHAR8 *)&TablePrintSignature,
816 Context->Tables[DstIndex]->Signature,
818 Context->Tables[DstIndex],
819 Context->Tables[DstIndex]->Length,
826 if ((UINTN)Context->Tables[DstIndex] < BASE_1MB) {
829 "OCA: Unlocking table %.4a (%08x) at %p\n",
830 (CHAR8 *)&TablePrintSignature,
831 Context->Tables[DstIndex]->Signature,
832 Context->Tables[DstIndex]
834 LegacyRegionUnlock ((UINT32)(UINTN)Context->Tables[DstIndex], Context->Tables[DstIndex]->Length);
837 if (Context->Tables[DstIndex]->Signature == EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
838 Context->Fadt = (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE *)Context->Tables[DstIndex];
840 if (Context->Fadt->Header.Length >= OFFSET_OF (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE, XDsdt) +
sizeof (Context->Fadt->XDsdt)) {
841 Context->Dsdt = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Context->Fadt->XDsdt;
843 Context->Dsdt = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Context->Fadt->Dsdt;
848 "OCA: Detected DSDT at %p of %u bytes at index %u\n",
850 Context->Dsdt->Length,
854 if ((UINTN)Context->Dsdt < BASE_1MB) {
857 "OCA: Unlocking DSDT at %p\n",
860 LegacyRegionUnlock ((UINT32)(UINTN)Context->Tables[DstIndex], Context->Tables[DstIndex]->Length);
867 if (Context->NumberOfTables != DstIndex) {
868 DEBUG ((DEBUG_INFO,
"OCA: Only %u ACPI tables out of %u were valid\n", DstIndex, Context->NumberOfTables));
869 Context->NumberOfTables = DstIndex;
872 if (Context->Fadt == NULL) {
873 DEBUG ((DEBUG_WARN,
"OCA: Failed to find ACPI FADT table\n"));
876 if (Context->Dsdt == NULL) {
877 DEBUG ((DEBUG_WARN,
"OCA: Failed to find ACPI DSDT table\n"));
888 if (Context->Tables != NULL) {
889 FreePool (Context->Tables);
890 Context->Tables = NULL;
893 if (Context->Regions != NULL) {
894 FreePool (Context->Regions);
895 Context->Regions = NULL;
909 EFI_PHYSICAL_ADDRESS Table;
910 UINT32 TablePrintSignature;
912 XsdtSize = Context->Xsdt == NULL ? 0 :
sizeof (*Context->Xsdt) +
sizeof (Context->Xsdt->Tables[0]) * Context->NumberOfTables;
913 RsdtSize = Context->Rsdt == NULL ? 0 :
sizeof (*Context->Rsdt) +
sizeof (Context->Rsdt->Tables[0]) * Context->NumberOfTables;
914 Size = ALIGN_VALUE (XsdtSize,
sizeof (UINT64)) + ALIGN_VALUE (RsdtSize,
sizeof (UINT64));
916 Table = BASE_4GB - 1;
917 Status =
gBS->AllocatePages (
920 EFI_SIZE_TO_PAGES (
Size),
924 if (EFI_ERROR (Status)) {
925 DEBUG ((DEBUG_WARN,
"OCA: Failed to allocate %u bytes for ACPI system tables\n",
Size));
929 if (Context->Xsdt != NULL) {
930 CopyMem ((VOID *)(UINTN)Table, Context->Xsdt, sizeof (*Context->Xsdt));
932 Context->Xsdt->Header.Length = XsdtSize;
934 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
935 Context->Xsdt->Tables[Index] = (UINT64)(UINTN)Context->Tables[Index];
941 "OCA: Exposing XSDT table table %.4a (%08x) (OEM %016Lx) at %p of %u bytes at index %u\n",
942 (CHAR8 *)&TablePrintSignature,
943 Context->Tables[Index]->Signature,
945 Context->Tables[Index],
946 Context->Tables[Index]->Length,
951 Context->Xsdt->Header.Checksum = 0;
952 Context->Xsdt->Header.Checksum = CalculateCheckSum8 (
953 (UINT8 *)Context->Xsdt,
954 Context->Xsdt->Header.Length
957 Context->Rsdp->XsdtAddress = (UINT64)Table;
958 Table += ALIGN_VALUE (XsdtSize,
sizeof (UINT64));
961 if (Context->Rsdt != NULL) {
962 CopyMem ((VOID *)(UINTN)Table, Context->Rsdt, sizeof (*Context->Rsdt));
964 Context->Rsdt->Header.Length = RsdtSize;
966 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
967 Context->Rsdt->Tables[Index] = (UINT32)(UINTN)Context->Tables[Index];
973 "OCA: Exposing RSDT table table %.4a (%08x) (OEM %016Lx) at %p of %u bytes at index %u\n",
974 (CHAR8 *)&TablePrintSignature,
975 Context->Tables[Index]->Signature,
977 Context->Tables[Index],
978 Context->Tables[Index]->Length,
983 Context->Rsdt->Header.Checksum = 0;
984 Context->Rsdt->Header.Checksum = CalculateCheckSum8 (
985 (UINT8 *)Context->Rsdt,
986 Context->Rsdt->Header.Length
989 Context->Rsdp->RsdtAddress = (UINT32)Table;
990 Table += ALIGN_VALUE (RsdtSize,
sizeof (UINT64));
997 Context->Rsdp->Checksum = 0;
998 Context->Rsdp->Checksum = CalculateCheckSum8 (
999 (UINT8 *)Context->Rsdp,
1003 if (Context->Xsdt != NULL) {
1004 Context->Rsdp->ExtendedChecksum = 0;
1005 Context->Rsdp->ExtendedChecksum = CalculateCheckSum8 (
1006 (UINT8 *)Context->Rsdp,
1007 Context->Rsdp->Length
1019 IN UINT64 OemTableId,
1024 UINT64 CurrOemTableId;
1026 UINT32 TablePrintSignature;
1031 while (Index < Context->NumberOfTables) {
1033 && ((
Length == 0) || (Context->Tables[Index]->Length ==
Length)))
1035 if (Context->Tables[Index]->Length >= sizeof (EFI_ACPI_DESCRIPTION_HEADER)) {
1036 CurrOemTableId = ((EFI_ACPI_DESCRIPTION_HEADER *)Context->Tables[Index])->OemTableId;
1041 if ((OemTableId == 0) || (CurrOemTableId == OemTableId)) {
1046 "OCA: Deleting table %.4a (%08x) (OEM %016Lx) of %u bytes with %016Lx ID at index %u\n",
1047 (CHAR8 *)&TablePrintSignature,
1048 Context->Tables[Index]->Signature,
1050 Context->Tables[Index]->Length,
1056 &Context->Tables[Index],
1057 &Context->Tables[Index+1],
1058 (Context->NumberOfTables - Index - 1) * sizeof (Context->Tables[0])
1060 --Context->NumberOfTables;
1078 return EFI_NOT_FOUND;
1084 IN CONST UINT8 *Data,
1088 EFI_ACPI_COMMON_HEADER *Common;
1090 EFI_ACPI_COMMON_HEADER *NewTable;
1091 EFI_ACPI_COMMON_HEADER **NewTables;
1092 BOOLEAN ReplaceDsdt;
1093 UINT32 TablePrintSignature;
1095 if (
Length <
sizeof (EFI_ACPI_COMMON_HEADER)) {
1096 DEBUG ((DEBUG_WARN,
"OCA: Inserted ACPI table is only %u bytes, ignoring\n",
Length));
1097 return EFI_INVALID_PARAMETER;
1100 Common = (EFI_ACPI_COMMON_HEADER *)Data;
1101 if (Common->Length !=
Length) {
1102 DEBUG ((DEBUG_WARN,
"OCA: Inserted ACPI table has length mismatch %u vs %u, ignoring\n",
Length, Common->Length));
1103 return EFI_INVALID_PARAMETER;
1106 ReplaceDsdt = Common->Signature == EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1108 if (ReplaceDsdt && ((Context->Dsdt == NULL) || (Context->Fadt == NULL))) {
1109 DEBUG ((DEBUG_WARN,
"OCA: We do not have DSDT to replace\n"));
1110 return EFI_INVALID_PARAMETER;
1113 if (!ReplaceDsdt && (Context->NumberOfTables == Context->AllocatedTables)) {
1114 NewTables = AllocatePool ((Context->NumberOfTables + 2) * sizeof (Context->Tables[0]));
1115 if (NewTables == NULL) {
1116 DEBUG ((DEBUG_WARN,
"OCA: Cannot allocate space for new %u ACPI tables\n", Context->NumberOfTables+2));
1117 return EFI_OUT_OF_RESOURCES;
1120 CopyMem (NewTables, Context->Tables, Context->NumberOfTables * sizeof (Context->Tables[0]));
1121 FreePool (Context->Tables);
1123 Context->Tables = NewTables;
1124 Context->AllocatedTables += 2;
1130 "OCA: Replaced DSDT of %u bytes into ACPI\n",
1135 if (EFI_ERROR (Status)) {
1140 if (EFI_ERROR (Status)) {
1148 "OCA: Inserted table %.4a (%08x) (OEM %016Lx) of %u bytes into ACPI at index %u\n",
1149 (CHAR8 *)&TablePrintSignature,
1150 NewTable->Signature,
1153 Context->NumberOfTables
1156 Context->Tables[Context->NumberOfTables] = NewTable;
1157 ++Context->NumberOfTables;
1170 EFI_ACPI_COMMON_HEADER *NewTable;
1171 UINT32 TablePrintSignature;
1174 DEBUG ((DEBUG_INFO,
"OCA: Normalized RSDP\n"));
1177 if (Context->Xsdt != NULL) {
1180 if (EFI_ERROR (Status)) {
1188 DEBUG ((DEBUG_INFO,
"OCA: Normalized XSDT of %u bytes headers\n", Context->Xsdt->Header.Length));
1192 if (Context->Rsdt != NULL) {
1195 if (EFI_ERROR (Status)) {
1203 DEBUG ((DEBUG_INFO,
"OCA: Normalized RSDT of %u bytes headers\n", Context->Rsdt->Header.Length));
1207 if (Context->Dsdt != NULL) {
1210 if (EFI_ERROR (Status)) {
1216 DEBUG ((DEBUG_INFO,
"OCA: Normalized DSDT of %u bytes headers\n", Context->Dsdt->Length));
1220 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
1223 if (EFI_ERROR (Status)) {
1227 Context->Tables[Index] = NewTable;
1235 "OCA: Normalized %.4a (%08x) (OEM %016Lx) of %u bytes headers at index %u\n",
1236 (CHAR8 *)&TablePrintSignature,
1237 Context->Tables[Index]->Signature,
1239 Context->Tables[Index]->Length,
1253 EFI_ACPI_COMMON_HEADER *NewTable;
1256 UINT64 CurrOemTableId;
1257 UINT32 ReplaceCount;
1258 UINT32 ReplaceLimit;
1259 UINT32 TablePrintSignature;
1261 if ( (Context->Dsdt != NULL)
1262 && ((Patch->TableSignature == 0) || (Patch->TableSignature == EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE))
1263 && ((Patch->TableLength == 0) || (Context->Dsdt->Length == Patch->TableLength))
1264 && ((Patch->OemTableId == 0) || (Context->Dsdt->OemTableId == Patch->OemTableId)))
1266 ReplaceLimit = Context->Dsdt->Length;
1267 if (Patch->Limit > 0) {
1268 ReplaceLimit =
MIN (ReplaceLimit, Patch->Limit);
1273 if ((Patch->Base != NULL) && (Patch->Base[0] !=
'\0')) {
1275 (VOID *)Context->Dsdt,
1277 (UINT8)(Patch->BaseSkip + 1),
1279 Context->Dsdt->Length
1281 if (!EFI_ERROR (Status)) {
1282 ReplaceLimit =
MIN (ReplaceLimit, Context->Dsdt->Length - BaseOffset);
1286 "OCA: Patching DSDT of %u bytes failed to find base %a\n",
1287 Context->Dsdt->Length,
1292 Status = EFI_SUCCESS;
1295 if (!EFI_ERROR (Status)) {
1298 if (EFI_ERROR (Status)) {
1309 (UINT8 *)Context->Dsdt + BaseOffset,
1317 "OCA: Patching DSDT of %u bytes with %016Lx ID replaced %u of %u\n",
1324 if (ReplaceCount > 0) {
1330 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
1331 if ( ((Patch->TableSignature == 0) || (Context->Tables[Index]->Signature == Patch->TableSignature))
1332 && ((Patch->TableLength == 0) || (Context->Tables[Index]->Length == Patch->TableLength)))
1334 if (Context->Tables[Index]->Length >= sizeof (EFI_ACPI_DESCRIPTION_HEADER)) {
1335 CurrOemTableId = ((EFI_ACPI_DESCRIPTION_HEADER *)Context->Tables[Index])->OemTableId;
1340 if ((Patch->OemTableId != 0) && (CurrOemTableId != Patch->OemTableId)) {
1344 ReplaceLimit = Context->Tables[Index]->Length;
1345 if (Patch->Limit > 0) {
1346 ReplaceLimit =
MIN (ReplaceLimit, Patch->Limit);
1352 if ((Patch->Base != NULL) && (Patch->Base[0] !=
'\0')) {
1354 (VOID *)Context->Tables[Index],
1356 (UINT8)(Patch->BaseSkip + 1),
1358 Context->Tables[Index]->Length
1360 if (EFI_ERROR (Status)) {
1363 "OCA: Patching %.4a (%08x) (OEM %016Lx) of %u bytes with %016Lx ID failed to find base %a\n",
1364 (CHAR8 *)&TablePrintSignature,
1365 Context->Tables[Index]->Signature,
1367 Context->Tables[Index]->Length,
1374 ReplaceLimit =
MIN (ReplaceLimit, Context->Tables[Index]->Length - BaseOffset);
1379 if (EFI_ERROR (Status)) {
1383 Context->Tables[Index] = NewTable;
1392 (UINT8 *)Context->Tables[Index] + BaseOffset,
1400 "OCA: Patching %.4a (%08x) (OEM %016Lx) of %u bytes with %016Lx ID at %u replaced %u of %u\n",
1401 (CHAR8 *)&TablePrintSignature,
1402 Context->Tables[Index]->Signature,
1404 Context->Tables[Index]->Length,
1411 if ((ReplaceCount > 0) && (Context->Tables[Index]->Length >= sizeof (EFI_ACPI_DESCRIPTION_HEADER))) {
1431 ASSERT (Context->Regions == NULL);
1436 Context->NumberOfRegions = 0;
1437 Context->AllocatedRegions = 8;
1438 Context->Regions = AllocatePool (
sizeof (Context->Regions[0]) * Context->AllocatedRegions);
1440 if (Context->Regions == NULL) {
1441 DEBUG ((DEBUG_WARN,
"OCA: Failed to allocate memory for %u regions\n", Context->NumberOfRegions));
1442 return EFI_OUT_OF_RESOURCES;
1445 if (Context->Dsdt != NULL) {
1447 if (EFI_ERROR (Status)) {
1452 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
1453 if (Context->Tables[Index]->Signature == EFI_ACPI_6_2_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) {
1455 if (EFI_ERROR (Status)) {
1471 EFI_ACPI_COMMON_HEADER *NewTable;
1476 ASSERT (Context->Regions != NULL);
1478 if (Context->NumberOfRegions == 0) {
1482 if (Context->Dsdt != NULL) {
1485 if (EFI_ERROR (Status)) {
1493 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
1494 if (Context->Tables[Index]->Signature == EFI_ACPI_6_2_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) {
1497 if (EFI_ERROR (Status)) {
1501 Context->Tables[Index] = NewTable;
1516 UINT32 RequiredSize;
1518 if (Context->Fadt == NULL) {
1519 return EFI_NOT_FOUND;
1522 OldSize = Context->Fadt->Header.Length;
1523 RequiredSize = OFFSET_OF (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE, ArmBootArch);
1525 DEBUG ((DEBUG_INFO,
"OCA: FADT reset got %u need %u\n", OldSize, RequiredSize));
1535 if ((OldSize < RequiredSize) || !
AcpiIsTableWritable ((EFI_ACPI_COMMON_HEADER *)Context->Fadt)) {
1537 if (EFI_ERROR (Status)) {
1540 }
else if (!( ((Context->Fadt->Flags & EFI_ACPI_6_2_RESET_REG_SUP) == 0)
1541 || ((Context->Fadt->Flags & EFI_ACPI_6_2_SLP_BUTTON) == 0)
1542 || ((Context->Fadt->Flags & EFI_ACPI_6_2_PWR_BUTTON) != 0)
1543 || (Context->Fadt->ResetReg.Address == 0)
1544 || (Context->Fadt->ResetReg.Address == 0x64)
1545 || (Context->Fadt->ResetReg.RegisterBitWidth != 8)))
1554 Context->Fadt->Flags |= EFI_ACPI_6_2_SLP_BUTTON | EFI_ACPI_6_2_RESET_REG_SUP;
1555 Context->Fadt->Flags &= ~EFI_ACPI_6_2_PWR_BUTTON;
1562 if ( (Context->Fadt->ResetReg.Address == 0)
1563 || (Context->Fadt->ResetReg.Address == 0x64)
1564 || (Context->Fadt->ResetReg.RegisterBitWidth != 8))
1574 Context->Fadt->ResetReg.Address = 0x64;
1575 Context->Fadt->ResetValue = 0xFE;
1580 Context->Fadt->ResetReg.Address = 0xCF9;
1581 Context->Fadt->ResetValue = 6;
1584 Context->Fadt->ResetReg.AddressSpaceId = EFI_ACPI_6_2_SYSTEM_IO;
1585 Context->Fadt->ResetReg.RegisterBitWidth = 8;
1586 Context->Fadt->ResetReg.RegisterBitOffset = 0;
1587 Context->Fadt->ResetReg.AccessSize = EFI_ACPI_6_2_BYTE;
1603 EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE *Bgrt;
1604 EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE *BgrtNew;
1606 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
1607 if (Context->Tables[Index]->Signature == EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE_SIGNATURE) {
1608 if (Context->Tables[Index]->Length >= sizeof (EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE)) {
1609 Bgrt = (EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE *)Context->Tables[Index];
1613 (EFI_ACPI_COMMON_HEADER *)Bgrt,
1615 (EFI_ACPI_COMMON_HEADER **)&BgrtNew
1618 if (EFI_ERROR (Status)) {
1622 Context->Tables[Index] = (EFI_ACPI_COMMON_HEADER *)BgrtNew;
1626 Bgrt->Status &= ~EFI_ACPI_6_2_BGRT_STATUS_DISPLAYED;
1627 Bgrt->Header.Checksum = 0;
1628 Bgrt->Header.Checksum = CalculateCheckSum8 (
1644 EFI_ACPI_DESCRIPTION_HEADER *Slic;
1648 for (Index = 0; Index < Context->NumberOfTables; ++Index) {
1649 if (Context->Tables[Index]->Signature == EFI_ACPI_6_2_SOFTWARE_LICENSING_TABLE_SIGNATURE) {
1650 Slic = (VOID *)Context->Tables[Index];
1656 DEBUG ((DEBUG_INFO,
"OCA: SLIC table is not found\n"));
1665 if (Context->Rsdt != NULL) {
1666 CopyMem (&Context->Rsdt->Header.OemId, &Slic->OemId, sizeof (Context->Rsdt->Header.OemId));
1667 Context->Rsdt->Header.OemTableId = Slic->OemTableId;
1668 Context->Rsdt->Header.Checksum = 0;
1669 Context->Rsdt->Header.Checksum = CalculateCheckSum8 (
1670 (UINT8 *)Context->Rsdt,
1671 Context->Rsdt->Header.Length
1673 DEBUG ((DEBUG_INFO,
"OCA: SLIC table IDs fixed in RSDT\n"));
1676 if (Context->Xsdt != NULL) {
1677 CopyMem (&Context->Xsdt->Header.OemId, &Slic->OemId, sizeof (Context->Xsdt->Header.OemId));
1678 Context->Xsdt->Header.OemTableId = Slic->OemTableId;
1679 Context->Xsdt->Header.Checksum = 0;
1680 Context->Xsdt->Header.Checksum = CalculateCheckSum8 (
1681 (UINT8 *)Context->Xsdt,
1682 Context->Xsdt->Header.Length
1684 DEBUG ((DEBUG_INFO,
"OCA: SLIC table IDs fixed in XSDT\n"));
1687 if (Context->Fadt != NULL) {
1688 CopyMem (&Context->Fadt->Header.OemId, &Slic->OemId, sizeof (Context->Fadt->Header.OemId));
1689 Context->Fadt->Header.OemTableId = Slic->OemTableId;
1690 Context->Fadt->Header.Checksum = 0;
1691 Context->Fadt->Header.Checksum = CalculateCheckSum8 (
1692 (UINT8 *)Context->Fadt,
1693 Context->Fadt->Header.Length
1695 DEBUG ((DEBUG_INFO,
"OCA: SLIC table IDs fixed in FADT\n"));
1706 BOOLEAN IsXFirmwareCtrl;
1708 EFI_ACPI_6_2_FIRMWARE_ACPI_CONTROL_STRUCTURE *Facs;
1709 EFI_ACPI_6_2_FIRMWARE_ACPI_CONTROL_STRUCTURE *FacsNew;
1711 if (Context->Fadt == NULL) {
1715 if (Context->Fadt->Header.Length >= OFFSET_OF (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE, XFirmwareCtrl) +
sizeof (Context->Fadt->XFirmwareCtrl)) {
1716 Facs = (EFI_ACPI_6_2_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)Context->Fadt->XFirmwareCtrl;
1717 IsXFirmwareCtrl = TRUE;
1720 IsXFirmwareCtrl = FALSE;
1723 if ((Facs == NULL) && (Context->Fadt->Header.Length >= OFFSET_OF (EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE, FirmwareCtrl) +
sizeof (Context->Fadt->FirmwareCtrl))) {
1724 Facs = (EFI_ACPI_6_2_FIRMWARE_ACPI_CONTROL_STRUCTURE *)(UINTN)Context->Fadt->FirmwareCtrl;
1727 if ((Facs != NULL) && (Facs->Length >= OFFSET_OF (EFI_ACPI_6_2_FIRMWARE_ACPI_CONTROL_STRUCTURE, Flags) +
sizeof (Facs->HardwareSignature))) {
1728 DEBUG ((DEBUG_INFO,
"OCA: FACS signature is %X (%d)\n", Facs->Flags, Reset));
1733 (EFI_ACPI_COMMON_HEADER *)Facs,
1735 (EFI_ACPI_COMMON_HEADER **)&FacsNew
1737 if (EFI_ERROR (Status)) {
1746 if (EFI_ERROR (Status)) {
1752 if (IsXFirmwareCtrl) {
1753 Context->Fadt->XFirmwareCtrl = (UINT64)(UINTN)FacsNew;
1755 Context->Fadt->FirmwareCtrl = (UINT32)(UINTN)FacsNew;
1766 Facs->HardwareSignature = 0x0;
1774 "OCA: FACS signature is too far %d / %u\n",
1776 Facs != NULL ? Facs->Length : 0
STATIC UINT32 AcpiFindName(IN CONST UINT8 *Data, IN UINT32 Length, IN CONST CHAR8 *Name)
STATIC EFI_STATUS AcpiAllocateCopyFadt(IN OUT OC_ACPI_CONTEXT *Context, IN UINT32 NewSize)
STATIC UINT64 AcpiReadOemTableId(IN CONST EFI_ACPI_COMMON_HEADER *Common)
STATIC VOID AcpiRelocateTableRegions(IN OUT OC_ACPI_CONTEXT *Context, IN EFI_ACPI_COMMON_HEADER *Table)
VOID AcpiNormalizeHeaders(IN OUT OC_ACPI_CONTEXT *Context)
EFI_STATUS AcpiApplyContext(IN OUT OC_ACPI_CONTEXT *Context)
EFI_STATUS AcpiFadtEnableReset(IN OUT OC_ACPI_CONTEXT *Context)
STATIC EFI_STATUS AcpiAllocateCopyTable(IN EFI_ACPI_COMMON_HEADER *Table, IN UINT32 NewSize OPTIONAL, OUT EFI_ACPI_COMMON_HEADER **NewTable)
STATIC EFI_STATUS AcpiLoadTableRegions(IN OUT OC_ACPI_CONTEXT *Context, IN EFI_ACPI_COMMON_HEADER *Table)
STATIC EFI_STATUS AcpiAllocateCopyDsdt(IN OUT OC_ACPI_CONTEXT *Context, IN EFI_ACPI_DESCRIPTION_HEADER *NewDsdt OPTIONAL)
EFI_STATUS AcpiDeleteTable(IN OUT OC_ACPI_CONTEXT *Context, IN UINT32 Signature, IN UINT32 Length, IN UINT64 OemTableId, IN BOOLEAN All)
EFI_STATUS AcpiApplyPatch(IN OUT OC_ACPI_CONTEXT *Context, IN OC_ACPI_PATCH *Patch)
VOID AcpiSyncTableIds(IN OUT OC_ACPI_CONTEXT *Context)
VOID AcpiFreeContext(IN OUT OC_ACPI_CONTEXT *Context)
STATIC UINT32 AcpiReadSignature(IN CONST EFI_ACPI_COMMON_HEADER *Common)
VOID AcpiRelocateRegions(IN OUT OC_ACPI_CONTEXT *Context)
VOID AcpiHandleHardwareSignature(IN OUT OC_ACPI_CONTEXT *Context, IN BOOLEAN Reset)
STATIC VOID AcpiRefreshTableChecksum(IN EFI_ACPI_DESCRIPTION_HEADER *Table)
STATIC BOOLEAN AcpiIsTableWritable(IN EFI_ACPI_COMMON_HEADER *Table)
VOID AcpiResetLogoStatus(IN OUT OC_ACPI_CONTEXT *Context)
STATIC EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER * AcpiFindLegacyRsdp(VOID)
STATIC BOOLEAN AcpiReadName(IN CONST UINT8 *Data, OUT CHAR8 *Name, OUT UINT32 *NameOffset OPTIONAL)
STATIC EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER * AcpiFindRsdp(VOID)
STATIC BOOLEAN AcpiNormalizeTableHeaders(IN EFI_ACPI_DESCRIPTION_HEADER *Table)
#define PCI_VENDOR_NVIDIA
EFI_STATUS AcpiInitContext(IN OUT OC_ACPI_CONTEXT *Context)
EFI_STATUS AcpiLoadRegions(IN OUT OC_ACPI_CONTEXT *Context)
EFI_STATUS AcpiInsertTable(IN OUT OC_ACPI_CONTEXT *Context, IN CONST UINT8 *Data, IN UINT32 Length)
STATIC BOOLEAN AcpiNormalizeRsdp(IN EFI_ACPI_6_2_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp, IN BOOLEAN HasXsdt)
EFI_STATUS AcpiFindEntryInMemory(IN UINT8 *Table, IN CONST CHAR8 *PathString, IN UINT8 Entry, OUT UINT32 *Offset, IN UINT32 TableLength OPTIONAL)
#define OC_ACPI_NAME_SIZE
DMG_SIZE_DEVICE_PATH Size
EFI_STATUS LegacyRegionUnlock(IN UINT32 LegacyAddress, IN UINT32 LegacyLength)
UINT32 ApplyPatch(IN CONST UINT8 *Pattern, IN CONST UINT8 *PatternMask OPTIONAL, IN CONST UINT32 PatternSize, IN CONST UINT8 *Replace, IN CONST UINT8 *ReplaceMask OPTIONAL, IN UINT8 *Data, IN UINT32 DataSize, IN UINT32 Count, IN UINT32 Skip)
BOOLEAN IsAsciiPrint(IN CHAR8 Char)
OC_TYPING_BUFFER_ENTRY Buffer[OC_TYPING_BUFFER_SIZE]
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)