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
;
25 This function resets the ACPI table error counter to Zero.
36 This function returns the ACPI table error count.
38 @retval Returns the count of errors detected in the ACPI tables.
45 return mTableErrorCount
;
49 This function resets the ACPI table warning counter to Zero.
56 mTableWarningCount
= 0;
60 This function returns the ACPI table warning count.
62 @retval Returns the count of warning detected in the ACPI tables.
69 return mTableWarningCount
;
73 This function increments the ACPI table error counter.
85 This function increments the ACPI table warning counter.
89 IncrementWarningCount (
97 This function verifies the ACPI table checksum.
99 This function verifies the checksum for the ACPI table and optionally
102 @param [in] Log If TRUE log the status of the checksum.
103 @param [in] Ptr Pointer to the start of the table buffer.
104 @param [in] Length The length of the buffer.
106 @retval TRUE The checksum is OK.
107 @retval FALSE The checksum failed.
119 UINTN OriginalAttribute
;
121 while (ByteCount
< Length
) {
122 Checksum
+= *(Ptr
++);
127 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
129 if (GetColourHighlighting ()) {
130 gST
->ConOut
->SetAttribute (
132 EFI_TEXT_ATTR (EFI_GREEN
,
133 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
136 Print (L
"\n\nTable Checksum : OK\n\n");
138 IncrementErrorCount ();
139 if (GetColourHighlighting ()) {
140 gST
->ConOut
->SetAttribute (
142 EFI_TEXT_ATTR (EFI_RED
,
143 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
146 Print (L
"\n\nTable Checksum : FAILED (0x%X)\n\n", Checksum
);
148 if (GetColourHighlighting ()) {
149 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
153 return (Checksum
== 0);
157 This function performs a raw data dump of the ACPI table.
159 @param [in] Ptr Pointer to the start of the table buffer.
160 @param [in] Length The length of the buffer.
171 UINTN AsciiBufferIndex
= 0;
172 CHAR8 AsciiBuffer
[17];
174 Print (L
"Address : 0x%p\n", Ptr
);
175 Print (L
"Length : %d\n", Length
);
177 while (ByteCount
< Length
) {
178 if ((ByteCount
& 0x0F) == 0) {
179 AsciiBuffer
[AsciiBufferIndex
] = '\0';
180 Print (L
" %a\n%08X : ", AsciiBuffer
, ByteCount
);
181 AsciiBufferIndex
= 0;
182 } else if ((ByteCount
& 0x07) == 0) {
186 if ((*Ptr
>= ' ') && (*Ptr
< 0x7F)) {
187 AsciiBuffer
[AsciiBufferIndex
++] = *Ptr
;
189 AsciiBuffer
[AsciiBufferIndex
++] = '.';
192 Print (L
"%02X ", *Ptr
++);
197 // Justify the final line using spaces before printing
199 PartLineChars
= (Length
& 0x0F);
200 if (PartLineChars
!= 0) {
201 PartLineChars
= 48 - (PartLineChars
* 3);
202 if ((Length
& 0x0F) <= 8) {
205 while (PartLineChars
> 0) {
211 // Print ASCII data for the final line.
212 AsciiBuffer
[AsciiBufferIndex
] = '\0';
213 Print (L
" %a", AsciiBuffer
);
217 This function traces 1 byte of data as specified in the format string.
219 @param [in] Format The format string for tracing the data.
220 @param [in] Ptr Pointer to the start of the buffer.
225 IN CONST CHAR16
* Format
,
229 Print (Format
, *Ptr
);
233 This function traces 2 bytes of data as specified in the format string.
235 @param [in] Format The format string for tracing the data.
236 @param [in] Ptr Pointer to the start of the buffer.
241 IN CONST CHAR16
* Format
,
245 Print (Format
, *(UINT16
*)Ptr
);
249 This function traces 4 bytes of data as specified in the format string.
251 @param [in] Format The format string for tracing the data.
252 @param [in] Ptr Pointer to the start of the buffer.
257 IN CONST CHAR16
* Format
,
261 Print (Format
, *(UINT32
*)Ptr
);
265 This function traces 8 bytes of data as specified by the format string.
267 @param [in] Format The format string for tracing the data.
268 @param [in] Ptr Pointer to the start of the buffer.
273 IN CONST CHAR16
* Format
,
277 // Some fields are not aligned and this causes alignment faults
278 // on ARM platforms if the compiler generates LDRD instructions.
279 // Perform word access so that LDRD instructions are not generated.
280 UINT64 Val
= *(UINT32
*)(Ptr
+ sizeof (UINT32
));
282 Val
|= *(UINT32
*)Ptr
;
288 This function traces 3 characters which can be optionally
289 formated using the format string if specified.
291 If no format string is specified the Format must be NULL.
293 @param [in] Format Optional format string for tracing the data.
294 @param [in] Ptr Pointer to the start of the buffer.
299 IN CONST CHAR16
* Format OPTIONAL
,
304 (Format
!= NULL
) ? Format
: L
"%c%c%c",
312 This function traces 4 characters which can be optionally
313 formated using the format string if specified.
315 If no format string is specified the Format must be NULL.
317 @param [in] Format Optional format string for tracing the data.
318 @param [in] Ptr Pointer to the start of the buffer.
323 IN CONST CHAR16
* Format OPTIONAL
,
328 (Format
!= NULL
) ? Format
: L
"%c%c%c%c",
337 This function traces 6 characters which can be optionally
338 formated using the format string if specified.
340 If no format string is specified the Format must be NULL.
342 @param [in] Format Optional format string for tracing the data.
343 @param [in] Ptr Pointer to the start of the buffer.
348 IN CONST CHAR16
* Format OPTIONAL
,
353 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c",
364 This function traces 8 characters which can be optionally
365 formated using the format string if specified.
367 If no format string is specified the Format must be NULL.
369 @param [in] Format Optional format string for tracing the data.
370 @param [in] Ptr Pointer to the start of the buffer.
375 IN CONST CHAR16
* Format OPTIONAL
,
380 (Format
!= NULL
) ? Format
: L
"%c%c%c%c%c%c%c%c",
393 This function indents and prints the ACPI table Field Name.
395 @param [in] Indent Number of spaces to add to the global table indent.
396 The global table indent is 0 by default; however
397 this value is updated on entry to the ParseAcpi()
398 by adding the indent value provided to ParseAcpi()
399 and restored back on exit.
400 Therefore the total indent in the output is
401 dependent on from where this function is called.
402 @param [in] FieldName Pointer to the Field Name.
408 IN CONST CHAR16
* FieldName
415 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
- Indent
),
421 This function is used to parse an ACPI table buffer.
423 The ACPI table buffer is parsed using the ACPI table parser information
424 specified by a pointer to an array of ACPI_PARSER elements. This parser
425 function iterates through each item on the ACPI_PARSER array and logs the
428 This function can optionally be used to parse ACPI tables and fetch specific
429 field values. The ItemPtr member of the ACPI_PARSER structure (where used)
430 is updated by this parser function to point to the selected field data
431 (e.g. useful for variable length nested fields).
433 @param [in] Trace Trace the ACPI fields TRUE else only parse the
435 @param [in] Indent Number of spaces to indent the output.
436 @param [in] AsciiName Optional pointer to an ASCII string that describes
437 the table being parsed.
438 @param [in] Ptr Pointer to the start of the buffer.
439 @param [in] Length Length of the buffer pointed by Ptr.
440 @param [in] Parser Pointer to an array of ACPI_PARSER structure that
441 describes the table being parsed.
442 @param [in] ParserItems Number of items in the ACPI_PARSER array.
444 @retval Number of bytes parsed.
451 IN CONST CHAR8
* AsciiName OPTIONAL
,
454 IN CONST ACPI_PARSER
* Parser
,
455 IN UINT32 ParserItems
461 // Increment the Indent
464 if (Trace
&& (AsciiName
!= NULL
)){
465 BOOLEAN HighLight
= GetColourHighlighting ();
466 UINTN OriginalAttribute
;
469 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
470 gST
->ConOut
->SetAttribute (
472 EFI_TEXT_ATTR(EFI_YELLOW
,
473 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
480 (OUTPUT_FIELD_COLUMN_WIDTH
- gIndent
),
484 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
488 for (Index
= 0; Index
< ParserItems
; Index
++) {
489 if ((Offset
+ Parser
[Index
].Length
) > Length
) {
490 // We don't parse past the end of the max length specified
494 if (Offset
!= Parser
[Index
].Offset
) {
495 IncrementErrorCount ();
497 L
"\nERROR: %a: Offset Mismatch for %s\n"
498 "CurrentOffset = %d FieldOffset = %d\n",
500 Parser
[Index
].NameStr
,
507 // if there is a Formatter function let the function handle
508 // the printing else if a Format is specified in the table use
509 // the Format for printing
510 PrintFieldName (2, Parser
[Index
].NameStr
);
511 if (Parser
[Index
].PrintFormatter
!= NULL
) {
512 Parser
[Index
].PrintFormatter (Parser
[Index
].Format
, Ptr
);
513 } else if (Parser
[Index
].Format
!= NULL
) {
514 switch (Parser
[Index
].Length
) {
516 DumpUint8 (Parser
[Index
].Format
, Ptr
);
519 DumpUint16 (Parser
[Index
].Format
, Ptr
);
522 DumpUint32 (Parser
[Index
].Format
, Ptr
);
525 DumpUint64 (Parser
[Index
].Format
, Ptr
);
529 L
"\nERROR: %a: CANNOT PARSE THIS FIELD, Field Length = %d\n",
535 // Validating only makes sense if we are tracing
536 // the parsed table entries, to report by table name.
537 if (Parser
[Index
].FieldValidator
!= NULL
) {
538 Parser
[Index
].FieldValidator (Ptr
, Parser
[Index
].Context
);
544 if (Parser
[Index
].ItemPtr
!= NULL
) {
545 *Parser
[Index
].ItemPtr
= (VOID
*)Ptr
;
548 Ptr
+= Parser
[Index
].Length
;
549 Offset
+= Parser
[Index
].Length
;
552 // Decrement the Indent
558 An array describing the ACPI Generic Address Structure.
559 The GasParser array is used by the ParseAcpi function to parse and/or trace
562 STATIC CONST ACPI_PARSER GasParser
[] = {
563 {L
"Address Space ID", 1, 0, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
564 {L
"Register Bit Width", 1, 1, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
565 {L
"Register Bit Offset", 1, 2, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
566 {L
"Address Size", 1, 3, L
"0x%x", NULL
, NULL
, NULL
, NULL
},
567 {L
"Address", 8, 4, L
"0x%lx", NULL
, NULL
, NULL
, NULL
}
571 This function indents and traces the GAS structure as described by the GasParser.
573 @param [in] Ptr Pointer to the start of the buffer.
574 @param [in] Indent Number of spaces to indent the output.
590 PARSER_PARAMS (GasParser
)
595 This function traces the GAS structure as described by the GasParser.
597 @param [in] Format Optional format string for tracing the data.
598 @param [in] Ptr Pointer to the start of the buffer.
603 IN CONST CHAR16
* Format OPTIONAL
,
607 DumpGasStruct (Ptr
, 2);
611 This function traces the ACPI header as described by the AcpiHeaderParser.
613 @param [in] Ptr Pointer to the start of the buffer.
615 @retval Number of bytes parsed.
623 ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo
;
624 ACPI_PARSER AcpiHeaderParser
[] = {
625 PARSE_ACPI_HEADER (&AcpiHdrInfo
)
633 ACPI_DESCRIPTION_HEADER_LENGTH
,
634 PARSER_PARAMS (AcpiHeaderParser
)
639 This function parses the ACPI header as described by the AcpiHeaderParser.
641 This function optionally returns the signature, length and revision of the
644 @param [in] Ptr Pointer to the start of the buffer.
645 @param [out] Signature Gets location of the ACPI table signature.
646 @param [out] Length Gets location of the length of the ACPI table.
647 @param [out] Revision Gets location of the revision of the ACPI table.
649 @retval Number of bytes parsed.
655 OUT CONST UINT32
** Signature
,
656 OUT CONST UINT32
** Length
,
657 OUT CONST UINT8
** Revision
661 ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo
;
662 ACPI_PARSER AcpiHeaderParser
[] = {
663 PARSE_ACPI_HEADER (&AcpiHdrInfo
)
666 BytesParsed
= ParseAcpi (
671 ACPI_DESCRIPTION_HEADER_LENGTH
,
672 PARSER_PARAMS (AcpiHeaderParser
)
675 *Signature
= AcpiHdrInfo
.Signature
;
676 *Length
= AcpiHdrInfo
.Length
;
677 *Revision
= AcpiHdrInfo
.Revision
;