85f95a69c1f050036dd2daf3bca052187cd56613
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Capsule.py
1 ## @file
2 # generate capsule
3 #
4 # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #
14
15 ##
16 # Import Modules
17 #
18 from GenFdsGlobalVariable import GenFdsGlobalVariable
19 from CommonDataClass.FdfClass import CapsuleClassObject
20 import Common.LongFilePathOs as os
21 import subprocess
22 import StringIO
23 from Common.Misc import SaveFileOnChange
24 from GenFds import GenFds
25
26
27 T_CHAR_LF = '\n'
28
29 ## create inf file describes what goes into capsule and call GenFv to generate capsule
30 #
31 #
32 class Capsule (CapsuleClassObject) :
33 ## The constructor
34 #
35 # @param self The object pointer
36 #
37 def __init__(self):
38 CapsuleClassObject.__init__(self)
39 # For GenFv
40 self.BlockSize = None
41 # For GenFv
42 self.BlockNum = None
43 self.CapsuleName = None
44
45 ## Generate capsule
46 #
47 # @param self The object pointer
48 # @retval string Generated Capsule file path
49 #
50 def GenCapsule(self):
51 if self.UiCapsuleName.upper() + 'cap' in GenFds.ImageBinDict.keys():
52 return GenFds.ImageBinDict[self.UiCapsuleName.upper() + 'cap']
53
54 GenFdsGlobalVariable.InfLogger( "\nGenerate %s Capsule" %self.UiCapsuleName)
55 CapInfFile = self.GenCapInf()
56 CapInfFile.writelines("[files]" + T_CHAR_LF)
57 CapFileList = []
58 for CapsuleDataObj in self.CapsuleDataList :
59 CapsuleDataObj.CapsuleName = self.CapsuleName
60 FileName = CapsuleDataObj.GenCapsuleSubItem()
61 CapsuleDataObj.CapsuleName = None
62 CapFileList.append(FileName)
63 CapInfFile.writelines("EFI_FILE_NAME = " + \
64 FileName + \
65 T_CHAR_LF)
66 SaveFileOnChange(self.CapInfFileName, CapInfFile.getvalue(), False)
67 CapInfFile.close()
68 #
69 # Call GenFv tool to generate capsule
70 #
71 CapOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName)
72 CapOutputFile = CapOutputFile + '.Cap'
73 GenFdsGlobalVariable.GenerateFirmwareVolume(
74 CapOutputFile,
75 [self.CapInfFileName],
76 Capsule=True,
77 FfsList=CapFileList
78 )
79
80 GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s Capsule Successfully" %self.UiCapsuleName)
81 GenFdsGlobalVariable.SharpCounter = 0
82 GenFds.ImageBinDict[self.UiCapsuleName.upper() + 'cap'] = CapOutputFile
83 return CapOutputFile
84
85 ## Generate inf file for capsule
86 #
87 # @param self The object pointer
88 # @retval file inf file object
89 #
90 def GenCapInf(self):
91 self.CapInfFileName = os.path.join(GenFdsGlobalVariable.FvDir,
92 self.UiCapsuleName + "_Cap" + '.inf')
93 CapInfFile = StringIO.StringIO() #open (self.CapInfFileName , 'w+')
94
95 CapInfFile.writelines("[options]" + T_CHAR_LF)
96
97 for Item in self.TokensDict.keys():
98 CapInfFile.writelines("EFI_" + \
99 Item + \
100 ' = ' + \
101 self.TokensDict.get(Item) + \
102 T_CHAR_LF)
103
104 return CapInfFile