summaryrefslogtreecommitdiff
path: root/Library/Debug.c
blob: 02cb692f7d507a82c5a5151c882bd576499b8fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/SOURCE/Core/Library/Debug.c 10    7/30/09 4:39p Vyacheslava $
//
// $Revision: 10 $
//
// $Date: 7/30/09 4:39p $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Library/Debug.c $
// 
// 10    7/30/09 4:39p Vyacheslava
// Minor bug fix. EIP#24453: Synopsis: CPU exception when printing long
// debug messages.
// 
// 9     7/28/09 4:54p Vyacheslava
//  Bug fix. EIP#24453: Synopsis: CPU exception when printing long
// debug messages.
// 
// 8     7/10/09 3:49p Felixp
// Function headers added
// 
// 7     8/24/06 9:26a Felixp
// Preliminary x64 support (work in progress):
// 1. Processor architecture specific functions moved to a processor
// library
// 2. Makefile removed (AmiLib files are build by the AmiPeiLib and
// AmeDxeLib makefile)
// 3. Tokens.c added
// 
// 6     3/13/06 1:49a Felixp
// 
// 5     3/04/05 10:50a Mandal
// 
// 4     1/19/05 5:03p Felixp
// 
// 2     1/18/05 3:22p Felixp
// PrintDebugMessage renamed to Trace
// 
// 1     12/23/04 9:41a Felixp
// 
// 5     11/24/04 9:36a Felixp
// GetCpuTimer added
// 
// 4     10/22/04 8:01p Felixp
// 
// 3     10/22/04 7:42p Felixp
// 
// 2     7/13/04 10:42a Felixp
// 
// 1     7/12/04 6:31p Felixp
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name: Debug.c
//
// Description:
//  Contains generic debug library functions.
//
//<AMI_FHDR_END>
//*************************************************************************
#include <AmiLib.h>
#include <StatusCodes.h>

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: Debug_Functions
//
// Description:
//  Debug related functions defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// AmiPeiLib PEI_TRACE         Macro which creates a serial debug message in PEI.
// AmiDxeLib TRACE             Macro which creates a serial debug message in DXE.
// AmiDxeLib PROGRESS_CODE     Macro to report boot progress information in DXE.
// AmiPeiLib PEI_PROGRESS_CODE Macro report boot progress information in PEI.
// AmiDxeLib ERROR_CODE        Macro to report error condition in DXE.
// AmiPeiLib PEI_ERROR_CODE    Macro to report error condition in PEI.
// AmiLib    checkpoint        Send a checkpoint to port 0x80.  Use PROGRESS_CODE or PEI_PROGRESS_CODE if possible.
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions.  Append .h to the given name.
//   
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: PROGRESS_CODE
//
// Description:	
//  PROGRESS_CODE(Code) is a macro which reports progress code
//    using UEFI ReportStatusCode API.
//
// Input:
//  Code - progress code value. 
//    Standard values are defined in AmiStatusCodes.h
//
// Output:
//  VOID.
//
//<AMI_PHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: PEI_PROGRESS_CODE
//
// Description:	
//  PEI_PROGRESS_CODE(PeiServices, Code) is a macro which reports progress code
//    using (*PeiServices)->ReportStatusCode
//
// Input:
//  PeiServices - Double pointer to PEI Services Table.    
//  Code - progress code value. 
//    Standard values are defined in AmiStatusCodes.h
// 
// Output:
//  VOID.
//
//<AMI_PHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: PEI_TRACE
//
// Description:	
//  PEI_TRACE(Arguments) is a macro which creates a serial debug message in
// PEI if the DEBUG_MODE SDL token is enabled by invoking the PeiTrace
// function with the provided arguments.  If DEBUG_MODE is off, nothing is
// generated.  See notes for an example usage.
//
// Input:
//  Arguments
// A bracket enclosed list including the following input parameters for
// PeiTrace (in order from left to right).
//
//  IN UINTN Level
// The error level of the debug message.
//
//  IN EFI_PEI_SERVICES **ppPS
// Double pointer to PEI Services Table.
//
//  IN CHAR8 *sFormat
// Format string for the debug message to print.
//
//  IN ...
// Additional parameters utilized by the format string.
//
// Output:
//  VOID.
//
// Modified:
//
// Referrals:
//  PeiTrace
//  Sprintf
// 
// Notes:
//  See Sprintf for a detailed description of the format string parameters.
// Example usage:
//
// PEI_TRACE((TRACE_ALWAYS, PeiServices, "This is a test message.  Status
// is %r.\n", Status));
//          
//<AMI_PHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: TRACE
//
// Description:
//  TRACE(Arguments) is a macro which creates a serial debug message in DXE
// if the DEBUG_MODE SDL token is enabled by invoking the Trace function with
// the provided arguments.  If DEBUG_MODE is off, nothing is generated.  See
// notes for an example usage.
//
// Input:
//  Arguments
// A bracket enclosed list including the following input parameters for
// Trace (in order from left to right).
//
//  IN UINTN Level
// The error level of the debug message. 
//
//  IN CHAR8 *sFormat
// Format string for the debug message to print.
//
//  IN ...
// Argument list for the format string.
//
// Output:
//  VOID.
//
// Modified:
//
// Referrals:
//  Trace
//  Sprintf
// 
// Notes:
//  See Sprintf for a detailed description of the format string parameters.
// Example usage:
//
// TRACE((TRACE_ALWAYS, "This is a test message.  Status is %r.\n", Status));
//          
//<AMI_PHDR_END>
//*************************************************************************

//*************************************************************************
//
// Name: PrepareStatusCodeString
//
// Description: Produces a Status Code ASCII string in the output
// buffer.  The ASCII string is produced by parsing the format string 
// specified by format.  Arguments are pulled from the variable argument
// list specified by ArgList based on the contents of the format string.
// This is internal function used by TRACE and PEI_TRACE
//
// Input:
//  pBuffer - Pointer to a EFI_STATUS_CODE_STRING_DATA buffer. User is 
//  responsible for allocating the necessary memory resources!
//  
//  Size - Size of the buffer in bytes.
//
//  sFormat - Pointer to a null-terminated format ASCII string.
//
//  ArgList - Marker for the variable argument list.
//
// Output: VOID
//
// Referrals: Sprintf_va_list
//
//          
//*************************************************************************
VOID PrepareStatusCodeString( VOID* pBuffer, UINTN Size, CHAR8 *sFormat, va_list ArgList )
{
    static CHAR8 OverflowMessageStr[] = "... DEBUG BUFFER OVERFLOW!!!\n";
    UINTN n;

    static EFI_STATUS_CODE_STRING_DATA Header = 
    {
        {
            sizeof(EFI_STATUS_CODE_DATA),
            sizeof(EFI_STATUS_CODE_STRING_DATA) - sizeof(EFI_STATUS_CODE_DATA),
            EFI_STATUS_CODE_DATA_TYPE_STRING_GUID
        }, EfiStringAscii, NULL
    };
    EFI_STATUS_CODE_STRING_DATA *pData = pBuffer;

    //Prepare the data strucutre
    *pData = Header;
    pData->String.Ascii = (CHAR8*)pData + sizeof(EFI_STATUS_CODE_STRING_DATA);

    //Prepare the string
    n = Sprintf_s_va_list( 
        (CHAR8*)(pData + 1), 
        Size - sizeof(EFI_STATUS_CODE_STRING_DATA), 
        sFormat, 
        ArgList 
    );
    if ( n < (Size - sizeof(EFI_STATUS_CODE_STRING_DATA) - 1) || 
         Size < (sizeof(OverflowMessageStr) + sizeof(EFI_STATUS_CODE_STRING_DATA)) )
        return;

    // Buffer Overflow!
    // Cut of the previous debug message plus error message
    MemCpy(
        (CHAR8*)pBuffer + Size - sizeof(OverflowMessageStr),
        OverflowMessageStr, 
        sizeof(OverflowMessageStr)
    );
}

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************