Re: [edk2-devel] [PATCH 1/4] UefiCpuPkg: Add MicrocodeLib for loading microcode

2021-04-07 Thread Dong, Eric
Reviewed-by: Eric Dong 

-Original Message-
From: Ni, Ray  
Sent: Friday, April 2, 2021 1:58 PM
To: devel@edk2.groups.io
Cc: Dong, Eric ; Laszlo Ersek ; Kumar, 
Rahul1 
Subject: [PATCH 1/4] UefiCpuPkg: Add MicrocodeLib for loading microcode

Signed-off-by: Ray Ni 
Cc: Eric Dong 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
---
 UefiCpuPkg/Include/Library/MicrocodeLib.h | 120 +++
 .../Library/MicrocodeLib/MicrocodeLib.c   | 322 ++
 .../Library/MicrocodeLib/MicrocodeLib.inf |  32 ++
 UefiCpuPkg/UefiCpuPkg.dec |   5 +-
 UefiCpuPkg/UefiCpuPkg.dsc |   1 +
 5 files changed, 479 insertions(+), 1 deletion(-)
 create mode 100644 UefiCpuPkg/Include/Library/MicrocodeLib.h
 create mode 100644 UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.c
 create mode 100644 UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.inf

diff --git a/UefiCpuPkg/Include/Library/MicrocodeLib.h 
b/UefiCpuPkg/Include/Library/MicrocodeLib.h
new file mode 100644
index 00..2570c43cce
--- /dev/null
+++ b/UefiCpuPkg/Include/Library/MicrocodeLib.h
@@ -0,0 +1,120 @@
+/** @file

+  Public include file for Microcode library.

+

+  Copyright (c) 2021, Intel Corporation. All rights reserved.

+  SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef __MICROCODE_LIB_H__

+#define __MICROCODE_LIB_H__

+

+#include 

+#include 

+

+/**

+  Get microcode update signature of currently loaded microcode update.

+

+  @return  Microcode signature.

+**/

+UINT32

+EFIAPI

+GetProcessorMicrocodeSignature (

+  VOID

+  );

+

+/**

+  Get the processor signature and platform ID for current processor.

+

+  @param MicrocodeCpuId  Return the processor signature and platform ID.

+**/

+VOID

+EFIAPI

+GetProcessorMicrocodeCpuId (

+  EDKII_PEI_MICROCODE_CPU_ID  *MicrocodeCpuId

+  );

+

+/**

+  Return the total size of the microcode entry.

+

+  Logic follows pseudo code in SDM as below:

+

+ N = 512

+ If (Update.DataSize != H)

+   N = Update.TotalSize / 4

+

+  If Microcode is NULL, then ASSERT.

+

+  @param Microcode  Pointer to the microcode entry.

+

+  @return The microcode total size.

+**/

+UINT32

+EFIAPI

+GetMicrocodeLength (

+  IN CPU_MICROCODE_HEADER *Microcode

+  );

+

+/**

+  Load the microcode to the processor.

+

+  If Microcode is NULL, then ASSERT.

+

+  @param Microcode  Pointer to the microcode entry.

+**/

+VOID

+EFIAPI

+LoadMicrocode (

+  IN CPU_MICROCODE_HEADER *Microcode

+  );

+

+/**

+  Detect whether specified processor can find matching microcode patch and 
load it.

+

+  Microcode format is as below:

+  
++-+

+  |  CPU_MICROCODE_HEADER  |   
  |

+  ++   
  V

+  |  Update Data   |   
CPU_MICROCODE_HEADER.Checksum

+  ++---+   
  ^

+  |  CPU_MICROCODE_EXTENDED_TABLE_HEADER   |   |   
  |

+  ++   V   
  |

+  |  CPU_MICROCODE_EXTENDED_TABLE[0]   |  
CPU_MICROCODE_EXTENDED_TABLE_HEADER.Checksum   |

+  |  CPU_MICROCODE_EXTENDED_TABLE[1]   |   ^   
  |

+  |   ...  |   |   
  |

+  
++---+-+

+

+  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.

+

+  If Microcode is NULL, then ASSERT.

+

+  @param MicrocodePointer to a microcode entry.

+  @param MicrocodeLength  The total length of the microcode entry.

+  @param MinimumRevision  The microcode whose revision <= MinimumRevision 
is treated as invalid.

+  Caller can supply value get from 
GetProcessorMicrocodeSignature() to check

+  whether the microcode is newer than loaded one.

+  Caller can supply 0 to treat any revision 
(except 0) microcode as valid.

+  @param MicrocodeCpuIds  Pointer to an array of processor signature and 
platform ID that represents

+  a set of processors.

+  Caller can supply zero-element array to skip the 
processor signature and

+  platform ID check.

+  @param MicrocodeCpuIdCount  The number of elements in MicrocodeCpuIds.

+  @param VerifyChecksum   FALSE to skip all the checksum 

Re: [edk2-devel] [PATCH 1/4] UefiCpuPkg: Add MicrocodeLib for loading microcode

2021-04-07 Thread Laszlo Ersek
On 04/02/21 07:58, Ni, Ray wrote:
> Signed-off-by: Ray Ni 
> Cc: Eric Dong 
> Cc: Laszlo Ersek 
> Cc: Rahul Kumar 
> ---
>  UefiCpuPkg/Include/Library/MicrocodeLib.h | 120 +++
>  .../Library/MicrocodeLib/MicrocodeLib.c   | 322 ++
>  .../Library/MicrocodeLib/MicrocodeLib.inf |  32 ++
>  UefiCpuPkg/UefiCpuPkg.dec |   5 +-
>  UefiCpuPkg/UefiCpuPkg.dsc |   1 +
>  5 files changed, 479 insertions(+), 1 deletion(-)
>  create mode 100644 UefiCpuPkg/Include/Library/MicrocodeLib.h
>  create mode 100644 UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.c
>  create mode 100644 UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.inf

With the BZ ref added:

Acked-by: Laszlo Ersek 

Thanks
Laszlo


> diff --git a/UefiCpuPkg/Include/Library/MicrocodeLib.h 
> b/UefiCpuPkg/Include/Library/MicrocodeLib.h
> new file mode 100644
> index 00..2570c43cce
> --- /dev/null
> +++ b/UefiCpuPkg/Include/Library/MicrocodeLib.h
> @@ -0,0 +1,120 @@
> +/** @file
> +  Public include file for Microcode library.
> +
> +  Copyright (c) 2021, Intel Corporation. All rights reserved.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef __MICROCODE_LIB_H__
> +#define __MICROCODE_LIB_H__
> +
> +#include 
> +#include 
> +
> +/**
> +  Get microcode update signature of currently loaded microcode update.
> +
> +  @return  Microcode signature.
> +**/
> +UINT32
> +EFIAPI
> +GetProcessorMicrocodeSignature (
> +  VOID
> +  );
> +
> +/**
> +  Get the processor signature and platform ID for current processor.
> +
> +  @param MicrocodeCpuId  Return the processor signature and platform ID.
> +**/
> +VOID
> +EFIAPI
> +GetProcessorMicrocodeCpuId (
> +  EDKII_PEI_MICROCODE_CPU_ID  *MicrocodeCpuId
> +  );
> +
> +/**
> +  Return the total size of the microcode entry.
> +
> +  Logic follows pseudo code in SDM as below:
> +
> + N = 512
> + If (Update.DataSize != H)
> +   N = Update.TotalSize / 4
> +
> +  If Microcode is NULL, then ASSERT.
> +
> +  @param Microcode  Pointer to the microcode entry.
> +
> +  @return The microcode total size.
> +**/
> +UINT32
> +EFIAPI
> +GetMicrocodeLength (
> +  IN CPU_MICROCODE_HEADER *Microcode
> +  );
> +
> +/**
> +  Load the microcode to the processor.
> +
> +  If Microcode is NULL, then ASSERT.
> +
> +  @param Microcode  Pointer to the microcode entry.
> +**/
> +VOID
> +EFIAPI
> +LoadMicrocode (
> +  IN CPU_MICROCODE_HEADER *Microcode
> +  );
> +
> +/**
> +  Detect whether specified processor can find matching microcode patch and 
> load it.
> +
> +  Microcode format is as below:
> +  
> ++-+
> +  |  CPU_MICROCODE_HEADER  | 
> |
> +  ++ 
> V
> +  |  Update Data   | 
>   CPU_MICROCODE_HEADER.Checksum
> +  ++---+ 
> ^
> +  |  CPU_MICROCODE_EXTENDED_TABLE_HEADER   |   | 
> |
> +  ++   V 
> |
> +  |  CPU_MICROCODE_EXTENDED_TABLE[0]   |  
> CPU_MICROCODE_EXTENDED_TABLE_HEADER.Checksum   |
> +  |  CPU_MICROCODE_EXTENDED_TABLE[1]   |   ^ 
> |
> +  |   ...  |   | 
> |
> +  
> ++---+-+
> +
> +  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.
> +
> +  If Microcode is NULL, then ASSERT.
> +
> +  @param MicrocodePointer to a microcode entry.
> +  @param MicrocodeLength  The total length of the microcode entry.
> +  @param MinimumRevision  The microcode whose revision <= 
> MinimumRevision is treated as invalid.
> +  Caller can supply value get from 
> GetProcessorMicrocodeSignature() to check
> +  whether the microcode is newer than loaded one.
> +  Caller can supply 0 to treat any revision 
> (except 0) microcode as valid.
> +  @param MicrocodeCpuIds  Pointer to an array of processor signature and 
> platform ID that represents
> +  a set of processors.
> +  Caller can supply zero-element array to skip 
> the processor signature and
> +  platform ID check.
> +  @param MicrocodeCpuIdCount  The number of elements in MicrocodeCpuIds.
> +  @param VerifyChecksum   

[edk2-devel] [PATCH 1/4] UefiCpuPkg: Add MicrocodeLib for loading microcode

2021-04-01 Thread Ni, Ray
Signed-off-by: Ray Ni 
Cc: Eric Dong 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
---
 UefiCpuPkg/Include/Library/MicrocodeLib.h | 120 +++
 .../Library/MicrocodeLib/MicrocodeLib.c   | 322 ++
 .../Library/MicrocodeLib/MicrocodeLib.inf |  32 ++
 UefiCpuPkg/UefiCpuPkg.dec |   5 +-
 UefiCpuPkg/UefiCpuPkg.dsc |   1 +
 5 files changed, 479 insertions(+), 1 deletion(-)
 create mode 100644 UefiCpuPkg/Include/Library/MicrocodeLib.h
 create mode 100644 UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.c
 create mode 100644 UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.inf

diff --git a/UefiCpuPkg/Include/Library/MicrocodeLib.h 
b/UefiCpuPkg/Include/Library/MicrocodeLib.h
new file mode 100644
index 00..2570c43cce
--- /dev/null
+++ b/UefiCpuPkg/Include/Library/MicrocodeLib.h
@@ -0,0 +1,120 @@
+/** @file
+  Public include file for Microcode library.
+
+  Copyright (c) 2021, Intel Corporation. All rights reserved.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef __MICROCODE_LIB_H__
+#define __MICROCODE_LIB_H__
+
+#include 
+#include 
+
+/**
+  Get microcode update signature of currently loaded microcode update.
+
+  @return  Microcode signature.
+**/
+UINT32
+EFIAPI
+GetProcessorMicrocodeSignature (
+  VOID
+  );
+
+/**
+  Get the processor signature and platform ID for current processor.
+
+  @param MicrocodeCpuId  Return the processor signature and platform ID.
+**/
+VOID
+EFIAPI
+GetProcessorMicrocodeCpuId (
+  EDKII_PEI_MICROCODE_CPU_ID  *MicrocodeCpuId
+  );
+
+/**
+  Return the total size of the microcode entry.
+
+  Logic follows pseudo code in SDM as below:
+
+ N = 512
+ If (Update.DataSize != H)
+   N = Update.TotalSize / 4
+
+  If Microcode is NULL, then ASSERT.
+
+  @param Microcode  Pointer to the microcode entry.
+
+  @return The microcode total size.
+**/
+UINT32
+EFIAPI
+GetMicrocodeLength (
+  IN CPU_MICROCODE_HEADER *Microcode
+  );
+
+/**
+  Load the microcode to the processor.
+
+  If Microcode is NULL, then ASSERT.
+
+  @param Microcode  Pointer to the microcode entry.
+**/
+VOID
+EFIAPI
+LoadMicrocode (
+  IN CPU_MICROCODE_HEADER *Microcode
+  );
+
+/**
+  Detect whether specified processor can find matching microcode patch and 
load it.
+
+  Microcode format is as below:
+  
++-+
+  |  CPU_MICROCODE_HEADER  |   
  |
+  ++   
  V
+  |  Update Data   |   
CPU_MICROCODE_HEADER.Checksum
+  ++---+   
  ^
+  |  CPU_MICROCODE_EXTENDED_TABLE_HEADER   |   |   
  |
+  ++   V   
  |
+  |  CPU_MICROCODE_EXTENDED_TABLE[0]   |  
CPU_MICROCODE_EXTENDED_TABLE_HEADER.Checksum   |
+  |  CPU_MICROCODE_EXTENDED_TABLE[1]   |   ^   
  |
+  |   ...  |   |   
  |
+  
++---+-+
+
+  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.
+
+  If Microcode is NULL, then ASSERT.
+
+  @param MicrocodePointer to a microcode entry.
+  @param MicrocodeLength  The total length of the microcode entry.
+  @param MinimumRevision  The microcode whose revision <= MinimumRevision 
is treated as invalid.
+  Caller can supply value get from 
GetProcessorMicrocodeSignature() to check
+  whether the microcode is newer than loaded one.
+  Caller can supply 0 to treat any revision 
(except 0) microcode as valid.
+  @param MicrocodeCpuIds  Pointer to an array of processor signature and 
platform ID that represents
+  a set of processors.
+  Caller can supply zero-element array to skip the 
processor signature and
+  platform ID check.
+  @param MicrocodeCpuIdCount  The number of elements in MicrocodeCpuIds.
+  @param VerifyChecksum   FALSE to skip all the checksum verifications.
+
+  @retval TRUE  The microcode is valid.
+  @retval FALSE The microcode is invalid.
+**/
+BOOLEAN
+EFIAPI
+IsValidMicrocode (
+  IN CPU_MICROCODE_HEADER   *Microcode,
+  IN UINTN  MicrocodeLength,
+  IN UINT32 MinimumRevision,
+  IN EDKII_PEI_MICROCODE_CPU_ID *MicrocodeCpuIds,
+  IN UINTN