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