Split Nt32OemHookStatusCodeLib to PEI/DXE instances
[mirror_edk2.git] / EdkNt32Pkg / Library / DxeNt32OemHookStatusCodeLib / 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 // Cache of WinNtThunk protocol
19 //
20 STATIC
21 EFI_WIN_NT_THUNK_PROTOCOL *mWinNt;
22
23 //
24 // Cache of standard output handle .
25 //
26 STATIC
27 HANDLE mStdOut;
28
29 /**
30
31 Initialize OEM status code device .
32
33 @return Always return EFI_SUCCESS.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 OemHookStatusCodeInitialize (
39 VOID
40 )
41 {
42 EFI_HOB_GUID_TYPE *GuidHob;
43
44 //
45 // Retrieve WinNtThunkProtocol from GUID'ed HOB
46 //
47 GuidHob = GetFirstGuidHob (&gEfiWinNtThunkProtocolGuid);
48 ASSERT (GuidHob != NULL);
49 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));
50 ASSERT (mWinNt != NULL);
51
52 //
53 // Cache standard output handle.
54 //
55 mStdOut = mWinNt->GetStdHandle (STD_OUTPUT_HANDLE);
56
57 return EFI_SUCCESS;
58 }
59
60 /**
61 Report status code to OEM device.
62
63 @param CodeType Indicates the type of status code being reported. Type EFI_STATUS_CODE_TYPE is defined in "Related Definitions" below.
64
65 @param Value Describes the current status of a hardware or software entity.
66 This included information about the class and subclass that is used to classify the entity
67 as well as an operation. For progress codes, the operation is the current activity.
68 For error codes, it is the exception. For debug codes, it is not defined at this time.
69 Type EFI_STATUS_CODE_VALUE is defined in "Related Definitions" below.
70 Specific values are discussed in the Intel? Platform Innovation Framework for EFI Status Code Specification.
71
72 @param Instance The enumeration of a hardware or software entity within the system.
73 A system may contain multiple entities that match a class/subclass pairing.
74 The instance differentiates between them. An instance of 0 indicates that instance information is unavailable,
75 not meaningful, or not relevant. Valid instance numbers start with 1.
76
77
78 @param CallerId This optional parameter may be used to identify the caller.
79 This parameter allows the status code driver to apply different rules to different callers.
80 Type EFI_GUID is defined in InstallProtocolInterface() in the EFI 1.10 Specification.
81
82
83 @param Data This optional parameter may be used to pass additional data
84
85 @return The function always return EFI_SUCCESS.
86
87 **/
88 EFI_STATUS
89 EFIAPI
90 OemHookStatusCodeReport (
91 IN EFI_STATUS_CODE_TYPE CodeType,
92 IN EFI_STATUS_CODE_VALUE Value,
93 IN UINT32 Instance,
94 IN EFI_GUID *CallerId, OPTIONAL
95 IN EFI_STATUS_CODE_DATA *Data OPTIONAL
96 )
97 {
98 CHAR8 *Filename;
99 CHAR8 *Description;
100 CHAR8 *Format;
101 CHAR8 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE];
102 UINT32 ErrorLevel;
103 UINT32 LineNumber;
104 UINTN CharCount;
105 VA_LIST Marker;
106 EFI_DEBUG_INFO *DebugInfo;
107
108 Buffer[0] = '\0';
109
110 if (Data != NULL &&
111 ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {
112 //
113 // Print ASSERT() information into output buffer.
114 //
115 CharCount = AsciiSPrint (
116 Buffer,
117 EFI_STATUS_CODE_DATA_MAX_SIZE,
118 "\n\rASSERT!: %a (%d): %a\n\r",
119 Filename,
120 LineNumber,
121 Description
122 );
123
124 //
125 // Callout to standard output.
126 //
127 mWinNt->WriteFile (
128 mStdOut,
129 Buffer,
130 CharCount,
131 &CharCount,
132 NULL
133 );
134
135 return EFI_SUCCESS;
136
137 } else if (Data != NULL &&
138 ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {
139 //
140 // Print DEBUG() information into output buffer.
141 //
142 CharCount = AsciiVSPrint (
143 Buffer,
144 EFI_STATUS_CODE_DATA_MAX_SIZE,
145 Format,
146 Marker
147 );
148 } else if (Data != NULL &&
149 CompareGuid (&Data->Type, &gEfiStatusCodeSpecificDataGuid) &&
150 (CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_DEBUG_CODE) {
151 //
152 // Print specific data into output buffer.
153 //
154 DebugInfo = (EFI_DEBUG_INFO *) (Data + 1);
155 Marker = (VA_LIST) (DebugInfo + 1);
156 Format = (CHAR8 *) (((UINT64 *) Marker) + 12);
157
158 CharCount = AsciiVSPrint (Buffer, EFI_STATUS_CODE_DATA_MAX_SIZE, Format, Marker);
159 } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) {
160 //
161 // Print ERROR information into output buffer.
162 //
163 CharCount = AsciiSPrint (
164 Buffer,
165 EFI_STATUS_CODE_DATA_MAX_SIZE,
166 "ERROR: C%x:V%x I%x",
167 CodeType,
168 Value,
169 Instance
170 );
171
172 //
173 // Make sure we don't try to print values that weren't intended to be printed, especially NULL GUID pointers.
174 //
175
176 if (CallerId != NULL) {
177 CharCount += AsciiSPrint (
178 &Buffer[CharCount - 1],
179 (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
180 " %g",
181 CallerId
182 );
183 }
184
185 if (Data != NULL) {
186 CharCount += AsciiSPrint (
187 &Buffer[CharCount - 1],
188 (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
189 " %x",
190 Data
191 );
192 }
193
194 CharCount += AsciiSPrint (
195 &Buffer[CharCount - 1],
196 (EFI_STATUS_CODE_DATA_MAX_SIZE - (sizeof (Buffer[0]) * CharCount)),
197 "\n\r"
198 );
199 } else if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_PROGRESS_CODE) {
200 CharCount = AsciiSPrint (
201 Buffer,
202 EFI_STATUS_CODE_DATA_MAX_SIZE,
203 "PROGRESS CODE: V%x I%x\n\r",
204 Value,
205 Instance
206 );
207 } else {
208 CharCount = AsciiSPrint (
209 Buffer,
210 EFI_STATUS_CODE_DATA_MAX_SIZE,
211 "Undefined: C%x:V%x I%x\n\r",
212 CodeType,
213 Value,
214 Instance
215 );
216 }
217
218 //
219 // Callout to standard output.
220 //
221 mWinNt->WriteFile (
222 mStdOut,
223 Buffer,
224 CharCount,
225 &CharCount,
226 NULL
227 );
228
229 return EFI_SUCCESS;
230 }
231