Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 2 | # |
Amin Hassani | f94b643 | 2018-01-26 17:39:47 -0800 | [diff] [blame] | 3 | # Copyright (C) 2013 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 17 | |
| 18 | """Command-line tool for checking and applying Chrome OS update payloads.""" |
| 19 | |
Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 20 | from __future__ import absolute_import |
Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 21 | from __future__ import print_function |
| 22 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 23 | # pylint: disable=import-error |
| 24 | import argparse |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 25 | import filecmp |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 26 | import os |
| 27 | import sys |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 28 | import tempfile |
| 29 | |
Amin Hassani | 42c2f98 | 2020-10-29 12:10:05 -0700 | [diff] [blame] | 30 | # pylint: disable=redefined-builtin |
Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 31 | from six.moves import zip |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 32 | from update_payload import error |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 33 | |
Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 34 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 35 | lib_dir = os.path.join(os.path.dirname(__file__), 'lib') |
| 36 | if os.path.exists(lib_dir) and os.path.isdir(lib_dir): |
| 37 | sys.path.insert(1, lib_dir) |
Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 38 | import update_payload # pylint: disable=wrong-import-position |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | _TYPE_FULL = 'full' |
| 42 | _TYPE_DELTA = 'delta' |
| 43 | |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 44 | def CheckApplyPayload(args): |
| 45 | """Whether to check the result after applying the payload. |
| 46 | |
| 47 | Args: |
| 48 | args: Parsed command arguments (the return value of |
| 49 | ArgumentParser.parse_args). |
| 50 | |
| 51 | Returns: |
| 52 | Boolean value whether to check. |
| 53 | """ |
| 54 | return args.dst_part_paths is not None |
| 55 | |
| 56 | def ApplyPayload(args): |
| 57 | """Whether to apply the payload. |
| 58 | |
| 59 | Args: |
| 60 | args: Parsed command arguments (the return value of |
| 61 | ArgumentParser.parse_args). |
| 62 | |
| 63 | Returns: |
| 64 | Boolean value whether to apply the payload. |
| 65 | """ |
| 66 | return CheckApplyPayload(args) or args.out_dst_part_paths is not None |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 67 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 68 | def ParseArguments(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 69 | """Parse and validate command-line arguments. |
| 70 | |
| 71 | Args: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 72 | argv: command-line arguments to parse (excluding the program name) |
Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 73 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 74 | Returns: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 75 | Returns the arguments returned by the argument parser. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 76 | """ |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 77 | parser = argparse.ArgumentParser( |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 78 | description=('Applies a Chrome OS update PAYLOAD to src_part_paths' |
| 79 | 'emitting dst_part_paths, respectively. ' |
| 80 | 'src_part_paths are only needed for delta payloads. ' |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 81 | 'When no partitions are provided, verifies the payload ' |
| 82 | 'integrity.'), |
| 83 | epilog=('Note: a payload may verify correctly but fail to apply, and ' |
| 84 | 'vice versa; this is by design and can be thought of as static ' |
| 85 | 'vs dynamic correctness. A payload that both verifies and ' |
| 86 | 'applies correctly should be safe for use by the Chrome OS ' |
| 87 | 'Update Engine. Use --check to verify a payload prior to ' |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 88 | 'applying it.'), |
| 89 | formatter_class=argparse.RawDescriptionHelpFormatter |
| 90 | ) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 91 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 92 | check_args = parser.add_argument_group('Checking payload integrity') |
| 93 | check_args.add_argument('-c', '--check', action='store_true', default=False, |
| 94 | help=('force payload integrity check (e.g. before ' |
| 95 | 'applying)')) |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 96 | check_args.add_argument('-r', '--report', metavar='FILE', |
| 97 | help="dump payload report (`-' for stdout)") |
| 98 | check_args.add_argument('-t', '--type', dest='assert_type', |
| 99 | help='assert the payload type', |
| 100 | choices=[_TYPE_FULL, _TYPE_DELTA]) |
| 101 | check_args.add_argument('-z', '--block-size', metavar='NUM', default=0, |
| 102 | type=int, |
| 103 | help='assert a non-default (4096) payload block size') |
| 104 | check_args.add_argument('-u', '--allow-unhashed', action='store_true', |
| 105 | default=False, help='allow unhashed operations') |
| 106 | check_args.add_argument('-d', '--disabled_tests', default=(), metavar='', |
| 107 | help=('space separated list of tests to disable. ' |
| 108 | 'allowed options include: ' + |
| 109 | ', '.join(update_payload.CHECKS_TO_DISABLE)), |
| 110 | choices=update_payload.CHECKS_TO_DISABLE) |
| 111 | check_args.add_argument('-k', '--key', metavar='FILE', |
| 112 | help=('override standard key used for signature ' |
| 113 | 'validation')) |
| 114 | check_args.add_argument('-m', '--meta-sig', metavar='FILE', |
| 115 | help='verify metadata against its signature') |
Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 116 | check_args.add_argument('-s', '--metadata-size', metavar='NUM', default=0, |
| 117 | help='the metadata size to verify with the one in' |
| 118 | ' payload') |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 119 | check_args.add_argument('--part_sizes', metavar='NUM', nargs='+', type=int, |
| 120 | help='override partition size auto-inference') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 121 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 122 | apply_args = parser.add_argument_group('Applying payload') |
| 123 | # TODO(ahassani): Extent extract-bsdiff to puffdiff too. |
| 124 | apply_args.add_argument('-x', '--extract-bsdiff', action='store_true', |
| 125 | default=False, |
| 126 | help=('use temp input/output files with BSDIFF ' |
| 127 | 'operations (not in-place)')) |
| 128 | apply_args.add_argument('--bspatch-path', metavar='FILE', |
| 129 | help='use the specified bspatch binary') |
| 130 | apply_args.add_argument('--puffpatch-path', metavar='FILE', |
| 131 | help='use the specified puffpatch binary') |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 132 | |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 133 | apply_args.add_argument('--src_part_paths', metavar='FILE', nargs='+', |
| 134 | help='source partitition files') |
| 135 | apply_args.add_argument('--dst_part_paths', metavar='FILE', nargs='+', |
| 136 | help='destination partition files') |
| 137 | apply_args.add_argument('--out_dst_part_paths', metavar='FILE', nargs='+', |
| 138 | help='created destination partition files') |
| 139 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 140 | parser.add_argument('payload', metavar='PAYLOAD', help='the payload file') |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 141 | parser.add_argument('--part_names', metavar='NAME', nargs='+', |
| 142 | help='names of partitions') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 143 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 144 | # Parse command-line arguments. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 145 | args = parser.parse_args(argv) |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 146 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 147 | # There are several options that imply --check. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 148 | args.check = (args.check or args.report or args.assert_type or |
| 149 | args.block_size or args.allow_unhashed or |
| 150 | args.disabled_tests or args.meta_sig or args.key or |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 151 | args.part_sizes is not None or args.metadata_size) |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 152 | |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 153 | # Makes sure the following arguments have the same length as |part_names| if |
| 154 | # set. |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 155 | for arg in ['part_sizes', 'src_part_paths', 'dst_part_paths', |
| 156 | 'out_dst_part_paths']: |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 157 | if getattr(args, arg) is None: |
| 158 | # Parameter is not set. |
| 159 | continue |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 160 | if len(args.part_names) != len(getattr(args, arg, [])): |
| 161 | parser.error('partitions in --%s do not match --part_names' % arg) |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 162 | |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 163 | def _IsSrcPartPathsProvided(args): |
| 164 | return args.src_part_paths is not None |
| 165 | |
| 166 | # Makes sure parameters are coherent with payload type. |
| 167 | if ApplyPayload(args): |
| 168 | if _IsSrcPartPathsProvided(args): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 169 | if args.assert_type == _TYPE_FULL: |
| 170 | parser.error('%s payload does not accept source partition arguments' |
| 171 | % _TYPE_FULL) |
| 172 | else: |
| 173 | args.assert_type = _TYPE_DELTA |
| 174 | else: |
| 175 | if args.assert_type == _TYPE_DELTA: |
| 176 | parser.error('%s payload requires source partitions arguments' |
| 177 | % _TYPE_DELTA) |
| 178 | else: |
| 179 | args.assert_type = _TYPE_FULL |
| 180 | else: |
Amin Hassani | a548902 | 2018-01-26 11:23:26 -0800 | [diff] [blame] | 181 | # Not applying payload. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 182 | if args.extract_bsdiff: |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 183 | parser.error('--extract-bsdiff can only be used when applying payloads') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 184 | if args.bspatch_path: |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame] | 185 | parser.error('--bspatch-path can only be used when applying payloads') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 186 | if args.puffpatch_path: |
Amin Hassani | 6be7168 | 2017-12-01 10:46:45 -0800 | [diff] [blame] | 187 | parser.error('--puffpatch-path can only be used when applying payloads') |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 188 | |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 189 | # By default, look for a metadata-signature file with a name based on the name |
Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 190 | # of the payload we are checking. We only do it if check was triggered. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 191 | if args.check and not args.meta_sig: |
| 192 | default_meta_sig = args.payload + '.metadata-signature' |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 193 | if os.path.isfile(default_meta_sig): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 194 | args.meta_sig = default_meta_sig |
| 195 | print('Using default metadata signature', args.meta_sig, file=sys.stderr) |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 196 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 197 | return args |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def main(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 201 | # Parse and validate arguments. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 202 | args = ParseArguments(argv[1:]) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 203 | |
Andrew | 1f6bcab | 2019-11-21 11:34:22 -0800 | [diff] [blame] | 204 | with open(args.payload, 'rb') as payload_file: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 205 | payload = update_payload.Payload(payload_file) |
| 206 | try: |
| 207 | # Initialize payload. |
| 208 | payload.Init() |
| 209 | |
| 210 | # Perform payload integrity checks. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 211 | if args.check: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 212 | report_file = None |
| 213 | do_close_report_file = False |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 214 | metadata_sig_file = None |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 215 | try: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 216 | if args.report: |
| 217 | if args.report == '-': |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 218 | report_file = sys.stdout |
| 219 | else: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 220 | report_file = open(args.report, 'w') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 221 | do_close_report_file = True |
Gilad Arnold | 03959b7 | 2013-05-07 17:08:18 -0700 | [diff] [blame] | 222 | |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 223 | part_sizes = (args.part_sizes and |
| 224 | dict(zip(args.part_names, args.part_sizes))) |
Andrew | 1f6bcab | 2019-11-21 11:34:22 -0800 | [diff] [blame] | 225 | metadata_sig_file = args.meta_sig and open(args.meta_sig, 'rb') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 226 | payload.Check( |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 227 | pubkey_file_name=args.key, |
Gilad Arnold | 4f8c17c | 2013-05-04 22:57:45 -0700 | [diff] [blame] | 228 | metadata_sig_file=metadata_sig_file, |
Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 229 | metadata_size=int(args.metadata_size), |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 230 | report_out_file=report_file, |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 231 | assert_type=args.assert_type, |
| 232 | block_size=int(args.block_size), |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 233 | part_sizes=part_sizes, |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 234 | allow_unhashed=args.allow_unhashed, |
| 235 | disabled_tests=args.disabled_tests) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 236 | finally: |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 237 | if metadata_sig_file: |
| 238 | metadata_sig_file.close() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 239 | if do_close_report_file: |
| 240 | report_file.close() |
| 241 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 242 | # Apply payload. |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 243 | if ApplyPayload(args): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 244 | dargs = {'bsdiff_in_place': not args.extract_bsdiff} |
| 245 | if args.bspatch_path: |
| 246 | dargs['bspatch_path'] = args.bspatch_path |
| 247 | if args.puffpatch_path: |
| 248 | dargs['puffpatch_path'] = args.puffpatch_path |
| 249 | if args.assert_type == _TYPE_DELTA: |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 250 | dargs['old_parts'] = dict(zip(args.part_names, args.src_part_paths)) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 251 | |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 252 | out_dst_parts = {} |
| 253 | file_handles = [] |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 254 | if args.out_dst_part_paths is not None: |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 255 | for name, path in zip(args.part_names, args.out_dst_part_paths): |
Andrew | 1f6bcab | 2019-11-21 11:34:22 -0800 | [diff] [blame] | 256 | handle = open(path, 'wb+') |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 257 | file_handles.append(handle) |
| 258 | out_dst_parts[name] = handle.name |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 259 | else: |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 260 | for name in args.part_names: |
| 261 | handle = tempfile.NamedTemporaryFile() |
| 262 | file_handles.append(handle) |
| 263 | out_dst_parts[name] = handle.name |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 264 | |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 265 | payload.Apply(out_dst_parts, **dargs) |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 266 | |
| 267 | # If destination kernel and rootfs partitions are not given, then this |
| 268 | # just becomes an apply operation with no check. |
Xiaochu Liu | 6cf8e67 | 2019-03-14 16:15:42 -0700 | [diff] [blame] | 269 | if CheckApplyPayload(args): |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 270 | # Prior to comparing, add the unused space past the filesystem |
| 271 | # boundary in the new target partitions to become the same size as |
| 272 | # the given partitions. This will truncate to larger size. |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 273 | for part_name, out_dst_part, dst_part in zip(args.part_names, |
| 274 | file_handles, |
| 275 | args.dst_part_paths): |
| 276 | out_dst_part.truncate(os.path.getsize(dst_part)) |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 277 | |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 278 | # Compare resulting partitions with the ones from the target image. |
| 279 | if not filecmp.cmp(out_dst_part.name, dst_part): |
| 280 | raise error.PayloadError( |
| 281 | 'Resulting %s partition corrupted.' % part_name) |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame] | 282 | |
| 283 | # Close the output files. If args.out_dst_* was not given, then these |
| 284 | # files are created as temp files and will be deleted upon close(). |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 285 | for handle in file_handles: |
| 286 | handle.close() |
Andrew Lassalle | 165843c | 2019-11-05 13:30:34 -0800 | [diff] [blame] | 287 | except error.PayloadError as e: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 288 | sys.stderr.write('Error: %s\n' % e) |
| 289 | return 1 |
| 290 | |
| 291 | return 0 |
| 292 | |
| 293 | |
| 294 | if __name__ == '__main__': |
| 295 | sys.exit(main(sys.argv)) |