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 |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 25 | import subprocess |
| 26 | import sys |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 27 | import commands |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 28 | import common |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 29 | import shutil |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 30 | import tempfile |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 31 | |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 32 | OPTIONS = common.OPTIONS |
| 33 | |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 34 | FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" |
| 35 | |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 36 | def RunCommand(cmd): |
| 37 | """ Echo and run the given command |
| 38 | |
| 39 | Args: |
| 40 | cmd: the command represented as a list of strings. |
| 41 | Returns: |
| 42 | The exit code. |
| 43 | """ |
| 44 | print "Running: ", " ".join(cmd) |
| 45 | p = subprocess.Popen(cmd) |
| 46 | p.communicate() |
| 47 | return p.returncode |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 48 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 49 | def GetVerityTreeSize(partition_size): |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 50 | cmd = "build_verity_tree -s %d" |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 51 | cmd %= partition_size |
| 52 | status, output = commands.getstatusoutput(cmd) |
| 53 | if status: |
| 54 | print output |
| 55 | return False, 0 |
| 56 | return True, int(output) |
| 57 | |
| 58 | def GetVerityMetadataSize(partition_size): |
| 59 | cmd = "system/extras/verity/build_verity_metadata.py -s %d" |
| 60 | cmd %= partition_size |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 61 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 62 | status, output = commands.getstatusoutput(cmd) |
| 63 | if status: |
| 64 | print output |
| 65 | return False, 0 |
| 66 | return True, int(output) |
| 67 | |
| 68 | def AdjustPartitionSizeForVerity(partition_size): |
| 69 | """Modifies the provided partition size to account for the verity metadata. |
| 70 | |
| 71 | This information is used to size the created image appropriately. |
| 72 | Args: |
| 73 | partition_size: the size of the partition to be verified. |
| 74 | Returns: |
| 75 | The size of the partition adjusted for verity metadata. |
| 76 | """ |
| 77 | success, verity_tree_size = GetVerityTreeSize(partition_size) |
| 78 | if not success: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 79 | return 0 |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 80 | success, verity_metadata_size = GetVerityMetadataSize(partition_size) |
| 81 | if not success: |
| 82 | return 0 |
| 83 | return partition_size - verity_tree_size - verity_metadata_size |
| 84 | |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 85 | def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 86 | cmd = "build_verity_tree -A %s %s %s" % ( |
| 87 | FIXED_SALT, sparse_image_path, verity_image_path) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 88 | print cmd |
| 89 | status, output = commands.getstatusoutput(cmd) |
| 90 | if status: |
| 91 | print "Could not build verity tree! Error: %s" % output |
| 92 | return False |
| 93 | root, salt = output.split() |
| 94 | prop_dict["verity_root_hash"] = root |
| 95 | prop_dict["verity_salt"] = salt |
| 96 | return True |
| 97 | |
| 98 | def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
| 99 | block_device, signer_path, key): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 100 | cmd_template = ( |
| 101 | "system/extras/verity/build_verity_metadata.py %s %s %s %s %s %s %s") |
| 102 | cmd = cmd_template % (image_size, verity_metadata_path, root_hash, salt, |
| 103 | block_device, signer_path, key) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 104 | print cmd |
| 105 | status, output = commands.getstatusoutput(cmd) |
| 106 | if status: |
| 107 | print "Could not build verity metadata! Error: %s" % output |
| 108 | return False |
| 109 | return True |
| 110 | |
| 111 | def Append2Simg(sparse_image_path, unsparse_image_path, error_message): |
| 112 | """Appends the unsparse image to the given sparse image. |
| 113 | |
| 114 | Args: |
| 115 | sparse_image_path: the path to the (sparse) image |
| 116 | unsparse_image_path: the path to the (unsparse) image |
| 117 | Returns: |
| 118 | True on success, False on failure. |
| 119 | """ |
| 120 | cmd = "append2simg %s %s" |
| 121 | cmd %= (sparse_image_path, unsparse_image_path) |
| 122 | print cmd |
| 123 | status, output = commands.getstatusoutput(cmd) |
| 124 | if status: |
| 125 | print "%s: %s" % (error_message, output) |
| 126 | return False |
| 127 | return True |
| 128 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 129 | def BuildVerifiedImage(data_image_path, verity_image_path, |
| 130 | verity_metadata_path): |
| 131 | if not Append2Simg(data_image_path, verity_metadata_path, |
| 132 | "Could not append verity metadata!"): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 133 | return False |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 134 | if not Append2Simg(data_image_path, verity_image_path, |
| 135 | "Could not append verity tree!"): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 136 | return False |
| 137 | return True |
| 138 | |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 139 | def UnsparseImage(sparse_image_path, replace=True): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 140 | img_dir = os.path.dirname(sparse_image_path) |
| 141 | unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path) |
| 142 | unsparse_image_path = os.path.join(img_dir, unsparse_image_path) |
| 143 | if os.path.exists(unsparse_image_path): |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 144 | if replace: |
| 145 | os.unlink(unsparse_image_path) |
| 146 | else: |
| 147 | return True, unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 148 | inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] |
| 149 | exit_code = RunCommand(inflate_command) |
| 150 | if exit_code != 0: |
| 151 | os.remove(unsparse_image_path) |
| 152 | return False, None |
| 153 | return True, unsparse_image_path |
| 154 | |
| 155 | def MakeVerityEnabledImage(out_file, prop_dict): |
| 156 | """Creates an image that is verifiable using dm-verity. |
| 157 | |
| 158 | Args: |
| 159 | out_file: the location to write the verifiable image at |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 160 | prop_dict: a dictionary of properties required for image creation and |
| 161 | verification |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 162 | Returns: |
| 163 | True on success, False otherwise. |
| 164 | """ |
| 165 | # get properties |
| 166 | image_size = prop_dict["partition_size"] |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 167 | block_dev = prop_dict["verity_block_device"] |
Paul Lawrence | a37b2bb | 2014-11-13 17:54:30 -0800 | [diff] [blame] | 168 | signer_key = prop_dict["verity_key"] + ".pk8" |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 169 | if OPTIONS.verity_signer_path is not None: |
| 170 | signer_path = OPTIONS.verity_signer_path + ' ' |
| 171 | signer_path += ' '.join(OPTIONS.verity_signer_args) |
| 172 | else: |
| 173 | signer_path = prop_dict["verity_signer_cmd"] |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 174 | |
| 175 | # make a tempdir |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 176 | tempdir_name = tempfile.mkdtemp(suffix="_verity_images") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 177 | |
| 178 | # get partial image paths |
| 179 | verity_image_path = os.path.join(tempdir_name, "verity.img") |
| 180 | verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 181 | |
| 182 | # build the verity tree and get the root hash and salt |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 183 | if not BuildVerityTree(out_file, verity_image_path, prop_dict): |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 184 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 185 | return False |
| 186 | |
| 187 | # build the metadata blocks |
| 188 | root_hash = prop_dict["verity_root_hash"] |
| 189 | salt = prop_dict["verity_salt"] |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 190 | if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
| 191 | block_dev, signer_path, signer_key): |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 192 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 193 | return False |
| 194 | |
| 195 | # build the full verified image |
| 196 | if not BuildVerifiedImage(out_file, |
| 197 | verity_image_path, |
| 198 | verity_metadata_path): |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 199 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 200 | return False |
| 201 | |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 202 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 203 | return True |
| 204 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 205 | def BuildImage(in_dir, prop_dict, out_file): |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 206 | """Build an image to out_file from in_dir with property prop_dict. |
| 207 | |
| 208 | Args: |
| 209 | in_dir: path of input directory. |
| 210 | prop_dict: property dictionary. |
| 211 | out_file: path of the output image file. |
| 212 | |
| 213 | Returns: |
| 214 | True iff the image is built successfully. |
| 215 | """ |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 216 | # system_root_image=true: build a system.img that combines the contents of |
| 217 | # /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] | 218 | origin_in = in_dir |
| 219 | fs_config = prop_dict.get("fs_config") |
| 220 | if (prop_dict.get("system_root_image") == "true" |
| 221 | and prop_dict["mount_point"] == "system"): |
| 222 | in_dir = tempfile.mkdtemp() |
| 223 | # Change the mount point to "/" |
| 224 | prop_dict["mount_point"] = "/" |
| 225 | if fs_config: |
| 226 | # We need to merge the fs_config files of system and ramdisk. |
| 227 | fd, merged_fs_config = tempfile.mkstemp(prefix="root_fs_config", |
| 228 | suffix=".txt") |
| 229 | os.close(fd) |
| 230 | with open(merged_fs_config, "w") as fw: |
| 231 | if "ramdisk_fs_config" in prop_dict: |
| 232 | with open(prop_dict["ramdisk_fs_config"]) as fr: |
| 233 | fw.writelines(fr.readlines()) |
| 234 | with open(fs_config) as fr: |
| 235 | fw.writelines(fr.readlines()) |
| 236 | fs_config = merged_fs_config |
| 237 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 238 | build_command = [] |
| 239 | fs_type = prop_dict.get("fs_type", "") |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 240 | run_fsck = False |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 241 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 242 | fs_spans_partition = True |
| 243 | if fs_type.startswith("squash"): |
| 244 | fs_spans_partition = False |
| 245 | |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 246 | is_verity_partition = "verity_block_device" in prop_dict |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 247 | verity_supported = prop_dict.get("verity") == "true" |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 248 | # adjust the partition size to make room for the hashes if this is to be verified |
| 249 | if verity_supported and is_verity_partition and fs_spans_partition: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 250 | partition_size = int(prop_dict.get("partition_size")) |
Baligh Uddin | 601ddea | 2015-06-09 15:48:14 -0700 | [diff] [blame] | 251 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 252 | adjusted_size = AdjustPartitionSizeForVerity(partition_size) |
| 253 | if not adjusted_size: |
| 254 | return False |
| 255 | prop_dict["partition_size"] = str(adjusted_size) |
| 256 | prop_dict["original_partition_size"] = str(partition_size) |
| 257 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 258 | if fs_type.startswith("ext"): |
| 259 | build_command = ["mkuserimg.sh"] |
| 260 | if "extfs_sparse_flag" in prop_dict: |
| 261 | build_command.append(prop_dict["extfs_sparse_flag"]) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 262 | run_fsck = True |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 263 | build_command.extend([in_dir, out_file, fs_type, |
| 264 | prop_dict["mount_point"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 265 | build_command.append(prop_dict["partition_size"]) |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 266 | if "journal_size" in prop_dict: |
| 267 | build_command.extend(["-j", prop_dict["journal_size"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 268 | if "timestamp" in prop_dict: |
| 269 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 270 | if fs_config: |
Doug Zongker | 8282282 | 2014-06-16 09:10:55 -0700 | [diff] [blame] | 271 | build_command.extend(["-C", fs_config]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 272 | if "block_list" in prop_dict: |
| 273 | build_command.extend(["-B", prop_dict["block_list"]]) |
Christoffer Dall | 8ed01f3 | 2014-12-17 21:34:12 +0100 | [diff] [blame] | 274 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 275 | if "selinux_fc" in prop_dict: |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 276 | build_command.append(prop_dict["selinux_fc"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 277 | elif fs_type.startswith("squash"): |
| 278 | build_command = ["mksquashfsimage.sh"] |
| 279 | build_command.extend([in_dir, out_file]) |
Mohamad Ayyash | fa6c8a9 | 2015-06-24 10:44:29 -0700 | [diff] [blame] | 280 | build_command.extend(["-s"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 281 | build_command.extend(["-m", prop_dict["mount_point"]]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 282 | if "selinux_fc" in prop_dict: |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 283 | build_command.extend(["-c", prop_dict["selinux_fc"]]) |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 284 | if "squashfs_compressor" in prop_dict: |
| 285 | build_command.extend(["-z", prop_dict["squashfs_compressor"]]) |
| 286 | if "squashfs_compressor_opt" in prop_dict: |
| 287 | build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 288 | elif fs_type.startswith("f2fs"): |
| 289 | build_command = ["mkf2fsuserimg.sh"] |
| 290 | build_command.extend([out_file, prop_dict["partition_size"]]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 291 | else: |
| 292 | build_command = ["mkyaffs2image", "-f"] |
| 293 | if prop_dict.get("mkyaffs2_extra_flags", None): |
| 294 | build_command.extend(prop_dict["mkyaffs2_extra_flags"].split()) |
| 295 | build_command.append(in_dir) |
| 296 | build_command.append(out_file) |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 297 | if "selinux_fc" in prop_dict: |
| 298 | build_command.append(prop_dict["selinux_fc"]) |
| 299 | build_command.append(prop_dict["mount_point"]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 300 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 301 | if in_dir != origin_in: |
| 302 | # Construct a staging directory of the root file system. |
| 303 | ramdisk_dir = prop_dict.get("ramdisk_dir") |
| 304 | if ramdisk_dir: |
| 305 | shutil.rmtree(in_dir) |
| 306 | shutil.copytree(ramdisk_dir, in_dir, symlinks=True) |
| 307 | staging_system = os.path.join(in_dir, "system") |
| 308 | shutil.rmtree(staging_system, ignore_errors=True) |
| 309 | shutil.copytree(origin_in, staging_system, symlinks=True) |
| 310 | try: |
| 311 | exit_code = RunCommand(build_command) |
| 312 | finally: |
| 313 | if in_dir != origin_in: |
| 314 | # Clean up temporary directories and files. |
| 315 | shutil.rmtree(in_dir, ignore_errors=True) |
| 316 | if fs_config: |
| 317 | os.remove(fs_config) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 318 | if exit_code != 0: |
| 319 | return False |
| 320 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 321 | if not fs_spans_partition: |
| 322 | mount_point = prop_dict.get("mount_point") |
| 323 | partition_size = int(prop_dict.get("partition_size")) |
| 324 | image_size = os.stat(out_file).st_size |
| 325 | if image_size > partition_size: |
| 326 | print "Error: %s image size of %d is larger than partition size of %d" % (mount_point, image_size, partition_size) |
| 327 | return False |
| 328 | if verity_supported and is_verity_partition: |
| 329 | if 2 * image_size - AdjustPartitionSizeForVerity(image_size) > partition_size: |
| 330 | print "Error: No more room on %s to fit verity data" % mount_point |
| 331 | return False |
| 332 | prop_dict["original_partition_size"] = prop_dict["partition_size"] |
| 333 | prop_dict["partition_size"] = str(image_size) |
| 334 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 335 | # create the verified image if this is to be verified |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 336 | if verity_supported and is_verity_partition: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 337 | if not MakeVerityEnabledImage(out_file, prop_dict): |
| 338 | return False |
| 339 | |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 340 | if run_fsck and prop_dict.get("skip_fsck") != "true": |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 341 | success, unsparse_image = UnsparseImage(out_file, replace=False) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 342 | if not success: |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 343 | return False |
| 344 | |
| 345 | # Run e2fsck on the inflated image file |
| 346 | e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] |
| 347 | exit_code = RunCommand(e2fsck_command) |
| 348 | |
| 349 | os.remove(unsparse_image) |
| 350 | |
| 351 | return exit_code == 0 |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 352 | |
| 353 | |
| 354 | def ImagePropFromGlobalDict(glob_dict, mount_point): |
| 355 | """Build an image property dictionary from the global dictionary. |
| 356 | |
| 357 | Args: |
| 358 | glob_dict: the global dictionary from the build system. |
| 359 | mount_point: such as "system", "data" etc. |
| 360 | """ |
Doug Zongker | 1ad7ade | 2013-12-06 11:53:27 -0800 | [diff] [blame] | 361 | d = {} |
| 362 | if "build.prop" in glob_dict: |
| 363 | bp = glob_dict["build.prop"] |
| 364 | if "ro.build.date.utc" in bp: |
| 365 | d["timestamp"] = bp["ro.build.date.utc"] |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 366 | |
| 367 | def copy_prop(src_p, dest_p): |
| 368 | if src_p in glob_dict: |
| 369 | d[dest_p] = str(glob_dict[src_p]) |
| 370 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 371 | common_props = ( |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 372 | "extfs_sparse_flag", |
| 373 | "mkyaffs2_extra_flags", |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 374 | "selinux_fc", |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 375 | "skip_fsck", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 376 | "verity", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 377 | "verity_key", |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 378 | "verity_signer_cmd" |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 379 | ) |
| 380 | for p in common_props: |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 381 | copy_prop(p, p) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 382 | |
| 383 | d["mount_point"] = mount_point |
| 384 | if mount_point == "system": |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 385 | copy_prop("fs_type", "fs_type") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 386 | # Copy the generic sysetem fs type first, override with specific one if |
| 387 | # available. |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 388 | copy_prop("system_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 389 | copy_prop("system_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 390 | copy_prop("system_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 391 | copy_prop("system_verity_block_device", "verity_block_device") |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 392 | copy_prop("system_root_image", "system_root_image") |
| 393 | copy_prop("ramdisk_dir", "ramdisk_dir") |
Simon Wilson | f86e7ee | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 394 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 395 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 396 | elif mount_point == "data": |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 397 | # Copy the generic fs type first, override with specific one if available. |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 398 | copy_prop("fs_type", "fs_type") |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 399 | copy_prop("userdata_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 400 | copy_prop("userdata_size", "partition_size") |
| 401 | elif mount_point == "cache": |
| 402 | copy_prop("cache_fs_type", "fs_type") |
| 403 | copy_prop("cache_size", "partition_size") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 404 | elif mount_point == "vendor": |
| 405 | copy_prop("vendor_fs_type", "fs_type") |
| 406 | copy_prop("vendor_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 407 | copy_prop("vendor_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 408 | copy_prop("vendor_verity_block_device", "verity_block_device") |
Ying Wang | b888843 | 2014-03-11 17:13:27 -0700 | [diff] [blame] | 409 | elif mount_point == "oem": |
| 410 | copy_prop("fs_type", "fs_type") |
| 411 | copy_prop("oem_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 412 | copy_prop("oem_journal_size", "journal_size") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 413 | |
| 414 | return d |
| 415 | |
| 416 | |
| 417 | def LoadGlobalDict(filename): |
| 418 | """Load "name=value" pairs from filename""" |
| 419 | d = {} |
| 420 | f = open(filename) |
| 421 | for line in f: |
| 422 | line = line.strip() |
| 423 | if not line or line.startswith("#"): |
| 424 | continue |
| 425 | k, v = line.split("=", 1) |
| 426 | d[k] = v |
| 427 | f.close() |
| 428 | return d |
| 429 | |
| 430 | |
| 431 | def main(argv): |
| 432 | if len(argv) != 3: |
| 433 | print __doc__ |
| 434 | sys.exit(1) |
| 435 | |
| 436 | in_dir = argv[0] |
| 437 | glob_dict_file = argv[1] |
| 438 | out_file = argv[2] |
| 439 | |
| 440 | glob_dict = LoadGlobalDict(glob_dict_file) |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 441 | if "mount_point" in glob_dict: |
| 442 | # The caller knows the mount point and provides a dictionay needed by BuildImage(). |
| 443 | image_properties = glob_dict |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 444 | else: |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 445 | image_filename = os.path.basename(out_file) |
| 446 | mount_point = "" |
| 447 | if image_filename == "system.img": |
| 448 | mount_point = "system" |
| 449 | elif image_filename == "userdata.img": |
| 450 | mount_point = "data" |
| 451 | elif image_filename == "cache.img": |
| 452 | mount_point = "cache" |
| 453 | elif image_filename == "vendor.img": |
| 454 | mount_point = "vendor" |
| 455 | elif image_filename == "oem.img": |
| 456 | mount_point = "oem" |
| 457 | else: |
| 458 | print >> sys.stderr, "error: unknown image file name ", image_filename |
| 459 | exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 460 | |
Ying Wang | ae61f50 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 461 | image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) |
| 462 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 463 | if not BuildImage(in_dir, image_properties, out_file): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 464 | print >> sys.stderr, "error: failed to build %s from %s" % (out_file, |
| 465 | in_dir) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 466 | exit(1) |
| 467 | |
| 468 | |
| 469 | if __name__ == '__main__': |
| 470 | main(sys.argv[1:]) |