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
;
246 if (FilterMonotonicCount
!= 0) {
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 *MonotonicCount
= FilterMonotonicCount
;
253 if (FilterMonotonicCount
== 0) {
255 // If there is no new record to get exit now.
257 return EFI_NOT_FOUND
;
265 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
266 if (*Record
== NULL
) {
267 return EFI_NOT_FOUND
;
270 if (FilterDriver
!= NULL
) {
272 // If we have a filter driver update the records that have been read.
273 // If MonotonicCount is zero No more reacords left.
275 if (*MonotonicCount
== 0) {
277 // Save the current Record MonotonicCount.
279 FilterDriver
->GetNextMonotonicCount
= (*Record
)->LogMonotonicCount
;
282 // Point to next undread record
284 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
292 This function registers the data hub filter driver that is represented
293 by FilterEvent. Only one instance of each FilterEvent can be registered.
294 After the FilterEvent is registered, it will be signaled so it can sync
295 with data records that have been recorded prior to the FilterEvent being
298 @param This - The EFI_DATA_HUB_PROTOCOL instance.
299 @param FilterEvent - The EFI_EVENT to signal whenever data that matches
300 FilterClass is logged in the system.
301 @param FilterTpl - The maximum EFI_TPL at which FilterEvent can be
302 signaled. It is strongly recommended that you use the
303 lowest EFI_TPL possible.
304 @param FilterClass - FilterEvent will be signaled whenever a bit in
305 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
306 FilterClass. If FilterClass is zero, no class-based
307 filtering will be performed.
308 @param FilterDataRecordGuid - FilterEvent will be signaled whenever FilterDataRecordGuid
309 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
310 FilterDataRecordGuid is NULL, then no GUID-based filtering
313 @retval EFI_SUCCESS - The filter driver event was registered.
314 @retval EFI_ALREADY_STARTED - FilterEvent was previously registered and cannot be
316 @retval EFI_OUT_OF_RESOURCES - The filter driver event was not registered due to lack of
322 DataHubRegisterFilterDriver (
323 IN EFI_DATA_HUB_PROTOCOL
* This
,
324 IN EFI_EVENT FilterEvent
,
325 IN EFI_TPL FilterTpl
,
326 IN UINT64 FilterClass
,
327 IN EFI_GUID
* FilterDataRecordGuid OPTIONAL
331 DATA_HUB_INSTANCE
*Private
;
332 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
334 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
336 FilterDriver
= (DATA_HUB_FILTER_DRIVER
*) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER
));
337 if (FilterDriver
== NULL
) {
338 return EFI_OUT_OF_RESOURCES
;
341 // Initialize filter driver info
343 FilterDriver
->Signature
= EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE
;
344 FilterDriver
->Event
= FilterEvent
;
345 FilterDriver
->Tpl
= FilterTpl
;
346 FilterDriver
->GetNextMonotonicCount
= 0;
347 if (FilterClass
== 0) {
348 FilterDriver
->ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
349 EFI_DATA_RECORD_CLASS_ERROR
|
350 EFI_DATA_RECORD_CLASS_DATA
|
351 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
353 FilterDriver
->ClassFilter
= FilterClass
;
356 if (FilterDataRecordGuid
!= NULL
) {
357 CopyMem (&FilterDriver
->FilterDataRecordGuid
, FilterDataRecordGuid
, sizeof (EFI_GUID
));
360 // Search for duplicate entries
362 if (FindFilterDriverByEvent (&Private
->FilterDriverListHead
, FilterEvent
) != NULL
) {
363 FreePool (FilterDriver
);
364 return EFI_ALREADY_STARTED
;
367 // Make insertion an atomic operation with the lock.
369 EfiAcquireLock (&Private
->DataLock
);
370 InsertTailList (&Private
->FilterDriverListHead
, &FilterDriver
->Link
);
371 EfiReleaseLock (&Private
->DataLock
);
374 // Signal the Filter driver we just loaded so they will recieve all the
375 // previous history. If we did not signal here we would have to wait until
376 // the next data was logged to get the history. In a case where no next
377 // data was logged we would never get synced up.
379 gBS
->SignalEvent (FilterEvent
);
385 Remove a Filter Driver, so it no longer gets called when data
386 information is logged.
388 @param This - Protocol instance structure
390 @param FilterEvent - Event that represents a filter driver that is to be
393 @retval EFI_SUCCESS - If FilterEvent was unregistered
395 @retval EFI_NOT_FOUND - If FilterEvent does not exist
400 DataHubUnregisterFilterDriver (
401 IN EFI_DATA_HUB_PROTOCOL
*This
,
402 IN EFI_EVENT FilterEvent
405 DATA_HUB_INSTANCE
*Private
;
406 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
408 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
411 // Search for duplicate entries
413 FilterDriver
= FindFilterDriverByEvent (
414 &Private
->FilterDriverListHead
,
417 if (FilterDriver
== NULL
) {
418 return EFI_NOT_FOUND
;
421 // Make removal an atomic operation with the lock
423 EfiAcquireLock (&Private
->DataLock
);
424 RemoveEntryList (&FilterDriver
->Link
);
425 EfiReleaseLock (&Private
->DataLock
);
431 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
432 represents Event and return it.
434 @param Head - Head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER
437 @param Event - Event to be search for in the Head list.
439 @retval EFI_DATA_HUB_FILTER_DRIVER - Returned if Event stored in the
440 Head doubly linked list.
442 @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.
482 EFI_DATA_RECORD_HEADER
*
485 IN UINT64 ClassFilter
,
486 IN OUT UINT64
*PtrCurrentMTC
490 EFI_DATA_ENTRY
*LogEntry
;
492 BOOLEAN ReturnFirstEntry
;
493 EFI_DATA_RECORD_HEADER
*Record
;
494 EFI_DATA_ENTRY
*NextLogEntry
;
497 // If MonotonicCount == 0 just return the first one
499 ReturnFirstEntry
= (BOOLEAN
) (*PtrCurrentMTC
== 0);
502 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
503 LogEntry
= DATA_ENTRY_FROM_LINK (Link
);
504 if ((LogEntry
->Record
->DataRecordClass
& ClassFilter
) == 0) {
506 // Skip any entry that does not have the correct ClassFilter
511 if ((LogEntry
->Record
->LogMonotonicCount
== *PtrCurrentMTC
) || ReturnFirstEntry
) {
513 // Return record to the user
515 Record
= LogEntry
->Record
;
518 // Calculate the next MTC value. If there is no next entry set
522 for (Link
= Link
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
523 NextLogEntry
= DATA_ENTRY_FROM_LINK (Link
);
524 if ((NextLogEntry
->Record
->DataRecordClass
& ClassFilter
) != 0) {
526 // Return the MTC of the next thing to search for if found
528 *PtrCurrentMTC
= NextLogEntry
->Record
->LogMonotonicCount
;
533 // Record found exit loop and return
544 Install Driver to produce Data Hub protocol.
546 @param ImageHandle Module's image handle
547 @param SystemTable Pointer of EFI_SYSTEM_TABLE
550 @retval EFI_SUCCESS - Logging Hub protocol installed
552 @retval Other - No protocol installed, unload driver.
558 IN EFI_HANDLE ImageHandle
,
559 IN EFI_SYSTEM_TABLE
*SystemTable
563 UINT32 HighMontonicCount
;
565 mPrivateData
.Signature
= DATA_HUB_INSTANCE_SIGNATURE
;
566 mPrivateData
.DataHub
.LogData
= DataHubLogData
;
567 mPrivateData
.DataHub
.GetNextRecord
= DataHubGetNextRecord
;
568 mPrivateData
.DataHub
.RegisterFilterDriver
= DataHubRegisterFilterDriver
;
569 mPrivateData
.DataHub
.UnregisterFilterDriver
= DataHubUnregisterFilterDriver
;
572 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
573 // required by this protocol
575 InitializeListHead (&mPrivateData
.DataListHead
);
576 InitializeListHead (&mPrivateData
.FilterDriverListHead
);
578 EfiInitializeLock (&mPrivateData
.DataLock
, TPL_NOTIFY
);
581 // Make sure we get a bigger MTC number on every boot!
583 Status
= gRT
->GetNextHighMonotonicCount (&HighMontonicCount
);
584 if (EFI_ERROR (Status
)) {
586 // if system service fails pick a sane value.
588 mPrivateData
.GlobalMonotonicCount
= 0;
590 mPrivateData
.GlobalMonotonicCount
= LShiftU64 ((UINT64
) HighMontonicCount
, 32);
593 // Make a new handle and install the protocol
595 mPrivateData
.Handle
= NULL
;
596 Status
= gBS
->InstallProtocolInterface (
597 &mPrivateData
.Handle
,
598 &gEfiDataHubProtocolGuid
,
599 EFI_NATIVE_INTERFACE
,
600 &mPrivateData
.DataHub