]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.c
ShellPkg: Add acpiview tool to dump ACPI tables
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / AcpiParser.c
1 /**
2 ACPI parser
3
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
9
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.
12 **/
13
14 #include <Uefi.h>
15 #include <Library/UefiLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include "AcpiParser.h"
18 #include "AcpiView.h"
19
20 STATIC UINT32 gIndent;
21 STATIC UINT32 mTableErrorCount;
22 STATIC UINT32 mTableWarningCount;
23
24 /** This function resets the ACPI table error counter to Zero.
25 */
26 VOID
27 ResetErrorCount (
28 VOID
29 )
30 {
31 mTableErrorCount = 0;
32 }
33
34 /** This function returns the ACPI table error count.
35
36 @retval Returns the count of errors detected in the ACPI tables.
37 */
38 UINT32
39 GetErrorCount (
40 VOID
41 )
42 {
43 return mTableErrorCount;
44 }
45
46 /** This function resets the ACPI table warning counter to Zero.
47 */
48 VOID
49 ResetWarningCount (
50 VOID
51 )
52 {
53 mTableWarningCount = 0;
54 }
55
56 /** This function returns the ACPI table warning count.
57
58 @retval Returns the count of warning detected in the ACPI tables.
59 */
60 UINT32
61 GetWarningCount (
62 VOID
63 )
64 {
65 return mTableWarningCount;
66 }
67
68 /** This function increments the ACPI table error counter.
69 */
70 VOID
71 EFIAPI
72 IncrementErrorCount (
73 VOID
74 )
75 {
76 mTableErrorCount++;
77 }
78
79 /** This function increments the ACPI table warning counter.
80 */
81 VOID
82 EFIAPI
83 IncrementWarningCount (
84 VOID
85 )
86 {
87 mTableWarningCount++;
88 }
89
90 /** This function verifies the ACPI table checksum.
91
92 This function verifies the checksum for the ACPI table and optionally
93 prints the status.
94
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.
98
99 @retval TRUE The checksum is OK.
100 @retval FALSE The checksum failed.
101 */
102 BOOLEAN
103 EFIAPI
104 VerifyChecksum (
105 IN BOOLEAN Log,
106 IN UINT8* Ptr,
107 IN UINT32 Length
108 )
109 {
110 UINTN ByteCount = 0;
111 UINT8 Checksum = 0;
112 UINTN OriginalAttribute;
113
114 while (ByteCount < Length) {
115 Checksum += *(Ptr++);
116 ByteCount++;
117 }
118
119 if (Log) {
120 OriginalAttribute = gST->ConOut->Mode->Attribute;
121 if (Checksum == 0) {
122 if (GetColourHighlighting ()) {
123 gST->ConOut->SetAttribute (
124 gST->ConOut,
125 EFI_TEXT_ATTR (EFI_GREEN,
126 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))
127 );
128 }
129 Print (L"\n\nTable Checksum : OK\n\n");
130 } else {
131 IncrementErrorCount ();
132 if (GetColourHighlighting ()) {
133 gST->ConOut->SetAttribute (
134 gST->ConOut,
135 EFI_TEXT_ATTR (EFI_RED,
136 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))
137 );
138 }
139 Print (L"\n\nTable Checksum : FAILED (0x%X)\n\n", Checksum);
140 }
141 if (GetColourHighlighting ()) {
142 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);
143 }
144 }
145
146 return (Checksum == 0);
147 }
148
149 /** This function performs a raw data dump of the ACPI table.
150
151 @param [in] Ptr Pointer to the start of the table buffer.
152 @param [in] Length The length of the buffer.
153 */
154 VOID
155 EFIAPI
156 DumpRaw (
157 IN UINT8* Ptr,
158 IN UINT32 Length
159 )
160 {
161 UINTN ByteCount = 0;
162 UINTN PartLineChars;
163 UINTN AsciiBufferIndex = 0;
164 CHAR8 AsciiBuffer[17];
165
166 Print (L"Address : 0x%p\n", Ptr);
167 Print (L"Length : %d\n", Length);
168
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) {
175 Print (L"- ");
176 }
177
178 if ((*Ptr >= ' ') && (*Ptr < 0x7F)) {
179 AsciiBuffer[AsciiBufferIndex++] = *Ptr;
180 } else {
181 AsciiBuffer[AsciiBufferIndex++] = '.';
182 }
183
184 Print (L"%02X ", *Ptr++);
185
186 ByteCount++;
187 }
188
189 // Justify the final line using spaces before printing
190 // the ASCII data.
191 PartLineChars = (Length & 0x0F);
192 if (PartLineChars != 0) {
193 PartLineChars = 48 - (PartLineChars * 3);
194 if ((Length & 0x0F) <= 8) {
195 PartLineChars += 2;
196 }
197 while (PartLineChars > 0) {
198 Print (L" ");
199 PartLineChars--;
200 }
201 }
202
203 // Print ASCII data for the final line.
204 AsciiBuffer[AsciiBufferIndex] = '\0';
205 Print (L" %a", AsciiBuffer);
206 }
207
208 /** This function traces 1 byte of data as specified in the
209 format string.
210
211 @param [in] Format The format string for tracing the data.
212 @param [in] Ptr Pointer to the start of the buffer.
213 */
214 VOID
215 EFIAPI
216 DumpUint8 (
217 IN CONST CHAR16* Format,
218 IN UINT8* Ptr
219 )
220 {
221 Print (Format, *Ptr);
222 }
223
224 /** This function traces 2 bytes of data as specified in the
225 format string.
226
227 @param [in] Format The format string for tracing the data.
228 @param [in] Ptr Pointer to the start of the buffer.
229 */
230 VOID
231 EFIAPI
232 DumpUint16 (
233 IN CONST CHAR16* Format,
234 IN UINT8* Ptr
235 )
236 {
237 Print (Format, *(UINT16*)Ptr);
238 }
239
240 /** This function traces 4 bytes of data as specified in the
241 format string.
242
243 @param [in] Format The format string for tracing the data.
244 @param [in] Ptr Pointer to the start of the buffer.
245 */
246 VOID
247 EFIAPI
248 DumpUint32 (
249 IN CONST CHAR16* Format,
250 IN UINT8* Ptr
251 )
252 {
253 Print (Format, *(UINT32*)Ptr);
254 }
255
256 /** This function traces 8 bytes of data as specified by the
257 format string.
258
259 @param [in] Format The format string for tracing the data.
260 @param [in] Ptr Pointer to the start of the buffer.
261 */
262 VOID
263 EFIAPI
264 DumpUint64 (
265 IN CONST CHAR16* Format,
266 IN UINT8* Ptr
267 )
268 {
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));
273 Val <<= 32;
274 Val |= *(UINT32*)Ptr;
275
276 Print (Format, Val);
277 }
278
279 /** This function traces 3 characters which can be optionally
280 formated using the format string if specified.
281
282 If no format string is specified the Format must be NULL.
283
284 @param [in] Format Optional format string for tracing the data.
285 @param [in] Ptr Pointer to the start of the buffer.
286 */
287 VOID
288 EFIAPI
289 Dump3Chars (
290 IN CONST CHAR16* Format OPTIONAL,
291 IN UINT8* Ptr
292 )
293 {
294 Print (
295 (Format != NULL) ? Format : L"%c%c%c",
296 Ptr[0],
297 Ptr[1],
298 Ptr[2]
299 );
300 }
301
302 /** This function traces 4 characters which can be optionally
303 formated using the format string if specified.
304
305 If no format string is specified the Format must be NULL.
306
307 @param [in] Format Optional format string for tracing the data.
308 @param [in] Ptr Pointer to the start of the buffer.
309 */
310 VOID
311 EFIAPI
312 Dump4Chars (
313 IN CONST CHAR16* Format OPTIONAL,
314 IN UINT8* Ptr
315 )
316 {
317 Print (
318 (Format != NULL) ? Format : L"%c%c%c%c",
319 Ptr[0],
320 Ptr[1],
321 Ptr[2],
322 Ptr[3]
323 );
324 }
325
326 /** This function traces 6 characters which can be optionally
327 formated using the format string if specified.
328
329 If no format string is specified the Format must be NULL.
330
331 @param [in] Format Optional format string for tracing the data.
332 @param [in] Ptr Pointer to the start of the buffer.
333 */
334 VOID
335 EFIAPI
336 Dump6Chars (
337 IN CONST CHAR16* Format OPTIONAL,
338 IN UINT8* Ptr
339 )
340 {
341 Print (
342 (Format != NULL) ? Format : L"%c%c%c%c%c%c",
343 Ptr[0],
344 Ptr[1],
345 Ptr[2],
346 Ptr[3],
347 Ptr[4],
348 Ptr[5]
349 );
350 }
351
352 /** This function traces 8 characters which can be optionally
353 formated using the format string if specified.
354
355 If no format string is specified the Format must be NULL.
356
357 @param [in] Format Optional format string for tracing the data.
358 @param [in] Ptr Pointer to the start of the buffer.
359 */
360 VOID
361 EFIAPI
362 Dump8Chars (
363 IN CONST CHAR16* Format OPTIONAL,
364 IN UINT8* Ptr
365 )
366 {
367 Print (
368 (Format != NULL) ? Format : L"%c%c%c%c%c%c%c%c",
369 Ptr[0],
370 Ptr[1],
371 Ptr[2],
372 Ptr[3],
373 Ptr[4],
374 Ptr[5],
375 Ptr[6],
376 Ptr[7]
377 );
378 }
379
380 /** This function indents and prints the ACPI table Field Name.
381
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.
390 */
391 VOID
392 EFIAPI
393 PrintFieldName (
394 IN UINT32 Indent,
395 IN CONST CHAR16* FieldName
396 )
397 {
398 Print (
399 L"%*a%-*s : ",
400 gIndent + Indent,
401 "",
402 (OUTPUT_FIELD_COLUMN_WIDTH - gIndent - Indent),
403 FieldName
404 );
405 }
406
407 /** This function is used to parse an ACPI table buffer.
408
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
412 ACPI table fields.
413
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).
418
419 @param [in] Trace Trace the ACPI fields TRUE else only parse the
420 table.
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.
429
430 @retval Number of bytes parsed.
431 */
432 UINT32
433 EFIAPI
434 ParseAcpi (
435 IN BOOLEAN Trace,
436 IN UINT32 Indent,
437 IN CONST CHAR8* AsciiName OPTIONAL,
438 IN UINT8* Ptr,
439 IN UINT32 Length,
440 IN CONST ACPI_PARSER* Parser,
441 IN UINT32 ParserItems
442 )
443 {
444 UINT32 Index;
445 UINT32 Offset = 0;
446
447 // Increment the Indent
448 gIndent += Indent;
449
450 if (Trace && (AsciiName != NULL)){
451 BOOLEAN HighLight = GetColourHighlighting ();
452 UINTN OriginalAttribute;
453
454 if (HighLight) {
455 OriginalAttribute = gST->ConOut->Mode->Attribute;
456 gST->ConOut->SetAttribute (
457 gST->ConOut,
458 EFI_TEXT_ATTR(EFI_YELLOW,
459 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))
460 );
461 }
462 Print (
463 L"%*a%-*a :\n",
464 gIndent,
465 "",
466 (OUTPUT_FIELD_COLUMN_WIDTH - gIndent),
467 AsciiName
468 );
469 if (HighLight) {
470 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);
471 }
472 }
473
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
477 break;
478 }
479
480 if (Offset != Parser[Index].Offset) {
481 IncrementErrorCount ();
482 Print (
483 L"\nERROR: %a: Offset Mismatch for %s\n"
484 "CurrentOffset = %d FieldOffset = %d\n",
485 AsciiName,
486 Parser[Index].NameStr,
487 Offset,
488 Parser[Index].Offset
489 );
490 }
491
492 if (Trace) {
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) {
501 case 1:
502 DumpUint8 (Parser[Index].Format, Ptr);
503 break;
504 case 2:
505 DumpUint16 (Parser[Index].Format, Ptr);
506 break;
507 case 4:
508 DumpUint32 (Parser[Index].Format, Ptr);
509 break;
510 case 8:
511 DumpUint64 (Parser[Index].Format, Ptr);
512 break;
513 default:
514 Print (
515 L"\nERROR: %a: CANNOT PARSE THIS FIELD, Field Length = %d\n",
516 AsciiName,
517 Parser[Index].Length
518 );
519 } // switch
520
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);
525 }
526 }
527 Print (L"\n");
528 } // if (Trace)
529
530 if (Parser[Index].ItemPtr != NULL) {
531 *Parser[Index].ItemPtr = (VOID*)Ptr;
532 }
533
534 Ptr += Parser[Index].Length;
535 Offset += Parser[Index].Length;
536 } // for
537
538 // Decrement the Indent
539 gIndent -= Indent;
540 return Offset;
541 }
542
543 /** An array describing the ACPI Generic Address Structure.
544 The GasParser array is used by the ParseAcpi function to parse and/or trace
545 the GAS structure.
546 */
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}
553 };
554
555 /** This function indents and traces the GAS structure as described
556 by the GasParser.
557
558 @param [in] Ptr Pointer to the start of the buffer.
559 @param [in] Indent Number of spaces to indent the output.
560 */
561 VOID
562 EFIAPI
563 DumpGasStruct (
564 IN UINT8* Ptr,
565 IN UINT32 Indent
566 )
567 {
568 Print (L"\n");
569 ParseAcpi (
570 TRUE,
571 Indent,
572 NULL,
573 Ptr,
574 GAS_LENGTH,
575 PARSER_PARAMS (GasParser)
576 );
577 }
578
579 /** This function traces the GAS structure as described by the GasParser.
580
581 @param [in] Format Optional format string for tracing the data.
582 @param [in] Ptr Pointer to the start of the buffer.
583 */
584 VOID
585 EFIAPI
586 DumpGas (
587 IN CONST CHAR16* Format OPTIONAL,
588 IN UINT8* Ptr
589 )
590 {
591 DumpGasStruct (Ptr, 2);
592 }
593
594 /** This function traces the ACPI header as described by the AcpiHeaderParser.
595
596 @param [in] Ptr Pointer to the start of the buffer.
597
598 @retval Number of bytes parsed.
599 */
600 UINT32
601 EFIAPI
602 DumpAcpiHeader (
603 IN UINT8* Ptr
604 )
605 {
606 ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
607 ACPI_PARSER AcpiHeaderParser[] = {
608 PARSE_ACPI_HEADER (&AcpiHdrInfo)
609 };
610
611 return ParseAcpi (
612 TRUE,
613 0,
614 "ACPI Table Header",
615 Ptr,
616 ACPI_DESCRIPTION_HEADER_LENGTH,
617 PARSER_PARAMS (AcpiHeaderParser)
618 );
619 }
620
621 /** This function parses the ACPI header as described by the AcpiHeaderParser.
622
623 This function optionally returns the signature, length and revision of the
624 ACPI table.
625
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.
630
631 @retval Number of bytes parsed.
632 */
633 UINT32
634 EFIAPI
635 ParseAcpiHeader (
636 IN UINT8* Ptr,
637 OUT CONST UINT32** Signature,
638 OUT CONST UINT32** Length,
639 OUT CONST UINT8** Revision
640 )
641 {
642 UINT32 BytesParsed;
643 ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
644 ACPI_PARSER AcpiHeaderParser[] = {
645 PARSE_ACPI_HEADER (&AcpiHdrInfo)
646 };
647
648 BytesParsed = ParseAcpi (
649 FALSE,
650 0,
651 NULL,
652 Ptr,
653 ACPI_DESCRIPTION_HEADER_LENGTH,
654 PARSER_PARAMS (AcpiHeaderParser)
655 );
656
657 *Signature = AcpiHdrInfo.Signature;
658 *Length = AcpiHdrInfo.Length;
659 *Revision = AcpiHdrInfo.Revision;
660
661 return BytesParsed;
662 }