Sync EDKII BaseTools to BaseTools project r1903.
[efi/edk2/.git] / edk2 / BaseTools / Source / Python / GenFds / FfsFileStatement.py
1 ## @file\r
2 # process FFS generation from FILE statement\r
3 #\r
4 #  Copyright (c) 2007 - 2010, Intel Corporation\r
5 #\r
6 #  All rights reserved. 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
18 import Ffs\r
19 import Rule\r
20 import os\r
21 import StringIO\r
22 import subprocess\r
23 \r
24 from GenFdsGlobalVariable import GenFdsGlobalVariable\r
25 from CommonDataClass.FdfClass import FileStatementClassObject\r
26 from Common import EdkLogger\r
27 from Common.BuildToolError import *\r
28 from Common.Misc import GuidStructureByteArrayToGuidString\r
29 from GuidSection import GuidSection\r
30 from FvImageSection import FvImageSection\r
31 \r
32 ## generate FFS from FILE\r
33 #\r
34 #\r
35 class FileStatement (FileStatementClassObject) :\r
36     ## The constructor\r
37     #\r
38     #   @param  self        The object pointer\r
39     #\r
40     def __init__(self):\r
41         FileStatementClassObject.__init__(self)\r
42 \r
43     ## GenFfs() method\r
44     #\r
45     #   Generate FFS\r
46     #\r
47     #   @param  self         The object pointer\r
48     #   @param  Dict         dictionary contains macro and value pair\r
49     #   @param  FvChildAddr  Array of the inside FvImage base address\r
50     #   @param  FvParentAddr Parent Fv base address\r
51     #   @retval string       Generated FFS file name\r
52     #\r
53     def GenFfs(self, Dict = {}, FvChildAddr=[], FvParentAddr=None):\r
54         \r
55         if self.NameGuid != None and self.NameGuid.startswith('PCD('):\r
56             PcdValue = GenFdsGlobalVariable.GetPcdValue(self.NameGuid)\r
57             if len(PcdValue) == 0:\r
58                 EdkLogger.error("GenFds", GENFDS_ERROR, '%s NOT defined.' \\r
59                             % (self.NameGuid))\r
60             if PcdValue.startswith('{'):\r
61                 PcdValue = GuidStructureByteArrayToGuidString(PcdValue)\r
62             RegistryGuidStr = PcdValue\r
63             if len(RegistryGuidStr) == 0:\r
64                 EdkLogger.error("GenFds", GENFDS_ERROR, 'GUID value for %s in wrong format.' \\r
65                             % (self.NameGuid))\r
66             self.NameGuid = RegistryGuidStr\r
67         \r
68         OutputDir = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid)\r
69         if not os.path.exists(OutputDir):\r
70              os.makedirs(OutputDir)\r
71 \r
72         Dict.update(self.DefineVarDict)\r
73         SectionAlignments = None\r
74         if self.FvName != None :\r
75             Buffer = StringIO.StringIO('')\r
76             if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
77                 EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (self.FvName))\r
78             Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
79             FileName = Fv.AddToBuffer(Buffer)\r
80             SectionFiles = [FileName]\r
81 \r
82         elif self.FdName != None:\r
83             if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
84                 EdkLogger.error("GenFds", GENFDS_ERROR, "FD (%s) is NOT described in FDF file!" % (self.FdName))\r
85             Fd = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
86             FileName = Fd.GenFd()\r
87             SectionFiles = [FileName]\r
88 \r
89         elif self.FileName != None:\r
90             self.FileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FileName)\r
91             SectionFiles = [GenFdsGlobalVariable.MacroExtend(self.FileName, Dict)]\r
92 \r
93         else:\r
94             SectionFiles = []\r
95             Index = 0\r
96             SectionAlignments = []\r
97             for section in self.SectionList :\r
98                 Index = Index + 1\r
99                 SecIndex = '%d' %Index\r
100                 # process the inside FvImage from FvSection or GuidSection\r
101                 if FvChildAddr != []:\r
102                     if isinstance(section, FvImageSection):\r
103                         section.FvAddr = FvChildAddr.pop(0)\r
104                     elif isinstance(section, GuidSection):\r
105                         section.FvAddr = FvChildAddr\r
106                 if FvParentAddr != None and isinstance(section, GuidSection):\r
107                     section.FvParentAddr = FvParentAddr\r
108 \r
109                 sectList, align = section.GenSection(OutputDir, self.NameGuid, SecIndex, self.KeyStringList, None, Dict)\r
110                 if sectList != []:\r
111                     for sect in sectList:\r
112                         SectionFiles.append(sect)\r
113                         SectionAlignments.append(align)\r
114 \r
115         #\r
116         # Prepare the parameter\r
117         #\r
118         FfsFileOutput = os.path.join(OutputDir, self.NameGuid + '.ffs')\r
119         GenFdsGlobalVariable.GenerateFfs(FfsFileOutput, SectionFiles,\r
120                                          Ffs.Ffs.FdfFvFileTypeToFileType.get(self.FvFileType),\r
121                                          self.NameGuid,\r
122                                          Fixed=self.Fixed,\r
123                                          CheckSum=self.CheckSum,\r
124                                          Align=self.Alignment,\r
125                                          SectionAlign=SectionAlignments\r
126                                         )\r
127 \r
128         return FfsFileOutput\r
129 \r
130 \r
131 \r