Skip to content

Commit

Permalink
Refactor ProviderValueAsAscii()
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenbo Hou committed Mar 7, 2023
1 parent eaaeae8 commit fa792bf
Showing 1 changed file with 57 additions and 45 deletions.
102 changes: 57 additions & 45 deletions DfciPkg/SettingsManager/SettingsManagerProvider.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,6 @@ SetProviderValueFromAscii (
return Status;
}

#define ENABLED_STRING_SIZE (13)
#define SECURE_BOOT_ENUM_STRING_SIZE (20)
#define SYSTEM_PASSWORD_STATE_STRING_SIZE (30)
#define USB_PORT_STATE_STRING_SIZE (21)

/**
Helper function to Print out the Value as Ascii text.
NOTE: -- This must match the XML format
Expand All @@ -355,7 +350,8 @@ ProviderValueAsAscii (
)
{
EFI_STATUS Status;
CHAR8 *Value = NULL;
CHAR8 *Value = NULL;
CHAR8 *AsciiString = NULL;
UINTN AsciiSize;
UINT8 *Buffer;
BOOLEAN v = FALSE; // Boolean Types
Expand All @@ -376,19 +372,23 @@ ProviderValueAsAscii (
break;
}

Value = AllocateZeroPool (ENABLED_STRING_SIZE);
if (v == ENABLE_INCONSISTENT) {
AsciiString = "Inconsistent";
} else if (v) {
AsciiString = "Enabled";
} else {
AsciiString = "Disabled";
}

ValueSize = AsciiStrLen (AsciiString) + 1;

Value = AllocateZeroPool (ValueSize);
if (Value == NULL) {
DEBUG ((DEBUG_ERROR, "Failed - Couldn't allocate for string. \n"));
break;
}

if (v == ENABLE_INCONSISTENT) {
AsciiStrCpyS (Value, ENABLED_STRING_SIZE, "Inconsistent");
} else if (v) {
AsciiStrCpyS (Value, ENABLED_STRING_SIZE, "Enabled");
} else {
AsciiStrCpyS (Value, ENABLED_STRING_SIZE, "Disabled");
}
AsciiStrCpyS (Value, ValueSize, AsciiString);

break;

Expand All @@ -405,23 +405,27 @@ ProviderValueAsAscii (
break;
}

Value = AllocateZeroPool (SECURE_BOOT_ENUM_STRING_SIZE);
if (Value == NULL) {
DEBUG ((DEBUG_ERROR, "Failed - Couldn't allocate for string. \n"));
break;
}

if (b == 0) {
AsciiStrCpyS (Value, SECURE_BOOT_ENUM_STRING_SIZE, "MsOnly");
AsciiString = "MsOnly";
} else if (b == 1) {
AsciiStrCpyS (Value, SECURE_BOOT_ENUM_STRING_SIZE, "MsPlus3rdParty");
AsciiString = "MsPlus3rdParty";
} else if (b == 3) {
// This is a special case. Only supported as output.
AsciiStrCpyS (Value, SECURE_BOOT_ENUM_STRING_SIZE, "Custom");
AsciiString = "Custom";
} else {
AsciiStrCpyS (Value, SECURE_BOOT_ENUM_STRING_SIZE, "None");
AsciiString = "None";
}

ValueSize = AsciiStrLen (AsciiString) + 1;

Value = AllocateZeroPool (ValueSize);
if (Value == NULL) {
DEBUG ((DEBUG_ERROR, "Failed - Couldn't allocate for string. \n"));
break;
}

AsciiStrCpyS (Value, ValueSize, AsciiString);

break;

case DFCI_SETTING_TYPE_PASSWORD:
Expand All @@ -437,17 +441,21 @@ ProviderValueAsAscii (
break;
}

Value = AllocateZeroPool (SYSTEM_PASSWORD_STATE_STRING_SIZE);
if (v) {
AsciiString = "System Password Set";
} else {
AsciiString = "No System Password";
}

ValueSize = AsciiStrLen (AsciiString) + 1;

Value = AllocateZeroPool (ValueSize);
if (Value == NULL) {
DEBUG ((DEBUG_ERROR, "Failed - Couldn't allocate for string. \n"));
break;
}

if (v) {
AsciiStrCpyS (Value, SYSTEM_PASSWORD_STATE_STRING_SIZE, "System Password Set");
} else {
AsciiStrCpyS (Value, SYSTEM_PASSWORD_STATE_STRING_SIZE, "No System Password");
}
AsciiStrCpyS (Value, ValueSize, AsciiString);

break;

Expand All @@ -464,26 +472,30 @@ ProviderValueAsAscii (
break;
}

Value = AllocateZeroPool (USB_PORT_STATE_STRING_SIZE);
if (Value == NULL) {
DEBUG ((DEBUG_ERROR, "Failed - Couldn't allocate for string. \n"));
break;
}

if (b == DfciUsbPortHwDisabled) {
AsciiStrCpyS (Value, USB_PORT_STATE_STRING_SIZE, "UsbPortHwDisabled");
AsciiString = "UsbPortHwDisabled";
} else if (b == DfciUsbPortEnabled) {
AsciiStrCpyS (Value, USB_PORT_STATE_STRING_SIZE, "UsbPortEnabled");
AsciiString = "UsbPortEnabled";
} else if (b == DfciUsbPortDataDisabled) {
AsciiStrCpyS (Value, USB_PORT_STATE_STRING_SIZE, "UsbPortDataDisabled");
AsciiString = "UsbPortDataDisabled";
} else if (b == DfciUsbPortAuthenticated) {
AsciiStrCpyS (Value, USB_PORT_STATE_STRING_SIZE, "UsbPortAuthenticated");
AsciiString = "UsbPortAuthenticated";
} else if (b == ENABLE_INCONSISTENT) {
AsciiStrCpyS (Value, USB_PORT_STATE_STRING_SIZE, "Inconsistent");
AsciiString = "Inconsistent";
} else {
AsciiStrCpyS (Value, USB_PORT_STATE_STRING_SIZE, "UnsupportedValue");
AsciiString = "UnsupportedValue";
}

ValueSize = AsciiStrLen (AsciiString) + 1;

Value = AllocateZeroPool (ValueSize);
if (Value == NULL) {
DEBUG ((DEBUG_ERROR, "Failed - Couldn't allocate for string. \n"));
break;
}

AsciiStrCpyS (Value, ValueSize, AsciiString);

break;

case DFCI_SETTING_TYPE_STRING:
Expand All @@ -501,7 +513,7 @@ ProviderValueAsAscii (
break;
}

if (0 == ValueSize ) {
if (0 == ValueSize) {
break; // Return NULL for Value silently
}

Expand Down Expand Up @@ -555,7 +567,7 @@ ProviderValueAsAscii (
break;
}

if (0 == ValueSize ) {
if (0 == ValueSize) {
ValueSize = sizeof ("");
Value = AllocatePool (ValueSize);
if (NULL != Value) {
Expand Down Expand Up @@ -643,7 +655,7 @@ ProviderValueAsAscii (
break;
}

if (0 == ValueSize ) {
if (0 == ValueSize) {
break; // Return NULL for Value silently
}

Expand Down

0 comments on commit fa792bf

Please sign in to comment.