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 - 2009, Intel Corporation
6 All rights reserved. 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.
18 CONST EFI_GUID gZeroGuid
= { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
21 // Since this driver will only ever produce one instance of the Logging Hub
22 // protocol you are not required to dynamically allocate the PrivateData.
24 DATA_HUB_INSTANCE mPrivateData
;
27 // Worker functions private to this file
29 DATA_HUB_FILTER_DRIVER
*
30 FindFilterDriverByEvent (
35 EFI_DATA_RECORD_HEADER
*
38 IN UINT64 ClassFilter
,
39 IN OUT UINT64
*PtrCurrentMTC
43 Log data record into the data logging hub
45 @param This Protocol instance structure
46 @param DataRecordGuid GUID that defines record contents
47 @param ProducerName GUID that defines the name of the producer of the data
48 @param DataRecordClass Class that defines generic record type
49 @param RawData Data Log record as defined by DataRecordGuid
50 @param RawDataSize Size of Data Log data in bytes
52 @retval EFI_SUCCESS If data was logged
53 @retval EFI_OUT_OF_RESOURCES If data was not logged due to lack of system
59 IN EFI_DATA_HUB_PROTOCOL
*This
,
60 IN EFI_GUID
*DataRecordGuid
,
61 IN EFI_GUID
*ProducerName
,
62 IN UINT64 DataRecordClass
,
68 DATA_HUB_INSTANCE
*Private
;
69 EFI_DATA_ENTRY
*LogEntry
;
72 EFI_DATA_RECORD_HEADER
*Record
;
74 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
78 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
81 // Combine the storage for the internal structs and a copy of the log record.
82 // Record follows PrivateLogEntry. The consumer will be returned a pointer
83 // to Record so we don't what it to be the thing that was allocated from
84 // pool, so the consumer can't free an data record by mistake.
86 RecordSize
= sizeof (EFI_DATA_RECORD_HEADER
) + RawDataSize
;
87 TotalSize
= sizeof (EFI_DATA_ENTRY
) + RecordSize
;
90 // The Logging action is the critical section, so it is locked.
91 // The MTC asignment & update, time, and logging must be an
92 // atomic operation, so use the lock.
94 Status
= EfiAcquireLockOrFail (&Private
->DataLock
);
95 if (EFI_ERROR (Status
)) {
97 // Reentrancy detected so exit!
102 LogEntry
= AllocatePool (TotalSize
);
104 if (LogEntry
== NULL
) {
105 EfiReleaseLock (&Private
->DataLock
);
106 return EFI_OUT_OF_RESOURCES
;
109 ZeroMem (LogEntry
, TotalSize
);
111 Record
= (EFI_DATA_RECORD_HEADER
*) (LogEntry
+ 1);
112 Raw
= (VOID
*) (Record
+ 1);
115 // Build Standard Log Header
117 Record
->Version
= EFI_DATA_RECORD_HEADER_VERSION
;
118 Record
->HeaderSize
= sizeof (EFI_DATA_RECORD_HEADER
);
119 Record
->RecordSize
= RecordSize
;
120 CopyMem (&Record
->DataRecordGuid
, DataRecordGuid
, sizeof (EFI_GUID
));
121 CopyMem (&Record
->ProducerName
, ProducerName
, sizeof (EFI_GUID
));
122 Record
->DataRecordClass
= DataRecordClass
;
125 // Ensure LogMonotonicCount is not zero
127 Record
->LogMonotonicCount
= ++Private
->GlobalMonotonicCount
;
129 gRT
->GetTime (&Record
->LogTime
, NULL
);
132 // Insert log into the internal linked list.
134 LogEntry
->Signature
= EFI_DATA_ENTRY_SIGNATURE
;
135 LogEntry
->Record
= Record
;
136 LogEntry
->RecordSize
= sizeof (EFI_DATA_ENTRY
) + RawDataSize
;
137 InsertTailList (&Private
->DataListHead
, &LogEntry
->Link
);
139 CopyMem (Raw
, RawData
, RawDataSize
);
141 EfiReleaseLock (&Private
->DataLock
);
144 // Send Signal to all the filter drivers which are interested
145 // in the record's class and guid.
147 Head
= &Private
->FilterDriverListHead
;
148 for (Link
= GetFirstNode(Head
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
149 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
150 if (((FilterEntry
->ClassFilter
& DataRecordClass
) != 0) &&
151 (CompareGuid (&FilterEntry
->FilterDataRecordGuid
, &gZeroGuid
) ||
152 CompareGuid (&FilterEntry
->FilterDataRecordGuid
, DataRecordGuid
))) {
153 gBS
->SignalEvent (FilterEntry
->Event
);
162 Get a previously logged data record and the MonotonicCount for the next
163 availible Record. This allows all records or all records later
164 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
165 is passed in with a MonotonicCout of zero return the first record
166 not yet read by the filter driver. If FilterDriverEvent is NULL and
167 MonotonicCount is zero return the first data record.
169 @param This Pointer to the EFI_DATA_HUB_PROTOCOL instance.
170 @param MonotonicCount Specifies the Record to return. On input, zero means
171 return the first record. On output, contains the next
172 record to availible. Zero indicates no more records.
173 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
174 of zero, it means to return the first data record.
175 If FilterDriverEvent is passed in, then a MonotonicCount
176 of zero means to return the first data not yet read by
178 @param Record Returns a dynamically allocated memory buffer with a data
179 record that matches MonotonicCount.
181 @retval EFI_SUCCESS Data was returned in Record.
182 @retval EFI_INVALID_PARAMETER FilterDriverEvent was passed in but does not exist.
183 @retval EFI_NOT_FOUND MonotonicCount does not match any data record in the
184 system. If a MonotonicCount of zero was passed in, then
185 no data records exist in the system.
186 @retval EFI_OUT_OF_RESOURCES Record was not returned due to lack of system resources.
191 DataHubGetNextRecord (
192 IN EFI_DATA_HUB_PROTOCOL
*This
,
193 IN OUT UINT64
*MonotonicCount
,
194 IN EFI_EVENT
*FilterDriverEvent
, OPTIONAL
195 OUT EFI_DATA_RECORD_HEADER
**Record
198 DATA_HUB_INSTANCE
*Private
;
199 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
201 UINT64 FilterMonotonicCount
;
203 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
206 FilterMonotonicCount
= 0;
207 ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
208 EFI_DATA_RECORD_CLASS_ERROR
|
209 EFI_DATA_RECORD_CLASS_DATA
|
210 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
212 if (FilterDriverEvent
!= NULL
) {
214 // For events the beginning is the last unread record. This info is
215 // stored in the instance structure, so we must look up the event
218 FilterDriver
= FindFilterDriverByEvent (
219 &Private
->FilterDriverListHead
,
222 if (FilterDriver
== NULL
) {
223 return EFI_INVALID_PARAMETER
;
226 // Use the Class filter the event was created with.
228 ClassFilter
= FilterDriver
->ClassFilter
;
230 if (*MonotonicCount
== 0) {
232 // Use the MTC from the Filter Driver.
234 FilterMonotonicCount
= FilterDriver
->GetNextMonotonicCount
;
237 // The GetNextMonotonicCount field remembers the last value from the previous time.
238 // But we already processed this vaule, so we need to find the next one.
240 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, &FilterMonotonicCount
);
241 if (FilterMonotonicCount
!= 0) {
242 *MonotonicCount
= FilterMonotonicCount
;
245 if ((FilterDriver
->GetNextMonotonicCount
!= 0) && (FilterMonotonicCount
== 0)) {
247 // If there is no new record to get exit now.
250 return EFI_NOT_FOUND
;
257 *Record
= GetNextDataRecord (&Private
->DataListHead
, ClassFilter
, MonotonicCount
);
258 if (*Record
== NULL
) {
259 return EFI_NOT_FOUND
;
262 if (FilterDriver
!= NULL
) {
264 // If we have a filter driver update the records that have been read.
265 // If MonotonicCount is zero No more reacords left.
267 if (*MonotonicCount
== 0) {
269 // Save the current Record MonotonicCount.
271 FilterDriver
->GetNextMonotonicCount
= (*Record
)->LogMonotonicCount
;
274 // Point to next undread record
276 FilterDriver
->GetNextMonotonicCount
= *MonotonicCount
;
284 This function registers the data hub filter driver that is represented
285 by FilterEvent. Only one instance of each FilterEvent can be registered.
286 After the FilterEvent is registered, it will be signaled so it can sync
287 with data records that have been recorded prior to the FilterEvent being
290 @param This Pointer to The EFI_DATA_HUB_PROTOCOL instance.
291 @param FilterEvent The EFI_EVENT to signal whenever data that matches
292 FilterClass is logged in the system.
293 @param FilterTpl The maximum EFI_TPL at which FilterEvent can be
294 signaled. It is strongly recommended that you use the
295 lowest EFI_TPL possible.
296 @param FilterClass FilterEvent will be signaled whenever a bit in
297 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
298 FilterClass. If FilterClass is zero, no class-based
299 filtering will be performed.
300 @param FilterDataRecordGuid FilterEvent will be signaled whenever FilterDataRecordGuid
301 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
302 FilterDataRecordGuid is NULL, then no GUID-based filtering
305 @retval EFI_SUCCESS The filter driver event was registered.
306 @retval EFI_ALREADY_STARTED FilterEvent was previously registered and cannot be
308 @retval EFI_OUT_OF_RESOURCES The filter driver event was not registered due to lack of
314 DataHubRegisterFilterDriver (
315 IN EFI_DATA_HUB_PROTOCOL
* This
,
316 IN EFI_EVENT FilterEvent
,
317 IN EFI_TPL FilterTpl
,
318 IN UINT64 FilterClass
,
319 IN EFI_GUID
* FilterDataRecordGuid OPTIONAL
323 DATA_HUB_INSTANCE
*Private
;
324 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
326 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
328 FilterDriver
= (DATA_HUB_FILTER_DRIVER
*) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER
));
329 if (FilterDriver
== NULL
) {
330 return EFI_OUT_OF_RESOURCES
;
333 // Initialize filter driver info
335 FilterDriver
->Signature
= EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE
;
336 FilterDriver
->Event
= FilterEvent
;
337 FilterDriver
->Tpl
= FilterTpl
;
338 FilterDriver
->GetNextMonotonicCount
= 0;
339 if (FilterClass
== 0) {
340 FilterDriver
->ClassFilter
= EFI_DATA_RECORD_CLASS_DEBUG
|
341 EFI_DATA_RECORD_CLASS_ERROR
|
342 EFI_DATA_RECORD_CLASS_DATA
|
343 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
345 FilterDriver
->ClassFilter
= FilterClass
;
348 if (FilterDataRecordGuid
!= NULL
) {
349 CopyMem (&FilterDriver
->FilterDataRecordGuid
, FilterDataRecordGuid
, sizeof (EFI_GUID
));
352 // Search for duplicate entries
354 if (FindFilterDriverByEvent (&Private
->FilterDriverListHead
, FilterEvent
) != NULL
) {
355 FreePool (FilterDriver
);
356 return EFI_ALREADY_STARTED
;
359 // Make insertion an atomic operation with the lock.
361 EfiAcquireLock (&Private
->DataLock
);
362 InsertTailList (&Private
->FilterDriverListHead
, &FilterDriver
->Link
);
363 EfiReleaseLock (&Private
->DataLock
);
366 // Signal the Filter driver we just loaded so they will recieve all the
367 // previous history. If we did not signal here we would have to wait until
368 // the next data was logged to get the history. In a case where no next
369 // data was logged we would never get synced up.
371 gBS
->SignalEvent (FilterEvent
);
377 Remove a Filter Driver, so it no longer gets called when data
378 information is logged.
380 @param This Protocol instance structure
382 @param FilterEvent Event that represents a filter driver that is to be
385 @retval EFI_SUCCESS If FilterEvent was unregistered
386 @retval EFI_NOT_FOUND If FilterEvent does not exist
390 DataHubUnregisterFilterDriver (
391 IN EFI_DATA_HUB_PROTOCOL
*This
,
392 IN EFI_EVENT FilterEvent
395 DATA_HUB_INSTANCE
*Private
;
396 DATA_HUB_FILTER_DRIVER
*FilterDriver
;
398 Private
= DATA_HUB_INSTANCE_FROM_THIS (This
);
401 // Search for duplicate entries
403 FilterDriver
= FindFilterDriverByEvent (
404 &Private
->FilterDriverListHead
,
407 if (FilterDriver
== NULL
) {
408 return EFI_NOT_FOUND
;
411 // Make removal an atomic operation with the lock
413 EfiAcquireLock (&Private
->DataLock
);
414 RemoveEntryList (&FilterDriver
->Link
);
415 EfiReleaseLock (&Private
->DataLock
);
421 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
422 represents Event and return it.
424 @param Head Pointer to head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER structures.
425 @param Event Event to be search for in the Head list.
427 @retval EFI_DATA_HUB_FILTER_DRIVER Returned if Event stored in the Head doubly linked list.
428 @retval NULL If Event is not in the list
431 DATA_HUB_FILTER_DRIVER
*
432 FindFilterDriverByEvent (
437 DATA_HUB_FILTER_DRIVER
*FilterEntry
;
440 for (Link
= GetFirstNode(Head
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
441 FilterEntry
= FILTER_ENTRY_FROM_LINK (Link
);
442 if (FilterEntry
->Event
== Event
) {
451 Search the Head doubly linked list for the passed in MTC. Return the
452 matching element in Head and the MTC on the next entry.
454 @param Head Head of Data Log linked list.
455 @param ClassFilter Only match the MTC if it is in the same Class as the
457 @param PtrCurrentMTC On IN contians MTC to search for. On OUT contians next
458 MTC in the data log list or zero if at end of the list.
460 @retval EFI_DATA_LOG_ENTRY Return pointer to data log data from Head list.
461 @retval NULL If no data record exists.
464 EFI_DATA_RECORD_HEADER
*
467 IN UINT64 ClassFilter
,
468 IN OUT UINT64
*PtrCurrentMTC
472 EFI_DATA_ENTRY
*LogEntry
;
474 BOOLEAN ReturnFirstEntry
;
475 EFI_DATA_RECORD_HEADER
*Record
;
476 EFI_DATA_ENTRY
*NextLogEntry
;
479 // If MonotonicCount == 0 just return the first one
481 ReturnFirstEntry
= (BOOLEAN
) (*PtrCurrentMTC
== 0);
484 for (Link
= GetFirstNode(Head
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
485 LogEntry
= DATA_ENTRY_FROM_LINK (Link
);
486 if ((LogEntry
->Record
->DataRecordClass
& ClassFilter
) == 0) {
488 // Skip any entry that does not have the correct ClassFilter
493 if ((LogEntry
->Record
->LogMonotonicCount
== *PtrCurrentMTC
) || ReturnFirstEntry
) {
495 // Return record to the user
497 Record
= LogEntry
->Record
;
500 // Calculate the next MTC value. If there is no next entry set
504 for (Link
= GetNextNode(Head
, Link
); Link
!= Head
; Link
= GetNextNode(Head
, Link
)) {
505 NextLogEntry
= DATA_ENTRY_FROM_LINK (Link
);
506 if ((NextLogEntry
->Record
->DataRecordClass
& ClassFilter
) != 0) {
508 // Return the MTC of the next thing to search for if found
510 *PtrCurrentMTC
= NextLogEntry
->Record
->LogMonotonicCount
;
515 // Record found exit loop and return
525 Driver's Entry point routine that install Driver to produce Data Hub protocol.
527 @param ImageHandle Module's image handle
528 @param SystemTable Pointer of EFI_SYSTEM_TABLE
530 @retval EFI_SUCCESS Logging Hub protocol installed
531 @retval Other No protocol installed, unload driver.
537 IN EFI_HANDLE ImageHandle
,
538 IN EFI_SYSTEM_TABLE
*SystemTable
542 UINT32 HighMontonicCount
;
544 mPrivateData
.Signature
= DATA_HUB_INSTANCE_SIGNATURE
;
545 mPrivateData
.DataHub
.LogData
= DataHubLogData
;
546 mPrivateData
.DataHub
.GetNextRecord
= DataHubGetNextRecord
;
547 mPrivateData
.DataHub
.RegisterFilterDriver
= DataHubRegisterFilterDriver
;
548 mPrivateData
.DataHub
.UnregisterFilterDriver
= DataHubUnregisterFilterDriver
;
551 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
552 // required by this protocol
554 InitializeListHead (&mPrivateData
.DataListHead
);
555 InitializeListHead (&mPrivateData
.FilterDriverListHead
);
557 EfiInitializeLock (&mPrivateData
.DataLock
, TPL_NOTIFY
);
560 // Make sure we get a bigger MTC number on every boot!
562 Status
= gRT
->GetNextHighMonotonicCount (&HighMontonicCount
);
563 if (EFI_ERROR (Status
)) {
565 // if system service fails pick a sane value.
567 mPrivateData
.GlobalMonotonicCount
= 0;
569 mPrivateData
.GlobalMonotonicCount
= LShiftU64 ((UINT64
) HighMontonicCount
, 32);
572 // Make a new handle and install the protocol
574 mPrivateData
.Handle
= NULL
;
575 Status
= gBS
->InstallProtocolInterface (
576 &mPrivateData
.Handle
,
577 &gEfiDataHubProtocolGuid
,
578 EFI_NATIVE_INTERFACE
,
579 &mPrivateData
.DataHub