Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
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 | |
Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 22 | # pylint: disable=import-error |
| 23 | import argparse |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 24 | import filecmp |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 25 | import os |
| 26 | import sys |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 27 | import tempfile |
| 28 | |
| 29 | from update_payload import error |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 30 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 31 | lib_dir = os.path.join(os.path.dirname(__file__), 'lib') |
| 32 | if os.path.exists(lib_dir) and os.path.isdir(lib_dir): |
| 33 | sys.path.insert(1, lib_dir) |
| 34 | import update_payload |
| 35 | |
| 36 | |
| 37 | _TYPE_FULL = 'full' |
| 38 | _TYPE_DELTA = 'delta' |
| 39 | |
| 40 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 41 | def ParseArguments(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 42 | """Parse and validate command-line arguments. |
| 43 | |
| 44 | Args: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 45 | argv: command-line arguments to parse (excluding the program name) |
Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 46 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 47 | Returns: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 48 | Returns the arguments returned by the argument parser. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 49 | """ |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 50 | parser = argparse.ArgumentParser( |
| 51 | description=('Applies a Chrome OS update PAYLOAD to src_kern and ' |
| 52 | 'src_root emitting dst_kern and dst_root, respectively. ' |
| 53 | 'src_kern and src_root are only needed for delta payloads. ' |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 54 | 'When no partitions are provided, verifies the payload ' |
| 55 | 'integrity.'), |
| 56 | epilog=('Note: a payload may verify correctly but fail to apply, and ' |
| 57 | 'vice versa; this is by design and can be thought of as static ' |
| 58 | 'vs dynamic correctness. A payload that both verifies and ' |
| 59 | 'applies correctly should be safe for use by the Chrome OS ' |
| 60 | 'Update Engine. Use --check to verify a payload prior to ' |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 61 | 'applying it.'), |
| 62 | formatter_class=argparse.RawDescriptionHelpFormatter |
| 63 | ) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 64 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 65 | check_args = parser.add_argument_group('Checking payload integrity') |
| 66 | check_args.add_argument('-c', '--check', action='store_true', default=False, |
| 67 | help=('force payload integrity check (e.g. before ' |
| 68 | 'applying)')) |
| 69 | check_args.add_argument('-D', '--describe', action='store_true', |
| 70 | default=False, |
| 71 | help='Print a friendly description of the payload.') |
| 72 | check_args.add_argument('-r', '--report', metavar='FILE', |
| 73 | help="dump payload report (`-' for stdout)") |
| 74 | check_args.add_argument('-t', '--type', dest='assert_type', |
| 75 | help='assert the payload type', |
| 76 | choices=[_TYPE_FULL, _TYPE_DELTA]) |
| 77 | check_args.add_argument('-z', '--block-size', metavar='NUM', default=0, |
| 78 | type=int, |
| 79 | help='assert a non-default (4096) payload block size') |
| 80 | check_args.add_argument('-u', '--allow-unhashed', action='store_true', |
| 81 | default=False, help='allow unhashed operations') |
| 82 | check_args.add_argument('-d', '--disabled_tests', default=(), metavar='', |
| 83 | help=('space separated list of tests to disable. ' |
| 84 | 'allowed options include: ' + |
| 85 | ', '.join(update_payload.CHECKS_TO_DISABLE)), |
| 86 | choices=update_payload.CHECKS_TO_DISABLE) |
| 87 | check_args.add_argument('-k', '--key', metavar='FILE', |
| 88 | help=('override standard key used for signature ' |
| 89 | 'validation')) |
| 90 | check_args.add_argument('-m', '--meta-sig', metavar='FILE', |
| 91 | help='verify metadata against its signature') |
| 92 | check_args.add_argument('-p', '--root-part-size', metavar='NUM', |
| 93 | default=0, type=int, |
| 94 | help='override rootfs partition size auto-inference') |
| 95 | check_args.add_argument('-P', '--kern-part-size', metavar='NUM', |
| 96 | default=0, type=int, |
| 97 | help='override kernel partition size auto-inference') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 98 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 99 | apply_args = parser.add_argument_group('Applying payload') |
| 100 | # TODO(ahassani): Extent extract-bsdiff to puffdiff too. |
| 101 | apply_args.add_argument('-x', '--extract-bsdiff', action='store_true', |
| 102 | default=False, |
| 103 | help=('use temp input/output files with BSDIFF ' |
| 104 | 'operations (not in-place)')) |
| 105 | apply_args.add_argument('--bspatch-path', metavar='FILE', |
| 106 | help='use the specified bspatch binary') |
| 107 | apply_args.add_argument('--puffpatch-path', metavar='FILE', |
| 108 | help='use the specified puffpatch binary') |
| 109 | apply_args.add_argument('--dst_kern', metavar='FILE', |
| 110 | help='destination kernel partition file') |
| 111 | apply_args.add_argument('--dst_root', metavar='FILE', |
| 112 | help='destination root partition file') |
| 113 | apply_args.add_argument('--src_kern', metavar='FILE', |
| 114 | help='source kernel partition file') |
| 115 | apply_args.add_argument('--src_root', metavar='FILE', |
| 116 | help='source root partition file') |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 117 | apply_args.add_argument('--out_dst_kern', metavar='FILE', |
| 118 | help='created destination kernel partition file') |
| 119 | apply_args.add_argument('--out_dst_root', metavar='FILE', |
| 120 | help='created destination root partition file') |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 121 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 122 | parser.add_argument('payload', metavar='PAYLOAD', help='the payload file') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 123 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 124 | # Parse command-line arguments. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 125 | args = parser.parse_args(argv) |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 126 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 127 | # There are several options that imply --check. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 128 | args.check = (args.check or args.report or args.assert_type or |
| 129 | args.block_size or args.allow_unhashed or |
| 130 | args.disabled_tests or args.meta_sig or args.key or |
| 131 | args.root_part_size or args.kern_part_size) |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 132 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 133 | # Check the arguments, enforce payload type accordingly. |
| 134 | if (args.src_kern is None) != (args.src_root is None): |
| 135 | parser.error('--src_kern and --src_root should be given together') |
| 136 | if (args.dst_kern is None) != (args.dst_root is None): |
| 137 | parser.error('--dst_kern and --dst_root should be given together') |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 138 | if (args.out_dst_kern is None) != (args.out_dst_root is None): |
| 139 | parser.error('--out_dst_kern and --out_dst_root should be given together') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 140 | |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 141 | if (args.dst_kern and args.dst_root) or \ |
| 142 | (args.out_dst_kern and args.out_dst_root): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 143 | if args.src_kern and args.src_root: |
| 144 | if args.assert_type == _TYPE_FULL: |
| 145 | parser.error('%s payload does not accept source partition arguments' |
| 146 | % _TYPE_FULL) |
| 147 | else: |
| 148 | args.assert_type = _TYPE_DELTA |
| 149 | else: |
| 150 | if args.assert_type == _TYPE_DELTA: |
| 151 | parser.error('%s payload requires source partitions arguments' |
| 152 | % _TYPE_DELTA) |
| 153 | else: |
| 154 | args.assert_type = _TYPE_FULL |
| 155 | else: |
Amin Hassani | a548902 | 2018-01-26 11:23:26 -0800 | [diff] [blame] | 156 | # Not applying payload. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 157 | if args.extract_bsdiff: |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 158 | parser.error('--extract-bsdiff can only be used when applying payloads') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 159 | if args.bspatch_path: |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame] | 160 | parser.error('--bspatch-path can only be used when applying payloads') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 161 | if args.puffpatch_path: |
Amin Hassani | 6be7168 | 2017-12-01 10:46:45 -0800 | [diff] [blame] | 162 | parser.error('--puffpatch-path can only be used when applying payloads') |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 163 | |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 164 | # 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] | 165 | # 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] | 166 | if args.check and not args.meta_sig: |
| 167 | default_meta_sig = args.payload + '.metadata-signature' |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 168 | if os.path.isfile(default_meta_sig): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 169 | args.meta_sig = default_meta_sig |
| 170 | print('Using default metadata signature', args.meta_sig, file=sys.stderr) |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 171 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 172 | return args |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def main(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 176 | # Parse and validate arguments. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 177 | args = ParseArguments(argv[1:]) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 178 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 179 | with open(args.payload) as payload_file: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 180 | payload = update_payload.Payload(payload_file) |
| 181 | try: |
| 182 | # Initialize payload. |
| 183 | payload.Init() |
| 184 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 185 | if args.describe: |
Don Garrett | 432d601 | 2013-05-10 15:01:36 -0700 | [diff] [blame] | 186 | payload.Describe() |
| 187 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 188 | # Perform payload integrity checks. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 189 | if args.check: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 190 | report_file = None |
| 191 | do_close_report_file = False |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 192 | metadata_sig_file = None |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 193 | try: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 194 | if args.report: |
| 195 | if args.report == '-': |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 196 | report_file = sys.stdout |
| 197 | else: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 198 | report_file = open(args.report, 'w') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 199 | do_close_report_file = True |
Gilad Arnold | 03959b7 | 2013-05-07 17:08:18 -0700 | [diff] [blame] | 200 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 201 | metadata_sig_file = args.meta_sig and open(args.meta_sig) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 202 | payload.Check( |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 203 | pubkey_file_name=args.key, |
Gilad Arnold | 4f8c17c | 2013-05-04 22:57:45 -0700 | [diff] [blame] | 204 | metadata_sig_file=metadata_sig_file, |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 205 | report_out_file=report_file, |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 206 | assert_type=args.assert_type, |
| 207 | block_size=int(args.block_size), |
| 208 | rootfs_part_size=args.root_part_size, |
| 209 | kernel_part_size=args.kern_part_size, |
| 210 | allow_unhashed=args.allow_unhashed, |
| 211 | disabled_tests=args.disabled_tests) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 212 | finally: |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 213 | if metadata_sig_file: |
| 214 | metadata_sig_file.close() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 215 | if do_close_report_file: |
| 216 | report_file.close() |
| 217 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 218 | # Apply payload. |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 219 | if (args.dst_root and args.dst_kern) or \ |
| 220 | (args.out_dst_root and args.out_dst_kern): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 221 | dargs = {'bsdiff_in_place': not args.extract_bsdiff} |
| 222 | if args.bspatch_path: |
| 223 | dargs['bspatch_path'] = args.bspatch_path |
| 224 | if args.puffpatch_path: |
| 225 | dargs['puffpatch_path'] = args.puffpatch_path |
| 226 | if args.assert_type == _TYPE_DELTA: |
| 227 | dargs['old_kernel_part'] = args.src_kern |
| 228 | dargs['old_rootfs_part'] = args.src_root |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 229 | |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 230 | if args.out_dst_kern and args.out_dst_root: |
| 231 | out_dst_kern = open(args.out_dst_kern, 'w+') |
| 232 | out_dst_root = open(args.out_dst_root, 'w+') |
| 233 | else: |
| 234 | out_dst_kern = tempfile.NamedTemporaryFile() |
| 235 | out_dst_root = tempfile.NamedTemporaryFile() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 236 | |
Amin Hassani | e20eb91 | 2018-02-21 12:39:42 -0800 | [diff] [blame^] | 237 | payload.Apply(out_dst_kern.name, out_dst_root.name, **dargs) |
| 238 | |
| 239 | # If destination kernel and rootfs partitions are not given, then this |
| 240 | # just becomes an apply operation with no check. |
| 241 | if args.dst_kern and args.dst_root: |
| 242 | # Prior to comparing, add the unused space past the filesystem |
| 243 | # boundary in the new target partitions to become the same size as |
| 244 | # the given partitions. This will truncate to larger size. |
| 245 | out_dst_kern.truncate(os.path.getsize(args.dst_kern)) |
| 246 | out_dst_root.truncate(os.path.getsize(args.dst_root)) |
| 247 | |
| 248 | # Compare resulting partitions with the ones from the target image. |
| 249 | if not filecmp.cmp(out_dst_kern.name, args.dst_kern): |
| 250 | raise error.PayloadError('Resulting kernel partition corrupted.') |
| 251 | if not filecmp.cmp(out_dst_root.name, args.dst_root): |
| 252 | raise error.PayloadError('Resulting rootfs partition corrupted.') |
| 253 | |
| 254 | # Close the output files. If args.out_dst_* was not given, then these |
| 255 | # files are created as temp files and will be deleted upon close(). |
| 256 | out_dst_kern.close() |
| 257 | out_dst_root.close() |
| 258 | |
| 259 | except error.PayloadError, e: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 260 | sys.stderr.write('Error: %s\n' % e) |
| 261 | return 1 |
| 262 | |
| 263 | return 0 |
| 264 | |
| 265 | |
| 266 | if __name__ == '__main__': |
| 267 | sys.exit(main(sys.argv)) |