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