Add missing BPDG tool sources when sync to BaseTools r2042
[mirror_edk2.git] / BaseTools / Source / Python / BPDG / BPDG.py
1 ## @file
2 # Intel Binary Product Data Generation Tool (Intel BPDG).
3 # This tool provide a simple process for the creation of a binary file containing read-only
4 # configuration data for EDK II platforms that contain Dynamic and DynamicEx PCDs described
5 # in VPD sections. It also provide an option for specifying an alternate name for a mapping
6 # file of PCD layout for use during the build when the platform integrator selects to use
7 # automatic offset calculation.
8 #
9 # Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
10 #
11 # This program and the accompanying materials
12 # are licensed and made available under the terms and conditions of the BSD License
13 # which accompanies this distribution. The full text of the license may be found at
14 # http://opensource.org/licenses/bsd-license.php
15 #
16 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #
19
20 ##
21 # Import Modules
22 #
23 import os
24 import sys
25 import encodings.ascii
26
27 from optparse import OptionParser
28 from encodings import gbk
29 from Common import EdkLogger
30 from Common.BuildToolError import *
31
32 import StringTable as st
33 import GenVpd
34
35 PROJECT_NAME = st.LBL_BPDG_LONG_UNI
36 VERSION = st.LBL_BPDG_VERSION
37
38 ## Tool entrance method
39 #
40 # This method mainly dispatch specific methods per the command line options.
41 # If no error found, return zero value so the caller of this tool can know
42 # if it's executed successfully or not.
43 #
44 # @retval 0 Tool was successful
45 # @retval 1 Tool failed
46 #
47 def main():
48 global Options, Args
49
50 # Initialize log system
51 EdkLogger.Initialize()
52 Options, Args = myOptionParser()
53
54 ReturnCode = 0
55
56 if Options.opt_slient:
57 EdkLogger.SetLevel(EdkLogger.ERROR)
58 elif Options.opt_verbose:
59 EdkLogger.SetLevel(EdkLogger.VERBOSE)
60 elif Options.opt_quiet:
61 EdkLogger.SetLevel(EdkLogger.QUIET)
62 elif Options.debug_level != None:
63 EdkLogger.SetLevel(Options.debug_level + 1)
64 else:
65 EdkLogger.SetLevel(EdkLogger.INFO)
66
67 if Options.vpd_filename == None:
68 EdkLogger.error("BPDG", ATTRIBUTE_NOT_AVAILABLE, "Please use the -o option to specify the file name for the VPD binary file")
69 if Options.filename == None:
70 EdkLogger.error("BPDG", ATTRIBUTE_NOT_AVAILABLE, "Please use the -m option to specify the file name for the mapping file")
71
72 Force = False
73 if Options.opt_force != None:
74 Force = True
75
76 if (Args[0] != None) :
77 startBPDG(Args[0], Options.filename, Options.vpd_filename, Force)
78 else :
79 EdkLogger.error("BPDG", ATTRIBUTE_NOT_AVAILABLE, "Please specify the file which contain the VPD pcd info.",
80 None)
81
82 return ReturnCode
83
84 def myOptionParser():
85 #
86 # Process command line firstly.
87 #
88 parser = OptionParser(version="%s - Version %s\n" % (PROJECT_NAME, VERSION),
89 description='',
90 prog='BPDG',
91 usage=st.LBL_BPDG_USAGE
92 )
93 parser.add_option('-d', '--debug', action='store', type="int", dest='debug_level',
94 help=st.MSG_OPTION_DEBUG_LEVEL)
95 parser.add_option('-v', '--verbose', action='store_true', dest='opt_verbose',
96 help=st.MSG_OPTION_VERBOSE)
97 parser.add_option('-s', '--silent', action='store_true', dest='opt_slient', default=False,
98 help=st.MSG_OPTION_SILENT)
99 parser.add_option('-q', '--quiet', action='store_true', dest='opt_quiet', default=False,
100 help=st.MSG_OPTION_QUIET)
101 parser.add_option('-o', '--vpd-filename', action='store', dest='vpd_filename',
102 help=st.MSG_OPTION_VPD_FILENAME)
103 parser.add_option('-m', '--map-filename', action='store', dest='filename',
104 help=st.MSG_OPTION_MAP_FILENAME)
105 parser.add_option('-f', '--force', action='store_true', dest='opt_force',
106 help=st.MSG_OPTION_FORCE)
107
108 (options, args) = parser.parse_args()
109 if len(args) == 0:
110 EdkLogger.info("Please specify the filename.txt file which contain the VPD pcd info!")
111 EdkLogger.info(parser.usage)
112 sys.exit(1)
113 return options, args
114
115 def startBPDG(InputFileName, MapFileName, VpdFileName, Force):
116 if os.path.exists(VpdFileName) and not Force:
117 print "\nFile %s already exist, Overwrite(Yes/No)?[Y]: " % VpdFileName
118 choice = sys.stdin.readline()
119 if choice.strip().lower() not in ['y', 'yes', '']:
120 return
121
122 GenVPD = GenVpd.GenVPD (InputFileName, MapFileName, VpdFileName)
123
124 EdkLogger.info('%-24s = %s' % ("VPD input data file: ", InputFileName))
125 EdkLogger.info('%-24s = %s' % ("VPD output map file: ", MapFileName))
126 EdkLogger.info('%-24s = %s' % ("VPD output binary file: ", VpdFileName))
127
128 GenVPD.ParserInputFile()
129 GenVPD.FormatFileLine()
130 GenVPD.FixVpdOffset()
131 GenVPD.GenerateVpdFile(MapFileName, VpdFileName)
132
133 EdkLogger.info("- Vpd pcd fixed done! -")
134
135 if __name__ == '__main__':
136 r = main()
137 ## 0-127 is a safe return range, and 1 is a standard default error
138 if r < 0 or r > 127: r = 1
139 sys.exit(r)
140
141