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