]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/SmmRuntimeDxeReportStatusCodeLibFramework/SmmRuntimeDxeSupport.c
OvmfPkg: Drop build flag USE_LEGACY_ISA_STACK and legacy ISA stack
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / SmmRuntimeDxeReportStatusCodeLibFramework / SmmRuntimeDxeSupport.c
1 /** @file
2 Library constructor & destructor, event handlers, and other internal worker functions.
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "ReportStatusCodeLibInternal.h"
10
11 EFI_EVENT mVirtualAddressChangeEvent;
12 static EFI_EVENT mExitBootServicesEvent;
13 EFI_STATUS_CODE_DATA *mStatusCodeData;
14 BOOLEAN mInSmm;
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;
20
21 /**
22 Locates and caches SMM Status Code Protocol.
23
24 **/
25 VOID
26 SmmStatusCodeInitialize (
27 VOID
28 )
29 {
30 EFI_STATUS Status;
31
32 Status = gBS->LocateProtocol (&gEfiSmmStatusCodeProtocolGuid, NULL, (VOID **) &mSmmStatusCodeProtocol);
33 if (EFI_ERROR (Status)) {
34 mSmmStatusCodeProtocol = NULL;
35 }
36 }
37
38 /**
39 Report status code via SMM Status Code Protocol.
40
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
53
54 @retval EFI_SUCCESS Always return EFI_SUCCESS.
55
56 **/
57 EFI_STATUS
58 SmmStatusCodeReport (
59 IN EFI_STATUS_CODE_TYPE Type,
60 IN EFI_STATUS_CODE_VALUE Value,
61 IN UINT32 Instance,
62 IN EFI_GUID *CallerId OPTIONAL,
63 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
64 )
65 {
66 if (mSmmStatusCodeProtocol != NULL) {
67 (mSmmStatusCodeProtocol->ReportStatusCode) (mSmmStatusCodeProtocol, Type, Value, Instance, CallerId, Data);
68 }
69 return EFI_SUCCESS;
70 }
71
72 /**
73 Locate the report status code service.
74
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.
78
79 @return Function pointer to the report status code service.
80 NULL is returned if no status code service is available.
81
82 **/
83 EFI_REPORT_STATUS_CODE
84 InternalGetReportStatusCode (
85 VOID
86 )
87 {
88 EFI_STATUS_CODE_PROTOCOL *StatusCodeProtocol;
89 EFI_STATUS Status;
90
91 if (mInSmm) {
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) {
96 //
97 // Check gBS just in case. ReportStatusCode is called before gBS is initialized.
98 //
99 if (gBS != NULL) {
100 Status = gBS->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid, NULL, (VOID**)&StatusCodeProtocol);
101 if (!EFI_ERROR (Status) && StatusCodeProtocol != NULL) {
102 return StatusCodeProtocol->ReportStatusCode;
103 }
104 }
105 }
106
107 return NULL;
108 }
109
110 /**
111 Internal worker function that reports a status code through the status code service.
112
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.
117
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
123 NULL.
124 @param Data Pointer to the extended data buffer. This is an
125 optional parameter that may be NULL.
126
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.
130
131 **/
132 EFI_STATUS
133 InternalReportStatusCode (
134 IN EFI_STATUS_CODE_TYPE Type,
135 IN EFI_STATUS_CODE_VALUE Value,
136 IN UINT32 Instance,
137 IN CONST EFI_GUID *CallerId OPTIONAL,
138 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
139 )
140 {
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)) {
144 //
145 // If mReportStatusCode is NULL, then check if status code service is available in system.
146 //
147 if (mReportStatusCode == NULL) {
148 mReportStatusCode = InternalGetReportStatusCode ();
149 if (mReportStatusCode == NULL) {
150 return EFI_UNSUPPORTED;
151 }
152 }
153
154 //
155 // A status code service is present in system, so pass in all the parameters to the service.
156 //
157 return (*mReportStatusCode) (Type, Value, Instance, (EFI_GUID *)CallerId, Data);
158 }
159
160 return EFI_UNSUPPORTED;
161 }
162
163 /**
164 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
165
166 @param Event Event whose notification function is being invoked.
167 @param Context Pointer to the notification function's context
168
169 **/
170 VOID
171 EFIAPI
172 ReportStatusCodeLibVirtualAddressChange (
173 IN EFI_EVENT Event,
174 IN VOID *Context
175 )
176 {
177 if (mReportStatusCode != NULL) {
178 mInternalRT->ConvertPointer (0, (VOID **) &mReportStatusCode);
179 }
180 mInternalRT->ConvertPointer (0, (VOID **) &mStatusCodeData);
181 mInternalRT->ConvertPointer (0, (VOID **) &mInternalRT);
182 }
183
184 /**
185 Notification function of EVT_SIGNAL_EXIT_BOOT_SERVICES.
186
187 @param Event Event whose notification function is being invoked.
188 @param Context Pointer to the notification function's context
189
190 **/
191 VOID
192 EFIAPI
193 ReportStatusCodeLibExitBootServices (
194 IN EFI_EVENT Event,
195 IN VOID *Context
196 )
197 {
198 //
199 // If mReportStatusCode is NULL, then see if a Status Code Protocol instance is present
200 // in the handle database.
201 //
202 if (mReportStatusCode == NULL) {
203 mReportStatusCode = InternalGetReportStatusCode ();
204 }
205
206 mHaveExitedBootServices = TRUE;
207 }
208
209 /**
210 The constructor function of SMM Runtime DXE Report Status Code Lib.
211
212 This function allocates memory for extended status code data, caches
213 the report status code service, and registers events.
214
215 @param ImageHandle The firmware allocated handle for the EFI image.
216 @param SystemTable A pointer to the EFI System Table.
217
218 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
219
220 **/
221 EFI_STATUS
222 EFIAPI
223 ReportStatusCodeLibConstruct (
224 IN EFI_HANDLE ImageHandle,
225 IN EFI_SYSTEM_TABLE *SystemTable
226 )
227 {
228 EFI_STATUS Status;
229
230 //
231 // If in SMM mode, then allocates memory from SMRAM for extended status code data.
232 //
233 Status = gBS->LocateProtocol (&gEfiSmmBaseProtocolGuid, NULL, (VOID **) &mSmmBase);
234 if (!EFI_ERROR (Status)) {
235 mSmmBase->InSmm (mSmmBase, &mInSmm);
236 if (mInSmm) {
237 Status = mSmmBase->SmmAllocatePool (
238 mSmmBase,
239 EfiRuntimeServicesData,
240 sizeof (EFI_STATUS_CODE_DATA) + EFI_STATUS_CODE_DATA_MAX_SIZE,
241 (VOID **) &mStatusCodeData
242 );
243 ASSERT_EFI_ERROR (Status);
244 SmmStatusCodeInitialize ();
245 return EFI_SUCCESS;
246 }
247 }
248
249
250 //
251 // If not in SMM mode, then allocate runtime memory for extended status code data.
252 //
253 // Library should not use the gRT directly, for it may be converted by other library instance.
254 //
255 mInternalRT = gRT;
256 mInSmm = FALSE;
257
258 mStatusCodeData = AllocateRuntimePool (sizeof (EFI_STATUS_CODE_DATA) + EFI_STATUS_CODE_DATA_MAX_SIZE);
259 ASSERT (mStatusCodeData != NULL);
260 //
261 // Cache the report status code service
262 //
263 mReportStatusCode = InternalGetReportStatusCode ();
264
265 //
266 // Register notify function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
267 //
268 Status = gBS->CreateEventEx (
269 EVT_NOTIFY_SIGNAL,
270 TPL_NOTIFY,
271 ReportStatusCodeLibVirtualAddressChange,
272 NULL,
273 &gEfiEventVirtualAddressChangeGuid,
274 &mVirtualAddressChangeEvent
275 );
276 ASSERT_EFI_ERROR (Status);
277
278 //
279 // Register notify function for EVT_SIGNAL_EXIT_BOOT_SERVICES
280 //
281 Status = gBS->CreateEventEx (
282 EVT_NOTIFY_SIGNAL,
283 TPL_NOTIFY,
284 ReportStatusCodeLibExitBootServices,
285 NULL,
286 &gEfiEventExitBootServicesGuid,
287 &mExitBootServicesEvent
288 );
289 ASSERT_EFI_ERROR (Status);
290
291 return EFI_SUCCESS;
292 }
293
294 /**
295 The destructor function of SMM Runtime DXE Report Status Code Lib.
296
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.
299
300 @param ImageHandle The firmware allocated handle for the EFI image.
301 @param SystemTable A pointer to the EFI System Table.
302
303 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
304
305 **/
306 EFI_STATUS
307 EFIAPI
308 ReportStatusCodeLibDestruct (
309 IN EFI_HANDLE ImageHandle,
310 IN EFI_SYSTEM_TABLE *SystemTable
311 )
312 {
313 EFI_STATUS Status;
314
315 if (!mInSmm) {
316 ASSERT (gBS != NULL);
317 Status = gBS->CloseEvent (mVirtualAddressChangeEvent);
318 ASSERT_EFI_ERROR (Status);
319 Status = gBS->CloseEvent (mExitBootServicesEvent);
320 ASSERT_EFI_ERROR (Status);
321
322 FreePool (mStatusCodeData);
323 } else {
324 mSmmBase->SmmFreePool (mSmmBase, mStatusCodeData);
325 }
326
327 return EFI_SUCCESS;
328 }
329