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 | |
| 20 | _TYPE_FULL = 'full' |
| 21 | _TYPE_DELTA = 'delta' |
| 22 | |
| 23 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 24 | def ParseArguments(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 25 | """Parse and validate command-line arguments. |
| 26 | |
| 27 | Args: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 28 | argv: command-line arguments to parse (excluding the program name) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 29 | Returns: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 30 | A tuple (opts, payload, extra_args), where `opts' are the options |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 31 | returned by the parser, `payload' is the name of the payload file |
| 32 | (mandatory argument) and `extra_args' are any additional command-line |
| 33 | arguments. |
| 34 | |
| 35 | """ |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 36 | parser = optparse.OptionParser( |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 37 | usage=('Usage: %prog [OPTION...] PAYLOAD [DST_KERN DST_ROOT ' |
| 38 | '[SRC_KERN SRC_ROOT]]'), |
| 39 | description=('Applies a Chrome OS update PAYLOAD to SRC_KERN and ' |
| 40 | 'SRC_ROOT emitting DST_KERN and DST_ROOT, respectively. ' |
| 41 | 'SRC_KERN and SRC_ROOT are only needed for delta payloads. ' |
| 42 | 'When no partitions are provided, verifies the payload ' |
| 43 | 'integrity.'), |
| 44 | epilog=('Note: a payload may verify correctly but fail to apply, and ' |
| 45 | 'vice versa; this is by design and can be thought of as static ' |
| 46 | 'vs dynamic correctness. A payload that both verifies and ' |
| 47 | 'applies correctly should be safe for use by the Chrome OS ' |
| 48 | 'Update Engine. Use --check to verify a payload prior to ' |
| 49 | 'applying it.')) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 50 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 51 | check_opts = optparse.OptionGroup(parser, 'Payload integrity checking') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 52 | check_opts.add_option('-c', '--check', action='store_true', default=False, |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 53 | help=('force payload integrity check (e.g. before ' |
| 54 | 'applying)')) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 55 | check_opts.add_option('-r', '--report', metavar='FILE', |
| 56 | help="dump payload report (`-' for stdout)") |
| 57 | check_opts.add_option('-t', '--type', metavar='TYPE', dest='assert_type', |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 58 | help=("assert that payload is either `%s' or `%s'" % |
| 59 | (_TYPE_FULL, _TYPE_DELTA))) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 60 | check_opts.add_option('-z', '--block-size', metavar='NUM', default=0, |
| 61 | type='int', |
| 62 | help='assert a non-default (4096) payload block size') |
| 63 | check_opts.add_option('-u', '--allow-unhashed', action='store_true', |
| 64 | default=False, help='allow unhashed operations') |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame^] | 65 | check_opts.add_option('-d', '--disabled_tests', metavar='TESTLIST', |
| 66 | default=(), |
| 67 | help=('comma-separated list of tests to disable; ' |
| 68 | 'available values: ' + |
| 69 | ', '.join(update_payload.CHECKS_TO_DISABLE))) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 70 | check_opts.add_option('-k', '--key', metavar='FILE', |
| 71 | help='public key to be used for signature verification') |
| 72 | check_opts.add_option('-m', '--meta-sig', metavar='FILE', |
| 73 | help='verify metadata against its signature') |
| 74 | parser.add_option_group(check_opts) |
| 75 | |
| 76 | trace_opts = optparse.OptionGroup(parser, 'Block tracing') |
| 77 | trace_opts.add_option('-b', '--root-block', metavar='BLOCK', type='int', |
| 78 | help='trace the origin for a rootfs block') |
| 79 | trace_opts.add_option('-B', '--kern-block', metavar='BLOCK', type='int', |
| 80 | help='trace the origin for a kernel block') |
| 81 | trace_opts.add_option('-s', '--skip', metavar='NUM', default='0', type='int', |
| 82 | help='skip first NUM occurrences of traced block') |
| 83 | parser.add_option_group(trace_opts) |
| 84 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 85 | # Parse command-line arguments. |
| 86 | opts, args = parser.parse_args(argv) |
| 87 | |
| 88 | # Validate a value given to --type, if any. |
| 89 | if opts.assert_type not in (None, _TYPE_FULL, _TYPE_DELTA): |
| 90 | parser.error('invalid argument to --type: %s' % opts.assert_type) |
| 91 | |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame^] | 92 | # Convert and validate --disabled_tests value list, if provided. |
| 93 | if opts.disabled_tests: |
| 94 | opts.disabled_tests = opts.disabled_tests.split(',') |
| 95 | for test in opts.disabled_tests: |
| 96 | if test not in update_payload.CHECKS_TO_DISABLE: |
| 97 | parser.error('invalid argument to --disabled_tests: %s' % test) |
| 98 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 99 | # Ensure consistent use of block tracing options. |
| 100 | do_block_trace = opts.root_block or opts.kern_block |
| 101 | if opts.skip and not do_block_trace: |
| 102 | parser.error('--skip must be used with either --root-block or --kern-block') |
| 103 | |
| 104 | # There are several options that imply --check. |
| 105 | opts.check = (opts.check or opts.report or opts.assert_type or |
| 106 | opts.block_size or opts.allow_unhashed or opts.key or |
| 107 | opts.meta_sig) |
| 108 | |
| 109 | # Check number of arguments, enforce payload type accordingly. |
| 110 | if len(args) == 3: |
| 111 | if opts.assert_type == _TYPE_DELTA: |
| 112 | parser.error('%s payload requires source partition arguments' % |
| 113 | _TYPE_DELTA) |
| 114 | opts.assert_type = _TYPE_FULL |
| 115 | elif len(args) == 5: |
| 116 | if opts.assert_type == _TYPE_FULL: |
| 117 | parser.error('%s payload does not accept source partition arguments' % |
| 118 | _TYPE_FULL) |
| 119 | opts.assert_type = _TYPE_DELTA |
| 120 | elif len(args) == 1: |
| 121 | # Not applying payload; if block tracing not requested either, do an |
| 122 | # integrity check. |
| 123 | if not do_block_trace: |
| 124 | opts.check = True |
| 125 | else: |
| 126 | parser.error('unexpected number of arguments') |
| 127 | |
| 128 | return opts, args[0], args[1:] |
| 129 | |
| 130 | |
| 131 | def main(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 132 | # Parse and validate arguments. |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 133 | options, payload_file_name, extra_args = ParseArguments(argv[1:]) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 134 | |
| 135 | with open(payload_file_name) as payload_file: |
| 136 | payload = update_payload.Payload(payload_file) |
| 137 | try: |
| 138 | # Initialize payload. |
| 139 | payload.Init() |
| 140 | |
| 141 | # Perform payload integrity checks. |
| 142 | if options.check: |
| 143 | report_file = None |
| 144 | do_close_report_file = False |
| 145 | try: |
| 146 | if options.report: |
| 147 | if options.report == '-': |
| 148 | report_file = sys.stdout |
| 149 | else: |
| 150 | report_file = open(options.report, 'w') |
| 151 | do_close_report_file = True |
| 152 | |
| 153 | payload.Check( |
| 154 | pubkey_file_name=options.key, |
| 155 | metadata_sig_file=open(options.meta_sig) |
| 156 | if options.meta_sig else None, |
| 157 | report_out_file=report_file, |
| 158 | assert_type=options.assert_type, |
| 159 | block_size=int(options.block_size), |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame^] | 160 | allow_unhashed=options.allow_unhashed, |
| 161 | disabled_tests=options.disabled_tests) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 162 | finally: |
| 163 | if do_close_report_file: |
| 164 | report_file.close() |
| 165 | |
| 166 | # Trace blocks. |
| 167 | if options.root_block is not None: |
| 168 | payload.TraceBlock(options.root_block, options.skip, sys.stdout, False) |
| 169 | if options.kern_block is not None: |
| 170 | payload.TraceBlock(options.kern_block, options.skip, sys.stdout, True) |
| 171 | |
| 172 | # Apply payload. |
| 173 | if extra_args: |
| 174 | if options.assert_type == _TYPE_FULL: |
| 175 | payload.Apply(extra_args[0], extra_args[1]) |
| 176 | elif options.assert_type == _TYPE_DELTA: |
| 177 | payload.Apply(extra_args[0], extra_args[1], |
| 178 | src_kernel_part=extra_args[2], |
| 179 | src_rootfs_part=extra_args[3]) |
| 180 | else: |
| 181 | assert False, 'cannot get here' |
| 182 | |
| 183 | except update_payload.PayloadError, e: |
| 184 | sys.stderr.write('Error: %s\n' % e) |
| 185 | return 1 |
| 186 | |
| 187 | return 0 |
| 188 | |
| 189 | |
| 190 | if __name__ == '__main__': |
| 191 | sys.exit(main(sys.argv)) |