1 /** @file
2   Data Hub filter driver that takes DEBUG () info from Data Hub and writes it
3   to StdErr if it exists.
4 
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #include <FrameworkDxe.h>
11 #include <Guid/DataHubStatusCodeRecord.h>
12 #include <Guid/StatusCodeDataTypeId.h>
13 #include <Guid/StatusCodeDataTypeDebug.h>
14 #include <Protocol/DataHub.h>
15 #include <Protocol/SimpleTextOut.h>
16 
17 #include <Library/DebugLib.h>
18 #include <Library/UefiDriverEntryPoint.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 
22 EFI_DATA_HUB_PROTOCOL *mDataHub = NULL;
23 
24 EFI_EVENT             mDataHubStdErrEvent;
25 
26 /**
27   Event handler registered with the Data Hub to parse EFI_DEBUG_CODE. This
28   handler reads the Data Hub and sends any DEBUG info to StdErr.
29 
30   @param Event      The event that occured, not used
31   @param Context    DataHub Protocol Pointer
32 **/
33 VOID
34 EFIAPI
DataHubStdErrEventHandler(IN EFI_EVENT Event,IN VOID * Context)35 DataHubStdErrEventHandler (
36   IN EFI_EVENT Event,
37   IN VOID      *Context
38   )
39 {
40   EFI_STATUS                           Status;
41   EFI_DATA_HUB_PROTOCOL                *DataHub;
42   EFI_DATA_RECORD_HEADER               *Record;
43   DATA_HUB_STATUS_CODE_DATA_RECORD     *DataRecord;
44   UINT64                               Mtc;
45   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL      *Sto;
46   INT32                                OldAttribute;
47 
48   DataHub = (EFI_DATA_HUB_PROTOCOL *) Context;
49 
50   //
51   // If StdErr is not yet initialized just return a DEBUG print in the BDS
52   // after consoles are connect will make sure data gets flushed properly
53   // when StdErr is available.
54   //
55   if (gST == NULL) {
56     return ;
57   }
58 
59   if (gST->StdErr == NULL) {
60     return ;
61   }
62 
63   //
64   // Mtc of zero means return the next record that has not been read by the
65   // event handler.
66   //
67   Mtc = 0;
68   do {
69     Status = DataHub->GetNextRecord (DataHub, &Mtc, &mDataHubStdErrEvent, &Record);
70     if (!EFI_ERROR (Status)) {
71       if (CompareGuid (&Record->DataRecordGuid, &gEfiDataHubStatusCodeRecordGuid)) {
72         DataRecord = (DATA_HUB_STATUS_CODE_DATA_RECORD *) (((CHAR8 *) Record) + Record->HeaderSize);
73 
74         if (DataRecord->Data.HeaderSize > 0) {
75           if (CompareGuid (&DataRecord->Data.Type, &gEfiStatusCodeDataTypeDebugGuid)) {
76             //
77             // If the Data record is from a DEBUG () then send it to Standard Error
78             //
79             Sto           = gST->StdErr;
80             OldAttribute  = Sto->Mode->Attribute;
81             Sto->SetAttribute (Sto, EFI_TEXT_ATTR (EFI_MAGENTA, EFI_BLACK));
82             Sto->OutputString (Sto, (CHAR16 *) (DataRecord + 1));
83             Sto->SetAttribute (Sto, OldAttribute);
84           }
85         }
86       }
87     }
88   } while ((Mtc != 0) && !EFI_ERROR (Status));
89 }
90 
91 /**
92   Register an event handler with the Data Hub to parse EFI_DEBUG_CODE. This
93   handler reads the Data Hub and sends any DEBUG info to StdErr.
94 
95   @param ImageHandle                Image handle of this driver.
96   @param SystemTable                Pointer to EFI system table.
97 
98   @retval EFI_SUCCESS               The event handler was registered.
99   @retval EFI_OUT_OF_RESOURCES      The event hadler was not registered due to lack of system resources.
100 **/
101 EFI_STATUS
102 EFIAPI
DataHubStdErrInitialize(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)103 DataHubStdErrInitialize (
104   IN EFI_HANDLE         ImageHandle,
105   IN EFI_SYSTEM_TABLE   *SystemTable
106   )
107 {
108   EFI_STATUS  Status;
109   UINT64      DataClass;
110 
111   gBS->LocateProtocol (&gEfiDataHubProtocolGuid, NULL, (VOID **) &mDataHub);
112   //
113   // Should never fail due to Depex grammer.
114   //
115   ASSERT (mDataHub != NULL);
116 
117   //
118   // Create an event and register it with the filter driver
119   //
120   Status = gBS->CreateEvent (
121                   EVT_NOTIFY_SIGNAL,
122                   TPL_CALLBACK,
123                   DataHubStdErrEventHandler,
124                   mDataHub,
125                   &mDataHubStdErrEvent
126                   );
127   if (EFI_ERROR (Status)) {
128     return Status;
129   }
130 
131   DataClass = EFI_DATA_RECORD_CLASS_DEBUG | EFI_DATA_RECORD_CLASS_ERROR;
132   Status = mDataHub->RegisterFilterDriver (
133                       mDataHub,
134                       mDataHubStdErrEvent,
135                       TPL_CALLBACK,
136                       DataClass,
137                       NULL
138                       );
139   if (EFI_ERROR (Status)) {
140     gBS->CloseEvent (mDataHubStdErrEvent);
141   }
142 
143   return Status;
144 }
145 
146