blob: 231c5041141b006c8a8e3cc2939f66d8143d4af0 [file] [log] [blame]
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001# 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 Wood12f59aa2015-04-06 11:05:12 -07007from __future__ import print_function
8
Amin Hassanib05a65a2017-12-18 15:15:32 -08009from update_payload import update_metadata_pb2
10from update_payload.error import PayloadError
Gilad Arnold553b0ec2013-01-26 01:00:39 -080011
12
13#
14# Constants.
15#
Alex Deymocf6f30d2015-06-11 13:51:46 -070016PSEUDO_EXTENT_MARKER = (1L << 64) - 1 # UINT64_MAX
Gilad Arnold553b0ec2013-01-26 01:00:39 -080017
Gilad Arnold5502b562013-03-08 13:22:31 -080018SIG_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
Alex Deymoef497352015-10-15 09:14:58 -070024CHROMEOS_MAJOR_PAYLOAD_VERSION = 1
25BRILLO_MAJOR_PAYLOAD_VERSION = 2
26
Allie Wood12f59aa2015-04-06 11:05:12 -070027INPLACE_MINOR_PAYLOAD_VERSION = 1
28SOURCE_MINOR_PAYLOAD_VERSION = 2
Sen Jiangd6122bb2015-12-11 10:27:04 -080029OPSRCHASH_MINOR_PAYLOAD_VERSION = 3
Amin Hassani5ef5d452017-08-04 13:10:59 -070030PUFFDIFF_MINOR_PAYLOAD_VERSION = 4
Gilad Arnold553b0ec2013-01-26 01:00:39 -080031
32#
33# Payload operation types.
34#
35class OpType(object):
36 """Container for operation type constants."""
Alex Deymo28466772015-09-11 17:16:44 -070037 _CLASS = update_metadata_pb2.InstallOperation
Gilad Arnold553b0ec2013-01-26 01:00:39 -080038 REPLACE = _CLASS.REPLACE
39 REPLACE_BZ = _CLASS.REPLACE_BZ
40 MOVE = _CLASS.MOVE
41 BSDIFF = _CLASS.BSDIFF
Allie Woodc11dc732015-02-18 15:53:05 -080042 SOURCE_COPY = _CLASS.SOURCE_COPY
43 SOURCE_BSDIFF = _CLASS.SOURCE_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070044 ZERO = _CLASS.ZERO
45 DISCARD = _CLASS.DISCARD
46 REPLACE_XZ = _CLASS.REPLACE_XZ
Amin Hassani5ef5d452017-08-04 13:10:59 -070047 PUFFDIFF = _CLASS.PUFFDIFF
Amin Hassaniefa62d92017-11-09 13:46:56 -080048 BROTLI_BSDIFF = _CLASS.BROTLI_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070049 ALL = (REPLACE, REPLACE_BZ, MOVE, BSDIFF, SOURCE_COPY, SOURCE_BSDIFF, ZERO,
Amin Hassaniefa62d92017-11-09 13:46:56 -080050 DISCARD, REPLACE_XZ, PUFFDIFF, BROTLI_BSDIFF)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080051 NAMES = {
52 REPLACE: 'REPLACE',
53 REPLACE_BZ: 'REPLACE_BZ',
54 MOVE: 'MOVE',
55 BSDIFF: 'BSDIFF',
Allie Woodc11dc732015-02-18 15:53:05 -080056 SOURCE_COPY: 'SOURCE_COPY',
Alex Deymo28466772015-09-11 17:16:44 -070057 SOURCE_BSDIFF: 'SOURCE_BSDIFF',
58 ZERO: 'ZERO',
59 DISCARD: 'DISCARD',
Sen Jiangc2538fa2016-02-24 14:15:02 -080060 REPLACE_XZ: 'REPLACE_XZ',
Amin Hassani5ef5d452017-08-04 13:10:59 -070061 PUFFDIFF: 'PUFFDIFF',
Amin Hassaniefa62d92017-11-09 13:46:56 -080062 BROTLI_BSDIFF: 'BROTLI_BSDIFF',
Gilad Arnold553b0ec2013-01-26 01:00:39 -080063 }
64
65 def __init__(self):
66 pass
67
68
69#
Gilad Arnold382df5c2013-05-03 12:49:28 -070070# Checked and hashed reading of data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080071#
Gilad Arnold5502b562013-03-08 13:22:31 -080072def IntPackingFmtStr(size, is_unsigned):
73 """Returns an integer format string for use by the struct module.
74
75 Args:
76 size: the integer size in bytes (2, 4 or 8)
77 is_unsigned: whether it is signed or not
Allie Wood12f59aa2015-04-06 11:05:12 -070078
Gilad Arnold5502b562013-03-08 13:22:31 -080079 Returns:
80 A format string for packing/unpacking integer values; assumes network byte
81 order (big-endian).
Allie Wood12f59aa2015-04-06 11:05:12 -070082
Gilad Arnold5502b562013-03-08 13:22:31 -080083 Raises:
84 PayloadError if something is wrong with the arguments.
Gilad Arnold5502b562013-03-08 13:22:31 -080085 """
86 # Determine the base conversion format.
87 if size == 2:
88 fmt = 'h'
89 elif size == 4:
90 fmt = 'i'
91 elif size == 8:
92 fmt = 'q'
93 else:
94 raise PayloadError('unsupport numeric field size (%s)' % size)
95
96 # Signed or unsigned?
97 if is_unsigned:
98 fmt = fmt.upper()
99
100 # Make it network byte order (big-endian).
101 fmt = '!' + fmt
102
103 return fmt
104
105
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800106def Read(file_obj, length, offset=None, hasher=None):
107 """Reads binary data from a file.
108
109 Args:
110 file_obj: an open file object
111 length: the length of the data to read
112 offset: an offset to seek to prior to reading; this is an absolute offset
113 from either the beginning (non-negative) or end (negative) of the
114 file. (optional)
115 hasher: a hashing object to pass the read data through (optional)
Allie Wood12f59aa2015-04-06 11:05:12 -0700116
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800117 Returns:
118 A string containing the read data.
Allie Wood12f59aa2015-04-06 11:05:12 -0700119
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800120 Raises:
121 PayloadError if a read error occurred or not enough data was read.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800122 """
123 if offset is not None:
124 if offset >= 0:
125 file_obj.seek(offset)
126 else:
127 file_obj.seek(offset, 2)
128
129 try:
130 data = file_obj.read(length)
131 except IOError, e:
132 raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e))
133
134 if len(data) != length:
135 raise PayloadError(
136 'reading from file (%s) too short (%d instead of %d bytes)' %
137 (file_obj.name, len(data), length))
138
139 if hasher:
140 hasher.update(data)
141
142 return data
143
144
145#
146# Formatting functions.
147#
148def FormatExtent(ex, block_size=0):
149 end_block = ex.start_block + ex.num_blocks
150 if block_size:
151 return '%d->%d * %d' % (ex.start_block, end_block, block_size)
152 else:
153 return '%d->%d' % (ex.start_block, end_block)
154
155
156def FormatSha256(digest):
157 """Returns a canonical string representation of a SHA256 digest."""
Gilad Arnolda7aa0bc2013-11-12 08:18:08 -0800158 return digest.encode('base64').strip()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800159
160
161#
162# Useful iterators.
163#
164def _ObjNameIter(items, base_name, reverse=False, name_format_func=None):
165 """A generic (item, name) tuple iterators.
166
167 Args:
168 items: the sequence of objects to iterate on
169 base_name: the base name for all objects
170 reverse: whether iteration should be in reverse order
171 name_format_func: a function to apply to the name string
Allie Wood12f59aa2015-04-06 11:05:12 -0700172
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800173 Yields:
174 An iterator whose i-th invocation returns (items[i], name), where name ==
175 base_name + '[i]' (with a formatting function optionally applied to it).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800176 """
177 idx, inc = (len(items), -1) if reverse else (1, 1)
Gilad Arnolde4beff72015-07-16 14:14:03 -0700178 if reverse:
179 items = reversed(items)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800180 for item in items:
181 item_name = '%s[%d]' % (base_name, idx)
182 if name_format_func:
183 item_name = name_format_func(item, item_name)
184 yield (item, item_name)
185 idx += inc
186
187
188def _OperationNameFormatter(op, op_name):
189 return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?'))
190
191
192def OperationIter(operations, base_name, reverse=False):
193 """An (item, name) iterator for update operations."""
194 return _ObjNameIter(operations, base_name, reverse=reverse,
195 name_format_func=_OperationNameFormatter)
196
197
198def ExtentIter(extents, base_name, reverse=False):
199 """An (item, name) iterator for operation extents."""
200 return _ObjNameIter(extents, base_name, reverse=reverse)
201
202
203def SignatureIter(sigs, base_name, reverse=False):
204 """An (item, name) iterator for signatures."""
205 return _ObjNameIter(sigs, base_name, reverse=reverse)