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