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 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 21 | Usage: build_image.py input_directory properties_file output_image \\ |
| 22 | target_output_directory |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 23 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 24 | |
| 25 | from __future__ import print_function |
| 26 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 27 | import os |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 28 | import os.path |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 29 | import re |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 30 | import shlex |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 31 | import shutil |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 32 | import subprocess |
| 33 | import sys |
| 34 | |
| 35 | import common |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 36 | import sparse_img |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 37 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 38 | |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 39 | OPTIONS = common.OPTIONS |
| 40 | |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 41 | FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 42 | BLOCK_SIZE = 4096 |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 43 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 44 | |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 45 | def RunCommand(cmd, verbose=None): |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 46 | """Echo and run the given command. |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 47 | |
| 48 | Args: |
| 49 | cmd: the command represented as a list of strings. |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 50 | verbose: show commands being executed. |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 51 | Returns: |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 52 | A tuple of the output and the exit code. |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 53 | """ |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 54 | if verbose is None: |
| 55 | verbose = OPTIONS.verbose |
| 56 | if verbose: |
| 57 | print("Running: " + " ".join(cmd)) |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 58 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 59 | output, _ = p.communicate() |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 60 | |
| 61 | if verbose: |
| 62 | print(output.rstrip()) |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 63 | return (output, p.returncode) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 64 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 65 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 66 | def GetVerityFECSize(partition_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 67 | cmd = ["fec", "-s", str(partition_size)] |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 68 | output, exit_code = RunCommand(cmd, False) |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 69 | if exit_code != 0: |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 70 | return False, 0 |
| 71 | return True, int(output) |
| 72 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 73 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 74 | def GetVerityTreeSize(partition_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 75 | cmd = ["build_verity_tree", "-s", str(partition_size)] |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 76 | output, exit_code = RunCommand(cmd, False) |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 77 | if exit_code != 0: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 78 | return False, 0 |
| 79 | return True, int(output) |
| 80 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 81 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 82 | def GetVerityMetadataSize(partition_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 83 | cmd = ["system/extras/verity/build_verity_metadata.py", "size", |
| 84 | 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 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 91 | def GetVeritySize(partition_size, fec_supported): |
| 92 | success, verity_tree_size = GetVerityTreeSize(partition_size) |
| 93 | if not success: |
| 94 | return 0 |
| 95 | success, verity_metadata_size = GetVerityMetadataSize(partition_size) |
| 96 | if not success: |
| 97 | return 0 |
| 98 | verity_size = verity_tree_size + verity_metadata_size |
| 99 | if fec_supported: |
| 100 | success, fec_size = GetVerityFECSize(partition_size + verity_size) |
| 101 | if not success: |
| 102 | return 0 |
| 103 | return verity_size + fec_size |
| 104 | return verity_size |
| 105 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 106 | |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 107 | def GetSimgSize(image_file): |
| 108 | simg = sparse_img.SparseImage(image_file, build_map=False) |
| 109 | return simg.blocksize * simg.total_blocks |
| 110 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 111 | |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 112 | def ZeroPadSimg(image_file, pad_size): |
| 113 | blocks = pad_size // BLOCK_SIZE |
| 114 | print("Padding %d blocks (%d bytes)" % (blocks, pad_size)) |
| 115 | simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False) |
| 116 | simg.AppendFillChunk(0, blocks) |
| 117 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 118 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 119 | def AVBCalcMaxImageSize(avbtool, footer_type, partition_size, additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 120 | """Calculates max image size for a given partition size. |
| 121 | |
| 122 | Args: |
| 123 | avbtool: String with path to avbtool. |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 124 | footer_type: 'hash' or 'hashtree' for generating footer. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 125 | partition_size: The size of the partition in question. |
| 126 | additional_args: Additional arguments to pass to 'avbtool |
| 127 | add_hashtree_image'. |
| 128 | Returns: |
| 129 | The maximum image size or 0 if an error occurred. |
| 130 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 131 | cmd = [avbtool, "add_%s_footer" % footer_type, |
| 132 | "--partition_size", partition_size, "--calc_max_image_size"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 133 | cmd.extend(shlex.split(additional_args)) |
| 134 | |
| 135 | (output, exit_code) = RunCommand(cmd) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 136 | if exit_code != 0: |
| 137 | return 0 |
| 138 | else: |
| 139 | return int(output) |
| 140 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 141 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 142 | def AVBAddFooter(image_path, avbtool, footer_type, partition_size, |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 143 | partition_name, key_path, algorithm, salt, |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 144 | additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 145 | """Adds dm-verity hashtree and AVB metadata to an image. |
| 146 | |
| 147 | Args: |
| 148 | image_path: Path to image to modify. |
| 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 | partition_name: The name of the partition - will be embedded in metadata. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 153 | key_path: Path to key to use or None. |
| 154 | algorithm: Name of algorithm to use or None. |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 155 | salt: The salt to use (a hexadecimal string) or None. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 156 | additional_args: Additional arguments to pass to 'avbtool |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 157 | add_hashtree_image'. |
| 158 | |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 159 | Returns: |
| 160 | True if the operation succeeded. |
| 161 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 162 | cmd = [avbtool, "add_%s_footer" % footer_type, |
| 163 | "--partition_size", partition_size, |
| 164 | "--partition_name", partition_name, |
| 165 | "--image", image_path] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 166 | |
| 167 | if key_path and algorithm: |
| 168 | cmd.extend(["--key", key_path, "--algorithm", algorithm]) |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 169 | if salt: |
| 170 | cmd.extend(["--salt", salt]) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 171 | |
| 172 | cmd.extend(shlex.split(additional_args)) |
| 173 | |
| 174 | (_, exit_code) = RunCommand(cmd) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 175 | return exit_code == 0 |
| 176 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 177 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 178 | def AdjustPartitionSizeForVerity(partition_size, fec_supported): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 179 | """Modifies the provided partition size to account for the verity metadata. |
| 180 | |
| 181 | This information is used to size the created image appropriately. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 182 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 183 | Args: |
| 184 | partition_size: the size of the partition to be verified. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 185 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 186 | Returns: |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 187 | A tuple of the size of the partition adjusted for verity metadata, and |
| 188 | the size of verity metadata. |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 189 | """ |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 190 | key = "%d %d" % (partition_size, fec_supported) |
| 191 | if key in AdjustPartitionSizeForVerity.results: |
| 192 | return AdjustPartitionSizeForVerity.results[key] |
| 193 | |
| 194 | hi = partition_size |
| 195 | if hi % BLOCK_SIZE != 0: |
| 196 | hi = (hi // BLOCK_SIZE) * BLOCK_SIZE |
| 197 | |
| 198 | # verity tree and fec sizes depend on the partition size, which |
| 199 | # means this estimate is always going to be unnecessarily small |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 200 | verity_size = GetVeritySize(hi, fec_supported) |
| 201 | lo = partition_size - verity_size |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 202 | result = lo |
| 203 | |
| 204 | # do a binary search for the optimal size |
| 205 | while lo < hi: |
| 206 | i = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 207 | v = GetVeritySize(i, fec_supported) |
| 208 | if i + v <= partition_size: |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 209 | if result < i: |
| 210 | result = i |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 211 | verity_size = v |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 212 | lo = i + BLOCK_SIZE |
| 213 | else: |
| 214 | hi = i |
| 215 | |
Tomasz Wasilczyk | 29ec06b | 2017-11-15 10:34:01 -0800 | [diff] [blame] | 216 | if OPTIONS.verbose: |
| 217 | print("Adjusted partition size for verity, partition_size: {}," |
| 218 | " verity_size: {}".format(result, verity_size)) |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 219 | AdjustPartitionSizeForVerity.results[key] = (result, verity_size) |
| 220 | return (result, verity_size) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 221 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 222 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 223 | AdjustPartitionSizeForVerity.results = {} |
| 224 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 225 | |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 226 | def BuildVerityFEC(sparse_image_path, verity_path, verity_fec_path, |
| 227 | padding_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 228 | cmd = ["fec", "-e", "-p", str(padding_size), sparse_image_path, |
| 229 | verity_path, verity_fec_path] |
| 230 | output, exit_code = RunCommand(cmd) |
| 231 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 232 | print("Could not build FEC data! Error: %s" % output) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 233 | return False |
| 234 | return True |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 235 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 236 | |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 237 | def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 238 | cmd = ["build_verity_tree", "-A", FIXED_SALT, sparse_image_path, |
| 239 | verity_image_path] |
| 240 | output, exit_code = RunCommand(cmd) |
| 241 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 242 | print("Could not build verity tree! Error: %s" % output) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 243 | return False |
| 244 | root, salt = output.split() |
| 245 | prop_dict["verity_root_hash"] = root |
| 246 | prop_dict["verity_salt"] = salt |
| 247 | return True |
| 248 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 249 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 250 | def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 251 | block_device, signer_path, key, signer_args, |
| 252 | verity_disable): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 253 | cmd = ["system/extras/verity/build_verity_metadata.py", "build", |
| 254 | str(image_size), verity_metadata_path, root_hash, salt, block_device, |
| 255 | signer_path, key] |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 256 | if signer_args: |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 257 | cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),)) |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 258 | if verity_disable: |
| 259 | cmd.append("--verity_disable") |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 260 | output, exit_code = RunCommand(cmd) |
| 261 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 262 | print("Could not build verity metadata! Error: %s" % output) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 263 | return False |
| 264 | return True |
| 265 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 266 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 267 | def Append2Simg(sparse_image_path, unsparse_image_path, error_message): |
| 268 | """Appends the unsparse image to the given sparse image. |
| 269 | |
| 270 | Args: |
| 271 | sparse_image_path: the path to the (sparse) image |
| 272 | unsparse_image_path: the path to the (unsparse) image |
| 273 | Returns: |
| 274 | True on success, False on failure. |
| 275 | """ |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 276 | cmd = ["append2simg", sparse_image_path, unsparse_image_path] |
| 277 | output, exit_code = RunCommand(cmd) |
| 278 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 279 | print("%s: %s" % (error_message, output)) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 280 | return False |
| 281 | return True |
| 282 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 283 | |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 284 | def Append(target, file_to_append, error_message): |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 285 | """Appends file_to_append to target.""" |
| 286 | try: |
| 287 | 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] | 288 | for line in input_file: |
| 289 | out_file.write(line) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 290 | except IOError: |
| 291 | print(error_message) |
| 292 | return False |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 293 | return True |
| 294 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 295 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 296 | def BuildVerifiedImage(data_image_path, verity_image_path, |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 297 | verity_metadata_path, verity_fec_path, |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 298 | padding_size, fec_supported): |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 299 | if not Append(verity_image_path, verity_metadata_path, |
| 300 | "Could not append verity metadata!"): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 301 | return False |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 302 | |
| 303 | if fec_supported: |
| 304 | # build FEC for the entire partition, including metadata |
| 305 | if not BuildVerityFEC(data_image_path, verity_image_path, |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 306 | verity_fec_path, padding_size): |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 307 | return False |
| 308 | |
| 309 | if not Append(verity_image_path, verity_fec_path, "Could not append FEC!"): |
| 310 | return False |
| 311 | |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 312 | if not Append2Simg(data_image_path, verity_image_path, |
| 313 | "Could not append verity data!"): |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 314 | return False |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 315 | return True |
| 316 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 317 | |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 318 | def UnsparseImage(sparse_image_path, replace=True): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 319 | img_dir = os.path.dirname(sparse_image_path) |
| 320 | unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path) |
| 321 | unsparse_image_path = os.path.join(img_dir, unsparse_image_path) |
| 322 | if os.path.exists(unsparse_image_path): |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 323 | if replace: |
| 324 | os.unlink(unsparse_image_path) |
| 325 | else: |
| 326 | return True, unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 327 | inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 328 | (_, exit_code) = RunCommand(inflate_command) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 329 | if exit_code != 0: |
| 330 | os.remove(unsparse_image_path) |
| 331 | return False, None |
| 332 | return True, unsparse_image_path |
| 333 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 334 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 335 | def MakeVerityEnabledImage(out_file, fec_supported, prop_dict): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 336 | """Creates an image that is verifiable using dm-verity. |
| 337 | |
| 338 | Args: |
| 339 | out_file: the location to write the verifiable image at |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 340 | prop_dict: a dictionary of properties required for image creation and |
| 341 | verification |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 342 | Returns: |
| 343 | True on success, False otherwise. |
| 344 | """ |
| 345 | # get properties |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 346 | image_size = int(prop_dict["partition_size"]) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 347 | block_dev = prop_dict["verity_block_device"] |
Paul Lawrence | a37b2bb | 2014-11-13 17:54:30 -0800 | [diff] [blame] | 348 | signer_key = prop_dict["verity_key"] + ".pk8" |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 349 | if OPTIONS.verity_signer_path is not None: |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 350 | signer_path = OPTIONS.verity_signer_path |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 351 | else: |
| 352 | signer_path = prop_dict["verity_signer_cmd"] |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 353 | signer_args = OPTIONS.verity_signer_args |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 354 | |
| 355 | # make a tempdir |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 356 | tempdir_name = common.MakeTempDir(suffix="_verity_images") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 357 | |
| 358 | # get partial image paths |
| 359 | verity_image_path = os.path.join(tempdir_name, "verity.img") |
| 360 | verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img") |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 361 | verity_fec_path = os.path.join(tempdir_name, "verity_fec.img") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 362 | |
| 363 | # build the verity tree and get the root hash and salt |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 364 | if not BuildVerityTree(out_file, verity_image_path, prop_dict): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 365 | return False |
| 366 | |
| 367 | # build the metadata blocks |
| 368 | root_hash = prop_dict["verity_root_hash"] |
| 369 | salt = prop_dict["verity_salt"] |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 370 | verity_disable = "verity_disable" in prop_dict |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 371 | if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 372 | block_dev, signer_path, signer_key, signer_args, |
| 373 | verity_disable): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 374 | return False |
| 375 | |
| 376 | # build the full verified image |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 377 | target_size = int(prop_dict["original_partition_size"]) |
| 378 | verity_size = int(prop_dict["verity_size"]) |
| 379 | |
| 380 | padding_size = target_size - image_size - verity_size |
| 381 | assert padding_size >= 0 |
| 382 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 383 | if not BuildVerifiedImage(out_file, |
| 384 | verity_image_path, |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 385 | verity_metadata_path, |
| 386 | verity_fec_path, |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 387 | padding_size, |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 388 | fec_supported): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 389 | return False |
| 390 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 391 | return True |
| 392 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 393 | |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 394 | def ConvertBlockMapToBaseFs(block_map_file): |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 395 | base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 396 | convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file] |
| 397 | (_, exit_code) = RunCommand(convert_command) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 398 | return base_fs_file if exit_code == 0 else None |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 399 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 400 | |
| 401 | def CheckHeadroom(ext4fs_output, prop_dict): |
| 402 | """Checks if there's enough headroom space available. |
| 403 | |
| 404 | Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM), |
| 405 | which is useful for devices with low disk space that have system image |
| 406 | variation between builds. The 'partition_headroom' in prop_dict is the size |
| 407 | in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks. |
| 408 | |
| 409 | Args: |
| 410 | ext4fs_output: The output string from mke2fs command. |
| 411 | prop_dict: The property dict. |
| 412 | |
| 413 | Returns: |
| 414 | The check result. |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 415 | |
| 416 | Raises: |
| 417 | AssertionError: On invalid input. |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 418 | """ |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 419 | assert ext4fs_output is not None |
| 420 | assert prop_dict.get('fs_type', '').startswith('ext4') |
| 421 | assert 'partition_headroom' in prop_dict |
| 422 | assert 'mount_point' in prop_dict |
| 423 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 424 | ext4fs_stats = re.compile( |
| 425 | r'Created filesystem with .* (?P<used_blocks>[0-9]+)/' |
| 426 | r'(?P<total_blocks>[0-9]+) blocks') |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 427 | last_line = ext4fs_output.strip().split('\n')[-1] |
| 428 | m = ext4fs_stats.match(last_line) |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 429 | used_blocks = int(m.groupdict().get('used_blocks')) |
| 430 | total_blocks = int(m.groupdict().get('total_blocks')) |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 431 | headroom_blocks = int(prop_dict['partition_headroom']) / BLOCK_SIZE |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 432 | adjusted_blocks = total_blocks - headroom_blocks |
| 433 | if used_blocks > adjusted_blocks: |
Tao Bao | d8a953d | 2018-01-02 21:19:27 -0800 | [diff] [blame] | 434 | mount_point = prop_dict["mount_point"] |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 435 | print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, " |
| 436 | "headroom: %d blocks, available: %d blocks)" % ( |
| 437 | mount_point, total_blocks, used_blocks, headroom_blocks, |
| 438 | adjusted_blocks)) |
| 439 | return False |
| 440 | return True |
| 441 | |
| 442 | |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 443 | def BuildImage(in_dir, prop_dict, out_file, target_out=None): |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 444 | """Build an image to out_file from in_dir with property prop_dict. |
| 445 | |
| 446 | Args: |
| 447 | in_dir: path of input directory. |
| 448 | prop_dict: property dictionary. |
| 449 | out_file: path of the output image file. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 450 | target_out: path of the product out directory to read device specific FS |
| 451 | config files. |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 452 | |
| 453 | Returns: |
| 454 | True iff the image is built successfully. |
| 455 | """ |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 456 | # system_root_image=true: build a system.img that combines the contents of |
| 457 | # /system and the ramdisk, and can be mounted at the root of the file system. |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 458 | origin_in = in_dir |
| 459 | fs_config = prop_dict.get("fs_config") |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 460 | if (prop_dict.get("system_root_image") == "true" and |
| 461 | prop_dict["mount_point"] == "system"): |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 462 | in_dir = common.MakeTempDir() |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 463 | # Change the mount point to "/". |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 464 | prop_dict["mount_point"] = "/" |
| 465 | if fs_config: |
| 466 | # We need to merge the fs_config files of system and ramdisk. |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 467 | merged_fs_config = common.MakeTempFile(prefix="root_fs_config", |
| 468 | suffix=".txt") |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 469 | with open(merged_fs_config, "w") as fw: |
| 470 | if "ramdisk_fs_config" in prop_dict: |
| 471 | with open(prop_dict["ramdisk_fs_config"]) as fr: |
| 472 | fw.writelines(fr.readlines()) |
| 473 | with open(fs_config) as fr: |
| 474 | fw.writelines(fr.readlines()) |
| 475 | fs_config = merged_fs_config |
| 476 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 477 | build_command = [] |
| 478 | fs_type = prop_dict.get("fs_type", "") |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 479 | run_e2fsck = False |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 480 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 481 | fs_spans_partition = True |
| 482 | if fs_type.startswith("squash"): |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 483 | fs_spans_partition = False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 484 | |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 485 | is_verity_partition = "verity_block_device" in prop_dict |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 486 | verity_supported = prop_dict.get("verity") == "true" |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 487 | verity_fec_supported = prop_dict.get("verity_fec") == "true" |
| 488 | |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 489 | # Adjust the partition size to make room for the hashes if this is to be |
| 490 | # verified. |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 491 | if verity_supported and is_verity_partition: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 492 | partition_size = int(prop_dict.get("partition_size")) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 493 | (adjusted_size, verity_size) = AdjustPartitionSizeForVerity( |
| 494 | partition_size, verity_fec_supported) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 495 | if not adjusted_size: |
| 496 | return False |
| 497 | prop_dict["partition_size"] = str(adjusted_size) |
| 498 | prop_dict["original_partition_size"] = str(partition_size) |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 499 | prop_dict["verity_size"] = str(verity_size) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 500 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 501 | # Adjust partition size for AVB hash footer or AVB hashtree footer. |
| 502 | avb_footer_type = '' |
| 503 | if prop_dict.get("avb_hash_enable") == "true": |
| 504 | avb_footer_type = 'hash' |
| 505 | elif prop_dict.get("avb_hashtree_enable") == "true": |
| 506 | avb_footer_type = 'hashtree' |
| 507 | |
| 508 | if avb_footer_type: |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 509 | avbtool = prop_dict["avb_avbtool"] |
| 510 | partition_size = prop_dict["partition_size"] |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 511 | # avb_add_hash_footer_args or avb_add_hashtree_footer_args. |
| 512 | additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"] |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 513 | max_image_size = AVBCalcMaxImageSize(avbtool, avb_footer_type, |
| 514 | partition_size, additional_args) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 515 | if max_image_size == 0: |
| 516 | return False |
| 517 | prop_dict["partition_size"] = str(max_image_size) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 518 | prop_dict["original_partition_size"] = partition_size |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 519 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 520 | if fs_type.startswith("ext"): |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 521 | build_command = [prop_dict["ext_mkuserimg"]] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 522 | if "extfs_sparse_flag" in prop_dict: |
| 523 | build_command.append(prop_dict["extfs_sparse_flag"]) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 524 | run_e2fsck = True |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 525 | build_command.extend([in_dir, out_file, fs_type, |
| 526 | prop_dict["mount_point"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 527 | build_command.append(prop_dict["partition_size"]) |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 528 | if "journal_size" in prop_dict: |
| 529 | build_command.extend(["-j", prop_dict["journal_size"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 530 | if "timestamp" in prop_dict: |
| 531 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 532 | if fs_config: |
Doug Zongker | 8282282 | 2014-06-16 09:10:55 -0700 | [diff] [blame] | 533 | build_command.extend(["-C", fs_config]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 534 | if target_out: |
| 535 | build_command.extend(["-D", target_out]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 536 | if "block_list" in prop_dict: |
| 537 | build_command.extend(["-B", prop_dict["block_list"]]) |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 538 | if "base_fs_file" in prop_dict: |
| 539 | base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"]) |
| 540 | if base_fs_file is None: |
| 541 | return False |
| 542 | build_command.extend(["-d", base_fs_file]) |
Christoffer Dall | 8ed01f3 | 2014-12-17 21:34:12 +0100 | [diff] [blame] | 543 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 544 | if "extfs_inode_count" in prop_dict: |
| 545 | build_command.extend(["-i", prop_dict["extfs_inode_count"]]) |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 546 | if "flash_erase_block_size" in prop_dict: |
| 547 | build_command.extend(["-e", prop_dict["flash_erase_block_size"]]) |
| 548 | if "flash_logical_block_size" in prop_dict: |
| 549 | build_command.extend(["-o", prop_dict["flash_logical_block_size"]]) |
Tao Bao | d86e311 | 2017-09-22 15:45:33 -0700 | [diff] [blame] | 550 | # Specify UUID and hash_seed if using mke2fs. |
| 551 | if prop_dict["ext_mkuserimg"] == "mkuserimg_mke2fs.sh": |
| 552 | if "uuid" in prop_dict: |
| 553 | build_command.extend(["-U", prop_dict["uuid"]]) |
| 554 | if "hash_seed" in prop_dict: |
| 555 | build_command.extend(["-S", prop_dict["hash_seed"]]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 556 | if "selinux_fc" in prop_dict: |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 557 | build_command.append(prop_dict["selinux_fc"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 558 | elif fs_type.startswith("squash"): |
| 559 | build_command = ["mksquashfsimage.sh"] |
| 560 | build_command.extend([in_dir, out_file]) |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 561 | if "squashfs_sparse_flag" in prop_dict: |
| 562 | build_command.extend([prop_dict["squashfs_sparse_flag"]]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 563 | build_command.extend(["-m", prop_dict["mount_point"]]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 564 | if target_out: |
| 565 | build_command.extend(["-d", target_out]) |
Mohamad Ayyash | 8837882 | 2016-04-07 22:10:51 -0700 | [diff] [blame] | 566 | if fs_config: |
| 567 | build_command.extend(["-C", fs_config]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 568 | if "selinux_fc" in prop_dict: |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 569 | build_command.extend(["-c", prop_dict["selinux_fc"]]) |
Mohamad Ayyash | c3484f7 | 2016-06-13 09:46:58 -0700 | [diff] [blame] | 570 | if "block_list" in prop_dict: |
| 571 | build_command.extend(["-B", prop_dict["block_list"]]) |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 572 | if "squashfs_compressor" in prop_dict: |
| 573 | build_command.extend(["-z", prop_dict["squashfs_compressor"]]) |
| 574 | if "squashfs_compressor_opt" in prop_dict: |
| 575 | build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 576 | if "squashfs_block_size" in prop_dict: |
| 577 | build_command.extend(["-b", prop_dict["squashfs_block_size"]]) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 578 | if prop_dict.get("squashfs_disable_4k_align") == "true": |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 579 | build_command.extend(["-a"]) |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 580 | elif fs_type.startswith("f2fs"): |
| 581 | build_command = ["mkf2fsuserimg.sh"] |
| 582 | build_command.extend([out_file, prop_dict["partition_size"]]) |
Jaegeuk Kim | 2ea1eba | 2017-11-28 19:21:28 -0800 | [diff] [blame] | 583 | if fs_config: |
| 584 | build_command.extend(["-C", fs_config]) |
| 585 | build_command.extend(["-f", in_dir]) |
| 586 | if target_out: |
| 587 | build_command.extend(["-D", target_out]) |
| 588 | if "selinux_fc" in prop_dict: |
| 589 | build_command.extend(["-s", prop_dict["selinux_fc"]]) |
| 590 | build_command.extend(["-t", prop_dict["mount_point"]]) |
| 591 | if "timestamp" in prop_dict: |
| 592 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
| 593 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 594 | else: |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 595 | print("Error: unknown filesystem type '%s'" % (fs_type)) |
| 596 | return False |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 597 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 598 | if in_dir != origin_in: |
| 599 | # Construct a staging directory of the root file system. |
| 600 | ramdisk_dir = prop_dict.get("ramdisk_dir") |
| 601 | if ramdisk_dir: |
| 602 | shutil.rmtree(in_dir) |
| 603 | shutil.copytree(ramdisk_dir, in_dir, symlinks=True) |
| 604 | staging_system = os.path.join(in_dir, "system") |
| 605 | shutil.rmtree(staging_system, ignore_errors=True) |
| 606 | shutil.copytree(origin_in, staging_system, symlinks=True) |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 607 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 608 | (mkfs_output, exit_code) = RunCommand(build_command) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 609 | if exit_code != 0: |
Elliott Hughes | 73ff57f | 2017-12-06 12:16:39 -0800 | [diff] [blame] | 610 | print("Error: '%s' failed with exit code %d" % (build_command, exit_code)) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 611 | return False |
| 612 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 613 | # Check if there's enough headroom space available for ext4 image. |
Tao Bao | 79d52f8 | 2017-12-07 14:07:44 -0800 | [diff] [blame] | 614 | if "partition_headroom" in prop_dict and fs_type.startswith("ext4"): |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 615 | if not CheckHeadroom(mkfs_output, prop_dict): |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 616 | return False |
| 617 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 618 | if not fs_spans_partition: |
| 619 | mount_point = prop_dict.get("mount_point") |
| 620 | partition_size = int(prop_dict.get("partition_size")) |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 621 | image_size = GetSimgSize(out_file) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 622 | if image_size > partition_size: |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 623 | print("Error: %s image size of %d is larger than partition size of " |
| 624 | "%d" % (mount_point, image_size, partition_size)) |
| 625 | return False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 626 | if verity_supported and is_verity_partition: |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 627 | ZeroPadSimg(out_file, partition_size - image_size) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 628 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 629 | # Create the verified image if this is to be verified. |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 630 | if verity_supported and is_verity_partition: |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 631 | if not MakeVerityEnabledImage(out_file, verity_fec_supported, prop_dict): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 632 | return False |
| 633 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 634 | # Add AVB HASH or HASHTREE footer (metadata). |
| 635 | if avb_footer_type: |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 636 | avbtool = prop_dict["avb_avbtool"] |
| 637 | original_partition_size = prop_dict["original_partition_size"] |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 638 | partition_name = prop_dict["partition_name"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 639 | # key_path and algorithm are only available when chain partition is used. |
| 640 | key_path = prop_dict.get("avb_key_path") |
| 641 | algorithm = prop_dict.get("avb_algorithm") |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 642 | salt = prop_dict.get("avb_salt") |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 643 | # avb_add_hash_footer_args or avb_add_hashtree_footer_args |
| 644 | additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"] |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 645 | if not AVBAddFooter(out_file, avbtool, avb_footer_type, |
| 646 | original_partition_size, partition_name, key_path, |
| 647 | algorithm, salt, additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 648 | return False |
| 649 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 650 | if run_e2fsck and prop_dict.get("skip_fsck") != "true": |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 651 | success, unsparse_image = UnsparseImage(out_file, replace=False) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 652 | if not success: |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 653 | return False |
| 654 | |
| 655 | # Run e2fsck on the inflated image file |
| 656 | e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 657 | (_, exit_code) = RunCommand(e2fsck_command) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 658 | |
| 659 | os.remove(unsparse_image) |
| 660 | |
Elliott Hughes | 73ff57f | 2017-12-06 12:16:39 -0800 | [diff] [blame] | 661 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 662 | print("Error: '%s' failed with exit code %d" % (e2fsck_command, |
| 663 | exit_code)) |
Elliott Hughes | 73ff57f | 2017-12-06 12:16:39 -0800 | [diff] [blame] | 664 | return False |
| 665 | |
| 666 | return True |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 667 | |
| 668 | |
| 669 | def ImagePropFromGlobalDict(glob_dict, mount_point): |
| 670 | """Build an image property dictionary from the global dictionary. |
| 671 | |
| 672 | Args: |
| 673 | glob_dict: the global dictionary from the build system. |
| 674 | mount_point: such as "system", "data" etc. |
| 675 | """ |
Doug Zongker | 1ad7ade | 2013-12-06 11:53:27 -0800 | [diff] [blame] | 676 | d = {} |
Tao Bao | 052ae35 | 2015-09-28 13:44:13 -0700 | [diff] [blame] | 677 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 678 | if "build.prop" in glob_dict: |
| 679 | bp = glob_dict["build.prop"] |
| 680 | if "ro.build.date.utc" in bp: |
| 681 | d["timestamp"] = bp["ro.build.date.utc"] |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 682 | |
| 683 | def copy_prop(src_p, dest_p): |
| 684 | if src_p in glob_dict: |
| 685 | d[dest_p] = str(glob_dict[src_p]) |
| 686 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 687 | common_props = ( |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 688 | "extfs_sparse_flag", |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 689 | "squashfs_sparse_flag", |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 690 | "selinux_fc", |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 691 | "skip_fsck", |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 692 | "ext_mkuserimg", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 693 | "verity", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 694 | "verity_key", |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 695 | "verity_signer_cmd", |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 696 | "verity_fec", |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 697 | "verity_disable", |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 698 | "avb_enable", |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 699 | "avb_avbtool", |
| 700 | "avb_salt", |
| 701 | ) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 702 | for p in common_props: |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 703 | copy_prop(p, p) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 704 | |
| 705 | d["mount_point"] = mount_point |
| 706 | if mount_point == "system": |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 707 | copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable") |
| 708 | copy_prop("avb_system_add_hashtree_footer_args", |
| 709 | "avb_add_hashtree_footer_args") |
| 710 | copy_prop("avb_system_key_path", "avb_key_path") |
| 711 | copy_prop("avb_system_algorithm", "avb_algorithm") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 712 | copy_prop("fs_type", "fs_type") |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 713 | # Copy the generic system fs type first, override with specific one if |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 714 | # available. |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 715 | copy_prop("system_fs_type", "fs_type") |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 716 | copy_prop("system_headroom", "partition_headroom") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 717 | copy_prop("system_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 718 | copy_prop("system_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 719 | copy_prop("system_verity_block_device", "verity_block_device") |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 720 | copy_prop("system_root_image", "system_root_image") |
| 721 | copy_prop("ramdisk_dir", "ramdisk_dir") |
Tao Bao | 84e7568 | 2015-07-19 02:38:53 -0700 | [diff] [blame] | 722 | copy_prop("ramdisk_fs_config", "ramdisk_fs_config") |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 723 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 724 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 725 | copy_prop("system_squashfs_block_size", "squashfs_block_size") |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 726 | copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 727 | copy_prop("system_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 728 | copy_prop("system_extfs_inode_count", "extfs_inode_count") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 729 | elif mount_point == "system_other": |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 730 | # We inherit the selinux policies of /system since we contain some of its |
| 731 | # files. |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 732 | d["mount_point"] = "system" |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 733 | copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable") |
| 734 | copy_prop("avb_system_add_hashtree_footer_args", |
| 735 | "avb_add_hashtree_footer_args") |
| 736 | copy_prop("avb_system_key_path", "avb_key_path") |
| 737 | copy_prop("avb_system_algorithm", "avb_algorithm") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 738 | copy_prop("fs_type", "fs_type") |
| 739 | copy_prop("system_fs_type", "fs_type") |
| 740 | copy_prop("system_size", "partition_size") |
| 741 | copy_prop("system_journal_size", "journal_size") |
| 742 | copy_prop("system_verity_block_device", "verity_block_device") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 743 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 744 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
| 745 | copy_prop("system_squashfs_block_size", "squashfs_block_size") |
| 746 | copy_prop("system_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 747 | copy_prop("system_extfs_inode_count", "extfs_inode_count") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 748 | elif mount_point == "data": |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 749 | # Copy the generic fs type first, override with specific one if available. |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 750 | copy_prop("fs_type", "fs_type") |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 751 | copy_prop("userdata_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 752 | copy_prop("userdata_size", "partition_size") |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 753 | copy_prop("flash_logical_block_size", "flash_logical_block_size") |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 754 | copy_prop("flash_erase_block_size", "flash_erase_block_size") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 755 | elif mount_point == "cache": |
| 756 | copy_prop("cache_fs_type", "fs_type") |
| 757 | copy_prop("cache_size", "partition_size") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 758 | elif mount_point == "vendor": |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 759 | copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable") |
| 760 | copy_prop("avb_vendor_add_hashtree_footer_args", |
| 761 | "avb_add_hashtree_footer_args") |
| 762 | copy_prop("avb_vendor_key_path", "avb_key_path") |
| 763 | copy_prop("avb_vendor_algorithm", "avb_algorithm") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 764 | copy_prop("vendor_fs_type", "fs_type") |
| 765 | copy_prop("vendor_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 766 | copy_prop("vendor_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 767 | copy_prop("vendor_verity_block_device", "verity_block_device") |
Patrick Tjin | e11aa50 | 2016-02-09 15:40:38 -0800 | [diff] [blame] | 768 | copy_prop("vendor_squashfs_compressor", "squashfs_compressor") |
| 769 | copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt") |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 770 | copy_prop("vendor_squashfs_block_size", "squashfs_block_size") |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 771 | copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 772 | copy_prop("vendor_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 773 | copy_prop("vendor_extfs_inode_count", "extfs_inode_count") |
Ying Wang | b888843 | 2014-03-11 17:13:27 -0700 | [diff] [blame] | 774 | elif mount_point == "oem": |
| 775 | copy_prop("fs_type", "fs_type") |
| 776 | copy_prop("oem_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 777 | copy_prop("oem_journal_size", "journal_size") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 778 | copy_prop("oem_extfs_inode_count", "extfs_inode_count") |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 779 | d["partition_name"] = mount_point |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 780 | return d |
| 781 | |
| 782 | |
| 783 | def LoadGlobalDict(filename): |
| 784 | """Load "name=value" pairs from filename""" |
| 785 | d = {} |
| 786 | f = open(filename) |
| 787 | for line in f: |
| 788 | line = line.strip() |
| 789 | if not line or line.startswith("#"): |
| 790 | continue |
| 791 | k, v = line.split("=", 1) |
| 792 | d[k] = v |
| 793 | f.close() |
| 794 | return d |
| 795 | |
| 796 | |
| 797 | def main(argv): |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 798 | if len(argv) != 4: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 799 | print(__doc__) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 800 | sys.exit(1) |
| 801 | |
| 802 | in_dir = argv[0] |
| 803 | glob_dict_file = argv[1] |
| 804 | out_file = argv[2] |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 805 | target_out = argv[3] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 806 | |
| 807 | glob_dict = LoadGlobalDict(glob_dict_file) |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 808 | if "mount_point" in glob_dict: |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 809 | # The caller knows the mount point and provides a dictionay needed by |
| 810 | # BuildImage(). |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 811 | image_properties = glob_dict |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 812 | else: |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 813 | image_filename = os.path.basename(out_file) |
| 814 | mount_point = "" |
| 815 | if image_filename == "system.img": |
| 816 | mount_point = "system" |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 817 | elif image_filename == "system_other.img": |
| 818 | mount_point = "system_other" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 819 | elif image_filename == "userdata.img": |
| 820 | mount_point = "data" |
| 821 | elif image_filename == "cache.img": |
| 822 | mount_point = "cache" |
| 823 | elif image_filename == "vendor.img": |
| 824 | mount_point = "vendor" |
| 825 | elif image_filename == "oem.img": |
| 826 | mount_point = "oem" |
| 827 | else: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 828 | print("error: unknown image file name ", image_filename, file=sys.stderr) |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 829 | sys.exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 830 | |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 831 | image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) |
| 832 | |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 833 | if not BuildImage(in_dir, image_properties, out_file, target_out): |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 834 | print("error: failed to build %s from %s" % (out_file, in_dir), |
| 835 | file=sys.stderr) |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 836 | sys.exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 837 | |
| 838 | |
| 839 | if __name__ == '__main__': |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 840 | try: |
| 841 | main(sys.argv[1:]) |
| 842 | finally: |
| 843 | common.Cleanup() |