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