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 } };
31 // Since this driver will only ever produce one instance of the Logging Hub
32 // protocol you are not required to dynamically allocate the PrivateData.
34 DATA_HUB_INSTANCE mPrivateData
;
37 // Worker functions private to this file
39 DATA_HUB_FILTER_DRIVER
*
40 FindFilterDriverByEvent (
45 EFI_DATA_RECORD_HEADER
*
48 IN UINT64 ClassFilter
,
49 IN OUT UINT64
*PtrCurrentMTC
54 Log data record into the data logging hub
56 @param This - Protocol instance structure
57 @param DataRecordGuid - GUID that defines record contents
58 @param ProducerName - GUID that defines the name of the producer of the data
59 @param DataRecordClass - Class that defines generic record type
60 @param RawData - Data Log record as defined by DataRecordGuid
61 @param RawDataSize - Size of Data Log data in bytes
63 @retval EFI_SUCCESS - If data was logged
64 @retval EFI_OUT_OF_RESOURCES - If data was not logged due to lack of system
70 IN EFI_DATA_HUB_PROTOCOL
*This
,
71 IN EFI_GUID
*DataRecordGuid
,
72 IN EFI_GUID
*ProducerName
,
73 IN UINT64 DataRecordClass
,
79 DATA_HUB_INSTANCE
*Private
;
80 EFI_DATA_ENTRY
*LogEntry
;
83 EFI_DATA_RECORD_HEADER
*Record
;
85 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
89 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
92 // Combine the storage for the internal structs and a copy of the log record.
93 // Record follows PrivateLogEntry. The consumer will be returned a pointer
94 // to Record so we don't what it to be the thing that was allocated from
95 // pool, so the consumer can't free an data record by mistake.
97 RecordSize
= sizeof (EFI_DATA_RECORD_HEADER
) + RawDataSize
;
98 TotalSize
= sizeof (EFI_DATA_ENTRY
) + RecordSize
;
101 // The Logging action is the critical section, so it is locked.
102 // The MTC asignment & update, time, and logging must be an
103 // atomic operation, so use the lock.
105 Status
= EfiAcquireLockOrFail (&Private
->DataLock
);
106 if (EFI_ERROR (Status
)) {
108 // Reentrancy detected so exit!
113 LogEntry
= AllocatePool (TotalSize
);
115 if (LogEntry
== NULL
) {
116 EfiReleaseLock (&Private
->DataLock
);
117 return EFI_OUT_OF_RESOURCES
;
120 ZeroMem (LogEntry
, TotalSize
);
122 Record
= (EFI_DATA_RECORD_HEADER
*) (LogEntry
+ 1);
123 Raw
= (VOID
*) (Record
+ 1);
126 // Build Standard Log Header
128 Record
->Version
= EFI_DATA_RECORD_HEADER_VERSION
;
129 Record
->HeaderSize
= sizeof (EFI_DATA_RECORD_HEADER
);
130 Record
->RecordSize
= RecordSize
;
131 CopyMem (&Record
->DataRecordGuid
, DataRecordGuid
, sizeof (EFI_GUID
));
132 CopyMem (&Record
->ProducerName
, ProducerName
, sizeof (EFI_GUID
));
133 Record
->DataRecordClass
= DataRecordClass
;
136 // Ensure LogMonotonicCount is not zero
138 Record
->LogMonotonicCount
= ++Private
->GlobalMonotonicCount
;
140 gRT
->GetTime (&Record
->LogTime
, NULL
);
143 // Insert log into the internal linked list.
145 LogEntry
->Signature
= EFI_DATA_ENTRY_SIGNATURE
;
146 LogEntry
->Record
= Record
;
147 LogEntry
->RecordSize
= sizeof (EFI_DATA_ENTRY
) + RawDataSize
;
148 InsertTailList (&Private
->DataListHead
, &LogEntry
->Link
);
150 CopyMem (Raw
, RawData
, RawDataSize
);
152 EfiReleaseLock (&Private
->DataLock
);
155 // Send Signal to all the filter drivers which are interested
156 // in the record's class and guid.
158 Head
= &Private
->FilterDriverListHead
;
159 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
160 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
161 if (((FilterEntry
->ClassFilter
& DataRecordClass
) != 0) &&
162 (CompareGuid (&FilterEntry
->FilterDataRecordGuid
, &gZeroGuid
) ||
163 CompareGuid (&FilterEntry
->FilterDataRecordGuid
, DataRecordGuid
))) {
164 gBS
->SignalEvent (FilterEntry
->Event
);
173 Get a previously logged data record and the MonotonicCount for the next
174 availible Record. This allows all records or all records later
175 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
176 is passed in with a MonotonicCout of zero return the first record
177 not yet read by the filter driver. If FilterDriverEvent is NULL and
178 MonotonicCount is zero return the first data record.
180 @param This The EFI_DATA_HUB_PROTOCOL instance.
181 @param MonotonicCount Specifies the Record to return. On input, zero means
182 return the first record. On output, contains the next
183 record to availible. Zero indicates no more records.
184 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
185 of zero, it means to return the first data record.
186 If FilterDriverEvent is passed in, then a MonotonicCount
187 of zero means to return the first data not yet read by
189 @param Record Returns a dynamically allocated memory buffer with a data
190 record that matches MonotonicCount.
192 @retval EFI_SUCCESS - Data was returned in Record.
193 @retval EFI_INVALID_PARAMETER - FilterDriverEvent was passed in but does not exist.
194 @retval EFI_NOT_FOUND - MonotonicCount does not match any data record in the
195 system. If a MonotonicCount of zero was passed in, then
196 no data records exist in the system.
197 @retval EFI_OUT_OF_RESOURCES - Record was not returned due to lack of system resources.
202 DataHubGetNextRecord (
203 IN EFI_DATA_HUB_PROTOCOL
*This
,
204 IN OUT UINT64
*MonotonicCount
,
205 IN EFI_EVENT
*FilterDriverEvent
, OPTIONAL
206 OUT EFI_DATA_RECORD_HEADER
**Record
209 DATA_HUB_INSTANCE
*Private
;
210 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
212 UINT64 FilterMonotonicCount
;
214 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
217 FilterMonotonicCount
= 0;
218 ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
219 EFI_DATA_RECORD_CLASS_ERROR
|
220 EFI_DATA_RECORD_CLASS_DATA
|
221 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
223 if (FilterDriverEvent
!= NULL
) {
225 // For events the beginning is the last unread record. This info is
226 // stored in the instance structure, so we must look up the event
229 FilterDriver
= FindFilterDriverByEvent (
230 &Private
->FilterDriverListHead
,
233 if (FilterDriver
== NULL
) {
234 return EFI_INVALID_PARAMETER
;
237 // Use the Class filter the event was created with.
239 ClassFilter
= FilterDriver
->ClassFilter
;
241 if (*MonotonicCount
== 0) {
243 // Use the MTC from the Filter Driver.
245 FilterMonotonicCount
= FilterDriver
->GetNextMonotonicCount
;
248 // The GetNextMonotonicCount field remembers the last value from the previous time.
249 // But we already processed this vaule, so we need to find the next one.
251 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, &FilterMonotonicCount
);
252 if (FilterMonotonicCount
!= 0) {
253 *MonotonicCount
= FilterMonotonicCount
;
256 if ((FilterDriver
->GetNextMonotonicCount
!= 0) && (FilterMonotonicCount
== 0)) {
258 // If there is no new record to get exit now.
261 return EFI_NOT_FOUND
;
268 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
269 if (*Record
== NULL
) {
270 return EFI_NOT_FOUND
;
273 if (FilterDriver
!= NULL
) {
275 // If we have a filter driver update the records that have been read.
276 // If MonotonicCount is zero No more reacords left.
278 if (*MonotonicCount
== 0) {
280 // Save the current Record MonotonicCount.
282 FilterDriver
->GetNextMonotonicCount
= (*Record
)->LogMonotonicCount
;
285 // Point to next undread record
287 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
295 This function registers the data hub filter driver that is represented
296 by FilterEvent. Only one instance of each FilterEvent can be registered.
297 After the FilterEvent is registered, it will be signaled so it can sync
298 with data records that have been recorded prior to the FilterEvent being
301 @param This - The EFI_DATA_HUB_PROTOCOL instance.
302 @param FilterEvent - The EFI_EVENT to signal whenever data that matches
303 FilterClass is logged in the system.
304 @param FilterTpl - The maximum EFI_TPL at which FilterEvent can be
305 signaled. It is strongly recommended that you use the
306 lowest EFI_TPL possible.
307 @param FilterClass - FilterEvent will be signaled whenever a bit in
308 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
309 FilterClass. If FilterClass is zero, no class-based
310 filtering will be performed.
311 @param FilterDataRecordGuid - FilterEvent will be signaled whenever FilterDataRecordGuid
312 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
313 FilterDataRecordGuid is NULL, then no GUID-based filtering
316 @retval EFI_SUCCESS - The filter driver event was registered.
317 @retval EFI_ALREADY_STARTED - FilterEvent was previously registered and cannot be
319 @retval EFI_OUT_OF_RESOURCES - The filter driver event was not registered due to lack of
325 DataHubRegisterFilterDriver (
326 IN EFI_DATA_HUB_PROTOCOL
* This
,
327 IN EFI_EVENT FilterEvent
,
328 IN EFI_TPL FilterTpl
,
329 IN UINT64 FilterClass
,
330 IN EFI_GUID
* FilterDataRecordGuid OPTIONAL
334 DATA_HUB_INSTANCE
*Private
;
335 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
337 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
339 FilterDriver
= (DATA_HUB_FILTER_DRIVER
*) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER
));
340 if (FilterDriver
== NULL
) {
341 return EFI_OUT_OF_RESOURCES
;
344 // Initialize filter driver info
346 FilterDriver
->Signature
= EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE
;
347 FilterDriver
->Event
= FilterEvent
;
348 FilterDriver
->Tpl
= FilterTpl
;
349 FilterDriver
->GetNextMonotonicCount
= 0;
350 if (FilterClass
== 0) {
351 FilterDriver
->ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
352 EFI_DATA_RECORD_CLASS_ERROR
|
353 EFI_DATA_RECORD_CLASS_DATA
|
354 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
356 FilterDriver
->ClassFilter
= FilterClass
;
359 if (FilterDataRecordGuid
!= NULL
) {
360 CopyMem (&FilterDriver
->FilterDataRecordGuid
, FilterDataRecordGuid
, sizeof (EFI_GUID
));
363 // Search for duplicate entries
365 if (FindFilterDriverByEvent (&Private
->FilterDriverListHead
, FilterEvent
) != NULL
) {
366 FreePool (FilterDriver
);
367 return EFI_ALREADY_STARTED
;
370 // Make insertion an atomic operation with the lock.
372 EfiAcquireLock (&Private
->DataLock
);
373 InsertTailList (&Private
->FilterDriverListHead
, &FilterDriver
->Link
);
374 EfiReleaseLock (&Private
->DataLock
);
377 // Signal the Filter driver we just loaded so they will recieve all the
378 // previous history. If we did not signal here we would have to wait until
379 // the next data was logged to get the history. In a case where no next
380 // data was logged we would never get synced up.
382 gBS
->SignalEvent (FilterEvent
);
388 Remove a Filter Driver, so it no longer gets called when data
389 information is logged.
391 @param This - Protocol instance structure
393 @param FilterEvent - Event that represents a filter driver that is to be
396 @retval EFI_SUCCESS - If FilterEvent was unregistered
398 @retval EFI_NOT_FOUND - If FilterEvent does not exist
403 DataHubUnregisterFilterDriver (
404 IN EFI_DATA_HUB_PROTOCOL
*This
,
405 IN EFI_EVENT FilterEvent
408 DATA_HUB_INSTANCE
*Private
;
409 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
411 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
414 // Search for duplicate entries
416 FilterDriver
= FindFilterDriverByEvent (
417 &Private
->FilterDriverListHead
,
420 if (FilterDriver
== NULL
) {
421 return EFI_NOT_FOUND
;
424 // Make removal an atomic operation with the lock
426 EfiAcquireLock (&Private
->DataLock
);
427 RemoveEntryList (&FilterDriver
->Link
);
428 EfiReleaseLock (&Private
->DataLock
);
434 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
435 represents Event and return it.
437 @param Head - Head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER
440 @param Event - Event to be search for in the Head list.
442 @retval EFI_DATA_HUB_FILTER_DRIVER - Returned if Event stored in the
443 Head doubly linked list.
445 @retval NULL - If Event is not in the list
448 DATA_HUB_FILTER_DRIVER
*
449 FindFilterDriverByEvent (
455 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
458 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
459 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
460 if (FilterEntry
->Event
== Event
) {
469 Search the Head doubly linked list for the passed in MTC. Return the
470 matching element in Head and the MTC on the next entry.
472 @param Head - Head of Data Log linked list.
474 @param ClassFilter - Only match the MTC if it is in the same Class as the
477 @param PtrCurrentMTC - On IN contians MTC to search for. On OUT contians next
478 MTC in the data log list or zero if at end of the list.
480 @retval EFI_DATA_LOG_ENTRY - Return pointer to data log data from Head list.
482 @retval NULL - If no data record exists.
485 EFI_DATA_RECORD_HEADER
*
488 IN UINT64 ClassFilter
,
489 IN OUT UINT64
*PtrCurrentMTC
493 EFI_DATA_ENTRY
*LogEntry
;
495 BOOLEAN ReturnFirstEntry
;
496 EFI_DATA_RECORD_HEADER
*Record
;
497 EFI_DATA_ENTRY
*NextLogEntry
;
500 // If MonotonicCount == 0 just return the first one
502 ReturnFirstEntry
= (BOOLEAN
) (*PtrCurrentMTC
== 0);
505 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
506 LogEntry
= DATA_ENTRY_FROM_LINK (Link
);
507 if ((LogEntry
->Record
->DataRecordClass
& ClassFilter
) == 0) {
509 // Skip any entry that does not have the correct ClassFilter
514 if ((LogEntry
->Record
->LogMonotonicCount
== *PtrCurrentMTC
) || ReturnFirstEntry
) {
516 // Return record to the user
518 Record
= LogEntry
->Record
;
521 // Calculate the next MTC value. If there is no next entry set
525 for (Link
= Link
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
526 NextLogEntry
= DATA_ENTRY_FROM_LINK (Link
);
527 if ((NextLogEntry
->Record
->DataRecordClass
& ClassFilter
) != 0) {
529 // Return the MTC of the next thing to search for if found
531 *PtrCurrentMTC
= NextLogEntry
->Record
->LogMonotonicCount
;
536 // Record found exit loop and return
547 Install Driver to produce Data Hub protocol.
549 @param ImageHandle Module's image handle
550 @param SystemTable Pointer of EFI_SYSTEM_TABLE
553 @retval EFI_SUCCESS - Logging Hub protocol installed
555 @retval Other - No protocol installed, unload driver.
561 IN EFI_HANDLE ImageHandle
,
562 IN EFI_SYSTEM_TABLE
*SystemTable
566 UINT32 HighMontonicCount
;
568 mPrivateData
.Signature
= DATA_HUB_INSTANCE_SIGNATURE
;
569 mPrivateData
.DataHub
.LogData
= DataHubLogData
;
570 mPrivateData
.DataHub
.GetNextRecord
= DataHubGetNextRecord
;
571 mPrivateData
.DataHub
.RegisterFilterDriver
= DataHubRegisterFilterDriver
;
572 mPrivateData
.DataHub
.UnregisterFilterDriver
= DataHubUnregisterFilterDriver
;
575 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
576 // required by this protocol
578 InitializeListHead (&mPrivateData
.DataListHead
);
579 InitializeListHead (&mPrivateData
.FilterDriverListHead
);
581 EfiInitializeLock (&mPrivateData
.DataLock
, TPL_NOTIFY
);
584 // Make sure we get a bigger MTC number on every boot!
586 Status
= gRT
->GetNextHighMonotonicCount (&HighMontonicCount
);
587 if (EFI_ERROR (Status
)) {
589 // if system service fails pick a sane value.
591 mPrivateData
.GlobalMonotonicCount
= 0;
593 mPrivateData
.GlobalMonotonicCount
= LShiftU64 ((UINT64
) HighMontonicCount
, 32);
596 // Make a new handle and install the protocol
598 mPrivateData
.Handle
= NULL
;
599 Status
= gBS
->InstallProtocolInterface (
600 &mPrivateData
.Handle
,
601 &gEfiDataHubProtocolGuid
,
602 EFI_NATIVE_INTERFACE
,
603 &mPrivateData
.DataHub