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