blob: 7a3262104a1eac3f5ebe17034aec4bf21013305b [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
26"""
27
28import sys
29
30if sys.hexversion < 0x02040000:
31 print >> sys.stderr, "Python 2.4 or newer is required."
32 sys.exit(1)
33
Mike Rittere44fade2009-09-15 11:18:31 -070034import errno
Doug Zongkereef39442009-04-02 12:14:19 -070035import os
36import re
37import shutil
38import subprocess
39import tempfile
40import zipfile
41
42# missing in Python 2.4 and before
43if not hasattr(os, "SEEK_SET"):
44 os.SEEK_SET = 0
45
46import common
47
48OPTIONS = common.OPTIONS
49
Doug Zongkereef39442009-04-02 12:14:19 -070050def AddUserdata(output_zip):
51 """Create an empty userdata image and store it in output_zip."""
52
53 print "creating userdata.img..."
54
55 # The name of the directory it is making an image out of matters to
56 # mkyaffs2image. So we create a temp dir, and within it we create an
57 # empty dir named "data", and build the image from that.
58 temp_dir = tempfile.mkdtemp()
59 user_dir = os.path.join(temp_dir, "data")
60 os.mkdir(user_dir)
61 img = tempfile.NamedTemporaryFile()
62
Ying Wang933abf12010-06-16 14:31:34 -070063 build_command = []
Doug Zongker33a4b082010-09-21 16:12:55 -070064 fstab = OPTIONS.info_dict["fstab"]
65 if fstab and fstab["/data"].fs_type.startswith("ext"):
Ying Wang933abf12010-06-16 14:31:34 -070066 build_command = ["mkuserimg.sh",
Doug Zongker9ce0fb62010-09-20 18:04:41 -070067 user_dir, img.name,
Doug Zongker33a4b082010-09-21 16:12:55 -070068 fstab["/data"].fs_type, "userdata"]
Doug Zongker1684d9c2010-09-17 07:44:38 -070069 if "userdata_size" in OPTIONS.info_dict:
70 build_command.append(str(OPTIONS.info_dict["userdata_size"]))
Ying Wang933abf12010-06-16 14:31:34 -070071 else:
Ying Wangd421f572010-08-25 20:39:41 -070072 build_command = ["mkyaffs2image", "-f"]
Doug Zongker37974732010-09-16 17:44:38 -070073 extra = OPTIONS.info_dict.get("mkyaffs2_extra_flags", None)
74 if extra:
75 build_command.extend(extra.split())
Ying Wangd421f572010-08-25 20:39:41 -070076 build_command.append(user_dir)
77 build_command.append(img.name)
Ying Wang933abf12010-06-16 14:31:34 -070078
79 p = common.Run(build_command);
Doug Zongkereef39442009-04-02 12:14:19 -070080 p.communicate()
Ying Wang933abf12010-06-16 14:31:34 -070081 assert p.returncode == 0, "build userdata.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -070082
Doug Zongker37974732010-09-16 17:44:38 -070083 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
Doug Zongkereef39442009-04-02 12:14:19 -070084 output_zip.write(img.name, "userdata.img")
85 img.close()
86 os.rmdir(user_dir)
87 os.rmdir(temp_dir)
88
89
90def AddSystem(output_zip):
91 """Turn the contents of SYSTEM into a system image and store it in
92 output_zip."""
93
94 print "creating system.img..."
95
96 img = tempfile.NamedTemporaryFile()
97
98 # The name of the directory it is making an image out of matters to
99 # mkyaffs2image. It wants "system" but we have a directory named
100 # "SYSTEM", so create a symlink.
Mike Rittere44fade2009-09-15 11:18:31 -0700101 try:
102 os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
103 os.path.join(OPTIONS.input_tmp, "system"))
104 except OSError, e:
105 # bogus error on my mac version?
106 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
107 # os.path.join(OPTIONS.input_tmp, "system"))
108 # OSError: [Errno 17] File exists
109 if (e.errno == errno.EEXIST):
110 pass
Doug Zongkereef39442009-04-02 12:14:19 -0700111
Ying Wang933abf12010-06-16 14:31:34 -0700112 build_command = []
Doug Zongker33a4b082010-09-21 16:12:55 -0700113 fstab = OPTIONS.info_dict["fstab"]
114 if fstab and fstab["/system"].fs_type.startswith("ext"):
Ying Wang933abf12010-06-16 14:31:34 -0700115 build_command = ["mkuserimg.sh",
116 os.path.join(OPTIONS.input_tmp, "system"), img.name,
Doug Zongker33a4b082010-09-21 16:12:55 -0700117 fstab["/system"].fs_type, "system"]
Doug Zongker1684d9c2010-09-17 07:44:38 -0700118 if "system_img" in OPTIONS.info_dict:
119 build_command.append(str(OPTIONS.info_dict["system_size"]))
Ying Wang933abf12010-06-16 14:31:34 -0700120 else:
Ying Wangd421f572010-08-25 20:39:41 -0700121 build_command = ["mkyaffs2image", "-f"]
Doug Zongker37974732010-09-16 17:44:38 -0700122 extra = OPTIONS.info_dict.get("mkyaffs2_extra_flags", None)
123 if extra:
124 build_command.extend(extra.split())
Ying Wangd421f572010-08-25 20:39:41 -0700125 build_command.append(os.path.join(OPTIONS.input_tmp, "system"))
126 build_command.append(img.name)
Ying Wang933abf12010-06-16 14:31:34 -0700127
Doug Zongker39a98452010-09-03 14:15:34 -0700128 p = common.Run(build_command)
129 p.communicate()
130 assert p.returncode == 0, "build system.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -0700131
132 img.seek(os.SEEK_SET, 0)
133 data = img.read()
134 img.close()
135
Doug Zongker37974732010-09-16 17:44:38 -0700136 common.CheckSize(data, "system.img", OPTIONS.info_dict)
Doug Zongker048e7ca2009-06-15 14:31:53 -0700137 common.ZipWriteStr(output_zip, "system.img", data)
Doug Zongkereef39442009-04-02 12:14:19 -0700138
139
140def CopyInfo(output_zip):
141 """Copy the android-info.txt file from the input to the output."""
142 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
143 "android-info.txt")
144
145
146def main(argv):
147
148 def option_handler(o, a):
149 if o in ("-b", "--board_config"):
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700150 pass # deprecated
Doug Zongkereef39442009-04-02 12:14:19 -0700151 else:
152 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700153 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700154
155 args = common.ParseOptions(argv, __doc__,
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700156 extra_opts="b:",
157 extra_long_opts=["board_config="],
Doug Zongkereef39442009-04-02 12:14:19 -0700158 extra_option_handler=option_handler)
159
160 if len(args) != 2:
161 common.Usage(__doc__)
162 sys.exit(1)
163
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700164 OPTIONS.input_tmp = common.UnzipTemp(args[0])
165
Doug Zongker37974732010-09-16 17:44:38 -0700166 input_zip = zipfile.ZipFile(args[0], "r")
167 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Ying Wangd421f572010-08-25 20:39:41 -0700168
Doug Zongkereef39442009-04-02 12:14:19 -0700169 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
170
Doug Zongker37974732010-09-16 17:44:38 -0700171 common.AddBoot(output_zip, OPTIONS.info_dict)
172 common.AddRecovery(output_zip, OPTIONS.info_dict)
Doug Zongkereef39442009-04-02 12:14:19 -0700173 AddSystem(output_zip)
174 AddUserdata(output_zip)
175 CopyInfo(output_zip)
176
177 print "cleaning up..."
178 output_zip.close()
179 shutil.rmtree(OPTIONS.input_tmp)
180
181 print "done."
182
183
184if __name__ == '__main__':
185 try:
186 main(sys.argv[1:])
187 except common.ExternalError, e:
188 print
189 print " ERROR: %s" % (e,)
190 print
191 sys.exit(1)