]> git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Common/AcpiHelperLib/AcpiHelper.c
DynamicTablesPkg: Extract AcpiHelperLib from TableHelperLib
[mirror_edk2.git] / DynamicTablesPkg / Library / Common / AcpiHelperLib / AcpiHelper.c
1 /** @file
2 Acpi Helper
3
4 Copyright (c) 2017 - 2021, Arm Limited. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include <Library/BaseLib.h>
10 #include <Library/DebugLib.h>
11
12 // Module specific include files.
13 #include <Library/AcpiHelperLib.h>
14
15 /** Convert a hex number to its ASCII code.
16
17 @param [in] x Hex number to convert.
18 Must be 0 <= x < 16.
19
20 @return The ASCII code corresponding to x.
21 **/
22 UINT8
23 EFIAPI
24 AsciiFromHex (
25 IN UINT8 x
26 )
27 {
28 if (x < 10) {
29 return (UINT8)(x + '0');
30 }
31
32 if (x < 16) {
33 return (UINT8)(x - 10 + 'A');
34 }
35
36 ASSERT (FALSE);
37 return (UINT8)-1;
38 }
39
40 /** Check if a HID is a valid PNP ID.
41
42 @param [in] Hid The Hid to validate.
43
44 @retval TRUE The Hid is a valid PNP ID.
45 @retval FALSE The Hid is not a valid PNP ID.
46 **/
47 BOOLEAN
48 IsValidPnpId (
49 IN CONST CHAR8 * Hid
50 )
51 {
52 UINTN Index;
53
54 if (AsciiStrLen (Hid) != 7) {
55 return FALSE;
56 }
57
58 // A valid PNP ID must be of the form "AAA####"
59 // where A is an uppercase letter and # is a hex digit.
60 for (Index = 0; Index < 3; Index++) {
61 if (!IS_UPPER_CHAR (Hid[Index])) {
62 return FALSE;
63 }
64 }
65
66 for (Index = 3; Index < 7; Index++) {
67 if (!IS_UPPER_HEX (Hid[Index])) {
68 return FALSE;
69 }
70 }
71
72 return TRUE;
73 }
74
75 /** Check if a HID is a valid ACPI ID.
76
77 @param [in] Hid The Hid to validate.
78
79 @retval TRUE The Hid is a valid ACPI ID.
80 @retval FALSE The Hid is not a valid ACPI ID.
81 **/
82 BOOLEAN
83 IsValidAcpiId (
84 IN CONST CHAR8 * Hid
85 )
86 {
87 UINTN Index;
88
89 if (AsciiStrLen (Hid) != 8) {
90 return FALSE;
91 }
92
93 // A valid ACPI ID must be of the form "NNNN####"
94 // where N is an uppercase letter or a digit ('0'-'9')
95 // and # is a hex digit.
96 for (Index = 0; Index < 4; Index++) {
97 if (!(IS_UPPER_CHAR (Hid[Index]) || IS_DIGIT (Hid[Index]))) {
98 return FALSE;
99 }
100 }
101
102 for (Index = 4; Index < 8; Index++) {
103 if (!IS_UPPER_HEX (Hid[Index])) {
104 return FALSE;
105 }
106 }
107
108 return TRUE;
109 }