blob: 88df32eae939ebba63c5fbf38614c79b6e669a90 [file] [log] [blame]
Amin Hassanif94b6432018-01-26 17:39:47 -08001#
2# Copyright (C) 2013 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Gilad Arnold553b0ec2013-01-26 01:00:39 -080016
17"""Utilities for update payload processing."""
18
Allie Wood12f59aa2015-04-06 11:05:12 -070019from __future__ import print_function
20
Amin Hassanib05a65a2017-12-18 15:15:32 -080021from update_payload import update_metadata_pb2
22from update_payload.error import PayloadError
Gilad Arnold553b0ec2013-01-26 01:00:39 -080023
24
25#
26# Constants.
27#
Alex Deymocf6f30d2015-06-11 13:51:46 -070028PSEUDO_EXTENT_MARKER = (1L << 64) - 1 # UINT64_MAX
Gilad Arnold553b0ec2013-01-26 01:00:39 -080029
Gilad Arnold5502b562013-03-08 13:22:31 -080030SIG_ASN1_HEADER = (
31 '\x30\x31\x30\x0d\x06\x09\x60\x86'
32 '\x48\x01\x65\x03\x04\x02\x01\x05'
33 '\x00\x04\x20'
34)
35
Alex Deymoef497352015-10-15 09:14:58 -070036CHROMEOS_MAJOR_PAYLOAD_VERSION = 1
37BRILLO_MAJOR_PAYLOAD_VERSION = 2
38
Allie Wood12f59aa2015-04-06 11:05:12 -070039INPLACE_MINOR_PAYLOAD_VERSION = 1
40SOURCE_MINOR_PAYLOAD_VERSION = 2
Sen Jiangd6122bb2015-12-11 10:27:04 -080041OPSRCHASH_MINOR_PAYLOAD_VERSION = 3
Amin Hassani77d7cbc2018-02-07 16:21:33 -080042BROTLI_BSDIFF_MINOR_PAYLOAD_VERSION = 4
43PUFFDIFF_MINOR_PAYLOAD_VERSION = 5
Gilad Arnold553b0ec2013-01-26 01:00:39 -080044
Tudor Brindus8d05a7e2018-06-14 11:18:18 -070045KERNEL = 'kernel'
46ROOTFS = 'rootfs'
47CROS_PARTITIONS = (KERNEL, ROOTFS)
48
Gilad Arnold553b0ec2013-01-26 01:00:39 -080049#
50# Payload operation types.
51#
52class OpType(object):
53 """Container for operation type constants."""
Alex Deymo28466772015-09-11 17:16:44 -070054 _CLASS = update_metadata_pb2.InstallOperation
Gilad Arnold553b0ec2013-01-26 01:00:39 -080055 REPLACE = _CLASS.REPLACE
56 REPLACE_BZ = _CLASS.REPLACE_BZ
57 MOVE = _CLASS.MOVE
58 BSDIFF = _CLASS.BSDIFF
Allie Woodc11dc732015-02-18 15:53:05 -080059 SOURCE_COPY = _CLASS.SOURCE_COPY
60 SOURCE_BSDIFF = _CLASS.SOURCE_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070061 ZERO = _CLASS.ZERO
62 DISCARD = _CLASS.DISCARD
63 REPLACE_XZ = _CLASS.REPLACE_XZ
Amin Hassani5ef5d452017-08-04 13:10:59 -070064 PUFFDIFF = _CLASS.PUFFDIFF
Amin Hassaniefa62d92017-11-09 13:46:56 -080065 BROTLI_BSDIFF = _CLASS.BROTLI_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070066 ALL = (REPLACE, REPLACE_BZ, MOVE, BSDIFF, SOURCE_COPY, SOURCE_BSDIFF, ZERO,
Amin Hassaniefa62d92017-11-09 13:46:56 -080067 DISCARD, REPLACE_XZ, PUFFDIFF, BROTLI_BSDIFF)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080068 NAMES = {
69 REPLACE: 'REPLACE',
70 REPLACE_BZ: 'REPLACE_BZ',
71 MOVE: 'MOVE',
72 BSDIFF: 'BSDIFF',
Allie Woodc11dc732015-02-18 15:53:05 -080073 SOURCE_COPY: 'SOURCE_COPY',
Alex Deymo28466772015-09-11 17:16:44 -070074 SOURCE_BSDIFF: 'SOURCE_BSDIFF',
75 ZERO: 'ZERO',
76 DISCARD: 'DISCARD',
Sen Jiangc2538fa2016-02-24 14:15:02 -080077 REPLACE_XZ: 'REPLACE_XZ',
Amin Hassani5ef5d452017-08-04 13:10:59 -070078 PUFFDIFF: 'PUFFDIFF',
Amin Hassaniefa62d92017-11-09 13:46:56 -080079 BROTLI_BSDIFF: 'BROTLI_BSDIFF',
Gilad Arnold553b0ec2013-01-26 01:00:39 -080080 }
81
82 def __init__(self):
83 pass
84
85
86#
Gilad Arnold382df5c2013-05-03 12:49:28 -070087# Checked and hashed reading of data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080088#
Gilad Arnold5502b562013-03-08 13:22:31 -080089def IntPackingFmtStr(size, is_unsigned):
90 """Returns an integer format string for use by the struct module.
91
92 Args:
93 size: the integer size in bytes (2, 4 or 8)
94 is_unsigned: whether it is signed or not
Allie Wood12f59aa2015-04-06 11:05:12 -070095
Gilad Arnold5502b562013-03-08 13:22:31 -080096 Returns:
97 A format string for packing/unpacking integer values; assumes network byte
98 order (big-endian).
Allie Wood12f59aa2015-04-06 11:05:12 -070099
Gilad Arnold5502b562013-03-08 13:22:31 -0800100 Raises:
101 PayloadError if something is wrong with the arguments.
Gilad Arnold5502b562013-03-08 13:22:31 -0800102 """
103 # Determine the base conversion format.
104 if size == 2:
105 fmt = 'h'
106 elif size == 4:
107 fmt = 'i'
108 elif size == 8:
109 fmt = 'q'
110 else:
111 raise PayloadError('unsupport numeric field size (%s)' % size)
112
113 # Signed or unsigned?
114 if is_unsigned:
115 fmt = fmt.upper()
116
117 # Make it network byte order (big-endian).
118 fmt = '!' + fmt
119
120 return fmt
121
122
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800123def Read(file_obj, length, offset=None, hasher=None):
124 """Reads binary data from a file.
125
126 Args:
127 file_obj: an open file object
128 length: the length of the data to read
129 offset: an offset to seek to prior to reading; this is an absolute offset
130 from either the beginning (non-negative) or end (negative) of the
131 file. (optional)
132 hasher: a hashing object to pass the read data through (optional)
Allie Wood12f59aa2015-04-06 11:05:12 -0700133
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800134 Returns:
135 A string containing the read data.
Allie Wood12f59aa2015-04-06 11:05:12 -0700136
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800137 Raises:
138 PayloadError if a read error occurred or not enough data was read.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800139 """
140 if offset is not None:
141 if offset >= 0:
142 file_obj.seek(offset)
143 else:
144 file_obj.seek(offset, 2)
145
146 try:
147 data = file_obj.read(length)
148 except IOError, e:
149 raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e))
150
151 if len(data) != length:
152 raise PayloadError(
153 'reading from file (%s) too short (%d instead of %d bytes)' %
154 (file_obj.name, len(data), length))
155
156 if hasher:
157 hasher.update(data)
158
159 return data
160
161
162#
163# Formatting functions.
164#
165def FormatExtent(ex, block_size=0):
166 end_block = ex.start_block + ex.num_blocks
167 if block_size:
168 return '%d->%d * %d' % (ex.start_block, end_block, block_size)
169 else:
170 return '%d->%d' % (ex.start_block, end_block)
171
172
173def FormatSha256(digest):
174 """Returns a canonical string representation of a SHA256 digest."""
Gilad Arnolda7aa0bc2013-11-12 08:18:08 -0800175 return digest.encode('base64').strip()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800176
177
178#
179# Useful iterators.
180#
181def _ObjNameIter(items, base_name, reverse=False, name_format_func=None):
182 """A generic (item, name) tuple iterators.
183
184 Args:
185 items: the sequence of objects to iterate on
186 base_name: the base name for all objects
187 reverse: whether iteration should be in reverse order
188 name_format_func: a function to apply to the name string
Allie Wood12f59aa2015-04-06 11:05:12 -0700189
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800190 Yields:
191 An iterator whose i-th invocation returns (items[i], name), where name ==
192 base_name + '[i]' (with a formatting function optionally applied to it).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800193 """
194 idx, inc = (len(items), -1) if reverse else (1, 1)
Gilad Arnolde4beff72015-07-16 14:14:03 -0700195 if reverse:
196 items = reversed(items)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800197 for item in items:
198 item_name = '%s[%d]' % (base_name, idx)
199 if name_format_func:
200 item_name = name_format_func(item, item_name)
201 yield (item, item_name)
202 idx += inc
203
204
205def _OperationNameFormatter(op, op_name):
206 return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?'))
207
208
209def OperationIter(operations, base_name, reverse=False):
210 """An (item, name) iterator for update operations."""
211 return _ObjNameIter(operations, base_name, reverse=reverse,
212 name_format_func=_OperationNameFormatter)
213
214
215def ExtentIter(extents, base_name, reverse=False):
216 """An (item, name) iterator for operation extents."""
217 return _ObjNameIter(extents, base_name, reverse=reverse)
218
219
220def SignatureIter(sigs, base_name, reverse=False):
221 """An (item, name) iterator for signatures."""
222 return _ObjNameIter(sigs, base_name, reverse=reverse)