summaryrefslogtreecommitdiff
path: root/Core/EM/SMBIOS/SmbiosDMIEditSupport/SmbiosDMIEdit.c
blob: 2e1eb931d1b301da2e6ec823764dfaa152e626db (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
//**********************************************************************//
//**********************************************************************//
//**                                                                  **//
//**        (C)Copyright 1985-2016, American Megatrends, Inc.         **//
//**                                                                  **//
//**                       All Rights Reserved.                       **//
//**                                                                  **//
//**        5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093         **//
//**                                                                  **//
//**                       Phone: (770)-246-8600                      **//
//**                                                                  **//
//**********************************************************************//
//**********************************************************************//

//**********************************************************************//
// $Header: /Alaska/SOURCE/Modules/SMBIOS/SmbiosDMIEditSupport/SmbiosDMIEdit.c 27    4/07/16 6:02p Davidd $
//
// $Revision: 27 $
//
// $Date: 4/07/16 6:02p $
//**********************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SMBIOS/SmbiosDMIEditSupport/SmbiosDMIEdit.c $
// 
// 27    4/07/16 6:02p Davidd
// [TAG]  		EIP231162
// [Category]  	New Feature
// [Description]  	Merge Aptio V Smbios -09 changes for Aptio 4
// 4.6.5.5_SMBIOS_40 release
// [Files]  		Smbios.c
// SmbiosDmiEdit.c
// SmbiosDmiEditFunc.c
//
// 26    4/04/16 11:43a Davidd
// [TAG]  		EIP262865
// [Category]  	Improvement
// [Description]  	[APTIO4][Smbios]DmiEdit needs changes as smiflash
// protocol is being changed to deny calls with Smm buffer
// [Files]  		SmbiosDMIEdit.mak
// SmbiosDMIEdit.h
// SmbiosDMIEdit.c
// SmbiosDMIEditFunc.c
//
// 25    2/17/15 1:15p Davidd
// [TAG]  		EIP205509
// [Category]  	Improvement
// [Description]  	Merge Aptio V Smbios EIP193807, 193858, 196901 changes
// into Aptio 4 Smbios
// [Files]  		SmbiosDmiEdit.sdl
// SmbiosDmiEdit.c
// SmbiosNvramFunc.c
// SmbiosGetFlashData.c
//
// 24    11/15/13 4:33p Davidd
// [TAG]           EIP143321
// [Category]      Improvement
// [Description]   Perform "CppCheck" on Smbios module for
// '4.6.5.1_SMBIOS_36' release
// [Files]         SmbiosBoard.c
//                 Smbios.c
//                 SmbiosDMIEdit.c
//                 SmbiosDMIEditFunc.c
//                 SmbiosNvramFunc.c
//
// 23    6/05/13 2:54p Davidd
// [TAG]  		EIP125802
// [Category]  	Improvement
// [Description]  	Privilege escalation into SMM via SMBIOS SMM driver -
// BugID 305325
// [Files]  		SmbiosDMIEdit.c
//
// 22    6/08/12 6:00p Davidd
// [TAG]  		EIP88664
// [Category]  	New Feature
// [Description]  	Need tool to update smbios information
// [Files]  		SmbiosDMIEdit.c
// SmbiosDMIEdit.h
// SmbiosDMIEdit.sdl
// SmbiosDMIEditFunc.c
//
// 21    6/06/12 3:07p Davidd
// [TAG]  		EIP81954
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	WriteOnceTable is not supported when
// SMBIOS_DMIEDIT_DATA_LOC is 2
// [RootCause]  	WriteOnce is not properly checked
// [Solution]  	Changes added to check for WriteOnce status
// [Files]  		SmbiosDMIEdit.c
// SmbiosDMIEditFunc.c
//
// 20    12/19/11 11:23a Davidd
// [TAG]  		EIP78619
// [Category]  	Improvement
// [Description]  	Customer and project name in SmbiosDMIEdit.c file
// [Files]  		SmbiosDMIEdit.c
//
// 19    10/06/11 4:48p Davidd
// EIP65648 extended change.
//
// 18    8/30/11 4:14p Davidd
// [TAG]           EIP65648
// [Category]      Improvement
// [Description]   Update SMBIOS eModules for uEFI 2.3.1 / PI 1.2
// compliance
// [Files]         Smbios.c
//                 Smbios.dxs
//                 SmbiosDMIEdit.c
//                 SmbiosDMIEditFunc.c
//                 SmbiosGetFlashData.c
//                 SmbiosGetFlashData.dxs
//
// 17    8/03/11 10:30a Davidd
// [TAG]           EIP63920
// [Category]      Bug Fix
// [Severity]      Non-Urgent
// [Symptom]       uEFI OS can't support DMI edit tool
// [RootCause]     UEFI OS runs in virtual memory mode. So call to
// GetVariale will crash.
// [Solution]      Added call to InitSmmHandle so pRS will be properly
// converted per Runtime Services Table in SMM mode.
// [Files]         SmbiosDMIEdit.c
//
// 16    3/16/11 3:35p Davidd
// [TAG]           EIP53939
// [Category]      Bug Fix
// [Severity]      Normal
// [Symptom]       SMBIOS DMIEdit Driver always assumes FLASH_BLOCK_SIZE =
// 64KB
// [RootCause]     Block size was assumed to be 64K
// [Solution]      Problem has been fixed with code changes
// [Files]         SmbiosDMIEdit.c
//                 SmbiosDMIEditFunc.c
//
// 15    5/18/10 5:11p Davidd
// Added PnP function 52h commands 3 and 4 support - EIP 38010.
//
// 14    6/02/09 11:18a Davidd
// Updated function headers. (EIP 22180)
//
// 13    1/28/09 11:52a Davidd
// New changes added to support DMIEdit data storage location in flash
// selectable via SMBIOS_DMIEDIT_DATA_LOC SDL token
//
// 12    9/26/08 10:43a Davidd
// Renamed shadow enable / disable functions.
//
// 11    11/27/07 10:26a Davidd
//
// 10    11/19/07 11:53a Robert
//
// 9     10/22/07 4:27p Davidd
// Fixed a hanging problem with DMIEdit running on server2003 64bit SP2 -
// EIP 10754
//
// 8     3/29/07 6:06p Davidd
// Changed the year in the AMI banner.
//
// 7     12/15/06 5:42p Davidd
// Code cleanup and reformatted to coding standard.
//
// 6     3/21/06 8:43p Fasihm
// Added the directives for DevicePath.h to build and changed the protocol
// names to be compatable with the new Aptio 4.5 Core and later.
//
// 5     3/02/06 1:55p Davidd
// Updated include path to SMBios.h. It has been moved to
// Include\Protocol
//
// 4     8/10/05 10:40a Davidd
// Added code to call porting hooks to enable/disable F000 shadow to allow
// updating the SMBIOS Entry Point header.
//
// 3     7/20/05 1:22p Davidd
// Corrected build error with the last change made to detect which CPU is
// running.
//
// 2     7/12/05 11:20a Markw
// Add code to detect which CPU is executing.
//
// 1     4/29/05 2:06p Davidd
// Initial checkin.
//
//**********************************************************************//

#include <AmiDxeLib.h>
#include <Token.h>
#include <AmiHobs.h>
#include <Smm.h>
#include <AmiSmm.h>
#if PI_SPECIFICATION_VERSION >= 0x0001000a && SMM_MINOR_VER >= 43
    #include <Protocol\SmmCpu.h>
    #include <Protocol\SmmBase2.h>
    EFI_SMM_SYSTEM_TABLE2           *mSmst;
#else
    #include <Protocol\SmmBase.h>
    EFI_SMM_SYSTEM_TABLE            *mSmst;
#endif
#include <Protocol\SmmSwDispatch.h>
#include <Protocol\LoadedImage.h>
#include "Protocol\Smbios.h"
#include "SmbiosDMIEdit.h"

#define SMBIOS_SIG (UINT32)'_MS_'

extern  SMBIOS_TABLE_ENTRY_POINT    *SmbiosTableEntryPoint;
extern  UINT8                       *ScratchBufferPtr;
extern  UINT16                      MaximumBufferSize;
extern  BOOLEAN                     SwSmiMethod;

extern VOID EnableShadowWrite();
extern VOID DisableShadowWrite();

EFI_GUID gSwSmiCpuTriggerGuid = SW_SMI_CPU_TRIGGER_GUID;

#if !defined(SMBIOS_DMIEDIT_DATA_LOC) || (SMBIOS_DMIEDIT_DATA_LOC != 2)
#include <Protocol\SmbiosGetFlashDataProtocol.h>
#include <Protocol\SmiFlash.h>
#include <Protocol\FlashProtocol.h>

EFI_GUID                        gSmbiosFlashDataProtocolGuid = EFI_SMBIOS_FLASH_DATA_PROTOCOL_GUID;
EFI_SMBIOS_FLASH_DATA_PROTOCOL  *mSmbiosFlashDataProtocol;
EFI_SMI_FLASH_PROTOCOL          *mSmiFlash;
FLASH_PROTOCOL 					*mFlash = NULL;

#if defined(NonSmiDmiEdit_Support) && (NonSmiDmiEdit_Support == 1)
EFI_GUID                        FlashProtocolGuid = FLASH_PROTOCOL_GUID;
FLASH_PROTOCOL                  *FlashProtocol;
#endif                                  // NonSmiDmiEdit_Support
#endif                                  // SMBIOS_DMIEDIT_DATA_LOC

#if AmiBufferValidationLib_SUPPORT
	#include <Include/AmiBufferValidationLib.h>

    extern EFI_STATUS EFIAPI InitAmiBufferValidationLib(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable);
    extern EFI_STATUS        AmiValidateMemoryBuffer(VOID* Buffer, UINTN BufferSize);
#else
    EFI_PHYSICAL_ADDRESS        TsegStart = 0;
    EFI_PHYSICAL_ADDRESS        TsegEnd = 0;
#endif

#if defined(NonSmiDmiEdit_Support) && (NonSmiDmiEdit_Support == 1)
EFI_GUID    gEfiAmiDmieditSmbiosProtocolGuid = AMI_DMIEDIT_SMBIOS_GUID;

EFI_SMBIOS_DMIEDIT_PROTOCOL     SmbiosDmieditProtocol = {DmiEditNonSmiHandler};
#endif                                  // NonSmiDmiEdit_Support

VOID WriteOnceStatusInit(VOID);

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetSmbiosTableF000
//
// Description: Searches 0x0F0000 for the SMBIOS signature and fills in the
//              SmbiosTableEntryPoint once it has been found
//
// Input:       None
//
// Output:      None
//
// Modified: SmbiosTableEntryPoint -is modified if table entry point found
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
GetSmbiosTableF000 (VOID)
{
    UINT32              *ptr32;
    UINT16              i;
    static BOOLEAN      NotDone = TRUE;

    if (NotDone) {
        ptr32 = (UINT32*)0xF0000;
        for (i = 0; i < (0x10000 / 16); i++) {
            if (*ptr32 == SMBIOS_SIG) {
                SmbiosTableEntryPoint = (SMBIOS_TABLE_ENTRY_POINT*)ptr32;
                NotDone = FALSE;
                break;
            }

            ptr32 += 4;
        }
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CheckAddress
//
// Description: Check address to avoid TSEG area.
//
// Input:
//  Address     - starting address
//  Function    - DMIEdit function
//
// Output:
//  EFI_SUCCESS         - Access granted
//  DMI_BAD_PARAMETER   - Access denied!
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT16
CheckAddress (
    IN UINT8 *Address,
    IN UINT8 Function
)
{
    UINT16  Status = EFI_SUCCESS;

#if AmiBufferValidationLib_SUPPORT
{
	if (AmiValidateMemoryBuffer(Address, 1)) {
		Status = DMI_BAD_PARAMETER;
	}
	else {
		switch(Function) {
			case 0x50:  {
							if (AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_INFO*)Address)->DmiBiosRevision32BitAddr, 1) |
								AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_INFO*)Address)->NumStructures32BitAddr, 1) |
								AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_INFO*)Address)->StructureSize32BitAddr, 1) |
								AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_INFO*)Address)->DmiStorageBase32BitAddr, 1) |
								AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_INFO*)Address)->DmiStorageSize32BitAddr, 1)) {
								Status = DMI_BAD_PARAMETER;
							}
							break;
						}
			case 0x51:  {
							if (AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_STRUCTURE*)Address)->Handle32BitAddr, 1)) {
								Status = DMI_BAD_PARAMETER;
							}
							if (AmiValidateMemoryBuffer((UINT8*)((GET_SMBIOS_STRUCTURE*)Address)->Buffer32BitAddr, 1)) {
								Status = DMI_BAD_PARAMETER;
							}
						}
		}
	}

    return Status;
}
#else
{
    if (TsegStart != 0) {
        //
        // Check address against TSEG
        //
        if ((EFI_PHYSICAL_ADDRESS)Address >= TsegStart &&
            (EFI_PHYSICAL_ADDRESS)Address <= TsegEnd) {
            Status = DMI_BAD_PARAMETER;
        }
        else {
            switch(Function) {
                case 0x50:  {
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->DmiBiosRevision32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->DmiBiosRevision32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->NumStructures32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->NumStructures32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->StructureSize32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->StructureSize32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->DmiStorageBase32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->DmiStorageBase32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->DmiStorageSize32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_INFO*)Address)->DmiStorageSize32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                                break;
                            }
                case 0x51:  {
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_STRUCTURE*)Address)->Handle32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_STRUCTURE*)Address)->Handle32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                                if ((EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_STRUCTURE*)Address)->Buffer32BitAddr) >= TsegStart &&
                                    (EFI_PHYSICAL_ADDRESS)(((GET_SMBIOS_STRUCTURE*)Address)->Buffer32BitAddr) <= TsegEnd) {
                                    Status = DMI_BAD_PARAMETER;
                                }
                            }
            }
        }
    }

    return Status;
}
#endif
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SmiHandler
//
// Description: Handles the SMI
//
// Input:       IN EFI_HANDLE                    DispatchHandle
//              IN EFI_SMM_SW_DISPATCH_CONTEXT   *DispatchContext
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
SmiHandler (
    IN  EFI_HANDLE                    DispatchHandle,
    IN  EFI_SMM_SW_DISPATCH_CONTEXT   *DispatchContext
)
{
    SW_SMI_CPU_TRIGGER      *SwSmiCpuTrigger;
    UINT8                   Data;
    VOID                    *pInterface;
    UINTN                   i;
    UINT16                  RetStatus;
#if PI_SPECIFICATION_VERSION >= 0x0001000a && SMM_MINOR_VER >= 43
    UINTN                   Cpu = mSmst->CurrentlyExecutingCpu;   // For PI 1.1
    EFI_GUID                SmmCpuProtocolGuid = EFI_SMM_CPU_PROTOCOL_GUID;
    EFI_SMM_CPU_PROTOCOL    *SmmCpuProtocol;
#else
	EFI_SMM_CPU_SAVE_STATE	*pCpuSaveState;
    UINTN                   Cpu = mSmst->CurrentlyExecutingCpu - 1;
#endif

    GetSmbiosTableF000();

 	for (i = 0; i < mSmst->NumberOfTableEntries; ++i) {
		if (guidcmp(&mSmst->SmmConfigurationTable[i].VendorGuid,&gSwSmiCpuTriggerGuid) == 0) {
			break;
		}
 	}

    // If found table, check for the CPU that caused the software Smi.
 	if (i != mSmst->NumberOfTableEntries) {
		SwSmiCpuTrigger = mSmst->SmmConfigurationTable[i].VendorTable;
		Cpu = SwSmiCpuTrigger->Cpu;
 	}

#if PI_SPECIFICATION_VERSION >= 0x0001000a && SMM_MINOR_VER >= 43
    mSmst->SmmLocateProtocol(
                        &SmmCpuProtocolGuid,
                        NULL,
                        &SmmCpuProtocol
                        );

    pInterface = 0;         // Clear upper 64-bits.
    SmmCpuProtocol->ReadSaveState(
                            SmmCpuProtocol,
                            4,
                            EFI_SMM_SAVE_STATE_REGISTER_RBX,
                            Cpu,
                            &pInterface
                            );
#else
	pCpuSaveState = mSmst->CpuSaveState;
	pInterface = (void*)pCpuSaveState[Cpu].Ia32SaveState.EBX;
#endif

    SwSmiMethod = TRUE;

	Data = (UINT8)DispatchContext->SwSmiInputValue;

    RetStatus = CheckAddress(pInterface, Data);

    if (RetStatus == EFI_SUCCESS) {
        switch(Data) {
            case 0x50:
                        RetStatus = GetSmbiosInfo(pInterface);
                        break;
            case 0x51:
                        RetStatus = GetSmbiosStructure(pInterface);
                        break;
            case 0x52:
                        EnableShadowWrite();
                        RetStatus = SetSmbiosStructure(pInterface);
                        DisableShadowWrite();
        }
    }

#if PI_SPECIFICATION_VERSION >= 0x0001000a && SMM_MINOR_VER >= 43
    SmmCpuProtocol->WriteSaveState(
                            SmmCpuProtocol,
                            2,
                            EFI_SMM_SAVE_STATE_REGISTER_RAX,
                            Cpu,
                            &RetStatus
                            );
#else
   (UINT16)pCpuSaveState[Cpu].Ia32SaveState.EAX = RetStatus;
#endif
}

#if defined(NonSmiDmiEdit_Support) && (NonSmiDmiEdit_Support == 1)
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   DmiEditNonSmiHandler
//
// Description: Handles the SMI
//
// Input:       IN UINT8    Data
//              IN UINT64   pCommBuff
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32
DmiEditNonSmiHandler(
    IN UINT8    Data,
    IN UINT64   pCommBuff
)
{
    UINT32      eax;
    VOID        *pInterface;

	TRACE((-1, "SMISMBIOSHandler Data: 0x%X \n",Data));
	pInterface = (void*)pCommBuff;

    GetSmbiosTableF000();

    SwSmiMethod = FALSE;

    switch(Data) {
        case 0x50:
                    eax = GetSmbiosInfo(pInterface);
	TRACE((-1, "eax : 0x%X \n",eax));
                    break;
        case 0x51:
                    eax = GetSmbiosStructure(pInterface);
	TRACE((-1, "eax : 0x%X \n",eax));
                    break;
        case 0x52:
                    EnableShadowWrite();
                    eax = SetSmbiosStructure(pInterface);
                    DisableShadowWrite();
	TRACE((-1, "eax : 0x%X \n",eax));
    }
    return eax;
}
#endif                                  // NonSmiDmiEdit_Support

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   NotInSmmFunction
//
// Description: Performs non-smi initialization
//
// Input:       IN EFI_HANDLE           ImageHandle,
//              IN EFI_SYSTEM_TABLE     *SystemTable
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
NotInSmmFunction(
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable
)
{
#if defined(NonSmiDmiEdit_Support) && (NonSmiDmiEdit_Support == 1)
    EFI_STATUS  Status;

#if !defined(SMBIOS_DMIEDIT_DATA_LOC) || SMBIOS_DMIEDIT_DATA_LOC != 2
    Status = pBS->LocateProtocol (&FlashProtocolGuid, NULL, &FlashProtocol);
    ASSERT_EFI_ERROR(Status);
#endif                                  // SMBIOS_DMIEDIT_DATA_LOC

	Status = pBS->InstallMultipleProtocolInterfaces(
                 &ImageHandle,
                 &gEfiAmiDmieditSmbiosProtocolGuid,&SmbiosDmieditProtocol,
                 NULL
                 );

    return Status;
#endif                                  // NonSmiDmiEdit_Support

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InSmmFunction
//
// Description: Initialize pointers and register SW SMI handlers for
//              DMIEdit support.
//
// Input:       IN EFI_HANDLE           ImageHandle,
//              IN EFI_SYSTEM_TABLE     *SystemTable
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
InSmmFunction(
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable
)
{
    EFI_STATUS                      Status;
	EFI_GUID                        gEfiSmmSwDispatchProtocolGuid = EFI_SMM_SW_DISPATCH_PROTOCOL_GUID;
    EFI_SMM_SW_DISPATCH_PROTOCOL    *SwDispatch;
#if REGISTER_SW_SMI_FN50 || REGISTER_SW_SMI_FN51 || REGISTER_SW_SMI_FN52
    EFI_HANDLE                      SwHandle;
    EFI_SMM_SW_DISPATCH_CONTEXT     SwContext;
#endif

#if PI_SPECIFICATION_VERSION >= 0x0001000a && SMM_MINOR_VER >= 43
    EFI_SMM_BASE2_PROTOCOL          *SmmBase;
	EFI_GUID						gEfiSmmBaseProtocolGuid = EFI_SMM_BASE2_PROTOCOL_GUID;
#else
    EFI_SMM_BASE_PROTOCOL           *SmmBase;
	EFI_GUID						gEfiSmmBaseProtocolGuid = EFI_SMM_BASE_PROTOCOL_GUID;
#endif

#if !defined(SMBIOS_DMIEDIT_DATA_LOC) || SMBIOS_DMIEDIT_DATA_LOC != 2
    EFI_GUID                        gEfiSmiFlashProtocolGuid = EFI_SMI_FLASH_GUID;
    EFI_GUID                        gFlashSmmProtocolGuid = FLASH_SMM_PROTOCOL_GUID;

    Status = pBS->LocateProtocol (&gEfiSmiFlashProtocolGuid, NULL, &mSmiFlash);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) {
        return Status;
    }

    Status = pBS->LocateProtocol (&gFlashSmmProtocolGuid, NULL, &mFlash);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) {
        return Status;
    }
#endif                                  // SMBIOS_DMIEDIT_DATA_LOC

#if AmiBufferValidationLib_SUPPORT
    Status = InitAmiBufferValidationLib(ImageHandle, SystemTable);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) {
        return Status;
    }
#endif

    Status = pBS->LocateProtocol (&gEfiSmmBaseProtocolGuid, NULL, &SmmBase);
    ASSERT_EFI_ERROR(Status);
    if(EFI_ERROR(Status)) {
        return Status;
    }

    SmmBase->GetSmstLocation (SmmBase, &mSmst);   // Save the system table pointer

    // Register the SW SMI handler
    Status = pBS->LocateProtocol (&gEfiSmmSwDispatchProtocolGuid, NULL, &SwDispatch);
    ASSERT_EFI_ERROR(Status);

#if REGISTER_SW_SMI_FN50
    SwContext.SwSmiInputValue = 0x50;
    Status = SwDispatch->Register (SwDispatch, SmiHandler, &SwContext, &SwHandle);

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

#if REGISTER_SW_SMI_FN51
    SwContext.SwSmiInputValue = 0x51;
    Status = SwDispatch->Register (SwDispatch, SmiHandler, &SwContext, &SwHandle);

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

#if REGISTER_SW_SMI_FN52
    SwContext.SwSmiInputValue = 0x52;
    Status = SwDispatch->Register (SwDispatch, SmiHandler, &SwContext, &SwHandle);

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

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SmbiosDmiEditSupportInstall
//
// Description: DMIEdit support driver entry point
//
// Input:       IN EFI_HANDLE           ImageHandle,
//              IN EFI_SYSTEM_TABLE     *SystemTable
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
SmbiosDmiEditSupportInstall(
    IN EFI_HANDLE           ImageHandle,
	IN EFI_SYSTEM_TABLE     *SystemTable
)
{
    EFI_STATUS              Status;
    EFI_GUID                gEfiSmbiosProtocolGuid  = EFI_SMBIOS_PROTOCOL_GUID;
    EFI_SMBIOS_PROTOCOL     *SmbiosProtocol;

    InitAmiLib(ImageHandle, SystemTable);

    WriteOnceStatusInit();

    Status = pBS->LocateProtocol (&gEfiSmbiosProtocolGuid, NULL, &SmbiosProtocol);
    ASSERT_EFI_ERROR(Status);
    if(EFI_ERROR(Status)) {
        return Status;
    }

    SmbiosTableEntryPoint = SmbiosProtocol->SmbiosGetTableEntryPoint();
    ScratchBufferPtr = SmbiosProtocol->SmbiosGetScratchBufferPtr();
	MaximumBufferSize = SmbiosProtocol->SmbiosGetBufferMaxSize();

#if !defined(AmiBufferValidationLib_SUPPORT) || (AmiBufferValidationLib_SUPPORT == 0)
{
    CPUINFO_HOB             *CpuInfoHob = NULL;
    EFI_GUID                CpuInfoHobGuid = AMI_CPUINFO_HOB_GUID;
    EFI_GUID                HobListGuid = HOB_LIST_GUID;

    CpuInfoHob = (CPUINFO_HOB*)GetEfiConfigurationTable(pST, &HobListGuid);
    if (CpuInfoHob != NULL) {
        Status = FindNextHobByGuid(&CpuInfoHobGuid,(VOID**)&CpuInfoHob);
        if (Status == EFI_SUCCESS) {
            TsegStart = CpuInfoHob->TsegAddress;
            TsegEnd = CpuInfoHob->TsegAddress + CpuInfoHob->TsegSize;
        }
    }
}
#endif

#if !defined(SMBIOS_DMIEDIT_DATA_LOC) || SMBIOS_DMIEDIT_DATA_LOC != 2
    Status = pBS->LocateProtocol (&gSmbiosFlashDataProtocolGuid, NULL, &mSmbiosFlashDataProtocol);
    ASSERT_EFI_ERROR(Status);
    mSmbiosFlashDataProtocol->GetFlashTableInfo(mSmbiosFlashDataProtocol, &gFlashData, &gFlashDataSize);
#endif                                  // SMBIOS_DMIEDIT_DATA_LOC

    InitSmmHandler(ImageHandle, SystemTable, InSmmFunction, NotInSmmFunction);

    return EFI_SUCCESS;
}

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