]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/SmmRuntimeDxeReportStatusCodeLibFramework/SmmRuntimeDxeSupport.c
IntelFrameworkModulePkg: make global variable static
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / SmmRuntimeDxeReportStatusCodeLibFramework / SmmRuntimeDxeSupport.c
CommitLineData
e5516b49 1/** @file\r
29f766e4 2 Library constructor & destructor, event handlers, and other internal worker functions.\r
e5516b49 3\r
4e4b2304 4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
c0a00b14 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e5516b49 6\r
7**/\r
8\r
9#include "ReportStatusCodeLibInternal.h"\r
10\r
19796be3 11EFI_EVENT mVirtualAddressChangeEvent;\r
4e4b2304 12static EFI_EVENT mExitBootServicesEvent;\r
19796be3 13EFI_STATUS_CODE_DATA *mStatusCodeData;\r
14BOOLEAN mInSmm;\r
15EFI_SMM_BASE_PROTOCOL *mSmmBase;\r
16EFI_RUNTIME_SERVICES *mInternalRT;\r
17BOOLEAN mHaveExitedBootServices = FALSE;\r
18EFI_REPORT_STATUS_CODE mReportStatusCode = NULL;\r
19EFI_SMM_STATUS_CODE_PROTOCOL *mSmmStatusCodeProtocol;\r
20\r
21/**\r
22 Locates and caches SMM Status Code Protocol.\r
0a6f4824 23\r
19796be3 24**/\r
25VOID\r
26SmmStatusCodeInitialize (\r
27 VOID\r
28 )\r
29{\r
30 EFI_STATUS Status;\r
31\r
32 Status = gBS->LocateProtocol (&gEfiSmmStatusCodeProtocolGuid, NULL, (VOID **) &mSmmStatusCodeProtocol);\r
33 if (EFI_ERROR (Status)) {\r
34 mSmmStatusCodeProtocol = NULL;\r
35 }\r
36}\r
37\r
38/**\r
39 Report status code via SMM Status Code Protocol.\r
0a6f4824 40\r
19796be3 41 @param Type Indicates the type of status code being reported.\r
0a6f4824
LG
42 @param Value Describes the current status of a hardware or software entity.\r
43 This included information about the class and subclass that is used to classify the entity\r
44 as well as an operation. For progress codes, the operation is the current activity.\r
45 For error codes, it is the exception. For debug codes, it is not defined at this time.\r
46 @param Instance The enumeration of a hardware or software entity within the system.\r
47 A system may contain multiple entities that match a class/subclass pairing.\r
48 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,\r
19796be3 49 not meaningful, or not relevant. Valid instance numbers start with 1.\r
0a6f4824
LG
50 @param CallerId This optional parameter may be used to identify the caller.\r
51 This parameter allows the status code driver to apply different rules to different callers.\r
19796be3 52 @param Data This optional parameter may be used to pass additional data\r
0a6f4824 53\r
19796be3 54 @retval EFI_SUCCESS Always return EFI_SUCCESS.\r
55\r
56**/\r
57EFI_STATUS\r
58SmmStatusCodeReport (\r
59 IN EFI_STATUS_CODE_TYPE Type,\r
60 IN EFI_STATUS_CODE_VALUE Value,\r
61 IN UINT32 Instance,\r
62 IN EFI_GUID *CallerId OPTIONAL,\r
63 IN EFI_STATUS_CODE_DATA *Data OPTIONAL\r
64 )\r
65{\r
66 if (mSmmStatusCodeProtocol != NULL) {\r
67 (mSmmStatusCodeProtocol->ReportStatusCode) (mSmmStatusCodeProtocol, Type, Value, Instance, CallerId, Data);\r
68 }\r
69 return EFI_SUCCESS;\r
70}\r
e5516b49 71\r
29f766e4 72/**\r
73 Locate the report status code service.\r
b13b4473 74\r
19796be3 75 In SMM, it tries to retrieve SMM Status Code Protocol.\r
29f766e4 76 Otherwise, it first tries to retrieve ReportStatusCode() in Runtime Services Table.\r
77 If not found, it then tries to retrieve ReportStatusCode() API of Report Status Code Protocol.\r
e5516b49 78\r
29f766e4 79 @return Function pointer to the report status code service.\r
80 NULL is returned if no status code service is available.\r
e5516b49 81\r
e5516b49 82**/\r
83EFI_REPORT_STATUS_CODE\r
84InternalGetReportStatusCode (\r
85 VOID\r
86 )\r
87{\r
88 EFI_STATUS_CODE_PROTOCOL *StatusCodeProtocol;\r
e5516b49 89 EFI_STATUS Status;\r
90\r
404d4e5c 91 if (mInSmm) {\r
19796be3 92 return (EFI_REPORT_STATUS_CODE) SmmStatusCodeReport;\r
29f766e4 93 } else if (mInternalRT != NULL && mInternalRT->Hdr.Revision < 0x20000) {\r
94 return ((FRAMEWORK_EFI_RUNTIME_SERVICES*)mInternalRT)->ReportStatusCode;\r
e5516b49 95 } else if (!mHaveExitedBootServices) {\r
0a6f4824
LG
96 //\r
97 // Check gBS just in case. ReportStatusCode is called before gBS is initialized.\r
98 //\r
9556741c 99 if (gBS != NULL) {\r
100 Status = gBS->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid, NULL, (VOID**)&StatusCodeProtocol);\r
101 if (!EFI_ERROR (Status) && StatusCodeProtocol != NULL) {\r
102 return StatusCodeProtocol->ReportStatusCode;\r
103 }\r
e5516b49 104 }\r
105 }\r
106\r
107 return NULL;\r
108}\r
109\r
29f766e4 110/**\r
111 Internal worker function that reports a status code through the status code service.\r
112\r
113 If status code service is not cached, then this function checks if status code service is\r
114 available in system. If status code service is not available, then EFI_UNSUPPORTED is\r
115 returned. If status code service is present, then it is cached in mReportStatusCode.\r
116 Finally this function reports status code through the status code service.\r
117\r
118 @param Type Status code type.\r
119 @param Value Status code value.\r
120 @param Instance Status code instance number.\r
121 @param CallerId Pointer to a GUID that identifies the caller of this\r
122 function. This is an optional parameter that may be\r
123 NULL.\r
124 @param Data Pointer to the extended data buffer. This is an\r
125 optional parameter that may be NULL.\r
126\r
127 @retval EFI_SUCCESS The status code was reported.\r
128 @retval EFI_UNSUPPORTED Status code service is not available.\r
129 @retval EFI_UNSUPPORTED Status code type is not supported.\r
130\r
131**/\r
132EFI_STATUS\r
133InternalReportStatusCode (\r
134 IN EFI_STATUS_CODE_TYPE Type,\r
135 IN EFI_STATUS_CODE_VALUE Value,\r
136 IN UINT32 Instance,\r
137 IN CONST EFI_GUID *CallerId OPTIONAL,\r
138 IN EFI_STATUS_CODE_DATA *Data OPTIONAL\r
139 )\r
140{\r
141 if ((ReportProgressCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) ||\r
142 (ReportErrorCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) ||\r
143 (ReportDebugCodeEnabled() && ((Type) & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE)) {\r
144 //\r
145 // If mReportStatusCode is NULL, then check if status code service is available in system.\r
146 //\r
147 if (mReportStatusCode == NULL) {\r
148 mReportStatusCode = InternalGetReportStatusCode ();\r
149 if (mReportStatusCode == NULL) {\r
150 return EFI_UNSUPPORTED;\r
151 }\r
152 }\r
0a6f4824 153\r
29f766e4 154 //\r
155 // A status code service is present in system, so pass in all the parameters to the service.\r
156 //\r
157 return (*mReportStatusCode) (Type, Value, Instance, (EFI_GUID *)CallerId, Data);\r
158 }\r
0a6f4824 159\r
29f766e4 160 return EFI_UNSUPPORTED;\r
161}\r
e5516b49 162\r
163/**\r
29f766e4 164 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
165\r
166 @param Event Event whose notification function is being invoked.\r
167 @param Context Pointer to the notification function's context\r
e5516b49 168\r
e5516b49 169**/\r
e5516b49 170VOID\r
171EFIAPI\r
172ReportStatusCodeLibVirtualAddressChange (\r
173 IN EFI_EVENT Event,\r
174 IN VOID *Context\r
175 )\r
176{\r
29f766e4 177 if (mReportStatusCode != NULL) {\r
178 mInternalRT->ConvertPointer (0, (VOID **) &mReportStatusCode);\r
e5516b49 179 }\r
29f766e4 180 mInternalRT->ConvertPointer (0, (VOID **) &mStatusCodeData);\r
181 mInternalRT->ConvertPointer (0, (VOID **) &mInternalRT);\r
e5516b49 182}\r
183\r
184/**\r
29f766e4 185 Notification function of EVT_SIGNAL_EXIT_BOOT_SERVICES.\r
186\r
187 @param Event Event whose notification function is being invoked.\r
188 @param Context Pointer to the notification function's context\r
e5516b49 189\r
e5516b49 190**/\r
e5516b49 191VOID\r
192EFIAPI\r
193ReportStatusCodeLibExitBootServices (\r
194 IN EFI_EVENT Event,\r
195 IN VOID *Context\r
196 )\r
197{\r
3c28a728
LG
198 //\r
199 // If mReportStatusCode is NULL, then see if a Status Code Protocol instance is present\r
200 // in the handle database.\r
201 //\r
202 if (mReportStatusCode == NULL) {\r
203 mReportStatusCode = InternalGetReportStatusCode ();\r
204 }\r
205\r
e5516b49 206 mHaveExitedBootServices = TRUE;\r
207}\r
208\r
209/**\r
29f766e4 210 The constructor function of SMM Runtime DXE Report Status Code Lib.\r
e5516b49 211\r
29f766e4 212 This function allocates memory for extended status code data, caches\r
213 the report status code service, and registers events.\r
214\r
215 @param ImageHandle The firmware allocated handle for the EFI image.\r
216 @param SystemTable A pointer to the EFI System Table.\r
0a6f4824 217\r
29f766e4 218 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
e5516b49 219\r
e5516b49 220**/\r
221EFI_STATUS\r
222EFIAPI\r
223ReportStatusCodeLibConstruct (\r
224 IN EFI_HANDLE ImageHandle,\r
225 IN EFI_SYSTEM_TABLE *SystemTable\r
226 )\r
227{\r
29f766e4 228 EFI_STATUS Status;\r
e5516b49 229\r
230 //\r
29f766e4 231 // If in SMM mode, then allocates memory from SMRAM for extended status code data.\r
232 //\r
9556741c 233 Status = gBS->LocateProtocol (&gEfiSmmBaseProtocolGuid, NULL, (VOID **) &mSmmBase);\r
e5516b49 234 if (!EFI_ERROR (Status)) {\r
c0522bd7 235 mSmmBase->InSmm (mSmmBase, &mInSmm);\r
404d4e5c 236 if (mInSmm) {\r
c0522bd7 237 Status = mSmmBase->SmmAllocatePool (\r
238 mSmmBase,\r
0a6f4824
LG
239 EfiRuntimeServicesData,\r
240 sizeof (EFI_STATUS_CODE_DATA) + EFI_STATUS_CODE_DATA_MAX_SIZE,\r
e5516b49 241 (VOID **) &mStatusCodeData\r
242 );\r
243 ASSERT_EFI_ERROR (Status);\r
19796be3 244 SmmStatusCodeInitialize ();\r
e5516b49 245 return EFI_SUCCESS;\r
246 }\r
247 }\r
248\r
29f766e4 249\r
b13b4473 250 //\r
29f766e4 251 // If not in SMM mode, then allocate runtime memory for extended status code data.\r
252 //\r
253 // Library should not use the gRT directly, for it may be converted by other library instance.\r
0a6f4824 254 //\r
29f766e4 255 mInternalRT = gRT;\r
256 mInSmm = FALSE;\r
b13b4473 257\r
29f766e4 258 mStatusCodeData = AllocateRuntimePool (sizeof (EFI_STATUS_CODE_DATA) + EFI_STATUS_CODE_DATA_MAX_SIZE);\r
259 ASSERT (mStatusCodeData != NULL);\r
e5516b49 260 //\r
261 // Cache the report status code service\r
0a6f4824 262 //\r
e5516b49 263 mReportStatusCode = InternalGetReportStatusCode ();\r
264\r
265 //\r
29f766e4 266 // Register notify function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE\r
0a6f4824 267 //\r
6a27a4eb 268 Status = gBS->CreateEventEx (\r
269 EVT_NOTIFY_SIGNAL,\r
e5516b49 270 TPL_NOTIFY,\r
271 ReportStatusCodeLibVirtualAddressChange,\r
272 NULL,\r
6a27a4eb 273 &gEfiEventVirtualAddressChangeGuid,\r
e5516b49 274 &mVirtualAddressChangeEvent\r
275 );\r
276 ASSERT_EFI_ERROR (Status);\r
277\r
e5516b49 278 //\r
29f766e4 279 // Register notify function for EVT_SIGNAL_EXIT_BOOT_SERVICES\r
0a6f4824 280 //\r
6a27a4eb 281 Status = gBS->CreateEventEx (\r
282 EVT_NOTIFY_SIGNAL,\r
e5516b49 283 TPL_NOTIFY,\r
284 ReportStatusCodeLibExitBootServices,\r
285 NULL,\r
6a27a4eb 286 &gEfiEventExitBootServicesGuid,\r
e5516b49 287 &mExitBootServicesEvent\r
288 );\r
289 ASSERT_EFI_ERROR (Status);\r
290\r
29f766e4 291 return EFI_SUCCESS;\r
e5516b49 292}\r
293\r
ed7752ec 294/**\r
29f766e4 295 The destructor function of SMM Runtime DXE Report Status Code Lib.\r
0a6f4824 296\r
29f766e4 297 The destructor function frees memory allocated by constructor, and closes related events.\r
0a6f4824 298 It will ASSERT() if that related operation fails and it will always return EFI_SUCCESS.\r
29f766e4 299\r
300 @param ImageHandle The firmware allocated handle for the EFI image.\r
301 @param SystemTable A pointer to the EFI System Table.\r
0a6f4824 302\r
29f766e4 303 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
304\r
ed7752ec 305**/\r
e5516b49 306EFI_STATUS\r
307EFIAPI\r
308ReportStatusCodeLibDestruct (\r
309 IN EFI_HANDLE ImageHandle,\r
310 IN EFI_SYSTEM_TABLE *SystemTable\r
311 )\r
312{\r
313 EFI_STATUS Status;\r
314\r
c0522bd7 315 if (!mInSmm) {\r
9556741c 316 ASSERT (gBS != NULL);\r
317 Status = gBS->CloseEvent (mVirtualAddressChangeEvent);\r
c0522bd7 318 ASSERT_EFI_ERROR (Status);\r
9556741c 319 Status = gBS->CloseEvent (mExitBootServicesEvent);\r
c0522bd7 320 ASSERT_EFI_ERROR (Status);\r
321\r
29f766e4 322 FreePool (mStatusCodeData);\r
c0522bd7 323 } else {\r
324 mSmmBase->SmmFreePool (mSmmBase, mStatusCodeData);\r
325 }\r
e5516b49 326\r
c0522bd7 327 return EFI_SUCCESS;\r
e5516b49 328}\r
329\r