]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2Pkg/Tools/GenCfgOpt.py
IntelFsp2Pkg/GenCfgOpt.py: support FixedAtBuild PCD
[mirror_edk2.git] / IntelFsp2Pkg / Tools / GenCfgOpt.py
1 ## @ GenCfgOpt.py
2 #
3 # Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
4 # This program and the accompanying materials are licensed and made available under
5 # the terms and conditions of the BSD License that accompanies this distribution.
6 # The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php.
8 #
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 #
12 ##
13
14 import os
15 import re
16 import sys
17 import struct
18 from datetime import date
19
20 # Generated file copyright header
21
22 __copyright_txt__ = """## @file
23 #
24 # THIS IS AUTO-GENERATED FILE BY BUILD TOOLS AND PLEASE DO NOT MAKE MODIFICATION.
25 #
26 # This file lists all VPD informations for a platform collected by build.exe.
27 #
28 # Copyright (c) %4d, Intel Corporation. All rights reserved.<BR>
29 # This program and the accompanying materials
30 # are licensed and made available under the terms and conditions of the BSD License
31 # which accompanies this distribution. The full text of the license may be found at
32 # http://opensource.org/licenses/bsd-license.php
33 #
34 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
35 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
36 #
37 """
38
39 __copyright_bsf__ = """/** @file
40
41 Boot Setting File for Platform Configuration.
42
43 Copyright (c) %4d, Intel Corporation. All rights reserved.<BR>
44 This program and the accompanying materials
45 are licensed and made available under the terms and conditions of the BSD License
46 which accompanies this distribution. The full text of the license may be found at
47 http://opensource.org/licenses/bsd-license.php
48
49 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
50 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
51
52 This file is automatically generated. Please do NOT modify !!!
53
54 **/
55
56 """
57
58 __copyright_h__ = """/** @file
59
60 Copyright (c) %4d, Intel Corporation. All rights reserved.<BR>
61
62 Redistribution and use in source and binary forms, with or without modification,
63 are permitted provided that the following conditions are met:
64
65 * Redistributions of source code must retain the above copyright notice, this
66 list of conditions and the following disclaimer.
67 * Redistributions in binary form must reproduce the above copyright notice, this
68 list of conditions and the following disclaimer in the documentation and/or
69 other materials provided with the distribution.
70 * Neither the name of Intel Corporation nor the names of its contributors may
71 be used to endorse or promote products derived from this software without
72 specific prior written permission.
73
74 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
75 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
78 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
79 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
80 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
81 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
82 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
83 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
84 THE POSSIBILITY OF SUCH DAMAGE.
85
86 This file is automatically generated. Please do NOT modify !!!
87
88 **/
89 """
90
91 class CLogicalExpression:
92 def __init__(self):
93 self.index = 0
94 self.string = ''
95
96 def errExit(self, err = ''):
97 print "ERROR: Express parsing for:"
98 print " %s" % self.string
99 print " %s^" % (' ' * self.index)
100 if err:
101 print "INFO : %s" % err
102 raise SystemExit
103
104 def getNonNumber (self, n1, n2):
105 if not n1.isdigit():
106 return n1
107 if not n2.isdigit():
108 return n2
109 return None
110
111 def getCurr(self, lens = 1):
112 try:
113 if lens == -1:
114 return self.string[self.index :]
115 else:
116 if self.index + lens > len(self.string):
117 lens = len(self.string) - self.index
118 return self.string[self.index : self.index + lens]
119 except Exception:
120 return ''
121
122 def isLast(self):
123 return self.index == len(self.string)
124
125 def moveNext(self, len = 1):
126 self.index += len
127
128 def skipSpace(self):
129 while not self.isLast():
130 if self.getCurr() in ' \t':
131 self.moveNext()
132 else:
133 return
134
135 def normNumber (self, val):
136 return True if val else False
137
138 def getNumber(self, var):
139 var = var.strip()
140 if re.match('^0x[a-fA-F0-9]+$', var):
141 value = int(var, 16)
142 elif re.match('^[+-]?\d+$', var):
143 value = int(var, 10)
144 else:
145 value = None
146 return value
147
148 def parseValue(self):
149 self.skipSpace()
150 var = ''
151 while not self.isLast():
152 char = self.getCurr()
153 if re.match('^[\w.]', char):
154 var += char
155 self.moveNext()
156 else:
157 break
158 val = self.getNumber(var)
159 if val is None:
160 value = var
161 else:
162 value = "%d" % val
163 return value
164
165 def parseSingleOp(self):
166 self.skipSpace()
167 if re.match('^NOT\W', self.getCurr(-1)):
168 self.moveNext(3)
169 op = self.parseBrace()
170 val = self.getNumber (op)
171 if val is None:
172 self.errExit ("'%s' is not a number" % op)
173 return "%d" % (not self.normNumber(int(op)))
174 else:
175 return self.parseValue()
176
177 def parseBrace(self):
178 self.skipSpace()
179 char = self.getCurr()
180 if char == '(':
181 self.moveNext()
182 value = self.parseExpr()
183 self.skipSpace()
184 if self.getCurr() != ')':
185 self.errExit ("Expecting closing brace or operator")
186 self.moveNext()
187 return value
188 else:
189 value = self.parseSingleOp()
190 return value
191
192 def parseCompare(self):
193 value = self.parseBrace()
194 while True:
195 self.skipSpace()
196 char = self.getCurr()
197 if char in ['<', '>']:
198 self.moveNext()
199 next = self.getCurr()
200 if next == '=':
201 op = char + next
202 self.moveNext()
203 else:
204 op = char
205 result = self.parseBrace()
206 test = self.getNonNumber(result, value)
207 if test is None:
208 value = "%d" % self.normNumber(eval (value + op + result))
209 else:
210 self.errExit ("'%s' is not a valid number for comparision" % test)
211 elif char in ['=', '!']:
212 op = self.getCurr(2)
213 if op in ['==', '!=']:
214 self.moveNext(2)
215 result = self.parseBrace()
216 test = self.getNonNumber(result, value)
217 if test is None:
218 value = "%d" % self.normNumber((eval (value + op + result)))
219 else:
220 value = "%d" % self.normNumber(eval ("'" + value + "'" + op + "'" + result + "'"))
221 else:
222 break
223 else:
224 break
225 return value
226
227 def parseAnd(self):
228 value = self.parseCompare()
229 while True:
230 self.skipSpace()
231 if re.match('^AND\W', self.getCurr(-1)):
232 self.moveNext(3)
233 result = self.parseCompare()
234 test = self.getNonNumber(result, value)
235 if test is None:
236 value = "%d" % self.normNumber(int(value) & int(result))
237 else:
238 self.errExit ("'%s' is not a valid op number for AND" % test)
239 else:
240 break
241 return value
242
243 def parseOrXor(self):
244 value = self.parseAnd()
245 op = None
246 while True:
247 self.skipSpace()
248 op = None
249 if re.match('^XOR\W', self.getCurr(-1)):
250 self.moveNext(3)
251 op = '^'
252 elif re.match('^OR\W', self.getCurr(-1)):
253 self.moveNext(2)
254 op = '|'
255 else:
256 break
257 if op:
258 result = self.parseAnd()
259 test = self.getNonNumber(result, value)
260 if test is None:
261 value = "%d" % self.normNumber(eval (value + op + result))
262 else:
263 self.errExit ("'%s' is not a valid op number for XOR/OR" % test)
264 return value
265
266 def parseExpr(self):
267 return self.parseOrXor()
268
269 def getResult(self):
270 value = self.parseExpr()
271 self.skipSpace()
272 if not self.isLast():
273 self.errExit ("Unexpected character found '%s'" % self.getCurr())
274 test = self.getNumber(value)
275 if test is None:
276 self.errExit ("Result '%s' is not a number" % value)
277 return int(value)
278
279 def evaluateExpress (self, Expr):
280 self.index = 0
281 self.string = Expr
282 if self.getResult():
283 Result = True
284 else:
285 Result = False
286 return Result
287
288 class CGenCfgOpt:
289 def __init__(self):
290 self.Debug = False
291 self.Error = ''
292
293 self._GlobalDataDef = """
294 GlobalDataDef
295 SKUID = 0, "DEFAULT"
296 EndGlobalData
297
298 """
299 self._BuidinOptionTxt = """
300 List &EN_DIS
301 Selection 0x1 , "Enabled"
302 Selection 0x0 , "Disabled"
303 EndList
304
305 """
306
307 self._BsfKeyList = ['FIND','NAME','HELP','TYPE','PAGE','OPTION','ORDER']
308 self._HdrKeyList = ['HEADER','STRUCT', 'EMBED', 'COMMENT']
309 self._BuidinOption = {'$EN_DIS' : 'EN_DIS'}
310
311 self._MacroDict = {}
312 self._PcdsDict = {}
313 self._CfgBlkDict = {}
314 self._CfgPageDict = {}
315 self._CfgItemList = []
316 self._DscFile = ''
317 self._FvDir = ''
318 self._MapVer = 0
319
320 def ParseMacros (self, MacroDefStr):
321 # ['-DABC=1', '-D', 'CFG_DEBUG=1', '-D', 'CFG_OUTDIR=Build']
322 self._MacroDict = {}
323 IsExpression = False
324 for Macro in MacroDefStr:
325 if Macro.startswith('-D'):
326 IsExpression = True
327 if len(Macro) > 2:
328 Macro = Macro[2:]
329 else :
330 continue
331 if IsExpression:
332 IsExpression = False
333 Match = re.match("(\w+)=(.+)", Macro)
334 if Match:
335 self._MacroDict[Match.group(1)] = Match.group(2)
336 else:
337 Match = re.match("(\w+)", Macro)
338 if Match:
339 self._MacroDict[Match.group(1)] = ''
340 if len(self._MacroDict) == 0:
341 Error = 1
342 else:
343 Error = 0
344 if self.Debug:
345 print "INFO : Macro dictionary:"
346 for Each in self._MacroDict:
347 print " $(%s) = [ %s ]" % (Each , self._MacroDict[Each])
348 return Error
349
350 def EvaulateIfdef (self, Macro):
351 Result = Macro in self._MacroDict
352 if self.Debug:
353 print "INFO : Eval Ifdef [%s] : %s" % (Macro, Result)
354 return Result
355
356 def ExpandMacros (self, Input):
357 Line = Input
358 Match = re.findall("\$\(\w+\)", Input)
359 if Match:
360 for Each in Match:
361 Variable = Each[2:-1]
362 if Variable in self._MacroDict:
363 Line = Line.replace(Each, self._MacroDict[Variable])
364 else:
365 if self.Debug:
366 print "WARN : %s is not defined" % Each
367 Line = Line.replace(Each, Each[2:-1])
368 return Line
369
370 def ExpandPcds (self, Input):
371 Line = Input
372 Match = re.findall("(\w+\.\w+)", Input)
373 if Match:
374 for PcdName in Match:
375 if PcdName in self._PcdsDict:
376 Line = Line.replace(PcdName, self._PcdsDict[PcdName])
377 else:
378 if self.Debug:
379 print "WARN : %s is not defined" % PcdName
380 return Line
381
382 def EvaluateExpress (self, Expr):
383 ExpExpr = self.ExpandPcds(Expr)
384 ExpExpr = self.ExpandMacros(ExpExpr)
385 LogExpr = CLogicalExpression()
386 Result = LogExpr.evaluateExpress (ExpExpr)
387 if self.Debug:
388 print "INFO : Eval Express [%s] : %s" % (Expr, Result)
389 return Result
390
391 def FormatListValue(self, ConfigDict):
392 Struct = ConfigDict['struct']
393 if Struct not in ['UINT8','UINT16','UINT32','UINT64']:
394 return
395
396 dataarray = []
397 binlist = ConfigDict['value'][1:-1].split(',')
398 for each in binlist:
399 each = each.strip()
400 if each.startswith('0x'):
401 value = int(each, 16)
402 else:
403 value = int(each)
404 dataarray.append(value)
405
406 unit = int(Struct[4:]) / 8
407 if int(ConfigDict['length']) != unit * len(dataarray):
408 raise Exception("Array size is not proper for '%s' !" % ConfigDict['cname'])
409
410 bytearray = []
411 for each in dataarray:
412 value = each
413 for loop in xrange(unit):
414 bytearray.append("0x%02X" % (value & 0xFF))
415 value = value >> 8
416 newvalue = '{' + ','.join(bytearray) + '}'
417 ConfigDict['value'] = newvalue
418 return ""
419
420 def ParseDscFile (self, DscFile, FvDir):
421 self._CfgItemList = []
422 self._CfgPageDict = {}
423 self._CfgBlkDict = {}
424 self._DscFile = DscFile
425 self._FvDir = FvDir
426
427 IsDefSect = False
428 IsPcdSect = False
429 IsUpdSect = False
430 IsVpdSect = False
431
432 IfStack = []
433 ElifStack = []
434 Error = 0
435 ConfigDict = {}
436
437 DscFd = open(DscFile, "r")
438 DscLines = DscFd.readlines()
439 DscFd.close()
440
441 while len(DscLines):
442 DscLine = DscLines.pop(0).strip()
443 Handle = False
444 Match = re.match("^\[(.+)\]", DscLine)
445 if Match is not None:
446 IsDefSect = False
447 IsPcdSect = False
448 IsVpdSect = False
449 IsUpdSect = False
450 if Match.group(1).lower() == "Defines".lower():
451 IsDefSect = True
452 if (Match.group(1).lower() == "PcdsFeatureFlag".lower() or Match.group(1).lower() == "PcdsFixedAtBuild".lower()):
453 IsPcdSect = True
454 elif Match.group(1).lower() == "PcdsDynamicVpd.Upd".lower():
455 ConfigDict = {}
456 ConfigDict['header'] = 'ON'
457 ConfigDict['region'] = 'UPD'
458 ConfigDict['order'] = -1
459 ConfigDict['page'] = ''
460 ConfigDict['name'] = ''
461 ConfigDict['find'] = ''
462 ConfigDict['struct'] = ''
463 ConfigDict['embed'] = ''
464 ConfigDict['comment'] = ''
465 ConfigDict['subreg'] = []
466 IsUpdSect = True
467 else:
468 if IsDefSect or IsPcdSect or IsUpdSect or IsVpdSect:
469 if re.match("^!else($|\s+#.+)", DscLine):
470 if IfStack:
471 IfStack[-1] = not IfStack[-1]
472 else:
473 print("ERROR: No paired '!if' found for '!else' for line '%s'" % DscLine)
474 raise SystemExit
475 elif re.match("^!endif($|\s+#.+)", DscLine):
476 if IfStack:
477 IfStack.pop()
478 Level = ElifStack.pop()
479 if Level > 0:
480 del IfStack[-Level:]
481 else:
482 print("ERROR: No paired '!if' found for '!endif' for line '%s'" % DscLine)
483 raise SystemExit
484 else:
485 Result = False
486 Match = re.match("!(ifdef|ifndef)\s+(.+)", DscLine)
487 if Match:
488 Result = self.EvaulateIfdef (Match.group(2))
489 if Match.group(1) == 'ifndef':
490 Result = not Result
491 IfStack.append(Result)
492 ElifStack.append(0)
493 else:
494 Match = re.match("!(if|elseif)\s+(.+)", DscLine.split("#")[0])
495 if Match:
496 Result = self.EvaluateExpress(Match.group(2))
497 if Match.group(1) == "if":
498 ElifStack.append(0)
499 IfStack.append(Result)
500 else: #elseif
501 if IfStack:
502 IfStack[-1] = not IfStack[-1]
503 IfStack.append(Result)
504 ElifStack[-1] = ElifStack[-1] + 1
505 else:
506 print("ERROR: No paired '!if' found for '!elif' for line '%s'" % DscLine)
507 raise SystemExit
508 else:
509 if IfStack:
510 Handle = reduce(lambda x,y: x and y, IfStack)
511 else:
512 Handle = True
513 if Handle:
514 Match = re.match("!include\s+(.+)", DscLine)
515 if Match:
516 IncludeFilePath = Match.group(1)
517 IncludeFilePath = self.ExpandMacros(IncludeFilePath)
518 PackagesPath = os.getenv("PACKAGES_PATH")
519 if PackagesPath:
520 for PackagePath in PackagesPath.split(os.pathsep):
521 IncludeFilePathAbs = os.path.join(os.path.normpath(PackagePath), os.path.normpath(IncludeFilePath))
522 if os.path.exists(IncludeFilePathAbs):
523 IncludeDsc = open(IncludeFilePathAbs, "r")
524 break
525 else:
526 IncludeDsc = open(IncludeFilePath, "r")
527 if IncludeDsc == None:
528 print("ERROR: Cannot open file '%s'" % IncludeFilePath)
529 raise SystemExit
530 NewDscLines = IncludeDsc.readlines()
531 IncludeDsc.close()
532 DscLines = NewDscLines + DscLines
533 else:
534 if DscLine.startswith('!'):
535 print("ERROR: Unrecoginized directive for line '%s'" % DscLine)
536 raise SystemExit
537 if not Handle:
538 continue
539
540 if IsDefSect:
541 #DEFINE UPD_TOOL_GUID = 8C3D856A-9BE6-468E-850A-24F7A8D38E09
542 #DEFINE FSP_T_UPD_TOOL_GUID = 34686CA3-34F9-4901-B82A-BA630F0714C6
543 #DEFINE FSP_M_UPD_TOOL_GUID = 39A250DB-E465-4DD1-A2AC-E2BD3C0E2385
544 #DEFINE FSP_S_UPD_TOOL_GUID = CAE3605B-5B34-4C85-B3D7-27D54273C40F
545 Match = re.match("^\s*(?:DEFINE\s+)*(\w+)\s*=\s*([-.\w]+)", DscLine)
546 if Match:
547 self._MacroDict[Match.group(1)] = Match.group(2)
548 if self.Debug:
549 print "INFO : DEFINE %s = [ %s ]" % (Match.group(1), Match.group(2))
550 elif IsPcdSect:
551 #gSiPkgTokenSpaceGuid.PcdTxtEnable|FALSE
552 #gSiPkgTokenSpaceGuid.PcdOverclockEnable|TRUE
553 Match = re.match("^\s*([\w\.]+)\s*\|\s*(\w+)", DscLine)
554 if Match:
555 self._PcdsDict[Match.group(1)] = Match.group(2)
556 if self.Debug:
557 print "INFO : PCD %s = [ %s ]" % (Match.group(1), Match.group(2))
558 else:
559 Match = re.match("^\s*#\s+(!BSF|@Bsf|!HDR)\s+(.+)", DscLine)
560 if Match:
561 Remaining = Match.group(2)
562 if Match.group(1) == '!BSF' or Match.group(1) == '@Bsf':
563 Match = re.match("(?:^|.+\s+)PAGES:{(.+?)}", Remaining)
564 if Match:
565 # !BSF PAGES:{HSW:"Haswell System Agent", LPT:"Lynx Point PCH"}
566 PageList = Match.group(1).split(',')
567 for Page in PageList:
568 Page = Page.strip()
569 Match = re.match("(\w+):\"(.+)\"", Page)
570 self._CfgPageDict[Match.group(1)] = Match.group(2)
571
572 Match = re.match("(?:^|.+\s+)BLOCK:{NAME:\"(.+)\"\s*,\s*VER:\"(.+)\"\s*}", Remaining)
573 if Match:
574 self._CfgBlkDict['name'] = Match.group(1)
575 self._CfgBlkDict['ver'] = Match.group(2)
576
577 for Key in self._BsfKeyList:
578 Match = re.match("(?:^|.+\s+)%s:{(.+?)}" % Key, Remaining)
579 if Match:
580 if Key in ['NAME', 'HELP', 'OPTION'] and Match.group(1).startswith('+'):
581 ConfigDict[Key.lower()] += Match.group(1)[1:]
582 else:
583 ConfigDict[Key.lower()] = Match.group(1)
584 else:
585 for Key in self._HdrKeyList:
586 Match = re.match("(?:^|.+\s+)%s:{(.+?)}" % Key, Remaining)
587 if Match:
588 ConfigDict[Key.lower()] = Match.group(1)
589
590 Match = re.match("^\s*#\s+@Prompt\s+(.+)", DscLine)
591 if Match:
592 ConfigDict['name'] = Match.group(1)
593
594 Match = re.match("^\s*#\s*@ValidList\s*(.+)\s*\|\s*(.+)\s*\|\s*(.+)\s*", DscLine)
595 if Match:
596 if Match.group(2).strip() in self._BuidinOption:
597 ConfigDict['option'] = Match.group(2).strip()
598 else:
599 OptionValueList = Match.group(2).split(',')
600 OptionStringList = Match.group(3).split(',')
601 Index = 0
602 for Option in OptionValueList:
603 Option = Option.strip()
604 ConfigDict['option'] = ConfigDict['option'] + str(Option) + ':' + OptionStringList[Index].strip()
605 Index += 1
606 if Index in range(len(OptionValueList)):
607 ConfigDict['option'] += ', '
608 ConfigDict['type'] = "Combo"
609
610 Match = re.match("^\s*#\s*@ValidRange\s*(.+)\s*\|\s*(.+)\s*-\s*(.+)\s*", DscLine)
611 if Match:
612 if "0x" in Match.group(2) or "0x" in Match.group(3):
613 ConfigDict['type'] = "EditNum, HEX, (%s,%s)" % (Match.group(2), Match.group(3))
614 else:
615 ConfigDict['type'] = "EditNum, DEC, (%s,%s)" % (Match.group(2), Match.group(3))
616
617 Match = re.match("^\s*##\s+(.+)", DscLine)
618 if Match:
619 ConfigDict['help'] = Match.group(1)
620
621 # Check VPD/UPD
622 if IsUpdSect:
623 Match = re.match("^([_a-zA-Z0-9]+).([_a-zA-Z0-9]+)\s*\|\s*(0x[0-9A-F]+)\s*\|\s*(\d+|0x[0-9a-fA-F]+)\s*\|\s*(.+)",DscLine)
624 else:
625 Match = re.match("^([_a-zA-Z0-9]+).([_a-zA-Z0-9]+)\s*\|\s*(0x[0-9A-F]+)(?:\s*\|\s*(.+))?", DscLine)
626 if Match:
627 ConfigDict['space'] = Match.group(1)
628 ConfigDict['cname'] = Match.group(2)
629 ConfigDict['offset'] = int (Match.group(3), 16)
630 if ConfigDict['order'] == -1:
631 ConfigDict['order'] = ConfigDict['offset'] << 8
632 else:
633 (Major, Minor) = ConfigDict['order'].split('.')
634 ConfigDict['order'] = (int (Major, 16) << 8 ) + int (Minor, 16)
635 if IsUpdSect:
636 Value = Match.group(5).strip()
637 if Match.group(4).startswith("0x"):
638 Length = int (Match.group(4), 16)
639 else :
640 Length = int (Match.group(4))
641 else:
642 Value = Match.group(4)
643 if Value is None:
644 Value = ''
645 Value = Value.strip()
646 if '|' in Value:
647 Match = re.match("^.+\s*\|\s*(.+)", Value)
648 if Match:
649 Value = Match.group(1)
650 Length = -1
651
652 ConfigDict['length'] = Length
653 Match = re.match("\$\((\w+)\)", Value)
654 if Match:
655 if Match.group(1) in self._MacroDict:
656 Value = self._MacroDict[Match.group(1)]
657
658 ConfigDict['value'] = Value
659 if (len(Value) > 0) and (Value[0] == '{'):
660 Value = self.FormatListValue(ConfigDict)
661
662 if ConfigDict['name'] == '':
663 # Clear BSF specific items
664 ConfigDict['bsfname'] = ''
665 ConfigDict['help'] = ''
666 ConfigDict['type'] = ''
667 ConfigDict['option'] = ''
668
669 self._CfgItemList.append(ConfigDict.copy())
670 ConfigDict['name'] = ''
671 ConfigDict['find'] = ''
672 ConfigDict['struct'] = ''
673 ConfigDict['embed'] = ''
674 ConfigDict['comment'] = ''
675 ConfigDict['order'] = -1
676 ConfigDict['subreg'] = []
677 ConfigDict['option'] = ''
678 else:
679 # It could be a virtual item as below
680 # !BSF FIELD:{SerialDebugPortAddress0:1}
681 # or
682 # @Bsf FIELD:{SerialDebugPortAddress0:1b}
683 Match = re.match("^\s*#\s+(!BSF|@Bsf)\s+FIELD:{(.+):(\d+)([Bb])?}", DscLine)
684 if Match:
685 SubCfgDict = ConfigDict.copy()
686 if (Match.group(4) == None) or (Match.group(4) == 'B'):
687 UnitBitLen = 8
688 elif Match.group(4) == 'b':
689 UnitBitLen = 1
690 else:
691 print("ERROR: Invalide BSF FIELD length for line '%s'" % DscLine)
692 raise SystemExit
693 SubCfgDict['cname'] = Match.group(2)
694 SubCfgDict['bitlength'] = int (Match.group(3)) * UnitBitLen
695 if SubCfgDict['bitlength'] > 0:
696 LastItem = self._CfgItemList[-1]
697 if len(LastItem['subreg']) == 0:
698 SubOffset = 0
699 else:
700 SubOffset = LastItem['subreg'][-1]['bitoffset'] + LastItem['subreg'][-1]['bitlength']
701 SubCfgDict['bitoffset'] = SubOffset
702 LastItem['subreg'].append (SubCfgDict.copy())
703 ConfigDict['name'] = ''
704 return Error
705
706 def GetBsfBitFields (self, subitem, bytes):
707 start = subitem['bitoffset']
708 end = start + subitem['bitlength']
709 bitsvalue = ''.join('{0:08b}'.format(i) for i in bytes[::-1])
710 bitsvalue = bitsvalue[::-1]
711 bitslen = len(bitsvalue)
712 if start > bitslen or end > bitslen:
713 print "Invalid bits offset [%d,%d] for %s" % (start, end, subitem['name'])
714 raise SystemExit
715 return hex(int(bitsvalue[start:end][::-1], 2))
716
717 def UpdateSubRegionDefaultValue (self):
718 Error = 0
719 for Item in self._CfgItemList:
720 if len(Item['subreg']) == 0:
721 continue
722 bytearray = []
723 if Item['value'][0] == '{':
724 binlist = Item['value'][1:-1].split(',')
725 for each in binlist:
726 each = each.strip()
727 if each.startswith('0x'):
728 value = int(each, 16)
729 else:
730 value = int(each)
731 bytearray.append(value)
732 else:
733 if Item['value'].startswith('0x'):
734 value = int(Item['value'], 16)
735 else:
736 value = int(Item['value'])
737 idx = 0
738 while idx < Item['length']:
739 bytearray.append(value & 0xFF)
740 value = value >> 8
741 idx = idx + 1
742 for SubItem in Item['subreg']:
743 valuestr = self.GetBsfBitFields(SubItem, bytearray)
744 SubItem['value'] = valuestr
745 return Error
746
747 def CreateSplitUpdTxt (self, UpdTxtFile):
748 GuidList = ['FSP_T_UPD_TOOL_GUID','FSP_M_UPD_TOOL_GUID','FSP_S_UPD_TOOL_GUID']
749 SignatureList = ['0x545F', '0x4D5F','0x535F'] # _T, _M, and _S signature for FSPT, FSPM, FSPS
750 for Index in range(len(GuidList)):
751 UpdTxtFile = ''
752 FvDir = self._FvDir
753 if GuidList[Index] not in self._MacroDict:
754 self.Error = "%s definition is missing in DSC file" % (GuidList[Index])
755 return 1
756
757 if UpdTxtFile == '':
758 UpdTxtFile = os.path.join(FvDir, self._MacroDict[GuidList[Index]] + '.txt')
759
760 ReCreate = False
761 if not os.path.exists(UpdTxtFile):
762 ReCreate = True
763 else:
764 DscTime = os.path.getmtime(self._DscFile)
765 TxtTime = os.path.getmtime(UpdTxtFile)
766 if DscTime > TxtTime:
767 ReCreate = True
768
769 if not ReCreate:
770 # DSC has not been modified yet
771 # So don't have to re-generate other files
772 self.Error = 'No DSC file change, skip to create UPD TXT file'
773 return 256
774
775 TxtFd = open(UpdTxtFile, "w")
776 TxtFd.write("%s\n" % (__copyright_txt__ % date.today().year))
777
778 NextOffset = 0
779 SpaceIdx = 0
780 StartAddr = 0
781 EndAddr = 0
782 Default = 'DEFAULT|'
783 InRange = False
784 for Item in self._CfgItemList:
785 if Item['cname'] == 'Signature' and str(Item['value'])[0:6] == SignatureList[Index]:
786 StartAddr = Item['offset']
787 NextOffset = StartAddr
788 InRange = True
789 if Item['cname'] == 'UpdTerminator' and InRange == True:
790 EndAddr = Item['offset']
791 InRange = False
792 InRange = False
793 for Item in self._CfgItemList:
794 if Item['cname'] == 'Signature' and str(Item['value'])[0:6] == SignatureList[Index]:
795 InRange = True
796 if InRange != True:
797 continue
798 if Item['cname'] == 'UpdTerminator':
799 InRange = False
800 if Item['region'] != 'UPD':
801 continue
802 Offset = Item['offset']
803 if StartAddr > Offset or EndAddr < Offset:
804 continue
805 if NextOffset < Offset:
806 # insert one line
807 TxtFd.write("%s.UnusedUpdSpace%d|%s0x%04X|0x%04X|{0}\n" % (Item['space'], SpaceIdx, Default, NextOffset - StartAddr, Offset - NextOffset))
808 SpaceIdx = SpaceIdx + 1
809 NextOffset = Offset + Item['length']
810 TxtFd.write("%s.%s|%s0x%04X|%s|%s\n" % (Item['space'],Item['cname'],Default,Item['offset'] - StartAddr,Item['length'],Item['value']))
811 TxtFd.close()
812 return 0
813
814 def ProcessMultilines (self, String, MaxCharLength):
815 Multilines = ''
816 StringLength = len(String)
817 CurrentStringStart = 0
818 StringOffset = 0
819 BreakLineDict = []
820 if len(String) <= MaxCharLength:
821 while (StringOffset < StringLength):
822 if StringOffset >= 1:
823 if String[StringOffset - 1] == '\\' and String[StringOffset] == 'n':
824 BreakLineDict.append (StringOffset + 1)
825 StringOffset += 1
826 if BreakLineDict != []:
827 for Each in BreakLineDict:
828 Multilines += " %s\n" % String[CurrentStringStart:Each].lstrip()
829 CurrentStringStart = Each
830 if StringLength - CurrentStringStart > 0:
831 Multilines += " %s\n" % String[CurrentStringStart:].lstrip()
832 else:
833 Multilines = " %s\n" % String
834 else:
835 NewLineStart = 0
836 NewLineCount = 0
837 FoundSpaceChar = False
838 while (StringOffset < StringLength):
839 if StringOffset >= 1:
840 if NewLineCount >= MaxCharLength - 1:
841 if String[StringOffset] == ' ' and StringLength - StringOffset > 10:
842 BreakLineDict.append (NewLineStart + NewLineCount)
843 NewLineStart = NewLineStart + NewLineCount
844 NewLineCount = 0
845 FoundSpaceChar = True
846 elif StringOffset == StringLength - 1 and FoundSpaceChar == False:
847 BreakLineDict.append (0)
848 if String[StringOffset - 1] == '\\' and String[StringOffset] == 'n':
849 BreakLineDict.append (StringOffset + 1)
850 NewLineStart = StringOffset + 1
851 NewLineCount = 0
852 StringOffset += 1
853 NewLineCount += 1
854 if BreakLineDict != []:
855 BreakLineDict.sort ()
856 for Each in BreakLineDict:
857 if Each > 0:
858 Multilines += " %s\n" % String[CurrentStringStart:Each].lstrip()
859 CurrentStringStart = Each
860 if StringLength - CurrentStringStart > 0:
861 Multilines += " %s\n" % String[CurrentStringStart:].lstrip()
862 return Multilines
863
864 def CreateField (self, Item, Name, Length, Offset, Struct, BsfName, Help, Option):
865 PosName = 28
866 PosComment = 30
867 NameLine=''
868 HelpLine=''
869 OptionLine=''
870
871 IsArray = False
872 if Length in [1,2,4,8]:
873 Type = "UINT%d" % (Length * 8)
874 if Name.startswith("UnusedUpdSpace") and Length != 1:
875 IsArray = True
876 Type = "UINT8"
877 else:
878 IsArray = True
879 Type = "UINT8"
880
881 if Item and Item['value'].startswith('{'):
882 Type = "UINT8"
883 IsArray = True
884
885 if Struct != '':
886 Type = Struct
887 if Struct in ['UINT8','UINT16','UINT32','UINT64']:
888 IsArray = True
889 Unit = int(Type[4:]) / 8
890 Length = Length / Unit
891 else:
892 IsArray = False
893
894 if IsArray:
895 Name = Name + '[%d]' % Length
896
897 if len(Type) < PosName:
898 Space1 = PosName - len(Type)
899 else:
900 Space1 = 1
901
902 if BsfName != '':
903 NameLine=" - %s\n" % BsfName
904 else:
905 NameLine="\n"
906
907 if Help != '':
908 HelpLine = self.ProcessMultilines (Help, 80)
909
910 if Option != '':
911 OptionLine = self.ProcessMultilines (Option, 80)
912
913 if Offset is None:
914 OffsetStr = '????'
915 else:
916 OffsetStr = '0x%04X' % Offset
917
918 return "\n/** Offset %s%s%s%s**/\n %s%s%s;\n" % (OffsetStr, NameLine, HelpLine, OptionLine, Type, ' ' * Space1, Name,)
919
920 def PostProcessBody (self, TextBody):
921 NewTextBody = []
922 OldTextBody = []
923 IncludeLine = False
924 StructName = ''
925 VariableName = ''
926 IsUpdHdrDefined = False
927 IsUpdHeader = False
928 for Line in TextBody:
929 SplitToLines = Line.splitlines()
930 MatchComment = re.match("^/\*\sCOMMENT:(\w+):([\w|\W|\s]+)\s\*/\s([\s\S]*)", SplitToLines[0])
931 if MatchComment:
932 if MatchComment.group(1) == 'FSP_UPD_HEADER':
933 IsUpdHeader = True
934 else:
935 IsUpdHeader = False
936 if IsUpdHdrDefined != True or IsUpdHeader != True:
937 CommentLine = " " + MatchComment.group(2) + "\n"
938 NewTextBody.append("/**" + CommentLine + "**/\n")
939 Line = Line[(len(SplitToLines[0]) + 1):]
940
941 Match = re.match("^/\*\sEMBED_STRUCT:(\w+):(\w+):(START|END)\s\*/\s([\s\S]*)", Line)
942 if Match:
943 Line = Match.group(4)
944 if Match.group(1) == 'FSP_UPD_HEADER':
945 IsUpdHeader = True
946 else:
947 IsUpdHeader = False
948
949 if Match and Match.group(3) == 'START':
950 if IsUpdHdrDefined != True or IsUpdHeader != True:
951 NewTextBody.append ('typedef struct {\n')
952 StructName = Match.group(1)
953 VariableName = Match.group(2)
954 MatchOffset = re.search('/\*\*\sOffset\s0x([a-fA-F0-9]+)', Line)
955 if MatchOffset:
956 Offset = int(MatchOffset.group(1), 16)
957 else:
958 Offset = None
959 Line
960 IncludeLine = True
961 OldTextBody.append (self.CreateField (None, VariableName, 0, Offset, StructName, '', '', ''))
962 if IncludeLine:
963 if IsUpdHdrDefined != True or IsUpdHeader != True:
964 NewTextBody.append (Line)
965 else:
966 OldTextBody.append (Line)
967
968 if Match and Match.group(3) == 'END':
969 if (StructName != Match.group(1)) or (VariableName != Match.group(2)):
970 print "Unmatched struct name '%s' and '%s' !" % (StructName, Match.group(1))
971 else:
972 if IsUpdHdrDefined != True or IsUpdHeader != True:
973 NewTextBody.append ('} %s;\n\n' % StructName)
974 IsUpdHdrDefined = True
975 IncludeLine = False
976 NewTextBody.extend(OldTextBody)
977 return NewTextBody
978
979 def CreateHeaderFile (self, InputHeaderFile):
980 FvDir = self._FvDir
981
982 HeaderFileName = 'FspUpd.h'
983 HeaderFile = os.path.join(FvDir, HeaderFileName)
984
985 # Check if header needs to be recreated
986 ReCreate = False
987
988 TxtBody = []
989 for Item in self._CfgItemList:
990 if str(Item['cname']) == 'Signature' and Item['length'] == 8:
991 Value = int(Item['value'], 16)
992 Chars = []
993 while Value != 0x0:
994 Chars.append(chr(Value & 0xFF))
995 Value = Value >> 8
996 SignatureStr = ''.join(Chars)
997 # Signature will be _T / _M / _S for FSPT / FSPM / FSPS accordingly
998 if '_T' in SignatureStr[6:6+2]:
999 TxtBody.append("#define FSPT_UPD_SIGNATURE %s /* '%s' */\n\n" % (Item['value'], SignatureStr))
1000 elif '_M' in SignatureStr[6:6+2]:
1001 TxtBody.append("#define FSPM_UPD_SIGNATURE %s /* '%s' */\n\n" % (Item['value'], SignatureStr))
1002 elif '_S' in SignatureStr[6:6+2]:
1003 TxtBody.append("#define FSPS_UPD_SIGNATURE %s /* '%s' */\n\n" % (Item['value'], SignatureStr))
1004 TxtBody.append("\n")
1005
1006 for Region in ['UPD']:
1007 UpdOffsetTable = []
1008 UpdSignature = ['0x545F', '0x4D5F', '0x535F'] #['_T', '_M', '_S'] signature for FSPT, FSPM, FSPS
1009 UpdStructure = ['FSPT_UPD', 'FSPM_UPD', 'FSPS_UPD']
1010 for Item in self._CfgItemList:
1011 if Item["cname"] == 'Signature' and Item["value"][0:6] in UpdSignature:
1012 UpdOffsetTable.append (Item["offset"])
1013
1014 for UpdIdx in range(len(UpdOffsetTable)):
1015 CommentLine = ""
1016 for Item in self._CfgItemList:
1017 if Item["comment"] != '' and Item["offset"] >= UpdOffsetTable[UpdIdx]:
1018 MatchComment = re.match("^(U|V)PD_DATA_REGION:([\w|\W|\s]+)", Item["comment"])
1019 if MatchComment and MatchComment.group(1) == Region[0]:
1020 CommentLine = " " + MatchComment.group(2) + "\n"
1021 TxtBody.append("/**" + CommentLine + "**/\n")
1022 elif Item["offset"] >= UpdOffsetTable[UpdIdx] and Item["comment"] == '':
1023 Match = re.match("^FSP([\w|\W|\s])_UPD", UpdStructure[UpdIdx])
1024 if Match:
1025 TxtBody.append("/** Fsp " + Match.group(1) + " UPD Configuration\n**/\n")
1026 TxtBody.append("typedef struct {\n")
1027 NextOffset = 0
1028 SpaceIdx = 0
1029 Offset = 0
1030
1031 LastVisible = True
1032 ResvOffset = 0
1033 ResvIdx = 0
1034 LineBuffer = []
1035 InRange = False
1036 for Item in self._CfgItemList:
1037 if Item['cname'] == 'Signature' and str(Item['value'])[0:6] == UpdSignature[UpdIdx] or Region[0] == 'V':
1038 InRange = True
1039 if InRange != True:
1040 continue
1041 if Item['cname'] == 'UpdTerminator':
1042 InRange = False
1043
1044 if Item['region'] != Region:
1045 continue
1046
1047 if Item["offset"] < UpdOffsetTable[UpdIdx]:
1048 continue
1049
1050 NextVisible = LastVisible
1051
1052 if LastVisible and (Item['header'] == 'OFF'):
1053 NextVisible = False
1054 ResvOffset = Item['offset']
1055 elif (not LastVisible) and Item['header'] == 'ON':
1056 NextVisible = True
1057 Name = "Reserved" + Region[0] + "pdSpace%d" % ResvIdx
1058 ResvIdx = ResvIdx + 1
1059 TxtBody.append(self.CreateField (Item, Name, Item["offset"] - ResvOffset, ResvOffset, '', '', '', ''))
1060
1061 if Offset < Item["offset"]:
1062 if LastVisible:
1063 Name = "Unused" + Region[0] + "pdSpace%d" % SpaceIdx
1064 LineBuffer.append(self.CreateField (Item, Name, Item["offset"] - Offset, Offset, '', '', '', ''))
1065 SpaceIdx = SpaceIdx + 1
1066 Offset = Item["offset"]
1067
1068 LastVisible = NextVisible
1069
1070 Offset = Offset + Item["length"]
1071 if LastVisible:
1072 for Each in LineBuffer:
1073 TxtBody.append (Each)
1074 LineBuffer = []
1075 Comment = Item["comment"]
1076 Embed = Item["embed"].upper()
1077 if Embed.endswith(':START') or Embed.endswith(':END'):
1078 if not Comment == '' and Embed.endswith(':START'):
1079 Marker = '/* COMMENT:%s */ \n' % Item["comment"]
1080 Marker = Marker + '/* EMBED_STRUCT:%s */ ' % Item["embed"]
1081 else:
1082 Marker = '/* EMBED_STRUCT:%s */ ' % Item["embed"]
1083 else:
1084 if Embed == '':
1085 Marker = ''
1086 else:
1087 self.Error = "Invalid embedded structure format '%s'!\n" % Item["embed"]
1088 return 4
1089 Line = Marker + self.CreateField (Item, Item["cname"], Item["length"], Item["offset"], Item['struct'], Item['name'], Item['help'], Item['option'])
1090 TxtBody.append(Line)
1091 if Item['cname'] == 'UpdTerminator':
1092 break
1093 TxtBody.append("} " + UpdStructure[UpdIdx] + ";\n\n")
1094
1095 # Handle the embedded data structure
1096 TxtBody = self.PostProcessBody (TxtBody)
1097
1098 HeaderTFileName = 'FsptUpd.h'
1099 HeaderMFileName = 'FspmUpd.h'
1100 HeaderSFileName = 'FspsUpd.h'
1101
1102 UpdRegionCheck = ['FSPT', 'FSPM', 'FSPS'] # FSPX_UPD_REGION
1103 UpdConfigCheck = ['FSP_T', 'FSP_M', 'FSP_S'] # FSP_X_CONFIG, FSP_X_TEST_CONFIG, FSP_X_RESTRICTED_CONFIG
1104 UpdSignatureCheck = ['FSPT_UPD_SIGNATURE', 'FSPM_UPD_SIGNATURE', 'FSPS_UPD_SIGNATURE']
1105 ExcludedSpecificUpd = 'FSPM_ARCH_UPD'
1106
1107 if InputHeaderFile != '':
1108 if not os.path.exists(InputHeaderFile):
1109 self.Error = "Input header file '%s' does not exist" % InputHeaderFile
1110 return 6
1111
1112 InFd = open(InputHeaderFile, "r")
1113 IncLines = InFd.readlines()
1114 InFd.close()
1115
1116 for item in range(len(UpdRegionCheck)):
1117 if UpdRegionCheck[item] == 'FSPT':
1118 HeaderFd = open(os.path.join(FvDir, HeaderTFileName), "w")
1119 FileBase = os.path.basename(os.path.join(FvDir, HeaderTFileName))
1120 elif UpdRegionCheck[item] == 'FSPM':
1121 HeaderFd = open(os.path.join(FvDir, HeaderMFileName), "w")
1122 FileBase = os.path.basename(os.path.join(FvDir, HeaderMFileName))
1123 elif UpdRegionCheck[item] == 'FSPS':
1124 HeaderFd = open(os.path.join(FvDir, HeaderSFileName), "w")
1125 FileBase = os.path.basename(os.path.join(FvDir, HeaderSFileName))
1126 FileName = FileBase.replace(".", "_").upper()
1127 HeaderFd.write("%s\n" % (__copyright_h__ % date.today().year))
1128 HeaderFd.write("#ifndef __%s__\n" % FileName)
1129 HeaderFd.write("#define __%s__\n\n" % FileName)
1130 HeaderFd.write("#include <%s>\n\n" % HeaderFileName)
1131 HeaderFd.write("#pragma pack(1)\n\n")
1132
1133 Export = False
1134 for Line in IncLines:
1135 Match = re.search ("!EXPORT\s+([A-Z]+)\s+EXTERNAL_BOOTLOADER_STRUCT_(BEGIN|END)\s+", Line)
1136 if Match:
1137 if Match.group(2) == "BEGIN" and Match.group(1) == UpdRegionCheck[item]:
1138 Export = True
1139 continue
1140 else:
1141 Export = False
1142 continue
1143 if Export:
1144 HeaderFd.write(Line)
1145 HeaderFd.write("\n")
1146
1147 Index = 0
1148 StartIndex = 0
1149 EndIndex = 0
1150 StructStart = []
1151 StructStartWithComment = []
1152 StructEnd = []
1153 for Line in TxtBody:
1154 Index += 1
1155 Match = re.match("(typedef struct {)", Line)
1156 if Match:
1157 StartIndex = Index - 1
1158 Match = re.match("}\s([_A-Z0-9]+);", Line)
1159 if Match and (UpdRegionCheck[item] in Match.group(1) or UpdConfigCheck[item] in Match.group(1)) and (ExcludedSpecificUpd not in Match.group(1)):
1160 EndIndex = Index
1161 StructStart.append(StartIndex)
1162 StructEnd.append(EndIndex)
1163 Index = 0
1164 for Line in TxtBody:
1165 Index += 1
1166 for Item in range(len(StructStart)):
1167 if Index == StructStart[Item]:
1168 Match = re.match("^(/\*\*\s*)", Line)
1169 if Match:
1170 StructStartWithComment.append(StructStart[Item])
1171 else:
1172 StructStartWithComment.append(StructStart[Item] + 1)
1173 Index = 0
1174 for Line in TxtBody:
1175 Index += 1
1176 for Item in range(len(StructStart)):
1177 if Index >= StructStartWithComment[Item] and Index <= StructEnd[Item]:
1178 HeaderFd.write (Line)
1179 HeaderFd.write("#pragma pack()\n\n")
1180 HeaderFd.write("#endif\n")
1181 HeaderFd.close()
1182
1183 HeaderFd = open(HeaderFile, "w")
1184 FileBase = os.path.basename(HeaderFile)
1185 FileName = FileBase.replace(".", "_").upper()
1186 HeaderFd.write("%s\n" % (__copyright_h__ % date.today().year))
1187 HeaderFd.write("#ifndef __%s__\n" % FileName)
1188 HeaderFd.write("#define __%s__\n\n" % FileName)
1189 HeaderFd.write("#include <FspEas.h>\n\n")
1190 HeaderFd.write("#pragma pack(1)\n\n")
1191
1192 for item in range(len(UpdRegionCheck)):
1193 Index = 0
1194 StartIndex = 0
1195 EndIndex = 0
1196 StructStart = []
1197 StructStartWithComment = []
1198 StructEnd = []
1199 for Line in TxtBody:
1200 Index += 1
1201 Match = re.match("(typedef struct {)", Line)
1202 if Match:
1203 StartIndex = Index - 1
1204 Match = re.match("#define\s([_A-Z0-9]+)\s*", Line)
1205 if Match and (UpdSignatureCheck[item] in Match.group(1) or UpdSignatureCheck[item] in Match.group(1)):
1206 StructStart.append(Index - 1)
1207 StructEnd.append(Index)
1208 Index = 0
1209 for Line in TxtBody:
1210 Index += 1
1211 for Item in range(len(StructStart)):
1212 if Index == StructStart[Item]:
1213 Match = re.match("^(/\*\*\s*)", Line)
1214 if Match:
1215 StructStartWithComment.append(StructStart[Item])
1216 else:
1217 StructStartWithComment.append(StructStart[Item] + 1)
1218 Index = 0
1219 for Line in TxtBody:
1220 Index += 1
1221 for Item in range(len(StructStart)):
1222 if Index >= StructStartWithComment[Item] and Index <= StructEnd[Item]:
1223 HeaderFd.write (Line)
1224 HeaderFd.write("#pragma pack()\n\n")
1225 HeaderFd.write("#endif\n")
1226 HeaderFd.close()
1227
1228 return 0
1229
1230 def WriteBsfStruct (self, BsfFd, Item):
1231 LogExpr = CLogicalExpression()
1232 if Item['type'] == "None":
1233 Space = "gPlatformFspPkgTokenSpaceGuid"
1234 else:
1235 Space = Item['space']
1236 Line = " $%s_%s" % (Space, Item['cname'])
1237 Match = re.match("\s*\{([x0-9a-fA-F,\s]+)\}\s*", Item['value'])
1238 if Match:
1239 DefaultValue = Match.group(1).strip()
1240 else:
1241 DefaultValue = Item['value'].strip()
1242 if 'bitlength' in Item:
1243 BsfFd.write(" %s%s%4d bits $_DEFAULT_ = %s\n" % (Line, ' ' * (64 - len(Line)), Item['bitlength'], DefaultValue))
1244 else:
1245 BsfFd.write(" %s%s%4d bytes $_DEFAULT_ = %s\n" % (Line, ' ' * (64 - len(Line)), Item['length'], DefaultValue))
1246 TmpList = []
1247 if Item['type'] == "Combo":
1248 if not Item['option'] in self._BuidinOption:
1249 OptList = Item['option'].split(',')
1250 for Option in OptList:
1251 Option = Option.strip()
1252 (OpVal, OpStr) = Option.split(':')
1253 test = LogExpr.getNumber (OpVal)
1254 if test is None:
1255 raise Exception("Selection Index '%s' is not a number" % OpVal)
1256 TmpList.append((OpVal, OpStr))
1257 return TmpList
1258
1259 def WriteBsfOption (self, BsfFd, Item):
1260 PcdName = Item['space'] + '_' + Item['cname']
1261 WriteHelp = 0
1262 if Item['type'] == "Combo":
1263 if Item['option'] in self._BuidinOption:
1264 Options = self._BuidinOption[Item['option']]
1265 else:
1266 Options = PcdName
1267 BsfFd.write(' %s $%s, "%s", &%s,\n' % (Item['type'], PcdName, Item['name'], Options))
1268 WriteHelp = 1
1269 elif Item['type'].startswith("EditNum"):
1270 Match = re.match("EditNum\s*,\s*(HEX|DEC)\s*,\s*\((\d+|0x[0-9A-Fa-f]+)\s*,\s*(\d+|0x[0-9A-Fa-f]+)\)", Item['type'])
1271 if Match:
1272 BsfFd.write(' EditNum $%s, "%s", %s,\n' % (PcdName, Item['name'], Match.group(1)))
1273 WriteHelp = 2
1274 elif Item['type'].startswith("EditText"):
1275 BsfFd.write(' %s $%s, "%s",\n' % (Item['type'], PcdName, Item['name']))
1276 WriteHelp = 1
1277 elif Item['type'] == "Table":
1278 Columns = Item['option'].split(',')
1279 if len(Columns) != 0:
1280 BsfFd.write(' %s $%s "%s",' % (Item['type'], PcdName, Item['name']))
1281 for Col in Columns:
1282 Fmt = Col.split(':')
1283 if len(Fmt) != 3:
1284 raise Exception("Column format '%s' is invalid !" % Fmt)
1285 try:
1286 Dtype = int(Fmt[1].strip())
1287 except:
1288 raise Exception("Column size '%s' is invalid !" % Fmt[1])
1289 BsfFd.write('\n Column "%s", %d bytes, %s' % (Fmt[0].strip(), Dtype, Fmt[2].strip()))
1290 BsfFd.write(',\n')
1291 WriteHelp = 1
1292
1293 if WriteHelp > 0:
1294 HelpLines = Item['help'].split('\\n\\r')
1295 FirstLine = True
1296 for HelpLine in HelpLines:
1297 if FirstLine:
1298 FirstLine = False
1299 BsfFd.write(' Help "%s"\n' % (HelpLine))
1300 else:
1301 BsfFd.write(' "%s"\n' % (HelpLine))
1302 if WriteHelp == 2:
1303 BsfFd.write(' "Valid range: %s ~ %s"\n' % (Match.group(2), Match.group(3)))
1304
1305 def GenerateBsfFile (self, BsfFile):
1306
1307 if BsfFile == '':
1308 self.Error = "BSF output file '%s' is invalid" % BsfFile
1309 return 1
1310
1311 Error = 0
1312 OptionDict = {}
1313 BsfFd = open(BsfFile, "w")
1314 BsfFd.write("%s\n" % (__copyright_bsf__ % date.today().year))
1315 BsfFd.write("%s\n" % self._GlobalDataDef)
1316 BsfFd.write("StructDef\n")
1317 NextOffset = -1
1318 for Item in self._CfgItemList:
1319 if Item['find'] != '':
1320 BsfFd.write('\n Find "%s"\n' % Item['find'])
1321 NextOffset = Item['offset'] + Item['length']
1322 if Item['name'] != '':
1323 if NextOffset != Item['offset']:
1324 BsfFd.write(" Skip %d bytes\n" % (Item['offset'] - NextOffset))
1325 if len(Item['subreg']) > 0:
1326 NextOffset = Item['offset']
1327 BitsOffset = NextOffset * 8
1328 for SubItem in Item['subreg']:
1329 BitsOffset += SubItem['bitlength']
1330 if SubItem['name'] == '':
1331 if 'bitlength' in SubItem:
1332 BsfFd.write(" Skip %d bits\n" % (SubItem['bitlength']))
1333 else:
1334 BsfFd.write(" Skip %d bytes\n" % (SubItem['length']))
1335 else:
1336 Options = self.WriteBsfStruct(BsfFd, SubItem)
1337 if len(Options) > 0:
1338 OptionDict[SubItem['space']+'_'+SubItem['cname']] = Options
1339
1340 NextBitsOffset = (Item['offset'] + Item['length']) * 8
1341 if NextBitsOffset > BitsOffset:
1342 BitsGap = NextBitsOffset - BitsOffset
1343 BitsRemain = BitsGap % 8
1344 if BitsRemain:
1345 BsfFd.write(" Skip %d bits\n" % BitsRemain)
1346 BitsGap -= BitsRemain
1347 BytesRemain = BitsGap / 8
1348 if BytesRemain:
1349 BsfFd.write(" Skip %d bytes\n" % BytesRemain)
1350 NextOffset = Item['offset'] + Item['length']
1351 else:
1352 NextOffset = Item['offset'] + Item['length']
1353 Options = self.WriteBsfStruct(BsfFd, Item)
1354 if len(Options) > 0:
1355 OptionDict[Item['space']+'_'+Item['cname']] = Options
1356 BsfFd.write("\nEndStruct\n\n")
1357
1358 BsfFd.write("%s" % self._BuidinOptionTxt)
1359
1360 for Each in OptionDict:
1361 BsfFd.write("List &%s\n" % Each)
1362 for Item in OptionDict[Each]:
1363 BsfFd.write(' Selection %s , "%s"\n' % (Item[0], Item[1]))
1364 BsfFd.write("EndList\n\n")
1365
1366 BsfFd.write("BeginInfoBlock\n")
1367 BsfFd.write(' PPVer "%s"\n' % (self._CfgBlkDict['ver']))
1368 BsfFd.write(' Description "%s"\n' % (self._CfgBlkDict['name']))
1369 BsfFd.write("EndInfoBlock\n\n")
1370
1371 for Each in self._CfgPageDict:
1372 BsfFd.write('Page "%s"\n' % self._CfgPageDict[Each])
1373 BsfItems = []
1374 for Item in self._CfgItemList:
1375 if Item['name'] != '':
1376 if Item['page'] != Each:
1377 continue
1378 if len(Item['subreg']) > 0:
1379 for SubItem in Item['subreg']:
1380 if SubItem['name'] != '':
1381 BsfItems.append(SubItem)
1382 else:
1383 BsfItems.append(Item)
1384
1385 BsfItems.sort(key=lambda x: x['order'])
1386
1387 for Item in BsfItems:
1388 self.WriteBsfOption (BsfFd, Item)
1389 BsfFd.write("EndPage\n\n")
1390
1391 BsfFd.close()
1392 return Error
1393
1394
1395 def Usage():
1396 print "GenCfgOpt Version 0.52"
1397 print "Usage:"
1398 print " GenCfgOpt UPDTXT PlatformDscFile BuildFvDir [-D Macros]"
1399 print " GenCfgOpt HEADER PlatformDscFile BuildFvDir InputHFile [-D Macros]"
1400 print " GenCfgOpt GENBSF PlatformDscFile BuildFvDir BsfOutFile [-D Macros]"
1401
1402 def Main():
1403 #
1404 # Parse the options and args
1405 #
1406 GenCfgOpt = CGenCfgOpt()
1407 argc = len(sys.argv)
1408 if argc < 4:
1409 Usage()
1410 return 1
1411 else:
1412 DscFile = sys.argv[2]
1413 if not os.path.exists(DscFile):
1414 print "ERROR: Cannot open DSC file '%s' !" % DscFile
1415 return 2
1416
1417 OutFile = ''
1418 if argc > 4:
1419 if sys.argv[4][0] == '-':
1420 Start = 4
1421 else:
1422 OutFile = sys.argv[4]
1423 Start = 5
1424 if argc > Start:
1425 if GenCfgOpt.ParseMacros(sys.argv[Start:]) != 0:
1426 print "ERROR: Macro parsing failed !"
1427 return 3
1428
1429 FvDir = sys.argv[3]
1430 if not os.path.exists(FvDir):
1431 os.makedirs(FvDir)
1432
1433 if GenCfgOpt.ParseDscFile(DscFile, FvDir) != 0:
1434 print "ERROR: %s !" % GenCfgOpt.Error
1435 return 5
1436
1437 if GenCfgOpt.UpdateSubRegionDefaultValue() != 0:
1438 print "ERROR: %s !" % GenCfgOpt.Error
1439 return 7
1440
1441 if sys.argv[1] == "UPDTXT":
1442 Ret = GenCfgOpt.CreateSplitUpdTxt(OutFile)
1443 if Ret != 0:
1444 # No change is detected
1445 if Ret == 256:
1446 print "INFO: %s !" % (GenCfgOpt.Error)
1447 else :
1448 print "ERROR: %s !" % (GenCfgOpt.Error)
1449 return Ret
1450 elif sys.argv[1] == "HEADER":
1451 if GenCfgOpt.CreateHeaderFile(OutFile) != 0:
1452 print "ERROR: %s !" % GenCfgOpt.Error
1453 return 8
1454 elif sys.argv[1] == "GENBSF":
1455 if GenCfgOpt.GenerateBsfFile(OutFile) != 0:
1456 print "ERROR: %s !" % GenCfgOpt.Error
1457 return 9
1458 else:
1459 if argc < 5:
1460 Usage()
1461 return 1
1462 print "ERROR: Unknown command '%s' !" % sys.argv[1]
1463 Usage()
1464 return 1
1465 return 0
1466 return 0
1467
1468
1469 if __name__ == '__main__':
1470 sys.exit(Main())