Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2014 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 that does not contain images (ie, does |
| 19 | not have an IMAGES/ top-level subdirectory), produce the images and |
| 20 | add them to the zipfile. |
| 21 | |
| 22 | Usage: add_img_to_target_files target_files |
| 23 | """ |
| 24 | |
| 25 | import sys |
| 26 | |
| 27 | if sys.hexversion < 0x02070000: |
| 28 | print >> sys.stderr, "Python 2.7 or newer is required." |
| 29 | sys.exit(1) |
| 30 | |
| 31 | import errno |
| 32 | import os |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 33 | import tempfile |
| 34 | import zipfile |
| 35 | |
| 36 | # missing in Python 2.4 and before |
| 37 | if not hasattr(os, "SEEK_SET"): |
| 38 | os.SEEK_SET = 0 |
| 39 | |
| 40 | import build_image |
| 41 | import common |
| 42 | |
| 43 | OPTIONS = common.OPTIONS |
| 44 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 45 | OPTIONS.add_missing = False |
| 46 | OPTIONS.rebuild_recovery = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 47 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 48 | def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 49 | """Turn the contents of SYSTEM into a system image and store it in |
| 50 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 51 | |
| 52 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img") |
| 53 | if os.path.exists(prebuilt_path): |
| 54 | print "system.img already exists in %s, no need to rebuild..." % (prefix,) |
| 55 | return |
| 56 | |
| 57 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 58 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 59 | ofile.write(data) |
| 60 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 61 | |
| 62 | if OPTIONS.rebuild_recovery: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 63 | print "Building new recovery patch" |
| 64 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 65 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 66 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 67 | block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map") |
| 68 | imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, |
| 69 | block_list=block_list) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 70 | common.ZipWrite(output_zip, imgname, prefix + "system.img") |
| 71 | common.ZipWrite(output_zip, block_list, prefix + "system.map") |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def BuildSystem(input_dir, info_dict, block_list=None): |
| 75 | """Build the (sparse) system image and return the name of a temp |
| 76 | file containing it.""" |
| 77 | return CreateImage(input_dir, info_dict, "system", block_list=block_list) |
| 78 | |
| 79 | |
| 80 | def AddVendor(output_zip, prefix="IMAGES/"): |
| 81 | """Turn the contents of VENDOR into a vendor image and store in it |
| 82 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 83 | |
| 84 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img") |
| 85 | if os.path.exists(prebuilt_path): |
| 86 | print "vendor.img already exists in %s, no need to rebuild..." % (prefix,) |
| 87 | return |
| 88 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 89 | block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map") |
| 90 | imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 91 | block_list=block_list) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 92 | common.ZipWrite(output_zip, imgname, prefix + "vendor.img") |
| 93 | common.ZipWrite(output_zip, block_list, prefix + "vendor.map") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 94 | |
| 95 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 96 | def BuildVendor(input_dir, info_dict, block_list=None): |
| 97 | """Build the (sparse) vendor image and return the name of a temp |
| 98 | file containing it.""" |
| 99 | return CreateImage(input_dir, info_dict, "vendor", block_list=block_list) |
| 100 | |
| 101 | |
| 102 | def CreateImage(input_dir, info_dict, what, block_list=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 103 | print "creating " + what + ".img..." |
| 104 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 105 | img = common.MakeTempFile(prefix=what + "-", suffix=".img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 106 | |
| 107 | # The name of the directory it is making an image out of matters to |
| 108 | # mkyaffs2image. It wants "system" but we have a directory named |
| 109 | # "SYSTEM", so create a symlink. |
| 110 | try: |
| 111 | os.symlink(os.path.join(input_dir, what.upper()), |
| 112 | os.path.join(input_dir, what)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 113 | except OSError as e: |
| 114 | # bogus error on my mac version? |
| 115 | # File "./build/tools/releasetools/img_from_target_files" |
| 116 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 117 | # OSError: [Errno 17] File exists |
| 118 | if e.errno == errno.EEXIST: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 119 | pass |
| 120 | |
| 121 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 122 | fstab = info_dict["fstab"] |
| 123 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 124 | image_props["fs_type"] = fstab["/" + what].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 125 | |
| 126 | if what == "system": |
| 127 | fs_config_prefix = "" |
| 128 | else: |
| 129 | fs_config_prefix = what + "_" |
| 130 | |
| 131 | fs_config = os.path.join( |
| 132 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 133 | if not os.path.exists(fs_config): |
| 134 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 135 | |
| 136 | fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 137 | if not os.path.exists(fc_config): |
| 138 | fc_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 139 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 140 | # Override values loaded from info_dict. |
| 141 | if fs_config: |
| 142 | image_props["fs_config"] = fs_config |
| 143 | if fc_config: |
| 144 | image_props["selinux_fc"] = fc_config |
| 145 | if block_list: |
| 146 | image_props["block_list"] = block_list |
| 147 | if image_props.get("system_root_image") == "true": |
| 148 | image_props["ramdisk_dir"] = os.path.join(input_dir, "BOOT/RAMDISK") |
| 149 | image_props["ramdisk_fs_config"] = os.path.join( |
| 150 | input_dir, "META/boot_filesystem_config.txt") |
| 151 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 152 | succ = build_image.BuildImage(os.path.join(input_dir, what), |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 153 | image_props, img) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 154 | assert succ, "build " + what + ".img image failed" |
| 155 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 156 | return img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 157 | |
| 158 | |
| 159 | def AddUserdata(output_zip, prefix="IMAGES/"): |
| 160 | """Create an empty userdata image and store it in output_zip.""" |
| 161 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 162 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img") |
| 163 | if os.path.exists(prebuilt_path): |
| 164 | print "userdata.img already exists in %s, no need to rebuild..." % (prefix,) |
| 165 | return |
| 166 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 167 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, |
| 168 | "data") |
| 169 | # We only allow yaffs to have a 0/missing partition_size. |
| 170 | # Extfs, f2fs must have a size. Skip userdata.img if no size. |
| 171 | if (not image_props.get("fs_type", "").startswith("yaffs") and |
| 172 | not image_props.get("partition_size")): |
| 173 | return |
| 174 | |
| 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 | |
| 185 | fstab = OPTIONS.info_dict["fstab"] |
| 186 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 187 | image_props["fs_type"] = fstab["/data"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 188 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 189 | assert succ, "build userdata.img image failed" |
| 190 | |
| 191 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
| 192 | output_zip.write(img.name, prefix + "userdata.img") |
| 193 | img.close() |
| 194 | os.rmdir(user_dir) |
| 195 | os.rmdir(temp_dir) |
| 196 | |
| 197 | |
| 198 | def AddCache(output_zip, prefix="IMAGES/"): |
| 199 | """Create an empty cache image and store it in output_zip.""" |
| 200 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 201 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img") |
| 202 | if os.path.exists(prebuilt_path): |
| 203 | print "cache.img already exists in %s, no need to rebuild..." % (prefix,) |
| 204 | return |
| 205 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 206 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, |
| 207 | "cache") |
| 208 | # The build system has to explicitly request for cache.img. |
| 209 | if "fs_type" not in image_props: |
| 210 | return |
| 211 | |
| 212 | print "creating cache.img..." |
| 213 | |
| 214 | # The name of the directory it is making an image out of matters to |
| 215 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 216 | # empty dir named "cache", and build the image from that. |
| 217 | temp_dir = tempfile.mkdtemp() |
| 218 | user_dir = os.path.join(temp_dir, "cache") |
| 219 | os.mkdir(user_dir) |
| 220 | img = tempfile.NamedTemporaryFile() |
| 221 | |
| 222 | fstab = OPTIONS.info_dict["fstab"] |
| 223 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 224 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 225 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 226 | assert succ, "build cache.img image failed" |
| 227 | |
| 228 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
| 229 | output_zip.write(img.name, prefix + "cache.img") |
| 230 | img.close() |
| 231 | os.rmdir(user_dir) |
| 232 | os.rmdir(temp_dir) |
| 233 | |
| 234 | |
| 235 | def AddImagesToTargetFiles(filename): |
| 236 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 237 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 238 | if not OPTIONS.add_missing: |
| 239 | for n in input_zip.namelist(): |
| 240 | if n.startswith("IMAGES/"): |
| 241 | print "target_files appears to already contain images." |
| 242 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 243 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 244 | try: |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 245 | input_zip.getinfo("VENDOR/") |
| 246 | has_vendor = True |
| 247 | except KeyError: |
| 248 | has_vendor = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 249 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 250 | OPTIONS.info_dict = common.LoadInfoDict(input_zip) |
| 251 | if "selinux_fc" in OPTIONS.info_dict: |
| 252 | OPTIONS.info_dict["selinux_fc"] = os.path.join( |
| 253 | OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 254 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 255 | input_zip.close() |
| 256 | output_zip = zipfile.ZipFile(filename, "a", |
| 257 | compression=zipfile.ZIP_DEFLATED) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 258 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 259 | def banner(s): |
| 260 | print "\n\n++++ " + s + " ++++\n\n" |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 261 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 262 | banner("boot") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 263 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 264 | boot_image = None |
| 265 | if os.path.exists(prebuilt_path): |
| 266 | print "boot.img already exists in IMAGES/, no need to rebuild..." |
| 267 | if OPTIONS.rebuild_recovery: |
| 268 | boot_image = common.GetBootableImage( |
| 269 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 270 | else: |
| 271 | boot_image = common.GetBootableImage( |
| 272 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 273 | if boot_image: |
| 274 | boot_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 275 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 276 | banner("recovery") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 277 | recovery_image = None |
| 278 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") |
| 279 | if os.path.exists(prebuilt_path): |
| 280 | print "recovery.img already exists in IMAGES/, no need to rebuild..." |
| 281 | if OPTIONS.rebuild_recovery: |
| 282 | recovery_image = common.GetBootableImage( |
| 283 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
| 284 | else: |
| 285 | recovery_image = common.GetBootableImage( |
| 286 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
| 287 | if recovery_image: |
| 288 | recovery_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 289 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 290 | banner("system") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 291 | AddSystem(output_zip, recovery_img=recovery_image, boot_img=boot_image) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 292 | if has_vendor: |
| 293 | banner("vendor") |
| 294 | AddVendor(output_zip) |
| 295 | banner("userdata") |
| 296 | AddUserdata(output_zip) |
| 297 | banner("cache") |
| 298 | AddCache(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 299 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 300 | output_zip.close() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 301 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 302 | def main(argv): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 303 | def option_handler(o, _): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 304 | if o in ("-a", "--add_missing"): |
| 305 | OPTIONS.add_missing = True |
| 306 | elif o in ("-r", "--rebuild_recovery",): |
| 307 | OPTIONS.rebuild_recovery = True |
| 308 | else: |
| 309 | return False |
| 310 | return True |
| 311 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 312 | args = common.ParseOptions( |
| 313 | argv, __doc__, extra_opts="ar", |
| 314 | extra_long_opts=["add_missing", "rebuild_recovery"], |
| 315 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 316 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 317 | |
| 318 | if len(args) != 1: |
| 319 | common.Usage(__doc__) |
| 320 | sys.exit(1) |
| 321 | |
| 322 | AddImagesToTargetFiles(args[0]) |
| 323 | print "done." |
| 324 | |
| 325 | if __name__ == '__main__': |
| 326 | try: |
| 327 | common.CloseInheritedPipes() |
| 328 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 329 | except common.ExternalError as e: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 330 | print |
| 331 | print " ERROR: %s" % (e,) |
| 332 | print |
| 333 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 334 | finally: |
| 335 | common.Cleanup() |