]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/FvImageSection.py
535b86ab5ea902d5b8be51208957fd10b9529f52
[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 # 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 Section
20 from io import BytesIO
21 from .Ffs import SectionSuffix
22 import subprocess
23 from .GenFdsGlobalVariable import GenFdsGlobalVariable
24 import Common.LongFilePathOs as os
25 from CommonDataClass.FdfClass import FvImageSectionClassObject
26 from Common import EdkLogger
27 from Common.BuildToolError import *
28 from Common.DataType import *
29
30 ## generate FV image section
31 #
32 #
33 class FvImageSection(FvImageSectionClassObject):
34
35 ## The constructor
36 #
37 # @param self The object pointer
38 #
39 def __init__(self):
40 FvImageSectionClassObject.__init__(self)
41
42 ## GenSection() method
43 #
44 # Generate FV image section
45 #
46 # @param self The object pointer
47 # @param OutputPath Where to place output file
48 # @param ModuleName Which module this section belongs to
49 # @param SecNum Index of section
50 # @param KeyStringList Filter for inputs of section generation
51 # @param FfsInf FfsInfStatement object that contains this section data
52 # @param Dict dictionary contains macro and its value
53 # @retval tuple (Generated file name, section alignment)
54 #
55 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False):
56
57 OutputFileList = []
58 if self.FvFileType is not None:
59 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FvFileType, self.FvFileExtension)
60 if IsSect :
61 return FileList, self.Alignment
62
63 Num = SecNum
64
65 MaxFvAlignment = 0
66 for FvFileName in FileList:
67 FvAlignmentValue = 0
68 if os.path.isfile(FvFileName):
69 FvFileObj = open (FvFileName, 'rb')
70 FvFileObj.seek(0)
71 # PI FvHeader is 0x48 byte
72 FvHeaderBuffer = FvFileObj.read(0x48)
73 # FV alignment position.
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 FvAlignmentValue = 1 << (ord (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