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