BaseTools: Supported FMP capsule image.
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / CapsuleData.py
1 ## @file
2 # generate capsule
3 #
4 # Copyright (c) 2007-2013, 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 import Ffs
19 from GenFdsGlobalVariable import GenFdsGlobalVariable
20 import StringIO
21 from struct import pack
22 import os
23 from Common.Misc import SaveFileOnChange
24
25 ## base class for capsule data
26 #
27 #
28 class CapsuleData:
29 ## The constructor
30 #
31 # @param self The object pointer
32 def __init__(self):
33 pass
34
35 ## generate capsule data
36 #
37 # @param self The object pointer
38 def GenCapsuleSubItem(self):
39 pass
40
41 ## FFS class for capsule data
42 #
43 #
44 class CapsuleFfs (CapsuleData):
45 ## The constructor
46 #
47 # @param self The object pointer
48 #
49 def __init__(self) :
50 self.Ffs = None
51 self.FvName = None
52
53 ## generate FFS capsule data
54 #
55 # @param self The object pointer
56 # @retval string Generated file name
57 #
58 def GenCapsuleSubItem(self):
59 FfsFile = self.Ffs.GenFfs()
60 return FfsFile
61
62 ## FV class for capsule data
63 #
64 #
65 class CapsuleFv (CapsuleData):
66 ## The constructor
67 #
68 # @param self The object pointer
69 #
70 def __init__(self) :
71 self.Ffs = None
72 self.FvName = None
73 self.CapsuleName = None
74
75 ## generate FV capsule data
76 #
77 # @param self The object pointer
78 # @retval string Generated file name
79 #
80 def GenCapsuleSubItem(self):
81 if self.FvName.find('.fv') == -1:
82 if self.FvName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
83 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())
84 FdBuffer = StringIO.StringIO('')
85 FvObj.CapsuleName = self.CapsuleName
86 FvFile = FvObj.AddToBuffer(FdBuffer)
87 FvObj.CapsuleName = None
88 FdBuffer.close()
89 return FvFile
90 else:
91 FvFile = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvName)
92 return FvFile
93
94 ## FD class for capsule data
95 #
96 #
97 class CapsuleFd (CapsuleData):
98 ## The constructor
99 #
100 # @param self The object pointer
101 #
102 def __init__(self) :
103 self.Ffs = None
104 self.FdName = None
105 self.CapsuleName = None
106
107 ## generate FD capsule data
108 #
109 # @param self The object pointer
110 # @retval string Generated file name
111 #
112 def GenCapsuleSubItem(self):
113 if self.FdName.find('.fd') == -1:
114 if self.FdName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
115 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())
116 FdFile = FdObj.GenFd()
117 return FdFile
118 else:
119 FdFile = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FdName)
120 return FdFile
121
122 ## AnyFile class for capsule data
123 #
124 #
125 class CapsuleAnyFile (CapsuleData):
126 ## The constructor
127 #
128 # @param self The object pointer
129 #
130 def __init__(self) :
131 self.Ffs = None
132 self.FileName = None
133
134 ## generate AnyFile capsule data
135 #
136 # @param self The object pointer
137 # @retval string Generated file name
138 #
139 def GenCapsuleSubItem(self):
140 return self.FileName
141
142 ## Afile class for capsule data
143 #
144 #
145 class CapsuleAfile (CapsuleData):
146 ## The constructor
147 #
148 # @param self The object pointer
149 #
150 def __init__(self) :
151 self.Ffs = None
152 self.FileName = None
153
154 ## generate Afile capsule data
155 #
156 # @param self The object pointer
157 # @retval string Generated file name
158 #
159 def GenCapsuleSubItem(self):
160 return self.FileName
161
162 class CapsulePayload(CapsuleData):
163 '''Generate payload file, the header is defined below:
164 #pragma pack(1)
165 typedef struct {
166 UINT32 Version;
167 EFI_GUID UpdateImageTypeId;
168 UINT8 UpdateImageIndex;
169 UINT8 reserved_bytes[3];
170 UINT32 UpdateImageSize;
171 UINT32 UpdateVendorCodeSize;
172 UINT64 UpdateHardwareInstance; //Introduced in v2
173 } EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER;
174 '''
175 def __init__(self):
176 self.UiName = None
177 self.Version = None
178 self.ImageTypeId = None
179 self.ImageIndex = None
180 self.HardwareInstance = None
181 self.ImageFile = None
182 self.VendorCodeFile = None
183
184 def GenCapsuleSubItem(self):
185 if not self.Version:
186 self.Version = 0x00000002
187 ImageFileSize = os.path.getsize(self.ImageFile)
188 VendorFileSize = 0
189 if self.VendorCodeFile:
190 VendorFileSize = os.path.getsize(self.VendorCodeFile)
191
192 #
193 # Fill structure
194 #
195 Guid = self.ImageTypeId.split('-')
196 Buffer = pack('=ILHHBBBBBBBBBBBBIIQ',
197 int(self.Version,16),
198 int(Guid[0], 16),
199 int(Guid[1], 16),
200 int(Guid[2], 16),
201 int(Guid[3][-4:-2], 16),
202 int(Guid[3][-2:], 16),
203 int(Guid[4][-12:-10], 16),
204 int(Guid[4][-10:-8], 16),
205 int(Guid[4][-8:-6], 16),
206 int(Guid[4][-6:-4], 16),
207 int(Guid[4][-4:-2], 16),
208 int(Guid[4][-2:], 16),
209 int(self.ImageIndex, 16),
210 0,
211 0,
212 0,
213 ImageFileSize,
214 VendorFileSize,
215 int(self.HardwareInstance, 16)
216 )
217 #
218 # Append file content to the structure
219 #
220 ImageFile = open(self.ImageFile, 'rb')
221 Buffer += ImageFile.read()
222 ImageFile.close()
223 if self.VendorCodeFile:
224 VendorFile = open(self.VendorCodeFile, 'rb')
225 Buffer += VendorFile.read()
226 VendorFile.close()
227 return Buffer