summaryrefslogtreecommitdiff
path: root/StandaloneMmPkg/Library/CperLib/CperLib.c
blob: 5cf9f7ee8b171c44d1d386e8608d87c8e5b8d176 (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
/** @file

  Copyright (c) 2017, ARM Limited. All rights reserved.

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

//#define MM_TEST    1

#include <Uefi.h>

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/PeCoffGetEntryPointLib.h>
#include <Library/UefiLib.h>
#include <Library/CpuLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/CperLib.h>

#include <Guid/DebugImageInfoTable.h>
#include <Guid/Cper.h>

#include <Protocol/Cpu.h>
#include <Protocol/DebugSupport.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/AcpiTable.h>
#include <Protocol/Apei.h>
#include <IndustryStandard/Acpi.h>
#include <Protocol/AcpiSystemDescriptionTable.h>

#include <Pi/PiDxeCis.h>

#ifdef MM_TEST
#include <Guid/MmUefiInfo.h>
#endif

EFI_GUID EfiErrorSections_Guid[] = {
  [EFI_ERROR_SECTION_PROCESSOR_GENERIC_TYPE] =
    EFI_ERROR_SECTION_PROCESSOR_GENERIC_GUID,
  [EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_TYPE] =
    EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_GUID,
  [EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_IA32X64_TYPE] =
    EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_IA32X64_GUID,
  [EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_ARM_TYPE] =
    EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_ARM_GUID,
  [EFI_ERROR_SECTION_PLATFORM_MEMORY_TYPE] =
    EFI_ERROR_SECTION_PLATFORM_MEMORY_GUID,
  [EFI_ERROR_SECTION_PLATFORM_MEMORY2_TYPE] =
    EFI_ERROR_SECTION_PLATFORM_MEMORY2_GUID,
  [EFI_ERROR_SECTION_PCIE_TYPE] =
    EFI_ERROR_SECTION_PCIE_GUID,
  [EFI_ERROR_SECTION_FW_ERROR_RECORD_TYPE] =
    EFI_ERROR_SECTION_FW_ERROR_RECORD_GUID,
  [EFI_ERROR_SECTION_PCI_PCIX_BUS_TYPE] =
    EFI_ERROR_SECTION_PCI_PCIX_BUS_GUID,
  [EFI_ERROR_SECTION_PCI_DEVICE_TYPE] =
    EFI_ERROR_SECTION_PCI_DEVICE_GUID,
  [EFI_ERROR_SECTION_DMAR_GENERIC_TYPE] =
    EFI_ERROR_SECTION_DMAR_GENERIC_GUID,
  [EFI_ERROR_SECTION_DIRECTED_IO_DMAR_TYPE] =
    EFI_ERROR_SECTION_DIRECTED_IO_DMAR_GUID,
  [EFI_ERROR_SECTION_IOMMU_DMAR_TYPE] =
    EFI_ERROR_SECTION_IOMMU_DMAR_GUID,
  [EFI_ERROR_SECTION_MAX_TYPE] = EFI_ERROR_SECTION_NULL_GUID
};

STATIC
EFI_STATUS
GetTimestamp (
  IN OUT EFI_ERROR_TIME_STAMP      *TimeStamp
  )
{
  //EFI_STATUS    Status;
  UINT8         Year, Century;
  EFI_TIME      Time;

  //
  // How to get the current time in StandaloneMm?
  // we can NOT use gRT !

  //Status = gRT->GetTime(&Time, NULL);
  //if (EFI_ERROR(Status)) {
  //  DEBUG ((EFI_D_ERROR, "CperLib.GetTimestamp - Failed to get RTC time.\n"));
  //  return EFI_DEVICE_ERROR;
  //}
  Time.Year = 2007;
  Time.Month = 11;
  Time.Day = 29;
  Time.Hour = 17;
  Time.Minute = 43;
  Time.Second = 30;

  Year  = (UINT8) (Time.Year % 100);
  Century  = (UINT8) (Time.Year / 100);

  //
  // Set the Time/Date values.
  //
  TimeStamp->Century  = DecimalToBcd8 (Century);
  TimeStamp->Year     = DecimalToBcd8 (Year);
  TimeStamp->Month    = DecimalToBcd8 (Time.Month);
  TimeStamp->Day      = DecimalToBcd8 (Time.Day);
  TimeStamp->Flag     = EFI_ERROR_TIME_STAMP_PRECISE;
  TimeStamp->Hours    = DecimalToBcd8 (Time.Hour);
  TimeStamp->Minutes  = DecimalToBcd8 (Time.Minute);
  TimeStamp->Seconds  = DecimalToBcd8 (Time.Second);

  return EFI_SUCCESS;
}

/**
  Match the section type GUID with the internal lookup table and create a common
  platform error record at the designated memory region. The list of section
  type GUIDs for most common platform error records are available in
  “MdePkg/Include/Guid/Cper.h”

  @param  SectionCount    Indicates the total number of section(s) in the error
                          record.
  @param  SectionType     GUID Array indicating the error type(s) to be included
                          in the error record.

  @param  Section         Data buffer pointing to the section(s).

  @retval EFI_UNSUPPORTED  Unable to create CPER for a variety of reasons.
  @retval EFI_SUCCESS      CPER successfully created.

EFI_STATUS
EFIAPI
CperWrite (
  IN OUT UINTN ErrorRecordAddress,
  IN _SECTIONS_INFO SectionsInfo
  )

**/
EFI_STATUS
EFIAPI
CperWrite (
  IN SECTIONS_INFO  *SectionInfo,
  IN UINTN          ErrorRecordAddress
  )
{
  EFI_STATUS                                       Status = EFI_SUCCESS;
  UINTN                                            i, j;
  //statistical data
  BOOLEAN                                          UncorrectableErrorValid;
  BOOLEAN                                          CorrectableErrorValid;
  BOOLEAN                                          MultipleUncorrectableErrors;
  BOOLEAN                                          MultipleCorrectableErrors;

  EFI_GUID                                         *_ErrorSentionType;
  VOID                                             *_ErrorSention;
  VOID                                             *ErrorSention_end;
  UINT32                                           _DataLength;

  UINTN                                            CperAddress;
  EFI_COMMON_ERROR_RECORD_HEADER                   *ErrorRecordHeader;
  EFI_ERROR_SECTION_DESCRIPTOR                     *_ErrorRecordDescriptor;
  VOID                                             *_ErrorRecord;

  //For Generic Error Status Block
  EFI_ACPI_6_1_GENERIC_ERROR_STATUS_STRUCTURE      *GenericErrorStatusBlock;
  EFI_ACPI_6_1_GENERIC_ERROR_DATA_ENTRY_STRUCTURE  *GenericErrorDataEntry;

  GenericErrorStatusBlock = (EFI_ACPI_6_1_GENERIC_ERROR_STATUS_STRUCTURE *)
                            ErrorRecordAddress;
  GenericErrorDataEntry   = (EFI_ACPI_6_1_GENERIC_ERROR_DATA_ENTRY_STRUCTURE *)
                            (ErrorRecordAddress +
                             sizeof(EFI_ACPI_6_1_GENERIC_ERROR_STATUS_STRUCTURE));

  CperAddress = ErrorRecordAddress +
                sizeof(EFI_ACPI_6_1_GENERIC_ERROR_STATUS_STRUCTURE) +
                sizeof(EFI_ACPI_6_1_GENERIC_ERROR_DATA_ENTRY_STRUCTURE);
  DEBUG ((DEBUG_INFO, "CperWrite - CperAddress@0x%x\n", CperAddress));
  DEBUG ((DEBUG_INFO, "CperWrite - %d Section@%p, Length %d, SectionType@%p \n",
          SectionInfo->SectionCount, SectionInfo->Section,
          SectionInfo->DataLength, SectionInfo->SectionType));

  ErrorRecordHeader = (EFI_COMMON_ERROR_RECORD_HEADER *)CperAddress;
  _ErrorRecordDescriptor = (EFI_ERROR_SECTION_DESCRIPTOR *)
                           (CperAddress +
                            sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
  _ErrorRecord = (VOID *)_ErrorRecordDescriptor +
                 sizeof(EFI_ERROR_SECTION_DESCRIPTOR) * SectionInfo->SectionCount;

  _ErrorSention = SectionInfo->Section;
  ErrorSention_end = _ErrorSention + SectionInfo->DataLength;

  //Init the Record Header
  ErrorRecordHeader->SignatureStart = EFI_ERROR_RECORD_SIGNATURE_START;
  ErrorRecordHeader->Revision = EFI_ERROR_RECORD_REVISION;
  ErrorRecordHeader->SignatureEnd = EFI_ERROR_RECORD_SIGNATURE_END;

  //TODO
  ErrorRecordHeader->ValidationBits = 0;
  //CopyGuid(&ErrorRecordHeader->PlatformID, );
  //CopyGuid(&ErrorRecordHeader->PartitionID, );
  //CopyGuid(&ErrorRecordHeader->CreatorID, );


  Status = GetTimestamp(&ErrorRecordHeader->TimeStamp);
  if (EFI_ERROR(Status))
    return Status;

  //TODO: How to get this?
  //Caller know this info, so can caller pass it to this function?
  //CopyGuid(&ErrorRecordHeader->NotificationType, );

  //TODO: These fields must to be filled according to the Hardware.
  //ErrorRecordHeader->RecordID = ;
  //ErrorRecordHeader->Flags = ;
  //ErrorRecordHeader->PersistenceInfo = ;

  for (j = 0; j < SectionInfo->SectionCount; j++) {

    if (_ErrorSention >= ErrorSention_end) {
      DEBUG ((EFI_D_ERROR, "CperWrite - SectionInfo Error.\n"));
      return EFI_INVALID_PARAMETER;
    }

    //Getting Error Type from GUID
    _ErrorSentionType = SectionInfo->SectionType + j;
    for (i = 0; i < EFI_ERROR_SECTION_MAX_TYPE; i++ ) {
      if (CompareGuid (EfiErrorSections_Guid + i, _ErrorSentionType))
        break;
    }

    //Getting section data length from section type.
    switch (i) {
    case EFI_ERROR_SECTION_PROCESSOR_GENERIC_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Processor Generic.\n"));
      _DataLength = sizeof(EFI_PROCESSOR_GENERIC_ERROR_DATA);
      break;

/*    Unsupported*/
/*    case EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_TYPE:*/
/*      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Processor Specific.\n"));*/
/*      _DataLength = sizeof(EFI_PROCESSOR_GENERIC_ERROR_DATA) + ;*/
/*      break;*/

/*    Comment out for 'default' debuging*/
/*    case EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_IA32X64_TYPE:*/
/*      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Processor Specific x86.\n"));*/
/*      break;*/

/*    Unsupported*/
/*    case EFI_ERROR_SECTION_PROCESSOR_SPECIFIC_ARM_TYPE:*/
/*      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Processor Specific ARM.\n"));*/
/*      break;*/

    case EFI_ERROR_SECTION_PLATFORM_MEMORY_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Platform Memory.\n"));
      _DataLength = sizeof(EFI_PLATFORM_MEMORY_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_PLATFORM_MEMORY2_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Platform Memory2.\n"));
      _DataLength = sizeof(EFI_PLATFORM_MEMORY2_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_PCIE_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: PCIe.\n"));
      _DataLength = sizeof(EFI_PCIE_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_FW_ERROR_RECORD_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Firmware Error.\n"));
      _DataLength = sizeof(EFI_FIRMWARE_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_PCI_PCIX_BUS_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: PCIX.\n"));
      _DataLength = sizeof(EFI_PCI_PCIX_BUS_ERROR_DATA);
      break;

/*    Unsupported*/
/*    case EFI_ERROR_SECTION_PCI_DEVICE_TYPE:*/
/*      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: PCI device.\n"));*/
/*      _DataLength = sizeof();*/
/*      break;*/

    case EFI_ERROR_SECTION_DMAR_GENERIC_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: DMAR Generic.\n"));
      _DataLength = sizeof(EFI_DMAR_GENERIC_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_DIRECTED_IO_DMAR_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: Directed IO DMAR.\n"));
      _DataLength = sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_IOMMU_DMAR_TYPE:
      DEBUG ((DEBUG_INFO, "CperWrite - Got Error Section: IOMMU DMAR.\n"));
      _DataLength = sizeof(EFI_IOMMU_DMAR_ERROR_DATA);
      break;

    case EFI_ERROR_SECTION_MAX_TYPE:
      DEBUG ((EFI_D_ERROR, "CperWrite - Fail to get Error Section Type.\n"));
      Status = EFI_INVALID_PARAMETER;
      break;

    default:
      DEBUG ((EFI_D_ERROR, "CperWrite - Unsupported Error Section Type: %d\n",
              i));
      Status = EFI_UNSUPPORTED;
      break;
    }

    if (EFI_ERROR (Status)) {
      return Status;
    }

    //Init the Record Descriptor
    _ErrorRecordDescriptor->SectionOffset = _ErrorSention - (VOID *)CperAddress;
    _ErrorRecordDescriptor->SectionLength = _DataLength;
    _ErrorRecordDescriptor->Revision = EFI_ERROR_SECTION_REVISION;
    CopyGuid(&_ErrorRecordDescriptor->SectionType, _ErrorSentionType);

    //TODO: Get this info from Cper data
    //_ErrorRecordDescriptor->SectionFlags = ;
    //_ErrorRecordDescriptor->Severity = ;

    //TODO: These fields must to be filled according to the Hardware.
    _ErrorRecordDescriptor->SecValidMask = 0x0;
    //CopyGuid(_ErrorRecordDescriptor->FruId, );
    //_ErrorRecordDescriptor->FruString = ;

    //Copy Section data to the destination address
    CopyMem (_ErrorRecord, _ErrorSention, _DataLength);

    //Move to next section:
    //increace src and des address of error data
    //increace address of error record descriptor
    _ErrorSention += _DataLength;
    _ErrorRecord  += _DataLength;
    _ErrorRecordDescriptor += 1;
  }

  ErrorRecordHeader->SectionCount = j;
  ErrorRecordHeader->RecordLength = _ErrorRecord - (VOID *)CperAddress;
  //TODO: How to decide this info?
  //ErrorRecordHeader->ErrorSeverity = ;

  //For testing
  UncorrectableErrorValid = 1;
  CorrectableErrorValid = 0;
  MultipleUncorrectableErrors = 0;
  MultipleCorrectableErrors = 0;
  //Fill the GenericErrorStatusBlock, according to the info above
  GenericErrorStatusBlock->BlockStatus.UncorrectableErrorValid = UncorrectableErrorValid;
  GenericErrorStatusBlock->BlockStatus.CorrectableErrorValid = CorrectableErrorValid;
  GenericErrorStatusBlock->BlockStatus.MultipleUncorrectableErrors = MultipleUncorrectableErrors;
  GenericErrorStatusBlock->BlockStatus.MultipleCorrectableErrors = MultipleCorrectableErrors;
  //TODO: we just support only one ErrorDataEntry for now.
  GenericErrorStatusBlock->BlockStatus.ErrorDataEntryCount = 1;

  //TODO: what is the definition of "Raw Data"
  GenericErrorStatusBlock->RawDataOffset = ErrorRecordHeader->RecordLength;
  GenericErrorStatusBlock->RawDataLength = 0;

  GenericErrorStatusBlock->DataLength = ErrorRecordHeader->RecordLength +
                                        sizeof(EFI_ACPI_6_1_GENERIC_ERROR_DATA_ENTRY_STRUCTURE);
  //TODO: set this value according to the error blob above
  //GenericErrorStatusBlock->ErrorSeverity = ;

  //TODO: which SectionType we should use if we have multiple sections?
  //CopyGuid(GenericErrorDataEntry->SectionType, _ErrorSentionType);
  //TODO: what ErrorSeverity we should set?
  //GenericErrorDataEntry->ErrorSeverity = ;
  GenericErrorDataEntry->Revision = EFI_ACPI_6_1_GENERIC_ERROR_DATA_ENTRY_REVISION;
  //TODO: Get this info from Cper data
  //GenericErrorDataEntry->Flags = ;
  GenericErrorDataEntry->ErrorDataLength = ErrorRecordHeader->RecordLength;

  GenericErrorDataEntry->ValidationBits = 0x04;
  //TODO: These fields must to be filled according to the Hardware.
  //CopyGuid(GenericErrorDataEntry->FruId, );
  //GenericErrorDataEntry->FruText = ;
  Status = GetTimestamp((EFI_ERROR_TIME_STAMP *)GenericErrorDataEntry->Timestamp);

  return Status;
}

/**
  This function parse the error source information argument to get the memory
  info for the error source.

  @param  ErrorSource    Error Source Information of a specific error source
  @param  ErrorRecordAddress

  @retval EFI_SUCCESS    Memory info successfully parsed.
  @retval Other          Some error occurred when executing this entry point.

**/
EFI_STATUS
EFIAPI
CperInit (
  IN EFI_APEI_ERROR_SOURCE  *ErrorSource,
  IN OUT UINTN              *ErrorRecordAddress
  )
{
  EFI_STATUS                 Status = EFI_SUCCESS;

  if (! ErrorSource) {
    DEBUG ((EFI_D_ERROR, "CperInit - Missing ErrorSection.\n"));
    return EFI_INVALID_PARAMETER;
  }

  if (! ErrorRecordAddress)
    DEBUG ((EFI_D_ERROR, "CperInit - Missing ErrorRecordAddress parameter.\n"));

  *ErrorRecordAddress = ErrorSource->ErrorStatusAddress.Address;

  //TODO: maybe we need more init here

  DEBUG ((DEBUG_INFO, "CperInit - ErrorSources %p, ErrorRecordAddress@0x%x\n",
          ErrorSource, ErrorRecordAddress));

  return Status;
}

EFI_STATUS
EFIAPI
CperLibConstructor (
  VOID
  )
{
  EFI_STATUS                    Status;

  //
  //TODO: What we need to do to init this library.
  //
  Status = EFI_SUCCESS;
  ASSERT_EFI_ERROR (Status);

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

  return EFI_SUCCESS;
}

/**
  The destructor function frees resource used in the Mm Mem library

  @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 deconstructor always returns EFI_SUCCESS.
**/
EFI_STATUS
EFIAPI
CperLibDestructor  (
  VOID
  )
{
  DEBUG ((DEBUG_INFO, "CperLibDestructor\n"));
  return EFI_SUCCESS;
}