]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/StatusCode/Dxe/DataHubStatusCodeWorker.c
Follow up EDKT238, EDKT239, EDKT242, EDKT243
[mirror_edk2.git] / EdkModulePkg / Universal / StatusCode / Dxe / DataHubStatusCodeWorker.c
1 /** @file
2 Data Hub status code worker in DXE.
3
4 Copyright (c) 2006, Intel Corporation
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 Module Name: DataHubStatusCodeWorker.c
14
15 **/
16 #include "DxeStatusCode.h"
17
18 //
19 // Initialize FIFO to cache records.
20 //
21 STATIC
22 EFI_LOCK mFifoLock = EFI_INITIALIZE_LOCK_VARIABLE (EFI_TPL_HIGH_LEVEL);
23 STATIC
24 LIST_ENTRY mRecordsFifo = INITIALIZE_LIST_HEAD_VARIABLE (mRecordsFifo);
25 STATIC
26 UINTN mNumberOfRecords = 0;
27 STATIC
28 EFI_EVENT mLogDataHubEvent;
29 //
30 // Cache data hub protocol.
31 //
32 STATIC
33 EFI_DATA_HUB_PROTOCOL *mDataHubProtocol;
34
35
36 /**
37 Return buffer of length DATAHUB_STATUSCODE_RECORD
38
39 @retval NULL Can not allocate free memeory for record.
40 @retval !NULL Point to buffer of record.
41
42 **/
43 DATAHUB_STATUSCODE_RECORD *
44 AcquireRecordBuffer (
45 VOID
46 )
47 {
48 DATAHUB_STATUSCODE_RECORD *Record;
49
50 Record = (DATAHUB_STATUSCODE_RECORD *) AllocateZeroPool (sizeof (DATAHUB_STATUSCODE_RECORD));
51 if (NULL == Record) {
52 return NULL;
53 }
54 Record->Signature = DATAHUB_STATUS_CODE_SIGNATURE;
55
56 EfiAcquireLock (&mFifoLock);
57 InsertTailList (&mRecordsFifo, &Record->Node);
58 mNumberOfRecords++;
59 EfiReleaseLock (&mFifoLock);
60
61 return Record;
62 }
63
64
65 /**
66 Release a mRecordBuffer entry allocated by AcquirRecordBuffer ().
67
68 @param Record Point to record buffer which is acquired by AcquirRecordBuffer()
69
70 **/
71 VOID
72 FreeRecordBuffer (
73 IN DATAHUB_STATUSCODE_RECORD *Record
74 )
75 {
76 ASSERT (Record != NULL);
77 ASSERT (mNumberOfRecords != 0);
78
79 EfiAcquireLock (&mFifoLock);
80 RemoveEntryList (&Record->Node);
81 mNumberOfRecords--;
82 EfiReleaseLock (&mFifoLock);
83
84 FreePool (Record);
85 }
86
87
88 /**
89 Report status code into DataHub.
90
91 @param CodeType Indicates the type of status code being reported. Type EFI_STATUS_CODE_TYPE is defined in "Related Definitions¡± below.
92
93 @param Value Describes the current status of a hardware or software entity.
94 This included information about the class and subclass that is used to classify the entity
95 as well as an operation. For progress codes, the operation is the current activity.
96 For error codes, it is the exception. For debug codes, it is not defined at this time.
97 Type EFI_STATUS_CODE_VALUE is defined in ¡°Related Definitions¡± below.
98 Specific values are discussed in the Intel? Platform Innovation Framework for EFI Status Code Specification.
99
100 @param Instance The enumeration of a hardware or software entity within the system.
101 A system may contain multiple entities that match a class/subclass pairing.
102 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,
103 not meaningful, or not relevant. Valid instance numbers start with 1.
104
105
106 @param CallerId This optional parameter may be used to identify the caller.
107 This parameter allows the status code driver to apply different rules to different callers.
108 Type EFI_GUID is defined in InstallProtocolInterface() in the EFI 1.10 Specification.
109
110
111 @param Data This optional parameter may be used to pass additional data
112
113 @retval EFI_OUT_OF_RESOURCES Can not acquire record buffer.
114 @retval EFI_SUCCESS Success to cache status code and signal log data event.
115
116 **/
117 EFI_STATUS
118 DataHubStatusCodeReportWorker (
119 IN EFI_STATUS_CODE_TYPE CodeType,
120 IN EFI_STATUS_CODE_VALUE Value,
121 IN UINT32 Instance,
122 IN EFI_GUID *CallerId,
123 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
124 )
125 {
126 DATAHUB_STATUSCODE_RECORD *Record;
127 UINT32 ErrorLevel;
128 VA_LIST Marker;
129 CHAR8 *Format;
130 UINTN CharCount;
131
132 //
133 // See whether in runtime phase or not.
134 //
135 if (EfiAtRuntime ()) {
136 return EFI_SUCCESS;
137 }
138
139 Record = (DATAHUB_STATUSCODE_RECORD *) AcquireRecordBuffer ();
140 if (Record == NULL) {
141 //
142 // There are no empty record buffer in private buffers
143 //
144 return EFI_OUT_OF_RESOURCES;
145 }
146 //
147 // Construct Data Hub Extended Data
148 //
149 Record->CodeType = CodeType;
150 Record->Value = Value;
151 Record->Instance = Instance;
152
153 if (CallerId != NULL) {
154 CopyMem (&Record->CallerId, CallerId, sizeof (EFI_GUID));
155 }
156
157 if (Data != NULL) {
158 if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
159 CharCount = UnicodeVSPrintAsciiFormat (
160 (CHAR16 *) Record->ExtendData,
161 EFI_STATUS_CODE_DATA_MAX_SIZE,
162 Format,
163 Marker
164 );
165 //
166 // Change record data type from DebugType to String Type.
167 //
168 CopyGuid (&Record->Data.Type, &gEfiStatusCodeDataTypeStringGuid);
169 Record->Data.HeaderSize = Data->HeaderSize;
170 Record->Data.Size = (UINT16) ((CharCount + 1) * sizeof (CHAR16));
171 } else {
172 //
173 // Copy status code data header
174 //
175 CopyMem (&Record->Data, Data, sizeof (EFI_STATUS_CODE_DATA));
176
177 if (Data->Size > EFI_STATUS_CODE_DATA_MAX_SIZE) {
178 Record->Data.Size = EFI_STATUS_CODE_DATA_MAX_SIZE;
179 }
180 CopyMem (Record->ExtendData, Data + 1, Record->Data.Size);
181 }
182 }
183
184 gBS->SignalEvent (mLogDataHubEvent);
185
186 return EFI_SUCCESS;
187 }
188
189
190 /**
191 The Event handler which will be notified to log data in Data Hub.
192
193 @param Event Instance of the EFI_EVENT to signal whenever data is
194 available to be logged in the system.
195 @param Context Context of the event.
196
197 **/
198 VOID
199 EFIAPI
200 LogDataHubEventCallBack (
201 IN EFI_EVENT Event,
202 IN VOID *Context
203 )
204 {
205 DATAHUB_STATUSCODE_RECORD *Record;
206 UINT32 Size;
207 UINT64 DataRecordClass;
208 LIST_ENTRY *Node;
209
210 //
211 // Log DataRecord in Data Hub.
212 // Journal records fifo to find all record entry.
213 //
214 //
215 for (Node = mRecordsFifo.ForwardLink; Node != &mRecordsFifo;) {
216 Record = CR (Node, DATAHUB_STATUSCODE_RECORD, Node, DATAHUB_STATUS_CODE_SIGNATURE);
217 Node = Node->ForwardLink;
218
219 //
220 // Add in the size of the header we added.
221 //
222 Size = sizeof (DATAHUB_STATUSCODE_RECORD) + (UINT32) Record->Data.Size;
223
224 if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) {
225 DataRecordClass = EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
226 } else if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) {
227 DataRecordClass = EFI_DATA_RECORD_CLASS_ERROR;
228 } else if ((Record->CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {
229 DataRecordClass = EFI_DATA_RECORD_CLASS_DEBUG;
230 } else {
231 //
232 // Should never get here.
233 //
234 DataRecordClass = EFI_DATA_RECORD_CLASS_DEBUG |
235 EFI_DATA_RECORD_CLASS_ERROR |
236 EFI_DATA_RECORD_CLASS_DATA |
237 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
238 }
239
240 //
241 // Log DataRecord in Data Hub
242 //
243
244 mDataHubProtocol->LogData (
245 mDataHubProtocol,
246 &gEfiStatusCodeGuid,
247 &gEfiStatusCodeRuntimeProtocolGuid,
248 DataRecordClass,
249 Record,
250 Size
251 );
252
253 FreeRecordBuffer (Record);
254 }
255 }
256
257
258 /**
259 Initialize data hubstatus code.
260 Create a data hub listener.
261
262 @return The function always return EFI_SUCCESS
263
264 **/
265 EFI_STATUS
266 DataHubStatusCodeInitializeWorker (
267 VOID
268 )
269 {
270 EFI_STATUS Status;
271
272 Status = gBS->LocateProtocol (
273 &gEfiDataHubProtocolGuid,
274 NULL,
275 (VOID **) &mDataHubProtocol
276 );
277 ASSERT_EFI_ERROR (Status);
278
279 //
280 // Create a Notify Event to log data in Data Hub
281 //
282 Status = gBS->CreateEvent (
283 EFI_EVENT_NOTIFY_SIGNAL,
284 EFI_TPL_CALLBACK,
285 LogDataHubEventCallBack,
286 NULL,
287 &mLogDataHubEvent
288 );
289
290 ASSERT_EFI_ERROR (Status);
291
292 return EFI_SUCCESS;
293 }
294
295