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
;
125 // set local variables to suppress incorrect compiler/analyzer warnings
127 OriginalAttribute
= 0;
131 while (ByteCount
< Length
) {
132 Checksum
+= *(Ptr
++);
137 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
139 if (GetColourHighlighting ()) {
140 gST
->ConOut
->SetAttribute (
142 EFI_TEXT_ATTR (EFI_GREEN
,
143 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
146 Print (L
"Table Checksum : OK\n\n");
148 IncrementErrorCount ();
149 if (GetColourHighlighting ()) {
150 gST
->ConOut
->SetAttribute (
152 EFI_TEXT_ATTR (EFI_RED
,
153 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
156 Print (L
"Table Checksum : FAILED (0x%X)\n\n", Checksum
);
158 if (GetColourHighlighting ()) {
159 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
163 return (Checksum
== 0);
167 This function performs a raw data dump of the ACPI table.
169 @param [in] Ptr Pointer to the start of the table buffer.
170 @param [in] Length The length of the buffer.
181 UINTN AsciiBufferIndex
;
182 CHAR8 AsciiBuffer
[17];
185 AsciiBufferIndex
= 0;
187 Print (L
"Address : 0x%p\n", Ptr
);
188 Print (L
"Length : %d\n", Length
);
190 while (ByteCount
< Length
) {
191 if ((ByteCount
& 0x0F) == 0) {
192 AsciiBuffer
[AsciiBufferIndex
] = '\0';
193 Print (L
" %a\n%08X : ", AsciiBuffer
, ByteCount
);
194 AsciiBufferIndex
= 0;
195 } else if ((ByteCount
& 0x07) == 0) {
199 if ((*Ptr
>= ' ') && (*Ptr
< 0x7F)) {
200 AsciiBuffer
[AsciiBufferIndex
++] = *Ptr
;
202 AsciiBuffer
[AsciiBufferIndex
++] = '.';
205 Print (L
"%02X ", *Ptr
++);
210 // Justify the final line using spaces before printing
212 PartLineChars
= (Length
& 0x0F);
213 if (PartLineChars
!= 0) {
214 PartLineChars
= 48 - (PartLineChars
* 3);
215 if ((Length
& 0x0F) <= 8) {
218 while (PartLineChars
> 0) {
224 // Print ASCII data for the final line.
225 AsciiBuffer
[AsciiBufferIndex
] = '\0';
226 Print (L
" %a\n\n", AsciiBuffer
);
230 This function traces 1 byte of data as specified in the format string.
232 @param [in] Format The format string for tracing the data.
233 @param [in] Ptr Pointer to the start of the buffer.
238 IN CONST CHAR16
* Format
,
242 Print (Format
, *Ptr
);
246 This function traces 2 bytes of data as specified in the format string.
248 @param [in] Format The format string for tracing the data.
249 @param [in] Ptr Pointer to the start of the buffer.
254 IN CONST CHAR16
* Format
,
258 Print (Format
, *(UINT16
*)Ptr
);
262 This function traces 4 bytes of data as specified in the format string.
264 @param [in] Format The format string for tracing the data.
265 @param [in] Ptr Pointer to the start of the buffer.
270 IN CONST CHAR16
* Format
,
274 Print (Format
, *(UINT32
*)Ptr
);
278 This function traces 8 bytes of data as specified by the format string.
280 @param [in] Format The format string for tracing the data.
281 @param [in] Ptr Pointer to the start of the buffer.
286 IN CONST CHAR16
* Format
,
290 // Some fields are not aligned and this causes alignment faults
291 // on ARM platforms if the compiler generates LDRD instructions.
292 // Perform word access so that LDRD instructions are not generated.
295 Val
= *(UINT32
*)(Ptr
+ sizeof (UINT32
));
297 Val
= LShiftU64(Val
,32);
298 Val
|= (UINT64
)*(UINT32
*)Ptr
;
304 This function traces 3 characters which can be optionally
305 formated using the format string if specified.
307 If no format string is specified the Format must be NULL.
309 @param [in] Format Optional format string for tracing the data.
310 @param [in] Ptr Pointer to the start of the buffer.
315 IN CONST CHAR16
* Format OPTIONAL
,
320 (Format
!= NULL
) ? Format
: L
"%c%c%c",
328 This function traces 4 characters which can be optionally
329 formated using the format string if specified.
331 If no format string is specified the Format must be NULL.
333 @param [in] Format Optional format string for tracing the data.
334 @param [in] Ptr Pointer to the start of the buffer.
339 IN CONST CHAR16
* Format OPTIONAL
,
344 (Format
!= NULL
) ? Format
: L
"%c%c%c%c",
353 This function traces 6 characters which can be optionally
354 formated using the format string if specified.
356 If no format string is specified the Format must be NULL.
358 @param [in] Format Optional format string for tracing the data.
359 @param [in] Ptr Pointer to the start of the buffer.
364 IN CONST CHAR16
* Format OPTIONAL
,
369 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c",
380 This function traces 8 characters which can be optionally
381 formated using the format string if specified.
383 If no format string is specified the Format must be NULL.
385 @param [in] Format Optional format string for tracing the data.
386 @param [in] Ptr Pointer to the start of the buffer.
391 IN CONST CHAR16
* Format OPTIONAL
,
396 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c%c%c",
409 This function indents and prints the ACPI table Field Name.
411 @param [in] Indent Number of spaces to add to the global table indent.
412 The global table indent is 0 by default; however
413 this value is updated on entry to the ParseAcpi()
414 by adding the indent value provided to ParseAcpi()
415 and restored back on exit.
416 Therefore the total indent in the output is
417 dependent on from where this function is called.
418 @param [in] FieldName Pointer to the Field Name.
424 IN CONST CHAR16
* FieldName
431 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
- Indent
),
437 This function is used to parse an ACPI table buffer.
439 The ACPI table buffer is parsed using the ACPI table parser information
440 specified by a pointer to an array of ACPI_PARSER elements. This parser
441 function iterates through each item on the ACPI_PARSER array and logs the
444 This function can optionally be used to parse ACPI tables and fetch specific
445 field values. The ItemPtr member of the ACPI_PARSER structure (where used)
446 is updated by this parser function to point to the selected field data
447 (e.g. useful for variable length nested fields).
449 @param [in] Trace Trace the ACPI fields TRUE else only parse the
451 @param [in] Indent Number of spaces to indent the output.
452 @param [in] AsciiName Optional pointer to an ASCII string that describes
453 the table being parsed.
454 @param [in] Ptr Pointer to the start of the buffer.
455 @param [in] Length Length of the buffer pointed by Ptr.
456 @param [in] Parser Pointer to an array of ACPI_PARSER structure that
457 describes the table being parsed.
458 @param [in] ParserItems Number of items in the ACPI_PARSER array.
460 @retval Number of bytes parsed.
467 IN CONST CHAR8
* AsciiName OPTIONAL
,
470 IN CONST ACPI_PARSER
* Parser
,
471 IN UINT32 ParserItems
477 UINTN OriginalAttribute
;
480 // set local variables to suppress incorrect compiler/analyzer warnings
482 OriginalAttribute
= 0;
485 // Increment the Indent
488 if (Trace
&& (AsciiName
!= NULL
)){
489 HighLight
= GetColourHighlighting ();
492 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
493 gST
->ConOut
->SetAttribute (
495 EFI_TEXT_ATTR(EFI_YELLOW
,
496 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
503 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
),
507 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
511 for (Index
= 0; Index
< ParserItems
; Index
++) {
512 if ((Offset
+ Parser
[Index
].Length
) > Length
) {
513 // We don't parse past the end of the max length specified
517 if (GetConsistencyChecking () &&
518 (Offset
!= Parser
[Index
].Offset
)) {
519 IncrementErrorCount ();
521 L
"\nERROR: %a: Offset Mismatch for %s\n"
522 L
"CurrentOffset = %d FieldOffset = %d\n",
524 Parser
[Index
].NameStr
,
531 // if there is a Formatter function let the function handle
532 // the printing else if a Format is specified in the table use
533 // the Format for printing
534 PrintFieldName (2, Parser
[Index
].NameStr
);
535 if (Parser
[Index
].PrintFormatter
!= NULL
) {
536 Parser
[Index
].PrintFormatter (Parser
[Index
].Format
, Ptr
);
537 } else if (Parser
[Index
].Format
!= NULL
) {
538 switch (Parser
[Index
].Length
) {
540 DumpUint8 (Parser
[Index
].Format
, Ptr
);
543 DumpUint16 (Parser
[Index
].Format
, Ptr
);
546 DumpUint32 (Parser
[Index
].Format
, Ptr
);
549 DumpUint64 (Parser
[Index
].Format
, Ptr
);
553 L
"\nERROR: %a: CANNOT PARSE THIS FIELD, Field Length = %d\n",
559 // Validating only makes sense if we are tracing
560 // the parsed table entries, to report by table name.
561 if (GetConsistencyChecking () &&
562 (Parser
[Index
].FieldValidator
!= NULL
)) {
563 Parser
[Index
].FieldValidator (Ptr
, Parser
[Index
].Context
);
569 if (Parser
[Index
].ItemPtr
!= NULL
) {
570 *Parser
[Index
].ItemPtr
= (VOID
*)Ptr
;
573 Ptr
+= Parser
[Index
].Length
;
574 Offset
+= Parser
[Index
].Length
;
577 // Decrement the Indent
583 An array describing the ACPI Generic Address Structure.
584 The GasParser array is used by the ParseAcpi function to parse and/or trace
587 STATIC CONST ACPI_PARSER GasParser
[] = {
588 {L
"Address Space ID", 1, 0, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
589 {L
"Register Bit Width", 1, 1, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
590 {L
"Register Bit Offset", 1, 2, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
591 {L
"Address Size", 1, 3, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
592 {L
"Address", 8, 4, L
"0x%lx", NULL
, NULL
, NULL
, NULL
}
596 This function indents and traces the GAS structure as described by the GasParser.
598 @param [in] Ptr Pointer to the start of the buffer.
599 @param [in] Indent Number of spaces to indent the output.
600 @param [in] Length Length of the GAS structure buffer.
602 @retval Number of bytes parsed.
619 PARSER_PARAMS (GasParser
)
624 This function traces the GAS structure as described by the GasParser.
626 @param [in] Format Optional format string for tracing the data.
627 @param [in] Ptr Pointer to the start of the buffer.
632 IN CONST CHAR16
* Format OPTIONAL
,
636 DumpGasStruct (Ptr
, 2, GAS_LENGTH
);
640 This function traces the ACPI header as described by the AcpiHeaderParser.
642 @param [in] Ptr Pointer to the start of the buffer.
644 @retval Number of bytes parsed.
657 ACPI_DESCRIPTION_HEADER_LENGTH
,
658 PARSER_PARAMS (AcpiHeaderParser
)
663 This function parses the ACPI header as described by the AcpiHeaderParser.
665 This function optionally returns the signature, length and revision of the
668 @param [in] Ptr Pointer to the start of the buffer.
669 @param [out] Signature Gets location of the ACPI table signature.
670 @param [out] Length Gets location of the length of the ACPI table.
671 @param [out] Revision Gets location of the revision of the ACPI table.
673 @retval Number of bytes parsed.
679 OUT CONST UINT32
** Signature
,
680 OUT CONST UINT32
** Length
,
681 OUT CONST UINT8
** Revision
686 BytesParsed
= ParseAcpi (
691 ACPI_DESCRIPTION_HEADER_LENGTH
,
692 PARSER_PARAMS (AcpiHeaderParser
)
695 *Signature
= AcpiHdrInfo
.Signature
;
696 *Length
= AcpiHdrInfo
.Length
;
697 *Revision
= AcpiHdrInfo
.Revision
;