blob: 42f05a7acad31ad1cfd179949483d3803ce37699 [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
Ying Wangbd93d422011-10-28 17:02:30 -070029import os
Ying Wang69e9b4d2012-11-26 18:10:23 -080030import os.path
Tao Baoc7a6f1e2015-06-23 11:16:05 -070031import re
David Zeuthen4014a9d2016-09-30 17:29:22 -040032import shlex
Geremy Condrafd6f7512013-06-16 17:26:08 -070033import shutil
Tao Baoc72727a2017-12-07 10:33:00 -080034import sys
35
36import common
Sami Tolvanen405e71d2016-02-09 12:28:58 -080037import sparse_img
Tao Baoc72727a2017-12-07 10:33:00 -080038
Ying Wangbd93d422011-10-28 17:02:30 -070039
Baligh Uddin601ddea2015-06-09 15:48:14 -070040OPTIONS = common.OPTIONS
41
Geremy Condrae8e982a2014-05-16 19:14:30 -070042FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"
Sami Tolvanenf99b5312015-05-20 07:30:57 +010043BLOCK_SIZE = 4096
Yifan Hongbbcba1e2018-06-18 16:32:35 -070044BYTES_IN_MB = 1024 * 1024
Geremy Condrae8e982a2014-05-16 19:14:30 -070045
Tao Baoc72727a2017-12-07 10:33:00 -080046
Tao Baoc6bd70a2018-09-27 16:58:00 -070047class BuildImageError(Exception):
48 """An Exception raised during image building."""
49
50 def __init__(self, message):
51 Exception.__init__(self, message)
52
53
Sami Tolvanenf99b5312015-05-20 07:30:57 +010054def GetVerityFECSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080055 cmd = ["fec", "-s", str(partition_size)]
Tao Bao986ee862018-10-04 15:46:16 -070056 output = common.RunAndCheckOutput(cmd, verbose=False)
Tao Baoc6bd70a2018-09-27 16:58:00 -070057 return int(output)
Sami Tolvanenf99b5312015-05-20 07:30:57 +010058
Tao Baoc72727a2017-12-07 10:33:00 -080059
Geremy Condrafd6f7512013-06-16 17:26:08 -070060def GetVerityTreeSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080061 cmd = ["build_verity_tree", "-s", str(partition_size)]
Tao Bao986ee862018-10-04 15:46:16 -070062 output = common.RunAndCheckOutput(cmd, verbose=False)
Tao Baoc6bd70a2018-09-27 16:58:00 -070063 return int(output)
Geremy Condrafd6f7512013-06-16 17:26:08 -070064
Tao Baoc72727a2017-12-07 10:33:00 -080065
Geremy Condrafd6f7512013-06-16 17:26:08 -070066def GetVerityMetadataSize(partition_size):
Tao Baob4ec6d72018-03-15 23:21:28 -070067 cmd = ["build_verity_metadata.py", "size", str(partition_size)]
Tao Bao986ee862018-10-04 15:46:16 -070068 output = common.RunAndCheckOutput(cmd, verbose=False)
Tao Baoc6bd70a2018-09-27 16:58:00 -070069 return int(output)
Geremy Condrafd6f7512013-06-16 17:26:08 -070070
Tao Baoc72727a2017-12-07 10:33:00 -080071
Sami Tolvanenf99b5312015-05-20 07:30:57 +010072def GetVeritySize(partition_size, fec_supported):
Tao Baoc6bd70a2018-09-27 16:58:00 -070073 verity_tree_size = GetVerityTreeSize(partition_size)
74 verity_metadata_size = GetVerityMetadataSize(partition_size)
Sami Tolvanenf99b5312015-05-20 07:30:57 +010075 verity_size = verity_tree_size + verity_metadata_size
76 if fec_supported:
Tao Baoc6bd70a2018-09-27 16:58:00 -070077 fec_size = GetVerityFECSize(partition_size + verity_size)
Sami Tolvanenf99b5312015-05-20 07:30:57 +010078 return verity_size + fec_size
79 return verity_size
80
Tao Baoc72727a2017-12-07 10:33:00 -080081
Yifan Hongbbcba1e2018-06-18 16:32:35 -070082def GetDiskUsage(path):
Tao Baoc6bd70a2018-09-27 16:58:00 -070083 """Returns the number of bytes that "path" occupies on host.
Yifan Hongbbcba1e2018-06-18 16:32:35 -070084
85 Args:
86 path: The directory or file to calculate size on
Tao Baoc6bd70a2018-09-27 16:58:00 -070087
Yifan Hongbbcba1e2018-06-18 16:32:35 -070088 Returns:
Tao Baoc6bd70a2018-09-27 16:58:00 -070089 The number of bytes.
90
91 Raises:
92 BuildImageError: On error.
Yifan Hongbbcba1e2018-06-18 16:32:35 -070093 """
Tao Bao986ee862018-10-04 15:46:16 -070094 env_copy = os.environ.copy()
95 env_copy["POSIXLY_CORRECT"] = "1"
Yifan Hongbbcba1e2018-06-18 16:32:35 -070096 cmd = ["du", "-s", path]
Tao Bao986ee862018-10-04 15:46:16 -070097 try:
98 output = common.RunAndCheckOutput(cmd, verbose=False, env=env_copy)
99 except common.ExternalError:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700100 raise BuildImageError("Failed to get disk usage:\n{}".format(output))
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700101 # POSIX du returns number of blocks with block size 512
Tao Baoc6bd70a2018-09-27 16:58:00 -0700102 return int(output.split()[0]) * 512
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700103
104
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800105def GetSimgSize(image_file):
106 simg = sparse_img.SparseImage(image_file, build_map=False)
107 return simg.blocksize * simg.total_blocks
108
Tao Baoc72727a2017-12-07 10:33:00 -0800109
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800110def ZeroPadSimg(image_file, pad_size):
111 blocks = pad_size // BLOCK_SIZE
112 print("Padding %d blocks (%d bytes)" % (blocks, pad_size))
113 simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False)
114 simg.AppendFillChunk(0, blocks)
115
Tao Baoc72727a2017-12-07 10:33:00 -0800116
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800117def AVBCalcMaxImageSize(avbtool, footer_type, partition_size, additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400118 """Calculates max image size for a given partition size.
119
120 Args:
121 avbtool: String with path to avbtool.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800122 footer_type: 'hash' or 'hashtree' for generating footer.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400123 partition_size: The size of the partition in question.
Bowgo Tsai040410c2018-09-20 16:40:01 +0800124 additional_args: Additional arguments to pass to "avbtool add_hash_footer"
125 or "avbtool add_hashtree_footer".
126
David Zeuthen4014a9d2016-09-30 17:29:22 -0400127 Returns:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700128 The maximum image size.
129
130 Raises:
Tao Bao986ee862018-10-04 15:46:16 -0700131 BuildImageError: On invalid image size.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400132 """
Tao Baoc72727a2017-12-07 10:33:00 -0800133 cmd = [avbtool, "add_%s_footer" % footer_type,
Bowgo Tsai040410c2018-09-20 16:40:01 +0800134 "--partition_size", str(partition_size), "--calc_max_image_size"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800135 cmd.extend(shlex.split(additional_args))
136
Tao Bao986ee862018-10-04 15:46:16 -0700137 output = common.RunAndCheckOutput(cmd)
Tao Baoc6bd70a2018-09-27 16:58:00 -0700138 image_size = int(output)
139 if image_size <= 0:
140 raise BuildImageError(
141 "Invalid max image size: {}".format(output))
142 return image_size
David Zeuthen4014a9d2016-09-30 17:29:22 -0400143
Tao Baoc72727a2017-12-07 10:33:00 -0800144
Bowgo Tsai040410c2018-09-20 16:40:01 +0800145def AVBCalcMinPartitionSize(image_size, size_calculator):
146 """Calculates min partition size for a given image size.
147
148 Args:
149 image_size: The size of the image in question.
150 size_calculator: The function to calculate max image size
151 for a given partition size.
152
153 Returns:
154 The minimum partition size required to accommodate the image size.
155 """
156 # Use image size as partition size to approximate final partition size.
157 image_ratio = size_calculator(image_size) / float(image_size)
158
159 # Prepare a binary search for the optimal partition size.
160 lo = int(image_size / image_ratio) // BLOCK_SIZE * BLOCK_SIZE - BLOCK_SIZE
161
162 # Ensure lo is small enough: max_image_size should <= image_size.
163 delta = BLOCK_SIZE
164 max_image_size = size_calculator(lo)
165 while max_image_size > image_size:
166 image_ratio = max_image_size / float(lo)
167 lo = int(image_size / image_ratio) // BLOCK_SIZE * BLOCK_SIZE - delta
168 delta *= 2
169 max_image_size = size_calculator(lo)
170
171 hi = lo + BLOCK_SIZE
172
173 # Ensure hi is large enough: max_image_size should >= image_size.
174 delta = BLOCK_SIZE
175 max_image_size = size_calculator(hi)
176 while max_image_size < image_size:
177 image_ratio = max_image_size / float(hi)
178 hi = int(image_size / image_ratio) // BLOCK_SIZE * BLOCK_SIZE + delta
179 delta *= 2
180 max_image_size = size_calculator(hi)
181
182 partition_size = hi
183
184 # Start to binary search.
185 while lo < hi:
186 mid = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE
187 max_image_size = size_calculator(mid)
188 if max_image_size >= image_size: # if mid can accommodate image_size
189 if mid < partition_size: # if a smaller partition size is found
190 partition_size = mid
191 hi = mid
192 else:
193 lo = mid + BLOCK_SIZE
194
195 if OPTIONS.verbose:
196 print("AVBCalcMinPartitionSize({}): partition_size: {}.".format(
197 image_size, partition_size))
198
199 return partition_size
200
201
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800202def AVBAddFooter(image_path, avbtool, footer_type, partition_size,
Tao Bao2b6dfd62017-09-27 17:17:43 -0700203 partition_name, key_path, algorithm, salt,
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800204 additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400205 """Adds dm-verity hashtree and AVB metadata to an image.
206
207 Args:
208 image_path: Path to image to modify.
209 avbtool: String with path to avbtool.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800210 footer_type: 'hash' or 'hashtree' for generating footer.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400211 partition_size: The size of the partition in question.
212 partition_name: The name of the partition - will be embedded in metadata.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800213 key_path: Path to key to use or None.
214 algorithm: Name of algorithm to use or None.
Tao Bao2b6dfd62017-09-27 17:17:43 -0700215 salt: The salt to use (a hexadecimal string) or None.
Bowgo Tsai040410c2018-09-20 16:40:01 +0800216 additional_args: Additional arguments to pass to "avbtool add_hash_footer"
217 or "avbtool add_hashtree_footer".
David Zeuthen4014a9d2016-09-30 17:29:22 -0400218 """
Tao Baoc72727a2017-12-07 10:33:00 -0800219 cmd = [avbtool, "add_%s_footer" % footer_type,
220 "--partition_size", partition_size,
221 "--partition_name", partition_name,
222 "--image", image_path]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800223
224 if key_path and algorithm:
225 cmd.extend(["--key", key_path, "--algorithm", algorithm])
Tao Bao2b6dfd62017-09-27 17:17:43 -0700226 if salt:
227 cmd.extend(["--salt", salt])
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800228
229 cmd.extend(shlex.split(additional_args))
230
Tao Bao986ee862018-10-04 15:46:16 -0700231 common.RunAndCheckOutput(cmd)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400232
Tao Baoc72727a2017-12-07 10:33:00 -0800233
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100234def AdjustPartitionSizeForVerity(partition_size, fec_supported):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700235 """Modifies the provided partition size to account for the verity metadata.
236
237 This information is used to size the created image appropriately.
Tao Baoc72727a2017-12-07 10:33:00 -0800238
Geremy Condrafd6f7512013-06-16 17:26:08 -0700239 Args:
240 partition_size: the size of the partition to be verified.
Tao Baoc72727a2017-12-07 10:33:00 -0800241
Geremy Condrafd6f7512013-06-16 17:26:08 -0700242 Returns:
Sami Tolvanen433905f2016-09-01 15:58:35 -0700243 A tuple of the size of the partition adjusted for verity metadata, and
244 the size of verity metadata.
Geremy Condrafd6f7512013-06-16 17:26:08 -0700245 """
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100246 key = "%d %d" % (partition_size, fec_supported)
247 if key in AdjustPartitionSizeForVerity.results:
248 return AdjustPartitionSizeForVerity.results[key]
249
250 hi = partition_size
251 if hi % BLOCK_SIZE != 0:
252 hi = (hi // BLOCK_SIZE) * BLOCK_SIZE
253
254 # verity tree and fec sizes depend on the partition size, which
255 # means this estimate is always going to be unnecessarily small
Sami Tolvanen433905f2016-09-01 15:58:35 -0700256 verity_size = GetVeritySize(hi, fec_supported)
257 lo = partition_size - verity_size
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100258 result = lo
259
260 # do a binary search for the optimal size
261 while lo < hi:
262 i = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE
Sami Tolvanen433905f2016-09-01 15:58:35 -0700263 v = GetVeritySize(i, fec_supported)
264 if i + v <= partition_size:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100265 if result < i:
266 result = i
Sami Tolvanen433905f2016-09-01 15:58:35 -0700267 verity_size = v
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100268 lo = i + BLOCK_SIZE
269 else:
270 hi = i
271
Tomasz Wasilczyk29ec06b2017-11-15 10:34:01 -0800272 if OPTIONS.verbose:
273 print("Adjusted partition size for verity, partition_size: {},"
274 " verity_size: {}".format(result, verity_size))
Sami Tolvanen433905f2016-09-01 15:58:35 -0700275 AdjustPartitionSizeForVerity.results[key] = (result, verity_size)
276 return (result, verity_size)
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100277
Tao Baoc72727a2017-12-07 10:33:00 -0800278
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100279AdjustPartitionSizeForVerity.results = {}
280
Tao Baoc72727a2017-12-07 10:33:00 -0800281
Sami Tolvanen433905f2016-09-01 15:58:35 -0700282def BuildVerityFEC(sparse_image_path, verity_path, verity_fec_path,
283 padding_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800284 cmd = ["fec", "-e", "-p", str(padding_size), sparse_image_path,
285 verity_path, verity_fec_path]
Tao Bao986ee862018-10-04 15:46:16 -0700286 common.RunAndCheckOutput(cmd)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700287
Tao Baoc72727a2017-12-07 10:33:00 -0800288
Tao Bao2f057462018-10-03 16:31:18 -0700289def BuildVerityTree(sparse_image_path, verity_image_path):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800290 cmd = ["build_verity_tree", "-A", FIXED_SALT, sparse_image_path,
291 verity_image_path]
Tao Bao986ee862018-10-04 15:46:16 -0700292 output = common.RunAndCheckOutput(cmd)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700293 root, salt = output.split()
Tao Bao2f057462018-10-03 16:31:18 -0700294 return root, salt
Geremy Condrafd6f7512013-06-16 17:26:08 -0700295
Tao Baoc72727a2017-12-07 10:33:00 -0800296
Geremy Condrafd6f7512013-06-16 17:26:08 -0700297def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800298 block_device, signer_path, key, signer_args,
299 verity_disable):
Tao Baob4ec6d72018-03-15 23:21:28 -0700300 cmd = ["build_verity_metadata.py", "build", str(image_size),
301 verity_metadata_path, root_hash, salt, block_device, signer_path, key]
Tao Bao45810422016-10-17 16:20:12 -0700302 if signer_args:
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800303 cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),))
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800304 if verity_disable:
305 cmd.append("--verity_disable")
Tao Bao986ee862018-10-04 15:46:16 -0700306 common.RunAndCheckOutput(cmd)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700307
Tao Baoc72727a2017-12-07 10:33:00 -0800308
Geremy Condrafd6f7512013-06-16 17:26:08 -0700309def Append2Simg(sparse_image_path, unsparse_image_path, error_message):
310 """Appends the unsparse image to the given sparse image.
311
312 Args:
313 sparse_image_path: the path to the (sparse) image
314 unsparse_image_path: the path to the (unsparse) image
Tao Baoc6bd70a2018-09-27 16:58:00 -0700315
316 Raises:
317 BuildImageError: On error.
Geremy Condrafd6f7512013-06-16 17:26:08 -0700318 """
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800319 cmd = ["append2simg", sparse_image_path, unsparse_image_path]
Tao Bao986ee862018-10-04 15:46:16 -0700320 try:
321 common.RunAndCheckOutput(cmd)
322 except:
323 raise BuildImageError(error_message)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700324
Tao Baoc72727a2017-12-07 10:33:00 -0800325
Sami Tolvanenff914f52015-12-18 13:24:56 +0000326def Append(target, file_to_append, error_message):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700327 """Appends file_to_append to target.
328
329 Raises:
330 BuildImageError: On error.
331 """
Tao Baoc72727a2017-12-07 10:33:00 -0800332 try:
333 with open(target, "a") as out_file, open(file_to_append, "r") as input_file:
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800334 for line in input_file:
335 out_file.write(line)
Tao Baoc72727a2017-12-07 10:33:00 -0800336 except IOError:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700337 raise BuildImageError(error_message)
Sami Tolvanenff914f52015-12-18 13:24:56 +0000338
Tao Baoc72727a2017-12-07 10:33:00 -0800339
Dan Albert8b72aef2015-03-23 19:13:21 -0700340def BuildVerifiedImage(data_image_path, verity_image_path,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000341 verity_metadata_path, verity_fec_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700342 padding_size, fec_supported):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700343 Append(
344 verity_image_path, verity_metadata_path,
345 "Could not append verity metadata!")
Sami Tolvanen4a060042015-12-18 15:50:25 +0000346
347 if fec_supported:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700348 # Build FEC for the entire partition, including metadata.
349 BuildVerityFEC(
350 data_image_path, verity_image_path, verity_fec_path, padding_size)
351 Append(verity_image_path, verity_fec_path, "Could not append FEC!")
Sami Tolvanen4a060042015-12-18 15:50:25 +0000352
Tao Baoc6bd70a2018-09-27 16:58:00 -0700353 Append2Simg(
354 data_image_path, verity_image_path, "Could not append verity data!")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700355
Tao Baoc72727a2017-12-07 10:33:00 -0800356
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800357def UnsparseImage(sparse_image_path, replace=True):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700358 img_dir = os.path.dirname(sparse_image_path)
359 unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
360 unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
361 if os.path.exists(unsparse_image_path):
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800362 if replace:
363 os.unlink(unsparse_image_path)
364 else:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700365 return unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700366 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
Tao Bao986ee862018-10-04 15:46:16 -0700367 try:
368 common.RunAndCheckOutput(inflate_command)
369 except:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700370 os.remove(unsparse_image_path)
Tao Bao986ee862018-10-04 15:46:16 -0700371 raise
Tao Baoc6bd70a2018-09-27 16:58:00 -0700372 return unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700373
Tao Baoc72727a2017-12-07 10:33:00 -0800374
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100375def MakeVerityEnabledImage(out_file, fec_supported, prop_dict):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700376 """Creates an image that is verifiable using dm-verity.
377
378 Args:
379 out_file: the location to write the verifiable image at
Dan Albert8b72aef2015-03-23 19:13:21 -0700380 prop_dict: a dictionary of properties required for image creation and
381 verification
Tao Baoc6bd70a2018-09-27 16:58:00 -0700382
383 Raises:
384 AssertionError: On invalid partition sizes.
385 BuildImageError: On other errors.
Geremy Condrafd6f7512013-06-16 17:26:08 -0700386 """
387 # get properties
Tao Bao35f4ebc2018-09-27 15:31:11 -0700388 image_size = int(prop_dict["image_size"])
Geremy Condrafd6f7512013-06-16 17:26:08 -0700389 block_dev = prop_dict["verity_block_device"]
Paul Lawrencea37b2bb2014-11-13 17:54:30 -0800390 signer_key = prop_dict["verity_key"] + ".pk8"
Baligh Uddin601ddea2015-06-09 15:48:14 -0700391 if OPTIONS.verity_signer_path is not None:
Tao Bao45810422016-10-17 16:20:12 -0700392 signer_path = OPTIONS.verity_signer_path
Baligh Uddin601ddea2015-06-09 15:48:14 -0700393 else:
394 signer_path = prop_dict["verity_signer_cmd"]
Tao Bao45810422016-10-17 16:20:12 -0700395 signer_args = OPTIONS.verity_signer_args
Geremy Condrafd6f7512013-06-16 17:26:08 -0700396
Tao Bao1c830bf2017-12-25 10:43:47 -0800397 tempdir_name = common.MakeTempDir(suffix="_verity_images")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700398
Tao Baoc6bd70a2018-09-27 16:58:00 -0700399 # Get partial image paths.
Geremy Condrafd6f7512013-06-16 17:26:08 -0700400 verity_image_path = os.path.join(tempdir_name, "verity.img")
401 verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img")
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100402 verity_fec_path = os.path.join(tempdir_name, "verity_fec.img")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700403
Tao Baoc6bd70a2018-09-27 16:58:00 -0700404 # Build the verity tree and get the root hash and salt.
Tao Bao2f057462018-10-03 16:31:18 -0700405 root_hash, salt = BuildVerityTree(out_file, verity_image_path)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700406
Tao Baoc6bd70a2018-09-27 16:58:00 -0700407 # Build the metadata blocks.
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800408 verity_disable = "verity_disable" in prop_dict
Tao Baoc6bd70a2018-09-27 16:58:00 -0700409 BuildVerityMetadata(
410 image_size, verity_metadata_path, root_hash, salt, block_dev, signer_path,
411 signer_key, signer_args, verity_disable)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700412
Tao Baoc6bd70a2018-09-27 16:58:00 -0700413 # Build the full verified image.
Tao Bao35f4ebc2018-09-27 15:31:11 -0700414 partition_size = int(prop_dict["partition_size"])
Sami Tolvanen433905f2016-09-01 15:58:35 -0700415 verity_size = int(prop_dict["verity_size"])
416
Tao Bao35f4ebc2018-09-27 15:31:11 -0700417 padding_size = partition_size - image_size - verity_size
Sami Tolvanen433905f2016-09-01 15:58:35 -0700418 assert padding_size >= 0
419
Tao Baoc6bd70a2018-09-27 16:58:00 -0700420 BuildVerifiedImage(
421 out_file, verity_image_path, verity_metadata_path, verity_fec_path,
422 padding_size, fec_supported)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700423
Tao Baoc72727a2017-12-07 10:33:00 -0800424
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800425def ConvertBlockMapToBaseFs(block_map_file):
Tao Bao1c830bf2017-12-25 10:43:47 -0800426 base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800427 convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file]
Tao Bao986ee862018-10-04 15:46:16 -0700428 common.RunAndCheckOutput(convert_command)
Tao Baoc6bd70a2018-09-27 16:58:00 -0700429 return base_fs_file
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800430
Tao Baod4349f22017-12-07 23:01:25 -0800431
Tao Baoc2606eb2018-07-20 14:44:46 -0700432def SetUpInDirAndFsConfig(origin_in, prop_dict):
433 """Returns the in_dir and fs_config that should be used for image building.
434
Tom Cherryd14b8952018-08-09 14:26:00 -0700435 When building system.img for all targets, it creates and returns a staged dir
436 that combines the contents of /system (i.e. in the given in_dir) and root.
Tao Baoc2606eb2018-07-20 14:44:46 -0700437
438 Args:
439 origin_in: Path to the input directory.
440 prop_dict: A property dict that contains info like partition size. Values
441 may be updated.
442
443 Returns:
444 A tuple of in_dir and fs_config that should be used to build the image.
445 """
446 fs_config = prop_dict.get("fs_config")
Tom Cherryd14b8952018-08-09 14:26:00 -0700447
448 if prop_dict["mount_point"] == "system_other":
449 prop_dict["mount_point"] = "system"
450 return origin_in, fs_config
451
452 if prop_dict["mount_point"] != "system":
Tao Baoc2606eb2018-07-20 14:44:46 -0700453 return origin_in, fs_config
454
455 # Construct a staging directory of the root file system.
456 in_dir = common.MakeTempDir()
457 root_dir = prop_dict.get("root_dir")
458 if root_dir:
459 shutil.rmtree(in_dir)
460 shutil.copytree(root_dir, in_dir, symlinks=True)
461 in_dir_system = os.path.join(in_dir, "system")
462 shutil.rmtree(in_dir_system, ignore_errors=True)
463 shutil.copytree(origin_in, in_dir_system, symlinks=True)
464
465 # Change the mount point to "/".
466 prop_dict["mount_point"] = "/"
467 if fs_config:
468 # We need to merge the fs_config files of system and root.
469 merged_fs_config = common.MakeTempFile(
470 prefix="merged_fs_config", suffix=".txt")
471 with open(merged_fs_config, "w") as fw:
472 if "root_fs_config" in prop_dict:
473 with open(prop_dict["root_fs_config"]) as fr:
474 fw.writelines(fr.readlines())
475 with open(fs_config) as fr:
476 fw.writelines(fr.readlines())
477 fs_config = merged_fs_config
478 return in_dir, fs_config
479
480
Tao Baod4349f22017-12-07 23:01:25 -0800481def CheckHeadroom(ext4fs_output, prop_dict):
482 """Checks if there's enough headroom space available.
483
484 Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM),
485 which is useful for devices with low disk space that have system image
486 variation between builds. The 'partition_headroom' in prop_dict is the size
487 in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks.
488
489 Args:
490 ext4fs_output: The output string from mke2fs command.
491 prop_dict: The property dict.
492
Tao Baod8a953d2018-01-02 21:19:27 -0800493 Raises:
494 AssertionError: On invalid input.
Tao Baoc6bd70a2018-09-27 16:58:00 -0700495 BuildImageError: On check failure.
Tao Baod4349f22017-12-07 23:01:25 -0800496 """
Tao Baod8a953d2018-01-02 21:19:27 -0800497 assert ext4fs_output is not None
498 assert prop_dict.get('fs_type', '').startswith('ext4')
499 assert 'partition_headroom' in prop_dict
500 assert 'mount_point' in prop_dict
501
Tao Baod4349f22017-12-07 23:01:25 -0800502 ext4fs_stats = re.compile(
503 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
504 r'(?P<total_blocks>[0-9]+) blocks')
Tao Baoc72727a2017-12-07 10:33:00 -0800505 last_line = ext4fs_output.strip().split('\n')[-1]
506 m = ext4fs_stats.match(last_line)
Tao Baod4349f22017-12-07 23:01:25 -0800507 used_blocks = int(m.groupdict().get('used_blocks'))
508 total_blocks = int(m.groupdict().get('total_blocks'))
Tao Baod8a953d2018-01-02 21:19:27 -0800509 headroom_blocks = int(prop_dict['partition_headroom']) / BLOCK_SIZE
Tao Baod4349f22017-12-07 23:01:25 -0800510 adjusted_blocks = total_blocks - headroom_blocks
511 if used_blocks > adjusted_blocks:
Tao Baod8a953d2018-01-02 21:19:27 -0800512 mount_point = prop_dict["mount_point"]
Tao Baoc6bd70a2018-09-27 16:58:00 -0700513 raise BuildImageError(
514 "Error: Not enough room on {} (total: {} blocks, used: {} blocks, "
515 "headroom: {} blocks, available: {} blocks)".format(
516 mount_point, total_blocks, used_blocks, headroom_blocks,
517 adjusted_blocks))
Tao Baod4349f22017-12-07 23:01:25 -0800518
519
Thierry Strudel74a81e62015-07-09 09:54:55 -0700520def BuildImage(in_dir, prop_dict, out_file, target_out=None):
Tao Baoc2606eb2018-07-20 14:44:46 -0700521 """Builds an image for the files under in_dir and writes it to out_file.
522
Ying Wangbd93d422011-10-28 17:02:30 -0700523 Args:
Tao Baoc2606eb2018-07-20 14:44:46 -0700524 in_dir: Path to input directory.
525 prop_dict: A property dict that contains info like partition size. Values
526 will be updated with computed values.
527 out_file: The output image file.
528 target_out: Path to the TARGET_OUT directory as in Makefile. It actually
529 points to the /system directory under PRODUCT_OUT. fs_config (the one
530 under system/core/libcutils) reads device specific FS config files from
531 there.
Ying Wangbd93d422011-10-28 17:02:30 -0700532
Tao Baoc6bd70a2018-09-27 16:58:00 -0700533 Raises:
534 BuildImageError: On build image failures.
Ying Wangbd93d422011-10-28 17:02:30 -0700535 """
Tao Baoc2606eb2018-07-20 14:44:46 -0700536 in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict)
Ying Wanga2292c92015-03-24 19:07:40 -0700537
Ying Wangbd93d422011-10-28 17:02:30 -0700538 build_command = []
539 fs_type = prop_dict.get("fs_type", "")
Tao Baoc72727a2017-12-07 10:33:00 -0800540 run_e2fsck = False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700541
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700542 fs_spans_partition = True
543 if fs_type.startswith("squash"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700544 fs_spans_partition = False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700545
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700546 is_verity_partition = "verity_block_device" in prop_dict
Geremy Condra5b5f4952014-05-05 22:19:37 -0700547 verity_supported = prop_dict.get("verity") == "true"
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100548 verity_fec_supported = prop_dict.get("verity_fec") == "true"
549
Bowgo Tsai040410c2018-09-20 16:40:01 +0800550 avb_footer_type = None
551 if prop_dict.get("avb_hash_enable") == "true":
552 avb_footer_type = "hash"
553 elif prop_dict.get("avb_hashtree_enable") == "true":
554 avb_footer_type = "hashtree"
555
556 if avb_footer_type:
557 avbtool = prop_dict.get("avb_avbtool")
558 avb_signing_args = prop_dict.get(
559 "avb_add_" + avb_footer_type + "_footer_args")
560
Yifan Hong2dae5722018-07-31 12:47:27 -0700561 if (prop_dict.get("use_dynamic_partition_size") == "true" and
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700562 "partition_size" not in prop_dict):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700563 # If partition_size is not defined, use output of `du' + reserved_size.
564 size = GetDiskUsage(in_dir)
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700565 if OPTIONS.verbose:
Tao Baoc2606eb2018-07-20 14:44:46 -0700566 print("The tree size of %s is %d MB." % (in_dir, size // BYTES_IN_MB))
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700567 size += int(prop_dict.get("partition_reserved_size", 0))
568 # Round this up to a multiple of 4K so that avbtool works
569 size = common.RoundUpTo4K(size)
Bowgo Tsai040410c2018-09-20 16:40:01 +0800570 # Adjust partition_size to add more space for AVB footer, to prevent
571 # it from consuming partition_reserved_size.
572 if avb_footer_type:
573 size = AVBCalcMinPartitionSize(
574 size,
575 lambda x: AVBCalcMaxImageSize(
576 avbtool, avb_footer_type, x, avb_signing_args))
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700577 prop_dict["partition_size"] = str(size)
578 if OPTIONS.verbose:
579 print("Allocating %d MB for %s." % (size // BYTES_IN_MB, out_file))
580
Tao Bao35f4ebc2018-09-27 15:31:11 -0700581 prop_dict["image_size"] = prop_dict["partition_size"]
582
583 # Adjust the image size to make room for the hashes if this is to be verified.
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800584 if verity_supported and is_verity_partition:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700585 partition_size = int(prop_dict.get("partition_size"))
Tao Bao35f4ebc2018-09-27 15:31:11 -0700586 image_size, verity_size = AdjustPartitionSizeForVerity(
Tao Baoc72727a2017-12-07 10:33:00 -0800587 partition_size, verity_fec_supported)
Tao Bao35f4ebc2018-09-27 15:31:11 -0700588 prop_dict["image_size"] = str(image_size)
Sami Tolvanen433905f2016-09-01 15:58:35 -0700589 prop_dict["verity_size"] = str(verity_size)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700590
Tao Bao35f4ebc2018-09-27 15:31:11 -0700591 # Adjust the image size for AVB hash footer or AVB hashtree footer.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800592 if avb_footer_type:
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800593 partition_size = prop_dict["partition_size"]
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800594 # avb_add_hash_footer_args or avb_add_hashtree_footer_args.
Tao Baoc6bd70a2018-09-27 16:58:00 -0700595 max_image_size = AVBCalcMaxImageSize(
596 avbtool, avb_footer_type, partition_size, avb_signing_args)
Tao Bao35f4ebc2018-09-27 15:31:11 -0700597 prop_dict["image_size"] = str(max_image_size)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400598
Ying Wangbd93d422011-10-28 17:02:30 -0700599 if fs_type.startswith("ext"):
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800600 build_command = [prop_dict["ext_mkuserimg"]]
Ying Wangbd93d422011-10-28 17:02:30 -0700601 if "extfs_sparse_flag" in prop_dict:
602 build_command.append(prop_dict["extfs_sparse_flag"])
Tao Baoc72727a2017-12-07 10:33:00 -0800603 run_e2fsck = True
Ying Wangbd93d422011-10-28 17:02:30 -0700604 build_command.extend([in_dir, out_file, fs_type,
605 prop_dict["mount_point"]])
Tao Bao35f4ebc2018-09-27 15:31:11 -0700606 build_command.append(prop_dict["image_size"])
Ying Wangf3b86352014-11-18 18:03:13 -0800607 if "journal_size" in prop_dict:
608 build_command.extend(["-j", prop_dict["journal_size"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800609 if "timestamp" in prop_dict:
610 build_command.extend(["-T", str(prop_dict["timestamp"])])
Ying Wanga2292c92015-03-24 19:07:40 -0700611 if fs_config:
Doug Zongker82822822014-06-16 09:10:55 -0700612 build_command.extend(["-C", fs_config])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700613 if target_out:
614 build_command.extend(["-D", target_out])
Ying Wanga2292c92015-03-24 19:07:40 -0700615 if "block_list" in prop_dict:
616 build_command.extend(["-B", prop_dict["block_list"]])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800617 if "base_fs_file" in prop_dict:
618 base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800619 build_command.extend(["-d", base_fs_file])
Christoffer Dall8ed01f32014-12-17 21:34:12 +0100620 build_command.extend(["-L", prop_dict["mount_point"]])
Patrick Tjina1900842016-10-20 10:58:12 -0700621 if "extfs_inode_count" in prop_dict:
622 build_command.extend(["-i", prop_dict["extfs_inode_count"]])
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700623 if "extfs_rsv_pct" in prop_dict:
624 build_command.extend(["-M", prop_dict["extfs_rsv_pct"]])
Connor O'Brien20f08c32017-01-05 16:48:14 -0800625 if "flash_erase_block_size" in prop_dict:
626 build_command.extend(["-e", prop_dict["flash_erase_block_size"]])
627 if "flash_logical_block_size" in prop_dict:
628 build_command.extend(["-o", prop_dict["flash_logical_block_size"]])
Tao Baod86e3112017-09-22 15:45:33 -0700629 # Specify UUID and hash_seed if using mke2fs.
Tianjie Xu57332222018-08-15 16:16:21 -0700630 if prop_dict["ext_mkuserimg"] == "mkuserimg_mke2fs":
Tao Baod86e3112017-09-22 15:45:33 -0700631 if "uuid" in prop_dict:
632 build_command.extend(["-U", prop_dict["uuid"]])
633 if "hash_seed" in prop_dict:
634 build_command.extend(["-S", prop_dict["hash_seed"]])
Jin Qianfde9f792018-01-22 13:15:46 -0800635 if "ext4_share_dup_blocks" in prop_dict:
636 build_command.append("-c")
Ying Wanga2292c92015-03-24 19:07:40 -0700637 if "selinux_fc" in prop_dict:
Kenny Rootf32dc712012-04-08 10:42:34 -0700638 build_command.append(prop_dict["selinux_fc"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800639 elif fs_type.startswith("squash"):
640 build_command = ["mksquashfsimage.sh"]
641 build_command.extend([in_dir, out_file])
Todd Poynorb2a555e2015-12-15 18:00:14 -0800642 if "squashfs_sparse_flag" in prop_dict:
643 build_command.extend([prop_dict["squashfs_sparse_flag"]])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800644 build_command.extend(["-m", prop_dict["mount_point"]])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700645 if target_out:
646 build_command.extend(["-d", target_out])
Mohamad Ayyash88378822016-04-07 22:10:51 -0700647 if fs_config:
648 build_command.extend(["-C", fs_config])
Ying Wanga2292c92015-03-24 19:07:40 -0700649 if "selinux_fc" in prop_dict:
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800650 build_command.extend(["-c", prop_dict["selinux_fc"]])
Mohamad Ayyashc3484f72016-06-13 09:46:58 -0700651 if "block_list" in prop_dict:
652 build_command.extend(["-B", prop_dict["block_list"]])
Ng Zhi An9446c1d2018-01-19 15:51:46 -0800653 if "squashfs_block_size" in prop_dict:
654 build_command.extend(["-b", prop_dict["squashfs_block_size"]])
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700655 if "squashfs_compressor" in prop_dict:
656 build_command.extend(["-z", prop_dict["squashfs_compressor"]])
657 if "squashfs_compressor_opt" in prop_dict:
658 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
Tao Baoc72727a2017-12-07 10:33:00 -0800659 if prop_dict.get("squashfs_disable_4k_align") == "true":
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700660 build_command.extend(["-a"])
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700661 elif fs_type.startswith("f2fs"):
662 build_command = ["mkf2fsuserimg.sh"]
Tao Bao35f4ebc2018-09-27 15:31:11 -0700663 build_command.extend([out_file, prop_dict["image_size"]])
Jaegeuk Kim2ea1eba2017-11-28 19:21:28 -0800664 if fs_config:
665 build_command.extend(["-C", fs_config])
666 build_command.extend(["-f", in_dir])
667 if target_out:
668 build_command.extend(["-D", target_out])
669 if "selinux_fc" in prop_dict:
670 build_command.extend(["-s", prop_dict["selinux_fc"]])
671 build_command.extend(["-t", prop_dict["mount_point"]])
672 if "timestamp" in prop_dict:
673 build_command.extend(["-T", str(prop_dict["timestamp"])])
674 build_command.extend(["-L", prop_dict["mount_point"]])
Ying Wangbd93d422011-10-28 17:02:30 -0700675 else:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700676 raise BuildImageError(
677 "Error: unknown filesystem type: {}".format(fs_type))
Ying Wangbd93d422011-10-28 17:02:30 -0700678
Tao Bao986ee862018-10-04 15:46:16 -0700679 try:
680 mkfs_output = common.RunAndCheckOutput(build_command)
681 except:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700682 try:
683 du = GetDiskUsage(in_dir)
684 du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB)
Tao Bao986ee862018-10-04 15:46:16 -0700685 # Suppress any errors from GetDiskUsage() to avoid hiding the real errors
686 # from common.RunAndCheckOutput().
687 except Exception as e: # pylint: disable=broad-except
Tao Baoc6bd70a2018-09-27 16:58:00 -0700688 print(e, file=sys.stderr)
689 du_str = "unknown"
Tao Bao4251fe92018-07-23 13:05:00 -0700690 print(
691 "Out of space? The tree size of {} is {}, with reserved space of {} "
692 "bytes ({} MB).".format(
Tao Baoc2606eb2018-07-20 14:44:46 -0700693 in_dir, du_str,
Tao Bao4251fe92018-07-23 13:05:00 -0700694 int(prop_dict.get("partition_reserved_size", 0)),
695 int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB))
Tao Bao35f4ebc2018-09-27 15:31:11 -0700696 print(
697 "The max image size for filsystem files is {} bytes ({} MB), out of a "
698 "total partition size of {} bytes ({} MB).".format(
699 int(prop_dict["image_size"]),
700 int(prop_dict["image_size"]) // BYTES_IN_MB,
701 int(prop_dict["partition_size"]),
702 int(prop_dict["partition_size"]) // BYTES_IN_MB))
Tao Bao986ee862018-10-04 15:46:16 -0700703 raise
Ying Wang69e9b4d2012-11-26 18:10:23 -0800704
Tao Baod4349f22017-12-07 23:01:25 -0800705 # Check if there's enough headroom space available for ext4 image.
Tao Bao79d52f82017-12-07 14:07:44 -0800706 if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
Tao Baoc6bd70a2018-09-27 16:58:00 -0700707 CheckHeadroom(mkfs_output, prop_dict)
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700708
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700709 if not fs_spans_partition:
710 mount_point = prop_dict.get("mount_point")
Tao Bao35f4ebc2018-09-27 15:31:11 -0700711 image_size = int(prop_dict["image_size"])
712 sparse_image_size = GetSimgSize(out_file)
713 if sparse_image_size > image_size:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700714 raise BuildImageError(
715 "Error: {} image size of {} is larger than partition size of "
716 "{}".format(mount_point, sparse_image_size, image_size))
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700717 if verity_supported and is_verity_partition:
Tao Bao35f4ebc2018-09-27 15:31:11 -0700718 ZeroPadSimg(out_file, image_size - sparse_image_size)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700719
Tao Baoc72727a2017-12-07 10:33:00 -0800720 # Create the verified image if this is to be verified.
Geremy Condra5b5f4952014-05-05 22:19:37 -0700721 if verity_supported and is_verity_partition:
Tao Baoc6bd70a2018-09-27 16:58:00 -0700722 MakeVerityEnabledImage(out_file, verity_fec_supported, prop_dict)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700723
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800724 # Add AVB HASH or HASHTREE footer (metadata).
725 if avb_footer_type:
Tao Bao35f4ebc2018-09-27 15:31:11 -0700726 partition_size = prop_dict["partition_size"]
David Zeuthen4014a9d2016-09-30 17:29:22 -0400727 partition_name = prop_dict["partition_name"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800728 # key_path and algorithm are only available when chain partition is used.
729 key_path = prop_dict.get("avb_key_path")
730 algorithm = prop_dict.get("avb_algorithm")
Tao Bao2b6dfd62017-09-27 17:17:43 -0700731 salt = prop_dict.get("avb_salt")
Tao Baoc6bd70a2018-09-27 16:58:00 -0700732 AVBAddFooter(
733 out_file, avbtool, avb_footer_type, partition_size, partition_name,
734 key_path, algorithm, salt, avb_signing_args)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400735
Tao Baoc72727a2017-12-07 10:33:00 -0800736 if run_e2fsck and prop_dict.get("skip_fsck") != "true":
Tao Baoc6bd70a2018-09-27 16:58:00 -0700737 unsparse_image = UnsparseImage(out_file, replace=False)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800738
739 # Run e2fsck on the inflated image file
740 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
Isaac Chenec7fa1c2018-08-02 14:02:56 +0800741 # TODO(b/112062612): work around e2fsck failure with SANITIZE_HOST=address
Tao Bao986ee862018-10-04 15:46:16 -0700742 env4e2fsck = os.environ.copy()
743 env4e2fsck["ASAN_OPTIONS"] = "detect_odr_violation=0"
744 try:
745 common.RunAndCheckOutput(e2fsck_command, env=env4e2fsck)
746 finally:
747 os.remove(unsparse_image)
Ying Wangbd93d422011-10-28 17:02:30 -0700748
749
750def ImagePropFromGlobalDict(glob_dict, mount_point):
751 """Build an image property dictionary from the global dictionary.
752
753 Args:
754 glob_dict: the global dictionary from the build system.
755 mount_point: such as "system", "data" etc.
756 """
Doug Zongker1ad7ade2013-12-06 11:53:27 -0800757 d = {}
Tao Bao052ae352015-09-28 13:44:13 -0700758
Tao Bao822f5842015-09-30 16:01:14 -0700759 if "build.prop" in glob_dict:
760 bp = glob_dict["build.prop"]
761 if "ro.build.date.utc" in bp:
762 d["timestamp"] = bp["ro.build.date.utc"]
Ying Wang9f8e8db2011-11-04 11:37:01 -0700763
764 def copy_prop(src_p, dest_p):
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700765 """Copy a property from the global dictionary.
766
767 Args:
768 src_p: The source property in the global dictionary.
769 dest_p: The destination property.
770 Returns:
771 True if property was found and copied, False otherwise.
772 """
Ying Wang9f8e8db2011-11-04 11:37:01 -0700773 if src_p in glob_dict:
774 d[dest_p] = str(glob_dict[src_p])
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700775 return True
776 return False
Ying Wang9f8e8db2011-11-04 11:37:01 -0700777
Ying Wangbd93d422011-10-28 17:02:30 -0700778 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -0700779 "extfs_sparse_flag",
Todd Poynorb2a555e2015-12-15 18:00:14 -0800780 "squashfs_sparse_flag",
Kenny Rootf32dc712012-04-08 10:42:34 -0700781 "selinux_fc",
Ying Wang6a42a252013-02-27 13:54:02 -0800782 "skip_fsck",
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800783 "ext_mkuserimg",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700784 "verity",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700785 "verity_key",
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100786 "verity_signer_cmd",
David Zeuthen4014a9d2016-09-30 17:29:22 -0400787 "verity_fec",
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800788 "verity_disable",
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800789 "avb_enable",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700790 "avb_avbtool",
791 "avb_salt",
Yifan Hong2dae5722018-07-31 12:47:27 -0700792 "use_dynamic_partition_size",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700793 )
Ying Wangbd93d422011-10-28 17:02:30 -0700794 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -0700795 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -0700796
797 d["mount_point"] = mount_point
798 if mount_point == "system":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800799 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
800 copy_prop("avb_system_add_hashtree_footer_args",
801 "avb_add_hashtree_footer_args")
802 copy_prop("avb_system_key_path", "avb_key_path")
803 copy_prop("avb_system_algorithm", "avb_algorithm")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700804 copy_prop("fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700805 # Copy the generic system fs type first, override with specific one if
Dan Albert8b72aef2015-03-23 19:13:21 -0700806 # available.
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800807 copy_prop("system_fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700808 copy_prop("system_headroom", "partition_headroom")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700809 copy_prop("system_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700810 if not copy_prop("system_journal_size", "journal_size"):
811 d["journal_size"] = "0"
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700812 copy_prop("system_verity_block_device", "verity_block_device")
Tao Baof3282b42015-04-01 11:21:55 -0700813 copy_prop("system_root_image", "system_root_image")
Tao Bao8bfd3c72018-07-20 15:20:28 -0700814 copy_prop("root_dir", "root_dir")
815 copy_prop("root_fs_config", "root_fs_config")
Jin Qianfde9f792018-01-22 13:15:46 -0800816 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700817 copy_prop("system_squashfs_compressor", "squashfs_compressor")
818 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700819 copy_prop("system_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700820 copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800821 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700822 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700823 if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
824 d["extfs_rsv_pct"] = "0"
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700825 copy_prop("system_reserved_size", "partition_reserved_size")
Alex Light4e358ab2016-06-16 14:47:10 -0700826 elif mount_point == "system_other":
Tao Baoc72727a2017-12-07 10:33:00 -0800827 # We inherit the selinux policies of /system since we contain some of its
828 # files.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800829 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
830 copy_prop("avb_system_add_hashtree_footer_args",
831 "avb_add_hashtree_footer_args")
832 copy_prop("avb_system_key_path", "avb_key_path")
833 copy_prop("avb_system_algorithm", "avb_algorithm")
Alex Light4e358ab2016-06-16 14:47:10 -0700834 copy_prop("fs_type", "fs_type")
835 copy_prop("system_fs_type", "fs_type")
836 copy_prop("system_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700837 if not copy_prop("system_journal_size", "journal_size"):
838 d["journal_size"] = "0"
Alex Light4e358ab2016-06-16 14:47:10 -0700839 copy_prop("system_verity_block_device", "verity_block_device")
Alex Light4e358ab2016-06-16 14:47:10 -0700840 copy_prop("system_squashfs_compressor", "squashfs_compressor")
841 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
842 copy_prop("system_squashfs_block_size", "squashfs_block_size")
843 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700844 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700845 if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
846 d["extfs_rsv_pct"] = "0"
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700847 copy_prop("system_reserved_size", "partition_reserved_size")
Ying Wangbd93d422011-10-28 17:02:30 -0700848 elif mount_point == "data":
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700849 # Copy the generic fs type first, override with specific one if available.
Ying Wang9f8e8db2011-11-04 11:37:01 -0700850 copy_prop("fs_type", "fs_type")
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700851 copy_prop("userdata_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700852 copy_prop("userdata_size", "partition_size")
Tao Baoc72727a2017-12-07 10:33:00 -0800853 copy_prop("flash_logical_block_size", "flash_logical_block_size")
Connor O'Brien20f08c32017-01-05 16:48:14 -0800854 copy_prop("flash_erase_block_size", "flash_erase_block_size")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700855 elif mount_point == "cache":
856 copy_prop("cache_fs_type", "fs_type")
857 copy_prop("cache_size", "partition_size")
Ying Wanga0febe52013-03-20 11:02:05 -0700858 elif mount_point == "vendor":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800859 copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable")
860 copy_prop("avb_vendor_add_hashtree_footer_args",
861 "avb_add_hashtree_footer_args")
862 copy_prop("avb_vendor_key_path", "avb_key_path")
863 copy_prop("avb_vendor_algorithm", "avb_algorithm")
Ying Wanga0febe52013-03-20 11:02:05 -0700864 copy_prop("vendor_fs_type", "fs_type")
865 copy_prop("vendor_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700866 if not copy_prop("vendor_journal_size", "journal_size"):
867 d["journal_size"] = "0"
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700868 copy_prop("vendor_verity_block_device", "verity_block_device")
Jin Qianfde9f792018-01-22 13:15:46 -0800869 copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
Patrick Tjine11aa502016-02-09 15:40:38 -0800870 copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
871 copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700872 copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700873 copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800874 copy_prop("vendor_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700875 copy_prop("vendor_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700876 if not copy_prop("vendor_extfs_rsv_pct", "extfs_rsv_pct"):
877 d["extfs_rsv_pct"] = "0"
Yifan Hong749062d2018-06-19 16:23:16 -0700878 copy_prop("vendor_reserved_size", "partition_reserved_size")
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900879 elif mount_point == "product":
880 copy_prop("avb_product_hashtree_enable", "avb_hashtree_enable")
881 copy_prop("avb_product_add_hashtree_footer_args",
882 "avb_add_hashtree_footer_args")
883 copy_prop("avb_product_key_path", "avb_key_path")
884 copy_prop("avb_product_algorithm", "avb_algorithm")
885 copy_prop("product_fs_type", "fs_type")
886 copy_prop("product_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700887 if not copy_prop("product_journal_size", "journal_size"):
888 d["journal_size"] = "0"
Jaekyun Seokb7735d82017-11-27 17:04:47 +0900889 copy_prop("product_verity_block_device", "verity_block_device")
890 copy_prop("product_squashfs_compressor", "squashfs_compressor")
891 copy_prop("product_squashfs_compressor_opt", "squashfs_compressor_opt")
892 copy_prop("product_squashfs_block_size", "squashfs_block_size")
893 copy_prop("product_squashfs_disable_4k_align", "squashfs_disable_4k_align")
894 copy_prop("product_base_fs_file", "base_fs_file")
895 copy_prop("product_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700896 if not copy_prop("product_extfs_rsv_pct", "extfs_rsv_pct"):
897 d["extfs_rsv_pct"] = "0"
Yifan Hong56a6c3b2018-07-20 15:19:34 -0700898 copy_prop("product_reserved_size", "partition_reserved_size")
Dario Freni924af7d2018-08-17 00:56:14 +0100899 elif mount_point == "product_services":
Yifan Hongebc041a2018-07-26 16:02:52 -0700900 copy_prop("avb_product_services_hashtree_enable", "avb_hashtree_enable")
901 copy_prop("avb_product_services_add_hashtree_footer_args",
Dario Freni5f681e12018-05-29 13:09:01 +0100902 "avb_add_hashtree_footer_args")
Yifan Hongebc041a2018-07-26 16:02:52 -0700903 copy_prop("avb_product_services_key_path", "avb_key_path")
904 copy_prop("avb_product_services_algorithm", "avb_algorithm")
905 copy_prop("product_services_fs_type", "fs_type")
906 copy_prop("product_services_size", "partition_size")
907 if not copy_prop("product_services_journal_size", "journal_size"):
Dario Freni5f681e12018-05-29 13:09:01 +0100908 d["journal_size"] = "0"
Yifan Hongebc041a2018-07-26 16:02:52 -0700909 copy_prop("product_services_verity_block_device", "verity_block_device")
910 copy_prop("product_services_squashfs_compressor", "squashfs_compressor")
911 copy_prop("product_services_squashfs_compressor_opt",
Dario Freni5f681e12018-05-29 13:09:01 +0100912 "squashfs_compressor_opt")
Yifan Hongebc041a2018-07-26 16:02:52 -0700913 copy_prop("product_services_squashfs_block_size", "squashfs_block_size")
914 copy_prop("product_services_squashfs_disable_4k_align",
Dario Freni5f681e12018-05-29 13:09:01 +0100915 "squashfs_disable_4k_align")
Yifan Hongebc041a2018-07-26 16:02:52 -0700916 copy_prop("product_services_base_fs_file", "base_fs_file")
917 copy_prop("product_services_extfs_inode_count", "extfs_inode_count")
918 if not copy_prop("product_services_extfs_rsv_pct", "extfs_rsv_pct"):
Dario Freni5f681e12018-05-29 13:09:01 +0100919 d["extfs_rsv_pct"] = "0"
Yifan Hongebc041a2018-07-26 16:02:52 -0700920 copy_prop("product_services_reserved_size", "partition_reserved_size")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800921 elif mount_point == "odm":
922 copy_prop("avb_odm_hashtree_enable", "avb_hashtree_enable")
923 copy_prop("avb_odm_add_hashtree_footer_args",
924 "avb_add_hashtree_footer_args")
925 copy_prop("avb_odm_key_path", "avb_key_path")
926 copy_prop("avb_odm_algorithm", "avb_algorithm")
927 copy_prop("odm_fs_type", "fs_type")
928 copy_prop("odm_size", "partition_size")
929 if not copy_prop("odm_journal_size", "journal_size"):
930 d["journal_size"] = "0"
931 copy_prop("odm_verity_block_device", "verity_block_device")
932 copy_prop("odm_squashfs_compressor", "squashfs_compressor")
933 copy_prop("odm_squashfs_compressor_opt", "squashfs_compressor_opt")
934 copy_prop("odm_squashfs_block_size", "squashfs_block_size")
935 copy_prop("odm_squashfs_disable_4k_align", "squashfs_disable_4k_align")
936 copy_prop("odm_base_fs_file", "base_fs_file")
937 copy_prop("odm_extfs_inode_count", "extfs_inode_count")
938 if not copy_prop("odm_extfs_rsv_pct", "extfs_rsv_pct"):
939 d["extfs_rsv_pct"] = "0"
940 copy_prop("odm_reserved_size", "partition_reserved_size")
Ying Wangb8888432014-03-11 17:13:27 -0700941 elif mount_point == "oem":
942 copy_prop("fs_type", "fs_type")
943 copy_prop("oem_size", "partition_size")
Tao Bao332a96b2018-03-31 10:27:35 -0700944 if not copy_prop("oem_journal_size", "journal_size"):
945 d["journal_size"] = "0"
Patrick Tjina1900842016-10-20 10:58:12 -0700946 copy_prop("oem_extfs_inode_count", "extfs_inode_count")
Patrick Tjin3f5f9932018-03-23 11:36:43 -0700947 if not copy_prop("oem_extfs_rsv_pct", "extfs_rsv_pct"):
948 d["extfs_rsv_pct"] = "0"
David Zeuthen4014a9d2016-09-30 17:29:22 -0400949 d["partition_name"] = mount_point
Ying Wangbd93d422011-10-28 17:02:30 -0700950 return d
951
952
953def LoadGlobalDict(filename):
954 """Load "name=value" pairs from filename"""
955 d = {}
956 f = open(filename)
957 for line in f:
958 line = line.strip()
959 if not line or line.startswith("#"):
960 continue
961 k, v = line.split("=", 1)
962 d[k] = v
963 f.close()
964 return d
965
966
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700967def GlobalDictFromImageProp(image_prop, mount_point):
968 d = {}
969 def copy_prop(src_p, dest_p):
970 if src_p in image_prop:
971 d[dest_p] = image_prop[src_p]
972 return True
973 return False
Tao Bao4251fe92018-07-23 13:05:00 -0700974
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700975 if mount_point == "system":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700976 copy_prop("partition_size", "system_size")
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700977 elif mount_point == "system_other":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700978 copy_prop("partition_size", "system_size")
Yifan Hong749062d2018-06-19 16:23:16 -0700979 elif mount_point == "vendor":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700980 copy_prop("partition_size", "vendor_size")
Bowgo Tsaid624fa62017-11-14 23:42:30 +0800981 elif mount_point == "odm":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700982 copy_prop("partition_size", "odm_size")
Yifan Hong56a6c3b2018-07-20 15:19:34 -0700983 elif mount_point == "product":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700984 copy_prop("partition_size", "product_size")
Dario Freni924af7d2018-08-17 00:56:14 +0100985 elif mount_point == "product_services":
Tao Bao35f4ebc2018-09-27 15:31:11 -0700986 copy_prop("partition_size", "product_services_size")
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700987 return d
988
989
990def SaveGlobalDict(filename, glob_dict):
991 with open(filename, "w") as f:
992 f.writelines(["%s=%s" % (key, value) for (key, value) in glob_dict.items()])
993
994
Ying Wangbd93d422011-10-28 17:02:30 -0700995def main(argv):
Yifan Hongbbcba1e2018-06-18 16:32:35 -0700996 if len(argv) < 4 or len(argv) > 5:
Tao Baoc72727a2017-12-07 10:33:00 -0800997 print(__doc__)
Ying Wangbd93d422011-10-28 17:02:30 -0700998 sys.exit(1)
999
1000 in_dir = argv[0]
1001 glob_dict_file = argv[1]
1002 out_file = argv[2]
Thierry Strudel74a81e62015-07-09 09:54:55 -07001003 target_out = argv[3]
Yifan Hongbbcba1e2018-06-18 16:32:35 -07001004 prop_file_out = argv[4] if len(argv) >= 5 else None
Ying Wangbd93d422011-10-28 17:02:30 -07001005
1006 glob_dict = LoadGlobalDict(glob_dict_file)
Ying Wangae61f502015-03-12 18:30:39 -07001007 if "mount_point" in glob_dict:
Tao Baoc7a6f1e2015-06-23 11:16:05 -07001008 # The caller knows the mount point and provides a dictionay needed by
1009 # BuildImage().
Ying Wangae61f502015-03-12 18:30:39 -07001010 image_properties = glob_dict
Ying Wang9f8e8db2011-11-04 11:37:01 -07001011 else:
Ying Wangae61f502015-03-12 18:30:39 -07001012 image_filename = os.path.basename(out_file)
1013 mount_point = ""
1014 if image_filename == "system.img":
1015 mount_point = "system"
Alex Light4e358ab2016-06-16 14:47:10 -07001016 elif image_filename == "system_other.img":
1017 mount_point = "system_other"
Ying Wangae61f502015-03-12 18:30:39 -07001018 elif image_filename == "userdata.img":
1019 mount_point = "data"
1020 elif image_filename == "cache.img":
1021 mount_point = "cache"
1022 elif image_filename == "vendor.img":
1023 mount_point = "vendor"
Bowgo Tsaid624fa62017-11-14 23:42:30 +08001024 elif image_filename == "odm.img":
1025 mount_point = "odm"
Ying Wangae61f502015-03-12 18:30:39 -07001026 elif image_filename == "oem.img":
1027 mount_point = "oem"
Jaekyun Seokb7735d82017-11-27 17:04:47 +09001028 elif image_filename == "product.img":
1029 mount_point = "product"
Dario Freni924af7d2018-08-17 00:56:14 +01001030 elif image_filename == "product_services.img":
1031 mount_point = "product_services"
Ying Wangae61f502015-03-12 18:30:39 -07001032 else:
Tao Baoc72727a2017-12-07 10:33:00 -08001033 print("error: unknown image file name ", image_filename, file=sys.stderr)
Tao Bao1c830bf2017-12-25 10:43:47 -08001034 sys.exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -07001035
Ying Wangae61f502015-03-12 18:30:39 -07001036 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
1037
Tao Baoc6bd70a2018-09-27 16:58:00 -07001038 try:
1039 BuildImage(in_dir, image_properties, out_file, target_out)
1040 except:
1041 print("Error: Failed to build {} from {}".format(out_file, in_dir),
Tao Baoc72727a2017-12-07 10:33:00 -08001042 file=sys.stderr)
Tao Baoc6bd70a2018-09-27 16:58:00 -07001043 raise
Ying Wangbd93d422011-10-28 17:02:30 -07001044
Yifan Hongbbcba1e2018-06-18 16:32:35 -07001045 if prop_file_out:
1046 glob_dict_out = GlobalDictFromImageProp(image_properties, mount_point)
1047 SaveGlobalDict(prop_file_out, glob_dict_out)
Ying Wangbd93d422011-10-28 17:02:30 -07001048
1049if __name__ == '__main__':
Tao Bao1c830bf2017-12-25 10:43:47 -08001050 try:
1051 main(sys.argv[1:])
1052 finally:
1053 common.Cleanup()