]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Library/EdkUefiDebugLibStdErr/DebugLib.c
Initial import.
[mirror_edk2.git] / EdkModulePkg / Library / EdkUefiDebugLibStdErr / DebugLib.c
1 /*++
2
3 Copyright (c) 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
8
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.
11
12 Module Name:
13
14 DebugLib.c
15
16 Abstract:
17
18 UEFI Debug Library that uses PrintLib to send messages to STDERR
19
20 --*/
21
22 static BOOLEAN mDebugLevelInstalled = FALSE;
23 static EFI_DEBUG_LEVEL_PROTOCOL mDebugLevel = { 0 };
24
25 EFI_STATUS
26 DebugLibConstructor (
27 IN EFI_HANDLE ImageHandle,
28 IN EFI_SYSTEM_TABLE *SystemTable
29 )
30 /*++
31
32 Routine Description:
33
34 Arguments:
35
36 Returns:
37
38 --*/
39 {
40 EFI_STATUS Status;
41
42 //
43 // Initialize Debug Level Protocol
44 //
45 mDebugLevel.DebugLevel = PcdGet32(PcdDebugPrintErrorLevel);
46
47 //
48 // Install Debug Level Protocol
49 //
50 Status = gBS->InstallMultipleProtocolInterfaces (
51 &ImageHandle,
52 &gEfiDebugLevelProtocolGuid, &mDebugLevel,
53 NULL
54 );
55 ASSERT_EFI_ERROR (Status);
56
57 //
58 // Set flag to show that the Debug Level Protocol has been installed
59 //
60 mDebugLevelInstalled = TRUE;
61
62 return EFI_SUCCESS;
63 }
64
65 VOID
66 EFIAPI
67 DebugPrint (
68 IN UINTN ErrorLevel,
69 IN CHAR8 *Format,
70 ...
71 )
72 /*++
73
74 Routine Description:
75
76 Wrapper for DebugVPrint ()
77
78 Arguments:
79
80 ErrorLevel - If error level is set do the debug print.
81
82 Format - String to use for the print, followed by Print arguments.
83
84 ... - Print arguments.
85
86 Returns:
87
88 None
89
90 --*/
91 {
92 CHAR16 Buffer[0x100];
93 CHAR16 UnicodeBuffer[0x100];
94 UINT32 Index;
95 VA_LIST Marker;
96
97 //
98 // Check to see if STDERR is avilable
99 //
100 if (gST->StdErr == NULL) {
101 return;
102 }
103
104 //
105 // Check driver Debug Level value and global debug level
106 //
107 if (mDebugLevelInstalled) {
108 if ((ErrorLevel & mDebugLevel.DebugLevel) == 0) {
109 return;
110 }
111 } else {
112 if ((ErrorLevel & PcdGet32(PcdDebugPrintErrorLevel)) == 0) {
113 return;
114 }
115 }
116
117 //
118 // BUGBUG: Need print that take CHAR8 Format and returns CHAR16 Buffer
119 //
120 for (Index = 0; Format[Index] != 0; Index++) {
121 UnicodeBuffer[Index] = Format[Index];
122 }
123 UnicodeBuffer[Index] = Format[Index];
124
125 //
126 // Convert the DEBUG() message to a Unicode String
127 //
128 VA_START (Marker, Format);
129 UnicodeVSPrint (Buffer, sizeof (Buffer), UnicodeBuffer, Marker);
130 VA_END (Marker);
131
132 //
133 // Send the print string to the Standard Error device
134 //
135 gST->StdErr->OutputString (gST->StdErr, Buffer);
136 }
137
138 VOID
139 EFIAPI
140 DebugAssert (
141 IN CHAR8 *FileName,
142 IN INTN LineNumber,
143 IN CHAR8 *Description
144 )
145 /*++
146
147 Routine Description:
148
149 Worker function for ASSERT(). If Error Logging hub is loaded log ASSERT
150 information. If Error Logging hub is not loaded CpuBreakpoint ().
151
152 We use UINT64 buffers due to IPF alignment concerns.
153
154 Arguments:
155
156 FileName - File name of failing routine.
157
158 LineNumber - Line number of failing ASSERT().
159
160 Description - Descritption, usally the assertion,
161
162 Returns:
163
164 None
165
166 --*/
167 {
168 CHAR16 Buffer[0x100];
169
170 //
171 // Check to see if STDERR is avilable
172 //
173 if (gST->StdErr == NULL) {
174 return;
175 }
176
177 //
178 // Generate the ASSERT() message in Unicode format
179 //
180 UnicodeSPrint (Buffer, sizeof (Buffer), (CHAR16 *)L"ASSERT %s(%d): %s\n", FileName, LineNumber, Description);
181
182 //
183 // Send the print string to the Standard Error device
184 //
185 gST->StdErr->OutputString (gST->StdErr, Buffer);
186
187 //
188 // Put break point in module that contained the error.
189 //
190 CpuBreakpoint ();
191 }
192
193 /**
194 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
195
196 This function fills Length bytes of Buffer with the value specified by
197 PcdDebugClearMemoryValue, and returns Buffer.
198
199 If Buffer is NULL, then ASSERT().
200
201 If Length is greater than (MAX_ADDRESS \96 Buffer + 1), then ASSERT().
202
203 @param Buffer Pointer to the target buffer to fill with PcdDebugClearMemoryValue.
204 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
205
206 @return Buffer
207
208 **/
209 VOID *
210 EFIAPI
211 DebugClearMemory (
212 OUT VOID *Buffer,
213 IN UINTN Length
214 )
215 {
216 // SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
217 SetMem (Buffer, Length, 0xAF);
218 return Buffer;
219 }
220
221 BOOLEAN
222 EFIAPI
223 DebugAssertEnabled (
224 VOID
225 )
226 {
227 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
228 }
229
230 BOOLEAN
231 EFIAPI
232 DebugPrintEnabled (
233 VOID
234 )
235 {
236 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
237 }
238
239 BOOLEAN
240 EFIAPI
241 DebugCodeEnabled (
242 VOID
243 )
244 {
245 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
246 }
247
248 BOOLEAN
249 EFIAPI
250 DebugClearMemoryEnabled (
251 VOID
252 )
253 {
254 return ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
255 }