blob: fd98ad2d65ce9507d8a324e285366989c30aded8 [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
Tao Bao89fbb0f2017-01-10 10:47:58 -080029from __future__ import print_function
30
Doug Zongkereef39442009-04-02 12:14:19 -070031import sys
32
Doug Zongkercf6d5a92014-02-18 10:57:07 -080033if sys.hexversion < 0x02070000:
Tao Bao89fbb0f2017-01-10 10:47:58 -080034 print("Python 2.7 or newer is required.", file=sys.stderr)
Doug Zongkereef39442009-04-02 12:14:19 -070035 sys.exit(1)
36
37import os
Doug Zongkereef39442009-04-02 12:14:19 -070038import shutil
Doug Zongkereef39442009-04-02 12:14:19 -070039import zipfile
40
Doug Zongkereef39442009-04-02 12:14:19 -070041import common
42
43OPTIONS = common.OPTIONS
44
Ying Wanga0febe52013-03-20 11:02:05 -070045
Doug Zongkereef39442009-04-02 12:14:19 -070046def CopyInfo(output_zip):
47 """Copy the android-info.txt file from the input to the output."""
Tao Bao2ed665a2015-04-01 11:21:55 -070048 common.ZipWrite(
49 output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
50 "android-info.txt")
Doug Zongkereef39442009-04-02 12:14:19 -070051
52
53def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -080054 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -070055
Dan Albert8b72aef2015-03-23 19:13:21 -070056 def option_handler(o, _):
Doug Zongker55d93282011-01-25 17:03:34 -080057 if o in ("-z", "--bootable_zip"):
58 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -070059 else:
60 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -070061 return True
Doug Zongkereef39442009-04-02 12:14:19 -070062
63 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -070064 extra_opts="z",
65 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -070066 extra_option_handler=option_handler)
67
Doug Zongker55d93282011-01-25 17:03:34 -080068 bootable_only = bootable_only[0]
69
Doug Zongkereef39442009-04-02 12:14:19 -070070 if len(args) != 2:
71 common.Usage(__doc__)
72 sys.exit(1)
73
Doug Zongker55d93282011-01-25 17:03:34 -080074 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongkereef39442009-04-02 12:14:19 -070075 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -070076 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070077
Doug Zongker3c84f562014-07-31 11:06:30 -070078 try:
79 done = False
80 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
81 if os.path.exists(images_path):
82 # If this is a new target-files, it already contains the images,
83 # and all we have to do is copy them to the output zip.
84 images = os.listdir(images_path)
85 if images:
Dan Albert8e0178d2015-01-27 15:53:15 -080086 for image in images:
87 if bootable_only and image not in ("boot.img", "recovery.img"):
88 continue
89 if not image.endswith(".img"):
90 continue
Tao Baod42e97e2016-11-30 12:11:57 -080091 if image == "recovery-two-step.img":
92 continue
Dan Albert8e0178d2015-01-27 15:53:15 -080093 common.ZipWrite(
94 output_zip, os.path.join(images_path, image), image)
Doug Zongker3c84f562014-07-31 11:06:30 -070095 done = True
Doug Zongker55d93282011-01-25 17:03:34 -080096
Doug Zongker3c84f562014-07-31 11:06:30 -070097 if not done:
98 # We have an old target-files that doesn't already contain the
99 # images, so build them.
100 import add_img_to_target_files
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700101
Tao Bao2c15d9e2015-07-09 11:51:16 -0700102 OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
Doug Zongker3c84f562014-07-31 11:06:30 -0700103
104 boot_image = common.GetBootableImage(
105 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
106 if boot_image:
Dan Albert8b72aef2015-03-23 19:13:21 -0700107 boot_image.AddToZip(output_zip)
Tao Bao7a5bf8a2015-07-21 18:01:20 -0700108
Tao Baodb45efa2015-10-27 19:25:18 -0700109 if OPTIONS.info_dict.get("no_recovery") != "true":
110 recovery_image = common.GetBootableImage(
111 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
112 if recovery_image:
113 recovery_image.AddToZip(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700114
115 def banner(s):
Tao Bao89fbb0f2017-01-10 10:47:58 -0800116 print("\n\n++++ " + s + " ++++\n\n")
Doug Zongker3c84f562014-07-31 11:06:30 -0700117
118 if not bootable_only:
119 banner("AddSystem")
120 add_img_to_target_files.AddSystem(output_zip, prefix="")
121 try:
122 input_zip.getinfo("VENDOR/")
123 banner("AddVendor")
124 add_img_to_target_files.AddVendor(output_zip, prefix="")
125 except KeyError:
126 pass # no vendor partition for this device
127 banner("AddUserdata")
128 add_img_to_target_files.AddUserdata(output_zip, prefix="")
129 banner("AddCache")
130 add_img_to_target_files.AddCache(output_zip, prefix="")
131
132 finally:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800133 print("cleaning up...")
Tao Bao2ed665a2015-04-01 11:21:55 -0700134 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -0700135 shutil.rmtree(OPTIONS.input_tmp)
Doug Zongkereef39442009-04-02 12:14:19 -0700136
Tao Bao89fbb0f2017-01-10 10:47:58 -0800137 print("done.")
Doug Zongkereef39442009-04-02 12:14:19 -0700138
139
140if __name__ == '__main__':
141 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800142 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700143 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700144 except common.ExternalError as e:
Tao Bao89fbb0f2017-01-10 10:47:58 -0800145 print("\n ERROR: %s\n" % (e,))
Doug Zongkereef39442009-04-02 12:14:19 -0700146 sys.exit(1)