]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/FvImageSection.py
BaseTools:ord() don't match in py2 and py3
[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
7fa0e68a
FB
74 if isinstance(FvHeaderBuffer[0x2E], str):\r
75 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)\r
76 else:\r
77 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)\r
321151fe
YZ
78 FvFileObj.close()\r
79 if FvAlignmentValue > MaxFvAlignment:\r
80 MaxFvAlignment = FvAlignmentValue\r
81\r
9e47e6f9 82 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get("FV_IMAGE"))\r
37de70b7 83 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)\r
30fdf114 84 OutputFileList.append(OutputFile)\r
321151fe
YZ
85\r
86 # MaxFvAlignment is larger than or equal to 1K\r
87 if MaxFvAlignment >= 0x400:\r
e921f58d
YZ
88 if MaxFvAlignment >= 0x100000:\r
89 #The max alignment supported by FFS is 16M.\r
1dc287c3 90 if MaxFvAlignment >= 0x1000000:\r
e921f58d
YZ
91 self.Alignment = "16M"\r
92 else:\r
b3e94a06 93 self.Alignment = str(MaxFvAlignment // 0x100000) + "M"\r
321151fe 94 else:\r
b3e94a06 95 self.Alignment = str (MaxFvAlignment // 0x400) + "K"\r
321151fe
YZ
96 else:\r
97 # MaxFvAlignment is less than 1K\r
98 self.Alignment = str (MaxFvAlignment)\r
99\r
30fdf114
LG
100 return OutputFileList, self.Alignment\r
101 #\r
102 # Generate Fv\r
103 #\r
4231a819 104 if self.FvName is not None:\r
1ccc4d89 105 Buffer = BytesIO('')\r
30fdf114 106 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName)\r
4231a819 107 if Fv is not None:\r
30fdf114 108 self.Fv = Fv\r
d7f40203
ZF
109 if not self.FvAddr and self.Fv.BaseAddress:\r
110 self.FvAddr = self.Fv.BaseAddress\r
37de70b7 111 FvFileName = Fv.AddToBuffer(Buffer, self.FvAddr, MacroDict = Dict, Flag=IsMakefile)\r
4231a819
CJ
112 if Fv.FvAlignment is not None:\r
113 if self.Alignment is None:\r
52302d4d
LG
114 self.Alignment = Fv.FvAlignment\r
115 else:\r
116 if GenFdsGlobalVariable.GetAlignment (Fv.FvAlignment) > GenFdsGlobalVariable.GetAlignment (self.Alignment):\r
117 self.Alignment = Fv.FvAlignment\r
30fdf114 118 else:\r
4231a819 119 if self.FvFileName is not None:\r
30fdf114 120 FvFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvFileName)\r
877c0a93 121 if os.path.isfile(FvFileName):\r
ccaa7754 122 FvFileObj = open (FvFileName, 'rb')\r
877c0a93
YZ
123 FvFileObj.seek(0)\r
124 # PI FvHeader is 0x48 byte\r
125 FvHeaderBuffer = FvFileObj.read(0x48)\r
126 # FV alignment position.\r
7fa0e68a
FB
127 if isinstance(FvHeaderBuffer[0x2E], str):\r
128 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)\r
129 else:\r
130 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)\r
877c0a93
YZ
131 # FvAlignmentValue is larger than or equal to 1K\r
132 if FvAlignmentValue >= 0x400:\r
e921f58d
YZ
133 if FvAlignmentValue >= 0x100000:\r
134 #The max alignment supported by FFS is 16M.\r
135 if FvAlignmentValue >= 0x1000000:\r
136 self.Alignment = "16M"\r
137 else:\r
b3e94a06 138 self.Alignment = str(FvAlignmentValue // 0x100000) + "M"\r
877c0a93 139 else:\r
b3e94a06 140 self.Alignment = str (FvAlignmentValue // 0x400) + "K"\r
877c0a93
YZ
141 else:\r
142 # FvAlignmentValue is less than 1K\r
143 self.Alignment = str (FvAlignmentValue)\r
144 FvFileObj.close()\r
75135cc6
YZ
145 else:\r
146 if len (mws.getPkgPath()) == 0:\r
147 EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in WORKSPACE: %s" % self.FvFileName, GenFdsGlobalVariable.WorkSpaceDir)\r
148 else:\r
149 EdkLogger.error("GenFds", FILE_NOT_FOUND, "%s is not found in packages path:\n\t%s" % (self.FvFileName, '\n\t'.join(mws.getPkgPath())))\r
150\r
30fdf114
LG
151 else:\r
152 EdkLogger.error("GenFds", GENFDS_ERROR, "FvImageSection Failed! %s NOT found in FDF" % self.FvName)\r
153\r
154 #\r
155 # Prepare the parameter of GenSection\r
156 #\r
9e47e6f9 157 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + SecNum + SectionSuffix.get("FV_IMAGE"))\r
37de70b7 158 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)\r
30fdf114
LG
159 OutputFileList.append(OutputFile)\r
160\r
161 return OutputFileList, self.Alignment\r