]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python\r | |
2 | ## @file\r | |
3 | # Firmware Configuration Editor (FCE) from https://firmware.intel.com/develop\r | |
4 | # can parse BIOS image and generate Firmware Configuration file.\r | |
5 | # This script bases on Firmware Configuration file, and generate the structure\r | |
6 | # PCD setting in DEC/DSC/INF files.\r | |
7 | #\r | |
8 | # Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>\r | |
9 | # This program and the accompanying materials\r | |
10 | # are licensed and made available under the terms and conditions of the BSD License\r | |
11 | # which accompanies this distribution. The full text of the license may be found at\r | |
12 | # http://opensource.org/licenses/bsd-license.php\r | |
13 | #\r | |
14 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
15 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
16 | #\r | |
17 | \r | |
18 | '''\r | |
19 | ConvertFceToStructurePcd\r | |
20 | '''\r | |
21 | \r | |
22 | import re\r | |
23 | import os\r | |
24 | import datetime\r | |
25 | import argparse\r | |
26 | \r | |
27 | #\r | |
28 | # Globals for help information\r | |
29 | #\r | |
30 | __prog__ = 'ConvertFceToStructurePcd'\r | |
31 | __version__ = '%s Version %s' % (__prog__, '0.1 ')\r | |
32 | __copyright__ = 'Copyright (c) 2018, Intel Corporation. All rights reserved.'\r | |
33 | __description__ = 'Generate Structure PCD in DEC/DSC/INF based on Firmware Configuration.\n'\r | |
34 | \r | |
35 | \r | |
36 | dscstatement='''[Defines]\r | |
37 | VPD_TOOL_GUID = 8C3D856A-9BE6-468E-850A-24F7A8D38E08\r | |
38 | \r | |
39 | [SkuIds]\r | |
40 | 0|DEFAULT # The entry: 0|DEFAULT is reserved and always required.\r | |
41 | \r | |
42 | [DefaultStores]\r | |
43 | 0|STANDARD # UEFI Standard default 0|STANDARD is reserved.\r | |
44 | 1|MANUFACTURING # UEFI Manufacturing default 1|MANUFACTURING is reserved.\r | |
45 | \r | |
46 | [PcdsDynamicExVpd.common.DEFAULT]\r | |
47 | gEfiMdeModulePkgTokenSpaceGuid.PcdNvStoreDefaultValueBuffer|*\r | |
48 | '''\r | |
49 | \r | |
50 | decstatement = '''[Guids]\r | |
51 | gStructPcdTokenSpaceGuid = {0x3f1406f4, 0x2b, 0x487a, {0x8b, 0x69, 0x74, 0x29, 0x1b, 0x36, 0x16, 0xf4}}\r | |
52 | \r | |
53 | [PcdsFixedAtBuild,PcdsPatchableInModule,PcdsDynamic,PcdsDynamicEx]\r | |
54 | '''\r | |
55 | \r | |
56 | infstatement = '''[Pcd]\r | |
57 | '''\r | |
58 | \r | |
59 | SECTION='PcdsDynamicHii'\r | |
60 | PCD_NAME='gStructPcdTokenSpaceGuid.Pcd'\r | |
61 | \r | |
62 | WARNING=[]\r | |
63 | ERRORMSG=[]\r | |
64 | \r | |
65 | class parser_lst(object):\r | |
66 | \r | |
67 | def __init__(self,filelist):\r | |
68 | self._ignore=['BOOLEAN', 'UINT8', 'UINT16', 'UINT32', 'UINT64']\r | |
69 | self.file=filelist\r | |
70 | self.text=self.megre_lst()[0]\r | |
71 | self.content=self.megre_lst()[1]\r | |
72 | \r | |
73 | def megre_lst(self):\r | |
74 | alltext=''\r | |
75 | content={}\r | |
76 | for file in self.file:\r | |
77 | with open(file,'r') as f:\r | |
78 | read =f.read()\r | |
79 | alltext += read\r | |
80 | content[file]=read\r | |
81 | return alltext,content\r | |
82 | \r | |
83 | def struct_lst(self):#{struct:lst file}\r | |
84 | structs_file={}\r | |
85 | name_format = re.compile(r'(?<!typedef)\s+struct (\w+) {.*?;', re.S)\r | |
86 | for i in list(self.content.keys()):\r | |
87 | structs= name_format.findall(self.content[i])\r | |
88 | if structs:\r | |
89 | for j in structs:\r | |
90 | if j not in self._ignore:\r | |
91 | structs_file[j]=i\r | |
92 | else:\r | |
93 | print("%s"%structs)\r | |
94 | return structs_file\r | |
95 | \r | |
96 | def struct(self):#struct:{offset:name}\r | |
97 | unit_num = re.compile('(\d+)')\r | |
98 | offset1_re = re.compile('(\d+)\[')\r | |
99 | pcdname_num_re = re.compile('\w+\[(\S+)\]')\r | |
100 | pcdname_re = re.compile('\](.*)\<')\r | |
101 | pcdname2_re = re.compile('(\w+)\[')\r | |
102 | uint_re = re.compile('\<(\S+)\>')\r | |
103 | name_format = re.compile(r'(?<!typedef)\s+struct (\w+) {.*?;', re.S)\r | |
104 | name=name_format.findall(self.text)\r | |
105 | info={}\r | |
106 | unparse=[]\r | |
107 | if name:\r | |
108 | tmp_n = [n for n in name if n not in self._ignore]\r | |
109 | name = list(set(tmp_n))\r | |
110 | name.sort(key = tmp_n.index)\r | |
111 | name.reverse()\r | |
112 | #name=list(set(name).difference(set(self._ignore)))\r | |
113 | for struct in name:\r | |
114 | s_re = re.compile(r'struct %s :(.*?)};'% struct, re.S)\r | |
115 | content = s_re.search(self.text)\r | |
116 | if content:\r | |
117 | tmp_dict = {}\r | |
118 | text = content.group().split('+')\r | |
119 | for line in text[1:]:\r | |
120 | offset = offset1_re.findall(line)\r | |
121 | t_name = pcdname_re.findall(line)\r | |
122 | uint = uint_re.findall(line)\r | |
123 | if offset and uint:\r | |
124 | offset = offset[0]\r | |
125 | uint = uint[0]\r | |
126 | if t_name:\r | |
127 | t_name = t_name[0].strip()\r | |
128 | if (' ' in t_name) or ("=" in t_name) or (";" in t_name) or("\\" in name) or (t_name ==''):\r | |
129 | WARNING.append("Warning:Invalid Pcd name '%s' for Offset %s in struct %s" % (t_name,offset, struct))\r | |
130 | else:\r | |
131 | if '[' in t_name:\r | |
132 | if uint in ['UINT8', 'UINT16', 'UINT32', 'UINT64']:\r | |
133 | offset = int(offset, 10)\r | |
134 | tmp_name = pcdname2_re.findall(t_name)[0] + '[0]'\r | |
135 | tmp_dict[offset] = tmp_name\r | |
136 | pcdname_num = int(pcdname_num_re.findall(t_name)[0],10)\r | |
137 | uint = int(unit_num.findall(uint)[0],10)\r | |
138 | bit = uint / 8\r | |
139 | for i in range(1, pcdname_num):\r | |
140 | offset += bit\r | |
141 | tmp_name = pcdname2_re.findall(t_name)[0] + '[%s]' % i\r | |
142 | tmp_dict[offset] = tmp_name\r | |
143 | else:\r | |
144 | tmp_name = pcdname2_re.findall(t_name)[0]\r | |
145 | pcdname_num = pcdname_num_re.findall(t_name)[0]\r | |
146 | line = [offset,tmp_name,pcdname_num,uint]\r | |
147 | line.append(struct)\r | |
148 | unparse.append(line)\r | |
149 | else:\r | |
150 | if uint not in ['UINT8', 'UINT16', 'UINT32', 'UINT64']:\r | |
151 | line = [offset, t_name, 0, uint]\r | |
152 | line.append(struct)\r | |
153 | unparse.append(line)\r | |
154 | else:\r | |
155 | offset = int(offset,10)\r | |
156 | tmp_dict[offset] = t_name\r | |
157 | info[struct] = tmp_dict\r | |
158 | if len(unparse) != 0:\r | |
159 | for u in unparse:\r | |
160 | if u[3] in list(info.keys()):\r | |
161 | unpar = self.nameISstruct(u,info[u[3]])\r | |
162 | info[u[4]]= dict(list(info[u[4]].items())+list(unpar[u[4]].items()))\r | |
163 | else:\r | |
164 | print("ERROR: No struct name found in %s" % self.file)\r | |
165 | ERRORMSG.append("ERROR: No struct name found in %s" % self.file)\r | |
166 | return info\r | |
167 | \r | |
168 | \r | |
169 | def nameISstruct(self,line,key_dict):\r | |
170 | dict={}\r | |
171 | dict2={}\r | |
172 | s_re = re.compile(r'struct %s :(.*?)};' % line[3], re.S)\r | |
173 | size_re = re.compile(r'mTotalSize \[(\S+)\]')\r | |
174 | content = s_re.search(self.text)\r | |
175 | if content:\r | |
176 | s_size = size_re.findall(content.group())[0]\r | |
177 | else:\r | |
178 | s_size = '0'\r | |
179 | print("ERROR: Struct %s not define mTotalSize in lst file" %line[3])\r | |
180 | ERRORMSG.append("ERROR: Struct %s not define mTotalSize in lst file" %line[3])\r | |
181 | size = int(line[0], 10)\r | |
182 | if line[2] != 0:\r | |
183 | for j in range(0, int(line[2], 10)):\r | |
184 | for k in list(key_dict.keys()):\r | |
185 | offset = size + k\r | |
186 | name ='%s.%s' %((line[1]+'[%s]'%j),key_dict[k])\r | |
187 | dict[offset] = name\r | |
188 | size = int(s_size,16)+size\r | |
189 | elif line[2] == 0:\r | |
190 | for k in list(key_dict.keys()):\r | |
191 | offset = size + k\r | |
192 | name = '%s.%s' % (line[1], key_dict[k])\r | |
193 | dict[offset] = name\r | |
194 | dict2[line[4]] = dict\r | |
195 | return dict2\r | |
196 | \r | |
197 | def efivarstore_parser(self):\r | |
198 | efivarstore_format = re.compile(r'efivarstore.*?;', re.S)\r | |
199 | struct_re = re.compile(r'efivarstore(.*?),',re.S)\r | |
200 | name_re = re.compile(r'name=(\w+)')\r | |
201 | efivarstore_dict={}\r | |
202 | efitxt = efivarstore_format.findall(self.text)\r | |
203 | for i in efitxt:\r | |
204 | struct = struct_re.findall(i.replace(' ',''))\r | |
205 | name = name_re.findall(i.replace(' ',''))\r | |
206 | if struct and name:\r | |
207 | efivarstore_dict[name[0]]=struct[0]\r | |
208 | else:\r | |
209 | print("ERROR: Can't find Struct or name in lst file, please check have this format:efivarstore XXXX, name=xxxx")\r | |
210 | ERRORMSG.append("ERROR: Can't find Struct or name in lst file, please check have this format:efivarstore XXXX, name=xxxx")\r | |
211 | return efivarstore_dict\r | |
212 | \r | |
213 | class Config(object):\r | |
214 | \r | |
215 | def __init__(self,Config):\r | |
216 | self.config=Config\r | |
217 | \r | |
218 | #Parser .config file,return list[offset,name,guid,value,help]\r | |
219 | def config_parser(self):\r | |
220 | ids_re =re.compile('_ID:(\d+)',re.S)\r | |
221 | id_re= re.compile('\s+')\r | |
222 | info = []\r | |
223 | info_dict={}\r | |
224 | with open(self.config, 'r') as text:\r | |
225 | read = text.read()\r | |
226 | if 'DEFAULT_ID:' in read:\r | |
227 | all_txt = read.split('FCEKEY DEFAULT')\r | |
228 | for i in all_txt[1:]:\r | |
229 | part = [] #save all infomation for DEFAULT_ID\r | |
230 | str_id=''\r | |
231 | ids = ids_re.findall(i.replace(' ',''))\r | |
232 | for m in ids:\r | |
233 | str_id +=m+'_'\r | |
234 | str_id=str_id[:-1]\r | |
235 | part.append(ids)\r | |
236 | section = i.split('\nQ') #split with '\nQ ' to get every block\r | |
237 | part +=self.section_parser(section)\r | |
238 | info_dict[str_id] = self.section_parser(section)\r | |
239 | info.append(part)\r | |
240 | else:\r | |
241 | part = []\r | |
242 | id=('0','0')\r | |
243 | str_id='0_0'\r | |
244 | part.append(id)\r | |
245 | section = read.split('\nQ')\r | |
246 | part +=self.section_parser(section)\r | |
247 | info_dict[str_id] = self.section_parser(section)\r | |
248 | info.append(part)\r | |
249 | return info_dict\r | |
250 | \r | |
251 | def eval_id(self,id):\r | |
252 | id = id.split("_")\r | |
253 | default_id=id[0:len(id)//2]\r | |
254 | platform_id=id[len(id)//2:]\r | |
255 | text=''\r | |
256 | for i in range(len(default_id)):\r | |
257 | text +="%s.common.%s.%s,"%(SECTION,self.id_name(platform_id[i],'PLATFORM'),self.id_name(default_id[i],'DEFAULT'))\r | |
258 | return '\n[%s]\n'%text[:-1]\r | |
259 | \r | |
260 | def id_name(self,ID, flag):\r | |
261 | platform_dict = {'0': 'DEFAULT'}\r | |
262 | default_dict = {'0': 'STANDARD', '1': 'MANUFACTURING'}\r | |
263 | if flag == "PLATFORM":\r | |
264 | try:\r | |
265 | value = platform_dict[ID]\r | |
266 | except KeyError:\r | |
267 | value = 'SKUID%s' % ID\r | |
268 | elif flag == 'DEFAULT':\r | |
269 | try:\r | |
270 | value = default_dict[ID]\r | |
271 | except KeyError:\r | |
272 | value = 'DEFAULTID%s' % ID\r | |
273 | else:\r | |
274 | value = None\r | |
275 | return value\r | |
276 | \r | |
277 | def section_parser(self,section):\r | |
278 | offset_re = re.compile(r'offset=(\w+)')\r | |
279 | name_re = re.compile(r'name=(\S+)')\r | |
280 | guid_re = re.compile(r'guid=(\S+)')\r | |
281 | # help_re = re.compile(r'help = (.*)')\r | |
282 | attribute_re=re.compile(r'attribute=(\w+)')\r | |
283 | value_re = re.compile(r'(//.*)')\r | |
284 | part = []\r | |
285 | for x in section[1:]:\r | |
286 | line=x.split('\n')[0]\r | |
287 | line=value_re.sub('',line) #delete \\... in "Q...." line\r | |
288 | list1=line.split(' ')\r | |
289 | value=self.value_parser(list1)\r | |
290 | offset = offset_re.findall(x.replace(' ',''))\r | |
291 | name = name_re.findall(x.replace(' ',''))\r | |
292 | guid = guid_re.findall(x.replace(' ',''))\r | |
293 | attribute =attribute_re.findall(x.replace(' ',''))\r | |
294 | if offset and name and guid and value and attribute:\r | |
295 | if attribute[0] in ['0x3','0x7']:\r | |
296 | offset = int(offset[0], 16)\r | |
297 | #help = help_re.findall(x)\r | |
298 | text = offset, name[0], guid[0], value, attribute[0]\r | |
299 | part.append(text)\r | |
300 | return(part)\r | |
301 | \r | |
302 | def value_parser(self, list1):\r | |
303 | list1 = [t for t in list1 if t != ''] # remove '' form list\r | |
304 | first_num = int(list1[0], 16)\r | |
305 | if list1[first_num + 1] == 'STRING': # parser STRING\r | |
306 | value = 'L%s' % list1[-1]\r | |
307 | elif list1[first_num + 1] == 'ORDERED_LIST': # parser ORDERED_LIST\r | |
308 | value_total = int(list1[first_num + 2])\r | |
309 | list2 = list1[-value_total:]\r | |
310 | tmp = []\r | |
311 | line = ''\r | |
312 | for i in list2:\r | |
313 | if len(i) % 2 == 0 and len(i) != 2:\r | |
314 | for m in range(0, len(i) // 2):\r | |
315 | tmp.append('0x%02x' % (int('0x%s' % i, 16) >> m * 8 & 0xff))\r | |
316 | else:\r | |
317 | tmp.append('0x%s' % i)\r | |
318 | for i in tmp:\r | |
319 | line += '%s,' % i\r | |
320 | value = '{%s}' % line[:-1]\r | |
321 | else:\r | |
322 | value = "0x%01x" % int(list1[-1], 16)\r | |
323 | return value\r | |
324 | \r | |
325 | \r | |
326 | #parser Guid file, get guid name form guid value\r | |
327 | class GUID(object):\r | |
328 | \r | |
329 | def __init__(self,path):\r | |
330 | self.path = path\r | |
331 | self.guidfile = self.gfile()\r | |
332 | self.guiddict = self.guid_dict()\r | |
333 | \r | |
334 | def gfile(self):\r | |
335 | for root, dir, file in os.walk(self.path, topdown=True, followlinks=False):\r | |
336 | if 'FV' in dir:\r | |
337 | gfile = os.path.join(root,'Fv','Guid.xref')\r | |
338 | if os.path.isfile(gfile):\r | |
339 | return gfile\r | |
340 | else:\r | |
341 | print("ERROR: Guid.xref file not found")\r | |
342 | ERRORMSG.append("ERROR: Guid.xref file not found")\r | |
343 | exit()\r | |
344 | \r | |
345 | def guid_dict(self):\r | |
346 | guiddict={}\r | |
347 | with open(self.guidfile,'r') as file:\r | |
348 | lines = file.readlines()\r | |
349 | guidinfo=lines\r | |
350 | for line in guidinfo:\r | |
351 | list=line.strip().split(' ')\r | |
352 | if list:\r | |
353 | if len(list)>1:\r | |
354 | guiddict[list[0].upper()]=list[1]\r | |
355 | elif list[0] != ''and len(list)==1:\r | |
356 | print("Error: line %s can't be parser in %s"%(line.strip(),self.guidfile))\r | |
357 | ERRORMSG.append("Error: line %s can't be parser in %s"%(line.strip(),self.guidfile))\r | |
358 | else:\r | |
359 | print("ERROR: No data in %s" %self.guidfile)\r | |
360 | ERRORMSG.append("ERROR: No data in %s" %self.guidfile)\r | |
361 | return guiddict\r | |
362 | \r | |
363 | def guid_parser(self,guid):\r | |
364 | if guid.upper() in self.guiddict:\r | |
365 | return self.guiddict[guid.upper()]\r | |
366 | else:\r | |
367 | print("ERROR: GUID %s not found in file %s"%(guid, self.guidfile))\r | |
368 | ERRORMSG.append("ERROR: GUID %s not found in file %s"%(guid, self.guidfile))\r | |
369 | return guid\r | |
370 | \r | |
371 | class PATH(object):\r | |
372 | \r | |
373 | def __init__(self,path):\r | |
374 | self.path=path\r | |
375 | self.rootdir=self.get_root_dir()\r | |
376 | self.usefuldir=[]\r | |
377 | self.lstinf = {}\r | |
378 | for path in self.rootdir:\r | |
379 | for o_root, o_dir, o_file in os.walk(os.path.join(path, "OUTPUT"), topdown=True, followlinks=False):\r | |
380 | for INF in o_file:\r | |
381 | if os.path.splitext(INF)[1] == '.inf':\r | |
382 | for l_root, l_dir, l_file in os.walk(os.path.join(path, "DEBUG"), topdown=True,\r | |
383 | followlinks=False):\r | |
384 | for LST in l_file:\r | |
385 | if os.path.splitext(LST)[1] == '.lst':\r | |
386 | self.lstinf[os.path.join(l_root, LST)] = os.path.join(o_root, INF)\r | |
387 | self.usefuldir.append(path)\r | |
388 | \r | |
389 | def get_root_dir(self):\r | |
390 | rootdir=[]\r | |
391 | for root,dir,file in os.walk(self.path,topdown=True,followlinks=False):\r | |
392 | if "OUTPUT" in root:\r | |
393 | updir=root.split("OUTPUT",1)[0]\r | |
394 | rootdir.append(updir)\r | |
395 | rootdir=list(set(rootdir))\r | |
396 | return rootdir\r | |
397 | \r | |
398 | def lst_inf(self):\r | |
399 | return self.lstinf\r | |
400 | \r | |
401 | def package(self):\r | |
402 | package={}\r | |
403 | package_re=re.compile(r'Packages\.\w+]\n(.*)',re.S)\r | |
404 | for i in list(self.lstinf.values()):\r | |
405 | with open(i,'r') as inf:\r | |
406 | read=inf.read()\r | |
407 | section=read.split('[')\r | |
408 | for j in section:\r | |
409 | p=package_re.findall(j)\r | |
410 | if p:\r | |
411 | package[i]=p[0].rstrip()\r | |
412 | return package\r | |
413 | \r | |
414 | def header(self,struct):\r | |
415 | header={}\r | |
416 | head_re = re.compile(r'} %s;[\s\S\n]+h{1}"'%struct,re.M|re.S)\r | |
417 | head_re2 = re.compile(r'#line[\s\d]+"(\S+h)"')\r | |
418 | for i in list(self.lstinf.keys()):\r | |
419 | with open(i,'r') as lst:\r | |
420 | read = lst.read()\r | |
421 | h = head_re.findall(read)\r | |
422 | if h:\r | |
423 | head=head_re2.findall(h[0])\r | |
424 | if head:\r | |
425 | format = head[0].replace('\\\\','/').replace('\\','/')\r | |
426 | name =format.split('/')[-1]\r | |
427 | head = self.makefile(name).replace('\\','/')\r | |
428 | header[struct] = head\r | |
429 | return header\r | |
430 | \r | |
431 | def makefile(self,filename):\r | |
432 | re_format = re.compile(r'DEBUG_DIR.*(?:\S+Pkg)\\(.*\\%s)'%filename)\r | |
433 | for i in self.usefuldir:\r | |
434 | with open(os.path.join(i,'Makefile'),'r') as make:\r | |
435 | read = make.read()\r | |
436 | dir = re_format.findall(read)\r | |
437 | if dir:\r | |
438 | return dir[0]\r | |
439 | \r | |
440 | class mainprocess(object):\r | |
441 | \r | |
442 | def __init__(self,InputPath,Config,OutputPath):\r | |
443 | self.init = 0xFCD00000\r | |
444 | self.inputpath = os.path.abspath(InputPath)\r | |
445 | self.outputpath = os.path.abspath(OutputPath)\r | |
446 | self.LST = PATH(self.inputpath)\r | |
447 | self.lst_dict = self.LST.lst_inf()\r | |
448 | self.Config = Config\r | |
449 | self.attribute_dict = {'0x3': 'NV, BS', '0x7': 'NV, BS, RT'}\r | |
450 | self.guid = GUID(self.inputpath)\r | |
451 | self.header={}\r | |
452 | \r | |
453 | def main(self):\r | |
454 | conf=Config(self.Config)\r | |
455 | config_dict=conf.config_parser() #get {'0_0':[offset,name,guid,value,attribute]...,'1_0':....}\r | |
456 | lst=parser_lst(list(self.lst_dict.keys()))\r | |
457 | efi_dict=lst.efivarstore_parser() #get {name:struct} form lst file\r | |
458 | keys=sorted(config_dict.keys())\r | |
459 | all_struct=lst.struct()\r | |
460 | stru_lst=lst.struct_lst()\r | |
461 | title_list=[]\r | |
462 | info_list=[]\r | |
463 | header_list=[]\r | |
464 | inf_list =[]\r | |
465 | for i in stru_lst:\r | |
466 | tmp = self.LST.header(i)\r | |
467 | self.header.update(tmp)\r | |
468 | for id_key in keys:\r | |
469 | tmp_id=[id_key] #['0_0',[(struct,[name...]),(struct,[name...])]]\r | |
470 | tmp_info={} #{name:struct}\r | |
471 | for section in config_dict[id_key]:\r | |
472 | c_offset,c_name,c_guid,c_value,c_attribute = section\r | |
473 | if c_name in efi_dict:\r | |
474 | struct = efi_dict[c_name]\r | |
475 | title='%s%s|L"%s"|%s|0x00||%s\n'%(PCD_NAME,c_name,c_name,self.guid.guid_parser(c_guid),self.attribute_dict[c_attribute])\r | |
476 | if struct in all_struct:\r | |
477 | lstfile = stru_lst[struct]\r | |
478 | struct_dict=all_struct[struct]\r | |
479 | try:\r | |
480 | title2 = '%s%s|{0}|%s|0xFCD00000{\n <HeaderFiles>\n %s\n <Packages>\n%s\n}\n' % (PCD_NAME, c_name, struct, self.header[struct], self.LST.package()[self.lst_dict[lstfile]])\r | |
481 | except KeyError:\r | |
482 | WARNING.append("Warning: No <HeaderFiles> for struct %s"%struct)\r | |
483 | title2 = '%s%s|{0}|%s|0xFCD00000{\n <HeaderFiles>\n %s\n <Packages>\n%s\n}\n' % (PCD_NAME, c_name, struct, '', self.LST.package()[self.lst_dict[lstfile]])\r | |
484 | header_list.append(title2)\r | |
485 | else:\r | |
486 | struct_dict ={}\r | |
487 | print("ERROR: Struct %s can't found in lst file" %struct)\r | |
488 | ERRORMSG.append("ERROR: Struct %s can't found in lst file" %struct)\r | |
489 | if c_offset in struct_dict:\r | |
490 | offset_name=struct_dict[c_offset]\r | |
491 | info = "%s%s.%s|%s\n"%(PCD_NAME,c_name,offset_name,c_value)\r | |
492 | inf = "%s%s\n"%(PCD_NAME,c_name)\r | |
493 | inf_list.append(inf)\r | |
494 | tmp_info[info]=title\r | |
495 | else:\r | |
496 | print("ERROR: Can't find offset %s with struct name %s"%(c_offset,struct))\r | |
497 | ERRORMSG.append("ERROR: Can't find offset %s with name %s"%(c_offset,struct))\r | |
498 | else:\r | |
499 | print("ERROR: Can't find name %s in lst file"%(c_name))\r | |
500 | ERRORMSG.append("ERROR: Can't find name %s in lst file"%(c_name))\r | |
501 | tmp_id.append(list(self.reverse_dict(tmp_info).items()))\r | |
502 | id,tmp_title_list,tmp_info_list = self.read_list(tmp_id)\r | |
503 | title_list +=tmp_title_list\r | |
504 | info_list.append(tmp_info_list)\r | |
505 | inf_list = self.del_repeat(inf_list)\r | |
506 | header_list = self.plus(self.del_repeat(header_list))\r | |
507 | title_all=list(set(title_list))\r | |
508 | info_list = self.del_repeat(info_list)\r | |
509 | for i in range(len(info_list)-1,-1,-1):\r | |
510 | if len(info_list[i]) == 0:\r | |
511 | info_list.remove(info_list[i])\r | |
512 | return keys,title_all,info_list,header_list,inf_list\r | |
513 | \r | |
514 | \r | |
515 | def write_all(self):\r | |
516 | title_flag=1\r | |
517 | info_flag=1\r | |
518 | if not os.path.isdir(self.outputpath):\r | |
519 | os.makedirs(self.outputpath)\r | |
520 | decwrite = write2file(os.path.join(self.outputpath,'StructurePcd.dec'))\r | |
521 | dscwrite = write2file(os.path.join(self.outputpath,'StructurePcd.dsc'))\r | |
522 | infwrite = write2file(os.path.join(self.outputpath, 'StructurePcd.inf'))\r | |
523 | conf = Config(self.Config)\r | |
524 | ids,title,info,header,inf=self.main()\r | |
525 | decwrite.add2file(decstatement)\r | |
526 | decwrite.add2file(header)\r | |
527 | infwrite.add2file(infstatement)\r | |
528 | infwrite.add2file(inf)\r | |
529 | dscwrite.add2file(dscstatement)\r | |
530 | for id in ids:\r | |
531 | dscwrite.add2file(conf.eval_id(id))\r | |
532 | if title_flag:\r | |
533 | dscwrite.add2file(title)\r | |
534 | title_flag=0\r | |
535 | if len(info) == 1:\r | |
536 | dscwrite.add2file(info)\r | |
537 | elif len(info) == 2:\r | |
538 | if info_flag:\r | |
539 | dscwrite.add2file(info[0])\r | |
540 | info_flag =0\r | |
541 | else:\r | |
542 | dscwrite.add2file(info[1])\r | |
543 | \r | |
544 | def del_repeat(self,List):\r | |
545 | if len(List) == 1 or len(List) == 0:\r | |
546 | return List\r | |
547 | else:\r | |
548 | if type(List[0]) != type('xxx'):\r | |
549 | alist=[]\r | |
550 | for i in range(len(List)):\r | |
551 | if i == 0:\r | |
552 | alist.append(List[0])\r | |
553 | else:\r | |
554 | plist = []\r | |
555 | for j in range(i):\r | |
556 | plist += List[j]\r | |
557 | alist.append(self.__del(list(set(plist)), List[i]))\r | |
558 | return alist\r | |
559 | else:\r | |
560 | return list(set(List))\r | |
561 | \r | |
562 | \r | |
563 | def __del(self,list1,list2):\r | |
564 | return list(set(list2).difference(set(list1)))\r | |
565 | \r | |
566 | def reverse_dict(self,dict):\r | |
567 | data={}\r | |
568 | for i in list(dict.items()):\r | |
569 | if i[1] not in list(data.keys()):\r | |
570 | data[i[1]]=[i[0]]\r | |
571 | else:\r | |
572 | data[i[1]].append(i[0])\r | |
573 | return data\r | |
574 | \r | |
575 | def read_list(self,list):\r | |
576 | title_list=[]\r | |
577 | info_list=[]\r | |
578 | for i in list[1]:\r | |
579 | title_list.append(i[0])\r | |
580 | for j in i[1]:\r | |
581 | info_list.append(j)\r | |
582 | return list[0],title_list,info_list\r | |
583 | \r | |
584 | def plus(self,list):\r | |
585 | nums=[]\r | |
586 | for i in list:\r | |
587 | if type(i) != type([0]):\r | |
588 | self.init += 1\r | |
589 | num = "0x%01x" % self.init\r | |
590 | j=i.replace('0xFCD00000',num.upper())\r | |
591 | nums.append(j)\r | |
592 | return nums\r | |
593 | \r | |
594 | class write2file(object):\r | |
595 | \r | |
596 | def __init__(self,Output):\r | |
597 | self.output=Output\r | |
598 | self.text=''\r | |
599 | if os.path.exists(self.output):\r | |
600 | os.remove(self.output)\r | |
601 | \r | |
602 | def add2file(self,content):\r | |
603 | self.text = ''\r | |
604 | with open(self.output,'a+') as file:\r | |
605 | file.write(self.__gen(content))\r | |
606 | \r | |
607 | def __gen(self,content):\r | |
608 | if type(content) == type(''):\r | |
609 | return content\r | |
610 | elif type(content) == type([0,0])or type(content) == type((0,0)):\r | |
611 | return self.__readlist(content)\r | |
612 | elif type(content) == type({0:0}):\r | |
613 | return self.__readdict(content)\r | |
614 | \r | |
615 | def __readlist(self,list):\r | |
616 | for i in list:\r | |
617 | if type(i) == type([0,0])or type(i) == type((0,0)):\r | |
618 | self.__readlist(i)\r | |
619 | elif type(i) == type('') :\r | |
620 | self.text +=i\r | |
621 | return self.text\r | |
622 | \r | |
623 | def __readdict(self,dict):\r | |
624 | content=list(dict.items())\r | |
625 | return self.__readlist(content)\r | |
626 | \r | |
627 | def stamp():\r | |
628 | return datetime.datetime.now()\r | |
629 | \r | |
630 | def dtime(start,end,id=None):\r | |
631 | if id:\r | |
632 | pass\r | |
633 | print("%s time:%s" % (id,str(end - start)))\r | |
634 | else:\r | |
635 | print("Total time:%s" %str(end-start)[:-7])\r | |
636 | \r | |
637 | \r | |
638 | def main():\r | |
639 | start = stamp()\r | |
640 | parser = argparse.ArgumentParser(prog = __prog__,\r | |
641 | description = __description__ + __copyright__,\r | |
642 | conflict_handler = 'resolve')\r | |
643 | parser.add_argument('-v', '--version', action = 'version',version = __version__, help="show program's version number and exit")\r | |
644 | parser.add_argument('-p', '--path', metavar='PATH', dest='path', help="platform build output directory")\r | |
645 | parser.add_argument('-c', '--config',metavar='FILENAME', dest='config', help="firmware configuration file")\r | |
646 | parser.add_argument('-o', '--outputdir', metavar='PATH', dest='output', help="output directoy")\r | |
647 | options = parser.parse_args()\r | |
648 | if options.config:\r | |
649 | if options.path:\r | |
650 | if options.output:\r | |
651 | run = mainprocess(options.path, options.config, options.output)\r | |
652 | print("Running...")\r | |
653 | run.write_all()\r | |
654 | if WARNING:\r | |
655 | warning = list(set(WARNING))\r | |
656 | for j in warning:\r | |
657 | print(j)\r | |
658 | if ERRORMSG:\r | |
659 | ERROR = list(set(ERRORMSG))\r | |
660 | with open("ERROR.log", 'w+') as error:\r | |
661 | for i in ERROR:\r | |
662 | error.write(i + '\n')\r | |
663 | print("Some error find, error log in ERROR.log")\r | |
664 | print('Finished, Output files in directory %s'%os.path.abspath(options.output))\r | |
665 | else:\r | |
666 | print('Error command, no output path, use -h for help')\r | |
667 | else:\r | |
668 | print('Error command, no build path input, use -h for help')\r | |
669 | else:\r | |
670 | print('Error command, no output file, use -h for help')\r | |
671 | end = stamp()\r | |
672 | dtime(start, end)\r | |
673 | \r | |
674 | if __name__ == '__main__':\r | |
675 | main()\r |