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