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 | """ |
| 18 | Given a target-files zipfile, produces an image zipfile suitable for |
| 19 | use with 'fastboot update'. |
| 20 | |
| 21 | Usage: img_from_target_files [flags] input_target_files output_image_zip |
| 22 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 23 | -z (--bootable_zip) |
| 24 | Include only the bootable images (eg 'boot' and 'recovery') in |
| 25 | the output. |
| 26 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 27 | """ |
| 28 | |
| 29 | import sys |
| 30 | |
Doug Zongker | cf6d5a9 | 2014-02-18 10:57:07 -0800 | [diff] [blame] | 31 | if sys.hexversion < 0x02070000: |
| 32 | print >> sys.stderr, "Python 2.7 or newer is required." |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 33 | sys.exit(1) |
| 34 | |
| 35 | import os |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 36 | import shutil |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 37 | import zipfile |
| 38 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 39 | import common |
| 40 | |
| 41 | OPTIONS = common.OPTIONS |
| 42 | |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 43 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 44 | def CopyInfo(output_zip): |
| 45 | """Copy the android-info.txt file from the input to the output.""" |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 46 | common.ZipWrite( |
| 47 | output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), |
| 48 | "android-info.txt") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def main(argv): |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 52 | bootable_only = [False] |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 53 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 54 | def option_handler(o, _): |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 55 | if o in ("-z", "--bootable_zip"): |
| 56 | bootable_only[0] = True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 57 | else: |
| 58 | return False |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 59 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 60 | |
| 61 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 62 | extra_opts="z", |
| 63 | extra_long_opts=["bootable_zip"], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 64 | extra_option_handler=option_handler) |
| 65 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 66 | bootable_only = bootable_only[0] |
| 67 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 68 | if len(args) != 2: |
| 69 | common.Usage(__doc__) |
| 70 | sys.exit(1) |
| 71 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 72 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 73 | output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 74 | CopyInfo(output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 75 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 76 | 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 Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 84 | 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 Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 91 | done = True |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 92 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 93 | 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 Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 97 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 98 | OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 99 | |
| 100 | boot_image = common.GetBootableImage( |
| 101 | "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 102 | if boot_image: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 103 | boot_image.AddToZip(output_zip) |
Tao Bao | 7a5bf8a | 2015-07-21 18:01:20 -0700 | [diff] [blame^] | 104 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 105 | 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 Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 129 | common.ZipClose(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 130 | shutil.rmtree(OPTIONS.input_tmp) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 131 | |
| 132 | print "done." |
| 133 | |
| 134 | |
| 135 | if __name__ == '__main__': |
| 136 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 137 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 138 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 139 | except common.ExternalError as e: |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 140 | print |
| 141 | print " ERROR: %s" % (e,) |
| 142 | print |
| 143 | sys.exit(1) |