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