]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
Add CONSTRUCTOR to BaseDebugLibSerialPort to call SerialPortInitialize() in the Seria...
[mirror_edk2.git] / MdePkg / Library / BaseDebugLibSerialPort / DebugLib.c
1 /** @file
2 Base Debug library instance base on Serial Port library.
3 It uses PrintLib to send debug messages to serial port device.
4
5 NOTE: If the Serial Port library enables hardware flow control, then a call
6 to DebugPrint() or DebugAssert() may hang if writes to the serial port are
7 being blocked. This may occur if a key(s) are pressed in a terminal emulator
8 used to monitor the DEBUG() and ASSERT() messages.
9
10 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php.
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include <Base.h>
22 #include <Library/DebugLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/PrintLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/SerialPortLib.h>
28
29 //
30 // Define the maximum debug and assert message length that this library supports
31 //
32 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
33
34 /**
35 The constructor function initialize the Serial Port Library
36
37 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
38
39 **/
40 RETURN_STATUS
41 EFIAPI
42 BaseDebugLibSerialPortConstructor (
43 VOID
44 )
45 {
46 return SerialPortInitialize ();
47 }
48
49 /**
50 Prints a debug message to the debug output device if the specified error level is enabled.
51
52 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
53 the message specified by Format and the associated variable argument list to
54 the debug output device.
55
56 If Format is NULL, then ASSERT().
57
58 @param ErrorLevel The error level of the debug message.
59 @param Format Format string for the debug message to print.
60 @param ... Variable argument list whose contents are accessed
61 based on the format string specified by Format.
62
63 **/
64 VOID
65 EFIAPI
66 DebugPrint (
67 IN UINTN ErrorLevel,
68 IN CONST CHAR8 *Format,
69 ...
70 )
71 {
72 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
73 VA_LIST Marker;
74
75 //
76 // If Format is NULL, then ASSERT().
77 //
78 ASSERT (Format != NULL);
79
80 //
81 // Check driver debug mask value and global mask
82 //
83 if ((ErrorLevel & PcdGet32 (PcdDebugPrintErrorLevel)) == 0) {
84 return;
85 }
86
87 //
88 // Convert the DEBUG() message to an ASCII String
89 //
90 VA_START (Marker, Format);
91 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
92 VA_END (Marker);
93
94 //
95 // Send the print string to a Serial Port
96 //
97 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
98 }
99
100
101 /**
102 Prints an assert message containing a filename, line number, and description.
103 This may be followed by a breakpoint or a dead loop.
104
105 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
106 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
107 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
108 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
109 CpuDeadLoop() is called. If neither of these bits are set, then this function
110 returns immediately after the message is printed to the debug output device.
111 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
112 processing another DebugAssert(), then DebugAssert() must return immediately.
113
114 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
115 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
116
117 @param FileName The pointer to the name of the source file that generated the assert condition.
118 @param LineNumber The line number in the source file that generated the assert condition
119 @param Description The pointer to the description of the assert condition.
120
121 **/
122 VOID
123 EFIAPI
124 DebugAssert (
125 IN CONST CHAR8 *FileName,
126 IN UINTN LineNumber,
127 IN CONST CHAR8 *Description
128 )
129 {
130 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
131
132 //
133 // Generate the ASSERT() message in Ascii format
134 //
135 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT %a(%d): %a\n", FileName, LineNumber, Description);
136
137 //
138 // Send the print string to the Console Output device
139 //
140 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
141
142 //
143 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
144 //
145 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
146 CpuBreakpoint ();
147 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
148 CpuDeadLoop ();
149 }
150 }
151
152
153 /**
154 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
155
156 This function fills Length bytes of Buffer with the value specified by
157 PcdDebugClearMemoryValue, and returns Buffer.
158
159 If Buffer is NULL, then ASSERT().
160 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
161
162 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
163 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
164
165 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
166
167 **/
168 VOID *
169 EFIAPI
170 DebugClearMemory (
171 OUT VOID *Buffer,
172 IN UINTN Length
173 )
174 {
175 //
176 // If Buffer is NULL, then ASSERT().
177 //
178 ASSERT (Buffer != NULL);
179
180 //
181 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
182 //
183 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
184 }
185
186
187 /**
188 Returns TRUE if ASSERT() macros are enabled.
189
190 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
191 PcdDebugProperyMask is set. Otherwise FALSE is returned.
192
193 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
194 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
195
196 **/
197 BOOLEAN
198 EFIAPI
199 DebugAssertEnabled (
200 VOID
201 )
202 {
203 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
204 }
205
206
207 /**
208 Returns TRUE if DEBUG() macros are enabled.
209
210 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
211 PcdDebugProperyMask is set. Otherwise FALSE is returned.
212
213 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
214 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
215
216 **/
217 BOOLEAN
218 EFIAPI
219 DebugPrintEnabled (
220 VOID
221 )
222 {
223 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
224 }
225
226
227 /**
228 Returns TRUE if DEBUG_CODE() macros are enabled.
229
230 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
231 PcdDebugProperyMask is set. Otherwise FALSE is returned.
232
233 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
234 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
235
236 **/
237 BOOLEAN
238 EFIAPI
239 DebugCodeEnabled (
240 VOID
241 )
242 {
243 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
244 }
245
246
247 /**
248 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
249
250 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
251 PcdDebugProperyMask is set. Otherwise FALSE is returned.
252
253 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
254 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
255
256 **/
257 BOOLEAN
258 EFIAPI
259 DebugClearMemoryEnabled (
260 VOID
261 )
262 {
263 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
264 }