summaryrefslogtreecommitdiffstats
path: root/DynamicTablesPkg/Library/Common/AmlLib/AmlDbgPrint/AmlDbgPrint.c
blob: 00a61a2fe63a1ac060cd1bd2ed3f438ca66177ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/** @file
  AML Print Function.

  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved. <BR>
  Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <AmlNodeDefines.h>
#include <AmlDbgPrint/AmlDbgPrint.h>

#include <AmlCoreInterface.h>
#include <String/AmlString.h>
#include <Tree/AmlNode.h>
#include <Tree/AmlTreeTraversal.h>

#if !defined (MDEPKG_NDEBUG)

/** String table representing AML Data types as defined by EAML_NODE_DATA_TYPE.
*/
CONST CHAR8 * NodeDataTypeStrTbl[] = {
  "EAmlNodeDataTypeNone",
  "EAmlNodeDataTypeReserved1",
  "EAmlNodeDataTypeReserved2",
  "EAmlNodeDataTypeReserved3",
  "EAmlNodeDataTypeReserved4",
  "EAmlNodeDataTypeReserved5",
  "EAmlNodeDataTypeNameString",
  "EAmlNodeDataTypeString",
  "EAmlNodeDataTypeUInt",
  "EAmlNodeDataTypeRaw",
  "EAmlNodeDataTypeResourceData",
  "EAmlNodeDataTypeFieldPkgLen",
  "EAmlNodeDataTypeMax"
};

/** String table representing AML Node types as defined by EAML_NODE_TYPE.
*/
CONST CHAR8 * NodeTypeStrTbl[] = {
  "EAmlNodeUnknown",
  "EAmlNodeRoot",
  "EAmlNodeObject",
  "EAmlNodeData",
  "EAmlNodeMax"
};

/** Print Size chars at Buffer address.

  @param  [in]  ErrorLevel    Error level for the DEBUG macro.
  @param  [in]  Buffer        Buffer containing the chars.
  @param  [in]  Size          Number of chars to print.
**/
VOID
EFIAPI
AmlDbgPrintChars (
  IN        UINT32      ErrorLevel,
  IN  CONST CHAR8     * Buffer,
  IN        UINT32      Size
  )
{
  UINT32  i;

  if (Buffer == NULL) {
    ASSERT (0);
    return;
  }

  for (i = 0; i < Size; i++) {
    DEBUG ((ErrorLevel, "%c", Buffer[i]));
  }
}

/** Print an AML NameSeg.
    Don't print trailing underscores ('_').

  @param  [in] Buffer   Buffer containing an AML NameSeg.
**/
VOID
EFIAPI
AmlDbgPrintNameSeg (
  IN  CONST CHAR8   * Buffer
  )
{
  if (Buffer == NULL) {
    ASSERT (0);
    return;
  }

  DEBUG ((DEBUG_INFO, "%c", Buffer[0]));
  if ((Buffer[1] == AML_NAME_CHAR__)  &&
      (Buffer[2] == AML_NAME_CHAR__)  &&
      (Buffer[3] == AML_NAME_CHAR__)) {
    return;
  }
  DEBUG ((DEBUG_INFO, "%c", Buffer[1]));
  if ((Buffer[2] == AML_NAME_CHAR__)  &&
      (Buffer[3] == AML_NAME_CHAR__)) {
    return;
  }
  DEBUG ((DEBUG_INFO, "%c", Buffer[2]));
  if (Buffer[3] == AML_NAME_CHAR__) {
    return;
  }
  DEBUG ((DEBUG_INFO, "%c", Buffer[3]));
  return;
}

/** Print an AML NameString.

  @param  [in] Buffer   Buffer containing an AML NameString.
  @param  [in] NewLine  Print a newline char at the end of the NameString.
**/
VOID
EFIAPI
AmlDbgPrintNameString (
  IN  CONST CHAR8   * Buffer,
  IN        BOOLEAN   NewLine
  )
{
  UINT8     SegCount;
  UINT8     Index;

  if (Buffer == NULL) {
    ASSERT (0);
    return;
  }

  // Handle Root and Parent(s).
  if (*Buffer == AML_ROOT_CHAR) {
    Buffer++;
    DEBUG ((DEBUG_INFO, "\\"));
  } else if (*Buffer == AML_PARENT_PREFIX_CHAR) {
    do {
      Buffer++;
      DEBUG ((DEBUG_INFO, "^"));
    } while (*Buffer == AML_PARENT_PREFIX_CHAR);
  }

  // Handle SegCount(s).
  if (*Buffer == AML_DUAL_NAME_PREFIX) {
    Buffer++;
    SegCount = 2;
  } else if (*Buffer == AML_MULTI_NAME_PREFIX) {
    Buffer++;
    // For multi name prefix the seg count is in the second byte.
    SegCount = *Buffer;
    Buffer++;
  } else if (AmlIsLeadNameChar (*Buffer)) {
    // Only check the first char first to avoid overflow.
    // Then the whole NameSeg can be checked.
    if (!AmlIsNameSeg (Buffer)) {
      ASSERT (0);
      return;
    }
    SegCount = 1;
  } else if (*Buffer == AML_ZERO_OP) {
    SegCount = 0;
  } else {
    // Should not be possible.
    ASSERT (0);
    return;
  }

  if (SegCount != 0) {
    AMLDBG_PRINT_NAMESEG (Buffer);
    Buffer += AML_NAME_SEG_SIZE;
    for (Index = 0; Index < SegCount - 1; Index++) {
      DEBUG ((DEBUG_INFO, "."));
      AMLDBG_PRINT_NAMESEG (Buffer);
      Buffer += AML_NAME_SEG_SIZE;
    }
  }

  if (NewLine) {
    DEBUG ((DEBUG_INFO, "\n"));
  }

  return;
}

/** Print the information contained in the header of the Node.

  @param  [in]  Node    Pointer to a node.
  @param  [in]  Level   Level of the indentation.
**/
STATIC
VOID
EFIAPI
AmlDbgPrintNodeHeader (
  IN  AML_NODE_HEADER  * Node,
  IN  UINT8              Level
  )
{
  if (!IS_AML_NODE_VALID (Node)) {
    ASSERT (0);
    return;
  }

  DEBUG ((
    DEBUG_INFO,
    "%3d | %-15s | ",
    Level,
    NodeTypeStrTbl[Node->NodeType]
    ));
}

/** Print fields of a data node.

  @param  [in]  DataNode  Pointer to a data node.
  @param  [in]  Level     Level of the indentation.
**/
STATIC
VOID
EFIAPI
AmlDbgPrintDataNode (
  IN  AML_DATA_NODE   * DataNode,
  IN  UINT8             Level
  )
{
  UINT32  Idx;

  if (!IS_AML_DATA_NODE (DataNode)) {
    ASSERT (0);
    return;
  }

  AmlDbgPrintNodeHeader ((AML_NODE_HEADER*)DataNode, Level);

  DEBUG ((DEBUG_INFO, "%-36s | ", NodeDataTypeStrTbl[DataNode->DataType]));
  DEBUG ((DEBUG_INFO, "0x%04x | ", DataNode->Size));

  if ((DataNode->DataType == EAmlNodeDataTypeNameString) ||
      (DataNode->DataType == EAmlNodeDataTypeString)) {
    AMLDBG_PRINT_CHARS (
      DEBUG_INFO,
      (CONST CHAR8*)DataNode->Buffer,
      DataNode->Size
      );
  } else if (DataNode->DataType == EAmlNodeDataTypeUInt) {
    switch (DataNode->Size) {
      case 1:
      {
        DEBUG ((DEBUG_INFO, "0x%0x", *((UINT8*)DataNode->Buffer)));
        break;
      }
      case 2:
      {
        DEBUG ((DEBUG_INFO, "0x%0x", *((UINT16*)DataNode->Buffer)));
        break;
      }
      case 4:
      {
        DEBUG ((DEBUG_INFO, "0x%0lx", *((UINT32*)DataNode->Buffer)));
        break;
      }
      case 8:
      {
        DEBUG ((DEBUG_INFO, "0x%0llx", *((UINT64*)DataNode->Buffer)));
        break;
      }
      default:
      {
        ASSERT (0);
        return;
      }
    }
  } else {
    // No specific format.
    for (Idx = 0; Idx < DataNode->Size; Idx++) {
      DEBUG ((DEBUG_INFO, "%02x ", DataNode->Buffer[Idx]));
    }
  }

  DEBUG ((DEBUG_INFO, "\n"));
}

/** Print fields of an object node.

  @param  [in]  ObjectNode  Pointer to an object node.
  @param  [in]  Level       Level of the indentation.
**/
STATIC
VOID
EFIAPI
AmlDbgPrintObjectNode (
  IN  AML_OBJECT_NODE  * ObjectNode,
  IN  UINT8              Level
  )
{
  if (!IS_AML_OBJECT_NODE (ObjectNode)) {
    ASSERT (0);
    return;
  }

  AmlDbgPrintNodeHeader ((AML_NODE_HEADER*)ObjectNode, Level);

  DEBUG ((DEBUG_INFO, "0x%02x | ", ObjectNode->AmlByteEncoding->OpCode));
  DEBUG ((DEBUG_INFO, "0x%02x | ", ObjectNode->AmlByteEncoding->SubOpCode));

  // Print a string corresponding to the field object OpCode/SubOpCode.
  if (AmlNodeHasAttribute (ObjectNode, AML_IS_FIELD_ELEMENT)) {
    DEBUG ((DEBUG_INFO, "%-15s ", AmlGetFieldOpCodeStr (
                                    ObjectNode->AmlByteEncoding->OpCode,
                                    0
                                    )));
  } else {
    // Print a string corresponding to the object OpCode/SubOpCode.
    DEBUG ((DEBUG_INFO, "%-15s | ", AmlGetOpCodeStr (
                                      ObjectNode->AmlByteEncoding->OpCode,
                                      ObjectNode->AmlByteEncoding->SubOpCode)
                                      ));
  }

  DEBUG ((DEBUG_INFO, "%3d | ", ObjectNode->AmlByteEncoding->MaxIndex));
  DEBUG ((DEBUG_INFO, "0x%08x | ", ObjectNode->AmlByteEncoding->Attribute));
  DEBUG ((DEBUG_INFO, "0x%04x | ", ObjectNode->PkgLen));
  if (AmlNodeHasAttribute (ObjectNode, AML_IN_NAMESPACE)) {
    AMLDBG_PRINT_NAMESTR (
      AmlNodeGetName ((CONST AML_OBJECT_NODE*)ObjectNode),
      FALSE
      );
  }

  DEBUG ((DEBUG_INFO, "\n"));
}

/** Print fields of a root node.

  @param  [in]  RootNode  Pointer to a root node.
  @param  [in]  Level     Level of the indentation.
**/
STATIC
VOID
EFIAPI
AmlDbgPrintRootNode (
  IN  AML_ROOT_NODE  * RootNode,
  IN  UINT8            Level
  )
{
  if (!IS_AML_ROOT_NODE (RootNode)) {
    ASSERT (0);
    return;
  }

  AmlDbgPrintNodeHeader ((AML_NODE_HEADER*)RootNode, Level);

  DEBUG ((DEBUG_INFO, "%8x | ", RootNode->SdtHeader->Signature));
  DEBUG ((DEBUG_INFO, "0x%08x | ", RootNode->SdtHeader->Length));
  DEBUG ((DEBUG_INFO, "%3d | ", RootNode->SdtHeader->Revision));
  DEBUG ((DEBUG_INFO, "0x%02x | ", RootNode->SdtHeader->Checksum));
  DEBUG ((
    DEBUG_INFO,
    "%c%c%c%c%c%c | ",
    RootNode->SdtHeader->OemId[0],
    RootNode->SdtHeader->OemId[1],
    RootNode->SdtHeader->OemId[2],
    RootNode->SdtHeader->OemId[3],
    RootNode->SdtHeader->OemId[4],
    RootNode->SdtHeader->OemId[5]
    ));
  DEBUG ((DEBUG_INFO, "%-16llx | ", RootNode->SdtHeader->OemTableId));
  DEBUG ((DEBUG_INFO, "%8x | ", RootNode->SdtHeader->OemRevision));
  DEBUG ((DEBUG_INFO, "%8x | ", RootNode->SdtHeader->CreatorId));
  DEBUG ((DEBUG_INFO, "%8x", RootNode->SdtHeader->CreatorRevision));
  DEBUG ((DEBUG_INFO, "\n"));
}

/** Print a header to help interpreting node information.
**/
STATIC
VOID
EFIAPI
AmlDbgPrintTableHeader (
  VOID
  )
{
  DEBUG ((DEBUG_INFO, "Lvl | Node Type       |\n"));
  DEBUG ((
    DEBUG_INFO,
    "    | %-15s | Signature| Length     | Rev | CSum | OemId  | "
      "OemTableId       | OemRev   | CreatorId| CreatorRev\n",
    NodeTypeStrTbl[EAmlNodeRoot]
    ));
  DEBUG ((
    DEBUG_INFO,
    "    | %-15s | Op   | SubOp| OpName          | MaxI| Attribute  | "
      "PkgLen | NodeName (opt)\n",
    NodeTypeStrTbl[EAmlNodeObject]
    ));
  DEBUG ((
    DEBUG_INFO,
    "    | %-15s | Data Type                            | Size   | "
      "Buffer\n",
    NodeTypeStrTbl[EAmlNodeData]
    ));
  DEBUG ((
    DEBUG_INFO,
    "---------------------------------------"
      "---------------------------------------\n"
    ));
}

/** Recursively print the subtree under the Node.
    This is an internal function.

  @param  [in]  Node            Pointer to the root of the subtree to print.
                                Can be a root/object/data node.
  @param  [in]  Recurse         If TRUE, recurse.
  @param  [in]  Level           Level in the tree.
**/
STATIC
VOID
EFIAPI
AmlDbgPrintTreeInternal (
  IN  AML_NODE_HEADER   * Node,
  IN  BOOLEAN             Recurse,
  IN  UINT8               Level
  )
{
  AML_NODE_HEADER   * ChildNode;

  if (!IS_AML_NODE_VALID (Node)) {
    ASSERT (0);
    return;
  }

  if (IS_AML_DATA_NODE (Node)) {
    AmlDbgPrintDataNode ((AML_DATA_NODE*)Node, Level);
    return;
  } else if (IS_AML_OBJECT_NODE (Node)) {
    AmlDbgPrintObjectNode ((AML_OBJECT_NODE*)Node, Level);
  } else if (IS_AML_ROOT_NODE (Node)) {
    AmlDbgPrintRootNode ((AML_ROOT_NODE*)Node, Level);
  } else {
    // Should not be possible.
    ASSERT (0);
    return;
  }

  if (!Recurse) {
    return;
  }

  // Get the first child node.
  ChildNode = AmlGetNextSibling (Node, NULL);
  while (ChildNode != NULL) {
    ASSERT (Level < MAX_UINT8);
    AmlDbgPrintTreeInternal (ChildNode, Recurse, (UINT8)(Level + 1));
    ChildNode = AmlGetNextSibling (Node, ChildNode);
  }
}

/** Print Node information.

  @param  [in]  Node    Pointer to the Node to print.
                        Can be a root/object/data node.
**/
VOID
EFIAPI
AmlDbgPrintNode (
  IN  AML_NODE_HEADER   * Node
  )
{
  AmlDbgPrintTableHeader ();
  AmlDbgPrintTreeInternal (Node, FALSE, 0);
}

/** Recursively print the subtree under the Node.

  @param  [in]  Node    Pointer to the root of the subtree to print.
                        Can be a root/object/data node.
**/
VOID
EFIAPI
AmlDbgPrintTree (
  IN  AML_NODE_HEADER   * Node
  )
{
  AmlDbgPrintTableHeader ();
  AmlDbgPrintTreeInternal (Node, TRUE, 0);
}

/** This function performs a raw data dump of the ACPI table.

  @param  [in]  Ptr     Pointer to the start of the table buffer.
  @param  [in]  Length  The length of the buffer.
**/
VOID
EFIAPI
AmlDbgDumpRaw (
  IN  CONST UINT8   * Ptr,
  IN        UINT32    Length
  )
{
  UINT32  ByteCount;
  UINT32  PartLineChars;
  UINT32  AsciiBufferIndex;
  CHAR8   AsciiBuffer[17];

  ByteCount = 0;
  AsciiBufferIndex = 0;

  DEBUG ((DEBUG_VERBOSE, "Address  : 0x%p\n", Ptr));
  DEBUG ((DEBUG_VERBOSE, "Length   : %lld", Length));

  while (ByteCount < Length) {
    if ((ByteCount & 0x0F) == 0) {
      AsciiBuffer[AsciiBufferIndex] = '\0';
      DEBUG ((DEBUG_VERBOSE, "  %a\n%08X : ", AsciiBuffer, ByteCount));
      AsciiBufferIndex = 0;
    } else if ((ByteCount & 0x07) == 0) {
      DEBUG ((DEBUG_VERBOSE, "- "));
    }

    if ((*Ptr >= ' ') && (*Ptr < 0x7F)) {
      AsciiBuffer[AsciiBufferIndex++] = *Ptr;
    } else {
      AsciiBuffer[AsciiBufferIndex++] = '.';
    }

    DEBUG ((DEBUG_VERBOSE, "%02X ", *Ptr++));

    ByteCount++;
  }

  // Justify the final line using spaces before printing
  // the ASCII data.
  PartLineChars = (Length & 0x0F);
  if (PartLineChars != 0) {
    PartLineChars = 48 - (PartLineChars * 3);
    if ((Length & 0x0F) <= 8) {
      PartLineChars += 2;
    }
    while (PartLineChars > 0) {
      DEBUG ((DEBUG_VERBOSE, " "));
      PartLineChars--;
    }
  }

  // Print ASCII data for the final line.
  AsciiBuffer[AsciiBufferIndex] = '\0';
  DEBUG ((DEBUG_VERBOSE, "  %a\n\n", AsciiBuffer));
}

#endif // MDEPKG_NDEBUG