]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/FfsFileStatement.py
BaseTools: Use absolute import in GenFds
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FfsFileStatement.py
CommitLineData
30fdf114
LG
1## @file\r
2# process FFS generation from FILE statement\r
3#\r
9eb87141 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
bfa65b61
GL
18from __future__ import absolute_import\r
19from . import Ffs\r
20from . import Rule\r
1be2ed90 21import Common.LongFilePathOs as os\r
86379ac4 22from io import BytesIO\r
30fdf114 23import subprocess\r
52302d4d 24\r
bfa65b61 25from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
30fdf114
LG
26from CommonDataClass.FdfClass import FileStatementClassObject\r
27from Common import EdkLogger\r
28from Common.BuildToolError import *\r
29from Common.Misc import GuidStructureByteArrayToGuidString\r
bfa65b61
GL
30from .GuidSection import GuidSection\r
31from .FvImageSection import FvImageSection\r
860992ed
YZ
32from Common.Misc import SaveFileOnChange\r
33from struct import *\r
30fdf114
LG
34\r
35## generate FFS from FILE\r
36#\r
37#\r
38class FileStatement (FileStatementClassObject) :\r
39 ## The constructor\r
40 #\r
41 # @param self The object pointer\r
42 #\r
43 def __init__(self):\r
44 FileStatementClassObject.__init__(self)\r
79b74a03
LG
45 self.CurrentLineNum = None\r
46 self.CurrentLineContent = None\r
47 self.FileName = None\r
48 self.InfFileName = None\r
cfaaf99b 49 self.SubAlignment = None\r
30fdf114
LG
50\r
51 ## GenFfs() method\r
52 #\r
53 # Generate FFS\r
54 #\r
52302d4d
LG
55 # @param self The object pointer\r
56 # @param Dict dictionary contains macro and value pair\r
57 # @param FvChildAddr Array of the inside FvImage base address\r
58 # @param FvParentAddr Parent Fv base address\r
59 # @retval string Generated FFS file name\r
30fdf114 60 #\r
a743986d 61 def GenFfs(self, Dict = {}, FvChildAddr=[], FvParentAddr=None, IsMakefile=False, FvName=None):\r
f7496d71 62\r
4231a819 63 if self.NameGuid is not None and self.NameGuid.startswith('PCD('):\r
30fdf114
LG
64 PcdValue = GenFdsGlobalVariable.GetPcdValue(self.NameGuid)\r
65 if len(PcdValue) == 0:\r
66 EdkLogger.error("GenFds", GENFDS_ERROR, '%s NOT defined.' \\r
67 % (self.NameGuid))\r
68 if PcdValue.startswith('{'):\r
69 PcdValue = GuidStructureByteArrayToGuidString(PcdValue)\r
70 RegistryGuidStr = PcdValue\r
71 if len(RegistryGuidStr) == 0:\r
72 EdkLogger.error("GenFds", GENFDS_ERROR, 'GUID value for %s in wrong format.' \\r
73 % (self.NameGuid))\r
74 self.NameGuid = RegistryGuidStr\r
f7496d71 75\r
a743986d
YZ
76 Str = self.NameGuid\r
77 if FvName:\r
78 Str += FvName\r
79 OutputDir = os.path.join(GenFdsGlobalVariable.FfsDir, Str)\r
30fdf114 80 if not os.path.exists(OutputDir):\r
0d2711a6 81 os.makedirs(OutputDir)\r
30fdf114
LG
82\r
83 Dict.update(self.DefineVarDict)\r
84 SectionAlignments = None\r
4231a819 85 if self.FvName is not None :\r
86379ac4 86 Buffer = BytesIO('')\r
9eb87141 87 if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
30fdf114
LG
88 EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (self.FvName))\r
89 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
90 FileName = Fv.AddToBuffer(Buffer)\r
91 SectionFiles = [FileName]\r
92\r
4231a819 93 elif self.FdName is not None:\r
9eb87141 94 if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
30fdf114
LG
95 EdkLogger.error("GenFds", GENFDS_ERROR, "FD (%s) is NOT described in FDF file!" % (self.FdName))\r
96 Fd = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
fd171542 97 FileName = Fd.GenFd()\r
30fdf114
LG
98 SectionFiles = [FileName]\r
99\r
4231a819 100 elif self.FileName is not None:\r
860992ed 101 if hasattr(self, 'FvFileType') and self.FvFileType == 'RAW':\r
cfaaf99b 102 if isinstance(self.FileName, list) and isinstance(self.SubAlignment, list) and len(self.FileName) == len(self.SubAlignment):\r
860992ed 103 FileContent = ''\r
cfaaf99b
YZ
104 MaxAlignIndex = 0\r
105 MaxAlignValue = 1\r
860992ed
YZ
106 for Index, File in enumerate(self.FileName):\r
107 try:\r
f8db6527 108 f = open(File, 'rb')\r
860992ed
YZ
109 except:\r
110 GenFdsGlobalVariable.ErrorLogger("Error opening RAW file %s." % (File))\r
111 Content = f.read()\r
112 f.close()\r
cfaaf99b 113 AlignValue = 1\r
4231a819 114 if self.SubAlignment[Index] is not None:\r
cfaaf99b
YZ
115 AlignValue = GenFdsGlobalVariable.GetAlignment(self.SubAlignment[Index])\r
116 if AlignValue > MaxAlignValue:\r
117 MaxAlignIndex = Index\r
118 MaxAlignValue = AlignValue\r
860992ed
YZ
119 FileContent += Content\r
120 if len(FileContent) % AlignValue != 0:\r
121 Size = AlignValue - len(FileContent) % AlignValue\r
122 for i in range(0, Size):\r
123 FileContent += pack('B', 0xFF)\r
124\r
125 if FileContent:\r
126 OutputRAWFile = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid, self.NameGuid + '.raw')\r
127 SaveFileOnChange(OutputRAWFile, FileContent, True)\r
128 self.FileName = OutputRAWFile\r
cfaaf99b
YZ
129 self.SubAlignment = self.SubAlignment[MaxAlignIndex]\r
130\r
131 if self.Alignment and self.SubAlignment:\r
132 if GenFdsGlobalVariable.GetAlignment (self.Alignment) < GenFdsGlobalVariable.GetAlignment (self.SubAlignment):\r
133 self.Alignment = self.SubAlignment\r
134 elif self.SubAlignment:\r
135 self.Alignment = self.SubAlignment\r
136\r
30fdf114 137 self.FileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FileName)\r
97fa0ee9
YL
138 #Replace $(SAPCE) with real space\r
139 self.FileName = self.FileName.replace('$(SPACE)', ' ')\r
30fdf114
LG
140 SectionFiles = [GenFdsGlobalVariable.MacroExtend(self.FileName, Dict)]\r
141\r
142 else:\r
143 SectionFiles = []\r
144 Index = 0\r
145 SectionAlignments = []\r
0d2711a6 146 for section in self.SectionList :\r
30fdf114
LG
147 Index = Index + 1\r
148 SecIndex = '%d' %Index\r
52302d4d
LG
149 # process the inside FvImage from FvSection or GuidSection\r
150 if FvChildAddr != []:\r
151 if isinstance(section, FvImageSection):\r
152 section.FvAddr = FvChildAddr.pop(0)\r
153 elif isinstance(section, GuidSection):\r
154 section.FvAddr = FvChildAddr\r
4231a819 155 if FvParentAddr is not None and isinstance(section, GuidSection):\r
52302d4d
LG
156 section.FvParentAddr = FvParentAddr\r
157\r
4afd3d04
LG
158 if self.KeepReloc == False:\r
159 section.KeepReloc = False\r
30fdf114
LG
160 sectList, align = section.GenSection(OutputDir, self.NameGuid, SecIndex, self.KeyStringList, None, Dict)\r
161 if sectList != []:\r
162 for sect in sectList:\r
163 SectionFiles.append(sect)\r
164 SectionAlignments.append(align)\r
165\r
166 #\r
167 # Prepare the parameter\r
168 #\r
169 FfsFileOutput = os.path.join(OutputDir, self.NameGuid + '.ffs')\r
170 GenFdsGlobalVariable.GenerateFfs(FfsFileOutput, SectionFiles,\r
171 Ffs.Ffs.FdfFvFileTypeToFileType.get(self.FvFileType),\r
172 self.NameGuid,\r
173 Fixed=self.Fixed,\r
174 CheckSum=self.CheckSum,\r
175 Align=self.Alignment,\r
176 SectionAlign=SectionAlignments\r
177 )\r
178\r
179 return FfsFileOutput\r
180\r
181\r
182\r