blob: 0156b72e17e854b0b254c38bc223f037aa14e304 [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
Tao Bao32fcdab2018-10-12 10:30:39 -070031import logging
Tao Bao76def242017-11-21 09:25:31 -080032import os
33import shutil
Doug Zongkereef39442009-04-02 12:14:19 -070034import sys
Tao Bao76def242017-11-21 09:25:31 -080035import zipfile
36
37import common
Doug Zongkereef39442009-04-02 12:14:19 -070038
Doug Zongkercf6d5a92014-02-18 10:57:07 -080039if sys.hexversion < 0x02070000:
Tao Bao89fbb0f2017-01-10 10:47:58 -080040 print("Python 2.7 or newer is required.", file=sys.stderr)
Doug Zongkereef39442009-04-02 12:14:19 -070041 sys.exit(1)
42
Tao Bao32fcdab2018-10-12 10:30:39 -070043logger = logging.getLogger(__name__)
Doug Zongkereef39442009-04-02 12:14:19 -070044
45OPTIONS = common.OPTIONS
46
Ying Wanga0febe52013-03-20 11:02:05 -070047
Doug Zongkereef39442009-04-02 12:14:19 -070048def CopyInfo(output_zip):
49 """Copy the android-info.txt file from the input to the output."""
Tao Bao2ed665a2015-04-01 11:21:55 -070050 common.ZipWrite(
51 output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
52 "android-info.txt")
Doug Zongkereef39442009-04-02 12:14:19 -070053
54
55def main(argv):
Tao Bao76def242017-11-21 09:25:31 -080056 # This allows modifying the value from inner function.
57 bootable_only_array = [False]
Doug Zongkereef39442009-04-02 12:14:19 -070058
Dan Albert8b72aef2015-03-23 19:13:21 -070059 def option_handler(o, _):
Doug Zongker55d93282011-01-25 17:03:34 -080060 if o in ("-z", "--bootable_zip"):
Tao Bao76def242017-11-21 09:25:31 -080061 bootable_only_array[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -070062 else:
63 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -070064 return True
Doug Zongkereef39442009-04-02 12:14:19 -070065
66 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -070067 extra_opts="z",
68 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -070069 extra_option_handler=option_handler)
70
Tao Bao76def242017-11-21 09:25:31 -080071 bootable_only = bootable_only_array[0]
Doug Zongker55d93282011-01-25 17:03:34 -080072
Doug Zongkereef39442009-04-02 12:14:19 -070073 if len(args) != 2:
74 common.Usage(__doc__)
75 sys.exit(1)
76
Tao Bao32fcdab2018-10-12 10:30:39 -070077 common.InitLogging()
78
Tao Baodba59ee2018-01-09 13:21:02 -080079 OPTIONS.input_tmp = common.UnzipTemp(args[0], ["IMAGES/*", "OTA/*"])
Doug Zongkereef39442009-04-02 12:14:19 -070080 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -070081 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070082
Doug Zongker3c84f562014-07-31 11:06:30 -070083 try:
Doug Zongker3c84f562014-07-31 11:06:30 -070084 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
Tao Bao2bb10972017-05-31 10:37:48 -070085 # A target-files zip must contain the images since Lollipop.
86 assert os.path.exists(images_path)
87 for image in sorted(os.listdir(images_path)):
88 if bootable_only and image not in ("boot.img", "recovery.img"):
89 continue
90 if not image.endswith(".img"):
91 continue
92 if image == "recovery-two-step.img":
93 continue
94 common.ZipWrite(output_zip, os.path.join(images_path, image), image)
Doug Zongker3c84f562014-07-31 11:06:30 -070095
96 finally:
Tao Bao32fcdab2018-10-12 10:30:39 -070097 logger.info("cleaning up...")
Tao Bao2ed665a2015-04-01 11:21:55 -070098 common.ZipClose(output_zip)
Doug Zongker3c84f562014-07-31 11:06:30 -070099 shutil.rmtree(OPTIONS.input_tmp)
Doug Zongkereef39442009-04-02 12:14:19 -0700100
Tao Bao32fcdab2018-10-12 10:30:39 -0700101 logger.info("done.")
Doug Zongkereef39442009-04-02 12:14:19 -0700102
103
104if __name__ == '__main__':
105 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800106 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700107 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700108 except common.ExternalError as e:
Tao Bao32fcdab2018-10-12 10:30:39 -0700109 logger.exception("\n ERROR:\n")
Doug Zongkereef39442009-04-02 12:14:19 -0700110 sys.exit(1)