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