]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/DataHubDxe/DataHub.c
Reviewed the code comments in the Include/Protocol directory for typos, grammar issue...
[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
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 if (FilterMonotonicCount != 0) {
253 *MonotonicCount = FilterMonotonicCount;
254 }
255
256 if ((FilterDriver->GetNextMonotonicCount != 0) && (FilterMonotonicCount == 0)) {
257 //
258 // If there is no new record to get exit now.
259 //
260 *MonotonicCount = 0;
261 return EFI_NOT_FOUND;
262 }
263 }
264 }
265 //
266 // Return the record
267 //
268 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
269 if (*Record == NULL) {
270 return EFI_NOT_FOUND;
271 }
272
273 if (FilterDriver != NULL) {
274 //
275 // If we have a filter driver update the records that have been read.
276 // If MonotonicCount is zero No more reacords left.
277 //
278 if (*MonotonicCount == 0) {
279 //
280 // Save the current Record MonotonicCount.
281 //
282 FilterDriver->GetNextMonotonicCount = (*Record)->LogMonotonicCount;
283 } else {
284 //
285 // Point to next undread record
286 //
287 FilterDriver->GetNextMonotonicCount = *MonotonicCount;
288 }
289 }
290
291 return EFI_SUCCESS;
292 }
293
294 /**
295 This function registers the data hub filter driver that is represented
296 by FilterEvent. Only one instance of each FilterEvent can be registered.
297 After the FilterEvent is registered, it will be signaled so it can sync
298 with data records that have been recorded prior to the FilterEvent being
299 registered.
300
301 @param This - The EFI_DATA_HUB_PROTOCOL instance.
302 @param FilterEvent - The EFI_EVENT to signal whenever data that matches
303 FilterClass is logged in the system.
304 @param FilterTpl - The maximum EFI_TPL at which FilterEvent can be
305 signaled. It is strongly recommended that you use the
306 lowest EFI_TPL possible.
307 @param FilterClass - FilterEvent will be signaled whenever a bit in
308 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
309 FilterClass. If FilterClass is zero, no class-based
310 filtering will be performed.
311 @param FilterDataRecordGuid - FilterEvent will be signaled whenever FilterDataRecordGuid
312 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
313 FilterDataRecordGuid is NULL, then no GUID-based filtering
314 will be performed.
315
316 @retval EFI_SUCCESS - The filter driver event was registered.
317 @retval EFI_ALREADY_STARTED - FilterEvent was previously registered and cannot be
318 registered again.
319 @retval EFI_OUT_OF_RESOURCES - The filter driver event was not registered due to lack of
320 system resources.
321
322 **/
323 EFI_STATUS
324 EFIAPI
325 DataHubRegisterFilterDriver (
326 IN EFI_DATA_HUB_PROTOCOL * This,
327 IN EFI_EVENT FilterEvent,
328 IN EFI_TPL FilterTpl,
329 IN UINT64 FilterClass,
330 IN EFI_GUID * FilterDataRecordGuid OPTIONAL
331 )
332
333 {
334 DATA_HUB_INSTANCE *Private;
335 DATA_HUB_FILTER_DRIVER *FilterDriver;
336
337 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
338
339 FilterDriver = (DATA_HUB_FILTER_DRIVER *) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER));
340 if (FilterDriver == NULL) {
341 return EFI_OUT_OF_RESOURCES;
342 }
343 //
344 // Initialize filter driver info
345 //
346 FilterDriver->Signature = EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE;
347 FilterDriver->Event = FilterEvent;
348 FilterDriver->Tpl = FilterTpl;
349 FilterDriver->GetNextMonotonicCount = 0;
350 if (FilterClass == 0) {
351 FilterDriver->ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
352 EFI_DATA_RECORD_CLASS_ERROR |
353 EFI_DATA_RECORD_CLASS_DATA |
354 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
355 } else {
356 FilterDriver->ClassFilter = FilterClass;
357 }
358
359 if (FilterDataRecordGuid != NULL) {
360 CopyMem (&FilterDriver->FilterDataRecordGuid, FilterDataRecordGuid, sizeof (EFI_GUID));
361 }
362 //
363 // Search for duplicate entries
364 //
365 if (FindFilterDriverByEvent (&Private->FilterDriverListHead, FilterEvent) != NULL) {
366 FreePool (FilterDriver);
367 return EFI_ALREADY_STARTED;
368 }
369 //
370 // Make insertion an atomic operation with the lock.
371 //
372 EfiAcquireLock (&Private->DataLock);
373 InsertTailList (&Private->FilterDriverListHead, &FilterDriver->Link);
374 EfiReleaseLock (&Private->DataLock);
375
376 //
377 // Signal the Filter driver we just loaded so they will recieve all the
378 // previous history. If we did not signal here we would have to wait until
379 // the next data was logged to get the history. In a case where no next
380 // data was logged we would never get synced up.
381 //
382 gBS->SignalEvent (FilterEvent);
383
384 return EFI_SUCCESS;
385 }
386
387 /**
388 Remove a Filter Driver, so it no longer gets called when data
389 information is logged.
390
391 @param This - Protocol instance structure
392
393 @param FilterEvent - Event that represents a filter driver that is to be
394 Unregistered.
395
396 @retval EFI_SUCCESS - If FilterEvent was unregistered
397
398 @retval EFI_NOT_FOUND - If FilterEvent does not exist
399
400 **/
401 EFI_STATUS
402 EFIAPI
403 DataHubUnregisterFilterDriver (
404 IN EFI_DATA_HUB_PROTOCOL *This,
405 IN EFI_EVENT FilterEvent
406 )
407 {
408 DATA_HUB_INSTANCE *Private;
409 DATA_HUB_FILTER_DRIVER *FilterDriver;
410
411 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
412
413 //
414 // Search for duplicate entries
415 //
416 FilterDriver = FindFilterDriverByEvent (
417 &Private->FilterDriverListHead,
418 FilterEvent
419 );
420 if (FilterDriver == NULL) {
421 return EFI_NOT_FOUND;
422 }
423 //
424 // Make removal an atomic operation with the lock
425 //
426 EfiAcquireLock (&Private->DataLock);
427 RemoveEntryList (&FilterDriver->Link);
428 EfiReleaseLock (&Private->DataLock);
429
430 return EFI_SUCCESS;
431 }
432
433 /**
434 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
435 represents Event and return it.
436
437 @param Head - Head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER
438 structures.
439
440 @param Event - Event to be search for in the Head list.
441
442 @retval EFI_DATA_HUB_FILTER_DRIVER - Returned if Event stored in the
443 Head doubly linked list.
444
445 @retval NULL - If Event is not in the list
446
447 **/
448 DATA_HUB_FILTER_DRIVER *
449 FindFilterDriverByEvent (
450 IN LIST_ENTRY *Head,
451 IN EFI_EVENT Event
452 )
453
454 {
455 DATA_HUB_FILTER_DRIVER *FilterEntry;
456 LIST_ENTRY *Link;
457
458 for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
459 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
460 if (FilterEntry->Event == Event) {
461 return FilterEntry;
462 }
463 }
464
465 return NULL;
466 }
467
468 /**
469 Search the Head doubly linked list for the passed in MTC. Return the
470 matching element in Head and the MTC on the next entry.
471
472 @param Head - Head of Data Log linked list.
473
474 @param ClassFilter - Only match the MTC if it is in the same Class as the
475 ClassFilter.
476
477 @param PtrCurrentMTC - On IN contians MTC to search for. On OUT contians next
478 MTC in the data log list or zero if at end of the list.
479
480 @retval EFI_DATA_LOG_ENTRY - Return pointer to data log data from Head list.
481
482 @retval NULL - If no data record exists.
483
484 **/
485 EFI_DATA_RECORD_HEADER *
486 GetNextDataRecord (
487 IN LIST_ENTRY *Head,
488 IN UINT64 ClassFilter,
489 IN OUT UINT64 *PtrCurrentMTC
490 )
491
492 {
493 EFI_DATA_ENTRY *LogEntry;
494 LIST_ENTRY *Link;
495 BOOLEAN ReturnFirstEntry;
496 EFI_DATA_RECORD_HEADER *Record;
497 EFI_DATA_ENTRY *NextLogEntry;
498
499 //
500 // If MonotonicCount == 0 just return the first one
501 //
502 ReturnFirstEntry = (BOOLEAN) (*PtrCurrentMTC == 0);
503
504 Record = NULL;
505 for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink) {
506 LogEntry = DATA_ENTRY_FROM_LINK (Link);
507 if ((LogEntry->Record->DataRecordClass & ClassFilter) == 0) {
508 //
509 // Skip any entry that does not have the correct ClassFilter
510 //
511 continue;
512 }
513
514 if ((LogEntry->Record->LogMonotonicCount == *PtrCurrentMTC) || ReturnFirstEntry) {
515 //
516 // Return record to the user
517 //
518 Record = LogEntry->Record;
519
520 //
521 // Calculate the next MTC value. If there is no next entry set
522 // MTC to zero.
523 //
524 *PtrCurrentMTC = 0;
525 for (Link = Link->ForwardLink; Link != Head; Link = Link->ForwardLink) {
526 NextLogEntry = DATA_ENTRY_FROM_LINK (Link);
527 if ((NextLogEntry->Record->DataRecordClass & ClassFilter) != 0) {
528 //
529 // Return the MTC of the next thing to search for if found
530 //
531 *PtrCurrentMTC = NextLogEntry->Record->LogMonotonicCount;
532 break;
533 }
534 }
535 //
536 // Record found exit loop and return
537 //
538 break;
539 }
540 }
541
542 return Record;
543 }
544
545 /**
546
547 Install Driver to produce Data Hub protocol.
548
549 @param ImageHandle Module's image handle
550 @param SystemTable Pointer of EFI_SYSTEM_TABLE
551
552
553 @retval EFI_SUCCESS - Logging Hub protocol installed
554
555 @retval Other - No protocol installed, unload driver.
556
557 **/
558 EFI_STATUS
559 EFIAPI
560 DataHubInstall (
561 IN EFI_HANDLE ImageHandle,
562 IN EFI_SYSTEM_TABLE *SystemTable
563 )
564 {
565 EFI_STATUS Status;
566 UINT32 HighMontonicCount;
567
568 mPrivateData.Signature = DATA_HUB_INSTANCE_SIGNATURE;
569 mPrivateData.DataHub.LogData = DataHubLogData;
570 mPrivateData.DataHub.GetNextRecord = DataHubGetNextRecord;
571 mPrivateData.DataHub.RegisterFilterDriver = DataHubRegisterFilterDriver;
572 mPrivateData.DataHub.UnregisterFilterDriver = DataHubUnregisterFilterDriver;
573
574 //
575 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
576 // required by this protocol
577 //
578 InitializeListHead (&mPrivateData.DataListHead);
579 InitializeListHead (&mPrivateData.FilterDriverListHead);
580
581 EfiInitializeLock (&mPrivateData.DataLock, TPL_NOTIFY);
582
583 //
584 // Make sure we get a bigger MTC number on every boot!
585 //
586 Status = gRT->GetNextHighMonotonicCount (&HighMontonicCount);
587 if (EFI_ERROR (Status)) {
588 //
589 // if system service fails pick a sane value.
590 //
591 mPrivateData.GlobalMonotonicCount = 0;
592 } else {
593 mPrivateData.GlobalMonotonicCount = LShiftU64 ((UINT64) HighMontonicCount, 32);
594 }
595 //
596 // Make a new handle and install the protocol
597 //
598 mPrivateData.Handle = NULL;
599 Status = gBS->InstallProtocolInterface (
600 &mPrivateData.Handle,
601 &gEfiDataHubProtocolGuid,
602 EFI_NATIVE_INTERFACE,
603 &mPrivateData.DataHub
604 );
605 return Status;
606 }
607