summaryrefslogtreecommitdiff
path: root/Core/EM/AMIDebugRx/binaries/PeiAMIDebugRx/XPORT/XportHdr.c
blob: 56c2ad7f42fc3e6460dd9d31740ddba0b0a24be8 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2009, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
// $Header: /Alaska/BIN/Modules/AMIDebugRx/binaries/PeiAMIDebugRx/XPORT/XportHdr.c 9     4/04/12 4:57p Sudhirv $
//
// $Revision: 9 $
//
// $Date: 4/04/12 4:57p $
//*****************************************************************
//*****************************************************************
//
// Revision History
// ----------------
// $Log: /Alaska/BIN/Modules/AMIDebugRx/binaries/PeiAMIDebugRx/XPORT/XportHdr.c $
// 
// 9     4/04/12 4:57p Sudhirv
// To Avoid Build errors in special cases.
// 
// 8     3/13/12 10:47a Sudhirv
// [TAG]  		EIP60224
// [Category]  	Improvement
// [Description]  Function implement When broken in SMM, show the register
// context for 'outside' SMM.
// 
// [TAG]  		EIP80406
// [Category]  	Improvement
// [Description]  Debugger support for Dynamic pci express base token
// 
// [TAG]  		EIP81423
// [Category]  	Improvement
// [Description]  VeB Debugger - Improve the EFI tab's Handle Display
// feature
// 
// [TAG]  		EIP84201
// [Category]  	Improvement
// [Description]  AMIDebugRx\Debugger Target modules usage of the Port 80
// must be controlable
// 
// 7     7/12/11 5:27p Sudhirv
// [TAG]  		EIP62342
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Target Crashes when Debugger enabled and AMIDebugRx is in
// Checkpoint mode
// [RootCause]  	Debugger is Enabled and AMIDebugRx device is connected in
// checkpoint mode. Target MAchine is unaware of the type of host
// connected in DXE phase.
// [Solution]  	Support to not crash when Debugger enabled and Rx switch
// in Checkpoint Mode.
// 
// 6     3/17/11 8:09a Sudhirv
// Support to Boot with S3 Resume in Core_4.6.5.0
// 
// 5     3/09/11 8:27a Sudhirv
// EIP 53658 - AMIDebugRx\Debugger support for PI 1.1
// 
// 4     2/15/10 8:05a Mallikarjunanv
// Updated after fix for EIP 35101 - Binary Debug Rx module doesn't report
// BIOS TAG correctly.
// 
// 3     9/30/09 3:03p Sudhirv
// For Debugger 1.31.0023_PreAlpha
// 
// 2     7/13/09 2:53p Sudhirv
// Updated with Coding Standards
// 
// 1     7/07/09 4:47p Sudhirv
// Restructure Binaries Created
// 
// 2     5/05/09 8:05p Madhans
// Use the timestamp.h instead of timstamp.mak to avoid the octal issues.
// 
// 1     5/01/09 7:48p Madhans
// AMIDebug Rx Module. Intial Checkin.
// 
// 1     4/29/09 7:52a Sudhirv
// AMI Debug Rx module created
//
// 8     3/27/09 8:43a Sudhirv
// Updated Coding Standards.
//
// 7     8/29/07 12:59p Madhans
// Common code for 4.x and 3.x
//
// 6     6/13/07 3:17p Madhans
// Copyright Year updated.
//
// 5     3/12/07 6:19p Madhans
// !!!coding standards!!!!
//
// 4     1/22/07 11:43a Madhans
// Modification made for Binary Release 1.04.0003.
//
// 4     1/05/07 6:15p Madhan
//
// 3     1/05/07 1:05p Ashrafj
// Latest version 1.04.0001 Beta
// Known issues from previous 1.04.000 Beta release has been fixed.
//
//
//*****************************************************************
//*****************************************************************

//**********************************************************************
//<AMI_FHDR_START>
//
// Name:		XportHdr.c
//
// Description:	File containing the globals for CAR base address & size.
//				Also the code which make use of this so that it can be
//				integrated as it is Debugger eModule of binary.
//
//<AMI_FHDR_END>
//**********************************************************************
#include "efi.h"
#include "Pei.h"

#include "misc.h"
#ifndef	EFIx64
#include "AMIPeiDebug.h"
#else
#include "AMIPeiDebugX64.h"
#include "AsmDbgrx64Libs.h"
#endif
#include "timestamp.h"

#define CONVERT_TO_STRING(a) #a
#define STR(a) CONVERT_TO_STRING(a)
#ifndef DBG_WRITE_IO_80_SUPPORT
#define DBG_WRITE_IO_80_SUPPORT 0
#endif

const char *sTargetProjectTag= STR(TARGET_PROJECT_TAG);

//EIP 62342 - Support to not crash when Debugger enabled and Rx switch in Checkpoint Mode.
UINTN gAMIDebuggerEnabled = REDIRECTION_ONLY_MODE;

volatile UINTN gDbgWriteIO80Support = DBG_WRITE_IO_80_SUPPORT;

typedef struct {
	UINT16		Year;
	UINT8		Month;
	UINT8		Day;
	UINT8		Hour;
	UINT8		Minute;
	UINT8		Second;
} DateTime_T;

VOID AddExtDelay();

//**********************************************************************
//<AMI_SHDR_START>
//
// Name:		PeiDbgDataSection of type PEI_DBG_DATA_SECTION
//
// Description:	The following global data structure is for relocation purpose
//				in order to support debugging after the debugger data section
//				is relocated. This second parameter of the data structure has
//				to be updated by the debugger service PEIM to point to relocated
//				data section into memory.The code section has this module
//				has to relocated before the update of second parameter.
//
//<AMI_SHDR_END>
//**********************************************************************

#if defined (PI_SPECIFICATION_VERSION) && (PI_SPECIFICATION_VERSION >= 0x00010000)
PEI_DBG_DATA_SECTION PeiDbgDataSection = {
	"XPRT",
	NULL
};
#else
PEI_DBG_DATA_SECTION PeiDbgDataSection = {
	"XPRT",
	PEI_DEBUG_DATASECTION_BASEADDRESS
};

#endif

UINTN GetCARBaseAddress()
{
    EFI_PEI_SERVICES          **PeiServices;
    VOID *PeiDbgData; 
    EFI_GUID  mPeiDbgBasePpiGuid = EFI_PEI_DBG_BASEADDRESS_PPI_GUID;
    EFI_STATUS Status;

	if((PeiDbgDataSection.DATABASEADDRESS)==NULL)
	{
		IDTBASE 	Idtr;
#ifndef	EFIx64
    	_asm{
    		sidt fword ptr Idtr
    	}
#else
        GetIdtr(&Idtr);
#endif
        PeiServices = (EFI_PEI_SERVICES **) (*(UINTN*)(Idtr.IDTBaseAddr - sizeof (UINTN)));

        if (PeiServices == NULL)
    		return (Idtr.IDTBaseAddr);	//Point to the area after PEI Services
        else
        {
            Status = (**PeiServices).LocatePpi(
                                     PeiServices,
                                     &mPeiDbgBasePpiGuid,  // GUID
                                     0,               // INSTANCE
                                     NULL,            // EFI_PEI_PPI_DESCRIPTOR
                                     &PeiDbgData     // PPI
                                     );
            if (!EFI_ERROR(Status)) 
                return *((UINTN*)PeiDbgData);
            else
                return (Idtr.IDTBaseAddr);
        }
	}
	else {
		return((UINTN)PeiDbgDataSection.DATABASEADDRESS);
	}
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	IsDbgrInCAR
//
// Description:	Internal Helper function.
//
// Input:		none
//
// Output:		1 for yes & 0 for no (UINT32)
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32	IsDbgrInCAR()
{
#if defined (PI_SPECIFICATION_VERSION) && (PI_SPECIFICATION_VERSION >= 0x00010000)
	if((PeiDbgDataSection.DATABASEADDRESS)==NULL)
#else
	if(PEI_DEBUG_DATA_BASEADDRESS == PEI_DEBUG_DATASECTION_BASEADDRESS)
#endif
		return 1;	//yes
	else
		return 0;	//no
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	IoRead32
//
// Description:	Internal Helper function. Reads 32-bit value from IO port
//
// Input:		UINT16 Port
//
// Output:		UINT32 - port data
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32 IoRead32(UINT16 Port)
{
#ifdef	EFIx64
	return IoReadDword(Port);
#else
	_asm {
		mov dx, Port
		in eax, dx
	}
#endif
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	Stall
//
// Description:	Internal Helper function.
//
// Input:		UINTN Usec (microseconds)
//
// Output:		void
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
VOID Stall (UINTN Usec)
{
    UINTN   Counter = (Usec * 7)/2; // 3.58 count per microsec
    UINTN   i;
    UINT32  Data32;
    UINT32  PrevData;

    PrevData = IoRead32(PM_BASE_ADDRESS + 8);
    for (i=0; i < Counter; ) {
       Data32 = IoRead32(PM_BASE_ADDRESS + 8);
        if (Data32 < PrevData) {        // Reset if there is a overlap
            PrevData=Data32;
            continue;
        }
        i += (Data32 - PrevData);
		PrevData=Data32; // FIX need to find out the real diff betweek different count.
    }
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	AddDelay
//
// Description:	Internal Helper function.
//
// Input:		is currently designed to provide either software delay
//				or through ACPI timer who has fixed frequency.
//
// Output:		void
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
void	AddDelay()
{
	Stall(10*1000);	//for 10 milliseconds
	return;
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	AddUsbDelay
//
// Description:	Internal Helper function.
//
// Input:		is currently designed to provide either software delay
//				or through ACPI timer who has fixed frequency.
//
// Output:		void
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
void	AddUsbDelay()
{
	Stall(2*1000);	//for 2 milliseconds
	return;
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	AddSpecialDelay()
//
// Description:	Internal Helper function.
//
// Input:		void
//
// Output:		void
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
void AddSpecialDelay()
{
#if !PEI_CACHE_ENABLED
	if(IsDbgrInCAR())
		AddExtDelay();
	else{
		UINTN CriticalDelay = 60;
		while(CriticalDelay--)
			AddExtDelay();
	}
#else
  UINTN CriticalDelay = 60;

CriticalDelay = 1; // TESTING...
	while(CriticalDelay--)
		AddExtDelay();
#endif
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	AddExtDelay()
//
// Description:	Internal Helper function.
//
// Input:		void
//
// Output:		void
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>
void AddExtDelay()
{
//	UINT8	i = 35;		//providing 250 ms delay
	UINT8	i = 25;
	i =1; // TESTING...
	while(i--)
		AddDelay();
	return;
}

//<AMI_PHDR_START>
//--------------------------------------------------------------------
// Procedure:	GetBuildTime()
//
// Description:	Sends the Build Time Details
//
// Input:		void*
//
// Output:		void
//
//--------------------------------------------------------------------
//<AMI_PHDR_END>

#define Str2No(A)	(A - '0')
	
#define TwoDigitStr2BCD(String) (Str2No(String[0])*0x10+Str2No(String[1]))

VOID GetBuildTime(void *Buffer)
{
	UINT8 *strYear = FOUR_DIGIT_YEAR;
	UINT8 *strMonth = TWO_DIGIT_MONTH;
	UINT8 *strDay = TWO_DIGIT_DAY;
	UINT8 *strHour = TWO_DIGIT_HOUR;
	UINT8 *strMin = TWO_DIGIT_MINUTE;
	UINT8 *strSec = TWO_DIGIT_SECOND;


	((DateTime_T*)Buffer)->Year=(Str2No(strYear[0])*0x1000+Str2No(strYear[1])*0x100+Str2No(strYear[2])*0x10+Str2No(strYear[3]));
	((DateTime_T*)Buffer)->Month=TwoDigitStr2BCD(strMonth);
	((DateTime_T*)Buffer)->Day=TwoDigitStr2BCD(strDay);
	((DateTime_T*)Buffer)->Hour=TwoDigitStr2BCD(strHour);
	((DateTime_T*)Buffer)->Minute=TwoDigitStr2BCD(strMin);
	((DateTime_T*)Buffer)->Second=TwoDigitStr2BCD(strSec);
	return;
}


//#endif
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2009, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//