]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/ConvertFceToStructurePcd.py
MdeModulePkg/FaultTolerantWriteDxe: implement standalone MM version
[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
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
19ConvertFceToStructurePcd\r
20'''\r
21\r
22import re\r
23import os\r
24import datetime\r
25import 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
36dscstatement='''[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
50decstatement = '''[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
56infstatement = '''[Pcd]\r
57'''\r
58\r
59SECTION='PcdsDynamicHii'\r
60PCD_NAME='gStructPcdTokenSpaceGuid.Pcd'\r
61\r
62WARNING=[]\r
63ERRORMSG=[]\r
64\r
65class 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
213class 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
ce283fd6
LG
306 if list1[-1] == '""':\r
307 value = "{0x0, 0x0}"\r
308 else:\r
309 value = 'L%s' % list1[-1]\r
ef529e6a
LG
310 elif list1[first_num + 1] == 'ORDERED_LIST': # parser ORDERED_LIST\r
311 value_total = int(list1[first_num + 2])\r
312 list2 = list1[-value_total:]\r
313 tmp = []\r
314 line = ''\r
315 for i in list2:\r
316 if len(i) % 2 == 0 and len(i) != 2:\r
317 for m in range(0, len(i) // 2):\r
318 tmp.append('0x%02x' % (int('0x%s' % i, 16) >> m * 8 & 0xff))\r
319 else:\r
320 tmp.append('0x%s' % i)\r
321 for i in tmp:\r
322 line += '%s,' % i\r
323 value = '{%s}' % line[:-1]\r
324 else:\r
325 value = "0x%01x" % int(list1[-1], 16)\r
326 return value\r
327\r
328\r
329#parser Guid file, get guid name form guid value\r
330class GUID(object):\r
331\r
332 def __init__(self,path):\r
333 self.path = path\r
334 self.guidfile = self.gfile()\r
335 self.guiddict = self.guid_dict()\r
336\r
337 def gfile(self):\r
338 for root, dir, file in os.walk(self.path, topdown=True, followlinks=False):\r
339 if 'FV' in dir:\r
340 gfile = os.path.join(root,'Fv','Guid.xref')\r
341 if os.path.isfile(gfile):\r
342 return gfile\r
343 else:\r
344 print("ERROR: Guid.xref file not found")\r
345 ERRORMSG.append("ERROR: Guid.xref file not found")\r
346 exit()\r
347\r
348 def guid_dict(self):\r
349 guiddict={}\r
350 with open(self.guidfile,'r') as file:\r
351 lines = file.readlines()\r
352 guidinfo=lines\r
353 for line in guidinfo:\r
354 list=line.strip().split(' ')\r
355 if list:\r
356 if len(list)>1:\r
357 guiddict[list[0].upper()]=list[1]\r
358 elif list[0] != ''and len(list)==1:\r
359 print("Error: line %s can't be parser in %s"%(line.strip(),self.guidfile))\r
360 ERRORMSG.append("Error: line %s can't be parser in %s"%(line.strip(),self.guidfile))\r
361 else:\r
362 print("ERROR: No data in %s" %self.guidfile)\r
363 ERRORMSG.append("ERROR: No data in %s" %self.guidfile)\r
364 return guiddict\r
365\r
366 def guid_parser(self,guid):\r
367 if guid.upper() in self.guiddict:\r
368 return self.guiddict[guid.upper()]\r
369 else:\r
370 print("ERROR: GUID %s not found in file %s"%(guid, self.guidfile))\r
371 ERRORMSG.append("ERROR: GUID %s not found in file %s"%(guid, self.guidfile))\r
372 return guid\r
373\r
374class PATH(object):\r
375\r
376 def __init__(self,path):\r
377 self.path=path\r
378 self.rootdir=self.get_root_dir()\r
379 self.usefuldir=[]\r
380 self.lstinf = {}\r
381 for path in self.rootdir:\r
382 for o_root, o_dir, o_file in os.walk(os.path.join(path, "OUTPUT"), topdown=True, followlinks=False):\r
383 for INF in o_file:\r
384 if os.path.splitext(INF)[1] == '.inf':\r
385 for l_root, l_dir, l_file in os.walk(os.path.join(path, "DEBUG"), topdown=True,\r
386 followlinks=False):\r
387 for LST in l_file:\r
388 if os.path.splitext(LST)[1] == '.lst':\r
389 self.lstinf[os.path.join(l_root, LST)] = os.path.join(o_root, INF)\r
390 self.usefuldir.append(path)\r
391\r
392 def get_root_dir(self):\r
393 rootdir=[]\r
394 for root,dir,file in os.walk(self.path,topdown=True,followlinks=False):\r
395 if "OUTPUT" in root:\r
396 updir=root.split("OUTPUT",1)[0]\r
397 rootdir.append(updir)\r
398 rootdir=list(set(rootdir))\r
399 return rootdir\r
400\r
401 def lst_inf(self):\r
402 return self.lstinf\r
403\r
404 def package(self):\r
405 package={}\r
406 package_re=re.compile(r'Packages\.\w+]\n(.*)',re.S)\r
407 for i in list(self.lstinf.values()):\r
408 with open(i,'r') as inf:\r
409 read=inf.read()\r
410 section=read.split('[')\r
411 for j in section:\r
412 p=package_re.findall(j)\r
413 if p:\r
414 package[i]=p[0].rstrip()\r
415 return package\r
416\r
417 def header(self,struct):\r
418 header={}\r
591a44c0 419 head_re = re.compile('typedef.*} %s;[\n]+(.*?)(?:typedef|formset)'%struct,re.M|re.S)\r
ef529e6a
LG
420 head_re2 = re.compile(r'#line[\s\d]+"(\S+h)"')\r
421 for i in list(self.lstinf.keys()):\r
422 with open(i,'r') as lst:\r
423 read = lst.read()\r
424 h = head_re.findall(read)\r
425 if h:\r
426 head=head_re2.findall(h[0])\r
427 if head:\r
428 format = head[0].replace('\\\\','/').replace('\\','/')\r
429 name =format.split('/')[-1]\r
430 head = self.makefile(name).replace('\\','/')\r
431 header[struct] = head\r
432 return header\r
433\r
434 def makefile(self,filename):\r
435 re_format = re.compile(r'DEBUG_DIR.*(?:\S+Pkg)\\(.*\\%s)'%filename)\r
436 for i in self.usefuldir:\r
437 with open(os.path.join(i,'Makefile'),'r') as make:\r
438 read = make.read()\r
439 dir = re_format.findall(read)\r
440 if dir:\r
441 return dir[0]\r
442\r
443class mainprocess(object):\r
444\r
445 def __init__(self,InputPath,Config,OutputPath):\r
446 self.init = 0xFCD00000\r
447 self.inputpath = os.path.abspath(InputPath)\r
448 self.outputpath = os.path.abspath(OutputPath)\r
449 self.LST = PATH(self.inputpath)\r
450 self.lst_dict = self.LST.lst_inf()\r
451 self.Config = Config\r
452 self.attribute_dict = {'0x3': 'NV, BS', '0x7': 'NV, BS, RT'}\r
453 self.guid = GUID(self.inputpath)\r
454 self.header={}\r
455\r
456 def main(self):\r
457 conf=Config(self.Config)\r
458 config_dict=conf.config_parser() #get {'0_0':[offset,name,guid,value,attribute]...,'1_0':....}\r
459 lst=parser_lst(list(self.lst_dict.keys()))\r
460 efi_dict=lst.efivarstore_parser() #get {name:struct} form lst file\r
461 keys=sorted(config_dict.keys())\r
462 all_struct=lst.struct()\r
463 stru_lst=lst.struct_lst()\r
464 title_list=[]\r
465 info_list=[]\r
466 header_list=[]\r
467 inf_list =[]\r
468 for i in stru_lst:\r
469 tmp = self.LST.header(i)\r
470 self.header.update(tmp)\r
471 for id_key in keys:\r
472 tmp_id=[id_key] #['0_0',[(struct,[name...]),(struct,[name...])]]\r
473 tmp_info={} #{name:struct}\r
474 for section in config_dict[id_key]:\r
475 c_offset,c_name,c_guid,c_value,c_attribute = section\r
476 if c_name in efi_dict:\r
477 struct = efi_dict[c_name]\r
478 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
479 if struct in all_struct:\r
480 lstfile = stru_lst[struct]\r
481 struct_dict=all_struct[struct]\r
482 try:\r
483 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
484 except KeyError:\r
485 WARNING.append("Warning: No <HeaderFiles> for struct %s"%struct)\r
486 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
487 header_list.append(title2)\r
488 else:\r
489 struct_dict ={}\r
490 print("ERROR: Struct %s can't found in lst file" %struct)\r
491 ERRORMSG.append("ERROR: Struct %s can't found in lst file" %struct)\r
492 if c_offset in struct_dict:\r
493 offset_name=struct_dict[c_offset]\r
494 info = "%s%s.%s|%s\n"%(PCD_NAME,c_name,offset_name,c_value)\r
495 inf = "%s%s\n"%(PCD_NAME,c_name)\r
496 inf_list.append(inf)\r
497 tmp_info[info]=title\r
498 else:\r
499 print("ERROR: Can't find offset %s with struct name %s"%(c_offset,struct))\r
500 ERRORMSG.append("ERROR: Can't find offset %s with name %s"%(c_offset,struct))\r
501 else:\r
502 print("ERROR: Can't find name %s in lst file"%(c_name))\r
503 ERRORMSG.append("ERROR: Can't find name %s in lst file"%(c_name))\r
504 tmp_id.append(list(self.reverse_dict(tmp_info).items()))\r
505 id,tmp_title_list,tmp_info_list = self.read_list(tmp_id)\r
506 title_list +=tmp_title_list\r
507 info_list.append(tmp_info_list)\r
508 inf_list = self.del_repeat(inf_list)\r
509 header_list = self.plus(self.del_repeat(header_list))\r
510 title_all=list(set(title_list))\r
ce283fd6 511 info_list = self.remove_bracket(self.del_repeat(info_list))\r
ef529e6a
LG
512 for i in range(len(info_list)-1,-1,-1):\r
513 if len(info_list[i]) == 0:\r
514 info_list.remove(info_list[i])\r
515 return keys,title_all,info_list,header_list,inf_list\r
516\r
ce283fd6
LG
517 def remove_bracket(self,List):\r
518 for i in List:\r
519 for j in i:\r
520 tmp = j.split("|")\r
521 if (('L"' in j) and ("[" in j)) or (tmp[1].strip() == '{0x0, 0x0}'):\r
522 tmp[0] = tmp[0][:tmp[0].index('[')]\r
523 List[List.index(i)][i.index(j)] = "|".join(tmp)\r
524 else:\r
525 List[List.index(i)][i.index(j)] = j\r
526 return List\r
ef529e6a
LG
527\r
528 def write_all(self):\r
529 title_flag=1\r
530 info_flag=1\r
531 if not os.path.isdir(self.outputpath):\r
532 os.makedirs(self.outputpath)\r
533 decwrite = write2file(os.path.join(self.outputpath,'StructurePcd.dec'))\r
534 dscwrite = write2file(os.path.join(self.outputpath,'StructurePcd.dsc'))\r
535 infwrite = write2file(os.path.join(self.outputpath, 'StructurePcd.inf'))\r
536 conf = Config(self.Config)\r
537 ids,title,info,header,inf=self.main()\r
538 decwrite.add2file(decstatement)\r
539 decwrite.add2file(header)\r
540 infwrite.add2file(infstatement)\r
541 infwrite.add2file(inf)\r
542 dscwrite.add2file(dscstatement)\r
543 for id in ids:\r
544 dscwrite.add2file(conf.eval_id(id))\r
545 if title_flag:\r
546 dscwrite.add2file(title)\r
547 title_flag=0\r
548 if len(info) == 1:\r
549 dscwrite.add2file(info)\r
550 elif len(info) == 2:\r
551 if info_flag:\r
552 dscwrite.add2file(info[0])\r
553 info_flag =0\r
554 else:\r
555 dscwrite.add2file(info[1])\r
556\r
557 def del_repeat(self,List):\r
558 if len(List) == 1 or len(List) == 0:\r
559 return List\r
560 else:\r
561 if type(List[0]) != type('xxx'):\r
562 alist=[]\r
563 for i in range(len(List)):\r
564 if i == 0:\r
565 alist.append(List[0])\r
566 else:\r
567 plist = []\r
568 for j in range(i):\r
569 plist += List[j]\r
570 alist.append(self.__del(list(set(plist)), List[i]))\r
571 return alist\r
572 else:\r
573 return list(set(List))\r
574\r
575\r
576 def __del(self,list1,list2):\r
577 return list(set(list2).difference(set(list1)))\r
578\r
579 def reverse_dict(self,dict):\r
580 data={}\r
581 for i in list(dict.items()):\r
582 if i[1] not in list(data.keys()):\r
583 data[i[1]]=[i[0]]\r
584 else:\r
585 data[i[1]].append(i[0])\r
586 return data\r
587\r
588 def read_list(self,list):\r
589 title_list=[]\r
590 info_list=[]\r
591 for i in list[1]:\r
592 title_list.append(i[0])\r
593 for j in i[1]:\r
594 info_list.append(j)\r
595 return list[0],title_list,info_list\r
596\r
597 def plus(self,list):\r
598 nums=[]\r
599 for i in list:\r
600 if type(i) != type([0]):\r
601 self.init += 1\r
602 num = "0x%01x" % self.init\r
603 j=i.replace('0xFCD00000',num.upper())\r
604 nums.append(j)\r
605 return nums\r
606\r
607class write2file(object):\r
608\r
609 def __init__(self,Output):\r
610 self.output=Output\r
611 self.text=''\r
612 if os.path.exists(self.output):\r
613 os.remove(self.output)\r
614\r
615 def add2file(self,content):\r
616 self.text = ''\r
617 with open(self.output,'a+') as file:\r
618 file.write(self.__gen(content))\r
619\r
620 def __gen(self,content):\r
621 if type(content) == type(''):\r
622 return content\r
623 elif type(content) == type([0,0])or type(content) == type((0,0)):\r
624 return self.__readlist(content)\r
625 elif type(content) == type({0:0}):\r
626 return self.__readdict(content)\r
627\r
628 def __readlist(self,list):\r
629 for i in list:\r
630 if type(i) == type([0,0])or type(i) == type((0,0)):\r
631 self.__readlist(i)\r
632 elif type(i) == type('') :\r
633 self.text +=i\r
634 return self.text\r
635\r
636 def __readdict(self,dict):\r
637 content=list(dict.items())\r
638 return self.__readlist(content)\r
639\r
640def stamp():\r
641 return datetime.datetime.now()\r
642\r
643def dtime(start,end,id=None):\r
644 if id:\r
645 pass\r
646 print("%s time:%s" % (id,str(end - start)))\r
647 else:\r
648 print("Total time:%s" %str(end-start)[:-7])\r
649\r
650\r
651def main():\r
652 start = stamp()\r
653 parser = argparse.ArgumentParser(prog = __prog__,\r
654 description = __description__ + __copyright__,\r
655 conflict_handler = 'resolve')\r
656 parser.add_argument('-v', '--version', action = 'version',version = __version__, help="show program's version number and exit")\r
657 parser.add_argument('-p', '--path', metavar='PATH', dest='path', help="platform build output directory")\r
658 parser.add_argument('-c', '--config',metavar='FILENAME', dest='config', help="firmware configuration file")\r
659 parser.add_argument('-o', '--outputdir', metavar='PATH', dest='output', help="output directoy")\r
660 options = parser.parse_args()\r
661 if options.config:\r
662 if options.path:\r
663 if options.output:\r
664 run = mainprocess(options.path, options.config, options.output)\r
665 print("Running...")\r
666 run.write_all()\r
667 if WARNING:\r
668 warning = list(set(WARNING))\r
669 for j in warning:\r
670 print(j)\r
671 if ERRORMSG:\r
672 ERROR = list(set(ERRORMSG))\r
673 with open("ERROR.log", 'w+') as error:\r
674 for i in ERROR:\r
675 error.write(i + '\n')\r
676 print("Some error find, error log in ERROR.log")\r
677 print('Finished, Output files in directory %s'%os.path.abspath(options.output))\r
678 else:\r
679 print('Error command, no output path, use -h for help')\r
680 else:\r
681 print('Error command, no build path input, use -h for help')\r
682 else:\r
683 print('Error command, no output file, use -h for help')\r
684 end = stamp()\r
685 dtime(start, end)\r
686\r
687if __name__ == '__main__':\r
688 main()\r