Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 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 | """ |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame^] | 18 | Given target-files, produces an image zipfile suitable for use |
| 19 | with 'fastboot update'. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 20 | |
| 21 | Usage: img_from_target_files [flags] input_target_files output_image_zip |
| 22 | |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame^] | 23 | input_target_files: one of the following: |
| 24 | - directory containing extracted target files. It will load info from |
| 25 | OTA/android-info.txt and build the image zipfile using images from IMAGES/. |
| 26 | - target files package. Same as above, but extracts the archive before |
| 27 | building the image zipfile. |
| 28 | |
| 29 | Flags: |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 30 | -z (--bootable_zip) |
| 31 | Include only the bootable images (eg 'boot' and 'recovery') in |
| 32 | the output. |
| 33 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 34 | """ |
| 35 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 36 | from __future__ import print_function |
| 37 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 38 | import logging |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 39 | import os |
| 40 | import shutil |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 41 | import sys |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 42 | import zipfile |
| 43 | |
| 44 | import common |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 45 | |
Doug Zongker | cf6d5a9 | 2014-02-18 10:57:07 -0800 | [diff] [blame] | 46 | if sys.hexversion < 0x02070000: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 47 | print("Python 2.7 or newer is required.", file=sys.stderr) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 48 | sys.exit(1) |
| 49 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 50 | logger = logging.getLogger(__name__) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 51 | |
| 52 | OPTIONS = common.OPTIONS |
| 53 | |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 54 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 55 | def CopyInfo(output_zip): |
| 56 | """Copy the android-info.txt file from the input to the output.""" |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 57 | common.ZipWrite( |
| 58 | output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), |
| 59 | "android-info.txt") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 60 | |
| 61 | |
| 62 | def main(argv): |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 63 | # This allows modifying the value from inner function. |
| 64 | bootable_only_array = [False] |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 65 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 66 | def option_handler(o, _): |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 67 | if o in ("-z", "--bootable_zip"): |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 68 | bootable_only_array[0] = True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 69 | else: |
| 70 | return False |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 71 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 72 | |
| 73 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 74 | extra_opts="z", |
| 75 | extra_long_opts=["bootable_zip"], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 76 | extra_option_handler=option_handler) |
| 77 | |
Tao Bao | 76def24 | 2017-11-21 09:25:31 -0800 | [diff] [blame] | 78 | bootable_only = bootable_only_array[0] |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 79 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 80 | if len(args) != 2: |
| 81 | common.Usage(__doc__) |
| 82 | sys.exit(1) |
| 83 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 84 | common.InitLogging() |
| 85 | |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame^] | 86 | target_files = args[0] |
| 87 | if os.path.isdir(target_files): |
| 88 | logger.info("Building image zip from extracted target files.") |
| 89 | OPTIONS.input_tmp = target_files |
| 90 | elif zipfile.is_zipfile(target_files): |
| 91 | logger.info("Building image zip from target files zip.") |
| 92 | OPTIONS.input_tmp = common.UnzipTemp(args[0], ["IMAGES/*", "OTA/*"]) |
| 93 | else: |
| 94 | raise ValueError("%s is not a valid path." % target_files) |
| 95 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 96 | output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 97 | CopyInfo(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 98 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 99 | try: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 100 | images_path = os.path.join(OPTIONS.input_tmp, "IMAGES") |
Tao Bao | 2bb1097 | 2017-05-31 10:37:48 -0700 | [diff] [blame] | 101 | # A target-files zip must contain the images since Lollipop. |
| 102 | assert os.path.exists(images_path) |
| 103 | for image in sorted(os.listdir(images_path)): |
| 104 | if bootable_only and image not in ("boot.img", "recovery.img"): |
| 105 | continue |
| 106 | if not image.endswith(".img"): |
| 107 | continue |
| 108 | if image == "recovery-two-step.img": |
| 109 | continue |
| 110 | common.ZipWrite(output_zip, os.path.join(images_path, image), image) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 111 | |
| 112 | finally: |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 113 | logger.info("cleaning up...") |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 114 | common.ZipClose(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 115 | |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 116 | logger.info("done.") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | if __name__ == '__main__': |
| 120 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 121 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 122 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 123 | except common.ExternalError as e: |
Tao Bao | 32fcdab | 2018-10-12 10:30:39 -0700 | [diff] [blame] | 124 | logger.exception("\n ERROR:\n") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 125 | sys.exit(1) |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 126 | finally: |
| 127 | common.Cleanup() |