2014-02-04 22:48:15 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""
|
2019-08-03 00:50:32 +02:00
|
|
|
Given an input target-files, produces an image zipfile suitable for use with
|
|
|
|
'fastboot update'.
|
2014-02-04 22:48:15 +01:00
|
|
|
|
|
|
|
Usage: img_from_target_files [flags] input_target_files output_image_zip
|
|
|
|
|
2019-08-03 00:50:32 +02:00
|
|
|
input_target_files: Path to the input target_files zip.
|
2019-04-24 21:55:51 +02:00
|
|
|
|
|
|
|
Flags:
|
2014-02-04 22:48:15 +01:00
|
|
|
-z (--bootable_zip)
|
|
|
|
Include only the bootable images (eg 'boot' and 'recovery') in
|
|
|
|
the output.
|
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
--additional <filespec>
|
|
|
|
Include an additional entry into the generated zip file. The filespec is
|
|
|
|
in a format that's accepted by zip2zip (e.g.
|
|
|
|
'OTA/android-info.txt:android-info.txt', to copy `OTA/android-info.txt`
|
|
|
|
from input_file into output_file as `android-info.txt`. Refer to the
|
|
|
|
`filespec` arg in zip2zip's help message). The option can be repeated to
|
|
|
|
include multiple entries.
|
|
|
|
|
2014-02-04 22:48:15 +01:00
|
|
|
"""
|
|
|
|
|
2017-01-10 19:47:58 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-10-12 19:30:39 +02:00
|
|
|
import logging
|
2017-11-21 18:25:31 +01:00
|
|
|
import os
|
2014-02-04 22:48:15 +01:00
|
|
|
import sys
|
2017-11-21 18:25:31 +01:00
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import common
|
2019-04-17 23:28:52 +02:00
|
|
|
from build_super_image import BuildSuperImage
|
2014-02-04 22:48:15 +01:00
|
|
|
|
2014-02-18 19:57:07 +01:00
|
|
|
if sys.hexversion < 0x02070000:
|
2019-08-27 05:33:11 +02:00
|
|
|
print('Python 2.7 or newer is required.', file=sys.stderr)
|
2014-02-04 22:48:15 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-10-12 19:30:39 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-02-04 22:48:15 +01:00
|
|
|
|
|
|
|
OPTIONS = common.OPTIONS
|
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
OPTIONS.additional_entries = []
|
2019-08-27 05:33:11 +02:00
|
|
|
OPTIONS.bootable_only = False
|
|
|
|
OPTIONS.put_super = None
|
|
|
|
OPTIONS.dynamic_partition_list = None
|
|
|
|
OPTIONS.super_device_list = None
|
|
|
|
OPTIONS.retrofit_dap = None
|
|
|
|
OPTIONS.build_super = None
|
|
|
|
OPTIONS.sparse_userimages = None
|
|
|
|
|
2014-02-04 22:48:15 +01:00
|
|
|
|
2019-04-17 23:28:52 +02:00
|
|
|
def LoadOptions(input_file):
|
2019-08-03 00:50:32 +02:00
|
|
|
"""Loads information from input_file to OPTIONS.
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
Args:
|
2019-08-15 08:08:18 +02:00
|
|
|
input_file: Path to the input target_files zip file.
|
2019-04-17 23:28:52 +02:00
|
|
|
"""
|
2019-08-15 08:08:18 +02:00
|
|
|
with zipfile.ZipFile(input_file) as input_zip:
|
|
|
|
info = OPTIONS.info_dict = common.LoadInfoDict(input_zip)
|
2019-04-17 23:28:52 +02:00
|
|
|
|
2019-08-27 05:33:11 +02:00
|
|
|
OPTIONS.put_super = info.get('super_image_in_update_package') == 'true'
|
|
|
|
OPTIONS.dynamic_partition_list = info.get('dynamic_partition_list',
|
|
|
|
'').strip().split()
|
|
|
|
OPTIONS.super_device_list = info.get('super_block_devices',
|
|
|
|
'').strip().split()
|
|
|
|
OPTIONS.retrofit_dap = info.get('dynamic_partition_retrofit') == 'true'
|
|
|
|
OPTIONS.build_super = info.get('build_super_partition') == 'true'
|
|
|
|
OPTIONS.sparse_userimages = bool(info.get('extfs_sparse_flag'))
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
def CopyZipEntries(input_file, output_file, entries):
|
|
|
|
"""Copies ZIP entries between input and output files.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
input_file: Path to the input target_files zip.
|
|
|
|
output_file: Output filename.
|
|
|
|
entries: A list of entries to copy, in a format that's accepted by zip2zip
|
|
|
|
(e.g. 'OTA/android-info.txt:android-info.txt', which copies
|
|
|
|
`OTA/android-info.txt` from input_file into output_file as
|
|
|
|
`android-info.txt`. Refer to the `filespec` arg in zip2zip's help
|
|
|
|
message).
|
|
|
|
"""
|
|
|
|
logger.info('Writing %d entries to archive...', len(entries))
|
|
|
|
cmd = ['zip2zip', '-i', input_file, '-o', output_file]
|
|
|
|
cmd.extend(entries)
|
|
|
|
common.RunAndCheckOutput(cmd)
|
2014-02-04 22:48:15 +01:00
|
|
|
|
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
def EntriesForUserImages(input_file):
|
|
|
|
"""Returns the user images entries to be copied.
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
Args:
|
2019-08-15 08:08:18 +02:00
|
|
|
input_file: Path to the input target_files zip file.
|
2019-04-17 23:28:52 +02:00
|
|
|
"""
|
2019-08-27 05:33:11 +02:00
|
|
|
dynamic_images = [p + '.img' for p in OPTIONS.dynamic_partition_list]
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
# Filter out system_other for launch DAP devices because it is in super image.
|
2019-08-27 05:33:11 +02:00
|
|
|
if not OPTIONS.retrofit_dap and 'system' in OPTIONS.dynamic_partition_list:
|
|
|
|
dynamic_images.append('system_other.img')
|
2019-04-17 23:28:52 +02:00
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
entries = [
|
|
|
|
'OTA/android-info.txt:android-info.txt',
|
|
|
|
]
|
|
|
|
with zipfile.ZipFile(input_file) as input_zip:
|
|
|
|
namelist = input_zip.namelist()
|
|
|
|
|
|
|
|
for image_path in [name for name in namelist if name.startswith('IMAGES/')]:
|
|
|
|
image = os.path.basename(image_path)
|
2019-08-27 05:33:11 +02:00
|
|
|
if OPTIONS.bootable_only and image not in ('boot.img', 'recovery.img'):
|
2019-04-17 23:28:52 +02:00
|
|
|
continue
|
2019-08-27 05:33:11 +02:00
|
|
|
if not image.endswith('.img'):
|
2019-04-17 23:28:52 +02:00
|
|
|
continue
|
2019-08-15 08:08:18 +02:00
|
|
|
# Filter out super_empty and the images that are already in super partition.
|
2019-04-17 23:28:52 +02:00
|
|
|
if OPTIONS.put_super:
|
2019-08-27 05:33:11 +02:00
|
|
|
if image == 'super_empty.img':
|
2019-04-17 23:28:52 +02:00
|
|
|
continue
|
|
|
|
if image in dynamic_images:
|
|
|
|
continue
|
2019-08-15 08:08:18 +02:00
|
|
|
entries.append('{}:{}'.format(image_path, image))
|
|
|
|
return entries
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
def EntriesForSplitSuperImages(input_file):
|
|
|
|
"""Returns the entries for split super images.
|
2019-08-03 00:50:32 +02:00
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
This is only done for retrofit dynamic partition devices.
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
Args:
|
2019-08-15 08:08:18 +02:00
|
|
|
input_file: Path to the input target_files zip file.
|
2019-04-17 23:28:52 +02:00
|
|
|
"""
|
2019-08-15 08:08:18 +02:00
|
|
|
with zipfile.ZipFile(input_file) as input_zip:
|
|
|
|
namelist = input_zip.namelist()
|
|
|
|
entries = []
|
|
|
|
for device in OPTIONS.super_device_list:
|
|
|
|
image = 'OTA/super_{}.img'.format(device)
|
|
|
|
assert image in namelist, 'Failed to find {}'.format(image)
|
|
|
|
entries.append('{}:{}'.format(image, os.path.basename(image)))
|
|
|
|
return entries
|
|
|
|
|
|
|
|
|
|
|
|
def RebuildAndWriteSuperImages(input_file, output_file):
|
|
|
|
"""Builds and writes super images to the output file."""
|
|
|
|
logger.info('Building super image...')
|
|
|
|
|
|
|
|
# We need files under IMAGES/, OTA/, META/ for img_from_target_files.py.
|
|
|
|
# However, common.LoadInfoDict() may read additional files under BOOT/,
|
|
|
|
# RECOVERY/ and ROOT/. So unzip everything from the target_files.zip.
|
|
|
|
input_tmp = common.UnzipTemp(input_file)
|
|
|
|
|
|
|
|
super_file = common.MakeTempFile('super_', '.img')
|
|
|
|
BuildSuperImage(input_tmp, super_file)
|
|
|
|
|
|
|
|
logger.info('Writing super.img to archive...')
|
|
|
|
with zipfile.ZipFile(
|
|
|
|
output_file, 'a', compression=zipfile.ZIP_DEFLATED,
|
|
|
|
allowZip64=not OPTIONS.sparse_userimages) as output_zip:
|
2019-08-27 05:33:11 +02:00
|
|
|
common.ZipWrite(output_zip, super_file, 'super.img')
|
2019-04-17 23:28:52 +02:00
|
|
|
|
|
|
|
|
2019-08-03 00:50:32 +02:00
|
|
|
def ImgFromTargetFiles(input_file, output_file):
|
|
|
|
"""Creates an image archive from the input target_files zip.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
input_file: Path to the input target_files zip.
|
|
|
|
output_file: Output filename.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: On invalid input.
|
|
|
|
"""
|
|
|
|
if not zipfile.is_zipfile(input_file):
|
2019-08-27 05:33:11 +02:00
|
|
|
raise ValueError('%s is not a valid zipfile' % input_file)
|
2019-08-03 00:50:32 +02:00
|
|
|
|
2019-08-27 05:33:11 +02:00
|
|
|
logger.info('Building image zip from target files zip.')
|
2019-08-03 00:50:32 +02:00
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
LoadOptions(input_file)
|
2019-08-03 00:50:32 +02:00
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
# Entries to be copied into the output file.
|
|
|
|
entries = EntriesForUserImages(input_file)
|
2019-08-03 00:50:32 +02:00
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
# Only for devices that retrofit dynamic partitions there're split super
|
|
|
|
# images available in the target_files.zip.
|
|
|
|
rebuild_super = False
|
|
|
|
if OPTIONS.build_super and OPTIONS.put_super:
|
|
|
|
if OPTIONS.retrofit_dap:
|
|
|
|
entries += EntriesForSplitSuperImages(input_file)
|
|
|
|
else:
|
|
|
|
rebuild_super = True
|
|
|
|
|
|
|
|
# Any additional entries provided by caller.
|
|
|
|
entries += OPTIONS.additional_entries
|
|
|
|
|
|
|
|
CopyZipEntries(input_file, output_file, entries)
|
|
|
|
|
|
|
|
if rebuild_super:
|
|
|
|
RebuildAndWriteSuperImages(input_file, output_file)
|
2019-08-03 00:50:32 +02:00
|
|
|
|
|
|
|
|
2014-02-04 22:48:15 +01:00
|
|
|
def main(argv):
|
|
|
|
|
2019-08-15 08:08:18 +02:00
|
|
|
def option_handler(o, a):
|
2019-08-27 05:33:11 +02:00
|
|
|
if o in ('-z', '--bootable_zip'):
|
|
|
|
OPTIONS.bootable_only = True
|
2019-08-15 08:08:18 +02:00
|
|
|
elif o == '--additional':
|
|
|
|
OPTIONS.additional_entries.append(a)
|
2014-02-04 22:48:15 +01:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
args = common.ParseOptions(argv, __doc__,
|
2019-08-27 05:33:11 +02:00
|
|
|
extra_opts='z',
|
2019-08-15 08:08:18 +02:00
|
|
|
extra_long_opts=[
|
|
|
|
'additional=',
|
|
|
|
'bootable_zip',
|
|
|
|
],
|
2014-02-04 22:48:15 +01:00
|
|
|
extra_option_handler=option_handler)
|
|
|
|
if len(args) != 2:
|
|
|
|
common.Usage(__doc__)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2018-10-12 19:30:39 +02:00
|
|
|
common.InitLogging()
|
|
|
|
|
2019-08-03 00:50:32 +02:00
|
|
|
ImgFromTargetFiles(args[0], args[1])
|
2014-02-04 22:48:15 +01:00
|
|
|
|
2019-08-27 05:33:11 +02:00
|
|
|
logger.info('done.')
|
2014-02-04 22:48:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
common.CloseInheritedPipes()
|
|
|
|
main(sys.argv[1:])
|
2015-03-24 03:13:21 +01:00
|
|
|
except common.ExternalError as e:
|
2019-08-27 05:33:11 +02:00
|
|
|
logger.exception('\n ERROR:\n')
|
2014-02-04 22:48:15 +01:00
|
|
|
sys.exit(1)
|
2019-04-18 21:32:18 +02:00
|
|
|
finally:
|
|
|
|
common.Cleanup()
|