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