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