]>
git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Common/AcpiHelperLib/AcpiHelper.c
4 Copyright (c) 2017 - 2021, Arm Limited. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/BaseLib.h>
10 #include <Library/DebugLib.h>
12 // Module specific include files.
13 #include <Library/AcpiHelperLib.h>
15 /** Convert a hex number to its ASCII code.
17 @param [in] Hex Hex number to convert.
20 @return The ASCII code corresponding to x.
30 return (UINT8
)(Hex
+ '0');
34 return (UINT8
)(Hex
- 10 + 'A');
41 /** Convert an ASCII char representing an hexadecimal number
44 @param [in] Char Char to convert.
45 Must be between '0'-'9' or 'A'-'F' or 'a'-'f'.
47 @return The corresponding integer (between 0-16).
56 if ((Char
>= '0') && (Char
<= '9')) {
57 return (UINT8
)(Char
- '0');
60 if ((Char
>= 'A') && (Char
<= 'F')) {
61 return (UINT8
)(Char
- 'A' + 10);
64 if ((Char
>= 'a') && (Char
<= 'f')) {
65 return (UINT8
)(Char
- 'a' + 10);
72 /** Check if a HID is a valid PNP ID.
74 @param [in] Hid The Hid to validate.
76 @retval TRUE The Hid is a valid PNP ID.
77 @retval FALSE The Hid is not a valid PNP ID.
86 if (AsciiStrLen (Hid
) != 7) {
90 // A valid PNP ID must be of the form "AAA####"
91 // where A is an uppercase letter and # is a hex digit.
92 for (Index
= 0; Index
< 3; Index
++) {
93 if (!IS_UPPER_CHAR (Hid
[Index
])) {
98 for (Index
= 3; Index
< 7; Index
++) {
99 if (!IS_UPPER_HEX (Hid
[Index
])) {
107 /** Check if a HID is a valid ACPI ID.
109 @param [in] Hid The Hid to validate.
111 @retval TRUE The Hid is a valid ACPI ID.
112 @retval FALSE The Hid is not a valid ACPI ID.
121 if (AsciiStrLen (Hid
) != 8) {
125 // A valid ACPI ID must be of the form "NNNN####"
126 // where N is an uppercase letter or a digit ('0'-'9')
127 // and # is a hex digit.
128 for (Index
= 0; Index
< 4; Index
++) {
129 if (!(IS_UPPER_CHAR (Hid
[Index
]) || IS_DIGIT (Hid
[Index
]))) {
134 for (Index
= 4; Index
< 8; Index
++) {
135 if (!IS_UPPER_HEX (Hid
[Index
])) {
143 /** Convert a EisaId string to its compressed UINT32 equivalent.
145 Cf. ACPI 6.4 specification, s19.3.4 "ASL Macros": "Eisaid"
147 @param [in] EisaIdStr Input EisaId string.
148 @param [out] EisaIdInt Output EisaId UINT32 (compressed).
150 @retval EFI_SUCCESS The function completed successfully.
151 @retval EFI_INVALID_PARAMETER Invalid parameter.
155 AmlGetEisaIdFromString (
156 IN CONST CHAR8
*EisaIdStr
,
157 OUT UINT32
*EisaIdInt
160 if ((EisaIdStr
== NULL
) ||
161 (!IsValidPnpId (EisaIdStr
)) ||
165 return EFI_INVALID_PARAMETER
;
168 /* Cf. ACPI 6.4 specification, s19.3.4 "ASL Macros": "Eisaid"
170 Converts and compresses the 7-character text argument into its corresponding
171 4-byte numeric EISA ID encoding (Integer). This can be used when declaring
172 IDs for devices that are EISA IDs.
174 The algorithm used to convert the TextID is as shown in the following
176 Starting with a seven character input string "PNP0303", we want to create
177 a DWordConst. This string contains a three character manufacturer code
178 "PNP", a three character hex product identifier "030", and a one character
179 revision identifier "3".
180 The compressed manufacturer code is created as follows:
181 1) Find hex ASCII value for each letter
182 2) Subtract 40h from each ASCII value
183 3) Retain 5 least significant bits for each letter and discard remaining
188 Bit 6-2: 1st character of compressed mfg code "P"
189 Bit 1-0: Upper 2 bits of 2nd character of mfg code "N"
191 Bit 7-5: Lower 3 bits of 2nd character of mfg code "N"
192 Bit 4-0: 3rd character of mfg code "P"
194 Bit 7-4: 1st hex digit of product number "0"
195 Bit 3-0: 2nd hex digit of product number "3"
197 Bit 7-4: 3rd hex digit of product number "0"
198 Bit 3-0: 4th hex digit of product number "3"
200 *EisaIdInt
= SwapBytes32 (
201 ((EisaIdStr
[0] - 0x40) << 26) |
202 ((EisaIdStr
[1] - 0x40) << 21) |
203 ((EisaIdStr
[2] - 0x40) << 16) |
204 (HexFromAscii (EisaIdStr
[3]) << 12) |
205 (HexFromAscii (EisaIdStr
[4]) << 8) |
206 (HexFromAscii (EisaIdStr
[5]) << 4) |
207 (HexFromAscii (EisaIdStr
[6]))