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