Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Applying a Chrome OS update payload. |
| 6 | |
| 7 | This module is used internally by the main Payload class for applying an update |
| 8 | payload. The interface for invoking the applier is as follows: |
| 9 | |
| 10 | applier = PayloadApplier(payload) |
| 11 | applier.Run(...) |
| 12 | |
| 13 | """ |
| 14 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 17 | import array |
| 18 | import bz2 |
| 19 | import hashlib |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 20 | import itertools |
Amin Hassani | f1d6cea | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 21 | try: |
| 22 | import lzma |
| 23 | except ImportError: |
| 24 | from backports import lzma |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 25 | import os |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
| 29 | import tempfile |
| 30 | |
| 31 | import common |
| 32 | from error import PayloadError |
| 33 | |
| 34 | |
| 35 | # |
| 36 | # Helper functions. |
| 37 | # |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 38 | def _VerifySha256(file_obj, expected_hash, name, length=-1): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 39 | """Verifies the SHA256 hash of a file. |
| 40 | |
| 41 | Args: |
| 42 | file_obj: file object to read |
| 43 | expected_hash: the hash digest we expect to be getting |
| 44 | name: name string of this hash, for error reporting |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 45 | length: precise length of data to verify (optional) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 46 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 47 | Raises: |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 48 | PayloadError if computed hash doesn't match expected one, or if fails to |
| 49 | read the specified length of data. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 50 | """ |
| 51 | # pylint: disable=E1101 |
| 52 | hasher = hashlib.sha256() |
| 53 | block_length = 1024 * 1024 |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 54 | max_length = length if length >= 0 else sys.maxint |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 55 | |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 56 | while max_length > 0: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 57 | read_length = min(max_length, block_length) |
| 58 | data = file_obj.read(read_length) |
| 59 | if not data: |
| 60 | break |
| 61 | max_length -= len(data) |
| 62 | hasher.update(data) |
| 63 | |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 64 | if length >= 0 and max_length > 0: |
| 65 | raise PayloadError( |
| 66 | 'insufficient data (%d instead of %d) when verifying %s' % |
| 67 | (length - max_length, length, name)) |
| 68 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 69 | actual_hash = hasher.digest() |
| 70 | if actual_hash != expected_hash: |
| 71 | raise PayloadError('%s hash (%s) not as expected (%s)' % |
Gilad Arnold | 9640537 | 2013-05-04 00:24:58 -0700 | [diff] [blame] | 72 | (name, common.FormatSha256(actual_hash), |
| 73 | common.FormatSha256(expected_hash))) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def _ReadExtents(file_obj, extents, block_size, max_length=-1): |
| 77 | """Reads data from file as defined by extent sequence. |
| 78 | |
| 79 | This tries to be efficient by not copying data as it is read in chunks. |
| 80 | |
| 81 | Args: |
| 82 | file_obj: file object |
| 83 | extents: sequence of block extents (offset and length) |
| 84 | block_size: size of each block |
| 85 | max_length: maximum length to read (optional) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 86 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 87 | Returns: |
| 88 | A character array containing the concatenated read data. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 89 | """ |
| 90 | data = array.array('c') |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 91 | if max_length < 0: |
| 92 | max_length = sys.maxint |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 93 | for ex in extents: |
| 94 | if max_length == 0: |
| 95 | break |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 96 | read_length = min(max_length, ex.num_blocks * block_size) |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 97 | |
| 98 | # Fill with zeros or read from file, depending on the type of extent. |
| 99 | if ex.start_block == common.PSEUDO_EXTENT_MARKER: |
| 100 | data.extend(itertools.repeat('\0', read_length)) |
| 101 | else: |
| 102 | file_obj.seek(ex.start_block * block_size) |
| 103 | data.fromfile(file_obj, read_length) |
| 104 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 105 | max_length -= read_length |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 106 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 107 | return data |
| 108 | |
| 109 | |
| 110 | def _WriteExtents(file_obj, data, extents, block_size, base_name): |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 111 | """Writes data to file as defined by extent sequence. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 112 | |
| 113 | This tries to be efficient by not copy data as it is written in chunks. |
| 114 | |
| 115 | Args: |
| 116 | file_obj: file object |
| 117 | data: data to write |
| 118 | extents: sequence of block extents (offset and length) |
| 119 | block_size: size of each block |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 120 | base_name: name string of extent sequence for error reporting |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 121 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 122 | Raises: |
| 123 | PayloadError when things don't add up. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 124 | """ |
| 125 | data_offset = 0 |
| 126 | data_length = len(data) |
| 127 | for ex, ex_name in common.ExtentIter(extents, base_name): |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 128 | if not data_length: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 129 | raise PayloadError('%s: more write extents than data' % ex_name) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 130 | write_length = min(data_length, ex.num_blocks * block_size) |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 131 | |
| 132 | # Only do actual writing if this is not a pseudo-extent. |
| 133 | if ex.start_block != common.PSEUDO_EXTENT_MARKER: |
| 134 | file_obj.seek(ex.start_block * block_size) |
| 135 | data_view = buffer(data, data_offset, write_length) |
| 136 | file_obj.write(data_view) |
| 137 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 138 | data_offset += write_length |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 139 | data_length -= write_length |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 140 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 141 | if data_length: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 142 | raise PayloadError('%s: more data than write extents' % base_name) |
| 143 | |
| 144 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 145 | def _ExtentsToBspatchArg(extents, block_size, base_name, data_length=-1): |
| 146 | """Translates an extent sequence into a bspatch-compatible string argument. |
| 147 | |
| 148 | Args: |
| 149 | extents: sequence of block extents (offset and length) |
| 150 | block_size: size of each block |
| 151 | base_name: name string of extent sequence for error reporting |
| 152 | data_length: the actual total length of the data in bytes (optional) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 153 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 154 | Returns: |
| 155 | A tuple consisting of (i) a string of the form |
| 156 | "off_1:len_1,...,off_n:len_n", (ii) an offset where zero padding is needed |
| 157 | for filling the last extent, (iii) the length of the padding (zero means no |
| 158 | padding is needed and the extents cover the full length of data). |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 159 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 160 | Raises: |
| 161 | PayloadError if data_length is too short or too long. |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 162 | """ |
| 163 | arg = '' |
| 164 | pad_off = pad_len = 0 |
| 165 | if data_length < 0: |
| 166 | data_length = sys.maxint |
| 167 | for ex, ex_name in common.ExtentIter(extents, base_name): |
| 168 | if not data_length: |
| 169 | raise PayloadError('%s: more extents than total data length' % ex_name) |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 170 | |
| 171 | is_pseudo = ex.start_block == common.PSEUDO_EXTENT_MARKER |
| 172 | start_byte = -1 if is_pseudo else ex.start_block * block_size |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 173 | num_bytes = ex.num_blocks * block_size |
| 174 | if data_length < num_bytes: |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 175 | # We're only padding a real extent. |
| 176 | if not is_pseudo: |
| 177 | pad_off = start_byte + data_length |
| 178 | pad_len = num_bytes - data_length |
| 179 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 180 | num_bytes = data_length |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 181 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 182 | arg += '%s%d:%d' % (arg and ',', start_byte, num_bytes) |
| 183 | data_length -= num_bytes |
| 184 | |
| 185 | if data_length: |
| 186 | raise PayloadError('%s: extents not covering full data length' % base_name) |
| 187 | |
| 188 | return arg, pad_off, pad_len |
| 189 | |
| 190 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 191 | # |
| 192 | # Payload application. |
| 193 | # |
| 194 | class PayloadApplier(object): |
| 195 | """Applying an update payload. |
| 196 | |
| 197 | This is a short-lived object whose purpose is to isolate the logic used for |
| 198 | applying an update payload. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 199 | """ |
| 200 | |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame] | 201 | def __init__(self, payload, bsdiff_in_place=True, bspatch_path=None, |
Amin Hassani | 5ef5d45 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 202 | puffpatch_path=None, truncate_to_expected_size=True): |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 203 | """Initialize the applier. |
| 204 | |
| 205 | Args: |
| 206 | payload: the payload object to check |
| 207 | bsdiff_in_place: whether to perform BSDIFF operation in-place (optional) |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame] | 208 | bspatch_path: path to the bspatch binary (optional) |
Amin Hassani | 5ef5d45 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 209 | puffpatch_path: path to the puffpatch binary (optional) |
Gilad Arnold | e5fdf18 | 2013-05-23 16:13:38 -0700 | [diff] [blame] | 210 | truncate_to_expected_size: whether to truncate the resulting partitions |
| 211 | to their expected sizes, as specified in the |
| 212 | payload (optional) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 213 | """ |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 214 | assert payload.is_init, 'uninitialized update payload' |
| 215 | self.payload = payload |
| 216 | self.block_size = payload.manifest.block_size |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 217 | self.minor_version = payload.manifest.minor_version |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 218 | self.bsdiff_in_place = bsdiff_in_place |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame] | 219 | self.bspatch_path = bspatch_path or 'bspatch' |
Amin Hassani | cdeb6e6 | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 220 | self.puffpatch_path = puffpatch_path or 'puffin' |
Gilad Arnold | e5fdf18 | 2013-05-23 16:13:38 -0700 | [diff] [blame] | 221 | self.truncate_to_expected_size = truncate_to_expected_size |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 222 | |
| 223 | def _ApplyReplaceOperation(self, op, op_name, out_data, part_file, part_size): |
Amin Hassani | f1d6cea | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 224 | """Applies a REPLACE{,_BZ,_XZ} operation. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 225 | |
| 226 | Args: |
| 227 | op: the operation object |
| 228 | op_name: name string for error reporting |
| 229 | out_data: the data to be written |
| 230 | part_file: the partition file object |
| 231 | part_size: the size of the partition |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 232 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 233 | Raises: |
| 234 | PayloadError if something goes wrong. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 235 | """ |
| 236 | block_size = self.block_size |
| 237 | data_length = len(out_data) |
| 238 | |
| 239 | # Decompress data if needed. |
| 240 | if op.type == common.OpType.REPLACE_BZ: |
| 241 | out_data = bz2.decompress(out_data) |
| 242 | data_length = len(out_data) |
Amin Hassani | f1d6cea | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 243 | elif op.type == common.OpType.REPLACE_XZ: |
| 244 | out_data = lzma.decompress(out_data) |
| 245 | data_length = len(out_data) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 246 | |
| 247 | # Write data to blocks specified in dst extents. |
| 248 | data_start = 0 |
| 249 | for ex, ex_name in common.ExtentIter(op.dst_extents, |
| 250 | '%s.dst_extents' % op_name): |
| 251 | start_block = ex.start_block |
| 252 | num_blocks = ex.num_blocks |
| 253 | count = num_blocks * block_size |
| 254 | |
| 255 | # Make sure it's not a fake (signature) operation. |
| 256 | if start_block != common.PSEUDO_EXTENT_MARKER: |
| 257 | data_end = data_start + count |
| 258 | |
| 259 | # Make sure we're not running past partition boundary. |
| 260 | if (start_block + num_blocks) * block_size > part_size: |
| 261 | raise PayloadError( |
| 262 | '%s: extent (%s) exceeds partition size (%d)' % |
| 263 | (ex_name, common.FormatExtent(ex, block_size), |
| 264 | part_size)) |
| 265 | |
| 266 | # Make sure that we have enough data to write. |
| 267 | if data_end >= data_length + block_size: |
| 268 | raise PayloadError( |
| 269 | '%s: more dst blocks than data (even with padding)') |
| 270 | |
| 271 | # Pad with zeros if necessary. |
| 272 | if data_end > data_length: |
| 273 | padding = data_end - data_length |
| 274 | out_data += '\0' * padding |
| 275 | |
| 276 | self.payload.payload_file.seek(start_block * block_size) |
| 277 | part_file.seek(start_block * block_size) |
| 278 | part_file.write(out_data[data_start:data_end]) |
| 279 | |
| 280 | data_start += count |
| 281 | |
| 282 | # Make sure we wrote all data. |
| 283 | if data_start < data_length: |
| 284 | raise PayloadError('%s: wrote fewer bytes (%d) than expected (%d)' % |
| 285 | (op_name, data_start, data_length)) |
| 286 | |
| 287 | def _ApplyMoveOperation(self, op, op_name, part_file): |
| 288 | """Applies a MOVE operation. |
| 289 | |
Gilad Arnold | 658185a | 2013-05-08 17:57:54 -0700 | [diff] [blame] | 290 | Note that this operation must read the whole block data from the input and |
| 291 | only then dump it, due to our in-place update semantics; otherwise, it |
| 292 | might clobber data midway through. |
| 293 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 294 | Args: |
| 295 | op: the operation object |
| 296 | op_name: name string for error reporting |
| 297 | part_file: the partition file object |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 298 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 299 | Raises: |
| 300 | PayloadError if something goes wrong. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 301 | """ |
| 302 | block_size = self.block_size |
| 303 | |
| 304 | # Gather input raw data from src extents. |
| 305 | in_data = _ReadExtents(part_file, op.src_extents, block_size) |
| 306 | |
| 307 | # Dump extracted data to dst extents. |
| 308 | _WriteExtents(part_file, in_data, op.dst_extents, block_size, |
| 309 | '%s.dst_extents' % op_name) |
| 310 | |
Amin Hassani | 8ad22ba | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 311 | def _ApplyZeroOperation(self, op, op_name, part_file): |
| 312 | """Applies a ZERO operation. |
| 313 | |
| 314 | Args: |
| 315 | op: the operation object |
| 316 | op_name: name string for error reporting |
| 317 | part_file: the partition file object |
| 318 | |
| 319 | Raises: |
| 320 | PayloadError if something goes wrong. |
| 321 | """ |
| 322 | block_size = self.block_size |
| 323 | base_name = '%s.dst_extents' % op_name |
| 324 | |
| 325 | # Iterate over the extents and write zero. |
| 326 | for ex, ex_name in common.ExtentIter(op.dst_extents, base_name): |
| 327 | # Only do actual writing if this is not a pseudo-extent. |
| 328 | if ex.start_block != common.PSEUDO_EXTENT_MARKER: |
| 329 | part_file.seek(ex.start_block * block_size) |
| 330 | part_file.write('\0' * (ex.num_blocks * block_size)) |
| 331 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 332 | def _ApplySourceCopyOperation(self, op, op_name, old_part_file, |
| 333 | new_part_file): |
| 334 | """Applies a SOURCE_COPY operation. |
| 335 | |
| 336 | Args: |
| 337 | op: the operation object |
| 338 | op_name: name string for error reporting |
| 339 | old_part_file: the old partition file object |
| 340 | new_part_file: the new partition file object |
| 341 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 342 | Raises: |
| 343 | PayloadError if something goes wrong. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 344 | """ |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 345 | if not old_part_file: |
| 346 | raise PayloadError( |
| 347 | '%s: no source partition file provided for operation type (%d)' % |
| 348 | (op_name, op.type)) |
| 349 | |
| 350 | block_size = self.block_size |
| 351 | |
| 352 | # Gather input raw data from src extents. |
| 353 | in_data = _ReadExtents(old_part_file, op.src_extents, block_size) |
| 354 | |
| 355 | # Dump extracted data to dst extents. |
| 356 | _WriteExtents(new_part_file, in_data, op.dst_extents, block_size, |
| 357 | '%s.dst_extents' % op_name) |
| 358 | |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 359 | def _BytesInExtents(self, extents, base_name): |
| 360 | """Counts the length of extents in bytes. |
| 361 | |
| 362 | Args: |
| 363 | extents: The list of Extents. |
| 364 | base_name: For error reporting. |
| 365 | |
| 366 | Returns: |
| 367 | The number of bytes in extents. |
| 368 | """ |
| 369 | |
| 370 | length = 0 |
| 371 | for ex, ex_name in common.ExtentIter(extents, base_name): |
| 372 | length += ex.num_blocks * self.block_size |
| 373 | return length |
| 374 | |
Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 375 | def _ApplyDiffOperation(self, op, op_name, patch_data, old_part_file, |
| 376 | new_part_file): |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 377 | """Applies a SOURCE_BSDIFF, BROTLI_BSDIFF or PUFFDIFF operation. |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 378 | |
| 379 | Args: |
| 380 | op: the operation object |
| 381 | op_name: name string for error reporting |
| 382 | patch_data: the binary patch content |
| 383 | old_part_file: the source partition file object |
| 384 | new_part_file: the target partition file object |
| 385 | |
| 386 | Raises: |
| 387 | PayloadError if something goes wrong. |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 388 | """ |
| 389 | if not old_part_file: |
| 390 | raise PayloadError( |
| 391 | '%s: no source partition file provided for operation type (%d)' % |
| 392 | (op_name, op.type)) |
| 393 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 394 | block_size = self.block_size |
| 395 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 396 | # Dump patch data to file. |
| 397 | with tempfile.NamedTemporaryFile(delete=False) as patch_file: |
| 398 | patch_file_name = patch_file.name |
| 399 | patch_file.write(patch_data) |
| 400 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 401 | if (hasattr(new_part_file, 'fileno') and |
Amin Hassani | cdeb6e6 | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 402 | ((not old_part_file) or hasattr(old_part_file, 'fileno'))): |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 403 | # Construct input and output extents argument for bspatch. |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 404 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 405 | in_extents_arg, _, _ = _ExtentsToBspatchArg( |
| 406 | op.src_extents, block_size, '%s.src_extents' % op_name, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 407 | data_length=op.src_length if op.src_length else |
| 408 | self._BytesInExtents(op.src_extents, "%s.src_extents")) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 409 | out_extents_arg, pad_off, pad_len = _ExtentsToBspatchArg( |
| 410 | op.dst_extents, block_size, '%s.dst_extents' % op_name, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 411 | data_length=op.dst_length if op.dst_length else |
| 412 | self._BytesInExtents(op.dst_extents, "%s.dst_extents")) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 413 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 414 | new_file_name = '/dev/fd/%d' % new_part_file.fileno() |
| 415 | # Diff from source partition. |
| 416 | old_file_name = '/dev/fd/%d' % old_part_file.fileno() |
| 417 | |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 418 | if op.type in (common.OpType.BSDIFF, common.OpType.SOURCE_BSDIFF, |
| 419 | common.OpType.BROTLI_BSDIFF): |
Amin Hassani | cdeb6e6 | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 420 | # Invoke bspatch on partition file with extents args. |
| 421 | bspatch_cmd = [self.bspatch_path, old_file_name, new_file_name, |
| 422 | patch_file_name, in_extents_arg, out_extents_arg] |
| 423 | subprocess.check_call(bspatch_cmd) |
| 424 | elif op.type == common.OpType.PUFFDIFF: |
| 425 | # Invoke puffpatch on partition file with extents args. |
| 426 | puffpatch_cmd = [self.puffpatch_path, |
| 427 | "--operation=puffpatch", |
| 428 | "--src_file=%s" % old_file_name, |
| 429 | "--dst_file=%s" % new_file_name, |
| 430 | "--patch_file=%s" % patch_file_name, |
| 431 | "--src_extents=%s" % in_extents_arg, |
| 432 | "--dst_extents=%s" % out_extents_arg] |
| 433 | subprocess.check_call(puffpatch_cmd) |
| 434 | else: |
| 435 | raise PayloadError("Unknown operation %s", op.type) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 436 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 437 | # Pad with zeros past the total output length. |
| 438 | if pad_len: |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 439 | new_part_file.seek(pad_off) |
| 440 | new_part_file.write('\0' * pad_len) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 441 | else: |
| 442 | # Gather input raw data and write to a temp file. |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 443 | input_part_file = old_part_file if old_part_file else new_part_file |
| 444 | in_data = _ReadExtents(input_part_file, op.src_extents, block_size, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 445 | max_length=op.src_length if op.src_length else |
| 446 | self._BytesInExtents(op.src_extents, |
| 447 | "%s.src_extents")) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 448 | with tempfile.NamedTemporaryFile(delete=False) as in_file: |
| 449 | in_file_name = in_file.name |
| 450 | in_file.write(in_data) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 451 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 452 | # Allocate temporary output file. |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 453 | with tempfile.NamedTemporaryFile(delete=False) as out_file: |
| 454 | out_file_name = out_file.name |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 455 | |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 456 | if op.type in (common.OpType.BSDIFF, common.OpType.SOURCE_BSDIFF, |
| 457 | common.OpType.BROTLI_BSDIFF): |
Amin Hassani | cdeb6e6 | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 458 | # Invoke bspatch. |
| 459 | bspatch_cmd = [self.bspatch_path, in_file_name, out_file_name, |
| 460 | patch_file_name] |
| 461 | subprocess.check_call(bspatch_cmd) |
| 462 | elif op.type == common.OpType.PUFFDIFF: |
| 463 | # Invoke puffpatch. |
| 464 | puffpatch_cmd = [self.puffpatch_path, |
| 465 | "--operation=puffpatch", |
| 466 | "--src_file=%s" % in_file_name, |
| 467 | "--dst_file=%s" % out_file_name, |
| 468 | "--patch_file=%s" % patch_file_name] |
| 469 | subprocess.check_call(puffpatch_cmd) |
| 470 | else: |
| 471 | raise PayloadError("Unknown operation %s", op.type) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 472 | |
| 473 | # Read output. |
| 474 | with open(out_file_name, 'rb') as out_file: |
| 475 | out_data = out_file.read() |
| 476 | if len(out_data) != op.dst_length: |
| 477 | raise PayloadError( |
| 478 | '%s: actual patched data length (%d) not as expected (%d)' % |
| 479 | (op_name, len(out_data), op.dst_length)) |
| 480 | |
| 481 | # Write output back to partition, with padding. |
| 482 | unaligned_out_len = len(out_data) % block_size |
| 483 | if unaligned_out_len: |
| 484 | out_data += '\0' * (block_size - unaligned_out_len) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 485 | _WriteExtents(new_part_file, out_data, op.dst_extents, block_size, |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 486 | '%s.dst_extents' % op_name) |
| 487 | |
| 488 | # Delete input/output files. |
| 489 | os.remove(in_file_name) |
| 490 | os.remove(out_file_name) |
| 491 | |
| 492 | # Delete patch file. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 493 | os.remove(patch_file_name) |
| 494 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 495 | def _ApplyOperations(self, operations, base_name, old_part_file, |
| 496 | new_part_file, part_size): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 497 | """Applies a sequence of update operations to a partition. |
| 498 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 499 | This assumes an in-place update semantics for MOVE and BSDIFF, namely all |
| 500 | reads are performed first, then the data is processed and written back to |
| 501 | the same file. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 502 | |
| 503 | Args: |
| 504 | operations: the sequence of operations |
| 505 | base_name: the name of the operation sequence |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 506 | old_part_file: the old partition file object, open for reading/writing |
| 507 | new_part_file: the new partition file object, open for reading/writing |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 508 | part_size: the partition size |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 509 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 510 | Raises: |
| 511 | PayloadError if anything goes wrong while processing the payload. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 512 | """ |
| 513 | for op, op_name in common.OperationIter(operations, base_name): |
| 514 | # Read data blob. |
| 515 | data = self.payload.ReadDataBlob(op.data_offset, op.data_length) |
| 516 | |
Amin Hassani | f1d6cea | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 517 | if op.type in (common.OpType.REPLACE, common.OpType.REPLACE_BZ, |
| 518 | common.OpType.REPLACE_XZ): |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 519 | self._ApplyReplaceOperation(op, op_name, data, new_part_file, part_size) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 520 | elif op.type == common.OpType.MOVE: |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 521 | self._ApplyMoveOperation(op, op_name, new_part_file) |
Amin Hassani | 8ad22ba | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 522 | elif op.type == common.OpType.ZERO: |
| 523 | self._ApplyZeroOperation(op, op_name, new_part_file) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 524 | elif op.type == common.OpType.BSDIFF: |
Amin Hassani | cdeb6e6 | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 525 | self._ApplyDiffOperation(op, op_name, data, new_part_file, |
| 526 | new_part_file) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 527 | elif op.type == common.OpType.SOURCE_COPY: |
| 528 | self._ApplySourceCopyOperation(op, op_name, old_part_file, |
| 529 | new_part_file) |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 530 | elif op.type in (common.OpType.SOURCE_BSDIFF, common.OpType.PUFFDIFF, |
| 531 | common.OpType.BROTLI_BSDIFF): |
Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 532 | self._ApplyDiffOperation(op, op_name, data, old_part_file, |
| 533 | new_part_file) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 534 | else: |
| 535 | raise PayloadError('%s: unknown operation type (%d)' % |
| 536 | (op_name, op.type)) |
| 537 | |
| 538 | def _ApplyToPartition(self, operations, part_name, base_name, |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 539 | new_part_file_name, new_part_info, |
| 540 | old_part_file_name=None, old_part_info=None): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 541 | """Applies an update to a partition. |
| 542 | |
| 543 | Args: |
| 544 | operations: the sequence of update operations to apply |
| 545 | part_name: the name of the partition, for error reporting |
| 546 | base_name: the name of the operation sequence |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 547 | new_part_file_name: file name to write partition data to |
| 548 | new_part_info: size and expected hash of dest partition |
| 549 | old_part_file_name: file name of source partition (optional) |
| 550 | old_part_info: size and expected hash of source partition (optional) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 551 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 552 | Raises: |
| 553 | PayloadError if anything goes wrong with the update. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 554 | """ |
| 555 | # Do we have a source partition? |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 556 | if old_part_file_name: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 557 | # Verify the source partition. |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 558 | with open(old_part_file_name, 'rb') as old_part_file: |
Gilad Arnold | 4b8f4c2 | 2015-07-16 11:45:39 -0700 | [diff] [blame] | 559 | _VerifySha256(old_part_file, old_part_info.hash, |
| 560 | 'old ' + part_name, length=old_part_info.size) |
Gilad Arnold | f69065c | 2013-05-27 16:54:59 -0700 | [diff] [blame] | 561 | new_part_file_mode = 'r+b' |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 562 | if self.minor_version == common.INPLACE_MINOR_PAYLOAD_VERSION: |
| 563 | # Copy the src partition to the dst one; make sure we don't truncate it. |
| 564 | shutil.copyfile(old_part_file_name, new_part_file_name) |
Sen Jiang | d6122bb | 2015-12-11 10:27:04 -0800 | [diff] [blame] | 565 | elif (self.minor_version == common.SOURCE_MINOR_PAYLOAD_VERSION or |
Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 566 | self.minor_version == common.OPSRCHASH_MINOR_PAYLOAD_VERSION or |
Amin Hassani | 5ef5d45 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 567 | self.minor_version == common.PUFFDIFF_MINOR_PAYLOAD_VERSION): |
Sen Jiang | d6122bb | 2015-12-11 10:27:04 -0800 | [diff] [blame] | 568 | # In minor version >= 2, we don't want to copy the partitions, so |
| 569 | # instead just make the new partition file. |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 570 | open(new_part_file_name, 'w').close() |
| 571 | else: |
| 572 | raise PayloadError("Unknown minor version: %d" % self.minor_version) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 573 | else: |
Gilad Arnold | f69065c | 2013-05-27 16:54:59 -0700 | [diff] [blame] | 574 | # We need to create/truncate the dst partition file. |
| 575 | new_part_file_mode = 'w+b' |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 576 | |
| 577 | # Apply operations. |
Gilad Arnold | f69065c | 2013-05-27 16:54:59 -0700 | [diff] [blame] | 578 | with open(new_part_file_name, new_part_file_mode) as new_part_file: |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 579 | old_part_file = (open(old_part_file_name, 'r+b') |
| 580 | if old_part_file_name else None) |
| 581 | try: |
| 582 | self._ApplyOperations(operations, base_name, old_part_file, |
| 583 | new_part_file, new_part_info.size) |
| 584 | finally: |
| 585 | if old_part_file: |
| 586 | old_part_file.close() |
| 587 | |
Gilad Arnold | e5fdf18 | 2013-05-23 16:13:38 -0700 | [diff] [blame] | 588 | # Truncate the result, if so instructed. |
| 589 | if self.truncate_to_expected_size: |
| 590 | new_part_file.seek(0, 2) |
| 591 | if new_part_file.tell() > new_part_info.size: |
| 592 | new_part_file.seek(new_part_info.size) |
| 593 | new_part_file.truncate() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 594 | |
| 595 | # Verify the resulting partition. |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 596 | with open(new_part_file_name, 'rb') as new_part_file: |
Gilad Arnold | 4b8f4c2 | 2015-07-16 11:45:39 -0700 | [diff] [blame] | 597 | _VerifySha256(new_part_file, new_part_info.hash, |
| 598 | 'new ' + part_name, length=new_part_info.size) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 599 | |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 600 | def Run(self, new_kernel_part, new_rootfs_part, old_kernel_part=None, |
| 601 | old_rootfs_part=None): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 602 | """Applier entry point, invoking all update operations. |
| 603 | |
| 604 | Args: |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 605 | new_kernel_part: name of dest kernel partition file |
| 606 | new_rootfs_part: name of dest rootfs partition file |
| 607 | old_kernel_part: name of source kernel partition file (optional) |
| 608 | old_rootfs_part: name of source rootfs partition file (optional) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 609 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 610 | Raises: |
| 611 | PayloadError if payload application failed. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 612 | """ |
| 613 | self.payload.ResetFile() |
| 614 | |
| 615 | # Make sure the arguments are sane and match the payload. |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 616 | if not (new_kernel_part and new_rootfs_part): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 617 | raise PayloadError('missing dst {kernel,rootfs} partitions') |
| 618 | |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 619 | if not (old_kernel_part or old_rootfs_part): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 620 | if not self.payload.IsFull(): |
| 621 | raise PayloadError('trying to apply a non-full update without src ' |
| 622 | '{kernel,rootfs} partitions') |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 623 | elif old_kernel_part and old_rootfs_part: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 624 | if not self.payload.IsDelta(): |
| 625 | raise PayloadError('trying to apply a non-delta update onto src ' |
| 626 | '{kernel,rootfs} partitions') |
| 627 | else: |
| 628 | raise PayloadError('not all src partitions provided') |
| 629 | |
| 630 | # Apply update to rootfs. |
| 631 | self._ApplyToPartition( |
| 632 | self.payload.manifest.install_operations, 'rootfs', |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 633 | 'install_operations', new_rootfs_part, |
| 634 | self.payload.manifest.new_rootfs_info, old_rootfs_part, |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 635 | self.payload.manifest.old_rootfs_info) |
| 636 | |
| 637 | # Apply update to kernel update. |
| 638 | self._ApplyToPartition( |
| 639 | self.payload.manifest.kernel_install_operations, 'kernel', |
Gilad Arnold | 1641660 | 2013-05-04 21:40:39 -0700 | [diff] [blame] | 640 | 'kernel_install_operations', new_kernel_part, |
| 641 | self.payload.manifest.new_kernel_info, old_kernel_part, |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 642 | self.payload.manifest.old_kernel_info) |