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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
|
//*************************************************************************
//*************************************************************************
//** **
//** (C)Copyright 1985-2011, American Megatrends, Inc. **
//** **
//** All Rights Reserved. **
//** **
//** 5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093 **
//** **
//** Phone: (770)-246-8600 **
//** **
//*************************************************************************
//*************************************************************************
//**********************************************************************
// $Header: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Core/EM/SMM/SmmInit.c 3 10/31/12 6:32a Wesleychen $
//
// $Revision: 3 $
//
// $Date: 10/31/12 6:32a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Core/EM/SMM/SmmInit.c $
//
// 3 10/31/12 6:32a Wesleychen
// Move 'DisableSmmCallbacks' event from BDS_CONNECT_DRIVERS_PROTOCOL_GUID
// to BDS_ALL_DRIVERS_CONNECTED_PROTOCOL_GUID.
//
// 8 1/05/12 1:54p Markw
// [TAG] EIP78457
// [Category] Bug Fix
// [Severity] Normal
// [Symptom] If BSP is switch, SMM may not bring all CPUs into SMM if
// not all CPUs are inside of SMM.
// [RootCause] During CPU initialization, the sytem uses the actual BSP
// instead of BSP from the SMM HOB.
// This creates a inconsistentancy with the rest of the SMM numbering.
// [Solution] Use SMM HOB BSP value.
// [Files] SmmInit.c
//
// 7 10/03/11 2:51p Markw
// Update StartupThisAP for PI 1.1.
// File: SmmInit.c
//
// 6 6/22/11 11:55a Markw
// Separate DxeSmmReadyToLock from Protocol\SmmReadyToLock.h.
//
//
// 5 5/10/11 12:06p Markw
// [TAG] EIP60051
// [Category] Bug Fix
// [Severity] Critical
// [Symptom] With PI 1.1, BIOS may hang in DisableSmmCallbacks .
// [RootCause] DisableSmmCallbacks calls SmmDispatcher. This must be
// executed inside SMM.
// [Solution] Call SmmDispatcher inside SMM.
// [Files] SmmInit.c
//
// 4 4/12/11 3:00p Markw
// [TAG] EIP57440
// [Category] New Feature
// [Description] PI Update allocated SMM BASE Protocol with boot
// services data. Change it to runtime services data.
// [Files] SmmInit.c
//
// 3 4/01/11 10:07a Markw
// [TAG] EIP57440
// [Category] New Feature
// [Description] Add PI 1.1 SMM support for reading/writing floating
// point/smm save state.
//
// [Files] CpuCspLib.h, CpuCspLib.c, SmmPiSmst.c, SmmPrivateShared.h,
// SmmHdr.equ, SmmInit.c
//
// 2 3/04/11 3:32p Markw
// Update headers.
//
// 1 2/07/11 3:34p Markw
// [TAG] EIP53481
// [Category] New Feature
// [Description] Add PIWG 1.1 SMM support.
// Moved common initialization from SmmBase.c to SmmInit.c.
// [Files] Smm.sdl, SmmPrivateShared.h, SmmDispatcher.mak,
// SmmDispatcher.h, SmmDispatcher.c,
// Smst.c, SmmPiSmst.c, SmmInit.c, SmmBase.c, SmmBase2.c,
// SmmDriverDispatcher.c, Smm Framewwork Protocol files, SmmPi.h,
// Smm Pi Protocol files, SmmPciRbio files
//
//**********************************************************************
//<AMI_FHDR_START>
//---------------------------------------------------------------------------
//
// Name: SmmInit.c
//
// Description: Dispatch SMM Drivers
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>
//This include should be first.
#include "SmmPrivateShared.h"
#include <AmiDxeLib.h>
#include "AmiCspLib.h"
#include <AmiSmm.h>
#include <AmiHobs.h>
#if USE_FV2
#include <Protocol\FirmwareVolume2.h>
#else
#include <Protocol\FirmwareVolume.h>
#endif
#include <Protocol\LoadPe32Image.h>
#include <Protocol\MpService.h>
#if SMM_THUNK_IN_CSM == 1
#include <Protocol\LegacyBiosExt.h>
#include <Protocol\LegacyBios.h>
#endif
#if SMM_USE_FRAMEWORK
#include <Protocol\SmmBase.h>
#endif
#if SMM_USE_PI
#include <Protocol\SmmBase2.h>
#include <Protocol\SmmReadyToLock.h>
#include <Protocol\DxeSmmReadyToLock.h>
#endif
#if SMM_USE_PI
#include <Protocol\SmmAccess2.h>
#include <Protocol\SmmControl2.h>
#else
#include <Protocol\SmmAccess.h>
#include <Protocol\SmmControl.h>
#endif
#include <Token.h>
// {3AA83745-9454-4f7a-A7C0-90DBD02FAB8E}
#define BDS_CONNECT_DRIVERS_PROTOCOL_GUID \
{ 0x3aa83745, 0x9454, 0x4f7a, { 0xa7, 0xc0, 0x90, 0xdb, 0xd0, 0x2f, 0xab, 0x8e } }
#define BDS_ALL_DRIVERS_CONNECTED_PROTOCOL_GUID \
{0xdbc9fd21, 0xfad8, 0x45b0, 0x9e, 0x78, 0x27, 0x15, 0x88, 0x67, 0xcc, 0x93}
CPUINFO_HOB *gCpuinfoHob;
SMM_HOB *gSmmHob;
EFI_HOB_CPU *gCpuHob;
UINT32 gBspSmmBase;
SMM_ENTRY_INIT_STRUCT *gSmmEntryData;
SMM_BASE_PRIVATE_STRUCT *gBasePrivate;
SMM_DISPATCHER_PRIVATE_STRUCT *gDispatcherPrivate;
EFI_PE32_IMAGE_PROTOCOL *gPe32Image;
EFI_MP_SERVICES_PROTOCOL *gMpServices;
#if SMM_USE_FRAMEWORK
EFI_SMM_SYSTEM_TABLE *gSmstTable;
#endif
#if SMM_USE_PI
EFI_SMM_SYSTEM_TABLE2 *gSmstTable2;
#endif
#if SMM_USE_PI
EFI_SMM_CONTROL2_PROTOCOL *gSmmControl;
EFI_SMM_ACCESS2_PROTOCOL *gSmmAccess;
#else
EFI_SMM_CONTROL_PROTOCOL *gSmmControl;
EFI_SMM_ACCESS_PROTOCOL *gSmmAccess;
#endif
#if SMM_THUNK_IN_CSM == 1
EFI_LEGACY_BIOS_PROTOCOL *gLegacyBios;
EFI_LEGACY_BIOS_EXT_PROTOCOL *gLegacyBiosExt;
#endif
EFI_HANDLE gThisImageHandle;
EFI_GUID gHobListGuid = HOB_LIST_GUID;
EFI_GUID gSmmHobGuid = SMM_HOB_GUID;
EFI_GUID gCpuinfoHobGuid = AMI_CPUINFO_HOB_GUID;
EFI_GUID gEfiPe32ImageProtocolGuid = PE32_IMAGE_PROTOCOL_GUID;
EFI_GUID gEfiMpServicesProtocolGuid = EFI_MP_SERVICES_PROTOCOL_GUID;
EFI_GUID gBdsConnectDriversProtocolGuid = BDS_CONNECT_DRIVERS_PROTOCOL_GUID;
EFI_GUID gBdsAllDriversConnectedProtocolGuid = BDS_ALL_DRIVERS_CONNECTED_PROTOCOL_GUID;
#if SMM_USE_PI
EFI_GUID gEfiEventDxeDispatchGuid = EFI_EVENT_GROUP_DXE_DISPATCH;
//EFI_GUID gEfiDxeSmmReadyToLockProtocolGuid = EFI_DXE_SMM_READY_TO_LOCK_PROTOCOL_GUID;
#endif
#if USE_FV2
EFI_GUID gEfiFVProtocolGuid = EFI_FIRMWARE_VOLUME2_PROTOCOL_GUID;
#else
EFI_GUID gEfiFVProtocolGuid = EFI_FIRMWARE_VOLUME_PROTOCOL_GUID;
#endif
#if SMM_THUNK_IN_CSM == 1
EFI_GUID gEfiLegacyBiosProtocolGuid = EFI_LEGACY_BIOS_PROTOCOL_GUID;
EFI_GUID gEfiLegacyBiosExtProtocolGuid = EFI_LEGACY_BIOS_EXT_PROTOCOL_GUID;
#endif
#if SMM_USE_FRAMEWORK
extern EFI_SMM_BASE_PROTOCOL gEfiSmmBase;
#endif
#if SMM_USE_PI
extern EFI_SMM_BASE2_PROTOCOL gEfiSmmBase2;
#endif
UINT32 gBspSmmBase;
SMM_ENTRY_INIT_STRUCT *gSmmEntryData;
UINTN gBsp;
EFI_GUID SmmEntryGuid =
{0xD2596F82,0xF0E1,0x49fa,0x95,0xBC,0x62,0x01,0x2C,0x79,0x57,0x28};
//D2596F82-F0E1-49fa-95BC-62012C795728
EFI_GUID gEfiSmmCoreDispatcherGuid =
{0x4A37320B,0x3FB3,0x4365,0x97,0x30,0x9e,0x89,0xC6,0x00,0x39,0x5D};
//4A37320B-3FB3-4365-9730-9E89C600395D
VOID SmmDispatcher();
VOID SetDisallowRegistering();
VOID SmmDriverDispatchHandler();
extern UINT32 InSmmFuncLength;
extern UINT32 InSmmFuncOffset;
EFI_STATUS EfiSmmInSmm(
IN EFI_SMM_BASE_PROTOCOL *This,
OUT BOOLEAN *InSmm
);
//
// This is the structure contains default values in Boot Services memory.
// This table will be updated and copied into Runtime memory.
//
SMM_BASE_PRIVATE_STRUCT gSmmBasePrivateStruct =
{
0, //*CallBackFunc
FALSE, //InSmm
FALSE //DisallowCallbacks
//Additional members not specified.
};
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: CallFuncInSmm
//
// Description: Call function in SMM. If not in SMM, generate SW SMI, otherwise
// call the function.
//
// Input: IN func -- Pointer to function
//
// Output: VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID CallFuncInSmm(IN VOID func())
{
if (!*gBasePrivate->InSmm) {
#if SMM_USE_PI
UINT8 CommandPort = SMMBASE_CALLBACK;
#else
UINT8 ArgBuffer = SMMBASE_CALLBACK;
UINTN ArgBufferSize = 1;
#endif
gBasePrivate->CallBackFunc = func;
//Generate SW SMI
#if SMM_USE_PI
gSmmControl->Trigger(gSmmControl, &CommandPort, NULL, FALSE, 0);
#else
gSmmControl->Trigger(gSmmControl, &ArgBuffer, &ArgBufferSize, FALSE, 0);
#endif
//This is cleared inside of SMM.
} else {
func();
}
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: FindMatchingMtrr
//
// Description: Search MTRRs for a matching MTRR.
//
// Input:
// IN UINT64 BASE
// IN UINT64 Mask
//
// Output:
// UINT32 -- MSR for matching MTTR.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
#if SMM_CACHE_SUPPORT == 1
#ifndef NEHALEM_CPU_MODULE
VOID InitSmmCacheData()
{
UINT32 Msr;
UINT32 i;
UINT8 NumMtrrs = (UINT8)ReadMsr(0xfe); //MSR_IA32_MTRR_CAP
UINT32 RegEAX, RegEBX, RegECX, RegEDX;
UINT8 NumOfMemoryBits = 36;
UINT64 MemoryMask;
CPULib_CpuID(0x80000008, &RegEAX, &RegEBX, &RegECX, &RegEDX);
//Check if CPU supports this CPUID.
if (RegEAX != 0 && RegEAX != 0xffffffff)
NumOfMemoryBits = (UINT8)RegEAX & 0xff;
MemoryMask = Shl64(1, NumOfMemoryBits)- 1;
gDispatcherPrivate->SmrrEnable = FALSE;
gDispatcherPrivate->SmrrMsr = GetSmrrBaseMsr();
gDispatcherPrivate->TsegMsr = 0;
gDispatcherPrivate->TsegMsrBase = (UINT64)(UINTN)gSmmHob->Tseg;
gDispatcherPrivate->TsegMsrMask = (MemoryMask & (~(TSEG_SIZE - 1))) | (1 << 11);
//MSRs will not be found if there is not enough MSRs for caching SMM.
for(i = 0, Msr = 0x200; i < NumMtrrs; ++i, Msr+=2) { //MTRR_PHYS_BASE_0
if ((ReadMsr(Msr) & ~(UINT64)0xff) == gDispatcherPrivate->TsegMsrBase
&& ReadMsr(Msr+1) == gDispatcherPrivate->TsegMsrMask
) {
gDispatcherPrivate->TsegMsr = Msr;
}
}
if (CPULib_IsSmrrEnabled()) gDispatcherPrivate->SmrrEnable = TRUE;
}
#endif
#endif
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: LoadRealModeFile
//
// Description: Load a real mode file into a buffer.
//
// Input:
// IN EFI_GUID *Guid
// OUT VOID **Buffer
// OUT UINTN *BufferSize
//
// Output: EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS LoadRealModeFile(EFI_GUID *Guid, VOID **Buffer, UINTN *BufferSize)
{
#if USE_FV2
EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
#else
EFI_FIRMWARE_VOLUME_PROTOCOL *Fv;
#endif
UINT32 Authentication;
EFI_STATUS Status;
UINTN NumHandles;
EFI_HANDLE *HandleBuffer;
UINTN i;
*Buffer=0;
*BufferSize=0;
Status = pBS->LocateHandleBuffer(ByProtocol,&gEfiFVProtocolGuid,NULL,&NumHandles,&HandleBuffer);
if (EFI_ERROR(Status)) return Status;
for (i = 0; i< NumHandles; ++i) {
Status = pBS->HandleProtocol(HandleBuffer[i],&guidFV,&Fv);
if (EFI_ERROR(Status)) continue;
Status = Fv->ReadSection(Fv,
Guid,
EFI_SECTION_RAW,
0, //Instance
Buffer,
BufferSize,
&Authentication);
if (Status == EFI_SUCCESS) break;
}
pBS->FreePool(HandleBuffer);
return Status;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: EarlyAllocSmmMemory
//
// Description: Allocate SMM memory before SMM memory manager available.
//
// Input:
// IN UINT32 Size -- Memory size
//
// Output: VOID * -- ptr to allocated memory.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID * EarlyAllocSmmMemory(IN UINT32 Size)
{
UINT8 *AllocatedAddress;
UINT32 AdjSize;
AdjSize = (Size + 7) & ~7; //Minimum 8 bytes.
if (AdjSize > gBasePrivate->SmmAllocMemoryLength) return 0;
AllocatedAddress = (UINT8*)gBasePrivate->SmmAllocMemoryStart;
gBasePrivate->SmmAllocMemoryStart = AllocatedAddress + AdjSize;
gBasePrivate->SmmAllocMemoryLength -= AdjSize;
return AllocatedAddress;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: EarlyAllocSmmMemoryAlign
//
// Description:
// Allocate SMM memory before SMM memory manager available with specified alignment.
// Memory address returned will be the next aligned address available and the free memory
// before the aligned address can not be allocated.
//
// Input:
// IN UINT32 Size -- Memory size.
// IN UINT32 Align -- Alignment. Must be 2^n.
//
// Output: VOID * -- ptr to allocated memory.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID * EarlyAllocSmmMemoryAlign(IN UINT32 Size, IN UINT32 Align)
{
UINT8* AllocatedAddress;
UINT8 *OrigAddress;
UINT32 AdjSize;
AdjSize = (Size + 7) & ~7; //Minimum 8 bytes.
if (AdjSize > gBasePrivate->SmmAllocMemoryLength) return 0;
OrigAddress = (UINT8*)gBasePrivate->SmmAllocMemoryStart;
AllocatedAddress = (UINT8*)(((UINT32)OrigAddress + (Align - 1)) & ~(Align - 1));
gBasePrivate->SmmAllocMemoryStart = AllocatedAddress + AdjSize;
gBasePrivate->SmmAllocMemoryLength -= (UINT32)(AllocatedAddress + AdjSize - OrigAddress);
return AllocatedAddress;
}
//SMM thunk either requires AB Segment of CSM.
#if SMM_THUNK_NO_AB_SEG == 0 || SMM_THUNK_IN_CSM == 1
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: CopySmmThunkAfterBspEntry
//
// Description:
// Copy SMM Thunk to immediately after SMM BSP entry.
//
// Input:
// IN VOID *SmmEntryBuffer -- pointer to SMM Entry file
// IN UINTN SmmEntryBufferSize -- Size of SMM Entry file
//
// Output: EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CopySmmThunkAfterBspEntry (
IN VOID *SmmEntryBuffer,
IN UINTN SmmEntryBufferSize
)
{
UINT8 *SmmThunkStart = (UINT8*)gBspSmmBase + 0x8000 + gSmmEntryData->SmmThunkProcOffset;
UINT16 AdrFixup;
UINT16 SegFixup;
*(UINT32*)(gBspSmmBase + 0x8000 + gSmmEntryData->LCodeSelBaseOffset) += (UINT32)SmmThunkStart;
*(UINT32*)((UINT8*)SmmEntryBuffer + gSmmEntryData->LCodeSelBaseOffset) += (UINT32)SmmThunkStart;
MemCpy(SmmThunkStart,
(UINT8*)SmmEntryBuffer + gSmmEntryData->SmmThunkProcOffset,
gSmmEntryData->SmmThunkLength
);
gDispatcherPrivate->SmmThunkProc = (VOID(*)())SmmThunkStart;
gDispatcherPrivate->SmmThunkData = (SMM_THUNK_DATA*)
(((UINTN)SmmThunkStart + gSmmEntryData->SmmThunkLength + 16) & ~0xf);
ASSERT(((UINTN)gDispatcherPrivate->SmmThunkData & 0xf) == 0);
//Update Thunk to set Smm Thunk Data.
AdrFixup = *(UINT16*)(SmmThunkStart + 2);
SegFixup = *(UINT16*)(SmmThunkStart + 4);
*(UINT32*)(SmmThunkStart + AdrFixup) = (UINT32)gDispatcherPrivate->SmmThunkData;
*(UINT16*)(SmmThunkStart + SegFixup) = (UINT16)((UINT32)gDispatcherPrivate->SmmThunkData >> 4);
//Max 4k of stack.
//In this case, code, data, and stack in this 4k block.
//The ss=ds, and points to the end of the code,
// so code will have to be subtracted from the 4k top,
// so ss:sp = cs:1000h
gDispatcherPrivate->SmmThunkData->Below1MStack =
0x1000
- gSmmEntryData->SmmEntrySize
- gSmmEntryData->SmmThunkLength
- 0xf; //Could be up to 15 bytes skipped for alignment purposes;
gDispatcherPrivate->SmmThunkData->Below1MStack &= ~1; //Align on word bounary;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Limit = 0x3ff;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Base = 0;
return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: CopySmmThunkAtStartABSegment
//
// Description:
// Copy SMM Thunk to start of AB Segment
//
// Input:
// IN VOID *SmmEntryBuffer -- pointer to SMM Entry file
// IN UINTN SmmEntryBufferSize -- Size of SMM Entry file
//
// Output: EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CopySmmThunkAtStartABSegment(
IN VOID *SmmEntryBuffer,
IN UINTN SmmEntryBufferSize
)
{
UINT8 *SmmThunkStart;
UINT16 AdrFixup;
UINT16 SegFixup;
#if SMM_EXCLUDE_A000 != 1
SmmThunkStart = (UINT8*)0xa0000;
#else
SmmThunkStart = (UINT8*)0xb0000;
#endif
*(UINT32*)(gBspSmmBase + 0x8000 + gSmmEntryData->LCodeSelBaseOffset) += (UINT32)SmmThunkStart;
//TODO: remove and share GDT between all CPUs. CPU0 is already copied. CPUx will copy this buffer later.
*(UINT32*)((UINT8*)SmmEntryBuffer + gSmmEntryData->LCodeSelBaseOffset) += (UINT32)SmmThunkStart;
MemCpy(SmmThunkStart,
(UINT8*)SmmEntryBuffer + gSmmEntryData->SmmThunkProcOffset,
gSmmEntryData->SmmThunkLength
);
gDispatcherPrivate->SmmThunkProc = (VOID(*)())SmmThunkStart;
//SmmThunkData must be aligned on 16 byte boundary.
gDispatcherPrivate->SmmThunkData = (SMM_THUNK_DATA*)
(((UINTN)SmmThunkStart + gSmmEntryData->SmmThunkLength + 16) & ~0xf);
ASSERT(((UINTN)gDispatcherPrivate->SmmThunkData & 0xf) == 0);
//Update Thunk to set Smm Thunk Data.
AdrFixup = *(UINT16*)(SmmThunkStart + 2);
SegFixup = *(UINT16*)(SmmThunkStart + 4);
*(UINT32*)(SmmThunkStart + AdrFixup) = (UINT32)gDispatcherPrivate->SmmThunkData;
*(UINT16*)(SmmThunkStart + SegFixup) = (UINT16)((UINT32)gDispatcherPrivate->SmmThunkData >> 4);
//Max 4k of stack.
//If in SMM, 1 block is reserved for stack and data.
gDispatcherPrivate->SmmThunkData->Below1MStack =
0x1000
- gSmmEntryData->SmmThunkLength
- 0xf; //Could be up to 15 bytes skipped for alignment purposes;
gDispatcherPrivate->SmmThunkData->Below1MStack &= ~1; //Align on word boundary;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Limit = 0x3ff;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Base = 0;
return EFI_SUCCESS;
}
#if SMM_THUNK_IN_CSM == 1
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: CopySmmThunkInCsmEBDA
//
// Description:
// Copy SMM Thunk to CSM. Data will be in EBDA.
//
// Input:
// IN VOID *SmmEntryBuffer -- pointer to SMM Entry file
// IN UINTN SmmEntryBufferSize -- Size of SMM Entry file
//
// Output: EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CopySmmThunkInCsmEBDA (
VOID *SmmEntryBuffer,
UINTN SmmEntryBufferSize
)
{
UINT8 *SmmThunkStart;
UINT16 AdrFixup;
UINT16 SegFixup;
UINT32 EbdaStart;
EFI_STATUS Status;
//Initialize this with the pointer to the Buffer in memory.
//This will be update later to the newly allocated region.
SmmThunkStart = (UINT8*)SmmEntryBuffer + gSmmEntryData->SmmThunkProcOffset;
//In case this is 64-bits, initialize upper 32-bits to 0.
//Allocate Ebda, updates only the lower 32-bits.
gDispatcherPrivate->SmmThunkData = 0;
Status = gLegacyBiosExt->AllocateEbda(
gLegacyBiosExt,
4,
(UINT32*)&gDispatcherPrivate->SmmThunkData,
&gDispatcherPrivate->EbdaStartOffset
);
if (EFI_ERROR(Status)) return Status;
EbdaStart = (*(UINT16*)(0x40e)) << 4;
//Max 4k of stack.
//If in SMM, 1 block is reserved for stack and data.
gDispatcherPrivate->SmmThunkData->Below1MStack = 0x1000;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Limit = 0x3ff;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Base = 0;
AdrFixup = *(UINT16*)(SmmThunkStart + 2);
SegFixup = *(UINT16*)(SmmThunkStart + 4);
*(UINT32*)(SmmThunkStart + AdrFixup) = (UINT32)gDispatcherPrivate->SmmThunkData - EbdaStart;
*(UINT16*)(SmmThunkStart + SegFixup) = (UINT16)(((UINT32)gDispatcherPrivate->SmmThunkData - EbdaStart) >> 4);
//Allocate CSM memory.
Status = gLegacyBios->GetLegacyRegion(
gLegacyBios,
gSmmEntryData->SmmThunkLength,
0, //In e_0000 - f_ffff.
1, //Byte alignment
&SmmThunkStart
);
if (EFI_ERROR(Status)) return Status;
*(UINT32*)(gBspSmmBase + 0x8000 + gSmmEntryData->LCodeSelBaseOffset) += (UINT32)SmmThunkStart;
*(UINT32*)((UINT8*)SmmEntryBuffer + gSmmEntryData->LCodeSelBaseOffset) += (UINT32)SmmThunkStart;
//Copy to CSM memory.
Status = gLegacyBios->CopyLegacyRegion(
gLegacyBios,
gSmmEntryData->SmmThunkLength,
SmmThunkStart,
(UINT8*)SmmEntryBuffer + gSmmEntryData->SmmThunkProcOffset
);
ASSERT_EFI_ERROR(Status);
gDispatcherPrivate->SmmThunkProc = (VOID(*)())SmmThunkStart;
ASSERT(((UINTN)gDispatcherPrivate->SmmThunkData & 0xf) == 0);
//Max 4k of stack.
gDispatcherPrivate->SmmThunkData->Below1MStack = 0x1000;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Limit = 0x3ff;
gDispatcherPrivate->SmmThunkData->LegacyIdtDesc.Base = 0;
return EFI_SUCCESS;
}
#endif
#endif
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: LoadAndInitDispatcherImages
//
// Description: Load and initialize dispatcher images.
//
// Input:
// IN EFI_SYSTEM_TABLE SystemTable
// IN UINT32 NumCpus
//
// Output: VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID LoadAndInitDispatcherImages(
IN EFI_SYSTEM_TABLE *SystemTable,
IN UINT32 NumCpus
)
{
EFI_STATUS Status;
VOID *SmmEntryBuffer;
UINTN SmmEntryBufferSize;
VOID *SmmBaseSaveBuffer;
VOID *Buffer;
UINTN BufferSize;
UINT32 AuthenticationStatus;
UINT8 *SmmBase;
UINT8 *ApStackTop;
UINT16 Bsp = gSmmHob->Bsp;
UINT32 i;
UINT32 TssBaseAddress;
SMM_ENTRY_INIT_STRUCT *SmmApEntryInit;
EFI_PHYSICAL_ADDRESS EntryPoint;
EFI_HANDLE ImageHandle;
CPU_SmmMemoryInit(gSmmHob);
//
// Load BSP SMM real mode.
// This must be done before loading protected mode dispatcher
// because the entry point of the protected mode dispatcher
// initializes some of SMM real mode variables.
//
Status = LoadRealModeFile(&SmmEntryGuid, &SmmEntryBuffer, &SmmEntryBufferSize);
ASSERT_EFI_ERROR(Status);
SmmBase = (UINT8*)gBspSmmBase;
MemCpy(SmmBase+0x8000, SmmEntryBuffer, ((SMM_ENTRY_INIT_STRUCT*)((UINT8*)SmmEntryBuffer+4))->SmmEntrySize);
gSmmEntryData->CpuNo = 0;
gSmmEntryData->SmmEntryStart = (VOID*)(SmmBase + 0x8000);
gSmmEntryData->DispatcherPrivate = gDispatcherPrivate;
*(UINT32*)(SmmBase + 0x8000 + gSmmEntryData->GdtDescBaseOffset) += gBspSmmBase + 0x8000;
TssBaseAddress=*(UINT16*)(SmmBase + 0x8000 + gSmmEntryData->TssSelBaseOffset) + gBspSmmBase + 0x8000;
*(UINT16*)(SmmBase + 0x8000 + gSmmEntryData->TssSelBaseOffset) = TssBaseAddress;
*(UINT8*)(SmmBase + 0x8000 + gSmmEntryData->TssSelBaseOffset+2) = TssBaseAddress>>16;
*(UINT8*)(SmmBase + 0x8000 + gSmmEntryData->TssSelBaseOffset+5) = TssBaseAddress>>24;
gDispatcherPrivate->EbdaStartOffset = 0; //CopySmmThunkInCsmEBDA will update this.
//SMM thunk either requires AB Segment of CSM.
#if SMM_THUNK_NO_AB_SEG == 0 || SMM_THUNK_IN_CSM == 1
#if SMM_THUNK_IN_CSM == 0
if (gBspSmmBase < 0x100000)
CopySmmThunkAfterBspEntry(SmmEntryBuffer, SmmEntryBufferSize);
else
CopySmmThunkAtStartABSegment(SmmEntryBuffer, SmmEntryBufferSize);
#else
CopySmmThunkInCsmEBDA(SmmEntryBuffer, SmmEntryBufferSize);
#endif
#endif
#ifdef EFIx64
//Setup pages for virtual memory after Stack.
//align page directories to 4k.
{
UINT8 *PageTable;
UINT32 NumAddrLines = gCpuHob->SizeOfMemorySpace;
if (NumAddrLines > SMM_PAGING_MAX_ADDRESS_LINES) NumAddrLines = SMM_PAGING_MAX_ADDRESS_LINES;
PageTable = EarlyAllocSmmMemoryAlign(GetPageTableNumPages(NumAddrLines) * 4096, 4096);
ASSERT(PageTable);
FillPageTable(NumAddrLines, PageTable);
gDispatcherPrivate->PageDirectories = PageTable;
}
#endif
//
// Load and call entry of protected mode dispatcher.
// This must be done before AP real mode entry point because
// the protected mode init provides a pointer for the AP real mode
// to call after switching to protected mode.
Buffer = 0;
Status = FvReadPe32Image(
&gEfiSmmCoreDispatcherGuid,&Buffer,&BufferSize,
&AuthenticationStatus);
ASSERT_EFI_ERROR(Status);
gBasePrivate->Pe32DispatcherImage =
(VOID*)(((UINTN)gBasePrivate->SmmAllocMemoryStart + 4095) & ~4095);
gBasePrivate->Pe32DispatcherImageSize=EFI_SIZE_TO_PAGES(gSmmHob->ManagedMemSize);
Status=gPe32Image->LoadPeImage(
gPe32Image, gThisImageHandle,
NULL, //Either DevicePath or
Buffer, BufferSize, //Buffer. This will be zero if DevicePath.
(EFI_PHYSICAL_ADDRESS)gBasePrivate->Pe32DispatcherImage,
&gBasePrivate->Pe32DispatcherImageSize, &ImageHandle,
&EntryPoint, EFI_LOAD_PE_IMAGE_ATTRIBUTE_NONE
);
ASSERT_EFI_ERROR(Status);
gBasePrivate->Pe32DispatcherImageSize=EFI_PAGES_TO_SIZE(gBasePrivate->Pe32DispatcherImageSize);
pBS->FreePool(Buffer);
//
// Give control to protected mode dispatcher. The dispatcher is in SMM memory,
// however, the CPU is NOT in SMM mode. Currently SMM space is open.
//
if (!EFI_ERROR(Status)) {
TRACE((TRACE_ALWAYS,"SMM.SmmDispatcher.Entry(%lX)\n",EntryPoint));
Status = ((DISPATCHER_ENTRY_POINT)EntryPoint)(
ImageHandle,
SystemTable,
gBasePrivate,
gDispatcherPrivate,
#if SMM_USE_FRAMEWORK
&gSmstTable,
#else
NULL,
#endif
#if SMM_USE_PI
&gSmstTable2
#else
NULL
#endif
);
}
ASSERT_EFI_ERROR(Status);
gSmmEntryData->SmmStackTop = (VOID*)(gSmmHob->ManagedMemStart + BSP_SMM_STACK_SIZE);
//Copy ApCode to SMM region.
ApStackTop = gSmmEntryData->SmmStackTop + AP_SMM_STACK_SIZE;
for (i = 0; i < NumCpus; ++i) {
static UINT32 ApNum = 0; //BSP is given CPU #0, and APs 1,2...
if (i == Bsp) continue;
++ApNum;
SmmBase = (VOID*)gSmmHob->SmmBase[i];
MemCpy(SmmBase + 0x8000, SmmEntryBuffer, SmmEntryBufferSize);
SmmApEntryInit = (SMM_ENTRY_INIT_STRUCT*) (SmmBase + 0x8004);
SmmApEntryInit->SmmEntryStart = SmmBase + 0x8000;
SmmApEntryInit->DispatcherEntry = gSmmEntryData->DispatcherEntry;
SmmApEntryInit->DispatcherPrivate = gDispatcherPrivate;
SmmApEntryInit->SmmStackTop = ApStackTop;
SmmApEntryInit->CpuNo = ApNum;
*(UINT32*)(SmmApEntryInit->GdtDescBaseOffset + SmmBase + 0x8000)
+= (UINT32)SmmBase + 0x8000;
TssBaseAddress=(UINT32)((*(UINT16*)(SmmBase + 0x8000 + SmmApEntryInit->TssSelBaseOffset)) + (SmmBase + 0x8000));
*(UINT16*)(SmmBase + 0x8000 + SmmApEntryInit->TssSelBaseOffset) = TssBaseAddress;
*(UINT8*)(SmmBase + 0x8000 + SmmApEntryInit->TssSelBaseOffset+2) = (TssBaseAddress>>16);
*(UINT8*)(SmmBase + 0x8000 + SmmApEntryInit->TssSelBaseOffset+5) = (TssBaseAddress>>24);
ApStackTop += AP_SMM_STACK_SIZE;
}
pBS->FreePool(SmmEntryBuffer);
SmmBaseSaveBuffer = Malloc(SmmGetBaseSaveBufferSize());
ASSERT(SmmBaseSaveBuffer);
SmmSetupDefaultHandler(SmmBaseSaveBuffer, gSmmHob);
for (i = 0; i < gSmmHob->NumCpus; ++i) {
if (i == gBsp) {
SmmBaseChangeOnCpu((VOID*)gSmmHob->SmmBase[i]);
continue;
}
gMpServices->StartupThisAP(
gMpServices,
SmmBaseChangeOnCpu,
i, //Cpu
NULL,
0,
(VOID*)gSmmHob->SmmBase[i]
#if !(PI_SPECIFICATION_VERSION < 0x0001000A || BACKWARD_COMPATIBLE_MODE && defined(NO_PI_MP_SERVICES_SUPPORT))
, NULL
#endif
);
}
SmmRemoveDefaultHandler(SmmBaseSaveBuffer);
pBS->FreePool(SmmBaseSaveBuffer);
}
VOID InitgBasePrivate()
{
UINT32 NumCpus = gSmmHob->NumCpus;
gBasePrivate->SmmHob = gSmmHob;
gBasePrivate->CallBackFunc = 0;
gBasePrivate->SmmAllocMemoryStart =
(VOID*) (gSmmHob->ManagedMemStart + BSP_SMM_STACK_SIZE + AP_SMM_STACK_SIZE * (NumCpus - 1));
gBasePrivate->SmmAllocMemoryLength =
gSmmHob->ManagedMemSize - BSP_SMM_STACK_SIZE - AP_SMM_STACK_SIZE * (NumCpus - 1);
}
VOID InitgDispatcherPrivate()
{
UINT8 i;
UINT32 SmmNum;
UINT32 NumCpus = gSmmHob->NumCpus;
UINT8 *XmmSaveBase;
gDispatcherPrivate = EarlyAllocSmmMemory(sizeof(*gDispatcherPrivate));
ASSERT(gDispatcherPrivate);
gDispatcherPrivate->NumCpus = NumCpus;
gDispatcherPrivate->NumCpusInSmm = 0;
gDispatcherPrivate->HandlerListHead = NULL;
gDispatcherPrivate->FloatingPointSave = FALSE;
//Allocate a XMM Save Restore on a 16-byte boundary
XmmSaveBase = EarlyAllocSmmMemoryAlign(NumCpus * 512, 16);
gDispatcherPrivate->SmmXmmSave = EarlyAllocSmmMemory(NumCpus * sizeof(VOID*));
for (i = 0; i < NumCpus; ++i) {
gDispatcherPrivate->SmmXmmSave[i] = XmmSaveBase;
XmmSaveBase += 512;
}
//Initialize SmmCpuInfo
gDispatcherPrivate->SmmCpuInfo = EarlyAllocSmmMemory(sizeof(SMM_CPU_INFO) * NumCpus);
ASSERT(gDispatcherPrivate->SmmCpuInfo);
MemSet(gDispatcherPrivate->SmmCpuInfo, sizeof(SMM_CPU_INFO) * NumCpus, 0);
gDispatcherPrivate->SmmCpuInfo[0].ApicId = gCpuinfoHob->Cpuinfo[gSmmHob->Bsp].ApicId;
SmmNum = 1;
for (i = 0; i < NumCpus; ++i) {
if (i == gSmmHob->Bsp) continue;
gDispatcherPrivate->SmmCpuInfo[SmmNum].ApicId = gCpuinfoHob->Cpuinfo[i].ApicId;
++SmmNum;
}
//Initialize AP control for multi-threaded SMM.
gDispatcherPrivate->SmmApCpuControl = EarlyAllocSmmMemory(sizeof(SMM_AP_CPU_CONTROL) * NumCpus);
ASSERT(gDispatcherPrivate->SmmApCpuControl);
MemSet((VOID*)gDispatcherPrivate->SmmApCpuControl, sizeof(SMM_AP_CPU_CONTROL) * NumCpus, 0);
gDispatcherPrivate->SmmControl = gSmmControl;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: CreateRuntimeInSmmFunction
//
// Description:
// Create InSmm function in runtime.
//
// Input: VOID
//
// Output: VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID *CreateRuntimeInSmmFunction()
{
UINT8 *RuntimeInSmmFunc;
EFI_STATUS Status;
Status = pBS->AllocatePool(EfiRuntimeServicesCode, InSmmFuncLength, &RuntimeInSmmFunc);
ASSERT_EFI_ERROR(Status);
MemCpy(RuntimeInSmmFunc, EfiSmmInSmm, InSmmFuncLength);
return RuntimeInSmmFunc;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: DisableSmmCallbacks
//
// Description:
// This is signaled before booting or shell.
// This makes the SMM Base protocol register/unregister unsupported.
//
// Input:
// IN EFI_EVENT Event
// IN VOID *Context
//
// Output:
// VOID
//
//
// NOTE: This routine could be called multiple times right before booting.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID DisableSmmCallbacks(IN EFI_EVENT Event, IN VOID *Context)
{
#if SMM_USE_PI
EFI_HANDLE Handle = NULL;
#endif
//This should only run once, even if called by different events.
static BOOLEAN HasRun = FALSE;
if (HasRun) return;
pBS->CloseEvent(Event);
//If we are following PI1.1 model we should dispatch rest of SMM drivers whose DEPEX
//was not TRUE at the time of the first pass.
#if SMM_USE_PI
CallFuncInSmm(SmmDispatcher);
pBS->InstallMultipleProtocolInterfaces (
&Handle,
&gEfiDxeSmmReadyToLockProtocolGuid, NULL,
NULL
);
#endif
CallFuncInSmm(SetDisallowRegistering);
HasRun = TRUE;
}
#if SMM_USE_PI
VOID SmmIplGuidedEventNotify (
IN EFI_EVENT Event,
IN VOID *Context)
{
CallFuncInSmm(SmmDriverDispatchHandler);
}
#endif
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: LocateProtocols
//
// Description:
// Locate protocols needed for SMM Base.
//
// Input: VOID
//
// Output: VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID LocateProtocols()
{
EFI_STATUS Status;
#if SMM_USE_PI
Status = pBS->LocateProtocol(&gEfiSmmAccess2ProtocolGuid, NULL, &gSmmAccess);
ASSERT_EFI_ERROR(Status);
Status = pBS->LocateProtocol(&gEfiSmmControl2ProtocolGuid, NULL, &gSmmControl);
ASSERT_EFI_ERROR(Status);
#else
Status = pBS->LocateProtocol(&gEfiSmmAccessProtocolGuid, NULL, &gSmmAccess);
ASSERT_EFI_ERROR(Status);
Status = pBS->LocateProtocol(&gEfiSmmControlProtocolGuid, NULL, &gSmmControl);
ASSERT_EFI_ERROR(Status);
#endif
Status = pBS->LocateProtocol(&gEfiPe32ImageProtocolGuid, NULL, &gPe32Image);
ASSERT_EFI_ERROR(Status);
Status = pBS->LocateProtocol(&gEfiMpServicesProtocolGuid, NULL, &gMpServices);
ASSERT_EFI_ERROR(Status);
#if SMM_THUNK_IN_CSM == 1
Status = pBS->LocateProtocol(&gEfiLegacyBiosProtocolGuid, NULL, &gLegacyBios);
ASSERT_EFI_ERROR(Status);
Status = pBS->LocateProtocol(&gEfiLegacyBiosExtProtocolGuid, NULL, &gLegacyBiosExt);
ASSERT_EFI_ERROR(Status);
#endif
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: FindHobs
//
// Description: Locate the HOBs needed.
//
// Input: VOID
//
// Output: VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS FindHobs()
{
VOID *FirstHob;
EFI_STATUS Status;
//Parse hobs to get CPU Hob.
//Find first Hob
FirstHob = GetEfiConfigurationTable(pST,&gHobListGuid);
if (!FirstHob) {
TRACE((TRACE_ALWAYS, "SmmBase.Entry()Error:Unable to find CPUInfo HOB\n"));
return EFI_NOT_FOUND;
}
//Find SMM Hob.
gSmmHob = (SMM_HOB*) FirstHob;
while (!EFI_ERROR(Status = FindNextHobByType(EFI_HOB_TYPE_GUID_EXTENSION, &gSmmHob))) {
if (guidcmp(&((EFI_HOB_GUID_TYPE*)gSmmHob)->Name, &gSmmHobGuid) == 0) {
break;
}
}
if (EFI_ERROR(Status)) return Status;
//Find Cpu Info Hob.
gCpuinfoHob = (CPUINFO_HOB*) FirstHob;
while (!EFI_ERROR(Status = FindNextHobByType(EFI_HOB_TYPE_GUID_EXTENSION, &gCpuinfoHob))) {
if (guidcmp(&((EFI_HOB_GUID_TYPE*)gCpuinfoHob)->Name, &gCpuinfoHobGuid) == 0) {
break;
}
}
if (EFI_ERROR(Status)) return Status;
gCpuHob = (EFI_HOB_CPU*)FirstHob;
Status = FindNextHobByType(EFI_HOB_TYPE_CPU, &gCpuHob);
if (EFI_ERROR(Status)) return Status;
return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure: SmmInitEntry
//
// Description: SMM Installer Entry Point.
//
// Input:
// IN EFI_HANDLE ImageHandle
// IN EFI_SYSTEM_TABLE *SystemTable
//
// Output: EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS SmmInitEntry(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_EVENT BootEvent;
EFI_EVENT Event;
VOID *Registration;
VOID *RuntimeInSmmFunc;
#if SMM_USE_FRAMEWORK
EFI_SMM_BASE_PROTOCOL *RuntimeSmmBase;
#endif
InitAmiLib(ImageHandle,SystemTable);
gThisImageHandle = ImageHandle;
LocateProtocols();
Status = FindHobs();
ASSERT_EFI_ERROR(Status);
if (EFI_ERROR(Status)) return Status;
Status = gMpServices->WhoAmI(gMpServices, &gBsp);
ASSERT_EFI_ERROR(Status);
gBspSmmBase = gSmmHob->SmmBase[gSmmHob->Bsp];
gSmmEntryData = (SMM_ENTRY_INIT_STRUCT *) (gBspSmmBase + 0x8004);
// Allocate space in runtime services for gPrivate.
// This is needed because this pointer will be used by SMM during runtime.
Status = pBS->AllocatePool(EfiRuntimeServicesData, sizeof(SMM_BASE_PRIVATE_STRUCT), &gBasePrivate);
ASSERT_EFI_ERROR(Status);
// Copy the private structure. This does not copy the pointer. It copies the entire structure.
*gBasePrivate = gSmmBasePrivateStruct; //Make available during runtime.
InitgBasePrivate();
RuntimeInSmmFunc = CreateRuntimeInSmmFunction();
#if SMM_USE_FRAMEWORK
gEfiSmmBase.InSmm = (EFI_SMM_INSIDE_OUT)RuntimeInSmmFunc;
#endif
#if SMM_USE_PI
gEfiSmmBase2.InSmm = (EFI_SMM_INSIDE_OUT2)RuntimeInSmmFunc;
#endif
gBasePrivate->InSmm = (UINT8*)RuntimeInSmmFunc + InSmmFuncOffset;
Status = gSmmAccess->Open(gSmmAccess
#if SMM_USE_PI == 0
,0
#endif
);
ASSERT_EFI_ERROR(Status);
InitgDispatcherPrivate();
#if SMM_CACHE_SUPPORT == 1
#ifndef NEHALEM_CPU_MODULE
InitSmmCacheData();
#endif
#endif
LoadAndInitDispatcherImages(pST, gSmmHob->NumCpus);
gSmmAccess->Close(gSmmAccess
#if SMM_USE_PI == 0
, 0
#endif
);
// locks SMM area so no one else can change it
gSmmAccess->Lock(gSmmAccess
#if SMM_USE_PI == 0
, 0
#endif
);
//For security reasons SMM should call from inside SMM to outside SMM before option ROMs are loaded.
//The following callback is registered to disable this. When this is disabled, no new SMM drivers
//can be registered.
//This call back is only available since core 4.6.3.5.
Status = RegisterProtocolCallback(
&gBdsAllDriversConnectedProtocolGuid,
DisableSmmCallbacks,
NULL,
&Event,
&Registration
);
ASSERT_EFI_ERROR(Status);
//In cases where core does not support a call back before option roms are executed.
//The calling outside of SMM will be disabled before shell or a OS is loaded.
Status = CreateReadyToBootEvent(
TPL_CALLBACK,
DisableSmmCallbacks,
NULL,
&BootEvent
);
ASSERT_EFI_ERROR(Status);
#if SMM_USE_PI
//Setup Notification when BDS has finished 1st pass on loading drivers...
Status = pBS->CreateEventEx (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
SmmIplGuidedEventNotify,
NULL,
&gEfiEventDxeDispatchGuid,
&Event
);
ASSERT_EFI_ERROR(Status);
#endif
#if SMM_USE_FRAMEWORK
Status = pBS->AllocatePool(EfiRuntimeServicesData, sizeof(EFI_SMM_BASE_PROTOCOL), &RuntimeSmmBase);
ASSERT_EFI_ERROR(Status);
*RuntimeSmmBase = gEfiSmmBase;
#endif
Status = pBS->InstallMultipleProtocolInterfaces(
&ImageHandle,
#if SMM_USE_FRAMEWORK
&gEfiSmmBaseProtocolGuid, RuntimeSmmBase,
#endif
#if SMM_USE_PI
&gEfiSmmBase2ProtocolGuid, &gEfiSmmBase2,
#endif
NULL);
ASSERT_EFI_ERROR(Status);
return Status;
}
//*************************************************************************
//*************************************************************************
//** **
//** (C)Copyright 1985-2011, American Megatrends, Inc. **
//** **
//** All Rights Reserved. **
//** **
//** 5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093 **
//** **
//** Phone: (770)-246-8600 **
//** **
//*************************************************************************
//*************************************************************************
|