]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/DataHubDxe/DataHub.c
Fix bug in DataHub where it would skip the first record in a set when a filter is...
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / DataHubDxe / DataHub.c
1 /** @file
2 This code produces the Data Hub protocol. It preloads the data hub
3 with status information copied in from PEI HOBs.
4
5 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "DataHub.h"
17
18 CONST EFI_GUID gZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
19
20 //
21 // Since this driver will only ever produce one instance of the Logging Hub
22 // protocol you are not required to dynamically allocate the PrivateData.
23 //
24 DATA_HUB_INSTANCE mPrivateData;
25
26 /**
27 Log data record into the data logging hub
28
29 @param This Protocol instance structure
30 @param DataRecordGuid GUID that defines record contents
31 @param ProducerName GUID that defines the name of the producer of the data
32 @param DataRecordClass Class that defines generic record type
33 @param RawData Data Log record as defined by DataRecordGuid
34 @param RawDataSize Size of Data Log data in bytes
35
36 @retval EFI_SUCCESS If data was logged
37 @retval EFI_OUT_OF_RESOURCES If data was not logged due to lack of system
38 resources.
39 **/
40 EFI_STATUS
41 EFIAPI
42 DataHubLogData (
43 IN EFI_DATA_HUB_PROTOCOL *This,
44 IN EFI_GUID *DataRecordGuid,
45 IN EFI_GUID *ProducerName,
46 IN UINT64 DataRecordClass,
47 IN VOID *RawData,
48 IN UINT32 RawDataSize
49 )
50 {
51 EFI_STATUS Status;
52 DATA_HUB_INSTANCE *Private;
53 EFI_DATA_ENTRY *LogEntry;
54 UINT32 TotalSize;
55 UINT32 RecordSize;
56 EFI_DATA_RECORD_HEADER *Record;
57 VOID *Raw;
58 DATA_HUB_FILTER_DRIVER *FilterEntry;
59 LIST_ENTRY *Link;
60 LIST_ENTRY *Head;
61
62 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
63
64 //
65 // Combine the storage for the internal structs and a copy of the log record.
66 // Record follows PrivateLogEntry. The consumer will be returned a pointer
67 // to Record so we don't what it to be the thing that was allocated from
68 // pool, so the consumer can't free an data record by mistake.
69 //
70 RecordSize = sizeof (EFI_DATA_RECORD_HEADER) + RawDataSize;
71 TotalSize = sizeof (EFI_DATA_ENTRY) + RecordSize;
72
73 //
74 // The Logging action is the critical section, so it is locked.
75 // The MTC asignment & update, time, and logging must be an
76 // atomic operation, so use the lock.
77 //
78 Status = EfiAcquireLockOrFail (&Private->DataLock);
79 if (EFI_ERROR (Status)) {
80 //
81 // Reentrancy detected so exit!
82 //
83 return Status;
84 }
85
86 LogEntry = AllocatePool (TotalSize);
87
88 if (LogEntry == NULL) {
89 EfiReleaseLock (&Private->DataLock);
90 return EFI_OUT_OF_RESOURCES;
91 }
92
93 ZeroMem (LogEntry, TotalSize);
94
95 Record = (EFI_DATA_RECORD_HEADER *) (LogEntry + 1);
96 Raw = (VOID *) (Record + 1);
97
98 //
99 // Build Standard Log Header
100 //
101 Record->Version = EFI_DATA_RECORD_HEADER_VERSION;
102 Record->HeaderSize = (UINT16) sizeof (EFI_DATA_RECORD_HEADER);
103 Record->RecordSize = RecordSize;
104 CopyMem (&Record->DataRecordGuid, DataRecordGuid, sizeof (EFI_GUID));
105 CopyMem (&Record->ProducerName, ProducerName, sizeof (EFI_GUID));
106 Record->DataRecordClass = DataRecordClass;
107
108 //
109 // Ensure LogMonotonicCount is not zero
110 //
111 Record->LogMonotonicCount = ++Private->GlobalMonotonicCount;
112
113 gRT->GetTime (&Record->LogTime, NULL);
114
115 //
116 // Insert log into the internal linked list.
117 //
118 LogEntry->Signature = EFI_DATA_ENTRY_SIGNATURE;
119 LogEntry->Record = Record;
120 LogEntry->RecordSize = sizeof (EFI_DATA_ENTRY) + RawDataSize;
121 InsertTailList (&Private->DataListHead, &LogEntry->Link);
122
123 CopyMem (Raw, RawData, RawDataSize);
124
125 EfiReleaseLock (&Private->DataLock);
126
127 //
128 // Send Signal to all the filter drivers which are interested
129 // in the record's class and guid.
130 //
131 Head = &Private->FilterDriverListHead;
132 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
133 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
134 if (((FilterEntry->ClassFilter & DataRecordClass) != 0) &&
135 (CompareGuid (&FilterEntry->FilterDataRecordGuid, &gZeroGuid) ||
136 CompareGuid (&FilterEntry->FilterDataRecordGuid, DataRecordGuid))) {
137 gBS->SignalEvent (FilterEntry->Event);
138 }
139 }
140
141 return EFI_SUCCESS;
142 }
143
144 /**
145 Search the Head doubly linked list for the passed in MTC. Return the
146 matching element in Head and the MTC on the next entry.
147
148 @param Head Head of Data Log linked list.
149 @param ClassFilter Only match the MTC if it is in the same Class as the
150 ClassFilter.
151 @param PtrCurrentMTC On IN contians MTC to search for. On OUT contians next
152 MTC in the data log list or zero if at end of the list.
153
154 @retval EFI_DATA_LOG_ENTRY Return pointer to data log data from Head list.
155 @retval NULL If no data record exists.
156
157 **/
158 EFI_DATA_RECORD_HEADER *
159 GetNextDataRecord (
160 IN LIST_ENTRY *Head,
161 IN UINT64 ClassFilter,
162 IN OUT UINT64 *PtrCurrentMTC
163 )
164
165 {
166 EFI_DATA_ENTRY *LogEntry;
167 LIST_ENTRY *Link;
168 BOOLEAN ReturnFirstEntry;
169 EFI_DATA_RECORD_HEADER *Record;
170 EFI_DATA_ENTRY *NextLogEntry;
171
172 //
173 // If MonotonicCount == 0 just return the first one
174 //
175 ReturnFirstEntry = (BOOLEAN) (*PtrCurrentMTC == 0);
176
177 Record = NULL;
178 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
179 LogEntry = DATA_ENTRY_FROM_LINK (Link);
180 if ((LogEntry->Record->DataRecordClass & ClassFilter) == 0) {
181 //
182 // Skip any entry that does not have the correct ClassFilter
183 //
184 continue;
185 }
186
187 if ((LogEntry->Record->LogMonotonicCount == *PtrCurrentMTC) || ReturnFirstEntry) {
188 //
189 // Return record to the user
190 //
191 Record = LogEntry->Record;
192
193 //
194 // Calculate the next MTC value. If there is no next entry set
195 // MTC to zero.
196 //
197 *PtrCurrentMTC = 0;
198 for (Link = GetNextNode(Head, Link); Link != Head; Link = GetNextNode(Head, Link)) {
199 NextLogEntry = DATA_ENTRY_FROM_LINK (Link);
200 if ((NextLogEntry->Record->DataRecordClass & ClassFilter) != 0) {
201 //
202 // Return the MTC of the next thing to search for if found
203 //
204 *PtrCurrentMTC = NextLogEntry->Record->LogMonotonicCount;
205 break;
206 }
207 }
208 //
209 // Record found exit loop and return
210 //
211 break;
212 }
213 }
214
215 return Record;
216 }
217
218 /**
219 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
220 represents Event and return it.
221
222 @param Head Pointer to head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER structures.
223 @param Event Event to be search for in the Head list.
224
225 @retval EFI_DATA_HUB_FILTER_DRIVER Returned if Event stored in the Head doubly linked list.
226 @retval NULL If Event is not in the list
227
228 **/
229 DATA_HUB_FILTER_DRIVER *
230 FindFilterDriverByEvent (
231 IN LIST_ENTRY *Head,
232 IN EFI_EVENT Event
233 )
234 {
235 DATA_HUB_FILTER_DRIVER *FilterEntry;
236 LIST_ENTRY *Link;
237
238 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
239 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
240 if (FilterEntry->Event == Event) {
241 return FilterEntry;
242 }
243 }
244
245 return NULL;
246 }
247
248 /**
249
250 Get a previously logged data record and the MonotonicCount for the next
251 availible Record. This allows all records or all records later
252 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
253 is passed in with a MonotonicCout of zero return the first record
254 not yet read by the filter driver. If FilterDriverEvent is NULL and
255 MonotonicCount is zero return the first data record.
256
257 @param This Pointer to the EFI_DATA_HUB_PROTOCOL instance.
258 @param MonotonicCount Specifies the Record to return. On input, zero means
259 return the first record. On output, contains the next
260 record to availible. Zero indicates no more records.
261 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
262 of zero, it means to return the first data record.
263 If FilterDriverEvent is passed in, then a MonotonicCount
264 of zero means to return the first data not yet read by
265 FilterDriverEvent.
266 @param Record Returns a dynamically allocated memory buffer with a data
267 record that matches MonotonicCount.
268
269 @retval EFI_SUCCESS Data was returned in Record.
270 @retval EFI_INVALID_PARAMETER FilterDriverEvent was passed in but does not exist.
271 @retval EFI_NOT_FOUND MonotonicCount does not match any data record in the
272 system. If a MonotonicCount of zero was passed in, then
273 no data records exist in the system.
274 @retval EFI_OUT_OF_RESOURCES Record was not returned due to lack of system resources.
275
276 **/
277 EFI_STATUS
278 EFIAPI
279 DataHubGetNextRecord (
280 IN EFI_DATA_HUB_PROTOCOL *This,
281 IN OUT UINT64 *MonotonicCount,
282 IN EFI_EVENT *FilterDriverEvent, OPTIONAL
283 OUT EFI_DATA_RECORD_HEADER **Record
284 )
285 {
286 DATA_HUB_INSTANCE *Private;
287 DATA_HUB_FILTER_DRIVER *FilterDriver;
288 UINT64 ClassFilter;
289
290 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
291
292 FilterDriver = NULL;
293 ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
294 EFI_DATA_RECORD_CLASS_ERROR |
295 EFI_DATA_RECORD_CLASS_DATA |
296 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
297
298 //
299 // If FilterDriverEvent is NULL, then return the next record
300 //
301 if (FilterDriverEvent == NULL) {
302 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
303 if (*Record == NULL) {
304 return EFI_NOT_FOUND;
305 }
306 return EFI_SUCCESS;
307 }
308
309 //
310 // For events the beginning is the last unread record. This info is
311 // stored in the instance structure, so we must look up the event
312 // to get the data.
313 //
314 FilterDriver = FindFilterDriverByEvent (
315 &Private->FilterDriverListHead,
316 *FilterDriverEvent
317 );
318 if (FilterDriver == NULL) {
319 return EFI_INVALID_PARAMETER;
320 }
321 //
322 // Use the Class filter the event was created with.
323 //
324 ClassFilter = FilterDriver->ClassFilter;
325
326 //
327 // Retrieve the next record or the first record.
328 //
329 if (*MonotonicCount != 0 || FilterDriver->GetNextMonotonicCount == 0) {
330 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
331 if (*Record == NULL) {
332 return EFI_NOT_FOUND;
333 }
334
335 if (*MonotonicCount != 0) {
336 //
337 // If this was not the last record then update the count associated with the filter
338 //
339 FilterDriver->GetNextMonotonicCount = *MonotonicCount;
340 } else {
341 //
342 // Save the MonotonicCount of the last record which has been read
343 //
344 FilterDriver->GetNextMonotonicCount = (*Record)->LogMonotonicCount;
345 }
346 return EFI_SUCCESS;
347 }
348
349 //
350 // This is a request to read the first record that has not been read yet.
351 // Set MonotoicCount to the last record successfuly read
352 //
353 *MonotonicCount = FilterDriver->GetNextMonotonicCount;
354
355 //
356 // Retrieve the last record successfuly read again, but do not return it since
357 // it has already been returned before.
358 //
359 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
360 if (*Record == NULL) {
361 return EFI_NOT_FOUND;
362 }
363
364 if (*MonotonicCount != 0) {
365 //
366 // Update the count associated with the filter
367 //
368 FilterDriver->GetNextMonotonicCount = *MonotonicCount;
369
370 //
371 // Retrieve the record after the last record successfuly read
372 //
373 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
374 if (*Record == NULL) {
375 return EFI_NOT_FOUND;
376 }
377 }
378
379 return EFI_SUCCESS;
380 }
381
382 /**
383 This function registers the data hub filter driver that is represented
384 by FilterEvent. Only one instance of each FilterEvent can be registered.
385 After the FilterEvent is registered, it will be signaled so it can sync
386 with data records that have been recorded prior to the FilterEvent being
387 registered.
388
389 @param This Pointer to The EFI_DATA_HUB_PROTOCOL instance.
390 @param FilterEvent The EFI_EVENT to signal whenever data that matches
391 FilterClass is logged in the system.
392 @param FilterTpl The maximum EFI_TPL at which FilterEvent can be
393 signaled. It is strongly recommended that you use the
394 lowest EFI_TPL possible.
395 @param FilterClass FilterEvent will be signaled whenever a bit in
396 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
397 FilterClass. If FilterClass is zero, no class-based
398 filtering will be performed.
399 @param FilterDataRecordGuid FilterEvent will be signaled whenever FilterDataRecordGuid
400 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
401 FilterDataRecordGuid is NULL, then no GUID-based filtering
402 will be performed.
403
404 @retval EFI_SUCCESS The filter driver event was registered.
405 @retval EFI_ALREADY_STARTED FilterEvent was previously registered and cannot be
406 registered again.
407 @retval EFI_OUT_OF_RESOURCES The filter driver event was not registered due to lack of
408 system resources.
409
410 **/
411 EFI_STATUS
412 EFIAPI
413 DataHubRegisterFilterDriver (
414 IN EFI_DATA_HUB_PROTOCOL * This,
415 IN EFI_EVENT FilterEvent,
416 IN EFI_TPL FilterTpl,
417 IN UINT64 FilterClass,
418 IN EFI_GUID * FilterDataRecordGuid OPTIONAL
419 )
420
421 {
422 DATA_HUB_INSTANCE *Private;
423 DATA_HUB_FILTER_DRIVER *FilterDriver;
424
425 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
426
427 FilterDriver = (DATA_HUB_FILTER_DRIVER *) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER));
428 if (FilterDriver == NULL) {
429 return EFI_OUT_OF_RESOURCES;
430 }
431 //
432 // Initialize filter driver info
433 //
434 FilterDriver->Signature = EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE;
435 FilterDriver->Event = FilterEvent;
436 FilterDriver->Tpl = FilterTpl;
437 FilterDriver->GetNextMonotonicCount = 0;
438 if (FilterClass == 0) {
439 FilterDriver->ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
440 EFI_DATA_RECORD_CLASS_ERROR |
441 EFI_DATA_RECORD_CLASS_DATA |
442 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
443 } else {
444 FilterDriver->ClassFilter = FilterClass;
445 }
446
447 if (FilterDataRecordGuid != NULL) {
448 CopyMem (&FilterDriver->FilterDataRecordGuid, FilterDataRecordGuid, sizeof (EFI_GUID));
449 }
450 //
451 // Search for duplicate entries
452 //
453 if (FindFilterDriverByEvent (&Private->FilterDriverListHead, FilterEvent) != NULL) {
454 FreePool (FilterDriver);
455 return EFI_ALREADY_STARTED;
456 }
457 //
458 // Make insertion an atomic operation with the lock.
459 //
460 EfiAcquireLock (&Private->DataLock);
461 InsertTailList (&Private->FilterDriverListHead, &FilterDriver->Link);
462 EfiReleaseLock (&Private->DataLock);
463
464 //
465 // Signal the Filter driver we just loaded so they will recieve all the
466 // previous history. If we did not signal here we would have to wait until
467 // the next data was logged to get the history. In a case where no next
468 // data was logged we would never get synced up.
469 //
470 gBS->SignalEvent (FilterEvent);
471
472 return EFI_SUCCESS;
473 }
474
475 /**
476 Remove a Filter Driver, so it no longer gets called when data
477 information is logged.
478
479 @param This Protocol instance structure
480
481 @param FilterEvent Event that represents a filter driver that is to be
482 Unregistered.
483
484 @retval EFI_SUCCESS If FilterEvent was unregistered
485 @retval EFI_NOT_FOUND If FilterEvent does not exist
486 **/
487 EFI_STATUS
488 EFIAPI
489 DataHubUnregisterFilterDriver (
490 IN EFI_DATA_HUB_PROTOCOL *This,
491 IN EFI_EVENT FilterEvent
492 )
493 {
494 DATA_HUB_INSTANCE *Private;
495 DATA_HUB_FILTER_DRIVER *FilterDriver;
496
497 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
498
499 //
500 // Search for duplicate entries
501 //
502 FilterDriver = FindFilterDriverByEvent (
503 &Private->FilterDriverListHead,
504 FilterEvent
505 );
506 if (FilterDriver == NULL) {
507 return EFI_NOT_FOUND;
508 }
509 //
510 // Make removal an atomic operation with the lock
511 //
512 EfiAcquireLock (&Private->DataLock);
513 RemoveEntryList (&FilterDriver->Link);
514 EfiReleaseLock (&Private->DataLock);
515
516 return EFI_SUCCESS;
517 }
518
519
520
521 /**
522 Driver's Entry point routine that install Driver to produce Data Hub protocol.
523
524 @param ImageHandle Module's image handle
525 @param SystemTable Pointer of EFI_SYSTEM_TABLE
526
527 @retval EFI_SUCCESS Logging Hub protocol installed
528 @retval Other No protocol installed, unload driver.
529
530 **/
531 EFI_STATUS
532 EFIAPI
533 DataHubInstall (
534 IN EFI_HANDLE ImageHandle,
535 IN EFI_SYSTEM_TABLE *SystemTable
536 )
537 {
538 EFI_STATUS Status;
539 UINT32 HighMontonicCount;
540
541 mPrivateData.Signature = DATA_HUB_INSTANCE_SIGNATURE;
542 mPrivateData.DataHub.LogData = DataHubLogData;
543 mPrivateData.DataHub.GetNextRecord = DataHubGetNextRecord;
544 mPrivateData.DataHub.RegisterFilterDriver = DataHubRegisterFilterDriver;
545 mPrivateData.DataHub.UnregisterFilterDriver = DataHubUnregisterFilterDriver;
546
547 //
548 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
549 // required by this protocol
550 //
551 InitializeListHead (&mPrivateData.DataListHead);
552 InitializeListHead (&mPrivateData.FilterDriverListHead);
553
554 EfiInitializeLock (&mPrivateData.DataLock, TPL_NOTIFY);
555
556 //
557 // Make sure we get a bigger MTC number on every boot!
558 //
559 Status = gRT->GetNextHighMonotonicCount (&HighMontonicCount);
560 if (EFI_ERROR (Status)) {
561 //
562 // if system service fails pick a sane value.
563 //
564 mPrivateData.GlobalMonotonicCount = 0;
565 } else {
566 mPrivateData.GlobalMonotonicCount = LShiftU64 ((UINT64) HighMontonicCount, 32);
567 }
568 //
569 // Make a new handle and install the protocol
570 //
571 mPrivateData.Handle = NULL;
572 Status = gBS->InstallProtocolInterface (
573 &mPrivateData.Handle,
574 &gEfiDataHubProtocolGuid,
575 EFI_NATIVE_INTERFACE,
576 &mPrivateData.DataHub
577 );
578 return Status;
579 }
580