diff --git a/edk2toollib/uefi/edk2/variable_policy.py b/edk2toollib/uefi/edk2/variable_policy.py new file mode 100644 index 00000000..4b04ec08 --- /dev/null +++ b/edk2toollib/uefi/edk2/variable_policy.py @@ -0,0 +1,188 @@ +# @file +# Module contains helper classes and functions to work with Variable Policy structures +# and substructures. +# +# Copyright (c) Microsoft Corporation +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +import uuid +import struct +from edk2toollib.uefi.uefi_multi_phase import EfiVariableAttributes + + +class VariableLockOnVarStatePolicy(object): + # typedef struct { + # EFI_GUID Namespace; + # UINT8 Value; + # UINT8 Reserved; + # // CHAR16 Name[]; // Variable Length Field + # } VARIABLE_LOCK_ON_VAR_STATE_POLICY; + _HdrStructFormat = "<16sBB" + _HdrStructSize = struct.calcsize(_HdrStructFormat) + + def __init__(self): + self.Namespace = uuid.UUID(bytes=b'\x00' * 16) + self.Value = 0 + self.Name = None + + def __str__(self): + return "VARIABLE_LOCK_ON_VAR_STATE_POLICY(%s, %d, %s)" % (self.Namespace, self.Value, self.Name) + + def decode(self, buffer): + """ + load this object from a bytes buffer + + return any remaining buffer + """ + (_namespace, self.Value, _) = struct.unpack( + self._HdrStructFormat, buffer[:self._HdrStructSize]) + + self.Namespace = uuid.UUID(bytes_le=_namespace) + + # Scan the rest of the buffer for a \x00\x00 to terminate the string. + buffer = buffer[self._HdrStructSize:] + if len(buffer) < 4: + raise ValueError("Buffer too short!") + + string_end = None + for i in range(0, len(buffer), 2): + if buffer[i] == 0 and buffer[i + 1] == 0: + string_end = i + 2 + break + + if string_end is None: + raise ValueError("String end not detected!") + + self.Name = buffer[:string_end].decode('utf-16').strip('\x00') + + return buffer[string_end:] + + +class VariablePolicyEntry(object): + # typedef struct { + # UINT32 Version; + # UINT16 Size; + # UINT16 OffsetToName; + # EFI_GUID Namespace; + # UINT32 MinSize; + # UINT32 MaxSize; + # UINT32 AttributesMustHave; + # UINT32 AttributesCantHave; + # UINT8 LockPolicyType; + # UINT8 Reserved[3]; + # // UINT8 LockPolicy[]; // Variable Length Field + # // CHAR16 Name[] // Variable Length Field + # } VARIABLE_POLICY_ENTRY; + _HdrStructFormat = "