public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
@ 2019-02-18  5:53 Chen A Chen
  2019-02-18  5:58 ` Yao, Jiewen
  2019-02-19  1:05 ` Zhang, Chao B
  0 siblings, 2 replies; 6+ messages in thread
From: Chen A Chen @ 2019-02-18  5:53 UTC (permalink / raw)
  To: edk2-devel; +Cc: Chen A Chen, Ray Ni, Eric Dong

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1020

The following Microcode payload format is define in SDM spec.
Payload: |MicrocodeHeader|MicrocodeBinary|ExtendedHeader|ExtendedTable|.
When we verify the CheckSum32 with ExtendedTable, we should use the fields
of ExtendedTable to replace corresponding fields in MicrocodeHeader,
and then calculate the CheckSum32 with MicrocodeHeader+MicrocodeBinary.
This patch already verified on ICL platform.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 UefiCpuPkg/Library/MpInitLib/Microcode.c | 38 ++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/Microcode.c b/UefiCpuPkg/Library/MpInitLib/Microcode.c
index d84344c6f5..38880cdbec 100644
--- a/UefiCpuPkg/Library/MpInitLib/Microcode.c
+++ b/UefiCpuPkg/Library/MpInitLib/Microcode.c
@@ -57,6 +57,7 @@ MicrocodeDetect (
   UINT32                                  LatestRevision;
   UINTN                                   TotalSize;
   UINT32                                  CheckSum32;
+  UINT32                                  InCompleteCheckSum32;
   BOOLEAN                                 CorrectMicrocode;
   VOID                                    *MicrocodeData;
   MSR_IA32_PLATFORM_ID_REGISTER           PlatformIdMsr;
@@ -121,6 +122,26 @@ MicrocodeDetect (
   MicrocodeData  = NULL;
   MicrocodeEnd = (UINTN) (CpuMpData->MicrocodePatchAddress + CpuMpData->MicrocodePatchRegionSize);
   MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN) CpuMpData->MicrocodePatchAddress;
+
+  //
+  // To avoid double calculate checksum32 value.
+  // Save the CheckSum32 of the common parts in advance.
+  //
+  if (MicrocodeEntryPoint->DataSize == 0) {
+    InCompleteCheckSum32 = CalculateSum32 (
+                             (UINT32 *) MicrocodeEntryPoint,
+                             sizeof (CPU_MICROCODE_HEADER) + 2000
+                             );
+  } else {
+    InCompleteCheckSum32 = CalculateSum32 (
+                             (UINT32 *) MicrocodeEntryPoint,
+                             sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize
+                             );
+  }
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorSignature.Uint32;
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
+
   do {
     //
     // Check if the microcode is for the Cpu and the version is newer
@@ -137,14 +158,10 @@ MicrocodeDetect (
           MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
           (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
           ) {
-        if (MicrocodeEntryPoint->DataSize == 0) {
-          CheckSum32 = CalculateSum32 ((UINT32 *) MicrocodeEntryPoint, 2048);
-        } else {
-          CheckSum32 = CalculateSum32 (
-                         (UINT32 *) MicrocodeEntryPoint,
-                         MicrocodeEntryPoint->DataSize + sizeof (CPU_MICROCODE_HEADER)
-                         );
-        }
+        CheckSum32 = InCompleteCheckSum32;
+        CheckSum32 += MicrocodeEntryPoint->ProcessorSignature.Uint32;
+        CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
+        CheckSum32 += MicrocodeEntryPoint->Checksum;
         if (CheckSum32 == 0) {
           CorrectMicrocode = TRUE;
           ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
@@ -171,7 +188,10 @@ MicrocodeDetect (
               ExtendedTableCount = ExtendedTableHeader->ExtendedSignatureCount;
               ExtendedTable      = (CPU_MICROCODE_EXTENDED_TABLE *) (ExtendedTableHeader + 1);
               for (Index = 0; Index < ExtendedTableCount; Index ++) {
-                CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTable, sizeof(CPU_MICROCODE_EXTENDED_TABLE));
+                CheckSum32 = InCompleteCheckSum32;
+                CheckSum32 += ExtendedTable->ProcessorSignature.Uint32;
+                CheckSum32 += ExtendedTable->ProcessorFlag;
+                CheckSum32 += ExtendedTable->Checksum;
                 if (CheckSum32 == 0) {
                   //
                   // Verify Header
-- 
2.16.2.windows.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
  2019-02-18  5:53 [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table Chen A Chen
@ 2019-02-18  5:58 ` Yao, Jiewen
  2019-02-19  1:05 ` Zhang, Chao B
  1 sibling, 0 replies; 6+ messages in thread
From: Yao, Jiewen @ 2019-02-18  5:58 UTC (permalink / raw)
  To: Chen, Chen A, edk2-devel@lists.01.org; +Cc: Dong, Eric

Hi ChenChen
Thanks!

Do you think we also need fix IntelSiliconPkg\Feature\Capsule\MicrocodeUpdateDxe?

Thank you
Yao Jiewen


> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Chen A Chen
> Sent: Monday, February 18, 2019 1:54 PM
> To: edk2-devel@lists.01.org
> Cc: Dong, Eric <eric.dong@intel.com>
> Subject: [edk2] [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum
> issue for extended table
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1020
> 
> The following Microcode payload format is define in SDM spec.
> Payload:
> |MicrocodeHeader|MicrocodeBinary|ExtendedHeader|ExtendedTable|.
> When we verify the CheckSum32 with ExtendedTable, we should use the
> fields
> of ExtendedTable to replace corresponding fields in MicrocodeHeader,
> and then calculate the CheckSum32 with
> MicrocodeHeader+MicrocodeBinary.
> This patch already verified on ICL platform.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  UefiCpuPkg/Library/MpInitLib/Microcode.c | 38
> ++++++++++++++++++++++++--------
>  1 file changed, 29 insertions(+), 9 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/MpInitLib/Microcode.c
> b/UefiCpuPkg/Library/MpInitLib/Microcode.c
> index d84344c6f5..38880cdbec 100644
> --- a/UefiCpuPkg/Library/MpInitLib/Microcode.c
> +++ b/UefiCpuPkg/Library/MpInitLib/Microcode.c
> @@ -57,6 +57,7 @@ MicrocodeDetect (
>    UINT32                                  LatestRevision;
>    UINTN                                   TotalSize;
>    UINT32                                  CheckSum32;
> +  UINT32
> InCompleteCheckSum32;
>    BOOLEAN                                 CorrectMicrocode;
>    VOID                                    *MicrocodeData;
>    MSR_IA32_PLATFORM_ID_REGISTER           PlatformIdMsr;
> @@ -121,6 +122,26 @@ MicrocodeDetect (
>    MicrocodeData  = NULL;
>    MicrocodeEnd = (UINTN) (CpuMpData->MicrocodePatchAddress +
> CpuMpData->MicrocodePatchRegionSize);
>    MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN)
> CpuMpData->MicrocodePatchAddress;
> +
> +  //
> +  // To avoid double calculate checksum32 value.
> +  // Save the CheckSum32 of the common parts in advance.
> +  //
> +  if (MicrocodeEntryPoint->DataSize == 0) {
> +    InCompleteCheckSum32 = CalculateSum32 (
> +                             (UINT32 *) MicrocodeEntryPoint,
> +                             sizeof (CPU_MICROCODE_HEADER) +
> 2000
> +                             );
> +  } else {
> +    InCompleteCheckSum32 = CalculateSum32 (
> +                             (UINT32 *) MicrocodeEntryPoint,
> +                             sizeof (CPU_MICROCODE_HEADER) +
> MicrocodeEntryPoint->DataSize
> +                             );
> +  }
> +  InCompleteCheckSum32 -=
> MicrocodeEntryPoint->ProcessorSignature.Uint32;
> +  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
> +  InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
> +
>    do {
>      //
>      // Check if the microcode is for the Cpu and the version is newer
> @@ -137,14 +158,10 @@ MicrocodeDetect (
>            MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
>            (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
>            ) {
> -        if (MicrocodeEntryPoint->DataSize == 0) {
> -          CheckSum32 = CalculateSum32 ((UINT32 *)
> MicrocodeEntryPoint, 2048);
> -        } else {
> -          CheckSum32 = CalculateSum32 (
> -                         (UINT32 *) MicrocodeEntryPoint,
> -                         MicrocodeEntryPoint->DataSize + sizeof
> (CPU_MICROCODE_HEADER)
> -                         );
> -        }
> +        CheckSum32 = InCompleteCheckSum32;
> +        CheckSum32 +=
> MicrocodeEntryPoint->ProcessorSignature.Uint32;
> +        CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
> +        CheckSum32 += MicrocodeEntryPoint->Checksum;
>          if (CheckSum32 == 0) {
>            CorrectMicrocode = TRUE;
>            ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
> @@ -171,7 +188,10 @@ MicrocodeDetect (
>                ExtendedTableCount =
> ExtendedTableHeader->ExtendedSignatureCount;
>                ExtendedTable      =
> (CPU_MICROCODE_EXTENDED_TABLE *) (ExtendedTableHeader + 1);
>                for (Index = 0; Index < ExtendedTableCount; Index ++) {
> -                CheckSum32 = CalculateSum32 ((UINT32 *)
> ExtendedTable, sizeof(CPU_MICROCODE_EXTENDED_TABLE));
> +                CheckSum32 = InCompleteCheckSum32;
> +                CheckSum32 +=
> ExtendedTable->ProcessorSignature.Uint32;
> +                CheckSum32 += ExtendedTable->ProcessorFlag;
> +                CheckSum32 += ExtendedTable->Checksum;
>                  if (CheckSum32 == 0) {
>                    //
>                    // Verify Header
> --
> 2.16.2.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
  2019-02-18  5:53 [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table Chen A Chen
  2019-02-18  5:58 ` Yao, Jiewen
@ 2019-02-19  1:05 ` Zhang, Chao B
  2019-02-19  2:37   ` Ni, Ray
  1 sibling, 1 reply; 6+ messages in thread
From: Zhang, Chao B @ 2019-02-19  1:05 UTC (permalink / raw)
  To: Chen, Chen A, edk2-devel@lists.01.org; +Cc: Dong, Eric

Chen Chen:
   I think you can add uCode format info into comments. Also please highlight in comment
Which part is header checksum calculation, which part is for extended header

-----Original Message-----
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Chen A Chen
Sent: Monday, February 18, 2019 1:54 PM
To: edk2-devel@lists.01.org
Cc: Dong, Eric <eric.dong@intel.com>
Subject: [edk2] [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1020

The following Microcode payload format is define in SDM spec.
Payload: |MicrocodeHeader|MicrocodeBinary|ExtendedHeader|ExtendedTable|.
When we verify the CheckSum32 with ExtendedTable, we should use the fields of ExtendedTable to replace corresponding fields in MicrocodeHeader, and then calculate the CheckSum32 with MicrocodeHeader+MicrocodeBinary.
This patch already verified on ICL platform.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
---
 UefiCpuPkg/Library/MpInitLib/Microcode.c | 38 ++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/Microcode.c b/UefiCpuPkg/Library/MpInitLib/Microcode.c
index d84344c6f5..38880cdbec 100644
--- a/UefiCpuPkg/Library/MpInitLib/Microcode.c
+++ b/UefiCpuPkg/Library/MpInitLib/Microcode.c
@@ -57,6 +57,7 @@ MicrocodeDetect (
   UINT32                                  LatestRevision;
   UINTN                                   TotalSize;
   UINT32                                  CheckSum32;
+  UINT32                                  InCompleteCheckSum32;
   BOOLEAN                                 CorrectMicrocode;
   VOID                                    *MicrocodeData;
   MSR_IA32_PLATFORM_ID_REGISTER           PlatformIdMsr;
@@ -121,6 +122,26 @@ MicrocodeDetect (
   MicrocodeData  = NULL;
   MicrocodeEnd = (UINTN) (CpuMpData->MicrocodePatchAddress + CpuMpData->MicrocodePatchRegionSize);
   MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN) CpuMpData->MicrocodePatchAddress;
+
+  //
+  // To avoid double calculate checksum32 value.
+  // Save the CheckSum32 of the common parts in advance.
+  //
+  if (MicrocodeEntryPoint->DataSize == 0) {
+    InCompleteCheckSum32 = CalculateSum32 (
+                             (UINT32 *) MicrocodeEntryPoint,
+                             sizeof (CPU_MICROCODE_HEADER) + 2000
+                             );
+  } else {
+    InCompleteCheckSum32 = CalculateSum32 (
+                             (UINT32 *) MicrocodeEntryPoint,
+                             sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize
+                             );
+  }
+  InCompleteCheckSum32 -= 
+ MicrocodeEntryPoint->ProcessorSignature.Uint32;
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
+
   do {
     //
     // Check if the microcode is for the Cpu and the version is newer @@ -137,14 +158,10 @@ MicrocodeDetect (
           MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
           (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
           ) {
-        if (MicrocodeEntryPoint->DataSize == 0) {
-          CheckSum32 = CalculateSum32 ((UINT32 *) MicrocodeEntryPoint, 2048);
-        } else {
-          CheckSum32 = CalculateSum32 (
-                         (UINT32 *) MicrocodeEntryPoint,
-                         MicrocodeEntryPoint->DataSize + sizeof (CPU_MICROCODE_HEADER)
-                         );
-        }
+        CheckSum32 = InCompleteCheckSum32;
+        CheckSum32 += MicrocodeEntryPoint->ProcessorSignature.Uint32;
+        CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
+        CheckSum32 += MicrocodeEntryPoint->Checksum;
         if (CheckSum32 == 0) {
           CorrectMicrocode = TRUE;
           ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
@@ -171,7 +188,10 @@ MicrocodeDetect (
               ExtendedTableCount = ExtendedTableHeader->ExtendedSignatureCount;
               ExtendedTable      = (CPU_MICROCODE_EXTENDED_TABLE *) (ExtendedTableHeader + 1);
               for (Index = 0; Index < ExtendedTableCount; Index ++) {
-                CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTable, sizeof(CPU_MICROCODE_EXTENDED_TABLE));
+                CheckSum32 = InCompleteCheckSum32;
+                CheckSum32 += ExtendedTable->ProcessorSignature.Uint32;
+                CheckSum32 += ExtendedTable->ProcessorFlag;
+                CheckSum32 += ExtendedTable->Checksum;
                 if (CheckSum32 == 0) {
                   //
                   // Verify Header
--
2.16.2.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
  2019-02-19  1:05 ` Zhang, Chao B
@ 2019-02-19  2:37   ` Ni, Ray
  0 siblings, 0 replies; 6+ messages in thread
From: Ni, Ray @ 2019-02-19  2:37 UTC (permalink / raw)
  To: Zhang, Chao B, Chen, Chen A, edk2-devel@lists.01.org; +Cc: Dong, Eric

I agree with Chao's comments.
Please add more code comments to:
1. describe the uCode format
2. explain all the 3 code blocks that checks the checksum.

> -----Original Message-----
> From: edk2-devel <edk2-devel-bounces@lists.01.org> On Behalf Of Zhang,
> Chao B
> Sent: Tuesday, February 19, 2019 9:06 AM
> To: Chen, Chen A <chen.a.chen@intel.com>; edk2-devel@lists.01.org
> Cc: Dong, Eric <eric.dong@intel.com>
> Subject: Re: [edk2] [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum
> issue for extended table
> 
> Chen Chen:
>    I think you can add uCode format info into comments. Also please highlight
> in comment Which part is header checksum calculation, which part is for
> extended header
> 
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Chen A Chen
> Sent: Monday, February 18, 2019 1:54 PM
> To: edk2-devel@lists.01.org
> Cc: Dong, Eric <eric.dong@intel.com>
> Subject: [edk2] [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum
> issue for extended table
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1020
> 
> The following Microcode payload format is define in SDM spec.
> Payload:
> |MicrocodeHeader|MicrocodeBinary|ExtendedHeader|ExtendedTable|.
> When we verify the CheckSum32 with ExtendedTable, we should use the
> fields of ExtendedTable to replace corresponding fields in MicrocodeHeader,
> and then calculate the CheckSum32 with MicrocodeHeader+MicrocodeBinary.
> This patch already verified on ICL platform.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> ---
>  UefiCpuPkg/Library/MpInitLib/Microcode.c | 38
> ++++++++++++++++++++++++--------
>  1 file changed, 29 insertions(+), 9 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/MpInitLib/Microcode.c
> b/UefiCpuPkg/Library/MpInitLib/Microcode.c
> index d84344c6f5..38880cdbec 100644
> --- a/UefiCpuPkg/Library/MpInitLib/Microcode.c
> +++ b/UefiCpuPkg/Library/MpInitLib/Microcode.c
> @@ -57,6 +57,7 @@ MicrocodeDetect (
>    UINT32                                  LatestRevision;
>    UINTN                                   TotalSize;
>    UINT32                                  CheckSum32;
> +  UINT32                                  InCompleteCheckSum32;
>    BOOLEAN                                 CorrectMicrocode;
>    VOID                                    *MicrocodeData;
>    MSR_IA32_PLATFORM_ID_REGISTER           PlatformIdMsr;
> @@ -121,6 +122,26 @@ MicrocodeDetect (
>    MicrocodeData  = NULL;
>    MicrocodeEnd = (UINTN) (CpuMpData->MicrocodePatchAddress +
> CpuMpData->MicrocodePatchRegionSize);
>    MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN)
> CpuMpData->MicrocodePatchAddress;
> +
> +  //
> +  // To avoid double calculate checksum32 value.
> +  // Save the CheckSum32 of the common parts in advance.
> +  //
> +  if (MicrocodeEntryPoint->DataSize == 0) {
> +    InCompleteCheckSum32 = CalculateSum32 (
> +                             (UINT32 *) MicrocodeEntryPoint,
> +                             sizeof (CPU_MICROCODE_HEADER) + 2000
> +                             );
> +  } else {
> +    InCompleteCheckSum32 = CalculateSum32 (
> +                             (UINT32 *) MicrocodeEntryPoint,
> +                             sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint-
> >DataSize
> +                             );
> +  }
> +  InCompleteCheckSum32 -=
> + MicrocodeEntryPoint->ProcessorSignature.Uint32;
> +  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
> +  InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
> +
>    do {
>      //
>      // Check if the microcode is for the Cpu and the version is newer @@ -
> 137,14 +158,10 @@ MicrocodeDetect (
>            MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
>            (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
>            ) {
> -        if (MicrocodeEntryPoint->DataSize == 0) {
> -          CheckSum32 = CalculateSum32 ((UINT32 *) MicrocodeEntryPoint, 2048);
> -        } else {
> -          CheckSum32 = CalculateSum32 (
> -                         (UINT32 *) MicrocodeEntryPoint,
> -                         MicrocodeEntryPoint->DataSize + sizeof
> (CPU_MICROCODE_HEADER)
> -                         );
> -        }
> +        CheckSum32 = InCompleteCheckSum32;
> +        CheckSum32 += MicrocodeEntryPoint->ProcessorSignature.Uint32;
> +        CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
> +        CheckSum32 += MicrocodeEntryPoint->Checksum;
>          if (CheckSum32 == 0) {
>            CorrectMicrocode = TRUE;
>            ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
> @@ -171,7 +188,10 @@ MicrocodeDetect (
>                ExtendedTableCount = ExtendedTableHeader-
> >ExtendedSignatureCount;
>                ExtendedTable      = (CPU_MICROCODE_EXTENDED_TABLE *)
> (ExtendedTableHeader + 1);
>                for (Index = 0; Index < ExtendedTableCount; Index ++) {
> -                CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTable,
> sizeof(CPU_MICROCODE_EXTENDED_TABLE));
> +                CheckSum32 = InCompleteCheckSum32;
> +                CheckSum32 += ExtendedTable->ProcessorSignature.Uint32;
> +                CheckSum32 += ExtendedTable->ProcessorFlag;
> +                CheckSum32 += ExtendedTable->Checksum;
>                  if (CheckSum32 == 0) {
>                    //
>                    // Verify Header
> --
> 2.16.2.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
@ 2019-02-19  6:32 Chen A Chen
  2019-02-20  8:27 ` Ni, Ray
  0 siblings, 1 reply; 6+ messages in thread
From: Chen A Chen @ 2019-02-19  6:32 UTC (permalink / raw)
  To: edk2-devel; +Cc: Chen A Chen, Ray Ni, Eric Dong, Zhang Chao B

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1020

The following Microcode payload format is define in SDM spec.
Payload: |MicrocodeHeader|MicrocodeBinary|ExtendedHeader|ExtendedTable|.
When we verify the CheckSum32 with ExtendedTable, we should use the fields
of ExtendedTable to replace corresponding fields in MicrocodeHeader,
and then calculate the CheckSum32 with MicrocodeHeader+MicrocodeBinary.
This patch already verified on ICL platform.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Zhang Chao B <chao.b.zhang@intel.com>
---
 UefiCpuPkg/Library/MpInitLib/Microcode.c | 82 ++++++++++++++++++++++++++++----
 1 file changed, 73 insertions(+), 9 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/Microcode.c b/UefiCpuPkg/Library/MpInitLib/Microcode.c
index d84344c6f5..e1f661d6b1 100644
--- a/UefiCpuPkg/Library/MpInitLib/Microcode.c
+++ b/UefiCpuPkg/Library/MpInitLib/Microcode.c
@@ -35,6 +35,42 @@ GetCurrentMicrocodeSignature (
 /**
   Detect whether specified processor can find matching microcode patch and load it.
 
+  Microcode Payload as the following format:
+  +----------------------------------------+------------------+
+  |          CPU_MICROCODE_HEADER          |                  |
+  +----------------------------------------+  CheckSum Part1  |
+  |            Microcode Binary            |                  |
+  +----------------------------------------+------------------+
+  |  CPU_MICROCODE_EXTENDED_TABLE_HEADER   |                  |
+  +----------------------------------------+  CheckSum Part2  |
+  |      CPU_MICROCODE_EXTENDED_TABLE      |                  |
+  |                   ...                  |                  |
+  +----------------------------------------+------------------+
+
+  There may by multiple CPU_MICROCODE_EXTENDED_TABLE in this format.
+  The count of CPU_MICROCODE_EXTENDED_TABLE is indicated by ExtendedSignatureCount
+  of CPU_MICROCODE_EXTENDED_TABLE_HEADER structure.
+
+  When we are trying to verify the CheckSum32 with extended table.
+  We should use the fields of exnteded table to replace the corresponding
+  fields in CPU_MICROCODE_HEADER structure, and recalculate the
+  CheckSum32 with CPU_MICROCODE_HEADER + Microcode Binary. We named
+  it as CheckSum Part3.
+
+  The CheckSum Part2 is used to verify the CPU_MICROCODE_EXTENDED_TABLE_HEADER
+  and CPU_MICROCODE_EXTENDED_TABLE parts. We should make sure CheckSum Part2
+  is correct before we are going to verify each CPU_MICROCODE_EXTENDED_TABLE.
+
+  Only ProcessorSignature, ProcessorFlag and CheckSum are different between
+  CheckSum Part1 and CheckSum Part3. To avoid multiple computing CheckSum Part3.
+  Save an in-complete CheckSum32 from CheckSum Part1 for common parts.
+  When we are going to calculate CheckSum32, just should use the corresponding part
+  of the ProcessorSignature, ProcessorFlag and CheckSum with in-complete CheckSum32.
+
+  Notes: CheckSum32 is not a strong verification.
+         It does not guarantee that the data has not been modified.
+         CPU has its own mechanism to verify Microcode Binary part.
+
   @param[in]  CpuMpData    The pointer to CPU MP Data structure.
   @param[in]  IsBspCallIn  Indicate whether the caller is BSP or not.
 **/
@@ -57,6 +93,7 @@ MicrocodeDetect (
   UINT32                                  LatestRevision;
   UINTN                                   TotalSize;
   UINT32                                  CheckSum32;
+  UINT32                                  InCompleteCheckSum32;
   BOOLEAN                                 CorrectMicrocode;
   VOID                                    *MicrocodeData;
   MSR_IA32_PLATFORM_ID_REGISTER           PlatformIdMsr;
@@ -121,6 +158,25 @@ MicrocodeDetect (
   MicrocodeData  = NULL;
   MicrocodeEnd = (UINTN) (CpuMpData->MicrocodePatchAddress + CpuMpData->MicrocodePatchRegionSize);
   MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN) CpuMpData->MicrocodePatchAddress;
+
+  //
+  // Save an in-complete CheckSum32 from CheckSum Part1 for common parts.
+  //
+  if (MicrocodeEntryPoint->DataSize == 0) {
+    InCompleteCheckSum32 = CalculateSum32 (
+                             (UINT32 *) MicrocodeEntryPoint,
+                             sizeof (CPU_MICROCODE_HEADER) + 2000
+                             );
+  } else {
+    InCompleteCheckSum32 = CalculateSum32 (
+                             (UINT32 *) MicrocodeEntryPoint,
+                             sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize
+                             );
+  }
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorSignature.Uint32;
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
+  InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
+
   do {
     //
     // Check if the microcode is for the Cpu and the version is newer
@@ -137,14 +193,13 @@ MicrocodeDetect (
           MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
           (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
           ) {
-        if (MicrocodeEntryPoint->DataSize == 0) {
-          CheckSum32 = CalculateSum32 ((UINT32 *) MicrocodeEntryPoint, 2048);
-        } else {
-          CheckSum32 = CalculateSum32 (
-                         (UINT32 *) MicrocodeEntryPoint,
-                         MicrocodeEntryPoint->DataSize + sizeof (CPU_MICROCODE_HEADER)
-                         );
-        }
+        //
+        // Calculate CheckSum Part1.
+        //
+        CheckSum32 = InCompleteCheckSum32;
+        CheckSum32 += MicrocodeEntryPoint->ProcessorSignature.Uint32;
+        CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
+        CheckSum32 += MicrocodeEntryPoint->Checksum;
         if (CheckSum32 == 0) {
           CorrectMicrocode = TRUE;
           ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
@@ -163,6 +218,9 @@ MicrocodeDetect (
           // Calculate Extended Checksum
           //
           if ((ExtendedTableLength % 4) == 0) {
+            //
+            // Calculate CheckSum Part2.
+            //
             CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTableHeader, ExtendedTableLength);
             if (CheckSum32 == 0) {
               //
@@ -171,7 +229,13 @@ MicrocodeDetect (
               ExtendedTableCount = ExtendedTableHeader->ExtendedSignatureCount;
               ExtendedTable      = (CPU_MICROCODE_EXTENDED_TABLE *) (ExtendedTableHeader + 1);
               for (Index = 0; Index < ExtendedTableCount; Index ++) {
-                CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTable, sizeof(CPU_MICROCODE_EXTENDED_TABLE));
+                //
+                // Calculate CheckSum Part3.
+                //
+                CheckSum32 = InCompleteCheckSum32;
+                CheckSum32 += ExtendedTable->ProcessorSignature.Uint32;
+                CheckSum32 += ExtendedTable->ProcessorFlag;
+                CheckSum32 += ExtendedTable->Checksum;
                 if (CheckSum32 == 0) {
                   //
                   // Verify Header
-- 
2.16.2.windows.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
  2019-02-19  6:32 Chen A Chen
@ 2019-02-20  8:27 ` Ni, Ray
  0 siblings, 0 replies; 6+ messages in thread
From: Ni, Ray @ 2019-02-20  8:27 UTC (permalink / raw)
  To: Chen A Chen, edk2-devel; +Cc: Eric Dong, Zhang Chao B

Reviewed-by: Ray Ni <ray.ni@intel.com>

-- 
Thanks,
Ray


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-02-20  8:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-18  5:53 [PATCH] UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table Chen A Chen
2019-02-18  5:58 ` Yao, Jiewen
2019-02-19  1:05 ` Zhang, Chao B
2019-02-19  2:37   ` Ni, Ray
  -- strict thread matches above, loose matches on Subject: below --
2019-02-19  6:32 Chen A Chen
2019-02-20  8:27 ` Ni, Ray

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox