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

//**********************************************************************//
// $Header: /Alaska/SOURCE/Modules/SMBIOS/SmbiosDMIEditSupport/SmbiosNvramFunc.c 11    2/17/15 1:16p Davidd $
//
// $Revision: 11 $
//
// $Date: 2/17/15 1:16p $
//**********************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SMBIOS/SmbiosDMIEditSupport/SmbiosNvramFunc.c $
// 
// 11    2/17/15 1:16p 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
//
// 10    11/15/13 4:34p 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
//
// 9     9/04/12 11:05a Davidd
// [TAG]  		EIP96286
// [Category]  	Improvement
// [Description]  	Please help to reserve DMI Data for AFUDOS with /r in
// Capsule Mode
// [Files]  		Smbios.sdl
// SmbiosDMIEdit.mak
// SmbiosNvramFunc.c
//
// 8     8/02/12 12:48p Davidd
// [TAG]  		EIP96064
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	SMBIOS: DmiEdit support creates NVRAM variables with names
// of incorrect length
// [RootCause]  	Swprintf_s function creates 15 characters variable name
// with NULL terminator in last byte.
// [Solution]  	Use Swprintf function instead
// [Files]  		Smbios.c
// SmbiosDMIEditFunc.c
// SmbiosNvramFunc.c
//
// 7     11/17/11 2:41p Davidd
// [TAG]           EIP74579
// [Category]      Improvement
// [Description]   Update SMBIOS moudule to let AMDELNX support SMBIOS
// spec 2.7
//                 (remove the 64 characters string limitation)
// [Files]         Smbios.h
//                 SmbiosStaticData.sdl
//                 Smbios.c
//                 SMBios.dxs
//                 SmbiosDMIEdit.sdl
//                 SmbiosDMIEdit.h
//                 SmbiosDMIEditFunc.c
//                 SmbiosNvram.c
//                 SmbiosFlashData.sdl
//
// 6     5/04/11 3:20p Davidd
// [TAG]           EIP57144
// [Category]      NEW FEATURE
// [Description]   Allow SMBIOS Type 39 to be modified using DMIEdit
// [Files]         SmbiosBoard.c
//                 Smbios.h
//                 SmbiosDynamicData.h
//                 Smbios.c
//                 SmbiosDmieditFunc.c
//                 SmbiosNvramFunc.c
//
// 5     3/02/11 11:47a Davidd
// [TAG]  		EIP54264
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Data is assumed valid without checking after some
// GetVariable calls
// [RootCause]  	No error checking
// [Solution]  	Problem has been fixed with code changes
// [Files]  		SmbiosNvramFunc.c
// SmbiosDMIEditFunc.c
//
// 4     1/14/11 3:57p Davidd
// [TAG]  		EIP50564
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	BIOS can't keep DMI data after flash BIOS with /p /b /n /r
// parameters
// [RootCause]  	Side effect of previous changes (NVRAM variable name
// format
// has been changed)
// [Solution]  	Problem has been fixed with code changes
// [Files]  		SmbiosNvramFunc.c
//
// 3     10/21/10 11:36a Davidd
// [TAG]           EIP46394
// [Category]      BUG FIX
// [Severity]      Important
// [Symptom]       Incorrect variable size for GetVariable() usage in
// Smbios module
// [RootCause]     GetVariable is called with incorrect "DataSize" type.
// [Solution]      Corrected "DataSize" type in all GetVariable calls.
// [Files]
//    Smbios.c
//    SmbiosDMIEditFunc.c
//    SmbiosNvramFunc.c
//
// 2     12/04/09 3:28p Davidd
// Corrected the DMIEdit data not updated after being updated 5-6 times
// (when NVRAM is used to store DMIEdit data) - EIP 30837.
//
// 1     2/02/09 4:21p Davidd
// Initial checkin
//
//**********************************************************************//

#include <AmiDxeLib.h>
#include <Token.h>
#include "Protocol\Smbios.h"
#include "SmbiosDMIEdit.h"

#if SMBIOS_DMIEDIT_DATA_LOC == 2
static BOOLEAN      DmiEditVarPresent =  FALSE;
EFI_GUID            gEfiSmbiosNvramGuid = EFI_SMBIOS_NVRAM_DATA_GUID;
CHAR16              *DmiArrayVar = L"DmiArray";
DMI_VAR             DmiArray[DMI_ARRAY_COUNT] = {0};
UINTN               DmiArraySize = DMI_ARRAY_COUNT * sizeof(DMI_VAR);
CHAR16              *S1 = L"                ";
UINT8               DmiData[0x1000];
UINT8               *DmiDataPtr;
UINT8               Buffer[0x400];
UINTN               BufferSize;
UINTN               DmiDataLength[DMI_ARRAY_COUNT];
UINT8               gRecoveryKeepDMIFlag = FALSE;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   PreserveDmiEditData
//
// Description: Preserve the DMIEdit data by loading its data into memory
//              prior to flashing
//
// Input:       None
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
PreserveDmiEditData (VOID)
{
    EFI_STATUS  Status;
    UINT8       Count;
    UINT8       Index;

	//
	// Get number of DMI data records in NVRAM
	//
	// Note: DMI data record actually starts with record #1,
	//		 first record #0 holds total number of DMI data records
	//       instead of TABLE_INFO
	//       ===> DmiArray[0].Type = count
	//
    Status = pRS->GetVariable(
                        DmiArrayVar,
                        &gEfiSmbiosNvramGuid,
                        NULL,
                        &DmiArraySize,
                        &DmiArray[0]);

    if (Status == EFI_SUCCESS) {
        DmiDataPtr = DmiData;

        Count = DmiArray[0].Type;     // Note: DmiArray[0] has count #

		// Get DMI data into memory. The data will be saved back into
		// NVRAM later in RestoreDmiEditData
        for (Index = 0; Index < Count; Index++) {
            Swprintf(S1, L"DmiVar%02x%04x%02x%02x",
					   DmiArray[Index + 1].Type,
					   DmiArray[Index + 1].Handle,
					   DmiArray[Index + 1].Offset,
					   DmiArray[Index + 1].Flags);

            BufferSize = sizeof(Buffer);
            Status = pRS->GetVariable(
                                S1,
                                &gEfiSmbiosNvramGuid,
                                NULL,
                                &BufferSize,
                                &Buffer);

			if (Status == EFI_SUCCESS) {
                MemCpy(DmiDataPtr, Buffer, BufferSize);
				DmiDataLength[Index] = BufferSize;
                DmiDataPtr += BufferSize;
			}
			else {
				DmiDataLength[Index] = 0;
			}
        }

        DmiEditVarPresent = TRUE;
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RestoreDmiEditData
//
// Description: Restore the DMIEdit data in NVRAM with data previously loaded
//              in memory by PreserveDmiEditData.
//
// Input:       None
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
RestoreDmiEditData (VOID)
{
    UINT8       Count;
    UINT8       Index;

	// DMI data were read and saved in memory in PreserveDmiEditData.
	// Now save DMI data back into NVRAM if present
    if (DmiEditVarPresent) {
        pRS->SetVariable(
                    DmiArrayVar,
                    &gEfiSmbiosNvramGuid,
                    EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
                    DmiArraySize,
                    &DmiArray[0]);

        DmiDataPtr = DmiData;

        Count = DmiArray[0].Type;     // Note: DmiArray[0] has count # instead of Type/Offset

        for (Index = 0; Index < Count; Index++) {
            Swprintf(S1, L"DmiVar%02x%04x%02x%02x",
					   DmiArray[Index + 1].Type,
					   DmiArray[Index + 1].Handle,
					   DmiArray[Index + 1].Offset,
					   DmiArray[Index + 1].Flags);

            pRS->SetVariable(
                        S1,
                        &gEfiSmbiosNvramGuid,
                        EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
						DmiDataLength[Index],
                        DmiDataPtr);

            DmiDataPtr += DmiDataLength[Index];
        }
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RecoveryPreserveDmiEditData
//
// Description: Preserve the DMIEdit data by loading its data into memory
//              prior to flashing (for capsule mode)
//
// Input:       None
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
RecoveryPreserveDmiEditData (VOID)
{
    EFI_STATUS      Status;
    EFI_GUID        gEfiSmbiosNvramGuid = {0x4b3082a3, 0x80c6, 0x4d7e, { 0x9c, 0xd0, 0x58, 0x39, 0x17, 0x26, 0x5d, 0xf1 }};
    CHAR16          *PreserveSmbiosNvramVar = L"PreserveSmbiosNvramVar";
    UINTN           Size = sizeof (UINT8);
    UINT32          PreserveSmbiosNvram;

    gRecoveryKeepDMIFlag = FALSE;

    Status = pRS->GetVariable (
            PreserveSmbiosNvramVar,
            &gEfiSmbiosNvramGuid,
            NULL,
            &Size,
            &PreserveSmbiosNvram
            );

    if (!EFI_ERROR (Status)) {
        gRecoveryKeepDMIFlag = TRUE;
        PreserveDmiEditData ();
    }

    Status = pRS->SetVariable (
            PreserveSmbiosNvramVar,
            &gEfiSmbiosNvramGuid,
            EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
            0,
            NULL
            );
    ASSERT_EFI_ERROR(Status);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RecoveryRestoreDmiEditData
//
// Description: Restore the DMIEdit data in NVRAM with data previously loaded
//              in memory by PreserveDmiEditData (for capsule mode)
//
// Input:       None
//
// Output:      None
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
RecoveryRestoreDmiEditData (VOID)
{
    if (gRecoveryKeepDMIFlag) {
        RestoreDmiEditData ();
    }

    gRecoveryKeepDMIFlag = FALSE;
}
#endif

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