add_img_to_target_files.py revision a6a3aa9398f6a27693521abd2601462a78a81c56
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""" 18Given a target-files zipfile that does not contain images (ie, does 19not have an IMAGES/ top-level subdirectory), produce the images and 20add them to the zipfile. 21 22Usage: add_img_to_target_files target_files 23""" 24 25import sys 26 27if sys.hexversion < 0x02070000: 28 print >> sys.stderr, "Python 2.7 or newer is required." 29 sys.exit(1) 30 31import datetime 32import errno 33import os 34import tempfile 35import zipfile 36 37import build_image 38import common 39 40OPTIONS = common.OPTIONS 41 42OPTIONS.add_missing = False 43OPTIONS.rebuild_recovery = False 44OPTIONS.replace_verity_public_key = False 45OPTIONS.replace_verity_private_key = False 46OPTIONS.verity_signer_path = None 47 48def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): 49 """Turn the contents of SYSTEM into a system image and store it in 50 output_zip.""" 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): 58 ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") 59 ofile.write(data) 60 ofile.close() 61 62 if OPTIONS.rebuild_recovery: 63 print "Building new recovery patch" 64 common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, 65 boot_img, info_dict=OPTIONS.info_dict) 66 67 block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map") 68 imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, 69 block_list=block_list) 70 common.ZipWrite(output_zip, imgname, prefix + "system.img") 71 common.ZipWrite(output_zip, block_list, prefix + "system.map") 72 73 74def 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 80def AddVendor(output_zip, prefix="IMAGES/"): 81 """Turn the contents of VENDOR into a vendor image and store in it 82 output_zip.""" 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 89 block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map") 90 imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, 91 block_list=block_list) 92 common.ZipWrite(output_zip, imgname, prefix + "vendor.img") 93 common.ZipWrite(output_zip, block_list, prefix + "vendor.map") 94 95 96def 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 102def CreateImage(input_dir, info_dict, what, block_list=None): 103 print "creating " + what + ".img..." 104 105 img = common.MakeTempFile(prefix=what + "-", suffix=".img") 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)) 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: 119 pass 120 121 image_props = build_image.ImagePropFromGlobalDict(info_dict, what) 122 fstab = info_dict["fstab"] 123 if fstab: 124 image_props["fs_type"] = fstab["/" + what].fs_type 125 126 # Use a fixed timestamp (01/01/2009) when packaging the image. 127 # Bug: 24377993 128 epoch = datetime.datetime.fromtimestamp(0) 129 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() 130 image_props["timestamp"] = int(timestamp) 131 132 if what == "system": 133 fs_config_prefix = "" 134 else: 135 fs_config_prefix = what + "_" 136 137 fs_config = os.path.join( 138 input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") 139 if not os.path.exists(fs_config): 140 fs_config = None 141 142 # Override values loaded from info_dict. 143 if fs_config: 144 image_props["fs_config"] = fs_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 152 succ = build_image.BuildImage(os.path.join(input_dir, what), 153 image_props, img) 154 assert succ, "build " + what + ".img image failed" 155 156 return img 157 158 159def AddUserdata(output_zip, prefix="IMAGES/"): 160 """Create an empty userdata image and store it in output_zip.""" 161 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 167 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") 168 # We only allow yaffs to have a 0/missing partition_size. 169 # Extfs, f2fs must have a size. Skip userdata.img if no size. 170 if (not image_props.get("fs_type", "").startswith("yaffs") and 171 not image_props.get("partition_size")): 172 return 173 174 print "creating userdata.img..." 175 176 # Use a fixed timestamp (01/01/2009) when packaging the image. 177 # Bug: 24377993 178 epoch = datetime.datetime.fromtimestamp(0) 179 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() 180 image_props["timestamp"] = int(timestamp) 181 182 # The name of the directory it is making an image out of matters to 183 # mkyaffs2image. So we create a temp dir, and within it we create an 184 # empty dir named "data", and build the image from that. 185 temp_dir = tempfile.mkdtemp() 186 user_dir = os.path.join(temp_dir, "data") 187 os.mkdir(user_dir) 188 img = tempfile.NamedTemporaryFile() 189 190 fstab = OPTIONS.info_dict["fstab"] 191 if fstab: 192 image_props["fs_type"] = fstab["/data"].fs_type 193 succ = build_image.BuildImage(user_dir, image_props, img.name) 194 assert succ, "build userdata.img image failed" 195 196 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) 197 common.ZipWrite(output_zip, img.name, prefix + "userdata.img") 198 img.close() 199 os.rmdir(user_dir) 200 os.rmdir(temp_dir) 201 202 203def AddCache(output_zip, prefix="IMAGES/"): 204 """Create an empty cache image and store it in output_zip.""" 205 206 prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img") 207 if os.path.exists(prebuilt_path): 208 print "cache.img already exists in %s, no need to rebuild..." % (prefix,) 209 return 210 211 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") 212 # The build system has to explicitly request for cache.img. 213 if "fs_type" not in image_props: 214 return 215 216 print "creating cache.img..." 217 218 # Use a fixed timestamp (01/01/2009) when packaging the image. 219 # Bug: 24377993 220 epoch = datetime.datetime.fromtimestamp(0) 221 timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() 222 image_props["timestamp"] = int(timestamp) 223 224 # The name of the directory it is making an image out of matters to 225 # mkyaffs2image. So we create a temp dir, and within it we create an 226 # empty dir named "cache", and build the image from that. 227 temp_dir = tempfile.mkdtemp() 228 user_dir = os.path.join(temp_dir, "cache") 229 os.mkdir(user_dir) 230 img = tempfile.NamedTemporaryFile() 231 232 fstab = OPTIONS.info_dict["fstab"] 233 if fstab: 234 image_props["fs_type"] = fstab["/cache"].fs_type 235 succ = build_image.BuildImage(user_dir, image_props, img.name) 236 assert succ, "build cache.img image failed" 237 238 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) 239 common.ZipWrite(output_zip, img.name, prefix + "cache.img") 240 img.close() 241 os.rmdir(user_dir) 242 os.rmdir(temp_dir) 243 244 245def AddImagesToTargetFiles(filename): 246 OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) 247 248 if not OPTIONS.add_missing: 249 for n in input_zip.namelist(): 250 if n.startswith("IMAGES/"): 251 print "target_files appears to already contain images." 252 sys.exit(1) 253 254 try: 255 input_zip.getinfo("VENDOR/") 256 has_vendor = True 257 except KeyError: 258 has_vendor = False 259 260 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) 261 262 common.ZipClose(input_zip) 263 output_zip = zipfile.ZipFile(filename, "a", 264 compression=zipfile.ZIP_DEFLATED) 265 266 has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") 267 268 def banner(s): 269 print "\n\n++++ " + s + " ++++\n\n" 270 271 banner("boot") 272 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") 273 boot_image = None 274 if os.path.exists(prebuilt_path): 275 print "boot.img already exists in IMAGES/, no need to rebuild..." 276 if OPTIONS.rebuild_recovery: 277 boot_image = common.GetBootableImage( 278 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 279 else: 280 boot_image = common.GetBootableImage( 281 "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 282 if boot_image: 283 boot_image.AddToZip(output_zip) 284 285 recovery_image = None 286 if has_recovery: 287 banner("recovery") 288 prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") 289 if os.path.exists(prebuilt_path): 290 print "recovery.img already exists in IMAGES/, no need to rebuild..." 291 if OPTIONS.rebuild_recovery: 292 recovery_image = common.GetBootableImage( 293 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, 294 "RECOVERY") 295 else: 296 recovery_image = common.GetBootableImage( 297 "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") 298 if recovery_image: 299 recovery_image.AddToZip(output_zip) 300 301 banner("system") 302 AddSystem(output_zip, recovery_img=recovery_image, boot_img=boot_image) 303 if has_vendor: 304 banner("vendor") 305 AddVendor(output_zip) 306 banner("userdata") 307 AddUserdata(output_zip) 308 banner("cache") 309 AddCache(output_zip) 310 311 # For devices using A/B update, copy over images from RADIO/ to IMAGES/ and 312 # make sure we have all the needed images ready under IMAGES/. 313 ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") 314 if os.path.exists(ab_partitions): 315 with open(ab_partitions, 'r') as f: 316 lines = f.readlines() 317 for line in lines: 318 img_name = line.strip() + ".img" 319 img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) 320 if os.path.exists(img_radio_path): 321 common.ZipWrite(output_zip, img_radio_path, 322 os.path.join("IMAGES", img_name)) 323 324 # Zip spec says: All slashes MUST be forward slashes. 325 img_path = 'IMAGES/' + img_name 326 assert img_path in output_zip.namelist(), "cannot find " + img_name 327 328 common.ZipClose(output_zip) 329 330def main(argv): 331 def option_handler(o, a): 332 if o in ("-a", "--add_missing"): 333 OPTIONS.add_missing = True 334 elif o in ("-r", "--rebuild_recovery",): 335 OPTIONS.rebuild_recovery = True 336 elif o == "--replace_verity_private_key": 337 OPTIONS.replace_verity_private_key = (True, a) 338 elif o == "--replace_verity_public_key": 339 OPTIONS.replace_verity_public_key = (True, a) 340 elif o == "--verity_signer_path": 341 OPTIONS.verity_signer_path = a 342 else: 343 return False 344 return True 345 346 args = common.ParseOptions( 347 argv, __doc__, extra_opts="ar", 348 extra_long_opts=["add_missing", "rebuild_recovery", 349 "replace_verity_public_key=", 350 "replace_verity_private_key=", 351 "verity_signer_path="], 352 extra_option_handler=option_handler) 353 354 355 if len(args) != 1: 356 common.Usage(__doc__) 357 sys.exit(1) 358 359 AddImagesToTargetFiles(args[0]) 360 print "done." 361 362if __name__ == '__main__': 363 try: 364 common.CloseInheritedPipes() 365 main(sys.argv[1:]) 366 except common.ExternalError as e: 367 print 368 print " ERROR: %s" % (e,) 369 print 370 sys.exit(1) 371 finally: 372 common.Cleanup() 373