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