blob: b43e49df85acd1752b7245fafd95c2b0bccdde66 [file] [log] [blame]
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -07001#!/usr/bin/env python3
2#
3# Copyright (C) 2018 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 OTA package file, produces update config JSON file.
19
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070020Example:
21 $ PYTHONPATH=$ANDROID_BUILD_TOP/build/make/tools/releasetools:$PYTHONPATH \\
22 bootable/recovery/updater_sample/tools/gen_update_config.py \\
23 --ab_install_type=STREAMING \\
24 ota-build-001.zip \\
25 my-config-001.json \\
26 http://foo.bar/ota-builds/ota-build-001.zip
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070027"""
28
29import argparse
30import json
31import os.path
32import sys
33import zipfile
34
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070035import ota_from_target_files # pylint: disable=import-error
36
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070037
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -070038class GenUpdateConfig(object):
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070039 """
40 A class that generates update configuration file from an OTA package.
41
42 Currently supports only A/B (seamless) OTA packages.
43 TODO: add non-A/B packages support.
44 """
45
46 AB_INSTALL_TYPE_STREAMING = 'STREAMING'
47 AB_INSTALL_TYPE_NON_STREAMING = 'NON_STREAMING'
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070048
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070049 def __init__(self, package, url, ab_install_type, ab_force_switch_slot):
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070050 self.package = package
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070051 self.url = url
52 self.ab_install_type = ab_install_type
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070053 self.ab_force_switch_slot = ab_force_switch_slot
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070054 self.streaming_required = (
55 # payload.bin and payload_properties.txt must exist.
56 'payload.bin',
57 'payload_properties.txt',
58 )
59 self.streaming_optional = (
60 # care_map.txt is available only if dm-verity is enabled.
61 'care_map.txt',
62 # compatibility.zip is available only if target supports Treble.
63 'compatibility.zip',
64 )
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -070065 self._config = None
66
67 @property
68 def config(self):
69 """Returns generated config object."""
70 return self._config
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070071
72 def run(self):
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -070073 """Generates config."""
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070074 streaming_metadata = None
75 if self.ab_install_type == GenUpdateConfig.AB_INSTALL_TYPE_STREAMING:
76 streaming_metadata = self._gen_ab_streaming_metadata()
77
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -070078 self._config = {
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070079 '__': '*** Generated using tools/gen_update_config.py ***',
80 'name': self.ab_install_type[0] + ' ' + os.path.basename(self.package)[:-4],
81 'url': self.url,
82 'ab_streaming_metadata': streaming_metadata,
83 'ab_install_type': self.ab_install_type,
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -070084 'ab_config': {
85 'force_switch_slot': self.ab_force_switch_slot,
86 }
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070087 }
88
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070089 def _gen_ab_streaming_metadata(self):
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -070090 """Builds metadata for files required for streaming update."""
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070091 with zipfile.ZipFile(self.package, 'r') as package_zip:
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070092 metadata = {
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070093 'property_files': self._get_property_files(package_zip)
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -070094 }
95
96 return metadata
97
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070098 @staticmethod
99 def _get_property_files(package_zip):
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -0700100 """Constructs the property-files list for A/B streaming metadata."""
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700101
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -0700102 ab_ota = ota_from_target_files.AbOtaPropertyFiles()
103 property_str = ab_ota.GetPropertyFilesString(package_zip, False)
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700104 property_files = []
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -0700105 for file in property_str.split(','):
106 filename, offset, size = file.split(':')
107 inner_file = {
108 'filename': filename,
109 'offset': int(offset),
110 'size': int(size)
111 }
112 property_files.append(inner_file)
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700113
114 return property_files
115
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -0700116 def write(self, out):
117 """Writes config to the output file."""
118 with open(out, 'w') as out_file:
119 json.dump(self.config, out_file, indent=4, separators=(',', ': '), sort_keys=True)
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700120
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -0700121
122def main(): # pylint: disable=missing-docstring
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700123 ab_install_type_choices = [
124 GenUpdateConfig.AB_INSTALL_TYPE_STREAMING,
125 GenUpdateConfig.AB_INSTALL_TYPE_NON_STREAMING]
126 parser = argparse.ArgumentParser(description=__doc__,
127 formatter_class=argparse.RawDescriptionHelpFormatter)
128 parser.add_argument('--ab_install_type',
129 type=str,
130 default=GenUpdateConfig.AB_INSTALL_TYPE_NON_STREAMING,
131 choices=ab_install_type_choices,
132 help='A/B update installation type')
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -0700133 parser.add_argument('--ab_force_switch_slot',
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -0700134 default=False,
Zhomart Mukhamejanovda960702018-06-06 18:38:51 -0700135 action='store_true',
136 help='if set device will boot to a new slot, otherwise user '
137 'manually switches slot on the screen')
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700138 parser.add_argument('package',
139 type=str,
140 help='OTA package zip file')
141 parser.add_argument('out',
142 type=str,
143 help='Update configuration JSON file')
144 parser.add_argument('url',
145 type=str,
146 help='OTA package download url')
147 args = parser.parse_args()
148
149 if not args.out.endswith('.json'):
150 print('out must be a json file')
151 sys.exit(1)
152
153 gen = GenUpdateConfig(
154 package=args.package,
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700155 url=args.url,
Zhomart Mukhamejanov238beb72018-05-09 16:25:40 -0700156 ab_install_type=args.ab_install_type,
157 ab_force_switch_slot=args.ab_force_switch_slot)
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700158 gen.run()
Zhomart Mukhamejanov72a4d462018-04-26 15:49:08 -0700159 gen.write(args.out)
160 print('Config is written to ' + args.out)
Zhomart Mukhamejanovd5a41822018-04-24 18:17:57 -0700161
162
163if __name__ == '__main__':
164 main()