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