From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (NAM10-MW2-obe.outbound.protection.outlook.com [40.107.94.131]) by mx.groups.io with SMTP id smtpd.web10.39138.1629206828346798979 for ; Tue, 17 Aug 2021 06:27:08 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@os.amperecomputing.com header.s=selector2 header.b=vU+GAwkx; spf=pass (domain: os.amperecomputing.com, ip: 40.107.94.131, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FUB75xFdwOGAqOvvCLWUHR8UDP9ES15TZ5awQbvGraqUgmyLtkdGAEMlxcmJnc7jfH1c+2sq+KSsfFeRC2EIr+L5OvcwXfaK4gogjWTmFdZQ1lwgj2sTfOwr7w+lf9PtTbQueHhWMD2KUFZaC74AbA6YUPyMYGOnjgqM5D3w6IFq/GpvGT252FiyePnLNHFNb5cp+tVQBuHkRbXyzWOv1Co7DI5SH6CW0NO55qVIF0ZFeEEwxVyjIx3k3WZ1tgFzZaXsNcL7mNjRmUvJMBFFnO+ZGLl2Eq0YW53KSc9elK2kR4EKxi+qKCxy7kcqPoUTax1DywyTCNil7bEHKnq5xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=swwAwEtDn+AOEdpPaWFJevkBYN0/cpj4+yjP93fyI68=; b=U7l5NG66hemN3dfllsiDshMPa0zI7zuGoJ/H5jBSs2ilLdftaPlVnh74m1Byw6bnhyrD5xiMktBAhzMT31xWY6QkNRL+jrcAdU4hce+VEUdek37Mr7WAIyanyrsQ9llPVvuPzBTYzy2aSHob7rW1kfLkIP4TTyuVf8qHhKH5MUM6snDNnX9HidQCMqDuL66AG4lwcWanQoY1tGe8tn1wCUVFfXDKrnaCqRfNtspE4jYKQCeVCj6rDwYPVe5MNJaut49REw/pbnaxuvxkECWqkRx/QDCh9qNTf/TIir4TAuVVGD73rr2EPXyOd5teOGPw7r4DK0JpE8YvW/4WglkfDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=swwAwEtDn+AOEdpPaWFJevkBYN0/cpj4+yjP93fyI68=; b=vU+GAwkxSnm2LTIDQSKKsAr9psdCippeb2PFaasMIb1xk+D4V70U86bbQ7MA6emcI78CZTWY27dihqSVqqTOc7o1jHZ7hJrIuj9ZBCcwaHaMJi7l0C/N+k+6A6sC1DJwVQA4F3mSSRVHiouhQJK/hR2Yvm1w2chiU93rs3bIQwY= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) by DM5PR01MB2428.prod.exchangelabs.com (2603:10b6:3:3c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.21; Tue, 17 Aug 2021 13:27:05 +0000 Received: from DM6PR01MB5849.prod.exchangelabs.com ([fe80::14f9:1eb8:c0b2:4b9f]) by DM6PR01MB5849.prod.exchangelabs.com ([fe80::14f9:1eb8:c0b2:4b9f%7]) with mapi id 15.20.4415.024; Tue, 17 Aug 2021 13:27:05 +0000 From: "Nhi Pham" To: devel@edk2.groups.io Cc: patches@amperecomputing.com, Nhi Pham , Leif Lindholm , Ard Biesheuvel , Abner Chang , Daniel Schaefer Subject: [PATCH 1/1] EmbeddedPkg/AcpiLib: Add more helper functions Date: Tue, 17 Aug 2021 20:24:13 +0700 Message-Id: <20210817132413.7621-1-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 X-ClientProxiedBy: HK2PR03CA0064.apcprd03.prod.outlook.com (2603:1096:202:17::34) To DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (118.69.219.201) by HK2PR03CA0064.apcprd03.prod.outlook.com (2603:1096:202:17::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4436.11 via Frontend Transport; Tue, 17 Aug 2021 13:27:02 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7375d29b-1776-4730-2e80-08d96182b45f X-MS-TrafficTypeDiagnostic: DM5PR01MB2428: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3968; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: J+xgRY+xDGXBh7OmUSn3sxPSUcZMsV2q7fTQSMZrWluTngoS/UgglT0uVXhz1b8BHhudMiFR2FHTaz7RmWwsgk/6Ai2/FPNTXhnkh3vpW/nqKtb7ipX5JleNPZJr8ED/RLnGqenStNmSV4qt7oQa1O7hwEcqpzC4RNWwlNWA9L278FhHhE7JgsJZxjy5VA/OtSCVtS6ZI5uwNx6LuJ4SWpHisgvkdZimYTZsgax77itEHCOk08yxePVhoOnelcDdVgxrfOB+i+HtpLvJMuMO9m5gJUKM3wsV4OXjwv+/L+ViGEgnba0+9kxKfldAhTPCcbSc9rjbVMzF26t0pC54q6j4+aD1pFw8xXEp11HbP65byI2VpBQfLAvkSuo+M7Lu2vyS7lf/6JfFIZmFHkKuiFKNbHD+5FVmK1j6EGWa5qf7tD99q6QfuN/TLfq3ihl2Q7YugT2ShtoMH6tcFUXyNVEcTdWAWZyzgobUAWWRcpSfDUHfXZFtjZKryj2NOcyWXpImjuHPWege+fmsHJRxgaYX7dStTCL0MjyjB2nWTMM4O5zhuYEGicclGtVL/kicPqKBjWuteJKSBBDlkbv+Vp5PiUVU35qC6t6kdi5brve8+EOeupekiR92xRtIb8ZOWwYsVgNr9ax9IM5wYU/wz6BJqYZ416tlLalIG6yMkmDIZwxUB6bdZiqDV/1O4HBwayz6GniCyRZsydzgC4m+cw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR01MB5849.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(2616005)(6916009)(6486002)(1076003)(956004)(186003)(26005)(6512007)(6506007)(19627235002)(52116002)(8936002)(6666004)(38350700002)(38100700002)(2906002)(66946007)(83380400001)(86362001)(8676002)(4326008)(316002)(508600001)(54906003)(66556008)(66476007)(5660300002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?EVxk23WwRfiL+u6m2xOEESt39mrJHyMhjlituDjEXKJ1vCHq1lI1b/od29Fj?= =?us-ascii?Q?f6SELOtP6o0O+4mt3qesoPHLCWphUMlxejuXaLtLv4AX4CgY7VoCqeuC6l9/?= =?us-ascii?Q?qHRwgJgWoLW344AqYy64EiDa0ovVavOiA7GUiOAC3FXTdKeG+J2Plebg0tYc?= =?us-ascii?Q?Wcn7GYdNdVJZSqVqw+FiJI6PPchJQRCKXpz11qQIYBzVd0toExi/DU12UpeO?= =?us-ascii?Q?W+AFjwIbUgv5KvensO63G6TIVsOTUDFw+oF9zawwwCzjLWAR7lrC4pvD0G4q?= =?us-ascii?Q?VkhgR0bOhDoMm+9jDqXK688W7mHmJgzKRvwa80USVNfGV3f2VoDZ6O//IfpY?= =?us-ascii?Q?EWjHQuLTJO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hxdcho73OMRxBa7CqZjV?= =?us-ascii?Q?5WekXZp9YRUBXS4CJ7jX4K81lMnaomBdcmdtthRHgVrGmBk2orb1LO+v4PcN?= =?us-ascii?Q?CrWBFI01lxJBpjDJsiVRCYdb?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7375d29b-1776-4730-2e80-08d96182b45f X-MS-Exchange-CrossTenant-AuthSource: DM6PR01MB5849.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Aug 2021 13:27:05.3472 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 20wVc8hwMSF41ZM9UwVLoNaGSp5l576J9KbV+4cR29ikWTyrCPUlE6R9hTbXwyR3anRRuvQ9FSaRWc+XTv0UWUIS+vQsqijys3wTdJ1Lju4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR01MB2428 Content-Type: text/plain This adds more helper functions that assist in calculating the checksum, locating an ACPI table by signature, and updating an AML integer object. Cc: Leif Lindholm Cc: Ard Biesheuvel Cc: Abner Chang Cc: Daniel Schaefer Signed-off-by: Nhi Pham --- EmbeddedPkg/Library/AcpiLib/AcpiLib.inf | 2 + EmbeddedPkg/Include/Library/AcpiLib.h | 68 ++++++++ EmbeddedPkg/Library/AcpiLib/AcpiLib.c | 183 ++++++++++++++++++++ 3 files changed, 253 insertions(+) diff --git a/EmbeddedPkg/Library/AcpiLib/AcpiLib.inf b/EmbeddedPkg/Library/AcpiLib/AcpiLib.inf index 538fe09cca29..154cb1eebc80 100644 --- a/EmbeddedPkg/Library/AcpiLib/AcpiLib.inf +++ b/EmbeddedPkg/Library/AcpiLib/AcpiLib.inf @@ -23,6 +23,8 @@ [Packages] EmbeddedPkg/EmbeddedPkg.dec [LibraryClasses] + BaseLib + BaseMemoryLib DebugLib UefiBootServicesTableLib diff --git a/EmbeddedPkg/Include/Library/AcpiLib.h b/EmbeddedPkg/Include/Library/AcpiLib.h index c142446d9d59..cdb6ea410c54 100644 --- a/EmbeddedPkg/Include/Library/AcpiLib.h +++ b/EmbeddedPkg/Include/Library/AcpiLib.h @@ -13,6 +13,7 @@ #include #include +#include // // Macros for the Generic Address Space @@ -128,4 +129,71 @@ LocateAndInstallAcpiFromFv ( IN CONST EFI_GUID* AcpiFile ); +/** + This function calculates and updates an UINT8 checksum. + + @param Buffer Pointer to buffer to checksum + @param Size Number of bytes to checksum + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +AcpiUpdateChecksum ( + IN UINT8 *Buffer, + IN UINTN Size + ); + +/** + This function uses the ACPI SDT protocol to locate an ACPI table + with a given signature that only have a single instance. + + Caution: This function does not act correctly with tables having + more than 2 instances like SSDT table. + + @param AcpiTableSdtProtocol Pointer to ACPI SDT protocol. + @param TableSignature ACPI table signature. + @param Table Pointer to the table. + @param TableKey Pointer to the table key. + + @return EFI_SUCCESS The function completed successfully. + @return EFI_INVALID_PARAMETER At least one of parameters is invalid. + @retval EFI_NOT_FOUND The requested index is too large and a table was not found. + +**/ +EFI_STATUS +EFIAPI +AcpiLocateTableBySignature ( + IN EFI_ACPI_SDT_PROTOCOL *AcpiSdtProtocol, + IN UINT32 TableSignature, + OUT EFI_ACPI_DESCRIPTION_HEADER **Table, + OUT UINTN *TableKey + ); + +/** + This function updates the integer value of an AML Object. + + @param AcpiTableSdtProtocol Pointer to ACPI SDT protocol. + @param TableHandle Points to the table representing the starting point + for the object path search. + @param AsciiObjectPath Pointer to the ACPI path of the object being updated. + @param Value New value to write to the object. + + @return EFI_SUCCESS The function completed successfully. + @return EFI_INVALID_PARAMETER At least one of parameters is invalid or the data type + of the ACPI object is not an integer value. + @retval EFI_NOT_FOUND The object is not found with the given path. + +**/ +EFI_STATUS +EFIAPI +AcpiAmlObjectUpdateInteger ( + IN EFI_ACPI_SDT_PROTOCOL *AcpiSdtProtocol, + IN EFI_ACPI_HANDLE TableHandle, + IN CHAR8 *AsciiObjectPath, + IN UINTN Value + ); + #endif // __ACPI_LIB_H__ diff --git a/EmbeddedPkg/Library/AcpiLib/AcpiLib.c b/EmbeddedPkg/Library/AcpiLib/AcpiLib.c index ff7d678433d5..e07919ae323f 100644 --- a/EmbeddedPkg/Library/AcpiLib/AcpiLib.c +++ b/EmbeddedPkg/Library/AcpiLib/AcpiLib.c @@ -9,9 +9,12 @@ #include #include +#include +#include #include #include +#include #include #include @@ -170,3 +173,183 @@ LocateAndInstallAcpiFromFv ( { return LocateAndInstallAcpiFromFvConditional (AcpiFile, NULL); } + +/** + This function calculates and updates an UINT8 checksum. + + @param Buffer Pointer to buffer to checksum + @param Size Number of bytes to checksum + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER Invalid parameter. + +**/ +EFI_STATUS +EFIAPI +AcpiUpdateChecksum ( + IN UINT8 *Buffer, + IN UINTN Size + ) +{ + UINTN ChecksumOffset; + + if (Buffer == NULL || Size == 0) { + return EFI_INVALID_PARAMETER; + } + + ChecksumOffset = OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER, Checksum); + + // + // Set checksum to 0 first + // + Buffer[ChecksumOffset] = 0; + + // + // Update checksum value + // + Buffer[ChecksumOffset] = CalculateCheckSum8 (Buffer, Size); + + return EFI_SUCCESS; +} + +/** + This function uses the ACPI SDT protocol to locate an ACPI table + with a given signature that only have a single instance. + + Caution: This function does not act correctly with tables having + more than 2 instances like SSDT table. + + @param AcpiTableSdtProtocol Pointer to ACPI SDT protocol. + @param TableSignature ACPI table signature. + @param Table Pointer to the table. + @param TableKey Pointer to the table key. + + @return EFI_SUCCESS The function completed successfully. + @return EFI_INVALID_PARAMETER At least one of parameters is invalid. + @retval EFI_NOT_FOUND The requested index is too large and a table was not found. + +**/ +EFI_STATUS +EFIAPI +AcpiLocateTableBySignature ( + IN EFI_ACPI_SDT_PROTOCOL *AcpiSdtProtocol, + IN UINT32 TableSignature, + OUT EFI_ACPI_DESCRIPTION_HEADER **Table, + OUT UINTN *TableKey + ) +{ + EFI_STATUS Status; + EFI_ACPI_SDT_HEADER *TempTable; + EFI_ACPI_TABLE_VERSION TableVersion; + UINTN TableIndex; + + if (AcpiSdtProtocol == NULL + || Table == NULL + || TableKey == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = EFI_SUCCESS; + + // + // Search for ACPI Table with matching signature + // + TableVersion = 0; + TableIndex = 0; + while (!EFI_ERROR (Status)) { + Status = AcpiSdtProtocol->GetAcpiTable ( + TableIndex, + &TempTable, + &TableVersion, + TableKey + ); + if (!EFI_ERROR (Status)) { + TableIndex++; + + if (((EFI_ACPI_DESCRIPTION_HEADER *)TempTable)->Signature == TableSignature) { + *Table = (EFI_ACPI_DESCRIPTION_HEADER *)TempTable; + break; + } + } + } + + return Status; +} + +/** + This function updates the integer value of an AML Object. + + @param AcpiTableSdtProtocol Pointer to ACPI SDT protocol. + @param TableHandle Points to the table representing the starting point + for the object path search. + @param AsciiObjectPath Pointer to the ACPI path of the object being updated. + @param Value New value to write to the object. + + @return EFI_SUCCESS The function completed successfully. + @return EFI_INVALID_PARAMETER At least one of parameters is invalid or the data type + of the ACPI object is not an integer value. + @retval EFI_NOT_FOUND The object is not found with the given path. + +**/ +EFI_STATUS +EFIAPI +AcpiAmlObjectUpdateInteger ( + IN EFI_ACPI_SDT_PROTOCOL *AcpiSdtProtocol, + IN EFI_ACPI_HANDLE TableHandle, + IN CHAR8 *AsciiObjectPath, + IN UINTN Value + ) +{ + EFI_STATUS Status; + EFI_ACPI_HANDLE ObjectHandle; + EFI_ACPI_DATA_TYPE DataType; + UINT8 *Buffer; + UINTN BufferSize; + + if (AcpiSdtProtocol == NULL || AsciiObjectPath == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = AcpiSdtProtocol->FindPath (TableHandle, AsciiObjectPath, &ObjectHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = AcpiSdtProtocol->GetOption (ObjectHandle, 0, &DataType, (VOID *)&Buffer, &BufferSize); + if (EFI_ERROR (Status)) { + return Status; + } + + if (Buffer[0] != AML_NAME_OP) { + return EFI_NOT_FOUND; + } + + switch (Buffer[5]) { + case AML_ZERO_OP: + case AML_ONE_OP: + Buffer[5] = (UINT8)Value; + break; + + case AML_BYTE_PREFIX: + Buffer[6] = (UINT8)Value; + break; + + case AML_WORD_PREFIX: + CopyMem ((VOID *)&Buffer[6], (VOID *)&Buffer, sizeof (UINT16)); + break; + + case AML_DWORD_PREFIX: + CopyMem ((VOID *)&Buffer[6], (VOID *)&Buffer, sizeof (UINT32)); + break; + + case AML_QWORD_PREFIX: + CopyMem ((VOID *)&Buffer[6], (VOID *)&Buffer, sizeof (UINT64)); + break; + + default: + // The data type of the ACPI object is not an integer value. + return EFI_INVALID_PARAMETER; + } + + return Status; +} -- 2.17.1