From b4b79d0808f05491e023c0361dd743bb91a0a8a2 Mon Sep 17 00:00:00 2001 From: Abhishek Narvaria Date: Mon, 7 Oct 2024 12:38:31 +0530 Subject: [PATCH] MdeModulePkg: Added SetPkgInfo, GetPkgInfo, CheckImg Functions Signed-off-by: Abhishek Narvaria --- .../CxlFirmwareMgmt/CxlFirmwareMgmt.c | 165 ++++++++++++++++++ .../CxlFirmwareMgmt/CxlFirmwareMgmt.h | 7 +- MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.c | 1 + MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.h | 4 + MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_fw_mgmt.c | 63 +++++++ MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_util.c | 30 ++++ 6 files changed, 269 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.c b/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.c index 3a3b4d87fa80f..e9103d37f313d 100644 --- a/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.c +++ b/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.c @@ -245,6 +245,69 @@ EFI_STATUS GetImageInfo(UINTN Bus, UINTN Device, UINTN Func) return Status; } +EFI_STATUS GetPackageInfo(UINTN Bus, UINTN Device, UINTN Func) +{ + EFI_STATUS Status = EFI_SUCCESS; + EFI_HANDLE *Handles = NULL; + UINTN Index; + UINTN NumOfHandles = 0; + UINT32 PackageVersion = 0; + CHAR16 *PackageVersionName = NULL; + UINT32 PackageVersionNameMaxLen = 0; + UINT64 AttributesSupported = 0; + UINT64 AttributesSetting = 0; + CXL_CONTROLLER_PRIVATE_DATA *Private = NULL; + + Status = getHandleNum(&NumOfHandles, &Handles); + if (Status != EFI_SUCCESS) { + Print(L"GetPackageInfo: Fail to locate handle buffer...\n"); + return Status; + } + + for (Index = 0; Index < NumOfHandles; Index++) { + Status = getPrivateStr(&Private, Handles, Index); + if (Status != EFI_SUCCESS) { + Print(L"GetImageInfo: Fail to locate handle buffer...\n"); + continue; + } + + if (Bus == Private->Bus && Device == Private->Dev && Func == Private->Func) { + PackageVersionName = AllocateZeroPool(CXL_STRING_BUFFER_WIDTH); + if (PackageVersionName == NULL) { + DEBUG((EFI_D_ERROR, "GetImageInfo: AllocateZeroPool failed!\n")); + Status = EFI_OUT_OF_RESOURCES; + return Status; + } + + Status = Private->FirmwareMgmt.GetPackageInfo( + &Private->FirmwareMgmt, + &PackageVersion, + &PackageVersionName, + &PackageVersionNameMaxLen, + &AttributesSupported, + &AttributesSetting + ); + + if (!EFI_ERROR(Status)) { + Print(L"Package Version Name : %s\n", PackageVersionName); + Print(L"Package Version : %d\n", PackageVersion); + Print(L"Attributes Supported : %d\n", AttributesSupported); + Print(L"Attributes Setting : %d\n", AttributesSetting); + } else { + Print(L"Calling FMP.GetPackageInfo...%r\n", Status); + } + + if (NULL != PackageVersionName) { + FreePool(PackageVersionName); + } + break; + } + } + + FreePool(Handles); + return Status; +} + EFI_STATUS GetImage(UINTN Bus, UINTN Device, UINTN Func, UINTN Slot) { EFI_STATUS Status = EFI_SUCCESS; @@ -313,6 +376,58 @@ EFI_STATUS GetImage(UINTN Bus, UINTN Device, UINTN Func, UINTN Slot) return Status; } +EFI_STATUS SetPackageInfo(UINTN Bus, UINTN Device, UINTN Func) +{ + EFI_STATUS Status = EFI_SUCCESS; + EFI_HANDLE *Handles = NULL; + UINTN Index; + UINTN NumOfHandles = 0; + + CONST VOID *Image = NULL; + UINTN ImageSize = 0; + CONST VOID *VendorCode = NULL; + UINT32 PackageVersion = 1; + CHAR16 PackageVersionName[CXL_STRING_BUFFER_WIDTH]; + + CXL_CONTROLLER_PRIVATE_DATA *Private = NULL; + + Status = getHandleNum(&NumOfHandles, &Handles); + if (Status != EFI_SUCCESS) { + Print(L"SetPackageInfo: Fail to locate handle buffer...\n"); + return Status; + } + + for (Index = 0; Index < NumOfHandles; Index++) { + Status = getPrivateStr(&Private, Handles, Index); + if (Status != EFI_SUCCESS) { + Print(L"GetImageInfo: Fail to locate handle buffer...\n"); + continue; + } + + if (Bus == Private->Bus && Device == Private->Dev && Func == Private->Func) { + strCpyApp_c16(PackageVersionName, CXL_PACKAGE_VERSION_NAME_APP); + Status = Private->FirmwareMgmt.SetPackageInfo( + &Private->FirmwareMgmt, + &Image, + ImageSize, + &VendorCode, + PackageVersion, + PackageVersionName + ); + + if (!EFI_ERROR(Status)) { + Print(L"SetPackageInfo Success\n"); + } else { + Print(L"Calling FMP SetPackageInfo Failed...%r\n", Status); + } + break; + } + } + + FreePool(Handles); + return Status; +} + EFI_STATUS GetCXLDeviceList() { EFI_HANDLE *Handles = NULL; @@ -402,6 +517,14 @@ SetImage(UINTN Bus, UINTN Device, UINTN Func, UINTN Slot, CHAR16 *FileName) return Status; } +EFI_STATUS +CheckImage(UINTN Bus, UINTN Device, UINTN Func, UINTN Slot) +{ + EFI_STATUS Status = EFI_SUCCESS; + Print(L"CheckImage Command not supported...\n"); + return Status; +} + void PrintHelpPage () { @@ -409,6 +532,9 @@ PrintHelpPage () Print (L" -fimginfo : Get information about current firmware image (GetImageInfo).\n"); Print (L" -fsetimg : Firmware download and activate (SetImage).\n"); Print (L" -fgetimg : Get information about the firmware package (GetImage).\n"); + Print (L" -fchkimg : Check the validity of a firmware image (CheckImage).\n"); + Print (L" -fsetpack : Set information about the firmware package (SetPackageInfo).\n"); + Print (L" -fgetpack : Get information about the firmware package (GetPackageInfo).\n"); } BOOLEAN isDigit(char ch) @@ -519,6 +645,27 @@ validArguments(UINTN Argc, CHAR16 **Argv, UINTN *Bus, UINTN *Device, UINTN *Func } break; + case OpTypeFmpCheckImg: + if (Argc != 7) { + Print(L"Invalid argument...\n"); + return FALSE; + } + break; + + case OpTypeFmpGetPkgInfo: + if (Argc != 5) { + Print(L"Invalid argument...\n"); + return FALSE; + } + break; + + case OpTypeSetPkgInfo: + if (Argc != 5) { + Print(L"Invalid argument...\n"); + return FALSE; + } + break; + default: return FALSE; } @@ -551,6 +698,12 @@ CXL_FMP_OPERATION_TYPE GetOptype(UINTN Argc, CHAR16 **Argv) { OpType = OpTypeFmpSetImg; } else if (!StrCmp(str, L"-fgetimg")) { OpType = OpTypeGetImage; + } else if (!StrCmp(str, L"-fchkimg")) { + OpType = OpTypeFmpCheckImg; + } else if (!StrCmp(str, L"-fsetpack")) { + OpType = OpTypeSetPkgInfo; + } else if (!StrCmp(str, L"-fgetpack")) { + OpType = OpTypeFmpGetPkgInfo; } else { Print(L"Invalid argument...\n"); OpType = OpTypeDisplayHelp; @@ -610,6 +763,18 @@ cxlFWMain( Status = GetImage(Bus, Device, Func, Slot); break; + case OpTypeFmpCheckImg: + Status = CheckImage(Bus, Device, Func, Slot); + break; + + case OpTypeFmpGetPkgInfo: + Status = GetPackageInfo(Bus, Device, Func); + break; + + case OpTypeSetPkgInfo: + Status = SetPackageInfo(Bus, Device, Func); + break; + default: Print(L"Invalid Operation Type\n"); break; diff --git a/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.h b/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.h index 1ef51e6b03bf4..d1384242f1fe4 100644 --- a/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.h +++ b/MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.h @@ -39,15 +39,20 @@ #define CXL_FW_IMAGE_DESCRIPTOR_COUNT 5 #define CXL_FW_MAX_SLOTS 5 #define CXL_STRING_BUFFER_WIDTH 256 +#define CXL_PACKAGE_VERSION_NAME_APP L"CXL Firmware Package Name Application" #define CXL_FW_SIZE 32768 /* 32 mb */ #define CXL_CONTROLLER_PRIVATE_FROM_FIRMWARE_MGMT(a) CR (a, CXL_CONTROLLER_PRIVATE_DATA, FirmwareMgmt, CXL_CONTROLLER_PRIVATE_DATA_SIGNATURE) typedef enum { OpTypeDisplayHelp, OpTypeListDevice, + OpTypeSetActiveFwSlot, OpTypeFmpGetImgInfo, OpTypeGetImage, - OpTypeFmpSetImg + OpTypeSetPkgInfo, + OpTypeFmpSetImg, + OpTypeFmpCheckImg, + OpTypeFmpGetPkgInfo, } CXL_FMP_OPERATION_TYPE; struct cxl_reg_map { diff --git a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.c b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.c index fb5d199e0c4bb..d68a80d387d45 100644 --- a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.c +++ b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.c @@ -5,6 +5,7 @@ supports Get Fw Info Sending/Receiving FMP commands Set Fw Image, Activate Fw image + SetPkgInfo, GetPkgInfo, CheckImg SPDX-License-Identifier: BSD-2-Clause-Patent **/ diff --git a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.h b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.h index f054c67d08e07..390ecbffcfc03 100644 --- a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.h +++ b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.h @@ -423,6 +423,10 @@ void strCpy(CHAR16 *st1, char *st2); void strCpy_c16(CHAR16 *st1, CHAR16 *st2); +void strCpy_const16(CHAR16 *st1, CONST CHAR16 *st2); + +int strCmp(CHAR16 *str1, CHAR16 *str2); + void InitializeFwImageDescriptor(CXL_CONTROLLER_PRIVATE_DATA *Private); EFI_STATUS pci_uefi_read_config_word(CXL_CONTROLLER_PRIVATE_DATA *Private, UINT32 start, UINT32 *val); diff --git a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_fw_mgmt.c b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_fw_mgmt.c index 6608e18ef1d68..b139c407daf2b 100644 --- a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_fw_mgmt.c +++ b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_fw_mgmt.c @@ -1,11 +1,13 @@ /** @file Firmware Management Protocol - supports Get Fw Info, Sending/Receiving FMP commands supports Set Fw Image, Activate Fw image + SetPkgInfo, GetPkgInfo, CheckImg SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "CxlDxe.h" extern void strCpy_c16(CHAR16 *st1, CHAR16 *st2); +extern void strCpy_const16(CHAR16 *st1, CONST CHAR16 *st2); /** Returns information about the current firmware image(s) of the device. @@ -168,9 +170,70 @@ CxlFirmwareMgmtSetImage ( return Status; } +EFI_STATUS +EFIAPI +CxlFirmwareMgmtCheckImage ( + IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This, + IN UINT8 ImageIndex, + IN CONST VOID *Image, + IN UINTN ImageSize, + OUT UINT32 *ImageUpdatable + ) +{ + return EFI_UNSUPPORTED; +} + +EFI_STATUS +EFIAPI +CxlFirmwareMgmtGetPackageInfo ( + IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This, + OUT UINT32 *PackageVersion, + OUT CHAR16 **PackageVersionName, + OUT UINT32 *PackageVersionNameMaxLen, + OUT UINT64 *AttributesSupported, + OUT UINT64 *AttributesSetting + ) +{ + DEBUG((EFI_D_INFO, "CxlFirmwareMgmtGetPackageInfo: Entering CxlFirmwareMgmtGetPackageInfo...\n")); + CXL_CONTROLLER_PRIVATE_DATA *Private; + Private = CXL_CONTROLLER_PRIVATE_FROM_FIRMWARE_MGMT(This); + + if (Private->PackageVersionName != NULL) { + strCpy_c16(*PackageVersionName, Private->PackageVersionName); + } + + *PackageVersion = Private->PackageVersion; + *AttributesSupported = Private->slotInfo.FwImageDescriptor[0].AttributesSupported; + *AttributesSetting = Private->slotInfo.FwImageDescriptor[0].AttributesSetting; + + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +CxlFirmwareMgmtSetPackageInfo ( + IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This, + IN CONST VOID *Image, + IN UINTN ImageSize, + IN CONST VOID *VendorCode, + IN UINT32 PackageVersion, + IN CONST CHAR16 *PackageVersionName + ) +{ + DEBUG((EFI_D_INFO, "CxlFirmwareMgmtSetPackageInfo: Entering CxlFirmwareMgmtSetPackageInfo...\n")); + CXL_CONTROLLER_PRIVATE_DATA *Private; + Private = CXL_CONTROLLER_PRIVATE_FROM_FIRMWARE_MGMT(This); + strCpy_const16(Private->PackageVersionName, PackageVersionName); + Private->PackageVersion = PackageVersion; + return EFI_SUCCESS; +} + GLOBAL_REMOVE_IF_UNREFERENCED EFI_FIRMWARE_MANAGEMENT_PROTOCOL gCxlFirmwareManagement = { CxlFirmwareMgmtGetImageInfo, CxlFirmwareMgmtGetImage, CxlFirmwareMgmtSetImage, + CxlFirmwareMgmtCheckImage, + CxlFirmwareMgmtGetPackageInfo, + CxlFirmwareMgmtSetPackageInfo }; diff --git a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_util.c b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_util.c index ff00ac2fee4f9..9ec7d7d508549 100644 --- a/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_util.c +++ b/MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_util.c @@ -89,6 +89,36 @@ void strCpy_c16(CHAR16 *st1, CHAR16 *st2) st1[i] = '\0'; } +void strCpy_const16(CHAR16 *st1, CONST CHAR16 *st2) +{ + int i = 0; + for (i = 0; st2[i] != '\0'; i++) { + st1[i] = st2[i]; + } + st1[i] = '\0'; +} + +int strCmp(CHAR16 *str1, CHAR16 *str2) { + + int i = 0; + if (str1 == NULL) { + return 1; + } + + while (str1[i] == str2[i]) { + if (str1[i] == '\0' || str2[i] == '\0') { + break; + } + i++; + } + + if (str1[i] == '\0' && str2[i] == '\0') { + return 0; + } else { + return str1[i] - str2[i]; + } +} + void InitializeFwImageDescriptor(CXL_CONTROLLER_PRIVATE_DATA *Private) {