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