]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/SmmRuntimeDxeReportStatusCodeLibFramework/SmmRuntimeDxeSupport.c
1) Replace mBS with gBS from UefiBootServicesTablePointer Lib to avoid library instan...
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / SmmRuntimeDxeReportStatusCodeLibFramework / SmmRuntimeDxeSupport.c
1 /** @file
2 Report Status Code Library for DXE Phase.
3
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. 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
9
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.
12
13 **/
14
15 #include "ReportStatusCodeLibInternal.h"
16
17 //
18 // Resources need by SMM runtime instance
19 //
20 #include <Library/OemHookStatusCodeLib.h>
21 #include <Protocol/SmmBase.h>
22
23 STATIC
24 EFI_EVENT mVirtualAddressChangeEvent;
25
26 STATIC
27 EFI_EVENT mExitBootServicesEvent;
28
29 STATIC
30 EFI_STATUS_CODE_DATA *mStatusCodeData;
31
32 STATIC
33 BOOLEAN mInSmm;
34
35 EFI_SMM_BASE_PROTOCOL *mSmmBase;
36
37 STATIC
38 EFI_RUNTIME_SERVICES *mRT;
39
40 STATIC
41 BOOLEAN mHaveExitedBootServices = FALSE;
42
43 /**
44 Locate he report status code service.
45
46 @return EFI_REPORT_STATUS_CODE function point to
47 ReportStatusCode.
48 **/
49 EFI_REPORT_STATUS_CODE
50 InternalGetReportStatusCode (
51 VOID
52 )
53 {
54 EFI_STATUS_CODE_PROTOCOL *StatusCodeProtocol;
55 EFI_STATUS Status;
56
57 if (mInSmm) {
58 return (EFI_REPORT_STATUS_CODE) OemHookStatusCodeReport;
59 } else if (mRT != NULL && mRT->Hdr.Revision < 0x20000) {
60 return ((FRAMEWORK_EFI_RUNTIME_SERVICES*)mRT)->ReportStatusCode;
61 } else if (!mHaveExitedBootServices) {
62 //
63 // Check gBS just in case. ReportStatusCode is called before gBS is initialized.
64 //
65 if (gBS != NULL) {
66 Status = gBS->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid, NULL, (VOID**)&StatusCodeProtocol);
67 if (!EFI_ERROR (Status) && StatusCodeProtocol != NULL) {
68 return StatusCodeProtocol->ReportStatusCode;
69 }
70 }
71 }
72
73 return NULL;
74 }
75
76
77 /**
78 Fixup internal report status code protocol interface.
79
80 @param[in] Event The Event that is being processed
81 @param[in] Context Event Context
82 **/
83 STATIC
84 VOID
85 EFIAPI
86 ReportStatusCodeLibVirtualAddressChange (
87 IN EFI_EVENT Event,
88 IN VOID *Context
89 )
90 {
91 if (NULL != mReportStatusCode) {
92 mRT->ConvertPointer (0, (VOID **) &mReportStatusCode);
93 }
94 mRT->ConvertPointer (0, (VOID **) &mStatusCodeData);
95 mRT->ConvertPointer (0, (VOID **) &mRT);
96 }
97
98 /**
99 Update the In Runtime Indicator.
100
101 @param[in] Event The Event that is being processed
102 @param[in] Context Event Context
103 **/
104 STATIC
105 VOID
106 EFIAPI
107 ReportStatusCodeLibExitBootServices (
108 IN EFI_EVENT Event,
109 IN VOID *Context
110 )
111 {
112 mHaveExitedBootServices = TRUE;
113 }
114
115 /**
116 Intialize Report Status Code Lib.
117
118 @param[in] ImageHandle The firmware allocated handle for the EFI image.
119 @param[in] SystemTable A pointer to the EFI System Table.
120
121 @return EFI_STATUS always returns EFI_SUCCESS.
122 **/
123 EFI_STATUS
124 EFIAPI
125 ReportStatusCodeLibConstruct (
126 IN EFI_HANDLE ImageHandle,
127 IN EFI_SYSTEM_TABLE *SystemTable
128 )
129 {
130 EFI_STATUS Status;
131
132 //
133 // SMM driver depends on the SMM BASE protocol.
134 // the SMM driver must be success to locate protocol.
135 //
136 Status = gBS->LocateProtocol (&gEfiSmmBaseProtocolGuid, NULL, (VOID **) &mSmmBase);
137 if (!EFI_ERROR (Status)) {
138 mSmmBase->InSmm (mSmmBase, &mInSmm);
139 if (mInSmm) {
140 Status = mSmmBase->SmmAllocatePool (
141 mSmmBase,
142 EfiRuntimeServicesData,
143 sizeof (EFI_STATUS_CODE_DATA) + EFI_STATUS_CODE_DATA_MAX_SIZE,
144 (VOID **) &mStatusCodeData
145 );
146 ASSERT_EFI_ERROR (Status);
147 OemHookStatusCodeInitialize ();
148 return EFI_SUCCESS;
149 }
150 }
151
152 //
153 // Library should not use the gRT directly, since it
154 // may be converted by other library instance.
155 //
156 mRT = gRT;
157 mInSmm = FALSE;
158
159 gBS->AllocatePool (EfiRuntimeServicesData, sizeof (EFI_STATUS_CODE_DATA) + EFI_STATUS_CODE_DATA_MAX_SIZE, (VOID **)&mStatusCodeData);
160 ASSERT (NULL != mStatusCodeData);
161 //
162 // Cache the report status code service
163 //
164 mReportStatusCode = InternalGetReportStatusCode ();
165
166 //
167 // Register the call back of virtual address change
168 //
169 Status = gBS->CreateEvent (
170 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
171 TPL_NOTIFY,
172 ReportStatusCodeLibVirtualAddressChange,
173 NULL,
174 &mVirtualAddressChangeEvent
175 );
176 ASSERT_EFI_ERROR (Status);
177
178
179 //
180 // Register the call back of virtual address change
181 //
182 Status = gBS->CreateEvent (
183 EVT_SIGNAL_EXIT_BOOT_SERVICES,
184 TPL_NOTIFY,
185 ReportStatusCodeLibExitBootServices,
186 NULL,
187 &mExitBootServicesEvent
188 );
189 ASSERT_EFI_ERROR (Status);
190
191 return Status;
192 }
193
194 /**
195 Desctructor of library will close events.
196
197 @param ImageHandle callder module's image handle
198 @param SystemTable pointer to EFI system table.
199 @return the status of close event.
200 **/
201 EFI_STATUS
202 EFIAPI
203 ReportStatusCodeLibDestruct (
204 IN EFI_HANDLE ImageHandle,
205 IN EFI_SYSTEM_TABLE *SystemTable
206 )
207 {
208 EFI_STATUS Status;
209
210 if (!mInSmm) {
211 //
212 // Close SetVirtualAddressMap () notify function
213 //
214 ASSERT (gBS != NULL);
215 Status = gBS->CloseEvent (mVirtualAddressChangeEvent);
216 ASSERT_EFI_ERROR (Status);
217 Status = gBS->CloseEvent (mExitBootServicesEvent);
218 ASSERT_EFI_ERROR (Status);
219
220 gBS->FreePool (mStatusCodeData);
221 } else {
222 mSmmBase->SmmFreePool (mSmmBase, mStatusCodeData);
223 }
224
225 return EFI_SUCCESS;
226 }
227
228 /**
229 Reports a status code with full parameters.
230
231 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
232 is 0, then an extended data buffer is not reported. If ExtendedData is not
233 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
234 ExtendedData is assumed not have the standard status code header, so this function
235 is responsible for allocating a buffer large enough for the standard header and
236 the extended data passed into this function. The standard header is filled in
237 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
238 GUID of gEfiStatusCodeSpecificDatauid is used. The status code is reported with
239 an instance specified by Instance and a caller ID specified by CallerId. If
240 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
241
242 ReportStatusCodeEx()must actively prevent recursion. If ReportStatusCodeEx()
243 is called while processing another any other Report Status Code Library function,
244 then ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
245
246 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
247 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
248
249 @param Type Status code type.
250 @param Value Status code value.
251 @param Instance Status code instance number.
252 @param CallerId Pointer to a GUID that identifies the caller of this
253 function. If this parameter is NULL, then a caller
254 ID of gEfiCallerIdGuid is used.
255 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
256 If this parameter is NULL, then a the status code
257 standard header is filled in with
258 gEfiStatusCodeSpecificDataGuid.
259 @param ExtendedData Pointer to the extended data buffer. This is an
260 optional parameter that may be NULL.
261 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
262
263 @retval EFI_SUCCESS The status code was reported.
264 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
265 the extended data section if it was specified.
266 @retval EFI_UNSUPPORTED Report status code is not supported
267
268 **/
269 EFI_STATUS
270 EFIAPI
271 InternalReportStatusCodeEx (
272 IN EFI_STATUS_CODE_TYPE Type,
273 IN EFI_STATUS_CODE_VALUE Value,
274 IN UINT32 Instance,
275 IN CONST EFI_GUID *CallerId OPTIONAL,
276 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
277 IN CONST VOID *ExtendedData OPTIONAL,
278 IN UINTN ExtendedDataSize
279 )
280 {
281 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
282 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
283
284 if (ExtendedDataSize > EFI_STATUS_CODE_DATA_MAX_SIZE) {
285 return EFI_OUT_OF_RESOURCES;
286 }
287
288 //
289 // Fill in the extended data header
290 //
291 mStatusCodeData->HeaderSize = sizeof (EFI_STATUS_CODE_DATA);
292 mStatusCodeData->Size = (UINT16)ExtendedDataSize;
293 if (ExtendedDataGuid == NULL) {
294 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
295 }
296 CopyGuid (&mStatusCodeData->Type, ExtendedDataGuid);
297
298 //
299 // Fill in the extended data buffer
300 //
301 CopyMem (mStatusCodeData + 1, ExtendedData, ExtendedDataSize);
302
303 //
304 // Report the status code
305 //
306 if (CallerId == NULL) {
307 CallerId = &gEfiCallerIdGuid;
308 }
309 return InternalReportStatusCode (Type, Value, Instance, CallerId, mStatusCodeData);
310 }
311