Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2011 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 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 18 | Builds output_image from the given input_directory, properties_file, |
| 19 | and writes the image to target_output_directory. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 20 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 21 | If argument generated_prop_file exists, write additional properties to the file. |
| 22 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 23 | Usage: build_image.py input_directory properties_file output_image \\ |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 24 | target_output_directory [generated_prop_file] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 25 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 26 | |
| 27 | from __future__ import print_function |
| 28 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 29 | import os |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 30 | import os.path |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 31 | import re |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 32 | import shlex |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 33 | import shutil |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 34 | import sys |
| 35 | |
| 36 | import common |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 37 | import sparse_img |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 38 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 39 | |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 40 | OPTIONS = common.OPTIONS |
| 41 | |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 42 | FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 43 | BLOCK_SIZE = 4096 |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 44 | BYTES_IN_MB = 1024 * 1024 |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 45 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 46 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 47 | class BuildImageError(Exception): |
| 48 | """An Exception raised during image building.""" |
| 49 | |
| 50 | def __init__(self, message): |
| 51 | Exception.__init__(self, message) |
| 52 | |
| 53 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 54 | def GetVerityFECSize(partition_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 55 | cmd = ["fec", "-s", str(partition_size)] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 56 | output = common.RunAndCheckOutput(cmd, verbose=False) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 57 | return int(output) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 58 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 59 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 60 | def GetVerityTreeSize(partition_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 61 | cmd = ["build_verity_tree", "-s", str(partition_size)] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 62 | output = common.RunAndCheckOutput(cmd, verbose=False) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 63 | return int(output) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 64 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 65 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 66 | def GetVerityMetadataSize(partition_size): |
Tao Bao | b4ec6d7 | 2018-03-15 23:21:28 -0700 | [diff] [blame] | 67 | cmd = ["build_verity_metadata.py", "size", str(partition_size)] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 68 | output = common.RunAndCheckOutput(cmd, verbose=False) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 69 | return int(output) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 70 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 71 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 72 | def GetVeritySize(partition_size, fec_supported): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 73 | verity_tree_size = GetVerityTreeSize(partition_size) |
| 74 | verity_metadata_size = GetVerityMetadataSize(partition_size) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 75 | verity_size = verity_tree_size + verity_metadata_size |
| 76 | if fec_supported: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 77 | fec_size = GetVerityFECSize(partition_size + verity_size) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 78 | return verity_size + fec_size |
| 79 | return verity_size |
| 80 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 81 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 82 | def GetDiskUsage(path): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 83 | """Returns the number of bytes that "path" occupies on host. |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 84 | |
| 85 | Args: |
| 86 | path: The directory or file to calculate size on |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 87 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 88 | Returns: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 89 | The number of bytes. |
| 90 | |
| 91 | Raises: |
| 92 | BuildImageError: On error. |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 93 | """ |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 94 | env_copy = os.environ.copy() |
| 95 | env_copy["POSIXLY_CORRECT"] = "1" |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 96 | cmd = ["du", "-s", path] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 97 | try: |
| 98 | output = common.RunAndCheckOutput(cmd, verbose=False, env=env_copy) |
| 99 | except common.ExternalError: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 100 | raise BuildImageError("Failed to get disk usage:\n{}".format(output)) |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 101 | # POSIX du returns number of blocks with block size 512 |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 102 | return int(output.split()[0]) * 512 |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 103 | |
| 104 | |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 105 | def GetSimgSize(image_file): |
| 106 | simg = sparse_img.SparseImage(image_file, build_map=False) |
| 107 | return simg.blocksize * simg.total_blocks |
| 108 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 109 | |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 110 | def ZeroPadSimg(image_file, pad_size): |
| 111 | blocks = pad_size // BLOCK_SIZE |
| 112 | print("Padding %d blocks (%d bytes)" % (blocks, pad_size)) |
| 113 | simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False) |
| 114 | simg.AppendFillChunk(0, blocks) |
| 115 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 116 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 117 | def AVBCalcMaxImageSize(avbtool, footer_type, partition_size, additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 118 | """Calculates max image size for a given partition size. |
| 119 | |
| 120 | Args: |
| 121 | avbtool: String with path to avbtool. |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 122 | footer_type: 'hash' or 'hashtree' for generating footer. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 123 | partition_size: The size of the partition in question. |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 124 | additional_args: Additional arguments to pass to "avbtool add_hash_footer" |
| 125 | or "avbtool add_hashtree_footer". |
| 126 | |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 127 | Returns: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 128 | The maximum image size. |
| 129 | |
| 130 | Raises: |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 131 | BuildImageError: On invalid image size. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 132 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 133 | cmd = [avbtool, "add_%s_footer" % footer_type, |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 134 | "--partition_size", str(partition_size), "--calc_max_image_size"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 135 | cmd.extend(shlex.split(additional_args)) |
| 136 | |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 137 | output = common.RunAndCheckOutput(cmd) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 138 | image_size = int(output) |
| 139 | if image_size <= 0: |
| 140 | raise BuildImageError( |
| 141 | "Invalid max image size: {}".format(output)) |
| 142 | return image_size |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 143 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 144 | |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 145 | def AVBCalcMinPartitionSize(image_size, size_calculator): |
| 146 | """Calculates min partition size for a given image size. |
| 147 | |
| 148 | Args: |
| 149 | image_size: The size of the image in question. |
| 150 | size_calculator: The function to calculate max image size |
| 151 | for a given partition size. |
| 152 | |
| 153 | Returns: |
| 154 | The minimum partition size required to accommodate the image size. |
| 155 | """ |
| 156 | # Use image size as partition size to approximate final partition size. |
| 157 | image_ratio = size_calculator(image_size) / float(image_size) |
| 158 | |
| 159 | # Prepare a binary search for the optimal partition size. |
| 160 | lo = int(image_size / image_ratio) // BLOCK_SIZE * BLOCK_SIZE - BLOCK_SIZE |
| 161 | |
| 162 | # Ensure lo is small enough: max_image_size should <= image_size. |
| 163 | delta = BLOCK_SIZE |
| 164 | max_image_size = size_calculator(lo) |
| 165 | while max_image_size > image_size: |
| 166 | image_ratio = max_image_size / float(lo) |
| 167 | lo = int(image_size / image_ratio) // BLOCK_SIZE * BLOCK_SIZE - delta |
| 168 | delta *= 2 |
| 169 | max_image_size = size_calculator(lo) |
| 170 | |
| 171 | hi = lo + BLOCK_SIZE |
| 172 | |
| 173 | # Ensure hi is large enough: max_image_size should >= image_size. |
| 174 | delta = BLOCK_SIZE |
| 175 | max_image_size = size_calculator(hi) |
| 176 | while max_image_size < image_size: |
| 177 | image_ratio = max_image_size / float(hi) |
| 178 | hi = int(image_size / image_ratio) // BLOCK_SIZE * BLOCK_SIZE + delta |
| 179 | delta *= 2 |
| 180 | max_image_size = size_calculator(hi) |
| 181 | |
| 182 | partition_size = hi |
| 183 | |
| 184 | # Start to binary search. |
| 185 | while lo < hi: |
| 186 | mid = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE |
| 187 | max_image_size = size_calculator(mid) |
| 188 | if max_image_size >= image_size: # if mid can accommodate image_size |
| 189 | if mid < partition_size: # if a smaller partition size is found |
| 190 | partition_size = mid |
| 191 | hi = mid |
| 192 | else: |
| 193 | lo = mid + BLOCK_SIZE |
| 194 | |
| 195 | if OPTIONS.verbose: |
| 196 | print("AVBCalcMinPartitionSize({}): partition_size: {}.".format( |
| 197 | image_size, partition_size)) |
| 198 | |
| 199 | return partition_size |
| 200 | |
| 201 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 202 | def AVBAddFooter(image_path, avbtool, footer_type, partition_size, |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 203 | partition_name, key_path, algorithm, salt, |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 204 | additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 205 | """Adds dm-verity hashtree and AVB metadata to an image. |
| 206 | |
| 207 | Args: |
| 208 | image_path: Path to image to modify. |
| 209 | avbtool: String with path to avbtool. |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 210 | footer_type: 'hash' or 'hashtree' for generating footer. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 211 | partition_size: The size of the partition in question. |
| 212 | partition_name: The name of the partition - will be embedded in metadata. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 213 | key_path: Path to key to use or None. |
| 214 | algorithm: Name of algorithm to use or None. |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 215 | salt: The salt to use (a hexadecimal string) or None. |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 216 | additional_args: Additional arguments to pass to "avbtool add_hash_footer" |
| 217 | or "avbtool add_hashtree_footer". |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 218 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 219 | cmd = [avbtool, "add_%s_footer" % footer_type, |
| 220 | "--partition_size", partition_size, |
| 221 | "--partition_name", partition_name, |
| 222 | "--image", image_path] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 223 | |
| 224 | if key_path and algorithm: |
| 225 | cmd.extend(["--key", key_path, "--algorithm", algorithm]) |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 226 | if salt: |
| 227 | cmd.extend(["--salt", salt]) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 228 | |
| 229 | cmd.extend(shlex.split(additional_args)) |
| 230 | |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 231 | common.RunAndCheckOutput(cmd) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 232 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 233 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 234 | def AdjustPartitionSizeForVerity(partition_size, fec_supported): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 235 | """Modifies the provided partition size to account for the verity metadata. |
| 236 | |
| 237 | This information is used to size the created image appropriately. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 238 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 239 | Args: |
| 240 | partition_size: the size of the partition to be verified. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 241 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 242 | Returns: |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 243 | A tuple of the size of the partition adjusted for verity metadata, and |
| 244 | the size of verity metadata. |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 245 | """ |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 246 | key = "%d %d" % (partition_size, fec_supported) |
| 247 | if key in AdjustPartitionSizeForVerity.results: |
| 248 | return AdjustPartitionSizeForVerity.results[key] |
| 249 | |
| 250 | hi = partition_size |
| 251 | if hi % BLOCK_SIZE != 0: |
| 252 | hi = (hi // BLOCK_SIZE) * BLOCK_SIZE |
| 253 | |
| 254 | # verity tree and fec sizes depend on the partition size, which |
| 255 | # means this estimate is always going to be unnecessarily small |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 256 | verity_size = GetVeritySize(hi, fec_supported) |
| 257 | lo = partition_size - verity_size |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 258 | result = lo |
| 259 | |
| 260 | # do a binary search for the optimal size |
| 261 | while lo < hi: |
| 262 | i = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 263 | v = GetVeritySize(i, fec_supported) |
| 264 | if i + v <= partition_size: |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 265 | if result < i: |
| 266 | result = i |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 267 | verity_size = v |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 268 | lo = i + BLOCK_SIZE |
| 269 | else: |
| 270 | hi = i |
| 271 | |
Tomasz Wasilczyk | 29ec06b | 2017-11-15 10:34:01 -0800 | [diff] [blame] | 272 | if OPTIONS.verbose: |
| 273 | print("Adjusted partition size for verity, partition_size: {}," |
| 274 | " verity_size: {}".format(result, verity_size)) |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 275 | AdjustPartitionSizeForVerity.results[key] = (result, verity_size) |
| 276 | return (result, verity_size) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 277 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 278 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 279 | AdjustPartitionSizeForVerity.results = {} |
| 280 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 281 | |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 282 | def BuildVerityFEC(sparse_image_path, verity_path, verity_fec_path, |
| 283 | padding_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 284 | cmd = ["fec", "-e", "-p", str(padding_size), sparse_image_path, |
| 285 | verity_path, verity_fec_path] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 286 | common.RunAndCheckOutput(cmd) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 287 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 288 | |
Tao Bao | 2f05746 | 2018-10-03 16:31:18 -0700 | [diff] [blame] | 289 | def BuildVerityTree(sparse_image_path, verity_image_path): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 290 | cmd = ["build_verity_tree", "-A", FIXED_SALT, sparse_image_path, |
| 291 | verity_image_path] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 292 | output = common.RunAndCheckOutput(cmd) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 293 | root, salt = output.split() |
Tao Bao | 2f05746 | 2018-10-03 16:31:18 -0700 | [diff] [blame] | 294 | return root, salt |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 295 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 296 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 297 | def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 298 | block_device, signer_path, key, signer_args, |
| 299 | verity_disable): |
Tao Bao | b4ec6d7 | 2018-03-15 23:21:28 -0700 | [diff] [blame] | 300 | cmd = ["build_verity_metadata.py", "build", str(image_size), |
| 301 | verity_metadata_path, root_hash, salt, block_device, signer_path, key] |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 302 | if signer_args: |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 303 | cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),)) |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 304 | if verity_disable: |
| 305 | cmd.append("--verity_disable") |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 306 | common.RunAndCheckOutput(cmd) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 307 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 308 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 309 | def Append2Simg(sparse_image_path, unsparse_image_path, error_message): |
| 310 | """Appends the unsparse image to the given sparse image. |
| 311 | |
| 312 | Args: |
| 313 | sparse_image_path: the path to the (sparse) image |
| 314 | unsparse_image_path: the path to the (unsparse) image |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 315 | |
| 316 | Raises: |
| 317 | BuildImageError: On error. |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 318 | """ |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 319 | cmd = ["append2simg", sparse_image_path, unsparse_image_path] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 320 | try: |
| 321 | common.RunAndCheckOutput(cmd) |
| 322 | except: |
| 323 | raise BuildImageError(error_message) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 324 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 325 | |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 326 | def Append(target, file_to_append, error_message): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 327 | """Appends file_to_append to target. |
| 328 | |
| 329 | Raises: |
| 330 | BuildImageError: On error. |
| 331 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 332 | try: |
| 333 | with open(target, "a") as out_file, open(file_to_append, "r") as input_file: |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 334 | for line in input_file: |
| 335 | out_file.write(line) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 336 | except IOError: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 337 | raise BuildImageError(error_message) |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 338 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 339 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 340 | def BuildVerifiedImage(data_image_path, verity_image_path, |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 341 | verity_metadata_path, verity_fec_path, |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 342 | padding_size, fec_supported): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 343 | Append( |
| 344 | verity_image_path, verity_metadata_path, |
| 345 | "Could not append verity metadata!") |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 346 | |
| 347 | if fec_supported: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 348 | # Build FEC for the entire partition, including metadata. |
| 349 | BuildVerityFEC( |
| 350 | data_image_path, verity_image_path, verity_fec_path, padding_size) |
| 351 | Append(verity_image_path, verity_fec_path, "Could not append FEC!") |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 352 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 353 | Append2Simg( |
| 354 | data_image_path, verity_image_path, "Could not append verity data!") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 355 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 356 | |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 357 | def UnsparseImage(sparse_image_path, replace=True): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 358 | img_dir = os.path.dirname(sparse_image_path) |
| 359 | unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path) |
| 360 | unsparse_image_path = os.path.join(img_dir, unsparse_image_path) |
| 361 | if os.path.exists(unsparse_image_path): |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 362 | if replace: |
| 363 | os.unlink(unsparse_image_path) |
| 364 | else: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 365 | return unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 366 | inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 367 | try: |
| 368 | common.RunAndCheckOutput(inflate_command) |
| 369 | except: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 370 | os.remove(unsparse_image_path) |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 371 | raise |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 372 | return unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 373 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 374 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 375 | def MakeVerityEnabledImage(out_file, fec_supported, prop_dict): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 376 | """Creates an image that is verifiable using dm-verity. |
| 377 | |
| 378 | Args: |
| 379 | out_file: the location to write the verifiable image at |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 380 | prop_dict: a dictionary of properties required for image creation and |
| 381 | verification |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 382 | |
| 383 | Raises: |
| 384 | AssertionError: On invalid partition sizes. |
| 385 | BuildImageError: On other errors. |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 386 | """ |
| 387 | # get properties |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 388 | image_size = int(prop_dict["image_size"]) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 389 | block_dev = prop_dict["verity_block_device"] |
Paul Lawrence | a37b2bb | 2014-11-13 17:54:30 -0800 | [diff] [blame] | 390 | signer_key = prop_dict["verity_key"] + ".pk8" |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 391 | if OPTIONS.verity_signer_path is not None: |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 392 | signer_path = OPTIONS.verity_signer_path |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 393 | else: |
| 394 | signer_path = prop_dict["verity_signer_cmd"] |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 395 | signer_args = OPTIONS.verity_signer_args |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 396 | |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 397 | tempdir_name = common.MakeTempDir(suffix="_verity_images") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 398 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 399 | # Get partial image paths. |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 400 | verity_image_path = os.path.join(tempdir_name, "verity.img") |
| 401 | verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img") |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 402 | verity_fec_path = os.path.join(tempdir_name, "verity_fec.img") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 403 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 404 | # Build the verity tree and get the root hash and salt. |
Tao Bao | 2f05746 | 2018-10-03 16:31:18 -0700 | [diff] [blame] | 405 | root_hash, salt = BuildVerityTree(out_file, verity_image_path) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 406 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 407 | # Build the metadata blocks. |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 408 | verity_disable = "verity_disable" in prop_dict |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 409 | BuildVerityMetadata( |
| 410 | image_size, verity_metadata_path, root_hash, salt, block_dev, signer_path, |
| 411 | signer_key, signer_args, verity_disable) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 412 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 413 | # Build the full verified image. |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 414 | partition_size = int(prop_dict["partition_size"]) |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 415 | verity_size = int(prop_dict["verity_size"]) |
| 416 | |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 417 | padding_size = partition_size - image_size - verity_size |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 418 | assert padding_size >= 0 |
| 419 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 420 | BuildVerifiedImage( |
| 421 | out_file, verity_image_path, verity_metadata_path, verity_fec_path, |
| 422 | padding_size, fec_supported) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 423 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 424 | |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 425 | def ConvertBlockMapToBaseFs(block_map_file): |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 426 | base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 427 | convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file] |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 428 | common.RunAndCheckOutput(convert_command) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 429 | return base_fs_file |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 430 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 431 | |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 432 | def SetUpInDirAndFsConfig(origin_in, prop_dict): |
| 433 | """Returns the in_dir and fs_config that should be used for image building. |
| 434 | |
Tom Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 435 | When building system.img for all targets, it creates and returns a staged dir |
| 436 | that combines the contents of /system (i.e. in the given in_dir) and root. |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 437 | |
| 438 | Args: |
| 439 | origin_in: Path to the input directory. |
| 440 | prop_dict: A property dict that contains info like partition size. Values |
| 441 | may be updated. |
| 442 | |
| 443 | Returns: |
| 444 | A tuple of in_dir and fs_config that should be used to build the image. |
| 445 | """ |
| 446 | fs_config = prop_dict.get("fs_config") |
Tom Cherry | d14b895 | 2018-08-09 14:26:00 -0700 | [diff] [blame] | 447 | |
| 448 | if prop_dict["mount_point"] == "system_other": |
| 449 | prop_dict["mount_point"] = "system" |
| 450 | return origin_in, fs_config |
| 451 | |
| 452 | if prop_dict["mount_point"] != "system": |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 453 | return origin_in, fs_config |
| 454 | |
| 455 | # Construct a staging directory of the root file system. |
| 456 | in_dir = common.MakeTempDir() |
| 457 | root_dir = prop_dict.get("root_dir") |
| 458 | if root_dir: |
| 459 | shutil.rmtree(in_dir) |
| 460 | shutil.copytree(root_dir, in_dir, symlinks=True) |
| 461 | in_dir_system = os.path.join(in_dir, "system") |
| 462 | shutil.rmtree(in_dir_system, ignore_errors=True) |
| 463 | shutil.copytree(origin_in, in_dir_system, symlinks=True) |
| 464 | |
| 465 | # Change the mount point to "/". |
| 466 | prop_dict["mount_point"] = "/" |
| 467 | if fs_config: |
| 468 | # We need to merge the fs_config files of system and root. |
| 469 | merged_fs_config = common.MakeTempFile( |
| 470 | prefix="merged_fs_config", suffix=".txt") |
| 471 | with open(merged_fs_config, "w") as fw: |
| 472 | if "root_fs_config" in prop_dict: |
| 473 | with open(prop_dict["root_fs_config"]) as fr: |
| 474 | fw.writelines(fr.readlines()) |
| 475 | with open(fs_config) as fr: |
| 476 | fw.writelines(fr.readlines()) |
| 477 | fs_config = merged_fs_config |
| 478 | return in_dir, fs_config |
| 479 | |
| 480 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 481 | def CheckHeadroom(ext4fs_output, prop_dict): |
| 482 | """Checks if there's enough headroom space available. |
| 483 | |
| 484 | Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM), |
| 485 | which is useful for devices with low disk space that have system image |
| 486 | variation between builds. The 'partition_headroom' in prop_dict is the size |
| 487 | in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks. |
| 488 | |
| 489 | Args: |
| 490 | ext4fs_output: The output string from mke2fs command. |
| 491 | prop_dict: The property dict. |
| 492 | |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 493 | Raises: |
| 494 | AssertionError: On invalid input. |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 495 | BuildImageError: On check failure. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 496 | """ |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 497 | assert ext4fs_output is not None |
| 498 | assert prop_dict.get('fs_type', '').startswith('ext4') |
| 499 | assert 'partition_headroom' in prop_dict |
| 500 | assert 'mount_point' in prop_dict |
| 501 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 502 | ext4fs_stats = re.compile( |
| 503 | r'Created filesystem with .* (?P<used_blocks>[0-9]+)/' |
| 504 | r'(?P<total_blocks>[0-9]+) blocks') |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 505 | last_line = ext4fs_output.strip().split('\n')[-1] |
| 506 | m = ext4fs_stats.match(last_line) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 507 | used_blocks = int(m.groupdict().get('used_blocks')) |
| 508 | total_blocks = int(m.groupdict().get('total_blocks')) |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 509 | headroom_blocks = int(prop_dict['partition_headroom']) / BLOCK_SIZE |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 510 | adjusted_blocks = total_blocks - headroom_blocks |
| 511 | if used_blocks > adjusted_blocks: |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 512 | mount_point = prop_dict["mount_point"] |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 513 | raise BuildImageError( |
| 514 | "Error: Not enough room on {} (total: {} blocks, used: {} blocks, " |
| 515 | "headroom: {} blocks, available: {} blocks)".format( |
| 516 | mount_point, total_blocks, used_blocks, headroom_blocks, |
| 517 | adjusted_blocks)) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 518 | |
| 519 | |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 520 | def BuildImage(in_dir, prop_dict, out_file, target_out=None): |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 521 | """Builds an image for the files under in_dir and writes it to out_file. |
| 522 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 523 | Args: |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 524 | in_dir: Path to input directory. |
| 525 | prop_dict: A property dict that contains info like partition size. Values |
| 526 | will be updated with computed values. |
| 527 | out_file: The output image file. |
| 528 | target_out: Path to the TARGET_OUT directory as in Makefile. It actually |
| 529 | points to the /system directory under PRODUCT_OUT. fs_config (the one |
| 530 | under system/core/libcutils) reads device specific FS config files from |
| 531 | there. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 532 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 533 | Raises: |
| 534 | BuildImageError: On build image failures. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 535 | """ |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 536 | in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 537 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 538 | build_command = [] |
| 539 | fs_type = prop_dict.get("fs_type", "") |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 540 | run_e2fsck = False |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 541 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 542 | fs_spans_partition = True |
| 543 | if fs_type.startswith("squash"): |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 544 | fs_spans_partition = False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 545 | |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 546 | is_verity_partition = "verity_block_device" in prop_dict |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 547 | verity_supported = prop_dict.get("verity") == "true" |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 548 | verity_fec_supported = prop_dict.get("verity_fec") == "true" |
| 549 | |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 550 | avb_footer_type = None |
| 551 | if prop_dict.get("avb_hash_enable") == "true": |
| 552 | avb_footer_type = "hash" |
| 553 | elif prop_dict.get("avb_hashtree_enable") == "true": |
| 554 | avb_footer_type = "hashtree" |
| 555 | |
| 556 | if avb_footer_type: |
| 557 | avbtool = prop_dict.get("avb_avbtool") |
| 558 | avb_signing_args = prop_dict.get( |
| 559 | "avb_add_" + avb_footer_type + "_footer_args") |
| 560 | |
Yifan Hong | 2dae572 | 2018-07-31 12:47:27 -0700 | [diff] [blame] | 561 | if (prop_dict.get("use_dynamic_partition_size") == "true" and |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 562 | "partition_size" not in prop_dict): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 563 | # If partition_size is not defined, use output of `du' + reserved_size. |
| 564 | size = GetDiskUsage(in_dir) |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 565 | if OPTIONS.verbose: |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 566 | print("The tree size of %s is %d MB." % (in_dir, size // BYTES_IN_MB)) |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 567 | size += int(prop_dict.get("partition_reserved_size", 0)) |
| 568 | # Round this up to a multiple of 4K so that avbtool works |
| 569 | size = common.RoundUpTo4K(size) |
Bowgo Tsai | 040410c | 2018-09-20 16:40:01 +0800 | [diff] [blame] | 570 | # Adjust partition_size to add more space for AVB footer, to prevent |
| 571 | # it from consuming partition_reserved_size. |
| 572 | if avb_footer_type: |
| 573 | size = AVBCalcMinPartitionSize( |
| 574 | size, |
| 575 | lambda x: AVBCalcMaxImageSize( |
| 576 | avbtool, avb_footer_type, x, avb_signing_args)) |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 577 | prop_dict["partition_size"] = str(size) |
| 578 | if OPTIONS.verbose: |
| 579 | print("Allocating %d MB for %s." % (size // BYTES_IN_MB, out_file)) |
| 580 | |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 581 | prop_dict["image_size"] = prop_dict["partition_size"] |
| 582 | |
| 583 | # Adjust the image size to make room for the hashes if this is to be verified. |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 584 | if verity_supported and is_verity_partition: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 585 | partition_size = int(prop_dict.get("partition_size")) |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 586 | image_size, verity_size = AdjustPartitionSizeForVerity( |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 587 | partition_size, verity_fec_supported) |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 588 | prop_dict["image_size"] = str(image_size) |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 589 | prop_dict["verity_size"] = str(verity_size) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 590 | |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 591 | # Adjust the image size for AVB hash footer or AVB hashtree footer. |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 592 | if avb_footer_type: |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 593 | partition_size = prop_dict["partition_size"] |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 594 | # avb_add_hash_footer_args or avb_add_hashtree_footer_args. |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 595 | max_image_size = AVBCalcMaxImageSize( |
| 596 | avbtool, avb_footer_type, partition_size, avb_signing_args) |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 597 | prop_dict["image_size"] = str(max_image_size) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 598 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 599 | if fs_type.startswith("ext"): |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 600 | build_command = [prop_dict["ext_mkuserimg"]] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 601 | if "extfs_sparse_flag" in prop_dict: |
| 602 | build_command.append(prop_dict["extfs_sparse_flag"]) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 603 | run_e2fsck = True |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 604 | build_command.extend([in_dir, out_file, fs_type, |
| 605 | prop_dict["mount_point"]]) |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 606 | build_command.append(prop_dict["image_size"]) |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 607 | if "journal_size" in prop_dict: |
| 608 | build_command.extend(["-j", prop_dict["journal_size"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 609 | if "timestamp" in prop_dict: |
| 610 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 611 | if fs_config: |
Doug Zongker | 8282282 | 2014-06-16 09:10:55 -0700 | [diff] [blame] | 612 | build_command.extend(["-C", fs_config]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 613 | if target_out: |
| 614 | build_command.extend(["-D", target_out]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 615 | if "block_list" in prop_dict: |
| 616 | build_command.extend(["-B", prop_dict["block_list"]]) |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 617 | if "base_fs_file" in prop_dict: |
| 618 | base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"]) |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 619 | build_command.extend(["-d", base_fs_file]) |
Christoffer Dall | 8ed01f3 | 2014-12-17 21:34:12 +0100 | [diff] [blame] | 620 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 621 | if "extfs_inode_count" in prop_dict: |
| 622 | build_command.extend(["-i", prop_dict["extfs_inode_count"]]) |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 623 | if "extfs_rsv_pct" in prop_dict: |
| 624 | build_command.extend(["-M", prop_dict["extfs_rsv_pct"]]) |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 625 | if "flash_erase_block_size" in prop_dict: |
| 626 | build_command.extend(["-e", prop_dict["flash_erase_block_size"]]) |
| 627 | if "flash_logical_block_size" in prop_dict: |
| 628 | build_command.extend(["-o", prop_dict["flash_logical_block_size"]]) |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 629 | # Specify UUID and hash_seed if using mke2fs. |
Tianjie Xu | 5733222 | 2018-08-15 16:16:21 -0700 | [diff] [blame] | 630 | if prop_dict["ext_mkuserimg"] == "mkuserimg_mke2fs": |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 631 | if "uuid" in prop_dict: |
| 632 | build_command.extend(["-U", prop_dict["uuid"]]) |
| 633 | if "hash_seed" in prop_dict: |
| 634 | build_command.extend(["-S", prop_dict["hash_seed"]]) |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 635 | if "ext4_share_dup_blocks" in prop_dict: |
| 636 | build_command.append("-c") |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 637 | if "selinux_fc" in prop_dict: |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 638 | build_command.append(prop_dict["selinux_fc"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 639 | elif fs_type.startswith("squash"): |
| 640 | build_command = ["mksquashfsimage.sh"] |
| 641 | build_command.extend([in_dir, out_file]) |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 642 | if "squashfs_sparse_flag" in prop_dict: |
| 643 | build_command.extend([prop_dict["squashfs_sparse_flag"]]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 644 | build_command.extend(["-m", prop_dict["mount_point"]]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 645 | if target_out: |
| 646 | build_command.extend(["-d", target_out]) |
Mohamad Ayyash | 8837882 | 2016-04-07 22:10:51 -0700 | [diff] [blame] | 647 | if fs_config: |
| 648 | build_command.extend(["-C", fs_config]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 649 | if "selinux_fc" in prop_dict: |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 650 | build_command.extend(["-c", prop_dict["selinux_fc"]]) |
Mohamad Ayyash | c3484f7 | 2016-06-13 09:46:58 -0700 | [diff] [blame] | 651 | if "block_list" in prop_dict: |
| 652 | build_command.extend(["-B", prop_dict["block_list"]]) |
Ng Zhi An | 9446c1d | 2018-01-19 15:51:46 -0800 | [diff] [blame] | 653 | if "squashfs_block_size" in prop_dict: |
| 654 | build_command.extend(["-b", prop_dict["squashfs_block_size"]]) |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 655 | if "squashfs_compressor" in prop_dict: |
| 656 | build_command.extend(["-z", prop_dict["squashfs_compressor"]]) |
| 657 | if "squashfs_compressor_opt" in prop_dict: |
| 658 | build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 659 | if prop_dict.get("squashfs_disable_4k_align") == "true": |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 660 | build_command.extend(["-a"]) |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 661 | elif fs_type.startswith("f2fs"): |
| 662 | build_command = ["mkf2fsuserimg.sh"] |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 663 | build_command.extend([out_file, prop_dict["image_size"]]) |
Jaegeuk Kim | 2ea1eba | 2017-11-28 19:21:28 -0800 | [diff] [blame] | 664 | if fs_config: |
| 665 | build_command.extend(["-C", fs_config]) |
| 666 | build_command.extend(["-f", in_dir]) |
| 667 | if target_out: |
| 668 | build_command.extend(["-D", target_out]) |
| 669 | if "selinux_fc" in prop_dict: |
| 670 | build_command.extend(["-s", prop_dict["selinux_fc"]]) |
| 671 | build_command.extend(["-t", prop_dict["mount_point"]]) |
| 672 | if "timestamp" in prop_dict: |
| 673 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
| 674 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 675 | else: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 676 | raise BuildImageError( |
| 677 | "Error: unknown filesystem type: {}".format(fs_type)) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 678 | |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 679 | try: |
| 680 | mkfs_output = common.RunAndCheckOutput(build_command) |
| 681 | except: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 682 | try: |
| 683 | du = GetDiskUsage(in_dir) |
| 684 | du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB) |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 685 | # Suppress any errors from GetDiskUsage() to avoid hiding the real errors |
| 686 | # from common.RunAndCheckOutput(). |
| 687 | except Exception as e: # pylint: disable=broad-except |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 688 | print(e, file=sys.stderr) |
| 689 | du_str = "unknown" |
Tao Bao | 4251fe9 | 2018-07-23 13:05:00 -0700 | [diff] [blame] | 690 | print( |
| 691 | "Out of space? The tree size of {} is {}, with reserved space of {} " |
| 692 | "bytes ({} MB).".format( |
Tao Bao | c2606eb | 2018-07-20 14:44:46 -0700 | [diff] [blame] | 693 | in_dir, du_str, |
Tao Bao | 4251fe9 | 2018-07-23 13:05:00 -0700 | [diff] [blame] | 694 | int(prop_dict.get("partition_reserved_size", 0)), |
| 695 | int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB)) |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 696 | print( |
| 697 | "The max image size for filsystem files is {} bytes ({} MB), out of a " |
| 698 | "total partition size of {} bytes ({} MB).".format( |
| 699 | int(prop_dict["image_size"]), |
| 700 | int(prop_dict["image_size"]) // BYTES_IN_MB, |
| 701 | int(prop_dict["partition_size"]), |
| 702 | int(prop_dict["partition_size"]) // BYTES_IN_MB)) |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 703 | raise |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 704 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 705 | # Check if there's enough headroom space available for ext4 image. |
Tao Bao | 79d52f8 | 2017-12-07 14:07:44 -0800 | [diff] [blame] | 706 | if "partition_headroom" in prop_dict and fs_type.startswith("ext4"): |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 707 | CheckHeadroom(mkfs_output, prop_dict) |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 708 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 709 | if not fs_spans_partition: |
| 710 | mount_point = prop_dict.get("mount_point") |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 711 | image_size = int(prop_dict["image_size"]) |
| 712 | sparse_image_size = GetSimgSize(out_file) |
| 713 | if sparse_image_size > image_size: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 714 | raise BuildImageError( |
| 715 | "Error: {} image size of {} is larger than partition size of " |
| 716 | "{}".format(mount_point, sparse_image_size, image_size)) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 717 | if verity_supported and is_verity_partition: |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 718 | ZeroPadSimg(out_file, image_size - sparse_image_size) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 719 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 720 | # Create the verified image if this is to be verified. |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 721 | if verity_supported and is_verity_partition: |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 722 | MakeVerityEnabledImage(out_file, verity_fec_supported, prop_dict) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 723 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 724 | # Add AVB HASH or HASHTREE footer (metadata). |
| 725 | if avb_footer_type: |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 726 | partition_size = prop_dict["partition_size"] |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 727 | partition_name = prop_dict["partition_name"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 728 | # key_path and algorithm are only available when chain partition is used. |
| 729 | key_path = prop_dict.get("avb_key_path") |
| 730 | algorithm = prop_dict.get("avb_algorithm") |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 731 | salt = prop_dict.get("avb_salt") |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 732 | AVBAddFooter( |
| 733 | out_file, avbtool, avb_footer_type, partition_size, partition_name, |
| 734 | key_path, algorithm, salt, avb_signing_args) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 735 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 736 | if run_e2fsck and prop_dict.get("skip_fsck") != "true": |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 737 | unsparse_image = UnsparseImage(out_file, replace=False) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 738 | |
| 739 | # Run e2fsck on the inflated image file |
| 740 | e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] |
Isaac Chen | ec7fa1c | 2018-08-02 14:02:56 +0800 | [diff] [blame] | 741 | # TODO(b/112062612): work around e2fsck failure with SANITIZE_HOST=address |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 742 | env4e2fsck = os.environ.copy() |
| 743 | env4e2fsck["ASAN_OPTIONS"] = "detect_odr_violation=0" |
| 744 | try: |
| 745 | common.RunAndCheckOutput(e2fsck_command, env=env4e2fsck) |
| 746 | finally: |
| 747 | os.remove(unsparse_image) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 748 | |
| 749 | |
| 750 | def ImagePropFromGlobalDict(glob_dict, mount_point): |
| 751 | """Build an image property dictionary from the global dictionary. |
| 752 | |
| 753 | Args: |
| 754 | glob_dict: the global dictionary from the build system. |
| 755 | mount_point: such as "system", "data" etc. |
| 756 | """ |
Doug Zongker | 1ad7ade | 2013-12-06 11:53:27 -0800 | [diff] [blame] | 757 | d = {} |
Tao Bao | 052ae35 | 2015-09-28 13:44:13 -0700 | [diff] [blame] | 758 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 759 | if "build.prop" in glob_dict: |
| 760 | bp = glob_dict["build.prop"] |
| 761 | if "ro.build.date.utc" in bp: |
| 762 | d["timestamp"] = bp["ro.build.date.utc"] |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 763 | |
| 764 | def copy_prop(src_p, dest_p): |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 765 | """Copy a property from the global dictionary. |
| 766 | |
| 767 | Args: |
| 768 | src_p: The source property in the global dictionary. |
| 769 | dest_p: The destination property. |
| 770 | Returns: |
| 771 | True if property was found and copied, False otherwise. |
| 772 | """ |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 773 | if src_p in glob_dict: |
| 774 | d[dest_p] = str(glob_dict[src_p]) |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 775 | return True |
| 776 | return False |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 777 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 778 | common_props = ( |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 779 | "extfs_sparse_flag", |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 780 | "squashfs_sparse_flag", |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 781 | "selinux_fc", |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 782 | "skip_fsck", |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 783 | "ext_mkuserimg", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 784 | "verity", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 785 | "verity_key", |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 786 | "verity_signer_cmd", |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 787 | "verity_fec", |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 788 | "verity_disable", |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 789 | "avb_enable", |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 790 | "avb_avbtool", |
| 791 | "avb_salt", |
Yifan Hong | 2dae572 | 2018-07-31 12:47:27 -0700 | [diff] [blame] | 792 | "use_dynamic_partition_size", |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 793 | ) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 794 | for p in common_props: |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 795 | copy_prop(p, p) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 796 | |
| 797 | d["mount_point"] = mount_point |
| 798 | if mount_point == "system": |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 799 | copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable") |
| 800 | copy_prop("avb_system_add_hashtree_footer_args", |
| 801 | "avb_add_hashtree_footer_args") |
| 802 | copy_prop("avb_system_key_path", "avb_key_path") |
| 803 | copy_prop("avb_system_algorithm", "avb_algorithm") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 804 | copy_prop("fs_type", "fs_type") |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 805 | # Copy the generic system fs type first, override with specific one if |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 806 | # available. |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 807 | copy_prop("system_fs_type", "fs_type") |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 808 | copy_prop("system_headroom", "partition_headroom") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 809 | copy_prop("system_size", "partition_size") |
Tao Bao | 332a96b | 2018-03-31 10:27:35 -0700 | [diff] [blame] | 810 | if not copy_prop("system_journal_size", "journal_size"): |
| 811 | d["journal_size"] = "0" |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 812 | copy_prop("system_verity_block_device", "verity_block_device") |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 813 | copy_prop("system_root_image", "system_root_image") |
Tao Bao | 8bfd3c7 | 2018-07-20 15:20:28 -0700 | [diff] [blame] | 814 | copy_prop("root_dir", "root_dir") |
| 815 | copy_prop("root_fs_config", "root_fs_config") |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 816 | copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks") |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 817 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 818 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 819 | copy_prop("system_squashfs_block_size", "squashfs_block_size") |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 820 | copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 821 | copy_prop("system_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 822 | copy_prop("system_extfs_inode_count", "extfs_inode_count") |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 823 | if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"): |
| 824 | d["extfs_rsv_pct"] = "0" |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 825 | copy_prop("system_reserved_size", "partition_reserved_size") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 826 | elif mount_point == "system_other": |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 827 | # We inherit the selinux policies of /system since we contain some of its |
| 828 | # files. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 829 | copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable") |
| 830 | copy_prop("avb_system_add_hashtree_footer_args", |
| 831 | "avb_add_hashtree_footer_args") |
| 832 | copy_prop("avb_system_key_path", "avb_key_path") |
| 833 | copy_prop("avb_system_algorithm", "avb_algorithm") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 834 | copy_prop("fs_type", "fs_type") |
| 835 | copy_prop("system_fs_type", "fs_type") |
| 836 | copy_prop("system_size", "partition_size") |
Tao Bao | 332a96b | 2018-03-31 10:27:35 -0700 | [diff] [blame] | 837 | if not copy_prop("system_journal_size", "journal_size"): |
| 838 | d["journal_size"] = "0" |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 839 | copy_prop("system_verity_block_device", "verity_block_device") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 840 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 841 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
| 842 | copy_prop("system_squashfs_block_size", "squashfs_block_size") |
| 843 | copy_prop("system_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 844 | copy_prop("system_extfs_inode_count", "extfs_inode_count") |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 845 | if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"): |
| 846 | d["extfs_rsv_pct"] = "0" |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 847 | copy_prop("system_reserved_size", "partition_reserved_size") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 848 | elif mount_point == "data": |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 849 | # Copy the generic fs type first, override with specific one if available. |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 850 | copy_prop("fs_type", "fs_type") |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 851 | copy_prop("userdata_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 852 | copy_prop("userdata_size", "partition_size") |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 853 | copy_prop("flash_logical_block_size", "flash_logical_block_size") |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 854 | copy_prop("flash_erase_block_size", "flash_erase_block_size") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 855 | elif mount_point == "cache": |
| 856 | copy_prop("cache_fs_type", "fs_type") |
| 857 | copy_prop("cache_size", "partition_size") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 858 | elif mount_point == "vendor": |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 859 | copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable") |
| 860 | copy_prop("avb_vendor_add_hashtree_footer_args", |
| 861 | "avb_add_hashtree_footer_args") |
| 862 | copy_prop("avb_vendor_key_path", "avb_key_path") |
| 863 | copy_prop("avb_vendor_algorithm", "avb_algorithm") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 864 | copy_prop("vendor_fs_type", "fs_type") |
| 865 | copy_prop("vendor_size", "partition_size") |
Tao Bao | 332a96b | 2018-03-31 10:27:35 -0700 | [diff] [blame] | 866 | if not copy_prop("vendor_journal_size", "journal_size"): |
| 867 | d["journal_size"] = "0" |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 868 | copy_prop("vendor_verity_block_device", "verity_block_device") |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 869 | copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks") |
Patrick Tjin | e11aa50 | 2016-02-09 15:40:38 -0800 | [diff] [blame] | 870 | copy_prop("vendor_squashfs_compressor", "squashfs_compressor") |
| 871 | copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt") |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 872 | copy_prop("vendor_squashfs_block_size", "squashfs_block_size") |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 873 | copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 874 | copy_prop("vendor_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 875 | copy_prop("vendor_extfs_inode_count", "extfs_inode_count") |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 876 | if not copy_prop("vendor_extfs_rsv_pct", "extfs_rsv_pct"): |
| 877 | d["extfs_rsv_pct"] = "0" |
Yifan Hong | 749062d | 2018-06-19 16:23:16 -0700 | [diff] [blame] | 878 | copy_prop("vendor_reserved_size", "partition_reserved_size") |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 879 | elif mount_point == "product": |
| 880 | copy_prop("avb_product_hashtree_enable", "avb_hashtree_enable") |
| 881 | copy_prop("avb_product_add_hashtree_footer_args", |
| 882 | "avb_add_hashtree_footer_args") |
| 883 | copy_prop("avb_product_key_path", "avb_key_path") |
| 884 | copy_prop("avb_product_algorithm", "avb_algorithm") |
| 885 | copy_prop("product_fs_type", "fs_type") |
| 886 | copy_prop("product_size", "partition_size") |
Tao Bao | 332a96b | 2018-03-31 10:27:35 -0700 | [diff] [blame] | 887 | if not copy_prop("product_journal_size", "journal_size"): |
| 888 | d["journal_size"] = "0" |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 889 | copy_prop("product_verity_block_device", "verity_block_device") |
| 890 | copy_prop("product_squashfs_compressor", "squashfs_compressor") |
| 891 | copy_prop("product_squashfs_compressor_opt", "squashfs_compressor_opt") |
| 892 | copy_prop("product_squashfs_block_size", "squashfs_block_size") |
| 893 | copy_prop("product_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
| 894 | copy_prop("product_base_fs_file", "base_fs_file") |
| 895 | copy_prop("product_extfs_inode_count", "extfs_inode_count") |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 896 | if not copy_prop("product_extfs_rsv_pct", "extfs_rsv_pct"): |
| 897 | d["extfs_rsv_pct"] = "0" |
Yifan Hong | 56a6c3b | 2018-07-20 15:19:34 -0700 | [diff] [blame] | 898 | copy_prop("product_reserved_size", "partition_reserved_size") |
Dario Freni | 924af7d | 2018-08-17 00:56:14 +0100 | [diff] [blame] | 899 | elif mount_point == "product_services": |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 900 | copy_prop("avb_product_services_hashtree_enable", "avb_hashtree_enable") |
| 901 | copy_prop("avb_product_services_add_hashtree_footer_args", |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 902 | "avb_add_hashtree_footer_args") |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 903 | copy_prop("avb_product_services_key_path", "avb_key_path") |
| 904 | copy_prop("avb_product_services_algorithm", "avb_algorithm") |
| 905 | copy_prop("product_services_fs_type", "fs_type") |
| 906 | copy_prop("product_services_size", "partition_size") |
| 907 | if not copy_prop("product_services_journal_size", "journal_size"): |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 908 | d["journal_size"] = "0" |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 909 | copy_prop("product_services_verity_block_device", "verity_block_device") |
| 910 | copy_prop("product_services_squashfs_compressor", "squashfs_compressor") |
| 911 | copy_prop("product_services_squashfs_compressor_opt", |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 912 | "squashfs_compressor_opt") |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 913 | copy_prop("product_services_squashfs_block_size", "squashfs_block_size") |
| 914 | copy_prop("product_services_squashfs_disable_4k_align", |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 915 | "squashfs_disable_4k_align") |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 916 | copy_prop("product_services_base_fs_file", "base_fs_file") |
| 917 | copy_prop("product_services_extfs_inode_count", "extfs_inode_count") |
| 918 | if not copy_prop("product_services_extfs_rsv_pct", "extfs_rsv_pct"): |
Dario Freni | 5f681e1 | 2018-05-29 13:09:01 +0100 | [diff] [blame] | 919 | d["extfs_rsv_pct"] = "0" |
Yifan Hong | ebc041a | 2018-07-26 16:02:52 -0700 | [diff] [blame] | 920 | copy_prop("product_services_reserved_size", "partition_reserved_size") |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 921 | elif mount_point == "odm": |
| 922 | copy_prop("avb_odm_hashtree_enable", "avb_hashtree_enable") |
| 923 | copy_prop("avb_odm_add_hashtree_footer_args", |
| 924 | "avb_add_hashtree_footer_args") |
| 925 | copy_prop("avb_odm_key_path", "avb_key_path") |
| 926 | copy_prop("avb_odm_algorithm", "avb_algorithm") |
| 927 | copy_prop("odm_fs_type", "fs_type") |
| 928 | copy_prop("odm_size", "partition_size") |
| 929 | if not copy_prop("odm_journal_size", "journal_size"): |
| 930 | d["journal_size"] = "0" |
| 931 | copy_prop("odm_verity_block_device", "verity_block_device") |
| 932 | copy_prop("odm_squashfs_compressor", "squashfs_compressor") |
| 933 | copy_prop("odm_squashfs_compressor_opt", "squashfs_compressor_opt") |
| 934 | copy_prop("odm_squashfs_block_size", "squashfs_block_size") |
| 935 | copy_prop("odm_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
| 936 | copy_prop("odm_base_fs_file", "base_fs_file") |
| 937 | copy_prop("odm_extfs_inode_count", "extfs_inode_count") |
| 938 | if not copy_prop("odm_extfs_rsv_pct", "extfs_rsv_pct"): |
| 939 | d["extfs_rsv_pct"] = "0" |
| 940 | copy_prop("odm_reserved_size", "partition_reserved_size") |
Ying Wang | b888843 | 2014-03-11 17:13:27 -0700 | [diff] [blame] | 941 | elif mount_point == "oem": |
| 942 | copy_prop("fs_type", "fs_type") |
| 943 | copy_prop("oem_size", "partition_size") |
Tao Bao | 332a96b | 2018-03-31 10:27:35 -0700 | [diff] [blame] | 944 | if not copy_prop("oem_journal_size", "journal_size"): |
| 945 | d["journal_size"] = "0" |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 946 | copy_prop("oem_extfs_inode_count", "extfs_inode_count") |
Patrick Tjin | 3f5f993 | 2018-03-23 11:36:43 -0700 | [diff] [blame] | 947 | if not copy_prop("oem_extfs_rsv_pct", "extfs_rsv_pct"): |
| 948 | d["extfs_rsv_pct"] = "0" |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 949 | d["partition_name"] = mount_point |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 950 | return d |
| 951 | |
| 952 | |
| 953 | def LoadGlobalDict(filename): |
| 954 | """Load "name=value" pairs from filename""" |
| 955 | d = {} |
| 956 | f = open(filename) |
| 957 | for line in f: |
| 958 | line = line.strip() |
| 959 | if not line or line.startswith("#"): |
| 960 | continue |
| 961 | k, v = line.split("=", 1) |
| 962 | d[k] = v |
| 963 | f.close() |
| 964 | return d |
| 965 | |
| 966 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 967 | def GlobalDictFromImageProp(image_prop, mount_point): |
| 968 | d = {} |
| 969 | def copy_prop(src_p, dest_p): |
| 970 | if src_p in image_prop: |
| 971 | d[dest_p] = image_prop[src_p] |
| 972 | return True |
| 973 | return False |
Tao Bao | 4251fe9 | 2018-07-23 13:05:00 -0700 | [diff] [blame] | 974 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 975 | if mount_point == "system": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 976 | copy_prop("partition_size", "system_size") |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 977 | elif mount_point == "system_other": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 978 | copy_prop("partition_size", "system_size") |
Yifan Hong | 749062d | 2018-06-19 16:23:16 -0700 | [diff] [blame] | 979 | elif mount_point == "vendor": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 980 | copy_prop("partition_size", "vendor_size") |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 981 | elif mount_point == "odm": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 982 | copy_prop("partition_size", "odm_size") |
Yifan Hong | 56a6c3b | 2018-07-20 15:19:34 -0700 | [diff] [blame] | 983 | elif mount_point == "product": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 984 | copy_prop("partition_size", "product_size") |
Dario Freni | 924af7d | 2018-08-17 00:56:14 +0100 | [diff] [blame] | 985 | elif mount_point == "product_services": |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 986 | copy_prop("partition_size", "product_services_size") |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 987 | return d |
| 988 | |
| 989 | |
| 990 | def SaveGlobalDict(filename, glob_dict): |
| 991 | with open(filename, "w") as f: |
| 992 | f.writelines(["%s=%s" % (key, value) for (key, value) in glob_dict.items()]) |
| 993 | |
| 994 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 995 | def main(argv): |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 996 | if len(argv) < 4 or len(argv) > 5: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 997 | print(__doc__) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 998 | sys.exit(1) |
| 999 | |
| 1000 | in_dir = argv[0] |
| 1001 | glob_dict_file = argv[1] |
| 1002 | out_file = argv[2] |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 1003 | target_out = argv[3] |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 1004 | prop_file_out = argv[4] if len(argv) >= 5 else None |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 1005 | |
| 1006 | glob_dict = LoadGlobalDict(glob_dict_file) |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1007 | if "mount_point" in glob_dict: |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 1008 | # The caller knows the mount point and provides a dictionay needed by |
| 1009 | # BuildImage(). |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1010 | image_properties = glob_dict |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 1011 | else: |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1012 | image_filename = os.path.basename(out_file) |
| 1013 | mount_point = "" |
| 1014 | if image_filename == "system.img": |
| 1015 | mount_point = "system" |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 1016 | elif image_filename == "system_other.img": |
| 1017 | mount_point = "system_other" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1018 | elif image_filename == "userdata.img": |
| 1019 | mount_point = "data" |
| 1020 | elif image_filename == "cache.img": |
| 1021 | mount_point = "cache" |
| 1022 | elif image_filename == "vendor.img": |
| 1023 | mount_point = "vendor" |
Bowgo Tsai | d624fa6 | 2017-11-14 23:42:30 +0800 | [diff] [blame] | 1024 | elif image_filename == "odm.img": |
| 1025 | mount_point = "odm" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1026 | elif image_filename == "oem.img": |
| 1027 | mount_point = "oem" |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 1028 | elif image_filename == "product.img": |
| 1029 | mount_point = "product" |
Dario Freni | 924af7d | 2018-08-17 00:56:14 +0100 | [diff] [blame] | 1030 | elif image_filename == "product_services.img": |
| 1031 | mount_point = "product_services" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1032 | else: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 1033 | print("error: unknown image file name ", image_filename, file=sys.stderr) |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 1034 | sys.exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 1035 | |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 1036 | image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) |
| 1037 | |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 1038 | try: |
| 1039 | BuildImage(in_dir, image_properties, out_file, target_out) |
| 1040 | except: |
| 1041 | print("Error: Failed to build {} from {}".format(out_file, in_dir), |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 1042 | file=sys.stderr) |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 1043 | raise |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 1044 | |
Yifan Hong | bbcba1e | 2018-06-18 16:32:35 -0700 | [diff] [blame] | 1045 | if prop_file_out: |
| 1046 | glob_dict_out = GlobalDictFromImageProp(image_properties, mount_point) |
| 1047 | SaveGlobalDict(prop_file_out, glob_dict_out) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 1048 | |
| 1049 | if __name__ == '__main__': |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 1050 | try: |
| 1051 | main(sys.argv[1:]) |
| 1052 | finally: |
| 1053 | common.Cleanup() |