blob: 18968aadfd2e480901c67f6c606ee685ed7d5d09 [file] [log] [blame]
Yifan Hong0c715502021-04-19 13:48:21 -07001#!/usr/bin/env python3
Alex Deymo6751bbe2017-03-21 11:20:02 -07002#
3# Copyright (C) 2017 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
18"""Send an A/B update to an Android device over adb."""
19
Kelvin Zhang4b883ea2020-10-08 13:26:44 -040020from __future__ import print_function
Andrew Lassalle165843c2019-11-05 13:30:34 -080021from __future__ import absolute_import
22
Alex Deymo6751bbe2017-03-21 11:20:02 -070023import argparse
Kelvin Zhangaba70ab2020-08-04 10:32:59 -040024import binascii
Sen Jiang3b15b592017-09-26 18:21:04 -070025import hashlib
Alex Deymo6751bbe2017-03-21 11:20:02 -070026import logging
27import os
Kelvin Zhang46a860c2022-09-28 19:42:39 -070028import re
Alex Deymo6751bbe2017-03-21 11:20:02 -070029import socket
30import subprocess
31import sys
Kelvin Zhangaba70ab2020-08-04 10:32:59 -040032import struct
Kelvin Zhang51aad992021-02-19 14:46:28 -050033import tempfile
Nikita Ioffe3a327e62021-06-30 16:05:09 +010034import time
Alex Deymo6751bbe2017-03-21 11:20:02 -070035import threading
Sen Jiang144f9f82017-09-26 15:49:45 -070036import xml.etree.ElementTree
Alex Deymo6751bbe2017-03-21 11:20:02 -070037import zipfile
38
Andrew Lassalle165843c2019-11-05 13:30:34 -080039from six.moves import BaseHTTPServer
40
Sen Jianga1784b72017-08-09 17:42:36 -070041import update_payload.payload
42
Alex Deymo6751bbe2017-03-21 11:20:02 -070043
44# The path used to store the OTA package when applying the package from a file.
45OTA_PACKAGE_PATH = '/data/ota_package'
46
Sen Jianga1784b72017-08-09 17:42:36 -070047# The path to the payload public key on the device.
48PAYLOAD_KEY_PATH = '/etc/update_engine/update-payload-key.pub.pem'
49
50# The port on the device that update_engine should connect to.
51DEVICE_PORT = 1234
Alex Deymo6751bbe2017-03-21 11:20:02 -070052
Andrew Lassalle165843c2019-11-05 13:30:34 -080053
Kelvin Zhang46a860c2022-09-28 19:42:39 -070054def CopyFileObjLength(fsrc, fdst, buffer_size=128 * 1024, copy_length=None, speed_limit=None):
Alex Deymo6751bbe2017-03-21 11:20:02 -070055 """Copy from a file object to another.
56
57 This function is similar to shutil.copyfileobj except that it allows to copy
58 less than the full source file.
59
60 Args:
61 fsrc: source file object where to read from.
62 fdst: destination file object where to write to.
63 buffer_size: size of the copy buffer in memory.
64 copy_length: maximum number of bytes to copy, or None to copy everything.
Kelvin Zhang46a860c2022-09-28 19:42:39 -070065 speed_limit: upper limit for copying speed, in bytes per second.
Alex Deymo6751bbe2017-03-21 11:20:02 -070066
67 Returns:
68 the number of bytes copied.
69 """
Kelvin Zhang46a860c2022-09-28 19:42:39 -070070 # If buffer size significantly bigger than speed limit
71 # traffic would seem extremely spiky to the client.
72 if speed_limit:
73 print(f"Applying speed limit: {speed_limit}")
74 buffer_size = min(speed_limit//32, buffer_size)
75
76 start_time = time.time()
Alex Deymo6751bbe2017-03-21 11:20:02 -070077 copied = 0
78 while True:
79 chunk_size = buffer_size
80 if copy_length is not None:
81 chunk_size = min(chunk_size, copy_length - copied)
82 if not chunk_size:
83 break
84 buf = fsrc.read(chunk_size)
85 if not buf:
86 break
Kelvin Zhang46a860c2022-09-28 19:42:39 -070087 if speed_limit:
88 expected_duration = copied/speed_limit
89 actual_duration = time.time() - start_time
90 if actual_duration < expected_duration:
91 time.sleep(expected_duration-actual_duration)
Alex Deymo6751bbe2017-03-21 11:20:02 -070092 fdst.write(buf)
93 copied += len(buf)
94 return copied
95
96
97class AndroidOTAPackage(object):
98 """Android update payload using the .zip format.
99
100 Android OTA packages traditionally used a .zip file to store the payload. When
101 applying A/B updates over the network, a payload binary is stored RAW inside
102 this .zip file which is used by update_engine to apply the payload. To do
103 this, an offset and size inside the .zip file are provided.
104 """
105
106 # Android OTA package file paths.
107 OTA_PAYLOAD_BIN = 'payload.bin'
108 OTA_PAYLOAD_PROPERTIES_TXT = 'payload_properties.txt'
Tianjie Xu3f9be772019-11-02 18:31:50 -0700109 SECONDARY_OTA_PAYLOAD_BIN = 'secondary/payload.bin'
110 SECONDARY_OTA_PAYLOAD_PROPERTIES_TXT = 'secondary/payload_properties.txt'
Kelvin Zhangaba70ab2020-08-04 10:32:59 -0400111 PAYLOAD_MAGIC_HEADER = b'CrAU'
Alex Deymo6751bbe2017-03-21 11:20:02 -0700112
Tianjie Xu3f9be772019-11-02 18:31:50 -0700113 def __init__(self, otafilename, secondary_payload=False):
Alex Deymo6751bbe2017-03-21 11:20:02 -0700114 self.otafilename = otafilename
115
116 otazip = zipfile.ZipFile(otafilename, 'r')
Tianjie Xu3f9be772019-11-02 18:31:50 -0700117 payload_entry = (self.SECONDARY_OTA_PAYLOAD_BIN if secondary_payload else
118 self.OTA_PAYLOAD_BIN)
119 payload_info = otazip.getinfo(payload_entry)
Kelvin Zhangaba70ab2020-08-04 10:32:59 -0400120
121 if payload_info.compress_type != 0:
122 logging.error(
Kelvin Zhang07676f52020-12-01 10:45:09 -0500123 "Expected payload to be uncompressed, got compression method %d",
Kelvin Zhangaba70ab2020-08-04 10:32:59 -0400124 payload_info.compress_type)
125 # Don't use len(payload_info.extra). Because that returns size of extra
126 # fields in central directory. We need to look at local file directory,
127 # as these two might have different sizes.
128 with open(otafilename, "rb") as fp:
129 fp.seek(payload_info.header_offset)
130 data = fp.read(zipfile.sizeFileHeader)
131 fheader = struct.unpack(zipfile.structFileHeader, data)
132 # Last two fields of local file header are filename length and
133 # extra length
134 filename_len = fheader[-2]
135 extra_len = fheader[-1]
136 self.offset = payload_info.header_offset
137 self.offset += zipfile.sizeFileHeader
138 self.offset += filename_len + extra_len
139 self.size = payload_info.file_size
140 fp.seek(self.offset)
141 payload_header = fp.read(4)
142 if payload_header != self.PAYLOAD_MAGIC_HEADER:
143 logging.warning(
Kelvin Zhang07676f52020-12-01 10:45:09 -0500144 "Invalid header, expected %s, got %s."
Kelvin Zhangaba70ab2020-08-04 10:32:59 -0400145 "Either the offset is not correct, or payload is corrupted",
146 binascii.hexlify(self.PAYLOAD_MAGIC_HEADER),
Kelvin Zhang07676f52020-12-01 10:45:09 -0500147 binascii.hexlify(payload_header))
Tianjie Xu3f9be772019-11-02 18:31:50 -0700148
149 property_entry = (self.SECONDARY_OTA_PAYLOAD_PROPERTIES_TXT if
150 secondary_payload else self.OTA_PAYLOAD_PROPERTIES_TXT)
151 self.properties = otazip.read(property_entry)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700152
153
154class UpdateHandler(BaseHTTPServer.BaseHTTPRequestHandler):
155 """A HTTPServer that supports single-range requests.
156
157 Attributes:
158 serving_payload: path to the only payload file we are serving.
Sen Jiang3b15b592017-09-26 18:21:04 -0700159 serving_range: the start offset and size tuple of the payload.
Alex Deymo6751bbe2017-03-21 11:20:02 -0700160 """
161
162 @staticmethod
Sen Jiang10485592017-08-15 18:20:24 -0700163 def _parse_range(range_str, file_size):
Alex Deymo6751bbe2017-03-21 11:20:02 -0700164 """Parse an HTTP range string.
165
166 Args:
167 range_str: HTTP Range header in the request, not including "Header:".
168 file_size: total size of the serving file.
169
170 Returns:
171 A tuple (start_range, end_range) with the range of bytes requested.
172 """
173 start_range = 0
174 end_range = file_size
175
176 if range_str:
177 range_str = range_str.split('=', 1)[1]
178 s, e = range_str.split('-', 1)
179 if s:
180 start_range = int(s)
181 if e:
182 end_range = int(e) + 1
183 elif e:
184 if int(e) < file_size:
185 start_range = file_size - int(e)
186 return start_range, end_range
187
Alex Deymo6751bbe2017-03-21 11:20:02 -0700188 def do_GET(self): # pylint: disable=invalid-name
189 """Reply with the requested payload file."""
190 if self.path != '/payload':
191 self.send_error(404, 'Unknown request')
192 return
193
194 if not self.serving_payload:
195 self.send_error(500, 'No serving payload set')
196 return
197
198 try:
199 f = open(self.serving_payload, 'rb')
200 except IOError:
201 self.send_error(404, 'File not found')
202 return
203 # Handle the range request.
204 if 'Range' in self.headers:
205 self.send_response(206)
206 else:
207 self.send_response(200)
208
Sen Jiang3b15b592017-09-26 18:21:04 -0700209 serving_start, serving_size = self.serving_range
Sen Jiang10485592017-08-15 18:20:24 -0700210 start_range, end_range = self._parse_range(self.headers.get('range'),
Sen Jiang3b15b592017-09-26 18:21:04 -0700211 serving_size)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700212 logging.info('Serving request for %s from %s [%d, %d) length: %d',
Sen Jiang3b15b592017-09-26 18:21:04 -0700213 self.path, self.serving_payload, serving_start + start_range,
214 serving_start + end_range, end_range - start_range)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700215
216 self.send_header('Accept-Ranges', 'bytes')
217 self.send_header('Content-Range',
218 'bytes ' + str(start_range) + '-' + str(end_range - 1) +
219 '/' + str(end_range - start_range))
220 self.send_header('Content-Length', end_range - start_range)
221
Sen Jiang3b15b592017-09-26 18:21:04 -0700222 stat = os.fstat(f.fileno())
Alex Deymo6751bbe2017-03-21 11:20:02 -0700223 self.send_header('Last-Modified', self.date_time_string(stat.st_mtime))
224 self.send_header('Content-type', 'application/octet-stream')
225 self.end_headers()
226
Sen Jiang3b15b592017-09-26 18:21:04 -0700227 f.seek(serving_start + start_range)
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700228 CopyFileObjLength(f, self.wfile, copy_length=end_range -
229 start_range, speed_limit=self.speed_limit)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700230
Sen Jianga1784b72017-08-09 17:42:36 -0700231
Alex Deymo6751bbe2017-03-21 11:20:02 -0700232class ServerThread(threading.Thread):
233 """A thread for serving HTTP requests."""
234
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700235 def __init__(self, ota_filename, serving_range, speed_limit):
Alex Deymo6751bbe2017-03-21 11:20:02 -0700236 threading.Thread.__init__(self)
Sen Jiang3b15b592017-09-26 18:21:04 -0700237 # serving_payload and serving_range are class attributes and the
238 # UpdateHandler class is instantiated with every request.
Alex Deymo6751bbe2017-03-21 11:20:02 -0700239 UpdateHandler.serving_payload = ota_filename
Sen Jiang3b15b592017-09-26 18:21:04 -0700240 UpdateHandler.serving_range = serving_range
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700241 UpdateHandler.speed_limit = speed_limit
Alex Deymo6751bbe2017-03-21 11:20:02 -0700242 self._httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), UpdateHandler)
243 self.port = self._httpd.server_port
244
245 def run(self):
246 try:
247 self._httpd.serve_forever()
248 except (KeyboardInterrupt, socket.error):
249 pass
250 logging.info('Server Terminated')
251
252 def StopServer(self):
Kelvin Zhang4b883ea2020-10-08 13:26:44 -0400253 self._httpd.shutdown()
Alex Deymo6751bbe2017-03-21 11:20:02 -0700254 self._httpd.socket.close()
255
256
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700257def StartServer(ota_filename, serving_range, speed_limit):
258 t = ServerThread(ota_filename, serving_range, speed_limit)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700259 t.start()
260 return t
261
262
Tianjie Xu3f9be772019-11-02 18:31:50 -0700263def AndroidUpdateCommand(ota_filename, secondary, payload_url, extra_headers):
Alex Deymo6751bbe2017-03-21 11:20:02 -0700264 """Return the command to run to start the update in the Android device."""
Tianjie Xu3f9be772019-11-02 18:31:50 -0700265 ota = AndroidOTAPackage(ota_filename, secondary)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700266 headers = ota.properties
Kelvin Zhang4b883ea2020-10-08 13:26:44 -0400267 headers += b'USER_AGENT=Dalvik (something, something)\n'
268 headers += b'NETWORK_ID=0\n'
269 headers += extra_headers.encode()
Alex Deymo6751bbe2017-03-21 11:20:02 -0700270
271 return ['update_engine_client', '--update', '--follow',
272 '--payload=%s' % payload_url, '--offset=%d' % ota.offset,
Kelvin Zhang4b883ea2020-10-08 13:26:44 -0400273 '--size=%d' % ota.size, '--headers="%s"' % headers.decode()]
Alex Deymo6751bbe2017-03-21 11:20:02 -0700274
275
276class AdbHost(object):
277 """Represents a device connected via ADB."""
278
279 def __init__(self, device_serial=None):
280 """Construct an instance.
281
282 Args:
283 device_serial: options string serial number of attached device.
284 """
285 self._device_serial = device_serial
286 self._command_prefix = ['adb']
287 if self._device_serial:
288 self._command_prefix += ['-s', self._device_serial]
289
Kelvin Zhang3a188952021-04-13 12:44:45 -0400290 def adb(self, command, timeout_seconds: float = None):
Alex Deymo6751bbe2017-03-21 11:20:02 -0700291 """Run an ADB command like "adb push".
292
293 Args:
294 command: list of strings containing command and arguments to run
295
296 Returns:
297 the program's return code.
298
299 Raises:
300 subprocess.CalledProcessError on command exit != 0.
301 """
302 command = self._command_prefix + command
303 logging.info('Running: %s', ' '.join(str(x) for x in command))
304 p = subprocess.Popen(command, universal_newlines=True)
Kelvin Zhang3a188952021-04-13 12:44:45 -0400305 p.wait(timeout_seconds)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700306 return p.returncode
307
Sen Jianga1784b72017-08-09 17:42:36 -0700308 def adb_output(self, command):
309 """Run an ADB command like "adb push" and return the output.
310
311 Args:
312 command: list of strings containing command and arguments to run
313
314 Returns:
315 the program's output as a string.
316
317 Raises:
318 subprocess.CalledProcessError on command exit != 0.
319 """
320 command = self._command_prefix + command
321 logging.info('Running: %s', ' '.join(str(x) for x in command))
322 return subprocess.check_output(command, universal_newlines=True)
323
Alex Deymo6751bbe2017-03-21 11:20:02 -0700324
Kelvin Zhang63b39112021-03-05 12:31:38 -0500325def PushMetadata(dut, otafile, metadata_path):
326 payload = update_payload.Payload(otafile)
327 payload.Init()
328 with tempfile.TemporaryDirectory() as tmpdir:
329 with zipfile.ZipFile(otafile, "r") as zfp:
330 extracted_path = os.path.join(tmpdir, "payload.bin")
331 with zfp.open("payload.bin") as payload_fp, \
332 open(extracted_path, "wb") as output_fp:
333 # Only extract the first |data_offset| bytes from the payload.
334 # This is because allocateSpaceForPayload only needs to see
335 # the manifest, not the entire payload.
336 # Extracting the entire payload works, but is slow for full
337 # OTA.
338 output_fp.write(payload_fp.read(payload.data_offset))
339
340 return dut.adb([
341 "push",
342 extracted_path,
343 metadata_path
344 ]) == 0
345
346
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700347def ParseSpeedLimit(arg: str) -> int:
348 arg = arg.strip().upper()
349 if not re.match(r"\d+[KkMmGgTt]?", arg):
350 raise argparse.ArgumentError(
351 "Wrong speed limit format, expected format is number followed by unit, such as 10K, 5m, 3G (case insensitive)")
352 unit = 1
353 if arg[-1].isalpha():
354 if arg[-1] == "K":
355 unit = 1024
356 elif arg[-1] == "M":
357 unit = 1024 * 1024
358 elif arg[-1] == "G":
359 unit = 1024 * 1024 * 1024
360 elif arg[-1] == "T":
361 unit = 1024 * 1024 * 1024 * 1024
362 else:
363 raise argparse.ArgumentError(
364 f"Unsupported unit for download speed: {arg[-1]}, supported units are K,M,G,T (case insensitive)")
365 return int(float(arg[:-1]) * unit)
366
367
Alex Deymo6751bbe2017-03-21 11:20:02 -0700368def main():
369 parser = argparse.ArgumentParser(description='Android A/B OTA helper.')
Sen Jiang3b15b592017-09-26 18:21:04 -0700370 parser.add_argument('otafile', metavar='PAYLOAD', type=str,
371 help='the OTA package file (a .zip file) or raw payload \
372 if device uses Omaha.')
Alex Deymo6751bbe2017-03-21 11:20:02 -0700373 parser.add_argument('--file', action='store_true',
374 help='Push the file to the device before updating.')
375 parser.add_argument('--no-push', action='store_true',
376 help='Skip the "push" command when using --file')
377 parser.add_argument('-s', type=str, default='', metavar='DEVICE',
378 help='The specific device to use.')
379 parser.add_argument('--no-verbose', action='store_true',
380 help='Less verbose output')
Sen Jianga1784b72017-08-09 17:42:36 -0700381 parser.add_argument('--public-key', type=str, default='',
382 help='Override the public key used to verify payload.')
Sen Jiang6fbfd7d2017-10-31 16:16:56 -0700383 parser.add_argument('--extra-headers', type=str, default='',
384 help='Extra headers to pass to the device.')
Tianjie Xu3f9be772019-11-02 18:31:50 -0700385 parser.add_argument('--secondary', action='store_true',
386 help='Update with the secondary payload in the package.')
Kelvin Zhang8212f532020-11-13 16:00:00 -0500387 parser.add_argument('--no-slot-switch', action='store_true',
388 help='Do not perform slot switch after the update.')
Kelvin Zhangbec0f072021-03-31 16:09:00 -0400389 parser.add_argument('--no-postinstall', action='store_true',
390 help='Do not execute postinstall scripts after the update.')
Kelvin Zhangffd21442021-04-14 09:09:41 -0400391 parser.add_argument('--allocate-only', action='store_true',
Kelvin Zhang51aad992021-02-19 14:46:28 -0500392 help='Allocate space for this OTA, instead of actually \
393 applying the OTA.')
Kelvin Zhangffd21442021-04-14 09:09:41 -0400394 parser.add_argument('--verify-only', action='store_true',
Kelvin Zhang63b39112021-03-05 12:31:38 -0500395 help='Verify metadata then exit, instead of applying the OTA.')
Kelvin Zhangffd21442021-04-14 09:09:41 -0400396 parser.add_argument('--no-care-map', action='store_true',
Kelvin Zhang5bd46222021-03-02 12:36:14 -0500397 help='Do not push care_map.pb to device.')
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700398 parser.add_argument('--perform-slot-switch', action='store_true',
399 help='Perform slot switch for this OTA package')
400 parser.add_argument('--perform-reset-slot-switch', action='store_true',
401 help='Perform reset slot switch for this OTA package')
Kelvin Zhang2451a302022-03-23 19:18:35 -0700402 parser.add_argument('--wipe-user-data', action='store_true',
403 help='Wipe userdata after installing OTA')
Kelvin Zhanga7407b52023-03-13 15:05:14 -0700404 parser.add_argument('--vabc-none', action='store_true',
405 help='Set Virtual AB Compression algorithm to none, but still use Android COW format')
Daniel Zheng9fc62b82023-03-24 22:57:20 +0000406 parser.add_argument('--disable-vabc', action='store_true',
407 help='Option to enable or disable vabc. If set to false, will fall back on A/B')
Kelvin Zhang6bef4902023-02-22 12:43:27 -0800408 parser.add_argument('--enable-threading', action='store_true',
409 help='Enable multi-threaded compression for VABC')
410 parser.add_argument('--batched-writes', action='store_true',
411 help='Enable batched writes for VABC')
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700412 parser.add_argument('--speed-limit', type=str,
413 help='Speed limit for serving payloads over HTTP. For '
414 'example: 10K, 5m, 1G, input is case insensitive')
415
Alex Deymo6751bbe2017-03-21 11:20:02 -0700416 args = parser.parse_args()
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700417 if args.speed_limit:
418 args.speed_limit = ParseSpeedLimit(args.speed_limit)
419
Alex Deymo6751bbe2017-03-21 11:20:02 -0700420 logging.basicConfig(
421 level=logging.WARNING if args.no_verbose else logging.INFO)
422
Nikita Ioffe3a327e62021-06-30 16:05:09 +0100423 start_time = time.perf_counter()
424
Alex Deymo6751bbe2017-03-21 11:20:02 -0700425 dut = AdbHost(args.s)
426
427 server_thread = None
428 # List of commands to execute on exit.
429 finalize_cmds = []
430 # Commands to execute when canceling an update.
431 cancel_cmd = ['shell', 'su', '0', 'update_engine_client', '--cancel']
432 # List of commands to perform the update.
433 cmds = []
434
Sen Jianga1784b72017-08-09 17:42:36 -0700435 help_cmd = ['shell', 'su', '0', 'update_engine_client', '--help']
Sen Jianga1784b72017-08-09 17:42:36 -0700436
Kelvin Zhang63b39112021-03-05 12:31:38 -0500437 metadata_path = "/data/ota_package/metadata"
Kelvin Zhang51aad992021-02-19 14:46:28 -0500438 if args.allocate_only:
Kelvin Zhang027eb382023-04-27 20:44:45 -0700439 with zipfile.ZipFile(args.otafile, "r") as zfp:
440 headers = zfp.read("payload_properties.txt").decode()
Kelvin Zhang63b39112021-03-05 12:31:38 -0500441 if PushMetadata(dut, args.otafile, metadata_path):
442 dut.adb([
443 "shell", "update_engine_client", "--allocate",
Kelvin Zhang027eb382023-04-27 20:44:45 -0700444 "--metadata={} --headers='{}'".format(metadata_path, headers)])
Kelvin Zhang63b39112021-03-05 12:31:38 -0500445 # Return 0, as we are executing ADB commands here, no work needed after
446 # this point
447 return 0
448 if args.verify_only:
449 if PushMetadata(dut, args.otafile, metadata_path):
450 dut.adb([
451 "shell", "update_engine_client", "--verify",
452 "--metadata={}".format(metadata_path)])
Kelvin Zhang51aad992021-02-19 14:46:28 -0500453 # Return 0, as we are executing ADB commands here, no work needed after
454 # this point
455 return 0
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700456 if args.perform_slot_switch:
457 assert PushMetadata(dut, args.otafile, metadata_path)
458 dut.adb(["shell", "update_engine_client",
459 "--switch_slot=true", "--metadata={}".format(metadata_path), "--follow"])
460 return 0
461 if args.perform_reset_slot_switch:
462 assert PushMetadata(dut, args.otafile, metadata_path)
463 dut.adb(["shell", "update_engine_client",
464 "--switch_slot=false", "--metadata={}".format(metadata_path)])
465 return 0
Kelvin Zhang51aad992021-02-19 14:46:28 -0500466
Kelvin Zhang8212f532020-11-13 16:00:00 -0500467 if args.no_slot_switch:
468 args.extra_headers += "\nSWITCH_SLOT_ON_REBOOT=0"
Kelvin Zhangbec0f072021-03-31 16:09:00 -0400469 if args.no_postinstall:
470 args.extra_headers += "\nRUN_POST_INSTALL=0"
Kelvin Zhang2451a302022-03-23 19:18:35 -0700471 if args.wipe_user_data:
472 args.extra_headers += "\nPOWERWASH=1"
Kelvin Zhanga7407b52023-03-13 15:05:14 -0700473 if args.vabc_none:
474 args.extra_headers += "\nVABC_NONE=1"
Daniel Zheng9fc62b82023-03-24 22:57:20 +0000475 if args.disable_vabc:
476 args.extra_headers += "\nDISABLE_VABC=1"
Kelvin Zhang6bef4902023-02-22 12:43:27 -0800477 if args.enable_threading:
478 args.extra_headers += "\nENABLE_THREADING=1"
479 if args.batched_writes:
480 args.extra_headers += "\nBATCHED_WRITES=1"
Kelvin Zhang8212f532020-11-13 16:00:00 -0500481
Kelvin Zhang5bd46222021-03-02 12:36:14 -0500482 with zipfile.ZipFile(args.otafile) as zfp:
483 CARE_MAP_ENTRY_NAME = "care_map.pb"
484 if CARE_MAP_ENTRY_NAME in zfp.namelist() and not args.no_care_map:
485 # Need root permission to push to /data
486 dut.adb(["root"])
Kelvin Zhang472d5612021-03-05 12:32:19 -0500487 with tempfile.NamedTemporaryFile() as care_map_fp:
Kelvin Zhang5bd46222021-03-02 12:36:14 -0500488 care_map_fp.write(zfp.read(CARE_MAP_ENTRY_NAME))
489 care_map_fp.flush()
490 dut.adb(["push", care_map_fp.name,
Kelvin Zhangffd21442021-04-14 09:09:41 -0400491 "/data/ota_package/" + CARE_MAP_ENTRY_NAME])
Kelvin Zhang5bd46222021-03-02 12:36:14 -0500492
Alex Deymo6751bbe2017-03-21 11:20:02 -0700493 if args.file:
494 # Update via pushing a file to /data.
495 device_ota_file = os.path.join(OTA_PACKAGE_PATH, 'debug.zip')
496 payload_url = 'file://' + device_ota_file
497 if not args.no_push:
Tao Baoabb45a52017-10-25 11:13:03 -0700498 data_local_tmp_file = '/data/local/tmp/debug.zip'
499 cmds.append(['push', args.otafile, data_local_tmp_file])
500 cmds.append(['shell', 'su', '0', 'mv', data_local_tmp_file,
501 device_ota_file])
502 cmds.append(['shell', 'su', '0', 'chcon',
503 'u:object_r:ota_package_file:s0', device_ota_file])
Alex Deymo6751bbe2017-03-21 11:20:02 -0700504 cmds.append(['shell', 'su', '0', 'chown', 'system:cache', device_ota_file])
505 cmds.append(['shell', 'su', '0', 'chmod', '0660', device_ota_file])
506 else:
507 # Update via sending the payload over the network with an "adb reverse"
508 # command.
Sen Jianga1784b72017-08-09 17:42:36 -0700509 payload_url = 'http://127.0.0.1:%d/payload' % DEVICE_PORT
Kelvin Zhang033e1732023-09-11 16:32:11 -0700510 serving_range = (0, os.stat(args.otafile).st_size)
Kelvin Zhang46a860c2022-09-28 19:42:39 -0700511 server_thread = StartServer(args.otafile, serving_range, args.speed_limit)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700512 cmds.append(
Sen Jianga1784b72017-08-09 17:42:36 -0700513 ['reverse', 'tcp:%d' % DEVICE_PORT, 'tcp:%d' % server_thread.port])
514 finalize_cmds.append(['reverse', '--remove', 'tcp:%d' % DEVICE_PORT])
515
516 if args.public_key:
517 payload_key_dir = os.path.dirname(PAYLOAD_KEY_PATH)
518 cmds.append(
519 ['shell', 'su', '0', 'mount', '-t', 'tmpfs', 'tmpfs', payload_key_dir])
520 # Allow adb push to payload_key_dir
521 cmds.append(['shell', 'su', '0', 'chcon', 'u:object_r:shell_data_file:s0',
522 payload_key_dir])
523 cmds.append(['push', args.public_key, PAYLOAD_KEY_PATH])
524 # Allow update_engine to read it.
525 cmds.append(['shell', 'su', '0', 'chcon', '-R', 'u:object_r:system_file:s0',
526 payload_key_dir])
527 finalize_cmds.append(['shell', 'su', '0', 'umount', payload_key_dir])
Alex Deymo6751bbe2017-03-21 11:20:02 -0700528
529 try:
530 # The main update command using the configured payload_url.
Kelvin Zhang033e1732023-09-11 16:32:11 -0700531 update_cmd = AndroidUpdateCommand(args.otafile, args.secondary,
Tianjie Xu3f9be772019-11-02 18:31:50 -0700532 payload_url, args.extra_headers)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700533 cmds.append(['shell', 'su', '0'] + update_cmd)
534
535 for cmd in cmds:
536 dut.adb(cmd)
537 except KeyboardInterrupt:
538 dut.adb(cancel_cmd)
539 finally:
540 if server_thread:
541 server_thread.StopServer()
542 for cmd in finalize_cmds:
Kelvin Zhang3a188952021-04-13 12:44:45 -0400543 dut.adb(cmd, 5)
Alex Deymo6751bbe2017-03-21 11:20:02 -0700544
Nikita Ioffe3a327e62021-06-30 16:05:09 +0100545 logging.info('Update took %.3f seconds', (time.perf_counter() - start_time))
Alex Deymo6751bbe2017-03-21 11:20:02 -0700546 return 0
547
Andrew Lassalle165843c2019-11-05 13:30:34 -0800548
Alex Deymo6751bbe2017-03-21 11:20:02 -0700549if __name__ == '__main__':
550 sys.exit(main())