blob: 9061a7543f3f1e4fa164fd217db2f0b6e4990fd8 [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'
Tudor Brindusb220d662018-07-10 23:55:51 -070046ROOTFS = 'root'
47# Tuple of (name in system, name in protobuf).
48CROS_PARTITIONS = ((KERNEL, KERNEL), (ROOTFS, 'rootfs'))
Tudor Brindus8d05a7e2018-06-14 11:18:18 -070049
Gilad Arnold553b0ec2013-01-26 01:00:39 -080050#
51# Payload operation types.
52#
53class OpType(object):
54 """Container for operation type constants."""
Alex Deymo28466772015-09-11 17:16:44 -070055 _CLASS = update_metadata_pb2.InstallOperation
Gilad Arnold553b0ec2013-01-26 01:00:39 -080056 REPLACE = _CLASS.REPLACE
57 REPLACE_BZ = _CLASS.REPLACE_BZ
58 MOVE = _CLASS.MOVE
59 BSDIFF = _CLASS.BSDIFF
Allie Woodc11dc732015-02-18 15:53:05 -080060 SOURCE_COPY = _CLASS.SOURCE_COPY
61 SOURCE_BSDIFF = _CLASS.SOURCE_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070062 ZERO = _CLASS.ZERO
63 DISCARD = _CLASS.DISCARD
64 REPLACE_XZ = _CLASS.REPLACE_XZ
Amin Hassani5ef5d452017-08-04 13:10:59 -070065 PUFFDIFF = _CLASS.PUFFDIFF
Amin Hassaniefa62d92017-11-09 13:46:56 -080066 BROTLI_BSDIFF = _CLASS.BROTLI_BSDIFF
Alex Deymo28466772015-09-11 17:16:44 -070067 ALL = (REPLACE, REPLACE_BZ, MOVE, BSDIFF, SOURCE_COPY, SOURCE_BSDIFF, ZERO,
Amin Hassaniefa62d92017-11-09 13:46:56 -080068 DISCARD, REPLACE_XZ, PUFFDIFF, BROTLI_BSDIFF)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080069 NAMES = {
70 REPLACE: 'REPLACE',
71 REPLACE_BZ: 'REPLACE_BZ',
72 MOVE: 'MOVE',
73 BSDIFF: 'BSDIFF',
Allie Woodc11dc732015-02-18 15:53:05 -080074 SOURCE_COPY: 'SOURCE_COPY',
Alex Deymo28466772015-09-11 17:16:44 -070075 SOURCE_BSDIFF: 'SOURCE_BSDIFF',
76 ZERO: 'ZERO',
77 DISCARD: 'DISCARD',
Sen Jiangc2538fa2016-02-24 14:15:02 -080078 REPLACE_XZ: 'REPLACE_XZ',
Amin Hassani5ef5d452017-08-04 13:10:59 -070079 PUFFDIFF: 'PUFFDIFF',
Amin Hassaniefa62d92017-11-09 13:46:56 -080080 BROTLI_BSDIFF: 'BROTLI_BSDIFF',
Gilad Arnold553b0ec2013-01-26 01:00:39 -080081 }
82
83 def __init__(self):
84 pass
85
86
87#
Gilad Arnold382df5c2013-05-03 12:49:28 -070088# Checked and hashed reading of data.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080089#
Gilad Arnold5502b562013-03-08 13:22:31 -080090def IntPackingFmtStr(size, is_unsigned):
91 """Returns an integer format string for use by the struct module.
92
93 Args:
94 size: the integer size in bytes (2, 4 or 8)
95 is_unsigned: whether it is signed or not
Allie Wood12f59aa2015-04-06 11:05:12 -070096
Gilad Arnold5502b562013-03-08 13:22:31 -080097 Returns:
98 A format string for packing/unpacking integer values; assumes network byte
99 order (big-endian).
Allie Wood12f59aa2015-04-06 11:05:12 -0700100
Gilad Arnold5502b562013-03-08 13:22:31 -0800101 Raises:
102 PayloadError if something is wrong with the arguments.
Gilad Arnold5502b562013-03-08 13:22:31 -0800103 """
104 # Determine the base conversion format.
105 if size == 2:
106 fmt = 'h'
107 elif size == 4:
108 fmt = 'i'
109 elif size == 8:
110 fmt = 'q'
111 else:
112 raise PayloadError('unsupport numeric field size (%s)' % size)
113
114 # Signed or unsigned?
115 if is_unsigned:
116 fmt = fmt.upper()
117
118 # Make it network byte order (big-endian).
119 fmt = '!' + fmt
120
121 return fmt
122
123
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800124def Read(file_obj, length, offset=None, hasher=None):
125 """Reads binary data from a file.
126
127 Args:
128 file_obj: an open file object
129 length: the length of the data to read
130 offset: an offset to seek to prior to reading; this is an absolute offset
131 from either the beginning (non-negative) or end (negative) of the
132 file. (optional)
133 hasher: a hashing object to pass the read data through (optional)
Allie Wood12f59aa2015-04-06 11:05:12 -0700134
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800135 Returns:
136 A string containing the read data.
Allie Wood12f59aa2015-04-06 11:05:12 -0700137
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800138 Raises:
139 PayloadError if a read error occurred or not enough data was read.
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800140 """
141 if offset is not None:
142 if offset >= 0:
143 file_obj.seek(offset)
144 else:
145 file_obj.seek(offset, 2)
146
147 try:
148 data = file_obj.read(length)
149 except IOError, e:
150 raise PayloadError('error reading from file (%s): %s' % (file_obj.name, e))
151
152 if len(data) != length:
153 raise PayloadError(
154 'reading from file (%s) too short (%d instead of %d bytes)' %
155 (file_obj.name, len(data), length))
156
157 if hasher:
158 hasher.update(data)
159
160 return data
161
162
163#
164# Formatting functions.
165#
166def FormatExtent(ex, block_size=0):
167 end_block = ex.start_block + ex.num_blocks
168 if block_size:
169 return '%d->%d * %d' % (ex.start_block, end_block, block_size)
170 else:
171 return '%d->%d' % (ex.start_block, end_block)
172
173
174def FormatSha256(digest):
175 """Returns a canonical string representation of a SHA256 digest."""
Gilad Arnolda7aa0bc2013-11-12 08:18:08 -0800176 return digest.encode('base64').strip()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800177
178
179#
180# Useful iterators.
181#
182def _ObjNameIter(items, base_name, reverse=False, name_format_func=None):
183 """A generic (item, name) tuple iterators.
184
185 Args:
186 items: the sequence of objects to iterate on
187 base_name: the base name for all objects
188 reverse: whether iteration should be in reverse order
189 name_format_func: a function to apply to the name string
Allie Wood12f59aa2015-04-06 11:05:12 -0700190
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800191 Yields:
192 An iterator whose i-th invocation returns (items[i], name), where name ==
193 base_name + '[i]' (with a formatting function optionally applied to it).
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800194 """
195 idx, inc = (len(items), -1) if reverse else (1, 1)
Gilad Arnolde4beff72015-07-16 14:14:03 -0700196 if reverse:
197 items = reversed(items)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800198 for item in items:
199 item_name = '%s[%d]' % (base_name, idx)
200 if name_format_func:
201 item_name = name_format_func(item, item_name)
202 yield (item, item_name)
203 idx += inc
204
205
206def _OperationNameFormatter(op, op_name):
207 return '%s(%s)' % (op_name, OpType.NAMES.get(op.type, '?'))
208
209
210def OperationIter(operations, base_name, reverse=False):
211 """An (item, name) iterator for update operations."""
212 return _ObjNameIter(operations, base_name, reverse=reverse,
213 name_format_func=_OperationNameFormatter)
214
215
216def ExtentIter(extents, base_name, reverse=False):
217 """An (item, name) iterator for operation extents."""
218 return _ObjNameIter(extents, base_name, reverse=reverse)
219
220
221def SignatureIter(sigs, base_name, reverse=False):
222 """An (item, name) iterator for signatures."""
223 return _ObjNameIter(sigs, base_name, reverse=reverse)