2018-04-16 15:52:13 +02:00
# Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
2017-11-24 07:46:58 +01:00
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
# This file is used to collect the Variable checking information
#
# #
# Import Modules
#
2018-06-25 12:31:33 +02:00
from struct import pack , unpack
2017-11-24 07:46:58 +01:00
import collections
import copy
from Common . VariableAttributes import VariableAttributes
from Common . Misc import *
import collections
2018-04-11 01:17:20 +02:00
import Common . DataType as DataType
2017-11-24 07:46:58 +01:00
2018-09-25 04:55:30 +02:00
var_info = collections . namedtuple ( " uefi_var " , " pcdindex,pcdname,defaultstoragename,skuname,var_name, var_guid, var_offset,var_attribute,pcd_default_value, default_value, data_type,PcdDscLine,StructurePcd " )
2017-11-24 07:46:58 +01:00
NvStorageHeaderSize = 28
VariableHeaderSize = 32
2017-12-22 13:46:15 +01:00
class VariableMgr ( object ) :
2018-06-25 12:31:33 +02:00
def __init__ ( self , DefaultStoreMap , SkuIdMap ) :
2017-11-24 07:46:58 +01:00
self . VarInfo = [ ]
2017-12-22 13:46:15 +01:00
self . DefaultStoreMap = DefaultStoreMap
self . SkuIdMap = SkuIdMap
2017-12-22 13:14:29 +01:00
self . VpdRegionSize = 0
self . VpdRegionOffset = 0
self . NVHeaderBuff = None
self . VarDefaultBuff = None
self . VarDeltaBuff = None
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
def append_variable ( self , uefi_var ) :
2017-11-24 07:46:58 +01:00
self . VarInfo . append ( uefi_var )
2018-06-25 12:31:33 +02:00
def SetVpdRegionMaxSize ( self , maxsize ) :
2017-12-22 13:14:29 +01:00
self . VpdRegionSize = maxsize
2018-06-25 12:31:33 +02:00
def SetVpdRegionOffset ( self , vpdoffset ) :
2017-12-22 13:14:29 +01:00
self . VpdRegionOffset = vpdoffset
2018-06-25 12:31:33 +02:00
def PatchNVStoreDefaultMaxSize ( self , maxsize ) :
2017-12-22 13:14:29 +01:00
if not self . NVHeaderBuff :
return " "
2018-06-25 12:31:33 +02:00
self . NVHeaderBuff = self . NVHeaderBuff [ : 8 ] + pack ( " =Q " , maxsize )
2018-04-28 00:32:21 +02:00
default_var_bin = VariableMgr . format_data ( self . NVHeaderBuff + self . VarDefaultBuff + self . VarDeltaBuff )
2017-12-22 13:14:29 +01:00
value_str = " { "
default_var_bin_strip = [ data . strip ( """ ' """ ) for data in default_var_bin ]
value_str + = " , " . join ( default_var_bin_strip )
value_str + = " } "
return value_str
2018-09-25 04:55:30 +02:00
def Do_combine ( self , sku_var_info_offset_list ) :
newvalue = { }
for item in sku_var_info_offset_list :
data_type = item . data_type
value_list = item . default_value . strip ( " { " ) . strip ( " } " ) . split ( " , " )
if data_type in DataType . TAB_PCD_NUMERIC_TYPES :
data_flag = DataType . PACK_CODE_BY_SIZE [ MAX_SIZE_TYPE [ data_type ] ]
data = value_list [ 0 ]
value_list = [ ]
for data_byte in pack ( data_flag , int ( data , 16 ) if data . upper ( ) . startswith ( ' 0X ' ) else int ( data ) ) :
2018-10-15 02:27:53 +02:00
value_list . append ( hex ( unpack ( " B " , data_byte ) [ 0 ] ) )
2018-09-25 04:55:30 +02:00
newvalue [ int ( item . var_offset , 16 ) if item . var_offset . upper ( ) . startswith ( " 0X " ) else int ( item . var_offset ) ] = value_list
try :
newvaluestr = " { " + " , " . join ( VariableMgr . assemble_variable ( newvalue ) ) + " } "
except :
EdkLogger . error ( " build " , AUTOGEN_ERROR , " Variable offset conflict in PCDs: %s \n " % ( " and " . join ( item . pcdname for item in sku_var_info_offset_list ) ) )
return newvaluestr
def Do_Merge ( self , sku_var_info_offset_list ) :
StructrurePcds = sorted ( [ item for item in sku_var_info_offset_list if item . StructurePcd ] , key = lambda x : x . PcdDscLine , reverse = True )
Base = StructrurePcds [ 0 ]
BaseValue = Base . default_value . strip ( " { " ) . strip ( " } " ) . split ( " , " )
Override = [ item for item in sku_var_info_offset_list if not item . StructurePcd and item . PcdDscLine > Base . PcdDscLine ]
newvalue = { }
for item in Override :
data_type = item . data_type
value_list = item . default_value . strip ( " { " ) . strip ( " } " ) . split ( " , " )
if data_type in DataType . TAB_PCD_NUMERIC_TYPES :
data_flag = DataType . PACK_CODE_BY_SIZE [ MAX_SIZE_TYPE [ data_type ] ]
data = value_list [ 0 ]
value_list = [ ]
for data_byte in pack ( data_flag , int ( data , 16 ) if data . upper ( ) . startswith ( ' 0X ' ) else int ( data ) ) :
2018-10-15 02:27:53 +02:00
value_list . append ( hex ( unpack ( " B " , data_byte ) [ 0 ] ) )
2018-09-25 04:55:30 +02:00
newvalue [ int ( item . var_offset , 16 ) if item . var_offset . upper ( ) . startswith ( " 0X " ) else int ( item . var_offset ) ] = ( value_list , item . pcdname , item . PcdDscLine )
for offset in newvalue :
value_list , itemPcdname , itemPcdDscLine = newvalue [ offset ]
if offset > len ( BaseValue ) or ( offset + len ( value_list ) > len ( BaseValue ) ) :
EdkLogger . error ( " build " , AUTOGEN_ERROR , " The EFI Variable referred by PCD %s in line %s exceeds variable size: %s \n " % ( itemPcdname , itemPcdDscLine , hex ( len ( BaseValue ) ) ) )
2018-10-15 02:27:53 +02:00
for i in xrange ( len ( value_list ) ) :
2018-09-25 04:55:30 +02:00
BaseValue [ offset + i ] = value_list [ i ]
newvaluestr = " { " + " , " . join ( BaseValue ) + " } "
return newvaluestr
def NeedMerge ( self , sku_var_info_offset_list ) :
if [ item for item in sku_var_info_offset_list if item . StructurePcd ] :
return True
return False
2018-01-23 03:18:53 +01:00
def combine_variable ( self ) :
indexedvarinfo = collections . OrderedDict ( )
for item in self . VarInfo :
2018-06-25 12:31:33 +02:00
if ( item . skuname , item . defaultstoragename , item . var_name , item . var_guid ) not in indexedvarinfo :
indexedvarinfo [ ( item . skuname , item . defaultstoragename , item . var_name , item . var_guid ) ] = [ ]
indexedvarinfo [ ( item . skuname , item . defaultstoragename , item . var_name , item . var_guid ) ] . append ( item )
2018-01-23 03:18:53 +01:00
for key in indexedvarinfo :
sku_var_info_offset_list = indexedvarinfo [ key ]
if len ( sku_var_info_offset_list ) == 1 :
continue
2018-09-25 04:55:30 +02:00
2018-01-23 03:18:53 +01:00
n = sku_var_info_offset_list [ 0 ]
2018-09-25 04:55:30 +02:00
if self . NeedMerge ( sku_var_info_offset_list ) :
newvaluestr = self . Do_Merge ( sku_var_info_offset_list )
else :
newvaluestr = self . Do_combine ( sku_var_info_offset_list )
indexedvarinfo [ key ] = [ var_info ( n . pcdindex , n . pcdname , n . defaultstoragename , n . skuname , n . var_name , n . var_guid , " 0x00 " , n . var_attribute , newvaluestr , newvaluestr , DataType . TAB_VOID , n . PcdDscLine , n . StructurePcd ) ]
2018-01-23 03:18:53 +01:00
self . VarInfo = [ item [ 0 ] for item in indexedvarinfo . values ( ) ]
2018-02-07 03:01:59 +01:00
2018-04-28 00:32:19 +02:00
@staticmethod
def assemble_variable ( valuedict ) :
2018-04-28 00:32:42 +02:00
ordered_valuedict_keys = sorted ( valuedict . keys ( ) )
2018-02-07 03:01:59 +01:00
var_value = [ ]
2018-04-28 00:32:42 +02:00
for current_valuedict_key in ordered_valuedict_keys :
if current_valuedict_key < len ( var_value ) :
2018-02-07 03:01:59 +01:00
raise
2018-10-15 02:27:53 +02:00
for _ in xrange ( current_valuedict_key - len ( var_value ) ) :
2018-02-07 03:01:59 +01:00
var_value . append ( ' 0x00 ' )
2018-04-28 00:32:42 +02:00
var_value + = valuedict [ current_valuedict_key ]
2018-02-07 03:01:59 +01:00
return var_value
2018-04-28 00:32:19 +02:00
2017-11-24 07:46:58 +01:00
def process_variable_data ( self ) :
2018-04-20 17:51:45 +02:00
var_data = collections . defaultdict ( collections . OrderedDict )
2017-11-24 07:46:58 +01:00
indexedvarinfo = collections . OrderedDict ( )
for item in self . VarInfo :
if item . pcdindex not in indexedvarinfo :
indexedvarinfo [ item . pcdindex ] = dict ( )
2018-06-25 12:31:33 +02:00
indexedvarinfo [ item . pcdindex ] [ ( item . skuname , item . defaultstoragename ) ] = item
2017-11-24 07:46:58 +01:00
for index in indexedvarinfo :
sku_var_info = indexedvarinfo [ index ]
default_data_buffer = " "
others_data_buffer = " "
tail = None
2018-06-25 12:31:33 +02:00
default_sku_default = indexedvarinfo [ index ] . get ( ( DataType . TAB_DEFAULT , DataType . TAB_DEFAULT_STORES_DEFAULT ) )
2017-11-24 07:46:58 +01:00
2018-04-11 18:14:05 +02:00
if default_sku_default . data_type not in DataType . TAB_PCD_NUMERIC_TYPES :
2018-04-28 00:32:54 +02:00
var_max_len = max ( len ( var_item . default_value . split ( " , " ) ) for var_item in sku_var_info . values ( ) )
2017-11-24 07:46:58 +01:00
if len ( default_sku_default . default_value . split ( " , " ) ) < var_max_len :
2018-04-28 00:32:54 +02:00
tail = " , " . join ( " 0x00 " for i in range ( var_max_len - len ( default_sku_default . default_value . split ( " , " ) ) ) )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
default_data_buffer = VariableMgr . PACK_VARIABLES_DATA ( default_sku_default . default_value , default_sku_default . data_type , tail )
2017-11-24 07:46:58 +01:00
default_data_array = ( )
for item in default_data_buffer :
2018-10-15 02:27:53 +02:00
default_data_array + = unpack ( " B " , item )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
var_data [ ( DataType . TAB_DEFAULT , DataType . TAB_DEFAULT_STORES_DEFAULT ) ] [ index ] = ( default_data_buffer , sku_var_info [ ( DataType . TAB_DEFAULT , DataType . TAB_DEFAULT_STORES_DEFAULT ) ] )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
for ( skuid , defaultstoragename ) in indexedvarinfo [ index ] :
2017-11-24 07:46:58 +01:00
tail = None
2018-06-25 12:31:33 +02:00
if ( skuid , defaultstoragename ) == ( DataType . TAB_DEFAULT , DataType . TAB_DEFAULT_STORES_DEFAULT ) :
2017-11-24 07:46:58 +01:00
continue
2018-06-25 12:31:33 +02:00
other_sku_other = indexedvarinfo [ index ] [ ( skuid , defaultstoragename ) ]
2017-11-24 07:46:58 +01:00
2018-04-11 18:14:05 +02:00
if default_sku_default . data_type not in DataType . TAB_PCD_NUMERIC_TYPES :
2017-11-24 07:46:58 +01:00
if len ( other_sku_other . default_value . split ( " , " ) ) < var_max_len :
2018-04-28 00:32:54 +02:00
tail = " , " . join ( " 0x00 " for i in range ( var_max_len - len ( other_sku_other . default_value . split ( " , " ) ) ) )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
others_data_buffer = VariableMgr . PACK_VARIABLES_DATA ( other_sku_other . default_value , other_sku_other . data_type , tail )
2017-11-24 07:46:58 +01:00
others_data_array = ( )
for item in others_data_buffer :
2018-10-15 02:27:53 +02:00
others_data_array + = unpack ( " B " , item )
2017-11-24 07:46:58 +01:00
2018-04-28 00:32:21 +02:00
data_delta = VariableMgr . calculate_delta ( default_data_array , others_data_array )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
var_data [ ( skuid , defaultstoragename ) ] [ index ] = ( data_delta , sku_var_info [ ( skuid , defaultstoragename ) ] )
2017-11-24 07:46:58 +01:00
return var_data
def new_process_varinfo ( self ) :
2018-01-23 03:18:53 +01:00
self . combine_variable ( )
2017-11-24 07:46:58 +01:00
var_data = self . process_variable_data ( )
2017-12-22 13:04:04 +01:00
if not var_data :
return [ ]
2018-06-25 12:31:33 +02:00
pcds_default_data = var_data . get ( ( DataType . TAB_DEFAULT , DataType . TAB_DEFAULT_STORES_DEFAULT ) , { } )
2018-10-15 02:27:53 +02:00
NvStoreDataBuffer = " "
2017-11-24 07:46:58 +01:00
var_data_offset = collections . OrderedDict ( )
offset = NvStorageHeaderSize
2018-06-25 12:31:33 +02:00
for default_data , default_info in pcds_default_data . values ( ) :
2018-04-28 00:32:21 +02:00
var_name_buffer = VariableMgr . PACK_VARIABLE_NAME ( default_info . var_name )
2017-11-24 07:46:58 +01:00
vendorguid = default_info . var_guid . split ( ' - ' )
if default_info . var_attribute :
2018-06-25 12:31:33 +02:00
var_attr_value , _ = VariableAttributes . GetVarAttributes ( default_info . var_attribute )
2017-11-24 07:46:58 +01:00
else :
var_attr_value = 0x07
2018-04-28 00:32:21 +02:00
DataBuffer = VariableMgr . AlignData ( var_name_buffer + default_data )
2017-11-24 07:46:58 +01:00
data_size = len ( DataBuffer )
offset + = VariableHeaderSize + len ( default_info . var_name . split ( " , " ) )
var_data_offset [ default_info . pcdindex ] = offset
offset + = data_size - len ( default_info . var_name . split ( " , " ) )
2018-04-28 00:32:21 +02:00
var_header_buffer = VariableMgr . PACK_VARIABLE_HEADER ( var_attr_value , len ( default_info . var_name . split ( " , " ) ) , len ( default_data ) , vendorguid )
2017-11-24 07:46:58 +01:00
NvStoreDataBuffer + = ( var_header_buffer + DataBuffer )
2018-04-28 00:32:21 +02:00
variable_storage_header_buffer = VariableMgr . PACK_VARIABLE_STORE_HEADER ( len ( NvStoreDataBuffer ) + 28 )
2017-11-24 07:46:58 +01:00
2018-04-28 00:32:21 +02:00
nv_default_part = VariableMgr . AlignData ( VariableMgr . PACK_DEFAULT_DATA ( 0 , 0 , VariableMgr . unpack_data ( variable_storage_header_buffer + NvStoreDataBuffer ) ) , 8 )
2017-11-24 07:46:58 +01:00
2018-10-15 02:27:53 +02:00
data_delta_structure_buffer = " "
2018-06-25 12:31:33 +02:00
for skuname , defaultstore in var_data :
if ( skuname , defaultstore ) == ( DataType . TAB_DEFAULT , DataType . TAB_DEFAULT_STORES_DEFAULT ) :
2017-11-24 07:46:58 +01:00
continue
2018-06-25 12:31:33 +02:00
pcds_sku_data = var_data [ ( skuname , defaultstore ) ]
2017-11-24 07:46:58 +01:00
delta_data_set = [ ]
for pcdindex in pcds_sku_data :
offset = var_data_offset [ pcdindex ]
2018-06-25 12:31:33 +02:00
delta_data , _ = pcds_sku_data [ pcdindex ]
2017-11-24 07:46:58 +01:00
delta_data = [ ( item [ 0 ] + offset , item [ 1 ] ) for item in delta_data ]
delta_data_set . extend ( delta_data )
2018-06-25 12:31:33 +02:00
data_delta_structure_buffer + = VariableMgr . AlignData ( self . PACK_DELTA_DATA ( skuname , defaultstore , delta_data_set ) , 8 )
2017-11-24 07:46:58 +01:00
2017-12-20 10:16:28 +01:00
size = len ( nv_default_part + data_delta_structure_buffer ) + 16
2017-12-22 13:14:29 +01:00
maxsize = self . VpdRegionSize if self . VpdRegionSize else size
2018-06-25 12:31:33 +02:00
NV_Store_Default_Header = VariableMgr . PACK_NV_STORE_DEFAULT_HEADER ( size , maxsize )
2017-12-22 13:14:29 +01:00
self . NVHeaderBuff = NV_Store_Default_Header
self . VarDefaultBuff = nv_default_part
self . VarDeltaBuff = data_delta_structure_buffer
2018-04-28 00:32:21 +02:00
return VariableMgr . format_data ( NV_Store_Default_Header + nv_default_part + data_delta_structure_buffer )
2017-11-24 07:46:58 +01:00
2018-04-28 00:32:21 +02:00
@staticmethod
def format_data ( data ) :
return [ hex ( item ) for item in VariableMgr . unpack_data ( data ) ]
2017-11-24 07:46:58 +01:00
2018-04-28 00:32:21 +02:00
@staticmethod
def unpack_data ( data ) :
2017-11-24 07:46:58 +01:00
final_data = ( )
for item in data :
2018-10-15 02:27:53 +02:00
final_data + = unpack ( " B " , item )
2017-11-24 07:46:58 +01:00
return final_data
2018-04-28 00:32:21 +02:00
@staticmethod
def calculate_delta ( default , theother ) :
2017-11-24 07:46:58 +01:00
if len ( default ) - len ( theother ) != 0 :
EdkLogger . error ( " build " , FORMAT_INVALID , ' The variable data length is not the same for the same PCD. ' )
data_delta = [ ]
for i in range ( len ( default ) ) :
if default [ i ] != theother [ i ] :
2018-06-25 12:31:33 +02:00
data_delta . append ( ( i , theother [ i ] ) )
2017-11-24 07:46:58 +01:00
return data_delta
def dump ( self ) :
default_var_bin = self . new_process_varinfo ( )
2017-12-22 13:04:04 +01:00
if default_var_bin :
value_str = " { "
default_var_bin_strip = [ data . strip ( """ ' """ ) for data in default_var_bin ]
value_str + = " , " . join ( default_var_bin_strip )
value_str + = " } "
return value_str
return " "
2017-11-24 07:46:58 +01:00
2018-04-28 00:32:21 +02:00
@staticmethod
def PACK_VARIABLE_STORE_HEADER ( size ) :
2017-11-24 07:46:58 +01:00
#Signature: gEfiVariableGuid
Guid = " { 0xddcf3616, 0x3275, 0x4164, { 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d }} "
Guid = GuidStructureStringToGuidString ( Guid )
GuidBuffer = PackGUID ( Guid . split ( ' - ' ) )
2018-06-25 12:31:33 +02:00
SizeBuffer = pack ( ' =L ' , size )
FormatBuffer = pack ( ' =B ' , 0x5A )
StateBuffer = pack ( ' =B ' , 0xFE )
reservedBuffer = pack ( ' =H ' , 0 )
reservedBuffer + = pack ( ' =L ' , 0 )
2017-11-24 07:46:58 +01:00
return GuidBuffer + SizeBuffer + FormatBuffer + StateBuffer + reservedBuffer
2018-04-28 00:32:21 +02:00
@staticmethod
2018-06-25 12:31:33 +02:00
def PACK_NV_STORE_DEFAULT_HEADER ( size , maxsize ) :
Signature = pack ( ' =B ' , ord ( ' N ' ) )
Signature + = pack ( " =B " , ord ( ' S ' ) )
Signature + = pack ( " =B " , ord ( ' D ' ) )
Signature + = pack ( " =B " , ord ( ' B ' ) )
2017-12-22 13:14:29 +01:00
2018-06-25 12:31:33 +02:00
SizeBuffer = pack ( " =L " , size )
MaxSizeBuffer = pack ( " =Q " , maxsize )
2017-12-22 13:14:29 +01:00
return Signature + SizeBuffer + MaxSizeBuffer
2017-11-24 07:46:58 +01:00
2018-04-28 00:32:21 +02:00
@staticmethod
2018-06-25 12:31:33 +02:00
def PACK_VARIABLE_HEADER ( attribute , namesize , datasize , vendorguid ) :
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
Buffer = pack ( ' =H ' , 0x55AA ) # pack StartID
Buffer + = pack ( ' =B ' , 0x3F ) # pack State
Buffer + = pack ( ' =B ' , 0 ) # pack reserved
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
Buffer + = pack ( ' =L ' , attribute )
Buffer + = pack ( ' =L ' , namesize )
Buffer + = pack ( ' =L ' , datasize )
2017-11-24 07:46:58 +01:00
Buffer + = PackGUID ( vendorguid )
return Buffer
2018-04-28 00:32:21 +02:00
@staticmethod
def PACK_VARIABLES_DATA ( var_value , data_type , tail = None ) :
2018-10-15 02:27:53 +02:00
Buffer = " "
2017-11-24 07:46:58 +01:00
data_len = 0
2018-04-11 18:14:05 +02:00
if data_type == DataType . TAB_VOID :
2017-11-24 07:46:58 +01:00
for value_char in var_value . strip ( " { " ) . strip ( " } " ) . split ( " , " ) :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =B " , int ( value_char , 16 ) )
2017-11-24 07:46:58 +01:00
data_len + = len ( var_value . split ( " , " ) )
if tail :
for value_char in tail . split ( " , " ) :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =B " , int ( value_char , 16 ) )
2017-11-24 07:46:58 +01:00
data_len + = len ( tail . split ( " , " ) )
elif data_type == " BOOLEAN " :
2018-08-10 10:12:26 +02:00
Buffer + = pack ( " =B " , True ) if var_value . upper ( ) in [ " TRUE " , " 1 " ] else pack ( " =B " , False )
2017-11-24 07:46:58 +01:00
data_len + = 1
2018-04-11 18:14:05 +02:00
elif data_type == DataType . TAB_UINT8 :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =B " , GetIntegerValue ( var_value ) )
2017-11-24 07:46:58 +01:00
data_len + = 1
2018-04-11 18:14:05 +02:00
elif data_type == DataType . TAB_UINT16 :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =H " , GetIntegerValue ( var_value ) )
2017-11-24 07:46:58 +01:00
data_len + = 2
2018-04-11 18:14:05 +02:00
elif data_type == DataType . TAB_UINT32 :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =L " , GetIntegerValue ( var_value ) )
2017-11-24 07:46:58 +01:00
data_len + = 4
2018-04-11 18:14:05 +02:00
elif data_type == DataType . TAB_UINT64 :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =Q " , GetIntegerValue ( var_value ) )
2017-11-24 07:46:58 +01:00
data_len + = 8
return Buffer
2018-04-28 00:32:21 +02:00
@staticmethod
2018-06-25 12:31:33 +02:00
def PACK_DEFAULT_DATA ( defaultstoragename , skuid , var_value ) :
2018-10-15 02:27:53 +02:00
Buffer = " "
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =L " , 4 + 8 + 8 )
Buffer + = pack ( " =Q " , int ( skuid ) )
Buffer + = pack ( " =Q " , int ( defaultstoragename ) )
2017-11-24 07:46:58 +01:00
for item in var_value :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =B " , item )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
Buffer = pack ( " =L " , len ( Buffer ) + 4 ) + Buffer
2017-11-24 07:46:58 +01:00
return Buffer
2018-06-25 12:31:33 +02:00
def GetSkuId ( self , skuname ) :
2017-12-22 13:46:15 +01:00
if skuname not in self . SkuIdMap :
return None
return self . SkuIdMap . get ( skuname ) [ 0 ]
2018-04-28 00:32:21 +02:00
2018-06-25 12:31:33 +02:00
def GetDefaultStoreId ( self , dname ) :
2017-12-22 13:46:15 +01:00
if dname not in self . DefaultStoreMap :
return None
return self . DefaultStoreMap . get ( dname ) [ 0 ]
2018-04-28 00:32:21 +02:00
2018-06-25 12:31:33 +02:00
def PACK_DELTA_DATA ( self , skuname , defaultstoragename , delta_list ) :
2017-12-22 13:46:15 +01:00
skuid = self . GetSkuId ( skuname )
defaultstorageid = self . GetDefaultStoreId ( defaultstoragename )
2018-10-15 02:27:53 +02:00
Buffer = " "
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =L " , 4 + 8 + 8 )
Buffer + = pack ( " =Q " , int ( skuid ) )
Buffer + = pack ( " =Q " , int ( defaultstorageid ) )
for ( delta_offset , value ) in delta_list :
Buffer + = pack ( " =L " , delta_offset )
Buffer = Buffer [ : - 1 ] + pack ( " =B " , value )
2017-11-24 07:46:58 +01:00
2018-06-25 12:31:33 +02:00
Buffer = pack ( " =L " , len ( Buffer ) + 4 ) + Buffer
2017-11-24 07:46:58 +01:00
return Buffer
2018-04-28 00:32:21 +02:00
@staticmethod
def AlignData ( data , align = 4 ) :
2017-11-24 07:46:58 +01:00
mybuffer = data
2017-12-20 10:16:28 +01:00
if ( len ( data ) % align ) > 0 :
for i in range ( align - ( len ( data ) % align ) ) :
2018-06-25 12:31:33 +02:00
mybuffer + = pack ( " =B " , 0 )
2017-11-24 07:46:58 +01:00
return mybuffer
2018-04-28 00:32:21 +02:00
@staticmethod
def PACK_VARIABLE_NAME ( var_name ) :
2018-10-15 02:27:53 +02:00
Buffer = " "
2017-11-24 07:46:58 +01:00
for name_char in var_name . strip ( " { " ) . strip ( " } " ) . split ( " , " ) :
2018-06-25 12:31:33 +02:00
Buffer + = pack ( " =B " , int ( name_char , 16 ) )
2017-11-24 07:46:58 +01:00
2017-12-22 13:46:15 +01:00
return Buffer