]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/FfsFileStatement.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
a8c77eba 24import shutil\r
30fdf114
LG
25\r
26## generate FFS from FILE\r
27#\r
28#\r
9e47e6f9 29class FileStatement (FileStatementClassObject):\r
30fdf114
LG
30 ## The constructor\r
31 #\r
32 # @param self The object pointer\r
33 #\r
34 def __init__(self):\r
35 FileStatementClassObject.__init__(self)\r
79b74a03
LG
36 self.CurrentLineNum = None\r
37 self.CurrentLineContent = None\r
38 self.FileName = None\r
39 self.InfFileName = None\r
cfaaf99b 40 self.SubAlignment = None\r
30fdf114
LG
41\r
42 ## GenFfs() method\r
43 #\r
44 # Generate FFS\r
45 #\r
52302d4d
LG
46 # @param self The object pointer\r
47 # @param Dict dictionary contains macro and value pair\r
48 # @param FvChildAddr Array of the inside FvImage base address\r
49 # @param FvParentAddr Parent Fv base address\r
50 # @retval string Generated FFS file name\r
30fdf114 51 #\r
e32f7bc9 52 def GenFfs(self, Dict = None, FvChildAddr=[], FvParentAddr=None, IsMakefile=False, FvName=None):\r
f7496d71 53\r
9e47e6f9 54 if self.NameGuid and self.NameGuid.startswith('PCD('):\r
30fdf114
LG
55 PcdValue = GenFdsGlobalVariable.GetPcdValue(self.NameGuid)\r
56 if len(PcdValue) == 0:\r
57 EdkLogger.error("GenFds", GENFDS_ERROR, '%s NOT defined.' \\r
58 % (self.NameGuid))\r
59 if PcdValue.startswith('{'):\r
60 PcdValue = GuidStructureByteArrayToGuidString(PcdValue)\r
61 RegistryGuidStr = PcdValue\r
62 if len(RegistryGuidStr) == 0:\r
63 EdkLogger.error("GenFds", GENFDS_ERROR, 'GUID value for %s in wrong format.' \\r
64 % (self.NameGuid))\r
65 self.NameGuid = RegistryGuidStr\r
f7496d71 66\r
a743986d
YZ
67 Str = self.NameGuid\r
68 if FvName:\r
69 Str += FvName\r
70 OutputDir = os.path.join(GenFdsGlobalVariable.FfsDir, Str)\r
a8c77eba
BF
71 if os.path.exists(OutputDir):\r
72 shutil.rmtree(OutputDir)\r
30fdf114 73 if not os.path.exists(OutputDir):\r
0d2711a6 74 os.makedirs(OutputDir)\r
30fdf114 75\r
e32f7bc9
FZ
76 if Dict is None:\r
77 Dict = {}\r
78\r
30fdf114
LG
79 Dict.update(self.DefineVarDict)\r
80 SectionAlignments = None\r
9e47e6f9 81 if self.FvName:\r
d943b0c3 82 Buffer = BytesIO()\r
9eb87141 83 if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
30fdf114
LG
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
9e47e6f9 89 elif self.FdName:\r
9eb87141 90 if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
30fdf114
LG
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
fd171542 93 FileName = Fd.GenFd()\r
30fdf114
LG
94 SectionFiles = [FileName]\r
95\r
9e47e6f9 96 elif self.FileName:\r
860992ed 97 if hasattr(self, 'FvFileType') and self.FvFileType == 'RAW':\r
cfaaf99b 98 if isinstance(self.FileName, list) and isinstance(self.SubAlignment, list) and len(self.FileName) == len(self.SubAlignment):\r
d943b0c3 99 FileContent = BytesIO()\r
cfaaf99b
YZ
100 MaxAlignIndex = 0\r
101 MaxAlignValue = 1\r
860992ed
YZ
102 for Index, File in enumerate(self.FileName):\r
103 try:\r
f8db6527 104 f = open(File, 'rb')\r
860992ed
YZ
105 except:\r
106 GenFdsGlobalVariable.ErrorLogger("Error opening RAW file %s." % (File))\r
107 Content = f.read()\r
108 f.close()\r
cfaaf99b 109 AlignValue = 1\r
9e47e6f9 110 if self.SubAlignment[Index]:\r
cfaaf99b
YZ
111 AlignValue = GenFdsGlobalVariable.GetAlignment(self.SubAlignment[Index])\r
112 if AlignValue > MaxAlignValue:\r
113 MaxAlignIndex = Index\r
114 MaxAlignValue = AlignValue\r
d943b0c3
FB
115 FileContent.write(Content)\r
116 if len(FileContent.getvalue()) % AlignValue != 0:\r
117 Size = AlignValue - len(FileContent.getvalue()) % AlignValue\r
860992ed 118 for i in range(0, Size):\r
d943b0c3 119 FileContent.write(pack('B', 0xFF))\r
860992ed 120\r
d943b0c3 121 if FileContent.getvalue() != b'':\r
860992ed 122 OutputRAWFile = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid, self.NameGuid + '.raw')\r
d943b0c3 123 SaveFileOnChange(OutputRAWFile, FileContent.getvalue(), True)\r
860992ed 124 self.FileName = OutputRAWFile\r
cfaaf99b
YZ
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
30fdf114 133 self.FileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FileName)\r
97fa0ee9
YL
134 #Replace $(SAPCE) with real space\r
135 self.FileName = self.FileName.replace('$(SPACE)', ' ')\r
30fdf114
LG
136 SectionFiles = [GenFdsGlobalVariable.MacroExtend(self.FileName, Dict)]\r
137\r
138 else:\r
139 SectionFiles = []\r
140 Index = 0\r
141 SectionAlignments = []\r
9e47e6f9 142 for section in self.SectionList:\r
30fdf114
LG
143 Index = Index + 1\r
144 SecIndex = '%d' %Index\r
52302d4d
LG
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
9e47e6f9 151 if FvParentAddr and isinstance(section, GuidSection):\r
52302d4d
LG
152 section.FvParentAddr = FvParentAddr\r
153\r
4afd3d04
LG
154 if self.KeepReloc == False:\r
155 section.KeepReloc = False\r
30fdf114
LG
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
9e47e6f9 167 FdfFvFileTypeToFileType.get(self.FvFileType),\r
30fdf114
LG
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