blob: 816cf4ef2491eea3ba0ae5ea17fbedac4285687c [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"""
Maria Bornski885dbb52015-09-04 11:13:16 -070018Build image output_image_file from input_directory, properties_file, and target_out_dir
Ying Wangbd93d422011-10-28 17:02:30 -070019
Maria Bornski885dbb52015-09-04 11:13:16 -070020Usage: build_image input_directory properties_file output_image_file target_out_dir
Ying Wangbd93d422011-10-28 17:02:30 -070021
22"""
23import os
Ying Wang69e9b4d2012-11-26 18:10:23 -080024import os.path
Tao Baoc7a6f1e2015-06-23 11:16:05 -070025import re
Ying Wangbd93d422011-10-28 17:02:30 -070026import subprocess
27import sys
Baligh Uddin601ddea2015-06-09 15:48:14 -070028import common
David Zeuthen4014a9d2016-09-30 17:29:22 -040029import shlex
Geremy Condrafd6f7512013-06-16 17:26:08 -070030import shutil
Sami Tolvanen405e71d2016-02-09 12:28:58 -080031import sparse_img
Geremy Condra5b5f4952014-05-05 22:19:37 -070032import tempfile
Ying Wangbd93d422011-10-28 17:02:30 -070033
Baligh Uddin601ddea2015-06-09 15:48:14 -070034OPTIONS = common.OPTIONS
35
Geremy Condrae8e982a2014-05-16 19:14:30 -070036FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"
Sami Tolvanenf99b5312015-05-20 07:30:57 +010037BLOCK_SIZE = 4096
Geremy Condrae8e982a2014-05-16 19:14:30 -070038
Tianjie Xu149b7fb2017-09-01 15:36:08 -070039def RunCommand(cmd, verbose=None):
Tao Baoc7a6f1e2015-06-23 11:16:05 -070040 """Echo and run the given command.
Ying Wang69e9b4d2012-11-26 18:10:23 -080041
42 Args:
43 cmd: the command represented as a list of strings.
Tianjie Xu149b7fb2017-09-01 15:36:08 -070044 verbose: show commands being executed.
Ying Wang69e9b4d2012-11-26 18:10:23 -080045 Returns:
Tao Baoc7a6f1e2015-06-23 11:16:05 -070046 A tuple of the output and the exit code.
Ying Wang69e9b4d2012-11-26 18:10:23 -080047 """
Tianjie Xu149b7fb2017-09-01 15:36:08 -070048 if verbose is None:
49 verbose = OPTIONS.verbose
50 if verbose:
51 print("Running: " + " ".join(cmd))
Tao Baoc7a6f1e2015-06-23 11:16:05 -070052 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
53 output, _ = p.communicate()
Tianjie Xu149b7fb2017-09-01 15:36:08 -070054
55 if verbose:
56 print(output.rstrip())
Tao Baoc7a6f1e2015-06-23 11:16:05 -070057 return (output, p.returncode)
Ying Wangbd93d422011-10-28 17:02:30 -070058
Sami Tolvanenf99b5312015-05-20 07:30:57 +010059def GetVerityFECSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080060 cmd = ["fec", "-s", str(partition_size)]
Tianjie Xu149b7fb2017-09-01 15:36:08 -070061 output, exit_code = RunCommand(cmd, False)
Tianjie Xue3ad41b2017-03-08 11:05:56 -080062 if exit_code != 0:
Sami Tolvanenf99b5312015-05-20 07:30:57 +010063 return False, 0
64 return True, int(output)
65
Geremy Condrafd6f7512013-06-16 17:26:08 -070066def GetVerityTreeSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080067 cmd = ["build_verity_tree", "-s", str(partition_size)]
Tianjie Xu149b7fb2017-09-01 15:36:08 -070068 output, exit_code = RunCommand(cmd, False)
Tianjie Xue3ad41b2017-03-08 11:05:56 -080069 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -070070 return False, 0
71 return True, int(output)
72
73def GetVerityMetadataSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080074 cmd = ["system/extras/verity/build_verity_metadata.py", "size",
75 str(partition_size)]
Tianjie Xu149b7fb2017-09-01 15:36:08 -070076 output, exit_code = RunCommand(cmd, False)
Tianjie Xue3ad41b2017-03-08 11:05:56 -080077 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -070078 return False, 0
79 return True, int(output)
80
Sami Tolvanenf99b5312015-05-20 07:30:57 +010081def GetVeritySize(partition_size, fec_supported):
82 success, verity_tree_size = GetVerityTreeSize(partition_size)
83 if not success:
84 return 0
85 success, verity_metadata_size = GetVerityMetadataSize(partition_size)
86 if not success:
87 return 0
88 verity_size = verity_tree_size + verity_metadata_size
89 if fec_supported:
90 success, fec_size = GetVerityFECSize(partition_size + verity_size)
91 if not success:
92 return 0
93 return verity_size + fec_size
94 return verity_size
95
Sami Tolvanen405e71d2016-02-09 12:28:58 -080096def GetSimgSize(image_file):
97 simg = sparse_img.SparseImage(image_file, build_map=False)
98 return simg.blocksize * simg.total_blocks
99
100def ZeroPadSimg(image_file, pad_size):
101 blocks = pad_size // BLOCK_SIZE
102 print("Padding %d blocks (%d bytes)" % (blocks, pad_size))
103 simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False)
104 simg.AppendFillChunk(0, blocks)
105
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800106def AVBCalcMaxImageSize(avbtool, footer_type, partition_size, additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400107 """Calculates max image size for a given partition size.
108
109 Args:
110 avbtool: String with path to avbtool.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800111 footer_type: 'hash' or 'hashtree' for generating footer.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400112 partition_size: The size of the partition in question.
113 additional_args: Additional arguments to pass to 'avbtool
114 add_hashtree_image'.
115 Returns:
116 The maximum image size or 0 if an error occurred.
117 """
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800118 cmd =[avbtool, "add_%s_footer" % footer_type,
119 "--partition_size", partition_size, "--calc_max_image_size"]
120 cmd.extend(shlex.split(additional_args))
121
122 (output, exit_code) = RunCommand(cmd)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400123 if exit_code != 0:
124 return 0
125 else:
126 return int(output)
127
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800128def AVBAddFooter(image_path, avbtool, footer_type, partition_size,
Tao Bao2b6dfd62017-09-27 17:17:43 -0700129 partition_name, key_path, algorithm, salt,
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800130 additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400131 """Adds dm-verity hashtree and AVB metadata to an image.
132
133 Args:
134 image_path: Path to image to modify.
135 avbtool: String with path to avbtool.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800136 footer_type: 'hash' or 'hashtree' for generating footer.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400137 partition_size: The size of the partition in question.
138 partition_name: The name of the partition - will be embedded in metadata.
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800139 key_path: Path to key to use or None.
140 algorithm: Name of algorithm to use or None.
Tao Bao2b6dfd62017-09-27 17:17:43 -0700141 salt: The salt to use (a hexadecimal string) or None.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400142 additional_args: Additional arguments to pass to 'avbtool
143 add_hashtree_image'.
144 Returns:
145 True if the operation succeeded.
146 """
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800147 cmd =[avbtool, "add_%s_footer" % footer_type,
148 "--partition_size", partition_size,
149 "--partition_name", partition_name,
150 "--image", image_path]
151
152 if key_path and algorithm:
153 cmd.extend(["--key", key_path, "--algorithm", algorithm])
Tao Bao2b6dfd62017-09-27 17:17:43 -0700154 if salt:
155 cmd.extend(["--salt", salt])
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800156
157 cmd.extend(shlex.split(additional_args))
158
159 (_, exit_code) = RunCommand(cmd)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400160 return exit_code == 0
161
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100162def AdjustPartitionSizeForVerity(partition_size, fec_supported):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700163 """Modifies the provided partition size to account for the verity metadata.
164
165 This information is used to size the created image appropriately.
166 Args:
167 partition_size: the size of the partition to be verified.
168 Returns:
Sami Tolvanen433905f2016-09-01 15:58:35 -0700169 A tuple of the size of the partition adjusted for verity metadata, and
170 the size of verity metadata.
Geremy Condrafd6f7512013-06-16 17:26:08 -0700171 """
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100172 key = "%d %d" % (partition_size, fec_supported)
173 if key in AdjustPartitionSizeForVerity.results:
174 return AdjustPartitionSizeForVerity.results[key]
175
176 hi = partition_size
177 if hi % BLOCK_SIZE != 0:
178 hi = (hi // BLOCK_SIZE) * BLOCK_SIZE
179
180 # verity tree and fec sizes depend on the partition size, which
181 # means this estimate is always going to be unnecessarily small
Sami Tolvanen433905f2016-09-01 15:58:35 -0700182 verity_size = GetVeritySize(hi, fec_supported)
183 lo = partition_size - verity_size
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100184 result = lo
185
186 # do a binary search for the optimal size
187 while lo < hi:
188 i = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE
Sami Tolvanen433905f2016-09-01 15:58:35 -0700189 v = GetVeritySize(i, fec_supported)
190 if i + v <= partition_size:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100191 if result < i:
192 result = i
Sami Tolvanen433905f2016-09-01 15:58:35 -0700193 verity_size = v
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100194 lo = i + BLOCK_SIZE
195 else:
196 hi = i
197
Tianjie Xu149b7fb2017-09-01 15:36:08 -0700198 print("Adjusted partition size for verity, partition_size: {},"
199 " verity_size: {}".format(result, verity_size))
Sami Tolvanen433905f2016-09-01 15:58:35 -0700200 AdjustPartitionSizeForVerity.results[key] = (result, verity_size)
201 return (result, verity_size)
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100202
203AdjustPartitionSizeForVerity.results = {}
204
Sami Tolvanen433905f2016-09-01 15:58:35 -0700205def BuildVerityFEC(sparse_image_path, verity_path, verity_fec_path,
206 padding_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800207 cmd = ["fec", "-e", "-p", str(padding_size), sparse_image_path,
208 verity_path, verity_fec_path]
209 output, exit_code = RunCommand(cmd)
210 if exit_code != 0:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100211 print "Could not build FEC data! Error: %s" % output
212 return False
213 return True
Geremy Condrafd6f7512013-06-16 17:26:08 -0700214
Colin Cross477cf2b2014-04-16 18:49:56 -0700215def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800216 cmd = ["build_verity_tree", "-A", FIXED_SALT, sparse_image_path,
217 verity_image_path]
218 output, exit_code = RunCommand(cmd)
219 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700220 print "Could not build verity tree! Error: %s" % output
221 return False
222 root, salt = output.split()
223 prop_dict["verity_root_hash"] = root
224 prop_dict["verity_salt"] = salt
225 return True
226
227def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800228 block_device, signer_path, key, signer_args,
229 verity_disable):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800230 cmd = ["system/extras/verity/build_verity_metadata.py", "build",
231 str(image_size), verity_metadata_path, root_hash, salt, block_device,
232 signer_path, key]
Tao Bao45810422016-10-17 16:20:12 -0700233 if signer_args:
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800234 cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),))
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800235 if verity_disable:
236 cmd.append("--verity_disable")
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800237 output, exit_code = RunCommand(cmd)
238 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700239 print "Could not build verity metadata! Error: %s" % output
240 return False
241 return True
242
243def Append2Simg(sparse_image_path, unsparse_image_path, error_message):
244 """Appends the unsparse image to the given sparse image.
245
246 Args:
247 sparse_image_path: the path to the (sparse) image
248 unsparse_image_path: the path to the (unsparse) image
249 Returns:
250 True on success, False on failure.
251 """
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800252 cmd = ["append2simg", sparse_image_path, unsparse_image_path]
253 output, exit_code = RunCommand(cmd)
254 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700255 print "%s: %s" % (error_message, output)
256 return False
257 return True
258
Sami Tolvanenff914f52015-12-18 13:24:56 +0000259def Append(target, file_to_append, error_message):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800260 print "appending %s to %s" % (file_to_append, target)
261 with open(target, "a") as out_file:
262 with open(file_to_append, "r") as input_file:
263 for line in input_file:
264 out_file.write(line)
Sami Tolvanenff914f52015-12-18 13:24:56 +0000265 return True
266
Dan Albert8b72aef2015-03-23 19:13:21 -0700267def BuildVerifiedImage(data_image_path, verity_image_path,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000268 verity_metadata_path, verity_fec_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700269 padding_size, fec_supported):
Sami Tolvanenff914f52015-12-18 13:24:56 +0000270 if not Append(verity_image_path, verity_metadata_path,
271 "Could not append verity metadata!"):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700272 return False
Sami Tolvanen4a060042015-12-18 15:50:25 +0000273
274 if fec_supported:
275 # build FEC for the entire partition, including metadata
276 if not BuildVerityFEC(data_image_path, verity_image_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700277 verity_fec_path, padding_size):
Sami Tolvanen4a060042015-12-18 15:50:25 +0000278 return False
279
280 if not Append(verity_image_path, verity_fec_path, "Could not append FEC!"):
281 return False
282
Sami Tolvanenff914f52015-12-18 13:24:56 +0000283 if not Append2Simg(data_image_path, verity_image_path,
284 "Could not append verity data!"):
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100285 return False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700286 return True
287
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800288def UnsparseImage(sparse_image_path, replace=True):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700289 img_dir = os.path.dirname(sparse_image_path)
290 unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
291 unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
292 if os.path.exists(unsparse_image_path):
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800293 if replace:
294 os.unlink(unsparse_image_path)
295 else:
296 return True, unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700297 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700298 (_, exit_code) = RunCommand(inflate_command)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700299 if exit_code != 0:
300 os.remove(unsparse_image_path)
301 return False, None
302 return True, unsparse_image_path
303
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100304def MakeVerityEnabledImage(out_file, fec_supported, prop_dict):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700305 """Creates an image that is verifiable using dm-verity.
306
307 Args:
308 out_file: the location to write the verifiable image at
Dan Albert8b72aef2015-03-23 19:13:21 -0700309 prop_dict: a dictionary of properties required for image creation and
310 verification
Geremy Condrafd6f7512013-06-16 17:26:08 -0700311 Returns:
312 True on success, False otherwise.
313 """
314 # get properties
Sami Tolvanen433905f2016-09-01 15:58:35 -0700315 image_size = int(prop_dict["partition_size"])
Geremy Condrafd6f7512013-06-16 17:26:08 -0700316 block_dev = prop_dict["verity_block_device"]
Paul Lawrencea37b2bb2014-11-13 17:54:30 -0800317 signer_key = prop_dict["verity_key"] + ".pk8"
Baligh Uddin601ddea2015-06-09 15:48:14 -0700318 if OPTIONS.verity_signer_path is not None:
Tao Bao45810422016-10-17 16:20:12 -0700319 signer_path = OPTIONS.verity_signer_path
Baligh Uddin601ddea2015-06-09 15:48:14 -0700320 else:
321 signer_path = prop_dict["verity_signer_cmd"]
Tao Bao45810422016-10-17 16:20:12 -0700322 signer_args = OPTIONS.verity_signer_args
Geremy Condrafd6f7512013-06-16 17:26:08 -0700323
324 # make a tempdir
Geremy Condra5b5f4952014-05-05 22:19:37 -0700325 tempdir_name = tempfile.mkdtemp(suffix="_verity_images")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700326
327 # get partial image paths
328 verity_image_path = os.path.join(tempdir_name, "verity.img")
329 verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img")
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100330 verity_fec_path = os.path.join(tempdir_name, "verity_fec.img")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700331
332 # build the verity tree and get the root hash and salt
Colin Cross477cf2b2014-04-16 18:49:56 -0700333 if not BuildVerityTree(out_file, verity_image_path, prop_dict):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700334 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700335 return False
336
337 # build the metadata blocks
338 root_hash = prop_dict["verity_root_hash"]
339 salt = prop_dict["verity_salt"]
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800340 verity_disable = "verity_disable" in prop_dict
Dan Albert8b72aef2015-03-23 19:13:21 -0700341 if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800342 block_dev, signer_path, signer_key, signer_args,
343 verity_disable):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700344 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700345 return False
346
347 # build the full verified image
Sami Tolvanen433905f2016-09-01 15:58:35 -0700348 target_size = int(prop_dict["original_partition_size"])
349 verity_size = int(prop_dict["verity_size"])
350
351 padding_size = target_size - image_size - verity_size
352 assert padding_size >= 0
353
Geremy Condrafd6f7512013-06-16 17:26:08 -0700354 if not BuildVerifiedImage(out_file,
355 verity_image_path,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000356 verity_metadata_path,
357 verity_fec_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700358 padding_size,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000359 fec_supported):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700360 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700361 return False
362
Geremy Condra5b5f4952014-05-05 22:19:37 -0700363 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700364 return True
365
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800366def ConvertBlockMapToBaseFs(block_map_file):
367 fd, base_fs_file = tempfile.mkstemp(prefix="script_gen_",
368 suffix=".base_fs")
369 os.close(fd)
370
371 convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file]
372 (_, exit_code) = RunCommand(convert_command)
373 if exit_code != 0:
374 os.remove(base_fs_file)
375 return None
376 return base_fs_file
377
Thierry Strudel74a81e62015-07-09 09:54:55 -0700378def BuildImage(in_dir, prop_dict, out_file, target_out=None):
Ying Wangbd93d422011-10-28 17:02:30 -0700379 """Build an image to out_file from in_dir with property prop_dict.
380
381 Args:
382 in_dir: path of input directory.
383 prop_dict: property dictionary.
384 out_file: path of the output image file.
Thierry Strudel74a81e62015-07-09 09:54:55 -0700385 target_out: path of the product out directory to read device specific FS config files.
Ying Wangbd93d422011-10-28 17:02:30 -0700386
387 Returns:
388 True iff the image is built successfully.
389 """
Tao Baof3282b42015-04-01 11:21:55 -0700390 # system_root_image=true: build a system.img that combines the contents of
391 # /system and the ramdisk, and can be mounted at the root of the file system.
Ying Wanga2292c92015-03-24 19:07:40 -0700392 origin_in = in_dir
393 fs_config = prop_dict.get("fs_config")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800394 base_fs_file = None
Ying Wanga2292c92015-03-24 19:07:40 -0700395 if (prop_dict.get("system_root_image") == "true"
396 and prop_dict["mount_point"] == "system"):
397 in_dir = tempfile.mkdtemp()
398 # Change the mount point to "/"
399 prop_dict["mount_point"] = "/"
400 if fs_config:
401 # We need to merge the fs_config files of system and ramdisk.
402 fd, merged_fs_config = tempfile.mkstemp(prefix="root_fs_config",
403 suffix=".txt")
404 os.close(fd)
405 with open(merged_fs_config, "w") as fw:
406 if "ramdisk_fs_config" in prop_dict:
407 with open(prop_dict["ramdisk_fs_config"]) as fr:
408 fw.writelines(fr.readlines())
409 with open(fs_config) as fr:
410 fw.writelines(fr.readlines())
411 fs_config = merged_fs_config
412
Ying Wangbd93d422011-10-28 17:02:30 -0700413 build_command = []
414 fs_type = prop_dict.get("fs_type", "")
Ying Wang69e9b4d2012-11-26 18:10:23 -0800415 run_fsck = False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700416
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700417 fs_spans_partition = True
418 if fs_type.startswith("squash"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700419 fs_spans_partition = False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700420
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700421 is_verity_partition = "verity_block_device" in prop_dict
Geremy Condra5b5f4952014-05-05 22:19:37 -0700422 verity_supported = prop_dict.get("verity") == "true"
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100423 verity_fec_supported = prop_dict.get("verity_fec") == "true"
424
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700425 # Adjust the partition size to make room for the hashes if this is to be
426 # verified.
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800427 if verity_supported and is_verity_partition:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700428 partition_size = int(prop_dict.get("partition_size"))
Sami Tolvanen433905f2016-09-01 15:58:35 -0700429 (adjusted_size, verity_size) = AdjustPartitionSizeForVerity(partition_size,
430 verity_fec_supported)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700431 if not adjusted_size:
432 return False
433 prop_dict["partition_size"] = str(adjusted_size)
434 prop_dict["original_partition_size"] = str(partition_size)
Sami Tolvanen433905f2016-09-01 15:58:35 -0700435 prop_dict["verity_size"] = str(verity_size)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700436
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800437 # Adjust partition size for AVB hash footer or AVB hashtree footer.
438 avb_footer_type = ''
439 if prop_dict.get("avb_hash_enable") == "true":
440 avb_footer_type = 'hash'
441 elif prop_dict.get("avb_hashtree_enable") == "true":
442 avb_footer_type = 'hashtree'
443
444 if avb_footer_type:
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800445 avbtool = prop_dict["avb_avbtool"]
446 partition_size = prop_dict["partition_size"]
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800447 # avb_add_hash_footer_args or avb_add_hashtree_footer_args.
448 additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"]
449 max_image_size = AVBCalcMaxImageSize(avbtool, avb_footer_type, partition_size,
David Zeuthen4014a9d2016-09-30 17:29:22 -0400450 additional_args)
451 if max_image_size == 0:
452 return False
453 prop_dict["partition_size"] = str(max_image_size)
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800454 prop_dict["original_partition_size"] = partition_size
David Zeuthen4014a9d2016-09-30 17:29:22 -0400455
Ying Wangbd93d422011-10-28 17:02:30 -0700456 if fs_type.startswith("ext"):
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800457 build_command = [prop_dict["ext_mkuserimg"]]
Ying Wangbd93d422011-10-28 17:02:30 -0700458 if "extfs_sparse_flag" in prop_dict:
459 build_command.append(prop_dict["extfs_sparse_flag"])
Ying Wang69e9b4d2012-11-26 18:10:23 -0800460 run_fsck = True
Ying Wangbd93d422011-10-28 17:02:30 -0700461 build_command.extend([in_dir, out_file, fs_type,
462 prop_dict["mount_point"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800463 build_command.append(prop_dict["partition_size"])
Ying Wangf3b86352014-11-18 18:03:13 -0800464 if "journal_size" in prop_dict:
465 build_command.extend(["-j", prop_dict["journal_size"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800466 if "timestamp" in prop_dict:
467 build_command.extend(["-T", str(prop_dict["timestamp"])])
Ying Wanga2292c92015-03-24 19:07:40 -0700468 if fs_config:
Doug Zongker82822822014-06-16 09:10:55 -0700469 build_command.extend(["-C", fs_config])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700470 if target_out:
471 build_command.extend(["-D", target_out])
Ying Wanga2292c92015-03-24 19:07:40 -0700472 if "block_list" in prop_dict:
473 build_command.extend(["-B", prop_dict["block_list"]])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800474 if "base_fs_file" in prop_dict:
475 base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"])
476 if base_fs_file is None:
477 return False
478 build_command.extend(["-d", base_fs_file])
Christoffer Dall8ed01f32014-12-17 21:34:12 +0100479 build_command.extend(["-L", prop_dict["mount_point"]])
Patrick Tjina1900842016-10-20 10:58:12 -0700480 if "extfs_inode_count" in prop_dict:
481 build_command.extend(["-i", prop_dict["extfs_inode_count"]])
Connor O'Brien20f08c32017-01-05 16:48:14 -0800482 if "flash_erase_block_size" in prop_dict:
483 build_command.extend(["-e", prop_dict["flash_erase_block_size"]])
484 if "flash_logical_block_size" in prop_dict:
485 build_command.extend(["-o", prop_dict["flash_logical_block_size"]])
Ying Wanga2292c92015-03-24 19:07:40 -0700486 if "selinux_fc" in prop_dict:
Kenny Rootf32dc712012-04-08 10:42:34 -0700487 build_command.append(prop_dict["selinux_fc"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800488 elif fs_type.startswith("squash"):
489 build_command = ["mksquashfsimage.sh"]
490 build_command.extend([in_dir, out_file])
Todd Poynorb2a555e2015-12-15 18:00:14 -0800491 if "squashfs_sparse_flag" in prop_dict:
492 build_command.extend([prop_dict["squashfs_sparse_flag"]])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800493 build_command.extend(["-m", prop_dict["mount_point"]])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700494 if target_out:
495 build_command.extend(["-d", target_out])
Mohamad Ayyash88378822016-04-07 22:10:51 -0700496 if fs_config:
497 build_command.extend(["-C", fs_config])
Ying Wanga2292c92015-03-24 19:07:40 -0700498 if "selinux_fc" in prop_dict:
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800499 build_command.extend(["-c", prop_dict["selinux_fc"]])
Mohamad Ayyashc3484f72016-06-13 09:46:58 -0700500 if "block_list" in prop_dict:
501 build_command.extend(["-B", prop_dict["block_list"]])
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700502 if "squashfs_compressor" in prop_dict:
503 build_command.extend(["-z", prop_dict["squashfs_compressor"]])
504 if "squashfs_compressor_opt" in prop_dict:
505 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700506 if "squashfs_block_size" in prop_dict:
507 build_command.extend(["-b", prop_dict["squashfs_block_size"]])
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700508 if "squashfs_disable_4k_align" in prop_dict and prop_dict.get("squashfs_disable_4k_align") == "true":
509 build_command.extend(["-a"])
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700510 elif fs_type.startswith("f2fs"):
511 build_command = ["mkf2fsuserimg.sh"]
512 build_command.extend([out_file, prop_dict["partition_size"]])
Ying Wangbd93d422011-10-28 17:02:30 -0700513 else:
Elliott Hughes305b0882016-06-15 17:04:54 -0700514 print("Error: unknown filesystem type '%s'" % (fs_type))
515 return False
Ying Wangbd93d422011-10-28 17:02:30 -0700516
Ying Wanga2292c92015-03-24 19:07:40 -0700517 if in_dir != origin_in:
518 # Construct a staging directory of the root file system.
519 ramdisk_dir = prop_dict.get("ramdisk_dir")
520 if ramdisk_dir:
521 shutil.rmtree(in_dir)
522 shutil.copytree(ramdisk_dir, in_dir, symlinks=True)
523 staging_system = os.path.join(in_dir, "system")
524 shutil.rmtree(staging_system, ignore_errors=True)
525 shutil.copytree(origin_in, staging_system, symlinks=True)
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700526
Julius D'souza001c6762017-05-03 13:43:27 -0700527 has_reserved_blocks = prop_dict.get("has_ext4_reserved_blocks") == "true"
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700528 ext4fs_output = None
529
Ying Wanga2292c92015-03-24 19:07:40 -0700530 try:
Julius D'souza001c6762017-05-03 13:43:27 -0700531 if fs_type.startswith("ext4"):
Tianjie Xu149b7fb2017-09-01 15:36:08 -0700532 (ext4fs_output, exit_code) = RunCommand(build_command, True)
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700533 else:
Tianjie Xu149b7fb2017-09-01 15:36:08 -0700534 (_, exit_code) = RunCommand(build_command, True)
Ying Wanga2292c92015-03-24 19:07:40 -0700535 finally:
536 if in_dir != origin_in:
537 # Clean up temporary directories and files.
538 shutil.rmtree(in_dir, ignore_errors=True)
539 if fs_config:
540 os.remove(fs_config)
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800541 if base_fs_file is not None:
542 os.remove(base_fs_file)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800543 if exit_code != 0:
544 return False
545
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700546 # Bug: 21522719, 22023465
547 # There are some reserved blocks on ext4 FS (lesser of 4096 blocks and 2%).
548 # We need to deduct those blocks from the available space, since they are
549 # not writable even with root privilege. It only affects devices using
550 # file-based OTA and a kernel version of 3.10 or greater (currently just
551 # sprout).
Julius D'souza001c6762017-05-03 13:43:27 -0700552 # Separately, check if there's enough headroom space available. This is useful for
553 # devices with low disk space that have system image variation between builds.
554 if (has_reserved_blocks or "partition_headroom" in prop_dict) and fs_type.startswith("ext4"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700555 assert ext4fs_output is not None
556 ext4fs_stats = re.compile(
557 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
558 r'(?P<total_blocks>[0-9]+) blocks')
559 m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
560 used_blocks = int(m.groupdict().get('used_blocks'))
561 total_blocks = int(m.groupdict().get('total_blocks'))
Julius D'souza001c6762017-05-03 13:43:27 -0700562 reserved_blocks = 0
563 headroom_blocks = 0
564 adjusted_blocks = total_blocks
565 if has_reserved_blocks:
566 reserved_blocks = min(4096, int(total_blocks * 0.02))
567 adjusted_blocks -= reserved_blocks
568 if "partition_headroom" in prop_dict:
569 headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
570 adjusted_blocks -= headroom_blocks
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700571 if used_blocks > adjusted_blocks:
572 mount_point = prop_dict.get("mount_point")
573 print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
Julius D'souza001c6762017-05-03 13:43:27 -0700574 "reserved: %d blocks, headroom: %d blocks, available: %d blocks)" % (
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700575 mount_point, total_blocks, used_blocks, reserved_blocks,
Julius D'souza001c6762017-05-03 13:43:27 -0700576 headroom_blocks, adjusted_blocks))
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700577 return False
578
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700579 if not fs_spans_partition:
580 mount_point = prop_dict.get("mount_point")
581 partition_size = int(prop_dict.get("partition_size"))
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800582 image_size = GetSimgSize(out_file)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700583 if image_size > partition_size:
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700584 print("Error: %s image size of %d is larger than partition size of "
585 "%d" % (mount_point, image_size, partition_size))
586 return False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700587 if verity_supported and is_verity_partition:
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800588 ZeroPadSimg(out_file, partition_size - image_size)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700589
Geremy Condrafd6f7512013-06-16 17:26:08 -0700590 # create the verified image if this is to be verified
Geremy Condra5b5f4952014-05-05 22:19:37 -0700591 if verity_supported and is_verity_partition:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100592 if not MakeVerityEnabledImage(out_file, verity_fec_supported, prop_dict):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700593 return False
594
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800595 # Add AVB HASH or HASHTREE footer (metadata).
596 if avb_footer_type:
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800597 avbtool = prop_dict["avb_avbtool"]
598 original_partition_size = prop_dict["original_partition_size"]
David Zeuthen4014a9d2016-09-30 17:29:22 -0400599 partition_name = prop_dict["partition_name"]
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800600 # key_path and algorithm are only available when chain partition is used.
601 key_path = prop_dict.get("avb_key_path")
602 algorithm = prop_dict.get("avb_algorithm")
Tao Bao2b6dfd62017-09-27 17:17:43 -0700603 salt = prop_dict.get("avb_salt")
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800604 # avb_add_hash_footer_args or avb_add_hashtree_footer_args
605 additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"]
606 if not AVBAddFooter(out_file, avbtool, avb_footer_type, original_partition_size,
Tao Bao2b6dfd62017-09-27 17:17:43 -0700607 partition_name, key_path, algorithm, salt, additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400608 return False
609
Ying Wang6a42a252013-02-27 13:54:02 -0800610 if run_fsck and prop_dict.get("skip_fsck") != "true":
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800611 success, unsparse_image = UnsparseImage(out_file, replace=False)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700612 if not success:
Ying Wang69e9b4d2012-11-26 18:10:23 -0800613 return False
614
615 # Run e2fsck on the inflated image file
616 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700617 (_, exit_code) = RunCommand(e2fsck_command)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800618
619 os.remove(unsparse_image)
620
621 return exit_code == 0
Ying Wangbd93d422011-10-28 17:02:30 -0700622
623
624def ImagePropFromGlobalDict(glob_dict, mount_point):
625 """Build an image property dictionary from the global dictionary.
626
627 Args:
628 glob_dict: the global dictionary from the build system.
629 mount_point: such as "system", "data" etc.
630 """
Doug Zongker1ad7ade2013-12-06 11:53:27 -0800631 d = {}
Tao Bao052ae352015-09-28 13:44:13 -0700632
Tao Bao822f5842015-09-30 16:01:14 -0700633 if "build.prop" in glob_dict:
634 bp = glob_dict["build.prop"]
635 if "ro.build.date.utc" in bp:
636 d["timestamp"] = bp["ro.build.date.utc"]
Ying Wang9f8e8db2011-11-04 11:37:01 -0700637
638 def copy_prop(src_p, dest_p):
639 if src_p in glob_dict:
640 d[dest_p] = str(glob_dict[src_p])
641
Ying Wangbd93d422011-10-28 17:02:30 -0700642 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -0700643 "extfs_sparse_flag",
Todd Poynorb2a555e2015-12-15 18:00:14 -0800644 "squashfs_sparse_flag",
Kenny Rootf32dc712012-04-08 10:42:34 -0700645 "selinux_fc",
Ying Wang6a42a252013-02-27 13:54:02 -0800646 "skip_fsck",
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800647 "ext_mkuserimg",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700648 "verity",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700649 "verity_key",
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100650 "verity_signer_cmd",
David Zeuthen4014a9d2016-09-30 17:29:22 -0400651 "verity_fec",
Bowgo Tsai6ceeb1a2017-10-11 16:21:48 +0800652 "verity_disable",
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800653 "avb_enable",
Tao Bao2b6dfd62017-09-27 17:17:43 -0700654 "avb_avbtool",
655 "avb_salt",
656 )
Ying Wangbd93d422011-10-28 17:02:30 -0700657 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -0700658 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -0700659
660 d["mount_point"] = mount_point
661 if mount_point == "system":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800662 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
663 copy_prop("avb_system_add_hashtree_footer_args",
664 "avb_add_hashtree_footer_args")
665 copy_prop("avb_system_key_path", "avb_key_path")
666 copy_prop("avb_system_algorithm", "avb_algorithm")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700667 copy_prop("fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700668 # Copy the generic system fs type first, override with specific one if
Dan Albert8b72aef2015-03-23 19:13:21 -0700669 # available.
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800670 copy_prop("system_fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700671 copy_prop("system_headroom", "partition_headroom")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700672 copy_prop("system_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800673 copy_prop("system_journal_size", "journal_size")
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700674 copy_prop("system_verity_block_device", "verity_block_device")
Tao Baof3282b42015-04-01 11:21:55 -0700675 copy_prop("system_root_image", "system_root_image")
676 copy_prop("ramdisk_dir", "ramdisk_dir")
Tao Bao84e75682015-07-19 02:38:53 -0700677 copy_prop("ramdisk_fs_config", "ramdisk_fs_config")
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700678 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700679 copy_prop("system_squashfs_compressor", "squashfs_compressor")
680 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700681 copy_prop("system_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700682 copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800683 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700684 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Alex Light4e358ab2016-06-16 14:47:10 -0700685 elif mount_point == "system_other":
686 # We inherit the selinux policies of /system since we contain some of its files.
687 d["mount_point"] = "system"
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800688 copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
689 copy_prop("avb_system_add_hashtree_footer_args",
690 "avb_add_hashtree_footer_args")
691 copy_prop("avb_system_key_path", "avb_key_path")
692 copy_prop("avb_system_algorithm", "avb_algorithm")
Alex Light4e358ab2016-06-16 14:47:10 -0700693 copy_prop("fs_type", "fs_type")
694 copy_prop("system_fs_type", "fs_type")
695 copy_prop("system_size", "partition_size")
696 copy_prop("system_journal_size", "journal_size")
697 copy_prop("system_verity_block_device", "verity_block_device")
698 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
699 copy_prop("system_squashfs_compressor", "squashfs_compressor")
700 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
701 copy_prop("system_squashfs_block_size", "squashfs_block_size")
702 copy_prop("system_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700703 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Ying Wangbd93d422011-10-28 17:02:30 -0700704 elif mount_point == "data":
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700705 # Copy the generic fs type first, override with specific one if available.
Ying Wang9f8e8db2011-11-04 11:37:01 -0700706 copy_prop("fs_type", "fs_type")
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700707 copy_prop("userdata_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700708 copy_prop("userdata_size", "partition_size")
Connor O'Brien20f08c32017-01-05 16:48:14 -0800709 copy_prop("flash_logical_block_size","flash_logical_block_size")
710 copy_prop("flash_erase_block_size", "flash_erase_block_size")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700711 elif mount_point == "cache":
712 copy_prop("cache_fs_type", "fs_type")
713 copy_prop("cache_size", "partition_size")
Ying Wanga0febe52013-03-20 11:02:05 -0700714 elif mount_point == "vendor":
Bowgo Tsai3e599ea2017-05-26 18:30:04 +0800715 copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable")
716 copy_prop("avb_vendor_add_hashtree_footer_args",
717 "avb_add_hashtree_footer_args")
718 copy_prop("avb_vendor_key_path", "avb_key_path")
719 copy_prop("avb_vendor_algorithm", "avb_algorithm")
Ying Wanga0febe52013-03-20 11:02:05 -0700720 copy_prop("vendor_fs_type", "fs_type")
721 copy_prop("vendor_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800722 copy_prop("vendor_journal_size", "journal_size")
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700723 copy_prop("vendor_verity_block_device", "verity_block_device")
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700724 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Patrick Tjine11aa502016-02-09 15:40:38 -0800725 copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
726 copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700727 copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700728 copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800729 copy_prop("vendor_base_fs_file", "base_fs_file")
Patrick Tjina1900842016-10-20 10:58:12 -0700730 copy_prop("vendor_extfs_inode_count", "extfs_inode_count")
Ying Wangb8888432014-03-11 17:13:27 -0700731 elif mount_point == "oem":
732 copy_prop("fs_type", "fs_type")
733 copy_prop("oem_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800734 copy_prop("oem_journal_size", "journal_size")
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700735 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Patrick Tjina1900842016-10-20 10:58:12 -0700736 copy_prop("oem_extfs_inode_count", "extfs_inode_count")
David Zeuthen4014a9d2016-09-30 17:29:22 -0400737 d["partition_name"] = mount_point
Ying Wangbd93d422011-10-28 17:02:30 -0700738 return d
739
740
741def LoadGlobalDict(filename):
742 """Load "name=value" pairs from filename"""
743 d = {}
744 f = open(filename)
745 for line in f:
746 line = line.strip()
747 if not line or line.startswith("#"):
748 continue
749 k, v = line.split("=", 1)
750 d[k] = v
751 f.close()
752 return d
753
754
755def main(argv):
Thierry Strudel74a81e62015-07-09 09:54:55 -0700756 if len(argv) != 4:
Ying Wangbd93d422011-10-28 17:02:30 -0700757 print __doc__
758 sys.exit(1)
759
760 in_dir = argv[0]
761 glob_dict_file = argv[1]
762 out_file = argv[2]
Thierry Strudel74a81e62015-07-09 09:54:55 -0700763 target_out = argv[3]
Ying Wangbd93d422011-10-28 17:02:30 -0700764
765 glob_dict = LoadGlobalDict(glob_dict_file)
Ying Wangae61f502015-03-12 18:30:39 -0700766 if "mount_point" in glob_dict:
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700767 # The caller knows the mount point and provides a dictionay needed by
768 # BuildImage().
Ying Wangae61f502015-03-12 18:30:39 -0700769 image_properties = glob_dict
Ying Wang9f8e8db2011-11-04 11:37:01 -0700770 else:
Ying Wangae61f502015-03-12 18:30:39 -0700771 image_filename = os.path.basename(out_file)
772 mount_point = ""
773 if image_filename == "system.img":
774 mount_point = "system"
Alex Light4e358ab2016-06-16 14:47:10 -0700775 elif image_filename == "system_other.img":
776 mount_point = "system_other"
Ying Wangae61f502015-03-12 18:30:39 -0700777 elif image_filename == "userdata.img":
778 mount_point = "data"
779 elif image_filename == "cache.img":
780 mount_point = "cache"
781 elif image_filename == "vendor.img":
782 mount_point = "vendor"
783 elif image_filename == "oem.img":
784 mount_point = "oem"
785 else:
786 print >> sys.stderr, "error: unknown image file name ", image_filename
787 exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -0700788
Ying Wangae61f502015-03-12 18:30:39 -0700789 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
790
Thierry Strudel74a81e62015-07-09 09:54:55 -0700791 if not BuildImage(in_dir, image_properties, out_file, target_out):
Dan Albert8b72aef2015-03-23 19:13:21 -0700792 print >> sys.stderr, "error: failed to build %s from %s" % (out_file,
793 in_dir)
Ying Wangbd93d422011-10-28 17:02:30 -0700794 exit(1)
795
796
797if __name__ == '__main__':
798 main(sys.argv[1:])