summaryrefslogtreecommitdiff
path: root/Core/EM/SMBIOS/SmbiosGetFlashData/SmbiosGetFlashData.c
blob: 49a35da65defecb120c4b71866ec51689707ee33 (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
//**********************************************************************//
//**********************************************************************//
//**                                                                  **//
//**        (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/SmbiosGetFlashData/SmbiosGetFlashData.c 11    2/17/15 1:17p Davidd $
//
// $Revision: 11 $
//
// $Date: 2/17/15 1:17p $
//**********************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SMBIOS/SmbiosGetFlashData/SmbiosGetFlashData.c $
// 
// 11    2/17/15 1:17p 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    5/23/13 6:10p Davidd
// [TAG]           EIP124635
// [Category]      Bug Fix
// [Severity]      Normal
// [Symptom]       When DMIEdit data area in BootBlock is erased with 0x00
// value, the system hangs at CP 0x62
// [RootCause]     In function FindRawSection, a pointer is used to
// traverse sections in the FFS to locate the DMIEdit raw data section. In
// case the "size" of the header become corrupted (cleared to zero), this
// zero value is added to the pointer causes it not to move making the
// system hang.
// [Solution]      Check if the size is zero then break out of the while
// loop
// [Files]         SmbiosGetFlashData.c
//
// 9     8/30/11 4:15p 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
//
// 8     5/11/11 12:35p Davidd
// [TAG]           EIP58171
// [Category]      Bug Fix
// [Severity]      Important
// [Symptom]       TABLE_INFO structure of SmbiosDMIEdit is not backward
// compatible for AFU preserve DMI structure feature.
// [RootCause]     TABLE_INFO structure of SmbiosDMIEdit is not backward
// compatible.
// [Solution]      New TABLE_INFO structure defined for backward
// compatibility and support added.
// [Files]         Smbios.c
//                 SmbiosDMIEdit.h
//                 SmbiosDMIEditFunc.c
//                 SmbiosGetFlashData.c
//
// 7     11/02/10 4:14p Davidd
// [TAG]           EIP42938
// [Category]      BUG FIX
// [Severity]      Critical
// [Symptom]       The SMBIOS string field cannot be successfully updated
// if it was programmed to Null by the 3-party SMBIOS tool
// [RootCause]     BIOS did not have support for NULL strings
// [Solution]      Problem has been fixed with code changes
// [Files]
//    Smbios.c
//    SmbiosDMIEditFunc.c
//    SmbiosGetFlashData.c
//    SmbiosDMIEdit.h
//    SmbiosFlashData.sdl
//
// 6     6/02/09 11:25a Davidd
// Updated AMI and function headers. (EIP 22180)
//
// 5     12/16/08 2:28a Iminglin
// (EIP17767) The function value of FindFile for compliance.
//
// 4     3/29/07 6:11p Davidd
// Changed the year in the AMI banner and adjust indentation to coding
// standard.
//
// 3     12/15/06 5:47p Davidd
// Code cleanup and reformatted to coding standard.
//
// 2     3/21/06 8:44p Fasihm
// Changed the protocol name FvBlock.h to FirmwareVolumeBlock.h to be
// compatable with the new Aptio 4.5 Core and later.
//
// 1     4/29/05 2:07p Davidd
// Initial checkin.
//
// 2     6/12/04 11:54p Markw
//
// 1     5/20/04 3:41p Markw
//
//**********************************************************************

#include <AmiDxeLib.h>
#include <Token.h>
#include <Protocol\SmbiosGetFlashDataProtocol.h>
#include <Protocol\FirmwareVolumeBlock.h>

#define Align4(Value) (((Value)+3) & ~3)
#define Align8(Value) (((Value)+7) & ~7)
#define SECTION_SIZE(SectionHeaderPtr) \
  ( (UINT32) ( *((UINT32 *) ((EFI_COMMON_SECTION_HEADER *)SectionHeaderPtr)->Size) & 0x00ffffff) )

#pragma pack(1)

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:  TABLE_INFO
//
// Description: DMI data record
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

#define DMIEDIT_WRITE_ONCE      0x01
#define DMIEDIT_DELETE_STRUC    0x02
#define DMIEDIT_ADD_STRUC       0x04
#define DMIEDIT_EXTENDED_HDR    0x80

typedef struct {
    UINT8   Type;
    UINT8   Offset;     // Structure field offset, or string number for Type 11 and 12
    UINT8   Reserved;   // Size of string including \0 or UUID (16)
    UINT8   Flags;      // Bit0 = Write Once
                        // Bit1 = Delete Structure
                        // Bit2 = Add structure
                        // Bit7 = Extended Header
    UINT8   HdrLength;
    UINT16  Size;
    UINT16  Handle;
} TABLE_INFO;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:  ROM_INFO
//
// Description: DMI Data Table Header
//----------------------------------------------------------------------------
//<AMI_SHDR_END>
typedef struct {
	UINT32	Signature;
	UINT32 	Size;
} ROM_INFO;

#pragma pack()


EFI_GUID FlashDataFile = {0xFD44820B,0xF1AB,0x41c0,0xAE,0x4E,0x0C,0x55,0x55,0x6E,0xB9,0xBD};
EFI_GUID ZeroGuid = {0,0,0,0,0,0,0,0,0,0,0};
EFI_GUID FFGuid = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

ROM_INFO    gRomInfo;
void        *gRomData;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CompareGuid
//
// Description: Compares two input GUIDs
//
// Input:       IN EFI_GUID *G1
//              IN EFI_GUID *G2
//
// Output:      INT8 - 0/-1 for Equal/Not Equal
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
INT8
CompareGuid (
    IN EFI_GUID *G1,
    IN EFI_GUID *G2
)
{
	UINT32 *p1 = (UINT32*)G1, *p2 = (UINT32*)G2;
	UINTN	i;
	for(i=0; i<4; ++i) {
		if(p1[i] != p2[i]) return -1;
	}
	return 0;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetFlashTableInfo
//
// Description: Get Flash Data location and size.
//
// Input:       IN      EFI_SMBIOS_FLASH_DATA_PROTOCOL  *This
//              IN OUT  VOID                            **Location
//              IN OUT  UINT32                          *Size
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetFlashTableInfo (
	IN      EFI_SMBIOS_FLASH_DATA_PROTOCOL  *This,
	IN OUT  VOID 	                        **Location,
	IN OUT  UINT32 	                        *Size
)
{
	*Location = gRomData;
	*Size = gRomInfo.Size;
	return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetField
//
// Description: Get Flash Data Field
//
// Input:       IN EFI_SMBIOS_FLASH_DATA_PROTOCOL   *This
//              IN UINT8                            Table
//              IN UINT8                            Offset
//              IN VOID                             **Field
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetField (
	IN EFI_SMBIOS_FLASH_DATA_PROTOCOL  *This,
	IN UINT8                           Table,
	IN UINT8                           Offset,
	IN VOID                            **Field
)
{
    TABLE_INFO *p = gRomData;

    while ( p->Offset != 0xff && (p->Type != Table || p->Offset != Offset))	{
		p = (TABLE_INFO*)  ((UINT8*)(p+1) + p->Size);
	}

	if (p->Offset != 0xff) {
		*Field = p + 1;
		return EFI_SUCCESS;
	}

	*Field = 0;
	return EFI_NOT_FOUND;
}


EFI_SMBIOS_FLASH_DATA_PROTOCOL gSmbiosFlashDataProtocol = {
	GetFlashTableInfo,
	GetField
};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   FindRawSection
//
// Description: Find the RAW section
//
// Input:       IN VOID  *Section
//              IN VOID  *End
//
// Output:      VOID* - Pointer to Raw Section if found
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID*
FindRawSection (
    IN VOID *Section,
    IN VOID *End
)
{
	EFI_COMMON_SECTION_HEADER	*p = Section;
	while((INT32)p < (INT32)End)	//Use signed because 0 = 0x100000000
	{
		if (p->Type == EFI_SECTION_RAW) return (p+1);

		if (Align4(SECTION_SIZE(p)) == 0x00) {
			return 0;               // Section size = 0 indicates data is corrupted
		}

		p = (EFI_COMMON_SECTION_HEADER*)((UINT8*)p+Align4(SECTION_SIZE(p)));
	}
	return 0;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   FindFile
//
// Description: Find file in the FV with the input GUID
//
// Input:       IN EFI_GUID  *Guid
//              IN VOID      *File
//              IN VOID      *EndOfFiles
//
// Output:      VOID* - Pointer to File if found
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID*
FindFile (
    IN EFI_GUID *Guid,
    IN VOID     *File,
    IN VOID     *EndOfFiles
)
{
	EFI_FFS_FILE_HEADER *p = File;

    while(((INT32)p < (INT32)EndOfFiles) &&
          ((INT32)(p+sizeof(EFI_FFS_FILE_HEADER)) < (INT32)EndOfFiles)) // Use signed because 0 = 0x100000000
	{
		if (0==CompareGuid(Guid,&p->Name)) {
			//Found File.
			return FindRawSection(
				        p+1,
				        (UINT8*)p + (*(UINT32*)p->Size & 0xffffff) - sizeof(*p)
				        );
		}
		//Check for last of File in firmware volume.
		if (0==CompareGuid(&p->Name,&FFGuid) || 0==CompareGuid(&p->Name,&ZeroGuid))
			return NULL;

		p = (EFI_FFS_FILE_HEADER*)((UINT8*)p + Align8((*(UINT32*)p->Size & 0xffffff)));
	}

	return NULL;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetDataLocation
//
// Description: Find the Flash Data file in the FV.
//
// Input:       None
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetDataLocation (VOID)
{
	EFI_GUID							gEfiFirmwareVolumeBlockProtocolGuid = EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID;
    EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock;
    EFI_PHYSICAL_ADDRESS                Address;
	EFI_HANDLE							*HandleBuffer;
	EFI_STATUS							Status;
    UINTN                               i;
	UINTN								NumHandles;
    void                                *FirstFile;
    void                                *EndOfFiles;
    void                                *File;

    Status = pBS->LocateHandleBuffer(ByProtocol,
                                    &gEfiFirmwareVolumeBlockProtocolGuid,
                                    NULL,
                                    &NumHandles,
                                    &HandleBuffer);
	if (EFI_ERROR(Status)) return Status;

    for (i = 0; i < NumHandles; ++i) {
        Status = pBS->HandleProtocol(HandleBuffer[i],
                                    &gEfiFirmwareVolumeBlockProtocolGuid,
                                    &FvBlock);
        if (EFI_ERROR(Status)) continue;

        Status = FvBlock->GetPhysicalAddress(FvBlock, &Address);

        if (Status == EFI_SUCCESS) {
            FirstFile = (UINT8*)Address
                        + ((EFI_FIRMWARE_VOLUME_HEADER*)Address)->HeaderLength;

            EndOfFiles = (UINT8*)Address
                        + ((EFI_FIRMWARE_VOLUME_HEADER*)Address)->FvLength;

            File = FindFile(&FlashDataFile, FirstFile, EndOfFiles);
            if (!File) {
                Status = EFI_NOT_FOUND;
                continue;
            }

            gRomInfo = *(ROM_INFO*)File;
            gRomData = (ROM_INFO*)File + 1;
            break;
        }
    }

	pBS->FreePool(HandleBuffer);
	return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SmbiosGetFlashDataInstall
//
// Description: Driver entry point for SmbiosGetFlashData
//
// Input:       IN EFI_HANDLE           ImageHandle
//              IN EFI_SYSTEM_TABLE     *SystemTable
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
SmbiosGetFlashDataInstall (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
)
{
	EFI_STATUS  Status;
	EFI_GUID	gSmbiosFlashDataProtocolGuid = EFI_SMBIOS_FLASH_DATA_PROTOCOL_GUID;

    InitAmiLib(ImageHandle, SystemTable);

    Status = GetDataLocation();
    if (EFI_ERROR(Status)) return Status;

	return pBS->InstallMultipleProtocolInterfaces(
		            &ImageHandle,
		            &gSmbiosFlashDataProtocolGuid,&gSmbiosFlashDataProtocol,
		            NULL);
}

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