img_from_target_files.py revision 8282282122a22f87c94862e97674bf48fd921d48
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""" 18Given a target-files zipfile, produces an image zipfile suitable for 19use with 'fastboot update'. 20 21Usage: img_from_target_files [flags] input_target_files output_image_zip 22 23 -b (--board_config) <file> 24 Deprecated. 25 26 -z (--bootable_zip) 27 Include only the bootable images (eg 'boot' and 'recovery') in 28 the output. 29 30""" 31 32import sys 33 34if sys.hexversion < 0x02070000: 35 print >> sys.stderr, "Python 2.7 or newer is required." 36 sys.exit(1) 37 38import errno 39import os 40import re 41import shutil 42import subprocess 43import tempfile 44import zipfile 45 46# missing in Python 2.4 and before 47if not hasattr(os, "SEEK_SET"): 48 os.SEEK_SET = 0 49 50import build_image 51import common 52 53OPTIONS = common.OPTIONS 54 55 56def AddSystem(output_zip, sparse=True): 57 """Turn the contents of SYSTEM into a system image and store it in 58 output_zip.""" 59 data = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, sparse=sparse) 60 common.ZipWriteStr(output_zip, "system.img", data) 61 62 63def BuildSystem(input_dir, info_dict, sparse=True, map_file=None): 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: 72 os.symlink(os.path.join(input_dir, "SYSTEM"), 73 os.path.join(input_dir, "system")) 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 82 image_props = build_image.ImagePropFromGlobalDict(info_dict, "system") 83 fstab = info_dict["fstab"] 84 if fstab: 85 image_props["fs_type" ] = fstab["/system"].fs_type 86 87 fs_config = os.path.join(input_dir, "META/filesystem_config.txt") 88 if not os.path.exists(fs_config): fs_config = None 89 90 fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts") 91 if not os.path.exists(fc_config): fc_config = None 92 93 succ = build_image.BuildImage(os.path.join(input_dir, "system"), 94 image_props, img.name, 95 fs_config=fs_config, 96 fc_config=fc_config) 97 assert succ, "build system.img image failed" 98 99 mapdata = None 100 101 if sparse: 102 data = open(img.name).read() 103 img.close() 104 else: 105 success, name = build_image.UnsparseImage(img.name, replace=False) 106 if not success: 107 assert False, "unsparsing system.img failed" 108 109 if map_file: 110 mmap = tempfile.NamedTemporaryFile() 111 mimg = tempfile.NamedTemporaryFile(delete=False) 112 success = build_image.MappedUnsparseImage( 113 img.name, name, mmap.name, mimg.name) 114 if not success: 115 assert False, "creating sparse map failed" 116 os.unlink(name) 117 name = mimg.name 118 119 with open(mmap.name) as f: 120 mapdata = f.read() 121 122 try: 123 with open(name) as f: 124 data = f.read() 125 finally: 126 os.unlink(name) 127 128 if mapdata is None: 129 return data 130 else: 131 return mapdata, data 132 133 134def AddVendor(output_zip): 135 """Turn the contents of VENDOR into vendor.img and store it in 136 output_zip.""" 137 138 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, 139 "vendor") 140 # The build system has to explicitly request for vendor.img. 141 if "fs_type" not in image_props: 142 return 143 144 print "creating vendor.img..." 145 146 img = tempfile.NamedTemporaryFile() 147 148 # The name of the directory it is making an image out of matters to 149 # mkyaffs2image. It wants "vendor" but we have a directory named 150 # "VENDOR", so create a symlink or an empty directory if VENDOR does not 151 # exist. 152 if not os.path.exists(os.path.join(OPTIONS.input_tmp, "vendor")): 153 if os.path.exists(os.path.join(OPTIONS.input_tmp, "VENDOR")): 154 os.symlink(os.path.join(OPTIONS.input_tmp, "VENDOR"), 155 os.path.join(OPTIONS.input_tmp, "vendor")) 156 else: 157 os.mkdir(os.path.join(OPTIONS.input_tmp, "vendor")) 158 159 img = tempfile.NamedTemporaryFile() 160 161 fstab = OPTIONS.info_dict["fstab"] 162 if fstab: 163 image_props["fs_type" ] = fstab["/vendor"].fs_type 164 succ = build_image.BuildImage(os.path.join(OPTIONS.input_tmp, "vendor"), 165 image_props, img.name) 166 assert succ, "build vendor.img image failed" 167 168 common.CheckSize(img.name, "vendor.img", OPTIONS.info_dict) 169 output_zip.write(img.name, "vendor.img") 170 img.close() 171 172 173def AddUserdata(output_zip): 174 """Create an empty userdata image and store it in output_zip.""" 175 176 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, 177 "data") 178 # If no userdata_size is provided for extfs, skip userdata.img. 179 if (image_props.get("fs_type", "").startswith("ext") and 180 not image_props.get("partition_size")): 181 return 182 183 print "creating userdata.img..." 184 185 # The name of the directory it is making an image out of matters to 186 # mkyaffs2image. So we create a temp dir, and within it we create an 187 # empty dir named "data", and build the image from that. 188 temp_dir = tempfile.mkdtemp() 189 user_dir = os.path.join(temp_dir, "data") 190 os.mkdir(user_dir) 191 img = tempfile.NamedTemporaryFile() 192 193 fstab = OPTIONS.info_dict["fstab"] 194 if fstab: 195 image_props["fs_type" ] = fstab["/data"].fs_type 196 succ = build_image.BuildImage(user_dir, image_props, img.name) 197 assert succ, "build userdata.img image failed" 198 199 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) 200 output_zip.write(img.name, "userdata.img") 201 img.close() 202 os.rmdir(user_dir) 203 os.rmdir(temp_dir) 204 205 206def AddCache(output_zip): 207 """Create an empty cache image and store it in output_zip.""" 208 209 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, 210 "cache") 211 # The build system has to explicitly request for cache.img. 212 if "fs_type" not in image_props: 213 return 214 215 print "creating cache.img..." 216 217 # The name of the directory it is making an image out of matters to 218 # mkyaffs2image. So we create a temp dir, and within it we create an 219 # empty dir named "cache", and build the image from that. 220 temp_dir = tempfile.mkdtemp() 221 user_dir = os.path.join(temp_dir, "cache") 222 os.mkdir(user_dir) 223 img = tempfile.NamedTemporaryFile() 224 225 fstab = OPTIONS.info_dict["fstab"] 226 if fstab: 227 image_props["fs_type" ] = fstab["/cache"].fs_type 228 succ = build_image.BuildImage(user_dir, image_props, img.name) 229 assert succ, "build cache.img image failed" 230 231 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) 232 output_zip.write(img.name, "cache.img") 233 img.close() 234 os.rmdir(user_dir) 235 os.rmdir(temp_dir) 236 237 238def CopyInfo(output_zip): 239 """Copy the android-info.txt file from the input to the output.""" 240 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), 241 "android-info.txt") 242 243 244def main(argv): 245 bootable_only = [False] 246 247 def option_handler(o, a): 248 if o in ("-b", "--board_config"): 249 pass # deprecated 250 if o in ("-z", "--bootable_zip"): 251 bootable_only[0] = True 252 else: 253 return False 254 return True 255 256 args = common.ParseOptions(argv, __doc__, 257 extra_opts="b:z", 258 extra_long_opts=["board_config=", 259 "bootable_zip"], 260 extra_option_handler=option_handler) 261 262 bootable_only = bootable_only[0] 263 264 if len(args) != 2: 265 common.Usage(__doc__) 266 sys.exit(1) 267 268 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) 269 OPTIONS.info_dict = common.LoadInfoDict(input_zip) 270 271 # If this image was originally labelled with SELinux contexts, make sure we 272 # also apply the labels in our new image. During building, the "file_contexts" 273 # is in the out/ directory tree, but for repacking from target-files.zip it's 274 # in the root directory of the ramdisk. 275 if "selinux_fc" in OPTIONS.info_dict: 276 OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK", 277 "file_contexts") 278 279 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) 280 281 boot_image = common.GetBootableImage( 282 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 283 if boot_image: 284 boot_image.AddToZip(output_zip) 285 recovery_image = common.GetBootableImage( 286 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") 287 if recovery_image: 288 recovery_image.AddToZip(output_zip) 289 290 if not bootable_only: 291 AddSystem(output_zip) 292 AddVendor(output_zip) 293 AddUserdata(output_zip) 294 AddCache(output_zip) 295 CopyInfo(output_zip) 296 297 print "cleaning up..." 298 output_zip.close() 299 shutil.rmtree(OPTIONS.input_tmp) 300 301 print "done." 302 303 304if __name__ == '__main__': 305 try: 306 common.CloseInheritedPipes() 307 main(sys.argv[1:]) 308 except common.ExternalError, e: 309 print 310 print " ERROR: %s" % (e,) 311 print 312 sys.exit(1) 313