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