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