blob: 88cd6edcbeac24ed3a4ca8d7a1f3fc902d1eb3b6 [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
Gilad Arnold553b0ec2013-01-26 01:00:39 -08009from error import PayloadError
10import update_metadata_pb2
11
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
Gilad Arnold553b0ec2013-01-26 01:00:39 -080030
31#
32# Payload operation types.
33#
34class OpType(object):
35 """Container for operation type constants."""
Alex Deymo28466772015-09-11 17:16:44 -070036 _CLASS = update_metadata_pb2.InstallOperation
Gilad Arnold553b0ec2013-01-26 01:00:39 -080037 # pylint: disable=E1101
38 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
Sen Jiangc2538fa2016-02-24 14:15:02 -080047 IMGDIFF = _CLASS.IMGDIFF
Alex Deymo28466772015-09-11 17:16:44 -070048 ALL = (REPLACE, REPLACE_BZ, MOVE, BSDIFF, SOURCE_COPY, SOURCE_BSDIFF, ZERO,
Sen Jiangc2538fa2016-02-24 14:15:02 -080049 DISCARD, REPLACE_XZ, IMGDIFF)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080050 NAMES = {
51 REPLACE: 'REPLACE',
52 REPLACE_BZ: 'REPLACE_BZ',
53 MOVE: 'MOVE',
54 BSDIFF: 'BSDIFF',
Allie Woodc11dc732015-02-18 15:53:05 -080055 SOURCE_COPY: 'SOURCE_COPY',
Alex Deymo28466772015-09-11 17:16:44 -070056 SOURCE_BSDIFF: 'SOURCE_BSDIFF',
57 ZERO: 'ZERO',
58 DISCARD: 'DISCARD',
Sen Jiangc2538fa2016-02-24 14:15:02 -080059 REPLACE_XZ: 'REPLACE_XZ',
60 IMGDIFF: 'IMGDIFF',
Gilad Arnold553b0ec2013-01-26 01:00:39 -080061 }
62
63 def __init__(self):
64 pass
65
66
67#
Gilad Arnold382df5c2013-05-03 12:49:28 -070068# Checked and hashed reading of data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080069#
Gilad Arnold5502b562013-03-08 13:22:31 -080070def IntPackingFmtStr(size, is_unsigned):
71 """Returns an integer format string for use by the struct module.
72
73 Args:
74 size: the integer size in bytes (2, 4 or 8)
75 is_unsigned: whether it is signed or not
Allie Wood12f59aa2015-04-06 11:05:12 -070076
Gilad Arnold5502b562013-03-08 13:22:31 -080077 Returns:
78 A format string for packing/unpacking integer values; assumes network byte
79 order (big-endian).
Allie Wood12f59aa2015-04-06 11:05:12 -070080
Gilad Arnold5502b562013-03-08 13:22:31 -080081 Raises:
82 PayloadError if something is wrong with the arguments.
Gilad Arnold5502b562013-03-08 13:22:31 -080083 """
84 # Determine the base conversion format.
85 if size == 2:
86 fmt = 'h'
87 elif size == 4:
88 fmt = 'i'
89 elif size == 8:
90 fmt = 'q'
91 else:
92 raise PayloadError('unsupport numeric field size (%s)' % size)
93
94 # Signed or unsigned?
95 if is_unsigned:
96 fmt = fmt.upper()
97
98 # Make it network byte order (big-endian).
99 fmt = '!' + fmt
100
101 return fmt
102
103
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800104def Read(file_obj, length, offset=None, hasher=None):
105 """Reads binary data from a file.
106
107 Args:
108 file_obj: an open file object
109 length: the length of the data to read
110 offset: an offset to seek to prior to reading; this is an absolute offset
111 from either the beginning (non-negative) or end (negative) of the
112 file. (optional)
113 hasher: a hashing object to pass the read data through (optional)
Allie Wood12f59aa2015-04-06 11:05:12 -0700114
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800115 Returns:
116 A string containing the read data.
Allie Wood12f59aa2015-04-06 11:05:12 -0700117
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800118 Raises:
119 PayloadError if a read error occurred or not enough data was read.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800120 """
121 if offset is not None:
122 if offset >= 0:
123 file_obj.seek(offset)
124 else:
125 file_obj.seek(offset, 2)
126
127 try:
128 data = file_obj.read(length)
129 except IOError, e:
130 raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e))
131
132 if len(data) != length:
133 raise PayloadError(
134 'reading from file (%s) too short (%d instead of %d bytes)' %
135 (file_obj.name, len(data), length))
136
137 if hasher:
138 hasher.update(data)
139
140 return data
141
142
143#
144# Formatting functions.
145#
146def FormatExtent(ex, block_size=0):
147 end_block = ex.start_block + ex.num_blocks
148 if block_size:
149 return '%d->%d * %d' % (ex.start_block, end_block, block_size)
150 else:
151 return '%d->%d' % (ex.start_block, end_block)
152
153
154def FormatSha256(digest):
155 """Returns a canonical string representation of a SHA256 digest."""
Gilad Arnolda7aa0bc2013-11-12 08:18:08 -0800156 return digest.encode('base64').strip()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800157
158
159#
160# Useful iterators.
161#
162def _ObjNameIter(items, base_name, reverse=False, name_format_func=None):
163 """A generic (item, name) tuple iterators.
164
165 Args:
166 items: the sequence of objects to iterate on
167 base_name: the base name for all objects
168 reverse: whether iteration should be in reverse order
169 name_format_func: a function to apply to the name string
Allie Wood12f59aa2015-04-06 11:05:12 -0700170
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800171 Yields:
172 An iterator whose i-th invocation returns (items[i], name), where name ==
173 base_name + '[i]' (with a formatting function optionally applied to it).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800174 """
175 idx, inc = (len(items), -1) if reverse else (1, 1)
Gilad Arnolde4beff72015-07-16 14:14:03 -0700176 if reverse:
177 items = reversed(items)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800178 for item in items:
179 item_name = '%s[%d]' % (base_name, idx)
180 if name_format_func:
181 item_name = name_format_func(item, item_name)
182 yield (item, item_name)
183 idx += inc
184
185
186def _OperationNameFormatter(op, op_name):
187 return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?'))
188
189
190def OperationIter(operations, base_name, reverse=False):
191 """An (item, name) iterator for update operations."""
192 return _ObjNameIter(operations, base_name, reverse=reverse,
193 name_format_func=_OperationNameFormatter)
194
195
196def ExtentIter(extents, base_name, reverse=False):
197 """An (item, name) iterator for operation extents."""
198 return _ObjNameIter(extents, base_name, reverse=reverse)
199
200
201def SignatureIter(sigs, base_name, reverse=False):
202 """An (item, name) iterator for signatures."""
203 return _ObjNameIter(sigs, base_name, reverse=reverse)