summaryrefslogtreecommitdiff
path: root/Board/EM/MeWrapper/AmtWrapper/Dxe/AmtWrapperDxe.c
blob: b5d4c0a85c41102e6eb0344276e9474ea9e7d52e (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************
//**********************************************************************
//
// $Header: /Alaska/SOURCE/Modules/SharkBayRefCodes/ME/AmtWrapper/AmtWrapperDxe/AmtWrapperDxe.c 10    6/18/14 3:08a Larryliu $
//
// $Revision: 10 $
//
// $Date: 6/18/14 3:08a $
//
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SharkBayRefCodes/ME/AmtWrapper/AmtWrapperDxe/AmtWrapperDxe.c $
// 
// 10    6/18/14 3:08a Larryliu
// [TAG]  		EIP173999
// [Category]  	Improvement
// [Description]  	[HWR]Remove useless comments from Intel ME
// component.(except RC)
// [Files]  		AmtWrapperDxe.c
// 
// 8     5/14/14 9:52p Tristinchou
// [TAG]  		EIP167030
// [Category]  	Improvement
// [Description]  	Remove the variable runtime attribute and keep original
// attributes.
// 
// 7     9/12/13 5:26a Klzhan
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	Correct gSerialOverLanDevicePath
// 
// 6     4/18/13 1:55a Klzhan
// [TAG]  		EIP119190
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	BIOS doesn't wait for KVM pass code input.
// [RootCause]  	The Con Out Start protocol GUID is different in AMI and
// ME RC.
// [Solution]  	Install ME RC ConOut Protocol when AMI ConOut Protocol
// installed.
// [Files]  		AmtWrapperDxe.c
// AmtWrapperDxe.h
// 
// 5     4/08/13 3:06a Klzhan
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	Support New Security protocol.
// 
// 4     10/30/12 8:33a Klzhan
// Correct Secure IDER boot
// 
// 3     8/14/12 7:24a Klzhan
// Support UEFI IDER boot.
// 
// 2     4/24/12 12:36a Klzhan
// Update modulepart to latest
// 
// 1     2/08/12 1:08a Klzhan
// Initial Check in 
// 
// 3     6/21/11 3:42a Klzhan
// Fix comnpiler error if PCI COM is 0.
// 
// 2     3/29/11 3:18a Klzhan
// Remove un-used code.
// 
// 1     2/25/11 1:43a Klzhan
// Initial Check-in
// 
// 2     12/28/10 2:00a Klzhan
// Improvement : Enable/Disable Terminal item for SOL redirection.
// 
// 1     12/03/10 5:10a Klzhan
// Initial Check-in.
// 
//
//**********************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:            AmtWrapperDxe.c
//
// Description:     Amt Wrapper Functions
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>
/*++
Copyright (c) 2009 Intel Corporation. All rights reserved.
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
--*/
#if EFI_SPECIFICATION_VERSION>0x20000
#include "token.h"
#define _HII_H_
#define _FORM_CALLBACK_H_
#define _GLOBAL_VARIABLE_GUID_H_
#ifndef GUID_VARIABLE_DECLARATION
#define GUID_VARIABLE_DECLARATION(Variable, Guid) extern EFI_GUID Variable
#endif
#include "AmtWrapperDxe.h"
#else
#include "AmtWrapperDxe.h"
#include "token.h"
#define __UEFI_HII__H__
#define __HII_PROTOCOL_H__
#define _HII_H_
#endif
#if defined(Terminal_SUPPORT) && (Terminal_SUPPORT == 1)
#include "Core\Em\Terminal\TerminalSetupVar.h"
#endif
#include <SetupDataDefinition.h>
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028e
#include <Protocol\Security2.h>
#else
#include <Protocol\Security.h>
#endif

#include <Amt.h>
#include EFI_GUID_DEFINITION(ConsoleOutDevice)

#define SETUP_GUID { 0xEC87D643, 0xEBA4, 0x4BB5, 0xA1, 0xE5, 0x3F, 0x3E, 0x36, 0xB2, 0x0D, 0xA9 }

AMT_WRAPPER_PROTOCOL AmtWrapperInstance = {
                                        AmtWrapperInit,
                                        AmtWrapperGet,
                                        AmtWrapperSet,                                      
                                        AmtWrapperEnableSol,
                                        AmtWrapperEnableIdeR,
                                        AmtWrapperPauseBoot,
                                        AmtWrapperEnterSetup,
                                        //AsfSupportInit,
                                        AmtWrapperBdsBootViaAsf,
                                        BdsLibUpdateConsoleVariable,
                                        ActiveManagementConsoleLocked,
                                        AmtWrapperEnableKvm,
                                        AmtWrapperInitializationKvm
                                  };

extern EFI_ACTIVE_MANAGEMENT_PROTOCOL   *mActiveManagement;
extern EFI_ASF_BOOT_OPTIONS             *mAsfBootOptions;
#if defined(LEGACYSREDIR_SUPPORT) && (LEGACYSREDIR_SUPPORT == 1)
EFI_GUID gEfiLegacySredirProtocolGuid = EFI_LEGACY_SREDIR_PROTOCOL_GUID;
#endif
EFI_GUID gSetupGuid                 	= SETUP_GUID;
EFI_GUID gEfiAmtWrapperProtocolGuid 	= EFI_AMT_WRAPPER_PROTOCOL_GUID;
EFI_GUID gEfiMeBiosExtensionSetupGuid 	= EFI_ME_BIOS_EXTENSION_SETUP_GUID;
CHAR16   gEfiMeBiosExtensionSetupName[] = EFI_ME_BIOS_EXTENSION_SETUP_VARIABLE_NAME;

PLATFORM_PCI_SERIAL_OVER_LAN_DEVICE_PATH gSerialOverLanDevicePath = {
            gPciRootBridge,
            {
                HARDWARE_DEVICE_PATH,
                HW_PCI_DP,
                (UINT8)(sizeof(PCI_DEVICE_PATH)),
                (UINT8)((sizeof(PCI_DEVICE_PATH)) >> 8),
                SOL_FUNCTION_NUMBER,
                ME_DEVICE_NUMBER
            },
            {
                MESSAGING_DEVICE_PATH,
                MSG_UART_DP,
                (UINT8)(sizeof(UART_DEVICE_PATH)),
                (UINT8)((sizeof(UART_DEVICE_PATH)) >> 8),
                0,
                115200,
                8,
                1,
                1
            },
            {
                MESSAGING_DEVICE_PATH,
                MSG_VENDOR_DP,
                (UINT8)(sizeof(VENDOR_DEVICE_PATH)),
                (UINT8)((sizeof(VENDOR_DEVICE_PATH)) >> 8),
                DEVICE_PATH_MESSAGING_PC_ANSI
            },
            gEndEntire
        };
        

EFI_HANDLE mBdsImageHandle;
UINT16 gPlatformBootTimeOutDefault = 1;


//
// Driver entry point
//
EFI_DRIVER_ENTRY_POINT (AmtWrapperDxeEntryPoint);

//
// Function implementations
//
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028e
EFI_STATUS EmptyFileAuthentication
(
    IN CONST EFI_SECURITY2_ARCH_PROTOCOL *This,
    IN CONST EFI_DEVICE_PATH_PROTOCOL *File,
    IN VOID *FileBuffer,
    IN UINTN FileSize,
    IN BOOLEAN BootPolicy
)
#else
EFI_STATUS EmptyFileAuthentication
(
    IN EFI_SECURITY_ARCH_PROTOCOL *This,
    IN UINT32 AuthenticationStatus,
    IN EFI_DEVICE_PATH_PROTOCOL *File
)
#endif
{
    if (File==NULL) return EFI_INVALID_PARAMETER;
    return EFI_SUCCESS;
}

VOID
EFIAPI
InstallEDKConOutStartProtocol (
  IN EFI_EVENT                    Event,
  IN VOID                         *Context
  )
{

  gBS->InstallProtocolInterface(
      &mBdsImageHandle, &gEfiConsoleOutDeviceGuid, EFI_NATIVE_INTERFACE, NULL);

  gBS->CloseEvent (Event);
}

EFI_STATUS
EFIAPI
AmtWrapperDxeEntryPoint (
    IN EFI_HANDLE ImageHandle,
    IN EFI_SYSTEM_TABLE *SystemTable
)
/*++
 
Routine Description:
 
  Entry point for the Active Management Driver.
  
Arguments:
 
  ImageHandle       Image handle of this driver.
  SystemTable       Global system service table.
 
Returns:
 
  EFI_SUCCESS           Initialization complete.
  EFI_UNSUPPORTED       The chipset is unsupported by this driver.
  EFI_OUT_OF_RESOURCES  Do not have enough resources to initialize the driver.
  EFI_DEVICE_ERROR      Device error, driver exits abnormally.
 
--*/
{
    EFI_STATUS Status;
    EFI_EVENT  ReadyToBootEvent;
    EFI_GUID   gSetupGuid = SETUP_GUID;
    UINTN      VariableSize  = sizeof(SETUP_DATA);
    SETUP_DATA SetupDataBuffer;
    SETUP_DATA *SetupData = &SetupDataBuffer;
    VOID       *AfterConsolOutNotifyReg;
    EFI_EVENT  AfterConsolOutInstalledEvent;
    EFI_GUID   AmiConOutStartProtocolGuid = AMI_CONSOLE_OUT_DEVICES_STARTED_PROTOCOL_GUID;

    mBdsImageHandle = ImageHandle;
    //
    // Initialize the EFI Driver Library
    //
    EfiInitializeDriverLib (ImageHandle, SystemTable);
    INITIALIZE_SCRIPT (ImageHandle, SystemTable);

    mAsfBootOptions = NULL;
    mActiveManagement = NULL;
    
    InitAmtWrapperLib();
    //
    // Install the EFI_ACTIVE_MANAGEMENT_PROTOCOL interface
    //
    Status = gBS->InstallMultipleProtocolInterfaces (
                 &ImageHandle,
                 &gEfiAmtWrapperProtocolGuid,   &AmtWrapperInstance,
                 NULL
             );

    // This event is for KVM function, because ConOut Start GUID is different from AMI to ME RC.
    Status = gBS->CreateEvent (
                    EFI_EVENT_NOTIFY_SIGNAL,
                    EFI_TPL_NOTIFY,
                    InstallEDKConOutStartProtocol,
                    NULL,
                    &AfterConsolOutInstalledEvent
                    );

    Status = gBS->RegisterProtocolNotify (
                    &AmiConOutStartProtocolGuid,
                    AfterConsolOutInstalledEvent,
                    &AfterConsolOutNotifyReg
                    );

#if defined(Terminal_SUPPORT) && (Terminal_SUPPORT == 1)
    // Enable/Disable Sol redirection
    SetSOLCOMEnable(AmtWrapperEnableSol());
#endif
    //
    // Register Ready to Boot Event for AMT Wrapper 
    //
#if defined(EFI_EVENT_SIGNAL_READY_TO_BOOT) && EFI_SPECIFICATION_VERSION<0x20000
    Status = gBS->CreateEvent(
                EFI_EVENT_SIGNAL_READY_TO_BOOT | EFI_EVENT_NOTIFY_SIGNAL_ALL,
                EFI_TPL_CALLBACK,
               	AmtWrapperReadyToBoot,
               	(VOID *)&ImageHandle,
                &ReadyToBootEvent
                );
#else
    Status = EfiCreateEventReadyToBoot(
                EFI_TPL_CALLBACK,
               	AmtWrapperReadyToBoot,
               	(VOID *)&ImageHandle,
                &ReadyToBootEvent
                );
#endif
  	
    return Status;
}

#if defined(Terminal_SUPPORT) && (Terminal_SUPPORT == 1)
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SetSOLCOMEnable
//
// Description: Enable/Disable SOL COM.
//
// Input:
//
// Output:
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
SetSOLCOMEnable(
    IN BOOLEAN         Enabled
)
{
    UINT32      ComPort;
    UINT32      SetupVarAttr;
    UINTN       SetupDataSize;
    SETUP_DATA  gSetupData;
    EFI_STATUS  Status;
#if (TOTAL_PCI_SERIAL_PORTS > 0)
    UINT32      i = 0;
    EFI_GUID    gTerminalVarGuid   = TERMINAL_VAR_GUID;
    UINTN       PciSerialPortsLocationVarSize = 
                                    sizeof(PCI_SERIAL_PORTS_LOCATION_VAR);
    UINT32      PciSerialPortsLocationVarAttributes=0;
    PCI_SERIAL_PORTS_LOCATION_VAR PciSerialPortsLocationVar; 
    UINT32 gTotalSioSerialPorts = TOTAL_SIO_SERIAL_PORTS;
    Status = gRT->GetVariable(L"PciSerialPortsLocationVar", 
                                &gTerminalVarGuid, 
                                &PciSerialPortsLocationVarAttributes, 
                                &PciSerialPortsLocationVarSize, 
                                &PciSerialPortsLocationVar);

    if(EFI_ERROR(Status)) {
        ComPort = gTotalSioSerialPorts;
    }

    ComPort = gTotalSioSerialPorts; 

    for (i = 0; i < TOTAL_PCI_SERIAL_PORTS; i++) {
        if ((PciSerialPortsLocationVar.Device[i] == SOL_DEVICE_NUMBER) && 
            (PciSerialPortsLocationVar.Function[i] == SOL_FUNCTION_NUMBER)) {
                ComPort = gTotalSioSerialPorts+i; 
                break;            
        }
    }	
#else 
    return;
#endif

    SetupVarAttr = 0;
    SetupDataSize = sizeof(SETUP_DATA);
    Status = gRT->GetVariable( \
                    L"Setup", \
                    &gSetupGuid, \
                    &SetupVarAttr, \
                    &SetupDataSize, \
                    &gSetupData );

    if(Enabled)
        gSetupData.ConsoleRedirectionEnable[ComPort] = 1;
    else
        gSetupData.ConsoleRedirectionEnable[ComPort] = 0;

    Status = gRT->SetVariable( \
                    L"Setup", \
                    &gSetupGuid, \
                    SetupVarAttr, \
                    SetupDataSize, \
                    &gSetupData );
}
#endif

EFI_STATUS
BdsRefreshBbsTableForBoot (
    IN BDS_COMMON_OPTION *Entry
)
{
    return EFI_NOT_FOUND;
}

VOID
PrintBbsTable (
    IN BBS_TABLE *LocalBbsTable
)
{
    UINT16 Idx;

    DEBUG ((EFI_D_ERROR, "\n"));
    DEBUG ((EFI_D_ERROR, " NO  Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
    DEBUG ((EFI_D_ERROR, "=============================================\n"));
    for (Idx = 0; Idx < MAX_BBS_ENTRIES; Idx++)
    {
        if (LocalBbsTable[Idx].BootPriority == BBS_IGNORE_ENTRY)
        {
            continue;
        }

        DEBUG (
            (EFI_D_ERROR,
             " %02x: %04x %02x/%02x/%02x %02x/02%x %04x %04x %04x:%04x\n",
             (UINTN) Idx,
             (UINTN) LocalBbsTable[Idx].BootPriority,
             (UINTN) LocalBbsTable[Idx].Bus,
             (UINTN) LocalBbsTable[Idx].Device,
             (UINTN) LocalBbsTable[Idx].Function,
             (UINTN) LocalBbsTable[Idx].Class,
             (UINTN) LocalBbsTable[Idx].SubClass,
             (UINTN) LocalBbsTable[Idx].DeviceType,
             (UINTN) * (UINT16 *) &LocalBbsTable[Idx].StatusFlags,
             (UINTN) LocalBbsTable[Idx].BootHandlerSegment,
             (UINTN) LocalBbsTable[Idx].BootHandlerOffset,
             (UINTN) ((LocalBbsTable[Idx].MfgStringSegment << 4) + LocalBbsTable[Idx].MfgStringOffset),
             (UINTN) ((LocalBbsTable[Idx].DescStringSegment << 4) + LocalBbsTable[Idx].DescStringOffset))
        );
    }

    DEBUG ((EFI_D_ERROR, "\n"));
}

BOOLEAN 
AmtWrapperGet(
    IN UINTN Index, 
    IN OUT VOID *pVar
)
{
    BOOLEAN st = FALSE;
    
    InitAmtWrapperLib();
    
    if (Index == GET_SOL_DP) {
        *((PLATFORM_PCI_SERIAL_OVER_LAN_DEVICE_PATH **)pVar) = &gSerialOverLanDevicePath;
        st = TRUE;
    } else if (Index == GET_TIME_OUT) {
        *((UINT16 *)pVar) = BdsLibGetTimeout();     
        st = TRUE;
    } else if (Index == GET_BOOT_OPTION) {    
        *((UINT16 *)pVar) = mAsfBootOptions->BootOptions;
         st = TRUE;
    } else if (Index == GET_IS_SOL) {    
        *((UINTN *)pVar) = IsSOL(*((EFI_HANDLE *)pVar));
         st = TRUE;
    } else if (Index == GET_IS_IDER) {    
        *((UINTN *)pVar) = IsIDER(*((EFI_HANDLE *)pVar));
         st = TRUE;
    }

    return st;
}

UINTN IsSOL(
	IN EFI_HANDLE Controller
) 
{
	EFI_STATUS 					Status;
	EFI_PCI_IO_PROTOCOL         *PciIo;
    UINTN                       Segment=0;
    UINTN                       Bus=0; 
    UINTN                       Device=0; 
    UINTN                       Function=0;
	UINTN						flag = FALSE;
	
	Status = gBS->HandleProtocol(Controller,
                                 &gEfiPciIoProtocolGuid,
                                 &PciIo);
    if (!EFI_ERROR(Status)) {	    
	    Status = PciIo->GetLocation(PciIo, 
	    							&Segment, 
	    							&Bus, 
	                                &Device, 
	                                &Function);
		if (!EFI_ERROR(Status)) {		
			if (Device == SOL_DEVICE_NUMBER && Function == SOL_FUNCTION_NUMBER) {
				flag = TRUE;
			}
		}
	}
                    
   	return flag;                    
}

UINTN IsIDER(
	IN EFI_HANDLE Controller
) 
{
	EFI_STATUS 					Status;
	EFI_PCI_IO_PROTOCOL         *PciIo;
    UINTN                       Segment=0;
    UINTN                       Bus=0; 
    UINTN                       Device=0; 
    UINTN                       Function=0;
	UINTN						flag = FALSE;
	
	Status = gBS->HandleProtocol(Controller,
                                 &gEfiPciIoProtocolGuid,
                                 &PciIo);
    if (!EFI_ERROR(Status)) {	    
		    Status = PciIo->GetLocation(PciIo, 
		    							&Segment, 
		    							&Bus, 
		                                &Device, 
		                                &Function);
	    if (!EFI_ERROR(Status)) {	    			
			if (Device == IDER_DEVICE_NUMBER && Function == IDER_FUNCTION_NUMBER) {
				flag = TRUE;
			}
		}
	}
                    
   	return flag;                    
}

UINT16 ConvertSetupDataToUART(
	UINT8 Parity,
	UINT8 DataBits,
	UINT8 StopBits
)
{
	UINT16 DataParityStopBit = 0;
	
    switch (Parity) {
        case NoParity:
            DataParityStopBit &= ~(PAREN | EVENPAR | STICPAR);
        break;
        case EvenParity:
            DataParityStopBit |= (PAREN | EVENPAR);
            DataParityStopBit &= ~STICPAR;
        break;
        case OddParity:
            DataParityStopBit |= PAREN;
            DataParityStopBit &= ~(EVENPAR | STICPAR);
            break;
        case SpaceParity:
            DataParityStopBit |= (PAREN | EVENPAR | STICPAR);
            break;
        case MarkParity:
            DataParityStopBit |= (PAREN | STICPAR);
            DataParityStopBit &= ~EVENPAR;
        break;
    }
    
    // Set stop bits.
    switch (StopBits) {
        case OneStopBit :
            DataParityStopBit &= ~STOPB;
        break;
        case OneFiveStopBits :
        case TwoStopBits :
            DataParityStopBit |= STOPB;
            break;
    }

    // Set data bits.
    DataParityStopBit &= ~SERIALDB;
    DataParityStopBit |= (UINT8)((DataBits - 5) & 0x03);
            
    return DataParityStopBit;    
}

BOOLEAN 
AmtWrapperSet(
    IN UINTN Index, 
    IN OUT VOID *pVar
)
{
#if defined(LEGACYSREDIR_SUPPORT) && (LEGACYSREDIR_SUPPORT == 1)
    EFI_LEGACY_SREDIR_PROTOCOL  *pLegacySredir;
    UINT64                      PciAddress;
    UINT32                      SerialAddress;
    UINT8                       SerialIRQ;
    EFI_STATUS                  Status;
    EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
    BOOLEAN st = FALSE;
    
    InitAmtWrapperLib();
    
    if (Index == SET_BIOS_WDT_START) {
        AsfStartWatchDog(ASF_START_BIOS_WDT);
        st = TRUE;
    }
    else if (Index == SET_OS_WDT_START) {
        AsfStartWatchDog(ASF_START_OS_WDT);
        st = TRUE;
    }
    else if (Index == SET_WDT_STOP) {
        AsfStopWatchDog();
        st = TRUE;
    }
    else if (Index == SET_OS_SOL) {
        Status = gBS->LocateProtocol(&gEfiLegacySredirProtocolGuid, NULL, &pLegacySredir);
        ASSERT_EFI_ERROR(Status);
        
        Status = gBS->LocateProtocol(&gEfiPciRootBridgeIoProtocolGuid, NULL, &PciRootBridgeIo);
        ASSERT_EFI_ERROR(Status);

        PciAddress = EFI_PCI_ADDRESS (SOL_BUS_NUMBER, SOL_DEVICE_NUMBER, SOL_FUNCTION_NUMBER, 0x10);
        PciRootBridgeIo->Pci.Read (
            PciRootBridgeIo,
            EfiPciWidthUint32,
            PciAddress,
            1,
            &SerialAddress
        );
        SerialAddress &= ~1;            // mask IO/MEM bit
        
        PciAddress = EFI_PCI_ADDRESS (SOL_BUS_NUMBER, SOL_DEVICE_NUMBER, SOL_FUNCTION_NUMBER, 0x3C);
        PciRootBridgeIo->Pci.Read (
            PciRootBridgeIo,
            EfiPciWidthUint8,
            PciAddress,
            1,
            &SerialIRQ
        );
/*
		UID = TOTAL_SIO_SERIAL_PORTS;
		gEfiComParameters.BaseAddress = (UINT16)SerialAddress;
		gEfiComParameters.SerialIRQ = (UINT8)SerialIRQ;
		
		VariableSize = sizeof(SETUP_DATA);
	   	Status = gRT->GetVariable(
	   					L"Setup", 
	   					&gSetupGuid, 
	   					NULL,
	   					&VariableSize, 
	   					&SetupData);
		ASSERT(!EFI_ERROR(Status));
		        
    	gEfiComParameters.Baudrate      = BaudRates[SetupData.BaudRate[UID]];
    	gEfiComParameters.TerminalType  = TerminalTypes[SetupData.TerminalType[UID]];
    	gEfiComParameters.FlowControl   = SetupData.FlowControl[UID];
        
    	gEfiComParameters.DataParityStop = ConvertSetupDataToUART(
        					SetupData.Parity[UID],
        					SetupData.DataBits[UID],
        					SetupData.StopBits[UID]);
        
		if (SREDIR_DISPLAY_MODE == DISPLAY_BY_80x24)
			gEfiComParameters.LegacyOsResolution = 0;
		else if (SREDIR_DISPLAY_MODE == DISPLAY_BY_80x25)
			gEfiComParameters.LegacyOsResolution = 1;
		else
			gEfiComParameters.LegacyOsResolution = SetupData.LegacyOsResolution[UID];
*/
        Status = pLegacySredir->EnableLegacySredir(pLegacySredir);
        
        ASSERT_EFI_ERROR (Status);
        st = TRUE;
    }
    
    return st;
#else
    return FALSE;
#endif
}

EFI_STATUS 
AmtWrapperInit(
    IN EFI_HANDLE ImageHandle, 
    IN OUT EFI_SYSTEM_TABLE *SystemTable
)
{
    mBdsImageHandle = ImageHandle;
    InitAmtWrapperLib();

    return EFI_SUCCESS;
}


VOID
InitAmtWrapperLib(VOID)
{
    if (mActiveManagement == NULL)
        AmtLibInit();
        
    if (mAsfBootOptions == NULL) {
		BdsAsfInitialization();
	}
}

BOOLEAN
AmtWrapperEnableSol(
	IN VOID
)
{
    BOOLEAN st;
    
    InitAmtWrapperLib();    
    st = ActiveManagementEnableSol();
    if(((mAsfBootOptions->OemParameters) & 0x01) && (mAsfBootOptions != NULL))
        st = TRUE;
    else
        st = FALSE;
    return st;
}

BOOLEAN
AmtWrapperEnableIdeR(
	IN VOID
)
{
    BOOLEAN     st;
    EFI_STATUS  Status;
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028e
    EFI_SECURITY2_ARCH_PROTOCOL *mSecurity;
#else
    EFI_SECURITY_ARCH_PROTOCOL *mSecurity;
#endif

    InitAmtWrapperLib();    
    st = ActiveManagementEnableIdeR();
    if(!st)
        return st;
    // IDER enabled and Enforce Secure Boot enabled
    if((mAsfBootOptions->SpecialCommandParam & ENFORCE_SECURE_BOOT) 
                                            != ENFORCE_SECURE_BOOT)
    {
#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028e
        EFI_GUID gEfiSecurity2ArchProtocolGuid = EFI_SECURITY2_ARCH_PROTOCOL_GUID;
        Status = gBS->LocateProtocol(&gEfiSecurity2ArchProtocolGuid, 

#else
        Status = gBS->LocateProtocol(&gEfiSecurityArchProtocolGuid, 
#endif
                                     NULL, &mSecurity);

        if(EFI_ERROR(Status))
            return st;
        // Set a empty File Authentication to skip Secure check.
        // This will not been restore, because IDER boot fail system HALT

#if defined(CORE_COMBINED_VERSION) && CORE_COMBINED_VERSION >=0x4028e
        mSecurity->FileAuthentication = EmptyFileAuthentication;
#else
        mSecurity->FileAuthenticationState = EmptyFileAuthentication;
#endif
    }

    return st;
}

BOOLEAN
AmtWrapperPauseBoot(
	IN VOID
)
{
    BOOLEAN st;
    
    InitAmtWrapperLib();    
    st = ActiveManagementPauseBoot();
    
    return st;
}

BOOLEAN
AmtWrapperEnterSetup(
	IN VOID
)
{
    BOOLEAN st;
    
    InitAmtWrapperLib();    
    st = ActiveManagementEnterSetup();
    
    return st;
}

EFI_STATUS
AmtWrapperBdsBootViaAsf(
	IN VOID
)
{
	EFI_STATUS Status = EFI_NOT_FOUND;
    
    InitAmtWrapperLib();
    
    if (mActiveManagement != NULL && mAsfBootOptions != NULL) {
    	Status = BdsBootViaAsf();
    }
    
    return Status;
}



EFI_STATUS
AmtWrapperReadyToBoot(
	EFI_EVENT           Event,
    VOID                *ParentImageHandle
)
{
	EFI_STATUS Status = EFI_SUCCESS;
	
	
	return Status;
}

BOOLEAN
AmtWrapperEnableKvm(
	IN VOID
)
{
  BOOLEAN Status;

  Status = ActiveManagementEnableKvm();    

  return Status;
}

BOOLEAN
AmtWrapperInitializationKvm(
	IN VOID
)
{
  BOOLEAN Status;

  Status = BdsKvmInitialization();    

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