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