Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1 | #!/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 | """ |
| 18 | Given a target-files zipfile, produces an image zipfile suitable for |
| 19 | use with 'fastboot update'. |
| 20 | |
| 21 | Usage: img_from_target_files [flags] input_target_files output_image_zip |
| 22 | |
| 23 | -b (--board_config) <file> |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 24 | Deprecated. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 25 | |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 26 | -f (--fs_type) <value> |
| 27 | The file system type of the user image files to be created. |
| 28 | It can be ext fs variants, such as ext2, ext3, ext4, etc. |
| 29 | Default is yaffs. |
| 30 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 31 | """ |
| 32 | |
| 33 | import sys |
| 34 | |
| 35 | if sys.hexversion < 0x02040000: |
| 36 | print >> sys.stderr, "Python 2.4 or newer is required." |
| 37 | sys.exit(1) |
| 38 | |
Mike Ritter | e44fade | 2009-09-15 11:18:31 -0700 | [diff] [blame] | 39 | import errno |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 40 | import os |
| 41 | import re |
| 42 | import shutil |
| 43 | import subprocess |
| 44 | import tempfile |
| 45 | import zipfile |
| 46 | |
| 47 | # missing in Python 2.4 and before |
| 48 | if not hasattr(os, "SEEK_SET"): |
| 49 | os.SEEK_SET = 0 |
| 50 | |
| 51 | import common |
| 52 | |
| 53 | OPTIONS = common.OPTIONS |
| 54 | |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 55 | class UserImageOptions(object): pass |
| 56 | USERIMAGE_OPTIONS = UserImageOptions() |
| 57 | USERIMAGE_OPTIONS.fs_type = None |
| 58 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 59 | |
| 60 | def AddUserdata(output_zip): |
| 61 | """Create an empty userdata image and store it in output_zip.""" |
| 62 | |
| 63 | print "creating userdata.img..." |
| 64 | |
| 65 | # The name of the directory it is making an image out of matters to |
| 66 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 67 | # empty dir named "data", and build the image from that. |
| 68 | temp_dir = tempfile.mkdtemp() |
| 69 | user_dir = os.path.join(temp_dir, "data") |
| 70 | os.mkdir(user_dir) |
| 71 | img = tempfile.NamedTemporaryFile() |
| 72 | |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 73 | build_command = [] |
| 74 | if USERIMAGE_OPTIONS.fs_type is not None and USERIMAGE_OPTIONS.fs_type.startswith("ext"): |
| 75 | build_command = ["mkuserimg.sh", |
| 76 | user_dir, img.name, USERIMAGE_OPTIONS.fs_type, "userdata"] |
| 77 | else: |
| 78 | build_command = ["mkyaffs2image", "-f", |
| 79 | user_dir, img.name] |
| 80 | |
| 81 | p = common.Run(build_command); |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 82 | p.communicate() |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 83 | assert p.returncode == 0, "build userdata.img image failed" |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 84 | |
| 85 | common.CheckSize(img.name, "userdata.img") |
| 86 | output_zip.write(img.name, "userdata.img") |
| 87 | img.close() |
| 88 | os.rmdir(user_dir) |
| 89 | os.rmdir(temp_dir) |
| 90 | |
| 91 | |
| 92 | def AddSystem(output_zip): |
| 93 | """Turn the contents of SYSTEM into a system image and store it in |
| 94 | output_zip.""" |
| 95 | |
| 96 | print "creating system.img..." |
| 97 | |
| 98 | img = tempfile.NamedTemporaryFile() |
| 99 | |
| 100 | # The name of the directory it is making an image out of matters to |
| 101 | # mkyaffs2image. It wants "system" but we have a directory named |
| 102 | # "SYSTEM", so create a symlink. |
Mike Ritter | e44fade | 2009-09-15 11:18:31 -0700 | [diff] [blame] | 103 | try: |
| 104 | os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"), |
| 105 | os.path.join(OPTIONS.input_tmp, "system")) |
| 106 | except OSError, e: |
| 107 | # bogus error on my mac version? |
| 108 | # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem |
| 109 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 110 | # OSError: [Errno 17] File exists |
| 111 | if (e.errno == errno.EEXIST): |
| 112 | pass |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 113 | |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 114 | build_command = [] |
| 115 | if USERIMAGE_OPTIONS.fs_type is not None and USERIMAGE_OPTIONS.fs_type.startswith("ext"): |
| 116 | build_command = ["mkuserimg.sh", |
| 117 | os.path.join(OPTIONS.input_tmp, "system"), img.name, |
| 118 | USERIMAGE_OPTIONS.fs_type, "system"] |
| 119 | else: |
| 120 | build_command = ["mkyaffs2image", "-f", |
| 121 | os.path.join(OPTIONS.input_tmp, "system"), img.name] |
| 122 | |
| 123 | p = common.Run(build_command) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 124 | p.communicate() |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 125 | assert p.returncode == 0, "build system.img image failed" |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 126 | |
| 127 | img.seek(os.SEEK_SET, 0) |
| 128 | data = img.read() |
| 129 | img.close() |
| 130 | |
| 131 | common.CheckSize(data, "system.img") |
Doug Zongker | 048e7ca | 2009-06-15 14:31:53 -0700 | [diff] [blame] | 132 | common.ZipWriteStr(output_zip, "system.img", data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 133 | |
| 134 | |
| 135 | def 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 | |
| 141 | def main(argv): |
| 142 | |
| 143 | def option_handler(o, a): |
| 144 | if o in ("-b", "--board_config"): |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 145 | pass # deprecated |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 146 | elif o in ("-f", "--fs_type"): |
| 147 | USERIMAGE_OPTIONS.fs_type = a |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 148 | else: |
| 149 | return False |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 150 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 151 | |
| 152 | args = common.ParseOptions(argv, __doc__, |
Ying Wang | 933abf1 | 2010-06-16 14:31:34 -0700 | [diff] [blame^] | 153 | extra_opts="b:f:", |
| 154 | extra_long_opts=["board_config=", "fs_type="], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 155 | extra_option_handler=option_handler) |
| 156 | |
| 157 | if len(args) != 2: |
| 158 | common.Usage(__doc__) |
| 159 | sys.exit(1) |
| 160 | |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 161 | OPTIONS.input_tmp = common.UnzipTemp(args[0]) |
| 162 | |
| 163 | common.LoadMaxSizes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 164 | if not OPTIONS.max_image_size: |
| 165 | print |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 166 | print " WARNING: Failed to load max image sizes; will not enforce" |
| 167 | print " image size limits." |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 168 | print |
| 169 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 170 | output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) |
| 171 | |
| 172 | common.AddBoot(output_zip) |
| 173 | common.AddRecovery(output_zip) |
| 174 | AddSystem(output_zip) |
| 175 | AddUserdata(output_zip) |
| 176 | CopyInfo(output_zip) |
| 177 | |
| 178 | print "cleaning up..." |
| 179 | output_zip.close() |
| 180 | shutil.rmtree(OPTIONS.input_tmp) |
| 181 | |
| 182 | print "done." |
| 183 | |
| 184 | |
| 185 | if __name__ == '__main__': |
| 186 | try: |
| 187 | main(sys.argv[1:]) |
| 188 | except common.ExternalError, e: |
| 189 | print |
| 190 | print " ERROR: %s" % (e,) |
| 191 | print |
| 192 | sys.exit(1) |