2018-04-25 03:17:57 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Given a OTA package file, produces update config JSON file.
|
|
|
|
|
2018-05-04 21:17:01 +02:00
|
|
|
Example:
|
|
|
|
$ PYTHONPATH=$ANDROID_BUILD_TOP/build/make/tools/releasetools:$PYTHONPATH \\
|
|
|
|
bootable/recovery/updater_sample/tools/gen_update_config.py \\
|
|
|
|
--ab_install_type=STREAMING \\
|
|
|
|
ota-build-001.zip \\
|
|
|
|
my-config-001.json \\
|
|
|
|
http://foo.bar/ota-builds/ota-build-001.zip
|
2018-04-25 03:17:57 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import zipfile
|
|
|
|
|
2023-11-20 11:07:19 +01:00
|
|
|
import ota_utils # pylint: disable=import-error
|
2018-05-04 21:17:01 +02:00
|
|
|
|
2018-04-25 03:17:57 +02:00
|
|
|
|
2018-04-27 00:49:08 +02:00
|
|
|
class GenUpdateConfig(object):
|
2018-04-25 03:17:57 +02:00
|
|
|
"""
|
|
|
|
A class that generates update configuration file from an OTA package.
|
|
|
|
|
|
|
|
Currently supports only A/B (seamless) OTA packages.
|
|
|
|
TODO: add non-A/B packages support.
|
|
|
|
"""
|
|
|
|
|
|
|
|
AB_INSTALL_TYPE_STREAMING = 'STREAMING'
|
|
|
|
AB_INSTALL_TYPE_NON_STREAMING = 'NON_STREAMING'
|
|
|
|
|
2018-08-21 21:19:02 +02:00
|
|
|
def __init__(self,
|
|
|
|
package,
|
|
|
|
url,
|
|
|
|
ab_install_type,
|
|
|
|
ab_force_switch_slot,
|
|
|
|
ab_verify_payload_metadata):
|
2018-04-25 03:17:57 +02:00
|
|
|
self.package = package
|
|
|
|
self.url = url
|
|
|
|
self.ab_install_type = ab_install_type
|
2018-05-10 01:25:40 +02:00
|
|
|
self.ab_force_switch_slot = ab_force_switch_slot
|
2018-08-21 21:19:02 +02:00
|
|
|
self.ab_verify_payload_metadata = ab_verify_payload_metadata
|
2018-04-25 03:17:57 +02:00
|
|
|
self.streaming_required = (
|
|
|
|
# payload.bin and payload_properties.txt must exist.
|
|
|
|
'payload.bin',
|
|
|
|
'payload_properties.txt',
|
|
|
|
)
|
|
|
|
self.streaming_optional = (
|
|
|
|
# care_map.txt is available only if dm-verity is enabled.
|
|
|
|
'care_map.txt',
|
|
|
|
# compatibility.zip is available only if target supports Treble.
|
|
|
|
'compatibility.zip',
|
|
|
|
)
|
2018-04-27 00:49:08 +02:00
|
|
|
self._config = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
"""Returns generated config object."""
|
|
|
|
return self._config
|
2018-04-25 03:17:57 +02:00
|
|
|
|
|
|
|
def run(self):
|
2018-04-27 00:49:08 +02:00
|
|
|
"""Generates config."""
|
|
|
|
self._config = {
|
2018-04-25 03:17:57 +02:00
|
|
|
'__': '*** Generated using tools/gen_update_config.py ***',
|
|
|
|
'name': self.ab_install_type[0] + ' ' + os.path.basename(self.package)[:-4],
|
|
|
|
'url': self.url,
|
2018-08-21 21:19:02 +02:00
|
|
|
'ab_config': self._gen_ab_config(),
|
2018-04-25 03:17:57 +02:00
|
|
|
'ab_install_type': self.ab_install_type,
|
|
|
|
}
|
|
|
|
|
2018-08-21 21:19:02 +02:00
|
|
|
def _gen_ab_config(self):
|
|
|
|
"""Builds config required for A/B update."""
|
2018-04-25 03:17:57 +02:00
|
|
|
with zipfile.ZipFile(self.package, 'r') as package_zip:
|
2018-08-21 21:19:02 +02:00
|
|
|
config = {
|
|
|
|
'property_files': self._get_property_files(package_zip),
|
|
|
|
'verify_payload_metadata': self.ab_verify_payload_metadata,
|
|
|
|
'force_switch_slot': self.ab_force_switch_slot,
|
2018-04-25 03:17:57 +02:00
|
|
|
}
|
|
|
|
|
2018-08-21 21:19:02 +02:00
|
|
|
return config
|
2018-04-25 03:17:57 +02:00
|
|
|
|
2018-05-04 21:17:01 +02:00
|
|
|
@staticmethod
|
|
|
|
def _get_property_files(package_zip):
|
2018-04-27 00:49:08 +02:00
|
|
|
"""Constructs the property-files list for A/B streaming metadata."""
|
2018-04-25 03:17:57 +02:00
|
|
|
|
2023-11-20 11:07:19 +01:00
|
|
|
ab_ota = ota_utils.AbOtaPropertyFiles()
|
2018-05-04 21:17:01 +02:00
|
|
|
property_str = ab_ota.GetPropertyFilesString(package_zip, False)
|
2018-04-25 03:17:57 +02:00
|
|
|
property_files = []
|
2018-05-04 21:17:01 +02:00
|
|
|
for file in property_str.split(','):
|
|
|
|
filename, offset, size = file.split(':')
|
|
|
|
inner_file = {
|
|
|
|
'filename': filename,
|
|
|
|
'offset': int(offset),
|
|
|
|
'size': int(size)
|
|
|
|
}
|
|
|
|
property_files.append(inner_file)
|
2018-04-25 03:17:57 +02:00
|
|
|
|
|
|
|
return property_files
|
|
|
|
|
2018-04-27 00:49:08 +02:00
|
|
|
def write(self, out):
|
|
|
|
"""Writes config to the output file."""
|
|
|
|
with open(out, 'w') as out_file:
|
|
|
|
json.dump(self.config, out_file, indent=4, separators=(',', ': '), sort_keys=True)
|
|
|
|
|
2018-04-25 03:17:57 +02:00
|
|
|
|
2018-04-27 00:49:08 +02:00
|
|
|
def main(): # pylint: disable=missing-docstring
|
2018-04-25 03:17:57 +02:00
|
|
|
ab_install_type_choices = [
|
|
|
|
GenUpdateConfig.AB_INSTALL_TYPE_STREAMING,
|
|
|
|
GenUpdateConfig.AB_INSTALL_TYPE_NON_STREAMING]
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
|
|
parser.add_argument('--ab_install_type',
|
|
|
|
type=str,
|
|
|
|
default=GenUpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
|
|
|
|
choices=ab_install_type_choices,
|
|
|
|
help='A/B update installation type')
|
2018-05-10 01:25:40 +02:00
|
|
|
parser.add_argument('--ab_force_switch_slot',
|
|
|
|
default=False,
|
2018-06-07 03:38:51 +02:00
|
|
|
action='store_true',
|
|
|
|
help='if set device will boot to a new slot, otherwise user '
|
|
|
|
'manually switches slot on the screen')
|
2018-08-21 21:19:02 +02:00
|
|
|
parser.add_argument('--ab_verify_payload_metadata',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='if set the app will verify the update payload metadata using '
|
|
|
|
'update_engine before downloading the whole package.')
|
2018-04-25 03:17:57 +02:00
|
|
|
parser.add_argument('package',
|
|
|
|
type=str,
|
|
|
|
help='OTA package zip file')
|
|
|
|
parser.add_argument('out',
|
|
|
|
type=str,
|
|
|
|
help='Update configuration JSON file')
|
|
|
|
parser.add_argument('url',
|
|
|
|
type=str,
|
|
|
|
help='OTA package download url')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if not args.out.endswith('.json'):
|
|
|
|
print('out must be a json file')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
gen = GenUpdateConfig(
|
|
|
|
package=args.package,
|
|
|
|
url=args.url,
|
2018-05-10 01:25:40 +02:00
|
|
|
ab_install_type=args.ab_install_type,
|
2018-08-21 21:19:02 +02:00
|
|
|
ab_force_switch_slot=args.ab_force_switch_slot,
|
|
|
|
ab_verify_payload_metadata=args.ab_verify_payload_metadata)
|
2018-04-25 03:17:57 +02:00
|
|
|
gen.run()
|
2018-04-27 00:49:08 +02:00
|
|
|
gen.write(args.out)
|
|
|
|
print('Config is written to ' + args.out)
|
2018-04-25 03:17:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|