Skip to content

Commit

Permalink
os/UefiVariableSupport: Support null termination (#581)
Browse files Browse the repository at this point in the history
Modified SetUefiVar and GetUefiVar to deal with null terminated strings being passed. Some consumers were passing null termination, while others were not.
  • Loading branch information
apop5 authored Jun 13, 2024
1 parent e9a1b21 commit 3984542
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions edk2toollib/os/uefivariablesupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)):
Expand Down

0 comments on commit 3984542

Please sign in to comment.