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
|
/** @file
Translate the DataHub records via EFI_DATA_HUB_PROTOCOL to Smbios recorders
via EFI_SMBIOS_PROTOCOL.
Copyright (c) 2009, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "Thunk.h"
EFI_GUID ZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
EFI_SMBIOS_PROTOCOL *mSmbiosProtocol = NULL;
/**
Release the structure Node.
@param StructureNode Point to SMBIOS_STRUCTURE_NODE which will be removed.
**/
VOID
ReleaseStructureNode (
SMBIOS_STRUCTURE_NODE *StructureNode
)
{
EFI_SMBIOS_PROTOCOL *Smbios;
RemoveEntryList (&(StructureNode->Link));
Smbios = GetSmbiosProtocol();
ASSERT (Smbios != NULL);
Smbios->Remove (Smbios, StructureNode->SmbiosHandle);
gBS->FreePool (StructureNode);
}
/**
Process a datahub's record and find corresponding translation way to translate
to SMBIOS record.
@param Record Point to datahub record.
**/
VOID
SmbiosProcessDataRecord (
IN EFI_DATA_RECORD_HEADER *Record
)
{
EFI_DATA_RECORD_HEADER *RecordHeader;
EFI_SUBCLASS_TYPE1_HEADER *DataHeader;
UINTN Index;
SMBIOS_CONVERSION_TABLE_ENTRY *Conversion;
UINT8 *SrcData;
UINTN SrcDataSize;
LIST_ENTRY *Link;
SMBIOS_STRUCTURE_NODE *StructureNode;
BOOLEAN StructureCreated;
EFI_STATUS Status;
Conversion = NULL;
StructureNode = NULL;
StructureCreated = FALSE;
RecordHeader = Record;
DataHeader = (EFI_SUBCLASS_TYPE1_HEADER *) (Record + 1);
SrcData = (UINT8 *) (DataHeader + 1);
SrcDataSize = RecordHeader->RecordSize - RecordHeader->HeaderSize - sizeof (EFI_SUBCLASS_TYPE1_HEADER);
if (DataHeader->HeaderSize != sizeof (EFI_SUBCLASS_TYPE1_HEADER) ||
DataHeader->Instance == EFI_SUBCLASS_INSTANCE_RESERVED ||
DataHeader->SubInstance == EFI_SUBCLASS_INSTANCE_RESERVED
) {
//
// Invalid Data Record
//
goto Done;
}
Index = 0;
while(TRUE) {
//
// Find a matching entry in the conversion table for this
// (SubClass, RecordNumber) pair
//
for (; !CompareGuid (&(mConversionTable[Index].SubClass), &ZeroGuid); Index++) {
if (CompareGuid (
&(mConversionTable[Index].SubClass),
&(RecordHeader->DataRecordGuid)
)) {
if (mConversionTable[Index].RecordType == DataHeader->RecordType) {
break;
}
}
}
if (CompareGuid (&(mConversionTable[Index].SubClass), &ZeroGuid)) {
//
// We cannot find a matching entry in conversion table,
// this means this data record cannot be used for SMBIOS.
// Just skip it.
//
goto Done;
}
Conversion = &mConversionTable[Index++];
//
// Find corresponding structure in the Structure List
//
for (Link = mStructureList.ForwardLink; Link != &mStructureList; Link = Link->ForwardLink) {
StructureNode = CR (
Link,
SMBIOS_STRUCTURE_NODE,
Link,
SMBIOS_STRUCTURE_NODE_SIGNATURE
);
if (Conversion->StructureLocatingMethod == BY_SUBCLASS_INSTANCE_SUBINSTANCE_PRODUCER) {
//
// Look at SubClass, Instance, SubInstance and ProducerName for a matching
// node
//
if (CompareGuid (&(StructureNode->SubClass), &(RecordHeader->DataRecordGuid)) &&
StructureNode->Instance == DataHeader->Instance &&
StructureNode->SubInstance == DataHeader->SubInstance &&
CompareGuid (&(StructureNode->ProducerName), &(RecordHeader->ProducerName))
) {
if (Conversion->SmbiosType >= 0X80) {
if (StructureNode->SmbiosType == ((SMBIOS_STRUCTURE_HDR *) SrcData)->Type) {
break;
}
} else if (StructureNode->SmbiosType == Conversion->SmbiosType) {
break;
}
}
} else if (Conversion->StructureLocatingMethod == BY_SUBCLASS_INSTANCE_PRODUCER) {
//
// Look at SubClass, Instance and ProducerName for a matching node
//
if (CompareGuid (&(StructureNode->SubClass), &(RecordHeader->DataRecordGuid)) &&
StructureNode->Instance == DataHeader->Instance &&
CompareGuid (&(StructureNode->ProducerName), &(RecordHeader->ProducerName))
) {
if (Conversion->SmbiosType >= 0X80) {
if (StructureNode->SmbiosType == ((SMBIOS_STRUCTURE_HDR *) SrcData)->Type) {
break;
}
} else if (StructureNode->SmbiosType == Conversion->SmbiosType) {
break;
}
}
} else {
//
// Invalid conversion table entry
//
goto Done;
}
}
if (Link == &mStructureList) {
//
// Not found, create a new structure
//
StructureNode = AllocateZeroPool (sizeof (SMBIOS_STRUCTURE_NODE));
if (StructureNode == NULL) {
goto Done;
}
if (Conversion->StructureLocatingMethod == BY_SUBCLASS_INSTANCE_SUBINSTANCE_PRODUCER) {
//
// Fill in SubClass, Instance, SubInstance and ProducerName
//
CopyMem (&(StructureNode->SubClass), &(RecordHeader->DataRecordGuid), sizeof (EFI_GUID));
StructureNode->Instance = DataHeader->Instance;
StructureNode->SubInstance = DataHeader->SubInstance;
CopyMem (&(StructureNode->ProducerName), &(RecordHeader->ProducerName), sizeof (EFI_GUID));
} else if (Conversion->StructureLocatingMethod == BY_SUBCLASS_INSTANCE_PRODUCER) {
//
// Fill in at SubClass, Instance and ProducerName, mark SubInstance as Non
// Applicable
//
CopyMem (&(StructureNode->SubClass), &(RecordHeader->DataRecordGuid), sizeof (EFI_GUID));
StructureNode->Instance = DataHeader->Instance;
StructureNode->SubInstance = EFI_SUBCLASS_INSTANCE_NON_APPLICABLE;
CopyMem (&(StructureNode->ProducerName), &(RecordHeader->ProducerName), sizeof (EFI_GUID));
}
//
// Allocate the structure instance
//
StructureNode->StructureSize = SmbiosGetTypeMinimalLength (Conversion->SmbiosType);
StructureNode->SmbiosType = Conversion->SmbiosType;
//
// StructureSize include the TWO trailing zero byte.
//
if (StructureNode->StructureSize < (sizeof(SMBIOS_STRUCTURE) + 2)) {
//
// Invalid Type
//
gBS->FreePool (StructureNode);
goto Done;
}
StructureNode->SmbiosType = Conversion->SmbiosType;
StructureNode->SmbiosHandle = 0;
Status = SmbiosProtocolCreateRecord (
NULL,
StructureNode
);
if (EFI_ERROR (Status)) {
goto Done;
}
//
// Temporary cache the structrue pointer to Smbios database.
//
StructureNode->Structure = GetSmbiosBufferFromHandle (StructureNode->SmbiosHandle, StructureNode->SmbiosType, NULL);
InitializeListHead (&StructureNode->LinkDataFixup);
//
// Insert the Structure Node into the Strucutre List
//
StructureNode->Signature = SMBIOS_STRUCTURE_NODE_SIGNATURE;
InsertTailList (&mStructureList, &(StructureNode->Link));
StructureCreated = TRUE;
}
//
// Re-calculate the structure pointer to Smbios database.
//
StructureNode->Structure = GetSmbiosBufferFromHandle (StructureNode->SmbiosHandle, StructureNode->SmbiosType, NULL);
//
// Fill the Structure's field corresponding to this data record
//
if (Conversion->FieldFillingMethod == RECORD_DATA_UNCHANGED_OFFSET_SPECIFIED) {
//
// Field data is just the record data without transforming and
// offset is specified directly in the conversion table entry
//
if (Conversion->FieldOffset + SrcDataSize > StructureNode->Structure->Length) {
//
// Invalid Conversion Table Entry
//
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
CopyMem ((UINT8 *) (StructureNode->Structure) + Conversion->FieldOffset, SrcData, SrcDataSize);
} else if (Conversion->FieldFillingMethod == BY_FUNCTION_WITH_OFFSET_SPECIFIED) {
//
// Field offfset is specified in the conversion table entry, but
// record data needs to be transformed to be filled into the field,
// so let the FieldFillingFunction do it.
//
if (Conversion->FieldFillingFunction == NULL) {
//
// Invalid Conversion Table Entry
//
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
Status = Conversion->FieldFillingFunction (
StructureNode,
Conversion->FieldOffset,
SrcData,
(UINT32) SrcDataSize
);
if (EFI_ERROR (Status)) {
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
} else if (Conversion->FieldFillingMethod == BY_FUNCTION) {
//
// Both field offset and field content are determined by
// FieldFillingFunction
//
if (Conversion->FieldFillingFunction == NULL) {
//
// Invalid Conversion Table Entry
//
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
Status = Conversion->FieldFillingFunction (
StructureNode,
0,
SrcData,
(UINT32) SrcDataSize
);
if (EFI_ERROR (Status)) {
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
} else if (Conversion->FieldFillingMethod == BY_FUNCTION_WITH_WHOLE_DATA_RECORD) {
//
// Both field offset and field content are determined by
// FieldFillingFunction and the function accepts the whole data record
// including the data header
//
if (Conversion->FieldFillingFunction == NULL) {
//
// Invalid Conversion Table Entry
//
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
Status = Conversion->FieldFillingFunction (
StructureNode,
0,
DataHeader,
RecordHeader->RecordSize - RecordHeader->HeaderSize
);
if (EFI_ERROR (Status)) {
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
} else {
//
// Invalid Conversion Table Entry
//
if (StructureCreated) {
ReleaseStructureNode (StructureNode);
}
goto Done;
}
}
Done:
return ;
}
/**
Calculate the minimal length for a SMBIOS type. This length maybe not equal
to sizeof (SMBIOS_RECORD_STRUCTURE), but defined in conformance chapter in SMBIOS specification.
@param Type SMBIOS's type.
@return the minimal length of a smbios record.
**/
UINT32
SmbiosGetTypeMinimalLength (
IN UINT8 Type
)
{
UINTN Index;
for (Index = 0; mTypeInfoTable[Index].MinLength != 0; Index++) {
if (mTypeInfoTable[Index].Type == Type) {
return mTypeInfoTable[Index].MinLength;
}
}
return 0;
}
/**
Get pointer of EFI_SMBIOS_PROTOCOL.
@return pointer of EFI_SMBIOS_PROTOCOL.
**/
EFI_SMBIOS_PROTOCOL*
GetSmbiosProtocol(
VOID
)
{
EFI_STATUS Status;
if (mSmbiosProtocol == NULL) {
Status = gBS->LocateProtocol (&gEfiSmbiosProtocolGuid, NULL, (VOID*) &mSmbiosProtocol);
ASSERT_EFI_ERROR (Status);
}
ASSERT (mSmbiosProtocol != NULL);
return mSmbiosProtocol;
}
/**
Create a blank smbios record. The datahub record is only a field of smbios record.
So before fill any field from datahub's record. A blank smbios record need to be
created.
@param ProducerHandle The produce handle for a datahub record
@param StructureNode Point to SMBIOS_STRUCTURE_NODE
@retval EFI_OUT_OF_RESOURCES Fail to allocate memory for new blank SMBIOS record.
@retval EFI_SUCCESS Success to create blank smbios record.
**/
EFI_STATUS
SmbiosProtocolCreateRecord (
IN EFI_HANDLE ProducerHandle, OPTIONAL
IN SMBIOS_STRUCTURE_NODE *StructureNode
)
{
EFI_SMBIOS_PROTOCOL *Smbios;
EFI_SMBIOS_TABLE_HEADER *BlankRecord;
EFI_STATUS Status;
SMBIOS_STRUCTURE_NODE *RefStructureNode;
LIST_ENTRY *Link;
LIST_ENTRY *Link1;
LIST_ENTRY *Link2;
SMBIOS_LINK_DATA_FIXUP_NODE *LinkDataFixupNode;
UINT8 *BufferPointer;
Smbios = GetSmbiosProtocol();
ASSERT (Smbios != NULL);
//
// Prepare a blank smbios record.
//
BlankRecord = (EFI_SMBIOS_TABLE_HEADER*) AllocateZeroPool (StructureNode->StructureSize);
if (BlankRecord == NULL) {
return EFI_OUT_OF_RESOURCES;
}
BlankRecord->Type = StructureNode->SmbiosType;
BlankRecord->Length = (UINT8) (StructureNode->StructureSize - 2);
//
// Add blank record into SMBIOS database.
//
Status = Smbios->Add (Smbios, NULL, &StructureNode->SmbiosHandle, BlankRecord);
FreePool (BlankRecord);
//
// Fix up the InterLink node for new added smbios record if some other
// existing smbios record want to link this new record's handle.
//
for (Link = mStructureList.ForwardLink; Link != &mStructureList; Link = Link->ForwardLink) {
RefStructureNode = CR (Link, SMBIOS_STRUCTURE_NODE, Link, SMBIOS_STRUCTURE_NODE_SIGNATURE);
for (Link1 = RefStructureNode->LinkDataFixup.ForwardLink; Link1 != &RefStructureNode->LinkDataFixup;) {
LinkDataFixupNode = CR (Link1, SMBIOS_LINK_DATA_FIXUP_NODE, Link, SMBIOS_LINK_DATA_FIXUP_NODE_SIGNATURE);
Link2 = Link1;
Link1 = Link1->ForwardLink;
if ((StructureNode->SmbiosType != LinkDataFixupNode->TargetType) ||
!(CompareGuid (&StructureNode->SubClass, &LinkDataFixupNode->SubClass)) ||
(StructureNode->Instance != LinkDataFixupNode->LinkData.Instance) ||
(StructureNode->SubInstance != LinkDataFixupNode->LinkData.SubInstance)) {
continue;
}
//
// Fill the field with the handle found
//
BufferPointer = (UINT8 *) (RefStructureNode->Structure) + LinkDataFixupNode->Offset;
*BufferPointer = (UINT8) (StructureNode->SmbiosHandle & 0xFF);
*(BufferPointer + 1) = (UINT8) ((StructureNode->SmbiosHandle >> 8) & 0xFF);
BufferPointer = NULL;
RemoveEntryList (Link2);
FreePool (LinkDataFixupNode);
}
}
return Status;
}
/**
Get pointer of a SMBIOS record's buffer according to its handle.
@param Handle The handle of SMBIOS record want to be searched.
@param Type The type of SMBIOS record want to be searched.
@param ProducerHandle The producer handle of SMBIOS record.
@return EFI_SMBIOS_TABLE_HEADER Point to a SMBIOS record's buffer.
**/
EFI_SMBIOS_TABLE_HEADER*
GetSmbiosBufferFromHandle (
IN EFI_SMBIOS_HANDLE Handle,
IN EFI_SMBIOS_TYPE Type,
IN EFI_HANDLE ProducerHandle OPTIONAL
)
{
EFI_SMBIOS_PROTOCOL* Smbios;
EFI_SMBIOS_HANDLE SearchingHandle;
EFI_SMBIOS_TABLE_HEADER *RecordInSmbiosDatabase;
EFI_STATUS Status;
SearchingHandle = 0;
Smbios = GetSmbiosProtocol();
ASSERT (Smbios != NULL);
do {
Status = Smbios->GetNext (Smbios, &SearchingHandle, &Type, &RecordInSmbiosDatabase, NULL);
} while ((SearchingHandle != Handle) && (Status != EFI_NOT_FOUND));
return RecordInSmbiosDatabase;
}
/**
Get the full size of smbios structure including optional strings that follow the formatted structure.
@param Head Pointer to the beginning of smbios structure.
@param Size The returned size.
@param NumberOfStrings The returned number of optional strings that follow the formatted structure.
@retval EFI_SUCCESS Size retured in Size.
@retval EFI_INVALID_PARAMETER Input smbios structure mal-formed or Size is NULL.
**/
EFI_STATUS
EFIAPI
GetSmbiosStructureSize (
IN EFI_SMBIOS_TABLE_HEADER *Head,
OUT UINT32 *Size,
OUT UINT8 *NumberOfStrings
)
{
UINT32 FullSize;
UINT8 StrLen;
INT8* CharInStr;
if (Size == NULL || NumberOfStrings == NULL) {
return EFI_INVALID_PARAMETER;
}
FullSize = Head->Length;
CharInStr = (INT8*)Head + Head->Length;
*Size = FullSize;
*NumberOfStrings = 0;
StrLen = 0;
//
// look for the two consecutive zeros, check the string limit by the way.
//
while (*CharInStr != 0 || *(CharInStr+1) != 0) {
if (*CharInStr == 0) {
*Size += 1;
CharInStr++;
}
for (StrLen = 0 ; StrLen < SMBIOS_STRING_MAX_LENGTH; StrLen++) {
if (*(CharInStr+StrLen) == 0) {
break;
}
}
if (StrLen == SMBIOS_STRING_MAX_LENGTH) {
return EFI_INVALID_PARAMETER;
}
//
// forward the pointer
//
CharInStr += StrLen;
*Size += StrLen;
*NumberOfStrings += 1;
}
//
// count ending two zeros.
//
*Size += 2;
return EFI_SUCCESS;
}
|