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 | |
| 24 | def ParseArguments(parser, argv): |
| 25 | """Parse and validate command-line arguments. |
| 26 | |
| 27 | Args: |
| 28 | parser: the command-line parser |
| 29 | Returns: |
| 30 | A tuple (options, payload, extra_args), where `options' are the options |
| 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 | """ |
| 36 | options, args = parser.parse_args(argv) |
| 37 | |
| 38 | # Validate a value given to --type, if any. |
| 39 | if options.assert_type not in (None, _TYPE_FULL, _TYPE_DELTA): |
| 40 | parser.error('invalid argument to --type: %s' % options.assert_type) |
| 41 | |
| 42 | # There are several options that imply --check. |
| 43 | options.check = (options.check or options.report or options.assert_type or |
| 44 | options.block_size or options.allow_unhashed or |
| 45 | options.key or options.meta_sig) |
| 46 | |
| 47 | # Check number of arguments, enforce payload type accordingly. |
| 48 | if len(args) == 3: |
| 49 | if options.assert_type == _TYPE_DELTA: |
| 50 | parser.error('%s payload requires source partition arguments' % |
| 51 | _TYPE_DELTA) |
| 52 | options.assert_type = _TYPE_FULL |
| 53 | elif len(args) == 5: |
| 54 | if options.assert_type == _TYPE_FULL: |
| 55 | parser.error('%s payload does not accept source partition arguments' % |
| 56 | _TYPE_FULL) |
| 57 | options.assert_type = _TYPE_DELTA |
| 58 | elif len(args) != 1: |
| 59 | parser.error('unexpected number of arguments') |
| 60 | |
| 61 | return options, args[0], args[1:] |
| 62 | |
| 63 | |
| 64 | def main(argv): |
| 65 | parser = optparse.OptionParser( |
| 66 | usage='Usage: %prog [OPTION...] PAYLOAD [DST_KERN DST_ROOT ' |
| 67 | '[SRC_KERN SRC_ROOT]]', |
| 68 | description='Applies a Chrome OS update PAYLOAD to SRC_KERN and ' |
| 69 | 'SRC_ROOT emitting DST_KERN and DST_ROOT, respectively. ' |
| 70 | 'SRC_KERN and SRC_ROOT need only be provided for delta ' |
| 71 | 'payloads. If no partitions are provided, only verifies ' |
| 72 | 'payload integrity.') |
| 73 | |
| 74 | check_opts = optparse.OptionGroup(parser, 'Payload checking') |
| 75 | check_opts.add_option('-c', '--check', action='store_true', default=False, |
| 76 | help='check payload integrity') |
| 77 | check_opts.add_option('-r', '--report', metavar='FILE', |
| 78 | help="dump payload report (`-' for stdout)") |
| 79 | check_opts.add_option('-t', '--type', metavar='TYPE', dest='assert_type', |
| 80 | help="assert that payload is either `%s' or `%s'" % |
| 81 | (_TYPE_FULL, _TYPE_DELTA)) |
| 82 | check_opts.add_option('-z', '--block-size', metavar='NUM', default=0, |
| 83 | type='int', |
| 84 | help='assert a non-default (4096) payload block size') |
| 85 | check_opts.add_option('-u', '--allow-unhashed', action='store_true', |
| 86 | default=False, help='allow unhashed operations') |
| 87 | check_opts.add_option('-k', '--key', metavar='FILE', |
| 88 | help='public key to be used for signature verification') |
| 89 | check_opts.add_option('-m', '--meta-sig', metavar='FILE', |
| 90 | help='verify metadata against its signature') |
| 91 | parser.add_option_group(check_opts) |
| 92 | |
| 93 | trace_opts = optparse.OptionGroup(parser, 'Block tracing') |
| 94 | trace_opts.add_option('-b', '--root-block', metavar='BLOCK', type='int', |
| 95 | help='trace the origin for a rootfs block') |
| 96 | trace_opts.add_option('-B', '--kern-block', metavar='BLOCK', type='int', |
| 97 | help='trace the origin for a kernel block') |
| 98 | trace_opts.add_option('-s', '--skip', metavar='NUM', default='0', type='int', |
| 99 | help='skip first NUM occurrences of traced block') |
| 100 | parser.add_option_group(trace_opts) |
| 101 | |
| 102 | # Parse and validate arguments. |
| 103 | options, payload_file_name, extra_args = ParseArguments(parser, argv[1:]) |
| 104 | |
| 105 | with open(payload_file_name) as payload_file: |
| 106 | payload = update_payload.Payload(payload_file) |
| 107 | try: |
| 108 | # Initialize payload. |
| 109 | payload.Init() |
| 110 | |
| 111 | # Perform payload integrity checks. |
| 112 | if options.check: |
| 113 | report_file = None |
| 114 | do_close_report_file = False |
| 115 | try: |
| 116 | if options.report: |
| 117 | if options.report == '-': |
| 118 | report_file = sys.stdout |
| 119 | else: |
| 120 | report_file = open(options.report, 'w') |
| 121 | do_close_report_file = True |
| 122 | |
| 123 | payload.Check( |
| 124 | pubkey_file_name=options.key, |
| 125 | metadata_sig_file=open(options.meta_sig) |
| 126 | if options.meta_sig else None, |
| 127 | report_out_file=report_file, |
| 128 | assert_type=options.assert_type, |
| 129 | block_size=int(options.block_size), |
| 130 | allow_unhashed=options.allow_unhashed) |
| 131 | finally: |
| 132 | if do_close_report_file: |
| 133 | report_file.close() |
| 134 | |
| 135 | # Trace blocks. |
| 136 | if options.root_block is not None: |
| 137 | payload.TraceBlock(options.root_block, options.skip, sys.stdout, False) |
| 138 | if options.kern_block is not None: |
| 139 | payload.TraceBlock(options.kern_block, options.skip, sys.stdout, True) |
| 140 | |
| 141 | # Apply payload. |
| 142 | if extra_args: |
| 143 | if options.assert_type == _TYPE_FULL: |
| 144 | payload.Apply(extra_args[0], extra_args[1]) |
| 145 | elif options.assert_type == _TYPE_DELTA: |
| 146 | payload.Apply(extra_args[0], extra_args[1], |
| 147 | src_kernel_part=extra_args[2], |
| 148 | src_rootfs_part=extra_args[3]) |
| 149 | else: |
| 150 | assert False, 'cannot get here' |
| 151 | |
| 152 | except update_payload.PayloadError, e: |
| 153 | sys.stderr.write('Error: %s\n' % e) |
| 154 | return 1 |
| 155 | |
| 156 | return 0 |
| 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | sys.exit(main(sys.argv)) |