blob: 8c5acd882cbe87533c61367d2d65e2e84c43f457 [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001#!/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
Doug Zongker55d93282011-01-25 17:03:34 -080023 -z (--bootable_zip)
24 Include only the bootable images (eg 'boot' and 'recovery') in
25 the output.
26
Doug Zongkereef39442009-04-02 12:14:19 -070027"""
28
29import sys
30
Doug Zongkercf6d5a92014-02-18 10:57:07 -080031if sys.hexversion < 0x02070000:
32 print >> sys.stderr, "Python 2.7 or newer is required."
Doug Zongkereef39442009-04-02 12:14:19 -070033 sys.exit(1)
34
35import os
Doug Zongkereef39442009-04-02 12:14:19 -070036import shutil
Doug Zongkereef39442009-04-02 12:14:19 -070037import zipfile
38
Doug Zongkereef39442009-04-02 12:14:19 -070039import common
40
41OPTIONS = common.OPTIONS
42
Ying Wanga0febe52013-03-20 11:02:05 -070043
Doug Zongkereef39442009-04-02 12:14:19 -070044def CopyInfo(output_zip):
45 """Copy the android-info.txt file from the input to the output."""
46 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
47 "android-info.txt")
48
49
50def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -080051 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -070052
Dan Albert8b72aef2015-03-23 19:13:21 -070053 def option_handler(o, _):
Doug Zongker55d93282011-01-25 17:03:34 -080054 if o in ("-z", "--bootable_zip"):
55 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -070056 else:
57 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -070058 return True
Doug Zongkereef39442009-04-02 12:14:19 -070059
60 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -070061 extra_opts="z",
62 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -070063 extra_option_handler=option_handler)
64
Doug Zongker55d93282011-01-25 17:03:34 -080065 bootable_only = bootable_only[0]
66
Doug Zongkereef39442009-04-02 12:14:19 -070067 if len(args) != 2:
68 common.Usage(__doc__)
69 sys.exit(1)
70
Doug Zongker55d93282011-01-25 17:03:34 -080071 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongkereef39442009-04-02 12:14:19 -070072 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -070073 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070074
Doug Zongker3c84f562014-07-31 11:06:30 -070075 try:
76 done = False
77 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
78 if os.path.exists(images_path):
79 # If this is a new target-files, it already contains the images,
80 # and all we have to do is copy them to the output zip.
81 images = os.listdir(images_path)
82 if images:
Dan Albert8e0178d2015-01-27 15:53:15 -080083 for image in images:
84 if bootable_only and image not in ("boot.img", "recovery.img"):
85 continue
86 if not image.endswith(".img"):
87 continue
88 common.ZipWrite(
89 output_zip, os.path.join(images_path, image), image)
Doug Zongker3c84f562014-07-31 11:06:30 -070090 done = True
Doug Zongker55d93282011-01-25 17:03:34 -080091
Doug Zongker3c84f562014-07-31 11:06:30 -070092 if not done:
93 # We have an old target-files that doesn't already contain the
94 # images, so build them.
95 import add_img_to_target_files
Doug Zongkerc8b4e842014-06-16 15:16:31 -070096
Doug Zongker3c84f562014-07-31 11:06:30 -070097 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070098
Doug Zongker3c84f562014-07-31 11:06:30 -070099 # If this image was originally labelled with SELinux contexts,
100 # make sure we also apply the labels in our new image. During
101 # building, the "file_contexts" is in the out/ directory tree,
102 # but for repacking from target-files.zip it's in the root
103 # directory of the ramdisk.
104 if "selinux_fc" in OPTIONS.info_dict:
105 OPTIONS.info_dict["selinux_fc"] = os.path.join(
106 OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts")
107
108 boot_image = common.GetBootableImage(
109 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
110 if boot_image:
Dan Albert8b72aef2015-03-23 19:13:21 -0700111 boot_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700112 recovery_image = common.GetBootableImage(
113 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
114 if recovery_image:
115 recovery_image.AddToZip(output_zip)
116
117 def banner(s):
118 print "\n\n++++ " + s + " ++++\n\n"
119
120 if not bootable_only:
121 banner("AddSystem")
122 add_img_to_target_files.AddSystem(output_zip, prefix="")
123 try:
124 input_zip.getinfo("VENDOR/")
125 banner("AddVendor")
126 add_img_to_target_files.AddVendor(output_zip, prefix="")
127 except KeyError:
128 pass # no vendor partition for this device
129 banner("AddUserdata")
130 add_img_to_target_files.AddUserdata(output_zip, prefix="")
131 banner("AddCache")
132 add_img_to_target_files.AddCache(output_zip, prefix="")
133
134 finally:
135 print "cleaning up..."
Greg Hackmann6701db82015-03-23 14:05:50 -0700136 # http://b/18015246
137 # See common.py for context. zipfile also refers to ZIP64_LIMIT during
138 # close() when it writes out the central directory.
139 saved_zip64_limit = zipfile.ZIP64_LIMIT
140 zipfile.ZIP64_LIMIT = (1 << 32) - 1
Doug Zongker3c84f562014-07-31 11:06:30 -0700141 output_zip.close()
Greg Hackmann6701db82015-03-23 14:05:50 -0700142 zipfile.ZIP64_LIMIT = saved_zip64_limit
Doug Zongker3c84f562014-07-31 11:06:30 -0700143 shutil.rmtree(OPTIONS.input_tmp)
Doug Zongkereef39442009-04-02 12:14:19 -0700144
145 print "done."
146
147
148if __name__ == '__main__':
149 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800150 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700151 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700152 except common.ExternalError as e:
Doug Zongkereef39442009-04-02 12:14:19 -0700153 print
154 print " ERROR: %s" % (e,)
155 print
156 sys.exit(1)