add_img_to_target_files.py revision a3e8e9c6f33ecd57221b7fe0ab1bf9c5035351ed
1#!/usr/bin/env python
2#
3# Copyright (C) 2014 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"""
18Given a target-files zipfile that does not contain images (ie, does
19not have an IMAGES/ top-level subdirectory), produce the images and
20add them to the zipfile.
21
22Usage:  add_img_to_target_files target_files
23"""
24
25import sys
26
27if sys.hexversion < 0x02070000:
28  print >> sys.stderr, "Python 2.7 or newer is required."
29  sys.exit(1)
30
31import datetime
32import errno
33import os
34import shlex
35import shutil
36import subprocess
37import tempfile
38import zipfile
39
40import build_image
41import common
42
43OPTIONS = common.OPTIONS
44
45OPTIONS.add_missing = False
46OPTIONS.rebuild_recovery = False
47OPTIONS.replace_verity_public_key = False
48OPTIONS.replace_verity_private_key = False
49OPTIONS.verity_signer_path = None
50
51def AddSystem(output_zip, prefix="IMAGES/", recovery_img=None, boot_img=None):
52  """Turn the contents of SYSTEM into a system image and store it in
53  output_zip. Returns the name of the system image file."""
54
55  prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system.img")
56  if os.path.exists(prebuilt_path):
57    print "system.img already exists in %s, no need to rebuild..." % (prefix,)
58    return prebuilt_path
59
60  def output_sink(fn, data):
61    ofile = open(os.path.join(OPTIONS.input_tmp, "SYSTEM", fn), "w")
62    ofile.write(data)
63    ofile.close()
64
65  if OPTIONS.rebuild_recovery:
66    print "Building new recovery patch"
67    common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img,
68                             boot_img, info_dict=OPTIONS.info_dict)
69
70  block_list = common.MakeTempFile(prefix="system-blocklist-", suffix=".map")
71  imgname = BuildSystem(OPTIONS.input_tmp, OPTIONS.info_dict,
72                        block_list=block_list)
73
74  # If requested, calculate and add dm-verity integrity hashes and
75  # metadata to system.img.
76  if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
77    bvbtool = os.getenv('BVBTOOL') or "bvbtool"
78    cmd = [bvbtool, "add_image_hashes", "--image", imgname]
79    args = OPTIONS.info_dict.get("board_bvb_add_image_hashes_args", None)
80    if args and args.strip():
81      cmd.extend(shlex.split(args))
82    p = common.Run(cmd, stdout=subprocess.PIPE)
83    p.communicate()
84    assert p.returncode == 0, "bvbtool add_image_hashes of %s image failed" % (
85      os.path.basename(OPTIONS.input_tmp),)
86
87  common.ZipWrite(output_zip, imgname, prefix + "system.img")
88  common.ZipWrite(output_zip, block_list, prefix + "system.map")
89  return imgname
90
91
92def BuildSystem(input_dir, info_dict, block_list=None):
93  """Build the (sparse) system image and return the name of a temp
94  file containing it."""
95  return CreateImage(input_dir, info_dict, "system", block_list=block_list)
96
97
98def AddSystemOther(output_zip, prefix="IMAGES/"):
99  """Turn the contents of SYSTEM_OTHER into a system_other image
100  and store it in output_zip."""
101
102  prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "system_other.img")
103  if os.path.exists(prebuilt_path):
104    print "system_other.img already exists in %s, no need to rebuild..." % (prefix,)
105    return
106
107  imgname = BuildSystemOther(OPTIONS.input_tmp, OPTIONS.info_dict)
108  common.ZipWrite(output_zip, imgname, prefix + "system_other.img")
109
110def BuildSystemOther(input_dir, info_dict):
111  """Build the (sparse) system_other image and return the name of a temp
112  file containing it."""
113  return CreateImage(input_dir, info_dict, "system_other", block_list=None)
114
115
116def AddVendor(output_zip, prefix="IMAGES/"):
117  """Turn the contents of VENDOR into a vendor image and store in it
118  output_zip."""
119
120  prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "vendor.img")
121  if os.path.exists(prebuilt_path):
122    print "vendor.img already exists in %s, no need to rebuild..." % (prefix,)
123    return
124
125  block_list = common.MakeTempFile(prefix="vendor-blocklist-", suffix=".map")
126  imgname = BuildVendor(OPTIONS.input_tmp, OPTIONS.info_dict,
127                        block_list=block_list)
128  common.ZipWrite(output_zip, imgname, prefix + "vendor.img")
129  common.ZipWrite(output_zip, block_list, prefix + "vendor.map")
130
131
132def BuildVendor(input_dir, info_dict, block_list=None):
133  """Build the (sparse) vendor image and return the name of a temp
134  file containing it."""
135  return CreateImage(input_dir, info_dict, "vendor", block_list=block_list)
136
137
138def CreateImage(input_dir, info_dict, what, block_list=None):
139  print "creating " + what + ".img..."
140
141  img = common.MakeTempFile(prefix=what + "-", suffix=".img")
142
143  # The name of the directory it is making an image out of matters to
144  # mkyaffs2image.  It wants "system" but we have a directory named
145  # "SYSTEM", so create a symlink.
146  try:
147    os.symlink(os.path.join(input_dir, what.upper()),
148               os.path.join(input_dir, what))
149  except OSError as e:
150    # bogus error on my mac version?
151    #   File "./build/tools/releasetools/img_from_target_files"
152    #     os.path.join(OPTIONS.input_tmp, "system"))
153    # OSError: [Errno 17] File exists
154    if e.errno == errno.EEXIST:
155      pass
156
157  image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
158  fstab = info_dict["fstab"]
159  if fstab:
160    image_props["fs_type"] = fstab["/" + what].fs_type
161
162  # Use a fixed timestamp (01/01/2009) when packaging the image.
163  # Bug: 24377993
164  epoch = datetime.datetime.fromtimestamp(0)
165  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
166  image_props["timestamp"] = int(timestamp)
167
168  if what == "system":
169    fs_config_prefix = ""
170  else:
171    fs_config_prefix = what + "_"
172
173  fs_config = os.path.join(
174      input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
175  if not os.path.exists(fs_config):
176    fs_config = None
177
178  # Override values loaded from info_dict.
179  if fs_config:
180    image_props["fs_config"] = fs_config
181  if block_list:
182    image_props["block_list"] = block_list
183
184  succ = build_image.BuildImage(os.path.join(input_dir, what),
185                                image_props, img)
186  assert succ, "build " + what + ".img image failed"
187
188  return img
189
190
191def AddUserdata(output_zip, prefix="IMAGES/"):
192  """Create a userdata image and store it in output_zip.
193
194  In most case we just create and store an empty userdata.img;
195  But the invoker can also request to create userdata.img with real
196  data from the target files, by setting "userdata_img_with_data=true"
197  in OPTIONS.info_dict.
198  """
199
200  prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
201  if os.path.exists(prebuilt_path):
202    print "userdata.img already exists in %s, no need to rebuild..." % (prefix,)
203    return
204
205  # Skip userdata.img if no size.
206  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
207  if not image_props.get("partition_size"):
208    return
209
210  print "creating userdata.img..."
211
212  # Use a fixed timestamp (01/01/2009) when packaging the image.
213  # Bug: 24377993
214  epoch = datetime.datetime.fromtimestamp(0)
215  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
216  image_props["timestamp"] = int(timestamp)
217
218  # The name of the directory it is making an image out of matters to
219  # mkyaffs2image.  So we create a temp dir, and within it we create an
220  # empty dir named "data", or a symlink to the DATA dir,
221  # and build the image from that.
222  temp_dir = tempfile.mkdtemp()
223  user_dir = os.path.join(temp_dir, "data")
224  empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
225  if empty:
226    # Create an empty dir.
227    os.mkdir(user_dir)
228  else:
229    # Symlink to the DATA dir.
230    os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
231               user_dir)
232
233  img = tempfile.NamedTemporaryFile()
234
235  fstab = OPTIONS.info_dict["fstab"]
236  if fstab:
237    image_props["fs_type"] = fstab["/data"].fs_type
238  succ = build_image.BuildImage(user_dir, image_props, img.name)
239  assert succ, "build userdata.img image failed"
240
241  common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
242  common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
243  img.close()
244  shutil.rmtree(temp_dir)
245
246
247def AddPartitionTable(output_zip, prefix="IMAGES/"):
248  """Create a partition table image and store it in output_zip."""
249
250  _, img_file_name = tempfile.mkstemp()
251  _, bpt_file_name = tempfile.mkstemp()
252
253  # use BPTTOOL from environ, or "bpttool" if empty or not set.
254  bpttool = os.getenv("BPTTOOL") or "bpttool"
255  cmd = [bpttool, "make_table", "--output_json", bpt_file_name,
256         "--output_gpt", img_file_name]
257  input_files_str = OPTIONS.info_dict["board_bpt_input_files"]
258  input_files = input_files_str.split(" ")
259  for i in input_files:
260    cmd.extend(["--input", i])
261  disk_size = OPTIONS.info_dict.get("board_bpt_disk_size")
262  if disk_size:
263    cmd.extend(["--disk_size", disk_size])
264  args = OPTIONS.info_dict.get("board_bpt_make_table_args")
265  if args:
266    cmd.extend(shlex.split(args))
267
268  p = common.Run(cmd, stdout=subprocess.PIPE)
269  p.communicate()
270  assert p.returncode == 0, "bpttool make_table failed"
271
272  common.ZipWrite(output_zip, img_file_name, prefix + "partition-table.img")
273  common.ZipWrite(output_zip, bpt_file_name, prefix + "partition-table.bpt")
274
275
276def AddCache(output_zip, prefix="IMAGES/"):
277  """Create an empty cache image and store it in output_zip."""
278
279  prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
280  if os.path.exists(prebuilt_path):
281    print "cache.img already exists in %s, no need to rebuild..." % (prefix,)
282    return
283
284  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "cache")
285  # The build system has to explicitly request for cache.img.
286  if "fs_type" not in image_props:
287    return
288
289  print "creating cache.img..."
290
291  # Use a fixed timestamp (01/01/2009) when packaging the image.
292  # Bug: 24377993
293  epoch = datetime.datetime.fromtimestamp(0)
294  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
295  image_props["timestamp"] = int(timestamp)
296
297  # The name of the directory it is making an image out of matters to
298  # mkyaffs2image.  So we create a temp dir, and within it we create an
299  # empty dir named "cache", and build the image from that.
300  temp_dir = tempfile.mkdtemp()
301  user_dir = os.path.join(temp_dir, "cache")
302  os.mkdir(user_dir)
303  img = tempfile.NamedTemporaryFile()
304
305  fstab = OPTIONS.info_dict["fstab"]
306  if fstab:
307    image_props["fs_type"] = fstab["/cache"].fs_type
308  succ = build_image.BuildImage(user_dir, image_props, img.name)
309  assert succ, "build cache.img image failed"
310
311  common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
312  common.ZipWrite(output_zip, img.name, prefix + "cache.img")
313  img.close()
314  os.rmdir(user_dir)
315  os.rmdir(temp_dir)
316
317
318def AddImagesToTargetFiles(filename):
319  OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
320
321  if not OPTIONS.add_missing:
322    for n in input_zip.namelist():
323      if n.startswith("IMAGES/"):
324        print "target_files appears to already contain images."
325        sys.exit(1)
326
327  try:
328    input_zip.getinfo("VENDOR/")
329    has_vendor = True
330  except KeyError:
331    has_vendor = False
332
333  has_system_other = "SYSTEM_OTHER/" in input_zip.namelist()
334
335  OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
336
337  common.ZipClose(input_zip)
338  output_zip = zipfile.ZipFile(filename, "a",
339                               compression=zipfile.ZIP_DEFLATED)
340
341  has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
342  system_root_image = (OPTIONS.info_dict.get("system_root_image", None) == "true")
343  board_bvb_enable = (OPTIONS.info_dict.get("board_bvb_enable", None) == "true")
344
345  # Brillo Verified Boot is incompatible with certain
346  # configurations. Explicitly check for these.
347  if board_bvb_enable:
348    assert not has_recovery, "has_recovery incompatible with bvb"
349    assert not system_root_image, "system_root_image incompatible with bvb"
350    assert not OPTIONS.rebuild_recovery, "rebuild_recovery incompatible with bvb"
351    assert not has_vendor, "VENDOR images currently incompatible with bvb"
352
353  def banner(s):
354    print "\n\n++++ " + s + " ++++\n\n"
355
356  prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
357  boot_image = None
358  if os.path.exists(prebuilt_path):
359    banner("boot")
360    print "boot.img already exists in IMAGES/, no need to rebuild..."
361    if OPTIONS.rebuild_recovery:
362      boot_image = common.GetBootableImage(
363          "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
364  else:
365    if board_bvb_enable:
366      # With Brillo Verified Boot, we need to build system.img before
367      # boot.img since the latter includes the dm-verity root hash and
368      # salt for the former.
369      pass
370    else:
371      banner("boot")
372      boot_image = common.GetBootableImage(
373        "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
374      if boot_image:
375        boot_image.AddToZip(output_zip)
376
377  recovery_image = None
378  if has_recovery:
379    banner("recovery")
380    prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
381    if os.path.exists(prebuilt_path):
382      print "recovery.img already exists in IMAGES/, no need to rebuild..."
383      if OPTIONS.rebuild_recovery:
384        recovery_image = common.GetBootableImage(
385            "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
386            "RECOVERY")
387    else:
388      recovery_image = common.GetBootableImage(
389          "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
390      if recovery_image:
391        recovery_image.AddToZip(output_zip)
392
393  banner("system")
394  system_img_path = AddSystem(
395    output_zip, recovery_img=recovery_image, boot_img=boot_image)
396  if OPTIONS.info_dict.get("board_bvb_enable", None) == "true":
397    # If we're using Brillo Verified Boot, we can now build boot.img
398    # given that we have system.img.
399    banner("boot")
400    boot_image = common.GetBootableImage(
401      "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT",
402      system_img_path=system_img_path)
403    if boot_image:
404      boot_image.AddToZip(output_zip)
405  if has_vendor:
406    banner("vendor")
407    AddVendor(output_zip)
408  if has_system_other:
409    banner("system_other")
410    AddSystemOther(output_zip)
411  banner("userdata")
412  AddUserdata(output_zip)
413  banner("cache")
414  AddCache(output_zip)
415  if OPTIONS.info_dict.get("board_bpt_enable", None) == "true":
416    banner("partition-table")
417    AddPartitionTable(output_zip)
418
419  # For devices using A/B update, copy over images from RADIO/ and/or
420  # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
421  # images ready under IMAGES/. All images should have '.img' as extension.
422  banner("radio")
423  ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
424  if os.path.exists(ab_partitions):
425    with open(ab_partitions, 'r') as f:
426      lines = f.readlines()
427    for line in lines:
428      img_name = line.strip() + ".img"
429      prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)
430      if os.path.exists(prebuilt_path):
431        print "%s already exists, no need to overwrite..." % (img_name,)
432        continue
433
434      img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
435      img_vendor_dir = os.path.join(
436        OPTIONS.input_tmp, "VENDOR_IMAGES")
437      if os.path.exists(img_radio_path):
438        common.ZipWrite(output_zip, img_radio_path,
439                        os.path.join("IMAGES", img_name))
440      else:
441        for root, _, files in os.walk(img_vendor_dir):
442          if img_name in files:
443            common.ZipWrite(output_zip, os.path.join(root, img_name),
444              os.path.join("IMAGES", img_name))
445            break
446
447      # Zip spec says: All slashes MUST be forward slashes.
448      img_path = 'IMAGES/' + img_name
449      assert img_path in output_zip.namelist(), "cannot find " + img_name
450
451  common.ZipClose(output_zip)
452
453def main(argv):
454  def option_handler(o, a):
455    if o in ("-a", "--add_missing"):
456      OPTIONS.add_missing = True
457    elif o in ("-r", "--rebuild_recovery",):
458      OPTIONS.rebuild_recovery = True
459    elif o == "--replace_verity_private_key":
460      OPTIONS.replace_verity_private_key = (True, a)
461    elif o == "--replace_verity_public_key":
462      OPTIONS.replace_verity_public_key = (True, a)
463    elif o == "--verity_signer_path":
464      OPTIONS.verity_signer_path = a
465    else:
466      return False
467    return True
468
469  args = common.ParseOptions(
470      argv, __doc__, extra_opts="ar",
471      extra_long_opts=["add_missing", "rebuild_recovery",
472                       "replace_verity_public_key=",
473                       "replace_verity_private_key=",
474                       "verity_signer_path="],
475      extra_option_handler=option_handler)
476
477
478  if len(args) != 1:
479    common.Usage(__doc__)
480    sys.exit(1)
481
482  AddImagesToTargetFiles(args[0])
483  print "done."
484
485if __name__ == '__main__':
486  try:
487    common.CloseInheritedPipes()
488    main(sys.argv[1:])
489  except common.ExternalError as e:
490    print
491    print "   ERROR: %s" % (e,)
492    print
493    sys.exit(1)
494  finally:
495    common.Cleanup()
496