blob: 38d949b47c7cea5c53b9850c90792387b0741f79 [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
Gilad Arnold553b0ec2013-01-26 01:00:39 -080029
30#
31# Payload operation types.
32#
33class OpType(object):
34 """Container for operation type constants."""
Alex Deymo28466772015-09-11 17:16:44 -070035 _CLASS = update_metadata_pb2.InstallOperation
Gilad Arnold553b0ec2013-01-26 01:00:39 -080036 # pylint: disable=E1101
37 REPLACE = _CLASS.REPLACE
38 REPLACE_BZ = _CLASS.REPLACE_BZ
39 MOVE = _CLASS.MOVE
40 BSDIFF = _CLASS.BSDIFF
Allie Woodc11dc732015-02-18 15:53:05 -080041 SOURCE_COPY = _CLASS.SOURCE_COPY
42 SOURCE_BSDIFF = _CLASS.SOURCE_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070043 ZERO = _CLASS.ZERO
44 DISCARD = _CLASS.DISCARD
45 REPLACE_XZ = _CLASS.REPLACE_XZ
46 ALL = (REPLACE, REPLACE_BZ, MOVE, BSDIFF, SOURCE_COPY, SOURCE_BSDIFF, ZERO,
47 DISCARD, REPLACE_XZ)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080048 NAMES = {
49 REPLACE: 'REPLACE',
50 REPLACE_BZ: 'REPLACE_BZ',
51 MOVE: 'MOVE',
52 BSDIFF: 'BSDIFF',
Allie Woodc11dc732015-02-18 15:53:05 -080053 SOURCE_COPY: 'SOURCE_COPY',
Alex Deymo28466772015-09-11 17:16:44 -070054 SOURCE_BSDIFF: 'SOURCE_BSDIFF',
55 ZERO: 'ZERO',
56 DISCARD: 'DISCARD',
57 REPLACE_XZ: 'REPLACE_XZ'
Gilad Arnold553b0ec2013-01-26 01:00:39 -080058 }
59
60 def __init__(self):
61 pass
62
63
64#
Gilad Arnold382df5c2013-05-03 12:49:28 -070065# Checked and hashed reading of data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080066#
Gilad Arnold5502b562013-03-08 13:22:31 -080067def IntPackingFmtStr(size, is_unsigned):
68 """Returns an integer format string for use by the struct module.
69
70 Args:
71 size: the integer size in bytes (2, 4 or 8)
72 is_unsigned: whether it is signed or not
Allie Wood12f59aa2015-04-06 11:05:12 -070073
Gilad Arnold5502b562013-03-08 13:22:31 -080074 Returns:
75 A format string for packing/unpacking integer values; assumes network byte
76 order (big-endian).
Allie Wood12f59aa2015-04-06 11:05:12 -070077
Gilad Arnold5502b562013-03-08 13:22:31 -080078 Raises:
79 PayloadError if something is wrong with the arguments.
Gilad Arnold5502b562013-03-08 13:22:31 -080080 """
81 # Determine the base conversion format.
82 if size == 2:
83 fmt = 'h'
84 elif size == 4:
85 fmt = 'i'
86 elif size == 8:
87 fmt = 'q'
88 else:
89 raise PayloadError('unsupport numeric field size (%s)' % size)
90
91 # Signed or unsigned?
92 if is_unsigned:
93 fmt = fmt.upper()
94
95 # Make it network byte order (big-endian).
96 fmt = '!' + fmt
97
98 return fmt
99
100
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800101def Read(file_obj, length, offset=None, hasher=None):
102 """Reads binary data from a file.
103
104 Args:
105 file_obj: an open file object
106 length: the length of the data to read
107 offset: an offset to seek to prior to reading; this is an absolute offset
108 from either the beginning (non-negative) or end (negative) of the
109 file. (optional)
110 hasher: a hashing object to pass the read data through (optional)
Allie Wood12f59aa2015-04-06 11:05:12 -0700111
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800112 Returns:
113 A string containing the read data.
Allie Wood12f59aa2015-04-06 11:05:12 -0700114
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800115 Raises:
116 PayloadError if a read error occurred or not enough data was read.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800117 """
118 if offset is not None:
119 if offset >= 0:
120 file_obj.seek(offset)
121 else:
122 file_obj.seek(offset, 2)
123
124 try:
125 data = file_obj.read(length)
126 except IOError, e:
127 raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e))
128
129 if len(data) != length:
130 raise PayloadError(
131 'reading from file (%s) too short (%d instead of %d bytes)' %
132 (file_obj.name, len(data), length))
133
134 if hasher:
135 hasher.update(data)
136
137 return data
138
139
140#
141# Formatting functions.
142#
143def FormatExtent(ex, block_size=0):
144 end_block = ex.start_block + ex.num_blocks
145 if block_size:
146 return '%d->%d * %d' % (ex.start_block, end_block, block_size)
147 else:
148 return '%d->%d' % (ex.start_block, end_block)
149
150
151def FormatSha256(digest):
152 """Returns a canonical string representation of a SHA256 digest."""
Gilad Arnolda7aa0bc2013-11-12 08:18:08 -0800153 return digest.encode('base64').strip()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800154
155
156#
157# Useful iterators.
158#
159def _ObjNameIter(items, base_name, reverse=False, name_format_func=None):
160 """A generic (item, name) tuple iterators.
161
162 Args:
163 items: the sequence of objects to iterate on
164 base_name: the base name for all objects
165 reverse: whether iteration should be in reverse order
166 name_format_func: a function to apply to the name string
Allie Wood12f59aa2015-04-06 11:05:12 -0700167
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800168 Yields:
169 An iterator whose i-th invocation returns (items[i], name), where name ==
170 base_name + '[i]' (with a formatting function optionally applied to it).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800171 """
172 idx, inc = (len(items), -1) if reverse else (1, 1)
Gilad Arnolde4beff72015-07-16 14:14:03 -0700173 if reverse:
174 items = reversed(items)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800175 for item in items:
176 item_name = '%s[%d]' % (base_name, idx)
177 if name_format_func:
178 item_name = name_format_func(item, item_name)
179 yield (item, item_name)
180 idx += inc
181
182
183def _OperationNameFormatter(op, op_name):
184 return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?'))
185
186
187def OperationIter(operations, base_name, reverse=False):
188 """An (item, name) iterator for update operations."""
189 return _ObjNameIter(operations, base_name, reverse=reverse,
190 name_format_func=_OperationNameFormatter)
191
192
193def ExtentIter(extents, base_name, reverse=False):
194 """An (item, name) iterator for operation extents."""
195 return _ObjNameIter(extents, base_name, reverse=reverse)
196
197
198def SignatureIter(sigs, base_name, reverse=False):
199 """An (item, name) iterator for signatures."""
200 return _ObjNameIter(sigs, base_name, reverse=reverse)