9b76078a07ff694b8b04e37b1c2ab26b35148eb2
[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 Variable(object):
50 def __init__(self,):
51 self.VarInfo = []
52
53 def append_variable(self,uefi_var):
54 self.VarInfo.append(uefi_var)
55
56 def process_variable_data(self):
57
58 var_data = dict()
59
60 indexedvarinfo = collections.OrderedDict()
61 for item in self.VarInfo:
62 if item.pcdindex not in indexedvarinfo:
63 indexedvarinfo[item.pcdindex] = dict()
64 indexedvarinfo[item.pcdindex][(int(item.skuname),int(item.defaultstoragename))] = item
65
66 for index in indexedvarinfo:
67 sku_var_info = indexedvarinfo[index]
68
69 default_data_buffer = ""
70 others_data_buffer = ""
71 tail = None
72 default_sku_default = indexedvarinfo.get(index).get((0,0))
73
74 if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:
75 var_max_len = max([len(var_item.default_value.split(",")) for var_item in sku_var_info.values()])
76 if len(default_sku_default.default_value.split(",")) < var_max_len:
77 tail = ",".join([ "0x00" for i in range(var_max_len-len(default_sku_default.default_value.split(",")))])
78
79 default_data_buffer = self.PACK_VARIABLES_DATA(default_sku_default.default_value,default_sku_default.data_type,tail)
80
81 default_data_array = ()
82 for item in default_data_buffer:
83 default_data_array += unpack("B",item)
84
85 if (0,0) not in var_data:
86 var_data[(0,0)] = collections.OrderedDict()
87 var_data[(0,0)][index] = (default_data_buffer,sku_var_info[(0,0)])
88
89 for (skuid,defaultstoragename) in indexedvarinfo.get(index):
90 tail = None
91 if (skuid,defaultstoragename) == (0,0):
92 continue
93 other_sku_other = indexedvarinfo.get(index).get((skuid,defaultstoragename))
94
95 if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:
96 if len(other_sku_other.default_value.split(",")) < var_max_len:
97 tail = ",".join([ "0x00" for i in range(var_max_len-len(other_sku_other.default_value.split(",")))])
98
99 others_data_buffer = self.PACK_VARIABLES_DATA(other_sku_other.default_value,other_sku_other.data_type,tail)
100
101 others_data_array = ()
102 for item in others_data_buffer:
103 others_data_array += unpack("B",item)
104
105 data_delta = self.calculate_delta(default_data_array, others_data_array)
106
107 if (skuid,defaultstoragename) not in var_data:
108 var_data[(skuid,defaultstoragename)] = collections.OrderedDict()
109 var_data[(skuid,defaultstoragename)][index] = (data_delta,sku_var_info[(skuid,defaultstoragename)])
110 return var_data
111
112 def new_process_varinfo(self):
113
114 var_data = self.process_variable_data()
115
116 pcds_default_data = var_data.get((0,0))
117 NvStoreDataBuffer = ""
118 var_data_offset = collections.OrderedDict()
119 offset = NvStorageHeaderSize
120 for default_data,default_info in pcds_default_data.values():
121 var_name_buffer = self.PACK_VARIABLE_NAME(default_info.var_name)
122
123 vendorguid = default_info.var_guid.split('-')
124
125 if default_info.var_attribute:
126 var_attr_value,_ = VariableAttributes.GetVarAttributes(default_info.var_attribute)
127 else:
128 var_attr_value = 0x07
129
130 print "default var_name_buffer"
131 print self.format_data(var_name_buffer)
132 print "default var_buffer"
133 print self.format_data(default_data)
134
135 DataBuffer = self.AlignData(var_name_buffer + default_data)
136
137 data_size = len(DataBuffer)
138 offset += VariableHeaderSize + len(default_info.var_name.split(","))
139 var_data_offset[default_info.pcdindex] = offset
140 offset += data_size - len(default_info.var_name.split(","))
141
142 var_header_buffer = self.PACK_VARIABLE_HEADER(var_attr_value, len(default_info.var_name.split(",")), data_size, vendorguid)
143 NvStoreDataBuffer += (var_header_buffer + DataBuffer)
144
145 variable_storage_header_buffer = self.PACK_VARIABLE_STORE_HEADER(len(NvStoreDataBuffer) + 28)
146
147 nv_default_part = self.AlignData(self.PACK_DEFAULT_DATA(0, 0, self.unpack_data(variable_storage_header_buffer+NvStoreDataBuffer)))
148
149 print "default whole data \n",self.format_data(nv_default_part)
150
151 data_delta_structure_buffer = ""
152 for skuid,defaultid in var_data:
153 if (skuid,defaultid) == (0,0):
154 continue
155 pcds_sku_data = var_data.get((skuid,defaultid))
156 delta_data_set = []
157 for pcdindex in pcds_sku_data:
158 offset = var_data_offset[pcdindex]
159 delta_data,_ = pcds_sku_data[pcdindex]
160 delta_data = [(item[0] + offset, item[1]) for item in delta_data]
161 delta_data_set.extend(delta_data)
162
163 data_delta_structure_buffer += self.AlignData(self.PACK_DELTA_DATA(defaultid,skuid,delta_data_set))
164 print "delta data"
165 print delta_data_set
166 print self.format_data(self.AlignData(self.PACK_DELTA_DATA(defaultid,skuid,delta_data_set)))
167
168 return self.format_data(nv_default_part + data_delta_structure_buffer)
169
170
171 def format_data(self,data):
172
173 return [hex(item) for item in self.unpack_data(data)]
174
175 def unpack_data(self,data):
176 final_data = ()
177 for item in data:
178 final_data += unpack("B",item)
179 return final_data
180
181 def calculate_delta(self, default, theother):
182 print "default data \n", default
183 print "other data \n",theother
184 if len(default) - len(theother) != 0:
185 EdkLogger.error("build", FORMAT_INVALID, 'The variable data length is not the same for the same PCD.')
186 data_delta = []
187 for i in range(len(default)):
188 if default[i] != theother[i]:
189 data_delta.append((i,theother[i]))
190 return data_delta
191
192 def dump(self):
193
194 default_var_bin = self.new_process_varinfo()
195 value_str = "{"
196 default_var_bin_strip = [ data.strip("""'""") for data in default_var_bin]
197 value_str += ",".join(default_var_bin_strip)
198 value_str += "}"
199 return value_str
200
201 def PACK_VARIABLE_STORE_HEADER(self,size):
202 #Signature: gEfiVariableGuid
203 Guid = "{ 0xddcf3616, 0x3275, 0x4164, { 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d }}"
204 Guid = GuidStructureStringToGuidString(Guid)
205 GuidBuffer = PackGUID(Guid.split('-'))
206
207 SizeBuffer = pack('=L',size)
208 FormatBuffer = pack('=B',0x5A)
209 StateBuffer = pack('=B',0xFE)
210 reservedBuffer = pack('=H',0)
211 reservedBuffer += pack('=L',0)
212
213 return GuidBuffer + SizeBuffer + FormatBuffer + StateBuffer + reservedBuffer
214
215
216 def PACK_VARIABLE_HEADER(self,attribute,namesize,datasize,vendorguid):
217
218 Buffer = pack('=H',0x55AA) # pack StartID
219 Buffer += pack('=B',0x3F) # pack State
220 Buffer += pack('=B',0) # pack reserved
221
222 Buffer += pack('=L',attribute)
223 Buffer += pack('=L',namesize)
224 Buffer += pack('=L',datasize)
225
226 Buffer += PackGUID(vendorguid)
227
228 return Buffer
229
230 def PACK_VARIABLES_DATA(self, var_value,data_type, tail = None):
231 Buffer = ""
232 data_len = 0
233 if data_type == "VOID*":
234 for value_char in var_value.strip("{").strip("}").split(","):
235 Buffer += pack("=B",int(value_char,16))
236 data_len += len(var_value.split(","))
237 if tail:
238 for value_char in tail.split(","):
239 Buffer += pack("=B",int(value_char,16))
240 data_len += len(tail.split(","))
241 elif data_type == "BOOLEAN":
242 Buffer += pack("=B",True) if var_value.upper() == "TRUE" else pack("=B",False)
243 data_len += 1
244 elif data_type == "UINT8":
245 Buffer += pack("=B",GetIntegerValue(var_value))
246 data_len += 1
247 elif data_type == "UINT16":
248 Buffer += pack("=H",GetIntegerValue(var_value))
249 data_len += 2
250 elif data_type == "UINT32":
251 Buffer += pack("=L",GetIntegerValue(var_value))
252 data_len += 4
253 elif data_type == "UINT64":
254 Buffer += pack("=Q",GetIntegerValue(var_value))
255 data_len += 8
256
257 return Buffer
258
259 def PACK_DEFAULT_DATA(self, defaultstoragename,skuid,var_value):
260 Buffer = ""
261 Buffer += pack("=H",6)
262 Buffer += pack("=H",int(defaultstoragename))
263 Buffer += pack("=H",int(skuid))
264
265 for item in var_value:
266 Buffer += pack("=B",item)
267
268 Buffer = pack("=L",len(Buffer)+4) + Buffer
269
270 return Buffer
271
272 def PACK_DELTA_DATA(self,defaultstoragename,skuid,delta_list):
273 Buffer = ""
274 Buffer += pack("=H",6)
275 Buffer += pack("=H",int(defaultstoragename))
276 Buffer += pack("=H",int(skuid))
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