blob: 05c895a956352e98534a637b7d1a6bda0953b092 [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
Ying Wang69e9b4d2012-11-26 18:10:23 -080039def RunCommand(cmd):
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.
44 Returns:
Tao Baoc7a6f1e2015-06-23 11:16:05 -070045 A tuple of the output and the exit code.
Ying Wang69e9b4d2012-11-26 18:10:23 -080046 """
47 print "Running: ", " ".join(cmd)
Tao Baoc7a6f1e2015-06-23 11:16:05 -070048 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
49 output, _ = p.communicate()
50 print "%s" % (output.rstrip(),)
51 return (output, p.returncode)
Ying Wangbd93d422011-10-28 17:02:30 -070052
Sami Tolvanenf99b5312015-05-20 07:30:57 +010053def GetVerityFECSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080054 cmd = ["fec", "-s", str(partition_size)]
55 output, exit_code = RunCommand(cmd)
56 if exit_code != 0:
Sami Tolvanenf99b5312015-05-20 07:30:57 +010057 return False, 0
58 return True, int(output)
59
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)]
62 output, exit_code = RunCommand(cmd)
63 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -070064 return False, 0
65 return True, int(output)
66
67def GetVerityMetadataSize(partition_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -080068 cmd = ["system/extras/verity/build_verity_metadata.py", "size",
69 str(partition_size)]
70 output, exit_code = RunCommand(cmd)
71 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -070072 return False, 0
73 return True, int(output)
74
Sami Tolvanenf99b5312015-05-20 07:30:57 +010075def GetVeritySize(partition_size, fec_supported):
76 success, verity_tree_size = GetVerityTreeSize(partition_size)
77 if not success:
78 return 0
79 success, verity_metadata_size = GetVerityMetadataSize(partition_size)
80 if not success:
81 return 0
82 verity_size = verity_tree_size + verity_metadata_size
83 if fec_supported:
84 success, fec_size = GetVerityFECSize(partition_size + verity_size)
85 if not success:
86 return 0
87 return verity_size + fec_size
88 return verity_size
89
Sami Tolvanen405e71d2016-02-09 12:28:58 -080090def GetSimgSize(image_file):
91 simg = sparse_img.SparseImage(image_file, build_map=False)
92 return simg.blocksize * simg.total_blocks
93
94def ZeroPadSimg(image_file, pad_size):
95 blocks = pad_size // BLOCK_SIZE
96 print("Padding %d blocks (%d bytes)" % (blocks, pad_size))
97 simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False)
98 simg.AppendFillChunk(0, blocks)
99
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800100def AVBCalcMaxImageSize(avbtool, footer_type, partition_size, additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400101 """Calculates max image size for a given partition size.
102
103 Args:
104 avbtool: String with path to avbtool.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800105 footer_type: 'hash' or 'hashtree' for generating footer.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400106 partition_size: The size of the partition in question.
107 additional_args: Additional arguments to pass to 'avbtool
108 add_hashtree_image'.
109 Returns:
110 The maximum image size or 0 if an error occurred.
111 """
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800112 cmdline = "%s add_%s_footer " % (avbtool, footer_type)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400113 cmdline += "--partition_size %d " % partition_size
114 cmdline += "--calc_max_image_size "
115 cmdline += additional_args
116 (output, exit_code) = RunCommand(shlex.split(cmdline))
117 if exit_code != 0:
118 return 0
119 else:
120 return int(output)
121
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800122def AVBAddFooter(image_path, avbtool, footer_type, partition_size,
123 partition_name, signing_args, additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400124 """Adds dm-verity hashtree and AVB metadata to an image.
125
126 Args:
127 image_path: Path to image to modify.
128 avbtool: String with path to avbtool.
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800129 footer_type: 'hash' or 'hashtree' for generating footer.
David Zeuthen4014a9d2016-09-30 17:29:22 -0400130 partition_size: The size of the partition in question.
131 partition_name: The name of the partition - will be embedded in metadata.
132 signing_args: Arguments for signing the image.
133 additional_args: Additional arguments to pass to 'avbtool
134 add_hashtree_image'.
135 Returns:
136 True if the operation succeeded.
137 """
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800138 cmdline = "%s add_%s_footer " % (avbtool, footer_type)
David Zeuthen4014a9d2016-09-30 17:29:22 -0400139 cmdline += "--partition_size %d " % partition_size
140 cmdline += "--partition_name %s " % partition_name
141 cmdline += "--image %s " % image_path
142 cmdline += signing_args + " "
143 cmdline += additional_args
144 (_, exit_code) = RunCommand(shlex.split(cmdline))
145 return exit_code == 0
146
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100147def AdjustPartitionSizeForVerity(partition_size, fec_supported):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700148 """Modifies the provided partition size to account for the verity metadata.
149
150 This information is used to size the created image appropriately.
151 Args:
152 partition_size: the size of the partition to be verified.
153 Returns:
Sami Tolvanen433905f2016-09-01 15:58:35 -0700154 A tuple of the size of the partition adjusted for verity metadata, and
155 the size of verity metadata.
Geremy Condrafd6f7512013-06-16 17:26:08 -0700156 """
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100157 key = "%d %d" % (partition_size, fec_supported)
158 if key in AdjustPartitionSizeForVerity.results:
159 return AdjustPartitionSizeForVerity.results[key]
160
161 hi = partition_size
162 if hi % BLOCK_SIZE != 0:
163 hi = (hi // BLOCK_SIZE) * BLOCK_SIZE
164
165 # verity tree and fec sizes depend on the partition size, which
166 # means this estimate is always going to be unnecessarily small
Sami Tolvanen433905f2016-09-01 15:58:35 -0700167 verity_size = GetVeritySize(hi, fec_supported)
168 lo = partition_size - verity_size
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100169 result = lo
170
171 # do a binary search for the optimal size
172 while lo < hi:
173 i = ((lo + hi) // (2 * BLOCK_SIZE)) * BLOCK_SIZE
Sami Tolvanen433905f2016-09-01 15:58:35 -0700174 v = GetVeritySize(i, fec_supported)
175 if i + v <= partition_size:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100176 if result < i:
177 result = i
Sami Tolvanen433905f2016-09-01 15:58:35 -0700178 verity_size = v
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100179 lo = i + BLOCK_SIZE
180 else:
181 hi = i
182
Sami Tolvanen433905f2016-09-01 15:58:35 -0700183 AdjustPartitionSizeForVerity.results[key] = (result, verity_size)
184 return (result, verity_size)
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100185
186AdjustPartitionSizeForVerity.results = {}
187
Sami Tolvanen433905f2016-09-01 15:58:35 -0700188def BuildVerityFEC(sparse_image_path, verity_path, verity_fec_path,
189 padding_size):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800190 cmd = ["fec", "-e", "-p", str(padding_size), sparse_image_path,
191 verity_path, verity_fec_path]
192 output, exit_code = RunCommand(cmd)
193 if exit_code != 0:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100194 print "Could not build FEC data! Error: %s" % output
195 return False
196 return True
Geremy Condrafd6f7512013-06-16 17:26:08 -0700197
Colin Cross477cf2b2014-04-16 18:49:56 -0700198def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800199 cmd = ["build_verity_tree", "-A", FIXED_SALT, sparse_image_path,
200 verity_image_path]
201 output, exit_code = RunCommand(cmd)
202 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700203 print "Could not build verity tree! Error: %s" % output
204 return False
205 root, salt = output.split()
206 prop_dict["verity_root_hash"] = root
207 prop_dict["verity_salt"] = salt
208 return True
209
210def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
Tao Bao45810422016-10-17 16:20:12 -0700211 block_device, signer_path, key, signer_args):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800212 cmd = ["system/extras/verity/build_verity_metadata.py", "build",
213 str(image_size), verity_metadata_path, root_hash, salt, block_device,
214 signer_path, key]
Tao Bao45810422016-10-17 16:20:12 -0700215 if signer_args:
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800216 cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),))
217 output, exit_code = RunCommand(cmd)
218 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700219 print "Could not build verity metadata! Error: %s" % output
220 return False
221 return True
222
223def Append2Simg(sparse_image_path, unsparse_image_path, error_message):
224 """Appends the unsparse image to the given sparse image.
225
226 Args:
227 sparse_image_path: the path to the (sparse) image
228 unsparse_image_path: the path to the (unsparse) image
229 Returns:
230 True on success, False on failure.
231 """
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800232 cmd = ["append2simg", sparse_image_path, unsparse_image_path]
233 output, exit_code = RunCommand(cmd)
234 if exit_code != 0:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700235 print "%s: %s" % (error_message, output)
236 return False
237 return True
238
Sami Tolvanenff914f52015-12-18 13:24:56 +0000239def Append(target, file_to_append, error_message):
Tianjie Xue3ad41b2017-03-08 11:05:56 -0800240 print "appending %s to %s" % (file_to_append, target)
241 with open(target, "a") as out_file:
242 with open(file_to_append, "r") as input_file:
243 for line in input_file:
244 out_file.write(line)
Sami Tolvanenff914f52015-12-18 13:24:56 +0000245 return True
246
Dan Albert8b72aef2015-03-23 19:13:21 -0700247def BuildVerifiedImage(data_image_path, verity_image_path,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000248 verity_metadata_path, verity_fec_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700249 padding_size, fec_supported):
Sami Tolvanenff914f52015-12-18 13:24:56 +0000250 if not Append(verity_image_path, verity_metadata_path,
251 "Could not append verity metadata!"):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700252 return False
Sami Tolvanen4a060042015-12-18 15:50:25 +0000253
254 if fec_supported:
255 # build FEC for the entire partition, including metadata
256 if not BuildVerityFEC(data_image_path, verity_image_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700257 verity_fec_path, padding_size):
Sami Tolvanen4a060042015-12-18 15:50:25 +0000258 return False
259
260 if not Append(verity_image_path, verity_fec_path, "Could not append FEC!"):
261 return False
262
Sami Tolvanenff914f52015-12-18 13:24:56 +0000263 if not Append2Simg(data_image_path, verity_image_path,
264 "Could not append verity data!"):
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100265 return False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700266 return True
267
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800268def UnsparseImage(sparse_image_path, replace=True):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700269 img_dir = os.path.dirname(sparse_image_path)
270 unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
271 unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
272 if os.path.exists(unsparse_image_path):
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800273 if replace:
274 os.unlink(unsparse_image_path)
275 else:
276 return True, unsparse_image_path
Geremy Condrafd6f7512013-06-16 17:26:08 -0700277 inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700278 (_, exit_code) = RunCommand(inflate_command)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700279 if exit_code != 0:
280 os.remove(unsparse_image_path)
281 return False, None
282 return True, unsparse_image_path
283
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100284def MakeVerityEnabledImage(out_file, fec_supported, prop_dict):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700285 """Creates an image that is verifiable using dm-verity.
286
287 Args:
288 out_file: the location to write the verifiable image at
Dan Albert8b72aef2015-03-23 19:13:21 -0700289 prop_dict: a dictionary of properties required for image creation and
290 verification
Geremy Condrafd6f7512013-06-16 17:26:08 -0700291 Returns:
292 True on success, False otherwise.
293 """
294 # get properties
Sami Tolvanen433905f2016-09-01 15:58:35 -0700295 image_size = int(prop_dict["partition_size"])
Geremy Condrafd6f7512013-06-16 17:26:08 -0700296 block_dev = prop_dict["verity_block_device"]
Paul Lawrencea37b2bb2014-11-13 17:54:30 -0800297 signer_key = prop_dict["verity_key"] + ".pk8"
Baligh Uddin601ddea2015-06-09 15:48:14 -0700298 if OPTIONS.verity_signer_path is not None:
Tao Bao45810422016-10-17 16:20:12 -0700299 signer_path = OPTIONS.verity_signer_path
Baligh Uddin601ddea2015-06-09 15:48:14 -0700300 else:
301 signer_path = prop_dict["verity_signer_cmd"]
Tao Bao45810422016-10-17 16:20:12 -0700302 signer_args = OPTIONS.verity_signer_args
Geremy Condrafd6f7512013-06-16 17:26:08 -0700303
304 # make a tempdir
Geremy Condra5b5f4952014-05-05 22:19:37 -0700305 tempdir_name = tempfile.mkdtemp(suffix="_verity_images")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700306
307 # get partial image paths
308 verity_image_path = os.path.join(tempdir_name, "verity.img")
309 verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img")
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100310 verity_fec_path = os.path.join(tempdir_name, "verity_fec.img")
Geremy Condrafd6f7512013-06-16 17:26:08 -0700311
312 # build the verity tree and get the root hash and salt
Colin Cross477cf2b2014-04-16 18:49:56 -0700313 if not BuildVerityTree(out_file, verity_image_path, prop_dict):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700314 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700315 return False
316
317 # build the metadata blocks
318 root_hash = prop_dict["verity_root_hash"]
319 salt = prop_dict["verity_salt"]
Dan Albert8b72aef2015-03-23 19:13:21 -0700320 if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
Tao Bao45810422016-10-17 16:20:12 -0700321 block_dev, signer_path, signer_key, signer_args):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700322 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700323 return False
324
325 # build the full verified image
Sami Tolvanen433905f2016-09-01 15:58:35 -0700326 target_size = int(prop_dict["original_partition_size"])
327 verity_size = int(prop_dict["verity_size"])
328
329 padding_size = target_size - image_size - verity_size
330 assert padding_size >= 0
331
Geremy Condrafd6f7512013-06-16 17:26:08 -0700332 if not BuildVerifiedImage(out_file,
333 verity_image_path,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000334 verity_metadata_path,
335 verity_fec_path,
Sami Tolvanen433905f2016-09-01 15:58:35 -0700336 padding_size,
Sami Tolvanen4a060042015-12-18 15:50:25 +0000337 fec_supported):
Geremy Condra5b5f4952014-05-05 22:19:37 -0700338 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700339 return False
340
Geremy Condra5b5f4952014-05-05 22:19:37 -0700341 shutil.rmtree(tempdir_name, ignore_errors=True)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700342 return True
343
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800344def ConvertBlockMapToBaseFs(block_map_file):
345 fd, base_fs_file = tempfile.mkstemp(prefix="script_gen_",
346 suffix=".base_fs")
347 os.close(fd)
348
349 convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file]
350 (_, exit_code) = RunCommand(convert_command)
351 if exit_code != 0:
352 os.remove(base_fs_file)
353 return None
354 return base_fs_file
355
Thierry Strudel74a81e62015-07-09 09:54:55 -0700356def BuildImage(in_dir, prop_dict, out_file, target_out=None):
Ying Wangbd93d422011-10-28 17:02:30 -0700357 """Build an image to out_file from in_dir with property prop_dict.
358
359 Args:
360 in_dir: path of input directory.
361 prop_dict: property dictionary.
362 out_file: path of the output image file.
Thierry Strudel74a81e62015-07-09 09:54:55 -0700363 target_out: path of the product out directory to read device specific FS config files.
Ying Wangbd93d422011-10-28 17:02:30 -0700364
365 Returns:
366 True iff the image is built successfully.
367 """
Tao Baof3282b42015-04-01 11:21:55 -0700368 # system_root_image=true: build a system.img that combines the contents of
369 # /system and the ramdisk, and can be mounted at the root of the file system.
Ying Wanga2292c92015-03-24 19:07:40 -0700370 origin_in = in_dir
371 fs_config = prop_dict.get("fs_config")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800372 base_fs_file = None
Ying Wanga2292c92015-03-24 19:07:40 -0700373 if (prop_dict.get("system_root_image") == "true"
374 and prop_dict["mount_point"] == "system"):
375 in_dir = tempfile.mkdtemp()
376 # Change the mount point to "/"
377 prop_dict["mount_point"] = "/"
378 if fs_config:
379 # We need to merge the fs_config files of system and ramdisk.
380 fd, merged_fs_config = tempfile.mkstemp(prefix="root_fs_config",
381 suffix=".txt")
382 os.close(fd)
383 with open(merged_fs_config, "w") as fw:
384 if "ramdisk_fs_config" in prop_dict:
385 with open(prop_dict["ramdisk_fs_config"]) as fr:
386 fw.writelines(fr.readlines())
387 with open(fs_config) as fr:
388 fw.writelines(fr.readlines())
389 fs_config = merged_fs_config
390
Ying Wangbd93d422011-10-28 17:02:30 -0700391 build_command = []
392 fs_type = prop_dict.get("fs_type", "")
Ying Wang69e9b4d2012-11-26 18:10:23 -0800393 run_fsck = False
Geremy Condrafd6f7512013-06-16 17:26:08 -0700394
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700395 fs_spans_partition = True
396 if fs_type.startswith("squash"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700397 fs_spans_partition = False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700398
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700399 is_verity_partition = "verity_block_device" in prop_dict
Geremy Condra5b5f4952014-05-05 22:19:37 -0700400 verity_supported = prop_dict.get("verity") == "true"
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100401 verity_fec_supported = prop_dict.get("verity_fec") == "true"
402
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700403 # Adjust the partition size to make room for the hashes if this is to be
404 # verified.
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800405 if verity_supported and is_verity_partition:
Geremy Condrafd6f7512013-06-16 17:26:08 -0700406 partition_size = int(prop_dict.get("partition_size"))
Sami Tolvanen433905f2016-09-01 15:58:35 -0700407 (adjusted_size, verity_size) = AdjustPartitionSizeForVerity(partition_size,
408 verity_fec_supported)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700409 if not adjusted_size:
410 return False
411 prop_dict["partition_size"] = str(adjusted_size)
412 prop_dict["original_partition_size"] = str(partition_size)
Sami Tolvanen433905f2016-09-01 15:58:35 -0700413 prop_dict["verity_size"] = str(verity_size)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700414
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800415 # Adjust partition size for AVB hash footer or AVB hashtree footer.
416 avb_footer_type = ''
417 if prop_dict.get("avb_hash_enable") == "true":
418 avb_footer_type = 'hash'
419 elif prop_dict.get("avb_hashtree_enable") == "true":
420 avb_footer_type = 'hashtree'
421
422 if avb_footer_type:
David Zeuthen4014a9d2016-09-30 17:29:22 -0400423 avbtool = prop_dict.get("avb_avbtool")
424 partition_size = int(prop_dict.get("partition_size"))
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800425 # avb_add_hash_footer_args or avb_add_hashtree_footer_args.
426 additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"]
427 max_image_size = AVBCalcMaxImageSize(avbtool, avb_footer_type, partition_size,
David Zeuthen4014a9d2016-09-30 17:29:22 -0400428 additional_args)
429 if max_image_size == 0:
430 return False
431 prop_dict["partition_size"] = str(max_image_size)
432 prop_dict["original_partition_size"] = str(partition_size)
433
Ying Wangbd93d422011-10-28 17:02:30 -0700434 if fs_type.startswith("ext"):
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800435 build_command = [prop_dict["ext_mkuserimg"]]
Ying Wangbd93d422011-10-28 17:02:30 -0700436 if "extfs_sparse_flag" in prop_dict:
437 build_command.append(prop_dict["extfs_sparse_flag"])
Ying Wang69e9b4d2012-11-26 18:10:23 -0800438 run_fsck = True
Ying Wangbd93d422011-10-28 17:02:30 -0700439 build_command.extend([in_dir, out_file, fs_type,
440 prop_dict["mount_point"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800441 build_command.append(prop_dict["partition_size"])
Ying Wangf3b86352014-11-18 18:03:13 -0800442 if "journal_size" in prop_dict:
443 build_command.extend(["-j", prop_dict["journal_size"]])
Doug Zongker850b8072013-12-05 15:54:55 -0800444 if "timestamp" in prop_dict:
445 build_command.extend(["-T", str(prop_dict["timestamp"])])
Ying Wanga2292c92015-03-24 19:07:40 -0700446 if fs_config:
Doug Zongker82822822014-06-16 09:10:55 -0700447 build_command.extend(["-C", fs_config])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700448 if target_out:
449 build_command.extend(["-D", target_out])
Ying Wanga2292c92015-03-24 19:07:40 -0700450 if "block_list" in prop_dict:
451 build_command.extend(["-B", prop_dict["block_list"]])
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800452 if "base_fs_file" in prop_dict:
453 base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"])
454 if base_fs_file is None:
455 return False
456 build_command.extend(["-d", base_fs_file])
Christoffer Dall8ed01f32014-12-17 21:34:12 +0100457 build_command.extend(["-L", prop_dict["mount_point"]])
Patrick Tjina1900842016-10-20 10:58:12 -0700458 if "extfs_inode_count" in prop_dict:
459 build_command.extend(["-i", prop_dict["extfs_inode_count"]])
Connor O'Brien20f08c32017-01-05 16:48:14 -0800460 if "flash_erase_block_size" in prop_dict:
461 build_command.extend(["-e", prop_dict["flash_erase_block_size"]])
462 if "flash_logical_block_size" in prop_dict:
463 build_command.extend(["-o", prop_dict["flash_logical_block_size"]])
Ying Wanga2292c92015-03-24 19:07:40 -0700464 if "selinux_fc" in prop_dict:
Kenny Rootf32dc712012-04-08 10:42:34 -0700465 build_command.append(prop_dict["selinux_fc"])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800466 elif fs_type.startswith("squash"):
467 build_command = ["mksquashfsimage.sh"]
468 build_command.extend([in_dir, out_file])
Todd Poynorb2a555e2015-12-15 18:00:14 -0800469 if "squashfs_sparse_flag" in prop_dict:
470 build_command.extend([prop_dict["squashfs_sparse_flag"]])
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800471 build_command.extend(["-m", prop_dict["mount_point"]])
Thierry Strudel74a81e62015-07-09 09:54:55 -0700472 if target_out:
473 build_command.extend(["-d", target_out])
Mohamad Ayyash88378822016-04-07 22:10:51 -0700474 if fs_config:
475 build_command.extend(["-C", fs_config])
Ying Wanga2292c92015-03-24 19:07:40 -0700476 if "selinux_fc" in prop_dict:
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800477 build_command.extend(["-c", prop_dict["selinux_fc"]])
Mohamad Ayyashc3484f72016-06-13 09:46:58 -0700478 if "block_list" in prop_dict:
479 build_command.extend(["-B", prop_dict["block_list"]])
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700480 if "squashfs_compressor" in prop_dict:
481 build_command.extend(["-z", prop_dict["squashfs_compressor"]])
482 if "squashfs_compressor_opt" in prop_dict:
483 build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700484 if "squashfs_block_size" in prop_dict:
485 build_command.extend(["-b", prop_dict["squashfs_block_size"]])
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700486 if "squashfs_disable_4k_align" in prop_dict and prop_dict.get("squashfs_disable_4k_align") == "true":
487 build_command.extend(["-a"])
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700488 elif fs_type.startswith("f2fs"):
489 build_command = ["mkf2fsuserimg.sh"]
490 build_command.extend([out_file, prop_dict["partition_size"]])
Ying Wangbd93d422011-10-28 17:02:30 -0700491 else:
Elliott Hughes305b0882016-06-15 17:04:54 -0700492 print("Error: unknown filesystem type '%s'" % (fs_type))
493 return False
Ying Wangbd93d422011-10-28 17:02:30 -0700494
Ying Wanga2292c92015-03-24 19:07:40 -0700495 if in_dir != origin_in:
496 # Construct a staging directory of the root file system.
497 ramdisk_dir = prop_dict.get("ramdisk_dir")
498 if ramdisk_dir:
499 shutil.rmtree(in_dir)
500 shutil.copytree(ramdisk_dir, in_dir, symlinks=True)
501 staging_system = os.path.join(in_dir, "system")
502 shutil.rmtree(staging_system, ignore_errors=True)
503 shutil.copytree(origin_in, staging_system, symlinks=True)
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700504
Julius D'souza001c6762017-05-03 13:43:27 -0700505 has_reserved_blocks = prop_dict.get("has_ext4_reserved_blocks") == "true"
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700506 ext4fs_output = None
507
Ying Wanga2292c92015-03-24 19:07:40 -0700508 try:
Julius D'souza001c6762017-05-03 13:43:27 -0700509 if fs_type.startswith("ext4"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700510 (ext4fs_output, exit_code) = RunCommand(build_command)
511 else:
512 (_, exit_code) = RunCommand(build_command)
Ying Wanga2292c92015-03-24 19:07:40 -0700513 finally:
514 if in_dir != origin_in:
515 # Clean up temporary directories and files.
516 shutil.rmtree(in_dir, ignore_errors=True)
517 if fs_config:
518 os.remove(fs_config)
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800519 if base_fs_file is not None:
520 os.remove(base_fs_file)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800521 if exit_code != 0:
522 return False
523
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700524 # Bug: 21522719, 22023465
525 # There are some reserved blocks on ext4 FS (lesser of 4096 blocks and 2%).
526 # We need to deduct those blocks from the available space, since they are
527 # not writable even with root privilege. It only affects devices using
528 # file-based OTA and a kernel version of 3.10 or greater (currently just
529 # sprout).
Julius D'souza001c6762017-05-03 13:43:27 -0700530 # Separately, check if there's enough headroom space available. This is useful for
531 # devices with low disk space that have system image variation between builds.
532 if (has_reserved_blocks or "partition_headroom" in prop_dict) and fs_type.startswith("ext4"):
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700533 assert ext4fs_output is not None
534 ext4fs_stats = re.compile(
535 r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
536 r'(?P<total_blocks>[0-9]+) blocks')
537 m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1])
538 used_blocks = int(m.groupdict().get('used_blocks'))
539 total_blocks = int(m.groupdict().get('total_blocks'))
Julius D'souza001c6762017-05-03 13:43:27 -0700540 reserved_blocks = 0
541 headroom_blocks = 0
542 adjusted_blocks = total_blocks
543 if has_reserved_blocks:
544 reserved_blocks = min(4096, int(total_blocks * 0.02))
545 adjusted_blocks -= reserved_blocks
546 if "partition_headroom" in prop_dict:
547 headroom_blocks = int(prop_dict.get('partition_headroom')) / BLOCK_SIZE
548 adjusted_blocks -= headroom_blocks
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700549 if used_blocks > adjusted_blocks:
550 mount_point = prop_dict.get("mount_point")
551 print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, "
Julius D'souza001c6762017-05-03 13:43:27 -0700552 "reserved: %d blocks, headroom: %d blocks, available: %d blocks)" % (
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700553 mount_point, total_blocks, used_blocks, reserved_blocks,
Julius D'souza001c6762017-05-03 13:43:27 -0700554 headroom_blocks, adjusted_blocks))
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700555 return False
556
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700557 if not fs_spans_partition:
558 mount_point = prop_dict.get("mount_point")
559 partition_size = int(prop_dict.get("partition_size"))
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800560 image_size = GetSimgSize(out_file)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700561 if image_size > partition_size:
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700562 print("Error: %s image size of %d is larger than partition size of "
563 "%d" % (mount_point, image_size, partition_size))
564 return False
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700565 if verity_supported and is_verity_partition:
Sami Tolvanen405e71d2016-02-09 12:28:58 -0800566 ZeroPadSimg(out_file, partition_size - image_size)
Mohamad Ayyashdd063522015-03-24 12:42:03 -0700567
Geremy Condrafd6f7512013-06-16 17:26:08 -0700568 # create the verified image if this is to be verified
Geremy Condra5b5f4952014-05-05 22:19:37 -0700569 if verity_supported and is_verity_partition:
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100570 if not MakeVerityEnabledImage(out_file, verity_fec_supported, prop_dict):
Geremy Condrafd6f7512013-06-16 17:26:08 -0700571 return False
572
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800573 # Add AVB HASH or HASHTREE footer (metadata).
574 if avb_footer_type:
David Zeuthen4014a9d2016-09-30 17:29:22 -0400575 avbtool = prop_dict.get("avb_avbtool")
576 original_partition_size = int(prop_dict.get("original_partition_size"))
577 partition_name = prop_dict["partition_name"]
578 signing_args = prop_dict["avb_signing_args"]
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800579 # avb_add_hash_footer_args or avb_add_hashtree_footer_args
580 additional_args = prop_dict["avb_add_" + avb_footer_type + "_footer_args"]
581 if not AVBAddFooter(out_file, avbtool, avb_footer_type, original_partition_size,
582 partition_name, signing_args, additional_args):
David Zeuthen4014a9d2016-09-30 17:29:22 -0400583 return False
584
Ying Wang6a42a252013-02-27 13:54:02 -0800585 if run_fsck and prop_dict.get("skip_fsck") != "true":
Geremy Condra6e8f53c2013-12-05 17:09:18 -0800586 success, unsparse_image = UnsparseImage(out_file, replace=False)
Geremy Condrafd6f7512013-06-16 17:26:08 -0700587 if not success:
Ying Wang69e9b4d2012-11-26 18:10:23 -0800588 return False
589
590 # Run e2fsck on the inflated image file
591 e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700592 (_, exit_code) = RunCommand(e2fsck_command)
Ying Wang69e9b4d2012-11-26 18:10:23 -0800593
594 os.remove(unsparse_image)
595
596 return exit_code == 0
Ying Wangbd93d422011-10-28 17:02:30 -0700597
598
599def ImagePropFromGlobalDict(glob_dict, mount_point):
600 """Build an image property dictionary from the global dictionary.
601
602 Args:
603 glob_dict: the global dictionary from the build system.
604 mount_point: such as "system", "data" etc.
605 """
Doug Zongker1ad7ade2013-12-06 11:53:27 -0800606 d = {}
Tao Bao052ae352015-09-28 13:44:13 -0700607
Tao Bao822f5842015-09-30 16:01:14 -0700608 if "build.prop" in glob_dict:
609 bp = glob_dict["build.prop"]
610 if "ro.build.date.utc" in bp:
611 d["timestamp"] = bp["ro.build.date.utc"]
Ying Wang9f8e8db2011-11-04 11:37:01 -0700612
613 def copy_prop(src_p, dest_p):
614 if src_p in glob_dict:
615 d[dest_p] = str(glob_dict[src_p])
616
Ying Wangbd93d422011-10-28 17:02:30 -0700617 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -0700618 "extfs_sparse_flag",
Todd Poynorb2a555e2015-12-15 18:00:14 -0800619 "squashfs_sparse_flag",
Kenny Rootf32dc712012-04-08 10:42:34 -0700620 "selinux_fc",
Ying Wang6a42a252013-02-27 13:54:02 -0800621 "skip_fsck",
Adrien Schildknecht9a072cc2016-11-18 17:06:29 -0800622 "ext_mkuserimg",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700623 "verity",
Geremy Condrafd6f7512013-06-16 17:26:08 -0700624 "verity_key",
Sami Tolvanenf99b5312015-05-20 07:30:57 +0100625 "verity_signer_cmd",
David Zeuthen4014a9d2016-09-30 17:29:22 -0400626 "verity_fec",
627 "avb_signing_args",
628 "avb_avbtool"
Ying Wangbd93d422011-10-28 17:02:30 -0700629 )
630 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -0700631 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -0700632
633 d["mount_point"] = mount_point
634 if mount_point == "system":
Ying Wang9f8e8db2011-11-04 11:37:01 -0700635 copy_prop("fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700636 # Copy the generic system fs type first, override with specific one if
Dan Albert8b72aef2015-03-23 19:13:21 -0700637 # available.
Mohamad Ayyashb97746e2015-03-03 12:30:37 -0800638 copy_prop("system_fs_type", "fs_type")
Julius D'souza001c6762017-05-03 13:43:27 -0700639 copy_prop("system_headroom", "partition_headroom")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700640 copy_prop("system_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800641 copy_prop("system_journal_size", "journal_size")
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700642 copy_prop("system_verity_block_device", "verity_block_device")
Tao Baof3282b42015-04-01 11:21:55 -0700643 copy_prop("system_root_image", "system_root_image")
644 copy_prop("ramdisk_dir", "ramdisk_dir")
Tao Bao84e75682015-07-19 02:38:53 -0700645 copy_prop("ramdisk_fs_config", "ramdisk_fs_config")
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700646 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Simon Wilsonf86e7ee2015-06-17 12:35:15 -0700647 copy_prop("system_squashfs_compressor", "squashfs_compressor")
648 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700649 copy_prop("system_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700650 copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800651 copy_prop("system_base_fs_file", "base_fs_file")
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800652 copy_prop("system_avb_hashtree_enable", "avb_hashtree_enable")
David Zeuthen4014a9d2016-09-30 17:29:22 -0400653 copy_prop("system_avb_add_hashtree_footer_args",
654 "avb_add_hashtree_footer_args")
Patrick Tjina1900842016-10-20 10:58:12 -0700655 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Alex Light4e358ab2016-06-16 14:47:10 -0700656 elif mount_point == "system_other":
657 # We inherit the selinux policies of /system since we contain some of its files.
658 d["mount_point"] = "system"
659 copy_prop("fs_type", "fs_type")
660 copy_prop("system_fs_type", "fs_type")
661 copy_prop("system_size", "partition_size")
662 copy_prop("system_journal_size", "journal_size")
663 copy_prop("system_verity_block_device", "verity_block_device")
664 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
665 copy_prop("system_squashfs_compressor", "squashfs_compressor")
666 copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
667 copy_prop("system_squashfs_block_size", "squashfs_block_size")
668 copy_prop("system_base_fs_file", "base_fs_file")
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800669 copy_prop("system_avb_hashtree_enable", "avb_hashtree_enable")
David Zeuthen4e9c89a2016-10-04 18:53:34 -0400670 copy_prop("system_avb_add_hashtree_footer_args",
671 "avb_add_hashtree_footer_args")
Patrick Tjina1900842016-10-20 10:58:12 -0700672 copy_prop("system_extfs_inode_count", "extfs_inode_count")
Ying Wangbd93d422011-10-28 17:02:30 -0700673 elif mount_point == "data":
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700674 # Copy the generic fs type first, override with specific one if available.
Ying Wang9f8e8db2011-11-04 11:37:01 -0700675 copy_prop("fs_type", "fs_type")
JP Abgrall5bfed5a2014-06-16 14:17:40 -0700676 copy_prop("userdata_fs_type", "fs_type")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700677 copy_prop("userdata_size", "partition_size")
Connor O'Brien20f08c32017-01-05 16:48:14 -0800678 copy_prop("flash_logical_block_size","flash_logical_block_size")
679 copy_prop("flash_erase_block_size", "flash_erase_block_size")
Ying Wang9f8e8db2011-11-04 11:37:01 -0700680 elif mount_point == "cache":
681 copy_prop("cache_fs_type", "fs_type")
682 copy_prop("cache_size", "partition_size")
Ying Wanga0febe52013-03-20 11:02:05 -0700683 elif mount_point == "vendor":
684 copy_prop("vendor_fs_type", "fs_type")
685 copy_prop("vendor_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800686 copy_prop("vendor_journal_size", "journal_size")
Daniel Rosenbergf4eabc32014-07-10 15:42:38 -0700687 copy_prop("vendor_verity_block_device", "verity_block_device")
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700688 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Patrick Tjine11aa502016-02-09 15:40:38 -0800689 copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
690 copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
Mohamad Ayyashdfec8152016-05-24 12:59:30 -0700691 copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
Mohamad Ayyash1b6d3482016-06-15 15:53:07 -0700692 copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align")
Mohamad Ayyashf8765552016-03-02 21:07:23 -0800693 copy_prop("vendor_base_fs_file", "base_fs_file")
Bowgo Tsai7ea994b2017-05-19 23:44:26 +0800694 copy_prop("vendor_avb_hashtree_enable", "avb_hashtree_enable")
David Zeuthen4014a9d2016-09-30 17:29:22 -0400695 copy_prop("vendor_avb_add_hashtree_footer_args",
696 "avb_add_hashtree_footer_args")
Patrick Tjina1900842016-10-20 10:58:12 -0700697 copy_prop("vendor_extfs_inode_count", "extfs_inode_count")
Ying Wangb8888432014-03-11 17:13:27 -0700698 elif mount_point == "oem":
699 copy_prop("fs_type", "fs_type")
700 copy_prop("oem_size", "partition_size")
Ying Wangf3b86352014-11-18 18:03:13 -0800701 copy_prop("oem_journal_size", "journal_size")
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700702 copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks")
Patrick Tjina1900842016-10-20 10:58:12 -0700703 copy_prop("oem_extfs_inode_count", "extfs_inode_count")
David Zeuthen4014a9d2016-09-30 17:29:22 -0400704 d["partition_name"] = mount_point
Ying Wangbd93d422011-10-28 17:02:30 -0700705 return d
706
707
708def LoadGlobalDict(filename):
709 """Load "name=value" pairs from filename"""
710 d = {}
711 f = open(filename)
712 for line in f:
713 line = line.strip()
714 if not line or line.startswith("#"):
715 continue
716 k, v = line.split("=", 1)
717 d[k] = v
718 f.close()
719 return d
720
721
722def main(argv):
Thierry Strudel74a81e62015-07-09 09:54:55 -0700723 if len(argv) != 4:
Ying Wangbd93d422011-10-28 17:02:30 -0700724 print __doc__
725 sys.exit(1)
726
727 in_dir = argv[0]
728 glob_dict_file = argv[1]
729 out_file = argv[2]
Thierry Strudel74a81e62015-07-09 09:54:55 -0700730 target_out = argv[3]
Ying Wangbd93d422011-10-28 17:02:30 -0700731
732 glob_dict = LoadGlobalDict(glob_dict_file)
Ying Wangae61f502015-03-12 18:30:39 -0700733 if "mount_point" in glob_dict:
Tao Baoc7a6f1e2015-06-23 11:16:05 -0700734 # The caller knows the mount point and provides a dictionay needed by
735 # BuildImage().
Ying Wangae61f502015-03-12 18:30:39 -0700736 image_properties = glob_dict
Ying Wang9f8e8db2011-11-04 11:37:01 -0700737 else:
Ying Wangae61f502015-03-12 18:30:39 -0700738 image_filename = os.path.basename(out_file)
739 mount_point = ""
740 if image_filename == "system.img":
741 mount_point = "system"
Alex Light4e358ab2016-06-16 14:47:10 -0700742 elif image_filename == "system_other.img":
743 mount_point = "system_other"
Ying Wangae61f502015-03-12 18:30:39 -0700744 elif image_filename == "userdata.img":
745 mount_point = "data"
746 elif image_filename == "cache.img":
747 mount_point = "cache"
748 elif image_filename == "vendor.img":
749 mount_point = "vendor"
750 elif image_filename == "oem.img":
751 mount_point = "oem"
752 else:
753 print >> sys.stderr, "error: unknown image file name ", image_filename
754 exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -0700755
Ying Wangae61f502015-03-12 18:30:39 -0700756 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
757
Thierry Strudel74a81e62015-07-09 09:54:55 -0700758 if not BuildImage(in_dir, image_properties, out_file, target_out):
Dan Albert8b72aef2015-03-23 19:13:21 -0700759 print >> sys.stderr, "error: failed to build %s from %s" % (out_file,
760 in_dir)
Ying Wangbd93d422011-10-28 17:02:30 -0700761 exit(1)
762
763
764if __name__ == '__main__':
765 main(sys.argv[1:])