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 | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 65 | import sparse_img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 66 | |
| 67 | OPTIONS = common.OPTIONS |
| 68 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 69 | OPTIONS.add_missing = False |
| 70 | OPTIONS.rebuild_recovery = False |
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 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 75 | def GetCareMap(which, imgname): |
| 76 | """Generate care_map of system (or vendor) partition""" |
| 77 | |
| 78 | assert which in ("system", "vendor") |
| 79 | _, blk_device = common.GetTypeAndDevice("/" + which, OPTIONS.info_dict) |
| 80 | |
| 81 | simg = sparse_img.SparseImage(imgname) |
| 82 | care_map_list = [] |
| 83 | care_map_list.append(blk_device) |
| 84 | care_map_list.append(simg.care_map.to_string_raw()) |
| 85 | return care_map_list |
| 86 | |
| 87 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 88 | def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 89 | """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] | 90 | output_zip. Returns the name of the system image file.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 91 | |
| 92 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img") |
| 93 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 94 | print("system.img already exists in %s, no need to rebuild..." % (prefix,)) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 95 | return prebuilt_path |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 96 | |
| 97 | def output_sink(fn, data): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 98 | ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w") |
| 99 | ofile.write(data) |
| 100 | ofile.close() |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 101 | |
| 102 | if OPTIONS.rebuild_recovery: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 103 | print("Building new recovery patch") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 104 | common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, |
| 105 | boot_img, info_dict=OPTIONS.info_dict) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 106 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 107 | block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map") |
| 108 | imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict, |
| 109 | block_list=block_list) |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 110 | |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 111 | common.ZipWrite(output_zip, imgname, prefix + "system.img") |
| 112 | common.ZipWrite(output_zip, block_list, prefix + "system.map") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 113 | return imgname |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def BuildSystem(input_dir, info_dict, block_list=None): |
| 117 | """Build the (sparse) system image and return the name of a temp |
| 118 | file containing it.""" |
| 119 | return CreateImage(input_dir, info_dict, "system", block_list=block_list) |
| 120 | |
| 121 | |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 122 | def AddSystemOther(output_zip, prefix="IMAGES/"): |
| 123 | """Turn the contents of SYSTEM_OTHER into a system_other image |
| 124 | and store it in output_zip.""" |
| 125 | |
| 126 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system_other.img") |
| 127 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 128 | print("system_other.img already exists in %s, no need to rebuild..." % ( |
| 129 | prefix,)) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 130 | return |
| 131 | |
| 132 | imgname = BuildSystemOther(OPTIONS.input_tmp, OPTIONS.info_dict) |
| 133 | common.ZipWrite(output_zip, imgname, prefix + "system_other.img") |
| 134 | |
| 135 | def BuildSystemOther(input_dir, info_dict): |
| 136 | """Build the (sparse) system_other image and return the name of a temp |
| 137 | file containing it.""" |
| 138 | return CreateImage(input_dir, info_dict, "system_other", block_list=None) |
| 139 | |
| 140 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 141 | def AddVendor(output_zip, prefix="IMAGES/"): |
| 142 | """Turn the contents of VENDOR into a vendor image and store in it |
| 143 | output_zip.""" |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 144 | |
| 145 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img") |
| 146 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 147 | print("vendor.img already exists in %s, no need to rebuild..." % (prefix,)) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 148 | return prebuilt_path |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 149 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 150 | block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map") |
| 151 | imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict, |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 152 | block_list=block_list) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 153 | common.ZipWrite(output_zip, imgname, prefix + "vendor.img") |
| 154 | common.ZipWrite(output_zip, block_list, prefix + "vendor.map") |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 155 | return imgname |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 156 | |
| 157 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 158 | def BuildVendor(input_dir, info_dict, block_list=None): |
| 159 | """Build the (sparse) vendor image and return the name of a temp |
| 160 | file containing it.""" |
| 161 | return CreateImage(input_dir, info_dict, "vendor", block_list=block_list) |
| 162 | |
| 163 | |
| 164 | def CreateImage(input_dir, info_dict, what, block_list=None): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 165 | print("creating " + what + ".img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 166 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 167 | img = common.MakeTempFile(prefix=what + "-", suffix=".img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 168 | |
| 169 | # The name of the directory it is making an image out of matters to |
| 170 | # mkyaffs2image. It wants "system" but we have a directory named |
| 171 | # "SYSTEM", so create a symlink. |
| 172 | try: |
| 173 | os.symlink(os.path.join(input_dir, what.upper()), |
| 174 | os.path.join(input_dir, what)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 175 | except OSError as e: |
| 176 | # bogus error on my mac version? |
| 177 | # File "./build/tools/releasetools/img_from_target_files" |
| 178 | # os.path.join(OPTIONS.input_tmp, "system")) |
| 179 | # OSError: [Errno 17] File exists |
| 180 | if e.errno == errno.EEXIST: |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 181 | pass |
| 182 | |
| 183 | image_props = build_image.ImagePropFromGlobalDict(info_dict, what) |
| 184 | fstab = info_dict["fstab"] |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 185 | mount_point = "/" + what |
| 186 | if fstab and mount_point in fstab: |
| 187 | image_props["fs_type"] = fstab[mount_point].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 188 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 189 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 190 | # Bug: 24377993 |
| 191 | epoch = datetime.datetime.fromtimestamp(0) |
| 192 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 193 | image_props["timestamp"] = int(timestamp) |
| 194 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 195 | if what == "system": |
| 196 | fs_config_prefix = "" |
| 197 | else: |
| 198 | fs_config_prefix = what + "_" |
| 199 | |
| 200 | fs_config = os.path.join( |
| 201 | input_dir, "META/" + fs_config_prefix + "filesystem_config.txt") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 202 | if not os.path.exists(fs_config): |
| 203 | fs_config = None |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 204 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 205 | # Override values loaded from info_dict. |
| 206 | if fs_config: |
| 207 | image_props["fs_config"] = fs_config |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 208 | if block_list: |
| 209 | image_props["block_list"] = block_list |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 210 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 211 | succ = build_image.BuildImage(os.path.join(input_dir, what), |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 212 | image_props, img) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 213 | assert succ, "build " + what + ".img image failed" |
| 214 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 215 | return img |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def AddUserdata(output_zip, prefix="IMAGES/"): |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 219 | """Create a userdata image and store it in output_zip. |
| 220 | |
| 221 | In most case we just create and store an empty userdata.img; |
| 222 | But the invoker can also request to create userdata.img with real |
| 223 | data from the target files, by setting "userdata_img_with_data=true" |
| 224 | in OPTIONS.info_dict. |
| 225 | """ |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 226 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 227 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img") |
| 228 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 229 | print("userdata.img already exists in %s, no need to rebuild..." % ( |
| 230 | prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 231 | return |
| 232 | |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 233 | # Skip userdata.img if no size. |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 234 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data") |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 235 | if not image_props.get("partition_size"): |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 236 | return |
| 237 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 238 | print("creating userdata.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 239 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 240 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 241 | # Bug: 24377993 |
| 242 | epoch = datetime.datetime.fromtimestamp(0) |
| 243 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 244 | image_props["timestamp"] = int(timestamp) |
| 245 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 246 | # The name of the directory it is making an image out of matters to |
| 247 | # 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] | 248 | # empty dir named "data", or a symlink to the DATA dir, |
| 249 | # and build the image from that. |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 250 | temp_dir = tempfile.mkdtemp() |
| 251 | user_dir = os.path.join(temp_dir, "data") |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 252 | empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true") |
| 253 | if empty: |
| 254 | # Create an empty dir. |
| 255 | os.mkdir(user_dir) |
| 256 | else: |
| 257 | # Symlink to the DATA dir. |
| 258 | os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"), |
| 259 | user_dir) |
| 260 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 261 | img = tempfile.NamedTemporaryFile() |
| 262 | |
| 263 | fstab = OPTIONS.info_dict["fstab"] |
| 264 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 265 | image_props["fs_type"] = fstab["/data"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 266 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 267 | assert succ, "build userdata.img image failed" |
| 268 | |
| 269 | common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 270 | common.ZipWrite(output_zip, img.name, prefix + "userdata.img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 271 | img.close() |
Ying Wang | 2a04839 | 2015-06-25 13:56:53 -0700 | [diff] [blame] | 272 | shutil.rmtree(temp_dir) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 273 | |
| 274 | |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 275 | def AddVBMeta(output_zip, boot_img_path, system_img_path, prefix="IMAGES/"): |
| 276 | """Create a VBMeta image and store it in output_zip.""" |
| 277 | _, img_file_name = tempfile.mkstemp() |
| 278 | avbtool = os.getenv('AVBTOOL') or "avbtool" |
| 279 | cmd = [avbtool, "make_vbmeta_image", |
| 280 | "--output", img_file_name, |
| 281 | "--include_descriptors_from_image", boot_img_path, |
| 282 | "--include_descriptors_from_image", system_img_path, |
| 283 | "--generate_dm_verity_cmdline_from_hashtree", system_img_path] |
| 284 | common.AppendAVBSigningArgs(cmd) |
| 285 | args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args", None) |
| 286 | if args and args.strip(): |
| 287 | cmd.extend(shlex.split(args)) |
| 288 | p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 289 | p.communicate() |
| 290 | assert p.returncode == 0, "avbtool make_vbmeta_image failed" |
| 291 | common.ZipWrite(output_zip, img_file_name, prefix + "vbmeta.img") |
| 292 | |
| 293 | |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 294 | def AddPartitionTable(output_zip, prefix="IMAGES/"): |
| 295 | """Create a partition table image and store it in output_zip.""" |
| 296 | |
| 297 | _, img_file_name = tempfile.mkstemp() |
| 298 | _, bpt_file_name = tempfile.mkstemp() |
| 299 | |
| 300 | # use BPTTOOL from environ, or "bpttool" if empty or not set. |
| 301 | bpttool = os.getenv("BPTTOOL") or "bpttool" |
| 302 | cmd = [bpttool, "make_table", "--output_json", bpt_file_name, |
| 303 | "--output_gpt", img_file_name] |
| 304 | input_files_str = OPTIONS.info_dict["board_bpt_input_files"] |
| 305 | input_files = input_files_str.split(" ") |
| 306 | for i in input_files: |
| 307 | cmd.extend(["--input", i]) |
| 308 | disk_size = OPTIONS.info_dict.get("board_bpt_disk_size") |
| 309 | if disk_size: |
| 310 | cmd.extend(["--disk_size", disk_size]) |
| 311 | args = OPTIONS.info_dict.get("board_bpt_make_table_args") |
| 312 | if args: |
| 313 | cmd.extend(shlex.split(args)) |
| 314 | |
| 315 | p = common.Run(cmd, stdout=subprocess.PIPE) |
| 316 | p.communicate() |
| 317 | assert p.returncode == 0, "bpttool make_table failed" |
| 318 | |
| 319 | common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img") |
| 320 | common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt") |
| 321 | |
| 322 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 323 | def AddCache(output_zip, prefix="IMAGES/"): |
| 324 | """Create an empty cache image and store it in output_zip.""" |
| 325 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 326 | prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img") |
| 327 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 328 | print("cache.img already exists in %s, no need to rebuild..." % (prefix,)) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 329 | return |
| 330 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 331 | image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 332 | # The build system has to explicitly request for cache.img. |
| 333 | if "fs_type" not in image_props: |
| 334 | return |
| 335 | |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 336 | print("creating cache.img...") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 337 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 338 | # Use a fixed timestamp (01/01/2009) when packaging the image. |
| 339 | # Bug: 24377993 |
| 340 | epoch = datetime.datetime.fromtimestamp(0) |
| 341 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 342 | image_props["timestamp"] = int(timestamp) |
| 343 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 344 | # The name of the directory it is making an image out of matters to |
| 345 | # mkyaffs2image. So we create a temp dir, and within it we create an |
| 346 | # empty dir named "cache", and build the image from that. |
| 347 | temp_dir = tempfile.mkdtemp() |
| 348 | user_dir = os.path.join(temp_dir, "cache") |
| 349 | os.mkdir(user_dir) |
| 350 | img = tempfile.NamedTemporaryFile() |
| 351 | |
| 352 | fstab = OPTIONS.info_dict["fstab"] |
| 353 | if fstab: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 354 | image_props["fs_type"] = fstab["/cache"].fs_type |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 355 | succ = build_image.BuildImage(user_dir, image_props, img.name) |
| 356 | assert succ, "build cache.img image failed" |
| 357 | |
| 358 | common.CheckSize(img.name, "cache.img", OPTIONS.info_dict) |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 359 | common.ZipWrite(output_zip, img.name, prefix + "cache.img") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 360 | img.close() |
| 361 | os.rmdir(user_dir) |
| 362 | os.rmdir(temp_dir) |
| 363 | |
| 364 | |
| 365 | def AddImagesToTargetFiles(filename): |
| 366 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 367 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 368 | if not OPTIONS.add_missing: |
| 369 | for n in input_zip.namelist(): |
| 370 | if n.startswith("IMAGES/"): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 371 | print("target_files appears to already contain images.") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 372 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 373 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 374 | try: |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 375 | input_zip.getinfo("VENDOR/") |
| 376 | has_vendor = True |
| 377 | except KeyError: |
| 378 | has_vendor = False |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 379 | |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 380 | has_system_other = "SYSTEM_OTHER/" in input_zip.namelist() |
| 381 | |
Tao Bao | 2c15d9e | 2015-07-09 11:51:16 -0700 | [diff] [blame] | 382 | OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 383 | |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 384 | common.ZipClose(input_zip) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 385 | output_zip = zipfile.ZipFile(filename, "a", |
Tao Bao | 9c84e50 | 2016-08-22 10:31:05 -0700 | [diff] [blame] | 386 | compression=zipfile.ZIP_DEFLATED, |
| 387 | allowZip64=True) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 388 | |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 389 | has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 390 | system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 391 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 392 | def banner(s): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 393 | print("\n\n++++ " + s + " ++++\n\n") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 394 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 395 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img") |
| 396 | boot_image = None |
| 397 | if os.path.exists(prebuilt_path): |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 398 | banner("boot") |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 399 | print("boot.img already exists in IMAGES/, no need to rebuild...") |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 400 | if OPTIONS.rebuild_recovery: |
| 401 | boot_image = common.GetBootableImage( |
| 402 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
| 403 | else: |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 404 | banner("boot") |
| 405 | boot_image = common.GetBootableImage( |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 406 | "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 407 | if boot_image: |
| 408 | boot_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 409 | |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 410 | recovery_image = None |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 411 | if has_recovery: |
| 412 | banner("recovery") |
| 413 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img") |
| 414 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 415 | print("recovery.img already exists in IMAGES/, no need to rebuild...") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 416 | if OPTIONS.rebuild_recovery: |
| 417 | recovery_image = common.GetBootableImage( |
| 418 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, |
| 419 | "RECOVERY") |
| 420 | else: |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 421 | recovery_image = common.GetBootableImage( |
| 422 | "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") |
Tao Bao | db45efa | 2015-10-27 19:25:18 -0700 | [diff] [blame] | 423 | if recovery_image: |
| 424 | recovery_image.AddToZip(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 425 | |
Tao Bao | d42e97e | 2016-11-30 12:11:57 -0800 | [diff] [blame] | 426 | banner("recovery (two-step image)") |
| 427 | # The special recovery.img for two-step package use. |
| 428 | recovery_two_step_image = common.GetBootableImage( |
| 429 | "IMAGES/recovery-two-step.img", "recovery-two-step.img", |
| 430 | OPTIONS.input_tmp, "RECOVERY", two_step_image=True) |
| 431 | if recovery_two_step_image: |
| 432 | recovery_two_step_image.AddToZip(output_zip) |
| 433 | |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 434 | banner("system") |
David Zeuthen | d995f4b | 2016-01-29 16:59:17 -0500 | [diff] [blame] | 435 | system_img_path = AddSystem( |
| 436 | output_zip, recovery_img=recovery_image, boot_img=boot_image) |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 437 | vendor_img_path = None |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 438 | if has_vendor: |
| 439 | banner("vendor") |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 440 | vendor_img_path = AddVendor(output_zip) |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 441 | if has_system_other: |
| 442 | banner("system_other") |
| 443 | AddSystemOther(output_zip) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 444 | if not OPTIONS.is_signing: |
| 445 | banner("userdata") |
| 446 | AddUserdata(output_zip) |
| 447 | banner("cache") |
| 448 | AddCache(output_zip) |
David Zeuthen | 2532862 | 2016-04-08 15:08:03 -0400 | [diff] [blame] | 449 | if OPTIONS.info_dict.get("board_bpt_enable", None) == "true": |
| 450 | banner("partition-table") |
| 451 | AddPartitionTable(output_zip) |
David Zeuthen | 2ce63ed | 2016-09-15 13:43:54 -0400 | [diff] [blame] | 452 | if OPTIONS.info_dict.get("board_avb_enable", None) == "true": |
| 453 | banner("vbmeta") |
| 454 | boot_contents = boot_image.WriteToTemp() |
| 455 | AddVBMeta(output_zip, boot_contents.name, system_img_path) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 456 | |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 457 | # For devices using A/B update, copy over images from RADIO/ and/or |
| 458 | # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed |
| 459 | # images ready under IMAGES/. All images should have '.img' as extension. |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 460 | banner("radio") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 461 | ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt") |
| 462 | if os.path.exists(ab_partitions): |
| 463 | with open(ab_partitions, 'r') as f: |
| 464 | lines = f.readlines() |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 465 | # For devices using A/B update, generate care_map for system and vendor |
| 466 | # partitions (if present), then write this file to target_files package. |
| 467 | care_map_list = [] |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 468 | for line in lines: |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 469 | if line.strip() == "system" and OPTIONS.info_dict.get( |
| 470 | "system_verity_block_device", None) is not None: |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 471 | assert os.path.exists(system_img_path) |
| 472 | care_map_list += GetCareMap("system", system_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 473 | if line.strip() == "vendor" and OPTIONS.info_dict.get( |
| 474 | "vendor_verity_block_device", None) is not None: |
Tianjie Xu | 737afb9 | 2016-07-11 11:42:53 -0700 | [diff] [blame] | 475 | assert os.path.exists(vendor_img_path) |
| 476 | care_map_list += GetCareMap("vendor", vendor_img_path) |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 477 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 478 | img_name = line.strip() + ".img" |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 479 | prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name) |
| 480 | if os.path.exists(prebuilt_path): |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 481 | print("%s already exists, no need to overwrite..." % (img_name,)) |
Tianjie Xu | aaca421 | 2016-06-28 14:34:03 -0700 | [diff] [blame] | 482 | continue |
| 483 | |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 484 | img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 485 | img_vendor_dir = os.path.join( |
| 486 | OPTIONS.input_tmp, "VENDOR_IMAGES") |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 487 | if os.path.exists(img_radio_path): |
| 488 | common.ZipWrite(output_zip, img_radio_path, |
| 489 | os.path.join("IMAGES", img_name)) |
Wei Wang | 2e735ca | 2016-05-10 22:48:13 -0700 | [diff] [blame] | 490 | else: |
| 491 | for root, _, files in os.walk(img_vendor_dir): |
| 492 | if img_name in files: |
| 493 | common.ZipWrite(output_zip, os.path.join(root, img_name), |
| 494 | os.path.join("IMAGES", img_name)) |
| 495 | break |
Tao Bao | a0421cd | 2015-11-16 16:32:27 -0800 | [diff] [blame] | 496 | |
| 497 | # Zip spec says: All slashes MUST be forward slashes. |
| 498 | img_path = 'IMAGES/' + img_name |
| 499 | assert img_path in output_zip.namelist(), "cannot find " + img_name |
| 500 | |
Tianjie Xu | cfa8622 | 2016-03-07 16:31:19 -0800 | [diff] [blame] | 501 | if care_map_list: |
| 502 | file_path = "META/care_map.txt" |
| 503 | common.ZipWriteStr(output_zip, file_path, '\n'.join(care_map_list)) |
| 504 | |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 505 | common.ZipClose(output_zip) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 506 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 507 | def main(argv): |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 508 | def option_handler(o, a): |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 509 | if o in ("-a", "--add_missing"): |
| 510 | OPTIONS.add_missing = True |
| 511 | elif o in ("-r", "--rebuild_recovery",): |
| 512 | OPTIONS.rebuild_recovery = True |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 513 | elif o == "--replace_verity_private_key": |
| 514 | OPTIONS.replace_verity_private_key = (True, a) |
| 515 | elif o == "--replace_verity_public_key": |
| 516 | OPTIONS.replace_verity_public_key = (True, a) |
Tianjie Xu | b48589a | 2016-08-03 19:21:52 -0700 | [diff] [blame] | 517 | elif o == "--is_signing": |
| 518 | OPTIONS.is_signing = True |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 519 | else: |
| 520 | return False |
| 521 | return True |
| 522 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 523 | args = common.ParseOptions( |
| 524 | argv, __doc__, extra_opts="ar", |
Baligh Uddin | 59f4ff1 | 2015-09-16 21:20:30 -0700 | [diff] [blame] | 525 | extra_long_opts=["add_missing", "rebuild_recovery", |
| 526 | "replace_verity_public_key=", |
| 527 | "replace_verity_private_key=", |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 528 | "is_signing"], |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 529 | extra_option_handler=option_handler) |
Michael Runge | 2e0d8fc | 2014-11-13 21:41:08 -0800 | [diff] [blame] | 530 | |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 531 | |
| 532 | if len(args) != 1: |
| 533 | common.Usage(__doc__) |
| 534 | sys.exit(1) |
| 535 | |
| 536 | AddImagesToTargetFiles(args[0]) |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 537 | print("done.") |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 538 | |
| 539 | if __name__ == '__main__': |
| 540 | try: |
| 541 | common.CloseInheritedPipes() |
| 542 | main(sys.argv[1:]) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 543 | except common.ExternalError as e: |
Tao Bao | 89fbb0f | 2017-01-10 10:47:58 -0800 | [diff] [blame] | 544 | print("\n ERROR: %s\n" % (e,)) |
Doug Zongker | 3c84f56 | 2014-07-31 11:06:30 -0700 | [diff] [blame] | 545 | sys.exit(1) |
Doug Zongker | fc44a51 | 2014-08-26 13:10:25 -0700 | [diff] [blame] | 546 | finally: |
| 547 | common.Cleanup() |