-
Notifications
You must be signed in to change notification settings - Fork 88
/
MfciDeviceIdSupportLibSmbios.c
295 lines (254 loc) · 7.86 KB
/
MfciDeviceIdSupportLibSmbios.c
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
/** @file
This library reads SMBIOS values to populate the MFCI Targeting UEFI Variables
This is _a_ method of populating these variables
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiDxe.h>
#include <Library/MfciDeviceIdSupportLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/PrintLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Uefi/UefiInternalFormRepresentation.h>
#include <Protocol/Smbios.h>
#define ID_NOT_FOUND "Not Found"
// populate the array of structures that pair up the functions with variable names
CONST MFCI_DEVICE_ID_FN_TO_VAR_NAME_MAP gDeviceIdFnToTargetVarNameMap[MFCI_TARGET_VAR_COUNT] = {
{ MfciIdSupportGetManufacturer, MFCI_MANUFACTURER_VARIABLE_NAME },
{ MfciIdSupportGetProductName, MFCI_PRODUCT_VARIABLE_NAME },
{ MfciIdSupportGetSerialNumber, MFCI_SERIALNUMBER_VARIABLE_NAME },
{ MfciIdSupportGetOem1, MFCI_OEM_01_VARIABLE_NAME },
{ MfciIdSupportGetOem2, MFCI_OEM_02_VARIABLE_NAME }
};
// Note: This protocol will guarantee to be met by the Depex and located at the
// constructor of this library, thus no null-pointer check in library code flow.
EFI_SMBIOS_PROTOCOL *mSmbiosProtocol;
/**
Acquire the string associated with the Index from smbios structure and return it.
The caller is responsible for freeing the string buffer.
@param OptionalStrStart The start position to search the string
@param Index The index of the string to extract
@param String The string that is extracted or ID_NOT_FOUND
@param Size Optional pointer to hold size of returned string (bytes including the terminating CHAR16 NULL)
@retval EFI_STATUS
**/
EFI_STATUS
GetOptionalStringByIndex (
IN CHAR8 *OptionalStrStart,
IN UINT8 Index,
OUT CHAR16 **String,
OUT UINTN *Size OPTIONAL
)
{
UINTN StrSize;
CHAR8 *WhichStr;
StrSize = 0;
if (Index != 0) {
do {
Index--;
OptionalStrStart += StrSize;
StrSize = AsciiStrSize (OptionalStrStart);
} while (OptionalStrStart[StrSize] != 0 && Index != 0);
}
if ((Index != 0) || (StrSize == 1) || (StrSize == 0)) {
//
// Meet the end of strings set but Index is non-zero, or
// found an empty string, or Index passed in was 0
//
DEBUG ((DEBUG_ERROR, "SMBIOS string not found, returning \"%s\"\n", ID_NOT_FOUND));
StrSize = sizeof (ID_NOT_FOUND);
WhichStr = ID_NOT_FOUND;
} else {
WhichStr = OptionalStrStart;
}
*String = AllocatePool (StrSize * sizeof (CHAR16)); // 0 page will catch AllocatePool failures
if (*String == NULL) {
return EFI_OUT_OF_RESOURCES;
}
AsciiStrToUnicodeStrS (WhichStr, *String, StrSize);
if (Size != NULL) {
*Size = StrSize * sizeof (CHAR16);
}
return EFI_SUCCESS;
}
/**
* Get the Manufacturer Name
*
* @param Manufacturer
* @param ManufacturerSize
*
* It is the callers responsibility to free the buffer returned.
*
* @return EFI_STATUS EFIAPI
*/
EFI_STATUS
EFIAPI
MfciIdSupportGetManufacturer (
CHAR16 **Manufacturer,
UINTN *ManufacturerSize OPTIONAL
)
{
EFI_STATUS Status;
EFI_SMBIOS_HANDLE SmbiosHandle;
EFI_SMBIOS_TABLE_HEADER *Record;
SMBIOS_TYPE Type;
SMBIOS_TABLE_TYPE1 *Type1Record;
if (Manufacturer == NULL) {
return EFI_INVALID_PARAMETER;
}
SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED; // Reset handle
Type = SMBIOS_TYPE_SYSTEM_INFORMATION; // Smbios type1
Status = mSmbiosProtocol->GetNext (mSmbiosProtocol, &SmbiosHandle, &Type, &Record, NULL);
if (EFI_ERROR (Status)) {
return Status;
}
Type1Record = (SMBIOS_TABLE_TYPE1 *)Record;
Status = GetOptionalStringByIndex ((CHAR8 *)((UINT8 *)Type1Record + Type1Record->Hdr.Length), Type1Record->Manufacturer, Manufacturer, ManufacturerSize);
return Status;
}
/**
* Get the ProductName
*
* @param ProductName
* @param ProductNameSize
*
* It is the callers responsibility to free the buffer returned.
*
* @return EFI_STATUS EFIAPI
*/
EFI_STATUS
EFIAPI
MfciIdSupportGetProductName (
CHAR16 **ProductName,
UINTN *ProductNameSize OPTIONAL
)
{
EFI_STATUS Status;
EFI_SMBIOS_HANDLE SmbiosHandle;
EFI_SMBIOS_TABLE_HEADER *Record;
SMBIOS_TYPE Type;
SMBIOS_TABLE_TYPE1 *Type1Record;
if (ProductName == NULL) {
return EFI_INVALID_PARAMETER;
}
SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED; // Reset handle
Type = SMBIOS_TYPE_SYSTEM_INFORMATION; // Smbios type1
Status = mSmbiosProtocol->GetNext (mSmbiosProtocol, &SmbiosHandle, &Type, &Record, NULL);
if (EFI_ERROR (Status)) {
return Status;
}
Type1Record = (SMBIOS_TABLE_TYPE1 *)Record;
Status = GetOptionalStringByIndex ((CHAR8 *)((UINT8 *)Type1Record + Type1Record->Hdr.Length), Type1Record->ProductName, ProductName, ProductNameSize);
return Status;
}
/**
* Get the SerialNumber
*
* @param SerialNumber
* @param SerialNumberSize
*
* It is the callers responsibility to free the buffer returned.
*
* @return EFI_STATUS EFIAPI
*/
EFI_STATUS
EFIAPI
MfciIdSupportGetSerialNumber (
CHAR16 **SerialNumber,
UINTN *SerialNumberSize OPTIONAL
)
{
EFI_STATUS Status;
EFI_SMBIOS_HANDLE SmbiosHandle;
EFI_SMBIOS_TABLE_HEADER *Record;
SMBIOS_TYPE Type;
SMBIOS_TABLE_TYPE1 *Type1Record;
SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED; // Reset handle
Type = SMBIOS_TYPE_SYSTEM_INFORMATION; // Smbios type1
Status = mSmbiosProtocol->GetNext (mSmbiosProtocol, &SmbiosHandle, &Type, &Record, NULL);
if (EFI_ERROR (Status)) {
return Status;
}
Type1Record = (SMBIOS_TABLE_TYPE1 *)Record;
Status = GetOptionalStringByIndex ((CHAR8 *)((UINT8 *)Type1Record + Type1Record->Hdr.Length), Type1Record->SerialNumber, SerialNumber, SerialNumberSize);
return Status;
}
EFI_STATUS
ReturnEmptyChar16 (
CHAR16 **String,
UINTN *StringSize OPTIONAL
)
{
CHAR16 *EmptyString;
EmptyString = L"";
*String = AllocatePool (sizeof (*EmptyString));
if (*String == NULL) {
return EFI_OUT_OF_RESOURCES;
}
CopyMem (*String, EmptyString, sizeof (*EmptyString));
if (StringSize != NULL) {
*StringSize = sizeof (*EmptyString);
}
return EFI_SUCCESS;
}
/**
* Get OEM1
*
* @param Oem1
* @param Oem1Size
*
* Get OEM1, an empty string in this SMBIOS example
*
* @return EFI_STATUS EFIAPI
*/
EFI_STATUS
EFIAPI
MfciIdSupportGetOem1 (
CHAR16 **Oem1,
UINTN *Oem1Size OPTIONAL
)
{
return ReturnEmptyChar16 (Oem1, Oem1Size);
}
/**
* Get OEM2, an empty string in this SMBIOS example
*
* @param Oem2
* @param Oem2Size
*
* It is the callers responsibility to free the buffer returned.
*
* @return EFI_STATUS EFIAPI
*/
EFI_STATUS
EFIAPI
MfciIdSupportGetOem2 (
CHAR16 **Oem2,
UINTN *Oem2Size OPTIONAL
)
{
return ReturnEmptyChar16 (Oem2, Oem2Size);
}
/**
Constructor for MfciIdSupportLib.
@param ImageHandle ImageHandle of the loaded driver.
@param SystemTable Pointer to the EFI System Table.
@retval EFI_SUCCESS The handlers were registered successfully.
**/
EFI_STATUS
EFIAPI
MfciIdSupportConstructor (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
Status = gBS->LocateProtocol (&gEfiSmbiosProtocolGuid, NULL, (VOID **)&mSmbiosProtocol);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Could not locate SMBIOS protocol. %r\n", Status));
}
return Status;
}