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