]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/ConvertFceToStructurePcd.py
BaseTools: Collect full Header files for struct finding.
[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
8501bb0c 373 self.usefuldir=set()\r
ef529e6a
LG
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
8501bb0c 384 self.usefuldir.add(path)\r
ef529e6a
LG
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
8501bb0c 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
8501bb0c
CC
424 head = self.headerfileset.get(name)\r
425 if head:\r
426 head = head.replace('\\','/')\r
427 header[struct] = head\r
ef529e6a 428 return header\r
8501bb0c
CC
429 @property\r
430 def headerfileset(self):\r
431 headerset = dict()\r
432 for root,dirs,files in os.walk(self.path):\r
433 for file in files:\r
434 if os.path.basename(file) == 'deps.txt':\r
435 with open(os.path.join(root,file),"r") as fr:\r
436 for line in fr.readlines():\r
437 headerset[os.path.basename(line).strip()] = line.strip()\r
438 return headerset\r
ef529e6a
LG
439\r
440 def makefile(self,filename):\r
441 re_format = re.compile(r'DEBUG_DIR.*(?:\S+Pkg)\\(.*\\%s)'%filename)\r
442 for i in self.usefuldir:\r
443 with open(os.path.join(i,'Makefile'),'r') as make:\r
444 read = make.read()\r
445 dir = re_format.findall(read)\r
446 if dir:\r
447 return dir[0]\r
8501bb0c 448 return None\r
ef529e6a
LG
449\r
450class mainprocess(object):\r
451\r
452 def __init__(self,InputPath,Config,OutputPath):\r
453 self.init = 0xFCD00000\r
454 self.inputpath = os.path.abspath(InputPath)\r
455 self.outputpath = os.path.abspath(OutputPath)\r
456 self.LST = PATH(self.inputpath)\r
457 self.lst_dict = self.LST.lst_inf()\r
458 self.Config = Config\r
459 self.attribute_dict = {'0x3': 'NV, BS', '0x7': 'NV, BS, RT'}\r
460 self.guid = GUID(self.inputpath)\r
461 self.header={}\r
462\r
463 def main(self):\r
464 conf=Config(self.Config)\r
465 config_dict=conf.config_parser() #get {'0_0':[offset,name,guid,value,attribute]...,'1_0':....}\r
466 lst=parser_lst(list(self.lst_dict.keys()))\r
467 efi_dict=lst.efivarstore_parser() #get {name:struct} form lst file\r
468 keys=sorted(config_dict.keys())\r
469 all_struct=lst.struct()\r
470 stru_lst=lst.struct_lst()\r
471 title_list=[]\r
472 info_list=[]\r
473 header_list=[]\r
474 inf_list =[]\r
475 for i in stru_lst:\r
476 tmp = self.LST.header(i)\r
477 self.header.update(tmp)\r
478 for id_key in keys:\r
479 tmp_id=[id_key] #['0_0',[(struct,[name...]),(struct,[name...])]]\r
480 tmp_info={} #{name:struct}\r
481 for section in config_dict[id_key]:\r
482 c_offset,c_name,c_guid,c_value,c_attribute = section\r
483 if c_name in efi_dict:\r
484 struct = efi_dict[c_name]\r
485 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
486 if struct in all_struct:\r
487 lstfile = stru_lst[struct]\r
488 struct_dict=all_struct[struct]\r
489 try:\r
490 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
491 except KeyError:\r
492 WARNING.append("Warning: No <HeaderFiles> for struct %s"%struct)\r
493 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
494 header_list.append(title2)\r
8501bb0c 495 elif struct not in lst._ignore:\r
ef529e6a
LG
496 struct_dict ={}\r
497 print("ERROR: Struct %s can't found in lst file" %struct)\r
498 ERRORMSG.append("ERROR: Struct %s can't found in lst file" %struct)\r
499 if c_offset in struct_dict:\r
500 offset_name=struct_dict[c_offset]\r
501 info = "%s%s.%s|%s\n"%(PCD_NAME,c_name,offset_name,c_value)\r
502 inf = "%s%s\n"%(PCD_NAME,c_name)\r
503 inf_list.append(inf)\r
504 tmp_info[info]=title\r
505 else:\r
506 print("ERROR: Can't find offset %s with struct name %s"%(c_offset,struct))\r
507 ERRORMSG.append("ERROR: Can't find offset %s with name %s"%(c_offset,struct))\r
508 else:\r
509 print("ERROR: Can't find name %s in lst file"%(c_name))\r
510 ERRORMSG.append("ERROR: Can't find name %s in lst file"%(c_name))\r
511 tmp_id.append(list(self.reverse_dict(tmp_info).items()))\r
512 id,tmp_title_list,tmp_info_list = self.read_list(tmp_id)\r
513 title_list +=tmp_title_list\r
514 info_list.append(tmp_info_list)\r
515 inf_list = self.del_repeat(inf_list)\r
516 header_list = self.plus(self.del_repeat(header_list))\r
517 title_all=list(set(title_list))\r
ce283fd6 518 info_list = self.remove_bracket(self.del_repeat(info_list))\r
ef529e6a
LG
519 for i in range(len(info_list)-1,-1,-1):\r
520 if len(info_list[i]) == 0:\r
521 info_list.remove(info_list[i])\r
d79b63c6
LL
522 for i in (inf_list, title_all, header_list):\r
523 i.sort()\r
ef529e6a
LG
524 return keys,title_all,info_list,header_list,inf_list\r
525\r
ce283fd6
LG
526 def remove_bracket(self,List):\r
527 for i in List:\r
528 for j in i:\r
529 tmp = j.split("|")\r
530 if (('L"' in j) and ("[" in j)) or (tmp[1].strip() == '{0x0, 0x0}'):\r
531 tmp[0] = tmp[0][:tmp[0].index('[')]\r
532 List[List.index(i)][i.index(j)] = "|".join(tmp)\r
533 else:\r
534 List[List.index(i)][i.index(j)] = j\r
d79b63c6
LL
535 for i in List:\r
536 if type(i) == type([0,0]):\r
537 i.sort()\r
ce283fd6 538 return List\r
ef529e6a
LG
539\r
540 def write_all(self):\r
541 title_flag=1\r
542 info_flag=1\r
543 if not os.path.isdir(self.outputpath):\r
544 os.makedirs(self.outputpath)\r
545 decwrite = write2file(os.path.join(self.outputpath,'StructurePcd.dec'))\r
546 dscwrite = write2file(os.path.join(self.outputpath,'StructurePcd.dsc'))\r
547 infwrite = write2file(os.path.join(self.outputpath, 'StructurePcd.inf'))\r
548 conf = Config(self.Config)\r
549 ids,title,info,header,inf=self.main()\r
550 decwrite.add2file(decstatement)\r
551 decwrite.add2file(header)\r
552 infwrite.add2file(infstatement)\r
553 infwrite.add2file(inf)\r
554 dscwrite.add2file(dscstatement)\r
555 for id in ids:\r
556 dscwrite.add2file(conf.eval_id(id))\r
557 if title_flag:\r
558 dscwrite.add2file(title)\r
559 title_flag=0\r
560 if len(info) == 1:\r
561 dscwrite.add2file(info)\r
562 elif len(info) == 2:\r
563 if info_flag:\r
564 dscwrite.add2file(info[0])\r
565 info_flag =0\r
566 else:\r
567 dscwrite.add2file(info[1])\r
568\r
569 def del_repeat(self,List):\r
570 if len(List) == 1 or len(List) == 0:\r
571 return List\r
572 else:\r
573 if type(List[0]) != type('xxx'):\r
574 alist=[]\r
575 for i in range(len(List)):\r
576 if i == 0:\r
577 alist.append(List[0])\r
578 else:\r
579 plist = []\r
580 for j in range(i):\r
581 plist += List[j]\r
582 alist.append(self.__del(list(set(plist)), List[i]))\r
583 return alist\r
584 else:\r
585 return list(set(List))\r
586\r
587\r
588 def __del(self,list1,list2):\r
589 return list(set(list2).difference(set(list1)))\r
590\r
591 def reverse_dict(self,dict):\r
592 data={}\r
593 for i in list(dict.items()):\r
594 if i[1] not in list(data.keys()):\r
595 data[i[1]]=[i[0]]\r
596 else:\r
597 data[i[1]].append(i[0])\r
598 return data\r
599\r
600 def read_list(self,list):\r
601 title_list=[]\r
602 info_list=[]\r
603 for i in list[1]:\r
604 title_list.append(i[0])\r
605 for j in i[1]:\r
606 info_list.append(j)\r
607 return list[0],title_list,info_list\r
608\r
609 def plus(self,list):\r
610 nums=[]\r
611 for i in list:\r
612 if type(i) != type([0]):\r
613 self.init += 1\r
614 num = "0x%01x" % self.init\r
615 j=i.replace('0xFCD00000',num.upper())\r
616 nums.append(j)\r
617 return nums\r
618\r
619class write2file(object):\r
620\r
621 def __init__(self,Output):\r
622 self.output=Output\r
623 self.text=''\r
624 if os.path.exists(self.output):\r
625 os.remove(self.output)\r
626\r
627 def add2file(self,content):\r
628 self.text = ''\r
629 with open(self.output,'a+') as file:\r
630 file.write(self.__gen(content))\r
631\r
632 def __gen(self,content):\r
633 if type(content) == type(''):\r
634 return content\r
635 elif type(content) == type([0,0])or type(content) == type((0,0)):\r
636 return self.__readlist(content)\r
637 elif type(content) == type({0:0}):\r
638 return self.__readdict(content)\r
639\r
640 def __readlist(self,list):\r
641 for i in list:\r
642 if type(i) == type([0,0])or type(i) == type((0,0)):\r
643 self.__readlist(i)\r
644 elif type(i) == type('') :\r
645 self.text +=i\r
646 return self.text\r
647\r
648 def __readdict(self,dict):\r
649 content=list(dict.items())\r
650 return self.__readlist(content)\r
651\r
652def stamp():\r
653 return datetime.datetime.now()\r
654\r
655def dtime(start,end,id=None):\r
656 if id:\r
657 pass\r
658 print("%s time:%s" % (id,str(end - start)))\r
659 else:\r
660 print("Total time:%s" %str(end-start)[:-7])\r
661\r
662\r
663def main():\r
664 start = stamp()\r
665 parser = argparse.ArgumentParser(prog = __prog__,\r
666 description = __description__ + __copyright__,\r
667 conflict_handler = 'resolve')\r
668 parser.add_argument('-v', '--version', action = 'version',version = __version__, help="show program's version number and exit")\r
669 parser.add_argument('-p', '--path', metavar='PATH', dest='path', help="platform build output directory")\r
670 parser.add_argument('-c', '--config',metavar='FILENAME', dest='config', help="firmware configuration file")\r
671 parser.add_argument('-o', '--outputdir', metavar='PATH', dest='output', help="output directoy")\r
672 options = parser.parse_args()\r
673 if options.config:\r
674 if options.path:\r
675 if options.output:\r
676 run = mainprocess(options.path, options.config, options.output)\r
677 print("Running...")\r
678 run.write_all()\r
679 if WARNING:\r
680 warning = list(set(WARNING))\r
681 for j in warning:\r
682 print(j)\r
683 if ERRORMSG:\r
684 ERROR = list(set(ERRORMSG))\r
685 with open("ERROR.log", 'w+') as error:\r
686 for i in ERROR:\r
687 error.write(i + '\n')\r
688 print("Some error find, error log in ERROR.log")\r
689 print('Finished, Output files in directory %s'%os.path.abspath(options.output))\r
690 else:\r
691 print('Error command, no output path, use -h for help')\r
692 else:\r
693 print('Error command, no build path input, use -h for help')\r
694 else:\r
695 print('Error command, no output file, use -h for help')\r
696 end = stamp()\r
697 dtime(start, end)\r
698\r
699if __name__ == '__main__':\r
700 main()\r