3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This implements a status code listener that logs status codes into the data
19 hub. This is only active during non-runtime DXE.
20 The status codes are recorded in a extensiable buffer, and a event is signalled
21 to log them to the data hub. The recorder is the producer of the status code in
22 buffer and the event notify function the consummer.
26 #include "BsDataHubStatusCode.h"
29 // Globals only work at BootService Time. NOT at Runtime!
31 static EFI_DATA_HUB_PROTOCOL
*mDataHub
;
32 static EFI_LIST_ENTRY
*mRecordHead
;
33 static EFI_LIST_ENTRY
*mRecordTail
;
34 static INTN mRecordNum
= 0;
35 static EFI_EVENT mLogDataHubEvent
;
36 static EFI_LOCK mStatusCodeReportLock
= EFI_INITIALIZE_LOCK_VARIABLE(EFI_TPL_HIGH_LEVEL
);
37 static BOOLEAN mEventHandlerActive
= FALSE
;
40 STATUS_CODE_RECORD_LIST
*
41 AllocateRecordBuffer (
48 Allocate a new record list node and initialize it.
49 Inserting the node into the list isn't the task of this function.
57 A pointer to the new allocated node or NULL if non available
61 STATUS_CODE_RECORD_LIST
*DataBuffer
;
65 gBS
->AllocatePool (EfiBootServicesData
, sizeof (STATUS_CODE_RECORD_LIST
), &DataBuffer
);
66 if (DataBuffer
== NULL
) {
70 EfiCommonLibZeroMem (DataBuffer
, sizeof (STATUS_CODE_RECORD_LIST
));
71 DataBuffer
->Signature
= BS_DATA_HUB_STATUS_CODE_SIGNATURE
;
76 DATA_HUB_STATUS_CODE_DATA_RECORD
*
77 AquireEmptyRecordBuffer (
84 Acquire an empty record buffer from the record list if there's free node,
85 or allocate one new node and insert it to the list if the list is full and
86 the function isn't run in EFI_TPL_HIGH_LEVEL.
94 Pointer to new record buffer. NULL if none available.
99 STATUS_CODE_RECORD_LIST
*DataBuffer
;
104 // This function must be reentrant because an event with higher priority may interrupt it
105 // and also report status code.
107 EfiAcquireLock (&mStatusCodeReportLock
);
108 if (mRecordTail
!= mRecordHead
->ForwardLink
) {
109 if (mRecordNum
!= 0) {
110 mRecordHead
= mRecordHead
->ForwardLink
;
112 DataBuffer
= CR (mRecordHead
, STATUS_CODE_RECORD_LIST
, Link
, BS_DATA_HUB_STATUS_CODE_SIGNATURE
);
114 EfiReleaseLock (&mStatusCodeReportLock
);
117 // Initalize the record buffer is the responsibility of the producer,
118 // because the consummer is in a lock so must keep it short.
120 EfiCommonLibZeroMem (&DataBuffer
->RecordBuffer
[0], BYTES_PER_BUFFER
);
121 } else if (mRecordNum
< MAX_RECORD_NUM
) {
123 // The condition of "mRecordNum < MAX_RECORD_NUM" is not promised,
124 // because mRecodeNum may be increased out of this lock.
126 EfiReleaseLock (&mStatusCodeReportLock
);
129 // Can't allocate additional buffer in EFI_TPL_HIGH_LEVEL.
130 // Reporting too many status code in EFI_TPL_HIGH_LEVEL may cause status code lost.
132 OldTpl
= gBS
->RaiseTPL (EFI_TPL_HIGH_LEVEL
);
133 if (OldTpl
== EFI_TPL_HIGH_LEVEL
) {
136 gBS
->RestoreTPL (OldTpl
);
137 DataBuffer
= AllocateRecordBuffer ();
138 if (DataBuffer
== NULL
) {
141 EfiAcquireLock (&mStatusCodeReportLock
);
142 InsertHeadList (mRecordHead
, &DataBuffer
->Link
);
143 mRecordHead
= mRecordHead
->ForwardLink
;
145 EfiReleaseLock (&mStatusCodeReportLock
);
147 EfiReleaseLock (&mStatusCodeReportLock
);
151 return (DATA_HUB_STATUS_CODE_DATA_RECORD
*) DataBuffer
->RecordBuffer
;
155 ReleaseRecordBuffer (
156 IN STATUS_CODE_RECORD_LIST
*RecordBuffer
162 Release a buffer in the list, remove some nodes to keep the list inital length.
166 RecordBuffer - Buffer to release
170 EFI_SUCCESS - If DataRecord is valid
171 EFI_UNSUPPORTED - The record list has empty
175 ASSERT (RecordBuffer
!= NULL
);
178 // The consummer needn't to be reentrient and the producer won't do any meaningful thing
179 // when consummer is logging records.
181 if (mRecordNum
<= 0) {
182 return EFI_UNSUPPORTED
;
183 } else if (mRecordNum
> INITIAL_RECORD_NUM
) {
184 mRecordTail
= mRecordTail
->ForwardLink
;
185 RemoveEntryList (&RecordBuffer
->Link
);
187 gBS
->FreePool (RecordBuffer
);
189 if (mRecordNum
!= 1) {
190 mRecordTail
= mRecordTail
->ForwardLink
;
201 BsDataHubReportStatusCode (
202 IN EFI_STATUS_CODE_TYPE CodeType
,
203 IN EFI_STATUS_CODE_VALUE Value
,
205 IN EFI_GUID
* CallerId
,
206 IN EFI_STATUS_CODE_DATA
* Data OPTIONAL
212 Boot service report status code listener. This function logs the status code
217 Same as gRT->ReportStatusCode (See Tiano Runtime Specification)
225 DATA_HUB_STATUS_CODE_DATA_RECORD
*DataHub
;
230 CHAR16 FormatBuffer
[BYTES_PER_RECORD
];
234 if (EfiAtRuntime ()) {
236 // For now all we do is post code at runtime
241 // If we had an error while in our event handler, then do nothing so
242 // that we don't get in an endless loop.
244 if (mEventHandlerActive
) {
248 DataHub
= (DATA_HUB_STATUS_CODE_DATA_RECORD
*) AquireEmptyRecordBuffer ();
249 if (DataHub
== NULL
) {
251 // There are no empty record buffer in private buffers
253 return EFI_OUT_OF_RESOURCES
;
256 // Construct Data Hub Extended Data
258 DataHub
->CodeType
= CodeType
;
259 DataHub
->Value
= Value
;
260 DataHub
->Instance
= Instance
;
262 if (CallerId
!= NULL
) {
263 EfiCopyMem (&DataHub
->CallerId
, CallerId
, sizeof (EFI_GUID
));
265 EfiZeroMem (&DataHub
->CallerId
, sizeof (EFI_GUID
));
269 EfiZeroMem (&DataHub
->Data
, sizeof (EFI_STATUS_CODE_DATA
));
272 // Copy generic Header
274 EfiCopyMem (&DataHub
->Data
, Data
, sizeof (EFI_STATUS_CODE_DATA
));
276 if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
278 // Convert Ascii Format string to Unicode.
280 for (Index
= 0; Format
[Index
] != '\0' && Index
< (BYTES_PER_RECORD
- 1); Index
+= 1) {
281 FormatBuffer
[Index
] = (CHAR16
) Format
[Index
];
284 FormatBuffer
[Index
] = L
'\0';
287 // Put processed string into the buffer
290 (UINT16
*) (DataHub
+ 1),
291 BYTES_PER_RECORD
- (sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD
)),
297 // DATA_HUB_STATUS_CODE_DATA_RECORD followed by VSPrint String Buffer
299 DataHub
->Data
.Size
= (UINT16
) (Index
* sizeof (CHAR16
));
303 // Default behavior is to copy optional data
305 if (Data
->Size
> (BYTES_PER_RECORD
- sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD
))) {
306 DataHub
->Data
.Size
= (UINT16
) (BYTES_PER_RECORD
- sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD
));
309 EfiCopyMem (DataHub
+ 1, Data
+ 1, DataHub
->Data
.Size
);
313 gBS
->SignalEvent (mLogDataHubEvent
);
320 LogDataHubEventHandler (
328 The Event handler which will be notified to log data in Data Hub.
332 Event - Instance of the EFI_EVENT to signal whenever data is
333 available to be logged in the system.
334 Context - Context of the event.
343 DATA_HUB_STATUS_CODE_DATA_RECORD
*DataRecord
;
345 UINT64 DataRecordClass
;
346 EFI_LIST_ENTRY
*Link
;
347 STATUS_CODE_RECORD_LIST
*BufferEntry
;
350 // Set our global flag so we don't recurse if we get an error here.
352 mEventHandlerActive
= TRUE
;
355 // Log DataRecord in Data Hub.
356 // If there are multiple DataRecords, Log all of them.
360 while (mRecordNum
!= 0) {
361 BufferEntry
= CR (Link
, STATUS_CODE_RECORD_LIST
, Link
, BS_DATA_HUB_STATUS_CODE_SIGNATURE
);
362 DataRecord
= (DATA_HUB_STATUS_CODE_DATA_RECORD
*) (BufferEntry
->RecordBuffer
);
363 Link
= Link
->ForwardLink
;
366 // Add in the size of the header we added.
368 Size
= sizeof (DATA_HUB_STATUS_CODE_DATA_RECORD
) + DataRecord
->Data
.Size
;
370 if ((DataRecord
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_PROGRESS_CODE
) {
371 DataRecordClass
= EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
372 } else if ((DataRecord
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) {
373 DataRecordClass
= EFI_DATA_RECORD_CLASS_ERROR
;
374 } else if ((DataRecord
->CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
) {
375 DataRecordClass
= EFI_DATA_RECORD_CLASS_DEBUG
;
378 // Should never get here.
380 DataRecordClass
= EFI_DATA_RECORD_CLASS_DEBUG
|
381 EFI_DATA_RECORD_CLASS_ERROR
|
382 EFI_DATA_RECORD_CLASS_DATA
|
383 EFI_DATA_RECORD_CLASS_PROGRESS_CODE
;
386 if (((DataRecord
->Instance
& EFI_D_ERROR
) != 0) &&
387 (((DataRecord
->Instance
& EFI_D_POOL
) != 0) || ((DataRecord
->Instance
& EFI_D_PAGE
) != 0))
390 // If memory error, do not call LogData ().
392 ErrorPrint (L
"ERROR", "Memory Error\n");
393 Status
= EFI_OUT_OF_RESOURCES
;
396 // We don't log EFI_D_POOL and EFI_D_PAGE debug info to datahub
397 // to avoid recursive logging due to the memory allocation in datahub
399 if (DataRecordClass
!= EFI_DATA_RECORD_CLASS_DEBUG
||
400 ((DataRecord
->Instance
& EFI_D_POOL
) == 0 && (DataRecord
->Instance
& EFI_D_PAGE
) == 0)) {
402 // Log DataRecord in Data Hub
404 Status
= mDataHub
->LogData (
407 &gEfiStatusCodeRuntimeProtocolGuid
,
415 ReleaseRecordBuffer (BufferEntry
);
418 mEventHandlerActive
= FALSE
;
425 BsDataHubInitializeStatusCode (
426 IN EFI_HANDLE ImageHandle
,
427 IN EFI_SYSTEM_TABLE
*SystemTable
433 Install a data hub listener.
437 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
441 EFI_SUCCESS - Logging Hub protocol installed
442 Other - No protocol installed, unload driver.
447 STATUS_CODE_RECORD_LIST
*DataBuffer
;
452 Status
= gBS
->LocateProtocol (&gEfiDataHubProtocolGuid
, NULL
, &mDataHub
);
454 // Should never fail due to dependency grammer
456 ASSERT_EFI_ERROR (Status
);
459 // Initialize a record list with length not greater than INITIAL_RECORD_NUM.
460 // If no buffer can be allocated, return EFI_OUT_OF_RESOURCES.
462 DataBuffer
= AllocateRecordBuffer ();
463 if (DataBuffer
== NULL
) {
464 return EFI_OUT_OF_RESOURCES
;
466 mRecordHead
= &DataBuffer
->Link
;
467 mRecordTail
= mRecordHead
;
468 InitializeListHead (mRecordHead
);
470 for (Index1
= 1; Index1
< INITIAL_RECORD_NUM
; Index1
++) {
471 DataBuffer
= AllocateRecordBuffer ();
472 if (DataBuffer
== NULL
) {
475 InsertHeadList (mRecordHead
, &DataBuffer
->Link
);
480 // Create a Notify Event to log data in Data Hub
482 Status
= gBS
->CreateEvent (
483 EFI_EVENT_NOTIFY_SIGNAL
,
485 LogDataHubEventHandler
,