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