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 | """Utilities for update payload processing.""" |
| 6 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 9 | from error import PayloadError |
| 10 | import update_metadata_pb2 |
| 11 | |
| 12 | |
| 13 | # |
| 14 | # Constants. |
| 15 | # |
Alex Deymo | cf6f30d | 2015-06-11 13:51:46 -0700 | [diff] [blame] | 16 | PSEUDO_EXTENT_MARKER = (1L << 64) - 1 # UINT64_MAX |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 17 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 18 | SIG_ASN1_HEADER = ( |
| 19 | '\x30\x31\x30\x0d\x06\x09\x60\x86' |
| 20 | '\x48\x01\x65\x03\x04\x02\x01\x05' |
| 21 | '\x00\x04\x20' |
| 22 | ) |
| 23 | |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 24 | INPLACE_MINOR_PAYLOAD_VERSION = 1 |
| 25 | SOURCE_MINOR_PAYLOAD_VERSION = 2 |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 26 | |
| 27 | # |
| 28 | # Payload operation types. |
| 29 | # |
| 30 | class OpType(object): |
| 31 | """Container for operation type constants.""" |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 32 | _CLASS = update_metadata_pb2.InstallOperation |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 33 | # pylint: disable=E1101 |
| 34 | REPLACE = _CLASS.REPLACE |
| 35 | REPLACE_BZ = _CLASS.REPLACE_BZ |
| 36 | MOVE = _CLASS.MOVE |
| 37 | BSDIFF = _CLASS.BSDIFF |
Allie Wood | c11dc73 | 2015-02-18 15:53:05 -0800 | [diff] [blame] | 38 | SOURCE_COPY = _CLASS.SOURCE_COPY |
| 39 | SOURCE_BSDIFF = _CLASS.SOURCE_BSDIFF |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 40 | ZERO = _CLASS.ZERO |
| 41 | DISCARD = _CLASS.DISCARD |
| 42 | REPLACE_XZ = _CLASS.REPLACE_XZ |
| 43 | ALL = (REPLACE, REPLACE_BZ, MOVE, BSDIFF, SOURCE_COPY, SOURCE_BSDIFF, ZERO, |
| 44 | DISCARD, REPLACE_XZ) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 45 | NAMES = { |
| 46 | REPLACE: 'REPLACE', |
| 47 | REPLACE_BZ: 'REPLACE_BZ', |
| 48 | MOVE: 'MOVE', |
| 49 | BSDIFF: 'BSDIFF', |
Allie Wood | c11dc73 | 2015-02-18 15:53:05 -0800 | [diff] [blame] | 50 | SOURCE_COPY: 'SOURCE_COPY', |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 51 | SOURCE_BSDIFF: 'SOURCE_BSDIFF', |
| 52 | ZERO: 'ZERO', |
| 53 | DISCARD: 'DISCARD', |
| 54 | REPLACE_XZ: 'REPLACE_XZ' |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | def __init__(self): |
| 58 | pass |
| 59 | |
| 60 | |
| 61 | # |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 62 | # Checked and hashed reading of data. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 63 | # |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 64 | def IntPackingFmtStr(size, is_unsigned): |
| 65 | """Returns an integer format string for use by the struct module. |
| 66 | |
| 67 | Args: |
| 68 | size: the integer size in bytes (2, 4 or 8) |
| 69 | is_unsigned: whether it is signed or not |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 70 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 71 | Returns: |
| 72 | A format string for packing/unpacking integer values; assumes network byte |
| 73 | order (big-endian). |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 74 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 75 | Raises: |
| 76 | PayloadError if something is wrong with the arguments. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 77 | """ |
| 78 | # Determine the base conversion format. |
| 79 | if size == 2: |
| 80 | fmt = 'h' |
| 81 | elif size == 4: |
| 82 | fmt = 'i' |
| 83 | elif size == 8: |
| 84 | fmt = 'q' |
| 85 | else: |
| 86 | raise PayloadError('unsupport numeric field size (%s)' % size) |
| 87 | |
| 88 | # Signed or unsigned? |
| 89 | if is_unsigned: |
| 90 | fmt = fmt.upper() |
| 91 | |
| 92 | # Make it network byte order (big-endian). |
| 93 | fmt = '!' + fmt |
| 94 | |
| 95 | return fmt |
| 96 | |
| 97 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 98 | def Read(file_obj, length, offset=None, hasher=None): |
| 99 | """Reads binary data from a file. |
| 100 | |
| 101 | Args: |
| 102 | file_obj: an open file object |
| 103 | length: the length of the data to read |
| 104 | offset: an offset to seek to prior to reading; this is an absolute offset |
| 105 | from either the beginning (non-negative) or end (negative) of the |
| 106 | file. (optional) |
| 107 | hasher: a hashing object to pass the read data through (optional) |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 108 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 109 | Returns: |
| 110 | A string containing the read data. |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 111 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 112 | Raises: |
| 113 | PayloadError if a read error occurred or not enough data was read. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 114 | """ |
| 115 | if offset is not None: |
| 116 | if offset >= 0: |
| 117 | file_obj.seek(offset) |
| 118 | else: |
| 119 | file_obj.seek(offset, 2) |
| 120 | |
| 121 | try: |
| 122 | data = file_obj.read(length) |
| 123 | except IOError, e: |
| 124 | raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e)) |
| 125 | |
| 126 | if len(data) != length: |
| 127 | raise PayloadError( |
| 128 | 'reading from file (%s) too short (%d instead of %d bytes)' % |
| 129 | (file_obj.name, len(data), length)) |
| 130 | |
| 131 | if hasher: |
| 132 | hasher.update(data) |
| 133 | |
| 134 | return data |
| 135 | |
| 136 | |
| 137 | # |
| 138 | # Formatting functions. |
| 139 | # |
| 140 | def FormatExtent(ex, block_size=0): |
| 141 | end_block = ex.start_block + ex.num_blocks |
| 142 | if block_size: |
| 143 | return '%d->%d * %d' % (ex.start_block, end_block, block_size) |
| 144 | else: |
| 145 | return '%d->%d' % (ex.start_block, end_block) |
| 146 | |
| 147 | |
| 148 | def FormatSha256(digest): |
| 149 | """Returns a canonical string representation of a SHA256 digest.""" |
Gilad Arnold | a7aa0bc | 2013-11-12 08:18:08 -0800 | [diff] [blame] | 150 | return digest.encode('base64').strip() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 151 | |
| 152 | |
| 153 | # |
| 154 | # Useful iterators. |
| 155 | # |
| 156 | def _ObjNameIter(items, base_name, reverse=False, name_format_func=None): |
| 157 | """A generic (item, name) tuple iterators. |
| 158 | |
| 159 | Args: |
| 160 | items: the sequence of objects to iterate on |
| 161 | base_name: the base name for all objects |
| 162 | reverse: whether iteration should be in reverse order |
| 163 | name_format_func: a function to apply to the name string |
Allie Wood | 12f59aa | 2015-04-06 11:05:12 -0700 | [diff] [blame] | 164 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 165 | Yields: |
| 166 | An iterator whose i-th invocation returns (items[i], name), where name == |
| 167 | base_name + '[i]' (with a formatting function optionally applied to it). |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 168 | """ |
| 169 | idx, inc = (len(items), -1) if reverse else (1, 1) |
Gilad Arnold | e4beff7 | 2015-07-16 14:14:03 -0700 | [diff] [blame] | 170 | if reverse: |
| 171 | items = reversed(items) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 172 | for item in items: |
| 173 | item_name = '%s[%d]' % (base_name, idx) |
| 174 | if name_format_func: |
| 175 | item_name = name_format_func(item, item_name) |
| 176 | yield (item, item_name) |
| 177 | idx += inc |
| 178 | |
| 179 | |
| 180 | def _OperationNameFormatter(op, op_name): |
| 181 | return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?')) |
| 182 | |
| 183 | |
| 184 | def OperationIter(operations, base_name, reverse=False): |
| 185 | """An (item, name) iterator for update operations.""" |
| 186 | return _ObjNameIter(operations, base_name, reverse=reverse, |
| 187 | name_format_func=_OperationNameFormatter) |
| 188 | |
| 189 | |
| 190 | def ExtentIter(extents, base_name, reverse=False): |
| 191 | """An (item, name) iterator for operation extents.""" |
| 192 | return _ObjNameIter(extents, base_name, reverse=reverse) |
| 193 | |
| 194 | |
| 195 | def SignatureIter(sigs, base_name, reverse=False): |
| 196 | """An (item, name) iterator for signatures.""" |
| 197 | return _ObjNameIter(sigs, base_name, reverse=reverse) |