4 Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/UefiLib.h>
10 #include <Library/UefiBootServicesTableLib.h>
11 #include "AcpiParser.h"
14 STATIC UINT32 gIndent
;
15 STATIC UINT32 mTableErrorCount
;
16 STATIC UINT32 mTableWarningCount
;
18 STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo
;
21 An ACPI_PARSER array describing the ACPI header.
23 STATIC CONST ACPI_PARSER AcpiHeaderParser
[] = {
24 PARSE_ACPI_HEADER (&AcpiHdrInfo
)
28 This function resets the ACPI table error counter to Zero.
39 This function returns the ACPI table error count.
41 @retval Returns the count of errors detected in the ACPI tables.
48 return mTableErrorCount
;
52 This function resets the ACPI table warning counter to Zero.
59 mTableWarningCount
= 0;
63 This function returns the ACPI table warning count.
65 @retval Returns the count of warning detected in the ACPI tables.
72 return mTableWarningCount
;
76 This function increments the ACPI table error counter.
88 This function increments the ACPI table warning counter.
92 IncrementWarningCount (
100 This function verifies the ACPI table checksum.
102 This function verifies the checksum for the ACPI table and optionally
105 @param [in] Log If TRUE log the status of the checksum.
106 @param [in] Ptr Pointer to the start of the table buffer.
107 @param [in] Length The length of the buffer.
109 @retval TRUE The checksum is OK.
110 @retval FALSE The checksum failed.
122 UINTN OriginalAttribute
;
127 while (ByteCount
< Length
) {
128 Checksum
+= *(Ptr
++);
133 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
135 if (GetColourHighlighting ()) {
136 gST
->ConOut
->SetAttribute (
138 EFI_TEXT_ATTR (EFI_GREEN
,
139 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
142 Print (L
"Table Checksum : OK\n\n");
144 IncrementErrorCount ();
145 if (GetColourHighlighting ()) {
146 gST
->ConOut
->SetAttribute (
148 EFI_TEXT_ATTR (EFI_RED
,
149 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
152 Print (L
"Table Checksum : FAILED (0x%X)\n\n", Checksum
);
154 if (GetColourHighlighting ()) {
155 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
159 return (Checksum
== 0);
163 This function performs a raw data dump of the ACPI table.
165 @param [in] Ptr Pointer to the start of the table buffer.
166 @param [in] Length The length of the buffer.
177 UINTN AsciiBufferIndex
;
178 CHAR8 AsciiBuffer
[17];
181 AsciiBufferIndex
= 0;
183 Print (L
"Address : 0x%p\n", Ptr
);
184 Print (L
"Length : %d\n", Length
);
186 while (ByteCount
< Length
) {
187 if ((ByteCount
& 0x0F) == 0) {
188 AsciiBuffer
[AsciiBufferIndex
] = '\0';
189 Print (L
" %a\n%08X : ", AsciiBuffer
, ByteCount
);
190 AsciiBufferIndex
= 0;
191 } else if ((ByteCount
& 0x07) == 0) {
195 if ((*Ptr
>= ' ') && (*Ptr
< 0x7F)) {
196 AsciiBuffer
[AsciiBufferIndex
++] = *Ptr
;
198 AsciiBuffer
[AsciiBufferIndex
++] = '.';
201 Print (L
"%02X ", *Ptr
++);
206 // Justify the final line using spaces before printing
208 PartLineChars
= (Length
& 0x0F);
209 if (PartLineChars
!= 0) {
210 PartLineChars
= 48 - (PartLineChars
* 3);
211 if ((Length
& 0x0F) <= 8) {
214 while (PartLineChars
> 0) {
220 // Print ASCII data for the final line.
221 AsciiBuffer
[AsciiBufferIndex
] = '\0';
222 Print (L
" %a\n\n", AsciiBuffer
);
226 This function traces 1 byte of data as specified in the format string.
228 @param [in] Format The format string for tracing the data.
229 @param [in] Ptr Pointer to the start of the buffer.
234 IN CONST CHAR16
* Format
,
238 Print (Format
, *Ptr
);
242 This function traces 2 bytes of data as specified in the format string.
244 @param [in] Format The format string for tracing the data.
245 @param [in] Ptr Pointer to the start of the buffer.
250 IN CONST CHAR16
* Format
,
254 Print (Format
, *(UINT16
*)Ptr
);
258 This function traces 4 bytes of data as specified in the format string.
260 @param [in] Format The format string for tracing the data.
261 @param [in] Ptr Pointer to the start of the buffer.
266 IN CONST CHAR16
* Format
,
270 Print (Format
, *(UINT32
*)Ptr
);
274 This function traces 8 bytes of data as specified by the format string.
276 @param [in] Format The format string for tracing the data.
277 @param [in] Ptr Pointer to the start of the buffer.
282 IN CONST CHAR16
* Format
,
286 // Some fields are not aligned and this causes alignment faults
287 // on ARM platforms if the compiler generates LDRD instructions.
288 // Perform word access so that LDRD instructions are not generated.
291 Val
= *(UINT32
*)(Ptr
+ sizeof (UINT32
));
294 Val
|= (UINT64
)*(UINT32
*)Ptr
;
300 This function traces 3 characters which can be optionally
301 formated using the format string if specified.
303 If no format string is specified the Format must be NULL.
305 @param [in] Format Optional format string for tracing the data.
306 @param [in] Ptr Pointer to the start of the buffer.
311 IN CONST CHAR16
* Format OPTIONAL
,
316 (Format
!= NULL
) ? Format
: L
"%c%c%c",
324 This function traces 4 characters which can be optionally
325 formated using the format string if specified.
327 If no format string is specified the Format must be NULL.
329 @param [in] Format Optional format string for tracing the data.
330 @param [in] Ptr Pointer to the start of the buffer.
335 IN CONST CHAR16
* Format OPTIONAL
,
340 (Format
!= NULL
) ? Format
: L
"%c%c%c%c",
349 This function traces 6 characters which can be optionally
350 formated using the format string if specified.
352 If no format string is specified the Format must be NULL.
354 @param [in] Format Optional format string for tracing the data.
355 @param [in] Ptr Pointer to the start of the buffer.
360 IN CONST CHAR16
* Format OPTIONAL
,
365 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c",
376 This function traces 8 characters which can be optionally
377 formated using the format string if specified.
379 If no format string is specified the Format must be NULL.
381 @param [in] Format Optional format string for tracing the data.
382 @param [in] Ptr Pointer to the start of the buffer.
387 IN CONST CHAR16
* Format OPTIONAL
,
392 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c%c%c",
405 This function indents and prints the ACPI table Field Name.
407 @param [in] Indent Number of spaces to add to the global table indent.
408 The global table indent is 0 by default; however
409 this value is updated on entry to the ParseAcpi()
410 by adding the indent value provided to ParseAcpi()
411 and restored back on exit.
412 Therefore the total indent in the output is
413 dependent on from where this function is called.
414 @param [in] FieldName Pointer to the Field Name.
420 IN CONST CHAR16
* FieldName
427 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
- Indent
),
433 This function is used to parse an ACPI table buffer.
435 The ACPI table buffer is parsed using the ACPI table parser information
436 specified by a pointer to an array of ACPI_PARSER elements. This parser
437 function iterates through each item on the ACPI_PARSER array and logs the
440 This function can optionally be used to parse ACPI tables and fetch specific
441 field values. The ItemPtr member of the ACPI_PARSER structure (where used)
442 is updated by this parser function to point to the selected field data
443 (e.g. useful for variable length nested fields).
445 @param [in] Trace Trace the ACPI fields TRUE else only parse the
447 @param [in] Indent Number of spaces to indent the output.
448 @param [in] AsciiName Optional pointer to an ASCII string that describes
449 the table being parsed.
450 @param [in] Ptr Pointer to the start of the buffer.
451 @param [in] Length Length of the buffer pointed by Ptr.
452 @param [in] Parser Pointer to an array of ACPI_PARSER structure that
453 describes the table being parsed.
454 @param [in] ParserItems Number of items in the ACPI_PARSER array.
456 @retval Number of bytes parsed.
463 IN CONST CHAR8
* AsciiName OPTIONAL
,
466 IN CONST ACPI_PARSER
* Parser
,
467 IN UINT32 ParserItems
473 UINTN OriginalAttribute
;
477 // Increment the Indent
480 if (Trace
&& (AsciiName
!= NULL
)){
481 HighLight
= GetColourHighlighting ();
484 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
485 gST
->ConOut
->SetAttribute (
487 EFI_TEXT_ATTR(EFI_YELLOW
,
488 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
495 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
),
499 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
503 for (Index
= 0; Index
< ParserItems
; Index
++) {
504 if ((Offset
+ Parser
[Index
].Length
) > Length
) {
505 // We don't parse past the end of the max length specified
509 if (GetConsistencyChecking () &&
510 (Offset
!= Parser
[Index
].Offset
)) {
511 IncrementErrorCount ();
513 L
"\nERROR: %a: Offset Mismatch for %s\n"
514 L
"CurrentOffset = %d FieldOffset = %d\n",
516 Parser
[Index
].NameStr
,
523 // if there is a Formatter function let the function handle
524 // the printing else if a Format is specified in the table use
525 // the Format for printing
526 PrintFieldName (2, Parser
[Index
].NameStr
);
527 if (Parser
[Index
].PrintFormatter
!= NULL
) {
528 Parser
[Index
].PrintFormatter (Parser
[Index
].Format
, Ptr
);
529 } else if (Parser
[Index
].Format
!= NULL
) {
530 switch (Parser
[Index
].Length
) {
532 DumpUint8 (Parser
[Index
].Format
, Ptr
);
535 DumpUint16 (Parser
[Index
].Format
, Ptr
);
538 DumpUint32 (Parser
[Index
].Format
, Ptr
);
541 DumpUint64 (Parser
[Index
].Format
, Ptr
);
545 L
"\nERROR: %a: CANNOT PARSE THIS FIELD, Field Length = %d\n",
551 // Validating only makes sense if we are tracing
552 // the parsed table entries, to report by table name.
553 if (GetConsistencyChecking () &&
554 (Parser
[Index
].FieldValidator
!= NULL
)) {
555 Parser
[Index
].FieldValidator (Ptr
, Parser
[Index
].Context
);
561 if (Parser
[Index
].ItemPtr
!= NULL
) {
562 *Parser
[Index
].ItemPtr
= (VOID
*)Ptr
;
565 Ptr
+= Parser
[Index
].Length
;
566 Offset
+= Parser
[Index
].Length
;
569 // Decrement the Indent
575 An array describing the ACPI Generic Address Structure.
576 The GasParser array is used by the ParseAcpi function to parse and/or trace
579 STATIC CONST ACPI_PARSER GasParser
[] = {
580 {L
"Address Space ID", 1, 0, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
581 {L
"Register Bit Width", 1, 1, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
582 {L
"Register Bit Offset", 1, 2, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
583 {L
"Address Size", 1, 3, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
584 {L
"Address", 8, 4, L
"0x%lx", NULL
, NULL
, NULL
, NULL
}
588 This function indents and traces the GAS structure as described by the GasParser.
590 @param [in] Ptr Pointer to the start of the buffer.
591 @param [in] Indent Number of spaces to indent the output.
592 @param [in] Length Length of the GAS structure buffer.
594 @retval Number of bytes parsed.
611 PARSER_PARAMS (GasParser
)
616 This function traces the GAS structure as described by the GasParser.
618 @param [in] Format Optional format string for tracing the data.
619 @param [in] Ptr Pointer to the start of the buffer.
624 IN CONST CHAR16
* Format OPTIONAL
,
628 DumpGasStruct (Ptr
, 2, GAS_LENGTH
);
632 This function traces the ACPI header as described by the AcpiHeaderParser.
634 @param [in] Ptr Pointer to the start of the buffer.
636 @retval Number of bytes parsed.
649 ACPI_DESCRIPTION_HEADER_LENGTH
,
650 PARSER_PARAMS (AcpiHeaderParser
)
655 This function parses the ACPI header as described by the AcpiHeaderParser.
657 This function optionally returns the signature, length and revision of the
660 @param [in] Ptr Pointer to the start of the buffer.
661 @param [out] Signature Gets location of the ACPI table signature.
662 @param [out] Length Gets location of the length of the ACPI table.
663 @param [out] Revision Gets location of the revision of the ACPI table.
665 @retval Number of bytes parsed.
671 OUT CONST UINT32
** Signature
,
672 OUT CONST UINT32
** Length
,
673 OUT CONST UINT8
** Revision
678 BytesParsed
= ParseAcpi (
683 ACPI_DESCRIPTION_HEADER_LENGTH
,
684 PARSER_PARAMS (AcpiHeaderParser
)
687 *Signature
= AcpiHdrInfo
.Signature
;
688 *Length
= AcpiHdrInfo
.Length
;
689 *Revision
= AcpiHdrInfo
.Revision
;