]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/DataHubDxe/DataHub.c
dad529bd38bf7f936ebfb0fa429ccd54d8b5a81b
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / DataHubDxe / DataHub.c
1 /** @file
2 This code produces the Data Hub protocol. It preloads the data hub
3 with status information copied in from PEI HOBs.
4
5 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 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
10
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.
13
14 **/
15
16 #include "DataHub.h"
17
18 CONST EFI_GUID gZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
19
20 //
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.
23 //
24 DATA_HUB_INSTANCE mPrivateData;
25
26 /**
27 Log data record into the data logging hub
28
29 @param This Protocol instance structure
30 @param DataRecordGuid GUID that defines record contents
31 @param ProducerName GUID that defines the name of the producer of the data
32 @param DataRecordClass Class that defines generic record type
33 @param RawData Data Log record as defined by DataRecordGuid
34 @param RawDataSize Size of Data Log data in bytes
35
36 @retval EFI_SUCCESS If data was logged
37 @retval EFI_OUT_OF_RESOURCES If data was not logged due to lack of system
38 resources.
39 **/
40 EFI_STATUS
41 EFIAPI
42 DataHubLogData (
43 IN EFI_DATA_HUB_PROTOCOL *This,
44 IN EFI_GUID *DataRecordGuid,
45 IN EFI_GUID *ProducerName,
46 IN UINT64 DataRecordClass,
47 IN VOID *RawData,
48 IN UINT32 RawDataSize
49 )
50 {
51 EFI_STATUS Status;
52 DATA_HUB_INSTANCE *Private;
53 EFI_DATA_ENTRY *LogEntry;
54 UINT32 TotalSize;
55 UINT32 RecordSize;
56 EFI_DATA_RECORD_HEADER *Record;
57 VOID *Raw;
58 DATA_HUB_FILTER_DRIVER *FilterEntry;
59 LIST_ENTRY *Link;
60 LIST_ENTRY *Head;
61
62 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
63
64 //
65 // Combine the storage for the internal structs and a copy of the log record.
66 // Record follows PrivateLogEntry. The consumer will be returned a pointer
67 // to Record so we don't what it to be the thing that was allocated from
68 // pool, so the consumer can't free an data record by mistake.
69 //
70 RecordSize = sizeof (EFI_DATA_RECORD_HEADER) + RawDataSize;
71 TotalSize = sizeof (EFI_DATA_ENTRY) + RecordSize;
72
73 //
74 // The Logging action is the critical section, so it is locked.
75 // The MTC asignment & update, time, and logging must be an
76 // atomic operation, so use the lock.
77 //
78 Status = EfiAcquireLockOrFail (&Private->DataLock);
79 if (EFI_ERROR (Status)) {
80 //
81 // Reentrancy detected so exit!
82 //
83 return Status;
84 }
85
86 LogEntry = AllocatePool (TotalSize);
87
88 if (LogEntry == NULL) {
89 EfiReleaseLock (&Private->DataLock);
90 return EFI_OUT_OF_RESOURCES;
91 }
92
93 ZeroMem (LogEntry, TotalSize);
94
95 Record = (EFI_DATA_RECORD_HEADER *) (LogEntry + 1);
96 Raw = (VOID *) (Record + 1);
97
98 //
99 // Build Standard Log Header
100 //
101 Record->Version = EFI_DATA_RECORD_HEADER_VERSION;
102 Record->HeaderSize = (UINT16) sizeof (EFI_DATA_RECORD_HEADER);
103 Record->RecordSize = RecordSize;
104 CopyMem (&Record->DataRecordGuid, DataRecordGuid, sizeof (EFI_GUID));
105 CopyMem (&Record->ProducerName, ProducerName, sizeof (EFI_GUID));
106 Record->DataRecordClass = DataRecordClass;
107
108 //
109 // Ensure LogMonotonicCount is not zero
110 //
111 Record->LogMonotonicCount = ++Private->GlobalMonotonicCount;
112
113 gRT->GetTime (&Record->LogTime, NULL);
114
115 //
116 // Insert log into the internal linked list.
117 //
118 LogEntry->Signature = EFI_DATA_ENTRY_SIGNATURE;
119 LogEntry->Record = Record;
120 LogEntry->RecordSize = sizeof (EFI_DATA_ENTRY) + RawDataSize;
121 InsertTailList (&Private->DataListHead, &LogEntry->Link);
122
123 CopyMem (Raw, RawData, RawDataSize);
124
125 EfiReleaseLock (&Private->DataLock);
126
127 //
128 // Send Signal to all the filter drivers which are interested
129 // in the record's class and guid.
130 //
131 Head = &Private->FilterDriverListHead;
132 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
133 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
134 if (((FilterEntry->ClassFilter & DataRecordClass) != 0) &&
135 (CompareGuid (&FilterEntry->FilterDataRecordGuid, &gZeroGuid) ||
136 CompareGuid (&FilterEntry->FilterDataRecordGuid, DataRecordGuid))) {
137 gBS->SignalEvent (FilterEntry->Event);
138 }
139 }
140
141 return EFI_SUCCESS;
142 }
143
144 /**
145 Search the Head doubly linked list for the passed in MTC. Return the
146 matching element in Head and the MTC on the next entry.
147
148 @param Head Head of Data Log linked list.
149 @param ClassFilter Only match the MTC if it is in the same Class as the
150 ClassFilter.
151 @param PtrCurrentMTC On IN contians MTC to search for. On OUT contians next
152 MTC in the data log list or zero if at end of the list.
153
154 @retval EFI_DATA_LOG_ENTRY Return pointer to data log data from Head list.
155 @retval NULL If no data record exists.
156
157 **/
158 EFI_DATA_RECORD_HEADER *
159 GetNextDataRecord (
160 IN LIST_ENTRY *Head,
161 IN UINT64 ClassFilter,
162 IN OUT UINT64 *PtrCurrentMTC
163 )
164
165 {
166 EFI_DATA_ENTRY *LogEntry;
167 LIST_ENTRY *Link;
168 BOOLEAN ReturnFirstEntry;
169 EFI_DATA_RECORD_HEADER *Record;
170 EFI_DATA_ENTRY *NextLogEntry;
171
172 //
173 // If MonotonicCount == 0 just return the first one
174 //
175 ReturnFirstEntry = (BOOLEAN) (*PtrCurrentMTC == 0);
176
177 Record = NULL;
178 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
179 LogEntry = DATA_ENTRY_FROM_LINK (Link);
180 if ((LogEntry->Record->DataRecordClass & ClassFilter) == 0) {
181 //
182 // Skip any entry that does not have the correct ClassFilter
183 //
184 continue;
185 }
186
187 if ((LogEntry->Record->LogMonotonicCount == *PtrCurrentMTC) || ReturnFirstEntry) {
188 //
189 // Return record to the user
190 //
191 Record = LogEntry->Record;
192
193 //
194 // Calculate the next MTC value. If there is no next entry set
195 // MTC to zero.
196 //
197 *PtrCurrentMTC = 0;
198 for (Link = GetNextNode(Head, Link); Link != Head; Link = GetNextNode(Head, Link)) {
199 NextLogEntry = DATA_ENTRY_FROM_LINK (Link);
200 if ((NextLogEntry->Record->DataRecordClass & ClassFilter) != 0) {
201 //
202 // Return the MTC of the next thing to search for if found
203 //
204 *PtrCurrentMTC = NextLogEntry->Record->LogMonotonicCount;
205 break;
206 }
207 }
208 //
209 // Record found exit loop and return
210 //
211 break;
212 }
213 }
214
215 return Record;
216 }
217
218 /**
219 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
220 represents Event and return it.
221
222 @param Head Pointer to head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER structures.
223 @param Event Event to be search for in the Head list.
224
225 @retval EFI_DATA_HUB_FILTER_DRIVER Returned if Event stored in the Head doubly linked list.
226 @retval NULL If Event is not in the list
227
228 **/
229 DATA_HUB_FILTER_DRIVER *
230 FindFilterDriverByEvent (
231 IN LIST_ENTRY *Head,
232 IN EFI_EVENT Event
233 )
234 {
235 DATA_HUB_FILTER_DRIVER *FilterEntry;
236 LIST_ENTRY *Link;
237
238 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
239 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
240 if (FilterEntry->Event == Event) {
241 return FilterEntry;
242 }
243 }
244
245 return NULL;
246 }
247
248 /**
249
250 Get a previously logged data record and the MonotonicCount for the next
251 availible Record. This allows all records or all records later
252 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
253 is passed in with a MonotonicCout of zero return the first record
254 not yet read by the filter driver. If FilterDriverEvent is NULL and
255 MonotonicCount is zero return the first data record.
256
257 @param This Pointer to the EFI_DATA_HUB_PROTOCOL instance.
258 @param MonotonicCount Specifies the Record to return. On input, zero means
259 return the first record. On output, contains the next
260 record to availible. Zero indicates no more records.
261 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
262 of zero, it means to return the first data record.
263 If FilterDriverEvent is passed in, then a MonotonicCount
264 of zero means to return the first data not yet read by
265 FilterDriverEvent.
266 @param Record Returns a dynamically allocated memory buffer with a data
267 record that matches MonotonicCount.
268
269 @retval EFI_SUCCESS Data was returned in Record.
270 @retval EFI_INVALID_PARAMETER FilterDriverEvent was passed in but does not exist.
271 @retval EFI_NOT_FOUND MonotonicCount does not match any data record in the
272 system. If a MonotonicCount of zero was passed in, then
273 no data records exist in the system.
274 @retval EFI_OUT_OF_RESOURCES Record was not returned due to lack of system resources.
275
276 **/
277 EFI_STATUS
278 EFIAPI
279 DataHubGetNextRecord (
280 IN EFI_DATA_HUB_PROTOCOL *This,
281 IN OUT UINT64 *MonotonicCount,
282 IN EFI_EVENT *FilterDriverEvent, OPTIONAL
283 OUT EFI_DATA_RECORD_HEADER **Record
284 )
285 {
286 DATA_HUB_INSTANCE *Private;
287 DATA_HUB_FILTER_DRIVER *FilterDriver;
288 UINT64 ClassFilter;
289 UINT64 FilterMonotonicCount;
290
291 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
292
293 FilterDriver = NULL;
294 FilterMonotonicCount = 0;
295 ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
296 EFI_DATA_RECORD_CLASS_ERROR |
297 EFI_DATA_RECORD_CLASS_DATA |
298 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
299
300 if (FilterDriverEvent != NULL) {
301 //
302 // For events the beginning is the last unread record. This info is
303 // stored in the instance structure, so we must look up the event
304 // to get the data.
305 //
306 FilterDriver = FindFilterDriverByEvent (
307 &Private->FilterDriverListHead,
308 *FilterDriverEvent
309 );
310 if (FilterDriver == NULL) {
311 return EFI_INVALID_PARAMETER;
312 }
313 //
314 // Use the Class filter the event was created with.
315 //
316 ClassFilter = FilterDriver->ClassFilter;
317
318 if (*MonotonicCount == 0) {
319 //
320 // Use the MTC from the Filter Driver.
321 //
322 FilterMonotonicCount = FilterDriver->GetNextMonotonicCount;
323
324 //
325 // The GetNextMonotonicCount field remembers the last value from the previous time.
326 // But we already processed this vaule, so we need to find the next one.
327 //
328 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, &FilterMonotonicCount);
329 if (FilterMonotonicCount != 0) {
330 *MonotonicCount = FilterMonotonicCount;
331 }
332
333 if ((FilterDriver->GetNextMonotonicCount != 0) && (FilterMonotonicCount == 0)) {
334 //
335 // If there is no new record to get exit now.
336 //
337 *MonotonicCount = 0;
338 return EFI_NOT_FOUND;
339 }
340 }
341 }
342 //
343 // Return the record
344 //
345 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
346 if (*Record == NULL) {
347 return EFI_NOT_FOUND;
348 }
349
350 if (FilterDriver != NULL) {
351 //
352 // If we have a filter driver update the records that have been read.
353 // If MonotonicCount is zero No more reacords left.
354 //
355 if (*MonotonicCount == 0) {
356 //
357 // Save the current Record MonotonicCount.
358 //
359 FilterDriver->GetNextMonotonicCount = (*Record)->LogMonotonicCount;
360 } else {
361 //
362 // Point to next undread record
363 //
364 FilterDriver->GetNextMonotonicCount = *MonotonicCount;
365 }
366 }
367
368 return EFI_SUCCESS;
369 }
370
371 /**
372 This function registers the data hub filter driver that is represented
373 by FilterEvent. Only one instance of each FilterEvent can be registered.
374 After the FilterEvent is registered, it will be signaled so it can sync
375 with data records that have been recorded prior to the FilterEvent being
376 registered.
377
378 @param This Pointer to The EFI_DATA_HUB_PROTOCOL instance.
379 @param FilterEvent The EFI_EVENT to signal whenever data that matches
380 FilterClass is logged in the system.
381 @param FilterTpl The maximum EFI_TPL at which FilterEvent can be
382 signaled. It is strongly recommended that you use the
383 lowest EFI_TPL possible.
384 @param FilterClass FilterEvent will be signaled whenever a bit in
385 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
386 FilterClass. If FilterClass is zero, no class-based
387 filtering will be performed.
388 @param FilterDataRecordGuid FilterEvent will be signaled whenever FilterDataRecordGuid
389 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
390 FilterDataRecordGuid is NULL, then no GUID-based filtering
391 will be performed.
392
393 @retval EFI_SUCCESS The filter driver event was registered.
394 @retval EFI_ALREADY_STARTED FilterEvent was previously registered and cannot be
395 registered again.
396 @retval EFI_OUT_OF_RESOURCES The filter driver event was not registered due to lack of
397 system resources.
398
399 **/
400 EFI_STATUS
401 EFIAPI
402 DataHubRegisterFilterDriver (
403 IN EFI_DATA_HUB_PROTOCOL * This,
404 IN EFI_EVENT FilterEvent,
405 IN EFI_TPL FilterTpl,
406 IN UINT64 FilterClass,
407 IN EFI_GUID * FilterDataRecordGuid OPTIONAL
408 )
409
410 {
411 DATA_HUB_INSTANCE *Private;
412 DATA_HUB_FILTER_DRIVER *FilterDriver;
413
414 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
415
416 FilterDriver = (DATA_HUB_FILTER_DRIVER *) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER));
417 if (FilterDriver == NULL) {
418 return EFI_OUT_OF_RESOURCES;
419 }
420 //
421 // Initialize filter driver info
422 //
423 FilterDriver->Signature = EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE;
424 FilterDriver->Event = FilterEvent;
425 FilterDriver->Tpl = FilterTpl;
426 FilterDriver->GetNextMonotonicCount = 0;
427 if (FilterClass == 0) {
428 FilterDriver->ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
429 EFI_DATA_RECORD_CLASS_ERROR |
430 EFI_DATA_RECORD_CLASS_DATA |
431 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
432 } else {
433 FilterDriver->ClassFilter = FilterClass;
434 }
435
436 if (FilterDataRecordGuid != NULL) {
437 CopyMem (&FilterDriver->FilterDataRecordGuid, FilterDataRecordGuid, sizeof (EFI_GUID));
438 }
439 //
440 // Search for duplicate entries
441 //
442 if (FindFilterDriverByEvent (&Private->FilterDriverListHead, FilterEvent) != NULL) {
443 FreePool (FilterDriver);
444 return EFI_ALREADY_STARTED;
445 }
446 //
447 // Make insertion an atomic operation with the lock.
448 //
449 EfiAcquireLock (&Private->DataLock);
450 InsertTailList (&Private->FilterDriverListHead, &FilterDriver->Link);
451 EfiReleaseLock (&Private->DataLock);
452
453 //
454 // Signal the Filter driver we just loaded so they will recieve all the
455 // previous history. If we did not signal here we would have to wait until
456 // the next data was logged to get the history. In a case where no next
457 // data was logged we would never get synced up.
458 //
459 gBS->SignalEvent (FilterEvent);
460
461 return EFI_SUCCESS;
462 }
463
464 /**
465 Remove a Filter Driver, so it no longer gets called when data
466 information is logged.
467
468 @param This Protocol instance structure
469
470 @param FilterEvent Event that represents a filter driver that is to be
471 Unregistered.
472
473 @retval EFI_SUCCESS If FilterEvent was unregistered
474 @retval EFI_NOT_FOUND If FilterEvent does not exist
475 **/
476 EFI_STATUS
477 EFIAPI
478 DataHubUnregisterFilterDriver (
479 IN EFI_DATA_HUB_PROTOCOL *This,
480 IN EFI_EVENT FilterEvent
481 )
482 {
483 DATA_HUB_INSTANCE *Private;
484 DATA_HUB_FILTER_DRIVER *FilterDriver;
485
486 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
487
488 //
489 // Search for duplicate entries
490 //
491 FilterDriver = FindFilterDriverByEvent (
492 &Private->FilterDriverListHead,
493 FilterEvent
494 );
495 if (FilterDriver == NULL) {
496 return EFI_NOT_FOUND;
497 }
498 //
499 // Make removal an atomic operation with the lock
500 //
501 EfiAcquireLock (&Private->DataLock);
502 RemoveEntryList (&FilterDriver->Link);
503 EfiReleaseLock (&Private->DataLock);
504
505 return EFI_SUCCESS;
506 }
507
508
509
510 /**
511 Driver's Entry point routine that install Driver to produce Data Hub protocol.
512
513 @param ImageHandle Module's image handle
514 @param SystemTable Pointer of EFI_SYSTEM_TABLE
515
516 @retval EFI_SUCCESS Logging Hub protocol installed
517 @retval Other No protocol installed, unload driver.
518
519 **/
520 EFI_STATUS
521 EFIAPI
522 DataHubInstall (
523 IN EFI_HANDLE ImageHandle,
524 IN EFI_SYSTEM_TABLE *SystemTable
525 )
526 {
527 EFI_STATUS Status;
528 UINT32 HighMontonicCount;
529
530 mPrivateData.Signature = DATA_HUB_INSTANCE_SIGNATURE;
531 mPrivateData.DataHub.LogData = DataHubLogData;
532 mPrivateData.DataHub.GetNextRecord = DataHubGetNextRecord;
533 mPrivateData.DataHub.RegisterFilterDriver = DataHubRegisterFilterDriver;
534 mPrivateData.DataHub.UnregisterFilterDriver = DataHubUnregisterFilterDriver;
535
536 //
537 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
538 // required by this protocol
539 //
540 InitializeListHead (&mPrivateData.DataListHead);
541 InitializeListHead (&mPrivateData.FilterDriverListHead);
542
543 EfiInitializeLock (&mPrivateData.DataLock, TPL_NOTIFY);
544
545 //
546 // Make sure we get a bigger MTC number on every boot!
547 //
548 Status = gRT->GetNextHighMonotonicCount (&HighMontonicCount);
549 if (EFI_ERROR (Status)) {
550 //
551 // if system service fails pick a sane value.
552 //
553 mPrivateData.GlobalMonotonicCount = 0;
554 } else {
555 mPrivateData.GlobalMonotonicCount = LShiftU64 ((UINT64) HighMontonicCount, 32);
556 }
557 //
558 // Make a new handle and install the protocol
559 //
560 mPrivateData.Handle = NULL;
561 Status = gBS->InstallProtocolInterface (
562 &mPrivateData.Handle,
563 &gEfiDataHubProtocolGuid,
564 EFI_NATIVE_INTERFACE,
565 &mPrivateData.DataHub
566 );
567 return Status;
568 }
569