]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/FvImageSection.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 = None, IsMakefile = False):
51
52 OutputFileList = []
53 if Dict is None:
54 Dict = {}
55 if self.FvFileType is not None:
56 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FvFileType, self.FvFileExtension)
57 if IsSect :
58 return FileList, self.Alignment
59
60 Num = SecNum
61
62 MaxFvAlignment = 0
63 for FvFileName in FileList:
64 FvAlignmentValue = 0
65 if os.path.isfile(FvFileName):
66 FvFileObj = open (FvFileName, 'rb')
67 FvFileObj.seek(0)
68 # PI FvHeader is 0x48 byte
69 FvHeaderBuffer = FvFileObj.read(0x48)
70 # FV alignment position.
71 if isinstance(FvHeaderBuffer[0x2E], str):
72 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)
73 else:
74 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)
75 FvFileObj.close()
76 if FvAlignmentValue > MaxFvAlignment:
77 MaxFvAlignment = FvAlignmentValue
78
79 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get("FV_IMAGE"))
80 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)
81 OutputFileList.append(OutputFile)
82
83 # MaxFvAlignment is larger than or equal to 1K
84 if MaxFvAlignment >= 0x400:
85 if MaxFvAlignment >= 0x100000:
86 #The max alignment supported by FFS is 16M.
87 if MaxFvAlignment >= 0x1000000:
88 self.Alignment = "16M"
89 else:
90 self.Alignment = str(MaxFvAlignment // 0x100000) + "M"
91 else:
92 self.Alignment = str (MaxFvAlignment // 0x400) + "K"
93 else:
94 # MaxFvAlignment is less than 1K
95 self.Alignment = str (MaxFvAlignment)
96
97 return OutputFileList, self.Alignment
98 #
99 # Generate Fv
100 #
101 if self.FvName is not None:
102 Buffer = BytesIO()
103 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName)
104 if Fv is not None:
105 self.Fv = Fv
106 if not self.FvAddr and self.Fv.BaseAddress:
107 self.FvAddr = self.Fv.BaseAddress
108 FvFileName = Fv.AddToBuffer(Buffer, self.FvAddr, MacroDict = Dict, Flag=IsMakefile)
109 if Fv.FvAlignment is not None:
110 if self.Alignment is None:
111 self.Alignment = Fv.FvAlignment
112 else:
113 if GenFdsGlobalVariable.GetAlignment (Fv.FvAlignment) > GenFdsGlobalVariable.GetAlignment (self.Alignment):
114 self.Alignment = Fv.FvAlignment
115 else:
116 if self.FvFileName is not None:
117 FvFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvFileName)
118 if os.path.isfile(FvFileName):
119 FvFileObj = open (FvFileName, 'rb')
120 FvFileObj.seek(0)
121 # PI FvHeader is 0x48 byte
122 FvHeaderBuffer = FvFileObj.read(0x48)
123 # FV alignment position.
124 if isinstance(FvHeaderBuffer[0x2E], str):
125 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)
126 else:
127 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)
128 # FvAlignmentValue is larger than or equal to 1K
129 if FvAlignmentValue >= 0x400:
130 if FvAlignmentValue >= 0x100000:
131 #The max alignment supported by FFS is 16M.
132 if FvAlignmentValue >= 0x1000000:
133 self.Alignment = "16M"
134 else:
135 self.Alignment = str(FvAlignmentValue // 0x100000) + "M"
136 else:
137 self.Alignment = str (FvAlignmentValue // 0x400) + "K"
138 else:
139 # FvAlignmentValue is less than 1K
140 self.Alignment = str (FvAlignmentValue)
141 FvFileObj.close()
142 else:
143 if len (mws.getPkgPath()) == 0:
144 EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in WORKSPACE: %s" % self.FvFileName, GenFdsGlobalVariable.WorkSpaceDir)
145 else:
146 EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in packages path:\n\t%s" % (self.FvFileName, '\n\t'.join(mws.getPkgPath())))
147
148 else:
149 EdkLogger.error("GenFds", GENFDS_ERROR, "FvImageSection Failed! %s NOT found in FDF" % self.FvName)
150
151 #
152 # Prepare the parameter of GenSection
153 #
154 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + SecNum + SectionSuffix.get("FV_IMAGE"))
155 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)
156 OutputFileList.append(OutputFile)
157
158 return OutputFileList, self.Alignment