blob: 4a013c2c3ca0e6783dd8874753b13888c1607e92 [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:
57 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:
Tao Baoc6bd70a2018-09-27 16:58:00 -070060 The number of bytes.
61
62 Raises:
63 BuildImageError: On error.
Yifan Hongbbcba1e2018-06-18 16:32:35 -070064 """
Tao Bao986ee862018-10-04 15:46:16 -070065 env_copy = os.environ.copy()
66 env_copy["POSIXLY_CORRECT"] = "1"
Yifan Hongbbcba1e2018-06-18 16:32:35 -070067 cmd = ["du", "-s", path]
Tao Bao986ee862018-10-04 15:46:16 -070068 try:
69 output = common.RunAndCheckOutput(cmd, verbose=False, env=env_copy)
70 except common.ExternalError:
Tao Baoc6bd70a2018-09-27 16:58:00 -070071 raise BuildImageError("Failed to get disk usage:\n{}".format(output))
Yifan Hongbbcba1e2018-06-18 16:32:35 -070072 # POSIX du returns number of blocks with block size 512
Tao Baoc6bd70a2018-09-27 16:58:00 -070073 return int(output.split()[0]) * 512
Yifan Hongbbcba1e2018-06-18 16:32:35 -070074
75
Geremy Condra6e8f53c2013-12-05 17:09:18 -080076def UnsparseImage(sparse_image_path, replace=True):
Geremy Condrafd6f7512013-06-16 17:26:08 -070077 img_dir = os.path.dirname(sparse_image_path)
78 unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
79 unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
80 if os.path.exists(unsparse_image_path):
Geremy Condra6e8f53c2013-12-05 17:09:18 -080081 if replace:
82 os.unlink(unsparse_image_path)
83 else:
Tao Baoc6bd70a2018-09-27 16:58:00 -070084 return unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -070085 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
Tao Bao986ee862018-10-04 15:46:16 -070086 try:
87 common.RunAndCheckOutput(inflate_command)
88 except:
Geremy Condrafd6f7512013-06-16 17:26:08 -070089 os.remove(unsparse_image_path)
Tao Bao986ee862018-10-04 15:46:16 -070090 raise
Tao Baoc6bd70a2018-09-27 16:58:00 -070091 return unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -070092
Tao Baoc72727a2017-12-07 10:33:00 -080093
Mohamad Ayyashf8765552016-03-02 21:07:23 -080094def ConvertBlockMapToBaseFs(block_map_file):
Tao Bao1c830bf2017-12-25 10:43:47 -080095 base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs")
Mohamad Ayyashf8765552016-03-02 21:07:23 -080096 convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file]
Tao Bao986ee862018-10-04 15:46:16 -070097 common.RunAndCheckOutput(convert_command)
Tao Baoc6bd70a2018-09-27 16:58:00 -070098 return base_fs_file
Mohamad Ayyashf8765552016-03-02 21:07:23 -080099
Tao Baod4349f22017-12-07 23:01:25 -0800100
Tao Baoc2606eb2018-07-20 14:44:46 -0700101def SetUpInDirAndFsConfig(origin_in, prop_dict):
102 """Returns the in_dir and fs_config that should be used for image building.
103
Tom Cherryd14b8952018-08-09 14:26:00 -0700104 When building system.img for all targets, it creates and returns a staged dir
105 that combines the contents of /system (i.e. in the given in_dir) and root.
Tao Baoc2606eb2018-07-20 14:44:46 -0700106
107 Args:
108 origin_in: Path to the input directory.
109 prop_dict: A property dict that contains info like partition size. Values
110 may be updated.
111
112 Returns:
113 A tuple of in_dir and fs_config that should be used to build the image.
114 """
115 fs_config = prop_dict.get("fs_config")
Tom Cherryd14b8952018-08-09 14:26:00 -0700116
117 if prop_dict["mount_point"] == "system_other":
118 prop_dict["mount_point"] = "system"
119 return origin_in, fs_config
120
121 if prop_dict["mount_point"] != "system":
Tao Baoc2606eb2018-07-20 14:44:46 -0700122 return origin_in, fs_config
123
124 # Construct a staging directory of the root file system.
125 in_dir = common.MakeTempDir()
126 root_dir = prop_dict.get("root_dir")
127 if root_dir:
128 shutil.rmtree(in_dir)
129 shutil.copytree(root_dir, in_dir, symlinks=True)
130 in_dir_system = os.path.join(in_dir, "system")
131 shutil.rmtree(in_dir_system, ignore_errors=True)
132 shutil.copytree(origin_in, in_dir_system, symlinks=True)
133
134 # Change the mount point to "/".
135 prop_dict["mount_point"] = "/"
136 if fs_config:
137 # We need to merge the fs_config files of system and root.
138 merged_fs_config = common.MakeTempFile(
139 prefix="merged_fs_config", suffix=".txt")
140 with open(merged_fs_config, "w") as fw:
141 if "root_fs_config" in prop_dict:
142 with open(prop_dict["root_fs_config"]) as fr:
143 fw.writelines(fr.readlines())
144 with open(fs_config) as fr:
145 fw.writelines(fr.readlines())
146 fs_config = merged_fs_config
147 return in_dir, fs_config
148
149
Tao Baod4349f22017-12-07 23:01:25 -0800150def CheckHeadroom(ext4fs_output, prop_dict):
151 """Checks if there's enough headroom space available.
152
153 Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM),
154 which is useful for devices with low disk space that have system image
155 variation between builds. The 'partition_headroom' in prop_dict is the size
156 in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks.
157
158 Args:
159 ext4fs_output: The output string from mke2fs command.
160 prop_dict: The property dict.
161
Tao Baod8a953d2018-01-02 21:19:27 -0800162 Raises:
163 AssertionError: On invalid input.
Tao Baoc6bd70a2018-09-27 16:58:00 -0700164 BuildImageError: On check failure.
Tao Baod4349f22017-12-07 23:01:25 -0800165 """
Tao Baod8a953d2018-01-02 21:19:27 -0800166 assert ext4fs_output is not None
167 assert prop_dict.get('fs_type', '').startswith('ext4')
168 assert 'partition_headroom' in prop_dict
169 assert 'mount_point' in prop_dict
170
Tao Baod4349f22017-12-07 23:01:25 -0800171 ext4fs_stats = re.compile(
172 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
173 r'(?P<total_blocks>[0-9]+) blocks')
Tao Baoc72727a2017-12-07 10:33:00 -0800174 last_line = ext4fs_output.strip().split('\n')[-1]
175 m = ext4fs_stats.match(last_line)
Tao Baod4349f22017-12-07 23:01:25 -0800176 used_blocks = int(m.groupdict().get('used_blocks'))
177 total_blocks = int(m.groupdict().get('total_blocks'))
Tao Baod8a953d2018-01-02 21:19:27 -0800178 headroom_blocks = int(prop_dict['partition_headroom']) / BLOCK_SIZE
Tao Baod4349f22017-12-07 23:01:25 -0800179 adjusted_blocks = total_blocks - headroom_blocks
180 if used_blocks > adjusted_blocks:
Tao Baod8a953d2018-01-02 21:19:27 -0800181 mount_point = prop_dict["mount_point"]
Tao Baoc6bd70a2018-09-27 16:58:00 -0700182 raise BuildImageError(
183 "Error: Not enough room on {} (total: {} blocks, used: {} blocks, "
184 "headroom: {} blocks, available: {} blocks)".format(
185 mount_point, total_blocks, used_blocks, headroom_blocks,
186 adjusted_blocks))
Tao Baod4349f22017-12-07 23:01:25 -0800187
188
Thierry Strudel74a81e62015-07-09 09:54:55 -0700189def BuildImage(in_dir, prop_dict, out_file, target_out=None):
Tao Baoc2606eb2018-07-20 14:44:46 -0700190 """Builds an image for the files under in_dir and writes it to out_file.
191
Ying Wangbd93d422011-10-28 17:02:30 -0700192 Args:
Tao Baoc2606eb2018-07-20 14:44:46 -0700193 in_dir: Path to input directory.
194 prop_dict: A property dict that contains info like partition size. Values
195 will be updated with computed values.
196 out_file: The output image file.
197 target_out: Path to the TARGET_OUT directory as in Makefile. It actually
198 points to the /system directory under PRODUCT_OUT. fs_config (the one
199 under system/core/libcutils) reads device specific FS config files from
200 there.
Ying Wangbd93d422011-10-28 17:02:30 -0700201
Tao Baoc6bd70a2018-09-27 16:58:00 -0700202 Raises:
203 BuildImageError: On build image failures.
Ying Wangbd93d422011-10-28 17:02:30 -0700204 """
Tao Baoc2606eb2018-07-20 14:44:46 -0700205 in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict)
Ying Wanga2292c92015-03-24 19:07:40 -0700206
Ying Wangbd93d422011-10-28 17:02:30 -0700207 build_command = []
208 fs_type = prop_dict.get("fs_type", "")
Tao Baoc72727a2017-12-07 10:33:00 -0800209 run_e2fsck = False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700210
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700211 fs_spans_partition = True
212 if fs_type.startswith("squash"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700213 fs_spans_partition = False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700214
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700215 is_verity_partition = "verity_block_device" in prop_dict
Geremy Condra5b5f4952014-05-05 22:19:37 -0700216 verity_supported = prop_dict.get("verity") == "true"
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100217 verity_fec_supported = prop_dict.get("verity_fec") == "true"
218
Bowgo Tsai040410c2018-09-20 16:40:01 +0800219 avb_footer_type = None
220 if prop_dict.get("avb_hash_enable") == "true":
221 avb_footer_type = "hash"
222 elif prop_dict.get("avb_hashtree_enable") == "true":
223 avb_footer_type = "hashtree"
224
225 if avb_footer_type:
226 avbtool = prop_dict.get("avb_avbtool")
227 avb_signing_args = prop_dict.get(
228 "avb_add_" + avb_footer_type + "_footer_args")
229
Yifan Hong2dae5722018-07-31 12:47:27 -0700230 if (prop_dict.get("use_dynamic_partition_size") == "true" and
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700231 "partition_size" not in prop_dict):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700232 # If partition_size is not defined, use output of `du' + reserved_size.
233 size = GetDiskUsage(in_dir)
Tao Bao32fcdab2018-10-12 10:30:39 -0700234 logger.info(
235 "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB)
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700236 size += int(prop_dict.get("partition_reserved_size", 0))
237 # Round this up to a multiple of 4K so that avbtool works
238 size = common.RoundUpTo4K(size)
Bowgo Tsai040410c2018-09-20 16:40:01 +0800239 # Adjust partition_size to add more space for AVB footer, to prevent
240 # it from consuming partition_reserved_size.
241 if avb_footer_type:
Tao Bao71197512018-10-11 14:08:45 -0700242 size = verity_utils.AVBCalcMinPartitionSize(
Bowgo Tsai040410c2018-09-20 16:40:01 +0800243 size,
Tao Bao71197512018-10-11 14:08:45 -0700244 lambda x: verity_utils.AVBCalcMaxImageSize(
Bowgo Tsai040410c2018-09-20 16:40:01 +0800245 avbtool, avb_footer_type, x, avb_signing_args))
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700246 prop_dict["partition_size"] = str(size)
Tao Bao32fcdab2018-10-12 10:30:39 -0700247 logger.info(
248 "Allocating %d MB for %s.", size // BYTES_IN_MB, out_file)
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700249
Tao Bao35f4ebc2018-09-27 15:31:11 -0700250 prop_dict["image_size"] = prop_dict["partition_size"]
251
252 # Adjust the image size to make room for the hashes if this is to be verified.
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800253 if verity_supported and is_verity_partition:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700254 partition_size = int(prop_dict.get("partition_size"))
Tao Bao71197512018-10-11 14:08:45 -0700255 image_size, verity_size = verity_utils.AdjustPartitionSizeForVerity(
Tao Baoc72727a2017-12-07 10:33:00 -0800256 partition_size, verity_fec_supported)
Tao Bao35f4ebc2018-09-27 15:31:11 -0700257 prop_dict["image_size"] = str(image_size)
Sami Tolvanen433905f2016-09-01 15:58:35 -0700258 prop_dict["verity_size"] = str(verity_size)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700259
Tao Bao35f4ebc2018-09-27 15:31:11 -0700260 # Adjust the image size for AVB hash footer or AVB hashtree footer.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800261 if avb_footer_type:
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800262 partition_size = prop_dict["partition_size"]
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800263 # avb_add_hash_footer_args or avb_add_hashtree_footer_args.
Tao Bao71197512018-10-11 14:08:45 -0700264 max_image_size = verity_utils.AVBCalcMaxImageSize(
Tao Baoc6bd70a2018-09-27 16:58:00 -0700265 avbtool, avb_footer_type, partition_size, avb_signing_args)
Tao Bao35f4ebc2018-09-27 15:31:11 -0700266 prop_dict["image_size"] = str(max_image_size)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400267
Ying Wangbd93d422011-10-28 17:02:30 -0700268 if fs_type.startswith("ext"):
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800269 build_command = [prop_dict["ext_mkuserimg"]]
Ying Wangbd93d422011-10-28 17:02:30 -0700270 if "extfs_sparse_flag" in prop_dict:
271 build_command.append(prop_dict["extfs_sparse_flag"])
Tao Baoc72727a2017-12-07 10:33:00 -0800272 run_e2fsck = True
Ying Wangbd93d422011-10-28 17:02:30 -0700273 build_command.extend([in_dir, out_file, fs_type,
274 prop_dict["mount_point"]])
Tao Bao35f4ebc2018-09-27 15:31:11 -0700275 build_command.append(prop_dict["image_size"])
Ying Wangf3b86352014-11-18 18:03:13 -0800276 if "journal_size" in prop_dict:
277 build_command.extend(["-j", prop_dict["journal_size"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800278 if "timestamp" in prop_dict:
279 build_command.extend(["-T", str(prop_dict["timestamp"])])
Ying Wanga2292c92015-03-24 19:07:40 -0700280 if fs_config:
Doug Zongker82822822014-06-16 09:10:55 -0700281 build_command.extend(["-C", fs_config])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700282 if target_out:
283 build_command.extend(["-D", target_out])
Ying Wanga2292c92015-03-24 19:07:40 -0700284 if "block_list" in prop_dict:
285 build_command.extend(["-B", prop_dict["block_list"]])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800286 if "base_fs_file" in prop_dict:
287 base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800288 build_command.extend(["-d", base_fs_file])
Christoffer Dall8ed01f32014-12-17 21:34:12 +0100289 build_command.extend(["-L", prop_dict["mount_point"]])
Patrick Tjina1900842016-10-20 10:58:12 -0700290 if "extfs_inode_count" in prop_dict:
291 build_command.extend(["-i", prop_dict["extfs_inode_count"]])
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700292 if "extfs_rsv_pct" in prop_dict:
293 build_command.extend(["-M", prop_dict["extfs_rsv_pct"]])
Connor O'Brien20f08c32017-01-05 16:48:14 -0800294 if "flash_erase_block_size" in prop_dict:
295 build_command.extend(["-e", prop_dict["flash_erase_block_size"]])
296 if "flash_logical_block_size" in prop_dict:
297 build_command.extend(["-o", prop_dict["flash_logical_block_size"]])
Tao Baod86e3112017-09-22 15:45:33 -0700298 # Specify UUID and hash_seed if using mke2fs.
Tianjie Xu57332222018-08-15 16:16:21 -0700299 if prop_dict["ext_mkuserimg"] == "mkuserimg_mke2fs":
Tao Baod86e3112017-09-22 15:45:33 -0700300 if "uuid" in prop_dict:
301 build_command.extend(["-U", prop_dict["uuid"]])
302 if "hash_seed" in prop_dict:
303 build_command.extend(["-S", prop_dict["hash_seed"]])
Jin Qianfde9f792018-01-22 13:15:46 -0800304 if "ext4_share_dup_blocks" in prop_dict:
305 build_command.append("-c")
Ying Wanga2292c92015-03-24 19:07:40 -0700306 if "selinux_fc" in prop_dict:
Kenny Rootf32dc712012-04-08 10:42:34 -0700307 build_command.append(prop_dict["selinux_fc"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800308 elif fs_type.startswith("squash"):
309 build_command = ["mksquashfsimage.sh"]
310 build_command.extend([in_dir, out_file])
Todd Poynorb2a555e2015-12-15 18:00:14 -0800311 if "squashfs_sparse_flag" in prop_dict:
312 build_command.extend([prop_dict["squashfs_sparse_flag"]])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800313 build_command.extend(["-m", prop_dict["mount_point"]])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700314 if target_out:
315 build_command.extend(["-d", target_out])
Mohamad Ayyash88378822016-04-07 22:10:51 -0700316 if fs_config:
317 build_command.extend(["-C", fs_config])
Ying Wanga2292c92015-03-24 19:07:40 -0700318 if "selinux_fc" in prop_dict:
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800319 build_command.extend(["-c", prop_dict["selinux_fc"]])
Mohamad Ayyashc3484f72016-06-13 09:46:58 -0700320 if "block_list" in prop_dict:
321 build_command.extend(["-B", prop_dict["block_list"]])
Ng Zhi An9446c1d2018-01-19 15:51:46 -0800322 if "squashfs_block_size" in prop_dict:
323 build_command.extend(["-b", prop_dict["squashfs_block_size"]])
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700324 if "squashfs_compressor" in prop_dict:
325 build_command.extend(["-z", prop_dict["squashfs_compressor"]])
326 if "squashfs_compressor_opt" in prop_dict:
327 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
Tao Baoc72727a2017-12-07 10:33:00 -0800328 if prop_dict.get("squashfs_disable_4k_align") == "true":
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700329 build_command.extend(["-a"])
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700330 elif fs_type.startswith("f2fs"):
331 build_command = ["mkf2fsuserimg.sh"]
Tao Bao35f4ebc2018-09-27 15:31:11 -0700332 build_command.extend([out_file, prop_dict["image_size"]])
Jaegeuk Kim2ea1eba2017-11-28 19:21:28 -0800333 if fs_config:
334 build_command.extend(["-C", fs_config])
335 build_command.extend(["-f", in_dir])
336 if target_out:
337 build_command.extend(["-D", target_out])
338 if "selinux_fc" in prop_dict:
339 build_command.extend(["-s", prop_dict["selinux_fc"]])
340 build_command.extend(["-t", prop_dict["mount_point"]])
341 if "timestamp" in prop_dict:
342 build_command.extend(["-T", str(prop_dict["timestamp"])])
343 build_command.extend(["-L", prop_dict["mount_point"]])
Ying Wangbd93d422011-10-28 17:02:30 -0700344 else:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700345 raise BuildImageError(
346 "Error: unknown filesystem type: {}".format(fs_type))
Ying Wangbd93d422011-10-28 17:02:30 -0700347
Tao Bao986ee862018-10-04 15:46:16 -0700348 try:
349 mkfs_output = common.RunAndCheckOutput(build_command)
350 except:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700351 try:
352 du = GetDiskUsage(in_dir)
353 du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB)
Tao Bao986ee862018-10-04 15:46:16 -0700354 # Suppress any errors from GetDiskUsage() to avoid hiding the real errors
355 # from common.RunAndCheckOutput().
Tao Bao32fcdab2018-10-12 10:30:39 -0700356 except Exception: # pylint: disable=broad-except
357 logger.exception("Failed to compute disk usage with du")
Tao Baoc6bd70a2018-09-27 16:58:00 -0700358 du_str = "unknown"
Tao Bao4251fe92018-07-23 13:05:00 -0700359 print(
360 "Out of space? The tree size of {} is {}, with reserved space of {} "
361 "bytes ({} MB).".format(
Tao Baoc2606eb2018-07-20 14:44:46 -0700362 in_dir, du_str,
Tao Bao4251fe92018-07-23 13:05:00 -0700363 int(prop_dict.get("partition_reserved_size", 0)),
364 int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB))
Tao Bao35f4ebc2018-09-27 15:31:11 -0700365 print(
366 "The max image size for filsystem files is {} bytes ({} MB), out of a "
367 "total partition size of {} bytes ({} MB).".format(
368 int(prop_dict["image_size"]),
369 int(prop_dict["image_size"]) // BYTES_IN_MB,
370 int(prop_dict["partition_size"]),
371 int(prop_dict["partition_size"]) // BYTES_IN_MB))
Tao Bao986ee862018-10-04 15:46:16 -0700372 raise
Ying Wang69e9b4d2012-11-26 18:10:23 -0800373
Tao Baod4349f22017-12-07 23:01:25 -0800374 # Check if there's enough headroom space available for ext4 image.
Tao Bao79d52f82017-12-07 14:07:44 -0800375 if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700376 CheckHeadroom(mkfs_output, prop_dict)
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700377
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700378 if not fs_spans_partition:
379 mount_point = prop_dict.get("mount_point")
Tao Bao35f4ebc2018-09-27 15:31:11 -0700380 image_size = int(prop_dict["image_size"])
Tao Bao71197512018-10-11 14:08:45 -0700381 sparse_image_size = verity_utils.GetSimgSize(out_file)
Tao Bao35f4ebc2018-09-27 15:31:11 -0700382 if sparse_image_size > image_size:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700383 raise BuildImageError(
384 "Error: {} image size of {} is larger than partition size of "
385 "{}".format(mount_point, sparse_image_size, image_size))
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700386 if verity_supported and is_verity_partition:
Tao Bao71197512018-10-11 14:08:45 -0700387 verity_utils.ZeroPadSimg(out_file, image_size - sparse_image_size)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700388
Tao Baoc72727a2017-12-07 10:33:00 -0800389 # Create the verified image if this is to be verified.
Geremy Condra5b5f4952014-05-05 22:19:37 -0700390 if verity_supported and is_verity_partition:
Tao Bao71197512018-10-11 14:08:45 -0700391 verity_utils.MakeVerityEnabledImage(
392 out_file, verity_fec_supported, prop_dict)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700393
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800394 # Add AVB HASH or HASHTREE footer (metadata).
395 if avb_footer_type:
Tao Bao35f4ebc2018-09-27 15:31:11 -0700396 partition_size = prop_dict["partition_size"]
David Zeuthen4014a9d2016-09-30 17:29:22 -0400397 partition_name = prop_dict["partition_name"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800398 # key_path and algorithm are only available when chain partition is used.
399 key_path = prop_dict.get("avb_key_path")
400 algorithm = prop_dict.get("avb_algorithm")
Tao Bao2b6dfd62017-09-27 17:17:43 -0700401 salt = prop_dict.get("avb_salt")
Tao Bao71197512018-10-11 14:08:45 -0700402 verity_utils.AVBAddFooter(
Tao Baoc6bd70a2018-09-27 16:58:00 -0700403 out_file, avbtool, avb_footer_type, partition_size, partition_name,
404 key_path, algorithm, salt, avb_signing_args)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400405
Tao Baoc72727a2017-12-07 10:33:00 -0800406 if run_e2fsck and prop_dict.get("skip_fsck") != "true":
Tao Baoc6bd70a2018-09-27 16:58:00 -0700407 unsparse_image = UnsparseImage(out_file, replace=False)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800408
409 # Run e2fsck on the inflated image file
410 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
Isaac Chenec7fa1c2018-08-02 14:02:56 +0800411 # TODO(b/112062612): work around e2fsck failure with SANITIZE_HOST=address
Tao Bao986ee862018-10-04 15:46:16 -0700412 env4e2fsck = os.environ.copy()
413 env4e2fsck["ASAN_OPTIONS"] = "detect_odr_violation=0"
414 try:
415 common.RunAndCheckOutput(e2fsck_command, env=env4e2fsck)
416 finally:
417 os.remove(unsparse_image)
Ying Wangbd93d422011-10-28 17:02:30 -0700418
419
420def ImagePropFromGlobalDict(glob_dict, mount_point):
421 """Build an image property dictionary from the global dictionary.
422
423 Args:
424 glob_dict: the global dictionary from the build system.
425 mount_point: such as "system", "data" etc.
426 """
Doug Zongker1ad7ade2013-12-06 11:53:27 -0800427 d = {}
Tao Bao052ae352015-09-28 13:44:13 -0700428
Tao Bao822f5842015-09-30 16:01:14 -0700429 if "build.prop" in glob_dict:
430 bp = glob_dict["build.prop"]
431 if "ro.build.date.utc" in bp:
432 d["timestamp"] = bp["ro.build.date.utc"]
Ying Wang9f8e8db2011-11-04 11:37:01 -0700433
434 def copy_prop(src_p, dest_p):
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700435 """Copy a property from the global dictionary.
436
437 Args:
438 src_p: The source property in the global dictionary.
439 dest_p: The destination property.
440 Returns:
441 True if property was found and copied, False otherwise.
442 """
Ying Wang9f8e8db2011-11-04 11:37:01 -0700443 if src_p in glob_dict:
444 d[dest_p] = str(glob_dict[src_p])
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700445 return True
446 return False
Ying Wang9f8e8db2011-11-04 11:37:01 -0700447
Ying Wangbd93d422011-10-28 17:02:30 -0700448 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -0700449 "extfs_sparse_flag",
Todd Poynorb2a555e2015-12-15 18:00:14 -0800450 "squashfs_sparse_flag",
Kenny Rootf32dc712012-04-08 10:42:34 -0700451 "selinux_fc",
Ying Wang6a42a252013-02-27 13:54:02 -0800452 "skip_fsck",
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800453 "ext_mkuserimg",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700454 "verity",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700455 "verity_key",
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100456 "verity_signer_cmd",
David Zeuthen4014a9d2016-09-30 17:29:22 -0400457 "verity_fec",
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800458 "verity_disable",
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800459 "avb_enable",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700460 "avb_avbtool",
461 "avb_salt",
Yifan Hong2dae5722018-07-31 12:47:27 -0700462 "use_dynamic_partition_size",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700463 )
Ying Wangbd93d422011-10-28 17:02:30 -0700464 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -0700465 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -0700466
467 d["mount_point"] = mount_point
468 if mount_point == "system":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800469 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
470 copy_prop("avb_system_add_hashtree_footer_args",
471 "avb_add_hashtree_footer_args")
472 copy_prop("avb_system_key_path", "avb_key_path")
473 copy_prop("avb_system_algorithm", "avb_algorithm")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700474 copy_prop("fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700475 # Copy the generic system fs type first, override with specific one if
Dan Albert8b72aef2015-03-23 19:13:21 -0700476 # available.
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800477 copy_prop("system_fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700478 copy_prop("system_headroom", "partition_headroom")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700479 copy_prop("system_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700480 if not copy_prop("system_journal_size", "journal_size"):
481 d["journal_size"] = "0"
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700482 copy_prop("system_verity_block_device", "verity_block_device")
Tao Baof3282b42015-04-01 11:21:55 -0700483 copy_prop("system_root_image", "system_root_image")
Tao Bao8bfd3c72018-07-20 15:20:28 -0700484 copy_prop("root_dir", "root_dir")
485 copy_prop("root_fs_config", "root_fs_config")
Jin Qianfde9f792018-01-22 13:15:46 -0800486 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700487 copy_prop("system_squashfs_compressor", "squashfs_compressor")
488 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700489 copy_prop("system_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700490 copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800491 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700492 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700493 if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
494 d["extfs_rsv_pct"] = "0"
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700495 copy_prop("system_reserved_size", "partition_reserved_size")
Alex Light4e358ab2016-06-16 14:47:10 -0700496 elif mount_point == "system_other":
Tao Baoc72727a2017-12-07 10:33:00 -0800497 # We inherit the selinux policies of /system since we contain some of its
498 # files.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800499 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
500 copy_prop("avb_system_add_hashtree_footer_args",
501 "avb_add_hashtree_footer_args")
502 copy_prop("avb_system_key_path", "avb_key_path")
503 copy_prop("avb_system_algorithm", "avb_algorithm")
Alex Light4e358ab2016-06-16 14:47:10 -0700504 copy_prop("fs_type", "fs_type")
505 copy_prop("system_fs_type", "fs_type")
506 copy_prop("system_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700507 if not copy_prop("system_journal_size", "journal_size"):
508 d["journal_size"] = "0"
Alex Light4e358ab2016-06-16 14:47:10 -0700509 copy_prop("system_verity_block_device", "verity_block_device")
Alex Light4e358ab2016-06-16 14:47:10 -0700510 copy_prop("system_squashfs_compressor", "squashfs_compressor")
511 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
512 copy_prop("system_squashfs_block_size", "squashfs_block_size")
513 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700514 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700515 if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
516 d["extfs_rsv_pct"] = "0"
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700517 copy_prop("system_reserved_size", "partition_reserved_size")
Ying Wangbd93d422011-10-28 17:02:30 -0700518 elif mount_point == "data":
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700519 # Copy the generic fs type first, override with specific one if available.
Ying Wang9f8e8db2011-11-04 11:37:01 -0700520 copy_prop("fs_type", "fs_type")
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700521 copy_prop("userdata_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700522 copy_prop("userdata_size", "partition_size")
Tao Baoc72727a2017-12-07 10:33:00 -0800523 copy_prop("flash_logical_block_size", "flash_logical_block_size")
Connor O'Brien20f08c32017-01-05 16:48:14 -0800524 copy_prop("flash_erase_block_size", "flash_erase_block_size")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700525 elif mount_point == "cache":
526 copy_prop("cache_fs_type", "fs_type")
527 copy_prop("cache_size", "partition_size")
Ying Wanga0febe52013-03-20 11:02:05 -0700528 elif mount_point == "vendor":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800529 copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable")
530 copy_prop("avb_vendor_add_hashtree_footer_args",
531 "avb_add_hashtree_footer_args")
532 copy_prop("avb_vendor_key_path", "avb_key_path")
533 copy_prop("avb_vendor_algorithm", "avb_algorithm")
Ying Wanga0febe52013-03-20 11:02:05 -0700534 copy_prop("vendor_fs_type", "fs_type")
535 copy_prop("vendor_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700536 if not copy_prop("vendor_journal_size", "journal_size"):
537 d["journal_size"] = "0"
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700538 copy_prop("vendor_verity_block_device", "verity_block_device")
Jin Qianfde9f792018-01-22 13:15:46 -0800539 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Patrick Tjine11aa502016-02-09 15:40:38 -0800540 copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
541 copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700542 copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700543 copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800544 copy_prop("vendor_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700545 copy_prop("vendor_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700546 if not copy_prop("vendor_extfs_rsv_pct", "extfs_rsv_pct"):
547 d["extfs_rsv_pct"] = "0"
Yifan Hong749062d2018-06-19 16:23:16 -0700548 copy_prop("vendor_reserved_size", "partition_reserved_size")
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900549 elif mount_point == "product":
550 copy_prop("avb_product_hashtree_enable", "avb_hashtree_enable")
551 copy_prop("avb_product_add_hashtree_footer_args",
552 "avb_add_hashtree_footer_args")
553 copy_prop("avb_product_key_path", "avb_key_path")
554 copy_prop("avb_product_algorithm", "avb_algorithm")
555 copy_prop("product_fs_type", "fs_type")
556 copy_prop("product_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700557 if not copy_prop("product_journal_size", "journal_size"):
558 d["journal_size"] = "0"
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900559 copy_prop("product_verity_block_device", "verity_block_device")
560 copy_prop("product_squashfs_compressor", "squashfs_compressor")
561 copy_prop("product_squashfs_compressor_opt", "squashfs_compressor_opt")
562 copy_prop("product_squashfs_block_size", "squashfs_block_size")
563 copy_prop("product_squashfs_disable_4k_align", "squashfs_disable_4k_align")
564 copy_prop("product_base_fs_file", "base_fs_file")
565 copy_prop("product_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700566 if not copy_prop("product_extfs_rsv_pct", "extfs_rsv_pct"):
567 d["extfs_rsv_pct"] = "0"
Yifan Hong56a6c3b2018-07-20 15:19:34 -0700568 copy_prop("product_reserved_size", "partition_reserved_size")
Dario Freni924af7d2018-08-17 00:56:14 +0100569 elif mount_point == "product_services":
Yifan Hongebc041a2018-07-26 16:02:52 -0700570 copy_prop("avb_product_services_hashtree_enable", "avb_hashtree_enable")
571 copy_prop("avb_product_services_add_hashtree_footer_args",
Dario Freni5f681e12018-05-29 13:09:01 +0100572 "avb_add_hashtree_footer_args")
Yifan Hongebc041a2018-07-26 16:02:52 -0700573 copy_prop("avb_product_services_key_path", "avb_key_path")
574 copy_prop("avb_product_services_algorithm", "avb_algorithm")
575 copy_prop("product_services_fs_type", "fs_type")
576 copy_prop("product_services_size", "partition_size")
577 if not copy_prop("product_services_journal_size", "journal_size"):
Dario Freni5f681e12018-05-29 13:09:01 +0100578 d["journal_size"] = "0"
Yifan Hongebc041a2018-07-26 16:02:52 -0700579 copy_prop("product_services_verity_block_device", "verity_block_device")
580 copy_prop("product_services_squashfs_compressor", "squashfs_compressor")
581 copy_prop("product_services_squashfs_compressor_opt",
Dario Freni5f681e12018-05-29 13:09:01 +0100582 "squashfs_compressor_opt")
Yifan Hongebc041a2018-07-26 16:02:52 -0700583 copy_prop("product_services_squashfs_block_size", "squashfs_block_size")
584 copy_prop("product_services_squashfs_disable_4k_align",
Dario Freni5f681e12018-05-29 13:09:01 +0100585 "squashfs_disable_4k_align")
Yifan Hongebc041a2018-07-26 16:02:52 -0700586 copy_prop("product_services_base_fs_file", "base_fs_file")
587 copy_prop("product_services_extfs_inode_count", "extfs_inode_count")
588 if not copy_prop("product_services_extfs_rsv_pct", "extfs_rsv_pct"):
Dario Freni5f681e12018-05-29 13:09:01 +0100589 d["extfs_rsv_pct"] = "0"
Yifan Hongebc041a2018-07-26 16:02:52 -0700590 copy_prop("product_services_reserved_size", "partition_reserved_size")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800591 elif mount_point == "odm":
592 copy_prop("avb_odm_hashtree_enable", "avb_hashtree_enable")
593 copy_prop("avb_odm_add_hashtree_footer_args",
594 "avb_add_hashtree_footer_args")
595 copy_prop("avb_odm_key_path", "avb_key_path")
596 copy_prop("avb_odm_algorithm", "avb_algorithm")
597 copy_prop("odm_fs_type", "fs_type")
598 copy_prop("odm_size", "partition_size")
599 if not copy_prop("odm_journal_size", "journal_size"):
600 d["journal_size"] = "0"
601 copy_prop("odm_verity_block_device", "verity_block_device")
602 copy_prop("odm_squashfs_compressor", "squashfs_compressor")
603 copy_prop("odm_squashfs_compressor_opt", "squashfs_compressor_opt")
604 copy_prop("odm_squashfs_block_size", "squashfs_block_size")
605 copy_prop("odm_squashfs_disable_4k_align", "squashfs_disable_4k_align")
606 copy_prop("odm_base_fs_file", "base_fs_file")
607 copy_prop("odm_extfs_inode_count", "extfs_inode_count")
608 if not copy_prop("odm_extfs_rsv_pct", "extfs_rsv_pct"):
609 d["extfs_rsv_pct"] = "0"
610 copy_prop("odm_reserved_size", "partition_reserved_size")
Ying Wangb8888432014-03-11 17:13:27 -0700611 elif mount_point == "oem":
612 copy_prop("fs_type", "fs_type")
613 copy_prop("oem_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700614 if not copy_prop("oem_journal_size", "journal_size"):
615 d["journal_size"] = "0"
Patrick Tjina1900842016-10-20 10:58:12 -0700616 copy_prop("oem_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700617 if not copy_prop("oem_extfs_rsv_pct", "extfs_rsv_pct"):
618 d["extfs_rsv_pct"] = "0"
David Zeuthen4014a9d2016-09-30 17:29:22 -0400619 d["partition_name"] = mount_point
Ying Wangbd93d422011-10-28 17:02:30 -0700620 return d
621
622
623def LoadGlobalDict(filename):
624 """Load "name=value" pairs from filename"""
625 d = {}
626 f = open(filename)
627 for line in f:
628 line = line.strip()
629 if not line or line.startswith("#"):
630 continue
631 k, v = line.split("=", 1)
632 d[k] = v
633 f.close()
634 return d
635
636
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700637def GlobalDictFromImageProp(image_prop, mount_point):
638 d = {}
639 def copy_prop(src_p, dest_p):
640 if src_p in image_prop:
641 d[dest_p] = image_prop[src_p]
642 return True
643 return False
Tao Bao4251fe92018-07-23 13:05:00 -0700644
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700645 if mount_point == "system":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700646 copy_prop("partition_size", "system_size")
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700647 elif mount_point == "system_other":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700648 copy_prop("partition_size", "system_size")
Yifan Hong749062d2018-06-19 16:23:16 -0700649 elif mount_point == "vendor":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700650 copy_prop("partition_size", "vendor_size")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800651 elif mount_point == "odm":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700652 copy_prop("partition_size", "odm_size")
Yifan Hong56a6c3b2018-07-20 15:19:34 -0700653 elif mount_point == "product":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700654 copy_prop("partition_size", "product_size")
Dario Freni924af7d2018-08-17 00:56:14 +0100655 elif mount_point == "product_services":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700656 copy_prop("partition_size", "product_services_size")
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700657 return d
658
659
660def SaveGlobalDict(filename, glob_dict):
661 with open(filename, "w") as f:
662 f.writelines(["%s=%s" % (key, value) for (key, value) in glob_dict.items()])
663
664
Ying Wangbd93d422011-10-28 17:02:30 -0700665def main(argv):
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700666 if len(argv) < 4 or len(argv) > 5:
Tao Baoc72727a2017-12-07 10:33:00 -0800667 print(__doc__)
Ying Wangbd93d422011-10-28 17:02:30 -0700668 sys.exit(1)
669
Tao Bao32fcdab2018-10-12 10:30:39 -0700670 common.InitLogging()
671
Ying Wangbd93d422011-10-28 17:02:30 -0700672 in_dir = argv[0]
673 glob_dict_file = argv[1]
674 out_file = argv[2]
Thierry Strudel74a81e62015-07-09 09:54:55 -0700675 target_out = argv[3]
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700676 prop_file_out = argv[4] if len(argv) >= 5 else None
Ying Wangbd93d422011-10-28 17:02:30 -0700677
678 glob_dict = LoadGlobalDict(glob_dict_file)
Ying Wangae61f502015-03-12 18:30:39 -0700679 if "mount_point" in glob_dict:
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700680 # The caller knows the mount point and provides a dictionay needed by
681 # BuildImage().
Ying Wangae61f502015-03-12 18:30:39 -0700682 image_properties = glob_dict
Ying Wang9f8e8db2011-11-04 11:37:01 -0700683 else:
Ying Wangae61f502015-03-12 18:30:39 -0700684 image_filename = os.path.basename(out_file)
685 mount_point = ""
686 if image_filename == "system.img":
687 mount_point = "system"
Alex Light4e358ab2016-06-16 14:47:10 -0700688 elif image_filename == "system_other.img":
689 mount_point = "system_other"
Ying Wangae61f502015-03-12 18:30:39 -0700690 elif image_filename == "userdata.img":
691 mount_point = "data"
692 elif image_filename == "cache.img":
693 mount_point = "cache"
694 elif image_filename == "vendor.img":
695 mount_point = "vendor"
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800696 elif image_filename == "odm.img":
697 mount_point = "odm"
Ying Wangae61f502015-03-12 18:30:39 -0700698 elif image_filename == "oem.img":
699 mount_point = "oem"
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900700 elif image_filename == "product.img":
701 mount_point = "product"
Dario Freni924af7d2018-08-17 00:56:14 +0100702 elif image_filename == "product_services.img":
703 mount_point = "product_services"
Ying Wangae61f502015-03-12 18:30:39 -0700704 else:
Tao Bao32fcdab2018-10-12 10:30:39 -0700705 logger.error("Unknown image file name %s", image_filename)
Tao Bao1c830bf2017-12-25 10:43:47 -0800706 sys.exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -0700707
Ying Wangae61f502015-03-12 18:30:39 -0700708 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
709
Tao Baoc6bd70a2018-09-27 16:58:00 -0700710 try:
711 BuildImage(in_dir, image_properties, out_file, target_out)
712 except:
Tao Bao32fcdab2018-10-12 10:30:39 -0700713 logger.error("Failed to build %s from %s", out_file, in_dir)
Tao Baoc6bd70a2018-09-27 16:58:00 -0700714 raise
Ying Wangbd93d422011-10-28 17:02:30 -0700715
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700716 if prop_file_out:
717 glob_dict_out = GlobalDictFromImageProp(image_properties, mount_point)
718 SaveGlobalDict(prop_file_out, glob_dict_out)
Ying Wangbd93d422011-10-28 17:02:30 -0700719
Tao Bao32fcdab2018-10-12 10:30:39 -0700720
Ying Wangbd93d422011-10-28 17:02:30 -0700721if __name__ == '__main__':
Tao Bao1c830bf2017-12-25 10:43:47 -0800722 try:
723 main(sys.argv[1:])
724 finally:
725 common.Cleanup()