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