blob: 568a3f13296d11453aef740c66fcb3efed94de58 [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
33import re
34import shutil
35import subprocess
36import tempfile
37import zipfile
38
39# missing in Python 2.4 and before
40if not hasattr(os, "SEEK_SET"):
41 os.SEEK_SET = 0
42
43import build_image
44import common
45
46OPTIONS = common.OPTIONS
47
48
49def AddSystem(output_zip, sparse=True, prefix="IMAGES/"):
50 """Turn the contents of SYSTEM into a system image and store it in
51 output_zip."""
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070052 block_list = tempfile.NamedTemporaryFile()
53 data = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse,
54 block_list=block_list.name)
Doug Zongker3c84f562014-07-31 11:06:30 -070055 common.ZipWriteStr(output_zip, prefix + "system.img", data)
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070056 with open(block_list.name, "rb") as f:
57 block_list_data = f.read()
58 common.ZipWriteStr(output_zip, prefix + "system.map", block_list_data)
59 block_list.close()
Doug Zongker3c84f562014-07-31 11:06:30 -070060
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070061def BuildSystem(input_dir, info_dict, sparse=True, map_file=None,
62 block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070063 return CreateImage(input_dir, info_dict, "system",
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070064 sparse=sparse, map_file=map_file, block_list=block_list)
Doug Zongker3c84f562014-07-31 11:06:30 -070065
66def AddVendor(output_zip, sparse=True, prefix="IMAGES/"):
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070067 block_list = tempfile.NamedTemporaryFile()
68 data = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse,
69 block_list=block_list.name)
Doug Zongker3c84f562014-07-31 11:06:30 -070070 common.ZipWriteStr(output_zip, prefix + "vendor.img", data)
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070071 with open(block_list.name, "rb") as f:
72 block_list_data = f.read()
73 common.ZipWriteStr(output_zip, prefix + "vendor.map", block_list_data)
74 block_list.close()
Doug Zongker3c84f562014-07-31 11:06:30 -070075
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070076def BuildVendor(input_dir, info_dict, sparse=True, map_file=None,
77 block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070078 return CreateImage(input_dir, info_dict, "vendor",
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070079 sparse=sparse, map_file=map_file, block_list=block_list)
Doug Zongker3c84f562014-07-31 11:06:30 -070080
81
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070082def CreateImage(input_dir, info_dict, what, sparse=True, map_file=None,
83 block_list=None):
Doug Zongker3c84f562014-07-31 11:06:30 -070084 print "creating " + what + ".img..."
85
86 img = tempfile.NamedTemporaryFile()
87
88 # The name of the directory it is making an image out of matters to
89 # mkyaffs2image. It wants "system" but we have a directory named
90 # "SYSTEM", so create a symlink.
91 try:
92 os.symlink(os.path.join(input_dir, what.upper()),
93 os.path.join(input_dir, what))
94 except OSError, e:
95 # bogus error on my mac version?
96 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
97 # os.path.join(OPTIONS.input_tmp, "system"))
98 # OSError: [Errno 17] File exists
99 if (e.errno == errno.EEXIST):
100 pass
101
102 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
103 fstab = info_dict["fstab"]
104 if fstab:
105 image_props["fs_type" ] = fstab["/" + what].fs_type
106
107 if what == "system":
108 fs_config_prefix = ""
109 else:
110 fs_config_prefix = what + "_"
111
112 fs_config = os.path.join(
113 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
114 if not os.path.exists(fs_config): fs_config = None
115
116 fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
117 if not os.path.exists(fc_config): fc_config = None
118
119 succ = build_image.BuildImage(os.path.join(input_dir, what),
120 image_props, img.name,
121 fs_config=fs_config,
Doug Zongkerf21cb5a2014-08-12 14:16:55 -0700122 fc_config=fc_config,
123 block_list=block_list)
Doug Zongker3c84f562014-07-31 11:06:30 -0700124 assert succ, "build " + what + ".img image failed"
125
126 mapdata = None
127
128 if sparse:
129 data = open(img.name).read()
130 img.close()
131 else:
132 success, name = build_image.UnsparseImage(img.name, replace=False)
133 if not success:
134 assert False, "unsparsing " + what + ".img failed"
135
136 if map_file:
137 mmap = tempfile.NamedTemporaryFile()
138 mimg = tempfile.NamedTemporaryFile(delete=False)
139 success = build_image.MappedUnsparseImage(
140 img.name, name, mmap.name, mimg.name)
141 if not success:
142 assert False, "creating sparse map failed"
143 os.unlink(name)
144 name = mimg.name
145
146 with open(mmap.name) as f:
147 mapdata = f.read()
148
149 try:
150 with open(name) as f:
151 data = f.read()
152 finally:
153 os.unlink(name)
154
155 if mapdata is None:
156 return data
157 else:
158 return mapdata, data
159
160
161def AddUserdata(output_zip, prefix="IMAGES/"):
162 """Create an empty userdata image and store it in output_zip."""
163
164 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
165 "data")
166 # We only allow yaffs to have a 0/missing partition_size.
167 # Extfs, f2fs must have a size. Skip userdata.img if no size.
168 if (not image_props.get("fs_type", "").startswith("yaffs") and
169 not image_props.get("partition_size")):
170 return
171
172 print "creating userdata.img..."
173
174 # The name of the directory it is making an image out of matters to
175 # mkyaffs2image. So we create a temp dir, and within it we create an
176 # empty dir named "data", and build the image from that.
177 temp_dir = tempfile.mkdtemp()
178 user_dir = os.path.join(temp_dir, "data")
179 os.mkdir(user_dir)
180 img = tempfile.NamedTemporaryFile()
181
182 fstab = OPTIONS.info_dict["fstab"]
183 if fstab:
184 image_props["fs_type" ] = fstab["/data"].fs_type
185 succ = build_image.BuildImage(user_dir, image_props, img.name)
186 assert succ, "build userdata.img image failed"
187
188 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
189 output_zip.write(img.name, prefix + "userdata.img")
190 img.close()
191 os.rmdir(user_dir)
192 os.rmdir(temp_dir)
193
194
195def AddCache(output_zip, prefix="IMAGES/"):
196 """Create an empty cache image and store it in output_zip."""
197
198 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
199 "cache")
200 # The build system has to explicitly request for cache.img.
201 if "fs_type" not in image_props:
202 return
203
204 print "creating cache.img..."
205
206 # The name of the directory it is making an image out of matters to
207 # mkyaffs2image. So we create a temp dir, and within it we create an
208 # empty dir named "cache", and build the image from that.
209 temp_dir = tempfile.mkdtemp()
210 user_dir = os.path.join(temp_dir, "cache")
211 os.mkdir(user_dir)
212 img = tempfile.NamedTemporaryFile()
213
214 fstab = OPTIONS.info_dict["fstab"]
215 if fstab:
216 image_props["fs_type" ] = fstab["/cache"].fs_type
217 succ = build_image.BuildImage(user_dir, image_props, img.name)
218 assert succ, "build cache.img image failed"
219
220 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
221 output_zip.write(img.name, prefix + "cache.img")
222 img.close()
223 os.rmdir(user_dir)
224 os.rmdir(temp_dir)
225
226
227def AddImagesToTargetFiles(filename):
228 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
229 try:
230
231 for n in input_zip.namelist():
232 if n.startswith("IMAGES/"):
233 print "target_files appears to already contain images."
234 sys.exit(1)
235
236 try:
237 input_zip.getinfo("VENDOR/")
238 has_vendor = True
239 except KeyError:
240 has_vendor = False
241
242 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
243 if "selinux_fc" in OPTIONS.info_dict:
244 OPTIONS.info_dict["selinux_fc"] = os.path.join(
245 OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts")
246
247 input_zip.close()
248 output_zip = zipfile.ZipFile(filename, "a",
249 compression=zipfile.ZIP_DEFLATED)
250
251 def banner(s):
252 print "\n\n++++ " + s + " ++++\n\n"
253
254 banner("boot")
255 boot_image = common.GetBootableImage(
256 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
257 if boot_image:
258 boot_image.AddToZip(output_zip)
259
260 banner("recovery")
261 recovery_image = common.GetBootableImage(
262 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
263 if recovery_image:
264 recovery_image.AddToZip(output_zip)
265
266 banner("system")
267 AddSystem(output_zip)
268 if has_vendor:
269 banner("vendor")
270 AddVendor(output_zip)
271 banner("userdata")
272 AddUserdata(output_zip)
273 banner("cache")
274 AddCache(output_zip)
275
276 output_zip.close()
277
278 finally:
279 shutil.rmtree(OPTIONS.input_tmp)
280
281
282def main(argv):
283 args = common.ParseOptions(argv, __doc__)
284
285 if len(args) != 1:
286 common.Usage(__doc__)
287 sys.exit(1)
288
289 AddImagesToTargetFiles(args[0])
290 print "done."
291
292if __name__ == '__main__':
293 try:
294 common.CloseInheritedPipes()
295 main(sys.argv[1:])
296 except common.ExternalError, e:
297 print
298 print " ERROR: %s" % (e,)
299 print
300 sys.exit(1)