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