2 This code produces the Data Hub protocol. It preloads the data hub
3 with status information copied in from PEI HOBs.
5 Only code that implements the Data Hub protocol should go in this file!
7 The Term MTC stands for MonoTonicCounter.
9 For more information please look at DataHub.doc
11 NOTE: For extra security of the log GetNextDataRecord () could return a copy
14 Copyright (c) 2006, Intel Corporation
15 All rights reserved. This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 CONST EFI_GUID gZeroGuid
= { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
30 // Worker functions private to this file
33 DATA_HUB_FILTER_DRIVER
*
34 FindFilterDriverByEvent (
40 EFI_DATA_RECORD_HEADER
*
43 IN UINT64 ClassFilter
,
44 IN OUT UINT64
*PtrCurrentMTC
49 Log data record into the data logging hub
51 @param This - Protocol instance structure
52 @param DataRecordGuid - GUID that defines record contents
53 @param ProducerName - GUID that defines the name of the producer of the data
54 @param DataRecordClass - Class that defines generic record type
55 @param RawData - Data Log record as defined by DataRecordGuid
56 @param RawDataSize - Size of Data Log data in bytes
58 @retval EFI_SUCCESS - If data was logged
59 @retval EFI_OUT_OF_RESOURCES - If data was not logged due to lack of system
66 IN EFI_DATA_HUB_PROTOCOL
*This
,
67 IN EFI_GUID
*DataRecordGuid
,
68 IN EFI_GUID
*ProducerName
,
69 IN UINT64 DataRecordClass
,
75 DATA_HUB_INSTANCE
*Private
;
76 EFI_DATA_ENTRY
*LogEntry
;
79 EFI_DATA_RECORD_HEADER
*Record
;
81 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
85 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
88 // Combine the storage for the internal structs and a copy of the log record.
89 // Record follows PrivateLogEntry. The consumer will be returned a pointer
90 // to Record so we don't what it to be the thing that was allocated from
91 // pool, so the consumer can't free an data record by mistake.
93 RecordSize
= sizeof (EFI_DATA_RECORD_HEADER
) + RawDataSize
;
94 TotalSize
= sizeof (EFI_DATA_ENTRY
) + RecordSize
;
97 // The Logging action is the critical section, so it is locked.
98 // The MTC asignment & update, time, and logging must be an
99 // atomic operation, so use the lock.
101 Status
= EfiAcquireLockOrFail (&Private
->DataLock
);
102 if (EFI_ERROR (Status
)) {
104 // Reentrancy detected so exit!
109 LogEntry
= AllocatePool (TotalSize
);
111 if (LogEntry
== NULL
) {
112 EfiReleaseLock (&Private
->DataLock
);
113 return EFI_OUT_OF_RESOURCES
;
116 ZeroMem (LogEntry
, TotalSize
);
118 Record
= (EFI_DATA_RECORD_HEADER
*) (LogEntry
+ 1);
119 Raw
= (VOID
*) (Record
+ 1);
122 // Build Standard Log Header
124 Record
->Version
= EFI_DATA_RECORD_HEADER_VERSION
;
125 Record
->HeaderSize
= sizeof (EFI_DATA_RECORD_HEADER
);
126 Record
->RecordSize
= RecordSize
;
127 CopyMem (&Record
->DataRecordGuid
, DataRecordGuid
, sizeof (EFI_GUID
));
128 CopyMem (&Record
->ProducerName
, ProducerName
, sizeof (EFI_GUID
));
129 Record
->DataRecordClass
= DataRecordClass
;
132 // Ensure LogMonotonicCount is not zero
134 Record
->LogMonotonicCount
= ++Private
->GlobalMonotonicCount
;
136 gRT
->GetTime (&Record
->LogTime
, NULL
);
139 // Insert log into the internal linked list.
141 LogEntry
->Signature
= EFI_DATA_ENTRY_SIGNATURE
;
142 LogEntry
->Record
= Record
;
143 LogEntry
->RecordSize
= sizeof (EFI_DATA_ENTRY
) + RawDataSize
;
144 InsertTailList (&Private
->DataListHead
, &LogEntry
->Link
);
146 CopyMem (Raw
, RawData
, RawDataSize
);
148 EfiReleaseLock (&Private
->DataLock
);
151 // Send Signal to all the filter drivers which are interested
152 // in the record's class and guid.
154 Head
= &Private
->FilterDriverListHead
;
155 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
156 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
157 if (((FilterEntry
->ClassFilter
& DataRecordClass
) != 0) &&
158 (CompareGuid (&FilterEntry
->FilterDataRecordGuid
, &gZeroGuid
) ||
159 CompareGuid (&FilterEntry
->FilterDataRecordGuid
, DataRecordGuid
))) {
160 gBS
->SignalEvent (FilterEntry
->Event
);
169 Get a previously logged data record and the MonotonicCount for the next
170 availible Record. This allows all records or all records later
171 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
172 is passed in with a MonotonicCout of zero return the first record
173 not yet read by the filter driver. If FilterDriverEvent is NULL and
174 MonotonicCount is zero return the first data record.
176 @param This The EFI_DATA_HUB_PROTOCOL instance.
177 @param MonotonicCount Specifies the Record to return. On input, zero means
178 return the first record. On output, contains the next
179 record to availible. Zero indicates no more records.
180 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
181 of zero, it means to return the first data record.
182 If FilterDriverEvent is passed in, then a MonotonicCount
183 of zero means to return the first data not yet read by
185 @param Record Returns a dynamically allocated memory buffer with a data
186 record that matches MonotonicCount.
188 @retval EFI_SUCCESS - Data was returned in Record.
189 @retval EFI_INVALID_PARAMETER - FilterDriverEvent was passed in but does not exist.
190 @retval EFI_NOT_FOUND - MonotonicCount does not match any data record in the
191 system. If a MonotonicCount of zero was passed in, then
192 no data records exist in the system.
193 @retval EFI_OUT_OF_RESOURCES - Record was not returned due to lack of system resources.
199 DataHubGetNextRecord (
200 IN EFI_DATA_HUB_PROTOCOL
*This
,
201 IN OUT UINT64
*MonotonicCount
,
202 IN EFI_EVENT
*FilterDriverEvent
, OPTIONAL
203 OUT EFI_DATA_RECORD_HEADER
**Record
206 DATA_HUB_INSTANCE
*Private
;
207 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
209 UINT64 FilterMonotonicCount
;
211 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
214 FilterMonotonicCount
= 0;
215 ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
216 EFI_DATA_RECORD_CLASS_ERROR
|
217 EFI_DATA_RECORD_CLASS_DATA
|
218 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
220 if (FilterDriverEvent
!= NULL
) {
222 // For events the beginning is the last unread record. This info is
223 // stored in the instance structure, so we must look up the event
226 FilterDriver
= FindFilterDriverByEvent (
227 &Private
->FilterDriverListHead
,
230 if (FilterDriver
== NULL
) {
231 return EFI_INVALID_PARAMETER
;
234 // Use the Class filter the event was created with.
236 ClassFilter
= FilterDriver
->ClassFilter
;
238 if (*MonotonicCount
== 0) {
240 // Use the MTC from the Filter Driver.
242 FilterMonotonicCount
= FilterDriver
->GetNextMonotonicCount
;
243 if (FilterMonotonicCount
!= 0) {
245 // The GetNextMonotonicCount field remembers the last value from the previous time.
246 // But we already processed this vaule, so we need to find the next one.
248 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, &FilterMonotonicCount
);
249 *MonotonicCount
= FilterMonotonicCount
;
250 if (FilterMonotonicCount
== 0) {
252 // If there is no new record to get exit now.
254 return EFI_NOT_FOUND
;
262 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
263 if (*Record
== NULL
) {
264 return EFI_NOT_FOUND
;
267 if (FilterDriver
!= NULL
) {
269 // If we have a filter driver update the records that have been read.
270 // If MonotonicCount is zero No more reacords left.
272 if (*MonotonicCount
== 0) {
274 // Save the current Record MonotonicCount.
276 FilterDriver
->GetNextMonotonicCount
= (*Record
)->LogMonotonicCount
;
279 // Point to next undread record
281 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
289 This function registers the data hub filter driver that is represented
290 by FilterEvent. Only one instance of each FilterEvent can be registered.
291 After the FilterEvent is registered, it will be signaled so it can sync
292 with data records that have been recorded prior to the FilterEvent being
295 @param This - The EFI_DATA_HUB_PROTOCOL instance.
296 @param FilterEvent - The EFI_EVENT to signal whenever data that matches
297 FilterClass is logged in the system.
298 @param FilterTpl - The maximum EFI_TPL at which FilterEvent can be
299 signaled. It is strongly recommended that you use the
300 lowest EFI_TPL possible.
301 @param FilterClass - FilterEvent will be signaled whenever a bit in
302 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
303 FilterClass. If FilterClass is zero, no class-based
304 filtering will be performed.
305 @param FilterDataRecordGuid - FilterEvent will be signaled whenever FilterDataRecordGuid
306 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
307 FilterDataRecordGuid is NULL, then no GUID-based filtering
310 @retval EFI_SUCCESS - The filter driver event was registered.
311 @retval EFI_ALREADY_STARTED - FilterEvent was previously registered and cannot be
313 @retval EFI_OUT_OF_RESOURCES - The filter driver event was not registered due to lack of
320 DataHubRegisterFilterDriver (
321 IN EFI_DATA_HUB_PROTOCOL
* This
,
322 IN EFI_EVENT FilterEvent
,
323 IN EFI_TPL FilterTpl
,
324 IN UINT64 FilterClass
,
325 IN EFI_GUID
* FilterDataRecordGuid OPTIONAL
329 DATA_HUB_INSTANCE
*Private
;
330 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
332 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
334 FilterDriver
= (DATA_HUB_FILTER_DRIVER
*) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER
));
335 if (FilterDriver
== NULL
) {
336 return EFI_OUT_OF_RESOURCES
;
339 // Initialize filter driver info
341 FilterDriver
->Signature
= EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE
;
342 FilterDriver
->Event
= FilterEvent
;
343 FilterDriver
->Tpl
= FilterTpl
;
344 FilterDriver
->GetNextMonotonicCount
= 0;
345 if (FilterClass
== 0) {
346 FilterDriver
->ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
347 EFI_DATA_RECORD_CLASS_ERROR
|
348 EFI_DATA_RECORD_CLASS_DATA
|
349 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
351 FilterDriver
->ClassFilter
= FilterClass
;
354 if (FilterDataRecordGuid
!= NULL
) {
355 CopyMem (&FilterDriver
->FilterDataRecordGuid
, FilterDataRecordGuid
, sizeof (EFI_GUID
));
358 // Search for duplicate entries
360 if (FindFilterDriverByEvent (&Private
->FilterDriverListHead
, FilterEvent
) != NULL
) {
361 FreePool (FilterDriver
);
362 return EFI_ALREADY_STARTED
;
365 // Make insertion an atomic operation with the lock.
367 EfiAcquireLock (&Private
->DataLock
);
368 InsertTailList (&Private
->FilterDriverListHead
, &FilterDriver
->Link
);
369 EfiReleaseLock (&Private
->DataLock
);
372 // Signal the Filter driver we just loaded so they will recieve all the
373 // previous history. If we did not signal here we would have to wait until
374 // the next data was logged to get the history. In a case where no next
375 // data was logged we would never get synced up.
377 gBS
->SignalEvent (FilterEvent
);
383 Remove a Filter Driver, so it no longer gets called when data
384 information is logged.
386 @param This - Protocol instance structure
388 @param FilterEvent - Event that represents a filter driver that is to be
391 @retval EFI_SUCCESS - If FilterEvent was unregistered
393 @retval EFI_NOT_FOUND - If FilterEvent does not exist
399 DataHubUnregisterFilterDriver (
400 IN EFI_DATA_HUB_PROTOCOL
*This
,
401 IN EFI_EVENT FilterEvent
404 DATA_HUB_INSTANCE
*Private
;
405 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
407 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
410 // Search for duplicate entries
412 FilterDriver
= FindFilterDriverByEvent (
413 &Private
->FilterDriverListHead
,
416 if (FilterDriver
== NULL
) {
417 return EFI_NOT_FOUND
;
420 // Make removal an atomic operation with the lock
422 EfiAcquireLock (&Private
->DataLock
);
423 RemoveEntryList (&FilterDriver
->Link
);
424 EfiReleaseLock (&Private
->DataLock
);
430 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
431 represents Event and return it.
433 @param Head - Head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER
436 @param Event - Event to be search for in the Head list.
438 @retval EFI_DATA_HUB_FILTER_DRIVER - Returned if Event stored in the
439 Head doubly linked list.
441 @retval NULL - If Event is not in the list
445 DATA_HUB_FILTER_DRIVER
*
446 FindFilterDriverByEvent (
452 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
455 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
456 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
457 if (FilterEntry
->Event
== Event
) {
466 Search the Head doubly linked list for the passed in MTC. Return the
467 matching element in Head and the MTC on the next entry.
469 @param Head - Head of Data Log linked list.
471 @param ClassFilter - Only match the MTC if it is in the same Class as the
474 @param PtrCurrentMTC - On IN contians MTC to search for. On OUT contians next
475 MTC in the data log list or zero if at end of the list.
477 @retval EFI_DATA_LOG_ENTRY - Return pointer to data log data from Head list.
479 @retval NULL - If no data record exists.
483 EFI_DATA_RECORD_HEADER
*
486 IN UINT64 ClassFilter
,
487 IN OUT UINT64
*PtrCurrentMTC
491 EFI_DATA_ENTRY
*LogEntry
;
493 BOOLEAN ReturnFirstEntry
;
494 EFI_DATA_RECORD_HEADER
*Record
;
495 EFI_DATA_ENTRY
*NextLogEntry
;
498 // If MonotonicCount == 0 just return the first one
500 ReturnFirstEntry
= (BOOLEAN
) (*PtrCurrentMTC
== 0);
503 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
504 LogEntry
= DATA_ENTRY_FROM_LINK (Link
);
505 if ((LogEntry
->Record
->DataRecordClass
& ClassFilter
) == 0) {
507 // Skip any entry that does not have the correct ClassFilter
512 if ((LogEntry
->Record
->LogMonotonicCount
== *PtrCurrentMTC
) || ReturnFirstEntry
) {
514 // Return record to the user
516 Record
= LogEntry
->Record
;
519 // Calculate the next MTC value. If there is no next entry set
523 for (Link
= Link
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
524 NextLogEntry
= DATA_ENTRY_FROM_LINK (Link
);
525 if ((NextLogEntry
->Record
->DataRecordClass
& ClassFilter
) != 0) {
527 // Return the MTC of the next thing to search for if found
529 *PtrCurrentMTC
= NextLogEntry
->Record
->LogMonotonicCount
;
534 // Record found exit loop and return
544 // Since this driver will only ever produce one instance of the Logging Hub
545 // protocol you are not required to dynamically allocate the PrivateData.
547 DATA_HUB_INSTANCE mPrivateData
;
551 Install Driver to produce Data Hub protocol.
553 @param ImageHandle Module's image handle
554 @param SystemTable Pointer of EFI_SYSTEM_TABLE
557 @retval EFI_SUCCESS - Logging Hub protocol installed
559 @retval Other - No protocol installed, unload driver.
565 IN EFI_HANDLE ImageHandle
,
566 IN EFI_SYSTEM_TABLE
*SystemTable
570 UINT32 HighMontonicCount
;
572 mPrivateData
.Signature
= DATA_HUB_INSTANCE_SIGNATURE
;
573 mPrivateData
.DataHub
.LogData
= DataHubLogData
;
574 mPrivateData
.DataHub
.GetNextRecord
= DataHubGetNextRecord
;
575 mPrivateData
.DataHub
.RegisterFilterDriver
= DataHubRegisterFilterDriver
;
576 mPrivateData
.DataHub
.UnregisterFilterDriver
= DataHubUnregisterFilterDriver
;
579 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
580 // required by this protocol
582 InitializeListHead (&mPrivateData
.DataListHead
);
583 InitializeListHead (&mPrivateData
.FilterDriverListHead
);
585 EfiInitializeLock (&mPrivateData
.DataLock
, TPL_NOTIFY
);
588 // Make sure we get a bigger MTC number on every boot!
590 Status
= gRT
->GetNextHighMonotonicCount (&HighMontonicCount
);
591 if (EFI_ERROR (Status
)) {
593 // if system service fails pick a sane value.
595 mPrivateData
.GlobalMonotonicCount
= 0;
597 mPrivateData
.GlobalMonotonicCount
= LShiftU64 ((UINT64
) HighMontonicCount
, 32);
600 // Make a new handle and install the protocol
602 mPrivateData
.Handle
= NULL
;
603 Status
= gBS
->InstallProtocolInterface (
604 &mPrivateData
.Handle
,
605 &gEfiDataHubProtocolGuid
,
606 EFI_NATIVE_INTERFACE
,
607 &mPrivateData
.DataHub