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