1. Clean up DxeNt32OemHookStatusCodeLib.inf and PeiNt32OemHookStatusCodeLib.inf
[mirror_edk2.git] / Nt32Pkg / Library / PeiNt32OemHookStatusCodeLib / Nt32OemHookStatusCodeLib.c
1 /** @file
2 OEM hook status code library functions with no library constructor/destructor
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: Nt32OemHookStatusCodeLib.c
14
15 **/
16
17 //
18 // The package level header files this module uses
19 //
20 #include <FrameworkPei.h>
21 #include <WinNtPeim.h>
22 //
23 // The protocols, PPI and GUID defintions for this module
24 //
25 #include <Guid/StatusCodeDataTypeId.h>
26 #include <Ppi/NtThunk.h>
27 //
28 // The Library classes this module consumes
29 //
30 #include <Library/OemHookStatusCodeLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/PrintLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/ReportStatusCodeLib.h>
35 #include <Library/PeiServicesLib.h>
36
37 //
38 // Cache of WinNtThunk protocol
39 //
40 STATIC
41 EFI_WIN_NT_THUNK_PROTOCOL *mWinNt;
42
43 //
44 // Cache of standard output handle .
45 //
46 STATIC
47 HANDLE mStdOut;
48
49 /**
50
51 Initialize OEM status code device .
52
53 @return Always return EFI_SUCCESS.
54
55 **/
56 EFI_STATUS
57 EFIAPI
58 OemHookStatusCodeInitialize (
59 VOID
60 )
61 {
62 PEI_NT_THUNK_PPI *NtThunkPpi;
63 EFI_STATUS Status;
64
65
66 //
67 // Locate NtThunkPpi for retrieving standard output handle
68 //
69 Status = PeiServicesLocatePpi (
70 &gPeiNtThunkPpiGuid,
71 0,
72 NULL,
73 (VOID **) &NtThunkPpi
74 );
75
76 ASSERT_EFI_ERROR (Status);
77
78 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *) NtThunkPpi->NtThunk ();
79
80
81 //
82 // Cache standard output handle.
83 //
84 mStdOut = mWinNt->GetStdHandle (STD_OUTPUT_HANDLE);
85
86 return EFI_SUCCESS;
87 }
88
89 /**
90 Report status code to OEM device.
91
92 @param CodeType Indicates the type of status code being reported. Type EFI_STATUS_CODE_TYPE is defined in "Related Definitions" below.
93
94 @param Value Describes the current status of a hardware or software entity.
95 This included information about the class and subclass that is used to classify the entity
96 as well as an operation. For progress codes, the operation is the current activity.
97 For error codes, it is the exception. For debug codes, it is not defined at this time.
98 Type EFI_STATUS_CODE_VALUE is defined in "Related Definitions" below.
99 Specific values are discussed in the Intel? Platform Innovation Framework for EFI Status Code Specification.
100
101 @param Instance The enumeration of a hardware or software entity within the system.
102 A system may contain multiple entities that match a class/subclass pairing.
103 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,
104 not meaningful, or not relevant. Valid instance numbers start with 1.
105
106
107 @param CallerId This optional parameter may be used to identify the caller.
108 This parameter allows the status code driver to apply different rules to different callers.
109 Type EFI_GUID is defined in InstallProtocolInterface() in the EFI 1.10 Specification.
110
111
112 @param Data This optional parameter may be used to pass additional data
113
114 @return The function always return EFI_SUCCESS.
115
116 **/
117 EFI_STATUS
118 EFIAPI
119 OemHookStatusCodeReport (
120 IN EFI_STATUS_CODE_TYPE CodeType,
121 IN EFI_STATUS_CODE_VALUE Value,
122 IN UINT32 Instance,
123 IN EFI_GUID *CallerId, OPTIONAL
124 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
125 )
126 {
127 CHAR8 *Filename;
128 CHAR8 *Description;
129 CHAR8 *Format;
130 CHAR8 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE];
131 UINT32 ErrorLevel;
132 UINT32 LineNumber;
133 UINTN CharCount;
134 VA_LIST Marker;
135 EFI_DEBUG_INFO *DebugInfo;
136
137 Buffer[0] = '\0';
138
139 if (Data != NULL &&
140 ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {
141 //
142 // Print ASSERT() information into output buffer.
143 //
144 CharCount = AsciiSPrint (
145 Buffer,
146 EFI_STATUS_CODE_DATA_MAX_SIZE,
147 "\n\rASSERT!: %a (%d): %a\n\r",
148 Filename,
149 LineNumber,
150 Description
151 );
152
153 //
154 // Callout to standard output.
155 //
156 mWinNt->WriteFile (
157 mStdOut,
158 Buffer,
159 CharCount,
160 &CharCount,
161 NULL
162 );
163
164 return EFI_SUCCESS;
165
166 } else if (Data != NULL &&
167 ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
168 //
169 // Print DEBUG() information into output buffer.
170 //
171 CharCount = AsciiVSPrint (
172 Buffer,
173 EFI_STATUS_CODE_DATA_MAX_SIZE,
174 Format,
175 Marker
176 );
177 } else if (Data != NULL &&
178 CompareGuid (&Data->Type, &gEfiStatusCodeSpecificDataGuid) &&
179 (CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {
180 //
181 // Print specific data into output buffer.
182 //
183 DebugInfo = (EFI_DEBUG_INFO *) (Data + 1);
184 Marker = (VA_LIST) (DebugInfo + 1);
185 Format = (CHAR8 *) (((UINT64 *) Marker) + 12);
186
187 CharCount = AsciiVSPrint (Buffer, EFI_STATUS_CODE_DATA_MAX_SIZE, Format, Marker);
188 } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) {
189 //
190 // Print ERROR information into output buffer.
191 //
192 CharCount = AsciiSPrint (
193 Buffer,
194 EFI_STATUS_CODE_DATA_MAX_SIZE,
195 "ERROR: C%x:V%x I%x",
196 CodeType,
197 Value,
198 Instance
199 );
200
201 //
202 // Make sure we don't try to print values that weren't intended to be printed, especially NULL GUID pointers.
203 //
204
205 if (CallerId != NULL) {
206 CharCount += AsciiSPrint (
207 &Buffer[CharCount - 1],
208 (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
209 " %g",
210 CallerId
211 );
212 }
213
214 if (Data != NULL) {
215 CharCount += AsciiSPrint (
216 &Buffer[CharCount - 1],
217 (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
218 " %x",
219 Data
220 );
221 }
222
223 CharCount += AsciiSPrint (
224 &Buffer[CharCount - 1],
225 (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
226 "\n\r"
227 );
228 } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) {
229 CharCount = AsciiSPrint (
230 Buffer,
231 EFI_STATUS_CODE_DATA_MAX_SIZE,
232 "PROGRESS CODE: V%x I%x\n\r",
233 Value,
234 Instance
235 );
236 } else {
237 CharCount = AsciiSPrint (
238 Buffer,
239 EFI_STATUS_CODE_DATA_MAX_SIZE,
240 "Undefined: C%x:V%x I%x\n\r",
241 CodeType,
242 Value,
243 Instance
244 );
245 }
246
247 //
248 // Callout to standard output.
249 //
250 mWinNt->WriteFile (
251 mStdOut,
252 Buffer,
253 CharCount,
254 &CharCount,
255 NULL
256 );
257
258 return EFI_SUCCESS;
259 }
260