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