blob: dfb8181a55c0e81d1f1e84b77c6f1c864dcb6b71 [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#
Gilad Arnold5502b562013-03-08 13:22:31 -080028SIG_ASN1_HEADER = (
29 '\x30\x31\x30\x0d\x06\x09\x60\x86'
30 '\x48\x01\x65\x03\x04\x02\x01\x05'
31 '\x00\x04\x20'
32)
33
Alex Deymoef497352015-10-15 09:14:58 -070034BRILLO_MAJOR_PAYLOAD_VERSION = 2
35
Allie Wood12f59aa2015-04-06 11:05:12 -070036SOURCE_MINOR_PAYLOAD_VERSION = 2
Sen Jiangd6122bb2015-12-11 10:27:04 -080037OPSRCHASH_MINOR_PAYLOAD_VERSION = 3
Amin Hassani77d7cbc2018-02-07 16:21:33 -080038BROTLI_BSDIFF_MINOR_PAYLOAD_VERSION = 4
39PUFFDIFF_MINOR_PAYLOAD_VERSION = 5
Gilad Arnold553b0ec2013-01-26 01:00:39 -080040
Tudor Brindus8d05a7e2018-06-14 11:18:18 -070041KERNEL = 'kernel'
Tudor Brindusb220d662018-07-10 23:55:51 -070042ROOTFS = 'root'
43# Tuple of (name in system, name in protobuf).
44CROS_PARTITIONS = ((KERNEL, KERNEL), (ROOTFS, 'rootfs'))
Tudor Brindus8d05a7e2018-06-14 11:18:18 -070045
Gilad Arnold553b0ec2013-01-26 01:00:39 -080046#
47# Payload operation types.
48#
49class OpType(object):
50 """Container for operation type constants."""
Alex Deymo28466772015-09-11 17:16:44 -070051 _CLASS = update_metadata_pb2.InstallOperation
Gilad Arnold553b0ec2013-01-26 01:00:39 -080052 REPLACE = _CLASS.REPLACE
53 REPLACE_BZ = _CLASS.REPLACE_BZ
Allie Woodc11dc732015-02-18 15:53:05 -080054 SOURCE_COPY = _CLASS.SOURCE_COPY
55 SOURCE_BSDIFF = _CLASS.SOURCE_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070056 ZERO = _CLASS.ZERO
57 DISCARD = _CLASS.DISCARD
58 REPLACE_XZ = _CLASS.REPLACE_XZ
Amin Hassani5ef5d452017-08-04 13:10:59 -070059 PUFFDIFF = _CLASS.PUFFDIFF
Amin Hassaniefa62d92017-11-09 13:46:56 -080060 BROTLI_BSDIFF = _CLASS.BROTLI_BSDIFF
Amin Hassani0f59a9a2019-09-27 10:24:31 -070061 ALL = (REPLACE, REPLACE_BZ, SOURCE_COPY, SOURCE_BSDIFF, ZERO,
Amin Hassaniefa62d92017-11-09 13:46:56 -080062 DISCARD, REPLACE_XZ, PUFFDIFF, BROTLI_BSDIFF)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080063 NAMES = {
64 REPLACE: 'REPLACE',
65 REPLACE_BZ: 'REPLACE_BZ',
Allie Woodc11dc732015-02-18 15:53:05 -080066 SOURCE_COPY: 'SOURCE_COPY',
Alex Deymo28466772015-09-11 17:16:44 -070067 SOURCE_BSDIFF: 'SOURCE_BSDIFF',
68 ZERO: 'ZERO',
69 DISCARD: 'DISCARD',
Sen Jiangc2538fa2016-02-24 14:15:02 -080070 REPLACE_XZ: 'REPLACE_XZ',
Amin Hassani5ef5d452017-08-04 13:10:59 -070071 PUFFDIFF: 'PUFFDIFF',
Amin Hassaniefa62d92017-11-09 13:46:56 -080072 BROTLI_BSDIFF: 'BROTLI_BSDIFF',
Gilad Arnold553b0ec2013-01-26 01:00:39 -080073 }
74
75 def __init__(self):
76 pass
77
78
79#
Gilad Arnold382df5c2013-05-03 12:49:28 -070080# Checked and hashed reading of data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080081#
Gilad Arnold5502b562013-03-08 13:22:31 -080082def IntPackingFmtStr(size, is_unsigned):
83 """Returns an integer format string for use by the struct module.
84
85 Args:
86 size: the integer size in bytes (2, 4 or 8)
87 is_unsigned: whether it is signed or not
Allie Wood12f59aa2015-04-06 11:05:12 -070088
Gilad Arnold5502b562013-03-08 13:22:31 -080089 Returns:
90 A format string for packing/unpacking integer values; assumes network byte
91 order (big-endian).
Allie Wood12f59aa2015-04-06 11:05:12 -070092
Gilad Arnold5502b562013-03-08 13:22:31 -080093 Raises:
94 PayloadError if something is wrong with the arguments.
Gilad Arnold5502b562013-03-08 13:22:31 -080095 """
96 # Determine the base conversion format.
97 if size == 2:
98 fmt = 'h'
99 elif size == 4:
100 fmt = 'i'
101 elif size == 8:
102 fmt = 'q'
103 else:
104 raise PayloadError('unsupport numeric field size (%s)' % size)
105
106 # Signed or unsigned?
107 if is_unsigned:
108 fmt = fmt.upper()
109
110 # Make it network byte order (big-endian).
111 fmt = '!' + fmt
112
113 return fmt
114
115
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800116def Read(file_obj, length, offset=None, hasher=None):
117 """Reads binary data from a file.
118
119 Args:
120 file_obj: an open file object
121 length: the length of the data to read
122 offset: an offset to seek to prior to reading; this is an absolute offset
123 from either the beginning (non-negative) or end (negative) of the
124 file. (optional)
125 hasher: a hashing object to pass the read data through (optional)
Allie Wood12f59aa2015-04-06 11:05:12 -0700126
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800127 Returns:
128 A string containing the read data.
Allie Wood12f59aa2015-04-06 11:05:12 -0700129
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800130 Raises:
131 PayloadError if a read error occurred or not enough data was read.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800132 """
133 if offset is not None:
134 if offset >= 0:
135 file_obj.seek(offset)
136 else:
137 file_obj.seek(offset, 2)
138
139 try:
140 data = file_obj.read(length)
141 except IOError, e:
142 raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e))
143
144 if len(data) != length:
145 raise PayloadError(
146 'reading from file (%s) too short (%d instead of %d bytes)' %
147 (file_obj.name, len(data), length))
148
149 if hasher:
150 hasher.update(data)
151
152 return data
153
154
155#
156# Formatting functions.
157#
158def FormatExtent(ex, block_size=0):
159 end_block = ex.start_block + ex.num_blocks
160 if block_size:
161 return '%d->%d * %d' % (ex.start_block, end_block, block_size)
Amin Hassani55c75412019-10-07 11:20:39 -0700162 return '%d->%d' % (ex.start_block, end_block)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800163
164
165def FormatSha256(digest):
166 """Returns a canonical string representation of a SHA256 digest."""
Gilad Arnolda7aa0bc2013-11-12 08:18:08 -0800167 return digest.encode('base64').strip()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800168
169
170#
171# Useful iterators.
172#
173def _ObjNameIter(items, base_name, reverse=False, name_format_func=None):
174 """A generic (item, name) tuple iterators.
175
176 Args:
177 items: the sequence of objects to iterate on
178 base_name: the base name for all objects
179 reverse: whether iteration should be in reverse order
180 name_format_func: a function to apply to the name string
Allie Wood12f59aa2015-04-06 11:05:12 -0700181
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800182 Yields:
183 An iterator whose i-th invocation returns (items[i], name), where name ==
184 base_name + '[i]' (with a formatting function optionally applied to it).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800185 """
186 idx, inc = (len(items), -1) if reverse else (1, 1)
Gilad Arnolde4beff72015-07-16 14:14:03 -0700187 if reverse:
188 items = reversed(items)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800189 for item in items:
190 item_name = '%s[%d]' % (base_name, idx)
191 if name_format_func:
192 item_name = name_format_func(item, item_name)
193 yield (item, item_name)
194 idx += inc
195
196
197def _OperationNameFormatter(op, op_name):
198 return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?'))
199
200
201def OperationIter(operations, base_name, reverse=False):
202 """An (item, name) iterator for update operations."""
203 return _ObjNameIter(operations, base_name, reverse=reverse,
204 name_format_func=_OperationNameFormatter)
205
206
207def ExtentIter(extents, base_name, reverse=False):
208 """An (item, name) iterator for operation extents."""
209 return _ObjNameIter(extents, base_name, reverse=reverse)
210
211
212def SignatureIter(sigs, base_name, reverse=False):
213 """An (item, name) iterator for signatures."""
214 return _ObjNameIter(sigs, base_name, reverse=reverse)