]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/Library/BiosIdLib/BiosIdLib.c
UefiCpuPkg/PiSmmCpu: Check for untested memory in GCD
[mirror_edk2.git] / Vlv2TbltDevicePkg / Library / BiosIdLib / BiosIdLib.c
1 /*++
2
3 Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved
4
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13
14 Module Name:
15
16 BiosIdLib.c
17
18 Abstract:
19
20 Boot service DXE BIOS ID library implementation.
21
22 These functions in this file can be called during DXE and cannot be called during runtime
23 or in SMM which should use a RT or SMM library.
24
25 --*/
26
27 #include <PiDxe.h>
28 #include <Library/BaseLib.h>
29 #include <Library/HobLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/DebugLib.h>
33
34 #include <Library/BiosIdLib.h>
35 #include <Guid/BiosId.h>
36 #include <Protocol/FirmwareVolume2.h>
37 #include <Protocol/LoadedImage.h>
38
39
40 EFI_STATUS
41 GetImageFromFv (
42 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
43 IN EFI_GUID *NameGuid,
44 IN EFI_SECTION_TYPE SectionType,
45 OUT VOID **Buffer,
46 OUT UINTN *Size
47 )
48 {
49 EFI_STATUS Status;
50 EFI_FV_FILETYPE FileType;
51 EFI_FV_FILE_ATTRIBUTES Attributes;
52 UINT32 AuthenticationStatus;
53
54 //
55 // Read desired section content in NameGuid file
56 //
57 *Buffer = NULL;
58 *Size = 0;
59 Status = Fv->ReadSection (
60 Fv,
61 NameGuid,
62 SectionType,
63 0,
64 Buffer,
65 Size,
66 &AuthenticationStatus
67 );
68
69 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {
70 //
71 // Try reading PE32 section, since the TE section does not exist
72 //
73 *Buffer = NULL;
74 *Size = 0;
75 Status = Fv->ReadSection (
76 Fv,
77 NameGuid,
78 EFI_SECTION_PE32,
79 0,
80 Buffer,
81 Size,
82 &AuthenticationStatus
83 );
84 }
85
86 if (EFI_ERROR (Status) &&
87 ((SectionType == EFI_SECTION_TE) || (SectionType == EFI_SECTION_PE32))) {
88 //
89 // Try reading raw file, since the desired section does not exist
90 //
91 *Buffer = NULL;
92 *Size = 0;
93 Status = Fv->ReadFile (
94 Fv,
95 NameGuid,
96 Buffer,
97 Size,
98 &FileType,
99 &Attributes,
100 &AuthenticationStatus
101 );
102 }
103
104 return Status;
105 }
106
107
108 EFI_STATUS
109 GetImageEx (
110 IN EFI_HANDLE ImageHandle,
111 IN EFI_GUID *NameGuid,
112 IN EFI_SECTION_TYPE SectionType,
113 OUT VOID **Buffer,
114 OUT UINTN *Size,
115 BOOLEAN WithinImageFv
116 )
117 {
118 EFI_STATUS Status;
119 EFI_HANDLE *HandleBuffer;
120 UINTN HandleCount;
121 UINTN Index;
122 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
123
124 EFI_FIRMWARE_VOLUME2_PROTOCOL *ImageFv;
125 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
126
127
128 if (ImageHandle == NULL && WithinImageFv) {
129 return EFI_INVALID_PARAMETER;
130 }
131
132 Status = EFI_NOT_FOUND;
133 ImageFv = NULL;
134 if (ImageHandle != NULL) {
135 Status = gBS->HandleProtocol (
136 ImageHandle,
137 &gEfiLoadedImageProtocolGuid,
138 (VOID **) &LoadedImage
139 );
140 if (EFI_ERROR (Status)) {
141 return Status;
142 }
143 Status = gBS->HandleProtocol (
144 LoadedImage->DeviceHandle,
145 &gEfiFirmwareVolume2ProtocolGuid,
146 (VOID **) &ImageFv
147 );
148 if (!EFI_ERROR (Status)) {
149 Status = GetImageFromFv (ImageFv, NameGuid, SectionType, Buffer, Size);
150 }
151 }
152
153 if (Status == EFI_SUCCESS || WithinImageFv) {
154 return Status;
155 }
156
157 Status = gBS->LocateHandleBuffer (
158 ByProtocol,
159 &gEfiFirmwareVolume2ProtocolGuid,
160 NULL,
161 &HandleCount,
162 &HandleBuffer
163 );
164 if (EFI_ERROR (Status)) {
165 return Status;
166 }
167
168 //
169 // Find desired image in all Fvs
170 //
171 for (Index = 0; Index < HandleCount; ++Index) {
172 Status = gBS->HandleProtocol (
173 HandleBuffer[Index],
174 &gEfiFirmwareVolume2ProtocolGuid,
175 (VOID**)&Fv
176 );
177
178 if (EFI_ERROR (Status)) {
179 gBS->FreePool(HandleBuffer);
180 return Status;
181 }
182
183 if (ImageFv != NULL && Fv == ImageFv) {
184 continue;
185 }
186
187 Status = GetImageFromFv (Fv, NameGuid, SectionType, Buffer, Size);
188
189 if (!EFI_ERROR (Status)) {
190 break;
191 }
192 }
193 gBS->FreePool(HandleBuffer);
194
195 //
196 // Not found image
197 //
198 if (Index == HandleCount) {
199 return EFI_NOT_FOUND;
200 }
201
202 return EFI_SUCCESS;
203 }
204
205 /**
206 This function returns BIOS ID by searching HOB or FV.
207
208 @param BiosIdImage The BIOS ID got from HOB or FV.
209
210 @retval EFI_SUCCESS All parameters were valid and BIOS ID has been got.
211 @retval EFI_NOT_FOUND BiosId image is not found, and no parameter will be modified.
212 @retval EFI_INVALID_PARAMETER The parameter is NULL.
213
214 **/
215 EFI_STATUS
216 GetBiosId (
217 OUT BIOS_ID_IMAGE *BiosIdImage
218 )
219
220 {
221 EFI_STATUS Status;
222 VOID *Address = NULL;
223 UINTN Size = 0;
224
225 DEBUG ((EFI_D_INFO, "Get BIOS ID from FV\n"));
226
227 Status = GetImageEx (
228 NULL,
229 &gEfiBiosIdGuid,
230 EFI_SECTION_RAW,
231 &Address,
232 &Size,
233 FALSE
234 );
235
236 if (Status == EFI_SUCCESS) {
237 //
238 // BiosId image is present in FV
239 //
240 if (Address != NULL) {
241 Size = sizeof (BIOS_ID_IMAGE);
242 gBS->CopyMem (
243 (void *) BiosIdImage,
244 Address,
245 Size
246 );
247 //
248 // GetImage () allocated buffer for Address, now clear it.
249 //
250 gBS->FreePool (Address);
251
252 DEBUG ((EFI_D_INFO, "Get BIOS ID from FV successfully\n"));
253 DEBUG ((EFI_D_INFO, "BIOS ID: %s\n", (CHAR16 *) (&(BiosIdImage->BiosIdString))));
254
255 return EFI_SUCCESS;
256 }
257 }
258 return EFI_NOT_FOUND;
259 }
260
261 /**
262 This function returns the Version & Release Date and Time by getting and converting
263 BIOS ID.
264
265 @param BiosVersion The Bios Version out of the conversion.
266 @param BiosReleaseDate The Bios Release Date out of the conversion.
267 @param BiosReleaseTime - The Bios Release Time out of the conversion.
268
269 @retval EFI_SUCCESS - BIOS Version & Release Date and Time have been got successfully.
270 @retval EFI_NOT_FOUND - BiosId image is not found, and no parameter will be modified.
271 @retval EFI_INVALID_PARAMETER - All the parameters are NULL.
272
273 **/
274 EFI_STATUS
275 GetBiosVersionDateTime (
276 OUT CHAR16 *BiosVersion, OPTIONAL
277 OUT CHAR16 *BiosReleaseDate, OPTIONAL
278 OUT CHAR16 *BiosReleaseTime OPTIONAL
279 )
280 {
281 EFI_STATUS Status;
282 BIOS_ID_IMAGE BiosIdImage;
283
284 if ((BiosVersion == NULL) && (BiosReleaseDate == NULL) && (BiosReleaseTime == NULL)) {
285 return EFI_INVALID_PARAMETER;
286 }
287
288 Status = GetBiosId (&BiosIdImage);
289 if (EFI_ERROR (Status)) {
290 return EFI_NOT_FOUND;
291 }
292
293 if (BiosVersion != NULL) {
294 //
295 // Fill the BiosVersion data from the BIOS ID.
296 //
297 StrCpy (BiosVersion, (CHAR16 *) (&(BiosIdImage.BiosIdString)));
298 }
299
300 if (BiosReleaseDate != NULL) {
301 //
302 // Fill the build timestamp date from the BIOS ID in the "MM/DD/YY" format.
303 //
304 BiosReleaseDate[0] = BiosIdImage.BiosIdString.TimeStamp[2];
305 BiosReleaseDate[1] = BiosIdImage.BiosIdString.TimeStamp[3];
306 BiosReleaseDate[2] = (CHAR16) ((UINT8) ('/'));
307
308 BiosReleaseDate[3] = BiosIdImage.BiosIdString.TimeStamp[4];
309 BiosReleaseDate[4] = BiosIdImage.BiosIdString.TimeStamp[5];
310 BiosReleaseDate[5] = (CHAR16) ((UINT8) ('/'));
311
312 //
313 // Add 20 for SMBIOS table
314 // Current Linux kernel will misjudge 09 as year 0, so using 2009 for SMBIOS table
315 //
316 BiosReleaseDate[6] = '2';
317 BiosReleaseDate[7] = '0';
318 BiosReleaseDate[8] = BiosIdImage.BiosIdString.TimeStamp[0];
319 BiosReleaseDate[9] = BiosIdImage.BiosIdString.TimeStamp[1];
320
321 BiosReleaseDate[10] = (CHAR16) ((UINT8) ('\0'));
322 }
323
324 if (BiosReleaseTime != NULL) {
325
326 //
327 // Fill the build timestamp time from the BIOS ID in the "HH:MM" format.
328 //
329
330 BiosReleaseTime[0] = BiosIdImage.BiosIdString.TimeStamp[6];
331 BiosReleaseTime[1] = BiosIdImage.BiosIdString.TimeStamp[7];
332 BiosReleaseTime[2] = (CHAR16) ((UINT8) (':'));
333
334 BiosReleaseTime[3] = BiosIdImage.BiosIdString.TimeStamp[8];
335 BiosReleaseTime[4] = BiosIdImage.BiosIdString.TimeStamp[9];
336
337 BiosReleaseTime[5] = (CHAR16) ((UINT8) ('\0'));
338 }
339
340 return EFI_SUCCESS;
341 }
342