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
32 DATA_HUB_FILTER_DRIVER
*
33 FindFilterDriverByEvent (
38 EFI_DATA_RECORD_HEADER
*
41 IN UINT64 ClassFilter
,
42 IN OUT UINT64
*PtrCurrentMTC
47 Log data record into the data logging hub
49 @param This - Protocol instance structure
50 @param DataRecordGuid - GUID that defines record contents
51 @param ProducerName - GUID that defines the name of the producer of the data
52 @param DataRecordClass - Class that defines generic record type
53 @param RawData - Data Log record as defined by DataRecordGuid
54 @param RawDataSize - Size of Data Log data in bytes
56 @retval EFI_SUCCESS - If data was logged
57 @retval EFI_OUT_OF_RESOURCES - If data was not logged due to lack of system
63 IN EFI_DATA_HUB_PROTOCOL
*This
,
64 IN EFI_GUID
*DataRecordGuid
,
65 IN EFI_GUID
*ProducerName
,
66 IN UINT64 DataRecordClass
,
72 DATA_HUB_INSTANCE
*Private
;
73 EFI_DATA_ENTRY
*LogEntry
;
76 EFI_DATA_RECORD_HEADER
*Record
;
78 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
82 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
85 // Combine the storage for the internal structs and a copy of the log record.
86 // Record follows PrivateLogEntry. The consumer will be returned a pointer
87 // to Record so we don't what it to be the thing that was allocated from
88 // pool, so the consumer can't free an data record by mistake.
90 RecordSize
= sizeof (EFI_DATA_RECORD_HEADER
) + RawDataSize
;
91 TotalSize
= sizeof (EFI_DATA_ENTRY
) + RecordSize
;
94 // The Logging action is the critical section, so it is locked.
95 // The MTC asignment & update, time, and logging must be an
96 // atomic operation, so use the lock.
98 Status
= EfiAcquireLockOrFail (&Private
->DataLock
);
99 if (EFI_ERROR (Status
)) {
101 // Reentrancy detected so exit!
106 LogEntry
= AllocatePool (TotalSize
);
108 if (LogEntry
== NULL
) {
109 EfiReleaseLock (&Private
->DataLock
);
110 return EFI_OUT_OF_RESOURCES
;
113 ZeroMem (LogEntry
, TotalSize
);
115 Record
= (EFI_DATA_RECORD_HEADER
*) (LogEntry
+ 1);
116 Raw
= (VOID
*) (Record
+ 1);
119 // Build Standard Log Header
121 Record
->Version
= EFI_DATA_RECORD_HEADER_VERSION
;
122 Record
->HeaderSize
= sizeof (EFI_DATA_RECORD_HEADER
);
123 Record
->RecordSize
= RecordSize
;
124 CopyMem (&Record
->DataRecordGuid
, DataRecordGuid
, sizeof (EFI_GUID
));
125 CopyMem (&Record
->ProducerName
, ProducerName
, sizeof (EFI_GUID
));
126 Record
->DataRecordClass
= DataRecordClass
;
129 // Ensure LogMonotonicCount is not zero
131 Record
->LogMonotonicCount
= ++Private
->GlobalMonotonicCount
;
133 gRT
->GetTime (&Record
->LogTime
, NULL
);
136 // Insert log into the internal linked list.
138 LogEntry
->Signature
= EFI_DATA_ENTRY_SIGNATURE
;
139 LogEntry
->Record
= Record
;
140 LogEntry
->RecordSize
= sizeof (EFI_DATA_ENTRY
) + RawDataSize
;
141 InsertTailList (&Private
->DataListHead
, &LogEntry
->Link
);
143 CopyMem (Raw
, RawData
, RawDataSize
);
145 EfiReleaseLock (&Private
->DataLock
);
148 // Send Signal to all the filter drivers which are interested
149 // in the record's class and guid.
151 Head
= &Private
->FilterDriverListHead
;
152 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
153 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
154 if (((FilterEntry
->ClassFilter
& DataRecordClass
) != 0) &&
155 (CompareGuid (&FilterEntry
->FilterDataRecordGuid
, &gZeroGuid
) ||
156 CompareGuid (&FilterEntry
->FilterDataRecordGuid
, DataRecordGuid
))) {
157 gBS
->SignalEvent (FilterEntry
->Event
);
166 Get a previously logged data record and the MonotonicCount for the next
167 availible Record. This allows all records or all records later
168 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
169 is passed in with a MonotonicCout of zero return the first record
170 not yet read by the filter driver. If FilterDriverEvent is NULL and
171 MonotonicCount is zero return the first data record.
173 @param This The EFI_DATA_HUB_PROTOCOL instance.
174 @param MonotonicCount Specifies the Record to return. On input, zero means
175 return the first record. On output, contains the next
176 record to availible. Zero indicates no more records.
177 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
178 of zero, it means to return the first data record.
179 If FilterDriverEvent is passed in, then a MonotonicCount
180 of zero means to return the first data not yet read by
182 @param Record Returns a dynamically allocated memory buffer with a data
183 record that matches MonotonicCount.
185 @retval EFI_SUCCESS - Data was returned in Record.
186 @retval EFI_INVALID_PARAMETER - FilterDriverEvent was passed in but does not exist.
187 @retval EFI_NOT_FOUND - MonotonicCount does not match any data record in the
188 system. If a MonotonicCount of zero was passed in, then
189 no data records exist in the system.
190 @retval EFI_OUT_OF_RESOURCES - Record was not returned due to lack of system resources.
195 DataHubGetNextRecord (
196 IN EFI_DATA_HUB_PROTOCOL
*This
,
197 IN OUT UINT64
*MonotonicCount
,
198 IN EFI_EVENT
*FilterDriverEvent
, OPTIONAL
199 OUT EFI_DATA_RECORD_HEADER
**Record
202 DATA_HUB_INSTANCE
*Private
;
203 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
205 UINT64 FilterMonotonicCount
;
207 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
210 FilterMonotonicCount
= 0;
211 ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
212 EFI_DATA_RECORD_CLASS_ERROR
|
213 EFI_DATA_RECORD_CLASS_DATA
|
214 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
216 if (FilterDriverEvent
!= NULL
) {
218 // For events the beginning is the last unread record. This info is
219 // stored in the instance structure, so we must look up the event
222 FilterDriver
= FindFilterDriverByEvent (
223 &Private
->FilterDriverListHead
,
226 if (FilterDriver
== NULL
) {
227 return EFI_INVALID_PARAMETER
;
230 // Use the Class filter the event was created with.
232 ClassFilter
= FilterDriver
->ClassFilter
;
234 if (*MonotonicCount
== 0) {
236 // Use the MTC from the Filter Driver.
238 FilterMonotonicCount
= FilterDriver
->GetNextMonotonicCount
;
239 if (FilterMonotonicCount
!= 0) {
241 // The GetNextMonotonicCount field remembers the last value from the previous time.
242 // But we already processed this vaule, so we need to find the next one.
244 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, &FilterMonotonicCount
);
245 *MonotonicCount
= FilterMonotonicCount
;
246 if (FilterMonotonicCount
== 0) {
248 // If there is no new record to get exit now.
250 return EFI_NOT_FOUND
;
258 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
259 if (*Record
== NULL
) {
260 return EFI_NOT_FOUND
;
263 if (FilterDriver
!= NULL
) {
265 // If we have a filter driver update the records that have been read.
266 // If MonotonicCount is zero No more reacords left.
268 if (*MonotonicCount
== 0) {
270 // Save the current Record MonotonicCount.
272 FilterDriver
->GetNextMonotonicCount
= (*Record
)->LogMonotonicCount
;
275 // Point to next undread record
277 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
285 This function registers the data hub filter driver that is represented
286 by FilterEvent. Only one instance of each FilterEvent can be registered.
287 After the FilterEvent is registered, it will be signaled so it can sync
288 with data records that have been recorded prior to the FilterEvent being
291 @param This - The EFI_DATA_HUB_PROTOCOL instance.
292 @param FilterEvent - The EFI_EVENT to signal whenever data that matches
293 FilterClass is logged in the system.
294 @param FilterTpl - The maximum EFI_TPL at which FilterEvent can be
295 signaled. It is strongly recommended that you use the
296 lowest EFI_TPL possible.
297 @param FilterClass - FilterEvent will be signaled whenever a bit in
298 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
299 FilterClass. If FilterClass is zero, no class-based
300 filtering will be performed.
301 @param FilterDataRecordGuid - FilterEvent will be signaled whenever FilterDataRecordGuid
302 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
303 FilterDataRecordGuid is NULL, then no GUID-based filtering
306 @retval EFI_SUCCESS - The filter driver event was registered.
307 @retval EFI_ALREADY_STARTED - FilterEvent was previously registered and cannot be
309 @retval EFI_OUT_OF_RESOURCES - The filter driver event was not registered due to lack of
315 DataHubRegisterFilterDriver (
316 IN EFI_DATA_HUB_PROTOCOL
* This
,
317 IN EFI_EVENT FilterEvent
,
318 IN EFI_TPL FilterTpl
,
319 IN UINT64 FilterClass
,
320 IN EFI_GUID
* FilterDataRecordGuid OPTIONAL
324 DATA_HUB_INSTANCE
*Private
;
325 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
327 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
329 FilterDriver
= (DATA_HUB_FILTER_DRIVER
*) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER
));
330 if (FilterDriver
== NULL
) {
331 return EFI_OUT_OF_RESOURCES
;
334 // Initialize filter driver info
336 FilterDriver
->Signature
= EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE
;
337 FilterDriver
->Event
= FilterEvent
;
338 FilterDriver
->Tpl
= FilterTpl
;
339 FilterDriver
->GetNextMonotonicCount
= 0;
340 if (FilterClass
== 0) {
341 FilterDriver
->ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
342 EFI_DATA_RECORD_CLASS_ERROR
|
343 EFI_DATA_RECORD_CLASS_DATA
|
344 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
346 FilterDriver
->ClassFilter
= FilterClass
;
349 if (FilterDataRecordGuid
!= NULL
) {
350 CopyMem (&FilterDriver
->FilterDataRecordGuid
, FilterDataRecordGuid
, sizeof (EFI_GUID
));
353 // Search for duplicate entries
355 if (FindFilterDriverByEvent (&Private
->FilterDriverListHead
, FilterEvent
) != NULL
) {
356 FreePool (FilterDriver
);
357 return EFI_ALREADY_STARTED
;
360 // Make insertion an atomic operation with the lock.
362 EfiAcquireLock (&Private
->DataLock
);
363 InsertTailList (&Private
->FilterDriverListHead
, &FilterDriver
->Link
);
364 EfiReleaseLock (&Private
->DataLock
);
367 // Signal the Filter driver we just loaded so they will recieve all the
368 // previous history. If we did not signal here we would have to wait until
369 // the next data was logged to get the history. In a case where no next
370 // data was logged we would never get synced up.
372 gBS
->SignalEvent (FilterEvent
);
378 Remove a Filter Driver, so it no longer gets called when data
379 information is logged.
381 @param This - Protocol instance structure
383 @param FilterEvent - Event that represents a filter driver that is to be
386 @retval EFI_SUCCESS - If FilterEvent was unregistered
388 @retval EFI_NOT_FOUND - If FilterEvent does not exist
393 DataHubUnregisterFilterDriver (
394 IN EFI_DATA_HUB_PROTOCOL
*This
,
395 IN EFI_EVENT FilterEvent
398 DATA_HUB_INSTANCE
*Private
;
399 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
401 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
404 // Search for duplicate entries
406 FilterDriver
= FindFilterDriverByEvent (
407 &Private
->FilterDriverListHead
,
410 if (FilterDriver
== NULL
) {
411 return EFI_NOT_FOUND
;
414 // Make removal an atomic operation with the lock
416 EfiAcquireLock (&Private
->DataLock
);
417 RemoveEntryList (&FilterDriver
->Link
);
418 EfiReleaseLock (&Private
->DataLock
);
424 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
425 represents Event and return it.
427 @param Head - Head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER
430 @param Event - Event to be search for in the Head list.
432 @retval EFI_DATA_HUB_FILTER_DRIVER - Returned if Event stored in the
433 Head doubly linked list.
435 @retval NULL - If Event is not in the list
438 DATA_HUB_FILTER_DRIVER
*
439 FindFilterDriverByEvent (
445 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
448 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
449 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
450 if (FilterEntry
->Event
== Event
) {
459 Search the Head doubly linked list for the passed in MTC. Return the
460 matching element in Head and the MTC on the next entry.
462 @param Head - Head of Data Log linked list.
464 @param ClassFilter - Only match the MTC if it is in the same Class as the
467 @param PtrCurrentMTC - On IN contians MTC to search for. On OUT contians next
468 MTC in the data log list or zero if at end of the list.
470 @retval EFI_DATA_LOG_ENTRY - Return pointer to data log data from Head list.
472 @retval NULL - If no data record exists.
475 EFI_DATA_RECORD_HEADER
*
478 IN UINT64 ClassFilter
,
479 IN OUT UINT64
*PtrCurrentMTC
483 EFI_DATA_ENTRY
*LogEntry
;
485 BOOLEAN ReturnFirstEntry
;
486 EFI_DATA_RECORD_HEADER
*Record
;
487 EFI_DATA_ENTRY
*NextLogEntry
;
490 // If MonotonicCount == 0 just return the first one
492 ReturnFirstEntry
= (BOOLEAN
) (*PtrCurrentMTC
== 0);
495 for (Link
= Head
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
496 LogEntry
= DATA_ENTRY_FROM_LINK (Link
);
497 if ((LogEntry
->Record
->DataRecordClass
& ClassFilter
) == 0) {
499 // Skip any entry that does not have the correct ClassFilter
504 if ((LogEntry
->Record
->LogMonotonicCount
== *PtrCurrentMTC
) || ReturnFirstEntry
) {
506 // Return record to the user
508 Record
= LogEntry
->Record
;
511 // Calculate the next MTC value. If there is no next entry set
515 for (Link
= Link
->ForwardLink
; Link
!= Head
; Link
= Link
->ForwardLink
) {
516 NextLogEntry
= DATA_ENTRY_FROM_LINK (Link
);
517 if ((NextLogEntry
->Record
->DataRecordClass
& ClassFilter
) != 0) {
519 // Return the MTC of the next thing to search for if found
521 *PtrCurrentMTC
= NextLogEntry
->Record
->LogMonotonicCount
;
526 // Record found exit loop and return
536 // Since this driver will only ever produce one instance of the Logging Hub
537 // protocol you are not required to dynamically allocate the PrivateData.
539 DATA_HUB_INSTANCE mPrivateData
;
543 Install Driver to produce Data Hub protocol.
545 @param ImageHandle Module's image handle
546 @param SystemTable Pointer of EFI_SYSTEM_TABLE
549 @retval EFI_SUCCESS - Logging Hub protocol installed
551 @retval Other - No protocol installed, unload driver.
557 IN EFI_HANDLE ImageHandle
,
558 IN EFI_SYSTEM_TABLE
*SystemTable
562 UINT32 HighMontonicCount
;
564 mPrivateData
.Signature
= DATA_HUB_INSTANCE_SIGNATURE
;
565 mPrivateData
.DataHub
.LogData
= DataHubLogData
;
566 mPrivateData
.DataHub
.GetNextRecord
= DataHubGetNextRecord
;
567 mPrivateData
.DataHub
.RegisterFilterDriver
= DataHubRegisterFilterDriver
;
568 mPrivateData
.DataHub
.UnregisterFilterDriver
= DataHubUnregisterFilterDriver
;
571 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
572 // required by this protocol
574 InitializeListHead (&mPrivateData
.DataListHead
);
575 InitializeListHead (&mPrivateData
.FilterDriverListHead
);
577 EfiInitializeLock (&mPrivateData
.DataLock
, TPL_NOTIFY
);
580 // Make sure we get a bigger MTC number on every boot!
582 Status
= gRT
->GetNextHighMonotonicCount (&HighMontonicCount
);
583 if (EFI_ERROR (Status
)) {
585 // if system service fails pick a sane value.
587 mPrivateData
.GlobalMonotonicCount
= 0;
589 mPrivateData
.GlobalMonotonicCount
= LShiftU64 ((UINT64
) HighMontonicCount
, 32);
592 // Make a new handle and install the protocol
594 mPrivateData
.Handle
= NULL
;
595 Status
= gBS
->InstallProtocolInterface (
596 &mPrivateData
.Handle
,
597 &gEfiDataHubProtocolGuid
,
598 EFI_NATIVE_INTERFACE
,
599 &mPrivateData
.DataHub