BaseTools: Support Structure PCD value inherit between the different SKUs
[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 pcds_default_data = var_data.get(("DEFAULT","STANDARD"))
119 NvStoreDataBuffer = ""
120 var_data_offset = collections.OrderedDict()
121 offset = NvStorageHeaderSize
122 for default_data,default_info in pcds_default_data.values():
123 var_name_buffer = self.PACK_VARIABLE_NAME(default_info.var_name)
124
125 vendorguid = default_info.var_guid.split('-')
126
127 if default_info.var_attribute:
128 var_attr_value,_ = VariableAttributes.GetVarAttributes(default_info.var_attribute)
129 else:
130 var_attr_value = 0x07
131
132 DataBuffer = self.AlignData(var_name_buffer + default_data)
133
134 data_size = len(DataBuffer)
135 offset += VariableHeaderSize + len(default_info.var_name.split(","))
136 var_data_offset[default_info.pcdindex] = offset
137 offset += data_size - len(default_info.var_name.split(","))
138
139 var_header_buffer = self.PACK_VARIABLE_HEADER(var_attr_value, len(default_info.var_name.split(",")), len (default_data), vendorguid)
140 NvStoreDataBuffer += (var_header_buffer + DataBuffer)
141
142 variable_storage_header_buffer = self.PACK_VARIABLE_STORE_HEADER(len(NvStoreDataBuffer) + 28)
143
144 nv_default_part = self.AlignData(self.PACK_DEFAULT_DATA(0, 0, self.unpack_data(variable_storage_header_buffer+NvStoreDataBuffer)))
145
146 data_delta_structure_buffer = ""
147 for skuname,defaultstore in var_data:
148 if (skuname,defaultstore) == ("DEFAULT","STANDARD"):
149 continue
150 pcds_sku_data = var_data.get((skuname,defaultstore))
151 delta_data_set = []
152 for pcdindex in pcds_sku_data:
153 offset = var_data_offset[pcdindex]
154 delta_data,_ = pcds_sku_data[pcdindex]
155 delta_data = [(item[0] + offset, item[1]) for item in delta_data]
156 delta_data_set.extend(delta_data)
157
158 data_delta_structure_buffer += self.AlignData(self.PACK_DELTA_DATA(skuname,defaultstore,delta_data_set))
159
160 return self.format_data(nv_default_part + data_delta_structure_buffer)
161
162
163 def format_data(self,data):
164
165 return [hex(item) for item in self.unpack_data(data)]
166
167 def unpack_data(self,data):
168 final_data = ()
169 for item in data:
170 final_data += unpack("B",item)
171 return final_data
172
173 def calculate_delta(self, default, theother):
174 if len(default) - len(theother) != 0:
175 EdkLogger.error("build", FORMAT_INVALID, 'The variable data length is not the same for the same PCD.')
176 data_delta = []
177 for i in range(len(default)):
178 if default[i] != theother[i]:
179 data_delta.append((i,theother[i]))
180 return data_delta
181
182 def dump(self):
183
184 default_var_bin = self.new_process_varinfo()
185 value_str = "{"
186 default_var_bin_strip = [ data.strip("""'""") for data in default_var_bin]
187 value_str += ",".join(default_var_bin_strip)
188 value_str += "}"
189 return value_str
190
191 def PACK_VARIABLE_STORE_HEADER(self,size):
192 #Signature: gEfiVariableGuid
193 Guid = "{ 0xddcf3616, 0x3275, 0x4164, { 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d }}"
194 Guid = GuidStructureStringToGuidString(Guid)
195 GuidBuffer = PackGUID(Guid.split('-'))
196
197 SizeBuffer = pack('=L',size)
198 FormatBuffer = pack('=B',0x5A)
199 StateBuffer = pack('=B',0xFE)
200 reservedBuffer = pack('=H',0)
201 reservedBuffer += pack('=L',0)
202
203 return GuidBuffer + SizeBuffer + FormatBuffer + StateBuffer + reservedBuffer
204
205
206 def PACK_VARIABLE_HEADER(self,attribute,namesize,datasize,vendorguid):
207
208 Buffer = pack('=H',0x55AA) # pack StartID
209 Buffer += pack('=B',0x3F) # pack State
210 Buffer += pack('=B',0) # pack reserved
211
212 Buffer += pack('=L',attribute)
213 Buffer += pack('=L',namesize)
214 Buffer += pack('=L',datasize)
215
216 Buffer += PackGUID(vendorguid)
217
218 return Buffer
219
220 def PACK_VARIABLES_DATA(self, var_value,data_type, tail = None):
221 Buffer = ""
222 data_len = 0
223 if data_type == "VOID*":
224 for value_char in var_value.strip("{").strip("}").split(","):
225 Buffer += pack("=B",int(value_char,16))
226 data_len += len(var_value.split(","))
227 if tail:
228 for value_char in tail.split(","):
229 Buffer += pack("=B",int(value_char,16))
230 data_len += len(tail.split(","))
231 elif data_type == "BOOLEAN":
232 Buffer += pack("=B",True) if var_value.upper() == "TRUE" else pack("=B",False)
233 data_len += 1
234 elif data_type == "UINT8":
235 Buffer += pack("=B",GetIntegerValue(var_value))
236 data_len += 1
237 elif data_type == "UINT16":
238 Buffer += pack("=H",GetIntegerValue(var_value))
239 data_len += 2
240 elif data_type == "UINT32":
241 Buffer += pack("=L",GetIntegerValue(var_value))
242 data_len += 4
243 elif data_type == "UINT64":
244 Buffer += pack("=Q",GetIntegerValue(var_value))
245 data_len += 8
246
247 return Buffer
248
249 def PACK_DEFAULT_DATA(self, defaultstoragename,skuid,var_value):
250 Buffer = ""
251 Buffer += pack("=L",8)
252 Buffer += pack("=H",int(defaultstoragename))
253 Buffer += pack("=H",int(skuid))
254
255 for item in var_value:
256 Buffer += pack("=B",item)
257
258 Buffer = pack("=L",len(Buffer)+4) + Buffer
259
260 return Buffer
261
262 def GetSkuId(self,skuname):
263 if skuname not in self.SkuIdMap:
264 return None
265 return self.SkuIdMap.get(skuname)[0]
266 def GetDefaultStoreId(self,dname):
267 if dname not in self.DefaultStoreMap:
268 return None
269 return self.DefaultStoreMap.get(dname)[0]
270 def PACK_DELTA_DATA(self,skuname,defaultstoragename,delta_list):
271 skuid = self.GetSkuId(skuname)
272 defaultstorageid = self.GetDefaultStoreId(defaultstoragename)
273 Buffer = ""
274 Buffer += pack("=L",8)
275 Buffer += pack("=H",int(skuid))
276 Buffer += pack("=H",int(defaultstorageid))
277 for (delta_offset,value) in delta_list:
278 Buffer += pack("=L",delta_offset)
279 Buffer = Buffer[:-1] + pack("=B",value)
280
281 Buffer = pack("=L",len(Buffer) + 4) + Buffer
282
283 return Buffer
284
285 def AlignData(self,data, align = 4):
286 mybuffer = data
287 for i in range(len(data) % align):
288 mybuffer += pack("=B",0)
289
290 return mybuffer
291
292 def PACK_VARIABLE_NAME(self, var_name):
293 Buffer = ""
294 for name_char in var_name.strip("{").strip("}").split(","):
295 Buffer += pack("=B",int(name_char,16))
296
297 return Buffer