2 Library constructor & destructor, event handlers, and other internal worker functions.
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "ReportStatusCodeLibInternal.h"
11 EFI_EVENT mVirtualAddressChangeEvent
;
12 static EFI_EVENT mExitBootServicesEvent
;
13 EFI_STATUS_CODE_DATA
*mStatusCodeData
;
15 EFI_SMM_BASE_PROTOCOL
*mSmmBase
;
16 EFI_RUNTIME_SERVICES
*mInternalRT
;
17 BOOLEAN mHaveExitedBootServices
= FALSE
;
18 EFI_REPORT_STATUS_CODE mReportStatusCode
= NULL
;
19 EFI_SMM_STATUS_CODE_PROTOCOL
*mSmmStatusCodeProtocol
;
22 Locates and caches SMM Status Code Protocol.
26 SmmStatusCodeInitialize (
32 Status
= gBS
->LocateProtocol (&gEfiSmmStatusCodeProtocolGuid
, NULL
, (VOID
**) &mSmmStatusCodeProtocol
);
33 if (EFI_ERROR (Status
)) {
34 mSmmStatusCodeProtocol
= NULL
;
39 Report status code via SMM Status Code Protocol.
41 @param Type Indicates the type of status code being reported.
42 @param Value Describes the current status of a hardware or software entity.
43 This included information about the class and subclass that is used to classify the entity
44 as well as an operation. For progress codes, the operation is the current activity.
45 For error codes, it is the exception. For debug codes, it is not defined at this time.
46 @param Instance The enumeration of a hardware or software entity within the system.
47 A system may contain multiple entities that match a class/subclass pairing.
48 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,
49 not meaningful, or not relevant. Valid instance numbers start with 1.
50 @param CallerId This optional parameter may be used to identify the caller.
51 This parameter allows the status code driver to apply different rules to different callers.
52 @param Data This optional parameter may be used to pass additional data
54 @retval EFI_SUCCESS Always return EFI_SUCCESS.
59 IN EFI_STATUS_CODE_TYPE Type
,
60 IN EFI_STATUS_CODE_VALUE Value
,
62 IN EFI_GUID
*CallerId OPTIONAL
,
63 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
66 if (mSmmStatusCodeProtocol
!= NULL
) {
67 (mSmmStatusCodeProtocol
->ReportStatusCode
) (mSmmStatusCodeProtocol
, Type
, Value
, Instance
, CallerId
, Data
);
73 Locate the report status code service.
75 In SMM, it tries to retrieve SMM Status Code Protocol.
76 Otherwise, it first tries to retrieve ReportStatusCode() in Runtime Services Table.
77 If not found, it then tries to retrieve ReportStatusCode() API of Report Status Code Protocol.
79 @return Function pointer to the report status code service.
80 NULL is returned if no status code service is available.
83 EFI_REPORT_STATUS_CODE
84 InternalGetReportStatusCode (
88 EFI_STATUS_CODE_PROTOCOL
*StatusCodeProtocol
;
92 return (EFI_REPORT_STATUS_CODE
) SmmStatusCodeReport
;
93 } else if (mInternalRT
!= NULL
&& mInternalRT
->Hdr
.Revision
< 0x20000) {
94 return ((FRAMEWORK_EFI_RUNTIME_SERVICES
*)mInternalRT
)->ReportStatusCode
;
95 } else if (!mHaveExitedBootServices
) {
97 // Check gBS just in case. ReportStatusCode is called before gBS is initialized.
100 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&StatusCodeProtocol
);
101 if (!EFI_ERROR (Status
) && StatusCodeProtocol
!= NULL
) {
102 return StatusCodeProtocol
->ReportStatusCode
;
111 Internal worker function that reports a status code through the status code service.
113 If status code service is not cached, then this function checks if status code service is
114 available in system. If status code service is not available, then EFI_UNSUPPORTED is
115 returned. If status code service is present, then it is cached in mReportStatusCode.
116 Finally this function reports status code through the status code service.
118 @param Type Status code type.
119 @param Value Status code value.
120 @param Instance Status code instance number.
121 @param CallerId Pointer to a GUID that identifies the caller of this
122 function. This is an optional parameter that may be
124 @param Data Pointer to the extended data buffer. This is an
125 optional parameter that may be NULL.
127 @retval EFI_SUCCESS The status code was reported.
128 @retval EFI_UNSUPPORTED Status code service is not available.
129 @retval EFI_UNSUPPORTED Status code type is not supported.
133 InternalReportStatusCode (
134 IN EFI_STATUS_CODE_TYPE Type
,
135 IN EFI_STATUS_CODE_VALUE Value
,
137 IN CONST EFI_GUID
*CallerId OPTIONAL
,
138 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
141 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
142 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
143 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
145 // If mReportStatusCode is NULL, then check if status code service is available in system.
147 if (mReportStatusCode
== NULL
) {
148 mReportStatusCode
= InternalGetReportStatusCode ();
149 if (mReportStatusCode
== NULL
) {
150 return EFI_UNSUPPORTED
;
155 // A status code service is present in system, so pass in all the parameters to the service.
157 return (*mReportStatusCode
) (Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
160 return EFI_UNSUPPORTED
;
164 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
166 @param Event Event whose notification function is being invoked.
167 @param Context Pointer to the notification function's context
172 ReportStatusCodeLibVirtualAddressChange (
177 if (mReportStatusCode
!= NULL
) {
178 mInternalRT
->ConvertPointer (0, (VOID
**) &mReportStatusCode
);
180 mInternalRT
->ConvertPointer (0, (VOID
**) &mStatusCodeData
);
181 mInternalRT
->ConvertPointer (0, (VOID
**) &mInternalRT
);
185 Notification function of EVT_SIGNAL_EXIT_BOOT_SERVICES.
187 @param Event Event whose notification function is being invoked.
188 @param Context Pointer to the notification function's context
193 ReportStatusCodeLibExitBootServices (
199 // If mReportStatusCode is NULL, then see if a Status Code Protocol instance is present
200 // in the handle database.
202 if (mReportStatusCode
== NULL
) {
203 mReportStatusCode
= InternalGetReportStatusCode ();
206 mHaveExitedBootServices
= TRUE
;
210 The constructor function of SMM Runtime DXE Report Status Code Lib.
212 This function allocates memory for extended status code data, caches
213 the report status code service, and registers events.
215 @param ImageHandle The firmware allocated handle for the EFI image.
216 @param SystemTable A pointer to the EFI System Table.
218 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
223 ReportStatusCodeLibConstruct (
224 IN EFI_HANDLE ImageHandle
,
225 IN EFI_SYSTEM_TABLE
*SystemTable
231 // If in SMM mode, then allocates memory from SMRAM for extended status code data.
233 Status
= gBS
->LocateProtocol (&gEfiSmmBaseProtocolGuid
, NULL
, (VOID
**) &mSmmBase
);
234 if (!EFI_ERROR (Status
)) {
235 mSmmBase
->InSmm (mSmmBase
, &mInSmm
);
237 Status
= mSmmBase
->SmmAllocatePool (
239 EfiRuntimeServicesData
,
240 sizeof (EFI_STATUS_CODE_DATA
) + EFI_STATUS_CODE_DATA_MAX_SIZE
,
241 (VOID
**) &mStatusCodeData
243 ASSERT_EFI_ERROR (Status
);
244 SmmStatusCodeInitialize ();
251 // If not in SMM mode, then allocate runtime memory for extended status code data.
253 // Library should not use the gRT directly, for it may be converted by other library instance.
258 mStatusCodeData
= AllocateRuntimePool (sizeof (EFI_STATUS_CODE_DATA
) + EFI_STATUS_CODE_DATA_MAX_SIZE
);
259 ASSERT (mStatusCodeData
!= NULL
);
261 // Cache the report status code service
263 mReportStatusCode
= InternalGetReportStatusCode ();
266 // Register notify function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
268 Status
= gBS
->CreateEventEx (
271 ReportStatusCodeLibVirtualAddressChange
,
273 &gEfiEventVirtualAddressChangeGuid
,
274 &mVirtualAddressChangeEvent
276 ASSERT_EFI_ERROR (Status
);
279 // Register notify function for EVT_SIGNAL_EXIT_BOOT_SERVICES
281 Status
= gBS
->CreateEventEx (
284 ReportStatusCodeLibExitBootServices
,
286 &gEfiEventExitBootServicesGuid
,
287 &mExitBootServicesEvent
289 ASSERT_EFI_ERROR (Status
);
295 The destructor function of SMM Runtime DXE Report Status Code Lib.
297 The destructor function frees memory allocated by constructor, and closes related events.
298 It will ASSERT() if that related operation fails and it will always return EFI_SUCCESS.
300 @param ImageHandle The firmware allocated handle for the EFI image.
301 @param SystemTable A pointer to the EFI System Table.
303 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
308 ReportStatusCodeLibDestruct (
309 IN EFI_HANDLE ImageHandle
,
310 IN EFI_SYSTEM_TABLE
*SystemTable
316 ASSERT (gBS
!= NULL
);
317 Status
= gBS
->CloseEvent (mVirtualAddressChangeEvent
);
318 ASSERT_EFI_ERROR (Status
);
319 Status
= gBS
->CloseEvent (mExitBootServicesEvent
);
320 ASSERT_EFI_ERROR (Status
);
322 FreePool (mStatusCodeData
);
324 mSmmBase
->SmmFreePool (mSmmBase
, mStatusCodeData
);