]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/SmmRuntimeDxeReportStatusCodeLibFramework/SmmRuntimeDxeSupport.c
Patch to remove STATIC modifier. This is on longer recommended by EFI Framework codin...
[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->CreateEvent (
162 EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
163 TPL_NOTIFY,
164 ReportStatusCodeLibVirtualAddressChange,
165 NULL,
166 &mVirtualAddressChangeEvent
167 );
168 ASSERT_EFI_ERROR (Status);
169
170
171 //
172 // Register the call back of virtual address change
173 //
174 Status = gBS->CreateEvent (
175 EVT_SIGNAL_EXIT_BOOT_SERVICES,
176 TPL_NOTIFY,
177 ReportStatusCodeLibExitBootServices,
178 NULL,
179 &mExitBootServicesEvent
180 );
181 ASSERT_EFI_ERROR (Status);
182
183 return Status;
184 }
185
186 /**
187 Desctructor of library will close events.
188
189 @param ImageHandle callder module's image handle
190 @param SystemTable pointer to EFI system table.
191 @return the status of close event.
192 **/
193 EFI_STATUS
194 EFIAPI
195 ReportStatusCodeLibDestruct (
196 IN EFI_HANDLE ImageHandle,
197 IN EFI_SYSTEM_TABLE *SystemTable
198 )
199 {
200 EFI_STATUS Status;
201
202 if (!mInSmm) {
203 //
204 // Close SetVirtualAddressMap () notify function
205 //
206 ASSERT (gBS != NULL);
207 Status = gBS->CloseEvent (mVirtualAddressChangeEvent);
208 ASSERT_EFI_ERROR (Status);
209 Status = gBS->CloseEvent (mExitBootServicesEvent);
210 ASSERT_EFI_ERROR (Status);
211
212 gBS->FreePool (mStatusCodeData);
213 } else {
214 mSmmBase->SmmFreePool (mSmmBase, mStatusCodeData);
215 }
216
217 return EFI_SUCCESS;
218 }
219
220 /**
221 Reports a status code with full parameters.
222
223 The function reports a status code. If ExtendedData is NULL and ExtendedDataSize
224 is 0, then an extended data buffer is not reported. If ExtendedData is not
225 NULL and ExtendedDataSize is not 0, then an extended data buffer is allocated.
226 ExtendedData is assumed not have the standard status code header, so this function
227 is responsible for allocating a buffer large enough for the standard header and
228 the extended data passed into this function. The standard header is filled in
229 with a GUID specified by ExtendedDataGuid. If ExtendedDataGuid is NULL, then a
230 GUID of gEfiStatusCodeSpecificDatauid is used. The status code is reported with
231 an instance specified by Instance and a caller ID specified by CallerId. If
232 CallerId is NULL, then a caller ID of gEfiCallerIdGuid is used.
233
234 ReportStatusCodeEx()must actively prevent recursion. If ReportStatusCodeEx()
235 is called while processing another any other Report Status Code Library function,
236 then ReportStatusCodeEx() must return EFI_DEVICE_ERROR immediately.
237
238 If ExtendedData is NULL and ExtendedDataSize is not zero, then ASSERT().
239 If ExtendedData is not NULL and ExtendedDataSize is zero, then ASSERT().
240
241 @param Type Status code type.
242 @param Value Status code value.
243 @param Instance Status code instance number.
244 @param CallerId Pointer to a GUID that identifies the caller of this
245 function. If this parameter is NULL, then a caller
246 ID of gEfiCallerIdGuid is used.
247 @param ExtendedDataGuid Pointer to the GUID for the extended data buffer.
248 If this parameter is NULL, then a the status code
249 standard header is filled in with
250 gEfiStatusCodeSpecificDataGuid.
251 @param ExtendedData Pointer to the extended data buffer. This is an
252 optional parameter that may be NULL.
253 @param ExtendedDataSize The size, in bytes, of the extended data buffer.
254
255 @retval EFI_SUCCESS The status code was reported.
256 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate
257 the extended data section if it was specified.
258 @retval EFI_UNSUPPORTED Report status code is not supported
259
260 **/
261 EFI_STATUS
262 EFIAPI
263 InternalReportStatusCodeEx (
264 IN EFI_STATUS_CODE_TYPE Type,
265 IN EFI_STATUS_CODE_VALUE Value,
266 IN UINT32 Instance,
267 IN CONST EFI_GUID *CallerId OPTIONAL,
268 IN CONST EFI_GUID *ExtendedDataGuid OPTIONAL,
269 IN CONST VOID *ExtendedData OPTIONAL,
270 IN UINTN ExtendedDataSize
271 )
272 {
273 ASSERT (!((ExtendedData == NULL) && (ExtendedDataSize != 0)));
274 ASSERT (!((ExtendedData != NULL) && (ExtendedDataSize == 0)));
275
276 if (ExtendedDataSize > EFI_STATUS_CODE_DATA_MAX_SIZE) {
277 return EFI_OUT_OF_RESOURCES;
278 }
279
280 //
281 // Fill in the extended data header
282 //
283 mStatusCodeData->HeaderSize = sizeof (EFI_STATUS_CODE_DATA);
284 mStatusCodeData->Size = (UINT16)ExtendedDataSize;
285 if (ExtendedDataGuid == NULL) {
286 ExtendedDataGuid = &gEfiStatusCodeSpecificDataGuid;
287 }
288 CopyGuid (&mStatusCodeData->Type, ExtendedDataGuid);
289
290 //
291 // Fill in the extended data buffer
292 //
293 CopyMem (mStatusCodeData + 1, ExtendedData, ExtendedDataSize);
294
295 //
296 // Report the status code
297 //
298 if (CallerId == NULL) {
299 CallerId = &gEfiCallerIdGuid;
300 }
301 return InternalReportStatusCode (Type, Value, Instance, CallerId, mStatusCodeData);
302 }
303