]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/Fd.py
BaseTools: refactor and remove un-needed use of .keys() on dictionaries
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Fd.py
1 ## @file
2 # process FD generation
3 #
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #
14
15 ##
16 # Import Modules
17 #
18 import Region
19 import Fv
20 import Common.LongFilePathOs as os
21 import StringIO
22 import sys
23 from struct import *
24 from GenFdsGlobalVariable import GenFdsGlobalVariable
25 from CommonDataClass.FdfClass import FDClassObject
26 from Common import EdkLogger
27 from Common.BuildToolError import *
28 from Common.Misc import SaveFileOnChange
29 from GenFds import GenFds
30
31 ## generate FD
32 #
33 #
34 class FD(FDClassObject):
35 ## The constructor
36 #
37 # @param self The object pointer
38 #
39 def __init__(self):
40 FDClassObject.__init__(self)
41
42 ## GenFd() method
43 #
44 # Generate FD
45 #
46 # @retval string Generated FD file name
47 #
48 def GenFd (self, Flag = False):
49 if self.FdUiName.upper() + 'fd' in GenFds.ImageBinDict:
50 return GenFds.ImageBinDict[self.FdUiName.upper() + 'fd']
51
52 #
53 # Print Information
54 #
55 FdFileName = os.path.join(GenFdsGlobalVariable.FvDir, self.FdUiName + '.fd')
56 if not Flag:
57 GenFdsGlobalVariable.InfLogger("\nFd File Name:%s (%s)" %(self.FdUiName, FdFileName))
58
59 Offset = 0x00
60 for item in self.BlockSizeList:
61 Offset = Offset + item[0] * item[1]
62 if Offset != self.Size:
63 EdkLogger.error("GenFds", GENFDS_ERROR, 'FD %s Size not consistent with block array' % self.FdUiName)
64 GenFdsGlobalVariable.VerboseLogger('Following Fv will be add to Fd !!!')
65 for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict:
66 GenFdsGlobalVariable.VerboseLogger(FvObj)
67
68 GenFdsGlobalVariable.VerboseLogger('################### Gen VTF ####################')
69 self.GenVtfFile()
70
71 HasCapsuleRegion = False
72 for RegionObj in self.RegionList:
73 if RegionObj.RegionType == 'CAPSULE':
74 HasCapsuleRegion = True
75 break
76 if HasCapsuleRegion:
77 TempFdBuffer = StringIO.StringIO('')
78 PreviousRegionStart = -1
79 PreviousRegionSize = 1
80
81 for RegionObj in self.RegionList :
82 if RegionObj.RegionType == 'CAPSULE':
83 continue
84 if RegionObj.Offset + RegionObj.Size <= PreviousRegionStart:
85 pass
86 elif RegionObj.Offset <= PreviousRegionStart or (RegionObj.Offset >=PreviousRegionStart and RegionObj.Offset < PreviousRegionStart + PreviousRegionSize):
87 pass
88 elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:
89 if not Flag:
90 GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))
91 PadRegion = Region.Region()
92 PadRegion.Offset = PreviousRegionStart + PreviousRegionSize
93 PadRegion.Size = RegionObj.Offset - PadRegion.Offset
94 if not Flag:
95 PadRegion.AddToBuffer(TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)
96 PreviousRegionStart = RegionObj.Offset
97 PreviousRegionSize = RegionObj.Size
98 #
99 # Call each region's AddToBuffer function
100 #
101 if PreviousRegionSize > self.Size:
102 pass
103 GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')
104 RegionObj.AddToBuffer (TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)
105
106 FdBuffer = StringIO.StringIO('')
107 PreviousRegionStart = -1
108 PreviousRegionSize = 1
109 for RegionObj in self.RegionList :
110 if RegionObj.Offset + RegionObj.Size <= PreviousRegionStart:
111 EdkLogger.error("GenFds", GENFDS_ERROR,
112 'Region offset 0x%X in wrong order with Region starting from 0x%X, size 0x%X\nRegions in FDF must have offsets appear in ascending order.'\
113 % (RegionObj.Offset, PreviousRegionStart, PreviousRegionSize))
114 elif RegionObj.Offset <= PreviousRegionStart or (RegionObj.Offset >=PreviousRegionStart and RegionObj.Offset < PreviousRegionStart + PreviousRegionSize):
115 EdkLogger.error("GenFds", GENFDS_ERROR,
116 'Region offset 0x%X overlaps with Region starting from 0x%X, size 0x%X' \
117 % (RegionObj.Offset, PreviousRegionStart, PreviousRegionSize))
118 elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:
119 if not Flag:
120 GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))
121 PadRegion = Region.Region()
122 PadRegion.Offset = PreviousRegionStart + PreviousRegionSize
123 PadRegion.Size = RegionObj.Offset - PadRegion.Offset
124 if not Flag:
125 PadRegion.AddToBuffer(FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)
126 PreviousRegionStart = RegionObj.Offset
127 PreviousRegionSize = RegionObj.Size
128 #
129 # Verify current region fits within allocated FD section Size
130 #
131 if PreviousRegionStart + PreviousRegionSize > self.Size:
132 EdkLogger.error("GenFds", GENFDS_ERROR,
133 'FD %s size too small to fit region with offset 0x%X and size 0x%X'
134 % (self.FdUiName, PreviousRegionStart, PreviousRegionSize))
135 #
136 # Call each region's AddToBuffer function
137 #
138 GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')
139 RegionObj.AddToBuffer (FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict,Flag=Flag)
140 #
141 # Write the buffer contents to Fd file
142 #
143 GenFdsGlobalVariable.VerboseLogger('Write the buffer contents to Fd file')
144 if not Flag:
145 SaveFileOnChange(FdFileName, FdBuffer.getvalue())
146 FdBuffer.close()
147 GenFds.ImageBinDict[self.FdUiName.upper() + 'fd'] = FdFileName
148 return FdFileName
149
150 ## generate VTF
151 #
152 # @param self The object pointer
153 #
154 def GenVtfFile (self) :
155 #
156 # Get this Fd's all Fv name
157 #
158 FvAddDict ={}
159 FvList = []
160 for RegionObj in self.RegionList:
161 if RegionObj.RegionType == 'FV':
162 if len(RegionObj.RegionDataList) == 1:
163 RegionData = RegionObj.RegionDataList[0]
164 FvList.append(RegionData.upper())
165 FvAddDict[RegionData.upper()] = (int(self.BaseAddress,16) + \
166 RegionObj.Offset, RegionObj.Size)
167 else:
168 Offset = RegionObj.Offset
169 for RegionData in RegionObj.RegionDataList:
170 FvList.append(RegionData.upper())
171 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())
172 if len(FvObj.BlockSizeList) < 1:
173 EdkLogger.error("GenFds", GENFDS_ERROR,
174 'FV.%s must point out FVs blocksize and Fv BlockNum' \
175 % FvObj.UiFvName)
176 else:
177 Size = 0
178 for blockStatement in FvObj.BlockSizeList:
179 Size = Size + blockStatement[0] * blockStatement[1]
180 FvAddDict[RegionData.upper()] = (int(self.BaseAddress,16) + \
181 Offset, Size)
182 Offset = Offset + Size
183 #
184 # Check whether this Fd need VTF
185 #
186 Flag = False
187 for VtfObj in GenFdsGlobalVariable.FdfParser.Profile.VtfList:
188 compLocList = VtfObj.GetFvList()
189 if set(compLocList).issubset(FvList):
190 Flag = True
191 break
192 if Flag == True:
193 self.vtfRawDict = VtfObj.GenVtf(FvAddDict)
194
195 ## generate flash map file
196 #
197 # @param self The object pointer
198 #
199 def GenFlashMap (self):
200 pass
201
202
203
204
205
206
207
208