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