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