]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / SmmReportStatusCodeLib / ReportStatusCodeLib.c
1 /** @file
2 Report Status Code Library for MM Phase.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Library/ReportStatusCodeLib.h>
10 #include <Library/DebugLib.h>
11 #include <Library/MmServicesTableLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/MemoryAllocationLib.h>
16
17 #include <Guid/StatusCodeDataTypeId.h>
18 #include <Guid/StatusCodeDataTypeDebug.h>
19 #include <Protocol/MmStatusCode.h>
20
21 #include "ReportStatusCodeLib.h"
22
23 EFI_MM_REPORT_STATUS_CODE mReportStatusCode = NULL;
24 EFI_MM_STATUS_CODE_PROTOCOL *mStatusCodeProtocol = NULL;
25
26 /**
27 Locate the report status code service.
28
29 @return Function pointer to the report status code service.
30 NULL is returned if no status code service is available.
31
32 **/
33 EFI_MM_REPORT_STATUS_CODE
34 InternalGetReportStatusCode (
35 VOID
36 )
37 {
38 EFI_STATUS Status;
39
40 Status = InternalLocateProtocol (&gEfiMmStatusCodeProtocolGuid, NULL, (VOID **)&mStatusCodeProtocol);
41 if (!EFI_ERROR (Status) && (mStatusCodeProtocol != NULL)) {
42 return mStatusCodeProtocol->ReportStatusCode;
43 }
44
45 return NULL;
46 }
47
48 /**
49 Internal worker function that reports a status code through the status code service.
50
51 If status code service is not cached, then this function checks if status code service is
52 available in system. If status code service is not available, then EFI_UNSUPPORTED is
53 returned. If status code service is present, then it is cached in mReportStatusCode.
54 Finally this function reports status code through the status code service.
55
56 @param Type Status code type.
57 @param Value Status code value.
58 @param Instance Status code instance number.
59 @param CallerId Pointer to a GUID that identifies the caller of this
60 function. This is an optional parameter that may be
61 NULL.
62 @param Data Pointer to the extended data buffer. This is an
63 optional parameter that may be NULL.
64
65 @retval EFI_SUCCESS The status code was reported.
66 @retval EFI_UNSUPPORTED Status code service is not available.
67 @retval EFI_UNSUPPORTED Status code type is not supported.
68
69 **/
70 EFI_STATUS
71 InternalReportStatusCode (
72 IN EFI_STATUS_CODE_TYPE Type,
73 IN EFI_STATUS_CODE_VALUE Value,
74 IN UINT32 Instance,
75 IN CONST EFI_GUID *CallerId OPTIONAL,
76 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
77 )
78 {
79 if ((ReportProgressCodeEnabled () && (((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE)) ||
80 (ReportErrorCodeEnabled () && (((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE)) ||
81 (ReportDebugCodeEnabled () && (((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE)))
82 {
83 //
84 // If mReportStatusCode is NULL, then check if status code service is available in system.
85 //
86 if (mReportStatusCode == NULL) {
87 mReportStatusCode = InternalGetReportStatusCode ();
88 if (mReportStatusCode == NULL) {
89 return EFI_UNSUPPORTED;
90 }
91 }
92
93 //
94 // A status code service is present in system, so pass in all the parameters to the service.
95 //
96 return (*mReportStatusCode)(mStatusCodeProtocol, Type, Value, Instance, (EFI_GUID *)CallerId, Data);
97 }
98
99 return EFI_UNSUPPORTED;
100 }
101
102 /**
103 Converts a status code to an 8-bit POST code value.
104
105 Converts the status code specified by CodeType and Value to an 8-bit POST code
106 and returns the 8-bit POST code in PostCode. If CodeType is an
107 EFI_PROGRESS_CODE or CodeType is an EFI_ERROR_CODE, then bits 0..4 of PostCode
108 are set to bits 16..20 of Value, and bits 5..7 of PostCode are set to bits
109 24..26 of Value., and TRUE is returned. Otherwise, FALSE is returned.
110
111 If PostCode is NULL, then ASSERT().
112
113 @param CodeType The type of status code being converted.
114 @param Value The status code value being converted.
115 @param PostCode A pointer to the 8-bit POST code value to return.
116
117 @retval TRUE The status code specified by CodeType and Value was converted
118 to an 8-bit POST code and returned in PostCode.
119 @retval FALSE The status code specified by CodeType and Value could not be
120 converted to an 8-bit POST code value.
121
122 **/
123 BOOLEAN
124 EFIAPI
125 CodeTypeToPostCode (
126 IN EFI_STATUS_CODE_TYPE CodeType,
127 IN EFI_STATUS_CODE_VALUE Value,
128 OUT UINT8 *PostCode
129 )
130 {
131 //
132 // If PostCode is NULL, then ASSERT()
133 //
134 ASSERT (PostCode != NULL);
135
136 //
137 // Convert Value to an 8 bit post code
138 //
139 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||
140 ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE))
141 {
142 *PostCode = (UINT8)((((Value & EFI_STATUS_CODE_CLASS_MASK) >> 24) << 5) |
143 (((Value & EFI_STATUS_CODE_SUBCLASS_MASK) >> 16) & 0x1f));
144 return TRUE;
145 }
146
147 return FALSE;
148 }
149
150 /**
151 Extracts ASSERT() information from a status code structure.
152
153 Converts the status code specified by CodeType, Value, and Data to the ASSERT()
154 arguments specified by Filename, Description, and LineNumber. If CodeType is
155 an EFI_ERROR_CODE, and CodeType has a severity of EFI_ERROR_UNRECOVERED, and
156 Value has an operation mask of EFI_SW_EC_ILLEGAL_SOFTWARE_STATE, extract
157 Filename, Description, and LineNumber from the optional data area of the
158 status code buffer specified by Data. The optional data area of Data contains
159 a Null-terminated ASCII string for the FileName, followed by a Null-terminated
160 ASCII string for the Description, followed by a 32-bit LineNumber. If the
161 ASSERT() information could be extracted from Data, then return TRUE.
162 Otherwise, FALSE is returned.
163
164 If Data is NULL, then ASSERT().
165 If Filename is NULL, then ASSERT().
166 If Description is NULL, then ASSERT().
167 If LineNumber is NULL, then ASSERT().
168
169 @param CodeType The type of status code being converted.
170 @param Value The status code value being converted.
171 @param Data Pointer to status code data buffer.
172 @param Filename Pointer to the source file name that generated the ASSERT().
173 @param Description Pointer to the description of the ASSERT().
174 @param LineNumber Pointer to source line number that generated the ASSERT().
175
176 @retval TRUE The status code specified by CodeType, Value, and Data was
177 converted ASSERT() arguments specified by Filename, Description,
178 and LineNumber.
179 @retval FALSE The status code specified by CodeType, Value, and Data could
180 not be converted to ASSERT() arguments.
181
182 **/
183 BOOLEAN
184 EFIAPI
185 ReportStatusCodeExtractAssertInfo (
186 IN EFI_STATUS_CODE_TYPE CodeType,
187 IN EFI_STATUS_CODE_VALUE Value,
188 IN CONST EFI_STATUS_CODE_DATA *Data,
189 OUT CHAR8 **Filename,
190 OUT CHAR8 **Description,
191 OUT UINT32 *LineNumber
192 )
193 {
194 EFI_DEBUG_ASSERT_DATA *AssertData;
195
196 ASSERT (Data != NULL);
197 ASSERT (Filename != NULL);
198 ASSERT (Description != NULL);
199 ASSERT (LineNumber != NULL);
200
201 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&
202 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED) &&
203 ((Value & EFI_STATUS_CODE_OPERATION_MASK) == EFI_SW_EC_ILLEGAL_SOFTWARE_STATE))
204 {
205 AssertData = (EFI_DEBUG_ASSERT_DATA *)(Data + 1);
206 *Filename = (CHAR8 *)(AssertData + 1);
207 *Description = *Filename + AsciiStrLen (*Filename) + 1;
208 *LineNumber = AssertData->LineNumber;
209 return TRUE;
210 }
211
212 return FALSE;
213 }
214
215 /**
216 Extracts DEBUG() information from a status code structure.
217
218 Converts the status code specified by Data to the DEBUG() arguments specified
219 by ErrorLevel, Marker, and Format. If type GUID in Data is
220 EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID, then extract ErrorLevel, Marker, and
221 Format from the optional data area of the status code buffer specified by Data.
222 The optional data area of Data contains a 32-bit ErrorLevel followed by Marker
223 which is 12 UINTN parameters, followed by a Null-terminated ASCII string for
224 the Format. If the DEBUG() information could be extracted from Data, then
225 return TRUE. Otherwise, FALSE is returned.
226
227 If Data is NULL, then ASSERT().
228 If ErrorLevel is NULL, then ASSERT().
229 If Marker is NULL, then ASSERT().
230 If Format is NULL, then ASSERT().
231
232 @param Data Pointer to status code data buffer.
233 @param ErrorLevel Pointer to error level mask for a debug message.
234 @param Marker Pointer to the variable argument list associated with Format.
235 @param Format Pointer to a Null-terminated ASCII format string of a
236 debug message.
237
238 @retval TRUE The status code specified by Data was converted DEBUG() arguments
239 specified by ErrorLevel, Marker, and Format.
240 @retval FALSE The status code specified by Data could not be converted to
241 DEBUG() arguments.
242
243 **/
244 BOOLEAN
245 EFIAPI
246 ReportStatusCodeExtractDebugInfo (
247 IN CONST EFI_STATUS_CODE_DATA *Data,
248 OUT UINT32 *ErrorLevel,
249 OUT BASE_LIST *Marker,
250 OUT CHAR8 **Format
251 )
252 {
253 EFI_DEBUG_INFO *DebugInfo;
254
255 ASSERT (Data != NULL);
256 ASSERT (ErrorLevel != NULL);
257 ASSERT (Marker != NULL);
258 ASSERT (Format != NULL);
259
260 //
261 // If the GUID type is not EFI_STATUS_CODE_DATA_TYPE_DEBUG_GUID then return FALSE
262 //
263 if (!CompareGuid (&Data->Type, &gEfiStatusCodeDataTypeDebugGuid)) {
264 return FALSE;
265 }
266
267 //
268 // Retrieve the debug information from the status code record
269 //
270 DebugInfo = (EFI_DEBUG_INFO *)(Data + 1);
271
272 *ErrorLevel = DebugInfo->ErrorLevel;
273
274 //
275 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
276 // of format in DEBUG string. Its address is returned in Marker and has to be 64-bit aligned.
277 // It must be noticed that EFI_DEBUG_INFO follows EFI_STATUS_CODE_DATA, whose size is
278 // 20 bytes. The size of EFI_DEBUG_INFO is 4 bytes, so we can ensure that Marker
279 // returned is 64-bit aligned.
280 // 64-bit aligned is a must, otherwise retrieving 64-bit parameter from BASE_LIST will
281 // cause unalignment exception.
282 //
283 *Marker = (BASE_LIST)(DebugInfo + 1);
284 *Format = (CHAR8 *)(((UINT64 *)*Marker) + 12);
285
286 return TRUE;
287 }
288
289 /**
290 Reports a status code.
291
292 Reports the status code specified by the parameters Type and Value. Status
293 code also require an instance, caller ID, and extended data. This function
294 passed in a zero instance, NULL extended data, and a caller ID of
295 gEfiCallerIdGuid, which is the GUID for the module.
296
297 ReportStatusCode()must actively prevent recusrsion. If ReportStatusCode()
298 is called while processing another any other Report Status Code Library function,
299 then ReportStatusCode() must return immediately.
300
301 @param Type Status code type.
302 @param Value Status code value.
303
304 @retval EFI_SUCCESS The status code was reported.
305 @retval EFI_DEVICE_ERROR There status code could not be reported due to a
306 device error.
307 @retval EFI_UNSUPPORTED Report status code is not supported
308
309 **/
310 EFI_STATUS
311 EFIAPI
312 ReportStatusCode (
313 IN EFI_STATUS_CODE_TYPE Type,
314 IN EFI_STATUS_CODE_VALUE Value
315 )
316 {
317 return InternalReportStatusCode (Type, Value, 0, &gEfiCallerIdGuid, NULL);
318 }
319
320 /**
321 Reports a status code with an extended data buffer.
322
323 Allocates and fills in the extended data section of a status code with the
324 extended data specified by ExtendedData and ExtendedDataSize. ExtendedData
325 is assumed to be one of the data structures specified in Related Definitions.
326 These data structure do not have the standard header, so this function is
327 responsible for allocating a buffer large enough for the standard header and
328 the extended data passed into this function. The standard header is filled
329 in with a GUID of gEfiStatusCodeSpecificDataGuid. The status code is reported
330 with a zero instance and a caller ID of gEfiCallerIdGuid.
331
332 ReportStatusCodeWithExtendedData()must actively prevent recursion. If
333 ReportStatusCodeWithExtendedData() is called while processing another any other
334 Report Status Code Library function, then ReportStatusCodeWithExtendedData()
335 must return EFI_DEVICE_ERROR immediately.
336
337 If ExtendedData is NULL, then ASSERT().
338 If ExtendedDataSize is 0, then ASSERT().
339
340 @param Type Status code type.
341 @param Value Status code value.
342 @param ExtendedData Pointer to the extended data buffer to be reported.
343 @param ExtendedDataSize The size, in bytes, of the extended data buffer to
344 be reported.
345
346 @retval EFI_SUCCESS The status code was reported with the extended
347 data specified by ExtendedData and ExtendedDataSize.
348 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
349 extended data section.
350 @retval EFI_UNSUPPORTED Report status code is not supported
351
352 **/
353 EFI_STATUS
354 EFIAPI
355 ReportStatusCodeWithExtendedData (
356 IN EFI_STATUS_CODE_TYPE Type,
357 IN EFI_STATUS_CODE_VALUE Value,
358 IN CONST VOID *ExtendedData,
359 IN UINTN ExtendedDataSize
360 )
361 {
362 ASSERT (ExtendedData != NULL);
363 ASSERT (ExtendedDataSize != 0);
364 return ReportStatusCodeEx (
365 Type,
366 Value,
367 0,
368 NULL,
369 NULL,
370 ExtendedData,
371 ExtendedDataSize
372 );
373 }
374
375 /**
376 Reports a status code with full parameters.
377
378 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
379 is 0, then an extended data buffer is not reported. If ExtendedData is not
380 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
381 ExtendedData is assumed not have the standard status code header, so this function
382 is responsible for allocating a buffer large enough for the standard header and
383 the extended data passed into this function. The standard header is filled in
384 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
385 GUID of gEfiStatusCodeSpecificDataGuid is used. The status code is reported with
386 an instance specified by Instance and a caller ID specified by CallerId. If
387 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
388
389 ReportStatusCodeEx()must actively prevent recursion. If
390 ReportStatusCodeEx() is called while processing another any
391 other Report Status Code Library function, then
392 ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
393
394 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
395 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
396
397 @param Type Status code type.
398 @param Value Status code value.
399 @param Instance Status code instance number.
400 @param CallerId Pointer to a GUID that identifies the caller of this
401 function. If this parameter is NULL, then a caller
402 ID of gEfiCallerIdGuid is used.
403 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
404 If this parameter is NULL, then a the status code
405 standard header is filled in with
406 gEfiStatusCodeSpecificDataGuid.
407 @param ExtendedData Pointer to the extended data buffer. This is an
408 optional parameter that may be NULL.
409 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
410
411 @retval EFI_SUCCESS The status code was reported.
412 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
413 the extended data section if it was specified.
414 @retval EFI_UNSUPPORTED Report status code is not supported
415
416 **/
417 EFI_STATUS
418 EFIAPI
419 ReportStatusCodeEx (
420 IN EFI_STATUS_CODE_TYPE Type,
421 IN EFI_STATUS_CODE_VALUE Value,
422 IN UINT32 Instance,
423 IN CONST EFI_GUID *CallerId OPTIONAL,
424 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
425 IN CONST VOID *ExtendedData OPTIONAL,
426 IN UINTN ExtendedDataSize
427 )
428 {
429 EFI_STATUS Status;
430 EFI_STATUS_CODE_DATA *StatusCodeData;
431
432 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
433 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
434
435 //
436 // Allocate space for the Status Code Header and its buffer
437 //
438 StatusCodeData = AllocatePool (sizeof (EFI_STATUS_CODE_DATA) + ExtendedDataSize);
439 if (StatusCodeData == NULL) {
440 return EFI_OUT_OF_RESOURCES;
441 }
442
443 //
444 // Fill in the extended data header
445 //
446 StatusCodeData->HeaderSize = (UINT16)sizeof (EFI_STATUS_CODE_DATA);
447 StatusCodeData->Size = (UINT16)ExtendedDataSize;
448 if (ExtendedDataGuid == NULL) {
449 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
450 }
451
452 CopyGuid (&StatusCodeData->Type, ExtendedDataGuid);
453
454 //
455 // Fill in the extended data buffer
456 //
457 if (ExtendedData != NULL) {
458 CopyMem (StatusCodeData + 1, ExtendedData, ExtendedDataSize);
459 }
460
461 //
462 // Report the status code
463 //
464 if (CallerId == NULL) {
465 CallerId = &gEfiCallerIdGuid;
466 }
467
468 Status = InternalReportStatusCode (Type, Value, Instance, CallerId, StatusCodeData);
469
470 //
471 // Free the allocated buffer
472 //
473 FreePool (StatusCodeData);
474
475 return Status;
476 }
477
478 /**
479 Returns TRUE if status codes of type EFI_PROGRESS_CODE are enabled
480
481 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED
482 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
483
484 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
485 PcdReportStatusCodeProperyMask is set.
486 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED bit of
487 PcdReportStatusCodeProperyMask is clear.
488
489 **/
490 BOOLEAN
491 EFIAPI
492 ReportProgressCodeEnabled (
493 VOID
494 )
495 {
496 return (BOOLEAN)((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_PROGRESS_CODE_ENABLED) != 0);
497 }
498
499 /**
500 Returns TRUE if status codes of type EFI_ERROR_CODE are enabled
501
502 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED
503 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
504
505 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
506 PcdReportStatusCodeProperyMask is set.
507 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED bit of
508 PcdReportStatusCodeProperyMask is clear.
509
510 **/
511 BOOLEAN
512 EFIAPI
513 ReportErrorCodeEnabled (
514 VOID
515 )
516 {
517 return (BOOLEAN)((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_ERROR_CODE_ENABLED) != 0);
518 }
519
520 /**
521 Returns TRUE if status codes of type EFI_DEBUG_CODE are enabled
522
523 This function returns TRUE if the REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED
524 bit of PcdReportStatusCodeProperyMask is set. Otherwise FALSE is returned.
525
526 @retval TRUE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
527 PcdReportStatusCodeProperyMask is set.
528 @retval FALSE The REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED bit of
529 PcdReportStatusCodeProperyMask is clear.
530
531 **/
532 BOOLEAN
533 EFIAPI
534 ReportDebugCodeEnabled (
535 VOID
536 )
537 {
538 return (BOOLEAN)((PcdGet8 (PcdReportStatusCodePropertyMask) & REPORT_STATUS_CODE_PROPERTY_DEBUG_CODE_ENABLED) != 0);
539 }