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 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 48 | import sys |
| 49 | |
| 50 | if sys.hexversion < 0x02070000: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 51 | print("Python 2.7 or newer is required.", file=sys.stderr) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 52 | sys.exit(1) |
| 53 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 54 | import datetime |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 55 | import errno |
| 56 | import os |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 57 | import shlex |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 58 | import shutil |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 59 | import subprocess |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 60 | import tempfile |
| 61 | import zipfile |
| 62 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 63 | import build_image |
| 64 | import common |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 65 | import rangelib |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 66 | import sparse_img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 67 | |
| 68 | OPTIONS = common.OPTIONS |
| 69 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 70 | OPTIONS.add_missing = False |
| 71 | OPTIONS.rebuild_recovery = False |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 72 | OPTIONS.replace_recovery_patch_files_list = [] |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 73 | OPTIONS.replace_verity_public_key = False |
| 74 | OPTIONS.replace_verity_private_key = False |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 75 | OPTIONS.is_signing = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 76 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 77 | |
| 78 | class OutputFile(object): |
| 79 | def __init__(self, output_zip, input_dir, prefix, name): |
| 80 | self._output_zip = output_zip |
| 81 | self.input_name = os.path.join(input_dir, prefix, name) |
| 82 | |
| 83 | if self._output_zip: |
| 84 | self._zip_name = os.path.join(prefix, name) |
| 85 | |
| 86 | root, suffix = os.path.splitext(name) |
| 87 | self.name = common.MakeTempFile(prefix=root + '-', suffix=suffix) |
| 88 | else: |
| 89 | self.name = self.input_name |
| 90 | |
| 91 | def Write(self): |
| 92 | if self._output_zip: |
| 93 | common.ZipWrite(self._output_zip, self.name, self._zip_name) |
| 94 | |
| 95 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 96 | def GetCareMap(which, imgname): |
| 97 | """Generate care_map of system (or vendor) partition""" |
| 98 | |
| 99 | assert which in ("system", "vendor") |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 100 | |
| 101 | simg = sparse_img.SparseImage(imgname) |
| 102 | care_map_list = [] |
Tianjie Xu | 955629b | 2017-03-01 11:48:25 -0800 | [diff] [blame] | 103 | care_map_list.append(which) |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 104 | |
| 105 | care_map_ranges = simg.care_map |
| 106 | key = which + "_adjusted_partition_size" |
| 107 | adjusted_blocks = OPTIONS.info_dict.get(key) |
| 108 | if adjusted_blocks: |
| 109 | assert adjusted_blocks > 0, "blocks should be positive for " + which |
| 110 | care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet( |
| 111 | "0-%d" % (adjusted_blocks,))) |
| 112 | |
| 113 | care_map_list.append(care_map_ranges.to_string_raw()) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 114 | return care_map_list |
| 115 | |
| 116 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 117 | def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 118 | """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] | 119 | output_zip. Returns the name of the system image file.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 120 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 121 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.img") |
| 122 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 123 | print("system.img already exists in %s, no need to rebuild..." % (prefix,)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 124 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 125 | |
| 126 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 127 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 128 | ofile.write(data) |
| 129 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 130 | |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 131 | arc_name = "SYSTEM/" + fn |
| 132 | if arc_name in output_zip.namelist(): |
| 133 | OPTIONS.replace_recovery_patch_files_list.append(arc_name) |
| 134 | else: |
| 135 | common.ZipWrite(output_zip, ofile.name, arc_name) |
| 136 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 137 | if OPTIONS.rebuild_recovery: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 138 | print("Building new recovery patch") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 139 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 140 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 141 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 142 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system.map") |
| 143 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system", img, |
| 144 | block_list=block_list) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 145 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 146 | return img.name |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 147 | |
| 148 | |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 149 | def AddSystemOther(output_zip, prefix="IMAGES/"): |
| 150 | """Turn the contents of SYSTEM_OTHER into a system_other image |
| 151 | and store it in output_zip.""" |
| 152 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 153 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "system_other.img") |
| 154 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 155 | print("system_other.img already exists in %s, no need to rebuild..." % ( |
| 156 | prefix,)) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 157 | return |
| 158 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 159 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "system_other", img) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 160 | |
| 161 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 162 | def AddVendor(output_zip, prefix="IMAGES/"): |
| 163 | """Turn the contents of VENDOR into a vendor image and store in it |
| 164 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 165 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 166 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.img") |
| 167 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 168 | print("vendor.img already exists in %s, no need to rebuild..." % (prefix,)) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 169 | return img.input_name |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 170 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 171 | block_list = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vendor.map") |
| 172 | CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img, |
| 173 | block_list=block_list) |
| 174 | return img.name |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 175 | |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 176 | |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 177 | def AddDtbo(output_zip, prefix="IMAGES/"): |
| 178 | """Adds the DTBO image. |
| 179 | |
| 180 | Uses the image under prefix if it already exists. Otherwise looks for the |
| 181 | image under PREBUILT_IMAGES/, signs it as needed, and returns the image name. |
| 182 | """ |
| 183 | |
| 184 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img") |
| 185 | if os.path.exists(img.input_name): |
| 186 | print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,)) |
| 187 | return img.input_name |
| 188 | |
| 189 | dtbo_prebuilt_path = os.path.join( |
| 190 | OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img") |
| 191 | assert os.path.exists(dtbo_prebuilt_path) |
| 192 | shutil.copy(dtbo_prebuilt_path, img.name) |
| 193 | |
| 194 | # AVB-sign the image as needed. |
| 195 | if OPTIONS.info_dict.get("board_avb_enable") == "true": |
| 196 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
| 197 | part_size = OPTIONS.info_dict.get("dtbo_size") |
| 198 | # The AVB hash footer will be replaced if already present. |
| 199 | cmd = [avbtool, "add_hash_footer", "--image", img.name, |
| 200 | "--partition_size", str(part_size), "--partition_name", "dtbo"] |
| 201 | common.AppendAVBSigningArgs(cmd) |
| 202 | args = OPTIONS.info_dict.get("board_avb_dtbo_add_hash_footer_args") |
| 203 | if args and args.strip(): |
| 204 | cmd.extend(shlex.split(args)) |
| 205 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 206 | p.communicate() |
| 207 | assert p.returncode == 0, \ |
| 208 | "avbtool add_hash_footer of %s failed" % (img.name,) |
| 209 | |
| 210 | img.Write() |
| 211 | return img.name |
| 212 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 213 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 214 | def CreateImage(input_dir, info_dict, what, output_file, block_list=None): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 215 | print("creating " + what + ".img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 216 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 217 | # The name of the directory it is making an image out of matters to |
| 218 | # mkyaffs2image. It wants "system" but we have a directory named |
| 219 | # "SYSTEM", so create a symlink. |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 220 | temp_dir = tempfile.mkdtemp() |
| 221 | OPTIONS.tempfiles.append(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 222 | try: |
| 223 | os.symlink(os.path.join(input_dir, what.upper()), |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 224 | os.path.join(temp_dir, what)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 225 | except OSError as e: |
| 226 | # bogus error on my mac version? |
| 227 | # File "./build/tools/releasetools/img_from_target_files" |
| 228 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 229 | # OSError: [Errno 17] File exists |
| 230 | if e.errno == errno.EEXIST: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 231 | pass |
| 232 | |
| 233 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 234 | fstab = info_dict["fstab"] |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 235 | mount_point = "/" + what |
| 236 | if fstab and mount_point in fstab: |
| 237 | image_props["fs_type"] = fstab[mount_point].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 238 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 239 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 240 | # Bug: 24377993 |
| 241 | epoch = datetime.datetime.fromtimestamp(0) |
| 242 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 243 | image_props["timestamp"] = int(timestamp) |
| 244 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 245 | if what == "system": |
| 246 | fs_config_prefix = "" |
| 247 | else: |
| 248 | fs_config_prefix = what + "_" |
| 249 | |
| 250 | fs_config = os.path.join( |
| 251 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 252 | if not os.path.exists(fs_config): |
| 253 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 254 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 255 | # Override values loaded from info_dict. |
| 256 | if fs_config: |
| 257 | image_props["fs_config"] = fs_config |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 258 | if block_list: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 259 | image_props["block_list"] = block_list.name |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 260 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 261 | succ = build_image.BuildImage(os.path.join(temp_dir, what), |
| 262 | image_props, output_file.name) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 263 | assert succ, "build " + what + ".img image failed" |
| 264 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 265 | output_file.Write() |
| 266 | if block_list: |
| 267 | block_list.Write() |
| 268 | |
Tianjie Xu | f1a1318 | 2017-01-19 17:39:30 -0800 | [diff] [blame] | 269 | is_verity_partition = "verity_block_device" in image_props |
| 270 | verity_supported = image_props.get("verity") == "true" |
| 271 | if is_verity_partition and verity_supported: |
| 272 | adjusted_blocks_value = image_props.get("partition_size") |
| 273 | if adjusted_blocks_value: |
| 274 | adjusted_blocks_key = what + "_adjusted_partition_size" |
| 275 | info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1 |
| 276 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 277 | |
| 278 | def AddUserdata(output_zip, prefix="IMAGES/"): |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 279 | """Create a userdata image and store it in output_zip. |
| 280 | |
| 281 | In most case we just create and store an empty userdata.img; |
| 282 | But the invoker can also request to create userdata.img with real |
| 283 | data from the target files, by setting "userdata_img_with_data=true" |
| 284 | in OPTIONS.info_dict. |
| 285 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 286 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 287 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img") |
| 288 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 289 | print("userdata.img already exists in %s, no need to rebuild..." % ( |
| 290 | prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 291 | return |
| 292 | |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 293 | # Skip userdata.img if no size. |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 294 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 295 | if not image_props.get("partition_size"): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 296 | return |
| 297 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 298 | print("creating userdata.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 299 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 300 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 301 | # Bug: 24377993 |
| 302 | epoch = datetime.datetime.fromtimestamp(0) |
| 303 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 304 | image_props["timestamp"] = int(timestamp) |
| 305 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 306 | # The name of the directory it is making an image out of matters to |
| 307 | # mkyaffs2image. So we create a temp dir, and within it we create an |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 308 | # empty dir named "data", or a symlink to the DATA dir, |
| 309 | # and build the image from that. |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 310 | temp_dir = tempfile.mkdtemp() |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 311 | OPTIONS.tempfiles.append(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 312 | user_dir = os.path.join(temp_dir, "data") |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 313 | empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true") |
| 314 | if empty: |
| 315 | # Create an empty dir. |
| 316 | os.mkdir(user_dir) |
| 317 | else: |
| 318 | # Symlink to the DATA dir. |
| 319 | os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"), |
| 320 | user_dir) |
| 321 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 322 | fstab = OPTIONS.info_dict["fstab"] |
| 323 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 324 | image_props["fs_type"] = fstab["/data"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 325 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 326 | assert succ, "build userdata.img image failed" |
| 327 | |
| 328 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 329 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 330 | |
| 331 | |
Bowgo Tsai | 8ee4a3d | 2017-03-31 15:21:26 +0800 | [diff] [blame] | 332 | def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 333 | dtbo_img_path, prefix="IMAGES/"): |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 334 | """Create a VBMeta image and store it in output_zip.""" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 335 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 336 | avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 337 | cmd = [avbtool, "make_vbmeta_image", |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 338 | "--output", img.name, |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 339 | "--include_descriptors_from_image", boot_img_path, |
Bowgo Tsai | 9b37760 | 2017-04-14 18:50:11 +0800 | [diff] [blame] | 340 | "--include_descriptors_from_image", system_img_path] |
Bowgo Tsai | 8ee4a3d | 2017-03-31 15:21:26 +0800 | [diff] [blame] | 341 | if vendor_img_path is not None: |
| 342 | cmd.extend(["--include_descriptors_from_image", vendor_img_path]) |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 343 | if dtbo_img_path is not None: |
| 344 | cmd.extend(["--include_descriptors_from_image", dtbo_img_path]) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 345 | if OPTIONS.info_dict.get("system_root_image") == "true": |
Bowgo Tsai | 9b37760 | 2017-04-14 18:50:11 +0800 | [diff] [blame] | 346 | cmd.extend(["--setup_rootfs_from_kernel", system_img_path]) |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 347 | common.AppendAVBSigningArgs(cmd) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 348 | args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 349 | if args and args.strip(): |
| 350 | cmd.extend(shlex.split(args)) |
| 351 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 352 | p.communicate() |
| 353 | assert p.returncode == 0, "avbtool make_vbmeta_image failed" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 354 | img.Write() |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 355 | |
| 356 | |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 357 | def AddPartitionTable(output_zip, prefix="IMAGES/"): |
| 358 | """Create a partition table image and store it in output_zip.""" |
| 359 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 360 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.img") |
| 361 | bpt = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "partition-table.bpt") |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 362 | |
| 363 | # use BPTTOOL from environ, or "bpttool" if empty or not set. |
| 364 | bpttool = os.getenv("BPTTOOL") or "bpttool" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 365 | cmd = [bpttool, "make_table", "--output_json", bpt.name, |
| 366 | "--output_gpt", img.name] |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 367 | input_files_str = OPTIONS.info_dict["board_bpt_input_files"] |
| 368 | input_files = input_files_str.split(" ") |
| 369 | for i in input_files: |
| 370 | cmd.extend(["--input", i]) |
| 371 | disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") |
| 372 | if disk_size: |
| 373 | cmd.extend(["--disk_size", disk_size]) |
| 374 | args = OPTIONS.info_dict.get("board_bpt_make_table_args") |
| 375 | if args: |
| 376 | cmd.extend(shlex.split(args)) |
| 377 | |
| 378 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 379 | p.communicate() |
| 380 | assert p.returncode == 0, "bpttool make_table failed" |
| 381 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 382 | img.Write() |
| 383 | bpt.Write() |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 384 | |
| 385 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 386 | def AddCache(output_zip, prefix="IMAGES/"): |
| 387 | """Create an empty cache image and store it in output_zip.""" |
| 388 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 389 | img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img") |
| 390 | if os.path.exists(img.input_name): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 391 | print("cache.img already exists in %s, no need to rebuild..." % (prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 392 | return |
| 393 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 394 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 395 | # The build system has to explicitly request for cache.img. |
| 396 | if "fs_type" not in image_props: |
| 397 | return |
| 398 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 399 | print("creating cache.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 400 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 401 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 402 | # Bug: 24377993 |
| 403 | epoch = datetime.datetime.fromtimestamp(0) |
| 404 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 405 | image_props["timestamp"] = int(timestamp) |
| 406 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 407 | # The name of the directory it is making an image out of matters to |
| 408 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 409 | # empty dir named "cache", and build the image from that. |
| 410 | temp_dir = tempfile.mkdtemp() |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 411 | OPTIONS.tempfiles.append(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 412 | user_dir = os.path.join(temp_dir, "cache") |
| 413 | os.mkdir(user_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 414 | |
| 415 | fstab = OPTIONS.info_dict["fstab"] |
| 416 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 417 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 418 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 419 | assert succ, "build cache.img image failed" |
| 420 | |
| 421 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 422 | img.Write() |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 423 | |
| 424 | |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 425 | def ReplaceRecoveryPatchFiles(zip_filename): |
| 426 | """Update the related files under SYSTEM/ after rebuilding recovery.""" |
| 427 | |
| 428 | cmd = ["zip", "-d", zip_filename] + OPTIONS.replace_recovery_patch_files_list |
| 429 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 430 | p.communicate() |
| 431 | |
| 432 | output_zip = zipfile.ZipFile(zip_filename, "a", |
| 433 | compression=zipfile.ZIP_DEFLATED, |
| 434 | allowZip64=True) |
| 435 | for item in OPTIONS.replace_recovery_patch_files_list: |
| 436 | file_path = os.path.join(OPTIONS.input_tmp, item) |
| 437 | assert os.path.exists(file_path) |
| 438 | common.ZipWrite(output_zip, file_path, arcname=item) |
| 439 | common.ZipClose(output_zip) |
| 440 | |
| 441 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 442 | def AddImagesToTargetFiles(filename): |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 443 | if os.path.isdir(filename): |
| 444 | OPTIONS.input_tmp = os.path.abspath(filename) |
| 445 | input_zip = None |
| 446 | else: |
| 447 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 448 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 449 | if not OPTIONS.add_missing: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 450 | if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")): |
| 451 | print("target_files appears to already contain images.") |
| 452 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 453 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 454 | has_vendor = os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) |
| 455 | has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp, |
| 456 | "SYSTEM_OTHER")) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 457 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 458 | if input_zip: |
| 459 | OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 460 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 461 | common.ZipClose(input_zip) |
| 462 | output_zip = zipfile.ZipFile(filename, "a", |
| 463 | compression=zipfile.ZIP_DEFLATED, |
| 464 | allowZip64=True) |
| 465 | else: |
| 466 | OPTIONS.info_dict = common.LoadInfoDict(filename, filename) |
| 467 | output_zip = None |
| 468 | images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES") |
| 469 | if not os.path.isdir(images_dir): |
| 470 | os.makedirs(images_dir) |
| 471 | images_dir = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 472 | |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 473 | has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") |
| 474 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 475 | def banner(s): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 476 | print("\n\n++++ " + s + " ++++\n\n") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 477 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 478 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 479 | boot_image = None |
| 480 | if os.path.exists(prebuilt_path): |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 481 | banner("boot") |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 482 | print("boot.img already exists in IMAGES/, no need to rebuild...") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 483 | if OPTIONS.rebuild_recovery: |
| 484 | boot_image = common.GetBootableImage( |
| 485 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 486 | else: |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 487 | banner("boot") |
| 488 | boot_image = common.GetBootableImage( |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 489 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 490 | if boot_image: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 491 | if output_zip: |
| 492 | boot_image.AddToZip(output_zip) |
| 493 | else: |
| 494 | boot_image.WriteToDir(OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 495 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 496 | recovery_image = None |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 497 | if has_recovery: |
| 498 | banner("recovery") |
| 499 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") |
| 500 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 501 | print("recovery.img already exists in IMAGES/, no need to rebuild...") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 502 | if OPTIONS.rebuild_recovery: |
| 503 | recovery_image = common.GetBootableImage( |
| 504 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, |
| 505 | "RECOVERY") |
| 506 | else: |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 507 | recovery_image = common.GetBootableImage( |
| 508 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 509 | if recovery_image: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 510 | if output_zip: |
| 511 | recovery_image.AddToZip(output_zip) |
| 512 | else: |
| 513 | recovery_image.WriteToDir(OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 514 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 515 | banner("recovery (two-step image)") |
| 516 | # The special recovery.img for two-step package use. |
| 517 | recovery_two_step_image = common.GetBootableImage( |
| 518 | "IMAGES/recovery-two-step.img", "recovery-two-step.img", |
| 519 | OPTIONS.input_tmp, "RECOVERY", two_step_image=True) |
| 520 | if recovery_two_step_image: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 521 | if output_zip: |
| 522 | recovery_two_step_image.AddToZip(output_zip) |
| 523 | else: |
| 524 | recovery_two_step_image.WriteToDir(OPTIONS.input_tmp) |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 525 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 526 | banner("system") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 527 | system_img_path = AddSystem( |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 528 | output_zip, recovery_img=recovery_image, boot_img=boot_image) |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 529 | vendor_img_path = None |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 530 | if has_vendor: |
| 531 | banner("vendor") |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 532 | vendor_img_path = AddVendor(output_zip) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 533 | if has_system_other: |
| 534 | banner("system_other") |
| 535 | AddSystemOther(output_zip) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 536 | if not OPTIONS.is_signing: |
| 537 | banner("userdata") |
| 538 | AddUserdata(output_zip) |
| 539 | banner("cache") |
| 540 | AddCache(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 541 | |
| 542 | if OPTIONS.info_dict.get("board_bpt_enable") == "true": |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 543 | banner("partition-table") |
| 544 | AddPartitionTable(output_zip) |
Tao Bao | c633ed0 | 2017-05-30 21:46:33 -0700 | [diff] [blame^] | 545 | |
| 546 | dtbo_img_path = None |
| 547 | if OPTIONS.info_dict.get("has_dtbo") == "true": |
| 548 | banner("dtbo") |
| 549 | dtbo_img_path = AddDtbo(output_zip) |
| 550 | |
| 551 | if OPTIONS.info_dict.get("board_avb_enable") == "true": |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 552 | banner("vbmeta") |
| 553 | boot_contents = boot_image.WriteToTemp() |
Yueyao Zhu | 889ee5e | 2017-05-12 17:50:46 -0700 | [diff] [blame] | 554 | AddVBMeta(output_zip, boot_contents.name, system_img_path, |
| 555 | vendor_img_path, dtbo_img_path) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 556 | |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 557 | # For devices using A/B update, copy over images from RADIO/ and/or |
| 558 | # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed |
| 559 | # images ready under IMAGES/. All images should have '.img' as extension. |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 560 | banner("radio") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 561 | ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") |
| 562 | if os.path.exists(ab_partitions): |
| 563 | with open(ab_partitions, 'r') as f: |
| 564 | lines = f.readlines() |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 565 | # For devices using A/B update, generate care_map for system and vendor |
| 566 | # partitions (if present), then write this file to target_files package. |
| 567 | care_map_list = [] |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 568 | for line in lines: |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 569 | if line.strip() == "system" and OPTIONS.info_dict.get( |
| 570 | "system_verity_block_device", None) is not None: |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 571 | assert os.path.exists(system_img_path) |
| 572 | care_map_list += GetCareMap("system", system_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 573 | if line.strip() == "vendor" and OPTIONS.info_dict.get( |
| 574 | "vendor_verity_block_device", None) is not None: |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 575 | assert os.path.exists(vendor_img_path) |
| 576 | care_map_list += GetCareMap("vendor", vendor_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 577 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 578 | img_name = line.strip() + ".img" |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 579 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 580 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 581 | print("%s already exists, no need to overwrite..." % (img_name,)) |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 582 | continue |
| 583 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 584 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 585 | img_vendor_dir = os.path.join( |
| 586 | OPTIONS.input_tmp, "VENDOR_IMAGES") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 587 | if os.path.exists(img_radio_path): |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 588 | if output_zip: |
| 589 | common.ZipWrite(output_zip, img_radio_path, |
| 590 | os.path.join("IMAGES", img_name)) |
| 591 | else: |
| 592 | shutil.copy(img_radio_path, prebuilt_path) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 593 | else: |
| 594 | for root, _, files in os.walk(img_vendor_dir): |
| 595 | if img_name in files: |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 596 | if output_zip: |
| 597 | common.ZipWrite(output_zip, os.path.join(root, img_name), |
| 598 | os.path.join("IMAGES", img_name)) |
| 599 | else: |
| 600 | shutil.copy(os.path.join(root, img_name), prebuilt_path) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 601 | break |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 602 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 603 | if output_zip: |
| 604 | # Zip spec says: All slashes MUST be forward slashes. |
| 605 | img_path = 'IMAGES/' + img_name |
| 606 | assert img_path in output_zip.namelist(), "cannot find " + img_name |
| 607 | else: |
| 608 | img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 609 | assert os.path.exists(img_path), "cannot find " + img_name |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 610 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 611 | if care_map_list: |
| 612 | file_path = "META/care_map.txt" |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 613 | if output_zip: |
| 614 | common.ZipWriteStr(output_zip, file_path, '\n'.join(care_map_list)) |
| 615 | else: |
| 616 | with open(os.path.join(OPTIONS.input_tmp, file_path), 'w') as fp: |
| 617 | fp.write('\n'.join(care_map_list)) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 618 | |
Dan Willemsen | 2ee00d5 | 2017-03-05 19:51:56 -0800 | [diff] [blame] | 619 | if output_zip: |
| 620 | common.ZipClose(output_zip) |
Tianjie Xu | 38af07f | 2017-05-25 17:38:53 -0700 | [diff] [blame] | 621 | if OPTIONS.replace_recovery_patch_files_list: |
| 622 | ReplaceRecoveryPatchFiles(output_zip.filename) |
| 623 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 624 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 625 | def main(argv): |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 626 | def option_handler(o, a): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 627 | if o in ("-a", "--add_missing"): |
| 628 | OPTIONS.add_missing = True |
| 629 | elif o in ("-r", "--rebuild_recovery",): |
| 630 | OPTIONS.rebuild_recovery = True |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 631 | elif o == "--replace_verity_private_key": |
| 632 | OPTIONS.replace_verity_private_key = (True, a) |
| 633 | elif o == "--replace_verity_public_key": |
| 634 | OPTIONS.replace_verity_public_key = (True, a) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 635 | elif o == "--is_signing": |
| 636 | OPTIONS.is_signing = True |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 637 | else: |
| 638 | return False |
| 639 | return True |
| 640 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 641 | args = common.ParseOptions( |
| 642 | argv, __doc__, extra_opts="ar", |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 643 | extra_long_opts=["add_missing", "rebuild_recovery", |
| 644 | "replace_verity_public_key=", |
| 645 | "replace_verity_private_key=", |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 646 | "is_signing"], |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 647 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 648 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 649 | |
| 650 | if len(args) != 1: |
| 651 | common.Usage(__doc__) |
| 652 | sys.exit(1) |
| 653 | |
| 654 | AddImagesToTargetFiles(args[0]) |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 655 | print("done.") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 656 | |
| 657 | if __name__ == '__main__': |
| 658 | try: |
| 659 | common.CloseInheritedPipes() |
| 660 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 661 | except common.ExternalError as e: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 662 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 663 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 664 | finally: |
| 665 | common.Cleanup() |