blob: 6bdb9d1c66c699b17c0bb9954c0893aed882321f [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
Tao Bao822f5842015-09-30 16:01:14 -070031import datetime
Doug Zongker3c84f562014-07-31 11:06:30 -070032import errno
33import os
Ying Wang2a048392015-06-25 13:56:53 -070034import shutil
Doug Zongker3c84f562014-07-31 11:06:30 -070035import tempfile
36import zipfile
37
Doug Zongker3c84f562014-07-31 11:06:30 -070038import build_image
39import common
40
41OPTIONS = common.OPTIONS
42
Michael Runge2e0d8fc2014-11-13 21:41:08 -080043OPTIONS.add_missing = False
44OPTIONS.rebuild_recovery = False
Doug Zongker3c84f562014-07-31 11:06:30 -070045
Michael Runge2e0d8fc2014-11-13 21:41:08 -080046def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070047 """Turn the contents of SYSTEM into a system image and store it in
48 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080049
50 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
51 if os.path.exists(prebuilt_path):
52 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
53 return
54
55 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -070056 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
57 ofile.write(data)
58 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -080059
60 if OPTIONS.rebuild_recovery:
Dan Albert8b72aef2015-03-23 19:13:21 -070061 print "Building new recovery patch"
62 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
63 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -080064
Doug Zongkerfc44a512014-08-26 13:10:25 -070065 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
66 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
67 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -080068 common.ZipWrite(output_zip, imgname, prefix + "system.img")
69 common.ZipWrite(output_zip, block_list, prefix + "system.map")
Doug Zongkerfc44a512014-08-26 13:10:25 -070070
71
72def BuildSystem(input_dir, info_dict, block_list=None):
73 """Build the (sparse) system image and return the name of a temp
74 file containing it."""
75 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
76
77
78def AddVendor(output_zip, prefix="IMAGES/"):
79 """Turn the contents of VENDOR into a vendor image and store in it
80 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080081
82 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
83 if os.path.exists(prebuilt_path):
84 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
85 return
86
Doug Zongkerfc44a512014-08-26 13:10:25 -070087 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
88 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Dan Albert8b72aef2015-03-23 19:13:21 -070089 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -080090 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
91 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Doug Zongker3c84f562014-07-31 11:06:30 -070092
93
Doug Zongkerfc44a512014-08-26 13:10:25 -070094def BuildVendor(input_dir, info_dict, block_list=None):
95 """Build the (sparse) vendor image and return the name of a temp
96 file containing it."""
97 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
98
99
100def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700101 print "creating " + what + ".img..."
102
Doug Zongkerfc44a512014-08-26 13:10:25 -0700103 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700104
105 # The name of the directory it is making an image out of matters to
106 # mkyaffs2image. It wants "system" but we have a directory named
107 # "SYSTEM", so create a symlink.
108 try:
109 os.symlink(os.path.join(input_dir, what.upper()),
110 os.path.join(input_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700111 except OSError as e:
112 # bogus error on my mac version?
113 # File "./build/tools/releasetools/img_from_target_files"
114 # os.path.join(OPTIONS.input_tmp, "system"))
115 # OSError: [Errno 17] File exists
116 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700117 pass
118
119 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
120 fstab = info_dict["fstab"]
121 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700122 image_props["fs_type"] = fstab["/" + what].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700123
Tao Bao822f5842015-09-30 16:01:14 -0700124 # Use a fixed timestamp (01/01/2009) when packaging the image.
125 # Bug: 24377993
126 epoch = datetime.datetime.fromtimestamp(0)
127 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
128 image_props["timestamp"] = int(timestamp)
129
Doug Zongker3c84f562014-07-31 11:06:30 -0700130 if what == "system":
131 fs_config_prefix = ""
132 else:
133 fs_config_prefix = what + "_"
134
135 fs_config = os.path.join(
136 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700137 if not os.path.exists(fs_config):
138 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700139
Ying Wanga2292c92015-03-24 19:07:40 -0700140 # Override values loaded from info_dict.
141 if fs_config:
142 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700143 if block_list:
144 image_props["block_list"] = block_list
Ying Wanga2292c92015-03-24 19:07:40 -0700145
Doug Zongker3c84f562014-07-31 11:06:30 -0700146 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700147 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700148 assert succ, "build " + what + ".img image failed"
149
Doug Zongkerfc44a512014-08-26 13:10:25 -0700150 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700151
152
153def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700154 """Create a userdata image and store it in output_zip.
155
156 In most case we just create and store an empty userdata.img;
157 But the invoker can also request to create userdata.img with real
158 data from the target files, by setting "userdata_img_with_data=true"
159 in OPTIONS.info_dict.
160 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700161
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800162 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
163 if os.path.exists(prebuilt_path):
164 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
165 return
166
Tao Bao2c15d9e2015-07-09 11:51:16 -0700167 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Doug Zongker3c84f562014-07-31 11:06:30 -0700168 # We only allow yaffs to have a 0/missing partition_size.
169 # Extfs, f2fs must have a size. Skip userdata.img if no size.
170 if (not image_props.get("fs_type", "").startswith("yaffs") and
171 not image_props.get("partition_size")):
172 return
173
174 print "creating userdata.img..."
175
Tao Bao822f5842015-09-30 16:01:14 -0700176 # Use a fixed timestamp (01/01/2009) when packaging the image.
177 # Bug: 24377993
178 epoch = datetime.datetime.fromtimestamp(0)
179 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
180 image_props["timestamp"] = int(timestamp)
181
Doug Zongker3c84f562014-07-31 11:06:30 -0700182 # The name of the directory it is making an image out of matters to
183 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700184 # empty dir named "data", or a symlink to the DATA dir,
185 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700186 temp_dir = tempfile.mkdtemp()
187 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700188 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
189 if empty:
190 # Create an empty dir.
191 os.mkdir(user_dir)
192 else:
193 # Symlink to the DATA dir.
194 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
195 user_dir)
196
Doug Zongker3c84f562014-07-31 11:06:30 -0700197 img = tempfile.NamedTemporaryFile()
198
199 fstab = OPTIONS.info_dict["fstab"]
200 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700201 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700202 succ = build_image.BuildImage(user_dir, image_props, img.name)
203 assert succ, "build userdata.img image failed"
204
205 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700206 common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700207 img.close()
Ying Wang2a048392015-06-25 13:56:53 -0700208 shutil.rmtree(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700209
210
211def AddCache(output_zip, prefix="IMAGES/"):
212 """Create an empty cache image and store it in output_zip."""
213
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800214 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
215 if os.path.exists(prebuilt_path):
216 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
217 return
218
Tao Bao2c15d9e2015-07-09 11:51:16 -0700219 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700220 # The build system has to explicitly request for cache.img.
221 if "fs_type" not in image_props:
222 return
223
224 print "creating cache.img..."
225
Tao Bao822f5842015-09-30 16:01:14 -0700226 # Use a fixed timestamp (01/01/2009) when packaging the image.
227 # Bug: 24377993
228 epoch = datetime.datetime.fromtimestamp(0)
229 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
230 image_props["timestamp"] = int(timestamp)
231
Doug Zongker3c84f562014-07-31 11:06:30 -0700232 # The name of the directory it is making an image out of matters to
233 # mkyaffs2image. So we create a temp dir, and within it we create an
234 # empty dir named "cache", and build the image from that.
235 temp_dir = tempfile.mkdtemp()
236 user_dir = os.path.join(temp_dir, "cache")
237 os.mkdir(user_dir)
238 img = tempfile.NamedTemporaryFile()
239
240 fstab = OPTIONS.info_dict["fstab"]
241 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700242 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700243 succ = build_image.BuildImage(user_dir, image_props, img.name)
244 assert succ, "build cache.img image failed"
245
246 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700247 common.ZipWrite(output_zip, img.name, prefix + "cache.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700248 img.close()
249 os.rmdir(user_dir)
250 os.rmdir(temp_dir)
251
252
253def AddImagesToTargetFiles(filename):
254 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700255
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800256 if not OPTIONS.add_missing:
257 for n in input_zip.namelist():
258 if n.startswith("IMAGES/"):
259 print "target_files appears to already contain images."
260 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700261
Doug Zongker3c84f562014-07-31 11:06:30 -0700262 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700263 input_zip.getinfo("VENDOR/")
264 has_vendor = True
265 except KeyError:
266 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700267
Tao Bao2c15d9e2015-07-09 11:51:16 -0700268 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700269
Tao Bao2ed665a2015-04-01 11:21:55 -0700270 common.ZipClose(input_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700271 output_zip = zipfile.ZipFile(filename, "a",
272 compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -0700273
Doug Zongkerfc44a512014-08-26 13:10:25 -0700274 def banner(s):
275 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700276
Doug Zongkerfc44a512014-08-26 13:10:25 -0700277 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800278 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
279 boot_image = None
280 if os.path.exists(prebuilt_path):
281 print "boot.img already exists in IMAGES/, no need to rebuild..."
282 if OPTIONS.rebuild_recovery:
283 boot_image = common.GetBootableImage(
284 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
285 else:
286 boot_image = common.GetBootableImage(
287 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
288 if boot_image:
289 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700290
Doug Zongkerfc44a512014-08-26 13:10:25 -0700291 banner("recovery")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800292 recovery_image = None
293 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
294 if os.path.exists(prebuilt_path):
295 print "recovery.img already exists in IMAGES/, no need to rebuild..."
296 if OPTIONS.rebuild_recovery:
297 recovery_image = common.GetBootableImage(
298 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
299 else:
300 recovery_image = common.GetBootableImage(
301 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
302 if recovery_image:
303 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700304
Doug Zongkerfc44a512014-08-26 13:10:25 -0700305 banner("system")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800306 AddSystem(output_zip, recovery_img=recovery_image, boot_img=boot_image)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700307 if has_vendor:
308 banner("vendor")
309 AddVendor(output_zip)
310 banner("userdata")
311 AddUserdata(output_zip)
312 banner("cache")
313 AddCache(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700314
Tao Bao2ed665a2015-04-01 11:21:55 -0700315 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700316
Doug Zongker3c84f562014-07-31 11:06:30 -0700317def main(argv):
Dan Albert8b72aef2015-03-23 19:13:21 -0700318 def option_handler(o, _):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800319 if o in ("-a", "--add_missing"):
320 OPTIONS.add_missing = True
321 elif o in ("-r", "--rebuild_recovery",):
322 OPTIONS.rebuild_recovery = True
323 else:
324 return False
325 return True
326
Dan Albert8b72aef2015-03-23 19:13:21 -0700327 args = common.ParseOptions(
328 argv, __doc__, extra_opts="ar",
329 extra_long_opts=["add_missing", "rebuild_recovery"],
330 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800331
Doug Zongker3c84f562014-07-31 11:06:30 -0700332
333 if len(args) != 1:
334 common.Usage(__doc__)
335 sys.exit(1)
336
337 AddImagesToTargetFiles(args[0])
338 print "done."
339
340if __name__ == '__main__':
341 try:
342 common.CloseInheritedPipes()
343 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700344 except common.ExternalError as e:
Doug Zongker3c84f562014-07-31 11:06:30 -0700345 print
346 print " ERROR: %s" % (e,)
347 print
348 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700349 finally:
350 common.Cleanup()