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