]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/Fd.py
53318c9ea5c0104172b92d5d0d8977a8f897fc1a
[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 from __future__ import absolute_import
19 from . import Region
20 from . import Fv
21 import Common.LongFilePathOs as os
22 from io import BytesIO
23 import sys
24 from struct import *
25 from .GenFdsGlobalVariable import GenFdsGlobalVariable
26 from CommonDataClass.FdfClass import FDClassObject
27 from Common import EdkLogger
28 from Common.BuildToolError import *
29 from Common.Misc import SaveFileOnChange
30 from Common.DataType import BINARY_FILE_TYPE_FV
31
32 ## generate FD
33 #
34 #
35 class FD(FDClassObject):
36 ## The constructor
37 #
38 # @param self The object pointer
39 #
40 def __init__(self):
41 FDClassObject.__init__(self)
42
43 ## GenFd() method
44 #
45 # Generate FD
46 #
47 # @retval string Generated FD file name
48 #
49 def GenFd (self, Flag = False):
50 from .GenFds import GenFds
51 if self.FdUiName.upper() + 'fd' in GenFds.ImageBinDict:
52 return GenFds.ImageBinDict[self.FdUiName.upper() + 'fd']
53
54 #
55 # Print Information
56 #
57 FdFileName = os.path.join(GenFdsGlobalVariable.FvDir, self.FdUiName + '.fd')
58 if not Flag:
59 GenFdsGlobalVariable.InfLogger("\nFd File Name:%s (%s)" %(self.FdUiName, FdFileName))
60
61 Offset = 0x00
62 for item in self.BlockSizeList:
63 Offset = Offset + item[0] * item[1]
64 if Offset != self.Size:
65 EdkLogger.error("GenFds", GENFDS_ERROR, 'FD %s Size not consistent with block array' % self.FdUiName)
66 GenFdsGlobalVariable.VerboseLogger('Following Fv will be add to Fd !!!')
67 for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict:
68 GenFdsGlobalVariable.VerboseLogger(FvObj)
69
70 GenFdsGlobalVariable.VerboseLogger('################### Gen VTF ####################')
71 self.GenVtfFile()
72
73 HasCapsuleRegion = False
74 for RegionObj in self.RegionList:
75 if RegionObj.RegionType == 'CAPSULE':
76 HasCapsuleRegion = True
77 break
78 if HasCapsuleRegion:
79 TempFdBuffer = BytesIO('')
80 PreviousRegionStart = -1
81 PreviousRegionSize = 1
82
83 for RegionObj in self.RegionList :
84 if RegionObj.RegionType == 'CAPSULE':
85 continue
86 if RegionObj.Offset + RegionObj.Size <= PreviousRegionStart:
87 pass
88 elif RegionObj.Offset <= PreviousRegionStart or (RegionObj.Offset >=PreviousRegionStart and RegionObj.Offset < PreviousRegionStart + PreviousRegionSize):
89 pass
90 elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:
91 if not Flag:
92 GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))
93 PadRegion = Region.Region()
94 PadRegion.Offset = PreviousRegionStart + PreviousRegionSize
95 PadRegion.Size = RegionObj.Offset - PadRegion.Offset
96 if not Flag:
97 PadRegion.AddToBuffer(TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)
98 PreviousRegionStart = RegionObj.Offset
99 PreviousRegionSize = RegionObj.Size
100 #
101 # Call each region's AddToBuffer function
102 #
103 if PreviousRegionSize > self.Size:
104 pass
105 GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')
106 RegionObj.AddToBuffer (TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)
107
108 FdBuffer = BytesIO('')
109 PreviousRegionStart = -1
110 PreviousRegionSize = 1
111 for RegionObj in self.RegionList :
112 if RegionObj.Offset + RegionObj.Size <= PreviousRegionStart:
113 EdkLogger.error("GenFds", GENFDS_ERROR,
114 '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.'\
115 % (RegionObj.Offset, PreviousRegionStart, PreviousRegionSize))
116 elif RegionObj.Offset <= PreviousRegionStart or (RegionObj.Offset >=PreviousRegionStart and RegionObj.Offset < PreviousRegionStart + PreviousRegionSize):
117 EdkLogger.error("GenFds", GENFDS_ERROR,
118 'Region offset 0x%X overlaps with Region starting from 0x%X, size 0x%X' \
119 % (RegionObj.Offset, PreviousRegionStart, PreviousRegionSize))
120 elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:
121 if not Flag:
122 GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))
123 PadRegion = Region.Region()
124 PadRegion.Offset = PreviousRegionStart + PreviousRegionSize
125 PadRegion.Size = RegionObj.Offset - PadRegion.Offset
126 if not Flag:
127 PadRegion.AddToBuffer(FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)
128 PreviousRegionStart = RegionObj.Offset
129 PreviousRegionSize = RegionObj.Size
130 #
131 # Verify current region fits within allocated FD section Size
132 #
133 if PreviousRegionStart + PreviousRegionSize > self.Size:
134 EdkLogger.error("GenFds", GENFDS_ERROR,
135 'FD %s size too small to fit region with offset 0x%X and size 0x%X'
136 % (self.FdUiName, PreviousRegionStart, PreviousRegionSize))
137 #
138 # Call each region's AddToBuffer function
139 #
140 GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')
141 RegionObj.AddToBuffer (FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict, Flag=Flag)
142 #
143 # Write the buffer contents to Fd file
144 #
145 GenFdsGlobalVariable.VerboseLogger('Write the buffer contents to Fd file')
146 if not Flag:
147 SaveFileOnChange(FdFileName, FdBuffer.getvalue())
148 FdBuffer.close()
149 GenFds.ImageBinDict[self.FdUiName.upper() + 'fd'] = FdFileName
150 return FdFileName
151
152 ## generate VTF
153 #
154 # @param self The object pointer
155 #
156 def GenVtfFile (self) :
157 #
158 # Get this Fd's all Fv name
159 #
160 FvAddDict ={}
161 FvList = []
162 for RegionObj in self.RegionList:
163 if RegionObj.RegionType == BINARY_FILE_TYPE_FV:
164 if len(RegionObj.RegionDataList) == 1:
165 RegionData = RegionObj.RegionDataList[0]
166 FvList.append(RegionData.upper())
167 FvAddDict[RegionData.upper()] = (int(self.BaseAddress, 16) + \
168 RegionObj.Offset, RegionObj.Size)
169 else:
170 Offset = RegionObj.Offset
171 for RegionData in RegionObj.RegionDataList:
172 FvList.append(RegionData.upper())
173 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())
174 if len(FvObj.BlockSizeList) < 1:
175 EdkLogger.error("GenFds", GENFDS_ERROR,
176 'FV.%s must point out FVs blocksize and Fv BlockNum' \
177 % FvObj.UiFvName)
178 else:
179 Size = 0
180 for blockStatement in FvObj.BlockSizeList:
181 Size = Size + blockStatement[0] * blockStatement[1]
182 FvAddDict[RegionData.upper()] = (int(self.BaseAddress, 16) + \
183 Offset, Size)
184 Offset = Offset + Size
185 #
186 # Check whether this Fd need VTF
187 #
188 Flag = False
189 for VtfObj in GenFdsGlobalVariable.FdfParser.Profile.VtfList:
190 compLocList = VtfObj.GetFvList()
191 if set(compLocList).issubset(FvList):
192 Flag = True
193 break
194 if Flag == True:
195 self.vtfRawDict = VtfObj.GenVtf(FvAddDict)
196
197 ## generate flash map file
198 #
199 # @param self The object pointer
200 #
201 def GenFlashMap (self):
202 pass
203
204
205
206
207
208
209
210