From 398454235c172e0680dbc178f2e7a1e9463dec28 Mon Sep 17 00:00:00 2001 From: Aaron <105021049+apop5@users.noreply.github.com> Date: Thu, 13 Jun 2024 13:45:15 -0700 Subject: [PATCH] os/UefiVariableSupport: Support null termination (#581) Modified SetUefiVar and GetUefiVar to deal with null terminated strings being passed. Some consumers were passing null termination, while others were not. --- edk2toollib/os/uefivariablesupport.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/edk2toollib/os/uefivariablesupport.py b/edk2toollib/os/uefivariablesupport.py index 3b5719f9..df8421b2 100644 --- a/edk2toollib/os/uefivariablesupport.py +++ b/edk2toollib/os/uefivariablesupport.py @@ -122,6 +122,10 @@ def GetUefiVar(self, name: str, guid: str) -> tuple[int, str]: Tuple: (error code, string of variable data) """ err = 0 + + # Remove null termination on the name, if it exists. + name = name.rstrip('\x00') + if os.name == 'nt': efi_var = create_string_buffer(EFI_VAR_MAX_BUFFER_SIZE) if self._GetFirmwareEnvironmentVariable is not None: @@ -267,6 +271,9 @@ def SetUefiVar(self, name: str, guid: str, var: str = None, attrs: int = None) - """ success = 0 # Fail + # Remove null termination on the name, if it exists. + name = name.rstrip('\x00') + if os.name == 'nt': var_len = 0 err = 0 @@ -306,8 +313,7 @@ def SetUefiVar(self, name: str, guid: str, var: str = None, attrs: int = None) - logging.error(WinError()) return success else: - # There is a null terminator at the end of the name - path = '/sys/firmware/efi/efivars/' + name[:-1] + '-' + str(guid) + path = '/sys/firmware/efi/efivars/' + name + '-' + str(guid) if var is None: # we are deleting the variable if (os.path.exists(path)):