BaseTools: PcdDataBase Optimization for multiple SkuIds
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenVar.py
1 # Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
2 # This program and the accompanying materials
3 # are licensed and made available under the terms and conditions of the BSD License
4 # which accompanies this distribution. The full text of the license may be found at
5 # http://opensource.org/licenses/bsd-license.php
6 #
7 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
8 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
9
10 #
11 # This file is used to collect the Variable checking information
12 #
13
14 # #
15 # Import Modules
16 #
17 from struct import pack,unpack
18 import collections
19 import copy
20 from Common.VariableAttributes import VariableAttributes
21 from Common.Misc import *
22 import collections
23
24 var_info = collections.namedtuple("uefi_var", "pcdindex,pcdname,defaultstoragename,skuname,var_name, var_guid, var_attribute,pcd_default_value, default_value, data_type")
25 NvStorageHeaderSize = 28
26 VariableHeaderSize = 32
27
28 def StringArrayToList(StringArray):
29 StringArray = StringArray[1:-1]
30 StringArray = '[' + StringArray + ']'
31 return eval(StringArray)
32
33 def PackGUID(Guid):
34 GuidBuffer = pack('=LHHBBBBBBBB',
35 int(Guid[0], 16),
36 int(Guid[1], 16),
37 int(Guid[2], 16),
38 int(Guid[3][-4:-2], 16),
39 int(Guid[3][-2:], 16),
40 int(Guid[4][-12:-10], 16),
41 int(Guid[4][-10:-8], 16),
42 int(Guid[4][-8:-6], 16),
43 int(Guid[4][-6:-4], 16),
44 int(Guid[4][-4:-2], 16),
45 int(Guid[4][-2:], 16)
46 )
47 return GuidBuffer
48
49 class VariableMgr(object):
50 def __init__(self, DefaultStoreMap,SkuIdMap):
51 self.VarInfo = []
52 self.DefaultStoreMap = DefaultStoreMap
53 self.SkuIdMap = SkuIdMap
54
55 def append_variable(self,uefi_var):
56 self.VarInfo.append(uefi_var)
57
58 def process_variable_data(self):
59
60 var_data = dict()
61
62 indexedvarinfo = collections.OrderedDict()
63 for item in self.VarInfo:
64 if item.pcdindex not in indexedvarinfo:
65 indexedvarinfo[item.pcdindex] = dict()
66 indexedvarinfo[item.pcdindex][(item.skuname,item.defaultstoragename)] = item
67
68 for index in indexedvarinfo:
69 sku_var_info = indexedvarinfo[index]
70
71 default_data_buffer = ""
72 others_data_buffer = ""
73 tail = None
74 default_sku_default = indexedvarinfo.get(index).get(("DEFAULT","STANDARD"))
75
76 if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:
77 var_max_len = max([len(var_item.default_value.split(",")) for var_item in sku_var_info.values()])
78 if len(default_sku_default.default_value.split(",")) < var_max_len:
79 tail = ",".join([ "0x00" for i in range(var_max_len-len(default_sku_default.default_value.split(",")))])
80
81 default_data_buffer = self.PACK_VARIABLES_DATA(default_sku_default.default_value,default_sku_default.data_type,tail)
82
83 default_data_array = ()
84 for item in default_data_buffer:
85 default_data_array += unpack("B",item)
86
87 if ("DEFAULT","STANDARD") not in var_data:
88 var_data[("DEFAULT","STANDARD")] = collections.OrderedDict()
89 var_data[("DEFAULT","STANDARD")][index] = (default_data_buffer,sku_var_info[("DEFAULT","STANDARD")])
90
91 for (skuid,defaultstoragename) in indexedvarinfo.get(index):
92 tail = None
93 if (skuid,defaultstoragename) == ("DEFAULT","STANDARD"):
94 continue
95 other_sku_other = indexedvarinfo.get(index).get((skuid,defaultstoragename))
96
97 if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:
98 if len(other_sku_other.default_value.split(",")) < var_max_len:
99 tail = ",".join([ "0x00" for i in range(var_max_len-len(other_sku_other.default_value.split(",")))])
100
101 others_data_buffer = self.PACK_VARIABLES_DATA(other_sku_other.default_value,other_sku_other.data_type,tail)
102
103 others_data_array = ()
104 for item in others_data_buffer:
105 others_data_array += unpack("B",item)
106
107 data_delta = self.calculate_delta(default_data_array, others_data_array)
108
109 if (skuid,defaultstoragename) not in var_data:
110 var_data[(skuid,defaultstoragename)] = collections.OrderedDict()
111 var_data[(skuid,defaultstoragename)][index] = (data_delta,sku_var_info[(skuid,defaultstoragename)])
112 return var_data
113
114 def new_process_varinfo(self):
115
116 var_data = self.process_variable_data()
117
118 if not var_data:
119 return []
120
121 pcds_default_data = var_data.get(("DEFAULT","STANDARD"))
122 NvStoreDataBuffer = ""
123 var_data_offset = collections.OrderedDict()
124 offset = NvStorageHeaderSize
125 for default_data,default_info in pcds_default_data.values():
126 var_name_buffer = self.PACK_VARIABLE_NAME(default_info.var_name)
127
128 vendorguid = default_info.var_guid.split('-')
129
130 if default_info.var_attribute:
131 var_attr_value,_ = VariableAttributes.GetVarAttributes(default_info.var_attribute)
132 else:
133 var_attr_value = 0x07
134
135 # print "default var_name_buffer"
136 # print self.format_data(var_name_buffer)
137 # print "default var_buffer"
138 # print self.format_data(default_data)
139
140 DataBuffer = self.AlignData(var_name_buffer + default_data)
141
142 data_size = len(DataBuffer)
143 offset += VariableHeaderSize + len(default_info.var_name.split(","))
144 var_data_offset[default_info.pcdindex] = offset
145 offset += data_size - len(default_info.var_name.split(","))
146
147 var_header_buffer = self.PACK_VARIABLE_HEADER(var_attr_value, len(default_info.var_name.split(",")), len (default_data), vendorguid)
148 NvStoreDataBuffer += (var_header_buffer + DataBuffer)
149
150 variable_storage_header_buffer = self.PACK_VARIABLE_STORE_HEADER(len(NvStoreDataBuffer) + 28)
151
152 nv_default_part = self.AlignData(self.PACK_DEFAULT_DATA(0, 0, self.unpack_data(variable_storage_header_buffer+NvStoreDataBuffer)))
153
154 # print "default whole data \n",self.format_data(nv_default_part)
155
156 data_delta_structure_buffer = ""
157 for skuname,defaultstore in var_data:
158 if (skuname,defaultstore) == ("DEFAULT","STANDARD"):
159 continue
160 pcds_sku_data = var_data.get((skuname,defaultstore))
161 delta_data_set = []
162 for pcdindex in pcds_sku_data:
163 offset = var_data_offset[pcdindex]
164 delta_data,_ = pcds_sku_data[pcdindex]
165 delta_data = [(item[0] + offset, item[1]) for item in delta_data]
166 delta_data_set.extend(delta_data)
167
168 data_delta_structure_buffer += self.AlignData(self.PACK_DELTA_DATA(skuname,defaultstore,delta_data_set))
169 # print "delta data"
170 # print delta_data_set
171 # print self.format_data(self.AlignData(self.PACK_DELTA_DATA(skuname,defaultstore,delta_data_set)))
172
173 return self.format_data(nv_default_part + data_delta_structure_buffer)
174
175
176 def format_data(self,data):
177
178 return [hex(item) for item in self.unpack_data(data)]
179
180 def unpack_data(self,data):
181 final_data = ()
182 for item in data:
183 final_data += unpack("B",item)
184 return final_data
185
186 def calculate_delta(self, default, theother):
187 # print "default data \n", default
188 # print "other data \n",theother
189 if len(default) - len(theother) != 0:
190 EdkLogger.error("build", FORMAT_INVALID, 'The variable data length is not the same for the same PCD.')
191 data_delta = []
192 for i in range(len(default)):
193 if default[i] != theother[i]:
194 data_delta.append((i,theother[i]))
195 return data_delta
196
197 def dump(self):
198
199 default_var_bin = self.new_process_varinfo()
200 if default_var_bin:
201 value_str = "{"
202 default_var_bin_strip = [ data.strip("""'""") for data in default_var_bin]
203 value_str += ",".join(default_var_bin_strip)
204 value_str += "}"
205 return value_str
206 return ""
207
208 def PACK_VARIABLE_STORE_HEADER(self,size):
209 #Signature: gEfiVariableGuid
210 Guid = "{ 0xddcf3616, 0x3275, 0x4164, { 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d }}"
211 Guid = GuidStructureStringToGuidString(Guid)
212 GuidBuffer = PackGUID(Guid.split('-'))
213
214 SizeBuffer = pack('=L',size)
215 FormatBuffer = pack('=B',0x5A)
216 StateBuffer = pack('=B',0xFE)
217 reservedBuffer = pack('=H',0)
218 reservedBuffer += pack('=L',0)
219
220 return GuidBuffer + SizeBuffer + FormatBuffer + StateBuffer + reservedBuffer
221
222
223 def PACK_VARIABLE_HEADER(self,attribute,namesize,datasize,vendorguid):
224
225 Buffer = pack('=H',0x55AA) # pack StartID
226 Buffer += pack('=B',0x3F) # pack State
227 Buffer += pack('=B',0) # pack reserved
228
229 Buffer += pack('=L',attribute)
230 Buffer += pack('=L',namesize)
231 Buffer += pack('=L',datasize)
232
233 Buffer += PackGUID(vendorguid)
234
235 return Buffer
236
237 def PACK_VARIABLES_DATA(self, var_value,data_type, tail = None):
238 Buffer = ""
239 data_len = 0
240 if data_type == "VOID*":
241 for value_char in var_value.strip("{").strip("}").split(","):
242 Buffer += pack("=B",int(value_char,16))
243 data_len += len(var_value.split(","))
244 if tail:
245 for value_char in tail.split(","):
246 Buffer += pack("=B",int(value_char,16))
247 data_len += len(tail.split(","))
248 elif data_type == "BOOLEAN":
249 Buffer += pack("=B",True) if var_value.upper() == "TRUE" else pack("=B",False)
250 data_len += 1
251 elif data_type == "UINT8":
252 Buffer += pack("=B",GetIntegerValue(var_value))
253 data_len += 1
254 elif data_type == "UINT16":
255 Buffer += pack("=H",GetIntegerValue(var_value))
256 data_len += 2
257 elif data_type == "UINT32":
258 Buffer += pack("=L",GetIntegerValue(var_value))
259 data_len += 4
260 elif data_type == "UINT64":
261 Buffer += pack("=Q",GetIntegerValue(var_value))
262 data_len += 8
263
264 return Buffer
265
266 def PACK_DEFAULT_DATA(self, defaultstoragename,skuid,var_value):
267 Buffer = ""
268 Buffer += pack("=L",8)
269 Buffer += pack("=H",int(defaultstoragename))
270 Buffer += pack("=H",int(skuid))
271
272 for item in var_value:
273 Buffer += pack("=B",item)
274
275 Buffer = pack("=L",len(Buffer)+4) + Buffer
276
277 return Buffer
278
279 def GetSkuId(self,skuname):
280 if skuname not in self.SkuIdMap:
281 return None
282 return self.SkuIdMap.get(skuname)[0]
283 def GetDefaultStoreId(self,dname):
284 if dname not in self.DefaultStoreMap:
285 return None
286 return self.DefaultStoreMap.get(dname)[0]
287 def PACK_DELTA_DATA(self,skuname,defaultstoragename,delta_list):
288 skuid = self.GetSkuId(skuname)
289 defaultstorageid = self.GetDefaultStoreId(defaultstoragename)
290 Buffer = ""
291 Buffer += pack("=L",8)
292 Buffer += pack("=H",int(skuid))
293 Buffer += pack("=H",int(defaultstorageid))
294 for (delta_offset,value) in delta_list:
295 Buffer += pack("=L",delta_offset)
296 Buffer = Buffer[:-1] + pack("=B",value)
297
298 Buffer = pack("=L",len(Buffer) + 4) + Buffer
299
300 return Buffer
301
302 def AlignData(self,data, align = 4):
303 mybuffer = data
304 for i in range(len(data) % align):
305 mybuffer += pack("=B",0)
306
307 return mybuffer
308
309 def PACK_VARIABLE_NAME(self, var_name):
310 Buffer = ""
311 for name_char in var_name.strip("{").strip("}").split(","):
312 Buffer += pack("=B",int(name_char,16))
313
314 return Buffer