Skip to content

Commit

Permalink
MdeModulePkg: Added SetPkgInfo, GetPkgInfo, CheckImg Functions
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Narvaria <[email protected]>
  • Loading branch information
abhi-narvaria committed Oct 8, 2024
1 parent 95ef528 commit b4b79d0
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 1 deletion.
165 changes: 165 additions & 0 deletions MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -402,13 +517,24 @@ 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 ()
{
Print (L" -fGetCXLDeviceList : Get CXL Device List (GetCXLDeviceList).\n");
Print (L" -fimginfo <b: Bus> <d: Device> <f: Function> : Get information about current firmware image (GetImageInfo).\n");
Print (L" -fsetimg <b: Bus> <d: Device> <f: Function> <Slot> <file> : Firmware download and activate (SetImage).\n");
Print (L" -fgetimg <b: Bus> <d: Device> <f: Function> <Slot> : Get information about the firmware package (GetImage).\n");
Print (L" -fchkimg <b: Bus> <d: Device> <f: Function> <Slot> <file> : Check the validity of a firmware image (CheckImage).\n");
Print (L" -fsetpack <b: Bus> <d: Device> <f: Function> : Set information about the firmware package (SetPackageInfo).\n");
Print (L" -fgetpack <b: Bus> <d: Device> <f: Function> : Get information about the firmware package (GetPackageInfo).\n");
}

BOOLEAN isDigit(char ch)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion MdeModulePkg/Application/CxlFirmwareMgmt/CxlFirmwareMgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/

Expand Down
4 changes: 4 additions & 0 deletions MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_fw_mgmt.c
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
};
30 changes: 30 additions & 0 deletions MdeModulePkg/Bus/Pci/CxlDxe/CxlDxe_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit b4b79d0

Please sign in to comment.