From 314c7f7937ee64e7d55b5db8ef1d0d6c49a9c495 Mon Sep 17 00:00:00 2001 From: Aaron <105021049+apop5@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:26:38 -0700 Subject: [PATCH] Bump edk2-pytool-library to 0.21.7. Switch functions using local UefiVariableSupportLib*.py to using edk2-pytool-library versions. Remove Local UefiVariableSupportLib instances. --- .../Tools/ReadUefiVarsToConfVarList.py | 5 +- .../Tools/SettingSupport/PyRobotRemote.py | 7 +- .../SettingSupport/UefiVariablesSupportLib.py | 201 ------------------ .../UefiVariablesSupportLinuxLib.py | 143 ------------- SetupDataPkg/Tools/WrapAndSetSvd.py | 6 +- .../Tools/WriteConfVarListToUefiVars.py | 5 +- 6 files changed, 8 insertions(+), 359 deletions(-) delete mode 100644 SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py delete mode 100644 SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLinuxLib.py diff --git a/SetupDataPkg/Tools/ReadUefiVarsToConfVarList.py b/SetupDataPkg/Tools/ReadUefiVarsToConfVarList.py index 92a12234..cb4d8f30 100644 --- a/SetupDataPkg/Tools/ReadUefiVarsToConfVarList.py +++ b/SetupDataPkg/Tools/ReadUefiVarsToConfVarList.py @@ -12,10 +12,7 @@ import struct import uuid import ctypes -if os.name == 'nt': - from SettingSupport.UefiVariablesSupportLib import UefiVariable -else: - from SettingSupport.UefiVariablesSupportLinuxLib import UefiVariable +from edk2toollib.os.uefivariablesupport import UefiVariable from VariableList import Schema, UEFIVariable, create_vlist_buffer diff --git a/SetupDataPkg/Tools/SettingSupport/PyRobotRemote.py b/SetupDataPkg/Tools/SettingSupport/PyRobotRemote.py index 1cf049a9..634fce18 100644 --- a/SetupDataPkg/Tools/SettingSupport/PyRobotRemote.py +++ b/SetupDataPkg/Tools/SettingSupport/PyRobotRemote.py @@ -55,18 +55,17 @@ def Run_Command_And_Return_Output(self, cmdline): def GetUefiVariable(self, name, guid, trim): UefiVar = UefiVariable() logging.info("Calling GetUefiVar(name='%s', GUID='%s')" % (name, "{%s}" % guid)) - (rc, var, error_string) = UefiVar.GetUefiVar(name, guid) + (rc, var) = UefiVar.GetUefiVar(name, guid) var2 = var if (var is not None) and (trim == 'trim'): varlen = len(var) if varlen > 1: var2 = var[0: varlen - 1] - return (rc, var2, error_string) + return (rc, var2, "") def SetUefiVariable(self, name, guid, attrs=None, contents=None): UefiVar = UefiVariable() - (rc, err, error_string) = UefiVar.SetUefiVar(name, guid, contents, attrs) - return rc + return UefiVar.SetUefiVar(name, guid, contents, attrs) def remote_ack(self): return True diff --git a/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py b/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py deleted file mode 100644 index 7a6c1e32..00000000 --- a/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py +++ /dev/null @@ -1,201 +0,0 @@ -# @file -# -# Python lib to support Reading and writing UEFI variables from windows -# -# Copyright (c), Microsoft Corporation -# SPDX-License-Identifier: BSD-2-Clause-Patent - -from ctypes import ( - windll, - c_wchar_p, - c_void_p, - c_int, - create_string_buffer, - WinError, - pointer -) -from ctypes.wintypes import DWORD -import logging -from win32 import win32api -from win32 import win32process -from win32 import win32security - -kernel32 = windll.kernel32 -EFI_VAR_MAX_BUFFER_SIZE = 1024 * 1024 - - -class UefiVariable(object): - ERROR_ENVVAR_NOT_FOUND = 0xcb - - def __init__(self): - # enable required SeSystemEnvironmentPrivilege privilege - privilege = win32security.LookupPrivilegeValue( - None, "SeSystemEnvironmentPrivilege" - ) - token = win32security.OpenProcessToken( - win32process.GetCurrentProcess(), - win32security.TOKEN_READ | win32security.TOKEN_ADJUST_PRIVILEGES, - ) - win32security.AdjustTokenPrivileges( - token, False, [(privilege, win32security.SE_PRIVILEGE_ENABLED)] - ) - win32api.CloseHandle(token) - - # import firmware variable API - try: - self._GetFirmwareEnvironmentVariable = ( - kernel32.GetFirmwareEnvironmentVariableW - ) - self._GetFirmwareEnvironmentVariable.restype = c_int - self._GetFirmwareEnvironmentVariable.argtypes = [ - c_wchar_p, - c_wchar_p, - c_void_p, - c_int, - ] - self._EnumerateFirmwareEnvironmentVariable = ( - windll.ntdll.NtEnumerateSystemEnvironmentValuesEx - ) - self._EnumerateFirmwareEnvironmentVariable.restype = c_int - self._EnumerateFirmwareEnvironmentVariable.argtypes = [ - c_int, - c_void_p, - c_void_p - ] - self._SetFirmwareEnvironmentVariable = ( - kernel32.SetFirmwareEnvironmentVariableW - ) - self._SetFirmwareEnvironmentVariable.restype = c_int - self._SetFirmwareEnvironmentVariable.argtypes = [ - c_wchar_p, - c_wchar_p, - c_void_p, - c_int, - ] - self._SetFirmwareEnvironmentVariableEx = ( - kernel32.SetFirmwareEnvironmentVariableExW - ) - self._SetFirmwareEnvironmentVariableEx.restype = c_int - self._SetFirmwareEnvironmentVariableEx.argtypes = [ - c_wchar_p, - c_wchar_p, - c_void_p, - c_int, - c_int, - ] - except Exception: - logging.warn( - "G[S]etFirmwareEnvironmentVariableW function doesn't seem to exist" - ) - pass - - # - # Function to get variable - # return a tuple of error code and variable data as string - # - def GetUefiVar(self, name, guid): - # success - err = 0 - efi_var = create_string_buffer(EFI_VAR_MAX_BUFFER_SIZE) - if self._GetFirmwareEnvironmentVariable is not None: - logging.info( - "calling GetFirmwareEnvironmentVariable( name='%s', GUID='%s' ).." - % (name, "{%s}" % guid) - ) - length = self._GetFirmwareEnvironmentVariable( - name, "{%s}" % guid, efi_var, EFI_VAR_MAX_BUFFER_SIZE - ) - if (0 == length) or (efi_var is None): - err = kernel32.GetLastError() - if err != 0 and err != UefiVariable.ERROR_ENVVAR_NOT_FOUND: - logging.error( - "GetFirmwareEnvironmentVariable[Ex] failed (GetLastError = 0x%x)" % err - ) - logging.error(WinError()) - if efi_var is None: - return (err, None) - return (err, efi_var[:length]) - - # - # Function to get all variable names - # return a tuple of error code and variable names byte array formatted as: - # - # typedef struct _VARIABLE_NAME { - # ULONG NextEntryOffset; - # GUID VendorGuid; - # WCHAR Name[ANYSIZE_ARRAY]; - # } VARIABLE_NAME, *PVARIABLE_NAME; - # - def GetUefiAllVarNames(self): - # From NTSTATUS definition - STATUS_BUFFER_TOO_SMALL = 0xC0000023 - VARIABLE_INFORMATION_NAMES = 1 - # success - length = DWORD(0) - efi_var_names = create_string_buffer(length.value) - if self._EnumerateFirmwareEnvironmentVariable is not None: - logging.info( - "calling _EnumerateFirmwareEnvironmentVariable to get size.." - ) - status = self._EnumerateFirmwareEnvironmentVariable( - VARIABLE_INFORMATION_NAMES, efi_var_names, pointer(length) - ) - # Only inspect the lower 32bit. - status = (0xFFFFFFFF & status) - if status == STATUS_BUFFER_TOO_SMALL: - logging.info( - "calling _EnumerateFirmwareEnvironmentVariable again to get data.." - ) - efi_var_names = create_string_buffer(length.value) - status = self._EnumerateFirmwareEnvironmentVariable( - VARIABLE_INFORMATION_NAMES, efi_var_names, pointer(length) - ) - if (0 != status): - logging.error( - "EnumerateFirmwareEnvironmentVariable failed (GetLastError = 0x%x)" % status - ) - return (status, None) - return (status, efi_var_names) - - # - # Function to set variable - # return a tuple of boolean status, error_code, error_string (None if not error) - # - def SetUefiVar(self, name, guid, var=None, attrs=None): - var_len = 0 - err = 0 - if var is None: - var = bytes(0) - else: - var_len = len(var) - success = 0 # Fail - if attrs is None: - if self._SetFirmwareEnvironmentVariable is not None: - logging.info( - "Calling SetFirmwareEnvironmentVariable (name='%s', Guid='%s')..." - % ( - name, - "{%s}" % guid, - ) - ) - success = self._SetFirmwareEnvironmentVariable( - name, "{%s}" % guid, var, var_len - ) - else: - attrs = int(attrs) - if self._SetFirmwareEnvironmentVariableEx is not None: - logging.info( - "Calling SetFirmwareEnvironmentVariableEx( name='%s', GUID='%s', length=0x%X, attributes=0x%X ).." - % (name, "{%s}" % guid, var_len, attrs) - ) - success = self._SetFirmwareEnvironmentVariableEx( - name, "{%s}" % guid, var, var_len, attrs - ) - - if 0 == success: - err = kernel32.GetLastError() - logging.error( - "SetFirmwareEnvironmentVariable failed (GetLastError = 0x%x)" % err - ) - logging.error(WinError()) - return success diff --git a/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLinuxLib.py b/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLinuxLib.py deleted file mode 100644 index 0558b6cd..00000000 --- a/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLinuxLib.py +++ /dev/null @@ -1,143 +0,0 @@ -# @file -# -# Python lib to support Reading and writing UEFI variables from Linux -# -# -# Copyright (c), Microsoft Corporation -# SPDX-License-Identifier: BSD-2-Clause-Patent -# -# GetUefiAllVarNames is based on information from -# https://github.com/awslabs/python-uefivars/blob/main/pyuefivars/efivarfs.py -# -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: MIT - -import os -import uuid -import struct - -from ctypes import ( - create_string_buffer -) - -EFI_VAR_MAX_BUFFER_SIZE = 1024 * 1024 - - -class UefiVariable(object): - ERROR_ENVVAR_NOT_FOUND = 0xcb - - def __init__(self): - pass - - # - # Function to get variable - # return a tuple of error code and variable data as string - # - def GetUefiVar(self, name, guid): - # success - err = 0 - # the variable name is VariableName-Guid - path = '/sys/firmware/efi/efivars/' + name + '-%s' % guid - - if not os.path.exists(path): - err = UefiVariable.ERROR_ENVVAR_NOT_FOUND - return (err, None) - - efi_var = create_string_buffer(EFI_VAR_MAX_BUFFER_SIZE) - with open(path, 'rb') as fd: - efi_var = fd.read() - - return (err, efi_var) - - # - # Function to get all variable names - # return a tuple of error code and variable names byte array formatted as: - # - # typedef struct _VARIABLE_NAME { - # ULONG NextEntryOffset; - # GUID VendorGuid; - # WCHAR Name[ANYSIZE_ARRAY]; - # } VARIABLE_NAME, *PVARIABLE_NAME; - # - def GetUefiAllVarNames(self): - # success - status = 0 - - # implementation borrowed from https://github.com/awslabs/python-uefivars/blob/main/pyuefivars/efivarfs.py - path = '/sys/firmware/efi/efivars' - if not os.path.exists(path): - status = UefiVariable.ERROR_ENVVAR_NOT_FOUND - return (status, None) - - vars = os.listdir(path) - - # get the total buffer length, converting to unicode - length = 0 - offset = 0 - for var in vars: - split_string = var.split('-') - name = '-'.join(split_string[:-5]) - name = name.encode('utf-16-le') - name_len = len(name) - length += (4 + 16 + name_len) - - efi_var_names = create_string_buffer(length) - - for var in vars: - # efivarfs stores vars as NAME-GUID - split_string = var.split('-') - try: - # GUID is last 5 elements of split_string - guid = uuid.UUID('-'.join(split_string[-5:])).bytes_le - except ValueError: - raise Exception(f'Could not parse "{var}"') - - # the other part is the name - name = '-'.join(split_string[:-5]) - name = name.encode('utf-16-le') - name_len = len(name) - - # NextEntryOffset - struct.pack_into('