summaryrefslogtreecommitdiff
path: root/Core/EM/Sredir/LegacySmmSredir.c
blob: bd167e58f40dedf76cb411cc820162cbff8efa97 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/Legacy Serial Redirection/LegacySmmSredir.c 6     12/08/14 3:36a Anbuprakashp $
//
// $Revision: 6 $
//
// $Date: 12/08/14 3:36a $
//****************************************************************************
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/Legacy Serial Redirection/LegacySmmSredir.c $
// 
// 6     12/08/14 3:36a Anbuprakashp
// [TAG]  		EIP192305
// [Category]  	Improvement
// [Description]  	Replacing SmmGetMemoryType usage in LegacySredir module
// with AmiBufferValidationLib
// [Files]  		LegacySredir.mak, LegacySmmSredir.c, LegacySredir_Setup.C
// 
// 5     6/25/13 4:40a Rameshr
// [TAG]  		EIP125655
// [Category]  	Improvement
// [Description]  	MMIO Address validated before reading or writing to the
// MMIO region in the SW SMI handler
// [Files]  		LegacySmmSredir.c, LegacySredir.dxs
// 
// 4     4/10/12 12:49a Rameshr
// [TAG]  		EIP84345
// [Category]  	Improvement
// [Description]  	Error checking and SMM dependency added
// [Files]  		LegacySmmSredir.c, LegacySredir.dxs
// 
// 3     6/17/11 4:41a Rameshr
// [TAG] - EIP 47188
// [Category]- IMPROVEMENT
// [Description]- MMIO COM device access width SDL token support added 
// [Files] - legacysredir.c, LegacySredirSmm.c, LegacySredir_setup.c
// 
// 2     9/24/10 1:37a Rameshr
// [TAG] - EIP 44133
// [Category]- BUG FIX & IMPROVEMENT
// [Severity]- Major
// [Symptom] - Flowcontrol and Terminal type is not working in Legacy
// console redirection 
// [RootCause] - Baud rate value overwrites the Flowcontrol and Terminal
// Type fields.
// [Solution] - BaudRate programming done in EFI driver. So removed the
// baud rate value passing into Sredir.bin
//              MMIO com device width added based on SDL token.
// [Files] - LegacySmmSredir.c , Legacysredir.c, Legacysredir.sdl
// 
// 1     3/05/10 4:17a Rameshr
// Initial Check-in
// 
// MMIO COM device support added.
// EIP 33847
// 
// 
//****************************************************************************
//****************************************************************************
//<AMI_FHDR_START>
//****************************************************************************
//
// Name:	LegacySmmSredir.C
//
// Description:	Legacy console redirection SMM support
//****************************************************************************
//<AMI_FHDR_END>

#include "token.h"
#include "Protocol/LegacySredir.h"
#include "AmiDxeLib.h"
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION>=0x4028B)
#include <Protocol\SmmCpu.h>
#include <Protocol\SmmBase2.h>
#include <Protocol\SmmSwDispatch2.h>
#else
#include <Protocol\SmmBase.h>
#include <Protocol\SmmSwDispatch.h>
#endif
#include <Protocol\DevicePath.h>
#include <Protocol\LoadedImage.h>
#include <AmiSmm.h>
#include <AmiBufferValidationLib.h>


#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION>=0x4028B)
EFI_SMM_BASE2_PROTOCOL          *gSmmBase2;
EFI_SMM_SYSTEM_TABLE2           *pSmst2;
EFI_GUID gEfiSmmCpuProtocolGuid = EFI_SMM_CPU_PROTOCOL_GUID;
EFI_SMM_CPU_PROTOCOL            *gSmmCpu=NULL;
#define RETURN(status) {return status;}
#else
EFI_GUID gEfiSmmSwDispatchProtocolGuid = EFI_SMM_SW_DISPATCH_PROTOCOL_GUID;
EFI_GUID gSwSmiCpuTriggerGuid = SW_SMI_CPU_TRIGGER_GUID;
#define RETURN(status) {return ;}
#endif

#pragma pack(1)
typedef struct {
    UINT32          MMIOAddress;
    UINT8           FuncNo;
    UINT8           ReadWrite;      
    UINT8           Offset;
    UINT8           Value;
    UINT8           Count;
    UINT32          BufferAddress;
} SREDIR_INPUT_PARAMETER;
#pragma pack()

EFI_STATUS
LegacySreDirInSmmFunction(
    IN EFI_HANDLE                ImageHandle,
    IN EFI_SYSTEM_TABLE          *SystemTable
 );

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS LegacySredirSMIHandler (
	IN EFI_HANDLE       DispatchHandle,
	IN CONST VOID       *DispatchContext OPTIONAL,
	IN OUT VOID         *CommBuffer OPTIONAL,
	IN OUT UINTN        *CommBufferSize OPTIONAL
#else
VOID LegacySredirSMIHandler (
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext
#endif
);

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   ReadSerialPort
//
// Description: Read the Data from Serial Port
//
// Input:       SREDIR_INPUT_PARAMETER SredirParam 
//
// Output:      
//
// Returns: 
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ReadSerialPort(
	IN OUT SREDIR_INPUT_PARAMETER	*SredirParam
)
{

    UINT32  TempValue;
#if COM_MMIO_WIDTH == 4
    TempValue=*(UINT32*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH));
#else
    #if COM_MMIO_WIDTH == 2
        TempValue=*(UINT16*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH));
    #else
        TempValue=*(UINT8*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH));
    #endif
#endif    

    SredirParam->Value=(UINT8)TempValue;
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   WriteSerialPort
//
// Description: Write the Data to Serial Port
//
// Input:       SREDIR_INPUT_PARAMETER SredirParam 
//
// Output:      
//
// Returns: 
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS WriteSerialPort(
	IN OUT SREDIR_INPUT_PARAMETER	*SredirParam
)
{
    UINT32  TempValue=(UINT32)SredirParam->Value;

#if COM_MMIO_WIDTH == 4
    *(UINT32*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH))=TempValue;
#else
    #if COM_MMIO_WIDTH == 2
        *(UINT16*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH))=(UINT16)TempValue;
    #else
        *(UINT8*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH))=(UINT8)TempValue;
    #endif
#endif   

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   WriteBufferSerialPort
//
// Description: Write the buffer of data to Serial port
//
// Input:       SREDIR_INPUT_PARAMETER SredirParam 
//
// Output:      
//
// Returns: 
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS WriteBufferSerialPort(
    IN OUT SREDIR_INPUT_PARAMETER	*SredirParam
)
{
    UINT8       i;
    UINT32      TempValue=0;
    UINT8       *DataBuffer=(UINT8*)SredirParam->BufferAddress;
    EFI_STATUS  Status;

    if(SredirParam->Count == 0) {
        return EFI_SUCCESS;
    }

    if( !DataBuffer ) {
        return EFI_INVALID_PARAMETER;
    }

    // Validate Input buffer is valid and not resides in SMRAM region
    Status = AmiValidateMemoryBuffer ( (VOID*)DataBuffer, SredirParam->Count );
    if( Status != EFI_SUCCESS ) {
        RETURN(Status);
    }

    for(i=0;i<SredirParam->Count;i++) {
        TempValue=*DataBuffer;

#if COM_MMIO_WIDTH == 4
        *(UINT32*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH))=TempValue;
#else
    #if COM_MMIO_WIDTH == 2
            *(UINT16*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH))=(UINT16)TempValue;
    #else
            *(UINT8*)(SredirParam->MMIOAddress+(SredirParam->Offset*COM_MMIO_WIDTH))=(UINT8)TempValue;
    #endif
#endif   
        DataBuffer++;
    }

    return EFI_SUCCESS;
}
 
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	LegacySredirSMIHandler
//
// Description:	Legacy Serial Redirection Smm handler function
//
// Input:	    DispatchHandle  - EFI Handle
//              DispatchContext - Pointer to the EFI_SMM_SW_DISPATCH_CONTEXT
//
// Output:      
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS LegacySredirSMIHandler (
	IN EFI_HANDLE       DispatchHandle,
	IN CONST VOID       *DispatchContext OPTIONAL,
	IN OUT VOID         *CommBuffer OPTIONAL,
	IN OUT UINTN        *CommBufferSize OPTIONAL
#else
VOID LegacySredirSMIHandler (
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext
#endif
)
{
    EFI_STATUS  Status = EFI_SUCCESS;
	UINT8		Data;
	UINT64		pCommBuff;
	UINT32		HighBufferAddress = 0;
	UINT32		LowBufferAddress = 0;
    UINTN       Cpu=(UINTN)-1;
    SREDIR_INPUT_PARAMETER  *SredirParam;
#if PI_SPECIFICATION_VERSION < 0x1000A   
    EFI_SMM_CPU_SAVE_STATE  *pCpuSaveState;
    SW_SMI_CPU_TRIGGER      *SwSmiCpuTrigger;
    UINTN       i;
#endif

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)

    Cpu = ((EFI_SMM_SW_CONTEXT*)CommBuffer)->SwSmiCpuIndex;
    //
    // Found Invalid CPU number, return 
    //
    if(Cpu == (UINTN)-1) RETURN(Status);

    Status = gSmmCpu->ReadSaveState ( gSmmCpu, \
                                      4, \
                                      EFI_SMM_SAVE_STATE_REGISTER_RBX, \
                                      Cpu, \
                                      &LowBufferAddress );

    Status = gSmmCpu->ReadSaveState ( gSmmCpu, \
                                      4, \
                                      EFI_SMM_SAVE_STATE_REGISTER_RCX, \
                                      Cpu, \
                                      &HighBufferAddress );

    Data = ((EFI_SMM_SW_CONTEXT*)CommBuffer)->CommandPort;
#else
  	for (i = 0; i < pSmst->NumberOfTableEntries; ++i) {
		if (guidcmp(&pSmst->SmmConfigurationTable[i].VendorGuid,&gSwSmiCpuTriggerGuid) == 0) {
			break;
		}
  	}
	
  	//If found table, check for the CPU that caused the software Smi.
  	if (i != pSmst->NumberOfTableEntries) {
		SwSmiCpuTrigger = pSmst->SmmConfigurationTable[i].VendorTable;
		Cpu = SwSmiCpuTrigger->Cpu;
  	}

    //
    // Found Invalid CPU number, return 
    //
    if(Cpu == (UINTN) -1) { 
        RETURN(Status);
    }

	Data	= (UINT8)DispatchContext->SwSmiInputValue;
	
	pCpuSaveState		= pSmst->CpuSaveState;
	HighBufferAddress	= pCpuSaveState[Cpu].Ia32SaveState.ECX;
	LowBufferAddress	= pCpuSaveState[Cpu].Ia32SaveState.EBX;

#endif

	pCommBuff			= HighBufferAddress;
	pCommBuff			= Shl64(pCommBuff, 32);
	pCommBuff			+= LowBufferAddress;
    SredirParam         =(SREDIR_INPUT_PARAMETER *)pCommBuff; 

    // Validate COM port register's MMIO address space are valid and not reside in SMRAM region
    Status = AmiValidateMmioBuffer( (VOID*)SredirParam->MMIOAddress, 8*COM_MMIO_WIDTH );
    if( Status != EFI_SUCCESS ) {
        SredirParam->Value=0;
        RETURN(Status);
    }

    switch(SredirParam->FuncNo) {

        case 0x1:
                ReadSerialPort(SredirParam);
                break;

        case 0x2:
                WriteSerialPort(SredirParam);
                break;

        case 0x3:
                Status = WriteBufferSerialPort(SredirParam);
                break;

    }

    RETURN(Status);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:          LegacySreDirInSmmFunction
//
// Description:   Legacy Serial Redirection  Smm entry point
//
// Input:         Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT
//                EFI System Table - Pointer to System Table
//
// Output:        EFI_STATUS OR EFI_NOT_FOUND
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
LegacySreDirInSmmFunction(
    IN EFI_HANDLE                ImageHandle,
    IN EFI_SYSTEM_TABLE          *SystemTable
 )
{

    EFI_HANDLE	Handle;
    EFI_STATUS	Status;
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    EFI_SMM_SW_DISPATCH2_PROTOCOL    *pSwDispatch = NULL;
    EFI_SMM_SW_REGISTER_CONTEXT      SwContext;
#else
    EFI_SMM_SW_DISPATCH_PROTOCOL    *pSwDispatch;
    EFI_SMM_SW_DISPATCH_CONTEXT     SwContext;
#endif

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    Status = InitAmiSmmLib( ImageHandle, SystemTable );

    Status = pBS->LocateProtocol(&gEfiSmmBase2ProtocolGuid, NULL, &gSmmBase2);
    
    if (EFI_ERROR(Status)) { 
        return Status;
    }

    //
    // We are in SMM, retrieve the pointer to SMM System Table
    //
    Status = gSmmBase2->GetSmstLocation( gSmmBase2, &pSmst2);
    if (EFI_ERROR(Status)) {  
        return EFI_UNSUPPORTED;
    }

    Status  = pSmst2->SmmLocateProtocol( &gEfiSmmSwDispatch2ProtocolGuid, \
                                          NULL, \
                                          &pSwDispatch );

    if(EFI_ERROR(Status)) {
        return Status;
    }

    Status = pSmst2->SmmLocateProtocol(&gEfiSmmCpuProtocolGuid, NULL, &gSmmCpu);

#else
	Status	= pBS->LocateProtocol(&gEfiSmmSwDispatchProtocolGuid, NULL, &pSwDispatch);
#endif

	ASSERT_EFI_ERROR(Status);

    if(EFI_ERROR(Status)) {
        return Status;
    }

    InitAmiBufferValidationLib( ImageHandle, SystemTable );

    SwContext.SwSmiInputValue	= LEGACY_SREDIR_SWSMI;
    Status	= pSwDispatch->Register(pSwDispatch, LegacySredirSMIHandler, &SwContext, &Handle);
    return EFI_SUCCESS;
}

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