blob: bf5d9dd0bfac2bdbe3e87345cf5932f124d703f9 [file] [log] [blame]
Ying Wangbd93d422011-10-28 17:02:30 -07001#!/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"""
18Build image output_image_file from input_directory and properties_file.
19
20Usage: build_image input_directory properties_file output_image_file
21
22"""
23import os
Ying Wang69e9b4d2012-11-26 18:10:23 -080024import os.path
Tao Bao4e663432015-06-23 11:16:05 -070025import re
Ying Wangbd93d422011-10-28 17:02:30 -070026import subprocess
27import sys
Geremy Condrafd6f7512013-06-16 17:26:08 -070028import commands
29import shutil
Geremy Condra5b5f4952014-05-05 22:19:37 -070030import tempfile
Ying Wangbd93d422011-10-28 17:02:30 -070031
Geremy Condrae8e982a2014-05-16 19:14:30 -070032FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"
33
Ying Wang69e9b4d2012-11-26 18:10:23 -080034def RunCommand(cmd):
Tao Bao4e663432015-06-23 11:16:05 -070035 """Echo and run the given command.
Ying Wang69e9b4d2012-11-26 18:10:23 -080036
37 Args:
38 cmd: the command represented as a list of strings.
39 Returns:
Tao Bao4e663432015-06-23 11:16:05 -070040 A tuple of the output and the exit code.
Ying Wang69e9b4d2012-11-26 18:10:23 -080041 """
42 print "Running: ", " ".join(cmd)
Tao Bao4e663432015-06-23 11:16:05 -070043 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
44 output, _ = p.communicate()
45 print "%s" % (output.rstrip(),)
46 return (output, p.returncode)
Ying Wangbd93d422011-10-28 17:02:30 -070047
Geremy Condrafd6f7512013-06-16 17:26:08 -070048def GetVerityTreeSize(partition_size):
Colin Cross477cf2b2014-04-16 18:49:56 -070049 cmd = "build_verity_tree -s %d"
Geremy Condrafd6f7512013-06-16 17:26:08 -070050 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
57def 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
66def 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:
Dan Albert8b72aef2015-03-23 19:13:21 -070077 return 0
Geremy Condrafd6f7512013-06-16 17:26:08 -070078 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 Cross477cf2b2014-04-16 18:49:56 -070083def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict):
Dan Albert8b72aef2015-03-23 19:13:21 -070084 cmd = "build_verity_tree -A %s %s %s" % (
85 FIXED_SALT, sparse_image_path, verity_image_path)
Geremy Condrafd6f7512013-06-16 17:26:08 -070086 print cmd
87 status, output = commands.getstatusoutput(cmd)
88 if status:
89 print "Could not build verity tree! Error: %s" % output
90 return False
91 root, salt = output.split()
92 prop_dict["verity_root_hash"] = root
93 prop_dict["verity_salt"] = salt
94 return True
95
96def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
97 block_device, signer_path, key):
Dan Albert8b72aef2015-03-23 19:13:21 -070098 cmd_template = (
99 "system/extras/verity/build_verity_metadata.py %s %s %s %s %s %s %s")
100 cmd = cmd_template % (image_size, verity_metadata_path, root_hash, salt,
101 block_device, signer_path, key)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700102 print cmd
103 status, output = commands.getstatusoutput(cmd)
104 if status:
105 print "Could not build verity metadata! Error: %s" % output
106 return False
107 return True
108
109def Append2Simg(sparse_image_path, unsparse_image_path, error_message):
110 """Appends the unsparse image to the given sparse image.
111
112 Args:
113 sparse_image_path: the path to the (sparse) image
114 unsparse_image_path: the path to the (unsparse) image
115 Returns:
116 True on success, False on failure.
117 """
118 cmd = "append2simg %s %s"
119 cmd %= (sparse_image_path, unsparse_image_path)
120 print cmd
121 status, output = commands.getstatusoutput(cmd)
122 if status:
123 print "%s: %s" % (error_message, output)
124 return False
125 return True
126
Dan Albert8b72aef2015-03-23 19:13:21 -0700127def BuildVerifiedImage(data_image_path, verity_image_path,
128 verity_metadata_path):
129 if not Append2Simg(data_image_path, verity_metadata_path,
130 "Could not append verity metadata!"):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700131 return False
Dan Albert8b72aef2015-03-23 19:13:21 -0700132 if not Append2Simg(data_image_path, verity_image_path,
133 "Could not append verity tree!"):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700134 return False
135 return True
136
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800137def UnsparseImage(sparse_image_path, replace=True):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700138 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 Condra6e8f53c2013-12-05 17:09:18 -0800142 if replace:
143 os.unlink(unsparse_image_path)
144 else:
145 return True, unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700146 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
Tao Bao4e663432015-06-23 11:16:05 -0700147 (_, exit_code) = RunCommand(inflate_command)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700148 if exit_code != 0:
149 os.remove(unsparse_image_path)
150 return False, None
151 return True, unsparse_image_path
152
153def MakeVerityEnabledImage(out_file, prop_dict):
154 """Creates an image that is verifiable using dm-verity.
155
156 Args:
157 out_file: the location to write the verifiable image at
Dan Albert8b72aef2015-03-23 19:13:21 -0700158 prop_dict: a dictionary of properties required for image creation and
159 verification
Geremy Condrafd6f7512013-06-16 17:26:08 -0700160 Returns:
161 True on success, False otherwise.
162 """
163 # get properties
164 image_size = prop_dict["partition_size"]
Geremy Condrafd6f7512013-06-16 17:26:08 -0700165 block_dev = prop_dict["verity_block_device"]
Paul Lawrencea37b2bb2014-11-13 17:54:30 -0800166 signer_key = prop_dict["verity_key"] + ".pk8"
Geremy Condrafd6f7512013-06-16 17:26:08 -0700167 signer_path = prop_dict["verity_signer_cmd"]
168
169 # make a tempdir
Geremy Condra5b5f4952014-05-05 22:19:37 -0700170 tempdir_name = tempfile.mkdtemp(suffix="_verity_images")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700171
172 # get partial image paths
173 verity_image_path = os.path.join(tempdir_name, "verity.img")
174 verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700175
176 # build the verity tree and get the root hash and salt
Colin Cross477cf2b2014-04-16 18:49:56 -0700177 if not BuildVerityTree(out_file, verity_image_path, prop_dict):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700178 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700179 return False
180
181 # build the metadata blocks
182 root_hash = prop_dict["verity_root_hash"]
183 salt = prop_dict["verity_salt"]
Dan Albert8b72aef2015-03-23 19:13:21 -0700184 if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
185 block_dev, signer_path, signer_key):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700186 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700187 return False
188
189 # build the full verified image
190 if not BuildVerifiedImage(out_file,
191 verity_image_path,
192 verity_metadata_path):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700193 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700194 return False
195
Geremy Condra5b5f4952014-05-05 22:19:37 -0700196 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700197 return True
198
Ying Wanga2292c92015-03-24 19:07:40 -0700199def BuildImage(in_dir, prop_dict, out_file):
Ying Wangbd93d422011-10-28 17:02:30 -0700200 """Build an image to out_file from in_dir with property prop_dict.
201
202 Args:
203 in_dir: path of input directory.
204 prop_dict: property dictionary.
205 out_file: path of the output image file.
206
207 Returns:
208 True iff the image is built successfully.
209 """
Tao Bao2ed665a2015-04-01 11:21:55 -0700210 # system_root_image=true: build a system.img that combines the contents of
211 # /system and the ramdisk, and can be mounted at the root of the file system.
Ying Wanga2292c92015-03-24 19:07:40 -0700212 origin_in = in_dir
213 fs_config = prop_dict.get("fs_config")
214 if (prop_dict.get("system_root_image") == "true"
215 and prop_dict["mount_point"] == "system"):
216 in_dir = tempfile.mkdtemp()
217 # Change the mount point to "/"
218 prop_dict["mount_point"] = "/"
219 if fs_config:
220 # We need to merge the fs_config files of system and ramdisk.
221 fd, merged_fs_config = tempfile.mkstemp(prefix="root_fs_config",
222 suffix=".txt")
223 os.close(fd)
224 with open(merged_fs_config, "w") as fw:
225 if "ramdisk_fs_config" in prop_dict:
226 with open(prop_dict["ramdisk_fs_config"]) as fr:
227 fw.writelines(fr.readlines())
228 with open(fs_config) as fr:
229 fw.writelines(fr.readlines())
230 fs_config = merged_fs_config
231
Ying Wangbd93d422011-10-28 17:02:30 -0700232 build_command = []
233 fs_type = prop_dict.get("fs_type", "")
Ying Wang69e9b4d2012-11-26 18:10:23 -0800234 run_fsck = False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700235
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700236 fs_spans_partition = True
237 if fs_type.startswith("squash"):
Tao Bao4e663432015-06-23 11:16:05 -0700238 fs_spans_partition = False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700239
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700240 is_verity_partition = "verity_block_device" in prop_dict
Geremy Condra5b5f4952014-05-05 22:19:37 -0700241 verity_supported = prop_dict.get("verity") == "true"
Tao Bao4e663432015-06-23 11:16:05 -0700242 # Adjust the partition size to make room for the hashes if this is to be
243 # verified.
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700244 if verity_supported and is_verity_partition and fs_spans_partition:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700245 partition_size = int(prop_dict.get("partition_size"))
246 adjusted_size = AdjustPartitionSizeForVerity(partition_size)
247 if not adjusted_size:
248 return False
249 prop_dict["partition_size"] = str(adjusted_size)
250 prop_dict["original_partition_size"] = str(partition_size)
251
Ying Wangbd93d422011-10-28 17:02:30 -0700252 if fs_type.startswith("ext"):
253 build_command = ["mkuserimg.sh"]
254 if "extfs_sparse_flag" in prop_dict:
255 build_command.append(prop_dict["extfs_sparse_flag"])
Ying Wang69e9b4d2012-11-26 18:10:23 -0800256 run_fsck = True
Ying Wangbd93d422011-10-28 17:02:30 -0700257 build_command.extend([in_dir, out_file, fs_type,
258 prop_dict["mount_point"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800259 build_command.append(prop_dict["partition_size"])
Ying Wangf3b86352014-11-18 18:03:13 -0800260 if "journal_size" in prop_dict:
261 build_command.extend(["-j", prop_dict["journal_size"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800262 if "timestamp" in prop_dict:
263 build_command.extend(["-T", str(prop_dict["timestamp"])])
Ying Wanga2292c92015-03-24 19:07:40 -0700264 if fs_config:
Doug Zongker82822822014-06-16 09:10:55 -0700265 build_command.extend(["-C", fs_config])
Ying Wanga2292c92015-03-24 19:07:40 -0700266 if "block_list" in prop_dict:
267 build_command.extend(["-B", prop_dict["block_list"]])
Christoffer Dall8ed01f32014-12-17 21:34:12 +0100268 build_command.extend(["-L", prop_dict["mount_point"]])
Ying Wanga2292c92015-03-24 19:07:40 -0700269 if "selinux_fc" in prop_dict:
Kenny Rootf32dc712012-04-08 10:42:34 -0700270 build_command.append(prop_dict["selinux_fc"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800271 elif fs_type.startswith("squash"):
272 build_command = ["mksquashfsimage.sh"]
273 build_command.extend([in_dir, out_file])
Mohamad Ayyash2cd51cc2015-06-24 10:44:29 -0700274 build_command.extend(["-s"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800275 build_command.extend(["-m", prop_dict["mount_point"]])
Ying Wanga2292c92015-03-24 19:07:40 -0700276 if "selinux_fc" in prop_dict:
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800277 build_command.extend(["-c", prop_dict["selinux_fc"]])
Simon Wilson011ea062015-06-17 12:35:15 -0700278 if "squashfs_compressor" in prop_dict:
279 build_command.extend(["-z", prop_dict["squashfs_compressor"]])
280 if "squashfs_compressor_opt" in prop_dict:
281 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700282 elif fs_type.startswith("f2fs"):
283 build_command = ["mkf2fsuserimg.sh"]
284 build_command.extend([out_file, prop_dict["partition_size"]])
Ying Wangbd93d422011-10-28 17:02:30 -0700285 else:
286 build_command = ["mkyaffs2image", "-f"]
287 if prop_dict.get("mkyaffs2_extra_flags", None):
288 build_command.extend(prop_dict["mkyaffs2_extra_flags"].split())
289 build_command.append(in_dir)
290 build_command.append(out_file)
Kenny Rootf32dc712012-04-08 10:42:34 -0700291 if "selinux_fc" in prop_dict:
292 build_command.append(prop_dict["selinux_fc"])
293 build_command.append(prop_dict["mount_point"])
Ying Wangbd93d422011-10-28 17:02:30 -0700294
Ying Wanga2292c92015-03-24 19:07:40 -0700295 if in_dir != origin_in:
296 # Construct a staging directory of the root file system.
297 ramdisk_dir = prop_dict.get("ramdisk_dir")
298 if ramdisk_dir:
299 shutil.rmtree(in_dir)
300 shutil.copytree(ramdisk_dir, in_dir, symlinks=True)
301 staging_system = os.path.join(in_dir, "system")
302 shutil.rmtree(staging_system, ignore_errors=True)
303 shutil.copytree(origin_in, staging_system, symlinks=True)
Tao Bao4e663432015-06-23 11:16:05 -0700304
305 reserved_blocks = prop_dict.get("has_ext4_reserved_blocks") == "true"
306 ext4fs_output = None
307
Ying Wanga2292c92015-03-24 19:07:40 -0700308 try:
Tao Bao4e663432015-06-23 11:16:05 -0700309 if reserved_blocks and fs_type.startswith("ext4"):
310 (ext4fs_output, exit_code) = RunCommand(build_command)
311 else:
312 (_, exit_code) = RunCommand(build_command)
Ying Wanga2292c92015-03-24 19:07:40 -0700313 finally:
314 if in_dir != origin_in:
315 # Clean up temporary directories and files.
316 shutil.rmtree(in_dir, ignore_errors=True)
317 if fs_config:
318 os.remove(fs_config)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800319 if exit_code != 0:
320 return False
321
Tao Bao4e663432015-06-23 11:16:05 -0700322 # Bug: 21522719, 22023465
323 # There are some reserved blocks on ext4 FS (lesser of 4096 blocks and 2%).
324 # We need to deduct those blocks from the available space, since they are
325 # not writable even with root privilege. It only affects devices using
326 # file-based OTA and a kernel version of 3.10 or greater (currently just
327 # sprout).
328 if reserved_blocks and fs_type.startswith("ext4"):
329 assert ext4fs_output is not None
330 ext4fs_stats = re.compile(
331 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
332 r'(?P<total_blocks>[0-9]+) blocks')
333 m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
334 used_blocks = int(m.groupdict().get('used_blocks'))
335 total_blocks = int(m.groupdict().get('total_blocks'))
336 reserved_blocks = min(4096, int(total_blocks * 0.02))
337 adjusted_blocks = total_blocks - reserved_blocks
338 if used_blocks > adjusted_blocks:
339 mount_point = prop_dict.get("mount_point")
340 print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
341 "reserved: %d blocks, available: %d blocks)" % (
342 mount_point, total_blocks, used_blocks, reserved_blocks,
343 adjusted_blocks))
344 return False
345
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700346 if not fs_spans_partition:
347 mount_point = prop_dict.get("mount_point")
348 partition_size = int(prop_dict.get("partition_size"))
349 image_size = os.stat(out_file).st_size
350 if image_size > partition_size:
Tao Bao4e663432015-06-23 11:16:05 -0700351 print("Error: %s image size of %d is larger than partition size of "
352 "%d" % (mount_point, image_size, partition_size))
353 return False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700354 if verity_supported and is_verity_partition:
Tao Bao4e663432015-06-23 11:16:05 -0700355 if 2 * image_size - AdjustPartitionSizeForVerity(image_size) > partition_size:
356 print "Error: No more room on %s to fit verity data" % mount_point
357 return False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700358 prop_dict["original_partition_size"] = prop_dict["partition_size"]
359 prop_dict["partition_size"] = str(image_size)
360
Geremy Condrafd6f7512013-06-16 17:26:08 -0700361 # create the verified image if this is to be verified
Geremy Condra5b5f4952014-05-05 22:19:37 -0700362 if verity_supported and is_verity_partition:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700363 if not MakeVerityEnabledImage(out_file, prop_dict):
364 return False
365
Ying Wang6a42a252013-02-27 13:54:02 -0800366 if run_fsck and prop_dict.get("skip_fsck") != "true":
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800367 success, unsparse_image = UnsparseImage(out_file, replace=False)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700368 if not success:
Ying Wang69e9b4d2012-11-26 18:10:23 -0800369 return False
370
371 # Run e2fsck on the inflated image file
372 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
Tao Bao4e663432015-06-23 11:16:05 -0700373 (_, exit_code) = RunCommand(e2fsck_command)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800374
375 os.remove(unsparse_image)
376
377 return exit_code == 0
Ying Wangbd93d422011-10-28 17:02:30 -0700378
379
380def ImagePropFromGlobalDict(glob_dict, mount_point):
381 """Build an image property dictionary from the global dictionary.
382
383 Args:
384 glob_dict: the global dictionary from the build system.
385 mount_point: such as "system", "data" etc.
386 """
Doug Zongker1ad7ade2013-12-06 11:53:27 -0800387 d = {}
388 if "build.prop" in glob_dict:
389 bp = glob_dict["build.prop"]
390 if "ro.build.date.utc" in bp:
391 d["timestamp"] = bp["ro.build.date.utc"]
Ying Wang9f8e8db2011-11-04 11:37:01 -0700392
393 def copy_prop(src_p, dest_p):
394 if src_p in glob_dict:
395 d[dest_p] = str(glob_dict[src_p])
396
Ying Wangbd93d422011-10-28 17:02:30 -0700397 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -0700398 "extfs_sparse_flag",
399 "mkyaffs2_extra_flags",
Kenny Rootf32dc712012-04-08 10:42:34 -0700400 "selinux_fc",
Ying Wang6a42a252013-02-27 13:54:02 -0800401 "skip_fsck",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700402 "verity",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700403 "verity_key",
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700404 "verity_signer_cmd"
Ying Wangbd93d422011-10-28 17:02:30 -0700405 )
406 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -0700407 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -0700408
409 d["mount_point"] = mount_point
410 if mount_point == "system":
Ying Wang9f8e8db2011-11-04 11:37:01 -0700411 copy_prop("fs_type", "fs_type")
Dan Albert8b72aef2015-03-23 19:13:21 -0700412 # Copy the generic sysetem fs type first, override with specific one if
413 # available.
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800414 copy_prop("system_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700415 copy_prop("system_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800416 copy_prop("system_journal_size", "journal_size")
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700417 copy_prop("system_verity_block_device", "verity_block_device")
Tao Bao2ed665a2015-04-01 11:21:55 -0700418 copy_prop("system_root_image", "system_root_image")
419 copy_prop("ramdisk_dir", "ramdisk_dir")
Tao Bao84e75682015-07-19 02:38:53 -0700420 copy_prop("ramdisk_fs_config", "ramdisk_fs_config")
Tao Bao4e663432015-06-23 11:16:05 -0700421 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Simon Wilson011ea062015-06-17 12:35:15 -0700422 copy_prop("system_squashfs_compressor", "squashfs_compressor")
423 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
Ying Wangbd93d422011-10-28 17:02:30 -0700424 elif mount_point == "data":
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700425 # Copy the generic fs type first, override with specific one if available.
Ying Wang9f8e8db2011-11-04 11:37:01 -0700426 copy_prop("fs_type", "fs_type")
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700427 copy_prop("userdata_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700428 copy_prop("userdata_size", "partition_size")
429 elif mount_point == "cache":
430 copy_prop("cache_fs_type", "fs_type")
431 copy_prop("cache_size", "partition_size")
Ying Wanga0febe52013-03-20 11:02:05 -0700432 elif mount_point == "vendor":
433 copy_prop("vendor_fs_type", "fs_type")
434 copy_prop("vendor_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800435 copy_prop("vendor_journal_size", "journal_size")
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700436 copy_prop("vendor_verity_block_device", "verity_block_device")
Tao Bao4e663432015-06-23 11:16:05 -0700437 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Ying Wangb8888432014-03-11 17:13:27 -0700438 elif mount_point == "oem":
439 copy_prop("fs_type", "fs_type")
440 copy_prop("oem_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800441 copy_prop("oem_journal_size", "journal_size")
Tao Bao4e663432015-06-23 11:16:05 -0700442 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Ying Wangbd93d422011-10-28 17:02:30 -0700443
444 return d
445
446
447def LoadGlobalDict(filename):
448 """Load "name=value" pairs from filename"""
449 d = {}
450 f = open(filename)
451 for line in f:
452 line = line.strip()
453 if not line or line.startswith("#"):
454 continue
455 k, v = line.split("=", 1)
456 d[k] = v
457 f.close()
458 return d
459
460
461def main(argv):
462 if len(argv) != 3:
463 print __doc__
464 sys.exit(1)
465
466 in_dir = argv[0]
467 glob_dict_file = argv[1]
468 out_file = argv[2]
469
470 glob_dict = LoadGlobalDict(glob_dict_file)
Ying Wang4540a852015-03-12 18:30:39 -0700471 if "mount_point" in glob_dict:
Tao Bao4e663432015-06-23 11:16:05 -0700472 # The caller knows the mount point and provides a dictionay needed by
473 # BuildImage().
Ying Wang4540a852015-03-12 18:30:39 -0700474 image_properties = glob_dict
Ying Wang9f8e8db2011-11-04 11:37:01 -0700475 else:
Ying Wang4540a852015-03-12 18:30:39 -0700476 image_filename = os.path.basename(out_file)
477 mount_point = ""
478 if image_filename == "system.img":
479 mount_point = "system"
480 elif image_filename == "userdata.img":
481 mount_point = "data"
482 elif image_filename == "cache.img":
483 mount_point = "cache"
484 elif image_filename == "vendor.img":
485 mount_point = "vendor"
486 elif image_filename == "oem.img":
487 mount_point = "oem"
488 else:
489 print >> sys.stderr, "error: unknown image file name ", image_filename
490 exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -0700491
Ying Wang4540a852015-03-12 18:30:39 -0700492 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
493
Ying Wangbd93d422011-10-28 17:02:30 -0700494 if not BuildImage(in_dir, image_properties, out_file):
Dan Albert8b72aef2015-03-23 19:13:21 -0700495 print >> sys.stderr, "error: failed to build %s from %s" % (out_file,
496 in_dir)
Ying Wangbd93d422011-10-28 17:02:30 -0700497 exit(1)
498
499
500if __name__ == '__main__':
501 main(sys.argv[1:])