| Yifan Hong | 0c71550 | 2021-04-19 13:48:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 2 | # | 
|  | 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 Zhang | 4b883ea | 2020-10-08 13:26:44 -0400 | [diff] [blame] | 20 | from __future__ import print_function | 
| Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 21 | from __future__ import absolute_import | 
|  | 22 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 23 | import argparse | 
| Kelvin Zhang | aba70ab | 2020-08-04 10:32:59 -0400 | [diff] [blame] | 24 | import binascii | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 25 | import hashlib | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 26 | import logging | 
|  | 27 | import os | 
|  | 28 | import socket | 
|  | 29 | import subprocess | 
|  | 30 | import sys | 
| Kelvin Zhang | aba70ab | 2020-08-04 10:32:59 -0400 | [diff] [blame] | 31 | import struct | 
| Kelvin Zhang | 51aad99 | 2021-02-19 14:46:28 -0500 | [diff] [blame] | 32 | import tempfile | 
| Nikita Ioffe | 3a327e6 | 2021-06-30 16:05:09 +0100 | [diff] [blame] | 33 | import time | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 34 | import threading | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 35 | import xml.etree.ElementTree | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 36 | import zipfile | 
|  | 37 |  | 
| Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 38 | from six.moves import BaseHTTPServer | 
|  | 39 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 40 | import update_payload.payload | 
|  | 41 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | # The path used to store the OTA package when applying the package from a file. | 
|  | 44 | OTA_PACKAGE_PATH = '/data/ota_package' | 
|  | 45 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 46 | # The path to the payload public key on the device. | 
|  | 47 | PAYLOAD_KEY_PATH = '/etc/update_engine/update-payload-key.pub.pem' | 
|  | 48 |  | 
|  | 49 | # The port on the device that update_engine should connect to. | 
|  | 50 | DEVICE_PORT = 1234 | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 51 |  | 
| Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 52 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 53 | def CopyFileObjLength(fsrc, fdst, buffer_size=128 * 1024, copy_length=None): | 
|  | 54 | """Copy from a file object to another. | 
|  | 55 |  | 
|  | 56 | This function is similar to shutil.copyfileobj except that it allows to copy | 
|  | 57 | less than the full source file. | 
|  | 58 |  | 
|  | 59 | Args: | 
|  | 60 | fsrc: source file object where to read from. | 
|  | 61 | fdst: destination file object where to write to. | 
|  | 62 | buffer_size: size of the copy buffer in memory. | 
|  | 63 | copy_length: maximum number of bytes to copy, or None to copy everything. | 
|  | 64 |  | 
|  | 65 | Returns: | 
|  | 66 | the number of bytes copied. | 
|  | 67 | """ | 
|  | 68 | copied = 0 | 
|  | 69 | while True: | 
|  | 70 | chunk_size = buffer_size | 
|  | 71 | if copy_length is not None: | 
|  | 72 | chunk_size = min(chunk_size, copy_length - copied) | 
|  | 73 | if not chunk_size: | 
|  | 74 | break | 
|  | 75 | buf = fsrc.read(chunk_size) | 
|  | 76 | if not buf: | 
|  | 77 | break | 
|  | 78 | fdst.write(buf) | 
|  | 79 | copied += len(buf) | 
|  | 80 | return copied | 
|  | 81 |  | 
|  | 82 |  | 
|  | 83 | class AndroidOTAPackage(object): | 
|  | 84 | """Android update payload using the .zip format. | 
|  | 85 |  | 
|  | 86 | Android OTA packages traditionally used a .zip file to store the payload. When | 
|  | 87 | applying A/B updates over the network, a payload binary is stored RAW inside | 
|  | 88 | this .zip file which is used by update_engine to apply the payload. To do | 
|  | 89 | this, an offset and size inside the .zip file are provided. | 
|  | 90 | """ | 
|  | 91 |  | 
|  | 92 | # Android OTA package file paths. | 
|  | 93 | OTA_PAYLOAD_BIN = 'payload.bin' | 
|  | 94 | OTA_PAYLOAD_PROPERTIES_TXT = 'payload_properties.txt' | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 95 | SECONDARY_OTA_PAYLOAD_BIN = 'secondary/payload.bin' | 
|  | 96 | SECONDARY_OTA_PAYLOAD_PROPERTIES_TXT = 'secondary/payload_properties.txt' | 
| Kelvin Zhang | aba70ab | 2020-08-04 10:32:59 -0400 | [diff] [blame] | 97 | PAYLOAD_MAGIC_HEADER = b'CrAU' | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 98 |  | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 99 | def __init__(self, otafilename, secondary_payload=False): | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 100 | self.otafilename = otafilename | 
|  | 101 |  | 
|  | 102 | otazip = zipfile.ZipFile(otafilename, 'r') | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 103 | payload_entry = (self.SECONDARY_OTA_PAYLOAD_BIN if secondary_payload else | 
|  | 104 | self.OTA_PAYLOAD_BIN) | 
|  | 105 | payload_info = otazip.getinfo(payload_entry) | 
| Kelvin Zhang | aba70ab | 2020-08-04 10:32:59 -0400 | [diff] [blame] | 106 |  | 
|  | 107 | if payload_info.compress_type != 0: | 
|  | 108 | logging.error( | 
| Kelvin Zhang | 07676f5 | 2020-12-01 10:45:09 -0500 | [diff] [blame] | 109 | "Expected payload to be uncompressed, got compression method %d", | 
| Kelvin Zhang | aba70ab | 2020-08-04 10:32:59 -0400 | [diff] [blame] | 110 | payload_info.compress_type) | 
|  | 111 | # Don't use len(payload_info.extra). Because that returns size of extra | 
|  | 112 | # fields in central directory. We need to look at local file directory, | 
|  | 113 | # as these two might have different sizes. | 
|  | 114 | with open(otafilename, "rb") as fp: | 
|  | 115 | fp.seek(payload_info.header_offset) | 
|  | 116 | data = fp.read(zipfile.sizeFileHeader) | 
|  | 117 | fheader = struct.unpack(zipfile.structFileHeader, data) | 
|  | 118 | # Last two fields of local file header are filename length and | 
|  | 119 | # extra length | 
|  | 120 | filename_len = fheader[-2] | 
|  | 121 | extra_len = fheader[-1] | 
|  | 122 | self.offset = payload_info.header_offset | 
|  | 123 | self.offset += zipfile.sizeFileHeader | 
|  | 124 | self.offset += filename_len + extra_len | 
|  | 125 | self.size = payload_info.file_size | 
|  | 126 | fp.seek(self.offset) | 
|  | 127 | payload_header = fp.read(4) | 
|  | 128 | if payload_header != self.PAYLOAD_MAGIC_HEADER: | 
|  | 129 | logging.warning( | 
| Kelvin Zhang | 07676f5 | 2020-12-01 10:45:09 -0500 | [diff] [blame] | 130 | "Invalid header, expected %s, got %s." | 
| Kelvin Zhang | aba70ab | 2020-08-04 10:32:59 -0400 | [diff] [blame] | 131 | "Either the offset is not correct, or payload is corrupted", | 
|  | 132 | binascii.hexlify(self.PAYLOAD_MAGIC_HEADER), | 
| Kelvin Zhang | 07676f5 | 2020-12-01 10:45:09 -0500 | [diff] [blame] | 133 | binascii.hexlify(payload_header)) | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 134 |  | 
|  | 135 | property_entry = (self.SECONDARY_OTA_PAYLOAD_PROPERTIES_TXT if | 
|  | 136 | secondary_payload else self.OTA_PAYLOAD_PROPERTIES_TXT) | 
|  | 137 | self.properties = otazip.read(property_entry) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 138 |  | 
|  | 139 |  | 
|  | 140 | class UpdateHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 
|  | 141 | """A HTTPServer that supports single-range requests. | 
|  | 142 |  | 
|  | 143 | Attributes: | 
|  | 144 | serving_payload: path to the only payload file we are serving. | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 145 | serving_range: the start offset and size tuple of the payload. | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 146 | """ | 
|  | 147 |  | 
|  | 148 | @staticmethod | 
| Sen Jiang | 1048559 | 2017-08-15 18:20:24 -0700 | [diff] [blame] | 149 | def _parse_range(range_str, file_size): | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 150 | """Parse an HTTP range string. | 
|  | 151 |  | 
|  | 152 | Args: | 
|  | 153 | range_str: HTTP Range header in the request, not including "Header:". | 
|  | 154 | file_size: total size of the serving file. | 
|  | 155 |  | 
|  | 156 | Returns: | 
|  | 157 | A tuple (start_range, end_range) with the range of bytes requested. | 
|  | 158 | """ | 
|  | 159 | start_range = 0 | 
|  | 160 | end_range = file_size | 
|  | 161 |  | 
|  | 162 | if range_str: | 
|  | 163 | range_str = range_str.split('=', 1)[1] | 
|  | 164 | s, e = range_str.split('-', 1) | 
|  | 165 | if s: | 
|  | 166 | start_range = int(s) | 
|  | 167 | if e: | 
|  | 168 | end_range = int(e) + 1 | 
|  | 169 | elif e: | 
|  | 170 | if int(e) < file_size: | 
|  | 171 | start_range = file_size - int(e) | 
|  | 172 | return start_range, end_range | 
|  | 173 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 174 | def do_GET(self):  # pylint: disable=invalid-name | 
|  | 175 | """Reply with the requested payload file.""" | 
|  | 176 | if self.path != '/payload': | 
|  | 177 | self.send_error(404, 'Unknown request') | 
|  | 178 | return | 
|  | 179 |  | 
|  | 180 | if not self.serving_payload: | 
|  | 181 | self.send_error(500, 'No serving payload set') | 
|  | 182 | return | 
|  | 183 |  | 
|  | 184 | try: | 
|  | 185 | f = open(self.serving_payload, 'rb') | 
|  | 186 | except IOError: | 
|  | 187 | self.send_error(404, 'File not found') | 
|  | 188 | return | 
|  | 189 | # Handle the range request. | 
|  | 190 | if 'Range' in self.headers: | 
|  | 191 | self.send_response(206) | 
|  | 192 | else: | 
|  | 193 | self.send_response(200) | 
|  | 194 |  | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 195 | serving_start, serving_size = self.serving_range | 
| Sen Jiang | 1048559 | 2017-08-15 18:20:24 -0700 | [diff] [blame] | 196 | start_range, end_range = self._parse_range(self.headers.get('range'), | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 197 | serving_size) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 198 | logging.info('Serving request for %s from %s [%d, %d) length: %d', | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 199 | self.path, self.serving_payload, serving_start + start_range, | 
|  | 200 | serving_start + end_range, end_range - start_range) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 201 |  | 
|  | 202 | self.send_header('Accept-Ranges', 'bytes') | 
|  | 203 | self.send_header('Content-Range', | 
|  | 204 | 'bytes ' + str(start_range) + '-' + str(end_range - 1) + | 
|  | 205 | '/' + str(end_range - start_range)) | 
|  | 206 | self.send_header('Content-Length', end_range - start_range) | 
|  | 207 |  | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 208 | stat = os.fstat(f.fileno()) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 209 | self.send_header('Last-Modified', self.date_time_string(stat.st_mtime)) | 
|  | 210 | self.send_header('Content-type', 'application/octet-stream') | 
|  | 211 | self.end_headers() | 
|  | 212 |  | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 213 | f.seek(serving_start + start_range) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 214 | CopyFileObjLength(f, self.wfile, copy_length=end_range - start_range) | 
|  | 215 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 216 | def do_POST(self):  # pylint: disable=invalid-name | 
|  | 217 | """Reply with the omaha response xml.""" | 
|  | 218 | if self.path != '/update': | 
|  | 219 | self.send_error(404, 'Unknown request') | 
|  | 220 | return | 
|  | 221 |  | 
|  | 222 | if not self.serving_payload: | 
|  | 223 | self.send_error(500, 'No serving payload set') | 
|  | 224 | return | 
|  | 225 |  | 
|  | 226 | try: | 
|  | 227 | f = open(self.serving_payload, 'rb') | 
|  | 228 | except IOError: | 
|  | 229 | self.send_error(404, 'File not found') | 
|  | 230 | return | 
|  | 231 |  | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 232 | content_length = int(self.headers.getheader('Content-Length')) | 
|  | 233 | request_xml = self.rfile.read(content_length) | 
|  | 234 | xml_root = xml.etree.ElementTree.fromstring(request_xml) | 
|  | 235 | appid = None | 
|  | 236 | for app in xml_root.iter('app'): | 
|  | 237 | if 'appid' in app.attrib: | 
|  | 238 | appid = app.attrib['appid'] | 
|  | 239 | break | 
|  | 240 | if not appid: | 
|  | 241 | self.send_error(400, 'No appid in Omaha request') | 
|  | 242 | return | 
|  | 243 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 244 | self.send_response(200) | 
|  | 245 | self.send_header("Content-type", "text/xml") | 
|  | 246 | self.end_headers() | 
|  | 247 |  | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 248 | serving_start, serving_size = self.serving_range | 
|  | 249 | sha256 = hashlib.sha256() | 
|  | 250 | f.seek(serving_start) | 
|  | 251 | bytes_to_hash = serving_size | 
|  | 252 | while bytes_to_hash: | 
|  | 253 | buf = f.read(min(bytes_to_hash, 1024 * 1024)) | 
|  | 254 | if not buf: | 
|  | 255 | self.send_error(500, 'Payload too small') | 
|  | 256 | return | 
|  | 257 | sha256.update(buf) | 
|  | 258 | bytes_to_hash -= len(buf) | 
|  | 259 |  | 
|  | 260 | payload = update_payload.Payload(f, payload_file_offset=serving_start) | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 261 | payload.Init() | 
|  | 262 |  | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 263 | response_xml = ''' | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 264 | <?xml version="1.0" encoding="UTF-8"?> | 
|  | 265 | <response protocol="3.0"> | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 266 | <app appid="{appid}"> | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 267 | <updatecheck status="ok"> | 
|  | 268 | <urls> | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 269 | <url codebase="http://127.0.0.1:{port}/"/> | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 270 | </urls> | 
|  | 271 | <manifest version="0.0.0.1"> | 
|  | 272 | <actions> | 
|  | 273 | <action event="install" run="payload"/> | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 274 | <action event="postinstall" MetadataSize="{metadata_size}"/> | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 275 | </actions> | 
|  | 276 | <packages> | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 277 | <package hash_sha256="{payload_hash}" name="payload" size="{payload_size}"/> | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 278 | </packages> | 
|  | 279 | </manifest> | 
|  | 280 | </updatecheck> | 
|  | 281 | </app> | 
|  | 282 | </response> | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 283 | '''.format(appid=appid, port=DEVICE_PORT, | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 284 | metadata_size=payload.metadata_size, | 
|  | 285 | payload_hash=sha256.hexdigest(), | 
|  | 286 | payload_size=serving_size) | 
| Sen Jiang | 144f9f8 | 2017-09-26 15:49:45 -0700 | [diff] [blame] | 287 | self.wfile.write(response_xml.strip()) | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 288 | return | 
|  | 289 |  | 
|  | 290 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 291 | class ServerThread(threading.Thread): | 
|  | 292 | """A thread for serving HTTP requests.""" | 
|  | 293 |  | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 294 | def __init__(self, ota_filename, serving_range): | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 295 | threading.Thread.__init__(self) | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 296 | # serving_payload and serving_range are class attributes and the | 
|  | 297 | # UpdateHandler class is instantiated with every request. | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 298 | UpdateHandler.serving_payload = ota_filename | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 299 | UpdateHandler.serving_range = serving_range | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 300 | self._httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), UpdateHandler) | 
|  | 301 | self.port = self._httpd.server_port | 
|  | 302 |  | 
|  | 303 | def run(self): | 
|  | 304 | try: | 
|  | 305 | self._httpd.serve_forever() | 
|  | 306 | except (KeyboardInterrupt, socket.error): | 
|  | 307 | pass | 
|  | 308 | logging.info('Server Terminated') | 
|  | 309 |  | 
|  | 310 | def StopServer(self): | 
| Kelvin Zhang | 4b883ea | 2020-10-08 13:26:44 -0400 | [diff] [blame] | 311 | self._httpd.shutdown() | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 312 | self._httpd.socket.close() | 
|  | 313 |  | 
|  | 314 |  | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 315 | def StartServer(ota_filename, serving_range): | 
|  | 316 | t = ServerThread(ota_filename, serving_range) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 317 | t.start() | 
|  | 318 | return t | 
|  | 319 |  | 
|  | 320 |  | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 321 | def AndroidUpdateCommand(ota_filename, secondary, payload_url, extra_headers): | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 322 | """Return the command to run to start the update in the Android device.""" | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 323 | ota = AndroidOTAPackage(ota_filename, secondary) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 324 | headers = ota.properties | 
| Kelvin Zhang | 4b883ea | 2020-10-08 13:26:44 -0400 | [diff] [blame] | 325 | headers += b'USER_AGENT=Dalvik (something, something)\n' | 
|  | 326 | headers += b'NETWORK_ID=0\n' | 
|  | 327 | headers += extra_headers.encode() | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 328 |  | 
|  | 329 | return ['update_engine_client', '--update', '--follow', | 
|  | 330 | '--payload=%s' % payload_url, '--offset=%d' % ota.offset, | 
| Kelvin Zhang | 4b883ea | 2020-10-08 13:26:44 -0400 | [diff] [blame] | 331 | '--size=%d' % ota.size, '--headers="%s"' % headers.decode()] | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 332 |  | 
|  | 333 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 334 | def OmahaUpdateCommand(omaha_url): | 
|  | 335 | """Return the command to run to start the update in a device using Omaha.""" | 
|  | 336 | return ['update_engine_client', '--update', '--follow', | 
|  | 337 | '--omaha_url=%s' % omaha_url] | 
|  | 338 |  | 
|  | 339 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 340 | class AdbHost(object): | 
|  | 341 | """Represents a device connected via ADB.""" | 
|  | 342 |  | 
|  | 343 | def __init__(self, device_serial=None): | 
|  | 344 | """Construct an instance. | 
|  | 345 |  | 
|  | 346 | Args: | 
|  | 347 | device_serial: options string serial number of attached device. | 
|  | 348 | """ | 
|  | 349 | self._device_serial = device_serial | 
|  | 350 | self._command_prefix = ['adb'] | 
|  | 351 | if self._device_serial: | 
|  | 352 | self._command_prefix += ['-s', self._device_serial] | 
|  | 353 |  | 
| Kelvin Zhang | 3a18895 | 2021-04-13 12:44:45 -0400 | [diff] [blame] | 354 | def adb(self, command, timeout_seconds: float = None): | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 355 | """Run an ADB command like "adb push". | 
|  | 356 |  | 
|  | 357 | Args: | 
|  | 358 | command: list of strings containing command and arguments to run | 
|  | 359 |  | 
|  | 360 | Returns: | 
|  | 361 | the program's return code. | 
|  | 362 |  | 
|  | 363 | Raises: | 
|  | 364 | subprocess.CalledProcessError on command exit != 0. | 
|  | 365 | """ | 
|  | 366 | command = self._command_prefix + command | 
|  | 367 | logging.info('Running: %s', ' '.join(str(x) for x in command)) | 
|  | 368 | p = subprocess.Popen(command, universal_newlines=True) | 
| Kelvin Zhang | 3a18895 | 2021-04-13 12:44:45 -0400 | [diff] [blame] | 369 | p.wait(timeout_seconds) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 370 | return p.returncode | 
|  | 371 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 372 | def adb_output(self, command): | 
|  | 373 | """Run an ADB command like "adb push" and return the output. | 
|  | 374 |  | 
|  | 375 | Args: | 
|  | 376 | command: list of strings containing command and arguments to run | 
|  | 377 |  | 
|  | 378 | Returns: | 
|  | 379 | the program's output as a string. | 
|  | 380 |  | 
|  | 381 | Raises: | 
|  | 382 | subprocess.CalledProcessError on command exit != 0. | 
|  | 383 | """ | 
|  | 384 | command = self._command_prefix + command | 
|  | 385 | logging.info('Running: %s', ' '.join(str(x) for x in command)) | 
|  | 386 | return subprocess.check_output(command, universal_newlines=True) | 
|  | 387 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 388 |  | 
| Kelvin Zhang | 63b3911 | 2021-03-05 12:31:38 -0500 | [diff] [blame] | 389 | def PushMetadata(dut, otafile, metadata_path): | 
|  | 390 | payload = update_payload.Payload(otafile) | 
|  | 391 | payload.Init() | 
|  | 392 | with tempfile.TemporaryDirectory() as tmpdir: | 
|  | 393 | with zipfile.ZipFile(otafile, "r") as zfp: | 
|  | 394 | extracted_path = os.path.join(tmpdir, "payload.bin") | 
|  | 395 | with zfp.open("payload.bin") as payload_fp, \ | 
|  | 396 | open(extracted_path, "wb") as output_fp: | 
|  | 397 | # Only extract the first |data_offset| bytes from the payload. | 
|  | 398 | # This is because allocateSpaceForPayload only needs to see | 
|  | 399 | # the manifest, not the entire payload. | 
|  | 400 | # Extracting the entire payload works, but is slow for full | 
|  | 401 | # OTA. | 
|  | 402 | output_fp.write(payload_fp.read(payload.data_offset)) | 
|  | 403 |  | 
|  | 404 | return dut.adb([ | 
|  | 405 | "push", | 
|  | 406 | extracted_path, | 
|  | 407 | metadata_path | 
|  | 408 | ]) == 0 | 
|  | 409 |  | 
|  | 410 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 411 | def main(): | 
|  | 412 | parser = argparse.ArgumentParser(description='Android A/B OTA helper.') | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 413 | parser.add_argument('otafile', metavar='PAYLOAD', type=str, | 
|  | 414 | help='the OTA package file (a .zip file) or raw payload \ | 
|  | 415 | if device uses Omaha.') | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 416 | parser.add_argument('--file', action='store_true', | 
|  | 417 | help='Push the file to the device before updating.') | 
|  | 418 | parser.add_argument('--no-push', action='store_true', | 
|  | 419 | help='Skip the "push" command when using --file') | 
|  | 420 | parser.add_argument('-s', type=str, default='', metavar='DEVICE', | 
|  | 421 | help='The specific device to use.') | 
|  | 422 | parser.add_argument('--no-verbose', action='store_true', | 
|  | 423 | help='Less verbose output') | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 424 | parser.add_argument('--public-key', type=str, default='', | 
|  | 425 | help='Override the public key used to verify payload.') | 
| Sen Jiang | 6fbfd7d | 2017-10-31 16:16:56 -0700 | [diff] [blame] | 426 | parser.add_argument('--extra-headers', type=str, default='', | 
|  | 427 | help='Extra headers to pass to the device.') | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 428 | parser.add_argument('--secondary', action='store_true', | 
|  | 429 | help='Update with the secondary payload in the package.') | 
| Kelvin Zhang | 8212f53 | 2020-11-13 16:00:00 -0500 | [diff] [blame] | 430 | parser.add_argument('--no-slot-switch', action='store_true', | 
|  | 431 | help='Do not perform slot switch after the update.') | 
| Kelvin Zhang | bec0f07 | 2021-03-31 16:09:00 -0400 | [diff] [blame] | 432 | parser.add_argument('--no-postinstall', action='store_true', | 
|  | 433 | help='Do not execute postinstall scripts after the update.') | 
| Kelvin Zhang | ffd2144 | 2021-04-14 09:09:41 -0400 | [diff] [blame] | 434 | parser.add_argument('--allocate-only', action='store_true', | 
| Kelvin Zhang | 51aad99 | 2021-02-19 14:46:28 -0500 | [diff] [blame] | 435 | help='Allocate space for this OTA, instead of actually \ | 
|  | 436 | applying the OTA.') | 
| Kelvin Zhang | ffd2144 | 2021-04-14 09:09:41 -0400 | [diff] [blame] | 437 | parser.add_argument('--verify-only', action='store_true', | 
| Kelvin Zhang | 63b3911 | 2021-03-05 12:31:38 -0500 | [diff] [blame] | 438 | help='Verify metadata then exit, instead of applying the OTA.') | 
| Kelvin Zhang | ffd2144 | 2021-04-14 09:09:41 -0400 | [diff] [blame] | 439 | parser.add_argument('--no-care-map', action='store_true', | 
| Kelvin Zhang | 5bd4622 | 2021-03-02 12:36:14 -0500 | [diff] [blame] | 440 | help='Do not push care_map.pb to device.') | 
| Kelvin Zhang | c56afa3 | 2021-08-13 12:32:31 -0700 | [diff] [blame] | 441 | parser.add_argument('--perform-slot-switch', action='store_true', | 
|  | 442 | help='Perform slot switch for this OTA package') | 
|  | 443 | parser.add_argument('--perform-reset-slot-switch', action='store_true', | 
|  | 444 | help='Perform reset slot switch for this OTA package') | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 445 | args = parser.parse_args() | 
|  | 446 | logging.basicConfig( | 
|  | 447 | level=logging.WARNING if args.no_verbose else logging.INFO) | 
|  | 448 |  | 
| Nikita Ioffe | 3a327e6 | 2021-06-30 16:05:09 +0100 | [diff] [blame] | 449 | start_time = time.perf_counter() | 
|  | 450 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 451 | dut = AdbHost(args.s) | 
|  | 452 |  | 
|  | 453 | server_thread = None | 
|  | 454 | # List of commands to execute on exit. | 
|  | 455 | finalize_cmds = [] | 
|  | 456 | # Commands to execute when canceling an update. | 
|  | 457 | cancel_cmd = ['shell', 'su', '0', 'update_engine_client', '--cancel'] | 
|  | 458 | # List of commands to perform the update. | 
|  | 459 | cmds = [] | 
|  | 460 |  | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 461 | help_cmd = ['shell', 'su', '0', 'update_engine_client', '--help'] | 
|  | 462 | use_omaha = 'omaha' in dut.adb_output(help_cmd) | 
|  | 463 |  | 
| Kelvin Zhang | 63b3911 | 2021-03-05 12:31:38 -0500 | [diff] [blame] | 464 | metadata_path = "/data/ota_package/metadata" | 
| Kelvin Zhang | 51aad99 | 2021-02-19 14:46:28 -0500 | [diff] [blame] | 465 | if args.allocate_only: | 
| Kelvin Zhang | 63b3911 | 2021-03-05 12:31:38 -0500 | [diff] [blame] | 466 | if PushMetadata(dut, args.otafile, metadata_path): | 
|  | 467 | dut.adb([ | 
|  | 468 | "shell", "update_engine_client", "--allocate", | 
|  | 469 | "--metadata={}".format(metadata_path)]) | 
|  | 470 | # Return 0, as we are executing ADB commands here, no work needed after | 
|  | 471 | # this point | 
|  | 472 | return 0 | 
|  | 473 | if args.verify_only: | 
|  | 474 | if PushMetadata(dut, args.otafile, metadata_path): | 
|  | 475 | dut.adb([ | 
|  | 476 | "shell", "update_engine_client", "--verify", | 
|  | 477 | "--metadata={}".format(metadata_path)]) | 
| Kelvin Zhang | 51aad99 | 2021-02-19 14:46:28 -0500 | [diff] [blame] | 478 | # Return 0, as we are executing ADB commands here, no work needed after | 
|  | 479 | # this point | 
|  | 480 | return 0 | 
| Kelvin Zhang | c56afa3 | 2021-08-13 12:32:31 -0700 | [diff] [blame] | 481 | if args.perform_slot_switch: | 
|  | 482 | assert PushMetadata(dut, args.otafile, metadata_path) | 
|  | 483 | dut.adb(["shell", "update_engine_client", | 
|  | 484 | "--switch_slot=true", "--metadata={}".format(metadata_path), "--follow"]) | 
|  | 485 | return 0 | 
|  | 486 | if args.perform_reset_slot_switch: | 
|  | 487 | assert PushMetadata(dut, args.otafile, metadata_path) | 
|  | 488 | dut.adb(["shell", "update_engine_client", | 
|  | 489 | "--switch_slot=false", "--metadata={}".format(metadata_path)]) | 
|  | 490 | return 0 | 
| Kelvin Zhang | 51aad99 | 2021-02-19 14:46:28 -0500 | [diff] [blame] | 491 |  | 
| Kelvin Zhang | 8212f53 | 2020-11-13 16:00:00 -0500 | [diff] [blame] | 492 | if args.no_slot_switch: | 
|  | 493 | args.extra_headers += "\nSWITCH_SLOT_ON_REBOOT=0" | 
| Kelvin Zhang | bec0f07 | 2021-03-31 16:09:00 -0400 | [diff] [blame] | 494 | if args.no_postinstall: | 
|  | 495 | args.extra_headers += "\nRUN_POST_INSTALL=0" | 
| Kelvin Zhang | 8212f53 | 2020-11-13 16:00:00 -0500 | [diff] [blame] | 496 |  | 
| Kelvin Zhang | 5bd4622 | 2021-03-02 12:36:14 -0500 | [diff] [blame] | 497 | with zipfile.ZipFile(args.otafile) as zfp: | 
|  | 498 | CARE_MAP_ENTRY_NAME = "care_map.pb" | 
|  | 499 | if CARE_MAP_ENTRY_NAME in zfp.namelist() and not args.no_care_map: | 
|  | 500 | # Need root permission to push to /data | 
|  | 501 | dut.adb(["root"]) | 
| Kelvin Zhang | 472d561 | 2021-03-05 12:32:19 -0500 | [diff] [blame] | 502 | with tempfile.NamedTemporaryFile() as care_map_fp: | 
| Kelvin Zhang | 5bd4622 | 2021-03-02 12:36:14 -0500 | [diff] [blame] | 503 | care_map_fp.write(zfp.read(CARE_MAP_ENTRY_NAME)) | 
|  | 504 | care_map_fp.flush() | 
|  | 505 | dut.adb(["push", care_map_fp.name, | 
| Kelvin Zhang | ffd2144 | 2021-04-14 09:09:41 -0400 | [diff] [blame] | 506 | "/data/ota_package/" + CARE_MAP_ENTRY_NAME]) | 
| Kelvin Zhang | 5bd4622 | 2021-03-02 12:36:14 -0500 | [diff] [blame] | 507 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 508 | if args.file: | 
|  | 509 | # Update via pushing a file to /data. | 
|  | 510 | device_ota_file = os.path.join(OTA_PACKAGE_PATH, 'debug.zip') | 
|  | 511 | payload_url = 'file://' + device_ota_file | 
|  | 512 | if not args.no_push: | 
| Tao Bao | abb45a5 | 2017-10-25 11:13:03 -0700 | [diff] [blame] | 513 | data_local_tmp_file = '/data/local/tmp/debug.zip' | 
|  | 514 | cmds.append(['push', args.otafile, data_local_tmp_file]) | 
|  | 515 | cmds.append(['shell', 'su', '0', 'mv', data_local_tmp_file, | 
|  | 516 | device_ota_file]) | 
|  | 517 | cmds.append(['shell', 'su', '0', 'chcon', | 
|  | 518 | 'u:object_r:ota_package_file:s0', device_ota_file]) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 519 | cmds.append(['shell', 'su', '0', 'chown', 'system:cache', device_ota_file]) | 
|  | 520 | cmds.append(['shell', 'su', '0', 'chmod', '0660', device_ota_file]) | 
|  | 521 | else: | 
|  | 522 | # Update via sending the payload over the network with an "adb reverse" | 
|  | 523 | # command. | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 524 | payload_url = 'http://127.0.0.1:%d/payload' % DEVICE_PORT | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 525 | if use_omaha and zipfile.is_zipfile(args.otafile): | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 526 | ota = AndroidOTAPackage(args.otafile, args.secondary) | 
| Sen Jiang | 3b15b59 | 2017-09-26 18:21:04 -0700 | [diff] [blame] | 527 | serving_range = (ota.offset, ota.size) | 
|  | 528 | else: | 
|  | 529 | serving_range = (0, os.stat(args.otafile).st_size) | 
|  | 530 | server_thread = StartServer(args.otafile, serving_range) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 531 | cmds.append( | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 532 | ['reverse', 'tcp:%d' % DEVICE_PORT, 'tcp:%d' % server_thread.port]) | 
|  | 533 | finalize_cmds.append(['reverse', '--remove', 'tcp:%d' % DEVICE_PORT]) | 
|  | 534 |  | 
|  | 535 | if args.public_key: | 
|  | 536 | payload_key_dir = os.path.dirname(PAYLOAD_KEY_PATH) | 
|  | 537 | cmds.append( | 
|  | 538 | ['shell', 'su', '0', 'mount', '-t', 'tmpfs', 'tmpfs', payload_key_dir]) | 
|  | 539 | # Allow adb push to payload_key_dir | 
|  | 540 | cmds.append(['shell', 'su', '0', 'chcon', 'u:object_r:shell_data_file:s0', | 
|  | 541 | payload_key_dir]) | 
|  | 542 | cmds.append(['push', args.public_key, PAYLOAD_KEY_PATH]) | 
|  | 543 | # Allow update_engine to read it. | 
|  | 544 | cmds.append(['shell', 'su', '0', 'chcon', '-R', 'u:object_r:system_file:s0', | 
|  | 545 | payload_key_dir]) | 
|  | 546 | finalize_cmds.append(['shell', 'su', '0', 'umount', payload_key_dir]) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 547 |  | 
|  | 548 | try: | 
|  | 549 | # The main update command using the configured payload_url. | 
| Sen Jiang | a1784b7 | 2017-08-09 17:42:36 -0700 | [diff] [blame] | 550 | if use_omaha: | 
|  | 551 | update_cmd = \ | 
|  | 552 | OmahaUpdateCommand('http://127.0.0.1:%d/update' % DEVICE_PORT) | 
|  | 553 | else: | 
| Tianjie Xu | 3f9be77 | 2019-11-02 18:31:50 -0700 | [diff] [blame] | 554 | update_cmd = AndroidUpdateCommand(args.otafile, args.secondary, | 
|  | 555 | payload_url, args.extra_headers) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 556 | cmds.append(['shell', 'su', '0'] + update_cmd) | 
|  | 557 |  | 
|  | 558 | for cmd in cmds: | 
|  | 559 | dut.adb(cmd) | 
|  | 560 | except KeyboardInterrupt: | 
|  | 561 | dut.adb(cancel_cmd) | 
|  | 562 | finally: | 
|  | 563 | if server_thread: | 
|  | 564 | server_thread.StopServer() | 
|  | 565 | for cmd in finalize_cmds: | 
| Kelvin Zhang | 3a18895 | 2021-04-13 12:44:45 -0400 | [diff] [blame] | 566 | dut.adb(cmd, 5) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 567 |  | 
| Nikita Ioffe | 3a327e6 | 2021-06-30 16:05:09 +0100 | [diff] [blame] | 568 | logging.info('Update took %.3f seconds', (time.perf_counter() - start_time)) | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 569 | return 0 | 
|  | 570 |  | 
| Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 571 |  | 
| Alex Deymo | 6751bbe | 2017-03-21 11:20:02 -0700 | [diff] [blame] | 572 | if __name__ == '__main__': | 
|  | 573 | sys.exit(main()) |