]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/FvImageSection.py
BaseTools:Add import in FvImageSection
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FvImageSection.py
1 ## @file
2 # process FV image section generation
3 #
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 #
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 #
8
9 ##
10 # Import Modules
11 #
12 from __future__ import absolute_import
13 from . import Section
14 from io import BytesIO
15 from .Ffs import SectionSuffix
16 import subprocess
17 from .GenFdsGlobalVariable import GenFdsGlobalVariable
18 import Common.LongFilePathOs as os
19 from CommonDataClass.FdfClass import FvImageSectionClassObject
20 from Common.MultipleWorkspace import MultipleWorkspace as mws
21 from Common import EdkLogger
22 from Common.BuildToolError import *
23 from Common.DataType import *
24
25 ## generate FV image section
26 #
27 #
28 class FvImageSection(FvImageSectionClassObject):
29
30 ## The constructor
31 #
32 # @param self The object pointer
33 #
34 def __init__(self):
35 FvImageSectionClassObject.__init__(self)
36
37 ## GenSection() method
38 #
39 # Generate FV image section
40 #
41 # @param self The object pointer
42 # @param OutputPath Where to place output file
43 # @param ModuleName Which module this section belongs to
44 # @param SecNum Index of section
45 # @param KeyStringList Filter for inputs of section generation
46 # @param FfsInf FfsInfStatement object that contains this section data
47 # @param Dict dictionary contains macro and its value
48 # @retval tuple (Generated file name, section alignment)
49 #
50 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False):
51
52 OutputFileList = []
53 if self.FvFileType is not None:
54 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FvFileType, self.FvFileExtension)
55 if IsSect :
56 return FileList, self.Alignment
57
58 Num = SecNum
59
60 MaxFvAlignment = 0
61 for FvFileName in FileList:
62 FvAlignmentValue = 0
63 if os.path.isfile(FvFileName):
64 FvFileObj = open (FvFileName, 'rb')
65 FvFileObj.seek(0)
66 # PI FvHeader is 0x48 byte
67 FvHeaderBuffer = FvFileObj.read(0x48)
68 # FV alignment position.
69 if isinstance(FvHeaderBuffer[0x2E], str):
70 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)
71 else:
72 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)
73 FvFileObj.close()
74 if FvAlignmentValue > MaxFvAlignment:
75 MaxFvAlignment = FvAlignmentValue
76
77 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get("FV_IMAGE"))
78 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)
79 OutputFileList.append(OutputFile)
80
81 # MaxFvAlignment is larger than or equal to 1K
82 if MaxFvAlignment >= 0x400:
83 if MaxFvAlignment >= 0x100000:
84 #The max alignment supported by FFS is 16M.
85 if MaxFvAlignment >= 0x1000000:
86 self.Alignment = "16M"
87 else:
88 self.Alignment = str(MaxFvAlignment // 0x100000) + "M"
89 else:
90 self.Alignment = str (MaxFvAlignment // 0x400) + "K"
91 else:
92 # MaxFvAlignment is less than 1K
93 self.Alignment = str (MaxFvAlignment)
94
95 return OutputFileList, self.Alignment
96 #
97 # Generate Fv
98 #
99 if self.FvName is not None:
100 Buffer = BytesIO()
101 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName)
102 if Fv is not None:
103 self.Fv = Fv
104 if not self.FvAddr and self.Fv.BaseAddress:
105 self.FvAddr = self.Fv.BaseAddress
106 FvFileName = Fv.AddToBuffer(Buffer, self.FvAddr, MacroDict = Dict, Flag=IsMakefile)
107 if Fv.FvAlignment is not None:
108 if self.Alignment is None:
109 self.Alignment = Fv.FvAlignment
110 else:
111 if GenFdsGlobalVariable.GetAlignment (Fv.FvAlignment) > GenFdsGlobalVariable.GetAlignment (self.Alignment):
112 self.Alignment = Fv.FvAlignment
113 else:
114 if self.FvFileName is not None:
115 FvFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvFileName)
116 if os.path.isfile(FvFileName):
117 FvFileObj = open (FvFileName, 'rb')
118 FvFileObj.seek(0)
119 # PI FvHeader is 0x48 byte
120 FvHeaderBuffer = FvFileObj.read(0x48)
121 # FV alignment position.
122 if isinstance(FvHeaderBuffer[0x2E], str):
123 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)
124 else:
125 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)
126 # FvAlignmentValue is larger than or equal to 1K
127 if FvAlignmentValue >= 0x400:
128 if FvAlignmentValue >= 0x100000:
129 #The max alignment supported by FFS is 16M.
130 if FvAlignmentValue >= 0x1000000:
131 self.Alignment = "16M"
132 else:
133 self.Alignment = str(FvAlignmentValue // 0x100000) + "M"
134 else:
135 self.Alignment = str (FvAlignmentValue // 0x400) + "K"
136 else:
137 # FvAlignmentValue is less than 1K
138 self.Alignment = str (FvAlignmentValue)
139 FvFileObj.close()
140 else:
141 if len (mws.getPkgPath()) == 0:
142 EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in WORKSPACE: %s" % self.FvFileName, GenFdsGlobalVariable.WorkSpaceDir)
143 else:
144 EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in packages path:\n\t%s" % (self.FvFileName, '\n\t'.join(mws.getPkgPath())))
145
146 else:
147 EdkLogger.error("GenFds", GENFDS_ERROR, "FvImageSection Failed! %s NOT found in FDF" % self.FvName)
148
149 #
150 # Prepare the parameter of GenSection
151 #
152 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + SecNum + SectionSuffix.get("FV_IMAGE"))
153 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)
154 OutputFileList.append(OutputFile)
155
156 return OutputFileList, self.Alignment