summaryrefslogtreecommitdiff
path: root/Core/EM/NVRAM/NVRAMPEI.c
blob: 53ebd2b544ecd1a5c15d10dcdf44ed3048b13ce6 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2013, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Core/Modules/NVRAM/NVRAMPEI.c 19    5/22/14 6:05p Oleksiyy $
//
// $Revision: 19 $
//
// $Date: 5/22/14 6:05p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Modules/NVRAM/NVRAMPEI.c $
// 
// 19    5/22/14 6:05p Oleksiyy
// [TAG]  		EIP170545
// [Category]  	Improvement
// [Description]  	Spelling errors corrected.
// [Files]  		NVRAMPEI.c
// 
// 18    1/04/13 10:35a Felixp
// Minor improvement in PeiInitNV: when CreateHob fails, still install the
// PPI.
// 
// 17    12/13/12 6:17p Oleksiyy
// [TAG]  		EIP109422
// [Category]  	Improvement
// [Description]  	Issues found by CppCheck in NVRAM Module part
// [Files]  		NVRAMDXE.c and NVRAMPEI.c
// 
// 16    8/12/11 12:15p Artems
// EIP 64107: Added changes for module to be compliant with UEFI
// specification v 2.3.1
// 
// 15    2/02/10 5:15p Oleksiyy
// EIP 33263 : Hooks for SetVariable, GetVariable and GetNextVarName
// added
// 
// 14    11/24/09 4:05p Oleksiyy
// EIP 26085: Added support to read All the NVRAM variable (including
// defoults) with GetNextVariable in PEI and DXE.
// 
// 13    11/23/09 6:25p Felixp
// 
// 12    11/23/09 4:44p Felixp
// Code to validate NVRAM header fields used by the NVRAM driver is added.
// If problem is found, the header fields are updated with the correct
// values.
// 
// 11    7/10/09 8:50a Felixp
// Code clean  up; comments and function header added
// 
// 10    6/26/09 3:29p Felixp
// Active varstore detection is moved into new IsMainNvramStoreValid
// function in NvramRead.c
// 
// 9     5/21/09 5:39p Felixp
// 1. Disable NVRAM content if it has been corrupted during failed BIOS
// update
// 2. Validate compatibility of the NVRAM data using external hook
// (IsNvramDataCompatible). 
//  Disable incompatible NVRAM content.
// 
// 8     1/16/09 3:39p Felixp
// New Feature. EIP 18483. Interface of the hooks that detect NVRAM
// operational mode is extended.
// NVRAM driver uses platform hooks(IsMfgMode, IsResetConfigMode,
// IsDefaultConfigMode) to detect, which set of configuration values 
// are to be used. In some cases the mode is detected based on value of
// the NVRAM variable.
// Interface of the hook functions is extended. New parameter is added. 
// Pointer to EFI_PEI_READ_ONLY_VARIABLE2_PPI PPI is passed to the hook
// functions. 
// The pointer can be used to read and enumerate existing NVRAM variables.
// 
// 7     10/09/08 11:47a Felixp
// 1. Fault tolerant NVRAM garbage collection support is added.
// Use FAULT_TOLERANT_NVRAM_UPDATE SDL token to enable/disable (disabled
// by default). 
// NOTE: This feature requires upgrade of the Board module. 
// NOTE: This feature requires porting:
// Fault tolerant update requires reserved flash area of size NVRAM_SIZE
// used for back up during NVRAM garbage collection.
// The address of the area is defined by NVRAM_BACKUP_ADDRESS SDL token
// defined in Board.sdl.
// Size and Base addresses of other firmware volumes may need to be
// adjusted to free up space for the NVRAM back up area.
// Default ROM layout expects NVRAM back up area immediately below the
// main NVRAM area.
// For projects with the customized ROM layout Core.mak has to be updated.
// 2. Top level NVRAM function updated to be reentrance-safe.
// NOTE: This feature requires upgrade of the SB module.
// 3. Improved recovery from the flash programming failures and
// corruptions within NVRAM area. 
// 4. More reliable non-fault tolerant garbage collection.
// 
// 6     6/06/08 11:01a Felixp
// 
// 4     2/19/08 11:07p Felixp
// Update in init routine PeiInitNV: during boot with default
// configuration, main NVRAM store is also exposed. This is needed to
// provide current value for variables without defaults.
// 
// 3     9/06/07 12:14a Felixp
// 
// 2     8/31/07 3:44p Felixp
// NVRAM code has been significantly changed to introduce the following
// improvements:
// 1. The code is chaned to always use the same amount of memory. 
// Previous implementation allocated memory as needed, which caused
// occasional S4 problems.
// Plus S4 resume never worked during the very first boot after the
// firmware update.
// 2. Support for NVRAM defaults added.
// NVRAM image with the default values for the Setup variables is now
// generated by the build process.
// The image is generated by the HpkTool (included into AMITSE module
// part). 
// In addition to standard Setup defaults it is also possible
// to generate manufactoring defaults. Support for the manufactoring
// defaults
// is disabled by default and can be enabled using
// MANUFACTURING_MODE_SUPPORT SDL token.
// 3. Support for boot with alternative configurations is added.
// Decision to switch to the alternative configuration
// (alternative set of values for NVRAM variables)
// is based on values returned by the porintg routine in OemPort.c.
// During boot with alternative configurations GetVariable service 
// returns alternative values for the setup-related variables.
// If variable does not have an alternative value, current value is
// returned.
// Two alternative configurations are supported:
// Boot with manufactoring settings(activated when IsMfgMode routine in
// OemPort.c returns TRUE).
// Boot with default settings(activated when IsDefaultConfigMode routine
// in OemPort.c returns TRUE).
// NOTE: This feature requires of the Board module
// 4.NVRAM reset option is added.
// If porting routine IsResetConfigMode in OemPort.c returns TRUE in PEI
// phase, 
// NVRAM content will be reset during initialization of the DXE NVRAM
// driver.
// During reset operation all setup-related variables are reset to their
// default values.
// All other variables are deleted.
// NOTE: This feature requires upgrade of the Board module
// 5.Detection of NVRAM update added.
// NVRAM implementation detects if NVRAM has been updated since the last
// NVRAM call.
// This provides ability to use variables services before and after
// firmware update.
// 6.Overall code clean up and simplification.
// 7.Core Sources are no longer required in order to use NV_SIMULATION
// option.
// 8.PI 1.0 support.
// Support for a PI 1.0 complient variable PPI is added. Old PPI is still
// preserved in this label for backward compatibility.
// New library routines PeiGetVariable and PeiGetNextVariableName are
// created in order to hide the differences between two PPIs.
// It is recommended to update existing code to use new library routines. 
// Support of the old PPI may be dropped in the future versions of Core.
// 9. NVRAM is now packaged as a raw FFS file embedded into the standard
// FV (used to be non-standard FV with raw NVRAM image).
// Validation: New NVRAM code has been validated using SCT and EFI version
// of Windows Server 2008
// Files modified: Core.sdl, Core.mak, AmiPeiLib.h, PeiLib.c,
// ReadOnlyVariable.h, , Token.c, HpkTool.exe, AMITSE.mak, Setup.ini,
//             NVRAM.sdl, NVRAM.mak, NVRAM.h, NVRAMRead.c, NVRAMPEI.c,
// NVRAMDXE.c
// 
// 1     8/24/06 12:38p Felixp
// 
// 3     10/09/05 11:25a Felixp
// Performance measurements added.
// 
// 2     3/04/05 9:42a Mandal
// 
// 1     1/28/05 12:44p Felixp
// 
// 2     1/18/05 3:22p Felixp
// PrintDebugMessage renamed to Trace
// 
// 1     12/27/04 4:08p Felixp
// 
// 3     4/05/04 1:10a Felixp
// 
// 2     4/04/04 2:58p Felixp
// 
// 1     3/29/04 2:32a Felixp
// 
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:	NVRAMPEI.h
//
// Description:	
//
//<AMI_FHDR_END>
//**********************************************************************
#include <PPI/ReadOnlyVariable2.h>
#include <PPI/ReadOnlyVariable.h>
#include <AmiPeiLib.h>
#include "NVRAM.h"

//Defined in CSPLib(OEMPort.c)
BOOLEAN IsMfgMode(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadVariablePpi
);
BOOLEAN IsResetConfigMode(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadVariablePpi
);
BOOLEAN IsDefaultConfigMode(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadVariablePpi
);
BOOLEAN IsNvramDataCompatible(
    IN EFI_PEI_SERVICES **PeiServices,
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadVariablePpi
);

//Defined in Tokens.c
extern UINTN NvramAddress;
extern UINTN NvramBackupAddress;
extern const UINTN NvramSize;
extern const BOOLEAN NvSimulation;

typedef struct{
    EFI_PEI_READ_ONLY_VARIABLE2_PPI Ppi;
    UINT32 InfoCount;
    UINT32 LastInfoIndex;
    NVRAM_STORE_INFO NvramInfo[3];
    NVRAM_STORE_INFO *MainInfo;
} VARIABLE_PPI;

//--- GetVariable and GetNextVarName Hooks ------
//============================================================================
// Type definitions
typedef EFI_STATUS (PEI_HOOK_GET_VARIABLE)(
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
    IN CONST CHAR16 *VariableName, IN CONST EFI_GUID *VendorGuid,
    OUT UINT32 *Attributes, IN OUT UINTN *DataSize, OUT VOID *Data
);

typedef EFI_STATUS (PEI_HOOK_GET_NEXT_VARIABLE_NAME)(
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
    IN OUT UINTN *VariableNameSize,
    IN OUT CHAR16 *VariableName, IN OUT EFI_GUID *VendorGuid
);

//======================================================================
// Global extern variables

extern PEI_HOOK_GET_VARIABLE PEI_GET_VAR_LIST EndOfGetVariableHook;
extern PEI_HOOK_GET_NEXT_VARIABLE_NAME PEI_GET_NEXT_VAR_NAME_LIST EndOfGetNextVarNameHook;

PEI_HOOK_GET_VARIABLE* PeiGetVariableHookList[]=
    {PEI_GET_VAR_LIST NULL};
PEI_HOOK_GET_NEXT_VARIABLE_NAME* PeiGetNextVarNameHookList[]=
    {PEI_GET_NEXT_VAR_NAME_LIST NULL};

EFI_STATUS PeiGetVariableHook(
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
    IN CONST CHAR16 *VariableName, IN CONST EFI_GUID *VendorGuid,
    OUT UINT32 *Attributes, IN OUT UINTN *DataSize, OUT VOID *Data
){
    UINTN i;
    EFI_STATUS Result = EFI_UNSUPPORTED;
    for(i=0; PeiGetVariableHookList[i] && (Result == EFI_UNSUPPORTED); i++) 
        Result = PeiGetVariableHookList[i](This,VariableName, VendorGuid,Attributes, DataSize, Data);
    return Result;
}

EFI_STATUS PeiGetNextVarNameHook(
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
    IN OUT UINTN *VariableNameSize,
    IN OUT CHAR16 *VariableName, IN OUT EFI_GUID *VendorGuid
){
    UINTN i;
    EFI_STATUS Result = EFI_UNSUPPORTED;
    for(i=0; PeiGetNextVarNameHookList[i] && (Result == EFI_UNSUPPORTED); i++) 
        Result = PeiGetNextVarNameHookList [i](This,VariableNameSize,VariableName, VendorGuid);
    return Result;
}
//---GetVariable and GetNextVarName Hooks END------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VarPpiGetVariable
//
// Description: This function searches for a Variable with specific GUID and Name
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - Double pointer to Pei Services instance
//              IN CHAR16 *VariableName - pointer to Var Name in Unicode
//              IN EFI_GUID *VendorGuid - pointer to Var GUID
//              OPTIONAL OUT UINT32* Attributes - Pointer to memory where Attributes will be returned 
//              IN OUT UINTN *DataSize - size of Var - if smaller than actual EFI_BUFFER_TOO_SMALL 
//              will be returned and DataSize will be set to actual size needed
//              OUT VOID *Data - Pointer to memory where Var will be returned
//
// Output:      EFI_STATUS - based on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VarPpiGetVariable(
	IN EFI_PEI_SERVICES **PeiServices,
	IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid,
	OUT UINT32 *Attributes OPTIONAL,
	IN OUT UINTN *DataSize, OUT VOID *Data
)
{
	return PeiGetVariable(
        PeiServices, VariableName, VendorGuid, Attributes, DataSize, Data
    );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VarPpiGetNextVariableName
//
// Description: This function searches for Var folowed by the Var with specific name (optional) 
//              and GUID and returns it's Name. 
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - Double pointer to Pei Services instance
//              IN OUT UINTN *VariableNameSize - size of Varname - if smaller than actual EFI_BUFFER_TOO_SMALL 
//              will be returned and DataSize will be set to actual size needed
//              IN OUT CHAR16 *VariableName - pointer where Var Name in Unicode will be stored
//              IN OUT EFI_GUID *VendorGuid - pointer to menory where Var GUID is stored
//
// Output:      EFI_STATUS - based on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VarPpiGetNextVariableName(
	IN EFI_PEI_SERVICES **PeiServices,
	IN OUT UINTN *VariableNameSize,
	IN OUT CHAR16 *VariableName, IN OUT EFI_GUID *VendorGuid
)
{
	return PeiGetNextVariableName(
        PeiServices, VariableNameSize, VariableName, VendorGuid
    );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VarPpi2GetVariable
//
// Description: This function searches for Var with specific GUID
//
// Input:       IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This - pointer to FI_PEI_READ_ONLY_VARIABLE2_PPI
//              IN CHAR16 *VariableName - pointer to Var Name in Unicode
//              IN EFI_GUID *VendorGuid - pointer to Var GUID
//              OPTIONAL OUT UINT32* Attributes - Pointer to memory where Attributes will be returned 
//              IN OUT UINTN *DataSize - size of Var - if smaller than actual EFI_BUFFER_TOO_SMALL 
//              will be returned and DataSize will be set to actual size needed
//              OUT VOID *Data - Pointer to memory where Var will be returned
//
// Output:      EFI_STATUS - based on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VarPpi2GetVariable(
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
    IN CONST CHAR16 *VariableName, IN CONST EFI_GUID *VendorGuid,
    OUT UINT32 *Attributes, IN OUT UINTN *DataSize, OUT VOID *Data
)
{
	EFI_STATUS Status;
    VARIABLE_PPI *VarPpi = (VARIABLE_PPI*)This;
    Status = PeiGetVariableHook (
                This, VariableName, VendorGuid, Attributes, DataSize, Data
             );
    if (Status != EFI_UNSUPPORTED) return Status;
	Status = NvGetVariable2(
				(CHAR16*)VariableName, (EFI_GUID*)VendorGuid, Attributes,
				DataSize, Data, VarPpi->InfoCount, VarPpi->NvramInfo
			 );
	return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   NvGetNextVariableName2
//
// Description: This function searches for Var with specific GUID and Enty number 
//              not betwin LastInfoIndex - InfoCount parameter and returns it's Name. 
//
// Input:       IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This - pointer to FI_PEI_READ_ONLY_VARIABLE2_PPI
//              IN OUT UINTN *VariableNameSize - size of Varname - if smaller than actual EFI_BUFFER_TOO_SMALL 
//              will be returned and DataSize will be set to actual size needed
//              IN OUT CHAR16 *VariableName - pointer where Var Name in Unicode will be stored
//              IN OUT EFI_GUID *VendorGuid - pointer to menory where Var GUID will be stored
//
// Output:      EFI_STATUS - based on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VarPpi2GetNextVariableName(
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
    IN OUT UINTN *VariableNameSize,
    IN OUT CHAR16 *VariableName, IN OUT EFI_GUID *VendorGuid
)
{
	EFI_STATUS Status;
    VARIABLE_PPI *VarPpi = (VARIABLE_PPI*)This;
    Status = PeiGetNextVarNameHook (
                This, VariableNameSize, VariableName, VendorGuid
             );
    if (Status != EFI_UNSUPPORTED) return Status;
    Status = NvGetNextVariableName2(
                 VariableNameSize, VariableName, VendorGuid,
                 VarPpi->InfoCount, VarPpi->NvramInfo,
                 &(VarPpi->LastInfoIndex),FALSE
		   );
	return Status;
}

// PPI interface definition
EFI_PEI_READ_ONLY_VARIABLE_PPI VariablePpi = {VarPpiGetVariable, VarPpiGetNextVariableName};
EFI_PEI_READ_ONLY_VARIABLE2_PPI Variable2Ppi = {VarPpi2GetVariable, VarPpi2GetNextVariableName};

// PPI to be installed
EFI_PEI_PPI_DESCRIPTOR VariablePpiListTemplate[] =
{ 
    {
        EFI_PEI_PPI_DESCRIPTOR_PPI,
	    &gEfiPeiReadOnlyVariable2PpiGuid, &Variable2Ppi
    },
    {
        EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
	    &gPeiReadOnlyVariablePpiGuid, &VariablePpi
    }
};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   PeiInitNV
//
// Description: This function inits NVRAM
//
// Input:       IN EFI_FFS_FILE_HEADER *FfsHeader - pointer to FfsHeader
//              IN EFI_PEI_SERVICES **PeiServices - double pointer to the Pei Sevices structure
//
// Output:      EFI_STATUS - based on result
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PeiInitNV (
	IN EFI_FFS_FILE_HEADER       *FfsHeader,
	IN EFI_PEI_SERVICES          **PeiServices
)
{
    EFI_STATUS Status;
    EFI_PEI_PPI_DESCRIPTOR *VariablePpiList;
    VARIABLE_PPI *VarPpi;
    UINT32 NvramMode=0;
    NVRAM_HOB *pHob;
    BOOLEAN ResetConfigMode;
    NVRAM_STORE_INFO MainNvram;
    VARIABLE_PPI TmpVarPpi;
    VOID *BackupNvramAddress = (VOID*)NvramBackupAddress;
    BOOLEAN BackupStoreValid;
    BOOLEAN NvramIsCorrupted = FALSE;

    Status = (*PeiServices)->AllocatePool(PeiServices, sizeof(VariablePpiListTemplate)+sizeof(VARIABLE_PPI), &VariablePpiList);
    if (EFI_ERROR(Status)) return Status;
    VariablePpiList[0]=VariablePpiListTemplate[0];
    VariablePpiList[1]=VariablePpiListTemplate[1];
    VarPpi = (VARIABLE_PPI*)(VariablePpiList + 2);
    VariablePpiList[0].Ppi = VarPpi;
    VarPpi->Ppi = Variable2Ppi;
    VarPpi->InfoCount = 0;
    VarPpi->LastInfoIndex = 0;
    MainNvram.NvramAddress = (UINT8*)NvramAddress;
    MainNvram.NvramSize = NvramSize;
    if (!IsMainNvramStoreValid(&MainNvram, BackupNvramAddress,&BackupStoreValid)){
        if (BackupStoreValid){
            VOID *Tmp = BackupNvramAddress;
            BackupNvramAddress = MainNvram.NvramAddress;
            MainNvram.NvramAddress = Tmp;
        }else{
            NvramIsCorrupted = TRUE;
        }
    }
    NvInitInfoBuffer(
        &MainNvram,
        NvramHeaderLength,
        NVRAM_STORE_FLAG_NON_VALATILE
    );
    //Check if FV signature is valid
    NvramIsCorrupted =   NvramIsCorrupted 
                      || ((EFI_FIRMWARE_VOLUME_HEADER*)MainNvram.NvramAddress)->Signature!=FV_SIGNATURE;
    if (NvramIsCorrupted){
        VarPpi->MainInfo = NULL;
        NvramMode=NVRAM_MODE_RESET_CONFIGURATION;
        PEI_TRACE((-1, PeiServices, "PEI: NVRAM header corruption is detected\n"));
    }else{
        TmpVarPpi = *VarPpi;
        TmpVarPpi.NvramInfo[0]=MainNvram;
        TmpVarPpi.MainInfo = &TmpVarPpi.NvramInfo[0];
        TmpVarPpi.InfoCount=1;
    
        if (   IsMfgMode(PeiServices,&TmpVarPpi.Ppi)
            && NvGetDefaultsInfo(
                   MfgDefaults,&MainNvram,&VarPpi->NvramInfo[VarPpi->InfoCount]
               ) != NULL
        ){
            VarPpi->InfoCount++;
            NvramMode|=NVRAM_MODE_MANUFACTORING;
        }
        ResetConfigMode = IsResetConfigMode(PeiServices,&TmpVarPpi.Ppi);
        if (!ResetConfigMode && !IsDefaultConfigMode(PeiServices,&TmpVarPpi.Ppi)){
            VarPpi->NvramInfo[VarPpi->InfoCount]=MainNvram;
            VarPpi->MainInfo = &VarPpi->NvramInfo[VarPpi->InfoCount];
            VarPpi->InfoCount++;
            if (NvGetDefaultsInfo(
                    StdDefaults,&MainNvram,&VarPpi->NvramInfo[VarPpi->InfoCount]
                ) != NULL
            ){
                VarPpi->InfoCount++;
            }
        }else{
            if (NvGetDefaultsInfo(
                    StdDefaults,&MainNvram,&VarPpi->NvramInfo[VarPpi->InfoCount]
                ) != NULL
            ){
                VarPpi->InfoCount++;
            }
            if (ResetConfigMode){
                VarPpi->MainInfo = NULL;
                NvramMode|=NVRAM_MODE_RESET_CONFIGURATION;
            }else{
                VarPpi->NvramInfo[VarPpi->InfoCount]=MainNvram;
                VarPpi->MainInfo = &VarPpi->NvramInfo[VarPpi->InfoCount];
                VarPpi->InfoCount++;
                NvramMode|=NVRAM_MODE_DEFAULT_CONFIGURATION;
            }
        }
        if (!IsNvramDataCompatible(PeiServices, &VarPpi->Ppi)){
            PEI_TRACE((-1, PeiServices, "PEI: Incompatible NVRAM detected\n"));
            VarPpi->MainInfo = NULL;
            VarPpi->InfoCount=0;
        }
    }
    Status=(*PeiServices)->CreateHob(PeiServices, EFI_HOB_TYPE_GUID_EXTENSION, sizeof(NVRAM_HOB),&pHob);
    ASSERT_PEI_ERROR(PeiServices,Status)
	if (!EFI_ERROR(Status)){
	    pHob->Header.Name=AmiNvramHobGuid;
	    pHob->NvramAddress=(EFI_PHYSICAL_ADDRESS)(UINTN)MainNvram.NvramAddress;
	    pHob->BackupAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)BackupNvramAddress;
	    pHob->NvramSize = MainNvram.NvramSize;
	    if (NvSimulation) NvramMode|=NVRAM_MODE_SIMULATION;
	    pHob->NvramMode = NvramMode;
	    pHob->HeaderLength = NvramHeaderLength;
	}
	return (*PeiServices)->InstallPpi(PeiServices,VariablePpiList);
}

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