blob: 93092b32ed971e9a867f0be95b9aea2a726ffc8c [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2008 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, produces an image zipfile suitable for
19use with 'fastboot update'.
20
21Usage: img_from_target_files [flags] input_target_files output_image_zip
22
23 -b (--board_config) <file>
Doug Zongkerfdd8e692009-08-03 17:27:48 -070024 Deprecated.
Doug Zongkereef39442009-04-02 12:14:19 -070025
Doug Zongker55d93282011-01-25 17:03:34 -080026 -z (--bootable_zip)
27 Include only the bootable images (eg 'boot' and 'recovery') in
28 the output.
29
Doug Zongkereef39442009-04-02 12:14:19 -070030"""
31
32import sys
33
Doug Zongkercf6d5a92014-02-18 10:57:07 -080034if sys.hexversion < 0x02070000:
35 print >> sys.stderr, "Python 2.7 or newer is required."
Doug Zongkereef39442009-04-02 12:14:19 -070036 sys.exit(1)
37
Mike Rittere44fade2009-09-15 11:18:31 -070038import errno
Doug Zongkereef39442009-04-02 12:14:19 -070039import os
40import re
41import shutil
42import subprocess
43import tempfile
44import zipfile
45
46# missing in Python 2.4 and before
47if not hasattr(os, "SEEK_SET"):
48 os.SEEK_SET = 0
49
Ying Wangbd93d422011-10-28 17:02:30 -070050import build_image
Doug Zongkereef39442009-04-02 12:14:19 -070051import common
52
53OPTIONS = common.OPTIONS
54
Ying Wanga0febe52013-03-20 11:02:05 -070055
Doug Zongker01ce19c2014-02-04 13:48:15 -080056def AddSystem(output_zip, sparse=True):
Ying Wanga0febe52013-03-20 11:02:05 -070057 """Turn the contents of SYSTEM into a system image and store it in
58 output_zip."""
Geremy Condra36bd3652014-02-06 19:45:10 -080059 data = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse)
60 common.ZipWriteStr(output_zip, "system.img", data)
Ying Wanga0febe52013-03-20 11:02:05 -070061
Geremy Condra36bd3652014-02-06 19:45:10 -080062
Doug Zongker5fad2032014-02-24 08:13:45 -080063def BuildSystem(input_dir, info_dict, sparse=True, map_file=None):
Ying Wanga0febe52013-03-20 11:02:05 -070064 print "creating system.img..."
65
66 img = tempfile.NamedTemporaryFile()
67
68 # The name of the directory it is making an image out of matters to
69 # mkyaffs2image. It wants "system" but we have a directory named
70 # "SYSTEM", so create a symlink.
71 try:
Geremy Condra36bd3652014-02-06 19:45:10 -080072 os.symlink(os.path.join(input_dir, "SYSTEM"),
73 os.path.join(input_dir, "system"))
Ying Wanga0febe52013-03-20 11:02:05 -070074 except OSError, e:
75 # bogus error on my mac version?
76 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
77 # os.path.join(OPTIONS.input_tmp, "system"))
78 # OSError: [Errno 17] File exists
79 if (e.errno == errno.EEXIST):
80 pass
81
Geremy Condra36bd3652014-02-06 19:45:10 -080082 image_props = build_image.ImagePropFromGlobalDict(info_dict, "system")
83 fstab = info_dict["fstab"]
Ying Wanga0febe52013-03-20 11:02:05 -070084 if fstab:
85 image_props["fs_type" ] = fstab["/system"].fs_type
Geremy Condra36bd3652014-02-06 19:45:10 -080086 succ = build_image.BuildImage(os.path.join(input_dir, "system"),
Ying Wanga0febe52013-03-20 11:02:05 -070087 image_props, img.name)
88 assert succ, "build system.img image failed"
89
Doug Zongker5fad2032014-02-24 08:13:45 -080090 mapdata = None
91
Doug Zongker01ce19c2014-02-04 13:48:15 -080092 if sparse:
Geremy Condra15d53482014-05-13 20:23:54 -070093 data = open(img.name).read()
Doug Zongker01ce19c2014-02-04 13:48:15 -080094 img.close()
95 else:
96 success, name = build_image.UnsparseImage(img.name, replace=False)
97 if not success:
98 assert False, "unsparsing system.img failed"
Doug Zongker5fad2032014-02-24 08:13:45 -080099
100 if map_file:
101 mmap = tempfile.NamedTemporaryFile()
102 mimg = tempfile.NamedTemporaryFile(delete=False)
103 success = build_image.MappedUnsparseImage(
104 img.name, name, mmap.name, mimg.name)
105 if not success:
106 assert False, "creating sparse map failed"
107 os.unlink(name)
108 name = mimg.name
109
110 with open(mmap.name) as f:
111 mapdata = f.read()
112
Doug Zongker01ce19c2014-02-04 13:48:15 -0800113 try:
114 with open(name) as f:
115 data = f.read()
116 finally:
117 os.unlink(name)
Ying Wanga0febe52013-03-20 11:02:05 -0700118
Doug Zongker5fad2032014-02-24 08:13:45 -0800119 if mapdata is None:
120 return data
121 else:
122 return mapdata, data
Ying Wanga0febe52013-03-20 11:02:05 -0700123
124
125def AddVendor(output_zip):
126 """Turn the contents of VENDOR into vendor.img and store it in
127 output_zip."""
128
129 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
130 "vendor")
131 # The build system has to explicitly request for vendor.img.
132 if "fs_type" not in image_props:
133 return
134
135 print "creating vendor.img..."
136
137 img = tempfile.NamedTemporaryFile()
138
139 # The name of the directory it is making an image out of matters to
140 # mkyaffs2image. It wants "vendor" but we have a directory named
141 # "VENDOR", so create a symlink or an empty directory if VENDOR does not
142 # exist.
143 if not os.path.exists(os.path.join(OPTIONS.input_tmp, "vendor")):
144 if os.path.exists(os.path.join(OPTIONS.input_tmp, "VENDOR")):
145 os.symlink(os.path.join(OPTIONS.input_tmp, "VENDOR"),
146 os.path.join(OPTIONS.input_tmp, "vendor"))
147 else:
148 os.mkdir(os.path.join(OPTIONS.input_tmp, "vendor"))
149
150 img = tempfile.NamedTemporaryFile()
151
152 fstab = OPTIONS.info_dict["fstab"]
153 if fstab:
154 image_props["fs_type" ] = fstab["/vendor"].fs_type
155 succ = build_image.BuildImage(os.path.join(OPTIONS.input_tmp, "vendor"),
156 image_props, img.name)
157 assert succ, "build vendor.img image failed"
158
159 common.CheckSize(img.name, "vendor.img", OPTIONS.info_dict)
160 output_zip.write(img.name, "vendor.img")
161 img.close()
162
163
Doug Zongkereef39442009-04-02 12:14:19 -0700164def AddUserdata(output_zip):
165 """Create an empty userdata image and store it in output_zip."""
166
Ying Wang4e3f44f2012-11-19 10:26:00 -0800167 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
168 "data")
Ying Wangd7321d32013-03-15 10:32:29 -0700169 # If no userdata_size is provided for extfs, skip userdata.img.
170 if (image_props.get("fs_type", "").startswith("ext") and
171 not image_props.get("partition_size")):
Ying Wang4e3f44f2012-11-19 10:26:00 -0800172 return
173
Doug Zongkereef39442009-04-02 12:14:19 -0700174 print "creating userdata.img..."
175
176 # The name of the directory it is making an image out of matters to
177 # mkyaffs2image. So we create a temp dir, and within it we create an
178 # empty dir named "data", and build the image from that.
179 temp_dir = tempfile.mkdtemp()
180 user_dir = os.path.join(temp_dir, "data")
181 os.mkdir(user_dir)
182 img = tempfile.NamedTemporaryFile()
183
Doug Zongker33a4b082010-09-21 16:12:55 -0700184 fstab = OPTIONS.info_dict["fstab"]
Ying Wangbd93d422011-10-28 17:02:30 -0700185 if fstab:
186 image_props["fs_type" ] = fstab["/data"].fs_type
187 succ = build_image.BuildImage(user_dir, image_props, img.name)
188 assert succ, "build userdata.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -0700189
Doug Zongker37974732010-09-16 17:44:38 -0700190 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Doug Zongkereef39442009-04-02 12:14:19 -0700191 output_zip.write(img.name, "userdata.img")
192 img.close()
193 os.rmdir(user_dir)
194 os.rmdir(temp_dir)
195
196
Ying Wang9f8e8db2011-11-04 11:37:01 -0700197def AddCache(output_zip):
198 """Create an empty cache image and store it in output_zip."""
199
200 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
201 "cache")
202 # The build system has to explicitly request for cache.img.
203 if "fs_type" not in image_props:
204 return
205
206 print "creating cache.img..."
207
208 # The name of the directory it is making an image out of matters to
209 # mkyaffs2image. So we create a temp dir, and within it we create an
210 # empty dir named "cache", and build the image from that.
211 temp_dir = tempfile.mkdtemp()
212 user_dir = os.path.join(temp_dir, "cache")
213 os.mkdir(user_dir)
214 img = tempfile.NamedTemporaryFile()
215
216 fstab = OPTIONS.info_dict["fstab"]
217 if fstab:
218 image_props["fs_type" ] = fstab["/cache"].fs_type
219 succ = build_image.BuildImage(user_dir, image_props, img.name)
220 assert succ, "build cache.img image failed"
221
222 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
223 output_zip.write(img.name, "cache.img")
224 img.close()
225 os.rmdir(user_dir)
226 os.rmdir(temp_dir)
227
228
Doug Zongkereef39442009-04-02 12:14:19 -0700229def CopyInfo(output_zip):
230 """Copy the android-info.txt file from the input to the output."""
231 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
232 "android-info.txt")
233
234
235def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -0800236 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -0700237
238 def option_handler(o, a):
239 if o in ("-b", "--board_config"):
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700240 pass # deprecated
Doug Zongker55d93282011-01-25 17:03:34 -0800241 if o in ("-z", "--bootable_zip"):
242 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -0700243 else:
244 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700245 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700246
247 args = common.ParseOptions(argv, __doc__,
Doug Zongker55d93282011-01-25 17:03:34 -0800248 extra_opts="b:z",
249 extra_long_opts=["board_config=",
250 "bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -0700251 extra_option_handler=option_handler)
252
Doug Zongker55d93282011-01-25 17:03:34 -0800253 bootable_only = bootable_only[0]
254
Doug Zongkereef39442009-04-02 12:14:19 -0700255 if len(args) != 2:
256 common.Usage(__doc__)
257 sys.exit(1)
258
Doug Zongker55d93282011-01-25 17:03:34 -0800259 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongker37974732010-09-16 17:44:38 -0700260 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Ying Wangd421f572010-08-25 20:39:41 -0700261
Kenny Roote2e9f612013-05-29 12:59:35 -0700262 # If this image was originally labelled with SELinux contexts, make sure we
263 # also apply the labels in our new image. During building, the "file_contexts"
264 # is in the out/ directory tree, but for repacking from target-files.zip it's
265 # in the root directory of the ramdisk.
266 if "selinux_fc" in OPTIONS.info_dict:
267 OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK",
268 "file_contexts")
269
Doug Zongkereef39442009-04-02 12:14:19 -0700270 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
271
Doug Zongker55d93282011-01-25 17:03:34 -0800272 common.GetBootableImage(
273 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT").AddToZip(output_zip)
274 common.GetBootableImage(
275 "recovery.img", "recovery.img", OPTIONS.input_tmp,
276 "RECOVERY").AddToZip(output_zip)
277
278 if not bootable_only:
279 AddSystem(output_zip)
Ying Wanga0febe52013-03-20 11:02:05 -0700280 AddVendor(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800281 AddUserdata(output_zip)
Ying Wang9f8e8db2011-11-04 11:37:01 -0700282 AddCache(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800283 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700284
285 print "cleaning up..."
286 output_zip.close()
287 shutil.rmtree(OPTIONS.input_tmp)
288
289 print "done."
290
291
292if __name__ == '__main__':
293 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800294 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700295 main(sys.argv[1:])
296 except common.ExternalError, e:
297 print
298 print " ERROR: %s" % (e,)
299 print
300 sys.exit(1)