3 # Copyright (C) 2008 The Android Open Source Project
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 Given a target-files zipfile, produces an image zipfile suitable for
19 use with 'fastboot update'.
21 Usage: img_from_target_files [flags] input_target_files output_image_zip
23 -b (--board_config) <file>
30 if sys.hexversion < 0x02040000:
31 print >> sys.stderr, "Python 2.4 or newer is required."
42 # missing in Python 2.4 and before
43 if not hasattr(os, "SEEK_SET"):
48 OPTIONS = common.OPTIONS
51 def AddUserdata(output_zip):
52 """Create an empty userdata image and store it in output_zip."""
54 print "creating userdata.img..."
56 # The name of the directory it is making an image out of matters to
57 # mkyaffs2image. So we create a temp dir, and within it we create an
58 # empty dir named "data", and build the image from that.
59 temp_dir = tempfile.mkdtemp()
60 user_dir = os.path.join(temp_dir, "data")
62 img = tempfile.NamedTemporaryFile()
64 p = common.Run(["mkyaffs2image", "-f", user_dir, img.name])
66 assert p.returncode == 0, "mkyaffs2image of userdata.img image failed"
68 common.CheckSize(img.name, "userdata.img")
69 output_zip.write(img.name, "userdata.img")
75 def AddSystem(output_zip):
76 """Turn the contents of SYSTEM into a system image and store it in
79 print "creating system.img..."
81 img = tempfile.NamedTemporaryFile()
83 # The name of the directory it is making an image out of matters to
84 # mkyaffs2image. It wants "system" but we have a directory named
85 # "SYSTEM", so create a symlink.
87 os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
88 os.path.join(OPTIONS.input_tmp, "system"))
90 # bogus error on my mac version?
91 # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
92 # os.path.join(OPTIONS.input_tmp, "system"))
93 # OSError: [Errno 17] File exists
94 if (e.errno == errno.EEXIST):
97 p = common.Run(["mkyaffs2image", "-f",
98 os.path.join(OPTIONS.input_tmp, "system"), img.name])
100 assert p.returncode == 0, "mkyaffs2image of system.img image failed"
102 img.seek(os.SEEK_SET, 0)
106 common.CheckSize(data, "system.img")
107 common.ZipWriteStr(output_zip, "system.img", data)
110 def CopyInfo(output_zip):
111 """Copy the android-info.txt file from the input to the output."""
112 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
118 def option_handler(o, a):
119 if o in ("-b", "--board_config"):
125 args = common.ParseOptions(argv, __doc__,
127 extra_long_opts=["board_config="],
128 extra_option_handler=option_handler)
131 common.Usage(__doc__)
134 OPTIONS.input_tmp = common.UnzipTemp(args[0])
136 common.LoadMaxSizes()
137 if not OPTIONS.max_image_size:
139 print " WARNING: Failed to load max image sizes; will not enforce"
140 print " image size limits."
143 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
145 common.AddBoot(output_zip)
146 common.AddRecovery(output_zip)
147 AddSystem(output_zip)
148 AddUserdata(output_zip)
151 print "cleaning up..."
153 shutil.rmtree(OPTIONS.input_tmp)
158 if __name__ == '__main__':
161 except common.ExternalError, e:
163 print " ERROR: %s" % (e,)