blob: a74ef890b94b7700d103c5a9a090b4a390628d42 [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 Wang065521b2010-08-23 11:34:40 -070063 build_command = []
Doug Zongkerc77a9ad2010-09-16 11:28:43 -070064 if OPTIONS.info_dict.get("fs_type", "").startswith("ext"):
Ying Wang065521b2010-08-23 11:34:40 -070065 build_command = ["mkuserimg.sh",
Doug Zongkerc77a9ad2010-09-16 11:28:43 -070066 user_dir, img.name, OPTIONS.info_dict["fs_type"], "userdata"]
Ying Wang065521b2010-08-23 11:34:40 -070067 if "userdata.img" in OPTIONS.max_image_size:
68 build_command.append(str(OPTIONS.max_image_size["userdata.img"]))
69 else:
Ying Wangd421f572010-08-25 20:39:41 -070070 build_command = ["mkyaffs2image", "-f"]
71 if OPTIONS.mkyaffs2_extra_flags is not None:
72 build_command.append(OPTIONS.mkyaffs2_extra_flags);
73 build_command.append(user_dir)
74 build_command.append(img.name)
Ying Wang065521b2010-08-23 11:34:40 -070075
76 p = common.Run(build_command)
Doug Zongkereef39442009-04-02 12:14:19 -070077 p.communicate()
Ying Wang065521b2010-08-23 11:34:40 -070078 assert p.returncode == 0, "build userdata.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -070079
Doug Zongkerc77a9ad2010-09-16 11:28:43 -070080 common.CheckSize(img.name, "userdata.img")
Doug Zongkereef39442009-04-02 12:14:19 -070081 output_zip.write(img.name, "userdata.img")
82 img.close()
83 os.rmdir(user_dir)
84 os.rmdir(temp_dir)
85
86
87def AddSystem(output_zip):
88 """Turn the contents of SYSTEM into a system image and store it in
89 output_zip."""
90
91 print "creating system.img..."
92
93 img = tempfile.NamedTemporaryFile()
94
95 # The name of the directory it is making an image out of matters to
96 # mkyaffs2image. It wants "system" but we have a directory named
97 # "SYSTEM", so create a symlink.
Mike Rittere44fade2009-09-15 11:18:31 -070098 try:
99 os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
100 os.path.join(OPTIONS.input_tmp, "system"))
101 except OSError, e:
102 # bogus error on my mac version?
103 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
104 # os.path.join(OPTIONS.input_tmp, "system"))
105 # OSError: [Errno 17] File exists
106 if (e.errno == errno.EEXIST):
107 pass
Doug Zongkereef39442009-04-02 12:14:19 -0700108
Ying Wang065521b2010-08-23 11:34:40 -0700109 build_command = []
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700110 if OPTIONS.info_dict.get("fs_type", "").startswith("ext"):
Ying Wang065521b2010-08-23 11:34:40 -0700111 build_command = ["mkuserimg.sh",
112 os.path.join(OPTIONS.input_tmp, "system"), img.name,
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700113 OPTIONS.info_dict["fs_type"], "system"]
Ying Wang065521b2010-08-23 11:34:40 -0700114 if "system.img" in OPTIONS.max_image_size:
115 build_command.append(str(OPTIONS.max_image_size["system.img"]))
116 else:
Ying Wangd421f572010-08-25 20:39:41 -0700117 build_command = ["mkyaffs2image", "-f"]
118 if OPTIONS.mkyaffs2_extra_flags is not None:
Doug Zongker39a98452010-09-03 14:15:34 -0700119 build_command.extend(OPTIONS.mkyaffs2_extra_flags.split());
Ying Wangd421f572010-08-25 20:39:41 -0700120 build_command.append(os.path.join(OPTIONS.input_tmp, "system"))
121 build_command.append(img.name)
Ying Wang065521b2010-08-23 11:34:40 -0700122
Doug Zongker39a98452010-09-03 14:15:34 -0700123 p = common.Run(build_command)
124 p.communicate()
125 assert p.returncode == 0, "build system.img image failed"
Doug Zongkereef39442009-04-02 12:14:19 -0700126
127 img.seek(os.SEEK_SET, 0)
128 data = img.read()
129 img.close()
130
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700131 common.CheckSize(data, "system.img")
Doug Zongker048e7ca2009-06-15 14:31:53 -0700132 common.ZipWriteStr(output_zip, "system.img", data)
Doug Zongkereef39442009-04-02 12:14:19 -0700133
134
135def CopyInfo(output_zip):
136 """Copy the android-info.txt file from the input to the output."""
137 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
138 "android-info.txt")
139
140
141def main(argv):
142
143 def option_handler(o, a):
144 if o in ("-b", "--board_config"):
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700145 pass # deprecated
Doug Zongkereef39442009-04-02 12:14:19 -0700146 else:
147 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700148 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700149
150 args = common.ParseOptions(argv, __doc__,
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700151 extra_opts="b:",
152 extra_long_opts=["board_config="],
Doug Zongkereef39442009-04-02 12:14:19 -0700153 extra_option_handler=option_handler)
154
155 if len(args) != 2:
156 common.Usage(__doc__)
157 sys.exit(1)
158
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700159 OPTIONS.input_tmp = common.UnzipTemp(args[0])
160
Doug Zongkerc77a9ad2010-09-16 11:28:43 -0700161 OPTIONS.info_dict = common.LoadInfoDict()
162 common.LoadMaxSizes(OPTIONS.info_dict)
Doug Zongkereef39442009-04-02 12:14:19 -0700163 if not OPTIONS.max_image_size:
164 print
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700165 print " WARNING: Failed to load max image sizes; will not enforce"
166 print " image size limits."
Doug Zongkereef39442009-04-02 12:14:19 -0700167 print
168
Ying Wangd421f572010-08-25 20:39:41 -0700169 common.LoadMkyaffs2ExtraFlags()
170
Doug Zongkereef39442009-04-02 12:14:19 -0700171 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
172
173 common.AddBoot(output_zip)
174 common.AddRecovery(output_zip)
175 AddSystem(output_zip)
176 AddUserdata(output_zip)
177 CopyInfo(output_zip)
178
179 print "cleaning up..."
180 output_zip.close()
181 shutil.rmtree(OPTIONS.input_tmp)
182
183 print "done."
184
185
186if __name__ == '__main__':
187 try:
188 main(sys.argv[1:])
189 except common.ExternalError, e:
190 print
191 print " ERROR: %s" % (e,)
192 print
193 sys.exit(1)