2 Library constructor & destructor, event handlers, and other internal worker functions.
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 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
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.
15 #include "ReportStatusCodeLibInternal.h"
17 EFI_EVENT mVirtualAddressChangeEvent
;
18 EFI_EVENT mExitBootServicesEvent
;
19 EFI_STATUS_CODE_DATA
*mStatusCodeData
;
21 EFI_SMM_BASE_PROTOCOL
*mSmmBase
;
22 EFI_RUNTIME_SERVICES
*mInternalRT
;
23 BOOLEAN mHaveExitedBootServices
= FALSE
;
24 EFI_REPORT_STATUS_CODE mReportStatusCode
= NULL
;
25 EFI_SMM_STATUS_CODE_PROTOCOL
*mSmmStatusCodeProtocol
;
28 Locates and caches SMM Status Code Protocol.
32 SmmStatusCodeInitialize (
38 Status
= gBS
->LocateProtocol (&gEfiSmmStatusCodeProtocolGuid
, NULL
, (VOID
**) &mSmmStatusCodeProtocol
);
39 if (EFI_ERROR (Status
)) {
40 mSmmStatusCodeProtocol
= NULL
;
45 Report status code via SMM Status Code Protocol.
47 @param Type Indicates the type of status code being reported.
48 @param Value Describes the current status of a hardware or software entity.
49 This included information about the class and subclass that is used to classify the entity
50 as well as an operation. For progress codes, the operation is the current activity.
51 For error codes, it is the exception. For debug codes, it is not defined at this time.
52 @param Instance The enumeration of a hardware or software entity within the system.
53 A system may contain multiple entities that match a class/subclass pairing.
54 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,
55 not meaningful, or not relevant. Valid instance numbers start with 1.
56 @param CallerId This optional parameter may be used to identify the caller.
57 This parameter allows the status code driver to apply different rules to different callers.
58 @param Data This optional parameter may be used to pass additional data
60 @retval EFI_SUCCESS Always return EFI_SUCCESS.
65 IN EFI_STATUS_CODE_TYPE Type
,
66 IN EFI_STATUS_CODE_VALUE Value
,
68 IN EFI_GUID
*CallerId OPTIONAL
,
69 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
72 if (mSmmStatusCodeProtocol
!= NULL
) {
73 (mSmmStatusCodeProtocol
->ReportStatusCode
) (mSmmStatusCodeProtocol
, Type
, Value
, Instance
, CallerId
, Data
);
79 Locate the report status code service.
81 In SMM, it tries to retrieve SMM Status Code Protocol.
82 Otherwise, it first tries to retrieve ReportStatusCode() in Runtime Services Table.
83 If not found, it then tries to retrieve ReportStatusCode() API of Report Status Code Protocol.
85 @return Function pointer to the report status code service.
86 NULL is returned if no status code service is available.
89 EFI_REPORT_STATUS_CODE
90 InternalGetReportStatusCode (
94 EFI_STATUS_CODE_PROTOCOL
*StatusCodeProtocol
;
98 return (EFI_REPORT_STATUS_CODE
) SmmStatusCodeReport
;
99 } else if (mInternalRT
!= NULL
&& mInternalRT
->Hdr
.Revision
< 0x20000) {
100 return ((FRAMEWORK_EFI_RUNTIME_SERVICES
*)mInternalRT
)->ReportStatusCode
;
101 } else if (!mHaveExitedBootServices
) {
103 // Check gBS just in case. ReportStatusCode is called before gBS is initialized.
106 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&StatusCodeProtocol
);
107 if (!EFI_ERROR (Status
) && StatusCodeProtocol
!= NULL
) {
108 return StatusCodeProtocol
->ReportStatusCode
;
117 Internal worker function that reports a status code through the status code service.
119 If status code service is not cached, then this function checks if status code service is
120 available in system. If status code service is not available, then EFI_UNSUPPORTED is
121 returned. If status code service is present, then it is cached in mReportStatusCode.
122 Finally this function reports status code through the status code service.
124 @param Type Status code type.
125 @param Value Status code value.
126 @param Instance Status code instance number.
127 @param CallerId Pointer to a GUID that identifies the caller of this
128 function. This is an optional parameter that may be
130 @param Data Pointer to the extended data buffer. This is an
131 optional parameter that may be NULL.
133 @retval EFI_SUCCESS The status code was reported.
134 @retval EFI_UNSUPPORTED Status code service is not available.
135 @retval EFI_UNSUPPORTED Status code type is not supported.
139 InternalReportStatusCode (
140 IN EFI_STATUS_CODE_TYPE Type
,
141 IN EFI_STATUS_CODE_VALUE Value
,
143 IN CONST EFI_GUID
*CallerId OPTIONAL
,
144 IN EFI_STATUS_CODE_DATA
*Data OPTIONAL
147 if ((ReportProgressCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) ||
148 (ReportErrorCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) ||
149 (ReportDebugCodeEnabled() && ((Type
) & EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
)) {
151 // If mReportStatusCode is NULL, then check if status code service is available in system.
153 if (mReportStatusCode
== NULL
) {
154 mReportStatusCode
= InternalGetReportStatusCode ();
155 if (mReportStatusCode
== NULL
) {
156 return EFI_UNSUPPORTED
;
161 // A status code service is present in system, so pass in all the parameters to the service.
163 return (*mReportStatusCode
) (Type
, Value
, Instance
, (EFI_GUID
*)CallerId
, Data
);
166 return EFI_UNSUPPORTED
;
170 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
172 @param Event Event whose notification function is being invoked.
173 @param Context Pointer to the notification function's context
178 ReportStatusCodeLibVirtualAddressChange (
183 if (mReportStatusCode
!= NULL
) {
184 mInternalRT
->ConvertPointer (0, (VOID
**) &mReportStatusCode
);
186 mInternalRT
->ConvertPointer (0, (VOID
**) &mStatusCodeData
);
187 mInternalRT
->ConvertPointer (0, (VOID
**) &mInternalRT
);
191 Notification function of EVT_SIGNAL_EXIT_BOOT_SERVICES.
193 @param Event Event whose notification function is being invoked.
194 @param Context Pointer to the notification function's context
199 ReportStatusCodeLibExitBootServices (
205 // If mReportStatusCode is NULL, then see if a Status Code Protocol instance is present
206 // in the handle database.
208 if (mReportStatusCode
== NULL
) {
209 mReportStatusCode
= InternalGetReportStatusCode ();
212 mHaveExitedBootServices
= TRUE
;
216 The constructor function of SMM Runtime DXE Report Status Code Lib.
218 This function allocates memory for extended status code data, caches
219 the report status code service, and registers events.
221 @param ImageHandle The firmware allocated handle for the EFI image.
222 @param SystemTable A pointer to the EFI System Table.
224 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
229 ReportStatusCodeLibConstruct (
230 IN EFI_HANDLE ImageHandle
,
231 IN EFI_SYSTEM_TABLE
*SystemTable
237 // If in SMM mode, then allocates memory from SMRAM for extended status code data.
239 Status
= gBS
->LocateProtocol (&gEfiSmmBaseProtocolGuid
, NULL
, (VOID
**) &mSmmBase
);
240 if (!EFI_ERROR (Status
)) {
241 mSmmBase
->InSmm (mSmmBase
, &mInSmm
);
243 Status
= mSmmBase
->SmmAllocatePool (
245 EfiRuntimeServicesData
,
246 sizeof (EFI_STATUS_CODE_DATA
) + EFI_STATUS_CODE_DATA_MAX_SIZE
,
247 (VOID
**) &mStatusCodeData
249 ASSERT_EFI_ERROR (Status
);
250 SmmStatusCodeInitialize ();
257 // If not in SMM mode, then allocate runtime memory for extended status code data.
259 // Library should not use the gRT directly, for it may be converted by other library instance.
264 mStatusCodeData
= AllocateRuntimePool (sizeof (EFI_STATUS_CODE_DATA
) + EFI_STATUS_CODE_DATA_MAX_SIZE
);
265 ASSERT (mStatusCodeData
!= NULL
);
267 // Cache the report status code service
269 mReportStatusCode
= InternalGetReportStatusCode ();
272 // Register notify function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
274 Status
= gBS
->CreateEventEx (
277 ReportStatusCodeLibVirtualAddressChange
,
279 &gEfiEventVirtualAddressChangeGuid
,
280 &mVirtualAddressChangeEvent
282 ASSERT_EFI_ERROR (Status
);
285 // Register notify function for EVT_SIGNAL_EXIT_BOOT_SERVICES
287 Status
= gBS
->CreateEventEx (
290 ReportStatusCodeLibExitBootServices
,
292 &gEfiEventExitBootServicesGuid
,
293 &mExitBootServicesEvent
295 ASSERT_EFI_ERROR (Status
);
301 The destructor function of SMM Runtime DXE Report Status Code Lib.
303 The destructor function frees memory allocated by constructor, and closes related events.
304 It will ASSERT() if that related operation fails and it will always return EFI_SUCCESS.
306 @param ImageHandle The firmware allocated handle for the EFI image.
307 @param SystemTable A pointer to the EFI System Table.
309 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
314 ReportStatusCodeLibDestruct (
315 IN EFI_HANDLE ImageHandle
,
316 IN EFI_SYSTEM_TABLE
*SystemTable
322 ASSERT (gBS
!= NULL
);
323 Status
= gBS
->CloseEvent (mVirtualAddressChangeEvent
);
324 ASSERT_EFI_ERROR (Status
);
325 Status
= gBS
->CloseEvent (mExitBootServicesEvent
);
326 ASSERT_EFI_ERROR (Status
);
328 FreePool (mStatusCodeData
);
330 mSmmBase
->SmmFreePool (mSmmBase
, mStatusCodeData
);