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