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