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