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
|
/** @file
ACPI Debug feature driver implementation.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/DxeServicesLib.h>
#include <Protocol/AcpiTable.h>
#include <IndustryStandard/Acpi.h>
#include <Protocol/SmmBase2.h>
#include <Protocol/SmmEndOfDxe.h>
#include <Protocol/SmmSwDispatch2.h>
#define ACPI_DEBUG_STR "INTEL ACPI DEBUG"
//
// ASL NAME structure
//
#pragma pack(1)
typedef struct {
UINT8 NameOp; // Byte [0]=0x08:NameOp.
UINT32 NameString; // Byte [4:1]=Name of object.
UINT8 DWordPrefix; // Byte [5]=0x0C:DWord Prefix.
UINT32 Value; // 0 ; Value of named object.
} NAME_LAYOUT;
#pragma pack()
#pragma pack(1)
typedef struct {
UINT8 Signature[16]; // "INTEL ACPI DEBUG"
UINT32 BufferSize; // Total size of Acpi Debug buffer including header structure
UINT32 Head; // Current buffer pointer for SMM to print out
UINT32 Tail; // Current buffer pointer for ASL to input
UINT8 SmiTrigger; // Value to trigger the SMI via B2 port
UINT8 Wrap; // If current Tail < Head
UINT8 SmmVersion; // If SMM version
UINT8 Truncate; // If the input from ASL > MAX_BUFFER_SIZE
} ACPI_DEBUG_HEAD;
#pragma pack()
#define AD_SIZE sizeof (ACPI_DEBUG_HEAD) // This is 0x20
#define MAX_BUFFER_SIZE 32
UINT32 mBufferEnd = 0;
ACPI_DEBUG_HEAD *mAcpiDebug = NULL;
EFI_SMM_SYSTEM_TABLE2 *mSmst = NULL;
/**
Patch and load ACPI table.
@param[in] AcpiDebugAddress Address of Acpi debug memory buffer.
@param[in] BufferIndex Index that starts after the Acpi Debug head.
@param[in] BufferEnd End of Acpi debug memory buffer.
**/
VOID
PatchAndLoadAcpiTable (
IN ACPI_DEBUG_HEAD *AcpiDebugAddress,
IN UINT32 BufferIndex,
IN UINT32 BufferEnd
)
{
EFI_STATUS Status;
EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
UINTN Size;
EFI_ACPI_DESCRIPTION_HEADER *TableHeader;
UINTN TableKey;
UINT8 *CurrPtr;
UINT32 *Signature;
NAME_LAYOUT *NamePtr;
UINT8 UpdateCounter;
Status = GetSectionFromFv (
&gEfiCallerIdGuid,
EFI_SECTION_RAW,
0,
(VOID **) &TableHeader,
&Size
);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return;
}
//
// This is Acpi Debug SSDT. Acpi Debug should be enabled if we reach here so load the table.
//
ASSERT (((EFI_ACPI_DESCRIPTION_HEADER *) TableHeader)->OemTableId == SIGNATURE_64 ('A', 'D', 'e', 'b', 'T', 'a', 'b', 'l'));
//
// Patch some pointers for the ASL code before loading the SSDT.
//
//
// Count pointer updates, so we can stop after all three pointers are patched.
//
UpdateCounter = 1;
for (CurrPtr = (UINT8 *) TableHeader; CurrPtr <= ((UINT8 *) TableHeader + TableHeader->Length) && UpdateCounter < 4; CurrPtr++) {
Signature = (UINT32 *) (CurrPtr + 1);
//
// patch DPTR (address of Acpi debug memory buffer)
//
if ((*CurrPtr == AML_NAME_OP) && *Signature == SIGNATURE_32 ('D', 'P', 'T', 'R')) {
NamePtr = (NAME_LAYOUT *) CurrPtr;
NamePtr->Value = (UINT32) (UINTN) AcpiDebugAddress;
UpdateCounter++;
}
//
// patch EPTR (end of Acpi debug memory buffer)
//
if ((*CurrPtr == AML_NAME_OP) && *Signature == SIGNATURE_32 ('E', 'P', 'T', 'R')) {
NamePtr = (NAME_LAYOUT *) CurrPtr;
NamePtr->Value = BufferEnd;
UpdateCounter++;
}
//
// patch CPTR (used as an index that starts after the Acpi Debug head)
//
if ((*CurrPtr == AML_NAME_OP) && *Signature == SIGNATURE_32 ('C', 'P', 'T', 'R')) {
NamePtr = (NAME_LAYOUT *) CurrPtr;
NamePtr->Value = BufferIndex;
UpdateCounter++;
}
}
//
// Add the table
//
Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
goto Done;
}
TableKey = 0;
Status = AcpiTable->InstallAcpiTable (
AcpiTable,
TableHeader,
Size,
&TableKey
);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
goto Done;
}
Done:
gBS->FreePool (TableHeader);
return ;
}
/**
Allocate Acpi Debug memory.
@param[out] BufferSize Pointer to Acpi debug memory buffer size.
@return Address of Acpi debug memory buffer.
**/
EFI_PHYSICAL_ADDRESS
AllocateAcpiDebugMemory (
OUT UINT32 *BufferSize
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS AcpiDebugAddress;
UINTN PagesNum;
AcpiDebugAddress = 0;
*BufferSize = 0;
//
// Reserve memory to store Acpi Debug data.
//
AcpiDebugAddress = 0xFFFFFFFF;
PagesNum = EFI_SIZE_TO_PAGES (PcdGet32 (PcdAcpiDebugBufferSize));
Status = gBS->AllocatePages (
AllocateMaxAddress,
EfiReservedMemoryType,
PagesNum,
&AcpiDebugAddress
);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return 0;
}
DEBUG ((DEBUG_INFO, "AcpiDebugAddress - 0x%08x\n", AcpiDebugAddress));
Status = PcdSet32S (PcdAcpiDebugAddress, (UINT32) AcpiDebugAddress);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
gBS->FreePages (AcpiDebugAddress, PagesNum);
return 0;
}
*BufferSize = PcdGet32 (PcdAcpiDebugBufferSize);
return AcpiDebugAddress;
}
/**
Acpi Debug EndOfDxe notification.
@param[in] Event Event whose notification function is being invoked.
@param[in] Context Pointer to the notification function's context.
**/
VOID
EFIAPI
AcpiDebugEndOfDxeNotification (
IN EFI_EVENT Event,
IN VOID *Context
)
{
UINT32 BufferSize;
UINT32 BufferIndex;
mAcpiDebug = (ACPI_DEBUG_HEAD *) (UINTN) AllocateAcpiDebugMemory (&BufferSize);
if (mAcpiDebug != NULL) {
//
// Init ACPI DEBUG buffer to lower case 'x'.
//
SetMem ((VOID *) mAcpiDebug, BufferSize, 0x78);
//
// Clear header of AD_SIZE bytes.
//
ZeroMem ((VOID *) mAcpiDebug, AD_SIZE);
//
// Write a signature to the first line of the buffer, "INTEL ACPI DEBUG".
//
CopyMem ((VOID *) mAcpiDebug, ACPI_DEBUG_STR, sizeof (ACPI_DEBUG_STR) - 1);
BufferIndex = (UINT32) (UINTN) mAcpiDebug;
mBufferEnd = BufferIndex + BufferSize;
//
// Leave the Index after the Acpi Debug head.
//
BufferIndex += AD_SIZE;
//
// Patch and Load the SSDT ACPI Tables.
//
PatchAndLoadAcpiTable (mAcpiDebug, BufferIndex, mBufferEnd);
mAcpiDebug->Head = BufferIndex;
mAcpiDebug->Tail = BufferIndex;
mAcpiDebug->BufferSize = BufferSize;
}
//
// Close event, so it will not be invoked again.
//
gBS->CloseEvent (Event);
return ;
}
/**
Initialize ACPI Debug.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The driver initializes correctly.
**/
EFI_STATUS
EFIAPI
InitializeAcpiDebugDxe (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_EVENT EndOfDxeEvent;
if (!PcdGetBool (PcdAcpiDebugFeatureActive)) {
return EFI_SUCCESS;
}
//
// Register EndOfDxe notification
// that point could ensure the Acpi Debug related PCDs initialized.
//
Status = gBS->CreateEventEx (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
AcpiDebugEndOfDxeNotification,
NULL,
&gEfiEndOfDxeEventGroupGuid,
&EndOfDxeEvent
);
ASSERT_EFI_ERROR (Status);
return Status;
}
/**
Software SMI callback for ACPI Debug which is called from ACPI method.
@param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
@param[in] Context Points to an optional handler context which was specified when the
handler was registered.
@param[in, out] CommBuffer A pointer to a collection of data in memory that will
be conveyed from a non-SMM environment into an SMM environment.
@param[in, out] CommBufferSize The size of the CommBuffer.
@retval EFI_SUCCESS The interrupt was handled successfully.
**/
EFI_STATUS
EFIAPI
AcpiDebugSmmCallback (
IN EFI_HANDLE DispatchHandle,
IN CONST VOID *Context,
IN OUT VOID *CommBuffer,
IN OUT UINTN *CommBufferSize
)
{
UINT8 Buffer[MAX_BUFFER_SIZE];
//
// Validate the fields in mAcpiDebug to ensure there is no harm to SMI handler.
// mAcpiDebug is below 4GB and the start address of whole buffer.
//
if ((mAcpiDebug->BufferSize != (mBufferEnd - (UINT32) (UINTN) mAcpiDebug)) ||
(mAcpiDebug->Head < (UINT32) ((UINTN) mAcpiDebug + AD_SIZE)) ||
(mAcpiDebug->Head > mBufferEnd) ||
(mAcpiDebug->Tail < (UINT32) ((UINTN) mAcpiDebug + AD_SIZE)) ||
(mAcpiDebug->Tail > mBufferEnd)) {
//
// If some fields in mAcpiDebug are invaid, return directly.
//
return EFI_SUCCESS;
}
if (!(BOOLEAN)mAcpiDebug->Wrap && ((mAcpiDebug->Head >= (UINT32) ((UINTN) mAcpiDebug + AD_SIZE))
&& (mAcpiDebug->Head < mAcpiDebug->Tail))){
//
// If curent ----- buffer + 020
// ...
// ... Head
// ... Data for SMM print
// ... Tail
// ... Vacant for ASL input
// ----- buffer end
//
// skip NULL block
//
while ((*(CHAR8 *) (UINTN) mAcpiDebug->Head == '\0') && (mAcpiDebug->Head < mAcpiDebug->Tail)) {
mAcpiDebug->Head ++;
}
if (mAcpiDebug->Head < mAcpiDebug->Tail) {
ZeroMem (Buffer, MAX_BUFFER_SIZE);
AsciiStrnCpyS ((CHAR8 *) Buffer, MAX_BUFFER_SIZE, (CHAR8 *) (UINTN) mAcpiDebug->Head, MAX_BUFFER_SIZE - 1);
DEBUG ((DEBUG_INFO | DEBUG_ERROR, "%a%a\n", Buffer, (BOOLEAN) mAcpiDebug->Truncate ? "..." : ""));
mAcpiDebug->Head += MAX_BUFFER_SIZE;
if (mAcpiDebug->Head >= (mAcpiDebug->Tail)) {
//
// When head == tail, we do nothing in handler.
//
mAcpiDebug->Head = mAcpiDebug->Tail;
}
}
} else if ((BOOLEAN) mAcpiDebug->Wrap && ((mAcpiDebug->Head > mAcpiDebug->Tail)
&& (mAcpiDebug->Head < (UINT32) ((UINTN) mAcpiDebug + mAcpiDebug->BufferSize)))){
//
// If curent ----- buffer + 020
// ... Tail
// ... Vacant for ASL input
// ... Head
// ... Data for SMM print
// ----- buffer end
//
while ((*(CHAR8 *) (UINTN) mAcpiDebug->Head == '\0') && (mAcpiDebug->Head < (UINT32) ((UINTN) mAcpiDebug + mAcpiDebug->BufferSize))) {
mAcpiDebug->Head ++;
}
if (mAcpiDebug->Head < (UINT32) ((UINTN) mAcpiDebug + mAcpiDebug->BufferSize)){
ZeroMem (Buffer, MAX_BUFFER_SIZE);
AsciiStrnCpyS ((CHAR8 *) Buffer, MAX_BUFFER_SIZE, (CHAR8 *) (UINTN) mAcpiDebug->Head, MAX_BUFFER_SIZE - 1);
DEBUG ((DEBUG_INFO | DEBUG_ERROR, "%a%a\n", Buffer, (BOOLEAN) mAcpiDebug->Truncate ? "..." : ""));
mAcpiDebug->Head += MAX_BUFFER_SIZE;
if (mAcpiDebug->Head >= (UINT32) ((UINTN) mAcpiDebug + mAcpiDebug->BufferSize)) {
//
// We met end of buffer.
//
mAcpiDebug->Wrap = 0;
mAcpiDebug->Head = (UINT32) ((UINTN) mAcpiDebug + AD_SIZE);
}
}
}
return EFI_SUCCESS;
}
/**
Acpi Debug SmmEndOfDxe notification.
@param[in] Protocol Points to the protocol's unique identifier.
@param[in] Interface Points to the interface instance.
@param[in] Handle The handle on which the interface was installed.
@retval EFI_SUCCESS Notification runs successfully.
**/
EFI_STATUS
EFIAPI
AcpiDebugSmmEndOfDxeNotification (
IN CONST EFI_GUID *Protocol,
IN VOID *Interface,
IN EFI_HANDLE Handle
)
{
EFI_STATUS Status;
EFI_SMM_SW_DISPATCH2_PROTOCOL *SwDispatch;
EFI_SMM_SW_REGISTER_CONTEXT SwContext;
EFI_HANDLE SwHandle;
AcpiDebugEndOfDxeNotification (NULL, NULL);
if (mAcpiDebug != NULL) {
//
// Get the Sw dispatch protocol and register SMI callback function.
//
SwDispatch = NULL;
Status = mSmst->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID **) &SwDispatch);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return Status;
}
SwContext.SwSmiInputValue = (UINTN) -1;
Status = SwDispatch->Register (SwDispatch, AcpiDebugSmmCallback, &SwContext, &SwHandle);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return Status;
}
mAcpiDebug->SmiTrigger = (UINT8) SwContext.SwSmiInputValue;
mAcpiDebug->SmmVersion = 1;
}
return EFI_SUCCESS;
}
/**
Initialize ACPI Debug.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The driver initializes correctly.
**/
EFI_STATUS
EFIAPI
InitializeAcpiDebugSmm (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
VOID *Registration;
EFI_SMM_BASE2_PROTOCOL *SmmBase2;
BOOLEAN InSmm;
if (!PcdGetBool (PcdAcpiDebugFeatureActive)) {
return EFI_SUCCESS;
}
Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **) &SmmBase2);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return Status;
}
Status = SmmBase2->InSmm (SmmBase2, &InSmm);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return Status;
}
ASSERT (InSmm);
if (!InSmm) {
return EFI_UNSUPPORTED;
}
Status = SmmBase2->GetSmstLocation (SmmBase2, &mSmst);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Register SmmEndOfDxe notification
// that point could ensure the Acpi Debug related PCDs initialized.
//
Registration = NULL;
Status = mSmst->SmmRegisterProtocolNotify (
&gEfiSmmEndOfDxeProtocolGuid,
AcpiDebugSmmEndOfDxeNotification,
&Registration
);
ASSERT_EFI_ERROR (Status);
return Status;
}
|