Modify GenFds related files.
[people/mcb30/basetools.git] / Source / Python / GenFds / Fv.py
1 import Ffs\r
2 import AprioriSection\r
3 from GenFdsGlobalVariable import GenFdsGlobalVariable\r
4 import os\r
5 import subprocess\r
6 \r
7 T_CHAR_LF = '\n'\r
8 \r
9 class FV:\r
10     def __init__(self):\r
11         self.UiFvName = None\r
12         self.CreateFileName = None\r
13         # 3-tuple list (blockSize, numBlocks, pcd)\r
14         self.BlockSizeList = []\r
15         # DefineVarDict[var] = value\r
16         self.DefineVarDict = {}\r
17         # SetVarDict[var] = value\r
18         self.SetVarDict = {}\r
19         self.FvAlignment = None\r
20         # FvAttributeDict[attribute] = TRUE/FALSE (1/0)\r
21         self.FvAttributeDict = {}\r
22 ##        self.FvAttributeset = None\r
23 ##        self.FvAttributeClear = None\r
24         self.AprioriSection = None\r
25         self.FfsList = []\r
26         \r
27         self.FvInfFile = None\r
28         self.BaseAddress = None\r
29         \r
30     #\r
31     #  Generate Fv and add it to the Buffer\r
32     #\r
33     def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None) :\r
34         self.__InitialInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)\r
35         #\r
36         # First Process the Apriori section\r
37         #\r
38         if not (self.AprioriSection == None):\r
39             FileNameList = self.AprioriSection.GenFfs ()\r
40             #\r
41             # Add Apriori section included Ffs file name to Inf file\r
42             #\r
43             for Ffs in FileNameList :\r
44                 self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
45                                            Ffs               + \\r
46                                            T_CHAR_LF)\r
47         #\r
48         # Process Modules in FfsList\r
49         #\r
50         for FfsFile in self.FfsList :\r
51             FileName = FfsFile.GenFfs()\r
52             self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
53                                        FileName          + \\r
54                                        T_CHAR_LF)\r
55             \r
56         self.FvInfFile.close()\r
57         #\r
58         # Call GenFv tool\r
59         #\r
60         \r
61         FvOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName)\r
62         FvOutputFile = FvOutputFile + '.Fv'\r
63         cmd = 'GenFv -i '                 + \\r
64                self.InfFileName           + \\r
65                ' -o '                     + \\r
66                FvOutputFile\r
67         #\r
68         # Call GenFv Tools\r
69         #\r
70         print cmd\r
71         GenFdsGlobalVariable.CallExternalTool(cmd, "GenFv Failed!")\r
72         #\r
73         # Write the Fv contents to Buffer\r
74         #\r
75         fv = open ( FvOutputFile,'r+b')\r
76                    \r
77         print "Generate %s Fv Successful" %self.UiFvName\r
78               \r
79         Buffer.write(fv.read())\r
80         fv.close\r
81         return FvOutputFile\r
82     \r
83     def __InitialInf__ (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1', VtfDict=None) :\r
84         self.InfFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r
85                                    self.UiFvName + '.inf')\r
86         self.FvInfFile = open (self.InfFileName, 'w+')\r
87         \r
88         #\r
89         # Add [Options]\r
90         #\r
91         self.FvInfFile.writelines("[options]" + T_CHAR_LF)\r
92         if BaseAddress != None :\r
93             self.FvInfFile.writelines("EFI_BASE_ADDRESS = " + \\r
94                                        BaseAddress          + \\r
95                                        T_CHAR_LF)\r
96                                        \r
97         if BlockSize != None and BlockNum != None:\r
98             self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \\r
99                                       '0x%x' %BlockSize    + \\r
100                                       T_CHAR_LF)\r
101             self.FvInfFile.writelines("EFI_NUM_BLOCKS   = "  + \\r
102                                       ' 0x%x' %BlockNum    + \\r
103                                       T_CHAR_LF)\r
104         else:\r
105             for BlockSize in self.BlockSizeList :\r
106                 self.FvInfFile.writelines("EFI_BLOCK_SIZE  = "  + \\r
107                                           '0x%x' %BlockSize[0]    + \\r
108                                           T_CHAR_LF)\r
109                                   \r
110                 self.FvInfFile.writelines("EFI_NUM_BLOCKS   = "  + \\r
111                                           ' 0x%x' %BlockSize[1]    + \\r
112                                           T_CHAR_LF)\r
113                                           \r
114         \r
115         #\r
116         # Add attribute\r
117         #\r
118         self.FvInfFile.writelines("[attributes]" + T_CHAR_LF)\r
119         \r
120         self.FvInfFile.writelines("EFI_ERASE_POLARITY   = "       + \\r
121                                           ' %s' %ErasePloarity    + \\r
122                                           T_CHAR_LF)\r
123         if not (self.FvAttributeDict == None):\r
124             for FvAttribute in self.FvAttributeDict.keys() :\r
125                 self.FvInfFile.writelines("EFI_"            + \\r
126                                           FvAttribute       + \\r
127                                           ' = '             + \\r
128                                           self.FvAttributeDict[FvAttribute] + \\r
129                                           T_CHAR_LF )\r
130         if self.FvAlignment != None:\r
131             self.FvInfFile.writelines("EFI_FVB2_ALIGNMENT_"     + \\r
132                                        self.FvAlignment.strip() + \\r
133                                        " = TRUE"                + \\r
134                                        T_CHAR_LF)\r
135         #\r
136         # Add [Files]\r
137         #\r
138             \r
139         self.FvInfFile.writelines("[files]" + T_CHAR_LF)\r
140         if VtfDict != None and self.UiFvName in VtfDict.keys():\r
141             self.FvInfFile.writelines("EFI_FILE_NAME = "                   + \\r
142                                        VtfDict.get(self.UiFvName)          + \\r
143                                        T_CHAR_LF)\r
144         \r
145 \r
146 \r
147 \r
148     \r
149 \r
150 \r