blob: 7611a4d43f9c49d0d022ec79de54d224b88d7965 [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"""
Tao Baoc72727a2017-12-07 10:33:00 -080018Builds output_image from the given input_directory, properties_file,
19and writes the image to target_output_directory.
Ying Wangbd93d422011-10-28 17:02:30 -070020
Yifan Hongbbcba1e2018-06-18 16:32:35 -070021If argument generated_prop_file exists, write additional properties to the file.
22
Tao Baoc72727a2017-12-07 10:33:00 -080023Usage: build_image.py input_directory properties_file output_image \\
Yifan Hongbbcba1e2018-06-18 16:32:35 -070024 target_output_directory [generated_prop_file]
Ying Wangbd93d422011-10-28 17:02:30 -070025"""
Tao Baoc72727a2017-12-07 10:33:00 -080026
27from __future__ import print_function
28
Tao Bao32fcdab2018-10-12 10:30:39 -070029import logging
Ying Wangbd93d422011-10-28 17:02:30 -070030import os
Ying Wang69e9b4d2012-11-26 18:10:23 -080031import os.path
Tao Baoc7a6f1e2015-06-23 11:16:05 -070032import re
Geremy Condrafd6f7512013-06-16 17:26:08 -070033import shutil
Tao Baoc72727a2017-12-07 10:33:00 -080034import sys
35
36import common
Tao Bao71197512018-10-11 14:08:45 -070037import verity_utils
Ying Wangbd93d422011-10-28 17:02:30 -070038
Tao Bao32fcdab2018-10-12 10:30:39 -070039logger = logging.getLogger(__name__)
40
Baligh Uddin601ddea2015-06-09 15:48:14 -070041OPTIONS = common.OPTIONS
Tao Bao71197512018-10-11 14:08:45 -070042BLOCK_SIZE = common.BLOCK_SIZE
Yifan Hongbbcba1e2018-06-18 16:32:35 -070043BYTES_IN_MB = 1024 * 1024
Geremy Condrae8e982a2014-05-16 19:14:30 -070044
Tao Baoc72727a2017-12-07 10:33:00 -080045
Tao Baoc6bd70a2018-09-27 16:58:00 -070046class BuildImageError(Exception):
47 """An Exception raised during image building."""
48
49 def __init__(self, message):
50 Exception.__init__(self, message)
51
52
Yifan Hongbbcba1e2018-06-18 16:32:35 -070053def GetDiskUsage(path):
Tao Baoc6bd70a2018-09-27 16:58:00 -070054 """Returns the number of bytes that "path" occupies on host.
Yifan Hongbbcba1e2018-06-18 16:32:35 -070055
56 Args:
Mark Salyzyn780f5952018-10-19 13:44:36 -070057 path: The directory or file to calculate size on.
Tao Baoc6bd70a2018-09-27 16:58:00 -070058
Yifan Hongbbcba1e2018-06-18 16:32:35 -070059 Returns:
Mark Salyzyn780f5952018-10-19 13:44:36 -070060 The number of bytes based on a 1K block_size.
Yifan Hongbbcba1e2018-06-18 16:32:35 -070061 """
Mark Salyzyn780f5952018-10-19 13:44:36 -070062 cmd = ["du", "-k", "-s", path]
Tao Baof3fc62c2018-10-25 12:23:12 -070063 output = common.RunAndCheckOutput(cmd, verbose=False)
Mark Salyzyn780f5952018-10-19 13:44:36 -070064 return int(output.split()[0]) * 1024
65
66
67def GetInodeUsage(path):
68 """Returns the number of inodes that "path" occupies on host.
69
70 Args:
71 path: The directory or file to calculate inode number on.
72
73 Returns:
74 The number of inodes used.
Mark Salyzyn780f5952018-10-19 13:44:36 -070075 """
76 cmd = ["find", path, "-print"]
Tao Baof3fc62c2018-10-25 12:23:12 -070077 output = common.RunAndCheckOutput(cmd, verbose=False)
Mark Salyzyn780f5952018-10-19 13:44:36 -070078 # increase by > 4% as number of files and directories is not whole picture.
79 return output.count('\n') * 25 // 24
80
81
82def GetFilesystemCharacteristics(sparse_image_path):
83 """Returns various filesystem characteristics of "sparse_image_path".
84
85 Args:
86 sparse_image_path: The file to analyze.
87
88 Returns:
89 The characteristics dictionary.
Mark Salyzyn780f5952018-10-19 13:44:36 -070090 """
91 unsparse_image_path = UnsparseImage(sparse_image_path, replace=False)
92
93 cmd = ["tune2fs", "-l", unsparse_image_path]
94 try:
95 output = common.RunAndCheckOutput(cmd, verbose=False)
Tao Baof3fc62c2018-10-25 12:23:12 -070096 finally:
97 os.remove(unsparse_image_path)
98 fs_dict = {}
Mark Salyzyn780f5952018-10-19 13:44:36 -070099 for line in output.splitlines():
100 fields = line.split(":")
101 if len(fields) == 2:
102 fs_dict[fields[0].strip()] = fields[1].strip()
103 return fs_dict
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700104
105
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800106def UnsparseImage(sparse_image_path, replace=True):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700107 img_dir = os.path.dirname(sparse_image_path)
108 unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
109 unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
110 if os.path.exists(unsparse_image_path):
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800111 if replace:
112 os.unlink(unsparse_image_path)
113 else:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700114 return unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700115 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
Tao Bao986ee862018-10-04 15:46:16 -0700116 try:
117 common.RunAndCheckOutput(inflate_command)
118 except:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700119 os.remove(unsparse_image_path)
Tao Bao986ee862018-10-04 15:46:16 -0700120 raise
Tao Baoc6bd70a2018-09-27 16:58:00 -0700121 return unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700122
Tao Baoc72727a2017-12-07 10:33:00 -0800123
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800124def ConvertBlockMapToBaseFs(block_map_file):
Tao Bao1c830bf2017-12-25 10:43:47 -0800125 base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800126 convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file]
Tao Bao986ee862018-10-04 15:46:16 -0700127 common.RunAndCheckOutput(convert_command)
Tao Baoc6bd70a2018-09-27 16:58:00 -0700128 return base_fs_file
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800129
Tao Baod4349f22017-12-07 23:01:25 -0800130
Tao Baoc2606eb2018-07-20 14:44:46 -0700131def SetUpInDirAndFsConfig(origin_in, prop_dict):
132 """Returns the in_dir and fs_config that should be used for image building.
133
Tom Cherryd14b8952018-08-09 14:26:00 -0700134 When building system.img for all targets, it creates and returns a staged dir
135 that combines the contents of /system (i.e. in the given in_dir) and root.
Tao Baoc2606eb2018-07-20 14:44:46 -0700136
137 Args:
138 origin_in: Path to the input directory.
139 prop_dict: A property dict that contains info like partition size. Values
140 may be updated.
141
142 Returns:
143 A tuple of in_dir and fs_config that should be used to build the image.
144 """
145 fs_config = prop_dict.get("fs_config")
Tom Cherryd14b8952018-08-09 14:26:00 -0700146
147 if prop_dict["mount_point"] == "system_other":
148 prop_dict["mount_point"] = "system"
149 return origin_in, fs_config
150
151 if prop_dict["mount_point"] != "system":
Tao Baoc2606eb2018-07-20 14:44:46 -0700152 return origin_in, fs_config
153
Mark Salyzyn780f5952018-10-19 13:44:36 -0700154 if "first_pass" in prop_dict:
155 prop_dict["mount_point"] = "/"
156 return prop_dict["first_pass"]
157
Tao Baoc2606eb2018-07-20 14:44:46 -0700158 # Construct a staging directory of the root file system.
159 in_dir = common.MakeTempDir()
160 root_dir = prop_dict.get("root_dir")
161 if root_dir:
162 shutil.rmtree(in_dir)
163 shutil.copytree(root_dir, in_dir, symlinks=True)
164 in_dir_system = os.path.join(in_dir, "system")
165 shutil.rmtree(in_dir_system, ignore_errors=True)
166 shutil.copytree(origin_in, in_dir_system, symlinks=True)
167
168 # Change the mount point to "/".
169 prop_dict["mount_point"] = "/"
170 if fs_config:
171 # We need to merge the fs_config files of system and root.
172 merged_fs_config = common.MakeTempFile(
173 prefix="merged_fs_config", suffix=".txt")
174 with open(merged_fs_config, "w") as fw:
175 if "root_fs_config" in prop_dict:
176 with open(prop_dict["root_fs_config"]) as fr:
177 fw.writelines(fr.readlines())
178 with open(fs_config) as fr:
179 fw.writelines(fr.readlines())
180 fs_config = merged_fs_config
Mark Salyzyn780f5952018-10-19 13:44:36 -0700181 prop_dict["first_pass"] = (in_dir, fs_config)
Tao Baoc2606eb2018-07-20 14:44:46 -0700182 return in_dir, fs_config
183
184
Tao Baod4349f22017-12-07 23:01:25 -0800185def CheckHeadroom(ext4fs_output, prop_dict):
186 """Checks if there's enough headroom space available.
187
188 Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM),
189 which is useful for devices with low disk space that have system image
190 variation between builds. The 'partition_headroom' in prop_dict is the size
191 in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks.
192
193 Args:
194 ext4fs_output: The output string from mke2fs command.
195 prop_dict: The property dict.
196
Tao Baod8a953d2018-01-02 21:19:27 -0800197 Raises:
198 AssertionError: On invalid input.
Tao Baoc6bd70a2018-09-27 16:58:00 -0700199 BuildImageError: On check failure.
Tao Baod4349f22017-12-07 23:01:25 -0800200 """
Tao Baod8a953d2018-01-02 21:19:27 -0800201 assert ext4fs_output is not None
202 assert prop_dict.get('fs_type', '').startswith('ext4')
203 assert 'partition_headroom' in prop_dict
204 assert 'mount_point' in prop_dict
205
Tao Baod4349f22017-12-07 23:01:25 -0800206 ext4fs_stats = re.compile(
207 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
208 r'(?P<total_blocks>[0-9]+) blocks')
Tao Baoc72727a2017-12-07 10:33:00 -0800209 last_line = ext4fs_output.strip().split('\n')[-1]
210 m = ext4fs_stats.match(last_line)
Tao Baod4349f22017-12-07 23:01:25 -0800211 used_blocks = int(m.groupdict().get('used_blocks'))
212 total_blocks = int(m.groupdict().get('total_blocks'))
Mark Salyzyn780f5952018-10-19 13:44:36 -0700213 headroom_blocks = int(prop_dict['partition_headroom']) // BLOCK_SIZE
Tao Baod4349f22017-12-07 23:01:25 -0800214 adjusted_blocks = total_blocks - headroom_blocks
215 if used_blocks > adjusted_blocks:
Tao Baod8a953d2018-01-02 21:19:27 -0800216 mount_point = prop_dict["mount_point"]
Tao Baoc6bd70a2018-09-27 16:58:00 -0700217 raise BuildImageError(
218 "Error: Not enough room on {} (total: {} blocks, used: {} blocks, "
219 "headroom: {} blocks, available: {} blocks)".format(
220 mount_point, total_blocks, used_blocks, headroom_blocks,
221 adjusted_blocks))
Tao Baod4349f22017-12-07 23:01:25 -0800222
223
Mark Salyzyn533558e2018-11-08 19:26:50 +0000224def BuildImage(in_dir, prop_dict, out_file, target_out=None):
225 """Builds an image for the files under in_dir and writes it to out_file.
Tao Baoc2606eb2018-07-20 14:44:46 -0700226
Ying Wangbd93d422011-10-28 17:02:30 -0700227 Args:
Tao Baoc2606eb2018-07-20 14:44:46 -0700228 in_dir: Path to input directory.
229 prop_dict: A property dict that contains info like partition size. Values
230 will be updated with computed values.
231 out_file: The output image file.
232 target_out: Path to the TARGET_OUT directory as in Makefile. It actually
233 points to the /system directory under PRODUCT_OUT. fs_config (the one
234 under system/core/libcutils) reads device specific FS config files from
235 there.
Ying Wangbd93d422011-10-28 17:02:30 -0700236
Tao Baoc6bd70a2018-09-27 16:58:00 -0700237 Raises:
238 BuildImageError: On build image failures.
Ying Wangbd93d422011-10-28 17:02:30 -0700239 """
Mark Salyzyn533558e2018-11-08 19:26:50 +0000240 original_mount_point = prop_dict["mount_point"]
241 in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict)
242
Ying Wangbd93d422011-10-28 17:02:30 -0700243 build_command = []
244 fs_type = prop_dict.get("fs_type", "")
Tao Baoc72727a2017-12-07 10:33:00 -0800245 run_e2fsck = False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700246
Mark Salyzyn533558e2018-11-08 19:26:50 +0000247 fs_spans_partition = True
248 if fs_type.startswith("squash"):
249 fs_spans_partition = False
250
251 # Get a builder for creating an image that's to be verified by Verified Boot,
252 # or None if not applicable.
253 verity_image_builder = verity_utils.CreateVerityImageBuilder(prop_dict)
254
255 if (prop_dict.get("use_dynamic_partition_size") == "true" and
256 "partition_size" not in prop_dict):
257 # If partition_size is not defined, use output of `du' + reserved_size.
258 size = GetDiskUsage(in_dir)
259 logger.info(
260 "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB)
261 # If not specified, give us 16MB margin for GetDiskUsage error ...
262 size += int(prop_dict.get("partition_reserved_size", BYTES_IN_MB * 16))
263 # Round this up to a multiple of 4K so that avbtool works
264 size = common.RoundUpTo4K(size)
265 if fs_type.startswith("ext"):
266 if verity_image_builder:
267 size = verity_image_builder.CalculateDynamicPartitionSize(size)
268 prop_dict["partition_size"] = str(size)
269 if "extfs_inode_count" not in prop_dict:
270 prop_dict["extfs_inode_count"] = str(GetInodeUsage(in_dir))
271 logger.info(
272 "First Pass based on estimates of %d MB and %s inodes.",
273 size // BYTES_IN_MB, prop_dict["extfs_inode_count"])
274 prop_dict["mount_point"] = original_mount_point
275 BuildImage(in_dir, prop_dict, out_file, target_out)
276 fs_dict = GetFilesystemCharacteristics(out_file)
277 os.remove(out_file)
278 block_size = int(fs_dict.get("Block size", "4096"))
279 free_size = int(fs_dict.get("Free blocks", "0")) * block_size
280 reserved_size = int(prop_dict.get("partition_reserved_size", 0))
281 if free_size <= reserved_size:
282 logger.info(
283 "Not worth reducing image %d <= %d.", free_size, reserved_size)
284 else:
285 size -= free_size
286 size += reserved_size
287 if block_size <= 4096:
288 size = common.RoundUpTo4K(size)
289 else:
290 size = ((size + block_size - 1) // block_size) * block_size
291 extfs_inode_count = prop_dict["extfs_inode_count"]
292 inodes = int(fs_dict.get("Inode count", extfs_inode_count))
293 inodes -= int(fs_dict.get("Free inodes", "0"))
294 prop_dict["extfs_inode_count"] = str(inodes)
295 prop_dict["partition_size"] = str(size)
296 logger.info(
297 "Allocating %d Inodes for %s.", inodes, out_file)
298 if verity_image_builder:
299 size = verity_image_builder.CalculateDynamicPartitionSize(size)
300 prop_dict["partition_size"] = str(size)
301 logger.info(
302 "Allocating %d MB for %s.", size // BYTES_IN_MB, out_file)
303
304 prop_dict["image_size"] = prop_dict["partition_size"]
305
306 # Adjust the image size to make room for the hashes if this is to be verified.
307 if verity_image_builder:
308 max_image_size = verity_image_builder.CalculateMaxImageSize()
309 prop_dict["image_size"] = str(max_image_size)
310
Ying Wangbd93d422011-10-28 17:02:30 -0700311 if fs_type.startswith("ext"):
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800312 build_command = [prop_dict["ext_mkuserimg"]]
Ying Wangbd93d422011-10-28 17:02:30 -0700313 if "extfs_sparse_flag" in prop_dict:
314 build_command.append(prop_dict["extfs_sparse_flag"])
Tao Baoc72727a2017-12-07 10:33:00 -0800315 run_e2fsck = True
Ying Wangbd93d422011-10-28 17:02:30 -0700316 build_command.extend([in_dir, out_file, fs_type,
317 prop_dict["mount_point"]])
Tao Bao35f4ebc2018-09-27 15:31:11 -0700318 build_command.append(prop_dict["image_size"])
Ying Wangf3b86352014-11-18 18:03:13 -0800319 if "journal_size" in prop_dict:
320 build_command.extend(["-j", prop_dict["journal_size"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800321 if "timestamp" in prop_dict:
322 build_command.extend(["-T", str(prop_dict["timestamp"])])
Ying Wanga2292c92015-03-24 19:07:40 -0700323 if fs_config:
Doug Zongker82822822014-06-16 09:10:55 -0700324 build_command.extend(["-C", fs_config])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700325 if target_out:
326 build_command.extend(["-D", target_out])
Ying Wanga2292c92015-03-24 19:07:40 -0700327 if "block_list" in prop_dict:
328 build_command.extend(["-B", prop_dict["block_list"]])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800329 if "base_fs_file" in prop_dict:
330 base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800331 build_command.extend(["-d", base_fs_file])
Christoffer Dall8ed01f32014-12-17 21:34:12 +0100332 build_command.extend(["-L", prop_dict["mount_point"]])
Patrick Tjina1900842016-10-20 10:58:12 -0700333 if "extfs_inode_count" in prop_dict:
334 build_command.extend(["-i", prop_dict["extfs_inode_count"]])
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700335 if "extfs_rsv_pct" in prop_dict:
336 build_command.extend(["-M", prop_dict["extfs_rsv_pct"]])
Connor O'Brien20f08c32017-01-05 16:48:14 -0800337 if "flash_erase_block_size" in prop_dict:
338 build_command.extend(["-e", prop_dict["flash_erase_block_size"]])
339 if "flash_logical_block_size" in prop_dict:
340 build_command.extend(["-o", prop_dict["flash_logical_block_size"]])
Tao Baod86e3112017-09-22 15:45:33 -0700341 # Specify UUID and hash_seed if using mke2fs.
Tianjie Xu57332222018-08-15 16:16:21 -0700342 if prop_dict["ext_mkuserimg"] == "mkuserimg_mke2fs":
Tao Baod86e3112017-09-22 15:45:33 -0700343 if "uuid" in prop_dict:
344 build_command.extend(["-U", prop_dict["uuid"]])
345 if "hash_seed" in prop_dict:
346 build_command.extend(["-S", prop_dict["hash_seed"]])
Jin Qianfde9f792018-01-22 13:15:46 -0800347 if "ext4_share_dup_blocks" in prop_dict:
348 build_command.append("-c")
Ying Wanga2292c92015-03-24 19:07:40 -0700349 if "selinux_fc" in prop_dict:
Kenny Rootf32dc712012-04-08 10:42:34 -0700350 build_command.append(prop_dict["selinux_fc"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800351 elif fs_type.startswith("squash"):
352 build_command = ["mksquashfsimage.sh"]
353 build_command.extend([in_dir, out_file])
Todd Poynorb2a555e2015-12-15 18:00:14 -0800354 if "squashfs_sparse_flag" in prop_dict:
355 build_command.extend([prop_dict["squashfs_sparse_flag"]])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800356 build_command.extend(["-m", prop_dict["mount_point"]])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700357 if target_out:
358 build_command.extend(["-d", target_out])
Mohamad Ayyash88378822016-04-07 22:10:51 -0700359 if fs_config:
360 build_command.extend(["-C", fs_config])
Ying Wanga2292c92015-03-24 19:07:40 -0700361 if "selinux_fc" in prop_dict:
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800362 build_command.extend(["-c", prop_dict["selinux_fc"]])
Mohamad Ayyashc3484f72016-06-13 09:46:58 -0700363 if "block_list" in prop_dict:
364 build_command.extend(["-B", prop_dict["block_list"]])
Ng Zhi An9446c1d2018-01-19 15:51:46 -0800365 if "squashfs_block_size" in prop_dict:
366 build_command.extend(["-b", prop_dict["squashfs_block_size"]])
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700367 if "squashfs_compressor" in prop_dict:
368 build_command.extend(["-z", prop_dict["squashfs_compressor"]])
369 if "squashfs_compressor_opt" in prop_dict:
370 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
Tao Baoc72727a2017-12-07 10:33:00 -0800371 if prop_dict.get("squashfs_disable_4k_align") == "true":
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700372 build_command.extend(["-a"])
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700373 elif fs_type.startswith("f2fs"):
374 build_command = ["mkf2fsuserimg.sh"]
Tao Bao35f4ebc2018-09-27 15:31:11 -0700375 build_command.extend([out_file, prop_dict["image_size"]])
Jaegeuk Kim2ea1eba2017-11-28 19:21:28 -0800376 if fs_config:
377 build_command.extend(["-C", fs_config])
378 build_command.extend(["-f", in_dir])
379 if target_out:
380 build_command.extend(["-D", target_out])
381 if "selinux_fc" in prop_dict:
382 build_command.extend(["-s", prop_dict["selinux_fc"]])
383 build_command.extend(["-t", prop_dict["mount_point"]])
384 if "timestamp" in prop_dict:
385 build_command.extend(["-T", str(prop_dict["timestamp"])])
386 build_command.extend(["-L", prop_dict["mount_point"]])
Ying Wangbd93d422011-10-28 17:02:30 -0700387 else:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700388 raise BuildImageError(
389 "Error: unknown filesystem type: {}".format(fs_type))
Ying Wangbd93d422011-10-28 17:02:30 -0700390
Tao Bao986ee862018-10-04 15:46:16 -0700391 try:
392 mkfs_output = common.RunAndCheckOutput(build_command)
393 except:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700394 try:
395 du = GetDiskUsage(in_dir)
396 du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB)
Tao Bao986ee862018-10-04 15:46:16 -0700397 # Suppress any errors from GetDiskUsage() to avoid hiding the real errors
398 # from common.RunAndCheckOutput().
Tao Bao32fcdab2018-10-12 10:30:39 -0700399 except Exception: # pylint: disable=broad-except
400 logger.exception("Failed to compute disk usage with du")
Tao Baoc6bd70a2018-09-27 16:58:00 -0700401 du_str = "unknown"
Tao Bao4251fe92018-07-23 13:05:00 -0700402 print(
Mark Salyzyn533558e2018-11-08 19:26:50 +0000403 "Out of space? The tree size of {} is {}, with reserved space of {} "
404 "bytes ({} MB).".format(
Tao Baoc2606eb2018-07-20 14:44:46 -0700405 in_dir, du_str,
Tao Bao4251fe92018-07-23 13:05:00 -0700406 int(prop_dict.get("partition_reserved_size", 0)),
407 int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB))
Tao Bao35f4ebc2018-09-27 15:31:11 -0700408 print(
Mark Salyzyn780f5952018-10-19 13:44:36 -0700409 "The max image size for filesystem files is {} bytes ({} MB), out of a "
Tao Bao35f4ebc2018-09-27 15:31:11 -0700410 "total partition size of {} bytes ({} MB).".format(
411 int(prop_dict["image_size"]),
412 int(prop_dict["image_size"]) // BYTES_IN_MB,
413 int(prop_dict["partition_size"]),
414 int(prop_dict["partition_size"]) // BYTES_IN_MB))
Tao Bao986ee862018-10-04 15:46:16 -0700415 raise
Ying Wang69e9b4d2012-11-26 18:10:23 -0800416
Tao Baod4349f22017-12-07 23:01:25 -0800417 # Check if there's enough headroom space available for ext4 image.
Tao Bao79d52f82017-12-07 14:07:44 -0800418 if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700419 CheckHeadroom(mkfs_output, prop_dict)
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700420
Tao Bao7549e5e2018-10-03 14:23:59 -0700421 if not fs_spans_partition and verity_image_builder:
422 verity_image_builder.PadSparseImage(out_file)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700423
Tao Baoc72727a2017-12-07 10:33:00 -0800424 # Create the verified image if this is to be verified.
Tao Bao7549e5e2018-10-03 14:23:59 -0700425 if verity_image_builder:
426 verity_image_builder.Build(out_file)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400427
Mark Salyzyn533558e2018-11-08 19:26:50 +0000428 if run_e2fsck and prop_dict.get("skip_fsck") != "true":
429 unsparse_image = UnsparseImage(out_file, replace=False)
430
431 # Run e2fsck on the inflated image file
432 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
433 try:
434 common.RunAndCheckOutput(e2fsck_command)
435 finally:
436 os.remove(unsparse_image)
437
Ying Wangbd93d422011-10-28 17:02:30 -0700438
439def ImagePropFromGlobalDict(glob_dict, mount_point):
440 """Build an image property dictionary from the global dictionary.
441
442 Args:
443 glob_dict: the global dictionary from the build system.
444 mount_point: such as "system", "data" etc.
445 """
Doug Zongker1ad7ade2013-12-06 11:53:27 -0800446 d = {}
Tao Bao052ae352015-09-28 13:44:13 -0700447
Tao Bao822f5842015-09-30 16:01:14 -0700448 if "build.prop" in glob_dict:
449 bp = glob_dict["build.prop"]
450 if "ro.build.date.utc" in bp:
451 d["timestamp"] = bp["ro.build.date.utc"]
Ying Wang9f8e8db2011-11-04 11:37:01 -0700452
453 def copy_prop(src_p, dest_p):
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700454 """Copy a property from the global dictionary.
455
456 Args:
457 src_p: The source property in the global dictionary.
458 dest_p: The destination property.
459 Returns:
460 True if property was found and copied, False otherwise.
461 """
Ying Wang9f8e8db2011-11-04 11:37:01 -0700462 if src_p in glob_dict:
463 d[dest_p] = str(glob_dict[src_p])
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700464 return True
465 return False
Ying Wang9f8e8db2011-11-04 11:37:01 -0700466
Ying Wangbd93d422011-10-28 17:02:30 -0700467 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -0700468 "extfs_sparse_flag",
Todd Poynorb2a555e2015-12-15 18:00:14 -0800469 "squashfs_sparse_flag",
Kenny Rootf32dc712012-04-08 10:42:34 -0700470 "selinux_fc",
Ying Wang6a42a252013-02-27 13:54:02 -0800471 "skip_fsck",
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800472 "ext_mkuserimg",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700473 "verity",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700474 "verity_key",
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100475 "verity_signer_cmd",
David Zeuthen4014a9d2016-09-30 17:29:22 -0400476 "verity_fec",
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800477 "verity_disable",
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800478 "avb_enable",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700479 "avb_avbtool",
480 "avb_salt",
Yifan Hong2dae5722018-07-31 12:47:27 -0700481 "use_dynamic_partition_size",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700482 )
Ying Wangbd93d422011-10-28 17:02:30 -0700483 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -0700484 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -0700485
486 d["mount_point"] = mount_point
487 if mount_point == "system":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800488 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
489 copy_prop("avb_system_add_hashtree_footer_args",
490 "avb_add_hashtree_footer_args")
491 copy_prop("avb_system_key_path", "avb_key_path")
492 copy_prop("avb_system_algorithm", "avb_algorithm")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700493 copy_prop("fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700494 # Copy the generic system fs type first, override with specific one if
Dan Albert8b72aef2015-03-23 19:13:21 -0700495 # available.
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800496 copy_prop("system_fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700497 copy_prop("system_headroom", "partition_headroom")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700498 copy_prop("system_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700499 if not copy_prop("system_journal_size", "journal_size"):
500 d["journal_size"] = "0"
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700501 copy_prop("system_verity_block_device", "verity_block_device")
Tao Baof3282b42015-04-01 11:21:55 -0700502 copy_prop("system_root_image", "system_root_image")
Tao Bao8bfd3c72018-07-20 15:20:28 -0700503 copy_prop("root_dir", "root_dir")
504 copy_prop("root_fs_config", "root_fs_config")
Jin Qianfde9f792018-01-22 13:15:46 -0800505 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700506 copy_prop("system_squashfs_compressor", "squashfs_compressor")
507 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700508 copy_prop("system_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700509 copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800510 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700511 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700512 if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
513 d["extfs_rsv_pct"] = "0"
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700514 copy_prop("system_reserved_size", "partition_reserved_size")
Alex Light4e358ab2016-06-16 14:47:10 -0700515 elif mount_point == "system_other":
Tao Baoc72727a2017-12-07 10:33:00 -0800516 # We inherit the selinux policies of /system since we contain some of its
517 # files.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800518 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
519 copy_prop("avb_system_add_hashtree_footer_args",
520 "avb_add_hashtree_footer_args")
521 copy_prop("avb_system_key_path", "avb_key_path")
522 copy_prop("avb_system_algorithm", "avb_algorithm")
Alex Light4e358ab2016-06-16 14:47:10 -0700523 copy_prop("fs_type", "fs_type")
524 copy_prop("system_fs_type", "fs_type")
525 copy_prop("system_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700526 if not copy_prop("system_journal_size", "journal_size"):
527 d["journal_size"] = "0"
Alex Light4e358ab2016-06-16 14:47:10 -0700528 copy_prop("system_verity_block_device", "verity_block_device")
Mark Salyzynf0cef8d2018-10-29 10:55:06 -0700529 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Alex Light4e358ab2016-06-16 14:47:10 -0700530 copy_prop("system_squashfs_compressor", "squashfs_compressor")
531 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
532 copy_prop("system_squashfs_block_size", "squashfs_block_size")
533 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700534 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700535 if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
536 d["extfs_rsv_pct"] = "0"
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700537 copy_prop("system_reserved_size", "partition_reserved_size")
Ying Wangbd93d422011-10-28 17:02:30 -0700538 elif mount_point == "data":
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700539 # Copy the generic fs type first, override with specific one if available.
Ying Wang9f8e8db2011-11-04 11:37:01 -0700540 copy_prop("fs_type", "fs_type")
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700541 copy_prop("userdata_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700542 copy_prop("userdata_size", "partition_size")
Tao Baoc72727a2017-12-07 10:33:00 -0800543 copy_prop("flash_logical_block_size", "flash_logical_block_size")
Connor O'Brien20f08c32017-01-05 16:48:14 -0800544 copy_prop("flash_erase_block_size", "flash_erase_block_size")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700545 elif mount_point == "cache":
546 copy_prop("cache_fs_type", "fs_type")
547 copy_prop("cache_size", "partition_size")
Ying Wanga0febe52013-03-20 11:02:05 -0700548 elif mount_point == "vendor":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800549 copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable")
550 copy_prop("avb_vendor_add_hashtree_footer_args",
551 "avb_add_hashtree_footer_args")
552 copy_prop("avb_vendor_key_path", "avb_key_path")
553 copy_prop("avb_vendor_algorithm", "avb_algorithm")
Ying Wanga0febe52013-03-20 11:02:05 -0700554 copy_prop("vendor_fs_type", "fs_type")
555 copy_prop("vendor_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700556 if not copy_prop("vendor_journal_size", "journal_size"):
557 d["journal_size"] = "0"
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700558 copy_prop("vendor_verity_block_device", "verity_block_device")
Jin Qianfde9f792018-01-22 13:15:46 -0800559 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Patrick Tjine11aa502016-02-09 15:40:38 -0800560 copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
561 copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700562 copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700563 copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800564 copy_prop("vendor_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700565 copy_prop("vendor_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700566 if not copy_prop("vendor_extfs_rsv_pct", "extfs_rsv_pct"):
567 d["extfs_rsv_pct"] = "0"
Yifan Hong749062d2018-06-19 16:23:16 -0700568 copy_prop("vendor_reserved_size", "partition_reserved_size")
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900569 elif mount_point == "product":
570 copy_prop("avb_product_hashtree_enable", "avb_hashtree_enable")
571 copy_prop("avb_product_add_hashtree_footer_args",
572 "avb_add_hashtree_footer_args")
573 copy_prop("avb_product_key_path", "avb_key_path")
574 copy_prop("avb_product_algorithm", "avb_algorithm")
575 copy_prop("product_fs_type", "fs_type")
576 copy_prop("product_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700577 if not copy_prop("product_journal_size", "journal_size"):
578 d["journal_size"] = "0"
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900579 copy_prop("product_verity_block_device", "verity_block_device")
Mark Salyzynf0cef8d2018-10-29 10:55:06 -0700580 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900581 copy_prop("product_squashfs_compressor", "squashfs_compressor")
582 copy_prop("product_squashfs_compressor_opt", "squashfs_compressor_opt")
583 copy_prop("product_squashfs_block_size", "squashfs_block_size")
584 copy_prop("product_squashfs_disable_4k_align", "squashfs_disable_4k_align")
585 copy_prop("product_base_fs_file", "base_fs_file")
586 copy_prop("product_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700587 if not copy_prop("product_extfs_rsv_pct", "extfs_rsv_pct"):
588 d["extfs_rsv_pct"] = "0"
Yifan Hong56a6c3b2018-07-20 15:19:34 -0700589 copy_prop("product_reserved_size", "partition_reserved_size")
Dario Freni924af7d2018-08-17 00:56:14 +0100590 elif mount_point == "product_services":
Yifan Hongebc041a2018-07-26 16:02:52 -0700591 copy_prop("avb_product_services_hashtree_enable", "avb_hashtree_enable")
592 copy_prop("avb_product_services_add_hashtree_footer_args",
Dario Freni5f681e12018-05-29 13:09:01 +0100593 "avb_add_hashtree_footer_args")
Yifan Hongebc041a2018-07-26 16:02:52 -0700594 copy_prop("avb_product_services_key_path", "avb_key_path")
595 copy_prop("avb_product_services_algorithm", "avb_algorithm")
596 copy_prop("product_services_fs_type", "fs_type")
597 copy_prop("product_services_size", "partition_size")
598 if not copy_prop("product_services_journal_size", "journal_size"):
Dario Freni5f681e12018-05-29 13:09:01 +0100599 d["journal_size"] = "0"
Yifan Hongebc041a2018-07-26 16:02:52 -0700600 copy_prop("product_services_verity_block_device", "verity_block_device")
Mark Salyzynf0cef8d2018-10-29 10:55:06 -0700601 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Yifan Hongebc041a2018-07-26 16:02:52 -0700602 copy_prop("product_services_squashfs_compressor", "squashfs_compressor")
603 copy_prop("product_services_squashfs_compressor_opt",
Dario Freni5f681e12018-05-29 13:09:01 +0100604 "squashfs_compressor_opt")
Yifan Hongebc041a2018-07-26 16:02:52 -0700605 copy_prop("product_services_squashfs_block_size", "squashfs_block_size")
606 copy_prop("product_services_squashfs_disable_4k_align",
Dario Freni5f681e12018-05-29 13:09:01 +0100607 "squashfs_disable_4k_align")
Yifan Hongebc041a2018-07-26 16:02:52 -0700608 copy_prop("product_services_base_fs_file", "base_fs_file")
609 copy_prop("product_services_extfs_inode_count", "extfs_inode_count")
610 if not copy_prop("product_services_extfs_rsv_pct", "extfs_rsv_pct"):
Dario Freni5f681e12018-05-29 13:09:01 +0100611 d["extfs_rsv_pct"] = "0"
Yifan Hongebc041a2018-07-26 16:02:52 -0700612 copy_prop("product_services_reserved_size", "partition_reserved_size")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800613 elif mount_point == "odm":
614 copy_prop("avb_odm_hashtree_enable", "avb_hashtree_enable")
615 copy_prop("avb_odm_add_hashtree_footer_args",
616 "avb_add_hashtree_footer_args")
617 copy_prop("avb_odm_key_path", "avb_key_path")
618 copy_prop("avb_odm_algorithm", "avb_algorithm")
619 copy_prop("odm_fs_type", "fs_type")
620 copy_prop("odm_size", "partition_size")
621 if not copy_prop("odm_journal_size", "journal_size"):
622 d["journal_size"] = "0"
623 copy_prop("odm_verity_block_device", "verity_block_device")
Mark Salyzynf0cef8d2018-10-29 10:55:06 -0700624 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800625 copy_prop("odm_squashfs_compressor", "squashfs_compressor")
626 copy_prop("odm_squashfs_compressor_opt", "squashfs_compressor_opt")
627 copy_prop("odm_squashfs_block_size", "squashfs_block_size")
628 copy_prop("odm_squashfs_disable_4k_align", "squashfs_disable_4k_align")
629 copy_prop("odm_base_fs_file", "base_fs_file")
630 copy_prop("odm_extfs_inode_count", "extfs_inode_count")
631 if not copy_prop("odm_extfs_rsv_pct", "extfs_rsv_pct"):
632 d["extfs_rsv_pct"] = "0"
633 copy_prop("odm_reserved_size", "partition_reserved_size")
Ying Wangb8888432014-03-11 17:13:27 -0700634 elif mount_point == "oem":
635 copy_prop("fs_type", "fs_type")
636 copy_prop("oem_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700637 if not copy_prop("oem_journal_size", "journal_size"):
638 d["journal_size"] = "0"
Patrick Tjina1900842016-10-20 10:58:12 -0700639 copy_prop("oem_extfs_inode_count", "extfs_inode_count")
Mark Salyzynf0cef8d2018-10-29 10:55:06 -0700640 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700641 if not copy_prop("oem_extfs_rsv_pct", "extfs_rsv_pct"):
642 d["extfs_rsv_pct"] = "0"
David Zeuthen4014a9d2016-09-30 17:29:22 -0400643 d["partition_name"] = mount_point
Ying Wangbd93d422011-10-28 17:02:30 -0700644 return d
645
646
647def LoadGlobalDict(filename):
648 """Load "name=value" pairs from filename"""
649 d = {}
650 f = open(filename)
651 for line in f:
652 line = line.strip()
653 if not line or line.startswith("#"):
654 continue
655 k, v = line.split("=", 1)
656 d[k] = v
657 f.close()
658 return d
659
660
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700661def GlobalDictFromImageProp(image_prop, mount_point):
662 d = {}
663 def copy_prop(src_p, dest_p):
664 if src_p in image_prop:
665 d[dest_p] = image_prop[src_p]
666 return True
667 return False
Tao Bao4251fe92018-07-23 13:05:00 -0700668
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700669 if mount_point == "system":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700670 copy_prop("partition_size", "system_size")
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700671 elif mount_point == "system_other":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700672 copy_prop("partition_size", "system_size")
Yifan Hong749062d2018-06-19 16:23:16 -0700673 elif mount_point == "vendor":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700674 copy_prop("partition_size", "vendor_size")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800675 elif mount_point == "odm":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700676 copy_prop("partition_size", "odm_size")
Yifan Hong56a6c3b2018-07-20 15:19:34 -0700677 elif mount_point == "product":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700678 copy_prop("partition_size", "product_size")
Dario Freni924af7d2018-08-17 00:56:14 +0100679 elif mount_point == "product_services":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700680 copy_prop("partition_size", "product_services_size")
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700681 return d
682
683
684def SaveGlobalDict(filename, glob_dict):
685 with open(filename, "w") as f:
686 f.writelines(["%s=%s" % (key, value) for (key, value) in glob_dict.items()])
687
688
Ying Wangbd93d422011-10-28 17:02:30 -0700689def main(argv):
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700690 if len(argv) < 4 or len(argv) > 5:
Tao Baoc72727a2017-12-07 10:33:00 -0800691 print(__doc__)
Ying Wangbd93d422011-10-28 17:02:30 -0700692 sys.exit(1)
693
Tao Bao32fcdab2018-10-12 10:30:39 -0700694 common.InitLogging()
695
Ying Wangbd93d422011-10-28 17:02:30 -0700696 in_dir = argv[0]
697 glob_dict_file = argv[1]
698 out_file = argv[2]
Thierry Strudel74a81e62015-07-09 09:54:55 -0700699 target_out = argv[3]
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700700 prop_file_out = argv[4] if len(argv) >= 5 else None
Ying Wangbd93d422011-10-28 17:02:30 -0700701
702 glob_dict = LoadGlobalDict(glob_dict_file)
Ying Wangae61f502015-03-12 18:30:39 -0700703 if "mount_point" in glob_dict:
Mark Salyzyn780f5952018-10-19 13:44:36 -0700704 # The caller knows the mount point and provides a dictionary needed by
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700705 # BuildImage().
Ying Wangae61f502015-03-12 18:30:39 -0700706 image_properties = glob_dict
Ying Wang9f8e8db2011-11-04 11:37:01 -0700707 else:
Ying Wangae61f502015-03-12 18:30:39 -0700708 image_filename = os.path.basename(out_file)
709 mount_point = ""
710 if image_filename == "system.img":
711 mount_point = "system"
Alex Light4e358ab2016-06-16 14:47:10 -0700712 elif image_filename == "system_other.img":
713 mount_point = "system_other"
Ying Wangae61f502015-03-12 18:30:39 -0700714 elif image_filename == "userdata.img":
715 mount_point = "data"
716 elif image_filename == "cache.img":
717 mount_point = "cache"
718 elif image_filename == "vendor.img":
719 mount_point = "vendor"
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800720 elif image_filename == "odm.img":
721 mount_point = "odm"
Ying Wangae61f502015-03-12 18:30:39 -0700722 elif image_filename == "oem.img":
723 mount_point = "oem"
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900724 elif image_filename == "product.img":
725 mount_point = "product"
Dario Freni924af7d2018-08-17 00:56:14 +0100726 elif image_filename == "product_services.img":
727 mount_point = "product_services"
Ying Wangae61f502015-03-12 18:30:39 -0700728 else:
Tao Bao32fcdab2018-10-12 10:30:39 -0700729 logger.error("Unknown image file name %s", image_filename)
Tao Bao1c830bf2017-12-25 10:43:47 -0800730 sys.exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -0700731
Ying Wangae61f502015-03-12 18:30:39 -0700732 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
733
Tao Baoc6bd70a2018-09-27 16:58:00 -0700734 try:
735 BuildImage(in_dir, image_properties, out_file, target_out)
736 except:
Tao Bao32fcdab2018-10-12 10:30:39 -0700737 logger.error("Failed to build %s from %s", out_file, in_dir)
Tao Baoc6bd70a2018-09-27 16:58:00 -0700738 raise
Ying Wangbd93d422011-10-28 17:02:30 -0700739
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700740 if prop_file_out:
741 glob_dict_out = GlobalDictFromImageProp(image_properties, mount_point)
742 SaveGlobalDict(prop_file_out, glob_dict_out)
Ying Wangbd93d422011-10-28 17:02:30 -0700743
Tao Bao32fcdab2018-10-12 10:30:39 -0700744
Ying Wangbd93d422011-10-28 17:02:30 -0700745if __name__ == '__main__':
Tao Bao1c830bf2017-12-25 10:43:47 -0800746 try:
747 main(sys.argv[1:])
748 finally:
749 common.Cleanup()