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 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 26 | -z (--bootable_zip) |
| 27 | Include only the bootable images (eg 'boot' and 'recovery') in |
| 28 | the output. |
| 29 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 30 | """ |
| 31 | |
| 32 | import sys |
| 33 | |
Doug Zongker | cf6d5a9 | 2014-02-18 10:57:07 -0800 | [diff] [blame^] | 34 | if sys.hexversion < 0x02070000: |
| 35 | print >> sys.stderr, "Python 2.7 or newer is required." |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 36 | sys.exit(1) |
| 37 | |
Mike Ritter | e44fade | 2009-09-15 11:18:31 -0700 | [diff] [blame] | 38 | import errno |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 39 | import os |
| 40 | import re |
| 41 | import shutil |
| 42 | import subprocess |
| 43 | import tempfile |
| 44 | import zipfile |
| 45 | |
| 46 | # missing in Python 2.4 and before |
| 47 | if not hasattr(os, "SEEK_SET"): |
| 48 | os.SEEK_SET = 0 |
| 49 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 50 | import build_image |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 51 | import common |
| 52 | |
| 53 | OPTIONS = common.OPTIONS |
| 54 | |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 55 | |
Doug Zongker | 01ce19c | 2014-02-04 13:48:15 -0800 | [diff] [blame] | 56 | def AddSystem(output_zip, sparse=True): |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 57 | """Turn the contents of SYSTEM into a system image and store it in |
| 58 | output_zip.""" |
| 59 | |
| 60 | print "creating system.img..." |
| 61 | |
| 62 | img = tempfile.NamedTemporaryFile() |
| 63 | |
| 64 | # The name of the directory it is making an image out of matters to |
| 65 | # mkyaffs2image. It wants "system" but we have a directory named |
| 66 | # "SYSTEM", so create a symlink. |
| 67 | try: |
| 68 | os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"), |
| 69 | os.path.join(OPTIONS.input_tmp, "system")) |
| 70 | except OSError, e: |
| 71 | # bogus error on my mac version? |
| 72 | # File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem |
| 73 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 74 | # OSError: [Errno 17] File exists |
| 75 | if (e.errno == errno.EEXIST): |
| 76 | pass |
| 77 | |
| 78 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, |
| 79 | "system") |
| 80 | fstab = OPTIONS.info_dict["fstab"] |
| 81 | if fstab: |
| 82 | image_props["fs_type" ] = fstab["/system"].fs_type |
| 83 | succ = build_image.BuildImage(os.path.join(OPTIONS.input_tmp, "system"), |
| 84 | image_props, img.name) |
| 85 | assert succ, "build system.img image failed" |
| 86 | |
Doug Zongker | 01ce19c | 2014-02-04 13:48:15 -0800 | [diff] [blame] | 87 | if sparse: |
| 88 | img.seek(os.SEEK_SET, 0) |
| 89 | data = img.read() |
| 90 | img.close() |
| 91 | else: |
| 92 | success, name = build_image.UnsparseImage(img.name, replace=False) |
| 93 | if not success: |
| 94 | assert False, "unsparsing system.img failed" |
| 95 | try: |
| 96 | with open(name) as f: |
| 97 | data = f.read() |
| 98 | finally: |
| 99 | os.unlink(name) |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 100 | |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 101 | common.ZipWriteStr(output_zip, "system.img", data) |
| 102 | |
| 103 | |
| 104 | def AddVendor(output_zip): |
| 105 | """Turn the contents of VENDOR into vendor.img and store it in |
| 106 | output_zip.""" |
| 107 | |
| 108 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, |
| 109 | "vendor") |
| 110 | # The build system has to explicitly request for vendor.img. |
| 111 | if "fs_type" not in image_props: |
| 112 | return |
| 113 | |
| 114 | print "creating vendor.img..." |
| 115 | |
| 116 | img = tempfile.NamedTemporaryFile() |
| 117 | |
| 118 | # The name of the directory it is making an image out of matters to |
| 119 | # mkyaffs2image. It wants "vendor" but we have a directory named |
| 120 | # "VENDOR", so create a symlink or an empty directory if VENDOR does not |
| 121 | # exist. |
| 122 | if not os.path.exists(os.path.join(OPTIONS.input_tmp, "vendor")): |
| 123 | if os.path.exists(os.path.join(OPTIONS.input_tmp, "VENDOR")): |
| 124 | os.symlink(os.path.join(OPTIONS.input_tmp, "VENDOR"), |
| 125 | os.path.join(OPTIONS.input_tmp, "vendor")) |
| 126 | else: |
| 127 | os.mkdir(os.path.join(OPTIONS.input_tmp, "vendor")) |
| 128 | |
| 129 | img = tempfile.NamedTemporaryFile() |
| 130 | |
| 131 | fstab = OPTIONS.info_dict["fstab"] |
| 132 | if fstab: |
| 133 | image_props["fs_type" ] = fstab["/vendor"].fs_type |
| 134 | succ = build_image.BuildImage(os.path.join(OPTIONS.input_tmp, "vendor"), |
| 135 | image_props, img.name) |
| 136 | assert succ, "build vendor.img image failed" |
| 137 | |
| 138 | common.CheckSize(img.name, "vendor.img", OPTIONS.info_dict) |
| 139 | output_zip.write(img.name, "vendor.img") |
| 140 | img.close() |
| 141 | |
| 142 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 143 | def AddUserdata(output_zip): |
| 144 | """Create an empty userdata image and store it in output_zip.""" |
| 145 | |
Ying Wang | 4e3f44f | 2012-11-19 10:26:00 -0800 | [diff] [blame] | 146 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, |
| 147 | "data") |
Ying Wang | d7321d3 | 2013-03-15 10:32:29 -0700 | [diff] [blame] | 148 | # If no userdata_size is provided for extfs, skip userdata.img. |
| 149 | if (image_props.get("fs_type", "").startswith("ext") and |
| 150 | not image_props.get("partition_size")): |
Ying Wang | 4e3f44f | 2012-11-19 10:26:00 -0800 | [diff] [blame] | 151 | return |
| 152 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 153 | print "creating userdata.img..." |
| 154 | |
| 155 | # The name of the directory it is making an image out of matters to |
| 156 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 157 | # empty dir named "data", and build the image from that. |
| 158 | temp_dir = tempfile.mkdtemp() |
| 159 | user_dir = os.path.join(temp_dir, "data") |
| 160 | os.mkdir(user_dir) |
| 161 | img = tempfile.NamedTemporaryFile() |
| 162 | |
Doug Zongker | 33a4b08 | 2010-09-21 16:12:55 -0700 | [diff] [blame] | 163 | fstab = OPTIONS.info_dict["fstab"] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 164 | if fstab: |
| 165 | image_props["fs_type" ] = fstab["/data"].fs_type |
| 166 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 167 | assert succ, "build userdata.img image failed" |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 168 | |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 169 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 170 | output_zip.write(img.name, "userdata.img") |
| 171 | img.close() |
| 172 | os.rmdir(user_dir) |
| 173 | os.rmdir(temp_dir) |
| 174 | |
| 175 | |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 176 | def AddCache(output_zip): |
| 177 | """Create an empty cache image and store it in output_zip.""" |
| 178 | |
| 179 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, |
| 180 | "cache") |
| 181 | # The build system has to explicitly request for cache.img. |
| 182 | if "fs_type" not in image_props: |
| 183 | return |
| 184 | |
| 185 | print "creating cache.img..." |
| 186 | |
| 187 | # The name of the directory it is making an image out of matters to |
| 188 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 189 | # empty dir named "cache", and build the image from that. |
| 190 | temp_dir = tempfile.mkdtemp() |
| 191 | user_dir = os.path.join(temp_dir, "cache") |
| 192 | os.mkdir(user_dir) |
| 193 | img = tempfile.NamedTemporaryFile() |
| 194 | |
| 195 | fstab = OPTIONS.info_dict["fstab"] |
| 196 | if fstab: |
| 197 | image_props["fs_type" ] = fstab["/cache"].fs_type |
| 198 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 199 | assert succ, "build cache.img image failed" |
| 200 | |
| 201 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
| 202 | output_zip.write(img.name, "cache.img") |
| 203 | img.close() |
| 204 | os.rmdir(user_dir) |
| 205 | os.rmdir(temp_dir) |
| 206 | |
| 207 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 208 | def CopyInfo(output_zip): |
| 209 | """Copy the android-info.txt file from the input to the output.""" |
| 210 | output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), |
| 211 | "android-info.txt") |
| 212 | |
| 213 | |
| 214 | def main(argv): |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 215 | bootable_only = [False] |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 216 | |
| 217 | def option_handler(o, a): |
| 218 | if o in ("-b", "--board_config"): |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 219 | pass # deprecated |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 220 | if o in ("-z", "--bootable_zip"): |
| 221 | bootable_only[0] = True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 222 | else: |
| 223 | return False |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 224 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 225 | |
| 226 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 227 | extra_opts="b:z", |
| 228 | extra_long_opts=["board_config=", |
| 229 | "bootable_zip"], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 230 | extra_option_handler=option_handler) |
| 231 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 232 | bootable_only = bootable_only[0] |
| 233 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 234 | if len(args) != 2: |
| 235 | common.Usage(__doc__) |
| 236 | sys.exit(1) |
| 237 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 238 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 239 | OPTIONS.info_dict = common.LoadInfoDict(input_zip) |
Ying Wang | d421f57 | 2010-08-25 20:39:41 -0700 | [diff] [blame] | 240 | |
Kenny Root | e2e9f61 | 2013-05-29 12:59:35 -0700 | [diff] [blame] | 241 | # If this image was originally labelled with SELinux contexts, make sure we |
| 242 | # also apply the labels in our new image. During building, the "file_contexts" |
| 243 | # is in the out/ directory tree, but for repacking from target-files.zip it's |
| 244 | # in the root directory of the ramdisk. |
| 245 | if "selinux_fc" in OPTIONS.info_dict: |
| 246 | OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK", |
| 247 | "file_contexts") |
| 248 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 249 | output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) |
| 250 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 251 | common.GetBootableImage( |
| 252 | "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT").AddToZip(output_zip) |
| 253 | common.GetBootableImage( |
| 254 | "recovery.img", "recovery.img", OPTIONS.input_tmp, |
| 255 | "RECOVERY").AddToZip(output_zip) |
| 256 | |
| 257 | if not bootable_only: |
| 258 | AddSystem(output_zip) |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 259 | AddVendor(output_zip) |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 260 | AddUserdata(output_zip) |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 261 | AddCache(output_zip) |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 262 | CopyInfo(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 263 | |
| 264 | print "cleaning up..." |
| 265 | output_zip.close() |
| 266 | shutil.rmtree(OPTIONS.input_tmp) |
| 267 | |
| 268 | print "done." |
| 269 | |
| 270 | |
| 271 | if __name__ == '__main__': |
| 272 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 273 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 274 | main(sys.argv[1:]) |
| 275 | except common.ExternalError, e: |
| 276 | print |
| 277 | print " ERROR: %s" % (e,) |
| 278 | print |
| 279 | sys.exit(1) |