blob: 2ca931604ae8a085226c0dd3f843290892624fad [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
Doug Zongker82822822014-06-16 09:10:55 -070086
87 fs_config = os.path.join(input_dir, "META/filesystem_config.txt")
88 if not os.path.exists(fs_config): fs_config = None
89
90 fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
91 if not os.path.exists(fc_config): fc_config = None
92
Geremy Condra36bd3652014-02-06 19:45:10 -080093 succ = build_image.BuildImage(os.path.join(input_dir, "system"),
Doug Zongker82822822014-06-16 09:10:55 -070094 image_props, img.name,
95 fs_config=fs_config,
96 fc_config=fc_config)
Ying Wanga0febe52013-03-20 11:02:05 -070097 assert succ, "build system.img image failed"
98
Doug Zongker5fad2032014-02-24 08:13:45 -080099 mapdata = None
100
Doug Zongker01ce19c2014-02-04 13:48:15 -0800101 if sparse:
Geremy Condra15d53482014-05-13 20:23:54 -0700102 data = open(img.name).read()
Doug Zongker01ce19c2014-02-04 13:48:15 -0800103 img.close()
104 else:
105 success, name = build_image.UnsparseImage(img.name, replace=False)
106 if not success:
107 assert False, "unsparsing system.img failed"
Doug Zongker5fad2032014-02-24 08:13:45 -0800108
109 if map_file:
110 mmap = tempfile.NamedTemporaryFile()
111 mimg = tempfile.NamedTemporaryFile(delete=False)
112 success = build_image.MappedUnsparseImage(
113 img.name, name, mmap.name, mimg.name)
114 if not success:
115 assert False, "creating sparse map failed"
116 os.unlink(name)
117 name = mimg.name
118
119 with open(mmap.name) as f:
120 mapdata = f.read()
121
Doug Zongker01ce19c2014-02-04 13:48:15 -0800122 try:
123 with open(name) as f:
124 data = f.read()
125 finally:
126 os.unlink(name)
Ying Wanga0febe52013-03-20 11:02:05 -0700127
Doug Zongker5fad2032014-02-24 08:13:45 -0800128 if mapdata is None:
129 return data
130 else:
131 return mapdata, data
Ying Wanga0febe52013-03-20 11:02:05 -0700132
133
134def AddVendor(output_zip):
135 """Turn the contents of VENDOR into vendor.img and store it in
136 output_zip."""
137
138 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
139 "vendor")
140 # The build system has to explicitly request for vendor.img.
141 if "fs_type" not in image_props:
142 return
143
144 print "creating vendor.img..."
145
146 img = tempfile.NamedTemporaryFile()
147
148 # The name of the directory it is making an image out of matters to
149 # mkyaffs2image. It wants "vendor" but we have a directory named
150 # "VENDOR", so create a symlink or an empty directory if VENDOR does not
151 # exist.
152 if not os.path.exists(os.path.join(OPTIONS.input_tmp, "vendor")):
153 if os.path.exists(os.path.join(OPTIONS.input_tmp, "VENDOR")):
154 os.symlink(os.path.join(OPTIONS.input_tmp, "VENDOR"),
155 os.path.join(OPTIONS.input_tmp, "vendor"))
156 else:
157 os.mkdir(os.path.join(OPTIONS.input_tmp, "vendor"))
158
159 img = tempfile.NamedTemporaryFile()
160
161 fstab = OPTIONS.info_dict["fstab"]
162 if fstab:
163 image_props["fs_type" ] = fstab["/vendor"].fs_type
164 succ = build_image.BuildImage(os.path.join(OPTIONS.input_tmp, "vendor"),
165 image_props, img.name)
166 assert succ, "build vendor.img image failed"
167
168 common.CheckSize(img.name, "vendor.img", OPTIONS.info_dict)
169 output_zip.write(img.name, "vendor.img")
170 img.close()
171
172
Doug Zongkereef39442009-04-02 12:14:19 -0700173def AddUserdata(output_zip):
174 """Create an empty userdata image and store it in output_zip."""
175
Ying Wang4e3f44f2012-11-19 10:26:00 -0800176 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
177 "data")
Ying Wangd7321d32013-03-15 10:32:29 -0700178 # If no userdata_size is provided for extfs, skip userdata.img.
179 if (image_props.get("fs_type", "").startswith("ext") and
180 not image_props.get("partition_size")):
Ying Wang4e3f44f2012-11-19 10:26:00 -0800181 return
182
Doug Zongkereef39442009-04-02 12:14:19 -0700183 print "creating userdata.img..."
184
185 # The name of the directory it is making an image out of matters to
186 # mkyaffs2image. So we create a temp dir, and within it we create an
187 # empty dir named "data", and build the image from that.
188 temp_dir = tempfile.mkdtemp()
189 user_dir = os.path.join(temp_dir, "data")
190 os.mkdir(user_dir)
191 img = tempfile.NamedTemporaryFile()
192
Doug Zongker33a4b082010-09-21 16:12:55 -0700193 fstab = OPTIONS.info_dict["fstab"]
Ying Wangbd93d422011-10-28 17:02:30 -0700194 if fstab:
195 image_props["fs_type" ] = fstab["/data"].fs_type
196 succ = build_image.BuildImage(user_dir, image_props, img.name)
197 assert succ, "build userdata.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -0700198
Doug Zongker37974732010-09-16 17:44:38 -0700199 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Doug Zongkereef39442009-04-02 12:14:19 -0700200 output_zip.write(img.name, "userdata.img")
201 img.close()
202 os.rmdir(user_dir)
203 os.rmdir(temp_dir)
204
205
Ying Wang9f8e8db2011-11-04 11:37:01 -0700206def AddCache(output_zip):
207 """Create an empty cache image and store it in output_zip."""
208
209 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
210 "cache")
211 # 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:
227 image_props["fs_type" ] = fstab["/cache"].fs_type
228 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)
232 output_zip.write(img.name, "cache.img")
233 img.close()
234 os.rmdir(user_dir)
235 os.rmdir(temp_dir)
236
237
Doug Zongkereef39442009-04-02 12:14:19 -0700238def CopyInfo(output_zip):
239 """Copy the android-info.txt file from the input to the output."""
240 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
241 "android-info.txt")
242
243
244def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -0800245 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -0700246
247 def option_handler(o, a):
248 if o in ("-b", "--board_config"):
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700249 pass # deprecated
Doug Zongker55d93282011-01-25 17:03:34 -0800250 if o in ("-z", "--bootable_zip"):
251 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -0700252 else:
253 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700254 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700255
256 args = common.ParseOptions(argv, __doc__,
Doug Zongker55d93282011-01-25 17:03:34 -0800257 extra_opts="b:z",
258 extra_long_opts=["board_config=",
259 "bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -0700260 extra_option_handler=option_handler)
261
Doug Zongker55d93282011-01-25 17:03:34 -0800262 bootable_only = bootable_only[0]
263
Doug Zongkereef39442009-04-02 12:14:19 -0700264 if len(args) != 2:
265 common.Usage(__doc__)
266 sys.exit(1)
267
Doug Zongker55d93282011-01-25 17:03:34 -0800268 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongker37974732010-09-16 17:44:38 -0700269 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Ying Wangd421f572010-08-25 20:39:41 -0700270
Kenny Roote2e9f612013-05-29 12:59:35 -0700271 # If this image was originally labelled with SELinux contexts, make sure we
272 # also apply the labels in our new image. During building, the "file_contexts"
273 # is in the out/ directory tree, but for repacking from target-files.zip it's
274 # in the root directory of the ramdisk.
275 if "selinux_fc" in OPTIONS.info_dict:
276 OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK",
277 "file_contexts")
278
Doug Zongkereef39442009-04-02 12:14:19 -0700279 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
280
Ying Wangf8824af2014-06-03 14:07:27 -0700281 boot_image = common.GetBootableImage(
282 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
283 if boot_image:
284 boot_image.AddToZip(output_zip)
285 recovery_image = common.GetBootableImage(
286 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
287 if recovery_image:
288 recovery_image.AddToZip(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800289
290 if not bootable_only:
291 AddSystem(output_zip)
Ying Wanga0febe52013-03-20 11:02:05 -0700292 AddVendor(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800293 AddUserdata(output_zip)
Ying Wang9f8e8db2011-11-04 11:37:01 -0700294 AddCache(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800295 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700296
297 print "cleaning up..."
298 output_zip.close()
299 shutil.rmtree(OPTIONS.input_tmp)
300
301 print "done."
302
303
304if __name__ == '__main__':
305 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800306 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700307 main(sys.argv[1:])
308 except common.ExternalError, e:
309 print
310 print " ERROR: %s" % (e,)
311 print
312 sys.exit(1)