blob: f31d416c3964deabf27a9fb176c879a1efe11c76 [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
Ying Wang2a048392015-06-25 13:56:53 -070033import shutil
Doug Zongker3c84f562014-07-31 11:06:30 -070034import tempfile
35import zipfile
36
Doug Zongker3c84f562014-07-31 11:06:30 -070037import build_image
38import common
39
40OPTIONS = common.OPTIONS
41
Michael Runge2e0d8fc2014-11-13 21:41:08 -080042OPTIONS.add_missing = False
43OPTIONS.rebuild_recovery = False
Doug Zongker3c84f562014-07-31 11:06:30 -070044
Michael Runge2e0d8fc2014-11-13 21:41:08 -080045def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070046 """Turn the contents of SYSTEM into a system image and store it in
47 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080048
49 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
50 if os.path.exists(prebuilt_path):
51 print "system.img already exists in %s, no need to rebuild..." % (prefix,)
52 return
53
54 def output_sink(fn, data):
Dan Albert8b72aef2015-03-23 19:13:21 -070055 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
56 ofile.write(data)
57 ofile.close()
Michael Runge2e0d8fc2014-11-13 21:41:08 -080058
59 if OPTIONS.rebuild_recovery:
Dan Albert8b72aef2015-03-23 19:13:21 -070060 print "Building new recovery patch"
61 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
62 boot_img, info_dict=OPTIONS.info_dict)
Michael Runge2e0d8fc2014-11-13 21:41:08 -080063
Doug Zongkerfc44a512014-08-26 13:10:25 -070064 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
65 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
66 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -080067 common.ZipWrite(output_zip, imgname, prefix + "system.img")
68 common.ZipWrite(output_zip, block_list, prefix + "system.map")
Doug Zongkerfc44a512014-08-26 13:10:25 -070069
70
71def BuildSystem(input_dir, info_dict, block_list=None):
72 """Build the (sparse) system image and return the name of a temp
73 file containing it."""
74 return CreateImage(input_dir, info_dict, "system", block_list=block_list)
75
76
77def AddVendor(output_zip, prefix="IMAGES/"):
78 """Turn the contents of VENDOR into a vendor image and store in it
79 output_zip."""
Michael Runge2e0d8fc2014-11-13 21:41:08 -080080
81 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
82 if os.path.exists(prebuilt_path):
83 print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
84 return
85
Doug Zongkerfc44a512014-08-26 13:10:25 -070086 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
87 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
Dan Albert8b72aef2015-03-23 19:13:21 -070088 block_list=block_list)
Dan Albert8e0178d2015-01-27 15:53:15 -080089 common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
90 common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
Doug Zongker3c84f562014-07-31 11:06:30 -070091
92
Doug Zongkerfc44a512014-08-26 13:10:25 -070093def BuildVendor(input_dir, info_dict, block_list=None):
94 """Build the (sparse) vendor image and return the name of a temp
95 file containing it."""
96 return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
97
98
99def CreateImage(input_dir, info_dict, what, block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -0700100 print "creating " + what + ".img..."
101
Doug Zongkerfc44a512014-08-26 13:10:25 -0700102 img = common.MakeTempFile(prefix=what + "-", suffix=".img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700103
104 # The name of the directory it is making an image out of matters to
105 # mkyaffs2image. It wants "system" but we have a directory named
106 # "SYSTEM", so create a symlink.
107 try:
108 os.symlink(os.path.join(input_dir, what.upper()),
109 os.path.join(input_dir, what))
Dan Albert8b72aef2015-03-23 19:13:21 -0700110 except OSError as e:
111 # bogus error on my mac version?
112 # File "./build/tools/releasetools/img_from_target_files"
113 # os.path.join(OPTIONS.input_tmp, "system"))
114 # OSError: [Errno 17] File exists
115 if e.errno == errno.EEXIST:
Doug Zongker3c84f562014-07-31 11:06:30 -0700116 pass
117
118 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
119 fstab = info_dict["fstab"]
120 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700121 image_props["fs_type"] = fstab["/" + what].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700122
123 if what == "system":
124 fs_config_prefix = ""
125 else:
126 fs_config_prefix = what + "_"
127
128 fs_config = os.path.join(
129 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Dan Albert8b72aef2015-03-23 19:13:21 -0700130 if not os.path.exists(fs_config):
131 fs_config = None
Doug Zongker3c84f562014-07-31 11:06:30 -0700132
Ying Wanga2292c92015-03-24 19:07:40 -0700133 # Override values loaded from info_dict.
134 if fs_config:
135 image_props["fs_config"] = fs_config
Ying Wanga2292c92015-03-24 19:07:40 -0700136 if block_list:
137 image_props["block_list"] = block_list
138 if image_props.get("system_root_image") == "true":
139 image_props["ramdisk_dir"] = os.path.join(input_dir, "BOOT/RAMDISK")
140 image_props["ramdisk_fs_config"] = os.path.join(
141 input_dir, "META/boot_filesystem_config.txt")
142
Doug Zongker3c84f562014-07-31 11:06:30 -0700143 succ = build_image.BuildImage(os.path.join(input_dir, what),
Ying Wanga2292c92015-03-24 19:07:40 -0700144 image_props, img)
Doug Zongker3c84f562014-07-31 11:06:30 -0700145 assert succ, "build " + what + ".img image failed"
146
Doug Zongkerfc44a512014-08-26 13:10:25 -0700147 return img
Doug Zongker3c84f562014-07-31 11:06:30 -0700148
149
150def AddUserdata(output_zip, prefix="IMAGES/"):
Ying Wang2a048392015-06-25 13:56:53 -0700151 """Create a userdata image and store it in output_zip.
152
153 In most case we just create and store an empty userdata.img;
154 But the invoker can also request to create userdata.img with real
155 data from the target files, by setting "userdata_img_with_data=true"
156 in OPTIONS.info_dict.
157 """
Doug Zongker3c84f562014-07-31 11:06:30 -0700158
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800159 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
160 if os.path.exists(prebuilt_path):
161 print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
162 return
163
Tao Bao2c15d9e2015-07-09 11:51:16 -0700164 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
Doug Zongker3c84f562014-07-31 11:06:30 -0700165 # We only allow yaffs to have a 0/missing partition_size.
166 # Extfs, f2fs must have a size. Skip userdata.img if no size.
167 if (not image_props.get("fs_type", "").startswith("yaffs") and
168 not image_props.get("partition_size")):
169 return
170
171 print "creating userdata.img..."
172
173 # The name of the directory it is making an image out of matters to
174 # mkyaffs2image. So we create a temp dir, and within it we create an
Ying Wang2a048392015-06-25 13:56:53 -0700175 # empty dir named "data", or a symlink to the DATA dir,
176 # and build the image from that.
Doug Zongker3c84f562014-07-31 11:06:30 -0700177 temp_dir = tempfile.mkdtemp()
178 user_dir = os.path.join(temp_dir, "data")
Ying Wang2a048392015-06-25 13:56:53 -0700179 empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
180 if empty:
181 # Create an empty dir.
182 os.mkdir(user_dir)
183 else:
184 # Symlink to the DATA dir.
185 os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
186 user_dir)
187
Doug Zongker3c84f562014-07-31 11:06:30 -0700188 img = tempfile.NamedTemporaryFile()
189
190 fstab = OPTIONS.info_dict["fstab"]
191 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700192 image_props["fs_type"] = fstab["/data"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700193 succ = build_image.BuildImage(user_dir, image_props, img.name)
194 assert succ, "build userdata.img image failed"
195
196 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700197 common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700198 img.close()
Ying Wang2a048392015-06-25 13:56:53 -0700199 shutil.rmtree(temp_dir)
Doug Zongker3c84f562014-07-31 11:06:30 -0700200
201
202def AddCache(output_zip, prefix="IMAGES/"):
203 """Create an empty cache image and store it in output_zip."""
204
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800205 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
206 if os.path.exists(prebuilt_path):
207 print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
208 return
209
Tao Bao2c15d9e2015-07-09 11:51:16 -0700210 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
Doug Zongker3c84f562014-07-31 11:06:30 -0700211 # The build system has to explicitly request for cache.img.
212 if "fs_type" not in image_props:
213 return
214
215 print "creating cache.img..."
216
217 # The name of the directory it is making an image out of matters to
218 # mkyaffs2image. So we create a temp dir, and within it we create an
219 # empty dir named "cache", and build the image from that.
220 temp_dir = tempfile.mkdtemp()
221 user_dir = os.path.join(temp_dir, "cache")
222 os.mkdir(user_dir)
223 img = tempfile.NamedTemporaryFile()
224
225 fstab = OPTIONS.info_dict["fstab"]
226 if fstab:
Dan Albert8b72aef2015-03-23 19:13:21 -0700227 image_props["fs_type"] = fstab["/cache"].fs_type
Doug Zongker3c84f562014-07-31 11:06:30 -0700228 succ = build_image.BuildImage(user_dir, image_props, img.name)
229 assert succ, "build cache.img image failed"
230
231 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
Tao Bao2ed665a2015-04-01 11:21:55 -0700232 common.ZipWrite(output_zip, img.name, prefix + "cache.img")
Doug Zongker3c84f562014-07-31 11:06:30 -0700233 img.close()
234 os.rmdir(user_dir)
235 os.rmdir(temp_dir)
236
237
238def AddImagesToTargetFiles(filename):
239 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700240
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800241 if not OPTIONS.add_missing:
242 for n in input_zip.namelist():
243 if n.startswith("IMAGES/"):
244 print "target_files appears to already contain images."
245 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700246
Doug Zongker3c84f562014-07-31 11:06:30 -0700247 try:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700248 input_zip.getinfo("VENDOR/")
249 has_vendor = True
250 except KeyError:
251 has_vendor = False
Doug Zongker3c84f562014-07-31 11:06:30 -0700252
Tao Bao2c15d9e2015-07-09 11:51:16 -0700253 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700254
Tao Bao2ed665a2015-04-01 11:21:55 -0700255 common.ZipClose(input_zip)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700256 output_zip = zipfile.ZipFile(filename, "a",
257 compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -0700258
Doug Zongkerfc44a512014-08-26 13:10:25 -0700259 def banner(s):
260 print "\n\n++++ " + s + " ++++\n\n"
Doug Zongker3c84f562014-07-31 11:06:30 -0700261
Doug Zongkerfc44a512014-08-26 13:10:25 -0700262 banner("boot")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800263 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
264 boot_image = None
265 if os.path.exists(prebuilt_path):
266 print "boot.img already exists in IMAGES/, no need to rebuild..."
267 if OPTIONS.rebuild_recovery:
268 boot_image = common.GetBootableImage(
269 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
270 else:
271 boot_image = common.GetBootableImage(
272 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
273 if boot_image:
274 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700275
Doug Zongkerfc44a512014-08-26 13:10:25 -0700276 banner("recovery")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800277 recovery_image = None
278 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
279 if os.path.exists(prebuilt_path):
280 print "recovery.img already exists in IMAGES/, no need to rebuild..."
281 if OPTIONS.rebuild_recovery:
282 recovery_image = common.GetBootableImage(
283 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
284 else:
285 recovery_image = common.GetBootableImage(
286 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
287 if recovery_image:
288 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700289
Doug Zongkerfc44a512014-08-26 13:10:25 -0700290 banner("system")
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800291 AddSystem(output_zip, recovery_img=recovery_image, boot_img=boot_image)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700292 if has_vendor:
293 banner("vendor")
294 AddVendor(output_zip)
295 banner("userdata")
296 AddUserdata(output_zip)
297 banner("cache")
298 AddCache(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700299
Tao Bao2ed665a2015-04-01 11:21:55 -0700300 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700301
Doug Zongker3c84f562014-07-31 11:06:30 -0700302def main(argv):
Dan Albert8b72aef2015-03-23 19:13:21 -0700303 def option_handler(o, _):
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800304 if o in ("-a", "--add_missing"):
305 OPTIONS.add_missing = True
306 elif o in ("-r", "--rebuild_recovery",):
307 OPTIONS.rebuild_recovery = True
308 else:
309 return False
310 return True
311
Dan Albert8b72aef2015-03-23 19:13:21 -0700312 args = common.ParseOptions(
313 argv, __doc__, extra_opts="ar",
314 extra_long_opts=["add_missing", "rebuild_recovery"],
315 extra_option_handler=option_handler)
Michael Runge2e0d8fc2014-11-13 21:41:08 -0800316
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:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700329 except common.ExternalError as e:
Doug Zongker3c84f562014-07-31 11:06:30 -0700330 print
331 print " ERROR: %s" % (e,)
332 print
333 sys.exit(1)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700334 finally:
335 common.Cleanup()