blob: b3d2edeb383d2015b429b4d7a5d55e782880bb0c [file] [log] [blame]
Doug Zongker3c84f562014-07-31 11:06:30 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Given a target-files zipfile that does not contain images (ie, does
19not have an IMAGES/ top-level subdirectory), produce the images and
20add them to the zipfile.
21
22Usage: add_img_to_target_files target_files
23"""
24
25import sys
26
27if sys.hexversion < 0x02070000:
28 print >> sys.stderr, "Python 2.7 or newer is required."
29 sys.exit(1)
30
31import errno
32import os
Doug Zongker3c84f562014-07-31 11:06:30 -070033import tempfile
34import zipfile
35
36# missing in Python 2.4 and before
37if not hasattr(os, "SEEK_SET"):
38 os.SEEK_SET = 0
39
40import build_image
41import common
42
43OPTIONS = common.OPTIONS
44
Michael Runge2e0d8fc2014-11-13 21:41:08 -080045OPTIONS.add_missing = False
46OPTIONS.rebuild_recovery = False
Doug Zongker3c84f562014-07-31 11:06:30 -070047
Michael Runge2e0d8fc2014-11-13 21:41:08 -080048def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070049 """Turn the contents of SYSTEM into a system image and store it in
50 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080051
52 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
53 if os.path.exists(prebuilt_path):
54 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
55 return
56
57 def output_sink(fn, data):
58 ofile = open(os.path.join(OPTIONS.input_tmp,"SYSTEM",fn), "w")
59 ofile.write(data)
60 ofile.close()
61
62 if OPTIONS.rebuild_recovery:
63 print("Building new recovery patch")
64 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, boot_img,
65 info_dict=OPTIONS.info_dict)
66
Doug Zongkerfc44a512014-08-26 13:10:25 -070067 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
68 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
69 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -080070 common.ZipWrite(output_zip, imgname, prefix + "system.img")
71 common.ZipWrite(output_zip, block_list, prefix + "system.map")
Doug Zongkerfc44a512014-08-26 13:10:25 -070072
73
74def BuildSystem(input_dir, info_dict, block_list=None):
75 """Build the (sparse) system image and return the name of a temp
76 file containing it."""
77 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
78
79
80def AddVendor(output_zip, prefix="IMAGES/"):
81 """Turn the contents of VENDOR into a vendor image and store in it
82 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080083
84 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
85 if os.path.exists(prebuilt_path):
86 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
87 return
88
Doug Zongkerfc44a512014-08-26 13:10:25 -070089 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
90 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Doug Zongker5f9c28c2014-08-22 14:48:50 -070091 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -080092 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
93 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Doug Zongker3c84f562014-07-31 11:06:30 -070094
95
Doug Zongkerfc44a512014-08-26 13:10:25 -070096def BuildVendor(input_dir, info_dict, block_list=None):
97 """Build the (sparse) vendor image and return the name of a temp
98 file containing it."""
99 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
100
101
102def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700103 print "creating " + what + ".img..."
104
Doug Zongkerfc44a512014-08-26 13:10:25 -0700105 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700106
107 # The name of the directory it is making an image out of matters to
108 # mkyaffs2image. It wants "system" but we have a directory named
109 # "SYSTEM", so create a symlink.
110 try:
111 os.symlink(os.path.join(input_dir, what.upper()),
112 os.path.join(input_dir, what))
113 except OSError, e:
114 # bogus error on my mac version?
115 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
116 # os.path.join(OPTIONS.input_tmp, "system"))
117 # OSError: [Errno 17] File exists
118 if (e.errno == errno.EEXIST):
119 pass
120
121 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
122 fstab = info_dict["fstab"]
123 if fstab:
124 image_props["fs_type" ] = fstab["/" + what].fs_type
125
126 if what == "system":
127 fs_config_prefix = ""
128 else:
129 fs_config_prefix = what + "_"
130
131 fs_config = os.path.join(
132 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
133 if not os.path.exists(fs_config): fs_config = None
134
135 fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
136 if not os.path.exists(fc_config): fc_config = None
137
Ying Wanga2292c92015-03-24 19:07:40 -0700138 # Override values loaded from info_dict.
139 if fs_config:
140 image_props["fs_config"] = fs_config
141 if fc_config:
142 image_props["selinux_fc"] = fc_config
143 if block_list:
144 image_props["block_list"] = block_list
145 if image_props.get("system_root_image") == "true":
146 image_props["ramdisk_dir"] = os.path.join(input_dir, "BOOT/RAMDISK")
147 image_props["ramdisk_fs_config"] = os.path.join(
148 input_dir, "META/boot_filesystem_config.txt")
149
Doug Zongker3c84f562014-07-31 11:06:30 -0700150 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700151 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700152 assert succ, "build " + what + ".img image failed"
153
Doug Zongkerfc44a512014-08-26 13:10:25 -0700154 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700155
156
157def AddUserdata(output_zip, prefix="IMAGES/"):
158 """Create an empty userdata image and store it in output_zip."""
159
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800160 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
161 if os.path.exists(prebuilt_path):
162 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
163 return
164
Doug Zongker3c84f562014-07-31 11:06:30 -0700165 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
166 "data")
167 # We only allow yaffs to have a 0/missing partition_size.
168 # Extfs, f2fs must have a size. Skip userdata.img if no size.
169 if (not image_props.get("fs_type", "").startswith("yaffs") and
170 not image_props.get("partition_size")):
171 return
172
173 print "creating userdata.img..."
174
175 # The name of the directory it is making an image out of matters to
176 # mkyaffs2image. So we create a temp dir, and within it we create an
177 # empty dir named "data", and build the image from that.
178 temp_dir = tempfile.mkdtemp()
179 user_dir = os.path.join(temp_dir, "data")
180 os.mkdir(user_dir)
181 img = tempfile.NamedTemporaryFile()
182
183 fstab = OPTIONS.info_dict["fstab"]
184 if fstab:
185 image_props["fs_type" ] = fstab["/data"].fs_type
186 succ = build_image.BuildImage(user_dir, image_props, img.name)
187 assert succ, "build userdata.img image failed"
188
189 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
190 output_zip.write(img.name, prefix + "userdata.img")
191 img.close()
192 os.rmdir(user_dir)
193 os.rmdir(temp_dir)
194
195
196def AddCache(output_zip, prefix="IMAGES/"):
197 """Create an empty cache image and store it in output_zip."""
198
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800199 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
200 if os.path.exists(prebuilt_path):
201 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
202 return
203
Doug Zongker3c84f562014-07-31 11:06:30 -0700204 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
205 "cache")
206 # The build system has to explicitly request for cache.img.
207 if "fs_type" not in image_props:
208 return
209
210 print "creating cache.img..."
211
212 # The name of the directory it is making an image out of matters to
213 # mkyaffs2image. So we create a temp dir, and within it we create an
214 # empty dir named "cache", and build the image from that.
215 temp_dir = tempfile.mkdtemp()
216 user_dir = os.path.join(temp_dir, "cache")
217 os.mkdir(user_dir)
218 img = tempfile.NamedTemporaryFile()
219
220 fstab = OPTIONS.info_dict["fstab"]
221 if fstab:
222 image_props["fs_type" ] = fstab["/cache"].fs_type
223 succ = build_image.BuildImage(user_dir, image_props, img.name)
224 assert succ, "build cache.img image failed"
225
226 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
227 output_zip.write(img.name, prefix + "cache.img")
228 img.close()
229 os.rmdir(user_dir)
230 os.rmdir(temp_dir)
231
232
233def AddImagesToTargetFiles(filename):
234 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700235
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800236 if not OPTIONS.add_missing:
237 for n in input_zip.namelist():
238 if n.startswith("IMAGES/"):
239 print "target_files appears to already contain images."
240 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700241
Doug Zongker3c84f562014-07-31 11:06:30 -0700242 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700243 input_zip.getinfo("VENDOR/")
244 has_vendor = True
245 except KeyError:
246 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700247
Doug Zongkerfc44a512014-08-26 13:10:25 -0700248 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
249 if "selinux_fc" in OPTIONS.info_dict:
250 OPTIONS.info_dict["selinux_fc"] = os.path.join(
251 OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts")
Doug Zongker3c84f562014-07-31 11:06:30 -0700252
Doug Zongkerfc44a512014-08-26 13:10:25 -0700253 input_zip.close()
254 output_zip = zipfile.ZipFile(filename, "a",
255 compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -0700256
Doug Zongkerfc44a512014-08-26 13:10:25 -0700257 def banner(s):
258 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700259
Doug Zongkerfc44a512014-08-26 13:10:25 -0700260 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800261 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
262 boot_image = None
263 if os.path.exists(prebuilt_path):
264 print "boot.img already exists in IMAGES/, no need to rebuild..."
265 if OPTIONS.rebuild_recovery:
266 boot_image = common.GetBootableImage(
267 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
268 else:
269 boot_image = common.GetBootableImage(
270 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
271 if boot_image:
272 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700273
Doug Zongkerfc44a512014-08-26 13:10:25 -0700274 banner("recovery")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800275 recovery_image = None
276 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
277 if os.path.exists(prebuilt_path):
278 print "recovery.img already exists in IMAGES/, no need to rebuild..."
279 if OPTIONS.rebuild_recovery:
280 recovery_image = common.GetBootableImage(
281 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
282 else:
283 recovery_image = common.GetBootableImage(
284 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
285 if recovery_image:
286 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700287
Doug Zongkerfc44a512014-08-26 13:10:25 -0700288 banner("system")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800289 AddSystem(output_zip, recovery_img=recovery_image, boot_img=boot_image)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700290 if has_vendor:
291 banner("vendor")
292 AddVendor(output_zip)
293 banner("userdata")
294 AddUserdata(output_zip)
295 banner("cache")
296 AddCache(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700297
Doug Zongkerfc44a512014-08-26 13:10:25 -0700298 output_zip.close()
Doug Zongker3c84f562014-07-31 11:06:30 -0700299
Doug Zongker3c84f562014-07-31 11:06:30 -0700300def main(argv):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800301 def option_handler(o, a):
302 if o in ("-a", "--add_missing"):
303 OPTIONS.add_missing = True
304 elif o in ("-r", "--rebuild_recovery",):
305 OPTIONS.rebuild_recovery = True
306 else:
307 return False
308 return True
309
310 args = common.ParseOptions(argv, __doc__,
311 extra_opts="ar",
312 extra_long_opts=["add_missing",
313 "rebuild_recovery",
314 ],
315 extra_option_handler=option_handler)
316
Doug Zongker3c84f562014-07-31 11:06:30 -0700317
318 if len(args) != 1:
319 common.Usage(__doc__)
320 sys.exit(1)
321
322 AddImagesToTargetFiles(args[0])
323 print "done."
324
325if __name__ == '__main__':
326 try:
327 common.CloseInheritedPipes()
328 main(sys.argv[1:])
329 except common.ExternalError, e:
330 print
331 print " ERROR: %s" % (e,)
332 print
333 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700334 finally:
335 common.Cleanup()