1#!/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"""
18Builds output_image from the given input_directory, properties_file,
19and writes the image to target_output_directory.
20
21Usage:  build_image input_directory properties_file output_image \\
22            target_output_directory
23"""
24
25from __future__ import print_function
26
27import logging
28import os
29import os.path
30import re
31import shutil
32import sys
33
34import common
35import verity_utils
36
37logger = logging.getLogger(__name__)
38
39OPTIONS = common.OPTIONS
40BLOCK_SIZE = common.BLOCK_SIZE
41BYTES_IN_MB = 1024 * 1024
42
43
44class BuildImageError(Exception):
45  """An Exception raised during image building."""
46
47  def __init__(self, message):
48    Exception.__init__(self, message)
49
50
51def GetDiskUsage(path):
52  """Returns the number of bytes that "path" occupies on host.
53
54  Args:
55    path: The directory or file to calculate size on.
56
57  Returns:
58    The number of bytes based on a 1K block_size.
59  """
60  cmd = ["du", "-b", "-k", "-s", path]
61  output = common.RunAndCheckOutput(cmd, verbose=False)
62  return int(output.split()[0]) * 1024
63
64
65def GetInodeUsage(path):
66  """Returns the number of inodes that "path" occupies on host.
67
68  Args:
69    path: The directory or file to calculate inode number on.
70
71  Returns:
72    The number of inodes used.
73  """
74  cmd = ["find", path, "-print"]
75  output = common.RunAndCheckOutput(cmd, verbose=False)
76  # increase by > 6% as number of files and directories is not whole picture.
77  inodes = output.count('\n')
78  spare_inodes = inodes * 6 // 100
79  min_spare_inodes = 12
80  if spare_inodes < min_spare_inodes:
81    spare_inodes = min_spare_inodes
82  return inodes + spare_inodes
83
84
85def GetFilesystemCharacteristics(fs_type, image_path, sparse_image=True):
86  """Returns various filesystem characteristics of "image_path".
87
88  Args:
89    image_path: The file to analyze.
90    sparse_image: Image is sparse
91
92  Returns:
93    The characteristics dictionary.
94  """
95  unsparse_image_path = image_path
96  if sparse_image:
97    unsparse_image_path = UnsparseImage(image_path, replace=False)
98
99  if fs_type.startswith("ext"):
100    cmd = ["tune2fs", "-l", unsparse_image_path]
101  elif fs_type.startswith("f2fs"):
102    cmd = ["fsck.f2fs", "-l", unsparse_image_path]
103
104  try:
105    output = common.RunAndCheckOutput(cmd, verbose=False)
106  finally:
107    if sparse_image:
108      os.remove(unsparse_image_path)
109  fs_dict = {}
110  for line in output.splitlines():
111    fields = line.split(":")
112    if len(fields) == 2:
113      fs_dict[fields[0].strip()] = fields[1].strip()
114  return fs_dict
115
116
117def UnsparseImage(sparse_image_path, replace=True):
118  img_dir = os.path.dirname(sparse_image_path)
119  unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
120  unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
121  if os.path.exists(unsparse_image_path):
122    if replace:
123      os.unlink(unsparse_image_path)
124    else:
125      return unsparse_image_path
126  inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
127  try:
128    common.RunAndCheckOutput(inflate_command)
129  except:
130    os.remove(unsparse_image_path)
131    raise
132  return unsparse_image_path
133
134
135def ConvertBlockMapToBaseFs(block_map_file):
136  base_fs_file = common.MakeTempFile(prefix="script_gen_", suffix=".base_fs")
137  convert_command = ["blk_alloc_to_base_fs", block_map_file, base_fs_file]
138  common.RunAndCheckOutput(convert_command)
139  return base_fs_file
140
141
142def SetUpInDirAndFsConfig(origin_in, prop_dict):
143  """Returns the in_dir and fs_config that should be used for image building.
144
145  When building system.img for all targets, it creates and returns a staged dir
146  that combines the contents of /system (i.e. in the given in_dir) and root.
147
148  Args:
149    origin_in: Path to the input directory.
150    prop_dict: A property dict that contains info like partition size. Values
151        may be updated.
152
153  Returns:
154    A tuple of in_dir and fs_config that should be used to build the image.
155  """
156  fs_config = prop_dict.get("fs_config")
157
158  if prop_dict["mount_point"] == "system_other":
159    prop_dict["mount_point"] = "system"
160    return origin_in, fs_config
161
162  if prop_dict["mount_point"] != "system":
163    return origin_in, fs_config
164
165  if "first_pass" in prop_dict:
166    prop_dict["mount_point"] = "/"
167    return prop_dict["first_pass"]
168
169  # Construct a staging directory of the root file system.
170  in_dir = common.MakeTempDir()
171  root_dir = prop_dict.get("root_dir")
172  if root_dir:
173    shutil.rmtree(in_dir)
174    shutil.copytree(root_dir, in_dir, symlinks=True)
175  in_dir_system = os.path.join(in_dir, "system")
176  shutil.rmtree(in_dir_system, ignore_errors=True)
177  shutil.copytree(origin_in, in_dir_system, symlinks=True)
178
179  # Change the mount point to "/".
180  prop_dict["mount_point"] = "/"
181  if fs_config:
182    # We need to merge the fs_config files of system and root.
183    merged_fs_config = common.MakeTempFile(
184        prefix="merged_fs_config", suffix=".txt")
185    with open(merged_fs_config, "w") as fw:
186      if "root_fs_config" in prop_dict:
187        with open(prop_dict["root_fs_config"]) as fr:
188          fw.writelines(fr.readlines())
189      with open(fs_config) as fr:
190        fw.writelines(fr.readlines())
191    fs_config = merged_fs_config
192  prop_dict["first_pass"] = (in_dir, fs_config)
193  return in_dir, fs_config
194
195
196def CheckHeadroom(ext4fs_output, prop_dict):
197  """Checks if there's enough headroom space available.
198
199  Headroom is the reserved space on system image (via PRODUCT_SYSTEM_HEADROOM),
200  which is useful for devices with low disk space that have system image
201  variation between builds. The 'partition_headroom' in prop_dict is the size
202  in bytes, while the numbers in 'ext4fs_output' are for 4K-blocks.
203
204  Args:
205    ext4fs_output: The output string from mke2fs command.
206    prop_dict: The property dict.
207
208  Raises:
209    AssertionError: On invalid input.
210    BuildImageError: On check failure.
211  """
212  assert ext4fs_output is not None
213  assert prop_dict.get('fs_type', '').startswith('ext4')
214  assert 'partition_headroom' in prop_dict
215  assert 'mount_point' in prop_dict
216
217  ext4fs_stats = re.compile(
218      r'Created filesystem with .* (?P<used_blocks>[0-9]+)/'
219      r'(?P<total_blocks>[0-9]+) blocks')
220  last_line = ext4fs_output.strip().split('\n')[-1]
221  m = ext4fs_stats.match(last_line)
222  used_blocks = int(m.groupdict().get('used_blocks'))
223  total_blocks = int(m.groupdict().get('total_blocks'))
224  headroom_blocks = int(prop_dict['partition_headroom']) // BLOCK_SIZE
225  adjusted_blocks = total_blocks - headroom_blocks
226  if used_blocks > adjusted_blocks:
227    mount_point = prop_dict["mount_point"]
228    raise BuildImageError(
229        "Error: Not enough room on {} (total: {} blocks, used: {} blocks, "
230        "headroom: {} blocks, available: {} blocks)".format(
231            mount_point, total_blocks, used_blocks, headroom_blocks,
232            adjusted_blocks))
233
234
235def BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config):
236  """Builds a pure image for the files under in_dir and writes it to out_file.
237
238  Args:
239    in_dir: Path to input directory.
240    prop_dict: A property dict that contains info like partition size. Values
241        will be updated with computed values.
242    out_file: The output image file.
243    target_out: Path to the TARGET_OUT directory as in Makefile. It actually
244        points to the /system directory under PRODUCT_OUT. fs_config (the one
245        under system/core/libcutils) reads device specific FS config files from
246        there.
247    fs_config: The fs_config file that drives the prototype
248
249  Raises:
250    BuildImageError: On build image failures.
251  """
252  build_command = []
253  fs_type = prop_dict.get("fs_type", "")
254  run_e2fsck = False
255  needs_projid = prop_dict.get("needs_projid", 0)
256  needs_casefold = prop_dict.get("needs_casefold", 0)
257  needs_compress = prop_dict.get("needs_compress", 0)
258
259  if fs_type.startswith("ext"):
260    build_command = [prop_dict["ext_mkuserimg"]]
261    if "extfs_sparse_flag" in prop_dict:
262      build_command.append(prop_dict["extfs_sparse_flag"])
263      run_e2fsck = True
264    build_command.extend([in_dir, out_file, fs_type,
265                          prop_dict["mount_point"]])
266    build_command.append(prop_dict["image_size"])
267    if "journal_size" in prop_dict:
268      build_command.extend(["-j", prop_dict["journal_size"]])
269    if "timestamp" in prop_dict:
270      build_command.extend(["-T", str(prop_dict["timestamp"])])
271    if fs_config:
272      build_command.extend(["-C", fs_config])
273    if target_out:
274      build_command.extend(["-D", target_out])
275    if "block_list" in prop_dict:
276      build_command.extend(["-B", prop_dict["block_list"]])
277    if "base_fs_file" in prop_dict:
278      base_fs_file = ConvertBlockMapToBaseFs(prop_dict["base_fs_file"])
279      build_command.extend(["-d", base_fs_file])
280    build_command.extend(["-L", prop_dict["mount_point"]])
281    if "extfs_inode_count" in prop_dict:
282      build_command.extend(["-i", prop_dict["extfs_inode_count"]])
283    if "extfs_rsv_pct" in prop_dict:
284      build_command.extend(["-M", prop_dict["extfs_rsv_pct"]])
285    if "flash_erase_block_size" in prop_dict:
286      build_command.extend(["-e", prop_dict["flash_erase_block_size"]])
287    if "flash_logical_block_size" in prop_dict:
288      build_command.extend(["-o", prop_dict["flash_logical_block_size"]])
289    # Specify UUID and hash_seed if using mke2fs.
290    if prop_dict["ext_mkuserimg"] == "mkuserimg_mke2fs":
291      if "uuid" in prop_dict:
292        build_command.extend(["-U", prop_dict["uuid"]])
293      if "hash_seed" in prop_dict:
294        build_command.extend(["-S", prop_dict["hash_seed"]])
295    if prop_dict.get("ext4_share_dup_blocks") == "true":
296      build_command.append("-c")
297    if (needs_projid):
298      build_command.extend(["--inode_size", "512"])
299    else:
300      build_command.extend(["--inode_size", "256"])
301    if "selinux_fc" in prop_dict:
302      build_command.append(prop_dict["selinux_fc"])
303  elif fs_type.startswith("erofs"):
304    build_command = ["mkerofsimage.sh"]
305    build_command.extend([in_dir, out_file])
306    if "erofs_sparse_flag" in prop_dict:
307      build_command.extend([prop_dict["erofs_sparse_flag"]])
308    build_command.extend(["-m", prop_dict["mount_point"]])
309    if target_out:
310      build_command.extend(["-d", target_out])
311    if fs_config:
312      build_command.extend(["-C", fs_config])
313    if "selinux_fc" in prop_dict:
314      build_command.extend(["-c", prop_dict["selinux_fc"]])
315    if "timestamp" in prop_dict:
316      build_command.extend(["-T", str(prop_dict["timestamp"])])
317    if "uuid" in prop_dict:
318      build_command.extend(["-U", prop_dict["uuid"]])
319  elif fs_type.startswith("squash"):
320    build_command = ["mksquashfsimage.sh"]
321    build_command.extend([in_dir, out_file])
322    if "squashfs_sparse_flag" in prop_dict:
323      build_command.extend([prop_dict["squashfs_sparse_flag"]])
324    build_command.extend(["-m", prop_dict["mount_point"]])
325    if target_out:
326      build_command.extend(["-d", target_out])
327    if fs_config:
328      build_command.extend(["-C", fs_config])
329    if "selinux_fc" in prop_dict:
330      build_command.extend(["-c", prop_dict["selinux_fc"]])
331    if "block_list" in prop_dict:
332      build_command.extend(["-B", prop_dict["block_list"]])
333    if "squashfs_block_size" in prop_dict:
334      build_command.extend(["-b", prop_dict["squashfs_block_size"]])
335    if "squashfs_compressor" in prop_dict:
336      build_command.extend(["-z", prop_dict["squashfs_compressor"]])
337    if "squashfs_compressor_opt" in prop_dict:
338      build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]])
339    if prop_dict.get("squashfs_disable_4k_align") == "true":
340      build_command.extend(["-a"])
341  elif fs_type.startswith("f2fs"):
342    build_command = ["mkf2fsuserimg.sh"]
343    build_command.extend([out_file, prop_dict["image_size"]])
344    if "f2fs_sparse_flag" in prop_dict:
345      build_command.extend([prop_dict["f2fs_sparse_flag"]])
346    if fs_config:
347      build_command.extend(["-C", fs_config])
348    build_command.extend(["-f", in_dir])
349    if target_out:
350      build_command.extend(["-D", target_out])
351    if "selinux_fc" in prop_dict:
352      build_command.extend(["-s", prop_dict["selinux_fc"]])
353    build_command.extend(["-t", prop_dict["mount_point"]])
354    if "timestamp" in prop_dict:
355      build_command.extend(["-T", str(prop_dict["timestamp"])])
356    build_command.extend(["-L", prop_dict["mount_point"]])
357    if (needs_projid):
358      build_command.append("--prjquota")
359    if (needs_casefold):
360      build_command.append("--casefold")
361    if (needs_compress or prop_dict.get("f2fs_compress") == "true"):
362      build_command.append("--compression")
363    if (prop_dict.get("f2fs_compress") == "true"):
364      build_command.append("--readonly")
365      build_command.append("--sldc")
366      if (prop_dict.get("f2fs_sldc_flags") == None):
367        build_command.append(str(0))
368      else:
369        sldc_flags_str = prop_dict.get("f2fs_sldc_flags")
370        sldc_flags = sldc_flags_str.split()
371        build_command.append(str(len(sldc_flags)))
372        build_command.extend(sldc_flags)
373  else:
374    raise BuildImageError(
375        "Error: unknown filesystem type: {}".format(fs_type))
376
377  try:
378    mkfs_output = common.RunAndCheckOutput(build_command)
379  except:
380    try:
381      du = GetDiskUsage(in_dir)
382      du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB)
383    # Suppress any errors from GetDiskUsage() to avoid hiding the real errors
384    # from common.RunAndCheckOutput().
385    except Exception:  # pylint: disable=broad-except
386      logger.exception("Failed to compute disk usage with du")
387      du_str = "unknown"
388    print(
389        "Out of space? Out of inodes? The tree size of {} is {}, "
390        "with reserved space of {} bytes ({} MB).".format(
391            in_dir, du_str,
392            int(prop_dict.get("partition_reserved_size", 0)),
393            int(prop_dict.get("partition_reserved_size", 0)) // BYTES_IN_MB))
394    if ("image_size" in prop_dict and "partition_size" in prop_dict):
395      print(
396          "The max image size for filesystem files is {} bytes ({} MB), "
397          "out of a total partition size of {} bytes ({} MB).".format(
398              int(prop_dict["image_size"]),
399              int(prop_dict["image_size"]) // BYTES_IN_MB,
400              int(prop_dict["partition_size"]),
401              int(prop_dict["partition_size"]) // BYTES_IN_MB))
402    raise
403
404  if run_e2fsck and prop_dict.get("skip_fsck") != "true":
405    unsparse_image = UnsparseImage(out_file, replace=False)
406
407    # Run e2fsck on the inflated image file
408    e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
409    try:
410      common.RunAndCheckOutput(e2fsck_command)
411    finally:
412      os.remove(unsparse_image)
413
414  return mkfs_output
415
416
417def BuildImage(in_dir, prop_dict, out_file, target_out=None):
418  """Builds an image for the files under in_dir and writes it to out_file.
419
420  Args:
421    in_dir: Path to input directory.
422    prop_dict: A property dict that contains info like partition size. Values
423        will be updated with computed values.
424    out_file: The output image file.
425    target_out: Path to the TARGET_OUT directory as in Makefile. It actually
426        points to the /system directory under PRODUCT_OUT. fs_config (the one
427        under system/core/libcutils) reads device specific FS config files from
428        there.
429
430  Raises:
431    BuildImageError: On build image failures.
432  """
433  in_dir, fs_config = SetUpInDirAndFsConfig(in_dir, prop_dict)
434
435  build_command = []
436  fs_type = prop_dict.get("fs_type", "")
437
438  fs_spans_partition = True
439  if fs_type.startswith("squash") or fs_type.startswith("erofs"):
440    fs_spans_partition = False
441  elif fs_type.startswith("f2fs") and prop_dict.get("f2fs_compress") == "true":
442    fs_spans_partition = False
443
444  # Get a builder for creating an image that's to be verified by Verified Boot,
445  # or None if not applicable.
446  verity_image_builder = verity_utils.CreateVerityImageBuilder(prop_dict)
447
448  if (prop_dict.get("use_dynamic_partition_size") == "true" and
449      "partition_size" not in prop_dict):
450    # If partition_size is not defined, use output of `du' + reserved_size.
451    # For compressed file system, it's better to use the compressed size to avoid wasting space.
452    if fs_type.startswith("erofs"):
453      tmp_dict = prop_dict.copy()
454      if "erofs_sparse_flag" in tmp_dict:
455        tmp_dict.pop("erofs_sparse_flag")
456      BuildImageMkfs(in_dir, tmp_dict, out_file, target_out, fs_config)
457      size = GetDiskUsage(out_file)
458      os.remove(out_file)
459    else:
460      size = GetDiskUsage(in_dir)
461    logger.info(
462        "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB)
463    # If not specified, give us 16MB margin for GetDiskUsage error ...
464    reserved_size = int(prop_dict.get("partition_reserved_size", BYTES_IN_MB * 16))
465    partition_headroom = int(prop_dict.get("partition_headroom", 0))
466    if fs_type.startswith("ext4") and partition_headroom > reserved_size:
467      reserved_size = partition_headroom
468    size += reserved_size
469    # Round this up to a multiple of 4K so that avbtool works
470    size = common.RoundUpTo4K(size)
471    if fs_type.startswith("ext"):
472      prop_dict["partition_size"] = str(size)
473      prop_dict["image_size"] = str(size)
474      if "extfs_inode_count" not in prop_dict:
475        prop_dict["extfs_inode_count"] = str(GetInodeUsage(in_dir))
476      logger.info(
477          "First Pass based on estimates of %d MB and %s inodes.",
478          size // BYTES_IN_MB, prop_dict["extfs_inode_count"])
479      BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config)
480      sparse_image = False
481      if "extfs_sparse_flag" in prop_dict:
482        sparse_image = True
483      fs_dict = GetFilesystemCharacteristics(fs_type, out_file, sparse_image)
484      os.remove(out_file)
485      block_size = int(fs_dict.get("Block size", "4096"))
486      free_size = int(fs_dict.get("Free blocks", "0")) * block_size
487      reserved_size = int(prop_dict.get("partition_reserved_size", 0))
488      partition_headroom = int(fs_dict.get("partition_headroom", 0))
489      if fs_type.startswith("ext4") and partition_headroom > reserved_size:
490        reserved_size = partition_headroom
491      if free_size <= reserved_size:
492        logger.info(
493            "Not worth reducing image %d <= %d.", free_size, reserved_size)
494      else:
495        size -= free_size
496        size += reserved_size
497        if reserved_size == 0:
498          # add .3% margin
499          size = size * 1003 // 1000
500        # Use a minimum size, otherwise we will fail to calculate an AVB footer
501        # or fail to construct an ext4 image.
502        size = max(size, 256 * 1024)
503        if block_size <= 4096:
504          size = common.RoundUpTo4K(size)
505        else:
506          size = ((size + block_size - 1) // block_size) * block_size
507      extfs_inode_count = prop_dict["extfs_inode_count"]
508      inodes = int(fs_dict.get("Inode count", extfs_inode_count))
509      inodes -= int(fs_dict.get("Free inodes", "0"))
510      # add .2% margin or 1 inode, whichever is greater
511      spare_inodes = inodes * 2 // 1000
512      min_spare_inodes = 1
513      if spare_inodes < min_spare_inodes:
514        spare_inodes = min_spare_inodes
515      inodes += spare_inodes
516      prop_dict["extfs_inode_count"] = str(inodes)
517      prop_dict["partition_size"] = str(size)
518      logger.info(
519          "Allocating %d Inodes for %s.", inodes, out_file)
520    elif fs_type.startswith("f2fs") and prop_dict.get("f2fs_compress") == "true":
521      prop_dict["partition_size"] = str(size)
522      prop_dict["image_size"] = str(size)
523      BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config)
524      sparse_image = False
525      if "f2fs_sparse_flag" in prop_dict:
526        sparse_image = True
527      fs_dict = GetFilesystemCharacteristics(fs_type, out_file, sparse_image)
528      os.remove(out_file)
529      block_count = int(fs_dict.get("block_count", "0"))
530      log_blocksize = int(fs_dict.get("log_blocksize", "12"))
531      size = block_count << log_blocksize
532      prop_dict["partition_size"] = str(size)
533    if verity_image_builder:
534      size = verity_image_builder.CalculateDynamicPartitionSize(size)
535    prop_dict["partition_size"] = str(size)
536    logger.info(
537        "Allocating %d MB for %s.", size // BYTES_IN_MB, out_file)
538
539  prop_dict["image_size"] = prop_dict["partition_size"]
540
541  # Adjust the image size to make room for the hashes if this is to be verified.
542  if verity_image_builder:
543    max_image_size = verity_image_builder.CalculateMaxImageSize()
544    prop_dict["image_size"] = str(max_image_size)
545
546  mkfs_output = BuildImageMkfs(in_dir, prop_dict, out_file, target_out, fs_config)
547
548  # Check if there's enough headroom space available for ext4 image.
549  if "partition_headroom" in prop_dict and fs_type.startswith("ext4"):
550    CheckHeadroom(mkfs_output, prop_dict)
551
552  if not fs_spans_partition and verity_image_builder:
553    verity_image_builder.PadSparseImage(out_file)
554
555  # Create the verified image if this is to be verified.
556  if verity_image_builder:
557    verity_image_builder.Build(out_file)
558
559
560def ImagePropFromGlobalDict(glob_dict, mount_point):
561  """Build an image property dictionary from the global dictionary.
562
563  Args:
564    glob_dict: the global dictionary from the build system.
565    mount_point: such as "system", "data" etc.
566  """
567  d = {}
568
569  if "build.prop" in glob_dict:
570    timestamp = glob_dict["build.prop"].GetProp("ro.build.date.utc")
571    if timestamp:
572      d["timestamp"] = timestamp
573
574  def copy_prop(src_p, dest_p):
575    """Copy a property from the global dictionary.
576
577    Args:
578      src_p: The source property in the global dictionary.
579      dest_p: The destination property.
580    Returns:
581      True if property was found and copied, False otherwise.
582    """
583    if src_p in glob_dict:
584      d[dest_p] = str(glob_dict[src_p])
585      return True
586    return False
587
588  common_props = (
589      "extfs_sparse_flag",
590      "erofs_sparse_flag",
591      "squashfs_sparse_flag",
592      "system_f2fs_compress",
593      "system_f2fs_sldc_flags",
594      "f2fs_sparse_flag",
595      "skip_fsck",
596      "ext_mkuserimg",
597      "verity",
598      "verity_key",
599      "verity_signer_cmd",
600      "verity_fec",
601      "verity_disable",
602      "avb_enable",
603      "avb_avbtool",
604      "use_dynamic_partition_size",
605  )
606  for p in common_props:
607    copy_prop(p, p)
608
609  d["mount_point"] = mount_point
610  if mount_point == "system":
611    copy_prop("avb_system_hashtree_enable", "avb_hashtree_enable")
612    copy_prop("avb_system_add_hashtree_footer_args",
613              "avb_add_hashtree_footer_args")
614    copy_prop("avb_system_key_path", "avb_key_path")
615    copy_prop("avb_system_algorithm", "avb_algorithm")
616    copy_prop("avb_system_salt", "avb_salt")
617    copy_prop("fs_type", "fs_type")
618    # Copy the generic system fs type first, override with specific one if
619    # available.
620    copy_prop("system_fs_type", "fs_type")
621    copy_prop("system_headroom", "partition_headroom")
622    copy_prop("system_size", "partition_size")
623    if not copy_prop("system_journal_size", "journal_size"):
624      d["journal_size"] = "0"
625    copy_prop("system_verity_block_device", "verity_block_device")
626    copy_prop("system_root_image", "system_root_image")
627    copy_prop("root_dir", "root_dir")
628    copy_prop("root_fs_config", "root_fs_config")
629    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
630    copy_prop("system_f2fs_compress", "f2fs_compress")
631    copy_prop("system_f2fs_sldc_flags", "f2fs_sldc_flags")
632    copy_prop("system_squashfs_compressor", "squashfs_compressor")
633    copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
634    copy_prop("system_squashfs_block_size", "squashfs_block_size")
635    copy_prop("system_squashfs_disable_4k_align", "squashfs_disable_4k_align")
636    copy_prop("system_base_fs_file", "base_fs_file")
637    copy_prop("system_extfs_inode_count", "extfs_inode_count")
638    if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
639      d["extfs_rsv_pct"] = "0"
640    copy_prop("system_reserved_size", "partition_reserved_size")
641    copy_prop("system_selinux_fc", "selinux_fc")
642  elif mount_point == "system_other":
643    # We inherit the selinux policies of /system since we contain some of its
644    # files.
645    copy_prop("avb_system_other_hashtree_enable", "avb_hashtree_enable")
646    copy_prop("avb_system_other_add_hashtree_footer_args",
647              "avb_add_hashtree_footer_args")
648    copy_prop("avb_system_other_key_path", "avb_key_path")
649    copy_prop("avb_system_other_algorithm", "avb_algorithm")
650    copy_prop("avb_system_other_salt", "avb_salt")
651    copy_prop("fs_type", "fs_type")
652    copy_prop("system_fs_type", "fs_type")
653    copy_prop("system_other_size", "partition_size")
654    if not copy_prop("system_journal_size", "journal_size"):
655      d["journal_size"] = "0"
656    copy_prop("system_verity_block_device", "verity_block_device")
657    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
658    copy_prop("system_f2fs_compress", "f2fs_compress")
659    copy_prop("system_f2fs_sldc_flags", "f2fs_sldc_flags")
660    copy_prop("system_squashfs_compressor", "squashfs_compressor")
661    copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt")
662    copy_prop("system_squashfs_block_size", "squashfs_block_size")
663    copy_prop("system_extfs_inode_count", "extfs_inode_count")
664    if not copy_prop("system_extfs_rsv_pct", "extfs_rsv_pct"):
665      d["extfs_rsv_pct"] = "0"
666    copy_prop("system_reserved_size", "partition_reserved_size")
667    copy_prop("system_selinux_fc", "selinux_fc")
668  elif mount_point == "data":
669    # Copy the generic fs type first, override with specific one if available.
670    copy_prop("fs_type", "fs_type")
671    copy_prop("userdata_fs_type", "fs_type")
672    copy_prop("userdata_size", "partition_size")
673    copy_prop("flash_logical_block_size", "flash_logical_block_size")
674    copy_prop("flash_erase_block_size", "flash_erase_block_size")
675    copy_prop("userdata_selinux_fc", "selinux_fc")
676    copy_prop("needs_casefold", "needs_casefold")
677    copy_prop("needs_projid", "needs_projid")
678    copy_prop("needs_compress", "needs_compress")
679  elif mount_point == "cache":
680    copy_prop("cache_fs_type", "fs_type")
681    copy_prop("cache_size", "partition_size")
682    copy_prop("cache_selinux_fc", "selinux_fc")
683  elif mount_point == "vendor":
684    copy_prop("avb_vendor_hashtree_enable", "avb_hashtree_enable")
685    copy_prop("avb_vendor_add_hashtree_footer_args",
686              "avb_add_hashtree_footer_args")
687    copy_prop("avb_vendor_key_path", "avb_key_path")
688    copy_prop("avb_vendor_algorithm", "avb_algorithm")
689    copy_prop("avb_vendor_salt", "avb_salt")
690    copy_prop("vendor_fs_type", "fs_type")
691    copy_prop("vendor_size", "partition_size")
692    if not copy_prop("vendor_journal_size", "journal_size"):
693      d["journal_size"] = "0"
694    copy_prop("vendor_verity_block_device", "verity_block_device")
695    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
696    copy_prop("vendor_f2fs_compress", "f2fs_compress")
697    copy_prop("vendor_f2fs_sldc_flags", "f2fs_sldc_flags")
698    copy_prop("vendor_squashfs_compressor", "squashfs_compressor")
699    copy_prop("vendor_squashfs_compressor_opt", "squashfs_compressor_opt")
700    copy_prop("vendor_squashfs_block_size", "squashfs_block_size")
701    copy_prop("vendor_squashfs_disable_4k_align", "squashfs_disable_4k_align")
702    copy_prop("vendor_base_fs_file", "base_fs_file")
703    copy_prop("vendor_extfs_inode_count", "extfs_inode_count")
704    if not copy_prop("vendor_extfs_rsv_pct", "extfs_rsv_pct"):
705      d["extfs_rsv_pct"] = "0"
706    copy_prop("vendor_reserved_size", "partition_reserved_size")
707    copy_prop("vendor_selinux_fc", "selinux_fc")
708  elif mount_point == "product":
709    copy_prop("avb_product_hashtree_enable", "avb_hashtree_enable")
710    copy_prop("avb_product_add_hashtree_footer_args",
711              "avb_add_hashtree_footer_args")
712    copy_prop("avb_product_key_path", "avb_key_path")
713    copy_prop("avb_product_algorithm", "avb_algorithm")
714    copy_prop("avb_product_salt", "avb_salt")
715    copy_prop("product_fs_type", "fs_type")
716    copy_prop("product_size", "partition_size")
717    if not copy_prop("product_journal_size", "journal_size"):
718      d["journal_size"] = "0"
719    copy_prop("product_verity_block_device", "verity_block_device")
720    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
721    copy_prop("product_f2fs_compress", "f2fs_compress")
722    copy_prop("product_f2fs_sldc_flags", "f2fs_sldc_flags")
723    copy_prop("product_squashfs_compressor", "squashfs_compressor")
724    copy_prop("product_squashfs_compressor_opt", "squashfs_compressor_opt")
725    copy_prop("product_squashfs_block_size", "squashfs_block_size")
726    copy_prop("product_squashfs_disable_4k_align", "squashfs_disable_4k_align")
727    copy_prop("product_base_fs_file", "base_fs_file")
728    copy_prop("product_extfs_inode_count", "extfs_inode_count")
729    if not copy_prop("product_extfs_rsv_pct", "extfs_rsv_pct"):
730      d["extfs_rsv_pct"] = "0"
731    copy_prop("product_reserved_size", "partition_reserved_size")
732    copy_prop("product_selinux_fc", "selinux_fc")
733  elif mount_point == "system_ext":
734    copy_prop("avb_system_ext_hashtree_enable", "avb_hashtree_enable")
735    copy_prop("avb_system_ext_add_hashtree_footer_args",
736              "avb_add_hashtree_footer_args")
737    copy_prop("avb_system_ext_key_path", "avb_key_path")
738    copy_prop("avb_system_ext_algorithm", "avb_algorithm")
739    copy_prop("avb_system_ext_salt", "avb_salt")
740    copy_prop("system_ext_fs_type", "fs_type")
741    copy_prop("system_ext_size", "partition_size")
742    if not copy_prop("system_ext_journal_size", "journal_size"):
743      d["journal_size"] = "0"
744    copy_prop("system_ext_verity_block_device", "verity_block_device")
745    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
746    copy_prop("system_ext_f2fs_compress", "f2fs_compress")
747    copy_prop("system_ext_f2fs_sldc_flags", "f2fs_sldc_flags")
748    copy_prop("system_ext_squashfs_compressor", "squashfs_compressor")
749    copy_prop("system_ext_squashfs_compressor_opt",
750              "squashfs_compressor_opt")
751    copy_prop("system_ext_squashfs_block_size", "squashfs_block_size")
752    copy_prop("system_ext_squashfs_disable_4k_align",
753              "squashfs_disable_4k_align")
754    copy_prop("system_ext_base_fs_file", "base_fs_file")
755    copy_prop("system_ext_extfs_inode_count", "extfs_inode_count")
756    if not copy_prop("system_ext_extfs_rsv_pct", "extfs_rsv_pct"):
757      d["extfs_rsv_pct"] = "0"
758    copy_prop("system_ext_reserved_size", "partition_reserved_size")
759    copy_prop("system_ext_selinux_fc", "selinux_fc")
760  elif mount_point == "odm":
761    copy_prop("avb_odm_hashtree_enable", "avb_hashtree_enable")
762    copy_prop("avb_odm_add_hashtree_footer_args",
763              "avb_add_hashtree_footer_args")
764    copy_prop("avb_odm_key_path", "avb_key_path")
765    copy_prop("avb_odm_algorithm", "avb_algorithm")
766    copy_prop("avb_odm_salt", "avb_salt")
767    copy_prop("odm_fs_type", "fs_type")
768    copy_prop("odm_size", "partition_size")
769    if not copy_prop("odm_journal_size", "journal_size"):
770      d["journal_size"] = "0"
771    copy_prop("odm_verity_block_device", "verity_block_device")
772    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
773    copy_prop("odm_squashfs_compressor", "squashfs_compressor")
774    copy_prop("odm_squashfs_compressor_opt", "squashfs_compressor_opt")
775    copy_prop("odm_squashfs_block_size", "squashfs_block_size")
776    copy_prop("odm_squashfs_disable_4k_align", "squashfs_disable_4k_align")
777    copy_prop("odm_base_fs_file", "base_fs_file")
778    copy_prop("odm_extfs_inode_count", "extfs_inode_count")
779    if not copy_prop("odm_extfs_rsv_pct", "extfs_rsv_pct"):
780      d["extfs_rsv_pct"] = "0"
781    copy_prop("odm_reserved_size", "partition_reserved_size")
782    copy_prop("odm_selinux_fc", "selinux_fc")
783  elif mount_point == "vendor_dlkm":
784    copy_prop("avb_vendor_dlkm_hashtree_enable", "avb_hashtree_enable")
785    copy_prop("avb_vendor_dlkm_add_hashtree_footer_args",
786              "avb_add_hashtree_footer_args")
787    copy_prop("avb_vendor_dlkm_key_path", "avb_key_path")
788    copy_prop("avb_vendor_dlkm_algorithm", "avb_algorithm")
789    copy_prop("avb_vendor_dlkm_salt", "avb_salt")
790    copy_prop("vendor_dlkm_fs_type", "fs_type")
791    copy_prop("vendor_dlkm_size", "partition_size")
792    copy_prop("vendor_dlkm_f2fs_compress", "f2fs_compress")
793    copy_prop("vendor_dlkm_f2fs_sldc_flags", "f2fs_sldc_flags")
794    if not copy_prop("vendor_dlkm_journal_size", "journal_size"):
795      d["journal_size"] = "0"
796    copy_prop("vendor_dlkm_verity_block_device", "verity_block_device")
797    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
798    copy_prop("vendor_dlkm_squashfs_compressor", "squashfs_compressor")
799    copy_prop("vendor_dlkm_squashfs_compressor_opt", "squashfs_compressor_opt")
800    copy_prop("vendor_dlkm_squashfs_block_size", "squashfs_block_size")
801    copy_prop("vendor_dlkm_squashfs_disable_4k_align", "squashfs_disable_4k_align")
802    copy_prop("vendor_dlkm_base_fs_file", "base_fs_file")
803    copy_prop("vendor_dlkm_extfs_inode_count", "extfs_inode_count")
804    if not copy_prop("vendor_dlkm_extfs_rsv_pct", "extfs_rsv_pct"):
805      d["extfs_rsv_pct"] = "0"
806    copy_prop("vendor_dlkm_reserved_size", "partition_reserved_size")
807    copy_prop("vendor_dlkm_selinux_fc", "selinux_fc")
808  elif mount_point == "odm_dlkm":
809    copy_prop("avb_odm_dlkm_hashtree_enable", "avb_hashtree_enable")
810    copy_prop("avb_odm_dlkm_add_hashtree_footer_args",
811              "avb_add_hashtree_footer_args")
812    copy_prop("avb_odm_dlkm_key_path", "avb_key_path")
813    copy_prop("avb_odm_dlkm_algorithm", "avb_algorithm")
814    copy_prop("avb_odm_dlkm_salt", "avb_salt")
815    copy_prop("odm_dlkm_fs_type", "fs_type")
816    copy_prop("odm_dlkm_size", "partition_size")
817    if not copy_prop("odm_dlkm_journal_size", "journal_size"):
818      d["journal_size"] = "0"
819    copy_prop("odm_dlkm_verity_block_device", "verity_block_device")
820    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
821    copy_prop("odm_dlkm_squashfs_compressor", "squashfs_compressor")
822    copy_prop("odm_dlkm_squashfs_compressor_opt", "squashfs_compressor_opt")
823    copy_prop("odm_dlkm_squashfs_block_size", "squashfs_block_size")
824    copy_prop("odm_dlkm_squashfs_disable_4k_align", "squashfs_disable_4k_align")
825    copy_prop("odm_dlkm_base_fs_file", "base_fs_file")
826    copy_prop("odm_dlkm_extfs_inode_count", "extfs_inode_count")
827    if not copy_prop("odm_dlkm_extfs_rsv_pct", "extfs_rsv_pct"):
828      d["extfs_rsv_pct"] = "0"
829    copy_prop("odm_dlkm_reserved_size", "partition_reserved_size")
830    copy_prop("odm_dlkm_selinux_fc", "selinux_fc")
831  elif mount_point == "oem":
832    copy_prop("fs_type", "fs_type")
833    copy_prop("oem_size", "partition_size")
834    if not copy_prop("oem_journal_size", "journal_size"):
835      d["journal_size"] = "0"
836    copy_prop("oem_extfs_inode_count", "extfs_inode_count")
837    copy_prop("ext4_share_dup_blocks", "ext4_share_dup_blocks")
838    if not copy_prop("oem_extfs_rsv_pct", "extfs_rsv_pct"):
839      d["extfs_rsv_pct"] = "0"
840    copy_prop("oem_selinux_fc", "selinux_fc")
841  d["partition_name"] = mount_point
842  return d
843
844
845def LoadGlobalDict(filename):
846  """Load "name=value" pairs from filename"""
847  d = {}
848  f = open(filename)
849  for line in f:
850    line = line.strip()
851    if not line or line.startswith("#"):
852      continue
853    k, v = line.split("=", 1)
854    d[k] = v
855  f.close()
856  return d
857
858
859def GlobalDictFromImageProp(image_prop, mount_point):
860  d = {}
861  def copy_prop(src_p, dest_p):
862    if src_p in image_prop:
863      d[dest_p] = image_prop[src_p]
864      return True
865    return False
866
867  if mount_point == "system":
868    copy_prop("partition_size", "system_size")
869  elif mount_point == "system_other":
870    copy_prop("partition_size", "system_other_size")
871  elif mount_point == "vendor":
872    copy_prop("partition_size", "vendor_size")
873  elif mount_point == "odm":
874    copy_prop("partition_size", "odm_size")
875  elif mount_point == "vendor_dlkm":
876    copy_prop("partition_size", "vendor_dlkm_size")
877  elif mount_point == "odm_dlkm":
878    copy_prop("partition_size", "odm_dlkm_size")
879  elif mount_point == "product":
880    copy_prop("partition_size", "product_size")
881  elif mount_point == "system_ext":
882    copy_prop("partition_size", "system_ext_size")
883  return d
884
885
886def main(argv):
887  if len(argv) != 4:
888    print(__doc__)
889    sys.exit(1)
890
891  common.InitLogging()
892
893  in_dir = argv[0]
894  glob_dict_file = argv[1]
895  out_file = argv[2]
896  target_out = argv[3]
897
898  glob_dict = LoadGlobalDict(glob_dict_file)
899  if "mount_point" in glob_dict:
900    # The caller knows the mount point and provides a dictionary needed by
901    # BuildImage().
902    image_properties = glob_dict
903  else:
904    image_filename = os.path.basename(out_file)
905    mount_point = ""
906    if image_filename == "system.img":
907      mount_point = "system"
908    elif image_filename == "system_other.img":
909      mount_point = "system_other"
910    elif image_filename == "userdata.img":
911      mount_point = "data"
912    elif image_filename == "cache.img":
913      mount_point = "cache"
914    elif image_filename == "vendor.img":
915      mount_point = "vendor"
916    elif image_filename == "odm.img":
917      mount_point = "odm"
918    elif image_filename == "vendor_dlkm.img":
919      mount_point = "vendor_dlkm"
920    elif image_filename == "odm_dlkm.img":
921      mount_point = "odm_dlkm"
922    elif image_filename == "oem.img":
923      mount_point = "oem"
924    elif image_filename == "product.img":
925      mount_point = "product"
926    elif image_filename == "system_ext.img":
927      mount_point = "system_ext"
928    else:
929      logger.error("Unknown image file name %s", image_filename)
930      sys.exit(1)
931
932    image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
933
934  try:
935    BuildImage(in_dir, image_properties, out_file, target_out)
936  except:
937    logger.error("Failed to build %s from %s", out_file, in_dir)
938    raise
939
940
941if __name__ == '__main__':
942  try:
943    main(sys.argv[1:])
944  finally:
945    common.Cleanup()
946