blob: 9aab41c25db8d9212449e1a20308d565bb1c9364 [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
Doug Zongker5fad2032014-02-24 08:13:45 -080062def BuildSystem(input_dir, info_dict, sparse=True, map_file=None):
Doug Zongkerc8b4e842014-06-16 15:16:31 -070063 return CreateImage(input_dir, info_dict, "system",
64 sparse=sparse, map_file=map_file)
65
66def AddVendor(output_zip, sparse=True):
67 data = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse)
68 common.ZipWriteStr(output_zip, "vendor.img", data)
69
70def BuildVendor(input_dir, info_dict, sparse=True, map_file=None):
71 return CreateImage(input_dir, info_dict, "vendor",
72 sparse=sparse, map_file=map_file)
73
74
75def CreateImage(input_dir, info_dict, what, sparse=True, map_file=None):
76 print "creating " + what + ".img..."
Ying Wanga0febe52013-03-20 11:02:05 -070077
78 img = tempfile.NamedTemporaryFile()
79
80 # The name of the directory it is making an image out of matters to
81 # mkyaffs2image. It wants "system" but we have a directory named
82 # "SYSTEM", so create a symlink.
83 try:
Doug Zongkerc8b4e842014-06-16 15:16:31 -070084 os.symlink(os.path.join(input_dir, what.upper()),
85 os.path.join(input_dir, what))
Ying Wanga0febe52013-03-20 11:02:05 -070086 except OSError, e:
87 # bogus error on my mac version?
88 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
89 # os.path.join(OPTIONS.input_tmp, "system"))
90 # OSError: [Errno 17] File exists
91 if (e.errno == errno.EEXIST):
92 pass
93
Doug Zongkerc8b4e842014-06-16 15:16:31 -070094 image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
Geremy Condra36bd3652014-02-06 19:45:10 -080095 fstab = info_dict["fstab"]
Ying Wanga0febe52013-03-20 11:02:05 -070096 if fstab:
Doug Zongkerc8b4e842014-06-16 15:16:31 -070097 image_props["fs_type" ] = fstab["/" + what].fs_type
Doug Zongker82822822014-06-16 09:10:55 -070098
Doug Zongkerc8b4e842014-06-16 15:16:31 -070099 if what == "system":
100 fs_config_prefix = ""
101 else:
102 fs_config_prefix = what + "_"
103
104 fs_config = os.path.join(
105 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
Doug Zongker82822822014-06-16 09:10:55 -0700106 if not os.path.exists(fs_config): fs_config = None
107
108 fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
109 if not os.path.exists(fc_config): fc_config = None
110
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700111 succ = build_image.BuildImage(os.path.join(input_dir, what),
Doug Zongker82822822014-06-16 09:10:55 -0700112 image_props, img.name,
113 fs_config=fs_config,
114 fc_config=fc_config)
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700115 assert succ, "build " + what + ".img image failed"
Ying Wanga0febe52013-03-20 11:02:05 -0700116
Doug Zongker5fad2032014-02-24 08:13:45 -0800117 mapdata = None
118
Doug Zongker01ce19c2014-02-04 13:48:15 -0800119 if sparse:
Geremy Condra15d53482014-05-13 20:23:54 -0700120 data = open(img.name).read()
Doug Zongker01ce19c2014-02-04 13:48:15 -0800121 img.close()
122 else:
123 success, name = build_image.UnsparseImage(img.name, replace=False)
124 if not success:
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700125 assert False, "unsparsing " + what + ".img failed"
Doug Zongker5fad2032014-02-24 08:13:45 -0800126
127 if map_file:
128 mmap = tempfile.NamedTemporaryFile()
129 mimg = tempfile.NamedTemporaryFile(delete=False)
130 success = build_image.MappedUnsparseImage(
131 img.name, name, mmap.name, mimg.name)
132 if not success:
133 assert False, "creating sparse map failed"
134 os.unlink(name)
135 name = mimg.name
136
137 with open(mmap.name) as f:
138 mapdata = f.read()
139
Doug Zongker01ce19c2014-02-04 13:48:15 -0800140 try:
141 with open(name) as f:
142 data = f.read()
143 finally:
144 os.unlink(name)
Ying Wanga0febe52013-03-20 11:02:05 -0700145
Doug Zongker5fad2032014-02-24 08:13:45 -0800146 if mapdata is None:
147 return data
148 else:
149 return mapdata, data
Ying Wanga0febe52013-03-20 11:02:05 -0700150
151
Doug Zongkereef39442009-04-02 12:14:19 -0700152def AddUserdata(output_zip):
153 """Create an empty userdata image and store it in output_zip."""
154
Ying Wang4e3f44f2012-11-19 10:26:00 -0800155 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
156 "data")
JP Abgrall4d09dcb2014-06-26 21:15:39 -0700157 # We only allow yaffs to have a 0/missing partition_size.
158 # Extfs, f2fs must have a size. Skip userdata.img if no size.
159 if (not image_props.get("fs_type", "").startswith("yaffs") and
Ying Wangd7321d32013-03-15 10:32:29 -0700160 not image_props.get("partition_size")):
Ying Wang4e3f44f2012-11-19 10:26:00 -0800161 return
162
Doug Zongkereef39442009-04-02 12:14:19 -0700163 print "creating userdata.img..."
164
165 # The name of the directory it is making an image out of matters to
166 # mkyaffs2image. So we create a temp dir, and within it we create an
167 # empty dir named "data", and build the image from that.
168 temp_dir = tempfile.mkdtemp()
169 user_dir = os.path.join(temp_dir, "data")
170 os.mkdir(user_dir)
171 img = tempfile.NamedTemporaryFile()
172
Doug Zongker33a4b082010-09-21 16:12:55 -0700173 fstab = OPTIONS.info_dict["fstab"]
Ying Wangbd93d422011-10-28 17:02:30 -0700174 if fstab:
175 image_props["fs_type" ] = fstab["/data"].fs_type
176 succ = build_image.BuildImage(user_dir, image_props, img.name)
177 assert succ, "build userdata.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -0700178
Doug Zongker37974732010-09-16 17:44:38 -0700179 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Doug Zongkereef39442009-04-02 12:14:19 -0700180 output_zip.write(img.name, "userdata.img")
181 img.close()
182 os.rmdir(user_dir)
183 os.rmdir(temp_dir)
184
185
Ying Wang9f8e8db2011-11-04 11:37:01 -0700186def AddCache(output_zip):
187 """Create an empty cache image and store it in output_zip."""
188
189 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
190 "cache")
191 # The build system has to explicitly request for cache.img.
192 if "fs_type" not in image_props:
193 return
194
195 print "creating cache.img..."
196
197 # The name of the directory it is making an image out of matters to
198 # mkyaffs2image. So we create a temp dir, and within it we create an
199 # empty dir named "cache", and build the image from that.
200 temp_dir = tempfile.mkdtemp()
201 user_dir = os.path.join(temp_dir, "cache")
202 os.mkdir(user_dir)
203 img = tempfile.NamedTemporaryFile()
204
205 fstab = OPTIONS.info_dict["fstab"]
206 if fstab:
207 image_props["fs_type" ] = fstab["/cache"].fs_type
208 succ = build_image.BuildImage(user_dir, image_props, img.name)
209 assert succ, "build cache.img image failed"
210
211 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
212 output_zip.write(img.name, "cache.img")
213 img.close()
214 os.rmdir(user_dir)
215 os.rmdir(temp_dir)
216
217
Doug Zongkereef39442009-04-02 12:14:19 -0700218def CopyInfo(output_zip):
219 """Copy the android-info.txt file from the input to the output."""
220 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
221 "android-info.txt")
222
223
224def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -0800225 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -0700226
227 def option_handler(o, a):
228 if o in ("-b", "--board_config"):
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700229 pass # deprecated
Doug Zongker55d93282011-01-25 17:03:34 -0800230 if o in ("-z", "--bootable_zip"):
231 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -0700232 else:
233 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700234 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700235
236 args = common.ParseOptions(argv, __doc__,
Doug Zongker55d93282011-01-25 17:03:34 -0800237 extra_opts="b:z",
238 extra_long_opts=["board_config=",
239 "bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -0700240 extra_option_handler=option_handler)
241
Doug Zongker55d93282011-01-25 17:03:34 -0800242 bootable_only = bootable_only[0]
243
Doug Zongkereef39442009-04-02 12:14:19 -0700244 if len(args) != 2:
245 common.Usage(__doc__)
246 sys.exit(1)
247
Doug Zongker55d93282011-01-25 17:03:34 -0800248 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongker37974732010-09-16 17:44:38 -0700249 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Ying Wangd421f572010-08-25 20:39:41 -0700250
Kenny Roote2e9f612013-05-29 12:59:35 -0700251 # If this image was originally labelled with SELinux contexts, make sure we
252 # also apply the labels in our new image. During building, the "file_contexts"
253 # is in the out/ directory tree, but for repacking from target-files.zip it's
254 # in the root directory of the ramdisk.
255 if "selinux_fc" in OPTIONS.info_dict:
256 OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK",
257 "file_contexts")
258
Doug Zongkereef39442009-04-02 12:14:19 -0700259 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
260
Ying Wangf8824af2014-06-03 14:07:27 -0700261 boot_image = common.GetBootableImage(
262 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
263 if boot_image:
264 boot_image.AddToZip(output_zip)
265 recovery_image = common.GetBootableImage(
266 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
267 if recovery_image:
268 recovery_image.AddToZip(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800269
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700270 def banner(s):
271 print "\n\n++++ " + s + " ++++\n\n"
272
Doug Zongker55d93282011-01-25 17:03:34 -0800273 if not bootable_only:
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700274 banner("AddSystem")
Doug Zongker55d93282011-01-25 17:03:34 -0800275 AddSystem(output_zip)
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700276 try:
277 input_zip.getinfo("VENDOR/")
278 banner("AddVendor")
279 AddVendor(output_zip)
280 except KeyError:
281 pass # no vendor partition for this device
282 banner("AddUserdata")
Doug Zongker55d93282011-01-25 17:03:34 -0800283 AddUserdata(output_zip)
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700284 banner("AddCache")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700285 AddCache(output_zip)
Doug Zongker55d93282011-01-25 17:03:34 -0800286 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700287
288 print "cleaning up..."
289 output_zip.close()
290 shutil.rmtree(OPTIONS.input_tmp)
291
292 print "done."
293
294
295if __name__ == '__main__':
296 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800297 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700298 main(sys.argv[1:])
299 except common.ExternalError, e:
300 print
301 print " ERROR: %s" % (e,)
302 print
303 sys.exit(1)