build_image.py revision 8b72aefb5a8ed4da28c6f83854e8babf53b9cb53
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"""
18Build image output_image_file from input_directory and properties_file.
19
20Usage:  build_image input_directory properties_file output_image_file
21
22"""
23import os
24import os.path
25import subprocess
26import sys
27import commands
28import shutil
29import tempfile
30
31FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"
32
33def RunCommand(cmd):
34  """ Echo and run the given command
35
36  Args:
37    cmd: the command represented as a list of strings.
38  Returns:
39    The exit code.
40  """
41  print "Running: ", " ".join(cmd)
42  p = subprocess.Popen(cmd)
43  p.communicate()
44  return p.returncode
45
46def GetVerityTreeSize(partition_size):
47  cmd = "build_verity_tree -s %d"
48  cmd %= partition_size
49  status, output = commands.getstatusoutput(cmd)
50  if status:
51    print output
52    return False, 0
53  return True, int(output)
54
55def GetVerityMetadataSize(partition_size):
56  cmd = "system/extras/verity/build_verity_metadata.py -s %d"
57  cmd %= partition_size
58  status, output = commands.getstatusoutput(cmd)
59  if status:
60    print output
61    return False, 0
62  return True, int(output)
63
64def AdjustPartitionSizeForVerity(partition_size):
65  """Modifies the provided partition size to account for the verity metadata.
66
67  This information is used to size the created image appropriately.
68  Args:
69    partition_size: the size of the partition to be verified.
70  Returns:
71    The size of the partition adjusted for verity metadata.
72  """
73  success, verity_tree_size = GetVerityTreeSize(partition_size)
74  if not success:
75    return 0
76  success, verity_metadata_size = GetVerityMetadataSize(partition_size)
77  if not success:
78    return 0
79  return partition_size - verity_tree_size - verity_metadata_size
80
81def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict):
82  cmd = "build_verity_tree -A %s %s %s" % (
83      FIXED_SALT, sparse_image_path, verity_image_path)
84  print cmd
85  status, output = commands.getstatusoutput(cmd)
86  if status:
87    print "Could not build verity tree! Error: %s" % output
88    return False
89  root, salt = output.split()
90  prop_dict["verity_root_hash"] = root
91  prop_dict["verity_salt"] = salt
92  return True
93
94def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
95                        block_device, signer_path, key):
96  cmd_template = (
97      "system/extras/verity/build_verity_metadata.py %s %s %s %s %s %s %s")
98  cmd = cmd_template % (image_size, verity_metadata_path, root_hash, salt,
99                        block_device, signer_path, key)
100  print cmd
101  status, output = commands.getstatusoutput(cmd)
102  if status:
103    print "Could not build verity metadata! Error: %s" % output
104    return False
105  return True
106
107def Append2Simg(sparse_image_path, unsparse_image_path, error_message):
108  """Appends the unsparse image to the given sparse image.
109
110  Args:
111    sparse_image_path: the path to the (sparse) image
112    unsparse_image_path: the path to the (unsparse) image
113  Returns:
114    True on success, False on failure.
115  """
116  cmd = "append2simg %s %s"
117  cmd %= (sparse_image_path, unsparse_image_path)
118  print cmd
119  status, output = commands.getstatusoutput(cmd)
120  if status:
121    print "%s: %s" % (error_message, output)
122    return False
123  return True
124
125def BuildVerifiedImage(data_image_path, verity_image_path,
126                       verity_metadata_path):
127  if not Append2Simg(data_image_path, verity_metadata_path,
128                     "Could not append verity metadata!"):
129    return False
130  if not Append2Simg(data_image_path, verity_image_path,
131                     "Could not append verity tree!"):
132    return False
133  return True
134
135def UnsparseImage(sparse_image_path, replace=True):
136  img_dir = os.path.dirname(sparse_image_path)
137  unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path)
138  unsparse_image_path = os.path.join(img_dir, unsparse_image_path)
139  if os.path.exists(unsparse_image_path):
140    if replace:
141      os.unlink(unsparse_image_path)
142    else:
143      return True, unsparse_image_path
144  inflate_command = ["simg2img", sparse_image_path, unsparse_image_path]
145  exit_code = RunCommand(inflate_command)
146  if exit_code != 0:
147    os.remove(unsparse_image_path)
148    return False, None
149  return True, unsparse_image_path
150
151def MakeVerityEnabledImage(out_file, prop_dict):
152  """Creates an image that is verifiable using dm-verity.
153
154  Args:
155    out_file: the location to write the verifiable image at
156    prop_dict: a dictionary of properties required for image creation and
157               verification
158  Returns:
159    True on success, False otherwise.
160  """
161  # get properties
162  image_size = prop_dict["partition_size"]
163  block_dev = prop_dict["verity_block_device"]
164  signer_key = prop_dict["verity_key"] + ".pk8"
165  signer_path = prop_dict["verity_signer_cmd"]
166
167  # make a tempdir
168  tempdir_name = tempfile.mkdtemp(suffix="_verity_images")
169
170  # get partial image paths
171  verity_image_path = os.path.join(tempdir_name, "verity.img")
172  verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img")
173
174  # build the verity tree and get the root hash and salt
175  if not BuildVerityTree(out_file, verity_image_path, prop_dict):
176    shutil.rmtree(tempdir_name, ignore_errors=True)
177    return False
178
179  # build the metadata blocks
180  root_hash = prop_dict["verity_root_hash"]
181  salt = prop_dict["verity_salt"]
182  if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
183                             block_dev, signer_path, signer_key):
184    shutil.rmtree(tempdir_name, ignore_errors=True)
185    return False
186
187  # build the full verified image
188  if not BuildVerifiedImage(out_file,
189                            verity_image_path,
190                            verity_metadata_path):
191    shutil.rmtree(tempdir_name, ignore_errors=True)
192    return False
193
194  shutil.rmtree(tempdir_name, ignore_errors=True)
195  return True
196
197def BuildImage(in_dir, prop_dict, out_file,
198               fs_config=None,
199               fc_config=None,
200               block_list=None):
201  """Build an image to out_file from in_dir with property prop_dict.
202
203  Args:
204    in_dir: path of input directory.
205    prop_dict: property dictionary.
206    out_file: path of the output image file.
207    fs_config: path to the fs_config file (typically
208      META/filesystem_config.txt).  If None then the configuration in
209      the local client will be used.
210    fc_config: path to the SELinux file_contexts file.  If None then
211      the value from prop_dict['selinux_fc'] will be used.
212
213  Returns:
214    True iff the image is built successfully.
215  """
216  build_command = []
217  fs_type = prop_dict.get("fs_type", "")
218  run_fsck = False
219
220  is_verity_partition = "verity_block_device" in prop_dict
221  verity_supported = prop_dict.get("verity") == "true"
222  # adjust the partition size to make room for the hashes if this is to be
223  # verified
224  if verity_supported and is_verity_partition:
225    partition_size = int(prop_dict.get("partition_size"))
226    adjusted_size = AdjustPartitionSizeForVerity(partition_size)
227    if not adjusted_size:
228      return False
229    prop_dict["partition_size"] = str(adjusted_size)
230    prop_dict["original_partition_size"] = str(partition_size)
231
232  if fs_type.startswith("ext"):
233    build_command = ["mkuserimg.sh"]
234    if "extfs_sparse_flag" in prop_dict:
235      build_command.append(prop_dict["extfs_sparse_flag"])
236      run_fsck = True
237    build_command.extend([in_dir, out_file, fs_type,
238                          prop_dict["mount_point"]])
239    build_command.append(prop_dict["partition_size"])
240    if "journal_size" in prop_dict:
241      build_command.extend(["-j", prop_dict["journal_size"]])
242    if "timestamp" in prop_dict:
243      build_command.extend(["-T", str(prop_dict["timestamp"])])
244    if fs_config is not None:
245      build_command.extend(["-C", fs_config])
246    if block_list is not None:
247      build_command.extend(["-B", block_list])
248    build_command.extend(["-L", prop_dict["mount_point"]])
249    if fc_config is not None:
250      build_command.append(fc_config)
251    elif "selinux_fc" in prop_dict:
252      build_command.append(prop_dict["selinux_fc"])
253  elif fs_type.startswith("squash"):
254    build_command = ["mksquashfsimage.sh"]
255    build_command.extend([in_dir, out_file])
256    build_command.extend(["-m", prop_dict["mount_point"]])
257    if fc_config is not None:
258      build_command.extend(["-c", fc_config])
259    elif "selinux_fc" in prop_dict:
260      build_command.extend(["-c", prop_dict["selinux_fc"]])
261  elif fs_type.startswith("f2fs"):
262    build_command = ["mkf2fsuserimg.sh"]
263    build_command.extend([out_file, prop_dict["partition_size"]])
264  else:
265    build_command = ["mkyaffs2image", "-f"]
266    if prop_dict.get("mkyaffs2_extra_flags", None):
267      build_command.extend(prop_dict["mkyaffs2_extra_flags"].split())
268    build_command.append(in_dir)
269    build_command.append(out_file)
270    if "selinux_fc" in prop_dict:
271      build_command.append(prop_dict["selinux_fc"])
272      build_command.append(prop_dict["mount_point"])
273
274  exit_code = RunCommand(build_command)
275  if exit_code != 0:
276    return False
277
278  # create the verified image if this is to be verified
279  if verity_supported and is_verity_partition:
280    if not MakeVerityEnabledImage(out_file, prop_dict):
281      return False
282
283  if run_fsck and prop_dict.get("skip_fsck") != "true":
284    success, unsparse_image = UnsparseImage(out_file, replace=False)
285    if not success:
286      return False
287
288    # Run e2fsck on the inflated image file
289    e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image]
290    exit_code = RunCommand(e2fsck_command)
291
292    os.remove(unsparse_image)
293
294  return exit_code == 0
295
296
297def ImagePropFromGlobalDict(glob_dict, mount_point):
298  """Build an image property dictionary from the global dictionary.
299
300  Args:
301    glob_dict: the global dictionary from the build system.
302    mount_point: such as "system", "data" etc.
303  """
304  d = {}
305  if "build.prop" in glob_dict:
306    bp = glob_dict["build.prop"]
307    if "ro.build.date.utc" in bp:
308      d["timestamp"] = bp["ro.build.date.utc"]
309
310  def copy_prop(src_p, dest_p):
311    if src_p in glob_dict:
312      d[dest_p] = str(glob_dict[src_p])
313
314  common_props = (
315      "extfs_sparse_flag",
316      "mkyaffs2_extra_flags",
317      "selinux_fc",
318      "skip_fsck",
319      "verity",
320      "verity_key",
321      "verity_signer_cmd"
322      )
323  for p in common_props:
324    copy_prop(p, p)
325
326  d["mount_point"] = mount_point
327  if mount_point == "system":
328    copy_prop("fs_type", "fs_type")
329    # Copy the generic sysetem fs type first, override with specific one if
330    # available.
331    copy_prop("system_fs_type", "fs_type")
332    copy_prop("system_size", "partition_size")
333    copy_prop("system_journal_size", "journal_size")
334    copy_prop("system_verity_block_device", "verity_block_device")
335  elif mount_point == "data":
336    # Copy the generic fs type first, override with specific one if available.
337    copy_prop("fs_type", "fs_type")
338    copy_prop("userdata_fs_type", "fs_type")
339    copy_prop("userdata_size", "partition_size")
340  elif mount_point == "cache":
341    copy_prop("cache_fs_type", "fs_type")
342    copy_prop("cache_size", "partition_size")
343  elif mount_point == "vendor":
344    copy_prop("vendor_fs_type", "fs_type")
345    copy_prop("vendor_size", "partition_size")
346    copy_prop("vendor_journal_size", "journal_size")
347    copy_prop("vendor_verity_block_device", "verity_block_device")
348  elif mount_point == "oem":
349    copy_prop("fs_type", "fs_type")
350    copy_prop("oem_size", "partition_size")
351    copy_prop("oem_journal_size", "journal_size")
352
353  return d
354
355
356def LoadGlobalDict(filename):
357  """Load "name=value" pairs from filename"""
358  d = {}
359  f = open(filename)
360  for line in f:
361    line = line.strip()
362    if not line or line.startswith("#"):
363      continue
364    k, v = line.split("=", 1)
365    d[k] = v
366  f.close()
367  return d
368
369
370def main(argv):
371  if len(argv) != 3:
372    print __doc__
373    sys.exit(1)
374
375  in_dir = argv[0]
376  glob_dict_file = argv[1]
377  out_file = argv[2]
378
379  glob_dict = LoadGlobalDict(glob_dict_file)
380  image_filename = os.path.basename(out_file)
381  mount_point = ""
382  if image_filename == "system.img":
383    mount_point = "system"
384  elif image_filename == "userdata.img":
385    mount_point = "data"
386  elif image_filename == "cache.img":
387    mount_point = "cache"
388  elif image_filename == "vendor.img":
389    mount_point = "vendor"
390  elif image_filename == "oem.img":
391    mount_point = "oem"
392  else:
393    print >> sys.stderr, "error: unknown image file name ", image_filename
394    exit(1)
395
396  image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
397  if not BuildImage(in_dir, image_properties, out_file):
398    print >> sys.stderr, "error: failed to build %s from %s" % (out_file,
399                                                                in_dir)
400    exit(1)
401
402
403if __name__ == '__main__':
404  main(sys.argv[1:])
405