]> git.proxmox.com Git - mirror_edk2.git/blame - IntelSiliconPkg/Feature/Capsule/MicrocodeUpdateDxe/MicrocodeFmp.c
IntelSiliconPkg MicrocodeUpdate: Fix typo EFI_SECURITY_VIOLATIO
[mirror_edk2.git] / IntelSiliconPkg / Feature / Capsule / MicrocodeUpdateDxe / MicrocodeFmp.c
CommitLineData
88266859
JY
1/** @file\r
2 Produce FMP instance for Microcode.\r
3\r
1779cf40 4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
88266859
JY
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "MicrocodeUpdate.h"\r
16\r
17//\r
18// MicrocodeFmp driver private data\r
19//\r
20MICROCODE_FMP_PRIVATE_DATA *mMicrocodeFmpPrivate = NULL;\r
21\r
22EFI_FIRMWARE_MANAGEMENT_PROTOCOL mFirmwareManagementProtocol = {\r
23 FmpGetImageInfo,\r
24 FmpGetImage,\r
25 FmpSetImage,\r
26 FmpCheckImage,\r
27 FmpGetPackageInfo,\r
28 FmpSetPackageInfo\r
29};\r
30\r
31/**\r
32 Initialize Microcode Descriptor.\r
33\r
34 @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
35\r
36 @return EFI_SUCCESS Microcode Descriptor is initialized.\r
37**/\r
38EFI_STATUS\r
39InitializeMicrocodeDescriptor (\r
40 IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
41 );\r
42\r
43/**\r
44 Returns information about the current firmware image(s) of the device.\r
45\r
46 This function allows a copy of the current firmware image to be created and saved.\r
47 The saved copy could later been used, for example, in firmware image recovery or rollback.\r
48\r
49 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
50 @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.\r
51 On input, this is the size of the buffer allocated by the caller.\r
52 On output, it is the size of the buffer returned by the firmware\r
53 if the buffer was large enough, or the size of the buffer needed\r
54 to contain the image(s) information if the buffer was too small.\r
55 @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)\r
56 information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.\r
57 @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number\r
58 associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
59 @param[out] DescriptorCount A pointer to the location in which firmware returns the number of\r
60 descriptors or firmware images within this device.\r
61 @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,\r
62 of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r
63 @param[out] PackageVersion A version number that represents all the firmware images in the device.\r
64 The format is vendor specific and new version must have a greater value\r
65 than the old version. If PackageVersion is not supported, the value is\r
66 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison\r
67 is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates\r
68 that package version update is in progress.\r
69 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the\r
70 package version name. The buffer is allocated by this function with\r
71 AllocatePool(), and it is the caller's responsibility to free it with a call\r
72 to FreePool().\r
73\r
74 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
75 @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size\r
76 needed to hold the image(s) information is returned in ImageInfoSize.\r
77 @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.\r
78 @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.\r
79\r
80**/\r
81EFI_STATUS\r
82EFIAPI\r
83FmpGetImageInfo (\r
84 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
85 IN OUT UINTN *ImageInfoSize,\r
86 IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,\r
87 OUT UINT32 *DescriptorVersion,\r
88 OUT UINT8 *DescriptorCount,\r
89 OUT UINTN *DescriptorSize,\r
90 OUT UINT32 *PackageVersion,\r
91 OUT CHAR16 **PackageVersionName\r
92 )\r
93{\r
94 MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate;\r
95 UINTN Index;\r
96\r
97 MicrocodeFmpPrivate = MICROCODE_FMP_PRIVATE_DATA_FROM_FMP(This);\r
98\r
99 if(ImageInfoSize == NULL) {\r
100 return EFI_INVALID_PARAMETER;\r
101 }\r
102\r
103 if (*ImageInfoSize < sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount) {\r
104 *ImageInfoSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount;\r
105 return EFI_BUFFER_TOO_SMALL;\r
106 }\r
107\r
108 if (ImageInfo == NULL ||\r
109 DescriptorVersion == NULL ||\r
110 DescriptorCount == NULL ||\r
111 DescriptorSize == NULL ||\r
112 PackageVersion == NULL ||\r
113 PackageVersionName == NULL) {\r
114 return EFI_INVALID_PARAMETER;\r
115 }\r
116\r
117 *ImageInfoSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount;\r
118 *DescriptorSize = sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR);\r
119 *DescriptorCount = MicrocodeFmpPrivate->DescriptorCount;\r
120 *DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;\r
121\r
122 //\r
123 // supports 1 ImageInfo descriptor\r
124 //\r
125 CopyMem(&ImageInfo[0], MicrocodeFmpPrivate->ImageDescriptor, sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR) * MicrocodeFmpPrivate->DescriptorCount);\r
126 for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r
127 if ((ImageInfo[Index].AttributesSetting & IMAGE_ATTRIBUTE_IN_USE) != 0) {\r
128 ImageInfo[Index].LastAttemptVersion = MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion;\r
129 ImageInfo[Index].LastAttemptStatus = MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus;\r
130 }\r
131 }\r
132\r
133 //\r
134 // package version\r
135 //\r
136 *PackageVersion = MicrocodeFmpPrivate->PackageVersion;\r
137 if (MicrocodeFmpPrivate->PackageVersionName != NULL) {\r
138 *PackageVersionName = AllocateCopyPool(StrSize(MicrocodeFmpPrivate->PackageVersionName), MicrocodeFmpPrivate->PackageVersionName);\r
139 }\r
140\r
141 return EFI_SUCCESS;\r
142}\r
143\r
144/**\r
145 Retrieves a copy of the current firmware image of the device.\r
146\r
147 This function allows a copy of the current firmware image to be created and saved.\r
148 The saved copy could later been used, for example, in firmware image recovery or rollback.\r
149\r
150 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
151 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
152 The number is between 1 and DescriptorCount.\r
153 @param[in,out] Image Points to the buffer where the current image is copied to.\r
154 @param[in,out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.\r
155 On return, points to the length of the image, in bytes.\r
156\r
157 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
158 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the\r
159 image. The current buffer size needed to hold the image is returned\r
160 in ImageSize.\r
161 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
162 @retval EFI_NOT_FOUND The current image is not copied to the buffer.\r
163 @retval EFI_UNSUPPORTED The operation is not supported.\r
1779cf40 164 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
88266859
JY
165\r
166**/\r
167EFI_STATUS\r
168EFIAPI\r
169FmpGetImage (\r
170 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
171 IN UINT8 ImageIndex,\r
172 IN OUT VOID *Image,\r
173 IN OUT UINTN *ImageSize\r
174 )\r
175{\r
176 MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate;\r
2ed65824 177 MICROCODE_INFO *MicrocodeInfo;\r
88266859
JY
178\r
179 if (Image == NULL || ImageSize == NULL) {\r
180 return EFI_INVALID_PARAMETER;\r
181 }\r
182\r
183 MicrocodeFmpPrivate = MICROCODE_FMP_PRIVATE_DATA_FROM_FMP(This);\r
184\r
185 if (ImageIndex == 0 || ImageIndex > MicrocodeFmpPrivate->DescriptorCount || ImageSize == NULL || Image == NULL) {\r
186 return EFI_INVALID_PARAMETER;\r
187 }\r
188\r
2ed65824
JY
189 MicrocodeInfo = &MicrocodeFmpPrivate->MicrocodeInfo[ImageIndex - 1];\r
190\r
191 if (*ImageSize < MicrocodeInfo->TotalSize) {\r
192 *ImageSize = MicrocodeInfo->TotalSize;\r
193 return EFI_BUFFER_TOO_SMALL;\r
194 }\r
195\r
196 *ImageSize = MicrocodeInfo->TotalSize;\r
197 CopyMem (Image, MicrocodeInfo->MicrocodeEntryPoint, MicrocodeInfo->TotalSize);\r
198 return EFI_SUCCESS;\r
88266859
JY
199}\r
200\r
201/**\r
202 Updates the firmware image of the device.\r
203\r
204 This function updates the hardware with the new firmware image.\r
205 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.\r
206 If the firmware image is updatable, the function should perform the following minimal validations\r
207 before proceeding to do the firmware image update.\r
208 - Validate the image authentication if image has attribute\r
209 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns\r
210 EFI_SECURITY_VIOLATION if the validation fails.\r
211 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if\r
212 the image is unsupported. The function can optionally provide more detailed information on\r
213 why the image is not a supported image.\r
214 - Validate the data from VendorCode if not null. Image validation must be performed before\r
215 VendorCode data validation. VendorCode data is ignored or considered invalid if image\r
216 validation failed. The function returns EFI_ABORTED if the data is invalid.\r
217\r
218 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if\r
219 the caller did not specify the policy or use the default policy. As an example, vendor can implement\r
220 a policy to allow an option to force a firmware image update when the abort reason is due to the new\r
221 firmware image version is older than the current firmware image version or bad image checksum.\r
222 Sensitive operations such as those wiping the entire firmware image and render the device to be\r
223 non-functional should be encoded in the image itself rather than passed with the VendorCode.\r
224 AbortReason enables vendor to have the option to provide a more detailed description of the abort\r
225 reason to the caller.\r
226\r
227 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
228 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
229 The number is between 1 and DescriptorCount.\r
230 @param[in] Image Points to the new image.\r
231 @param[in] ImageSize Size of the new image in bytes.\r
232 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.\r
233 Null indicates the caller did not specify the policy or use the default policy.\r
234 @param[in] Progress A function used by the driver to report the progress of the firmware update.\r
235 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r
236 details for the aborted operation. The buffer is allocated by this function\r
237 with AllocatePool(), and it is the caller's responsibility to free it with a\r
238 call to FreePool().\r
239\r
240 @retval EFI_SUCCESS The device was successfully updated with the new image.\r
241 @retval EFI_ABORTED The operation is aborted.\r
242 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
243 @retval EFI_UNSUPPORTED The operation is not supported.\r
1779cf40 244 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
88266859
JY
245\r
246**/\r
247EFI_STATUS\r
248EFIAPI\r
249FmpSetImage (\r
250 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
251 IN UINT8 ImageIndex,\r
252 IN CONST VOID *Image,\r
253 IN UINTN ImageSize,\r
254 IN CONST VOID *VendorCode,\r
255 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
256 OUT CHAR16 **AbortReason\r
257 )\r
258{\r
259 EFI_STATUS Status;\r
260 EFI_STATUS VarStatus;\r
261 MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate;\r
262\r
263 if (Image == NULL || AbortReason == NULL) {\r
264 return EFI_INVALID_PARAMETER;\r
265 }\r
266\r
267 MicrocodeFmpPrivate = MICROCODE_FMP_PRIVATE_DATA_FROM_FMP(This);\r
268 *AbortReason = NULL;\r
269\r
270 if (ImageIndex == 0 || ImageIndex > MicrocodeFmpPrivate->DescriptorCount || Image == NULL) {\r
271 return EFI_INVALID_PARAMETER;\r
272 }\r
273\r
2ed65824 274 Status = MicrocodeWrite(MicrocodeFmpPrivate, (VOID *)Image, ImageSize, &MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion, &MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus, AbortReason);\r
88266859
JY
275 DEBUG((DEBUG_INFO, "SetImage - LastAttemp Version - 0x%x, State - 0x%x\n", MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion, MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus));\r
276 VarStatus = gRT->SetVariable(\r
277 MICROCODE_FMP_LAST_ATTEMPT_VARIABLE_NAME,\r
278 &gEfiCallerIdGuid,\r
279 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
280 sizeof(MicrocodeFmpPrivate->LastAttempt),\r
281 &MicrocodeFmpPrivate->LastAttempt\r
282 );\r
283 DEBUG((DEBUG_INFO, "SetLastAttemp - %r\n", VarStatus));\r
284\r
285 if (!EFI_ERROR(Status)) {\r
286 InitializeMicrocodeDescriptor(MicrocodeFmpPrivate);\r
31d060d9 287 DumpPrivateInfo (MicrocodeFmpPrivate);\r
88266859
JY
288 }\r
289\r
290 return Status;\r
291}\r
292\r
293/**\r
294 Checks if the firmware image is valid for the device.\r
295\r
296 This function allows firmware update application to validate the firmware image without\r
297 invoking the SetImage() first.\r
298\r
299 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
300 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.\r
301 The number is between 1 and DescriptorCount.\r
302 @param[in] Image Points to the new image.\r
303 @param[in] ImageSize Size of the new image in bytes.\r
304 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
305 if available, additional information if the image is invalid.\r
306\r
307 @retval EFI_SUCCESS The image was successfully checked.\r
308 @retval EFI_INVALID_PARAMETER The Image was NULL.\r
309 @retval EFI_UNSUPPORTED The operation is not supported.\r
1779cf40 310 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
88266859
JY
311\r
312**/\r
313EFI_STATUS\r
314EFIAPI\r
315FmpCheckImage (\r
316 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
317 IN UINT8 ImageIndex,\r
318 IN CONST VOID *Image,\r
319 IN UINTN ImageSize,\r
320 OUT UINT32 *ImageUpdatable\r
321 )\r
322{\r
323 return EFI_UNSUPPORTED;\r
324}\r
325\r
326/**\r
327 Returns information about the firmware package.\r
328\r
329 This function returns package information.\r
330\r
331 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
332 @param[out] PackageVersion A version number that represents all the firmware images in the device.\r
333 The format is vendor specific and new version must have a greater value\r
334 than the old version. If PackageVersion is not supported, the value is\r
335 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version\r
336 comparison is to be performed using PackageVersionName. A value of\r
337 0xFFFFFFFD indicates that package version update is in progress.\r
338 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing\r
339 the package version name. The buffer is allocated by this function with\r
340 AllocatePool(), and it is the caller's responsibility to free it with a\r
341 call to FreePool().\r
342 @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of\r
343 package version name. A value of 0 indicates the device does not support\r
344 update of package version name. Length is the number of Unicode characters,\r
345 including the terminating null character.\r
346 @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute\r
347 Definitions' for possible returned values of this parameter. A value of 1\r
348 indicates the attribute is supported and the current setting value is\r
349 indicated in AttributesSetting. A value of 0 indicates the attribute is not\r
350 supported and the current setting value in AttributesSetting is meaningless.\r
351 @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned\r
352 values of this parameter\r
353\r
354 @retval EFI_SUCCESS The package information was successfully returned.\r
355 @retval EFI_UNSUPPORTED The operation is not supported.\r
356\r
357**/\r
358EFI_STATUS\r
359EFIAPI\r
360FmpGetPackageInfo (\r
361 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
362 OUT UINT32 *PackageVersion,\r
363 OUT CHAR16 **PackageVersionName,\r
364 OUT UINT32 *PackageVersionNameMaxLen,\r
365 OUT UINT64 *AttributesSupported,\r
366 OUT UINT64 *AttributesSetting\r
367 )\r
368{\r
369 return EFI_UNSUPPORTED;\r
370}\r
371\r
372/**\r
373 Updates information about the firmware package.\r
374\r
375 This function updates package information.\r
376 This function returns EFI_UNSUPPORTED if the package information is not updatable.\r
377 VendorCode enables vendor to implement vendor-specific package information update policy.\r
378 Null if the caller did not specify this policy or use the default policy.\r
379\r
380 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.\r
381 @param[in] Image Points to the authentication image.\r
382 Null if authentication is not required.\r
383 @param[in] ImageSize Size of the authentication image in bytes.\r
384 0 if authentication is not required.\r
385 @param[in] VendorCode This enables vendor to implement vendor-specific firmware\r
386 image update policy.\r
387 Null indicates the caller did not specify this policy or use\r
388 the default policy.\r
389 @param[in] PackageVersion The new package version.\r
390 @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing\r
391 the package version name.\r
392 The string length is equal to or less than the value returned in\r
393 PackageVersionNameMaxLen.\r
394\r
395 @retval EFI_SUCCESS The device was successfully updated with the new package\r
396 information.\r
397 @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value\r
398 returned in PackageVersionNameMaxLen.\r
399 @retval EFI_UNSUPPORTED The operation is not supported.\r
1779cf40 400 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.\r
88266859
JY
401\r
402**/\r
403EFI_STATUS\r
404EFIAPI\r
405FmpSetPackageInfo (\r
406 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,\r
407 IN CONST VOID *Image,\r
408 IN UINTN ImageSize,\r
409 IN CONST VOID *VendorCode,\r
410 IN UINT32 PackageVersion,\r
411 IN CONST CHAR16 *PackageVersionName\r
412 )\r
413{\r
414 return EFI_UNSUPPORTED;\r
415}\r
416\r
31d060d9
JY
417/**\r
418 Initialize Processor Microcode Index.\r
419\r
420 @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
421**/\r
422VOID\r
423InitializedProcessorMicrocodeIndex (\r
424 IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
425 )\r
426{\r
427 UINTN CpuIndex;\r
428 UINTN MicrocodeIndex;\r
429 UINTN TargetCpuIndex;\r
430 UINT32 AttemptStatus;\r
431 EFI_STATUS Status;\r
432\r
433 for (CpuIndex = 0; CpuIndex < MicrocodeFmpPrivate->ProcessorCount; CpuIndex++) {\r
434 if (MicrocodeFmpPrivate->ProcessorInfo[CpuIndex].MicrocodeIndex != (UINTN)-1) {\r
435 continue;\r
436 }\r
437 for (MicrocodeIndex = 0; MicrocodeIndex < MicrocodeFmpPrivate->DescriptorCount; MicrocodeIndex++) {\r
438 if (!MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeIndex].InUse) {\r
439 continue;\r
440 }\r
441 TargetCpuIndex = CpuIndex;\r
442 Status = VerifyMicrocode(\r
443 MicrocodeFmpPrivate,\r
444 MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeIndex].MicrocodeEntryPoint,\r
445 MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeIndex].TotalSize,\r
446 FALSE,\r
447 &AttemptStatus,\r
448 NULL,\r
449 &TargetCpuIndex\r
450 );\r
451 if (!EFI_ERROR(Status)) {\r
452 MicrocodeFmpPrivate->ProcessorInfo[CpuIndex].MicrocodeIndex = MicrocodeIndex;\r
453 }\r
454 }\r
455 }\r
456}\r
457\r
88266859
JY
458/**\r
459 Initialize Microcode Descriptor.\r
460\r
461 @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
462\r
463 @return EFI_SUCCESS Microcode Descriptor is initialized.\r
464**/\r
465EFI_STATUS\r
466InitializeMicrocodeDescriptor (\r
467 IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
468 )\r
469{\r
2ed65824 470 UINT8 CurrentMicrocodeCount;\r
88266859 471\r
2ed65824 472 CurrentMicrocodeCount = (UINT8)GetMicrocodeInfo (MicrocodeFmpPrivate, 0, NULL, NULL);\r
88266859
JY
473\r
474 if (CurrentMicrocodeCount > MicrocodeFmpPrivate->DescriptorCount) {\r
475 if (MicrocodeFmpPrivate->ImageDescriptor != NULL) {\r
476 FreePool(MicrocodeFmpPrivate->ImageDescriptor);\r
477 MicrocodeFmpPrivate->ImageDescriptor = NULL;\r
478 }\r
2ed65824
JY
479 if (MicrocodeFmpPrivate->MicrocodeInfo != NULL) {\r
480 FreePool(MicrocodeFmpPrivate->MicrocodeInfo);\r
481 MicrocodeFmpPrivate->MicrocodeInfo = NULL;\r
482 }\r
88266859
JY
483 } else {\r
484 ZeroMem(MicrocodeFmpPrivate->ImageDescriptor, MicrocodeFmpPrivate->DescriptorCount * sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR));\r
2ed65824 485 ZeroMem(MicrocodeFmpPrivate->MicrocodeInfo, MicrocodeFmpPrivate->DescriptorCount * sizeof(MICROCODE_INFO));\r
88266859
JY
486 }\r
487\r
488 MicrocodeFmpPrivate->DescriptorCount = CurrentMicrocodeCount;\r
489\r
490 if (MicrocodeFmpPrivate->ImageDescriptor == NULL) {\r
491 MicrocodeFmpPrivate->ImageDescriptor = AllocateZeroPool(MicrocodeFmpPrivate->DescriptorCount * sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR));\r
492 if (MicrocodeFmpPrivate->ImageDescriptor == NULL) {\r
493 return EFI_OUT_OF_RESOURCES;\r
494 }\r
495 }\r
2ed65824
JY
496 if (MicrocodeFmpPrivate->MicrocodeInfo == NULL) {\r
497 MicrocodeFmpPrivate->MicrocodeInfo = AllocateZeroPool(MicrocodeFmpPrivate->DescriptorCount * sizeof(MICROCODE_INFO));\r
498 if (MicrocodeFmpPrivate->MicrocodeInfo == NULL) {\r
499 return EFI_OUT_OF_RESOURCES;\r
500 }\r
501 }\r
88266859 502\r
2ed65824 503 CurrentMicrocodeCount = (UINT8)GetMicrocodeInfo (MicrocodeFmpPrivate, MicrocodeFmpPrivate->DescriptorCount, MicrocodeFmpPrivate->ImageDescriptor, MicrocodeFmpPrivate->MicrocodeInfo);\r
88266859
JY
504 ASSERT(CurrentMicrocodeCount == MicrocodeFmpPrivate->DescriptorCount);\r
505\r
31d060d9
JY
506 InitializedProcessorMicrocodeIndex (MicrocodeFmpPrivate);\r
507\r
508 return EFI_SUCCESS;\r
509}\r
510\r
511/**\r
512 Initialize MicrocodeFmpDriver multiprocessor information.\r
513\r
514 @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
515\r
516 @return EFI_SUCCESS private data is initialized.\r
517**/\r
518EFI_STATUS\r
519InitializeProcessorInfo (\r
520 IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
521 )\r
522{\r
523 EFI_STATUS Status;\r
524 EFI_MP_SERVICES_PROTOCOL *MpService;\r
525 UINTN NumberOfProcessors;\r
526 UINTN NumberOfEnabledProcessors;\r
527 UINTN Index;\r
528 UINTN BspIndex;\r
529\r
530 Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpService);\r
531 ASSERT_EFI_ERROR(Status);\r
532\r
533 MicrocodeFmpPrivate->MpService = MpService;\r
534 MicrocodeFmpPrivate->ProcessorCount = 0;\r
535 MicrocodeFmpPrivate->ProcessorInfo = NULL;\r
536\r
537 Status = MpService->GetNumberOfProcessors (MpService, &NumberOfProcessors, &NumberOfEnabledProcessors);\r
538 ASSERT_EFI_ERROR(Status);\r
539 MicrocodeFmpPrivate->ProcessorCount = NumberOfProcessors;\r
540\r
541 Status = MpService->WhoAmI (MpService, &BspIndex);\r
542 ASSERT_EFI_ERROR(Status);\r
543 MicrocodeFmpPrivate->BspIndex = BspIndex;\r
544\r
545 MicrocodeFmpPrivate->ProcessorInfo = AllocateZeroPool (sizeof(PROCESSOR_INFO) * MicrocodeFmpPrivate->ProcessorCount);\r
546 if (MicrocodeFmpPrivate->ProcessorInfo == NULL) {\r
547 return EFI_OUT_OF_RESOURCES;\r
548 }\r
549\r
550 for (Index = 0; Index < NumberOfProcessors; Index++) {\r
551 MicrocodeFmpPrivate->ProcessorInfo[Index].CpuIndex = Index;\r
552 MicrocodeFmpPrivate->ProcessorInfo[Index].MicrocodeIndex = (UINTN)-1;\r
553 if (Index == BspIndex) {\r
554 CollectProcessorInfo (&MicrocodeFmpPrivate->ProcessorInfo[Index]);\r
555 } else {\r
556 Status = MpService->StartupThisAP (\r
557 MpService,\r
558 CollectProcessorInfo,\r
559 Index,\r
560 NULL,\r
561 0,\r
562 &MicrocodeFmpPrivate->ProcessorInfo[Index],\r
563 NULL\r
564 );\r
565 ASSERT_EFI_ERROR(Status);\r
566 }\r
567 }\r
568\r
88266859
JY
569 return EFI_SUCCESS;\r
570}\r
571\r
31d060d9
JY
572/**\r
573 Dump private information.\r
574\r
575 @param[in] MicrocodeFmpPrivate private data structure.\r
576**/\r
577VOID\r
578DumpPrivateInfo (\r
579 IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
580 )\r
581{\r
582 UINTN Index;\r
583 PROCESSOR_INFO *ProcessorInfo;\r
584 MICROCODE_INFO *MicrocodeInfo;\r
585 EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageDescriptor;\r
586\r
587 DEBUG ((DEBUG_INFO, "ProcessorInfo:\n"));\r
588 DEBUG ((DEBUG_INFO, " ProcessorCount - 0x%x\n", MicrocodeFmpPrivate->ProcessorCount));\r
589 DEBUG ((DEBUG_INFO, " BspIndex - 0x%x\n", MicrocodeFmpPrivate->BspIndex));\r
590\r
591 ProcessorInfo = MicrocodeFmpPrivate->ProcessorInfo;\r
592 for (Index = 0; Index < MicrocodeFmpPrivate->ProcessorCount; Index++) {\r
593 DEBUG ((\r
594 DEBUG_INFO,\r
595 " ProcessorInfo[0x%x] - 0x%08x, 0x%02x, 0x%08x, (0x%x)\n",\r
596 ProcessorInfo[Index].CpuIndex,\r
597 ProcessorInfo[Index].ProcessorSignature,\r
598 ProcessorInfo[Index].PlatformId,\r
599 ProcessorInfo[Index].MicrocodeRevision,\r
600 ProcessorInfo[Index].MicrocodeIndex\r
601 ));\r
602 }\r
603\r
604 DEBUG ((DEBUG_INFO, "MicrocodeInfo:\n"));\r
605 MicrocodeInfo = MicrocodeFmpPrivate->MicrocodeInfo;\r
606 DEBUG ((DEBUG_INFO, " MicrocodeRegion - 0x%x - 0x%x\n", MicrocodeFmpPrivate->MicrocodePatchAddress, MicrocodeFmpPrivate->MicrocodePatchRegionSize));\r
607 DEBUG ((DEBUG_INFO, " MicrocodeCount - 0x%x\n", MicrocodeFmpPrivate->DescriptorCount));\r
608 for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r
609 DEBUG ((\r
610 DEBUG_INFO,\r
611 " MicrocodeInfo[0x%x] - 0x%08x, 0x%08x, (0x%x)\n",\r
612 Index,\r
613 MicrocodeInfo[Index].MicrocodeEntryPoint,\r
614 MicrocodeInfo[Index].TotalSize,\r
615 MicrocodeInfo[Index].InUse\r
616 ));\r
617 }\r
618\r
619 ImageDescriptor = MicrocodeFmpPrivate->ImageDescriptor;\r
620 DEBUG ((DEBUG_VERBOSE, "ImageDescriptor:\n"));\r
621 for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r
622 DEBUG((DEBUG_VERBOSE, " ImageDescriptor (%d)\n", Index));\r
623 DEBUG((DEBUG_VERBOSE, " ImageIndex - 0x%x\n", ImageDescriptor[Index].ImageIndex));\r
624 DEBUG((DEBUG_VERBOSE, " ImageTypeId - %g\n", &ImageDescriptor[Index].ImageTypeId));\r
625 DEBUG((DEBUG_VERBOSE, " ImageId - 0x%lx\n", ImageDescriptor[Index].ImageId));\r
626 DEBUG((DEBUG_VERBOSE, " ImageIdName - %s\n", ImageDescriptor[Index].ImageIdName));\r
627 DEBUG((DEBUG_VERBOSE, " Version - 0x%x\n", ImageDescriptor[Index].Version));\r
628 DEBUG((DEBUG_VERBOSE, " VersionName - %s\n", ImageDescriptor[Index].VersionName));\r
629 DEBUG((DEBUG_VERBOSE, " Size - 0x%x\n", ImageDescriptor[Index].Size));\r
630 DEBUG((DEBUG_VERBOSE, " AttributesSupported - 0x%lx\n", ImageDescriptor[Index].AttributesSupported));\r
631 DEBUG((DEBUG_VERBOSE, " AttributesSetting - 0x%lx\n", ImageDescriptor[Index].AttributesSetting));\r
632 DEBUG((DEBUG_VERBOSE, " Compatibilities - 0x%lx\n", ImageDescriptor[Index].Compatibilities));\r
633 DEBUG((DEBUG_VERBOSE, " LowestSupportedImageVersion - 0x%x\n", ImageDescriptor[Index].LowestSupportedImageVersion));\r
634 DEBUG((DEBUG_VERBOSE, " LastAttemptVersion - 0x%x\n", ImageDescriptor[Index].LastAttemptVersion));\r
635 DEBUG((DEBUG_VERBOSE, " LastAttemptStatus - 0x%x\n", ImageDescriptor[Index].LastAttemptStatus));\r
636 DEBUG((DEBUG_VERBOSE, " HardwareInstance - 0x%lx\n", ImageDescriptor[Index].HardwareInstance));\r
637 }\r
638}\r
639\r
88266859
JY
640/**\r
641 Initialize MicrocodeFmpDriver private data structure.\r
642\r
643 @param[in] MicrocodeFmpPrivate private data structure to be initialized.\r
644\r
645 @return EFI_SUCCESS private data is initialized.\r
646**/\r
647EFI_STATUS\r
648InitializePrivateData (\r
649 IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r
650 )\r
651{\r
652 EFI_STATUS Status;\r
653 EFI_STATUS VarStatus;\r
654 UINTN VarSize;\r
2ed65824 655 BOOLEAN Result;\r
88266859
JY
656\r
657 MicrocodeFmpPrivate->Signature = MICROCODE_FMP_PRIVATE_DATA_SIGNATURE;\r
658 MicrocodeFmpPrivate->Handle = NULL;\r
659 CopyMem(&MicrocodeFmpPrivate->Fmp, &mFirmwareManagementProtocol, sizeof(EFI_FIRMWARE_MANAGEMENT_PROTOCOL));\r
660\r
661 MicrocodeFmpPrivate->PackageVersion = 0x1;\r
662 MicrocodeFmpPrivate->PackageVersionName = L"Microcode";\r
663\r
664 MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion = 0x0;\r
665 MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus = 0x0;\r
666 VarSize = sizeof(MicrocodeFmpPrivate->LastAttempt);\r
667 VarStatus = gRT->GetVariable(\r
668 MICROCODE_FMP_LAST_ATTEMPT_VARIABLE_NAME,\r
669 &gEfiCallerIdGuid,\r
670 NULL,\r
671 &VarSize,\r
672 &MicrocodeFmpPrivate->LastAttempt\r
673 );\r
674 DEBUG((DEBUG_INFO, "GetLastAttemp - %r\n", VarStatus));\r
675 DEBUG((DEBUG_INFO, "GetLastAttemp Version - 0x%x, State - 0x%x\n", MicrocodeFmpPrivate->LastAttempt.LastAttemptVersion, MicrocodeFmpPrivate->LastAttempt.LastAttemptStatus));\r
676\r
2ed65824
JY
677 Result = GetMicrocodeRegion(&MicrocodeFmpPrivate->MicrocodePatchAddress, &MicrocodeFmpPrivate->MicrocodePatchRegionSize);\r
678 if (!Result) {\r
679 DEBUG((DEBUG_ERROR, "Fail to get Microcode Region\n"));\r
680 return EFI_NOT_FOUND;\r
681 }\r
682\r
31d060d9
JY
683 Status = InitializeProcessorInfo (MicrocodeFmpPrivate);\r
684 if (EFI_ERROR(Status)) {\r
685 DEBUG((DEBUG_ERROR, "InitializeProcessorInfo - %r\n", Status));\r
686 return Status;\r
687 }\r
688\r
88266859 689 Status = InitializeMicrocodeDescriptor(MicrocodeFmpPrivate);\r
31d060d9
JY
690 if (EFI_ERROR(Status)) {\r
691 DEBUG((DEBUG_ERROR, "InitializeMicrocodeDescriptor - %r\n", Status));\r
692 return Status;\r
693 }\r
694\r
695 DumpPrivateInfo (MicrocodeFmpPrivate);\r
88266859
JY
696\r
697 return Status;\r
698}\r
699\r
700/**\r
701 Microcode FMP module entrypoint\r
702\r
703 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
704 @param[in] SystemTable A pointer to the EFI System Table.\r
705\r
706 @return EFI_SUCCESS Microcode FMP module is initialized.\r
707**/\r
708EFI_STATUS\r
709EFIAPI\r
710MicrocodeFmpMain (\r
711 IN EFI_HANDLE ImageHandle,\r
712 IN EFI_SYSTEM_TABLE *SystemTable\r
713 )\r
714{\r
715 EFI_STATUS Status;\r
716\r
717 //\r
718 // Initialize MicrocodeFmpPrivateData\r
719 //\r
720 mMicrocodeFmpPrivate = AllocateZeroPool (sizeof(MICROCODE_FMP_PRIVATE_DATA));\r
721 if (mMicrocodeFmpPrivate == NULL) {\r
722 return EFI_OUT_OF_RESOURCES;\r
723 }\r
724\r
725 Status = InitializePrivateData(mMicrocodeFmpPrivate);\r
726 if (EFI_ERROR(Status)) {\r
727 FreePool(mMicrocodeFmpPrivate);\r
728 mMicrocodeFmpPrivate = NULL;\r
729 return Status;\r
730 }\r
731\r
732 //\r
733 // Install FMP protocol.\r
734 //\r
735 Status = gBS->InstallProtocolInterface (\r
736 &mMicrocodeFmpPrivate->Handle,\r
737 &gEfiFirmwareManagementProtocolGuid,\r
738 EFI_NATIVE_INTERFACE,\r
739 &mMicrocodeFmpPrivate->Fmp\r
740 );\r
741 if (EFI_ERROR (Status)) {\r
742 FreePool(mMicrocodeFmpPrivate);\r
743 mMicrocodeFmpPrivate = NULL;\r
744 return Status;\r
745 }\r
746\r
747 return Status;\r
748}\r