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