Check In tool source code based on Build tool project revision r1655.
[efi/edk2/.git] / edk2 / BaseTools / Source / Python / GenFds / Fv.py
1 ## @file\r
2 # process FV generation\r
3 #\r
4 #  Copyright (c) 2007, Intel Corporation\r
5 #\r
6 #  All rights reserved. This program and the accompanying materials\r
7 #  are licensed and made available under the terms and conditions of the BSD License\r
8 #  which accompanies this distribution.  The full text of the license may be found at\r
9 #  http://opensource.org/licenses/bsd-license.php\r
10 #\r
11 #  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 #\r
14 \r
15 ##\r
16 # Import Modules\r
17 #\r
18 import os\r
19 import shutil\r
20 import subprocess\r
21 import StringIO\r
22 \r
23 import Ffs\r
24 import AprioriSection\r
25 from GenFdsGlobalVariable import GenFdsGlobalVariable\r
26 from GenFds import GenFds\r
27 from CommonDataClass.FdfClass import FvClassObject\r
28 from Common.Misc import SaveFileOnChange\r
29 \r
30 T_CHAR_LF = '\n'\r
31 \r
32 ## generate FV\r
33 #\r
34 #\r
35 class FV (FvClassObject):\r
36     ## The constructor\r
37     #\r
38     #   @param  self        The object pointer\r
39     #\r
40     def __init__(self):\r
41         FvClassObject.__init__(self)\r
42         self.FvInfFile = None\r
43         self.FvAddressFile = None\r
44         self.BaseAddress = None\r
45         self.InfFileName = None\r
46         self.FvAddressFileName = None\r
47 \r
48     ## AddToBuffer()\r
49     #\r
50     #   Generate Fv and add it to the Buffer\r
51     #\r
52     #   @param  self        The object pointer\r
53     #   @param  Buffer      The buffer generated FV data will be put\r
54     #   @param  BaseAddress base address of FV\r
55     #   @param  BlockSize   block size of FV\r
56     #   @param  BlockNum    How many blocks in FV\r
57     #   @param  ErasePolarity      Flash erase polarity\r
58     #   @param  VtfDict     VTF objects\r
59     #   @param  MacroDict   macro value pair\r
60     #   @retval string      Generated FV file path\r
61     #\r
62     def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None, MacroDict = {}) :\r
63 \r
64         if self.UiFvName.upper() in GenFds.FvBinDict.keys():\r
65             return GenFds.FvBinDict[self.UiFvName.upper()]\r
66 \r
67         GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV ..." %self.UiFvName)\r
68 \r
69         self.__InitializeInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)\r
70         #\r
71         # First Process the Apriori section\r
72         #\r
73         MacroDict.update(self.DefineVarDict)\r
74 \r
75         GenFdsGlobalVariable.VerboseLogger('First generate Apriori file !')\r
76         FfsFileList = []\r
77         for AprSection in self.AprioriSectionList:\r
78             FileName = AprSection.GenFfs (self.UiFvName, MacroDict)\r
79             FfsFileList.append(FileName)\r
80             # Add Apriori file name to Inf file\r
81             self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
82                                        FileName          + \\r
83                                            T_CHAR_LF)\r
84 \r
85         # Process Modules in FfsList\r
86         for FfsFile in self.FfsList :\r
87             FileName = FfsFile.GenFfs(MacroDict)\r
88             FfsFileList.append(FileName)\r
89             self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
90                                        FileName          + \\r
91                                        T_CHAR_LF)\r
92 \r
93         SaveFileOnChange(self.InfFileName, self.FvInfFile.getvalue(), False)\r
94         self.FvInfFile.close()\r
95         #\r
96         # Call GenFv tool\r
97         #\r
98         FvOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName)\r
99         FvOutputFile = FvOutputFile + '.Fv'\r
100         # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)\r
101         if self.CreateFileName != None:\r
102             FvOutputFile = self.CreateFileName\r
103 \r
104         FvInfoFileName = os.path.join(GenFdsGlobalVariable.FfsDir, self.UiFvName + '.inf')\r
105         shutil.copy(GenFdsGlobalVariable.FvAddressFileName, FvInfoFileName)\r
106         GenFdsGlobalVariable.GenerateFirmwareVolume(\r
107                                 FvOutputFile,\r
108                                 [self.InfFileName],\r
109                                 AddressFile=FvInfoFileName,\r
110                                 FfsList=FfsFileList\r
111                                 )\r
112 \r
113         #\r
114         # Write the Fv contents to Buffer\r
115         #\r
116         FvFileObj = open ( FvOutputFile,'r+b')\r
117 \r
118         GenFdsGlobalVariable.InfLogger( "\nGenerate %s FV Successfully" %self.UiFvName)\r
119         GenFdsGlobalVariable.SharpCounter = 0\r
120 \r
121         Buffer.write(FvFileObj.read())\r
122         FvFileObj.close()\r
123         GenFds.FvBinDict[self.UiFvName.upper()] = FvOutputFile\r
124         return FvOutputFile\r
125 \r
126     ## __InitializeInf__()\r
127     #\r
128     #   Initilize the inf file to create FV\r
129     #\r
130     #   @param  self        The object pointer\r
131     #   @param  BaseAddress base address of FV\r
132     #   @param  BlockSize   block size of FV\r
133     #   @param  BlockNum    How many blocks in FV\r
134     #   @param  ErasePolarity      Flash erase polarity\r
135     #   @param  VtfDict     VTF objects\r
136     #\r
137     def __InitializeInf__ (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1', VtfDict=None) :\r
138         #\r
139         # Create FV inf file\r
140         #\r
141         self.InfFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r
142                                    self.UiFvName + '.inf')\r
143         self.FvInfFile = StringIO.StringIO()\r
144 \r
145         #\r
146         # Add [Options]\r
147         #\r
148         self.FvInfFile.writelines("[options]" + T_CHAR_LF)\r
149         if BaseAddress != None :\r
150             self.FvInfFile.writelines("EFI_BASE_ADDRESS = " + \\r
151                                        BaseAddress          + \\r
152                                        T_CHAR_LF)\r
153 \r
154         if BlockSize != None:\r
155             self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \\r
156                                       '0x%X' %BlockSize    + \\r
157                                       T_CHAR_LF)\r
158             if BlockNum != None:\r
159                 self.FvInfFile.writelines("EFI_NUM_BLOCKS   = "  + \\r
160                                       ' 0x%X' %BlockNum    + \\r
161                                       T_CHAR_LF)\r
162         else:\r
163             for BlockSize in self.BlockSizeList :\r
164                 if BlockSize[0] != None:\r
165                     self.FvInfFile.writelines("EFI_BLOCK_SIZE  = "  + \\r
166                                           '0x%X' %BlockSize[0]    + \\r
167                                           T_CHAR_LF)\r
168 \r
169                 if BlockSize[1] != None:\r
170                     self.FvInfFile.writelines("EFI_NUM_BLOCKS   = "  + \\r
171                                           ' 0x%X' %BlockSize[1]    + \\r
172                                           T_CHAR_LF)\r
173 \r
174         if self.BsBaseAddress != None:\r
175             self.FvInfFile.writelines('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \\r
176                                        '0x%X' %self.BsBaseAddress)\r
177         if self.RtBaseAddress != None:\r
178             self.FvInfFile.writelines('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \\r
179                                       '0x%X' %self.RtBaseAddress)\r
180         #\r
181         # Add attribute\r
182         #\r
183         self.FvInfFile.writelines("[attributes]" + T_CHAR_LF)\r
184 \r
185         self.FvInfFile.writelines("EFI_ERASE_POLARITY   = "       + \\r
186                                           ' %s' %ErasePloarity    + \\r
187                                           T_CHAR_LF)\r
188         if not (self.FvAttributeDict == None):\r
189             for FvAttribute in self.FvAttributeDict.keys() :\r
190                 self.FvInfFile.writelines("EFI_"            + \\r
191                                           FvAttribute       + \\r
192                                           ' = '             + \\r
193                                           self.FvAttributeDict[FvAttribute] + \\r
194                                           T_CHAR_LF )\r
195         if self.FvAlignment != None:\r
196             self.FvInfFile.writelines("EFI_FVB2_ALIGNMENT_"     + \\r
197                                        self.FvAlignment.strip() + \\r
198                                        " = TRUE"                + \\r
199                                        T_CHAR_LF)\r
200             \r
201         if self.FvNameGuid != None:\r
202             self.FvInfFile.writelines("EFI_FVNAME_GUID"     + \\r
203                                        " = %s" % self.FvNameGuid + \\r
204                                        T_CHAR_LF)\r
205         #\r
206         # Add [Files]\r
207         #\r
208 \r
209         self.FvInfFile.writelines("[files]" + T_CHAR_LF)\r
210         if VtfDict != None and self.UiFvName in VtfDict.keys():\r
211             self.FvInfFile.writelines("EFI_FILE_NAME = "                   + \\r
212                                        VtfDict.get(self.UiFvName)          + \\r
213                                        T_CHAR_LF)\r
214 \r
215 \r