]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/FvImageSection.py
BaseTools: Replace BSD License with BSD+Patent License
[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
LG
19from CommonDataClass.FdfClass import FvImageSectionClassObject\r
20from Common import EdkLogger\r
21from Common.BuildToolError import *\r
8bb63e37 22from Common.DataType import *\r
30fdf114
LG
23\r
24## generate FV image section\r
25#\r
26#\r
27class FvImageSection(FvImageSectionClassObject):\r
28\r
29 ## The constructor\r
30 #\r
31 # @param self The object pointer\r
32 #\r
33 def __init__(self):\r
34 FvImageSectionClassObject.__init__(self)\r
35\r
36 ## GenSection() method\r
37 #\r
38 # Generate FV image section\r
39 #\r
40 # @param self The object pointer\r
41 # @param OutputPath Where to place output file\r
42 # @param ModuleName Which module this section belongs to\r
43 # @param SecNum Index of section\r
44 # @param KeyStringList Filter for inputs of section generation\r
45 # @param FfsInf FfsInfStatement object that contains this section data\r
46 # @param Dict dictionary contains macro and its value\r
47 # @retval tuple (Generated file name, section alignment)\r
48 #\r
37de70b7 49 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}, IsMakefile = False):\r
30fdf114
LG
50\r
51 OutputFileList = []\r
4231a819 52 if self.FvFileType is not None:\r
30fdf114
LG
53 FileList, IsSect = Section.Section.GetFileList(FfsInf, self.FvFileType, self.FvFileExtension)\r
54 if IsSect :\r
55 return FileList, self.Alignment\r
56\r
57 Num = SecNum\r
58\r
321151fe
YZ
59 MaxFvAlignment = 0\r
60 for FvFileName in FileList:\r
61 FvAlignmentValue = 0\r
62 if os.path.isfile(FvFileName):\r
ccaa7754 63 FvFileObj = open (FvFileName, 'rb')\r
321151fe
YZ
64 FvFileObj.seek(0)\r
65 # PI FvHeader is 0x48 byte\r
66 FvHeaderBuffer = FvFileObj.read(0x48)\r
67 # FV alignment position.\r
7fa0e68a
FB
68 if isinstance(FvHeaderBuffer[0x2E], str):\r
69 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)\r
70 else:\r
71 FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F)\r
321151fe
YZ
72 FvFileObj.close()\r
73 if FvAlignmentValue > MaxFvAlignment:\r
74 MaxFvAlignment = FvAlignmentValue\r
75\r
9e47e6f9 76 OutputFile = os.path.join(OutputPath, ModuleName + SUP_MODULE_SEC + Num + SectionSuffix.get("FV_IMAGE"))\r
37de70b7 77 GenFdsGlobalVariable.GenerateSection(OutputFile, [FvFileName], 'EFI_SECTION_FIRMWARE_VOLUME_IMAGE', IsMakefile=IsMakefile)\r
30fdf114 78 OutputFileList.append(OutputFile)\r
321151fe
YZ
79\r
80 # MaxFvAlignment is larger than or equal to 1K\r
81 if MaxFvAlignment >= 0x400:\r
e921f58d
YZ
82 if MaxFvAlignment >= 0x100000:\r
83 #The max alignment supported by FFS is 16M.\r
1dc287c3 84 if MaxFvAlignment >= 0x1000000:\r
e921f58d
YZ
85 self.Alignment = "16M"\r
86 else:\r
b3e94a06 87 self.Alignment = str(MaxFvAlignment // 0x100000) + "M"\r
321151fe 88 else:\r
b3e94a06 89 self.Alignment = str (MaxFvAlignment // 0x400) + "K"\r
321151fe
YZ
90 else:\r
91 # MaxFvAlignment is less than 1K\r
92 self.Alignment = str (MaxFvAlignment)\r
93\r
30fdf114
LG
94 return OutputFileList, self.Alignment\r
95 #\r
96 # Generate Fv\r
97 #\r
4231a819 98 if self.FvName is not None:\r
d943b0c3 99 Buffer = BytesIO()\r
30fdf114 100 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName)\r
4231a819 101 if Fv is not None:\r
30fdf114 102 self.Fv = Fv\r
d7f40203
ZF
103 if not self.FvAddr and self.Fv.BaseAddress:\r
104 self.FvAddr = self.Fv.BaseAddress\r
37de70b7 105 FvFileName = Fv.AddToBuffer(Buffer, self.FvAddr, MacroDict = Dict, Flag=IsMakefile)\r
4231a819
CJ
106 if Fv.FvAlignment is not None:\r
107 if self.Alignment is None:\r
52302d4d
LG
108 self.Alignment = Fv.FvAlignment\r
109 else:\r
110 if GenFdsGlobalVariable.GetAlignment (Fv.FvAlignment) > GenFdsGlobalVariable.GetAlignment (self.Alignment):\r
111 self.Alignment = Fv.FvAlignment\r
30fdf114 112 else:\r
4231a819 113 if self.FvFileName is not None:\r
30fdf114 114 FvFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvFileName)\r
877c0a93 115 if os.path.isfile(FvFileName):\r
ccaa7754 116 FvFileObj = open (FvFileName, 'rb')\r
877c0a93
YZ
117 FvFileObj.seek(0)\r
118 # PI FvHeader is 0x48 byte\r
119 FvHeaderBuffer = FvFileObj.read(0x48)\r
120 # FV alignment position.\r
7fa0e68a
FB
121 if isinstance(FvHeaderBuffer[0x2E], str):\r
122 FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)\r
123 else:\r
124 FvAlignmentValue = 1 << (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