img_from_target_files.py revision 36bd365625e6beba77698a93795a603180a5b476
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 < 0x02040000: 35 print >> sys.stderr, "Python 2.4 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): 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 succ = build_image.BuildImage(os.path.join(input_dir, "system"), 87 image_props, img.name) 88 assert succ, "build system.img image failed" 89 90 if sparse: 91 img.seek(os.SEEK_SET, 0) 92 data = img.read() 93 img.close() 94 else: 95 success, name = build_image.UnsparseImage(img.name, replace=False) 96 if not success: 97 assert False, "unsparsing system.img failed" 98 try: 99 with open(name) as f: 100 data = f.read() 101 finally: 102 os.unlink(name) 103 104 return data 105 106 107def AddVendor(output_zip): 108 """Turn the contents of VENDOR into vendor.img and store it in 109 output_zip.""" 110 111 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, 112 "vendor") 113 # The build system has to explicitly request for vendor.img. 114 if "fs_type" not in image_props: 115 return 116 117 print "creating vendor.img..." 118 119 img = tempfile.NamedTemporaryFile() 120 121 # The name of the directory it is making an image out of matters to 122 # mkyaffs2image. It wants "vendor" but we have a directory named 123 # "VENDOR", so create a symlink or an empty directory if VENDOR does not 124 # exist. 125 if not os.path.exists(os.path.join(OPTIONS.input_tmp, "vendor")): 126 if os.path.exists(os.path.join(OPTIONS.input_tmp, "VENDOR")): 127 os.symlink(os.path.join(OPTIONS.input_tmp, "VENDOR"), 128 os.path.join(OPTIONS.input_tmp, "vendor")) 129 else: 130 os.mkdir(os.path.join(OPTIONS.input_tmp, "vendor")) 131 132 img = tempfile.NamedTemporaryFile() 133 134 fstab = OPTIONS.info_dict["fstab"] 135 if fstab: 136 image_props["fs_type" ] = fstab["/vendor"].fs_type 137 succ = build_image.BuildImage(os.path.join(OPTIONS.input_tmp, "vendor"), 138 image_props, img.name) 139 assert succ, "build vendor.img image failed" 140 141 common.CheckSize(img.name, "vendor.img", OPTIONS.info_dict) 142 output_zip.write(img.name, "vendor.img") 143 img.close() 144 145 146def AddUserdata(output_zip): 147 """Create an empty userdata image and store it in output_zip.""" 148 149 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, 150 "data") 151 # If no userdata_size is provided for extfs, skip userdata.img. 152 if (image_props.get("fs_type", "").startswith("ext") and 153 not image_props.get("partition_size")): 154 return 155 156 print "creating userdata.img..." 157 158 # The name of the directory it is making an image out of matters to 159 # mkyaffs2image. So we create a temp dir, and within it we create an 160 # empty dir named "data", and build the image from that. 161 temp_dir = tempfile.mkdtemp() 162 user_dir = os.path.join(temp_dir, "data") 163 os.mkdir(user_dir) 164 img = tempfile.NamedTemporaryFile() 165 166 fstab = OPTIONS.info_dict["fstab"] 167 if fstab: 168 image_props["fs_type" ] = fstab["/data"].fs_type 169 succ = build_image.BuildImage(user_dir, image_props, img.name) 170 assert succ, "build userdata.img image failed" 171 172 common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) 173 output_zip.write(img.name, "userdata.img") 174 img.close() 175 os.rmdir(user_dir) 176 os.rmdir(temp_dir) 177 178 179def AddCache(output_zip): 180 """Create an empty cache image and store it in output_zip.""" 181 182 image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, 183 "cache") 184 # The build system has to explicitly request for cache.img. 185 if "fs_type" not in image_props: 186 return 187 188 print "creating cache.img..." 189 190 # The name of the directory it is making an image out of matters to 191 # mkyaffs2image. So we create a temp dir, and within it we create an 192 # empty dir named "cache", and build the image from that. 193 temp_dir = tempfile.mkdtemp() 194 user_dir = os.path.join(temp_dir, "cache") 195 os.mkdir(user_dir) 196 img = tempfile.NamedTemporaryFile() 197 198 fstab = OPTIONS.info_dict["fstab"] 199 if fstab: 200 image_props["fs_type" ] = fstab["/cache"].fs_type 201 succ = build_image.BuildImage(user_dir, image_props, img.name) 202 assert succ, "build cache.img image failed" 203 204 common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) 205 output_zip.write(img.name, "cache.img") 206 img.close() 207 os.rmdir(user_dir) 208 os.rmdir(temp_dir) 209 210 211def CopyInfo(output_zip): 212 """Copy the android-info.txt file from the input to the output.""" 213 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), 214 "android-info.txt") 215 216 217def main(argv): 218 bootable_only = [False] 219 220 def option_handler(o, a): 221 if o in ("-b", "--board_config"): 222 pass # deprecated 223 if o in ("-z", "--bootable_zip"): 224 bootable_only[0] = True 225 else: 226 return False 227 return True 228 229 args = common.ParseOptions(argv, __doc__, 230 extra_opts="b:z", 231 extra_long_opts=["board_config=", 232 "bootable_zip"], 233 extra_option_handler=option_handler) 234 235 bootable_only = bootable_only[0] 236 237 if len(args) != 2: 238 common.Usage(__doc__) 239 sys.exit(1) 240 241 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) 242 OPTIONS.info_dict = common.LoadInfoDict(input_zip) 243 244 # If this image was originally labelled with SELinux contexts, make sure we 245 # also apply the labels in our new image. During building, the "file_contexts" 246 # is in the out/ directory tree, but for repacking from target-files.zip it's 247 # in the root directory of the ramdisk. 248 if "selinux_fc" in OPTIONS.info_dict: 249 OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK", 250 "file_contexts") 251 252 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) 253 254 common.GetBootableImage( 255 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT").AddToZip(output_zip) 256 common.GetBootableImage( 257 "recovery.img", "recovery.img", OPTIONS.input_tmp, 258 "RECOVERY").AddToZip(output_zip) 259 260 if not bootable_only: 261 AddSystem(output_zip) 262 AddVendor(output_zip) 263 AddUserdata(output_zip) 264 AddCache(output_zip) 265 CopyInfo(output_zip) 266 267 print "cleaning up..." 268 output_zip.close() 269 shutil.rmtree(OPTIONS.input_tmp) 270 271 print "done." 272 273 274if __name__ == '__main__': 275 try: 276 common.CloseInheritedPipes() 277 main(sys.argv[1:]) 278 except common.ExternalError, e: 279 print 280 print " ERROR: %s" % (e,) 281 print 282 sys.exit(1) 283