BaseTools: Update SkuId as SKU_ID type to follow current implementation
[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 self.VpdRegionSize = 0
55 self.VpdRegionOffset = 0
56 self.NVHeaderBuff = None
57 self.VarDefaultBuff = None
58 self.VarDeltaBuff = None
59
60 def append_variable(self,uefi_var):
61 self.VarInfo.append(uefi_var)
62
63 def SetVpdRegionMaxSize(self,maxsize):
64 self.VpdRegionSize = maxsize
65
66 def SetVpdRegionOffset(self,vpdoffset):
67 self.VpdRegionOffset = vpdoffset
68
69 def PatchNVStoreDefaultMaxSize(self,maxsize):
70 if not self.NVHeaderBuff:
71 return ""
72 self.NVHeaderBuff = self.NVHeaderBuff[:8] + pack("=Q",maxsize)
73 default_var_bin = self.format_data(self.NVHeaderBuff + self.VarDefaultBuff + self.VarDeltaBuff)
74 value_str = "{"
75 default_var_bin_strip = [ data.strip("""'""") for data in default_var_bin]
76 value_str += ",".join(default_var_bin_strip)
77 value_str += "}"
78 return value_str
79
80 def process_variable_data(self):
81
82 var_data = dict()
83
84 indexedvarinfo = collections.OrderedDict()
85 for item in self.VarInfo:
86 if item.pcdindex not in indexedvarinfo:
87 indexedvarinfo[item.pcdindex] = dict()
88 indexedvarinfo[item.pcdindex][(item.skuname,item.defaultstoragename)] = item
89
90 for index in indexedvarinfo:
91 sku_var_info = indexedvarinfo[index]
92
93 default_data_buffer = ""
94 others_data_buffer = ""
95 tail = None
96 default_sku_default = indexedvarinfo.get(index).get(("DEFAULT","STANDARD"))
97
98 if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:
99 var_max_len = max([len(var_item.default_value.split(",")) for var_item in sku_var_info.values()])
100 if len(default_sku_default.default_value.split(",")) < var_max_len:
101 tail = ",".join([ "0x00" for i in range(var_max_len-len(default_sku_default.default_value.split(",")))])
102
103 default_data_buffer = self.PACK_VARIABLES_DATA(default_sku_default.default_value,default_sku_default.data_type,tail)
104
105 default_data_array = ()
106 for item in default_data_buffer:
107 default_data_array += unpack("B",item)
108
109 if ("DEFAULT","STANDARD") not in var_data:
110 var_data[("DEFAULT","STANDARD")] = collections.OrderedDict()
111 var_data[("DEFAULT","STANDARD")][index] = (default_data_buffer,sku_var_info[("DEFAULT","STANDARD")])
112
113 for (skuid,defaultstoragename) in indexedvarinfo.get(index):
114 tail = None
115 if (skuid,defaultstoragename) == ("DEFAULT","STANDARD"):
116 continue
117 other_sku_other = indexedvarinfo.get(index).get((skuid,defaultstoragename))
118
119 if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:
120 if len(other_sku_other.default_value.split(",")) < var_max_len:
121 tail = ",".join([ "0x00" for i in range(var_max_len-len(other_sku_other.default_value.split(",")))])
122
123 others_data_buffer = self.PACK_VARIABLES_DATA(other_sku_other.default_value,other_sku_other.data_type,tail)
124
125 others_data_array = ()
126 for item in others_data_buffer:
127 others_data_array += unpack("B",item)
128
129 data_delta = self.calculate_delta(default_data_array, others_data_array)
130
131 if (skuid,defaultstoragename) not in var_data:
132 var_data[(skuid,defaultstoragename)] = collections.OrderedDict()
133 var_data[(skuid,defaultstoragename)][index] = (data_delta,sku_var_info[(skuid,defaultstoragename)])
134 return var_data
135
136 def new_process_varinfo(self):
137
138 var_data = self.process_variable_data()
139
140 if not var_data:
141 return []
142
143 pcds_default_data = var_data.get(("DEFAULT","STANDARD"),{})
144 NvStoreDataBuffer = ""
145 var_data_offset = collections.OrderedDict()
146 offset = NvStorageHeaderSize
147 for default_data,default_info in pcds_default_data.values():
148 var_name_buffer = self.PACK_VARIABLE_NAME(default_info.var_name)
149
150 vendorguid = default_info.var_guid.split('-')
151
152 if default_info.var_attribute:
153 var_attr_value,_ = VariableAttributes.GetVarAttributes(default_info.var_attribute)
154 else:
155 var_attr_value = 0x07
156
157 DataBuffer = self.AlignData(var_name_buffer + default_data)
158
159 data_size = len(DataBuffer)
160 offset += VariableHeaderSize + len(default_info.var_name.split(","))
161 var_data_offset[default_info.pcdindex] = offset
162 offset += data_size - len(default_info.var_name.split(","))
163
164 var_header_buffer = self.PACK_VARIABLE_HEADER(var_attr_value, len(default_info.var_name.split(",")), len (default_data), vendorguid)
165 NvStoreDataBuffer += (var_header_buffer + DataBuffer)
166
167 variable_storage_header_buffer = self.PACK_VARIABLE_STORE_HEADER(len(NvStoreDataBuffer) + 28)
168
169 nv_default_part = self.AlignData(self.PACK_DEFAULT_DATA(0, 0, self.unpack_data(variable_storage_header_buffer+NvStoreDataBuffer)), 8)
170
171 data_delta_structure_buffer = ""
172 for skuname,defaultstore in var_data:
173 if (skuname,defaultstore) == ("DEFAULT","STANDARD"):
174 continue
175 pcds_sku_data = var_data.get((skuname,defaultstore))
176 delta_data_set = []
177 for pcdindex in pcds_sku_data:
178 offset = var_data_offset[pcdindex]
179 delta_data,_ = pcds_sku_data[pcdindex]
180 delta_data = [(item[0] + offset, item[1]) for item in delta_data]
181 delta_data_set.extend(delta_data)
182
183 data_delta_structure_buffer += self.AlignData(self.PACK_DELTA_DATA(skuname,defaultstore,delta_data_set), 8)
184
185 size = len(nv_default_part + data_delta_structure_buffer) + 16
186 maxsize = self.VpdRegionSize if self.VpdRegionSize else size
187 NV_Store_Default_Header = self.PACK_NV_STORE_DEFAULT_HEADER(size,maxsize)
188
189 self.NVHeaderBuff = NV_Store_Default_Header
190 self.VarDefaultBuff =nv_default_part
191 self.VarDeltaBuff = data_delta_structure_buffer
192 return self.format_data(NV_Store_Default_Header + nv_default_part + data_delta_structure_buffer)
193
194
195 def format_data(self,data):
196
197 return [hex(item) for item in self.unpack_data(data)]
198
199 def unpack_data(self,data):
200 final_data = ()
201 for item in data:
202 final_data += unpack("B",item)
203 return final_data
204
205 def calculate_delta(self, default, theother):
206 if len(default) - len(theother) != 0:
207 EdkLogger.error("build", FORMAT_INVALID, 'The variable data length is not the same for the same PCD.')
208 data_delta = []
209 for i in range(len(default)):
210 if default[i] != theother[i]:
211 data_delta.append((i,theother[i]))
212 return data_delta
213
214 def dump(self):
215
216 default_var_bin = self.new_process_varinfo()
217 if default_var_bin:
218 value_str = "{"
219 default_var_bin_strip = [ data.strip("""'""") for data in default_var_bin]
220 value_str += ",".join(default_var_bin_strip)
221 value_str += "}"
222 return value_str
223 return ""
224
225 def PACK_VARIABLE_STORE_HEADER(self,size):
226 #Signature: gEfiVariableGuid
227 Guid = "{ 0xddcf3616, 0x3275, 0x4164, { 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d }}"
228 Guid = GuidStructureStringToGuidString(Guid)
229 GuidBuffer = PackGUID(Guid.split('-'))
230
231 SizeBuffer = pack('=L',size)
232 FormatBuffer = pack('=B',0x5A)
233 StateBuffer = pack('=B',0xFE)
234 reservedBuffer = pack('=H',0)
235 reservedBuffer += pack('=L',0)
236
237 return GuidBuffer + SizeBuffer + FormatBuffer + StateBuffer + reservedBuffer
238
239 def PACK_NV_STORE_DEFAULT_HEADER(self,size,maxsize):
240 Signature = pack('=B',ord('N'))
241 Signature += pack("=B",ord('S'))
242 Signature += pack("=B",ord('D'))
243 Signature += pack("=B",ord('B'))
244
245 SizeBuffer = pack("=L",size)
246 MaxSizeBuffer = pack("=Q",maxsize)
247
248 return Signature + SizeBuffer + MaxSizeBuffer
249
250 def PACK_VARIABLE_HEADER(self,attribute,namesize,datasize,vendorguid):
251
252 Buffer = pack('=H',0x55AA) # pack StartID
253 Buffer += pack('=B',0x3F) # pack State
254 Buffer += pack('=B',0) # pack reserved
255
256 Buffer += pack('=L',attribute)
257 Buffer += pack('=L',namesize)
258 Buffer += pack('=L',datasize)
259
260 Buffer += PackGUID(vendorguid)
261
262 return Buffer
263
264 def PACK_VARIABLES_DATA(self, var_value,data_type, tail = None):
265 Buffer = ""
266 data_len = 0
267 if data_type == "VOID*":
268 for value_char in var_value.strip("{").strip("}").split(","):
269 Buffer += pack("=B",int(value_char,16))
270 data_len += len(var_value.split(","))
271 if tail:
272 for value_char in tail.split(","):
273 Buffer += pack("=B",int(value_char,16))
274 data_len += len(tail.split(","))
275 elif data_type == "BOOLEAN":
276 Buffer += pack("=B",True) if var_value.upper() == "TRUE" else pack("=B",False)
277 data_len += 1
278 elif data_type == "UINT8":
279 Buffer += pack("=B",GetIntegerValue(var_value))
280 data_len += 1
281 elif data_type == "UINT16":
282 Buffer += pack("=H",GetIntegerValue(var_value))
283 data_len += 2
284 elif data_type == "UINT32":
285 Buffer += pack("=L",GetIntegerValue(var_value))
286 data_len += 4
287 elif data_type == "UINT64":
288 Buffer += pack("=Q",GetIntegerValue(var_value))
289 data_len += 8
290
291 return Buffer
292
293 def PACK_DEFAULT_DATA(self, defaultstoragename,skuid,var_value):
294 Buffer = ""
295 Buffer += pack("=L",4+8+8)
296 Buffer += pack("=Q",int(skuid))
297 Buffer += pack("=Q",int(defaultstoragename))
298
299 for item in var_value:
300 Buffer += pack("=B",item)
301
302 Buffer = pack("=L",len(Buffer)+4) + Buffer
303
304 return Buffer
305
306 def GetSkuId(self,skuname):
307 if skuname not in self.SkuIdMap:
308 return None
309 return self.SkuIdMap.get(skuname)[0]
310 def GetDefaultStoreId(self,dname):
311 if dname not in self.DefaultStoreMap:
312 return None
313 return self.DefaultStoreMap.get(dname)[0]
314 def PACK_DELTA_DATA(self,skuname,defaultstoragename,delta_list):
315 skuid = self.GetSkuId(skuname)
316 defaultstorageid = self.GetDefaultStoreId(defaultstoragename)
317 Buffer = ""
318 Buffer += pack("=L",4+8+8)
319 Buffer += pack("=Q",int(skuid))
320 Buffer += pack("=Q",int(defaultstorageid))
321 for (delta_offset,value) in delta_list:
322 Buffer += pack("=L",delta_offset)
323 Buffer = Buffer[:-1] + pack("=B",value)
324
325 Buffer = pack("=L",len(Buffer) + 4) + Buffer
326
327 return Buffer
328
329 def AlignData(self,data, align = 4):
330 mybuffer = data
331 if (len(data) % align) > 0:
332 for i in range(align - (len(data) % align)):
333 mybuffer += pack("=B",0)
334
335 return mybuffer
336
337 def PACK_VARIABLE_NAME(self, var_name):
338 Buffer = ""
339 for name_char in var_name.strip("{").strip("}").split(","):
340 Buffer += pack("=B",int(name_char,16))
341
342 return Buffer