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."
41 # missing in Python 2.4 and before
42 if not hasattr(os, "SEEK_SET"):
47 OPTIONS = common.OPTIONS
50 def AddUserdata(output_zip):
51 """Create an empty userdata image and store it in output_zip."""
53 print "creating userdata.img..."
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")
61 img = tempfile.NamedTemporaryFile()
63 p = common.Run(["mkyaffs2image", "-f", user_dir, img.name])
65 assert p.returncode == 0, "mkyaffs2image of userdata.img image failed"
67 common.CheckSize(img.name, "userdata.img")
68 output_zip.write(img.name, "userdata.img")
74 def AddSystem(output_zip):
75 """Turn the contents of SYSTEM into a system image and store it in
78 print "creating system.img..."
80 img = tempfile.NamedTemporaryFile()
82 # The name of the directory it is making an image out of matters to
83 # mkyaffs2image. It wants "system" but we have a directory named
84 # "SYSTEM", so create a symlink.
85 os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
86 os.path.join(OPTIONS.input_tmp, "system"))
88 p = common.Run(["mkyaffs2image", "-f",
89 os.path.join(OPTIONS.input_tmp, "system"), img.name])
91 assert p.returncode == 0, "mkyaffs2image of system.img image failed"
93 img.seek(os.SEEK_SET, 0)
97 common.CheckSize(data, "system.img")
98 common.ZipWriteStr(output_zip, "system.img", data)
101 def CopyInfo(output_zip):
102 """Copy the android-info.txt file from the input to the output."""
103 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
109 def option_handler(o, a):
110 if o in ("-b", "--board_config"):
116 args = common.ParseOptions(argv, __doc__,
118 extra_long_opts=["board_config="],
119 extra_option_handler=option_handler)
122 common.Usage(__doc__)
125 OPTIONS.input_tmp = common.UnzipTemp(args[0])
127 common.LoadMaxSizes()
128 if not OPTIONS.max_image_size:
130 print " WARNING: Failed to load max image sizes; will not enforce"
131 print " image size limits."
134 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
136 common.AddBoot(output_zip)
137 common.AddRecovery(output_zip)
138 AddSystem(output_zip)
139 AddUserdata(output_zip)
142 print "cleaning up..."
144 shutil.rmtree(OPTIONS.input_tmp)
149 if __name__ == '__main__':
152 except common.ExternalError, e:
154 print " ERROR: %s" % (e,)