public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] Platform/FitGen: Add FitGen feature to support uCode Capsule Update
@ 2019-12-02  1:22 Chen A Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Chen A Chen @ 2019-12-02  1:22 UTC (permalink / raw)
  To: devel; +Cc: Chen A Chen

Add slot mode handling with a header array.

Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
---
 Silicon/Intel/Tools/FitGen/FitGen.c | 183 +++++++++++++++++++++++++-----------
 1 file changed, 129 insertions(+), 54 deletions(-)

diff --git a/Silicon/Intel/Tools/FitGen/FitGen.c b/Silicon/Intel/Tools/FitGen/FitGen.c
index faf9880060..ab236d0ab0 100644
--- a/Silicon/Intel/Tools/FitGen/FitGen.c
+++ b/Silicon/Intel/Tools/FitGen/FitGen.c
@@ -312,12 +312,13 @@ Returns:
 {
   printf ("Usage (generate): %s [-D] InputFvRecoveryFile OutputFvRecoveryFile\n"
           "\t[-V <FitEntryDefaultVersion>]\n"
-          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
-          "\t[-NA]\n"
-          "\t[-CLEAR]\n"
-          "\t[-I <BiosInfoGuid>]\n"
-          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
-          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
+          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
+          "\t[-NA]\n"
+          "\t[-CLEAR]\n"
+          "\t[-L <MicrocodeSlotSize> <MicrocodeFfsGuid>]\n"
+          "\t[-I <BiosInfoGuid>]\n"
+          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
+          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
           "\t[-M <MicrocodeAddress MicrocodeSize>] [-M ...]|[-U <MicrocodeFv MicrocodeBase>|<MicrocodeRegionOffset MicrocodeRegionSize>|<MicrocodeGuid>] [-V <MicrocodeVersion>]\n"
           "\t[-O RecordType <RecordDataAddress RecordDataSize>|<RESERVE RecordDataSize>|<RecordDataGuid>|<RecordBinFile> [-V <RecordVersion>]] [-O ... [-V ...]]\n"
           "\t[-P RecordType <IndexPort DataPort Width Bit Index> [-V <RecordVersion>]] [-P ... [-V ...]]\n"
@@ -337,12 +338,14 @@ Returns:
   printf ("\tMicrocodeSize          - Size of Microcode.\n");
   printf ("\tMicrocodeFv            - Name of Microcode.fv file.\n");
   printf ("\tMicrocodeBase          - The base address of Microcode.fv in final FD image.\n");
-  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
-  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
-  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
-  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
+  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
+  printf ("\tMicrocodeSlotSize      - Occupied region size of each Microcode binary.\n");
+  printf ("\tMicrocodeFfsGuid       - Guid of FFS which is used to save Microcode binary");
+  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
+  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
+  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
   printf ("\tRecordDataSize         - FIT entry record data size.\n");
   printf ("\tRecordDataGuid         - FIT entry record data GUID.\n");
   printf ("\tRecordBinFile          - FIT entry record data binary file.\n");
@@ -761,32 +764,39 @@ Returns:
   FitEntryNumber - The FIT entry number
   0              - Argument parse fail
 
-*/
-{
-  EFI_GUID  Guid;
-  INTN      Index;
-  UINT8     *FileBuffer;
-  UINT32    FileSize;
+*/
+{
+  EFI_GUID  Guid;
+  EFI_GUID  MicrocodeFfsGuid;
+  INTN      Index;
+  UINT8     *FileBuffer;
+  UINT32    FileSize;
   UINT32    Type;
   UINT8     *MicrocodeFileBuffer;
   UINT8     *MicrocodeFileBufferRaw;
   UINT32    MicrocodeFileSize;
-  UINT32    MicrocodeBase;
-  UINT32    MicrocodeSize;
-  UINT8     *MicrocodeBuffer;
-  UINT32    MicrocodeRegionOffset;
-  UINT32    MicrocodeRegionSize;
-  STATUS    Status;
-  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
-  UINTN                       FitEntryNumber;
-  BOOLEAN                     BiosInfoExist;
-  BIOS_INFO_HEADER            *BiosInfo;
-  BIOS_INFO_STRUCT            *BiosInfoStruct;
-  UINTN                       BiosInfoIndex;
-
-  //
-  // Init index
-  //
+  UINT32    MicrocodeBase;
+  UINT32    MicrocodeSize;
+  UINT8     *MicrocodeBuffer;
+  UINT32    MicrocodeBufferSize;
+  UINT8     *Walker;
+  UINT32    MicrocodeRegionOffset;
+  UINT32    MicrocodeRegionSize;
+  UINT32    SlotSize;
+  STATUS    Status;
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
+  UINTN                       FitEntryNumber;
+  BOOLEAN                     BiosInfoExist;
+  BOOLEAN                     SlotMode;
+  BIOS_INFO_HEADER            *BiosInfo;
+  BIOS_INFO_STRUCT            *BiosInfoStruct;
+  UINTN                       BiosInfoIndex;
+
+  SlotMode = FALSE;
+
+  //
+  // Init index
+  //
   Index = 3;
   if (((strcmp (argv[1], "-D") == 0) ||
        (strcmp (argv[1], "-d") == 0)) ) {
@@ -897,13 +907,40 @@ Returns:
     // Do not parse any more
     //
     return 0;
-  }
-
-  //
-  // 0.5 BiosInfo
-  //
-  if ((Index + 1 >= argc) ||
-      ((strcmp (argv[Index], "-I") != 0) &&
+  }
+
+  //
+  // 0.5 SlotSize
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-L") != 0) &&
+       (strcmp (argv[Index], "-l") != 0)) ) {
+    //
+    // Bypass
+    //
+    SlotSize = 0;
+  } else {
+    SlotSize = xtoi (argv[Index + 1]);
+
+    if (SlotSize == 0) {
+      printf ("Invalid slotsize = %d\n", SlotSize);
+      return 0;
+    }
+
+    SlotMode = IsGuidData(argv[Index + 2], &MicrocodeFfsGuid);
+    if (!SlotMode) {
+      printf ("Need a ffs GUID for search uCode ffs\n");
+      return 0;
+    }
+
+    Index += 3;
+  }
+
+  //
+  // 0.6 BiosInfo
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-I") != 0) &&
        (strcmp (argv[Index], "-i") != 0)) ) {
     //
     // Bypass
@@ -1004,12 +1041,22 @@ Returns:
             if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
               // Skip FV header + FFS header
               MicrocodeBuffer = MicrocodeFileBuffer + sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(EFI_FFS_FILE_HEADER);
-            } else {
-              MicrocodeBuffer = MicrocodeFileBuffer;
-            }
-            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
-              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
-                break;
+            } else {
+              MicrocodeBuffer = MicrocodeFileBuffer;
+            }
+
+            if (SlotMode) {
+              MicrocodeBuffer = FindFileFromFvByGuid(MicrocodeFileBuffer, MicrocodeFileSize, &MicrocodeFfsGuid, &MicrocodeBufferSize);
+              if (MicrocodeBuffer == NULL) {
+                printf ("-L Parameter incorrect, GUID not found\n");
+                // not found
+                return 0;
+              }
+            }
+
+            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
+              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
+                break;
               }
               if (*(UINT32 *)(MicrocodeBuffer + 20) != 0x1) { // LoaderVersion
                 break;
@@ -1037,13 +1084,41 @@ Returns:
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Size = MicrocodeSize;
-              gFitTableContext.MicrocodeNumber++;
-              gFitTableContext.FitEntryNumber++;
-
-              MicrocodeBuffer += MicrocodeSize;
-            }
-          }
-          break;
+              gFitTableContext.MicrocodeNumber++;
+              gFitTableContext.FitEntryNumber++;
+
+              if (SlotSize != 0) {
+                MicrocodeBuffer += SlotSize;
+              } else {
+                MicrocodeBuffer += MicrocodeSize;
+              }
+            }
+
+            ///
+            /// Check the remaining buffer
+            ///
+            if (((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) && SlotMode != 0) {
+              for (Walker = MicrocodeBuffer; Walker < MicrocodeFileBuffer + MicrocodeFileSize; Walker++) {
+                if (*Walker != 0xFF) {
+                  printf ("Error: detect non-spare space after uCode array, please check uCode array!\n");
+                  return 0;
+                }
+              }
+
+              ///
+              /// Split the spare space as empty buffer for save uCode patch.
+              ///
+              while (MicrocodeBuffer + SlotSize <= MicrocodeFileBuffer + MicrocodeFileSize) {
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
+                gFitTableContext.MicrocodeNumber++;
+                gFitTableContext.FitEntryNumber++;
+
+                MicrocodeBuffer += SlotSize;
+              }
+            }
+          }
+          break;
         case FIT_TABLE_TYPE_TPM_POLICY:
         case FIT_TABLE_TYPE_BIOS_POLICY:
         case FIT_TABLE_TYPE_TXT_POLICY:
-- 
2.16.2.windows.1


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

* [PATCH] Platform/FitGen: Add FitGen feature to support uCode Capsule Update
@ 2019-12-02  5:19 Chen A Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Chen A Chen @ 2019-12-02  5:19 UTC (permalink / raw)
  To: devel; +Cc: Chen A Chen

Add slot mode handling with a header array.

Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
---
 Silicon/Intel/Tools/FitGen/FitGen.c | 183 +++++++++++++++++++++++++-----------
 1 file changed, 129 insertions(+), 54 deletions(-)

diff --git a/Silicon/Intel/Tools/FitGen/FitGen.c b/Silicon/Intel/Tools/FitGen/FitGen.c
index faf9880060..ab236d0ab0 100644
--- a/Silicon/Intel/Tools/FitGen/FitGen.c
+++ b/Silicon/Intel/Tools/FitGen/FitGen.c
@@ -312,12 +312,13 @@ Returns:
 {
   printf ("Usage (generate): %s [-D] InputFvRecoveryFile OutputFvRecoveryFile\n"
           "\t[-V <FitEntryDefaultVersion>]\n"
-          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
-          "\t[-NA]\n"
-          "\t[-CLEAR]\n"
-          "\t[-I <BiosInfoGuid>]\n"
-          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
-          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
+          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
+          "\t[-NA]\n"
+          "\t[-CLEAR]\n"
+          "\t[-L <MicrocodeSlotSize> <MicrocodeFfsGuid>]\n"
+          "\t[-I <BiosInfoGuid>]\n"
+          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
+          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
           "\t[-M <MicrocodeAddress MicrocodeSize>] [-M ...]|[-U <MicrocodeFv MicrocodeBase>|<MicrocodeRegionOffset MicrocodeRegionSize>|<MicrocodeGuid>] [-V <MicrocodeVersion>]\n"
           "\t[-O RecordType <RecordDataAddress RecordDataSize>|<RESERVE RecordDataSize>|<RecordDataGuid>|<RecordBinFile> [-V <RecordVersion>]] [-O ... [-V ...]]\n"
           "\t[-P RecordType <IndexPort DataPort Width Bit Index> [-V <RecordVersion>]] [-P ... [-V ...]]\n"
@@ -337,12 +338,14 @@ Returns:
   printf ("\tMicrocodeSize          - Size of Microcode.\n");
   printf ("\tMicrocodeFv            - Name of Microcode.fv file.\n");
   printf ("\tMicrocodeBase          - The base address of Microcode.fv in final FD image.\n");
-  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
-  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
-  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
-  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
+  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
+  printf ("\tMicrocodeSlotSize      - Occupied region size of each Microcode binary.\n");
+  printf ("\tMicrocodeFfsGuid       - Guid of FFS which is used to save Microcode binary");
+  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
+  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
+  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
   printf ("\tRecordDataSize         - FIT entry record data size.\n");
   printf ("\tRecordDataGuid         - FIT entry record data GUID.\n");
   printf ("\tRecordBinFile          - FIT entry record data binary file.\n");
@@ -761,32 +764,39 @@ Returns:
   FitEntryNumber - The FIT entry number
   0              - Argument parse fail
 
-*/
-{
-  EFI_GUID  Guid;
-  INTN      Index;
-  UINT8     *FileBuffer;
-  UINT32    FileSize;
+*/
+{
+  EFI_GUID  Guid;
+  EFI_GUID  MicrocodeFfsGuid;
+  INTN      Index;
+  UINT8     *FileBuffer;
+  UINT32    FileSize;
   UINT32    Type;
   UINT8     *MicrocodeFileBuffer;
   UINT8     *MicrocodeFileBufferRaw;
   UINT32    MicrocodeFileSize;
-  UINT32    MicrocodeBase;
-  UINT32    MicrocodeSize;
-  UINT8     *MicrocodeBuffer;
-  UINT32    MicrocodeRegionOffset;
-  UINT32    MicrocodeRegionSize;
-  STATUS    Status;
-  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
-  UINTN                       FitEntryNumber;
-  BOOLEAN                     BiosInfoExist;
-  BIOS_INFO_HEADER            *BiosInfo;
-  BIOS_INFO_STRUCT            *BiosInfoStruct;
-  UINTN                       BiosInfoIndex;
-
-  //
-  // Init index
-  //
+  UINT32    MicrocodeBase;
+  UINT32    MicrocodeSize;
+  UINT8     *MicrocodeBuffer;
+  UINT32    MicrocodeBufferSize;
+  UINT8     *Walker;
+  UINT32    MicrocodeRegionOffset;
+  UINT32    MicrocodeRegionSize;
+  UINT32    SlotSize;
+  STATUS    Status;
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
+  UINTN                       FitEntryNumber;
+  BOOLEAN                     BiosInfoExist;
+  BOOLEAN                     SlotMode;
+  BIOS_INFO_HEADER            *BiosInfo;
+  BIOS_INFO_STRUCT            *BiosInfoStruct;
+  UINTN                       BiosInfoIndex;
+
+  SlotMode = FALSE;
+
+  //
+  // Init index
+  //
   Index = 3;
   if (((strcmp (argv[1], "-D") == 0) ||
        (strcmp (argv[1], "-d") == 0)) ) {
@@ -897,13 +907,40 @@ Returns:
     // Do not parse any more
     //
     return 0;
-  }
-
-  //
-  // 0.5 BiosInfo
-  //
-  if ((Index + 1 >= argc) ||
-      ((strcmp (argv[Index], "-I") != 0) &&
+  }
+
+  //
+  // 0.5 SlotSize
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-L") != 0) &&
+       (strcmp (argv[Index], "-l") != 0)) ) {
+    //
+    // Bypass
+    //
+    SlotSize = 0;
+  } else {
+    SlotSize = xtoi (argv[Index + 1]);
+
+    if (SlotSize == 0) {
+      printf ("Invalid slotsize = %d\n", SlotSize);
+      return 0;
+    }
+
+    SlotMode = IsGuidData(argv[Index + 2], &MicrocodeFfsGuid);
+    if (!SlotMode) {
+      printf ("Need a ffs GUID for search uCode ffs\n");
+      return 0;
+    }
+
+    Index += 3;
+  }
+
+  //
+  // 0.6 BiosInfo
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-I") != 0) &&
        (strcmp (argv[Index], "-i") != 0)) ) {
     //
     // Bypass
@@ -1004,12 +1041,22 @@ Returns:
             if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
               // Skip FV header + FFS header
               MicrocodeBuffer = MicrocodeFileBuffer + sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(EFI_FFS_FILE_HEADER);
-            } else {
-              MicrocodeBuffer = MicrocodeFileBuffer;
-            }
-            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
-              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
-                break;
+            } else {
+              MicrocodeBuffer = MicrocodeFileBuffer;
+            }
+
+            if (SlotMode) {
+              MicrocodeBuffer = FindFileFromFvByGuid(MicrocodeFileBuffer, MicrocodeFileSize, &MicrocodeFfsGuid, &MicrocodeBufferSize);
+              if (MicrocodeBuffer == NULL) {
+                printf ("-L Parameter incorrect, GUID not found\n");
+                // not found
+                return 0;
+              }
+            }
+
+            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
+              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
+                break;
               }
               if (*(UINT32 *)(MicrocodeBuffer + 20) != 0x1) { // LoaderVersion
                 break;
@@ -1037,13 +1084,41 @@ Returns:
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Size = MicrocodeSize;
-              gFitTableContext.MicrocodeNumber++;
-              gFitTableContext.FitEntryNumber++;
-
-              MicrocodeBuffer += MicrocodeSize;
-            }
-          }
-          break;
+              gFitTableContext.MicrocodeNumber++;
+              gFitTableContext.FitEntryNumber++;
+
+              if (SlotSize != 0) {
+                MicrocodeBuffer += SlotSize;
+              } else {
+                MicrocodeBuffer += MicrocodeSize;
+              }
+            }
+
+            ///
+            /// Check the remaining buffer
+            ///
+            if (((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) && SlotMode != 0) {
+              for (Walker = MicrocodeBuffer; Walker < MicrocodeFileBuffer + MicrocodeFileSize; Walker++) {
+                if (*Walker != 0xFF) {
+                  printf ("Error: detect non-spare space after uCode array, please check uCode array!\n");
+                  return 0;
+                }
+              }
+
+              ///
+              /// Split the spare space as empty buffer for save uCode patch.
+              ///
+              while (MicrocodeBuffer + SlotSize <= MicrocodeFileBuffer + MicrocodeFileSize) {
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
+                gFitTableContext.MicrocodeNumber++;
+                gFitTableContext.FitEntryNumber++;
+
+                MicrocodeBuffer += SlotSize;
+              }
+            }
+          }
+          break;
         case FIT_TABLE_TYPE_TPM_POLICY:
         case FIT_TABLE_TYPE_BIOS_POLICY:
         case FIT_TABLE_TYPE_TXT_POLICY:
-- 
2.16.2.windows.1


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

* [PATCH] Platform/FitGen: Add FitGen feature to support uCode Capsule Update
@ 2019-12-02  5:27 Chen A Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Chen A Chen @ 2019-12-02  5:27 UTC (permalink / raw)
  To: devel; +Cc: Chen A Chen

Add slot mode handling with a header array.

Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
---
 Silicon/Intel/Tools/FitGen/FitGen.c | 183 +++++++++++++++++++++++++-----------
 1 file changed, 129 insertions(+), 54 deletions(-)

diff --git a/Silicon/Intel/Tools/FitGen/FitGen.c b/Silicon/Intel/Tools/FitGen/FitGen.c
index faf9880060..ab236d0ab0 100644
--- a/Silicon/Intel/Tools/FitGen/FitGen.c
+++ b/Silicon/Intel/Tools/FitGen/FitGen.c
@@ -312,12 +312,13 @@ Returns:
 {
   printf ("Usage (generate): %s [-D] InputFvRecoveryFile OutputFvRecoveryFile\n"
           "\t[-V <FitEntryDefaultVersion>]\n"
-          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
-          "\t[-NA]\n"
-          "\t[-CLEAR]\n"
-          "\t[-I <BiosInfoGuid>]\n"
-          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
-          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
+          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
+          "\t[-NA]\n"
+          "\t[-CLEAR]\n"
+          "\t[-L <MicrocodeSlotSize> <MicrocodeFfsGuid>]\n"
+          "\t[-I <BiosInfoGuid>]\n"
+          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
+          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
           "\t[-M <MicrocodeAddress MicrocodeSize>] [-M ...]|[-U <MicrocodeFv MicrocodeBase>|<MicrocodeRegionOffset MicrocodeRegionSize>|<MicrocodeGuid>] [-V <MicrocodeVersion>]\n"
           "\t[-O RecordType <RecordDataAddress RecordDataSize>|<RESERVE RecordDataSize>|<RecordDataGuid>|<RecordBinFile> [-V <RecordVersion>]] [-O ... [-V ...]]\n"
           "\t[-P RecordType <IndexPort DataPort Width Bit Index> [-V <RecordVersion>]] [-P ... [-V ...]]\n"
@@ -337,12 +338,14 @@ Returns:
   printf ("\tMicrocodeSize          - Size of Microcode.\n");
   printf ("\tMicrocodeFv            - Name of Microcode.fv file.\n");
   printf ("\tMicrocodeBase          - The base address of Microcode.fv in final FD image.\n");
-  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
-  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
-  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
-  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
+  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
+  printf ("\tMicrocodeSlotSize      - Occupied region size of each Microcode binary.\n");
+  printf ("\tMicrocodeFfsGuid       - Guid of FFS which is used to save Microcode binary");
+  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
+  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
+  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
   printf ("\tRecordDataSize         - FIT entry record data size.\n");
   printf ("\tRecordDataGuid         - FIT entry record data GUID.\n");
   printf ("\tRecordBinFile          - FIT entry record data binary file.\n");
@@ -761,32 +764,39 @@ Returns:
   FitEntryNumber - The FIT entry number
   0              - Argument parse fail
 
-*/
-{
-  EFI_GUID  Guid;
-  INTN      Index;
-  UINT8     *FileBuffer;
-  UINT32    FileSize;
+*/
+{
+  EFI_GUID  Guid;
+  EFI_GUID  MicrocodeFfsGuid;
+  INTN      Index;
+  UINT8     *FileBuffer;
+  UINT32    FileSize;
   UINT32    Type;
   UINT8     *MicrocodeFileBuffer;
   UINT8     *MicrocodeFileBufferRaw;
   UINT32    MicrocodeFileSize;
-  UINT32    MicrocodeBase;
-  UINT32    MicrocodeSize;
-  UINT8     *MicrocodeBuffer;
-  UINT32    MicrocodeRegionOffset;
-  UINT32    MicrocodeRegionSize;
-  STATUS    Status;
-  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
-  UINTN                       FitEntryNumber;
-  BOOLEAN                     BiosInfoExist;
-  BIOS_INFO_HEADER            *BiosInfo;
-  BIOS_INFO_STRUCT            *BiosInfoStruct;
-  UINTN                       BiosInfoIndex;
-
-  //
-  // Init index
-  //
+  UINT32    MicrocodeBase;
+  UINT32    MicrocodeSize;
+  UINT8     *MicrocodeBuffer;
+  UINT32    MicrocodeBufferSize;
+  UINT8     *Walker;
+  UINT32    MicrocodeRegionOffset;
+  UINT32    MicrocodeRegionSize;
+  UINT32    SlotSize;
+  STATUS    Status;
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
+  UINTN                       FitEntryNumber;
+  BOOLEAN                     BiosInfoExist;
+  BOOLEAN                     SlotMode;
+  BIOS_INFO_HEADER            *BiosInfo;
+  BIOS_INFO_STRUCT            *BiosInfoStruct;
+  UINTN                       BiosInfoIndex;
+
+  SlotMode = FALSE;
+
+  //
+  // Init index
+  //
   Index = 3;
   if (((strcmp (argv[1], "-D") == 0) ||
        (strcmp (argv[1], "-d") == 0)) ) {
@@ -897,13 +907,40 @@ Returns:
     // Do not parse any more
     //
     return 0;
-  }
-
-  //
-  // 0.5 BiosInfo
-  //
-  if ((Index + 1 >= argc) ||
-      ((strcmp (argv[Index], "-I") != 0) &&
+  }
+
+  //
+  // 0.5 SlotSize
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-L") != 0) &&
+       (strcmp (argv[Index], "-l") != 0)) ) {
+    //
+    // Bypass
+    //
+    SlotSize = 0;
+  } else {
+    SlotSize = xtoi (argv[Index + 1]);
+
+    if (SlotSize == 0) {
+      printf ("Invalid slotsize = %d\n", SlotSize);
+      return 0;
+    }
+
+    SlotMode = IsGuidData(argv[Index + 2], &MicrocodeFfsGuid);
+    if (!SlotMode) {
+      printf ("Need a ffs GUID for search uCode ffs\n");
+      return 0;
+    }
+
+    Index += 3;
+  }
+
+  //
+  // 0.6 BiosInfo
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-I") != 0) &&
        (strcmp (argv[Index], "-i") != 0)) ) {
     //
     // Bypass
@@ -1004,12 +1041,22 @@ Returns:
             if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
               // Skip FV header + FFS header
               MicrocodeBuffer = MicrocodeFileBuffer + sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(EFI_FFS_FILE_HEADER);
-            } else {
-              MicrocodeBuffer = MicrocodeFileBuffer;
-            }
-            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
-              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
-                break;
+            } else {
+              MicrocodeBuffer = MicrocodeFileBuffer;
+            }
+
+            if (SlotMode) {
+              MicrocodeBuffer = FindFileFromFvByGuid(MicrocodeFileBuffer, MicrocodeFileSize, &MicrocodeFfsGuid, &MicrocodeBufferSize);
+              if (MicrocodeBuffer == NULL) {
+                printf ("-L Parameter incorrect, GUID not found\n");
+                // not found
+                return 0;
+              }
+            }
+
+            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
+              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
+                break;
               }
               if (*(UINT32 *)(MicrocodeBuffer + 20) != 0x1) { // LoaderVersion
                 break;
@@ -1037,13 +1084,41 @@ Returns:
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Size = MicrocodeSize;
-              gFitTableContext.MicrocodeNumber++;
-              gFitTableContext.FitEntryNumber++;
-
-              MicrocodeBuffer += MicrocodeSize;
-            }
-          }
-          break;
+              gFitTableContext.MicrocodeNumber++;
+              gFitTableContext.FitEntryNumber++;
+
+              if (SlotSize != 0) {
+                MicrocodeBuffer += SlotSize;
+              } else {
+                MicrocodeBuffer += MicrocodeSize;
+              }
+            }
+
+            ///
+            /// Check the remaining buffer
+            ///
+            if (((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) && SlotMode != 0) {
+              for (Walker = MicrocodeBuffer; Walker < MicrocodeFileBuffer + MicrocodeFileSize; Walker++) {
+                if (*Walker != 0xFF) {
+                  printf ("Error: detect non-spare space after uCode array, please check uCode array!\n");
+                  return 0;
+                }
+              }
+
+              ///
+              /// Split the spare space as empty buffer for save uCode patch.
+              ///
+              while (MicrocodeBuffer + SlotSize <= MicrocodeFileBuffer + MicrocodeFileSize) {
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
+                gFitTableContext.MicrocodeNumber++;
+                gFitTableContext.FitEntryNumber++;
+
+                MicrocodeBuffer += SlotSize;
+              }
+            }
+          }
+          break;
         case FIT_TABLE_TYPE_TPM_POLICY:
         case FIT_TABLE_TYPE_BIOS_POLICY:
         case FIT_TABLE_TYPE_TXT_POLICY:
-- 
2.16.2.windows.1


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

* [PATCH] Platform/FitGen: Add FitGen feature to support uCode Capsule Update
@ 2019-12-02  5:33 Chen A Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Chen A Chen @ 2019-12-02  5:33 UTC (permalink / raw)
  To: devel

Add slot mode handling with a header array.

Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
---
 Silicon/Intel/Tools/FitGen/FitGen.c | 183 +++++++++++++++++++++++++-----------
 1 file changed, 129 insertions(+), 54 deletions(-)

diff --git a/Silicon/Intel/Tools/FitGen/FitGen.c b/Silicon/Intel/Tools/FitGen/FitGen.c
index faf9880060..ab236d0ab0 100644
--- a/Silicon/Intel/Tools/FitGen/FitGen.c
+++ b/Silicon/Intel/Tools/FitGen/FitGen.c
@@ -312,12 +312,13 @@ Returns:
 {
   printf ("Usage (generate): %s [-D] InputFvRecoveryFile OutputFvRecoveryFile\n"
           "\t[-V <FitEntryDefaultVersion>]\n"
-          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
-          "\t[-NA]\n"
-          "\t[-CLEAR]\n"
-          "\t[-I <BiosInfoGuid>]\n"
-          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
-          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
+          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
+          "\t[-NA]\n"
+          "\t[-CLEAR]\n"
+          "\t[-L <MicrocodeSlotSize> <MicrocodeFfsGuid>]\n"
+          "\t[-I <BiosInfoGuid>]\n"
+          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
+          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
           "\t[-M <MicrocodeAddress MicrocodeSize>] [-M ...]|[-U <MicrocodeFv MicrocodeBase>|<MicrocodeRegionOffset MicrocodeRegionSize>|<MicrocodeGuid>] [-V <MicrocodeVersion>]\n"
           "\t[-O RecordType <RecordDataAddress RecordDataSize>|<RESERVE RecordDataSize>|<RecordDataGuid>|<RecordBinFile> [-V <RecordVersion>]] [-O ... [-V ...]]\n"
           "\t[-P RecordType <IndexPort DataPort Width Bit Index> [-V <RecordVersion>]] [-P ... [-V ...]]\n"
@@ -337,12 +338,14 @@ Returns:
   printf ("\tMicrocodeSize          - Size of Microcode.\n");
   printf ("\tMicrocodeFv            - Name of Microcode.fv file.\n");
   printf ("\tMicrocodeBase          - The base address of Microcode.fv in final FD image.\n");
-  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
-  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
-  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
-  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
+  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
+  printf ("\tMicrocodeSlotSize      - Occupied region size of each Microcode binary.\n");
+  printf ("\tMicrocodeFfsGuid       - Guid of FFS which is used to save Microcode binary");
+  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
+  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
+  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
   printf ("\tRecordDataSize         - FIT entry record data size.\n");
   printf ("\tRecordDataGuid         - FIT entry record data GUID.\n");
   printf ("\tRecordBinFile          - FIT entry record data binary file.\n");
@@ -761,32 +764,39 @@ Returns:
   FitEntryNumber - The FIT entry number
   0              - Argument parse fail
 
-*/
-{
-  EFI_GUID  Guid;
-  INTN      Index;
-  UINT8     *FileBuffer;
-  UINT32    FileSize;
+*/
+{
+  EFI_GUID  Guid;
+  EFI_GUID  MicrocodeFfsGuid;
+  INTN      Index;
+  UINT8     *FileBuffer;
+  UINT32    FileSize;
   UINT32    Type;
   UINT8     *MicrocodeFileBuffer;
   UINT8     *MicrocodeFileBufferRaw;
   UINT32    MicrocodeFileSize;
-  UINT32    MicrocodeBase;
-  UINT32    MicrocodeSize;
-  UINT8     *MicrocodeBuffer;
-  UINT32    MicrocodeRegionOffset;
-  UINT32    MicrocodeRegionSize;
-  STATUS    Status;
-  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
-  UINTN                       FitEntryNumber;
-  BOOLEAN                     BiosInfoExist;
-  BIOS_INFO_HEADER            *BiosInfo;
-  BIOS_INFO_STRUCT            *BiosInfoStruct;
-  UINTN                       BiosInfoIndex;
-
-  //
-  // Init index
-  //
+  UINT32    MicrocodeBase;
+  UINT32    MicrocodeSize;
+  UINT8     *MicrocodeBuffer;
+  UINT32    MicrocodeBufferSize;
+  UINT8     *Walker;
+  UINT32    MicrocodeRegionOffset;
+  UINT32    MicrocodeRegionSize;
+  UINT32    SlotSize;
+  STATUS    Status;
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
+  UINTN                       FitEntryNumber;
+  BOOLEAN                     BiosInfoExist;
+  BOOLEAN                     SlotMode;
+  BIOS_INFO_HEADER            *BiosInfo;
+  BIOS_INFO_STRUCT            *BiosInfoStruct;
+  UINTN                       BiosInfoIndex;
+
+  SlotMode = FALSE;
+
+  //
+  // Init index
+  //
   Index = 3;
   if (((strcmp (argv[1], "-D") == 0) ||
        (strcmp (argv[1], "-d") == 0)) ) {
@@ -897,13 +907,40 @@ Returns:
     // Do not parse any more
     //
     return 0;
-  }
-
-  //
-  // 0.5 BiosInfo
-  //
-  if ((Index + 1 >= argc) ||
-      ((strcmp (argv[Index], "-I") != 0) &&
+  }
+
+  //
+  // 0.5 SlotSize
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-L") != 0) &&
+       (strcmp (argv[Index], "-l") != 0)) ) {
+    //
+    // Bypass
+    //
+    SlotSize = 0;
+  } else {
+    SlotSize = xtoi (argv[Index + 1]);
+
+    if (SlotSize == 0) {
+      printf ("Invalid slotsize = %d\n", SlotSize);
+      return 0;
+    }
+
+    SlotMode = IsGuidData(argv[Index + 2], &MicrocodeFfsGuid);
+    if (!SlotMode) {
+      printf ("Need a ffs GUID for search uCode ffs\n");
+      return 0;
+    }
+
+    Index += 3;
+  }
+
+  //
+  // 0.6 BiosInfo
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-I") != 0) &&
        (strcmp (argv[Index], "-i") != 0)) ) {
     //
     // Bypass
@@ -1004,12 +1041,22 @@ Returns:
             if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
               // Skip FV header + FFS header
               MicrocodeBuffer = MicrocodeFileBuffer + sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(EFI_FFS_FILE_HEADER);
-            } else {
-              MicrocodeBuffer = MicrocodeFileBuffer;
-            }
-            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
-              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
-                break;
+            } else {
+              MicrocodeBuffer = MicrocodeFileBuffer;
+            }
+
+            if (SlotMode) {
+              MicrocodeBuffer = FindFileFromFvByGuid(MicrocodeFileBuffer, MicrocodeFileSize, &MicrocodeFfsGuid, &MicrocodeBufferSize);
+              if (MicrocodeBuffer == NULL) {
+                printf ("-L Parameter incorrect, GUID not found\n");
+                // not found
+                return 0;
+              }
+            }
+
+            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
+              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
+                break;
               }
               if (*(UINT32 *)(MicrocodeBuffer + 20) != 0x1) { // LoaderVersion
                 break;
@@ -1037,13 +1084,41 @@ Returns:
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Size = MicrocodeSize;
-              gFitTableContext.MicrocodeNumber++;
-              gFitTableContext.FitEntryNumber++;
-
-              MicrocodeBuffer += MicrocodeSize;
-            }
-          }
-          break;
+              gFitTableContext.MicrocodeNumber++;
+              gFitTableContext.FitEntryNumber++;
+
+              if (SlotSize != 0) {
+                MicrocodeBuffer += SlotSize;
+              } else {
+                MicrocodeBuffer += MicrocodeSize;
+              }
+            }
+
+            ///
+            /// Check the remaining buffer
+            ///
+            if (((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) && SlotMode != 0) {
+              for (Walker = MicrocodeBuffer; Walker < MicrocodeFileBuffer + MicrocodeFileSize; Walker++) {
+                if (*Walker != 0xFF) {
+                  printf ("Error: detect non-spare space after uCode array, please check uCode array!\n");
+                  return 0;
+                }
+              }
+
+              ///
+              /// Split the spare space as empty buffer for save uCode patch.
+              ///
+              while (MicrocodeBuffer + SlotSize <= MicrocodeFileBuffer + MicrocodeFileSize) {
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
+                gFitTableContext.MicrocodeNumber++;
+                gFitTableContext.FitEntryNumber++;
+
+                MicrocodeBuffer += SlotSize;
+              }
+            }
+          }
+          break;
         case FIT_TABLE_TYPE_TPM_POLICY:
         case FIT_TABLE_TYPE_BIOS_POLICY:
         case FIT_TABLE_TYPE_TXT_POLICY:
-- 
2.16.2.windows.1


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

* [PATCH] Platform/FitGen: Add FitGen feature to support uCode Capsule Update
@ 2019-12-02  5:37 Chen A Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Chen A Chen @ 2019-12-02  5:37 UTC (permalink / raw)
  To: devel

Add slot mode handling with a header array.

Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
---
 Silicon/Intel/Tools/FitGen/FitGen.c | 183 +++++++++++++++++++++++++-----------
 1 file changed, 129 insertions(+), 54 deletions(-)

diff --git a/Silicon/Intel/Tools/FitGen/FitGen.c b/Silicon/Intel/Tools/FitGen/FitGen.c
index faf9880060..ab236d0ab0 100644
--- a/Silicon/Intel/Tools/FitGen/FitGen.c
+++ b/Silicon/Intel/Tools/FitGen/FitGen.c
@@ -312,12 +312,13 @@ Returns:
 {
   printf ("Usage (generate): %s [-D] InputFvRecoveryFile OutputFvRecoveryFile\n"
           "\t[-V <FitEntryDefaultVersion>]\n"
-          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
-          "\t[-NA]\n"
-          "\t[-CLEAR]\n"
-          "\t[-I <BiosInfoGuid>]\n"
-          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
-          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
+          "\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
+          "\t[-NA]\n"
+          "\t[-CLEAR]\n"
+          "\t[-L <MicrocodeSlotSize> <MicrocodeFfsGuid>]\n"
+          "\t[-I <BiosInfoGuid>]\n"
+          "\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-V <StartupAcmVersion>]\n"
+          "\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
           "\t[-M <MicrocodeAddress MicrocodeSize>] [-M ...]|[-U <MicrocodeFv MicrocodeBase>|<MicrocodeRegionOffset MicrocodeRegionSize>|<MicrocodeGuid>] [-V <MicrocodeVersion>]\n"
           "\t[-O RecordType <RecordDataAddress RecordDataSize>|<RESERVE RecordDataSize>|<RecordDataGuid>|<RecordBinFile> [-V <RecordVersion>]] [-O ... [-V ...]]\n"
           "\t[-P RecordType <IndexPort DataPort Width Bit Index> [-V <RecordVersion>]] [-P ... [-V ...]]\n"
@@ -337,12 +338,14 @@ Returns:
   printf ("\tMicrocodeSize          - Size of Microcode.\n");
   printf ("\tMicrocodeFv            - Name of Microcode.fv file.\n");
   printf ("\tMicrocodeBase          - The base address of Microcode.fv in final FD image.\n");
-  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
-  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
-  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
-  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
-  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
+  printf ("\tMicrocodeRegionOffset  - Offset of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeRegionSize    - Size of Microcode region in input FD image.\n");
+  printf ("\tMicrocodeGuid          - Guid of Microcode Module.\n");
+  printf ("\tMicrocodeSlotSize      - Occupied region size of each Microcode binary.\n");
+  printf ("\tMicrocodeFfsGuid       - Guid of FFS which is used to save Microcode binary");
+  printf ("\t-NA                    - No 0x800 aligned Microcode requirement. No -NA means Microcode is 0x800 aligned.\n");
+  printf ("\tRecordType             - FIT entry record type. User should ensure it is ordered.\n");
+  printf ("\tRecordDataAddress      - FIT entry record data address.\n");
   printf ("\tRecordDataSize         - FIT entry record data size.\n");
   printf ("\tRecordDataGuid         - FIT entry record data GUID.\n");
   printf ("\tRecordBinFile          - FIT entry record data binary file.\n");
@@ -761,32 +764,39 @@ Returns:
   FitEntryNumber - The FIT entry number
   0              - Argument parse fail
 
-*/
-{
-  EFI_GUID  Guid;
-  INTN      Index;
-  UINT8     *FileBuffer;
-  UINT32    FileSize;
+*/
+{
+  EFI_GUID  Guid;
+  EFI_GUID  MicrocodeFfsGuid;
+  INTN      Index;
+  UINT8     *FileBuffer;
+  UINT32    FileSize;
   UINT32    Type;
   UINT8     *MicrocodeFileBuffer;
   UINT8     *MicrocodeFileBufferRaw;
   UINT32    MicrocodeFileSize;
-  UINT32    MicrocodeBase;
-  UINT32    MicrocodeSize;
-  UINT8     *MicrocodeBuffer;
-  UINT32    MicrocodeRegionOffset;
-  UINT32    MicrocodeRegionSize;
-  STATUS    Status;
-  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
-  UINTN                       FitEntryNumber;
-  BOOLEAN                     BiosInfoExist;
-  BIOS_INFO_HEADER            *BiosInfo;
-  BIOS_INFO_STRUCT            *BiosInfoStruct;
-  UINTN                       BiosInfoIndex;
-
-  //
-  // Init index
-  //
+  UINT32    MicrocodeBase;
+  UINT32    MicrocodeSize;
+  UINT8     *MicrocodeBuffer;
+  UINT32    MicrocodeBufferSize;
+  UINT8     *Walker;
+  UINT32    MicrocodeRegionOffset;
+  UINT32    MicrocodeRegionSize;
+  UINT32    SlotSize;
+  STATUS    Status;
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
+  UINTN                       FitEntryNumber;
+  BOOLEAN                     BiosInfoExist;
+  BOOLEAN                     SlotMode;
+  BIOS_INFO_HEADER            *BiosInfo;
+  BIOS_INFO_STRUCT            *BiosInfoStruct;
+  UINTN                       BiosInfoIndex;
+
+  SlotMode = FALSE;
+
+  //
+  // Init index
+  //
   Index = 3;
   if (((strcmp (argv[1], "-D") == 0) ||
        (strcmp (argv[1], "-d") == 0)) ) {
@@ -897,13 +907,40 @@ Returns:
     // Do not parse any more
     //
     return 0;
-  }
-
-  //
-  // 0.5 BiosInfo
-  //
-  if ((Index + 1 >= argc) ||
-      ((strcmp (argv[Index], "-I") != 0) &&
+  }
+
+  //
+  // 0.5 SlotSize
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-L") != 0) &&
+       (strcmp (argv[Index], "-l") != 0)) ) {
+    //
+    // Bypass
+    //
+    SlotSize = 0;
+  } else {
+    SlotSize = xtoi (argv[Index + 1]);
+
+    if (SlotSize == 0) {
+      printf ("Invalid slotsize = %d\n", SlotSize);
+      return 0;
+    }
+
+    SlotMode = IsGuidData(argv[Index + 2], &MicrocodeFfsGuid);
+    if (!SlotMode) {
+      printf ("Need a ffs GUID for search uCode ffs\n");
+      return 0;
+    }
+
+    Index += 3;
+  }
+
+  //
+  // 0.6 BiosInfo
+  //
+  if ((Index + 1 >= argc) ||
+      ((strcmp (argv[Index], "-I") != 0) &&
        (strcmp (argv[Index], "-i") != 0)) ) {
     //
     // Bypass
@@ -1004,12 +1041,22 @@ Returns:
             if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
               // Skip FV header + FFS header
               MicrocodeBuffer = MicrocodeFileBuffer + sizeof(EFI_FIRMWARE_VOLUME_HEADER) + sizeof(EFI_FV_BLOCK_MAP_ENTRY) + sizeof(EFI_FFS_FILE_HEADER);
-            } else {
-              MicrocodeBuffer = MicrocodeFileBuffer;
-            }
-            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
-              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
-                break;
+            } else {
+              MicrocodeBuffer = MicrocodeFileBuffer;
+            }
+
+            if (SlotMode) {
+              MicrocodeBuffer = FindFileFromFvByGuid(MicrocodeFileBuffer, MicrocodeFileSize, &MicrocodeFfsGuid, &MicrocodeBufferSize);
+              if (MicrocodeBuffer == NULL) {
+                printf ("-L Parameter incorrect, GUID not found\n");
+                // not found
+                return 0;
+              }
+            }
+
+            while ((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) {
+              if (*(UINT32 *)(MicrocodeBuffer) != 0x1) { // HeaderVersion
+                break;
               }
               if (*(UINT32 *)(MicrocodeBuffer + 20) != 0x1) { // LoaderVersion
                 break;
@@ -1037,13 +1084,41 @@ Returns:
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
               gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Size = MicrocodeSize;
-              gFitTableContext.MicrocodeNumber++;
-              gFitTableContext.FitEntryNumber++;
-
-              MicrocodeBuffer += MicrocodeSize;
-            }
-          }
-          break;
+              gFitTableContext.MicrocodeNumber++;
+              gFitTableContext.FitEntryNumber++;
+
+              if (SlotSize != 0) {
+                MicrocodeBuffer += SlotSize;
+              } else {
+                MicrocodeBuffer += MicrocodeSize;
+              }
+            }
+
+            ///
+            /// Check the remaining buffer
+            ///
+            if (((UINT32)(MicrocodeBuffer - MicrocodeFileBuffer) < MicrocodeFileSize) && SlotMode != 0) {
+              for (Walker = MicrocodeBuffer; Walker < MicrocodeFileBuffer + MicrocodeFileSize; Walker++) {
+                if (*Walker != 0xFF) {
+                  printf ("Error: detect non-spare space after uCode array, please check uCode array!\n");
+                  return 0;
+                }
+              }
+
+              ///
+              /// Split the spare space as empty buffer for save uCode patch.
+              ///
+              while (MicrocodeBuffer + SlotSize <= MicrocodeFileBuffer + MicrocodeFileSize) {
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Type = FIT_TABLE_TYPE_MICROCODE;
+                gFitTableContext.Microcode[gFitTableContext.MicrocodeNumber].Address = MicrocodeBase + ((UINT32) (UINTN) MicrocodeBuffer - (UINT32) (UINTN) MicrocodeFileBuffer);
+                gFitTableContext.MicrocodeNumber++;
+                gFitTableContext.FitEntryNumber++;
+
+                MicrocodeBuffer += SlotSize;
+              }
+            }
+          }
+          break;
         case FIT_TABLE_TYPE_TPM_POLICY:
         case FIT_TABLE_TYPE_BIOS_POLICY:
         case FIT_TABLE_TYPE_TXT_POLICY:
-- 
2.16.2.windows.1


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

end of thread, other threads:[~2019-12-02  5:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-02  1:22 [PATCH] Platform/FitGen: Add FitGen feature to support uCode Capsule Update Chen A Chen
  -- strict thread matches above, loose matches on Subject: below --
2019-12-02  5:19 Chen A Chen
2019-12-02  5:27 Chen A Chen
2019-12-02  5:33 Chen A Chen
2019-12-02  5:37 Chen A Chen

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