Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2014 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 that does not contain images (ie, does |
| 19 | not have an IMAGES/ top-level subdirectory), produce the images and |
| 20 | add them to the zipfile. |
| 21 | |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 22 | Usage: add_img_to_target_files [flag] target_files |
| 23 | |
| 24 | -a (--add_missing) |
| 25 | Build and add missing images to "IMAGES/". If this option is |
| 26 | not specified, this script will simply exit when "IMAGES/" |
| 27 | directory exists in the target file. |
| 28 | |
| 29 | -r (--rebuild_recovery) |
| 30 | Rebuild the recovery patch and write it to the system image. Only |
| 31 | meaningful when system image needs to be rebuilt. |
| 32 | |
| 33 | --replace_verity_private_key |
| 34 | Replace the private key used for verity signing. (same as the option |
| 35 | in sign_target_files_apks) |
| 36 | |
| 37 | --replace_verity_public_key |
| 38 | Replace the certificate (public key) used for verity verification. (same |
| 39 | as the option in sign_target_files_apks) |
| 40 | |
| 41 | --is_signing |
| 42 | Skip building & adding the images for "userdata" and "cache" if we |
| 43 | are signing the target files. |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 44 | """ |
| 45 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 46 | from __future__ import print_function |
| 47 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 48 | import datetime |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 49 | import os |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 50 | import shlex |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 51 | import shutil |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 52 | import subprocess |
Tao Bao | 6b9fef5 | 2017-12-01 16:13:22 -0800 | [diff] [blame] | 53 | import sys |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 54 | import uuid |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 55 | import zipfile |
| 56 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 57 | import build_image |
| 58 | import common |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 59 | import rangelib |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 60 | import sparse_img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 61 | |
Tao Bao | 6b9fef5 | 2017-12-01 16:13:22 -0800 | [diff] [blame] | 62 | if sys.hexversion < 0x02070000: |
| 63 | print("Python 2.7 or newer is required.", file=sys.stderr) |
| 64 | sys.exit(1) |
| 65 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 66 | OPTIONS = common.OPTIONS |
| 67 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 68 | OPTIONS.add_missing = False |
| 69 | OPTIONS.rebuild_recovery = False |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 70 | OPTIONS.replace_updated_files_list = [] |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 71 | OPTIONS.replace_verity_public_key = False |
| 72 | OPTIONS.replace_verity_private_key = False |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 73 | OPTIONS.is_signing = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 74 | |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 75 | # Partitions that should have their care_map added to META/care_map.txt. |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 76 | PARTITIONS_WITH_CARE_MAP = ('system', 'vendor', 'product', 'product-services', |
| 77 | 'odm') |
Bryan Henry | e6d547d | 2018-07-31 18:32:00 -0700 | [diff] [blame] | 78 | # Use a fixed timestamp (01/01/2009 00:00:00 UTC) for files when packaging |
| 79 | # images. (b/24377993, b/80600931) |
| 80 | FIXED_FILE_TIMESTAMP = (datetime.datetime(2009, 1, 1, 0, 0, 0, 0, None) |
| 81 | - datetime.datetime.utcfromtimestamp(0)).total_seconds() |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 82 | |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 83 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 84 | class OutputFile(object): |
| 85 | def __init__(self, output_zip, input_dir, prefix, name): |
| 86 | self._output_zip = output_zip |
| 87 | self.input_name = os.path.join(input_dir, prefix, name) |
| 88 | |
| 89 | if self._output_zip: |
| 90 | self._zip_name = os.path.join(prefix, name) |
| 91 | |
| 92 | root, suffix = os.path.splitext(name) |
| 93 | self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix) |
| 94 | else: |
| 95 | self.name = self.input_name |
| 96 | |
| 97 | def Write(self): |
| 98 | if self._output_zip: |
| 99 | common.ZipWrite(self._output_zip, self.name, self._zip_name) |
| 100 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 101 | def GetCareMap(which, imgname): |
Tao Bao | 63c18fe | 2018-02-21 23:45:43 -0800 | [diff] [blame] | 102 | """Returns the care_map string for the given partition. |
| 103 | |
| 104 | Args: |
| 105 | which: The partition name, must be listed in PARTITIONS_WITH_CARE_MAP. |
| 106 | imgname: The filename of the image. |
| 107 | |
| 108 | Returns: |
| 109 | (which, care_map_ranges): care_map_ranges is the raw string of the care_map |
| 110 | RangeSet. |
| 111 | """ |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 112 | assert which in PARTITIONS_WITH_CARE_MAP |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 113 | |
| 114 | simg = sparse_img.SparseImage(imgname) |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 115 | care_map_ranges = simg.care_map |
| 116 | key = which + "_adjusted_partition_size" |
| 117 | adjusted_blocks = OPTIONS.info_dict.get(key) |
| 118 | if adjusted_blocks: |
| 119 | assert adjusted_blocks > 0, "blocks should be positive for " + which |
| 120 | care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet( |
| 121 | "0-%d" % (adjusted_blocks,))) |
| 122 | |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 123 | return [which, care_map_ranges.to_string_raw()] |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 124 | |
| 125 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 126 | def AddSystem(output_zip, recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 127 | """Turn the contents of SYSTEM into a system image and store it in |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 128 | output_zip. Returns the name of the system image file.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 129 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 130 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "system.img") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 131 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 132 | print("system.img already exists; no need to rebuild...") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 133 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 134 | |
| 135 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 136 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 137 | ofile.write(data) |
| 138 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 139 | |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 140 | arc_name = "SYSTEM/" + fn |
| 141 | if arc_name in output_zip.namelist(): |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 142 | OPTIONS.replace_updated_files_list.append(arc_name) |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 143 | else: |
| 144 | common.ZipWrite(output_zip, ofile.name, arc_name) |
| 145 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 146 | if OPTIONS.rebuild_recovery: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 147 | print("Building new recovery patch") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 148 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 149 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 150 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 151 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "system.map") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 152 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img, |
| 153 | block_list=block_list) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 154 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 155 | return img.name |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 156 | |
| 157 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 158 | def AddSystemOther(output_zip): |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 159 | """Turn the contents of SYSTEM_OTHER into a system_other image |
| 160 | and store it in output_zip.""" |
| 161 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 162 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "system_other.img") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 163 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 164 | print("system_other.img already exists; no need to rebuild...") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 165 | return |
| 166 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 167 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 168 | |
| 169 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 170 | def AddVendor(output_zip): |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 171 | """Turn the contents of VENDOR into a vendor image and store in it |
| 172 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 173 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 174 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "vendor.img") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 175 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 176 | print("vendor.img already exists; no need to rebuild...") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 177 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 178 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 179 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "vendor.map") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 180 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img, |
| 181 | block_list=block_list) |
| 182 | return img.name |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 183 | |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 184 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 185 | def AddProduct(output_zip): |
| 186 | """Turn the contents of PRODUCT into a product image and store it in |
| 187 | output_zip.""" |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 188 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 189 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "product.img") |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 190 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 191 | print("product.img already exists; no need to rebuild...") |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 192 | return img.input_name |
| 193 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 194 | block_list = OutputFile( |
| 195 | output_zip, OPTIONS.input_tmp, "IMAGES", "product.map") |
| 196 | CreateImage( |
| 197 | OPTIONS.input_tmp, OPTIONS.info_dict, "product", img, |
| 198 | block_list=block_list) |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 199 | return img.name |
| 200 | |
| 201 | |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 202 | def AddProductServices(output_zip): |
Dario Freni | 77c778c | 2018-08-17 00:28:17 +0100 | [diff] [blame] | 203 | """Turn the contents of PRODUCT_SERVICES into a product-services image and |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 204 | store it in output_zip.""" |
| 205 | |
| 206 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", |
| 207 | "product-services.img") |
| 208 | if os.path.exists(img.input_name): |
| 209 | print("product-services.img already exists; no need to rebuild...") |
| 210 | return img.input_name |
| 211 | |
| 212 | block_list = OutputFile( |
| 213 | output_zip, OPTIONS.input_tmp, "IMAGES", "product-services.map") |
| 214 | CreateImage( |
| 215 | OPTIONS.input_tmp, OPTIONS.info_dict, "product-services", img, |
| 216 | block_list=block_list) |
| 217 | return img.name |
| 218 | |
| 219 | |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 220 | def AddOdm(output_zip): |
| 221 | """Turn the contents of ODM into an odm image and store it in output_zip.""" |
| 222 | |
| 223 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "odm.img") |
| 224 | if os.path.exists(img.input_name): |
| 225 | print("odm.img already exists; no need to rebuild...") |
| 226 | return img.input_name |
| 227 | |
| 228 | block_list = OutputFile( |
| 229 | output_zip, OPTIONS.input_tmp, "IMAGES", "odm.map") |
| 230 | CreateImage( |
| 231 | OPTIONS.input_tmp, OPTIONS.info_dict, "odm", img, |
| 232 | block_list=block_list) |
| 233 | return img.name |
| 234 | |
| 235 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 236 | def AddDtbo(output_zip): |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 237 | """Adds the DTBO image. |
| 238 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 239 | Uses the image under IMAGES/ if it already exists. Otherwise looks for the |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 240 | image under PREBUILT_IMAGES/, signs it as needed, and returns the image name. |
| 241 | """ |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 242 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "dtbo.img") |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 243 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 244 | print("dtbo.img already exists; no need to rebuild...") |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 245 | return img.input_name |
| 246 | |
| 247 | dtbo_prebuilt_path = os.path.join( |
| 248 | OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img") |
| 249 | assert os.path.exists(dtbo_prebuilt_path) |
| 250 | shutil.copy(dtbo_prebuilt_path, img.name) |
| 251 | |
| 252 | # AVB-sign the image as needed. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 253 | if OPTIONS.info_dict.get("avb_enable") == "true": |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 254 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
Tao Bao | 3ebfdde | 2017-05-23 23:06:55 -0700 | [diff] [blame] | 255 | part_size = OPTIONS.info_dict["dtbo_size"] |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 256 | # The AVB hash footer will be replaced if already present. |
| 257 | cmd = [avbtool, "add_hash_footer", "--image", img.name, |
| 258 | "--partition_size", str(part_size), "--partition_name", "dtbo"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 259 | common.AppendAVBSigningArgs(cmd, "dtbo") |
| 260 | args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args") |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 261 | if args and args.strip(): |
| 262 | cmd.extend(shlex.split(args)) |
| 263 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 264 | p.communicate() |
| 265 | assert p.returncode == 0, \ |
| 266 | "avbtool add_hash_footer of %s failed" % (img.name,) |
| 267 | |
| 268 | img.Write() |
| 269 | return img.name |
| 270 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 271 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 272 | def CreateImage(input_dir, info_dict, what, output_file, block_list=None): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 273 | print("creating " + what + ".img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 274 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 275 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 276 | fstab = info_dict["fstab"] |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 277 | mount_point = "/" + what |
| 278 | if fstab and mount_point in fstab: |
| 279 | image_props["fs_type"] = fstab[mount_point].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 280 | |
Bryan Henry | e6d547d | 2018-07-31 18:32:00 -0700 | [diff] [blame] | 281 | image_props["timestamp"] = FIXED_FILE_TIMESTAMP |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 282 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 283 | if what == "system": |
| 284 | fs_config_prefix = "" |
| 285 | else: |
| 286 | fs_config_prefix = what + "_" |
| 287 | |
| 288 | fs_config = os.path.join( |
| 289 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 290 | if not os.path.exists(fs_config): |
| 291 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 292 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 293 | # Override values loaded from info_dict. |
| 294 | if fs_config: |
| 295 | image_props["fs_config"] = fs_config |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 296 | if block_list: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 297 | image_props["block_list"] = block_list.name |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 298 | |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 299 | # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and |
| 300 | # build fingerprint). |
| 301 | uuid_seed = what + "-" |
| 302 | if "build.prop" in info_dict: |
| 303 | build_prop = info_dict["build.prop"] |
| 304 | if "ro.build.fingerprint" in build_prop: |
| 305 | uuid_seed += build_prop["ro.build.fingerprint"] |
| 306 | elif "ro.build.thumbprint" in build_prop: |
| 307 | uuid_seed += build_prop["ro.build.thumbprint"] |
| 308 | image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed)) |
| 309 | hash_seed = "hash_seed-" + uuid_seed |
| 310 | image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed)) |
| 311 | |
Tao Bao | fa863c8 | 2017-05-23 23:49:03 -0700 | [diff] [blame] | 312 | succ = build_image.BuildImage(os.path.join(input_dir, what.upper()), |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 313 | image_props, output_file.name) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 314 | assert succ, "build " + what + ".img image failed" |
| 315 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 316 | output_file.Write() |
| 317 | if block_list: |
| 318 | block_list.Write() |
| 319 | |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 320 | # Set the 'adjusted_partition_size' that excludes the verity blocks of the |
| 321 | # given image. When avb is enabled, this size is the max image size returned |
| 322 | # by the avb tool. |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 323 | is_verity_partition = "verity_block_device" in image_props |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 324 | verity_supported = (image_props.get("verity") == "true" or |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 325 | image_props.get("avb_enable") == "true") |
Tianjie Xu | 6b2e155 | 2017-06-01 11:32:32 -0700 | [diff] [blame] | 326 | is_avb_enable = image_props.get("avb_hashtree_enable") == "true" |
| 327 | if verity_supported and (is_verity_partition or is_avb_enable): |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 328 | adjusted_blocks_value = image_props.get("partition_size") |
| 329 | if adjusted_blocks_value: |
| 330 | adjusted_blocks_key = what + "_adjusted_partition_size" |
| 331 | info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1 |
| 332 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 333 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 334 | def AddUserdata(output_zip): |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 335 | """Create a userdata image and store it in output_zip. |
| 336 | |
| 337 | In most case we just create and store an empty userdata.img; |
| 338 | But the invoker can also request to create userdata.img with real |
| 339 | data from the target files, by setting "userdata_img_with_data=true" |
| 340 | in OPTIONS.info_dict. |
| 341 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 342 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 343 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "userdata.img") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 344 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 345 | print("userdata.img already exists; no need to rebuild...") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 346 | return |
| 347 | |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 348 | # Skip userdata.img if no size. |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 349 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 350 | if not image_props.get("partition_size"): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 351 | return |
| 352 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 353 | print("creating userdata.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 354 | |
Bryan Henry | e6d547d | 2018-07-31 18:32:00 -0700 | [diff] [blame] | 355 | image_props["timestamp"] = FIXED_FILE_TIMESTAMP |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 356 | |
Tao Bao | fa863c8 | 2017-05-23 23:49:03 -0700 | [diff] [blame] | 357 | if OPTIONS.info_dict.get("userdata_img_with_data") == "true": |
| 358 | user_dir = os.path.join(OPTIONS.input_tmp, "DATA") |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 359 | else: |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 360 | user_dir = common.MakeTempDir() |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 361 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 362 | fstab = OPTIONS.info_dict["fstab"] |
| 363 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 364 | image_props["fs_type"] = fstab["/data"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 365 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 366 | assert succ, "build userdata.img image failed" |
| 367 | |
| 368 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 369 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 370 | |
| 371 | |
Tao Bao | 3e53ef7 | 2018-07-22 21:57:56 -0700 | [diff] [blame] | 372 | def AppendVBMetaArgsForPartition(cmd, partition, image): |
| 373 | """Appends the VBMeta arguments for partition. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 374 | |
Tao Bao | 3e53ef7 | 2018-07-22 21:57:56 -0700 | [diff] [blame] | 375 | It sets up the VBMeta argument by including the partition descriptor from the |
| 376 | given 'image', or by configuring the partition as a chained partition. |
| 377 | |
| 378 | Args: |
| 379 | cmd: A list of command args that will be used to generate the vbmeta image. |
| 380 | The argument for the partition will be appended to the list. |
| 381 | partition: The name of the partition (e.g. "system"). |
| 382 | image: The path to the partition image. |
| 383 | """ |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 384 | # Check if chain partition is used. |
| 385 | key_path = OPTIONS.info_dict.get("avb_" + partition + "_key_path") |
| 386 | if key_path: |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 387 | chained_partition_arg = common.GetAvbChainedPartitionArg( |
| 388 | partition, OPTIONS.info_dict) |
| 389 | cmd.extend(["--chain_partition", chained_partition_arg]) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 390 | else: |
Tao Bao | 3e53ef7 | 2018-07-22 21:57:56 -0700 | [diff] [blame] | 391 | cmd.extend(["--include_descriptors_from_image", image]) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 392 | |
| 393 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 394 | def AddVBMeta(output_zip, partitions): |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 395 | """Creates a VBMeta image and store it in output_zip. |
| 396 | |
| 397 | Args: |
| 398 | output_zip: The output zip file, which needs to be already open. |
| 399 | partitions: A dict that's keyed by partition names with image paths as |
Tao Bao | 3e53ef7 | 2018-07-22 21:57:56 -0700 | [diff] [blame] | 400 | values. Only valid partition names are accepted, as listed in |
| 401 | common.AVB_PARTITIONS. |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 402 | """ |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 403 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "vbmeta.img") |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 404 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 405 | print("vbmeta.img already exists; not rebuilding...") |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 406 | return img.input_name |
| 407 | |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 408 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 409 | cmd = [avbtool, "make_vbmeta_image", "--output", img.name] |
| 410 | common.AppendAVBSigningArgs(cmd, "vbmeta") |
| 411 | |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 412 | for partition, path in partitions.items(): |
Tao Bao | 3e53ef7 | 2018-07-22 21:57:56 -0700 | [diff] [blame] | 413 | assert partition in common.AVB_PARTITIONS, \ |
| 414 | 'Unknown partition: {}'.format(partition) |
| 415 | assert os.path.exists(path), \ |
| 416 | 'Failed to find {} for {}'.format(path, partition) |
| 417 | AppendVBMetaArgsForPartition(cmd, partition, path) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 418 | |
| 419 | args = OPTIONS.info_dict.get("avb_vbmeta_args") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 420 | if args and args.strip(): |
Tao Bao | 9a5f419 | 2017-07-20 23:51:16 -0700 | [diff] [blame] | 421 | split_args = shlex.split(args) |
| 422 | for index, arg in enumerate(split_args[:-1]): |
| 423 | # Sanity check that the image file exists. Some images might be defined |
| 424 | # as a path relative to source tree, which may not be available at the |
| 425 | # same location when running this script (we have the input target_files |
| 426 | # zip only). For such cases, we additionally scan other locations (e.g. |
| 427 | # IMAGES/, RADIO/, etc) before bailing out. |
| 428 | if arg == '--include_descriptors_from_image': |
| 429 | image_path = split_args[index + 1] |
| 430 | if os.path.exists(image_path): |
| 431 | continue |
| 432 | found = False |
Tao Bao | 36d7c56 | 2018-04-17 18:26:41 -0700 | [diff] [blame] | 433 | for dir_name in ['IMAGES', 'RADIO', 'PREBUILT_IMAGES']: |
Tao Bao | 9a5f419 | 2017-07-20 23:51:16 -0700 | [diff] [blame] | 434 | alt_path = os.path.join( |
Tao Bao | 6b9fef5 | 2017-12-01 16:13:22 -0800 | [diff] [blame] | 435 | OPTIONS.input_tmp, dir_name, os.path.basename(image_path)) |
Tao Bao | 9a5f419 | 2017-07-20 23:51:16 -0700 | [diff] [blame] | 436 | if os.path.exists(alt_path): |
| 437 | split_args[index + 1] = alt_path |
| 438 | found = True |
| 439 | break |
| 440 | assert found, 'failed to find %s' % (image_path,) |
| 441 | cmd.extend(split_args) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 442 | |
Bryan Henry | 69d3feb | 2018-04-14 23:07:46 -0700 | [diff] [blame] | 443 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 444 | stdoutdata, _ = p.communicate() |
| 445 | assert p.returncode == 0, \ |
| 446 | "avbtool make_vbmeta_image failed:\n{}".format(stdoutdata) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 447 | img.Write() |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 448 | |
| 449 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 450 | def AddPartitionTable(output_zip): |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 451 | """Create a partition table image and store it in output_zip.""" |
| 452 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 453 | img = OutputFile( |
| 454 | output_zip, OPTIONS.input_tmp, "IMAGES", "partition-table.img") |
| 455 | bpt = OutputFile( |
Bryan Henry | f130a23 | 2018-04-26 11:59:33 -0700 | [diff] [blame] | 456 | output_zip, OPTIONS.input_tmp, "META", "partition-table.bpt") |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 457 | |
| 458 | # use BPTTOOL from environ, or "bpttool" if empty or not set. |
| 459 | bpttool = os.getenv("BPTTOOL") or "bpttool" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 460 | cmd = [bpttool, "make_table", "--output_json", bpt.name, |
| 461 | "--output_gpt", img.name] |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 462 | input_files_str = OPTIONS.info_dict["board_bpt_input_files"] |
| 463 | input_files = input_files_str.split(" ") |
| 464 | for i in input_files: |
| 465 | cmd.extend(["--input", i]) |
| 466 | disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") |
| 467 | if disk_size: |
| 468 | cmd.extend(["--disk_size", disk_size]) |
| 469 | args = OPTIONS.info_dict.get("board_bpt_make_table_args") |
| 470 | if args: |
| 471 | cmd.extend(shlex.split(args)) |
| 472 | |
Bryan Henry | 69d3feb | 2018-04-14 23:07:46 -0700 | [diff] [blame] | 473 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 474 | stdoutdata, _ = p.communicate() |
| 475 | assert p.returncode == 0, \ |
| 476 | "bpttool make_table failed:\n{}".format(stdoutdata) |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 477 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 478 | img.Write() |
| 479 | bpt.Write() |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 480 | |
| 481 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 482 | def AddCache(output_zip): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 483 | """Create an empty cache image and store it in output_zip.""" |
| 484 | |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 485 | img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "cache.img") |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 486 | if os.path.exists(img.input_name): |
Tao Bao | 886d883 | 2018-02-27 11:46:19 -0800 | [diff] [blame] | 487 | print("cache.img already exists; no need to rebuild...") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 488 | return |
| 489 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 490 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 491 | # The build system has to explicitly request for cache.img. |
| 492 | if "fs_type" not in image_props: |
| 493 | return |
| 494 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 495 | print("creating cache.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 496 | |
Bryan Henry | e6d547d | 2018-07-31 18:32:00 -0700 | [diff] [blame] | 497 | image_props["timestamp"] = FIXED_FILE_TIMESTAMP |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 498 | |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 499 | user_dir = common.MakeTempDir() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 500 | |
| 501 | fstab = OPTIONS.info_dict["fstab"] |
| 502 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 503 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 504 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 505 | assert succ, "build cache.img image failed" |
| 506 | |
| 507 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 508 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 509 | |
| 510 | |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 511 | def CheckAbOtaImages(output_zip, ab_partitions): |
| 512 | """Checks that all the listed A/B partitions have their images available. |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 513 | |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 514 | The images need to be available under IMAGES/ or RADIO/, with the former takes |
| 515 | a priority. |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 516 | |
| 517 | Args: |
| 518 | output_zip: The output zip file (needs to be already open), or None to |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 519 | find images in OPTIONS.input_tmp/. |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 520 | ab_partitions: The list of A/B partitions. |
| 521 | |
| 522 | Raises: |
| 523 | AssertionError: If it can't find an image. |
| 524 | """ |
| 525 | for partition in ab_partitions: |
| 526 | img_name = partition.strip() + ".img" |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 527 | |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 528 | # Assert that the image is present under IMAGES/ now. |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 529 | if output_zip: |
| 530 | # Zip spec says: All slashes MUST be forward slashes. |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 531 | images_path = "IMAGES/" + img_name |
| 532 | radio_path = "RADIO/" + img_name |
| 533 | available = (images_path in output_zip.namelist() or |
| 534 | radio_path in output_zip.namelist()) |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 535 | else: |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 536 | images_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 537 | radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
| 538 | available = os.path.exists(images_path) or os.path.exists(radio_path) |
| 539 | |
| 540 | assert available, "Failed to find " + img_name |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 541 | |
| 542 | |
| 543 | def AddCareMapTxtForAbOta(output_zip, ab_partitions, image_paths): |
| 544 | """Generates and adds care_map.txt for system and vendor partitions. |
| 545 | |
| 546 | Args: |
| 547 | output_zip: The output zip file (needs to be already open), or None to |
Tianjie Xu | ccbae48 | 2018-08-15 14:28:27 -0700 | [diff] [blame^] | 548 | write care_map.txt to OPTIONS.input_tmp/. |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 549 | ab_partitions: The list of A/B partitions. |
| 550 | image_paths: A map from the partition name to the image path. |
| 551 | """ |
| 552 | care_map_list = [] |
| 553 | for partition in ab_partitions: |
| 554 | partition = partition.strip() |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 555 | if partition not in PARTITIONS_WITH_CARE_MAP: |
| 556 | continue |
| 557 | |
| 558 | verity_block_device = "{}_verity_block_device".format(partition) |
| 559 | avb_hashtree_enable = "avb_{}_hashtree_enable".format(partition) |
| 560 | if (verity_block_device in OPTIONS.info_dict or |
| 561 | OPTIONS.info_dict.get(avb_hashtree_enable) == "true"): |
| 562 | image_path = image_paths[partition] |
| 563 | assert os.path.exists(image_path) |
| 564 | care_map_list += GetCareMap(partition, image_path) |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 565 | |
Tianjie Xu | ccbae48 | 2018-08-15 14:28:27 -0700 | [diff] [blame^] | 566 | if not care_map_list: |
| 567 | return |
| 568 | |
| 569 | # Converts the list into proto buf message by calling care_map_generator; and |
| 570 | # writes the result to a temp file. |
| 571 | temp_care_map_text = common.MakeTempFile(prefix="caremap_text-", |
| 572 | suffix=".txt") |
| 573 | with open(temp_care_map_text, 'w') as text_file: |
| 574 | text_file.write('\n'.join(care_map_list)) |
| 575 | |
| 576 | temp_care_map = common.MakeTempFile(prefix="caremap-", suffix=".txt") |
| 577 | care_map_gen_cmd = (["care_map_generator", temp_care_map_text, temp_care_map]) |
| 578 | p = common.Run(care_map_gen_cmd, stdout=subprocess.PIPE, |
| 579 | stderr=subprocess.STDOUT) |
| 580 | output, _ = p.communicate() |
| 581 | assert p.returncode == 0, "Failed to generate the care_map proto message." |
| 582 | if OPTIONS.verbose: |
| 583 | print(output.rstrip()) |
| 584 | |
| 585 | care_map_path = "META/care_map.txt" |
| 586 | if output_zip and care_map_path not in output_zip.namelist(): |
| 587 | common.ZipWrite(output_zip, temp_care_map, arcname=care_map_path) |
| 588 | else: |
| 589 | shutil.copy(temp_care_map, os.path.join(OPTIONS.input_tmp, care_map_path)) |
| 590 | if output_zip: |
| 591 | OPTIONS.replace_updated_files_list.append(care_map_path) |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 592 | |
| 593 | |
| 594 | def AddPackRadioImages(output_zip, images): |
| 595 | """Copies images listed in META/pack_radioimages.txt from RADIO/ to IMAGES/. |
| 596 | |
| 597 | Args: |
| 598 | output_zip: The output zip file (needs to be already open), or None to |
| 599 | write images to OPTIONS.input_tmp/. |
| 600 | images: A list of image names. |
| 601 | |
| 602 | Raises: |
| 603 | AssertionError: If a listed image can't be found. |
| 604 | """ |
| 605 | for image in images: |
| 606 | img_name = image.strip() |
| 607 | _, ext = os.path.splitext(img_name) |
| 608 | if not ext: |
| 609 | img_name += ".img" |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 610 | |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 611 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 612 | if os.path.exists(prebuilt_path): |
| 613 | print("%s already exists, no need to overwrite..." % (img_name,)) |
| 614 | continue |
| 615 | |
| 616 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
| 617 | assert os.path.exists(img_radio_path), \ |
| 618 | "Failed to find %s at %s" % (img_name, img_radio_path) |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 619 | |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 620 | if output_zip: |
Tao Bao | a2ff4c9 | 2018-01-17 12:14:43 -0800 | [diff] [blame] | 621 | common.ZipWrite(output_zip, img_radio_path, "IMAGES/" + img_name) |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 622 | else: |
| 623 | shutil.copy(img_radio_path, prebuilt_path) |
| 624 | |
| 625 | |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 626 | def ReplaceUpdatedFiles(zip_filename, files_list): |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 627 | """Updates all the ZIP entries listed in files_list. |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 628 | |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 629 | For now the list includes META/care_map.txt, and the related files under |
| 630 | SYSTEM/ after rebuilding recovery. |
| 631 | """ |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 632 | common.ZipDelete(zip_filename, files_list) |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 633 | output_zip = zipfile.ZipFile(zip_filename, "a", |
| 634 | compression=zipfile.ZIP_DEFLATED, |
| 635 | allowZip64=True) |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 636 | for item in files_list: |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 637 | file_path = os.path.join(OPTIONS.input_tmp, item) |
| 638 | assert os.path.exists(file_path) |
| 639 | common.ZipWrite(output_zip, file_path, arcname=item) |
| 640 | common.ZipClose(output_zip) |
| 641 | |
| 642 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 643 | def AddImagesToTargetFiles(filename): |
Tao Bao | ae396d9 | 2017-11-20 11:56:43 -0800 | [diff] [blame] | 644 | """Creates and adds images (boot/recovery/system/...) to a target_files.zip. |
| 645 | |
| 646 | It works with either a zip file (zip mode), or a directory that contains the |
| 647 | files to be packed into a target_files.zip (dir mode). The latter is used when |
| 648 | being called from build/make/core/Makefile. |
| 649 | |
| 650 | The images will be created under IMAGES/ in the input target_files.zip. |
| 651 | |
| 652 | Args: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 653 | filename: the target_files.zip, or the zip root directory. |
Tao Bao | ae396d9 | 2017-11-20 11:56:43 -0800 | [diff] [blame] | 654 | """ |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 655 | if os.path.isdir(filename): |
| 656 | OPTIONS.input_tmp = os.path.abspath(filename) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 657 | else: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 658 | OPTIONS.input_tmp = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 659 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 660 | if not OPTIONS.add_missing: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 661 | if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")): |
| 662 | print("target_files appears to already contain images.") |
| 663 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 664 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 665 | OPTIONS.info_dict = common.LoadInfoDict(OPTIONS.input_tmp, OPTIONS.input_tmp) |
| 666 | |
| 667 | has_recovery = OPTIONS.info_dict.get("no_recovery") != "true" |
| 668 | |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 669 | # {vendor,odm,product,product-services}.img are unlike system.img or |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 670 | # system_other.img. Because it could be built from source, or dropped into |
| 671 | # target_files.zip as a prebuilt blob. We consider either of them as |
| 672 | # {vendor,product,product-services}.img being available, which could be |
| 673 | # used when generating vbmeta.img for AVB. |
Tao Bao | b22afea | 2017-09-12 12:39:09 -0700 | [diff] [blame] | 674 | has_vendor = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) or |
| 675 | os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES", |
| 676 | "vendor.img"))) |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 677 | has_odm = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "ODM")) or |
| 678 | os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES", |
| 679 | "odm.img"))) |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 680 | has_product = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "PRODUCT")) or |
| 681 | os.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES", |
| 682 | "product.img"))) |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 683 | has_product_services = (os.path.isdir(os.path.join(OPTIONS.input_tmp, |
Dario Freni | 77c778c | 2018-08-17 00:28:17 +0100 | [diff] [blame] | 684 | "PRODUCT_SERVICES")) or |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 685 | os.path.exists(os.path.join(OPTIONS.input_tmp, |
| 686 | "IMAGES", |
| 687 | "product-services.img"))) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 688 | has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp, |
| 689 | "SYSTEM_OTHER")) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 690 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 691 | # Set up the output destination. It writes to the given directory for dir |
| 692 | # mode; otherwise appends to the given ZIP. |
| 693 | if os.path.isdir(filename): |
| 694 | output_zip = None |
| 695 | else: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 696 | output_zip = zipfile.ZipFile(filename, "a", |
| 697 | compression=zipfile.ZIP_DEFLATED, |
| 698 | allowZip64=True) |
Tao Bao | ae396d9 | 2017-11-20 11:56:43 -0800 | [diff] [blame] | 699 | |
| 700 | # Always make input_tmp/IMAGES available, since we may stage boot / recovery |
| 701 | # images there even under zip mode. The directory will be cleaned up as part |
| 702 | # of OPTIONS.input_tmp. |
| 703 | images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES") |
| 704 | if not os.path.isdir(images_dir): |
| 705 | os.makedirs(images_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 706 | |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 707 | # A map between partition names and their paths, which could be used when |
| 708 | # generating AVB vbmeta image. |
| 709 | partitions = dict() |
| 710 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 711 | def banner(s): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 712 | print("\n\n++++ " + s + " ++++\n\n") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 713 | |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 714 | banner("boot") |
| 715 | # common.GetBootableImage() returns the image directly if present. |
| 716 | boot_image = common.GetBootableImage( |
| 717 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 718 | # boot.img may be unavailable in some targets (e.g. aosp_arm64). |
| 719 | if boot_image: |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 720 | partitions['boot'] = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 721 | if not os.path.exists(partitions['boot']): |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 722 | boot_image.WriteToDir(OPTIONS.input_tmp) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 723 | if output_zip: |
| 724 | boot_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 725 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 726 | recovery_image = None |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 727 | if has_recovery: |
| 728 | banner("recovery") |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 729 | recovery_image = common.GetBootableImage( |
| 730 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
| 731 | assert recovery_image, "Failed to create recovery.img." |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 732 | partitions['recovery'] = os.path.join( |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 733 | OPTIONS.input_tmp, "IMAGES", "recovery.img") |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 734 | if not os.path.exists(partitions['recovery']): |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 735 | recovery_image.WriteToDir(OPTIONS.input_tmp) |
| 736 | if output_zip: |
| 737 | recovery_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 738 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 739 | banner("recovery (two-step image)") |
| 740 | # The special recovery.img for two-step package use. |
| 741 | recovery_two_step_image = common.GetBootableImage( |
| 742 | "IMAGES/recovery-two-step.img", "recovery-two-step.img", |
| 743 | OPTIONS.input_tmp, "RECOVERY", two_step_image=True) |
Tao Bao | 262bf3f | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 744 | assert recovery_two_step_image, "Failed to create recovery-two-step.img." |
| 745 | recovery_two_step_image_path = os.path.join( |
| 746 | OPTIONS.input_tmp, "IMAGES", "recovery-two-step.img") |
| 747 | if not os.path.exists(recovery_two_step_image_path): |
| 748 | recovery_two_step_image.WriteToDir(OPTIONS.input_tmp) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 749 | if output_zip: |
| 750 | recovery_two_step_image.AddToZip(output_zip) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 751 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 752 | banner("system") |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 753 | partitions['system'] = AddSystem( |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 754 | output_zip, recovery_img=recovery_image, boot_img=boot_image) |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 755 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 756 | if has_vendor: |
| 757 | banner("vendor") |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 758 | partitions['vendor'] = AddVendor(output_zip) |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 759 | |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 760 | if has_product: |
| 761 | banner("product") |
| 762 | partitions['product'] = AddProduct(output_zip) |
| 763 | |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 764 | if has_product_services: |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 765 | banner("product-services") |
| 766 | partitions['product-services'] = AddProductServices(output_zip) |
| 767 | |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 768 | if has_odm: |
| 769 | banner("odm") |
| 770 | partitions['odm'] = AddOdm(output_zip) |
| 771 | |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 772 | if has_system_other: |
| 773 | banner("system_other") |
| 774 | AddSystemOther(output_zip) |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 775 | |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 776 | if not OPTIONS.is_signing: |
| 777 | banner("userdata") |
| 778 | AddUserdata(output_zip) |
| 779 | banner("cache") |
| 780 | AddCache(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 781 | |
| 782 | if OPTIONS.info_dict.get("board_bpt_enable") == "true": |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 783 | banner("partition-table") |
| 784 | AddPartitionTable(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 785 | |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 786 | if OPTIONS.info_dict.get("has_dtbo") == "true": |
| 787 | banner("dtbo") |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 788 | partitions['dtbo'] = AddDtbo(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame] | 789 | |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 790 | if OPTIONS.info_dict.get("avb_enable") == "true": |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 791 | banner("vbmeta") |
Tao Bao | bf70c31 | 2017-07-11 17:27:55 -0700 | [diff] [blame] | 792 | AddVBMeta(output_zip, partitions) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 793 | |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 794 | banner("radio") |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 795 | ab_partitions_txt = os.path.join(OPTIONS.input_tmp, "META", |
| 796 | "ab_partitions.txt") |
| 797 | if os.path.exists(ab_partitions_txt): |
| 798 | with open(ab_partitions_txt, 'r') as f: |
| 799 | ab_partitions = f.readlines() |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 800 | |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 801 | # For devices using A/B update, make sure we have all the needed images |
| 802 | # ready under IMAGES/ or RADIO/. |
| 803 | CheckAbOtaImages(output_zip, ab_partitions) |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 804 | |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 805 | # Generate care_map.txt for system and vendor partitions (if present), then |
| 806 | # write this file to target_files package. |
| 807 | AddCareMapTxtForAbOta(output_zip, ab_partitions, partitions) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 808 | |
Tao Bao | 95a95c3 | 2017-06-16 15:30:23 -0700 | [diff] [blame] | 809 | # Radio images that need to be packed into IMAGES/, and product-img.zip. |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 810 | pack_radioimages_txt = os.path.join( |
Tao Bao | 95a95c3 | 2017-06-16 15:30:23 -0700 | [diff] [blame] | 811 | OPTIONS.input_tmp, "META", "pack_radioimages.txt") |
Tao Bao | bea20ac | 2018-01-17 17:57:49 -0800 | [diff] [blame] | 812 | if os.path.exists(pack_radioimages_txt): |
| 813 | with open(pack_radioimages_txt, 'r') as f: |
| 814 | AddPackRadioImages(output_zip, f.readlines()) |
Tao Bao | 95a95c3 | 2017-06-16 15:30:23 -0700 | [diff] [blame] | 815 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 816 | if output_zip: |
| 817 | common.ZipClose(output_zip) |
Tianjie Xu | 9ac4cb0 | 2017-06-09 16:58:03 -0700 | [diff] [blame] | 818 | if OPTIONS.replace_updated_files_list: |
| 819 | ReplaceUpdatedFiles(output_zip.filename, |
| 820 | OPTIONS.replace_updated_files_list) |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 821 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 822 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 823 | def main(argv): |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 824 | def option_handler(o, a): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 825 | if o in ("-a", "--add_missing"): |
| 826 | OPTIONS.add_missing = True |
| 827 | elif o in ("-r", "--rebuild_recovery",): |
| 828 | OPTIONS.rebuild_recovery = True |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 829 | elif o == "--replace_verity_private_key": |
| 830 | OPTIONS.replace_verity_private_key = (True, a) |
| 831 | elif o == "--replace_verity_public_key": |
| 832 | OPTIONS.replace_verity_public_key = (True, a) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 833 | elif o == "--is_signing": |
| 834 | OPTIONS.is_signing = True |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 835 | else: |
| 836 | return False |
| 837 | return True |
| 838 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 839 | args = common.ParseOptions( |
| 840 | argv, __doc__, extra_opts="ar", |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 841 | extra_long_opts=["add_missing", "rebuild_recovery", |
| 842 | "replace_verity_public_key=", |
| 843 | "replace_verity_private_key=", |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 844 | "is_signing"], |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 845 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 846 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 847 | |
| 848 | if len(args) != 1: |
| 849 | common.Usage(__doc__) |
| 850 | sys.exit(1) |
| 851 | |
| 852 | AddImagesToTargetFiles(args[0]) |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 853 | print("done.") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 854 | |
| 855 | if __name__ == '__main__': |
| 856 | try: |
| 857 | common.CloseInheritedPipes() |
| 858 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 859 | except common.ExternalError as e: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 860 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 861 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 862 | finally: |
| 863 | common.Cleanup() |