]>
Commit | Line | Data |
---|---|---|
88266859 JY |
1 | /** @file\r |
2 | SetImage instance to update Microcode.\r | |
3 | \r | |
4 | Caution: This module requires additional review when modified.\r | |
5 | This module will have external input - capsule image.\r | |
6 | This external input must be validated carefully to avoid security issue like\r | |
7 | buffer overflow, integer overflow.\r | |
8 | \r | |
9 | MicrocodeWrite() and VerifyMicrocode() will receive untrusted input and do basic validation.\r | |
10 | \r | |
3bd91ae2 | 11 | Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r |
88266859 JY |
12 | This program and the accompanying materials\r |
13 | are licensed and made available under the terms and conditions of the BSD License\r | |
14 | which accompanies this distribution. The full text of the license may be found at\r | |
15 | http://opensource.org/licenses/bsd-license.php\r | |
16 | \r | |
17 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
18 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
19 | \r | |
20 | **/\r | |
21 | \r | |
22 | #include "MicrocodeUpdate.h"\r | |
23 | \r | |
24 | /**\r | |
25 | Get Microcode Region.\r | |
26 | \r | |
27 | @param[out] MicrocodePatchAddress The address of Microcode\r | |
28 | @param[out] MicrocodePatchRegionSize The region size of Microcode\r | |
29 | \r | |
30 | @retval TRUE The Microcode region is returned.\r | |
31 | @retval FALSE No Microcode region.\r | |
32 | **/\r | |
33 | BOOLEAN\r | |
34 | GetMicrocodeRegion (\r | |
2ed65824 JY |
35 | OUT VOID **MicrocodePatchAddress,\r |
36 | OUT UINTN *MicrocodePatchRegionSize\r | |
88266859 JY |
37 | )\r |
38 | {\r | |
2ed65824 JY |
39 | *MicrocodePatchAddress = (VOID *)(UINTN)PcdGet64(PcdCpuMicrocodePatchAddress);\r |
40 | *MicrocodePatchRegionSize = (UINTN)PcdGet64(PcdCpuMicrocodePatchRegionSize);\r | |
88266859 | 41 | \r |
2ed65824 | 42 | if ((*MicrocodePatchAddress == NULL) || (*MicrocodePatchRegionSize == 0)) {\r |
88266859 JY |
43 | return FALSE;\r |
44 | }\r | |
45 | \r | |
46 | return TRUE;\r | |
47 | }\r | |
48 | \r | |
49 | /**\r | |
50 | Get Microcode update signature of currently loaded Microcode update.\r | |
51 | \r | |
52 | @return Microcode signature.\r | |
53 | \r | |
54 | **/\r | |
55 | UINT32\r | |
56 | GetCurrentMicrocodeSignature (\r | |
57 | VOID\r | |
58 | )\r | |
59 | {\r | |
60 | UINT64 Signature;\r | |
61 | \r | |
62 | AsmWriteMsr64(MSR_IA32_BIOS_SIGN_ID, 0);\r | |
63 | AsmCpuid(CPUID_VERSION_INFO, NULL, NULL, NULL, NULL);\r | |
64 | Signature = AsmReadMsr64(MSR_IA32_BIOS_SIGN_ID);\r | |
65 | return (UINT32)RShiftU64(Signature, 32);\r | |
66 | }\r | |
67 | \r | |
68 | /**\r | |
69 | Get current processor signature.\r | |
70 | \r | |
71 | @return current processor signature.\r | |
72 | **/\r | |
73 | UINT32\r | |
74 | GetCurrentProcessorSignature (\r | |
75 | VOID\r | |
76 | )\r | |
77 | {\r | |
78 | UINT32 RegEax;\r | |
79 | AsmCpuid(CPUID_VERSION_INFO, &RegEax, NULL, NULL, NULL);\r | |
80 | return RegEax;\r | |
81 | }\r | |
82 | \r | |
83 | /**\r | |
84 | Get current platform ID.\r | |
85 | \r | |
86 | @return current platform ID.\r | |
87 | **/\r | |
88 | UINT8\r | |
89 | GetCurrentPlatformId (\r | |
90 | VOID\r | |
91 | )\r | |
92 | {\r | |
93 | UINT8 PlatformId;\r | |
94 | \r | |
95 | PlatformId = (UINT8)AsmMsrBitFieldRead64(MSR_IA32_PLATFORM_ID, 50, 52);\r | |
96 | return PlatformId;\r | |
97 | }\r | |
98 | \r | |
99 | /**\r | |
100 | Load new Microcode.\r | |
101 | \r | |
102 | @param[in] Address The address of new Microcode.\r | |
103 | \r | |
104 | @return Loaded Microcode signature.\r | |
105 | \r | |
106 | **/\r | |
107 | UINT32\r | |
108 | LoadMicrocode (\r | |
109 | IN UINT64 Address\r | |
110 | )\r | |
111 | {\r | |
112 | AsmWriteMsr64(MSR_IA32_BIOS_UPDT_TRIG, Address);\r | |
113 | return GetCurrentMicrocodeSignature();\r | |
114 | }\r | |
115 | \r | |
2ed65824 | 116 | /**\r |
31d060d9 JY |
117 | Load Microcode on an Application Processor.\r |
118 | The function prototype for invoking a function on an Application Processor.\r | |
119 | \r | |
120 | @param[in,out] Buffer The pointer to private data buffer.\r | |
121 | **/\r | |
122 | VOID\r | |
123 | EFIAPI\r | |
124 | MicrocodeLoadAp (\r | |
125 | IN OUT VOID *Buffer\r | |
126 | )\r | |
127 | {\r | |
128 | MICROCODE_LOAD_BUFFER *MicrocodeLoadBuffer;\r | |
129 | \r | |
130 | MicrocodeLoadBuffer = Buffer;\r | |
131 | MicrocodeLoadBuffer->Revision = LoadMicrocode (MicrocodeLoadBuffer->Address);\r | |
132 | }\r | |
133 | \r | |
134 | /**\r | |
135 | Load new Microcode on this processor\r | |
2ed65824 | 136 | \r |
31d060d9 JY |
137 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r |
138 | @param[in] CpuIndex The index of the processor.\r | |
139 | @param[in] Address The address of new Microcode.\r | |
140 | \r | |
141 | @return Loaded Microcode signature.\r | |
2ed65824 | 142 | \r |
2ed65824 | 143 | **/\r |
31d060d9 JY |
144 | UINT32\r |
145 | LoadMicrocodeOnThis (\r | |
146 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r | |
147 | IN UINTN CpuIndex,\r | |
148 | IN UINT64 Address\r | |
2ed65824 JY |
149 | )\r |
150 | {\r | |
31d060d9 JY |
151 | EFI_STATUS Status;\r |
152 | EFI_MP_SERVICES_PROTOCOL *MpService;\r | |
153 | MICROCODE_LOAD_BUFFER MicrocodeLoadBuffer;\r | |
2ed65824 | 154 | \r |
31d060d9 JY |
155 | if (CpuIndex == MicrocodeFmpPrivate->BspIndex) {\r |
156 | return LoadMicrocode (Address);\r | |
157 | } else {\r | |
158 | MpService = MicrocodeFmpPrivate->MpService;\r | |
159 | MicrocodeLoadBuffer.Address = Address;\r | |
160 | MicrocodeLoadBuffer.Revision = 0;\r | |
161 | Status = MpService->StartupThisAP (\r | |
162 | MpService,\r | |
163 | MicrocodeLoadAp,\r | |
164 | CpuIndex,\r | |
165 | NULL,\r | |
166 | 0,\r | |
167 | &MicrocodeLoadBuffer,\r | |
168 | NULL\r | |
169 | );\r | |
170 | ASSERT_EFI_ERROR(Status);\r | |
171 | return MicrocodeLoadBuffer.Revision;\r | |
2ed65824 | 172 | }\r |
31d060d9 JY |
173 | }\r |
174 | \r | |
175 | /**\r | |
176 | Collect processor information.\r | |
177 | The function prototype for invoking a function on an Application Processor.\r | |
178 | \r | |
179 | @param[in,out] Buffer The pointer to private data buffer.\r | |
180 | **/\r | |
181 | VOID\r | |
182 | EFIAPI\r | |
183 | CollectProcessorInfo (\r | |
184 | IN OUT VOID *Buffer\r | |
185 | )\r | |
186 | {\r | |
187 | PROCESSOR_INFO *ProcessorInfo;\r | |
188 | \r | |
189 | ProcessorInfo = Buffer;\r | |
190 | ProcessorInfo->ProcessorSignature = GetCurrentProcessorSignature();\r | |
191 | ProcessorInfo->PlatformId = GetCurrentPlatformId();\r | |
192 | ProcessorInfo->MicrocodeRevision = GetCurrentMicrocodeSignature();\r | |
2ed65824 JY |
193 | }\r |
194 | \r | |
88266859 JY |
195 | /**\r |
196 | Get current Microcode information.\r | |
197 | \r | |
31d060d9 JY |
198 | The ProcessorInformation (BspIndex/ProcessorCount/ProcessorInfo)\r |
199 | in MicrocodeFmpPrivate must be initialized.\r | |
200 | \r | |
201 | The MicrocodeInformation (DescriptorCount/ImageDescriptor/MicrocodeInfo)\r | |
202 | in MicrocodeFmpPrivate may not be avaiable in this function.\r | |
2ed65824 JY |
203 | \r |
204 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r | |
205 | @param[in] DescriptorCount The count of Microcode ImageDescriptor allocated.\r | |
206 | @param[out] ImageDescriptor Microcode ImageDescriptor\r | |
207 | @param[out] MicrocodeInfo Microcode information\r | |
88266859 JY |
208 | \r |
209 | @return Microcode count\r | |
210 | **/\r | |
211 | UINTN\r | |
212 | GetMicrocodeInfo (\r | |
2ed65824 JY |
213 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r |
214 | IN UINTN DescriptorCount, OPTIONAL\r | |
88266859 | 215 | OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageDescriptor, OPTIONAL\r |
2ed65824 | 216 | OUT MICROCODE_INFO *MicrocodeInfo OPTIONAL\r |
88266859 JY |
217 | )\r |
218 | {\r | |
2ed65824 JY |
219 | VOID *MicrocodePatchAddress;\r |
220 | UINTN MicrocodePatchRegionSize;\r | |
88266859 JY |
221 | CPU_MICROCODE_HEADER *MicrocodeEntryPoint;\r |
222 | UINTN MicrocodeEnd;\r | |
223 | UINTN TotalSize;\r | |
224 | UINTN Count;\r | |
225 | UINT64 ImageAttributes;\r | |
2ed65824 | 226 | BOOLEAN IsInUse;\r |
31d060d9 JY |
227 | EFI_STATUS Status;\r |
228 | UINT32 AttemptStatus;\r | |
229 | UINTN TargetCpuIndex;\r | |
88266859 | 230 | \r |
2ed65824 JY |
231 | MicrocodePatchAddress = MicrocodeFmpPrivate->MicrocodePatchAddress;\r |
232 | MicrocodePatchRegionSize = MicrocodeFmpPrivate->MicrocodePatchRegionSize;\r | |
233 | \r | |
234 | DEBUG((DEBUG_INFO, "Microcode Region - 0x%x - 0x%x\n", MicrocodePatchAddress, MicrocodePatchRegionSize));\r | |
88266859 JY |
235 | \r |
236 | Count = 0;\r | |
88266859 | 237 | \r |
2ed65824 | 238 | MicrocodeEnd = (UINTN)MicrocodePatchAddress + MicrocodePatchRegionSize;\r |
88266859 JY |
239 | MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN) MicrocodePatchAddress;\r |
240 | do {\r | |
241 | if (MicrocodeEntryPoint->HeaderVersion == 0x1 && MicrocodeEntryPoint->LoaderRevision == 0x1) {\r | |
242 | //\r | |
243 | // It is the microcode header. It is not the padding data between microcode patches\r | |
244 | // becasue the padding data should not include 0x00000001 and it should be the repeated\r | |
245 | // byte format (like 0xXYXYXYXY....).\r | |
246 | //\r | |
247 | if (MicrocodeEntryPoint->DataSize == 0) {\r | |
248 | TotalSize = 2048;\r | |
249 | } else {\r | |
250 | TotalSize = MicrocodeEntryPoint->TotalSize;\r | |
251 | }\r | |
252 | \r | |
31d060d9 JY |
253 | TargetCpuIndex = (UINTN)-1;\r |
254 | Status = VerifyMicrocode(MicrocodeFmpPrivate, MicrocodeEntryPoint, TotalSize, FALSE, &AttemptStatus, NULL, &TargetCpuIndex);\r | |
255 | if (!EFI_ERROR(Status)) {\r | |
256 | IsInUse = TRUE;\r | |
257 | ASSERT (TargetCpuIndex < MicrocodeFmpPrivate->ProcessorCount);\r | |
258 | MicrocodeFmpPrivate->ProcessorInfo[TargetCpuIndex].MicrocodeIndex = Count;\r | |
259 | } else {\r | |
260 | IsInUse = FALSE;\r | |
261 | }\r | |
2ed65824 | 262 | \r |
88266859 JY |
263 | if (ImageDescriptor != NULL && DescriptorCount > Count) {\r |
264 | ImageDescriptor[Count].ImageIndex = (UINT8)(Count + 1);\r | |
265 | CopyGuid (&ImageDescriptor[Count].ImageTypeId, &gMicrocodeFmpImageTypeIdGuid);\r | |
266 | ImageDescriptor[Count].ImageId = LShiftU64(MicrocodeEntryPoint->ProcessorFlags, 32) + MicrocodeEntryPoint->ProcessorSignature.Uint32;\r | |
267 | ImageDescriptor[Count].ImageIdName = NULL;\r | |
268 | ImageDescriptor[Count].Version = MicrocodeEntryPoint->UpdateRevision;\r | |
269 | ImageDescriptor[Count].VersionName = NULL;\r | |
270 | ImageDescriptor[Count].Size = TotalSize;\r | |
271 | ImageAttributes = IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_RESET_REQUIRED;\r | |
2ed65824 | 272 | if (IsInUse) {\r |
88266859 JY |
273 | ImageAttributes |= IMAGE_ATTRIBUTE_IN_USE;\r |
274 | }\r | |
275 | ImageDescriptor[Count].AttributesSupported = ImageAttributes | IMAGE_ATTRIBUTE_IN_USE;\r | |
276 | ImageDescriptor[Count].AttributesSetting = ImageAttributes;\r | |
277 | ImageDescriptor[Count].Compatibilities = 0;\r | |
278 | ImageDescriptor[Count].LowestSupportedImageVersion = MicrocodeEntryPoint->UpdateRevision; // do not support rollback\r | |
279 | ImageDescriptor[Count].LastAttemptVersion = 0;\r | |
280 | ImageDescriptor[Count].LastAttemptStatus = 0;\r | |
281 | ImageDescriptor[Count].HardwareInstance = 0;\r | |
282 | }\r | |
2ed65824 JY |
283 | if (MicrocodeInfo != NULL && DescriptorCount > Count) {\r |
284 | MicrocodeInfo[Count].MicrocodeEntryPoint = MicrocodeEntryPoint;\r | |
285 | MicrocodeInfo[Count].TotalSize = TotalSize;\r | |
286 | MicrocodeInfo[Count].InUse = IsInUse;\r | |
287 | }\r | |
88266859 JY |
288 | } else {\r |
289 | //\r | |
290 | // It is the padding data between the microcode patches for microcode patches alignment.\r | |
291 | // Because the microcode patch is the multiple of 1-KByte, the padding data should not\r | |
292 | // exist if the microcode patch alignment value is not larger than 1-KByte. So, the microcode\r | |
293 | // alignment value should be larger than 1-KByte. We could skip SIZE_1KB padding data to\r | |
294 | // find the next possible microcode patch header.\r | |
295 | //\r | |
296 | MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + SIZE_1KB);\r | |
297 | continue;\r | |
298 | }\r | |
299 | \r | |
300 | Count++;\r | |
301 | ASSERT(Count < 0xFF);\r | |
302 | \r | |
303 | //\r | |
304 | // Get the next patch.\r | |
305 | //\r | |
306 | MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + TotalSize);\r | |
307 | } while (((UINTN) MicrocodeEntryPoint < MicrocodeEnd));\r | |
308 | \r | |
309 | return Count;\r | |
310 | }\r | |
311 | \r | |
31d060d9 JY |
312 | /**\r |
313 | Return matched processor information.\r | |
314 | \r | |
315 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r | |
316 | @param[in] ProcessorSignature The processor signature to be matched\r | |
317 | @param[in] ProcessorFlags The processor flags to be matched\r | |
318 | @param[in, out] TargetCpuIndex On input, the index of target CPU which tries to match the Microcode. (UINTN)-1 means to try all.\r | |
319 | On output, the index of target CPU which matches the Microcode.\r | |
320 | \r | |
321 | @return matched processor information.\r | |
322 | **/\r | |
323 | PROCESSOR_INFO *\r | |
324 | GetMatchedProcessor (\r | |
325 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r | |
326 | IN UINT32 ProcessorSignature,\r | |
327 | IN UINT32 ProcessorFlags,\r | |
328 | IN OUT UINTN *TargetCpuIndex\r | |
329 | )\r | |
330 | {\r | |
331 | UINTN Index;\r | |
332 | \r | |
333 | if (*TargetCpuIndex != (UINTN)-1) {\r | |
334 | Index = *TargetCpuIndex;\r | |
335 | if ((ProcessorSignature == MicrocodeFmpPrivate->ProcessorInfo[Index].ProcessorSignature) &&\r | |
336 | ((ProcessorFlags & (1 << MicrocodeFmpPrivate->ProcessorInfo[Index].PlatformId)) != 0)) {\r | |
337 | return &MicrocodeFmpPrivate->ProcessorInfo[Index];\r | |
338 | } else {\r | |
339 | return NULL;\r | |
340 | }\r | |
341 | }\r | |
342 | \r | |
343 | for (Index = 0; Index < MicrocodeFmpPrivate->ProcessorCount; Index++) {\r | |
344 | if ((ProcessorSignature == MicrocodeFmpPrivate->ProcessorInfo[Index].ProcessorSignature) &&\r | |
345 | ((ProcessorFlags & (1 << MicrocodeFmpPrivate->ProcessorInfo[Index].PlatformId)) != 0)) {\r | |
346 | *TargetCpuIndex = Index;\r | |
347 | return &MicrocodeFmpPrivate->ProcessorInfo[Index];\r | |
348 | }\r | |
349 | }\r | |
350 | return NULL;\r | |
351 | }\r | |
352 | \r | |
88266859 JY |
353 | /**\r |
354 | Verify Microcode.\r | |
355 | \r | |
356 | Caution: This function may receive untrusted input.\r | |
357 | \r | |
31d060d9 JY |
358 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r |
359 | @param[in] Image The Microcode image buffer.\r | |
360 | @param[in] ImageSize The size of Microcode image buffer in bytes.\r | |
361 | @param[in] TryLoad Try to load Microcode or not.\r | |
362 | @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r | |
363 | @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r | |
364 | details for the aborted operation. The buffer is allocated by this function\r | |
365 | with AllocatePool(), and it is the caller's responsibility to free it with a\r | |
366 | call to FreePool().\r | |
367 | @param[in, out] TargetCpuIndex On input, the index of target CPU which tries to match the Microcode. (UINTN)-1 means to try all.\r | |
368 | On output, the index of target CPU which matches the Microcode.\r | |
88266859 JY |
369 | \r |
370 | @retval EFI_SUCCESS The Microcode image passes verification.\r | |
371 | @retval EFI_VOLUME_CORRUPTED The Microcode image is corrupt.\r | |
372 | @retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.\r | |
373 | @retval EFI_UNSUPPORTED The Microcode ProcessorSignature or ProcessorFlags is incorrect.\r | |
374 | @retval EFI_SECURITY_VIOLATION The Microcode image fails to load.\r | |
375 | **/\r | |
376 | EFI_STATUS\r | |
377 | VerifyMicrocode (\r | |
31d060d9 JY |
378 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r |
379 | IN VOID *Image,\r | |
380 | IN UINTN ImageSize,\r | |
381 | IN BOOLEAN TryLoad,\r | |
382 | OUT UINT32 *LastAttemptStatus,\r | |
383 | OUT CHAR16 **AbortReason, OPTIONAL\r | |
384 | IN OUT UINTN *TargetCpuIndex\r | |
88266859 JY |
385 | )\r |
386 | {\r | |
387 | UINTN Index;\r | |
388 | CPU_MICROCODE_HEADER *MicrocodeEntryPoint;\r | |
389 | UINTN TotalSize;\r | |
390 | UINTN DataSize;\r | |
391 | UINT32 CurrentRevision;\r | |
31d060d9 | 392 | PROCESSOR_INFO *ProcessorInfo;\r |
88266859 JY |
393 | UINT32 CheckSum32;\r |
394 | UINTN ExtendedTableLength;\r | |
395 | UINT32 ExtendedTableCount;\r | |
396 | CPU_MICROCODE_EXTENDED_TABLE *ExtendedTable;\r | |
397 | CPU_MICROCODE_EXTENDED_TABLE_HEADER *ExtendedTableHeader;\r | |
398 | BOOLEAN CorrectMicrocode;\r | |
399 | \r | |
400 | //\r | |
401 | // Check HeaderVersion\r | |
402 | //\r | |
403 | MicrocodeEntryPoint = Image;\r | |
404 | if (MicrocodeEntryPoint->HeaderVersion != 0x1) {\r | |
405 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on HeaderVersion\n"));\r | |
406 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
407 | if (AbortReason != NULL) {\r | |
408 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidHeaderVersion"), L"InvalidHeaderVersion");\r | |
409 | }\r | |
410 | return EFI_INCOMPATIBLE_VERSION;\r | |
411 | }\r | |
412 | //\r | |
413 | // Check LoaderRevision\r | |
414 | //\r | |
415 | if (MicrocodeEntryPoint->LoaderRevision != 0x1) {\r | |
416 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on LoaderRevision\n"));\r | |
417 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
418 | if (AbortReason != NULL) {\r | |
419 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidLoaderVersion"), L"InvalidLoaderVersion");\r | |
420 | }\r | |
421 | return EFI_INCOMPATIBLE_VERSION;\r | |
422 | }\r | |
423 | //\r | |
424 | // Check Size\r | |
425 | //\r | |
426 | if (MicrocodeEntryPoint->DataSize == 0) {\r | |
427 | TotalSize = 2048;\r | |
428 | } else {\r | |
429 | TotalSize = MicrocodeEntryPoint->TotalSize;\r | |
430 | }\r | |
431 | if (TotalSize <= sizeof(CPU_MICROCODE_HEADER)) {\r | |
432 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - TotalSize too small\n"));\r | |
433 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
434 | if (AbortReason != NULL) {\r | |
435 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidTotalSize"), L"InvalidTotalSize");\r | |
436 | }\r | |
437 | return EFI_VOLUME_CORRUPTED;\r | |
438 | }\r | |
439 | if (TotalSize != ImageSize) {\r | |
440 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on TotalSize\n"));\r | |
441 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
442 | if (AbortReason != NULL) {\r | |
443 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidTotalSize"), L"InvalidTotalSize");\r | |
444 | }\r | |
445 | return EFI_VOLUME_CORRUPTED;\r | |
446 | }\r | |
447 | //\r | |
448 | // Check CheckSum32\r | |
449 | //\r | |
450 | if (MicrocodeEntryPoint->DataSize == 0) {\r | |
451 | DataSize = 2048 - sizeof(CPU_MICROCODE_HEADER);\r | |
452 | } else {\r | |
453 | DataSize = MicrocodeEntryPoint->DataSize;\r | |
454 | }\r | |
455 | if (DataSize > TotalSize - sizeof(CPU_MICROCODE_HEADER)) {\r | |
456 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - DataSize too big\n"));\r | |
457 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
458 | if (AbortReason != NULL) {\r | |
459 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidDataSize"), L"InvalidDataSize");\r | |
460 | }\r | |
461 | return EFI_VOLUME_CORRUPTED;\r | |
462 | }\r | |
463 | if ((DataSize & 0x3) != 0) {\r | |
464 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - DataSize not aligned\n"));\r | |
465 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
466 | if (AbortReason != NULL) {\r | |
467 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidDataSize"), L"InvalidDataSize");\r | |
468 | }\r | |
469 | return EFI_VOLUME_CORRUPTED;\r | |
470 | }\r | |
471 | CheckSum32 = CalculateSum32((UINT32 *)MicrocodeEntryPoint, DataSize + sizeof(CPU_MICROCODE_HEADER));\r | |
472 | if (CheckSum32 != 0) {\r | |
473 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on CheckSum32\n"));\r | |
474 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;\r | |
475 | if (AbortReason != NULL) {\r | |
476 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidChecksum"), L"InvalidChecksum");\r | |
477 | }\r | |
478 | return EFI_VOLUME_CORRUPTED;\r | |
479 | }\r | |
480 | \r | |
481 | //\r | |
482 | // Check ProcessorSignature/ProcessorFlags\r | |
483 | //\r | |
31d060d9 JY |
484 | \r |
485 | ProcessorInfo = GetMatchedProcessor (MicrocodeFmpPrivate, MicrocodeEntryPoint->ProcessorSignature.Uint32, MicrocodeEntryPoint->ProcessorFlags, TargetCpuIndex);\r | |
486 | if (ProcessorInfo == NULL) {\r | |
487 | CorrectMicrocode = FALSE;\r | |
88266859 JY |
488 | ExtendedTableLength = TotalSize - (DataSize + sizeof(CPU_MICROCODE_HEADER));\r |
489 | if (ExtendedTableLength != 0) {\r | |
490 | //\r | |
491 | // Extended Table exist, check if the CPU in support list\r | |
492 | //\r | |
493 | ExtendedTableHeader = (CPU_MICROCODE_EXTENDED_TABLE_HEADER *)((UINT8 *)(MicrocodeEntryPoint) + DataSize + sizeof(CPU_MICROCODE_HEADER));\r | |
494 | //\r | |
495 | // Calculate Extended Checksum\r | |
496 | //\r | |
3bd91ae2 | 497 | if ((ExtendedTableLength > sizeof(CPU_MICROCODE_EXTENDED_TABLE_HEADER)) && ((ExtendedTableLength & 0x3) == 0)) {\r |
88266859 JY |
498 | CheckSum32 = CalculateSum32((UINT32 *)ExtendedTableHeader, ExtendedTableLength);\r |
499 | if (CheckSum32 == 0) {\r | |
500 | //\r | |
501 | // Checksum correct\r | |
502 | //\r | |
503 | ExtendedTableCount = ExtendedTableHeader->ExtendedSignatureCount;\r | |
504 | if (ExtendedTableCount <= (ExtendedTableLength - sizeof(CPU_MICROCODE_EXTENDED_TABLE_HEADER)) / sizeof(CPU_MICROCODE_EXTENDED_TABLE)) {\r | |
505 | ExtendedTable = (CPU_MICROCODE_EXTENDED_TABLE *)(ExtendedTableHeader + 1);\r | |
506 | for (Index = 0; Index < ExtendedTableCount; Index++) {\r | |
507 | CheckSum32 = CalculateSum32((UINT32 *)ExtendedTable, sizeof(CPU_MICROCODE_EXTENDED_TABLE));\r | |
508 | if (CheckSum32 == 0) {\r | |
509 | //\r | |
510 | // Verify Header\r | |
511 | //\r | |
31d060d9 JY |
512 | ProcessorInfo = GetMatchedProcessor (MicrocodeFmpPrivate, ExtendedTable->ProcessorSignature.Uint32, ExtendedTable->ProcessorFlag, TargetCpuIndex);\r |
513 | if (ProcessorInfo != NULL) {\r | |
88266859 JY |
514 | //\r |
515 | // Find one\r | |
516 | //\r | |
517 | CorrectMicrocode = TRUE;\r | |
518 | break;\r | |
519 | }\r | |
520 | }\r | |
521 | ExtendedTable++;\r | |
522 | }\r | |
523 | }\r | |
524 | }\r | |
525 | }\r | |
526 | }\r | |
527 | if (!CorrectMicrocode) {\r | |
2ed65824 JY |
528 | if (TryLoad) {\r |
529 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on CurrentProcessorSignature/ProcessorFlags\n"));\r | |
530 | }\r | |
88266859 JY |
531 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;\r |
532 | if (AbortReason != NULL) {\r | |
31d060d9 | 533 | *AbortReason = AllocateCopyPool(sizeof(L"UnsupportedProcessSignature/ProcessorFlags"), L"UnsupportedProcessSignature/ProcessorFlags");\r |
88266859 JY |
534 | }\r |
535 | return EFI_UNSUPPORTED;\r | |
536 | }\r | |
537 | }\r | |
538 | \r | |
539 | //\r | |
540 | // Check UpdateRevision\r | |
541 | //\r | |
31d060d9 JY |
542 | CurrentRevision = ProcessorInfo->MicrocodeRevision;\r |
543 | if ((MicrocodeEntryPoint->UpdateRevision < CurrentRevision) ||\r | |
544 | (TryLoad && (MicrocodeEntryPoint->UpdateRevision == CurrentRevision))) {\r | |
2ed65824 JY |
545 | if (TryLoad) {\r |
546 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on UpdateRevision\n"));\r | |
547 | }\r | |
88266859 JY |
548 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;\r |
549 | if (AbortReason != NULL) {\r | |
550 | *AbortReason = AllocateCopyPool(sizeof(L"IncorrectRevision"), L"IncorrectRevision");\r | |
551 | }\r | |
552 | return EFI_INCOMPATIBLE_VERSION;\r | |
553 | }\r | |
554 | \r | |
555 | //\r | |
556 | // try load MCU\r | |
557 | //\r | |
558 | if (TryLoad) {\r | |
31d060d9 | 559 | CurrentRevision = LoadMicrocodeOnThis(MicrocodeFmpPrivate, ProcessorInfo->CpuIndex, (UINTN)MicrocodeEntryPoint + sizeof(CPU_MICROCODE_HEADER));\r |
88266859 JY |
560 | if (MicrocodeEntryPoint->UpdateRevision != CurrentRevision) {\r |
561 | DEBUG((DEBUG_ERROR, "VerifyMicrocode - fail on LoadMicrocode\n"));\r | |
562 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;\r | |
563 | if (AbortReason != NULL) {\r | |
564 | *AbortReason = AllocateCopyPool(sizeof(L"InvalidData"), L"InvalidData");\r | |
565 | }\r | |
566 | return EFI_SECURITY_VIOLATION;\r | |
567 | }\r | |
568 | }\r | |
569 | \r | |
570 | return EFI_SUCCESS;\r | |
571 | }\r | |
572 | \r | |
2ed65824 JY |
573 | /**\r |
574 | Get next Microcode entrypoint.\r | |
88266859 | 575 | \r |
2ed65824 JY |
576 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r |
577 | @param[in] MicrocodeEntryPoint Current Microcode entrypoint\r | |
578 | \r | |
579 | @return next Microcode entrypoint.\r | |
580 | **/\r | |
581 | CPU_MICROCODE_HEADER *\r | |
582 | GetNextMicrocode (\r | |
583 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r | |
584 | IN CPU_MICROCODE_HEADER *MicrocodeEntryPoint\r | |
585 | )\r | |
586 | {\r | |
587 | UINTN Index;\r | |
588 | \r | |
589 | for (Index = 0; Index < MicrocodeFmpPrivate->DescriptorCount; Index++) {\r | |
590 | if (MicrocodeEntryPoint == MicrocodeFmpPrivate->MicrocodeInfo[Index].MicrocodeEntryPoint) {\r | |
591 | if (Index == (UINTN)MicrocodeFmpPrivate->DescriptorCount - 1) {\r | |
592 | // it is last one\r | |
593 | return NULL;\r | |
88266859 | 594 | } else {\r |
2ed65824 JY |
595 | // return next one\r |
596 | return MicrocodeFmpPrivate->MicrocodeInfo[Index + 1].MicrocodeEntryPoint;\r | |
88266859 | 597 | }\r |
88266859 | 598 | }\r |
2ed65824 | 599 | }\r |
88266859 | 600 | \r |
2ed65824 | 601 | ASSERT(FALSE);\r |
88266859 JY |
602 | return NULL;\r |
603 | }\r | |
604 | \r | |
605 | /**\r | |
606 | Get current Microcode used region size.\r | |
607 | \r | |
2ed65824 JY |
608 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r |
609 | \r | |
88266859 JY |
610 | @return current Microcode used region size.\r |
611 | **/\r | |
612 | UINTN\r | |
613 | GetCurrentMicrocodeUsedRegionSize (\r | |
2ed65824 | 614 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate\r |
88266859 JY |
615 | )\r |
616 | {\r | |
2ed65824 | 617 | if (MicrocodeFmpPrivate->DescriptorCount == 0) {\r |
88266859 JY |
618 | return 0;\r |
619 | }\r | |
88266859 | 620 | \r |
2ed65824 JY |
621 | return (UINTN)MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeFmpPrivate->DescriptorCount - 1].MicrocodeEntryPoint\r |
622 | + (UINTN)MicrocodeFmpPrivate->MicrocodeInfo[MicrocodeFmpPrivate->DescriptorCount - 1].TotalSize\r | |
623 | - (UINTN)MicrocodeFmpPrivate->MicrocodePatchAddress;\r | |
88266859 JY |
624 | }\r |
625 | \r | |
626 | /**\r | |
627 | Update Microcode.\r | |
628 | \r | |
629 | @param[in] Address The flash address of Microcode.\r | |
630 | @param[in] Image The Microcode image buffer.\r | |
631 | @param[in] ImageSize The size of Microcode image buffer in bytes.\r | |
632 | @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r | |
633 | \r | |
634 | @retval EFI_SUCCESS The Microcode image is updated.\r | |
635 | @retval EFI_WRITE_PROTECTED The flash device is read only.\r | |
636 | **/\r | |
637 | EFI_STATUS\r | |
638 | UpdateMicrocode (\r | |
639 | IN UINT64 Address,\r | |
640 | IN VOID *Image,\r | |
641 | IN UINTN ImageSize,\r | |
642 | OUT UINT32 *LastAttemptStatus\r | |
643 | )\r | |
644 | {\r | |
645 | EFI_STATUS Status;\r | |
646 | \r | |
647 | DEBUG((DEBUG_INFO, "PlatformUpdate:"));\r | |
648 | DEBUG((DEBUG_INFO, " Address - 0x%lx,", Address));\r | |
649 | DEBUG((DEBUG_INFO, " Legnth - 0x%x\n", ImageSize));\r | |
650 | \r | |
651 | Status = MicrocodeFlashWrite (\r | |
652 | Address,\r | |
653 | Image,\r | |
654 | ImageSize\r | |
655 | );\r | |
656 | if (!EFI_ERROR(Status)) {\r | |
657 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;\r | |
658 | } else {\r | |
659 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;\r | |
660 | }\r | |
661 | return Status;\r | |
662 | }\r | |
663 | \r | |
664 | /**\r | |
2ed65824 | 665 | Update Microcode flash region.\r |
88266859 | 666 | \r |
2ed65824 | 667 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r |
31d060d9 | 668 | @param[in] TargetMicrocodeEntryPoint Target Microcode entrypoint to be updated\r |
2ed65824 JY |
669 | @param[in] Image The Microcode image buffer.\r |
670 | @param[in] ImageSize The size of Microcode image buffer in bytes.\r | |
671 | @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r | |
88266859 | 672 | \r |
2ed65824 JY |
673 | @retval EFI_SUCCESS The Microcode image is written.\r |
674 | @retval EFI_WRITE_PROTECTED The flash device is read only.\r | |
88266859 JY |
675 | **/\r |
676 | EFI_STATUS\r | |
2ed65824 JY |
677 | UpdateMicrocodeFlashRegion (\r |
678 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r | |
31d060d9 | 679 | IN CPU_MICROCODE_HEADER *TargetMicrocodeEntryPoint,\r |
2ed65824 JY |
680 | IN VOID *Image,\r |
681 | IN UINTN ImageSize,\r | |
682 | OUT UINT32 *LastAttemptStatus\r | |
88266859 JY |
683 | )\r |
684 | {\r | |
2ed65824 JY |
685 | VOID *MicrocodePatchAddress;\r |
686 | UINTN MicrocodePatchRegionSize;\r | |
31d060d9 | 687 | UINTN TargetTotalSize;\r |
88266859 | 688 | UINTN UsedRegionSize;\r |
2ed65824 JY |
689 | EFI_STATUS Status;\r |
690 | VOID *MicrocodePatchScratchBuffer;\r | |
691 | UINT8 *ScratchBufferPtr;\r | |
692 | UINTN ScratchBufferSize;\r | |
693 | UINTN RestSize;\r | |
694 | UINTN AvailableSize;\r | |
695 | VOID *NextMicrocodeEntryPoint;\r | |
696 | MICROCODE_INFO *MicrocodeInfo;\r | |
697 | UINTN MicrocodeCount;\r | |
698 | UINTN Index;\r | |
699 | \r | |
700 | DEBUG((DEBUG_INFO, "UpdateMicrocodeFlashRegion: Image - 0x%x, size - 0x%x\n", Image, ImageSize));\r | |
88266859 | 701 | \r |
2ed65824 JY |
702 | MicrocodePatchAddress = MicrocodeFmpPrivate->MicrocodePatchAddress;\r |
703 | MicrocodePatchRegionSize = MicrocodeFmpPrivate->MicrocodePatchRegionSize;\r | |
704 | \r | |
705 | MicrocodePatchScratchBuffer = AllocateZeroPool (MicrocodePatchRegionSize);\r | |
706 | if (MicrocodePatchScratchBuffer == NULL) {\r | |
707 | DEBUG((DEBUG_ERROR, "Fail to allocate Microcode Scratch buffer\n"));\r | |
708 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;\r | |
709 | return EFI_OUT_OF_RESOURCES;\r | |
88266859 | 710 | }\r |
2ed65824 JY |
711 | ScratchBufferPtr = MicrocodePatchScratchBuffer;\r |
712 | ScratchBufferSize = 0;\r | |
88266859 | 713 | \r |
2ed65824 | 714 | //\r |
31d060d9 | 715 | // Target data collection\r |
2ed65824 | 716 | //\r |
31d060d9 | 717 | TargetTotalSize = 0;\r |
2ed65824 JY |
718 | AvailableSize = 0;\r |
719 | NextMicrocodeEntryPoint = NULL;\r | |
31d060d9 JY |
720 | if (TargetMicrocodeEntryPoint != NULL) {\r |
721 | if (TargetMicrocodeEntryPoint->DataSize == 0) {\r | |
722 | TargetTotalSize = 2048;\r | |
88266859 | 723 | } else {\r |
31d060d9 | 724 | TargetTotalSize = TargetMicrocodeEntryPoint->TotalSize;\r |
88266859 | 725 | }\r |
31d060d9 JY |
726 | DEBUG((DEBUG_INFO, " TargetTotalSize - 0x%x\n", TargetTotalSize));\r |
727 | NextMicrocodeEntryPoint = GetNextMicrocode(MicrocodeFmpPrivate, TargetMicrocodeEntryPoint);\r | |
2ed65824 JY |
728 | DEBUG((DEBUG_INFO, " NextMicrocodeEntryPoint - 0x%x\n", NextMicrocodeEntryPoint));\r |
729 | if (NextMicrocodeEntryPoint != NULL) {\r | |
31d060d9 JY |
730 | ASSERT ((UINTN)NextMicrocodeEntryPoint >= ((UINTN)TargetMicrocodeEntryPoint + TargetTotalSize));\r |
731 | AvailableSize = (UINTN)NextMicrocodeEntryPoint - (UINTN)TargetMicrocodeEntryPoint;\r | |
2ed65824 | 732 | } else {\r |
31d060d9 | 733 | AvailableSize = (UINTN)MicrocodePatchAddress + MicrocodePatchRegionSize - (UINTN)TargetMicrocodeEntryPoint;\r |
2ed65824 JY |
734 | }\r |
735 | DEBUG((DEBUG_INFO, " AvailableSize - 0x%x\n", AvailableSize));\r | |
736 | }\r | |
31d060d9 | 737 | ASSERT (AvailableSize >= TargetTotalSize);\r |
2ed65824 JY |
738 | UsedRegionSize = GetCurrentMicrocodeUsedRegionSize(MicrocodeFmpPrivate);\r |
739 | DEBUG((DEBUG_INFO, " UsedRegionSize - 0x%x\n", UsedRegionSize));\r | |
31d060d9 JY |
740 | ASSERT (UsedRegionSize >= TargetTotalSize);\r |
741 | if (TargetMicrocodeEntryPoint != NULL) {\r | |
742 | ASSERT ((UINTN)MicrocodePatchAddress + UsedRegionSize >= ((UINTN)TargetMicrocodeEntryPoint + TargetTotalSize));\r | |
2ed65824 JY |
743 | }\r |
744 | //\r | |
745 | // Total Size means the Microcode data size.\r | |
31d060d9 | 746 | // Available Size means the Microcode data size plus the pad till (1) next Microcode or (2) the end.\r |
2ed65824 JY |
747 | //\r |
748 | // (1)\r | |
749 | // +------+-----------+-----+------+===================+\r | |
750 | // | MCU1 | Microcode | PAD | MCU2 | Empty |\r | |
751 | // +------+-----------+-----+------+===================+\r | |
752 | // | TotalSize |\r | |
753 | // |<-AvailableSize->|\r | |
754 | // |<- UsedRegionSize ->|\r | |
755 | //\r | |
756 | // (2)\r | |
757 | // +------+-----------+===================+\r | |
758 | // | MCU | Microcode | Empty |\r | |
759 | // +------+-----------+===================+\r | |
760 | // | TotalSize |\r | |
761 | // |<- AvailableSize ->|\r | |
762 | // |<-UsedRegionSize->|\r | |
763 | //\r | |
764 | \r | |
765 | //\r | |
766 | // Update based on policy\r | |
767 | //\r | |
768 | \r | |
769 | //\r | |
770 | // 1. If there is enough space to update old one in situ, replace old microcode in situ.\r | |
771 | //\r | |
772 | if (AvailableSize >= ImageSize) {\r | |
773 | DEBUG((DEBUG_INFO, "Replace old microcode in situ\n"));\r | |
774 | //\r | |
775 | // +------+------------+------+===================+\r | |
776 | // |Other1| Old Image |Other2| Empty |\r | |
777 | // +------+------------+------+===================+\r | |
778 | //\r | |
779 | // +------+---------+--+------+===================+\r | |
780 | // |Other1|New Image|FF|Other2| Empty |\r | |
781 | // +------+---------+--+------+===================+\r | |
782 | //\r | |
783 | // 1.1. Copy new image\r | |
784 | CopyMem (ScratchBufferPtr, Image, ImageSize);\r | |
785 | ScratchBufferSize += ImageSize;\r | |
31d060d9 | 786 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 JY |
787 | // 1.2. Pad 0xFF\r |
788 | RestSize = AvailableSize - ImageSize;\r | |
789 | if (RestSize > 0) {\r | |
790 | SetMem (ScratchBufferPtr, RestSize, 0xFF);\r | |
791 | ScratchBufferSize += RestSize;\r | |
31d060d9 | 792 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 | 793 | }\r |
31d060d9 | 794 | Status = UpdateMicrocode((UINTN)TargetMicrocodeEntryPoint, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);\r |
2ed65824 | 795 | return Status;\r |
88266859 JY |
796 | }\r |
797 | \r | |
2ed65824 JY |
798 | //\r |
799 | // 2. If there is enough space to remove old one and add new one, reorg and replace old microcode.\r | |
800 | //\r | |
31d060d9 JY |
801 | if (MicrocodePatchRegionSize - (UsedRegionSize - TargetTotalSize) >= ImageSize) {\r |
802 | if (TargetMicrocodeEntryPoint == NULL) {\r | |
2ed65824 JY |
803 | DEBUG((DEBUG_INFO, "Append new microcode\n"));\r |
804 | //\r | |
805 | // +------+------------+------+===================+\r | |
806 | // |Other1| Other |Other2| Empty |\r | |
807 | // +------+------------+------+===================+\r | |
808 | //\r | |
809 | // +------+------------+------+-----------+=======+\r | |
810 | // |Other1| Other |Other2| New Image | Empty |\r | |
811 | // +------+------------+------+-----------+=======+\r | |
812 | //\r | |
813 | Status = UpdateMicrocode((UINTN)MicrocodePatchAddress + UsedRegionSize, Image, ImageSize, LastAttemptStatus);\r | |
814 | } else {\r | |
815 | DEBUG((DEBUG_INFO, "Reorg and replace old microcode\n"));\r | |
816 | //\r | |
817 | // +------+------------+------+===================+\r | |
818 | // |Other1| Old Image |Other2| Empty |\r | |
819 | // +------+------------+------+===================+\r | |
820 | //\r | |
821 | // +------+---------------+------+================+\r | |
822 | // |Other1| New Image |Other2| Empty |\r | |
823 | // +------+---------------+------+================+\r | |
824 | //\r | |
825 | // 2.1. Copy new image\r | |
31d060d9 | 826 | CopyMem (ScratchBufferPtr, Image, ImageSize);\r |
2ed65824 | 827 | ScratchBufferSize += ImageSize;\r |
31d060d9 | 828 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 JY |
829 | // 2.2. Copy rest images after the old image.\r |
830 | if (NextMicrocodeEntryPoint != 0) {\r | |
831 | RestSize = (UINTN)MicrocodePatchAddress + UsedRegionSize - ((UINTN)NextMicrocodeEntryPoint);\r | |
31d060d9 | 832 | CopyMem (ScratchBufferPtr, (UINT8 *)TargetMicrocodeEntryPoint + TargetTotalSize, RestSize);\r |
2ed65824 | 833 | ScratchBufferSize += RestSize;\r |
31d060d9 | 834 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 | 835 | }\r |
31d060d9 | 836 | Status = UpdateMicrocode((UINTN)TargetMicrocodeEntryPoint, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);\r |
2ed65824 JY |
837 | }\r |
838 | return Status;\r | |
839 | }\r | |
840 | \r | |
841 | //\r | |
842 | // 3. The new image can be put in MCU region, but not all others can be put.\r | |
843 | // So all the unused MCU is removed.\r | |
844 | //\r | |
845 | if (MicrocodePatchRegionSize >= ImageSize) {\r | |
846 | //\r | |
847 | // +------+------------+------+===================+\r | |
848 | // |Other1| Old Image |Other2| Empty |\r | |
849 | // +------+------------+------+===================+\r | |
850 | //\r | |
851 | // +-------------------------------------+--------+\r | |
852 | // | New Image | Other |\r | |
853 | // +-------------------------------------+--------+\r | |
854 | //\r | |
855 | DEBUG((DEBUG_INFO, "Add new microcode from beginning\n"));\r | |
856 | \r | |
857 | MicrocodeCount = MicrocodeFmpPrivate->DescriptorCount;\r | |
858 | MicrocodeInfo = MicrocodeFmpPrivate->MicrocodeInfo;\r | |
859 | \r | |
860 | // 3.1. Copy new image\r | |
31d060d9 | 861 | CopyMem (ScratchBufferPtr, Image, ImageSize);\r |
2ed65824 | 862 | ScratchBufferSize += ImageSize;\r |
31d060d9 | 863 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 JY |
864 | // 3.2. Copy some others to rest buffer\r |
865 | for (Index = 0; Index < MicrocodeCount; Index++) {\r | |
866 | if (!MicrocodeInfo[Index].InUse) {\r | |
867 | continue;\r | |
868 | }\r | |
31d060d9 | 869 | if (MicrocodeInfo[Index].MicrocodeEntryPoint == TargetMicrocodeEntryPoint) {\r |
2ed65824 JY |
870 | continue;\r |
871 | }\r | |
872 | if (MicrocodeInfo[Index].TotalSize <= MicrocodePatchRegionSize - ScratchBufferSize) {\r | |
873 | CopyMem (ScratchBufferPtr, MicrocodeInfo[Index].MicrocodeEntryPoint, MicrocodeInfo[Index].TotalSize);\r | |
874 | ScratchBufferSize += MicrocodeInfo[Index].TotalSize;\r | |
31d060d9 | 875 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 JY |
876 | }\r |
877 | }\r | |
878 | // 3.3. Pad 0xFF\r | |
879 | RestSize = MicrocodePatchRegionSize - ScratchBufferSize;\r | |
880 | if (RestSize > 0) {\r | |
881 | SetMem (ScratchBufferPtr, RestSize, 0xFF);\r | |
882 | ScratchBufferSize += RestSize;\r | |
31d060d9 | 883 | ScratchBufferPtr = (UINT8 *)MicrocodePatchScratchBuffer + ScratchBufferSize;\r |
2ed65824 JY |
884 | }\r |
885 | Status = UpdateMicrocode((UINTN)MicrocodePatchAddress, MicrocodePatchScratchBuffer, ScratchBufferSize, LastAttemptStatus);\r | |
886 | return Status;\r | |
887 | }\r | |
888 | \r | |
889 | //\r | |
890 | // 4. The new image size is bigger than the whole MCU region.\r | |
891 | //\r | |
892 | DEBUG((DEBUG_ERROR, "Microcode too big\n"));\r | |
893 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;\r | |
894 | Status = EFI_OUT_OF_RESOURCES;\r | |
895 | \r | |
896 | return Status;\r | |
897 | }\r | |
898 | \r | |
899 | /**\r | |
900 | Write Microcode.\r | |
901 | \r | |
902 | Caution: This function may receive untrusted input.\r | |
903 | \r | |
904 | @param[in] MicrocodeFmpPrivate The Microcode driver private data\r | |
905 | @param[in] Image The Microcode image buffer.\r | |
906 | @param[in] ImageSize The size of Microcode image buffer in bytes.\r | |
907 | @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r | |
908 | @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.\r | |
909 | @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r | |
910 | details for the aborted operation. The buffer is allocated by this function\r | |
911 | with AllocatePool(), and it is the caller's responsibility to free it with a\r | |
912 | call to FreePool().\r | |
913 | \r | |
914 | @retval EFI_SUCCESS The Microcode image is written.\r | |
915 | @retval EFI_VOLUME_CORRUPTED The Microcode image is corrupt.\r | |
916 | @retval EFI_INCOMPATIBLE_VERSION The Microcode image version is incorrect.\r | |
917 | @retval EFI_SECURITY_VIOLATION The Microcode image fails to load.\r | |
918 | @retval EFI_WRITE_PROTECTED The flash device is read only.\r | |
919 | **/\r | |
920 | EFI_STATUS\r | |
921 | MicrocodeWrite (\r | |
922 | IN MICROCODE_FMP_PRIVATE_DATA *MicrocodeFmpPrivate,\r | |
923 | IN VOID *Image,\r | |
924 | IN UINTN ImageSize,\r | |
925 | OUT UINT32 *LastAttemptVersion,\r | |
926 | OUT UINT32 *LastAttemptStatus,\r | |
927 | OUT CHAR16 **AbortReason\r | |
928 | )\r | |
929 | {\r | |
930 | EFI_STATUS Status;\r | |
931 | VOID *AlignedImage;\r | |
31d060d9 JY |
932 | CPU_MICROCODE_HEADER *TargetMicrocodeEntryPoint;\r |
933 | UINTN TargetCpuIndex;\r | |
934 | UINTN TargetMicrcodeIndex;\r | |
2ed65824 | 935 | \r |
88266859 JY |
936 | //\r |
937 | // MCU must be 16 bytes aligned\r | |
938 | //\r | |
939 | AlignedImage = AllocateCopyPool(ImageSize, Image);\r | |
940 | if (AlignedImage == NULL) {\r | |
941 | DEBUG((DEBUG_ERROR, "Fail to allocate aligned image\n"));\r | |
942 | *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;\r | |
943 | return EFI_OUT_OF_RESOURCES;\r | |
944 | }\r | |
945 | \r | |
946 | *LastAttemptVersion = ((CPU_MICROCODE_HEADER *)Image)->UpdateRevision;\r | |
31d060d9 JY |
947 | TargetCpuIndex = (UINTN)-1;\r |
948 | Status = VerifyMicrocode(MicrocodeFmpPrivate, AlignedImage, ImageSize, TRUE, LastAttemptStatus, AbortReason, &TargetCpuIndex);\r | |
88266859 JY |
949 | if (EFI_ERROR(Status)) {\r |
950 | DEBUG((DEBUG_ERROR, "Fail to verify Microcode Region\n"));\r | |
951 | FreePool(AlignedImage);\r | |
952 | return Status;\r | |
953 | }\r | |
954 | DEBUG((DEBUG_INFO, "Pass VerifyMicrocode\n"));\r | |
955 | \r | |
31d060d9 JY |
956 | DEBUG((DEBUG_INFO, " TargetCpuIndex - 0x%x\n", TargetCpuIndex));\r |
957 | ASSERT (TargetCpuIndex < MicrocodeFmpPrivate->ProcessorCount);\r | |
958 | TargetMicrcodeIndex = MicrocodeFmpPrivate->ProcessorInfo[TargetCpuIndex].MicrocodeIndex;\r | |
959 | DEBUG((DEBUG_INFO, " TargetMicrcodeIndex - 0x%x\n", TargetMicrcodeIndex));\r | |
960 | if (TargetMicrcodeIndex != (UINTN)-1) {\r | |
961 | ASSERT (TargetMicrcodeIndex < MicrocodeFmpPrivate->DescriptorCount);\r | |
962 | TargetMicrocodeEntryPoint = MicrocodeFmpPrivate->MicrocodeInfo[TargetMicrcodeIndex].MicrocodeEntryPoint;\r | |
963 | } else {\r | |
964 | TargetMicrocodeEntryPoint = NULL;\r | |
965 | }\r | |
966 | DEBUG((DEBUG_INFO, " TargetMicrocodeEntryPoint - 0x%x\n", TargetMicrocodeEntryPoint));\r | |
967 | \r | |
2ed65824 JY |
968 | Status = UpdateMicrocodeFlashRegion(\r |
969 | MicrocodeFmpPrivate,\r | |
31d060d9 | 970 | TargetMicrocodeEntryPoint,\r |
2ed65824 JY |
971 | AlignedImage,\r |
972 | ImageSize,\r | |
973 | LastAttemptStatus\r | |
974 | );\r | |
88266859 JY |
975 | \r |
976 | FreePool(AlignedImage);\r | |
977 | \r | |
978 | return Status;\r | |
979 | }\r | |
980 | \r | |
981 | \r |