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): |
Tao Bao | b4ec6d7 | 2018-03-15 23:21:28 -0700 | [diff] [blame^] | 83 | cmd = ["build_verity_metadata.py", "size", str(partition_size)] |
Tianjie Xu | 149b7fb | 2017-09-01 15:36:08 -0700 | [diff] [blame] | 84 | output, exit_code = RunCommand(cmd, False) |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 85 | if exit_code != 0: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 86 | return False, 0 |
| 87 | return True, int(output) |
| 88 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 89 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 90 | def GetVeritySize(partition_size, fec_supported): |
| 91 | success, verity_tree_size = GetVerityTreeSize(partition_size) |
| 92 | if not success: |
| 93 | return 0 |
| 94 | success, verity_metadata_size = GetVerityMetadataSize(partition_size) |
| 95 | if not success: |
| 96 | return 0 |
| 97 | verity_size = verity_tree_size + verity_metadata_size |
| 98 | if fec_supported: |
| 99 | success, fec_size = GetVerityFECSize(partition_size + verity_size) |
| 100 | if not success: |
| 101 | return 0 |
| 102 | return verity_size + fec_size |
| 103 | return verity_size |
| 104 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 105 | |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 106 | def GetSimgSize(image_file): |
| 107 | simg = sparse_img.SparseImage(image_file, build_map=False) |
| 108 | return simg.blocksize * simg.total_blocks |
| 109 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 110 | |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 111 | def ZeroPadSimg(image_file, pad_size): |
| 112 | blocks = pad_size // BLOCK_SIZE |
| 113 | print("Padding %d blocks (%d bytes)" % (blocks, pad_size)) |
| 114 | simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False) |
| 115 | simg.AppendFillChunk(0, blocks) |
| 116 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 117 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 118 | def AVBCalcMaxImageSize(avbtool, footer_type, partition_size, additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 119 | """Calculates max image size for a given partition size. |
| 120 | |
| 121 | Args: |
| 122 | avbtool: String with path to avbtool. |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 123 | footer_type: 'hash' or 'hashtree' for generating footer. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 124 | partition_size: The size of the partition in question. |
| 125 | additional_args: Additional arguments to pass to 'avbtool |
| 126 | add_hashtree_image'. |
| 127 | Returns: |
| 128 | The maximum image size or 0 if an error occurred. |
| 129 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 130 | cmd = [avbtool, "add_%s_footer" % footer_type, |
| 131 | "--partition_size", partition_size, "--calc_max_image_size"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 132 | cmd.extend(shlex.split(additional_args)) |
| 133 | |
| 134 | (output, exit_code) = RunCommand(cmd) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 135 | if exit_code != 0: |
| 136 | return 0 |
| 137 | else: |
| 138 | return int(output) |
| 139 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 140 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 141 | def AVBAddFooter(image_path, avbtool, footer_type, partition_size, |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 142 | partition_name, key_path, algorithm, salt, |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 143 | additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 144 | """Adds dm-verity hashtree and AVB metadata to an image. |
| 145 | |
| 146 | Args: |
| 147 | image_path: Path to image to modify. |
| 148 | avbtool: String with path to avbtool. |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 149 | footer_type: 'hash' or 'hashtree' for generating footer. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 150 | partition_size: The size of the partition in question. |
| 151 | partition_name: The name of the partition - will be embedded in metadata. |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 152 | key_path: Path to key to use or None. |
| 153 | algorithm: Name of algorithm to use or None. |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 154 | salt: The salt to use (a hexadecimal string) or None. |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 155 | additional_args: Additional arguments to pass to 'avbtool |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 156 | add_hashtree_image'. |
| 157 | |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 158 | Returns: |
| 159 | True if the operation succeeded. |
| 160 | """ |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 161 | cmd = [avbtool, "add_%s_footer" % footer_type, |
| 162 | "--partition_size", partition_size, |
| 163 | "--partition_name", partition_name, |
| 164 | "--image", image_path] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 165 | |
| 166 | if key_path and algorithm: |
| 167 | cmd.extend(["--key", key_path, "--algorithm", algorithm]) |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 168 | if salt: |
| 169 | cmd.extend(["--salt", salt]) |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 170 | |
| 171 | cmd.extend(shlex.split(additional_args)) |
| 172 | |
| 173 | (_, exit_code) = RunCommand(cmd) |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 174 | return exit_code == 0 |
| 175 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 176 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 177 | def AdjustPartitionSizeForVerity(partition_size, fec_supported): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 178 | """Modifies the provided partition size to account for the verity metadata. |
| 179 | |
| 180 | This information is used to size the created image appropriately. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 181 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 182 | Args: |
| 183 | partition_size: the size of the partition to be verified. |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 184 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 185 | Returns: |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 186 | A tuple of the size of the partition adjusted for verity metadata, and |
| 187 | the size of verity metadata. |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 188 | """ |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 189 | key = "%d %d" % (partition_size, fec_supported) |
| 190 | if key in AdjustPartitionSizeForVerity.results: |
| 191 | return AdjustPartitionSizeForVerity.results[key] |
| 192 | |
| 193 | hi = partition_size |
| 194 | if hi % BLOCK_SIZE != 0: |
| 195 | hi = (hi // BLOCK_SIZE) * BLOCK_SIZE |
| 196 | |
| 197 | # verity tree and fec sizes depend on the partition size, which |
| 198 | # means this estimate is always going to be unnecessarily small |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 199 | verity_size = GetVeritySize(hi, fec_supported) |
| 200 | lo = partition_size - verity_size |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 201 | result = lo |
| 202 | |
| 203 | # do a binary search for the optimal size |
| 204 | while lo < hi: |
| 205 | i = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 206 | v = GetVeritySize(i, fec_supported) |
| 207 | if i + v <= partition_size: |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 208 | if result < i: |
| 209 | result = i |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 210 | verity_size = v |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 211 | lo = i + BLOCK_SIZE |
| 212 | else: |
| 213 | hi = i |
| 214 | |
Tomasz Wasilczyk | 29ec06b | 2017-11-15 10:34:01 -0800 | [diff] [blame] | 215 | if OPTIONS.verbose: |
| 216 | print("Adjusted partition size for verity, partition_size: {}," |
| 217 | " verity_size: {}".format(result, verity_size)) |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 218 | AdjustPartitionSizeForVerity.results[key] = (result, verity_size) |
| 219 | return (result, verity_size) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 220 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 221 | |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 222 | AdjustPartitionSizeForVerity.results = {} |
| 223 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 224 | |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 225 | def BuildVerityFEC(sparse_image_path, verity_path, verity_fec_path, |
| 226 | padding_size): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 227 | cmd = ["fec", "-e", "-p", str(padding_size), sparse_image_path, |
| 228 | verity_path, verity_fec_path] |
| 229 | output, exit_code = RunCommand(cmd) |
| 230 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 231 | print("Could not build FEC data! Error: %s" % output) |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 232 | return False |
| 233 | return True |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 234 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 235 | |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 236 | def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict): |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 237 | cmd = ["build_verity_tree", "-A", FIXED_SALT, sparse_image_path, |
| 238 | verity_image_path] |
| 239 | output, exit_code = RunCommand(cmd) |
| 240 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 241 | print("Could not build verity tree! Error: %s" % output) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 242 | return False |
| 243 | root, salt = output.split() |
| 244 | prop_dict["verity_root_hash"] = root |
| 245 | prop_dict["verity_salt"] = salt |
| 246 | return True |
| 247 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 248 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 249 | def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 250 | block_device, signer_path, key, signer_args, |
| 251 | verity_disable): |
Tao Bao | b4ec6d7 | 2018-03-15 23:21:28 -0700 | [diff] [blame^] | 252 | cmd = ["build_verity_metadata.py", "build", str(image_size), |
| 253 | verity_metadata_path, root_hash, salt, block_device, signer_path, key] |
Tao Bao | 4581042 | 2016-10-17 16:20:12 -0700 | [diff] [blame] | 254 | if signer_args: |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 255 | cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),)) |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 256 | if verity_disable: |
| 257 | cmd.append("--verity_disable") |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 258 | output, exit_code = RunCommand(cmd) |
| 259 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 260 | print("Could not build verity metadata! Error: %s" % output) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 261 | return False |
| 262 | return True |
| 263 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 264 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 265 | def Append2Simg(sparse_image_path, unsparse_image_path, error_message): |
| 266 | """Appends the unsparse image to the given sparse image. |
| 267 | |
| 268 | Args: |
| 269 | sparse_image_path: the path to the (sparse) image |
| 270 | unsparse_image_path: the path to the (unsparse) image |
| 271 | Returns: |
| 272 | True on success, False on failure. |
| 273 | """ |
Tianjie Xu | e3ad41b | 2017-03-08 11:05:56 -0800 | [diff] [blame] | 274 | cmd = ["append2simg", sparse_image_path, unsparse_image_path] |
| 275 | output, exit_code = RunCommand(cmd) |
| 276 | if exit_code != 0: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 277 | print("%s: %s" % (error_message, output)) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 278 | return False |
| 279 | return True |
| 280 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 281 | |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 282 | def Append(target, file_to_append, error_message): |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 283 | """Appends file_to_append to target.""" |
| 284 | try: |
| 285 | 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] | 286 | for line in input_file: |
| 287 | out_file.write(line) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 288 | except IOError: |
| 289 | print(error_message) |
| 290 | return False |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 291 | return True |
| 292 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 293 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 294 | def BuildVerifiedImage(data_image_path, verity_image_path, |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 295 | verity_metadata_path, verity_fec_path, |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 296 | padding_size, fec_supported): |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 297 | if not Append(verity_image_path, verity_metadata_path, |
| 298 | "Could not append verity metadata!"): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 299 | return False |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 300 | |
| 301 | if fec_supported: |
| 302 | # build FEC for the entire partition, including metadata |
| 303 | if not BuildVerityFEC(data_image_path, verity_image_path, |
Sami Tolvanen | 433905f | 2016-09-01 15:58:35 -0700 | [diff] [blame] | 304 | verity_fec_path, padding_size): |
Sami Tolvanen | 4a06004 | 2015-12-18 15:50:25 +0000 | [diff] [blame] | 305 | return False |
| 306 | |
| 307 | if not Append(verity_image_path, verity_fec_path, "Could not append FEC!"): |
| 308 | return False |
| 309 | |
Sami Tolvanen | ff914f5 | 2015-12-18 13:24:56 +0000 | [diff] [blame] | 310 | if not Append2Simg(data_image_path, verity_image_path, |
| 311 | "Could not append verity data!"): |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 312 | return False |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 313 | return True |
| 314 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 315 | |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 316 | def UnsparseImage(sparse_image_path, replace=True): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 317 | img_dir = os.path.dirname(sparse_image_path) |
| 318 | unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path) |
| 319 | unsparse_image_path = os.path.join(img_dir, unsparse_image_path) |
| 320 | if os.path.exists(unsparse_image_path): |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 321 | if replace: |
| 322 | os.unlink(unsparse_image_path) |
| 323 | else: |
| 324 | return True, unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 325 | inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] |
Tao Bao | cd53a89 | 2018-01-19 10:29:52 -0800 | [diff] [blame] | 326 | (inflate_output, exit_code) = RunCommand(inflate_command) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 327 | if exit_code != 0: |
Tao Bao | cd53a89 | 2018-01-19 10:29:52 -0800 | [diff] [blame] | 328 | print("Error: '%s' failed with exit code %d:\n%s" % ( |
| 329 | inflate_command, exit_code, inflate_output)) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 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"]]) |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 556 | if "ext4_share_dup_blocks" in prop_dict: |
| 557 | build_command.append("-c") |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 558 | if "selinux_fc" in prop_dict: |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 559 | build_command.append(prop_dict["selinux_fc"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 560 | elif fs_type.startswith("squash"): |
| 561 | build_command = ["mksquashfsimage.sh"] |
| 562 | build_command.extend([in_dir, out_file]) |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 563 | if "squashfs_sparse_flag" in prop_dict: |
| 564 | build_command.extend([prop_dict["squashfs_sparse_flag"]]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 565 | build_command.extend(["-m", prop_dict["mount_point"]]) |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 566 | if target_out: |
| 567 | build_command.extend(["-d", target_out]) |
Mohamad Ayyash | 8837882 | 2016-04-07 22:10:51 -0700 | [diff] [blame] | 568 | if fs_config: |
| 569 | build_command.extend(["-C", fs_config]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 570 | if "selinux_fc" in prop_dict: |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 571 | build_command.extend(["-c", prop_dict["selinux_fc"]]) |
Mohamad Ayyash | c3484f7 | 2016-06-13 09:46:58 -0700 | [diff] [blame] | 572 | if "block_list" in prop_dict: |
| 573 | build_command.extend(["-B", prop_dict["block_list"]]) |
Ng Zhi An | 9446c1d | 2018-01-19 15:51:46 -0800 | [diff] [blame] | 574 | if "squashfs_block_size" in prop_dict: |
| 575 | build_command.extend(["-b", prop_dict["squashfs_block_size"]]) |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 576 | if "squashfs_compressor" in prop_dict: |
| 577 | build_command.extend(["-z", prop_dict["squashfs_compressor"]]) |
| 578 | if "squashfs_compressor_opt" in prop_dict: |
| 579 | build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 580 | if prop_dict.get("squashfs_disable_4k_align") == "true": |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 581 | build_command.extend(["-a"]) |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 582 | elif fs_type.startswith("f2fs"): |
| 583 | build_command = ["mkf2fsuserimg.sh"] |
| 584 | build_command.extend([out_file, prop_dict["partition_size"]]) |
Jaegeuk Kim | 2ea1eba | 2017-11-28 19:21:28 -0800 | [diff] [blame] | 585 | if fs_config: |
| 586 | build_command.extend(["-C", fs_config]) |
| 587 | build_command.extend(["-f", in_dir]) |
| 588 | if target_out: |
| 589 | build_command.extend(["-D", target_out]) |
| 590 | if "selinux_fc" in prop_dict: |
| 591 | build_command.extend(["-s", prop_dict["selinux_fc"]]) |
| 592 | build_command.extend(["-t", prop_dict["mount_point"]]) |
| 593 | if "timestamp" in prop_dict: |
| 594 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
| 595 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 596 | else: |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 597 | print("Error: unknown filesystem type '%s'" % (fs_type)) |
| 598 | return False |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 599 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 600 | if in_dir != origin_in: |
| 601 | # Construct a staging directory of the root file system. |
| 602 | ramdisk_dir = prop_dict.get("ramdisk_dir") |
| 603 | if ramdisk_dir: |
| 604 | shutil.rmtree(in_dir) |
| 605 | shutil.copytree(ramdisk_dir, in_dir, symlinks=True) |
| 606 | staging_system = os.path.join(in_dir, "system") |
| 607 | shutil.rmtree(staging_system, ignore_errors=True) |
| 608 | shutil.copytree(origin_in, staging_system, symlinks=True) |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 609 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 610 | (mkfs_output, exit_code) = RunCommand(build_command) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 611 | if exit_code != 0: |
Tao Bao | cd53a89 | 2018-01-19 10:29:52 -0800 | [diff] [blame] | 612 | print("Error: '%s' failed with exit code %d:\n%s" % ( |
| 613 | build_command, exit_code, mkfs_output)) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 614 | return False |
| 615 | |
Tao Bao | d4349f2 | 2017-12-07 23:01:25 -0800 | [diff] [blame] | 616 | # Check if there's enough headroom space available for ext4 image. |
Tao Bao | 79d52f8 | 2017-12-07 14:07:44 -0800 | [diff] [blame] | 617 | if "partition_headroom" in prop_dict and fs_type.startswith("ext4"): |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 618 | if not CheckHeadroom(mkfs_output, prop_dict): |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 619 | return False |
| 620 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 621 | if not fs_spans_partition: |
| 622 | mount_point = prop_dict.get("mount_point") |
| 623 | partition_size = int(prop_dict.get("partition_size")) |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 624 | image_size = GetSimgSize(out_file) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 625 | if image_size > partition_size: |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 626 | print("Error: %s image size of %d is larger than partition size of " |
| 627 | "%d" % (mount_point, image_size, partition_size)) |
| 628 | return False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 629 | if verity_supported and is_verity_partition: |
Sami Tolvanen | 405e71d | 2016-02-09 12:28:58 -0800 | [diff] [blame] | 630 | ZeroPadSimg(out_file, partition_size - image_size) |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 631 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 632 | # Create the verified image if this is to be verified. |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 633 | if verity_supported and is_verity_partition: |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 634 | if not MakeVerityEnabledImage(out_file, verity_fec_supported, prop_dict): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 635 | return False |
| 636 | |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 637 | # Add AVB HASH or HASHTREE footer (metadata). |
| 638 | if avb_footer_type: |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 639 | avbtool = prop_dict["avb_avbtool"] |
| 640 | original_partition_size = prop_dict["original_partition_size"] |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 641 | partition_name = prop_dict["partition_name"] |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 642 | # key_path and algorithm are only available when chain partition is used. |
| 643 | key_path = prop_dict.get("avb_key_path") |
| 644 | algorithm = prop_dict.get("avb_algorithm") |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 645 | salt = prop_dict.get("avb_salt") |
Bowgo Tsai | 7ea994b | 2017-05-19 23:44:26 +0800 | [diff] [blame] | 646 | # avb_add_hash_footer_args or avb_add_hashtree_footer_args |
| 647 | additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"] |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 648 | if not AVBAddFooter(out_file, avbtool, avb_footer_type, |
| 649 | original_partition_size, partition_name, key_path, |
| 650 | algorithm, salt, additional_args): |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 651 | return False |
| 652 | |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 653 | if run_e2fsck and prop_dict.get("skip_fsck") != "true": |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 654 | success, unsparse_image = UnsparseImage(out_file, replace=False) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 655 | if not success: |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 656 | return False |
| 657 | |
| 658 | # Run e2fsck on the inflated image file |
| 659 | e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] |
Tao Bao | cd53a89 | 2018-01-19 10:29:52 -0800 | [diff] [blame] | 660 | (e2fsck_output, exit_code) = RunCommand(e2fsck_command) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 661 | |
| 662 | os.remove(unsparse_image) |
| 663 | |
Elliott Hughes | 73ff57f | 2017-12-06 12:16:39 -0800 | [diff] [blame] | 664 | if exit_code != 0: |
Tao Bao | cd53a89 | 2018-01-19 10:29:52 -0800 | [diff] [blame] | 665 | print("Error: '%s' failed with exit code %d:\n%s" % ( |
| 666 | e2fsck_command, exit_code, e2fsck_output)) |
Elliott Hughes | 73ff57f | 2017-12-06 12:16:39 -0800 | [diff] [blame] | 667 | return False |
| 668 | |
| 669 | return True |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 670 | |
| 671 | |
| 672 | def ImagePropFromGlobalDict(glob_dict, mount_point): |
| 673 | """Build an image property dictionary from the global dictionary. |
| 674 | |
| 675 | Args: |
| 676 | glob_dict: the global dictionary from the build system. |
| 677 | mount_point: such as "system", "data" etc. |
| 678 | """ |
Doug Zongker | 1ad7ade | 2013-12-06 11:53:27 -0800 | [diff] [blame] | 679 | d = {} |
Tao Bao | 052ae35 | 2015-09-28 13:44:13 -0700 | [diff] [blame] | 680 | |
Tao Bao | 822f584 | 2015-09-30 16:01:14 -0700 | [diff] [blame] | 681 | if "build.prop" in glob_dict: |
| 682 | bp = glob_dict["build.prop"] |
| 683 | if "ro.build.date.utc" in bp: |
| 684 | d["timestamp"] = bp["ro.build.date.utc"] |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 685 | |
| 686 | def copy_prop(src_p, dest_p): |
| 687 | if src_p in glob_dict: |
| 688 | d[dest_p] = str(glob_dict[src_p]) |
| 689 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 690 | common_props = ( |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 691 | "extfs_sparse_flag", |
Todd Poynor | b2a555e | 2015-12-15 18:00:14 -0800 | [diff] [blame] | 692 | "squashfs_sparse_flag", |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 693 | "selinux_fc", |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 694 | "skip_fsck", |
Adrien Schildknecht | 9a072cc | 2016-11-18 17:06:29 -0800 | [diff] [blame] | 695 | "ext_mkuserimg", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 696 | "verity", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 697 | "verity_key", |
Sami Tolvanen | f99b531 | 2015-05-20 07:30:57 +0100 | [diff] [blame] | 698 | "verity_signer_cmd", |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 699 | "verity_fec", |
Bowgo Tsai | 6ceeb1a | 2017-10-11 16:21:48 +0800 | [diff] [blame] | 700 | "verity_disable", |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 701 | "avb_enable", |
Tao Bao | 2b6dfd6 | 2017-09-27 17:17:43 -0700 | [diff] [blame] | 702 | "avb_avbtool", |
| 703 | "avb_salt", |
| 704 | ) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 705 | for p in common_props: |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 706 | copy_prop(p, p) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 707 | |
| 708 | d["mount_point"] = mount_point |
| 709 | if mount_point == "system": |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 710 | copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable") |
| 711 | copy_prop("avb_system_add_hashtree_footer_args", |
| 712 | "avb_add_hashtree_footer_args") |
| 713 | copy_prop("avb_system_key_path", "avb_key_path") |
| 714 | copy_prop("avb_system_algorithm", "avb_algorithm") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 715 | copy_prop("fs_type", "fs_type") |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 716 | # Copy the generic system fs type first, override with specific one if |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 717 | # available. |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 718 | copy_prop("system_fs_type", "fs_type") |
Julius D'souza | 001c676 | 2017-05-03 13:43:27 -0700 | [diff] [blame] | 719 | copy_prop("system_headroom", "partition_headroom") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 720 | copy_prop("system_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 721 | copy_prop("system_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 722 | copy_prop("system_verity_block_device", "verity_block_device") |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 723 | copy_prop("system_root_image", "system_root_image") |
| 724 | copy_prop("ramdisk_dir", "ramdisk_dir") |
Tao Bao | 84e7568 | 2015-07-19 02:38:53 -0700 | [diff] [blame] | 725 | copy_prop("ramdisk_fs_config", "ramdisk_fs_config") |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 726 | copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks") |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 727 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 728 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 729 | copy_prop("system_squashfs_block_size", "squashfs_block_size") |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 730 | copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 731 | copy_prop("system_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 732 | copy_prop("system_extfs_inode_count", "extfs_inode_count") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 733 | elif mount_point == "system_other": |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 734 | # We inherit the selinux policies of /system since we contain some of its |
| 735 | # files. |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 736 | d["mount_point"] = "system" |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 737 | copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable") |
| 738 | copy_prop("avb_system_add_hashtree_footer_args", |
| 739 | "avb_add_hashtree_footer_args") |
| 740 | copy_prop("avb_system_key_path", "avb_key_path") |
| 741 | copy_prop("avb_system_algorithm", "avb_algorithm") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 742 | copy_prop("fs_type", "fs_type") |
| 743 | copy_prop("system_fs_type", "fs_type") |
| 744 | copy_prop("system_size", "partition_size") |
| 745 | copy_prop("system_journal_size", "journal_size") |
| 746 | copy_prop("system_verity_block_device", "verity_block_device") |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 747 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 748 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
| 749 | copy_prop("system_squashfs_block_size", "squashfs_block_size") |
| 750 | copy_prop("system_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 751 | copy_prop("system_extfs_inode_count", "extfs_inode_count") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 752 | elif mount_point == "data": |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 753 | # Copy the generic fs type first, override with specific one if available. |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 754 | copy_prop("fs_type", "fs_type") |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 755 | copy_prop("userdata_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 756 | copy_prop("userdata_size", "partition_size") |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 757 | copy_prop("flash_logical_block_size", "flash_logical_block_size") |
Connor O'Brien | 20f08c3 | 2017-01-05 16:48:14 -0800 | [diff] [blame] | 758 | copy_prop("flash_erase_block_size", "flash_erase_block_size") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 759 | elif mount_point == "cache": |
| 760 | copy_prop("cache_fs_type", "fs_type") |
| 761 | copy_prop("cache_size", "partition_size") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 762 | elif mount_point == "vendor": |
Bowgo Tsai | 3e599ea | 2017-05-26 18:30:04 +0800 | [diff] [blame] | 763 | copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable") |
| 764 | copy_prop("avb_vendor_add_hashtree_footer_args", |
| 765 | "avb_add_hashtree_footer_args") |
| 766 | copy_prop("avb_vendor_key_path", "avb_key_path") |
| 767 | copy_prop("avb_vendor_algorithm", "avb_algorithm") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 768 | copy_prop("vendor_fs_type", "fs_type") |
| 769 | copy_prop("vendor_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 770 | copy_prop("vendor_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 771 | copy_prop("vendor_verity_block_device", "verity_block_device") |
Jin Qian | fde9f79 | 2018-01-22 13:15:46 -0800 | [diff] [blame] | 772 | copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks") |
Patrick Tjin | e11aa50 | 2016-02-09 15:40:38 -0800 | [diff] [blame] | 773 | copy_prop("vendor_squashfs_compressor", "squashfs_compressor") |
| 774 | copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt") |
Mohamad Ayyash | dfec815 | 2016-05-24 12:59:30 -0700 | [diff] [blame] | 775 | copy_prop("vendor_squashfs_block_size", "squashfs_block_size") |
Mohamad Ayyash | 1b6d348 | 2016-06-15 15:53:07 -0700 | [diff] [blame] | 776 | copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
Mohamad Ayyash | f876555 | 2016-03-02 21:07:23 -0800 | [diff] [blame] | 777 | copy_prop("vendor_base_fs_file", "base_fs_file") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 778 | copy_prop("vendor_extfs_inode_count", "extfs_inode_count") |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 779 | elif mount_point == "product": |
| 780 | copy_prop("avb_product_hashtree_enable", "avb_hashtree_enable") |
| 781 | copy_prop("avb_product_add_hashtree_footer_args", |
| 782 | "avb_add_hashtree_footer_args") |
| 783 | copy_prop("avb_product_key_path", "avb_key_path") |
| 784 | copy_prop("avb_product_algorithm", "avb_algorithm") |
| 785 | copy_prop("product_fs_type", "fs_type") |
| 786 | copy_prop("product_size", "partition_size") |
| 787 | copy_prop("product_journal_size", "journal_size") |
| 788 | copy_prop("product_verity_block_device", "verity_block_device") |
| 789 | copy_prop("product_squashfs_compressor", "squashfs_compressor") |
| 790 | copy_prop("product_squashfs_compressor_opt", "squashfs_compressor_opt") |
| 791 | copy_prop("product_squashfs_block_size", "squashfs_block_size") |
| 792 | copy_prop("product_squashfs_disable_4k_align", "squashfs_disable_4k_align") |
| 793 | copy_prop("product_base_fs_file", "base_fs_file") |
| 794 | copy_prop("product_extfs_inode_count", "extfs_inode_count") |
Ying Wang | b888843 | 2014-03-11 17:13:27 -0700 | [diff] [blame] | 795 | elif mount_point == "oem": |
| 796 | copy_prop("fs_type", "fs_type") |
| 797 | copy_prop("oem_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 798 | copy_prop("oem_journal_size", "journal_size") |
Patrick Tjin | a190084 | 2016-10-20 10:58:12 -0700 | [diff] [blame] | 799 | copy_prop("oem_extfs_inode_count", "extfs_inode_count") |
David Zeuthen | 4014a9d | 2016-09-30 17:29:22 -0400 | [diff] [blame] | 800 | d["partition_name"] = mount_point |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 801 | return d |
| 802 | |
| 803 | |
| 804 | def LoadGlobalDict(filename): |
| 805 | """Load "name=value" pairs from filename""" |
| 806 | d = {} |
| 807 | f = open(filename) |
| 808 | for line in f: |
| 809 | line = line.strip() |
| 810 | if not line or line.startswith("#"): |
| 811 | continue |
| 812 | k, v = line.split("=", 1) |
| 813 | d[k] = v |
| 814 | f.close() |
| 815 | return d |
| 816 | |
| 817 | |
| 818 | def main(argv): |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 819 | if len(argv) != 4: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 820 | print(__doc__) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 821 | sys.exit(1) |
| 822 | |
| 823 | in_dir = argv[0] |
| 824 | glob_dict_file = argv[1] |
| 825 | out_file = argv[2] |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 826 | target_out = argv[3] |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 827 | |
| 828 | glob_dict = LoadGlobalDict(glob_dict_file) |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 829 | if "mount_point" in glob_dict: |
Tao Bao | c7a6f1e | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 830 | # The caller knows the mount point and provides a dictionay needed by |
| 831 | # BuildImage(). |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 832 | image_properties = glob_dict |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 833 | else: |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 834 | image_filename = os.path.basename(out_file) |
| 835 | mount_point = "" |
| 836 | if image_filename == "system.img": |
| 837 | mount_point = "system" |
Alex Light | 4e358ab | 2016-06-16 14:47:10 -0700 | [diff] [blame] | 838 | elif image_filename == "system_other.img": |
| 839 | mount_point = "system_other" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 840 | elif image_filename == "userdata.img": |
| 841 | mount_point = "data" |
| 842 | elif image_filename == "cache.img": |
| 843 | mount_point = "cache" |
| 844 | elif image_filename == "vendor.img": |
| 845 | mount_point = "vendor" |
| 846 | elif image_filename == "oem.img": |
| 847 | mount_point = "oem" |
Jaekyun Seok | b7735d8 | 2017-11-27 17:04:47 +0900 | [diff] [blame] | 848 | elif image_filename == "product.img": |
| 849 | mount_point = "product" |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 850 | else: |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 851 | print("error: unknown image file name ", image_filename, file=sys.stderr) |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 852 | sys.exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 853 | |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 854 | image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) |
| 855 | |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 856 | if not BuildImage(in_dir, image_properties, out_file, target_out): |
Tao Bao | c72727a | 2017-12-07 10:33:00 -0800 | [diff] [blame] | 857 | print("error: failed to build %s from %s" % (out_file, in_dir), |
| 858 | file=sys.stderr) |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 859 | sys.exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 860 | |
| 861 | |
| 862 | if __name__ == '__main__': |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 863 | try: |
| 864 | main(sys.argv[1:]) |
| 865 | finally: |
| 866 | common.Cleanup() |