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