2 This code produces the Data Hub protocol. It preloads the data hub
3 with status information copied in from PEI HOBs.
5 Copyright (c) 2006 - 2015, 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
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.
19 // Since this driver will only ever produce one instance of the Logging Hub
20 // protocol you are not required to dynamically allocate the PrivateData.
22 DATA_HUB_INSTANCE mPrivateData
;
25 Log data record into the data logging hub
27 @param This Protocol instance structure
28 @param DataRecordGuid GUID that defines record contents
29 @param ProducerName GUID that defines the name of the producer of the data
30 @param DataRecordClass Class that defines generic record type
31 @param RawData Data Log record as defined by DataRecordGuid
32 @param RawDataSize Size of Data Log data in bytes
34 @retval EFI_SUCCESS If data was logged
35 @retval EFI_OUT_OF_RESOURCES If data was not logged due to lack of system
41 IN EFI_DATA_HUB_PROTOCOL
*This
,
42 IN EFI_GUID
*DataRecordGuid
,
43 IN EFI_GUID
*ProducerName
,
44 IN UINT64 DataRecordClass
,
50 DATA_HUB_INSTANCE
*Private
;
51 EFI_DATA_ENTRY
*LogEntry
;
54 EFI_DATA_RECORD_HEADER
*Record
;
56 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
61 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
64 // Combine the storage for the internal structs and a copy of the log record.
65 // Record follows PrivateLogEntry. The consumer will be returned a pointer
66 // to Record so we don't what it to be the thing that was allocated from
67 // pool, so the consumer can't free an data record by mistake.
69 RecordSize
= sizeof (EFI_DATA_RECORD_HEADER
) + RawDataSize
;
70 TotalSize
= sizeof (EFI_DATA_ENTRY
) + RecordSize
;
73 // First try to get log time at TPL level <= TPL_CALLBACK.
75 ZeroMem (&LogTime
, sizeof (LogTime
));
76 if (EfiGetCurrentTpl() <= TPL_CALLBACK
) {
77 gRT
->GetTime (&LogTime
, NULL
);
81 // The Logging action is the critical section, so it is locked.
82 // The MTC asignment & update and logging must be an
83 // atomic operation, so use the lock.
85 Status
= EfiAcquireLockOrFail (&Private
->DataLock
);
86 if (EFI_ERROR (Status
)) {
88 // Reentrancy detected so exit!
93 LogEntry
= AllocatePool (TotalSize
);
95 if (LogEntry
== NULL
) {
96 EfiReleaseLock (&Private
->DataLock
);
97 return EFI_OUT_OF_RESOURCES
;
100 ZeroMem (LogEntry
, TotalSize
);
102 Record
= (EFI_DATA_RECORD_HEADER
*) (LogEntry
+ 1);
103 Raw
= (VOID
*) (Record
+ 1);
106 // Build Standard Log Header
108 Record
->Version
= EFI_DATA_RECORD_HEADER_VERSION
;
109 Record
->HeaderSize
= (UINT16
) sizeof (EFI_DATA_RECORD_HEADER
);
110 Record
->RecordSize
= RecordSize
;
111 CopyMem (&Record
->DataRecordGuid
, DataRecordGuid
, sizeof (EFI_GUID
));
112 CopyMem (&Record
->ProducerName
, ProducerName
, sizeof (EFI_GUID
));
113 Record
->DataRecordClass
= DataRecordClass
;
116 // Ensure LogMonotonicCount is not zero
118 Record
->LogMonotonicCount
= ++Private
->GlobalMonotonicCount
;
120 CopyMem (&Record
->LogTime
, &LogTime
, sizeof (LogTime
));
123 // Insert log into the internal linked list.
125 LogEntry
->Signature
= EFI_DATA_ENTRY_SIGNATURE
;
126 LogEntry
->Record
= Record
;
127 LogEntry
->RecordSize
= sizeof (EFI_DATA_ENTRY
) + RawDataSize
;
128 InsertTailList (&Private
->DataListHead
, &LogEntry
->Link
);
130 CopyMem (Raw
, RawData
, RawDataSize
);
132 EfiReleaseLock (&Private
->DataLock
);
135 // Send Signal to all the filter drivers which are interested
136 // in the record's class and guid.
138 Head
= &Private
->FilterDriverListHead
;
139 for (Link
= GetFirstNode(Head
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
140 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
141 if (((FilterEntry
->ClassFilter
& DataRecordClass
) != 0) &&
142 (CompareGuid (&FilterEntry
->FilterDataRecordGuid
, &gZeroGuid
) ||
143 CompareGuid (&FilterEntry
->FilterDataRecordGuid
, DataRecordGuid
))) {
144 gBS
->SignalEvent (FilterEntry
->Event
);
152 Search the Head doubly linked list for the passed in MTC. Return the
153 matching element in Head and the MTC on the next entry.
155 @param Head Head of Data Log linked list.
156 @param ClassFilter Only match the MTC if it is in the same Class as the
158 @param PtrCurrentMTC On IN contians MTC to search for. On OUT contians next
159 MTC in the data log list or zero if at end of the list.
161 @retval EFI_DATA_LOG_ENTRY Return pointer to data log data from Head list.
162 @retval NULL If no data record exists.
165 EFI_DATA_RECORD_HEADER
*
168 IN UINT64 ClassFilter
,
169 IN OUT UINT64
*PtrCurrentMTC
173 EFI_DATA_ENTRY
*LogEntry
;
175 BOOLEAN ReturnFirstEntry
;
176 EFI_DATA_RECORD_HEADER
*Record
;
177 EFI_DATA_ENTRY
*NextLogEntry
;
180 // If MonotonicCount == 0 just return the first one
182 ReturnFirstEntry
= (BOOLEAN
) (*PtrCurrentMTC
== 0);
185 for (Link
= GetFirstNode(Head
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
186 LogEntry
= DATA_ENTRY_FROM_LINK (Link
);
187 if ((LogEntry
->Record
->DataRecordClass
& ClassFilter
) == 0) {
189 // Skip any entry that does not have the correct ClassFilter
194 if ((LogEntry
->Record
->LogMonotonicCount
== *PtrCurrentMTC
) || ReturnFirstEntry
) {
196 // Return record to the user
198 Record
= LogEntry
->Record
;
201 // Calculate the next MTC value. If there is no next entry set
205 for (Link
= GetNextNode(Head
, Link
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
206 NextLogEntry
= DATA_ENTRY_FROM_LINK (Link
);
207 if ((NextLogEntry
->Record
->DataRecordClass
& ClassFilter
) != 0) {
209 // Return the MTC of the next thing to search for if found
211 *PtrCurrentMTC
= NextLogEntry
->Record
->LogMonotonicCount
;
216 // Record found exit loop and return
226 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
227 represents Event and return it.
229 @param Head Pointer to head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER structures.
230 @param Event Event to be search for in the Head list.
232 @retval EFI_DATA_HUB_FILTER_DRIVER Returned if Event stored in the Head doubly linked list.
233 @retval NULL If Event is not in the list
236 DATA_HUB_FILTER_DRIVER
*
237 FindFilterDriverByEvent (
242 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
245 for (Link
= GetFirstNode(Head
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
246 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
247 if (FilterEntry
->Event
== Event
) {
257 Get a previously logged data record and the MonotonicCount for the next
258 availible Record. This allows all records or all records later
259 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
260 is passed in with a MonotonicCout of zero return the first record
261 not yet read by the filter driver. If FilterDriverEvent is NULL and
262 MonotonicCount is zero return the first data record.
264 @param This Pointer to the EFI_DATA_HUB_PROTOCOL instance.
265 @param MonotonicCount Specifies the Record to return. On input, zero means
266 return the first record. On output, contains the next
267 record to availible. Zero indicates no more records.
268 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
269 of zero, it means to return the first data record.
270 If FilterDriverEvent is passed in, then a MonotonicCount
271 of zero means to return the first data not yet read by
273 @param Record Returns a dynamically allocated memory buffer with a data
274 record that matches MonotonicCount.
276 @retval EFI_SUCCESS Data was returned in Record.
277 @retval EFI_INVALID_PARAMETER FilterDriverEvent was passed in but does not exist.
278 @retval EFI_NOT_FOUND MonotonicCount does not match any data record in the
279 system. If a MonotonicCount of zero was passed in, then
280 no data records exist in the system.
281 @retval EFI_OUT_OF_RESOURCES Record was not returned due to lack of system resources.
286 DataHubGetNextRecord (
287 IN EFI_DATA_HUB_PROTOCOL
*This
,
288 IN OUT UINT64
*MonotonicCount
,
289 IN EFI_EVENT
*FilterDriverEvent
, OPTIONAL
290 OUT EFI_DATA_RECORD_HEADER
**Record
293 DATA_HUB_INSTANCE
*Private
;
294 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
297 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
300 ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
301 EFI_DATA_RECORD_CLASS_ERROR
|
302 EFI_DATA_RECORD_CLASS_DATA
|
303 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
306 // If FilterDriverEvent is NULL, then return the next record
308 if (FilterDriverEvent
== NULL
) {
309 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
310 if (*Record
== NULL
) {
311 return EFI_NOT_FOUND
;
317 // For events the beginning is the last unread record. This info is
318 // stored in the instance structure, so we must look up the event
321 FilterDriver
= FindFilterDriverByEvent (
322 &Private
->FilterDriverListHead
,
325 if (FilterDriver
== NULL
) {
326 return EFI_INVALID_PARAMETER
;
329 // Use the Class filter the event was created with.
331 ClassFilter
= FilterDriver
->ClassFilter
;
334 // Retrieve the next record or the first record.
336 if (*MonotonicCount
!= 0 || FilterDriver
->GetNextMonotonicCount
== 0) {
337 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
338 if (*Record
== NULL
) {
339 return EFI_NOT_FOUND
;
342 if (*MonotonicCount
!= 0) {
344 // If this was not the last record then update the count associated with the filter
346 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
349 // Save the MonotonicCount of the last record which has been read
351 FilterDriver
->GetNextMonotonicCount
= (*Record
)->LogMonotonicCount
;
357 // This is a request to read the first record that has not been read yet.
358 // Set MonotoicCount to the last record successfuly read
360 *MonotonicCount
= FilterDriver
->GetNextMonotonicCount
;
363 // Retrieve the last record successfuly read again, but do not return it since
364 // it has already been returned before.
366 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
367 if (*Record
== NULL
) {
368 return EFI_NOT_FOUND
;
371 if (*MonotonicCount
!= 0) {
373 // Update the count associated with the filter
375 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
378 // Retrieve the record after the last record successfuly read
380 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
381 if (*Record
== NULL
) {
382 return EFI_NOT_FOUND
;
390 This function registers the data hub filter driver that is represented
391 by FilterEvent. Only one instance of each FilterEvent can be registered.
392 After the FilterEvent is registered, it will be signaled so it can sync
393 with data records that have been recorded prior to the FilterEvent being
396 @param This Pointer to The EFI_DATA_HUB_PROTOCOL instance.
397 @param FilterEvent The EFI_EVENT to signal whenever data that matches
398 FilterClass is logged in the system.
399 @param FilterTpl The maximum EFI_TPL at which FilterEvent can be
400 signaled. It is strongly recommended that you use the
401 lowest EFI_TPL possible.
402 @param FilterClass FilterEvent will be signaled whenever a bit in
403 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
404 FilterClass. If FilterClass is zero, no class-based
405 filtering will be performed.
406 @param FilterDataRecordGuid FilterEvent will be signaled whenever FilterDataRecordGuid
407 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
408 FilterDataRecordGuid is NULL, then no GUID-based filtering
411 @retval EFI_SUCCESS The filter driver event was registered.
412 @retval EFI_ALREADY_STARTED FilterEvent was previously registered and cannot be
414 @retval EFI_OUT_OF_RESOURCES The filter driver event was not registered due to lack of
420 DataHubRegisterFilterDriver (
421 IN EFI_DATA_HUB_PROTOCOL
* This
,
422 IN EFI_EVENT FilterEvent
,
423 IN EFI_TPL FilterTpl
,
424 IN UINT64 FilterClass
,
425 IN EFI_GUID
* FilterDataRecordGuid OPTIONAL
429 DATA_HUB_INSTANCE
*Private
;
430 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
432 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
434 FilterDriver
= (DATA_HUB_FILTER_DRIVER
*) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER
));
435 if (FilterDriver
== NULL
) {
436 return EFI_OUT_OF_RESOURCES
;
439 // Initialize filter driver info
441 FilterDriver
->Signature
= EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE
;
442 FilterDriver
->Event
= FilterEvent
;
443 FilterDriver
->Tpl
= FilterTpl
;
444 FilterDriver
->GetNextMonotonicCount
= 0;
445 if (FilterClass
== 0) {
446 FilterDriver
->ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
447 EFI_DATA_RECORD_CLASS_ERROR
|
448 EFI_DATA_RECORD_CLASS_DATA
|
449 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
451 FilterDriver
->ClassFilter
= FilterClass
;
454 if (FilterDataRecordGuid
!= NULL
) {
455 CopyMem (&FilterDriver
->FilterDataRecordGuid
, FilterDataRecordGuid
, sizeof (EFI_GUID
));
458 // Search for duplicate entries
460 if (FindFilterDriverByEvent (&Private
->FilterDriverListHead
, FilterEvent
) != NULL
) {
461 FreePool (FilterDriver
);
462 return EFI_ALREADY_STARTED
;
465 // Make insertion an atomic operation with the lock.
467 EfiAcquireLock (&Private
->DataLock
);
468 InsertTailList (&Private
->FilterDriverListHead
, &FilterDriver
->Link
);
469 EfiReleaseLock (&Private
->DataLock
);
472 // Signal the Filter driver we just loaded so they will recieve all the
473 // previous history. If we did not signal here we would have to wait until
474 // the next data was logged to get the history. In a case where no next
475 // data was logged we would never get synced up.
477 gBS
->SignalEvent (FilterEvent
);
483 Remove a Filter Driver, so it no longer gets called when data
484 information is logged.
486 @param This Protocol instance structure
488 @param FilterEvent Event that represents a filter driver that is to be
491 @retval EFI_SUCCESS If FilterEvent was unregistered
492 @retval EFI_NOT_FOUND If FilterEvent does not exist
496 DataHubUnregisterFilterDriver (
497 IN EFI_DATA_HUB_PROTOCOL
*This
,
498 IN EFI_EVENT FilterEvent
501 DATA_HUB_INSTANCE
*Private
;
502 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
504 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
507 // Search for duplicate entries
509 FilterDriver
= FindFilterDriverByEvent (
510 &Private
->FilterDriverListHead
,
513 if (FilterDriver
== NULL
) {
514 return EFI_NOT_FOUND
;
517 // Make removal an atomic operation with the lock
519 EfiAcquireLock (&Private
->DataLock
);
520 RemoveEntryList (&FilterDriver
->Link
);
521 EfiReleaseLock (&Private
->DataLock
);
529 Driver's Entry point routine that install Driver to produce Data Hub protocol.
531 @param ImageHandle Module's image handle
532 @param SystemTable Pointer of EFI_SYSTEM_TABLE
534 @retval EFI_SUCCESS Logging Hub protocol installed
535 @retval Other No protocol installed, unload driver.
541 IN EFI_HANDLE ImageHandle
,
542 IN EFI_SYSTEM_TABLE
*SystemTable
546 UINT32 HighMontonicCount
;
548 mPrivateData
.Signature
= DATA_HUB_INSTANCE_SIGNATURE
;
549 mPrivateData
.DataHub
.LogData
= DataHubLogData
;
550 mPrivateData
.DataHub
.GetNextRecord
= DataHubGetNextRecord
;
551 mPrivateData
.DataHub
.RegisterFilterDriver
= DataHubRegisterFilterDriver
;
552 mPrivateData
.DataHub
.UnregisterFilterDriver
= DataHubUnregisterFilterDriver
;
555 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
556 // required by this protocol
558 InitializeListHead (&mPrivateData
.DataListHead
);
559 InitializeListHead (&mPrivateData
.FilterDriverListHead
);
561 EfiInitializeLock (&mPrivateData
.DataLock
, TPL_NOTIFY
);
564 // Make sure we get a bigger MTC number on every boot!
566 Status
= gRT
->GetNextHighMonotonicCount (&HighMontonicCount
);
567 if (EFI_ERROR (Status
)) {
569 // if system service fails pick a sane value.
571 mPrivateData
.GlobalMonotonicCount
= 0;
573 mPrivateData
.GlobalMonotonicCount
= LShiftU64 ((UINT64
) HighMontonicCount
, 32);
576 // Make a new handle and install the protocol
578 mPrivateData
.Handle
= NULL
;
579 Status
= gBS
->InstallProtocolInterface (
580 &mPrivateData
.Handle
,
581 &gEfiDataHubProtocolGuid
,
582 EFI_NATIVE_INTERFACE
,
583 &mPrivateData
.DataHub