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