]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2Pkg/Tools/GenCfgOpt.py
OvmfPkg/PlatformBootManagerLib: keep the logo after connecting devices
[mirror_edk2.git] / IntelFsp2Pkg / Tools / GenCfgOpt.py
1 ## @ GenCfgOpt.py
2 #
3 # Copyright (c) 2014 - 2016, 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 self.ReleaseMode = True
293
294 self._GlobalDataDef = """
295 GlobalDataDef
296 SKUID = 0, "DEFAULT"
297 EndGlobalData
298
299 """
300 self._BuidinOptionTxt = """
301 List &EN_DIS
302 Selection 0x1 , "Enabled"
303 Selection 0x0 , "Disabled"
304 EndList
305
306 """
307
308 self._BsfKeyList = ['FIND','NAME','HELP','TYPE','PAGE','OPTION','ORDER']
309 self._HdrKeyList = ['HEADER','STRUCT', 'EMBED', 'COMMENT']
310 self._BuidinOption = {'$EN_DIS' : 'EN_DIS'}
311
312 self._MacroDict = {}
313 self._CfgBlkDict = {}
314 self._CfgPageDict = {}
315 self._CfgItemList = []
316 self._DscFile = ''
317 self._FvDir = ''
318 self._MapVer = 0
319
320 def ParseBuildMode (self, OutputStr):
321 if "RELEASE_" in OutputStr:
322 self.ReleaseMode = True
323 if "DEBUG_" in OutputStr:
324 self.ReleaseMode = False
325 return
326
327 def ParseMacros (self, MacroDefStr):
328 # ['-DABC=1', '-D', 'CFG_DEBUG=1', '-D', 'CFG_OUTDIR=Build']
329 self._MacroDict = {}
330 IsExpression = False
331 for Macro in MacroDefStr:
332 if Macro.startswith('-D'):
333 IsExpression = True
334 if len(Macro) > 2:
335 Macro = Macro[2:]
336 else :
337 continue
338 if IsExpression:
339 IsExpression = False
340 Match = re.match("(\w+)=(.+)", Macro)
341 if Match:
342 self._MacroDict[Match.group(1)] = Match.group(2)
343 else:
344 Match = re.match("(\w+)", Macro)
345 if Match:
346 self._MacroDict[Match.group(1)] = ''
347 if len(self._MacroDict) == 0:
348 Error = 1
349 else:
350 Error = 0
351 if self.Debug:
352 print "INFO : Macro dictionary:"
353 for Each in self._MacroDict:
354 print " $(%s) = [ %s ]" % (Each , self._MacroDict[Each])
355 return Error
356
357 def EvaulateIfdef (self, Macro):
358 Result = Macro in self._MacroDict
359 if self.Debug:
360 print "INFO : Eval Ifdef [%s] : %s" % (Macro, Result)
361 return Result
362
363 def ExpandMacros (self, Input):
364 Line = Input
365 Match = re.findall("\$\(\w+\)", Input)
366 if Match:
367 for Each in Match:
368 Variable = Each[2:-1]
369 if Variable in self._MacroDict:
370 Line = Line.replace(Each, self._MacroDict[Variable])
371 else:
372 if self.Debug:
373 print "WARN : %s is not defined" % Each
374 Line = Line.replace(Each, Each[2:-1])
375 return Line
376
377 def EvaluateExpress (self, Expr):
378 ExpExpr = self.ExpandMacros(Expr)
379 LogExpr = CLogicalExpression()
380 Result = LogExpr.evaluateExpress (ExpExpr)
381 if self.Debug:
382 print "INFO : Eval Express [%s] : %s" % (Expr, Result)
383 return Result
384
385 def FormatListValue(self, ConfigDict):
386 Struct = ConfigDict['struct']
387 if Struct not in ['UINT8','UINT16','UINT32','UINT64']:
388 return
389
390 dataarray = []
391 binlist = ConfigDict['value'][1:-1].split(',')
392 for each in binlist:
393 each = each.strip()
394 if each.startswith('0x'):
395 value = int(each, 16)
396 else:
397 value = int(each)
398 dataarray.append(value)
399
400 unit = int(Struct[4:]) / 8
401 if int(ConfigDict['length']) != unit * len(dataarray):
402 raise Exception("Array size is not proper for '%s' !" % ConfigDict['cname'])
403
404 bytearray = []
405 for each in dataarray:
406 value = each
407 for loop in xrange(unit):
408 bytearray.append("0x%02X" % (value & 0xFF))
409 value = value >> 8
410 newvalue = '{' + ','.join(bytearray) + '}'
411 ConfigDict['value'] = newvalue
412 return ""
413
414 def ParseDscFile (self, DscFile, FvDir, ConfigDscFile, ExtConfigDscFile):
415 self._CfgItemList = []
416 self._CfgPageDict = {}
417 self._CfgBlkDict = {}
418 self._DscFile = DscFile
419 self._FvDir = FvDir
420
421 IsDefSect = False
422 IsUpdSect = False
423 IsVpdSect = False
424 Found = False
425
426 IfStack = []
427 ElifStack = []
428 Error = 0
429 ConfigDict = {}
430
431 DscFd = open(DscFile, "r")
432 DscLines = DscFd.readlines()
433 DscFd.close()
434
435 while len(DscLines):
436 DscLine = DscLines.pop(0).strip()
437 Handle = False
438 Match = re.match("^\[(.+)\]", DscLine)
439 if Match is not None:
440 if Match.group(1).lower() == "Defines".lower():
441 IsDefSect = True
442 IsVpdSect = False
443 IsUpdSect = False
444 elif Match.group(1).lower() == "PcdsDynamicVpd.Upd".lower():
445 ConfigDict = {}
446 ConfigDict['header'] = 'ON'
447 ConfigDict['region'] = 'UPD'
448 ConfigDict['order'] = -1
449 ConfigDict['page'] = ''
450 ConfigDict['name'] = ''
451 ConfigDict['find'] = ''
452 ConfigDict['struct'] = ''
453 ConfigDict['embed'] = ''
454 ConfigDict['comment'] = ''
455 ConfigDict['subreg'] = []
456 IsDefSect = False
457 IsUpdSect = True
458 IsVpdSect = False
459 Found = True
460 else:
461 IsDefSect = False
462 IsUpdSect = False
463 IsVpdSect = False
464 else:
465 if IsDefSect or IsUpdSect or IsVpdSect:
466 if re.match("^!else($|\s+#.+)", DscLine):
467 if IfStack:
468 IfStack[-1] = not IfStack[-1]
469 else:
470 print("ERROR: No paired '!if' found for '!else' for line '%s'" % DscLine)
471 raise SystemExit
472 elif re.match("^!endif($|\s+#.+)", DscLine):
473 if IfStack:
474 IfStack.pop()
475 Level = ElifStack.pop()
476 if Level > 0:
477 del IfStack[-Level:]
478 else:
479 print("ERROR: No paired '!if' found for '!endif' for line '%s'" % DscLine)
480 raise SystemExit
481 else:
482 Result = False
483 Match = re.match("!(ifdef|ifndef)\s+(.+)", DscLine)
484 if Match:
485 Result = self.EvaulateIfdef (Match.group(2))
486 if Match.group(1) == 'ifndef':
487 Result = not Result
488 IfStack.append(Result)
489 ElifStack.append(0)
490 else:
491 Match = re.match("!(if|elseif)\s+(.+)", DscLine)
492 if Match:
493 IsFoundInFile = False
494 MatchPcdFormat = re.match("^\s*(.+)\.(.+)\s*==\s*(.+)", Match.group(2))
495 if MatchPcdFormat:
496 ExtConfigDsc = open(ExtConfigDscFile, "r")
497 ExtConfigDscLines = ExtConfigDsc.readlines()
498 ExtConfigDsc.close()
499
500 while len(ExtConfigDscLines):
501 ExtConfigDscLine = ExtConfigDscLines.pop(0).strip()
502 MatchExtConfigPcd = re.match("^\s*(.+)\s*\|\s*(.+)", ExtConfigDscLine)
503 if MatchExtConfigPcd and IsFoundInFile == False:
504 PcdFormatStr = str(str(MatchPcdFormat.group(1)) + "." + str(MatchPcdFormat.group(2)))
505 ExtConfigPcd = str(MatchExtConfigPcd.group(1))
506 Result = False
507 if PcdFormatStr.strip() == ExtConfigPcd.strip():
508 Result = self.EvaluateExpress(str(str(MatchExtConfigPcd.group(2)) + " == " + str(MatchPcdFormat.group(3))))
509 IsFoundInFile = True
510 break
511 if IsFoundInFile == False:
512 ConfigDsc = open(ConfigDscFile, "r")
513 ConfigDscLines = ConfigDsc.readlines()
514 ConfigDsc.close()
515 while len(ConfigDscLines):
516 ConfigDscLine = ConfigDscLines.pop(0).strip()
517 MatchConfigPcd = re.match("^\s*(.+)\s*\|\s*(.+)", ConfigDscLine)
518 if MatchConfigPcd:
519 PcdFormatStr = str(str(MatchPcdFormat.group(1)) + "." + str(MatchPcdFormat.group(2)))
520 ConfigPcd = str(MatchConfigPcd.group(1))
521 Result = False
522 if PcdFormatStr.strip() == ConfigPcd.strip():
523 Result = self.EvaluateExpress(str(str(MatchConfigPcd.group(2)) + " == " + str(MatchPcdFormat.group(3))))
524 IsFoundInFile = True
525 break
526 else:
527 Result = self.EvaluateExpress(Match.group(2))
528 if Match.group(1) == "if":
529 ElifStack.append(0)
530 IfStack.append(Result)
531 else: #elseif
532 if IfStack:
533 IfStack[-1] = not IfStack[-1]
534 IfStack.append(Result)
535 ElifStack[-1] = ElifStack[-1] + 1
536 else:
537 print("ERROR: No paired '!if' found for '!elif' for line '%s'" % DscLine)
538 raise SystemExit
539 else:
540 if IfStack:
541 Handle = reduce(lambda x,y: x and y, IfStack)
542 else:
543 Handle = True
544 if Handle:
545 Match = re.match("!include\s+(.+)", DscLine)
546 if Match:
547 IncludeFilePath = Match.group(1)
548 IncludeFilePath = self.ExpandMacros(IncludeFilePath)
549 try:
550 IncludeDsc = open(IncludeFilePath, "r")
551 except:
552 print("ERROR: Cannot open file '%s'" % IncludeFilePath)
553 raise SystemExit
554 NewDscLines = IncludeDsc.readlines()
555 IncludeDsc.close()
556 DscLines = NewDscLines + DscLines
557 else:
558 if DscLine.startswith('!'):
559 print("ERROR: Unrecoginized directive for line '%s'" % DscLine)
560 raise SystemExit
561 if not Handle:
562 continue
563
564 if IsDefSect:
565 #DEFINE UPD_TOOL_GUID = 8C3D856A-9BE6-468E-850A-24F7A8D38E09
566 #DEFINE FSP_T_UPD_TOOL_GUID = 34686CA3-34F9-4901-B82A-BA630F0714C6
567 #DEFINE FSP_M_UPD_TOOL_GUID = 39A250DB-E465-4DD1-A2AC-E2BD3C0E2385
568 #DEFINE FSP_S_UPD_TOOL_GUID = CAE3605B-5B34-4C85-B3D7-27D54273C40F
569 Match = re.match("^\s*(?:DEFINE\s+)*(\w+)\s*=\s*([-.\w]+)", DscLine)
570 if Match:
571 self._MacroDict[Match.group(1)] = Match.group(2)
572 if self.Debug:
573 print "INFO : DEFINE %s = [ %s ]" % (Match.group(1), Match.group(2))
574 else:
575 Match = re.match("^\s*#\s+(!BSF|@Bsf|!HDR)\s+(.+)", DscLine)
576 if Match:
577 Remaining = Match.group(2)
578 if Match.group(1) == '!BSF' or Match.group(1) == '@Bsf':
579 Match = re.match("(?:^|.+\s+)PAGES:{(.+?)}", Remaining)
580 if Match:
581 # !BSF PAGES:{HSW:"Haswell System Agent", LPT:"Lynx Point PCH"}
582 PageList = Match.group(1).split(',')
583 for Page in PageList:
584 Page = Page.strip()
585 Match = re.match("(\w+):\"(.+)\"", Page)
586 self._CfgPageDict[Match.group(1)] = Match.group(2)
587
588 Match = re.match("(?:^|.+\s+)BLOCK:{NAME:\"(.+)\"\s*,\s*VER:\"(.+)\"\s*}", Remaining)
589 if Match:
590 self._CfgBlkDict['name'] = Match.group(1)
591 self._CfgBlkDict['ver'] = Match.group(2)
592
593 for Key in self._BsfKeyList:
594 Match = re.match("(?:^|.+\s+)%s:{(.+?)}" % Key, Remaining)
595 if Match:
596 if Key in ['NAME', 'HELP', 'OPTION'] and Match.group(1).startswith('+'):
597 ConfigDict[Key.lower()] += Match.group(1)[1:]
598 else:
599 ConfigDict[Key.lower()] = Match.group(1)
600 else:
601 for Key in self._HdrKeyList:
602 Match = re.match("(?:^|.+\s+)%s:{(.+?)}" % Key, Remaining)
603 if Match:
604 ConfigDict[Key.lower()] = Match.group(1)
605
606 Match = re.match("^\s*#\s+@Prompt\s+(.+)", DscLine)
607 if Match:
608 ConfigDict['name'] = Match.group(1)
609
610 Match = re.match("^\s*#\s*@ValidList\s*(.+)\s*\|\s*(.+)\s*\|\s*(.+)\s*", DscLine)
611 if Match:
612 if Match.group(2).strip() in self._BuidinOption:
613 ConfigDict['option'] = Match.group(2).strip()
614 else:
615 OptionValueList = Match.group(2).split(',')
616 OptionStringList = Match.group(3).split(',')
617 Index = 0
618 for Option in OptionValueList:
619 Option = Option.strip()
620 ConfigDict['option'] = ConfigDict['option'] + str(Option) + ':' + OptionStringList[Index].strip()
621 Index += 1
622 if Index in range(len(OptionValueList)):
623 ConfigDict['option'] += ', '
624 ConfigDict['type'] = "Combo"
625
626 Match = re.match("^\s*#\s*@ValidRange\s*(.+)\s*\|\s*(.+)\s*-\s*(.+)\s*", DscLine)
627 if Match:
628 if "0x" in Match.group(2) or "0x" in Match.group(3):
629 ConfigDict['type'] = "EditNum, HEX, (%s,%s)" % (Match.group(2), Match.group(3))
630 else:
631 ConfigDict['type'] = "EditNum, DEC, (%s,%s)" % (Match.group(2), Match.group(3))
632
633 Match = re.match("^\s*##\s+(.+)", DscLine)
634 if Match:
635 ConfigDict['help'] = Match.group(1)
636
637 # Check VPD/UPD
638 if IsUpdSect:
639 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)
640 else:
641 Match = re.match("^([_a-zA-Z0-9]+).([_a-zA-Z0-9]+)\s*\|\s*(0x[0-9A-F]+)(?:\s*\|\s*(.+))?", DscLine)
642 if Match:
643 ConfigDict['space'] = Match.group(1)
644 ConfigDict['cname'] = Match.group(2)
645 ConfigDict['offset'] = int (Match.group(3), 16)
646 if ConfigDict['order'] == -1:
647 ConfigDict['order'] = ConfigDict['offset'] << 8
648 else:
649 (Major, Minor) = ConfigDict['order'].split('.')
650 ConfigDict['order'] = (int (Major, 16) << 8 ) + int (Minor, 16)
651 if IsUpdSect:
652 Value = Match.group(5).strip()
653 if Match.group(4).startswith("0x"):
654 Length = int (Match.group(4), 16)
655 else :
656 Length = int (Match.group(4))
657 else:
658 Value = Match.group(4)
659 if Value is None:
660 Value = ''
661 Value = Value.strip()
662 if '|' in Value:
663 Match = re.match("^.+\s*\|\s*(.+)", Value)
664 if Match:
665 Value = Match.group(1)
666 Length = -1
667
668 ConfigDict['length'] = Length
669 Match = re.match("\$\((\w+)\)", Value)
670 if Match:
671 if Match.group(1) in self._MacroDict:
672 Value = self._MacroDict[Match.group(1)]
673
674 ConfigDict['value'] = Value
675 if (len(Value) > 0) and (Value[0] == '{'):
676 Value = self.FormatListValue(ConfigDict)
677
678 if ConfigDict['name'] == '':
679 # Clear BSF specific items
680 ConfigDict['bsfname'] = ''
681 ConfigDict['help'] = ''
682 ConfigDict['type'] = ''
683 ConfigDict['option'] = ''
684
685 self._CfgItemList.append(ConfigDict.copy())
686 ConfigDict['name'] = ''
687 ConfigDict['find'] = ''
688 ConfigDict['struct'] = ''
689 ConfigDict['embed'] = ''
690 ConfigDict['comment'] = ''
691 ConfigDict['order'] = -1
692 ConfigDict['subreg'] = []
693 ConfigDict['option'] = ''
694 else:
695 # It could be a virtual item as below
696 # !BSF FIELD:{1:SerialDebugPortAddress0}
697 # or
698 # @Bsf FIELD:{1:SerialDebugPortAddress0}
699 Match = re.match("^\s*#\s+(!BSF|@Bsf)\s+FIELD:{(.+):(\d+)}", DscLine)
700 if Match:
701 SubCfgDict = ConfigDict
702 SubCfgDict['cname'] = Match.group(2)
703 SubCfgDict['length'] = int (Match.group(3))
704 if SubCfgDict['length'] > 0:
705 LastItem = self._CfgItemList[-1]
706 if len(LastItem['subreg']) == 0:
707 SubOffset = 0
708 else:
709 SubOffset += LastItem['subreg'][-1]['length']
710 SubCfgDict['offset'] = SubOffset
711 LastItem['subreg'].append (SubCfgDict.copy())
712 ConfigDict['name'] = ''
713 return Error
714
715 def UpdateSubRegionDefaultValue (self):
716 Error = 0
717 for Item in self._CfgItemList:
718 if len(Item['subreg']) == 0:
719 continue
720 bytearray = []
721 if Item['value'][0] == '{':
722 binlist = Item['value'][1:-1].split(',')
723 for each in binlist:
724 each = each.strip()
725 if each.startswith('0x'):
726 value = int(each, 16)
727 else:
728 value = int(each)
729 bytearray.append(value)
730 else:
731 if Item['value'].startswith('0x'):
732 value = int(Item['value'], 16)
733 else:
734 value = int(Item['value'])
735 idx = 0;
736 while idx < Item['length']:
737 bytearray.append(value & 0xFF)
738 value = value >> 8
739 idx = idx + 1
740 for SubItem in Item['subreg']:
741 if SubItem['length'] in (1,2,4,8):
742 valuelist = [b for b in bytearray[SubItem['offset']:SubItem['offset']+SubItem['length']]]
743 valuelist.reverse()
744 valuestr = "".join('%02X' % b for b in valuelist)
745 SubItem['value'] = '0x%s' % valuestr
746 else:
747 valuestr = ",".join('0x%02X' % b for b in bytearray[SubItem['offset']:SubItem['offset']+SubItem['length']])
748 SubItem['value'] = '{%s}' % valuestr
749 return Error
750
751 def CreateSplitUpdTxt (self, UpdTxtFile):
752 GuidList = ['FSP_T_UPD_TOOL_GUID','FSP_M_UPD_TOOL_GUID','FSP_S_UPD_TOOL_GUID']
753 SignatureList = ['0x545F', '0x4D5F','0x535F'] # _T, _M, and _S signature for FSPT, FSPM, FSPS
754 for Index in range(len(GuidList)):
755 UpdTxtFile = ''
756 FvDir = self._FvDir
757 if GuidList[Index] not in self._MacroDict:
758 self.Error = "%s definition is missing in DSC file" % (GuidList[Index])
759 return 1
760
761 if UpdTxtFile == '':
762 UpdTxtFile = os.path.join(FvDir, self._MacroDict[GuidList[Index]] + '.txt')
763
764 ReCreate = False
765 if not os.path.exists(UpdTxtFile):
766 ReCreate = True
767 else:
768 DscTime = os.path.getmtime(self._DscFile)
769 TxtTime = os.path.getmtime(UpdTxtFile)
770 if DscTime > TxtTime:
771 ReCreate = True
772
773 if not ReCreate:
774 # DSC has not been modified yet
775 # So don't have to re-generate other files
776 self.Error = 'No DSC file change, skip to create UPD TXT file'
777 return 256
778
779 TxtFd = open(UpdTxtFile, "w")
780 TxtFd.write("%s\n" % (__copyright_txt__ % date.today().year))
781
782 NextOffset = 0
783 SpaceIdx = 0
784 StartAddr = 0
785 EndAddr = 0
786 Default = 'DEFAULT|'
787 InRange = False
788 for Item in self._CfgItemList:
789 if Item['cname'] == 'Signature' and str(Item['value'])[0:6] == SignatureList[Index]:
790 StartAddr = Item['offset']
791 NextOffset = StartAddr
792 InRange = True
793 if Item['cname'] == 'UpdTerminator' and InRange == True:
794 EndAddr = Item['offset']
795 InRange = False
796 InRange = False
797 for Item in self._CfgItemList:
798 if Item['cname'] == 'Signature' and str(Item['value'])[0:6] == SignatureList[Index]:
799 InRange = True
800 if InRange != True:
801 continue
802 if Item['cname'] == 'UpdTerminator':
803 InRange = False
804 if Item['region'] != 'UPD':
805 continue
806 Offset = Item['offset']
807 if StartAddr > Offset or EndAddr < Offset:
808 continue
809 if NextOffset < Offset:
810 # insert one line
811 TxtFd.write("%s.UnusedUpdSpace%d|%s0x%04X|0x%04X|{0}\n" % (Item['space'], SpaceIdx, Default, NextOffset - StartAddr, Offset - NextOffset))
812 SpaceIdx = SpaceIdx + 1
813 NextOffset = Offset + Item['length']
814 if Item['cname'] == 'PcdSerialIoUartDebugEnable':
815 if self.ReleaseMode == False:
816 Item['value'] = 0x01
817 TxtFd.write("%s.%s|%s0x%04X|%s|%s\n" % (Item['space'],Item['cname'],Default,Item['offset'] - StartAddr,Item['length'],Item['value']))
818 TxtFd.close()
819 return 0
820
821 def ProcessMultilines (self, String, MaxCharLength):
822 Multilines = ''
823 StringLength = len(String)
824 CurrentStringStart = 0
825 StringOffset = 0
826 BreakLineDict = []
827 if len(String) <= MaxCharLength:
828 while (StringOffset < StringLength):
829 if StringOffset >= 1:
830 if String[StringOffset - 1] == '\\' and String[StringOffset] == 'n':
831 BreakLineDict.append (StringOffset + 1)
832 StringOffset += 1
833 if BreakLineDict != []:
834 for Each in BreakLineDict:
835 Multilines += " %s\n" % String[CurrentStringStart:Each].lstrip()
836 CurrentStringStart = Each
837 if StringLength - CurrentStringStart > 0:
838 Multilines += " %s\n" % String[CurrentStringStart:].lstrip()
839 else:
840 Multilines = " %s\n" % String
841 else:
842 NewLineStart = 0
843 NewLineCount = 0
844 FoundSpaceChar = False
845 while (StringOffset < StringLength):
846 if StringOffset >= 1:
847 if NewLineCount >= MaxCharLength - 1:
848 if String[StringOffset] == ' ' and StringLength - StringOffset > 10:
849 BreakLineDict.append (NewLineStart + NewLineCount)
850 NewLineStart = NewLineStart + NewLineCount
851 NewLineCount = 0
852 FoundSpaceChar = True
853 elif StringOffset == StringLength - 1 and FoundSpaceChar == False:
854 BreakLineDict.append (0)
855 if String[StringOffset - 1] == '\\' and String[StringOffset] == 'n':
856 BreakLineDict.append (StringOffset + 1)
857 NewLineStart = StringOffset + 1
858 NewLineCount = 0
859 StringOffset += 1
860 NewLineCount += 1
861 if BreakLineDict != []:
862 BreakLineDict.sort ()
863 for Each in BreakLineDict:
864 if Each > 0:
865 Multilines += " %s\n" % String[CurrentStringStart:Each].lstrip()
866 CurrentStringStart = Each
867 if StringLength - CurrentStringStart > 0:
868 Multilines += " %s\n" % String[CurrentStringStart:].lstrip()
869 return Multilines
870
871 def CreateField (self, Item, Name, Length, Offset, Struct, BsfName, Help, Option):
872 PosName = 28
873 PosComment = 30
874 NameLine=''
875 HelpLine=''
876 OptionLine=''
877
878 IsArray = False
879 if Length in [1,2,4,8]:
880 Type = "UINT%d" % (Length * 8)
881 else:
882 IsArray = True
883 Type = "UINT8"
884
885 if Item and Item['value'].startswith('{'):
886 Type = "UINT8"
887 IsArray = True
888
889 if Struct != '':
890 Type = Struct
891 if Struct in ['UINT8','UINT16','UINT32','UINT64']:
892 IsArray = True
893 Unit = int(Type[4:]) / 8
894 Length = Length / Unit
895 else:
896 IsArray = False
897
898 if IsArray:
899 Name = Name + '[%d]' % Length
900
901 if len(Type) < PosName:
902 Space1 = PosName - len(Type)
903 else:
904 Space1 = 1
905
906 if BsfName != '':
907 NameLine=" - %s\n" % BsfName
908 else:
909 NameLine="\n"
910
911 if Help != '':
912 HelpLine = self.ProcessMultilines (Help, 80)
913
914 if Option != '':
915 OptionLine = self.ProcessMultilines (Option, 80)
916
917 if Offset is None:
918 OffsetStr = '????'
919 else:
920 OffsetStr = '0x%04X' % Offset
921
922 return "\n/** Offset %s%s%s%s**/\n %s%s%s;\n" % (OffsetStr, NameLine, HelpLine, OptionLine, Type, ' ' * Space1, Name,)
923
924 def PostProcessBody (self, TextBody):
925 NewTextBody = []
926 OldTextBody = []
927 IncludeLine = False
928 StructName = ''
929 VariableName = ''
930 IsUpdHdrDefined = False
931 IsUpdHeader = False
932 for Line in TextBody:
933 SplitToLines = Line.splitlines()
934 MatchComment = re.match("^/\*\sCOMMENT:(\w+):([\w|\W|\s]+)\s\*/\s([\s\S]*)", SplitToLines[0])
935 if MatchComment:
936 if MatchComment.group(1) == 'FSP_UPD_HEADER':
937 IsUpdHeader = True
938 else:
939 IsUpdHeader = False
940 if IsUpdHdrDefined != True or IsUpdHeader != True:
941 CommentLine = " " + MatchComment.group(2) + "\n"
942 NewTextBody.append("/**" + CommentLine + "**/\n")
943 Line = Line[(len(SplitToLines[0]) + 1):]
944
945 Match = re.match("^/\*\sEMBED_STRUCT:(\w+):(\w+):(START|END)\s\*/\s([\s\S]*)", Line)
946 if Match:
947 Line = Match.group(4)
948 if Match.group(1) == 'FSP_UPD_HEADER':
949 IsUpdHeader = True
950 else:
951 IsUpdHeader = False
952
953 if Match and Match.group(3) == 'START':
954 if IsUpdHdrDefined != True or IsUpdHeader != True:
955 NewTextBody.append ('typedef struct {\n')
956 StructName = Match.group(1)
957 VariableName = Match.group(2)
958 MatchOffset = re.search('/\*\*\sOffset\s0x([a-fA-F0-9]+)', Line)
959 if MatchOffset:
960 Offset = int(MatchOffset.group(1), 16)
961 else:
962 Offset = None
963 Line
964 IncludeLine = True
965 OldTextBody.append (self.CreateField (None, VariableName, 0, Offset, StructName, '', '', ''))
966 if IncludeLine:
967 if IsUpdHdrDefined != True or IsUpdHeader != True:
968 NewTextBody.append (Line)
969 else:
970 OldTextBody.append (Line)
971
972 if Match and Match.group(3) == 'END':
973 if (StructName != Match.group(1)) or (VariableName != Match.group(2)):
974 print "Unmatched struct name '%s' and '%s' !" % (StructName, Match.group(1))
975 else:
976 if IsUpdHdrDefined != True or IsUpdHeader != True:
977 NewTextBody.append ('} %s;\n\n' % StructName)
978 IsUpdHdrDefined = True
979 IncludeLine = False
980 NewTextBody.extend(OldTextBody)
981 return NewTextBody
982
983 def CreateHeaderFile (self, InputHeaderFile):
984 FvDir = self._FvDir
985
986 HeaderFileName = 'FspUpd.h'
987 HeaderFile = os.path.join(FvDir, HeaderFileName)
988
989 # Check if header needs to be recreated
990 ReCreate = False
991
992 TxtBody = []
993 for Item in self._CfgItemList:
994 if str(Item['cname']) == 'Signature' and Item['length'] == 8:
995 Value = int(Item['value'], 16)
996 Chars = []
997 while Value != 0x0:
998 Chars.append(chr(Value & 0xFF))
999 Value = Value >> 8
1000 SignatureStr = ''.join(Chars)
1001 # Signature will be _T / _M / _S for FSPT / FSPM / FSPS accordingly
1002 if '_T' in SignatureStr[6:6+2]:
1003 TxtBody.append("#define FSPT_UPD_SIGNATURE %s /* '%s' */\n\n" % (Item['value'], SignatureStr))
1004 elif '_M' in SignatureStr[6:6+2]:
1005 TxtBody.append("#define FSPM_UPD_SIGNATURE %s /* '%s' */\n\n" % (Item['value'], SignatureStr))
1006 elif '_S' in SignatureStr[6:6+2]:
1007 TxtBody.append("#define FSPS_UPD_SIGNATURE %s /* '%s' */\n\n" % (Item['value'], SignatureStr))
1008 TxtBody.append("\n")
1009
1010 for Region in ['UPD']:
1011 UpdOffsetTable = []
1012 UpdSignature = ['0x545F', '0x4D5F', '0x535F'] #['_T', '_M', '_S'] signature for FSPT, FSPM, FSPS
1013 UpdStructure = ['FSPT_UPD', 'FSPM_UPD', 'FSPS_UPD']
1014 for Item in self._CfgItemList:
1015 if Item["cname"] == 'Signature' and Item["value"][0:6] in UpdSignature:
1016 UpdOffsetTable.append (Item["offset"])
1017
1018 for UpdIdx in range(len(UpdOffsetTable)):
1019 CommentLine = ""
1020 for Item in self._CfgItemList:
1021 if Item["comment"] != '' and Item["offset"] >= UpdOffsetTable[UpdIdx]:
1022 MatchComment = re.match("^(U|V)PD_DATA_REGION:([\w|\W|\s]+)", Item["comment"])
1023 if MatchComment and MatchComment.group(1) == Region[0]:
1024 CommentLine = " " + MatchComment.group(2) + "\n"
1025 TxtBody.append("/**" + CommentLine + "**/\n")
1026 elif Item["offset"] >= UpdOffsetTable[UpdIdx] and Item["comment"] == '':
1027 Match = re.match("^FSP([\w|\W|\s])_UPD", UpdStructure[UpdIdx])
1028 if Match:
1029 TxtBody.append("/** Fsp " + Match.group(1) + " UPD Configuration\n**/\n")
1030 TxtBody.append("typedef struct {\n")
1031 NextOffset = 0
1032 SpaceIdx = 0
1033 Offset = 0
1034
1035 LastVisible = True
1036 ResvOffset = 0
1037 ResvIdx = 0
1038 LineBuffer = []
1039 InRange = False
1040 for Item in self._CfgItemList:
1041 if Item['cname'] == 'Signature' and str(Item['value'])[0:6] == UpdSignature[UpdIdx] or Region[0] == 'V':
1042 InRange = True
1043 if InRange != True:
1044 continue
1045 if Item['cname'] == 'UpdTerminator':
1046 InRange = False
1047
1048 if Item['region'] != Region:
1049 continue
1050
1051 if Item["offset"] < UpdOffsetTable[UpdIdx]:
1052 continue
1053
1054 NextVisible = LastVisible
1055
1056 if LastVisible and (Item['header'] == 'OFF'):
1057 NextVisible = False
1058 ResvOffset = Item['offset']
1059 elif (not LastVisible) and Item['header'] == 'ON':
1060 NextVisible = True
1061 Name = "Reserved" + Region[0] + "pdSpace%d" % ResvIdx
1062 ResvIdx = ResvIdx + 1
1063 TxtBody.append(self.CreateField (Item, Name, Item["offset"] - ResvOffset, ResvOffset, '', '', '', ''))
1064
1065 if Offset < Item["offset"]:
1066 if LastVisible:
1067 Name = "Unused" + Region[0] + "pdSpace%d" % SpaceIdx
1068 LineBuffer.append(self.CreateField (Item, Name, Item["offset"] - Offset, Offset, '', '', '', ''))
1069 SpaceIdx = SpaceIdx + 1
1070 Offset = Item["offset"]
1071
1072 LastVisible = NextVisible
1073
1074 Offset = Offset + Item["length"]
1075 if LastVisible:
1076 for Each in LineBuffer:
1077 TxtBody.append (Each)
1078 LineBuffer = []
1079 Comment = Item["comment"]
1080 Embed = Item["embed"].upper()
1081 if Embed.endswith(':START') or Embed.endswith(':END'):
1082 if not Comment == '' and Embed.endswith(':START'):
1083 Marker = '/* COMMENT:%s */ \n' % Item["comment"]
1084 Marker = Marker + '/* EMBED_STRUCT:%s */ ' % Item["embed"]
1085 else:
1086 Marker = '/* EMBED_STRUCT:%s */ ' % Item["embed"]
1087 else:
1088 if Embed == '':
1089 Marker = '';
1090 else:
1091 self.Error = "Invalid embedded structure format '%s'!\n" % Item["embed"]
1092 return 4
1093 Line = Marker + self.CreateField (Item, Item["cname"], Item["length"], Item["offset"], Item['struct'], Item['name'], Item['help'], Item['option'])
1094 TxtBody.append(Line)
1095 if Item['cname'] == 'UpdTerminator':
1096 break
1097 TxtBody.append("} " + UpdStructure[UpdIdx] + ";\n\n")
1098
1099 # Handle the embedded data structure
1100 TxtBody = self.PostProcessBody (TxtBody)
1101
1102 HeaderTFileName = 'FsptUpd.h'
1103 HeaderMFileName = 'FspmUpd.h'
1104 HeaderSFileName = 'FspsUpd.h'
1105
1106 UpdRegionCheck = ['FSPT', 'FSPM', 'FSPS'] # FSPX_UPD_REGION
1107 UpdConfigCheck = ['FSP_T', 'FSP_M', 'FSP_S'] # FSP_X_CONFIG, FSP_X_TEST_CONFIG, FSP_X_RESTRICTED_CONFIG
1108 UpdSignatureCheck = ['FSPT_UPD_SIGNATURE', 'FSPM_UPD_SIGNATURE', 'FSPS_UPD_SIGNATURE']
1109 ExcludedSpecificUpd = 'FSPM_ARCH_UPD'
1110
1111 if InputHeaderFile != '':
1112 if not os.path.exists(InputHeaderFile):
1113 self.Error = "Input header file '%s' does not exist" % InputHeaderFile
1114 return 6
1115
1116 InFd = open(InputHeaderFile, "r")
1117 IncLines = InFd.readlines()
1118 InFd.close()
1119
1120 for item in range(len(UpdRegionCheck)):
1121 if UpdRegionCheck[item] == 'FSPT':
1122 HeaderFd = open(os.path.join(FvDir, HeaderTFileName), "w")
1123 FileBase = os.path.basename(os.path.join(FvDir, HeaderTFileName))
1124 elif UpdRegionCheck[item] == 'FSPM':
1125 HeaderFd = open(os.path.join(FvDir, HeaderMFileName), "w")
1126 FileBase = os.path.basename(os.path.join(FvDir, HeaderMFileName))
1127 elif UpdRegionCheck[item] == 'FSPS':
1128 HeaderFd = open(os.path.join(FvDir, HeaderSFileName), "w")
1129 FileBase = os.path.basename(os.path.join(FvDir, HeaderSFileName))
1130 FileName = FileBase.replace(".", "_").upper()
1131 HeaderFd.write("%s\n" % (__copyright_h__ % date.today().year))
1132 HeaderFd.write("#ifndef __%s__\n" % FileName)
1133 HeaderFd.write("#define __%s__\n\n" % FileName)
1134 HeaderFd.write("#include <%s>\n\n" % HeaderFileName)
1135 HeaderFd.write("#pragma pack(push, 1)\n\n")
1136
1137 Export = False
1138 for Line in IncLines:
1139 Match = re.search ("!EXPORT\s+([A-Z]+)\s+EXTERNAL_BOOTLOADER_STRUCT_(BEGIN|END)\s+", Line)
1140 if Match:
1141 if Match.group(2) == "BEGIN" and Match.group(1) == UpdRegionCheck[item]:
1142 Export = True
1143 continue
1144 else:
1145 Export = False
1146 continue
1147 if Export:
1148 HeaderFd.write(Line)
1149 HeaderFd.write("\n")
1150
1151 Index = 0
1152 StartIndex = 0
1153 EndIndex = 0
1154 StructStart = []
1155 StructStartWithComment = []
1156 StructEnd = []
1157 for Line in TxtBody:
1158 Index += 1
1159 Match = re.match("(typedef struct {)", Line)
1160 if Match:
1161 StartIndex = Index - 1
1162 Match = re.match("}\s([_A-Z0-9]+);", Line)
1163 if Match and (UpdRegionCheck[item] in Match.group(1) or UpdConfigCheck[item] in Match.group(1)) and (ExcludedSpecificUpd not in Match.group(1)):
1164 EndIndex = Index
1165 StructStart.append(StartIndex)
1166 StructEnd.append(EndIndex)
1167 Index = 0
1168 for Line in TxtBody:
1169 Index += 1
1170 for Item in range(len(StructStart)):
1171 if Index == StructStart[Item]:
1172 Match = re.match("^(/\*\*\s*)", Line)
1173 if Match:
1174 StructStartWithComment.append(StructStart[Item])
1175 else:
1176 StructStartWithComment.append(StructStart[Item] + 1)
1177 Index = 0
1178 for Line in TxtBody:
1179 Index += 1
1180 for Item in range(len(StructStart)):
1181 if Index >= StructStartWithComment[Item] and Index <= StructEnd[Item]:
1182 HeaderFd.write (Line)
1183 HeaderFd.write("#pragma pack(pop)\n\n")
1184 HeaderFd.write("#endif\n")
1185 HeaderFd.close()
1186
1187 HeaderFd = open(HeaderFile, "w")
1188 FileBase = os.path.basename(HeaderFile)
1189 FileName = FileBase.replace(".", "_").upper()
1190 HeaderFd.write("%s\n" % (__copyright_h__ % date.today().year))
1191 HeaderFd.write("#ifndef __%s__\n" % FileName)
1192 HeaderFd.write("#define __%s__\n\n" % FileName)
1193 HeaderFd.write("#include <FspEas.h>\n\n")
1194 HeaderFd.write("#pragma pack(push, 1)\n\n")
1195
1196 for item in range(len(UpdRegionCheck)):
1197 Index = 0
1198 StartIndex = 0
1199 EndIndex = 0
1200 StructStart = []
1201 StructStartWithComment = []
1202 StructEnd = []
1203 for Line in TxtBody:
1204 Index += 1
1205 Match = re.match("(typedef struct {)", Line)
1206 if Match:
1207 StartIndex = Index - 1
1208 Match = re.match("#define\s([_A-Z0-9]+)\s*", Line)
1209 if Match and (UpdSignatureCheck[item] in Match.group(1) or UpdSignatureCheck[item] in Match.group(1)):
1210 StructStart.append(Index - 1)
1211 StructEnd.append(Index)
1212 Index = 0
1213 for Line in TxtBody:
1214 Index += 1
1215 for Item in range(len(StructStart)):
1216 if Index == StructStart[Item]:
1217 Match = re.match("^(/\*\*\s*)", Line)
1218 if Match:
1219 StructStartWithComment.append(StructStart[Item])
1220 else:
1221 StructStartWithComment.append(StructStart[Item] + 1)
1222 Index = 0
1223 for Line in TxtBody:
1224 Index += 1
1225 for Item in range(len(StructStart)):
1226 if Index >= StructStartWithComment[Item] and Index <= StructEnd[Item]:
1227 HeaderFd.write (Line)
1228 HeaderFd.write("#pragma pack(pop)\n\n")
1229 HeaderFd.write("#endif\n")
1230 HeaderFd.close()
1231
1232 return 0
1233
1234 def WriteBsfStruct (self, BsfFd, Item):
1235 if Item['type'] == "None":
1236 Space = "gPlatformFspPkgTokenSpaceGuid"
1237 else:
1238 Space = Item['space']
1239 Line = " $%s_%s" % (Space, Item['cname'])
1240 Match = re.match("\s*\{([x0-9a-fA-F,\s]+)\}\s*", Item['value'])
1241 if Match:
1242 DefaultValue = Match.group(1).strip()
1243 else:
1244 DefaultValue = Item['value'].strip()
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 TmpList.append((OpVal, OpStr))
1254 return TmpList
1255
1256 def WriteBsfOption (self, BsfFd, Item):
1257 PcdName = Item['space'] + '_' + Item['cname']
1258 WriteHelp = 0
1259 if Item['type'] == "Combo":
1260 if Item['option'] in self._BuidinOption:
1261 Options = self._BuidinOption[Item['option']]
1262 else:
1263 Options = PcdName
1264 BsfFd.write(' %s $%s, "%s", &%s,\n' % (Item['type'], PcdName, Item['name'], Options));
1265 WriteHelp = 1
1266 elif Item['type'].startswith("EditNum"):
1267 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'])
1268 if Match:
1269 BsfFd.write(' EditNum $%s, "%s", %s,\n' % (PcdName, Item['name'], Match.group(1)));
1270 WriteHelp = 2
1271 elif Item['type'].startswith("EditText"):
1272 BsfFd.write(' %s $%s, "%s",\n' % (Item['type'], PcdName, Item['name']));
1273 WriteHelp = 1
1274 elif Item['type'] == "Table":
1275 Columns = Item['option'].split(',')
1276 if len(Columns) != 0:
1277 BsfFd.write(' %s $%s "%s",' % (Item['type'], PcdName, Item['name']));
1278 for Col in Columns:
1279 Fmt = Col.split(':')
1280 if len(Fmt) != 3:
1281 raise Exception("Column format '%s' is invalid !" % Fmt)
1282 try:
1283 Dtype = int(Fmt[1].strip())
1284 except:
1285 raise Exception("Column size '%s' is invalid !" % Fmt[1])
1286 BsfFd.write('\n Column "%s", %d bytes, %s' % (Fmt[0].strip(), Dtype, Fmt[2].strip()))
1287 BsfFd.write(',\n')
1288 WriteHelp = 1
1289
1290 if WriteHelp > 0:
1291 HelpLines = Item['help'].split('\\n\\r')
1292 FirstLine = True
1293 for HelpLine in HelpLines:
1294 if FirstLine:
1295 FirstLine = False
1296 BsfFd.write(' Help "%s"\n' % (HelpLine));
1297 else:
1298 BsfFd.write(' "%s"\n' % (HelpLine));
1299 if WriteHelp == 2:
1300 BsfFd.write(' "Valid range: %s ~ %s"\n' % (Match.group(2), Match.group(3)));
1301
1302 def GenerateBsfFile (self, BsfFile):
1303
1304 if BsfFile == '':
1305 self.Error = "BSF output file '%s' is invalid" % BsfFile
1306 return 1
1307
1308 Error = 0
1309 OptionDict = {}
1310 BsfFd = open(BsfFile, "w")
1311 BsfFd.write("%s\n" % (__copyright_bsf__ % date.today().year))
1312 BsfFd.write("%s\n" % self._GlobalDataDef);
1313 BsfFd.write("StructDef\n")
1314 NextOffset = -1
1315 for Item in self._CfgItemList:
1316 if Item['find'] != '':
1317 BsfFd.write('\n Find "%s"\n' % Item['find'])
1318 NextOffset = Item['offset'] + Item['length']
1319 if Item['name'] != '':
1320 if NextOffset != Item['offset']:
1321 BsfFd.write(" Skip %d bytes\n" % (Item['offset'] - NextOffset))
1322 if len(Item['subreg']) > 0:
1323 NextOffset = Item['offset']
1324 for SubItem in Item['subreg']:
1325 NextOffset += SubItem['length']
1326 if SubItem['name'] == '':
1327 BsfFd.write(" Skip %d bytes\n" % (SubItem['length']))
1328 else:
1329 Options = self.WriteBsfStruct(BsfFd, SubItem)
1330 if len(Options) > 0:
1331 OptionDict[SubItem['space']+'_'+SubItem['cname']] = Options
1332 if (Item['offset'] + Item['length']) < NextOffset:
1333 self.Error = "BSF sub region '%s' length does not match" % (Item['space']+'.'+Item['cname'])
1334 return 2
1335 else:
1336 NextOffset = Item['offset'] + Item['length']
1337 Options = self.WriteBsfStruct(BsfFd, Item)
1338 if len(Options) > 0:
1339 OptionDict[Item['space']+'_'+Item['cname']] = Options
1340 BsfFd.write("\nEndStruct\n\n")
1341
1342 BsfFd.write("%s" % self._BuidinOptionTxt);
1343
1344 for Each in OptionDict:
1345 BsfFd.write("List &%s\n" % Each);
1346 for Item in OptionDict[Each]:
1347 BsfFd.write(' Selection %s , "%s"\n' % (Item[0], Item[1]));
1348 BsfFd.write("EndList\n\n");
1349
1350 BsfFd.write("BeginInfoBlock\n");
1351 BsfFd.write(' PPVer "%s"\n' % (self._CfgBlkDict['ver']));
1352 BsfFd.write(' Description "%s"\n' % (self._CfgBlkDict['name']));
1353 BsfFd.write("EndInfoBlock\n\n");
1354
1355 for Each in self._CfgPageDict:
1356 BsfFd.write('Page "%s"\n' % self._CfgPageDict[Each]);
1357 BsfItems = []
1358 for Item in self._CfgItemList:
1359 if Item['name'] != '':
1360 if Item['page'] != Each:
1361 continue
1362 if len(Item['subreg']) > 0:
1363 for SubItem in Item['subreg']:
1364 if SubItem['name'] != '':
1365 BsfItems.append(SubItem)
1366 else:
1367 BsfItems.append(Item)
1368
1369 BsfItems.sort(key=lambda x: x['order'])
1370
1371 for Item in BsfItems:
1372 self.WriteBsfOption (BsfFd, Item)
1373 BsfFd.write("EndPage\n\n");
1374
1375 BsfFd.close()
1376 return Error
1377
1378
1379 def Usage():
1380 print "GenCfgOpt Version 0.51"
1381 print "Usage:"
1382 print " GenCfgOpt UPDTXT PlatformDscFile BuildFvDir ConfigDscFile ExtConfigDscFile"
1383 print " [-D Macros]"
1384 print " GenCfgOpt HEADER PlatformDscFile BuildFvDir ConfigDscFile ExtConfigDscFile"
1385 print " InputHFile [-D Macros]"
1386 print " GenCfgOpt GENBSF PlatformDscFile BuildFvDir ConfigDscFile ExtConfigDscFile"
1387 print " BsfOutFile [-D Macros]"
1388
1389 def Main():
1390 #
1391 # Parse the options and args
1392 #
1393 GenCfgOpt = CGenCfgOpt()
1394 argc = len(sys.argv)
1395 if argc < 4:
1396 Usage()
1397 return 1
1398 else:
1399 DscFile = sys.argv[2]
1400 if not os.path.exists(DscFile):
1401 print "ERROR: Cannot open DSC file '%s' !" % DscFile
1402 return 2
1403 ConfigDscFile = sys.argv[4]
1404 if not os.path.exists(ConfigDscFile):
1405 print "ERROR: Cannot open Config DSC file '%s' !" % ConfigDscFile
1406 return 2
1407 ExtConfigDscFile = sys.argv[5]
1408 if not os.path.exists(ExtConfigDscFile):
1409 print "ERROR: Cannot open Ext Config DSC file '%s' !" % ExtConfigDscFile
1410 return 2
1411
1412 OutFile = ''
1413 if argc > 4:
1414 if sys.argv[6][0] == '-':
1415 Start = 4
1416 else:
1417 OutFile = sys.argv[6]
1418 Start = 5
1419 GenCfgOpt.ParseBuildMode(sys.argv[3])
1420 if GenCfgOpt.ParseMacros(sys.argv[Start:]) != 0:
1421 print "ERROR: Macro parsing failed !"
1422 return 3
1423
1424 FvDir = sys.argv[3]
1425 if not os.path.exists(FvDir):
1426 os.makedirs(FvDir)
1427
1428 if GenCfgOpt.ParseDscFile(DscFile, FvDir, ConfigDscFile, ExtConfigDscFile) != 0:
1429 print "ERROR: %s !" % GenCfgOpt.Error
1430 return 5
1431
1432 if GenCfgOpt.UpdateSubRegionDefaultValue() != 0:
1433 print "ERROR: %s !" % GenCfgOpt.Error
1434 return 7
1435
1436 if sys.argv[1] == "UPDTXT":
1437 Ret = GenCfgOpt.CreateSplitUpdTxt(OutFile)
1438 if Ret != 0:
1439 # No change is detected
1440 if Ret == 256:
1441 print "INFO: %s !" % (GenCfgOpt.Error)
1442 else :
1443 print "ERROR: %s !" % (GenCfgOpt.Error)
1444 return Ret
1445 elif sys.argv[1] == "HEADER":
1446 if GenCfgOpt.CreateHeaderFile(OutFile) != 0:
1447 print "ERROR: %s !" % GenCfgOpt.Error
1448 return 8
1449 elif sys.argv[1] == "GENBSF":
1450 if GenCfgOpt.GenerateBsfFile(OutFile) != 0:
1451 print "ERROR: %s !" % GenCfgOpt.Error
1452 return 9
1453 else:
1454 if argc < 5:
1455 Usage()
1456 return 1
1457 print "ERROR: Unknown command '%s' !" % sys.argv[1]
1458 Usage()
1459 return 1
1460 return 0
1461 return 0
1462
1463
1464 if __name__ == '__main__':
1465 sys.exit(Main())