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