]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/DataHubDxe/DataHub.c
b58de9db6fe8275ac65e85fbef3f9223f252418c
[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 Only code that implements the Data Hub protocol should go in this file!
6
7 The Term MTC stands for MonoTonicCounter.
8
9 For more information please look at DataHub.doc
10
11 NOTE: For extra security of the log GetNextDataRecord () could return a copy
12 of the data record.
13
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
19
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.
22
23 **/
24
25 #include "DataHub.h"
26
27 CONST EFI_GUID gZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
28
29 //
30 // Module Global:
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.
33 //
34 DATA_HUB_INSTANCE mPrivateData;
35
36 //
37 // Worker functions private to this file
38 //
39 DATA_HUB_FILTER_DRIVER *
40 FindFilterDriverByEvent (
41 IN LIST_ENTRY *Head,
42 IN EFI_EVENT Event
43 );
44
45 EFI_DATA_RECORD_HEADER *
46 GetNextDataRecord (
47 IN LIST_ENTRY *Head,
48 IN UINT64 ClassFilter,
49 IN OUT UINT64 *PtrCurrentMTC
50 );
51
52 /**
53
54 Log data record into the data logging hub
55
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
62
63 @retval EFI_SUCCESS - If data was logged
64 @retval EFI_OUT_OF_RESOURCES - If data was not logged due to lack of system
65 resources.
66 **/
67 EFI_STATUS
68 EFIAPI
69 DataHubLogData (
70 IN EFI_DATA_HUB_PROTOCOL *This,
71 IN EFI_GUID *DataRecordGuid,
72 IN EFI_GUID *ProducerName,
73 IN UINT64 DataRecordClass,
74 IN VOID *RawData,
75 IN UINT32 RawDataSize
76 )
77 {
78 EFI_STATUS Status;
79 DATA_HUB_INSTANCE *Private;
80 EFI_DATA_ENTRY *LogEntry;
81 UINT32 TotalSize;
82 UINT32 RecordSize;
83 EFI_DATA_RECORD_HEADER *Record;
84 VOID *Raw;
85 DATA_HUB_FILTER_DRIVER *FilterEntry;
86 LIST_ENTRY *Link;
87 LIST_ENTRY *Head;
88
89 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
90
91 //
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.
96 //
97 RecordSize = sizeof (EFI_DATA_RECORD_HEADER) + RawDataSize;
98 TotalSize = sizeof (EFI_DATA_ENTRY) + RecordSize;
99
100 //
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.
104 //
105 Status = EfiAcquireLockOrFail (&Private->DataLock);
106 if (EFI_ERROR (Status)) {
107 //
108 // Reentrancy detected so exit!
109 //
110 return Status;
111 }
112
113 LogEntry = AllocatePool (TotalSize);
114
115 if (LogEntry == NULL) {
116 EfiReleaseLock (&Private->DataLock);
117 return EFI_OUT_OF_RESOURCES;
118 }
119
120 ZeroMem (LogEntry, TotalSize);
121
122 Record = (EFI_DATA_RECORD_HEADER *) (LogEntry + 1);
123 Raw = (VOID *) (Record + 1);
124
125 //
126 // Build Standard Log Header
127 //
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;
134
135 //
136 // Ensure LogMonotonicCount is not zero
137 //
138 Record->LogMonotonicCount = ++Private->GlobalMonotonicCount;
139
140 gRT->GetTime (&Record->LogTime, NULL);
141
142 //
143 // Insert log into the internal linked list.
144 //
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);
149
150 CopyMem (Raw, RawData, RawDataSize);
151
152 EfiReleaseLock (&Private->DataLock);
153
154 //
155 // Send Signal to all the filter drivers which are interested
156 // in the record's class and guid.
157 //
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);
165 }
166 }
167
168 return EFI_SUCCESS;
169 }
170
171 /**
172
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.
179
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
188 FilterDriverEvent.
189 @param Record Returns a dynamically allocated memory buffer with a data
190 record that matches MonotonicCount.
191
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.
198
199 **/
200 EFI_STATUS
201 EFIAPI
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
207 )
208 {
209 DATA_HUB_INSTANCE *Private;
210 DATA_HUB_FILTER_DRIVER *FilterDriver;
211 UINT64 ClassFilter;
212 UINT64 FilterMonotonicCount;
213
214 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
215
216 FilterDriver = NULL;
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;
222
223 if (FilterDriverEvent != NULL) {
224 //
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
227 // to get the data.
228 //
229 FilterDriver = FindFilterDriverByEvent (
230 &Private->FilterDriverListHead,
231 *FilterDriverEvent
232 );
233 if (FilterDriver == NULL) {
234 return EFI_INVALID_PARAMETER;
235 }
236 //
237 // Use the Class filter the event was created with.
238 //
239 ClassFilter = FilterDriver->ClassFilter;
240
241 if (*MonotonicCount == 0) {
242 //
243 // Use the MTC from the Filter Driver.
244 //
245 FilterMonotonicCount = FilterDriver->GetNextMonotonicCount;
246 if (FilterMonotonicCount != 0) {
247 //
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.
250 //
251 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, &FilterMonotonicCount);
252 *MonotonicCount = FilterMonotonicCount;
253 if (FilterMonotonicCount == 0) {
254 //
255 // If there is no new record to get exit now.
256 //
257 return EFI_NOT_FOUND;
258 }
259 }
260 }
261 }
262 //
263 // Return the record
264 //
265 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
266 if (*Record == NULL) {
267 return EFI_NOT_FOUND;
268 }
269
270 if (FilterDriver != NULL) {
271 //
272 // If we have a filter driver update the records that have been read.
273 // If MonotonicCount is zero No more reacords left.
274 //
275 if (*MonotonicCount == 0) {
276 //
277 // Save the current Record MonotonicCount.
278 //
279 FilterDriver->GetNextMonotonicCount = (*Record)->LogMonotonicCount;
280 } else {
281 //
282 // Point to next undread record
283 //
284 FilterDriver->GetNextMonotonicCount = *MonotonicCount;
285 }
286 }
287
288 return EFI_SUCCESS;
289 }
290
291 /**
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
296 registered.
297
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
311 will be performed.
312
313 @retval EFI_SUCCESS - The filter driver event was registered.
314 @retval EFI_ALREADY_STARTED - FilterEvent was previously registered and cannot be
315 registered again.
316 @retval EFI_OUT_OF_RESOURCES - The filter driver event was not registered due to lack of
317 system resources.
318
319 **/
320 EFI_STATUS
321 EFIAPI
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
328 )
329
330 {
331 DATA_HUB_INSTANCE *Private;
332 DATA_HUB_FILTER_DRIVER *FilterDriver;
333
334 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
335
336 FilterDriver = (DATA_HUB_FILTER_DRIVER *) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER));
337 if (FilterDriver == NULL) {
338 return EFI_OUT_OF_RESOURCES;
339 }
340 //
341 // Initialize filter driver info
342 //
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;
352 } else {
353 FilterDriver->ClassFilter = FilterClass;
354 }
355
356 if (FilterDataRecordGuid != NULL) {
357 CopyMem (&FilterDriver->FilterDataRecordGuid, FilterDataRecordGuid, sizeof (EFI_GUID));
358 }
359 //
360 // Search for duplicate entries
361 //
362 if (FindFilterDriverByEvent (&Private->FilterDriverListHead, FilterEvent) != NULL) {
363 FreePool (FilterDriver);
364 return EFI_ALREADY_STARTED;
365 }
366 //
367 // Make insertion an atomic operation with the lock.
368 //
369 EfiAcquireLock (&Private->DataLock);
370 InsertTailList (&Private->FilterDriverListHead, &FilterDriver->Link);
371 EfiReleaseLock (&Private->DataLock);
372
373 //
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.
378 //
379 gBS->SignalEvent (FilterEvent);
380
381 return EFI_SUCCESS;
382 }
383
384 /**
385 Remove a Filter Driver, so it no longer gets called when data
386 information is logged.
387
388 @param This - Protocol instance structure
389
390 @param FilterEvent - Event that represents a filter driver that is to be
391 Unregistered.
392
393 @retval EFI_SUCCESS - If FilterEvent was unregistered
394
395 @retval EFI_NOT_FOUND - If FilterEvent does not exist
396
397 **/
398 EFI_STATUS
399 EFIAPI
400 DataHubUnregisterFilterDriver (
401 IN EFI_DATA_HUB_PROTOCOL *This,
402 IN EFI_EVENT FilterEvent
403 )
404 {
405 DATA_HUB_INSTANCE *Private;
406 DATA_HUB_FILTER_DRIVER *FilterDriver;
407
408 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
409
410 //
411 // Search for duplicate entries
412 //
413 FilterDriver = FindFilterDriverByEvent (
414 &Private->FilterDriverListHead,
415 FilterEvent
416 );
417 if (FilterDriver == NULL) {
418 return EFI_NOT_FOUND;
419 }
420 //
421 // Make removal an atomic operation with the lock
422 //
423 EfiAcquireLock (&Private->DataLock);
424 RemoveEntryList (&FilterDriver->Link);
425 EfiReleaseLock (&Private->DataLock);
426
427 return EFI_SUCCESS;
428 }
429
430 /**
431 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
432 represents Event and return it.
433
434 @param Head - Head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER
435 structures.
436
437 @param Event - Event to be search for in the Head list.
438
439 @retval EFI_DATA_HUB_FILTER_DRIVER - Returned if Event stored in the
440 Head doubly linked list.
441
442 @retval NULL - If Event is not in the list
443
444 **/
445 DATA_HUB_FILTER_DRIVER *
446 FindFilterDriverByEvent (
447 IN LIST_ENTRY *Head,
448 IN EFI_EVENT Event
449 )
450
451 {
452 DATA_HUB_FILTER_DRIVER *FilterEntry;
453 LIST_ENTRY *Link;
454
455 for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
456 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
457 if (FilterEntry->Event == Event) {
458 return FilterEntry;
459 }
460 }
461
462 return NULL;
463 }
464
465 /**
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.
468
469 @param Head - Head of Data Log linked list.
470
471 @param ClassFilter - Only match the MTC if it is in the same Class as the
472 ClassFilter.
473
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.
476
477 @retval EFI_DATA_LOG_ENTRY - Return pointer to data log data from Head list.
478
479 @retval NULL - If no data record exists.
480
481 **/
482 EFI_DATA_RECORD_HEADER *
483 GetNextDataRecord (
484 IN LIST_ENTRY *Head,
485 IN UINT64 ClassFilter,
486 IN OUT UINT64 *PtrCurrentMTC
487 )
488
489 {
490 EFI_DATA_ENTRY *LogEntry;
491 LIST_ENTRY *Link;
492 BOOLEAN ReturnFirstEntry;
493 EFI_DATA_RECORD_HEADER *Record;
494 EFI_DATA_ENTRY *NextLogEntry;
495
496 //
497 // If MonotonicCount == 0 just return the first one
498 //
499 ReturnFirstEntry = (BOOLEAN) (*PtrCurrentMTC == 0);
500
501 Record = NULL;
502 for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
503 LogEntry = DATA_ENTRY_FROM_LINK (Link);
504 if ((LogEntry->Record->DataRecordClass & ClassFilter) == 0) {
505 //
506 // Skip any entry that does not have the correct ClassFilter
507 //
508 continue;
509 }
510
511 if ((LogEntry->Record->LogMonotonicCount == *PtrCurrentMTC) || ReturnFirstEntry) {
512 //
513 // Return record to the user
514 //
515 Record = LogEntry->Record;
516
517 //
518 // Calculate the next MTC value. If there is no next entry set
519 // MTC to zero.
520 //
521 *PtrCurrentMTC = 0;
522 for (Link = Link->ForwardLink; Link != Head; Link = Link->ForwardLink) {
523 NextLogEntry = DATA_ENTRY_FROM_LINK (Link);
524 if ((NextLogEntry->Record->DataRecordClass & ClassFilter) != 0) {
525 //
526 // Return the MTC of the next thing to search for if found
527 //
528 *PtrCurrentMTC = NextLogEntry->Record->LogMonotonicCount;
529 break;
530 }
531 }
532 //
533 // Record found exit loop and return
534 //
535 break;
536 }
537 }
538
539 return Record;
540 }
541
542 /**
543
544 Install Driver to produce Data Hub protocol.
545
546 @param ImageHandle Module's image handle
547 @param SystemTable Pointer of EFI_SYSTEM_TABLE
548
549
550 @retval EFI_SUCCESS - Logging Hub protocol installed
551
552 @retval Other - No protocol installed, unload driver.
553
554 **/
555 EFI_STATUS
556 EFIAPI
557 DataHubInstall (
558 IN EFI_HANDLE ImageHandle,
559 IN EFI_SYSTEM_TABLE *SystemTable
560 )
561 {
562 EFI_STATUS Status;
563 UINT32 HighMontonicCount;
564
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;
570
571 //
572 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
573 // required by this protocol
574 //
575 InitializeListHead (&mPrivateData.DataListHead);
576 InitializeListHead (&mPrivateData.FilterDriverListHead);
577
578 EfiInitializeLock (&mPrivateData.DataLock, TPL_NOTIFY);
579
580 //
581 // Make sure we get a bigger MTC number on every boot!
582 //
583 Status = gRT->GetNextHighMonotonicCount (&HighMontonicCount);
584 if (EFI_ERROR (Status)) {
585 //
586 // if system service fails pick a sane value.
587 //
588 mPrivateData.GlobalMonotonicCount = 0;
589 } else {
590 mPrivateData.GlobalMonotonicCount = LShiftU64 ((UINT64) HighMontonicCount, 32);
591 }
592 //
593 // Make a new handle and install the protocol
594 //
595 mPrivateData.Handle = NULL;
596 Status = gBS->InstallProtocolInterface (
597 &mPrivateData.Handle,
598 &gEfiDataHubProtocolGuid,
599 EFI_NATIVE_INTERFACE,
600 &mPrivateData.DataHub
601 );
602 return Status;
603 }
604