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