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