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