]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - BaseTools/Source/Python/GenFds/FfsFileStatement.py
BaseTools: Enhance BaseTools supports FixedAtBuild usage in VFR file
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FfsFileStatement.py
... / ...
CommitLineData
1## @file\r
2# process FFS generation from FILE statement\r
3#\r
4# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
5#\r
6# This program and the accompanying materials\r
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
20import Common.LongFilePathOs as os\r
21import StringIO\r
22import subprocess\r
23\r
24from GenFdsGlobalVariable import GenFdsGlobalVariable\r
25from CommonDataClass.FdfClass import FileStatementClassObject\r
26from Common import EdkLogger\r
27from Common.BuildToolError import *\r
28from Common.Misc import GuidStructureByteArrayToGuidString\r
29from GuidSection import GuidSection\r
30from FvImageSection import FvImageSection\r
31from Common.Misc import SaveFileOnChange\r
32from struct import *\r
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
44 self.CurrentLineNum = None\r
45 self.CurrentLineContent = None\r
46 self.FileName = None\r
47 self.InfFileName = None\r
48 self.SubAlignment = None\r
49\r
50 ## GenFfs() method\r
51 #\r
52 # Generate FFS\r
53 #\r
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
59 #\r
60 def GenFfs(self, Dict = {}, FvChildAddr=[], FvParentAddr=None):\r
61 \r
62 if self.NameGuid != None and self.NameGuid.startswith('PCD('):\r
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
75 OutputDir = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid)\r
76 if not os.path.exists(OutputDir):\r
77 os.makedirs(OutputDir)\r
78\r
79 Dict.update(self.DefineVarDict)\r
80 SectionAlignments = None\r
81 if self.FvName != None :\r
82 Buffer = StringIO.StringIO('')\r
83 if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
84 EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (self.FvName))\r
85 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
86 FileName = Fv.AddToBuffer(Buffer)\r
87 SectionFiles = [FileName]\r
88\r
89 elif self.FdName != None:\r
90 if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
91 EdkLogger.error("GenFds", GENFDS_ERROR, "FD (%s) is NOT described in FDF file!" % (self.FdName))\r
92 Fd = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
93 FileName = Fd.GenFd()\r
94 SectionFiles = [FileName]\r
95\r
96 elif self.FileName != None:\r
97 if hasattr(self, 'FvFileType') and self.FvFileType == 'RAW':\r
98 if isinstance(self.FileName, list) and isinstance(self.SubAlignment, list) and len(self.FileName) == len(self.SubAlignment):\r
99 FileContent = ''\r
100 MaxAlignIndex = 0\r
101 MaxAlignValue = 1\r
102 for Index, File in enumerate(self.FileName):\r
103 try:\r
104 f = open(File, 'rb')\r
105 except:\r
106 GenFdsGlobalVariable.ErrorLogger("Error opening RAW file %s." % (File))\r
107 Content = f.read()\r
108 f.close()\r
109 AlignValue = 1\r
110 if self.SubAlignment[Index] != None:\r
111 AlignValue = GenFdsGlobalVariable.GetAlignment(self.SubAlignment[Index])\r
112 if AlignValue > MaxAlignValue:\r
113 MaxAlignIndex = Index\r
114 MaxAlignValue = AlignValue\r
115 FileContent += Content\r
116 if len(FileContent) % AlignValue != 0:\r
117 Size = AlignValue - len(FileContent) % AlignValue\r
118 for i in range(0, Size):\r
119 FileContent += pack('B', 0xFF)\r
120\r
121 if FileContent:\r
122 OutputRAWFile = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid, self.NameGuid + '.raw')\r
123 SaveFileOnChange(OutputRAWFile, FileContent, True)\r
124 self.FileName = OutputRAWFile\r
125 self.SubAlignment = self.SubAlignment[MaxAlignIndex]\r
126\r
127 if self.Alignment and self.SubAlignment:\r
128 if GenFdsGlobalVariable.GetAlignment (self.Alignment) < GenFdsGlobalVariable.GetAlignment (self.SubAlignment):\r
129 self.Alignment = self.SubAlignment\r
130 elif self.SubAlignment:\r
131 self.Alignment = self.SubAlignment\r
132\r
133 self.FileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FileName)\r
134 #Replace $(SAPCE) with real space\r
135 self.FileName = self.FileName.replace('$(SPACE)', ' ')\r
136 SectionFiles = [GenFdsGlobalVariable.MacroExtend(self.FileName, Dict)]\r
137\r
138 else:\r
139 SectionFiles = []\r
140 Index = 0\r
141 SectionAlignments = []\r
142 for section in self.SectionList :\r
143 Index = Index + 1\r
144 SecIndex = '%d' %Index\r
145 # process the inside FvImage from FvSection or GuidSection\r
146 if FvChildAddr != []:\r
147 if isinstance(section, FvImageSection):\r
148 section.FvAddr = FvChildAddr.pop(0)\r
149 elif isinstance(section, GuidSection):\r
150 section.FvAddr = FvChildAddr\r
151 if FvParentAddr != None and isinstance(section, GuidSection):\r
152 section.FvParentAddr = FvParentAddr\r
153\r
154 if self.KeepReloc == False:\r
155 section.KeepReloc = False\r
156 sectList, align = section.GenSection(OutputDir, self.NameGuid, SecIndex, self.KeyStringList, None, Dict)\r
157 if sectList != []:\r
158 for sect in sectList:\r
159 SectionFiles.append(sect)\r
160 SectionAlignments.append(align)\r
161\r
162 #\r
163 # Prepare the parameter\r
164 #\r
165 FfsFileOutput = os.path.join(OutputDir, self.NameGuid + '.ffs')\r
166 GenFdsGlobalVariable.GenerateFfs(FfsFileOutput, SectionFiles,\r
167 Ffs.Ffs.FdfFvFileTypeToFileType.get(self.FvFileType),\r
168 self.NameGuid,\r
169 Fixed=self.Fixed,\r
170 CheckSum=self.CheckSum,\r
171 Align=self.Alignment,\r
172 SectionAlign=SectionAlignments\r
173 )\r
174\r
175 return FfsFileOutput\r
176\r
177\r
178\r