Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Command-line tool for checking and applying Chrome OS update payloads.""" |
| 8 | |
| 9 | import optparse |
| 10 | import os |
| 11 | import sys |
| 12 | |
| 13 | # pylint: disable=F0401 |
| 14 | lib_dir = os.path.join(os.path.dirname(__file__), 'lib') |
| 15 | if os.path.exists(lib_dir) and os.path.isdir(lib_dir): |
| 16 | sys.path.insert(1, lib_dir) |
| 17 | import update_payload |
| 18 | |
| 19 | |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 20 | # The default sizes of partitions, based on current partitioning practice. |
Gilad Arnold | 0990f51 | 2013-05-30 17:09:31 -0700 | [diff] [blame] | 21 | # TODO(garnold)(chromium:243559) we should stop using these values once |
| 22 | # partition sizes are encoded in newly generated payloads; that said, we should |
| 23 | # allow users to specify partition sizes on the command-line, so as to be able |
| 24 | # to check older payloads. |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 25 | _DEFAULT_ROOTFS_PART_SIZE = 2 * 1024 * 1024 * 1024 |
| 26 | _DEFAULT_KERNEL_PART_SIZE = 16 * 1024 * 1024 |
| 27 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 28 | _TYPE_FULL = 'full' |
| 29 | _TYPE_DELTA = 'delta' |
| 30 | |
| 31 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 32 | def ParseArguments(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 33 | """Parse and validate command-line arguments. |
| 34 | |
| 35 | Args: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 36 | argv: command-line arguments to parse (excluding the program name) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 37 | Returns: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 38 | A tuple (opts, payload, extra_args), where `opts' are the options |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 39 | returned by the parser, `payload' is the name of the payload file |
| 40 | (mandatory argument) and `extra_args' are any additional command-line |
| 41 | arguments. |
| 42 | |
| 43 | """ |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 44 | parser = optparse.OptionParser( |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 45 | usage=('Usage: %prog [OPTION...] PAYLOAD [DST_KERN DST_ROOT ' |
| 46 | '[SRC_KERN SRC_ROOT]]'), |
| 47 | description=('Applies a Chrome OS update PAYLOAD to SRC_KERN and ' |
| 48 | 'SRC_ROOT emitting DST_KERN and DST_ROOT, respectively. ' |
| 49 | 'SRC_KERN and SRC_ROOT are only needed for delta payloads. ' |
| 50 | 'When no partitions are provided, verifies the payload ' |
| 51 | 'integrity.'), |
| 52 | epilog=('Note: a payload may verify correctly but fail to apply, and ' |
| 53 | 'vice versa; this is by design and can be thought of as static ' |
| 54 | 'vs dynamic correctness. A payload that both verifies and ' |
| 55 | 'applies correctly should be safe for use by the Chrome OS ' |
| 56 | 'Update Engine. Use --check to verify a payload prior to ' |
| 57 | 'applying it.')) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 58 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 59 | check_opts = optparse.OptionGroup(parser, 'Checking payload integrity') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 60 | check_opts.add_option('-c', '--check', action='store_true', default=False, |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 61 | help=('force payload integrity check (e.g. before ' |
| 62 | 'applying)')) |
Don Garrett | 432d601 | 2013-05-10 15:01:36 -0700 | [diff] [blame] | 63 | check_opts.add_option('-D', '--describe', action='store_true', default=False, |
| 64 | help='Print a friendly description of the payload.') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 65 | check_opts.add_option('-r', '--report', metavar='FILE', |
| 66 | help="dump payload report (`-' for stdout)") |
| 67 | check_opts.add_option('-t', '--type', metavar='TYPE', dest='assert_type', |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 68 | help=("assert that payload is either `%s' or `%s'" % |
| 69 | (_TYPE_FULL, _TYPE_DELTA))) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 70 | check_opts.add_option('-z', '--block-size', metavar='NUM', default=0, |
| 71 | type='int', |
| 72 | help='assert a non-default (4096) payload block size') |
| 73 | check_opts.add_option('-u', '--allow-unhashed', action='store_true', |
| 74 | default=False, help='allow unhashed operations') |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 75 | check_opts.add_option('-d', '--disabled_tests', metavar='TESTLIST', |
| 76 | default=(), |
| 77 | help=('comma-separated list of tests to disable; ' |
| 78 | 'available values: ' + |
| 79 | ', '.join(update_payload.CHECKS_TO_DISABLE))) |
Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 80 | check_opts.add_option('-k', '--key', metavar='FILE', |
Gilad Arnold | 4311643 | 2013-05-07 17:12:50 -0700 | [diff] [blame] | 81 | help=('Override standard key used for signature ' |
| 82 | 'validation')) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 83 | check_opts.add_option('-m', '--meta-sig', metavar='FILE', |
| 84 | help='verify metadata against its signature') |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 85 | check_opts.add_option('-p', '--root-part-size', metavar='NUM', |
| 86 | default=_DEFAULT_ROOTFS_PART_SIZE, type='int', |
| 87 | help=('override default (%default) rootfs partition ' |
| 88 | 'size')) |
| 89 | check_opts.add_option('-P', '--kern-part-size', metavar='NUM', |
| 90 | default=_DEFAULT_KERNEL_PART_SIZE, type='int', |
| 91 | help=('override default (%default) kernel partition ' |
| 92 | 'size')) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 93 | parser.add_option_group(check_opts) |
| 94 | |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 95 | trace_opts = optparse.OptionGroup(parser, 'Applying payload') |
| 96 | trace_opts.add_option('-x', '--extract-bsdiff', action='store_true', |
| 97 | default=False, |
| 98 | help=('use temp input/output files with BSDIFF ' |
| 99 | 'operations (not in-place)')) |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame^] | 100 | trace_opts.add_option('--bspatch-path', metavar='FILE', |
| 101 | help=('use the specified bspatch binary')) |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 102 | parser.add_option_group(trace_opts) |
| 103 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 104 | trace_opts = optparse.OptionGroup(parser, 'Block tracing') |
| 105 | trace_opts.add_option('-b', '--root-block', metavar='BLOCK', type='int', |
| 106 | help='trace the origin for a rootfs block') |
| 107 | trace_opts.add_option('-B', '--kern-block', metavar='BLOCK', type='int', |
| 108 | help='trace the origin for a kernel block') |
| 109 | trace_opts.add_option('-s', '--skip', metavar='NUM', default='0', type='int', |
| 110 | help='skip first NUM occurrences of traced block') |
| 111 | parser.add_option_group(trace_opts) |
| 112 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 113 | # Parse command-line arguments. |
| 114 | opts, args = parser.parse_args(argv) |
| 115 | |
| 116 | # Validate a value given to --type, if any. |
| 117 | if opts.assert_type not in (None, _TYPE_FULL, _TYPE_DELTA): |
| 118 | parser.error('invalid argument to --type: %s' % opts.assert_type) |
| 119 | |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 120 | # Convert and validate --disabled_tests value list, if provided. |
| 121 | if opts.disabled_tests: |
| 122 | opts.disabled_tests = opts.disabled_tests.split(',') |
| 123 | for test in opts.disabled_tests: |
| 124 | if test not in update_payload.CHECKS_TO_DISABLE: |
| 125 | parser.error('invalid argument to --disabled_tests: %s' % test) |
| 126 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 127 | # Ensure consistent use of block tracing options. |
| 128 | do_block_trace = opts.root_block or opts.kern_block |
| 129 | if opts.skip and not do_block_trace: |
| 130 | parser.error('--skip must be used with either --root-block or --kern-block') |
| 131 | |
| 132 | # There are several options that imply --check. |
| 133 | opts.check = (opts.check or opts.report or opts.assert_type or |
Gilad Arnold | e73dad9 | 2013-05-03 08:36:06 -0700 | [diff] [blame] | 134 | opts.block_size or opts.allow_unhashed or |
Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 135 | opts.disabled_tests or opts.meta_sig or opts.key or |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 136 | opts.root_part_size != _DEFAULT_ROOTFS_PART_SIZE or |
| 137 | opts.kern_part_size != _DEFAULT_KERNEL_PART_SIZE) |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 138 | |
| 139 | # Check number of arguments, enforce payload type accordingly. |
| 140 | if len(args) == 3: |
| 141 | if opts.assert_type == _TYPE_DELTA: |
| 142 | parser.error('%s payload requires source partition arguments' % |
| 143 | _TYPE_DELTA) |
| 144 | opts.assert_type = _TYPE_FULL |
| 145 | elif len(args) == 5: |
| 146 | if opts.assert_type == _TYPE_FULL: |
| 147 | parser.error('%s payload does not accept source partition arguments' % |
| 148 | _TYPE_FULL) |
| 149 | opts.assert_type = _TYPE_DELTA |
| 150 | elif len(args) == 1: |
| 151 | # Not applying payload; if block tracing not requested either, do an |
| 152 | # integrity check. |
| 153 | if not do_block_trace: |
| 154 | opts.check = True |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 155 | if opts.extract_bsdiff: |
| 156 | parser.error('--extract-bsdiff can only be used when applying payloads') |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame^] | 157 | if opts.bspatch_path: |
| 158 | parser.error('--bspatch-path can only be used when applying payloads') |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 159 | else: |
| 160 | parser.error('unexpected number of arguments') |
| 161 | |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 162 | # 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] | 163 | # of the payload we are checking. We only do it if check was triggered. |
| 164 | if opts.check and not opts.meta_sig: |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 165 | default_meta_sig = args[0] + '.metadata-signature' |
| 166 | if os.path.isfile(default_meta_sig): |
| 167 | opts.meta_sig = default_meta_sig |
Gilad Arnold | 4f8c17c | 2013-05-04 22:57:45 -0700 | [diff] [blame] | 168 | print >> sys.stderr, 'Using default metadata signature', opts.meta_sig |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 169 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 170 | return opts, args[0], args[1:] |
| 171 | |
| 172 | |
| 173 | def main(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 174 | # Parse and validate arguments. |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 175 | options, payload_file_name, extra_args = ParseArguments(argv[1:]) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 176 | |
| 177 | with open(payload_file_name) as payload_file: |
| 178 | payload = update_payload.Payload(payload_file) |
| 179 | try: |
| 180 | # Initialize payload. |
| 181 | payload.Init() |
| 182 | |
Don Garrett | 432d601 | 2013-05-10 15:01:36 -0700 | [diff] [blame] | 183 | if options.describe: |
| 184 | payload.Describe() |
| 185 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 186 | # Perform payload integrity checks. |
| 187 | if options.check: |
| 188 | report_file = None |
| 189 | do_close_report_file = False |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 190 | metadata_sig_file = None |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 191 | try: |
| 192 | if options.report: |
| 193 | if options.report == '-': |
| 194 | report_file = sys.stdout |
| 195 | else: |
| 196 | report_file = open(options.report, 'w') |
| 197 | do_close_report_file = True |
Gilad Arnold | 03959b7 | 2013-05-07 17:08:18 -0700 | [diff] [blame] | 198 | |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 199 | metadata_sig_file = options.meta_sig and open(options.meta_sig) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 200 | payload.Check( |
| 201 | pubkey_file_name=options.key, |
Gilad Arnold | 4f8c17c | 2013-05-04 22:57:45 -0700 | [diff] [blame] | 202 | metadata_sig_file=metadata_sig_file, |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 203 | report_out_file=report_file, |
| 204 | assert_type=options.assert_type, |
| 205 | block_size=int(options.block_size), |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 206 | rootfs_part_size=options.root_part_size, |
| 207 | kernel_part_size=options.kern_part_size, |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 208 | allow_unhashed=options.allow_unhashed, |
| 209 | disabled_tests=options.disabled_tests) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 210 | finally: |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 211 | if metadata_sig_file: |
| 212 | metadata_sig_file.close() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 213 | if do_close_report_file: |
| 214 | report_file.close() |
| 215 | |
| 216 | # Trace blocks. |
| 217 | if options.root_block is not None: |
| 218 | payload.TraceBlock(options.root_block, options.skip, sys.stdout, False) |
| 219 | if options.kern_block is not None: |
| 220 | payload.TraceBlock(options.kern_block, options.skip, sys.stdout, True) |
| 221 | |
| 222 | # Apply payload. |
| 223 | if extra_args: |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 224 | dargs = {'bsdiff_in_place': not options.extract_bsdiff} |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame^] | 225 | if options.bspatch_path: |
| 226 | dargs['bspatch_path'] = options.bspatch_path |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 227 | if options.assert_type == _TYPE_DELTA: |
| 228 | dargs['old_kernel_part'] = extra_args[2] |
| 229 | dargs['old_rootfs_part'] = extra_args[3] |
| 230 | |
| 231 | payload.Apply(extra_args[0], extra_args[1], **dargs) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 232 | |
| 233 | except update_payload.PayloadError, e: |
| 234 | sys.stderr.write('Error: %s\n' % e) |
| 235 | return 1 |
| 236 | |
| 237 | return 0 |
| 238 | |
| 239 | |
| 240 | if __name__ == '__main__': |
| 241 | sys.exit(main(sys.argv)) |