4 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <Library/UefiLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include "AcpiParser.h"
20 STATIC UINT32 gIndent
;
21 STATIC UINT32 mTableErrorCount
;
22 STATIC UINT32 mTableWarningCount
;
24 /** This function resets the ACPI table error counter to Zero.
34 /** This function returns the ACPI table error count.
36 @retval Returns the count of errors detected in the ACPI tables.
43 return mTableErrorCount
;
46 /** This function resets the ACPI table warning counter to Zero.
53 mTableWarningCount
= 0;
56 /** This function returns the ACPI table warning count.
58 @retval Returns the count of warning detected in the ACPI tables.
65 return mTableWarningCount
;
68 /** This function increments the ACPI table error counter.
79 /** This function increments the ACPI table warning counter.
83 IncrementWarningCount (
90 /** This function verifies the ACPI table checksum.
92 This function verifies the checksum for the ACPI table and optionally
95 @param [in] Log If TRUE log the status of the checksum.
96 @param [in] Ptr Pointer to the start of the table buffer.
97 @param [in] Length The length of the buffer.
99 @retval TRUE The checksum is OK.
100 @retval FALSE The checksum failed.
112 UINTN OriginalAttribute
;
114 while (ByteCount
< Length
) {
115 Checksum
+= *(Ptr
++);
120 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
122 if (GetColourHighlighting ()) {
123 gST
->ConOut
->SetAttribute (
125 EFI_TEXT_ATTR (EFI_GREEN
,
126 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
129 Print (L
"\n\nTable Checksum : OK\n\n");
131 IncrementErrorCount ();
132 if (GetColourHighlighting ()) {
133 gST
->ConOut
->SetAttribute (
135 EFI_TEXT_ATTR (EFI_RED
,
136 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
139 Print (L
"\n\nTable Checksum : FAILED (0x%X)\n\n", Checksum
);
141 if (GetColourHighlighting ()) {
142 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
146 return (Checksum
== 0);
149 /** This function performs a raw data dump of the ACPI table.
151 @param [in] Ptr Pointer to the start of the table buffer.
152 @param [in] Length The length of the buffer.
163 UINTN AsciiBufferIndex
= 0;
164 CHAR8 AsciiBuffer
[17];
166 Print (L
"Address : 0x%p\n", Ptr
);
167 Print (L
"Length : %d\n", Length
);
169 while (ByteCount
< Length
) {
170 if ((ByteCount
& 0x0F) == 0) {
171 AsciiBuffer
[AsciiBufferIndex
] = '\0';
172 Print (L
" %a\n%08X : ", AsciiBuffer
, ByteCount
);
173 AsciiBufferIndex
= 0;
174 } else if ((ByteCount
& 0x07) == 0) {
178 if ((*Ptr
>= ' ') && (*Ptr
< 0x7F)) {
179 AsciiBuffer
[AsciiBufferIndex
++] = *Ptr
;
181 AsciiBuffer
[AsciiBufferIndex
++] = '.';
184 Print (L
"%02X ", *Ptr
++);
189 // Justify the final line using spaces before printing
191 PartLineChars
= (Length
& 0x0F);
192 if (PartLineChars
!= 0) {
193 PartLineChars
= 48 - (PartLineChars
* 3);
194 if ((Length
& 0x0F) <= 8) {
197 while (PartLineChars
> 0) {
203 // Print ASCII data for the final line.
204 AsciiBuffer
[AsciiBufferIndex
] = '\0';
205 Print (L
" %a", AsciiBuffer
);
208 /** This function traces 1 byte of data as specified in the
211 @param [in] Format The format string for tracing the data.
212 @param [in] Ptr Pointer to the start of the buffer.
217 IN CONST CHAR16
* Format
,
221 Print (Format
, *Ptr
);
224 /** This function traces 2 bytes of data as specified in the
227 @param [in] Format The format string for tracing the data.
228 @param [in] Ptr Pointer to the start of the buffer.
233 IN CONST CHAR16
* Format
,
237 Print (Format
, *(UINT16
*)Ptr
);
240 /** This function traces 4 bytes of data as specified in the
243 @param [in] Format The format string for tracing the data.
244 @param [in] Ptr Pointer to the start of the buffer.
249 IN CONST CHAR16
* Format
,
253 Print (Format
, *(UINT32
*)Ptr
);
256 /** This function traces 8 bytes of data as specified by the
259 @param [in] Format The format string for tracing the data.
260 @param [in] Ptr Pointer to the start of the buffer.
265 IN CONST CHAR16
* Format
,
269 // Some fields are not aligned and this causes alignment faults
270 // on ARM platforms if the compiler generates LDRD instructions.
271 // Perform word access so that LDRD instructions are not generated.
272 UINT64 Val
= *(UINT32
*)(Ptr
+ sizeof (UINT32
));
274 Val
|= *(UINT32
*)Ptr
;
279 /** This function traces 3 characters which can be optionally
280 formated using the format string if specified.
282 If no format string is specified the Format must be NULL.
284 @param [in] Format Optional format string for tracing the data.
285 @param [in] Ptr Pointer to the start of the buffer.
290 IN CONST CHAR16
* Format OPTIONAL
,
295 (Format
!= NULL
) ? Format
: L
"%c%c%c",
302 /** This function traces 4 characters which can be optionally
303 formated using the format string if specified.
305 If no format string is specified the Format must be NULL.
307 @param [in] Format Optional format string for tracing the data.
308 @param [in] Ptr Pointer to the start of the buffer.
313 IN CONST CHAR16
* Format OPTIONAL
,
318 (Format
!= NULL
) ? Format
: L
"%c%c%c%c",
326 /** This function traces 6 characters which can be optionally
327 formated using the format string if specified.
329 If no format string is specified the Format must be NULL.
331 @param [in] Format Optional format string for tracing the data.
332 @param [in] Ptr Pointer to the start of the buffer.
337 IN CONST CHAR16
* Format OPTIONAL
,
342 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c",
352 /** This function traces 8 characters which can be optionally
353 formated using the format string if specified.
355 If no format string is specified the Format must be NULL.
357 @param [in] Format Optional format string for tracing the data.
358 @param [in] Ptr Pointer to the start of the buffer.
363 IN CONST CHAR16
* Format OPTIONAL
,
368 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c%c%c",
380 /** This function indents and prints the ACPI table Field Name.
382 @param [in] Indent Number of spaces to add to the global table indent.
383 The global table indent is 0 by default; however
384 this value is updated on entry to the ParseAcpi()
385 by adding the indent value provided to ParseAcpi()
386 and restored back on exit.
387 Therefore the total indent in the output is
388 dependent on from where this function is called.
389 @param [in] FieldName Pointer to the Field Name.
395 IN CONST CHAR16
* FieldName
402 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
- Indent
),
407 /** This function is used to parse an ACPI table buffer.
409 The ACPI table buffer is parsed using the ACPI table parser information
410 specified by a pointer to an array of ACPI_PARSER elements. This parser
411 function iterates through each item on the ACPI_PARSER array and logs the
414 This function can optionally be used to parse ACPI tables and fetch specific
415 field values. The ItemPtr member of the ACPI_PARSER structure (where used)
416 is updated by this parser function to point to the selected field data
417 (e.g. useful for variable length nested fields).
419 @param [in] Trace Trace the ACPI fields TRUE else only parse the
421 @param [in] Indent Number of spaces to indent the output.
422 @param [in] AsciiName Optional pointer to an ASCII string that describes
423 the table being parsed.
424 @param [in] Ptr Pointer to the start of the buffer.
425 @param [in] Length Length of the buffer pointed by Ptr.
426 @param [in] Parser Pointer to an array of ACPI_PARSER structure that
427 describes the table being parsed.
428 @param [in] ParserItems Number of items in the ACPI_PARSER array.
430 @retval Number of bytes parsed.
437 IN CONST CHAR8
* AsciiName OPTIONAL
,
440 IN CONST ACPI_PARSER
* Parser
,
441 IN UINT32 ParserItems
447 // Increment the Indent
450 if (Trace
&& (AsciiName
!= NULL
)){
451 BOOLEAN HighLight
= GetColourHighlighting ();
452 UINTN OriginalAttribute
;
455 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
456 gST
->ConOut
->SetAttribute (
458 EFI_TEXT_ATTR(EFI_YELLOW
,
459 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
466 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
),
470 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
474 for (Index
= 0; Index
< ParserItems
; Index
++) {
475 if ((Offset
+ Parser
[Index
].Length
) > Length
) {
476 // We don't parse past the end of the max length specified
480 if (Offset
!= Parser
[Index
].Offset
) {
481 IncrementErrorCount ();
483 L
"\nERROR: %a: Offset Mismatch for %s\n"
484 "CurrentOffset = %d FieldOffset = %d\n",
486 Parser
[Index
].NameStr
,
493 // if there is a Formatter function let the function handle
494 // the printing else if a Format is specified in the table use
495 // the Format for printing
496 PrintFieldName (2, Parser
[Index
].NameStr
);
497 if (Parser
[Index
].PrintFormatter
!= NULL
) {
498 Parser
[Index
].PrintFormatter (Parser
[Index
].Format
, Ptr
);
499 } else if (Parser
[Index
].Format
!= NULL
) {
500 switch (Parser
[Index
].Length
) {
502 DumpUint8 (Parser
[Index
].Format
, Ptr
);
505 DumpUint16 (Parser
[Index
].Format
, Ptr
);
508 DumpUint32 (Parser
[Index
].Format
, Ptr
);
511 DumpUint64 (Parser
[Index
].Format
, Ptr
);
515 L
"\nERROR: %a: CANNOT PARSE THIS FIELD, Field Length = %d\n",
521 // Validating only makes sense if we are tracing
522 // the parsed table entries, to report by table name.
523 if (Parser
[Index
].FieldValidator
!= NULL
) {
524 Parser
[Index
].FieldValidator (Ptr
, Parser
[Index
].Context
);
530 if (Parser
[Index
].ItemPtr
!= NULL
) {
531 *Parser
[Index
].ItemPtr
= (VOID
*)Ptr
;
534 Ptr
+= Parser
[Index
].Length
;
535 Offset
+= Parser
[Index
].Length
;
538 // Decrement the Indent
543 /** An array describing the ACPI Generic Address Structure.
544 The GasParser array is used by the ParseAcpi function to parse and/or trace
547 STATIC CONST ACPI_PARSER GasParser
[] = {
548 {L
"Address Space ID", 1, 0, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
549 {L
"Register Bit Width", 1, 1, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
550 {L
"Register Bit Offset", 1, 2, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
551 {L
"Address Size", 1, 3, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
552 {L
"Address", 8, 4, L
"0x%lx", NULL
, NULL
, NULL
, NULL
}
555 /** This function indents and traces the GAS structure as described
558 @param [in] Ptr Pointer to the start of the buffer.
559 @param [in] Indent Number of spaces to indent the output.
575 PARSER_PARAMS (GasParser
)
579 /** This function traces the GAS structure as described by the GasParser.
581 @param [in] Format Optional format string for tracing the data.
582 @param [in] Ptr Pointer to the start of the buffer.
587 IN CONST CHAR16
* Format OPTIONAL
,
591 DumpGasStruct (Ptr
, 2);
594 /** This function traces the ACPI header as described by the AcpiHeaderParser.
596 @param [in] Ptr Pointer to the start of the buffer.
598 @retval Number of bytes parsed.
606 ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo
;
607 ACPI_PARSER AcpiHeaderParser
[] = {
608 PARSE_ACPI_HEADER (&AcpiHdrInfo
)
616 ACPI_DESCRIPTION_HEADER_LENGTH
,
617 PARSER_PARAMS (AcpiHeaderParser
)
621 /** This function parses the ACPI header as described by the AcpiHeaderParser.
623 This function optionally returns the signature, length and revision of the
626 @param [in] Ptr Pointer to the start of the buffer.
627 @param [out] Signature Gets location of the ACPI table signature.
628 @param [out] Length Gets location of the length of the ACPI table.
629 @param [out] Revision Gets location of the revision of the ACPI table.
631 @retval Number of bytes parsed.
637 OUT CONST UINT32
** Signature
,
638 OUT CONST UINT32
** Length
,
639 OUT CONST UINT8
** Revision
643 ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo
;
644 ACPI_PARSER AcpiHeaderParser
[] = {
645 PARSE_ACPI_HEADER (&AcpiHdrInfo
)
648 BytesParsed
= ParseAcpi (
653 ACPI_DESCRIPTION_HEADER_LENGTH
,
654 PARSER_PARAMS (AcpiHeaderParser
)
657 *Signature
= AcpiHdrInfo
.Signature
;
658 *Length
= AcpiHdrInfo
.Length
;
659 *Revision
= AcpiHdrInfo
.Revision
;