]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/FfsFileStatement.py
BaseTools: Replace BSD License with BSD+Patent License
[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
a743986d 51 def GenFfs(self, Dict = {}, 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
LG
72\r
73 Dict.update(self.DefineVarDict)\r
74 SectionAlignments = None\r
9e47e6f9 75 if self.FvName:\r
d943b0c3 76 Buffer = BytesIO()\r
9eb87141 77 if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
30fdf114
LG
78 EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (self.FvName))\r
79 Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
80 FileName = Fv.AddToBuffer(Buffer)\r
81 SectionFiles = [FileName]\r
82\r
9e47e6f9 83 elif self.FdName:\r
9eb87141 84 if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
30fdf114
LG
85 EdkLogger.error("GenFds", GENFDS_ERROR, "FD (%s) is NOT described in FDF file!" % (self.FdName))\r
86 Fd = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
fd171542 87 FileName = Fd.GenFd()\r
30fdf114
LG
88 SectionFiles = [FileName]\r
89\r
9e47e6f9 90 elif self.FileName:\r
860992ed 91 if hasattr(self, 'FvFileType') and self.FvFileType == 'RAW':\r
cfaaf99b 92 if isinstance(self.FileName, list) and isinstance(self.SubAlignment, list) and len(self.FileName) == len(self.SubAlignment):\r
d943b0c3 93 FileContent = BytesIO()\r
cfaaf99b
YZ
94 MaxAlignIndex = 0\r
95 MaxAlignValue = 1\r
860992ed
YZ
96 for Index, File in enumerate(self.FileName):\r
97 try:\r
f8db6527 98 f = open(File, 'rb')\r
860992ed
YZ
99 except:\r
100 GenFdsGlobalVariable.ErrorLogger("Error opening RAW file %s." % (File))\r
101 Content = f.read()\r
102 f.close()\r
cfaaf99b 103 AlignValue = 1\r
9e47e6f9 104 if self.SubAlignment[Index]:\r
cfaaf99b
YZ
105 AlignValue = GenFdsGlobalVariable.GetAlignment(self.SubAlignment[Index])\r
106 if AlignValue > MaxAlignValue:\r
107 MaxAlignIndex = Index\r
108 MaxAlignValue = AlignValue\r
d943b0c3
FB
109 FileContent.write(Content)\r
110 if len(FileContent.getvalue()) % AlignValue != 0:\r
111 Size = AlignValue - len(FileContent.getvalue()) % AlignValue\r
860992ed 112 for i in range(0, Size):\r
d943b0c3 113 FileContent.write(pack('B', 0xFF))\r
860992ed 114\r
d943b0c3 115 if FileContent.getvalue() != b'':\r
860992ed 116 OutputRAWFile = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid, self.NameGuid + '.raw')\r
d943b0c3 117 SaveFileOnChange(OutputRAWFile, FileContent.getvalue(), True)\r
860992ed 118 self.FileName = OutputRAWFile\r
cfaaf99b
YZ
119 self.SubAlignment = self.SubAlignment[MaxAlignIndex]\r
120\r
121 if self.Alignment and self.SubAlignment:\r
122 if GenFdsGlobalVariable.GetAlignment (self.Alignment) < GenFdsGlobalVariable.GetAlignment (self.SubAlignment):\r
123 self.Alignment = self.SubAlignment\r
124 elif self.SubAlignment:\r
125 self.Alignment = self.SubAlignment\r
126\r
30fdf114 127 self.FileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FileName)\r
97fa0ee9
YL
128 #Replace $(SAPCE) with real space\r
129 self.FileName = self.FileName.replace('$(SPACE)', ' ')\r
30fdf114
LG
130 SectionFiles = [GenFdsGlobalVariable.MacroExtend(self.FileName, Dict)]\r
131\r
132 else:\r
133 SectionFiles = []\r
134 Index = 0\r
135 SectionAlignments = []\r
9e47e6f9 136 for section in self.SectionList:\r
30fdf114
LG
137 Index = Index + 1\r
138 SecIndex = '%d' %Index\r
52302d4d
LG
139 # process the inside FvImage from FvSection or GuidSection\r
140 if FvChildAddr != []:\r
141 if isinstance(section, FvImageSection):\r
142 section.FvAddr = FvChildAddr.pop(0)\r
143 elif isinstance(section, GuidSection):\r
144 section.FvAddr = FvChildAddr\r
9e47e6f9 145 if FvParentAddr and isinstance(section, GuidSection):\r
52302d4d
LG
146 section.FvParentAddr = FvParentAddr\r
147\r
4afd3d04
LG
148 if self.KeepReloc == False:\r
149 section.KeepReloc = False\r
30fdf114
LG
150 sectList, align = section.GenSection(OutputDir, self.NameGuid, SecIndex, self.KeyStringList, None, Dict)\r
151 if sectList != []:\r
152 for sect in sectList:\r
153 SectionFiles.append(sect)\r
154 SectionAlignments.append(align)\r
155\r
156 #\r
157 # Prepare the parameter\r
158 #\r
159 FfsFileOutput = os.path.join(OutputDir, self.NameGuid + '.ffs')\r
160 GenFdsGlobalVariable.GenerateFfs(FfsFileOutput, SectionFiles,\r
9e47e6f9 161 FdfFvFileTypeToFileType.get(self.FvFileType),\r
30fdf114
LG
162 self.NameGuid,\r
163 Fixed=self.Fixed,\r
164 CheckSum=self.CheckSum,\r
165 Align=self.Alignment,\r
166 SectionAlign=SectionAlignments\r
167 )\r
168\r
169 return FfsFileOutput\r