blob: ce5808f0406b447d900343b6edb69345cb097690 [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."""
Tao Bao2ed665a2015-04-01 11:21:55 -070046 common.ZipWrite(
47 output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
48 "android-info.txt")
Doug Zongkereef39442009-04-02 12:14:19 -070049
50
51def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -080052 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -070053
Dan Albert8b72aef2015-03-23 19:13:21 -070054 def option_handler(o, _):
Doug Zongker55d93282011-01-25 17:03:34 -080055 if o in ("-z", "--bootable_zip"):
56 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -070057 else:
58 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -070059 return True
Doug Zongkereef39442009-04-02 12:14:19 -070060
61 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -070062 extra_opts="z",
63 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -070064 extra_option_handler=option_handler)
65
Doug Zongker55d93282011-01-25 17:03:34 -080066 bootable_only = bootable_only[0]
67
Doug Zongkereef39442009-04-02 12:14:19 -070068 if len(args) != 2:
69 common.Usage(__doc__)
70 sys.exit(1)
71
Doug Zongker55d93282011-01-25 17:03:34 -080072 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongkereef39442009-04-02 12:14:19 -070073 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -070074 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070075
Doug Zongker3c84f562014-07-31 11:06:30 -070076 try:
77 done = False
78 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
79 if os.path.exists(images_path):
80 # If this is a new target-files, it already contains the images,
81 # and all we have to do is copy them to the output zip.
82 images = os.listdir(images_path)
83 if images:
Dan Albert8e0178d2015-01-27 15:53:15 -080084 for image in images:
85 if bootable_only and image not in ("boot.img", "recovery.img"):
86 continue
87 if not image.endswith(".img"):
88 continue
89 common.ZipWrite(
90 output_zip, os.path.join(images_path, image), image)
Doug Zongker3c84f562014-07-31 11:06:30 -070091 done = True
Doug Zongker55d93282011-01-25 17:03:34 -080092
Doug Zongker3c84f562014-07-31 11:06:30 -070093 if not done:
94 # We have an old target-files that doesn't already contain the
95 # images, so build them.
96 import add_img_to_target_files
Doug Zongkerc8b4e842014-06-16 15:16:31 -070097
Tao Bao2c15d9e2015-07-09 11:51:16 -070098 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -070099
100 boot_image = common.GetBootableImage(
101 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
102 if boot_image:
Dan Albert8b72aef2015-03-23 19:13:21 -0700103 boot_image.AddToZip(output_zip)
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700104
Doug Zongker3c84f562014-07-31 11:06:30 -0700105 recovery_image = common.GetBootableImage(
106 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
107 if recovery_image:
108 recovery_image.AddToZip(output_zip)
109
110 def banner(s):
111 print "\n\n++++ " + s + " ++++\n\n"
112
113 if not bootable_only:
114 banner("AddSystem")
115 add_img_to_target_files.AddSystem(output_zip, prefix="")
116 try:
117 input_zip.getinfo("VENDOR/")
118 banner("AddVendor")
119 add_img_to_target_files.AddVendor(output_zip, prefix="")
120 except KeyError:
121 pass # no vendor partition for this device
122 banner("AddUserdata")
123 add_img_to_target_files.AddUserdata(output_zip, prefix="")
124 banner("AddCache")
125 add_img_to_target_files.AddCache(output_zip, prefix="")
126
127 finally:
128 print "cleaning up..."
Tao Bao2ed665a2015-04-01 11:21:55 -0700129 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700130 shutil.rmtree(OPTIONS.input_tmp)
Doug Zongkereef39442009-04-02 12:14:19 -0700131
132 print "done."
133
134
135if __name__ == '__main__':
136 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800137 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700138 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700139 except common.ExternalError as e:
Doug Zongkereef39442009-04-02 12:14:19 -0700140 print
141 print " ERROR: %s" % (e,)
142 print
143 sys.exit(1)