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