blob: 80a5853b2fde70a2fb81f2d2c1d8ee3f8618b0fe [file] [log] [blame]
Gilad Arnold553b0ec2013-01-26 01:00:39 -08001#!/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
9import optparse
10import os
11import sys
12
13# pylint: disable=F0401
14lib_dir = os.path.join(os.path.dirname(__file__), 'lib')
15if os.path.exists(lib_dir) and os.path.isdir(lib_dir):
16 sys.path.insert(1, lib_dir)
17import update_payload
18
19
Gilad Arnold382df5c2013-05-03 12:49:28 -070020# 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 Arnold553b0ec2013-01-26 01:00:39 -080024_TYPE_FULL = 'full'
25_TYPE_DELTA = 'delta'
26
27
Gilad Arnold4fbe4092013-04-17 10:00:55 -070028def ParseArguments(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -080029 """Parse and validate command-line arguments.
30
31 Args:
Gilad Arnold4fbe4092013-04-17 10:00:55 -070032 argv: command-line arguments to parse (excluding the program name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080033 Returns:
Gilad Arnold4fbe4092013-04-17 10:00:55 -070034 A tuple (opts, payload, extra_args), where `opts' are the options
Gilad Arnold553b0ec2013-01-26 01:00:39 -080035 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 Arnold553b0ec2013-01-26 01:00:39 -080040 parser = optparse.OptionParser(
Gilad Arnold4fbe4092013-04-17 10:00:55 -070041 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 Arnold553b0ec2013-01-26 01:00:39 -080054
Gilad Arnold272a4992013-05-08 13:12:53 -070055 check_opts = optparse.OptionGroup(parser, 'Checking payload integrity')
Gilad Arnold553b0ec2013-01-26 01:00:39 -080056 check_opts.add_option('-c', '--check', action='store_true', default=False,
Gilad Arnold4fbe4092013-04-17 10:00:55 -070057 help=('force payload integrity check (e.g. before '
58 'applying)'))
Don Garrett432d6012013-05-10 15:01:36 -070059 check_opts.add_option('-D', '--describe', action='store_true', default=False,
60 help='Print a friendly description of the payload.')
Gilad Arnold553b0ec2013-01-26 01:00:39 -080061 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 Arnold4fbe4092013-04-17 10:00:55 -070064 help=("assert that payload is either `%s' or `%s'" %
65 (_TYPE_FULL, _TYPE_DELTA)))
Gilad Arnold553b0ec2013-01-26 01:00:39 -080066 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 Arnoldeaed0d12013-04-30 15:38:22 -070071 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 Arnold9b90c932013-05-22 17:12:56 -070076 check_opts.add_option('-k', '--key', metavar='FILE',
Gilad Arnold43116432013-05-07 17:12:50 -070077 help=('Override standard key used for signature '
78 'validation'))
Gilad Arnold553b0ec2013-01-26 01:00:39 -080079 check_opts.add_option('-m', '--meta-sig', metavar='FILE',
80 help='verify metadata against its signature')
Gilad Arnold382df5c2013-05-03 12:49:28 -070081 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 Arnold553b0ec2013-01-26 01:00:39 -080089 parser.add_option_group(check_opts)
90
Gilad Arnold272a4992013-05-08 13:12:53 -070091 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 Arnold553b0ec2013-01-26 01:00:39 -080098 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 Arnold4fbe4092013-04-17 10:00:55 -0700107 # 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 Arnoldeaed0d12013-04-30 15:38:22 -0700114 # 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 Arnold4fbe4092013-04-17 10:00:55 -0700121 # 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 Arnolde73dad92013-05-03 08:36:06 -0700128 opts.block_size or opts.allow_unhashed or
Gilad Arnold9b90c932013-05-22 17:12:56 -0700129 opts.disabled_tests or opts.meta_sig or opts.key or
Gilad Arnold382df5c2013-05-03 12:49:28 -0700130 opts.root_part_size != _DEFAULT_ROOTFS_PART_SIZE or
131 opts.kern_part_size != _DEFAULT_KERNEL_PART_SIZE)
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700132
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 Arnold272a4992013-05-08 13:12:53 -0700149 if opts.extract_bsdiff:
150 parser.error('--extract-bsdiff can only be used when applying payloads')
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700151 else:
152 parser.error('unexpected number of arguments')
153
Don Garrett30027fd2013-05-01 16:56:16 -0700154 # By default, look for a metadata-signature file with a name based on the name
Gilad Arnold9b90c932013-05-22 17:12:56 -0700155 # of the payload we are checking. We only do it if check was triggered.
156 if opts.check and not opts.meta_sig:
Don Garrett30027fd2013-05-01 16:56:16 -0700157 default_meta_sig = args[0] + '.metadata-signature'
158 if os.path.isfile(default_meta_sig):
159 opts.meta_sig = default_meta_sig
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700160 print >> sys.stderr, 'Using default metadata signature', opts.meta_sig
Don Garrett30027fd2013-05-01 16:56:16 -0700161
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700162 return opts, args[0], args[1:]
163
164
165def main(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800166 # Parse and validate arguments.
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700167 options, payload_file_name, extra_args = ParseArguments(argv[1:])
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800168
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 Garrett432d6012013-05-10 15:01:36 -0700175 if options.describe:
176 payload.Describe()
177
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800178 # 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 Arnold03959b72013-05-07 17:08:18 -0700189
190 metadata_sig_file = (
191 open(options.meta_sig) if options.meta_sig else None)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800192 payload.Check(
193 pubkey_file_name=options.key,
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700194 metadata_sig_file=metadata_sig_file,
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800195 report_out_file=report_file,
196 assert_type=options.assert_type,
197 block_size=int(options.block_size),
Gilad Arnold382df5c2013-05-03 12:49:28 -0700198 rootfs_part_size=options.root_part_size,
199 kernel_part_size=options.kern_part_size,
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700200 allow_unhashed=options.allow_unhashed,
201 disabled_tests=options.disabled_tests)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800202 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 Arnold272a4992013-05-08 13:12:53 -0700214 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 Arnold553b0ec2013-01-26 01:00:39 -0800220
221 except update_payload.PayloadError, e:
222 sys.stderr.write('Error: %s\n' % e)
223 return 1
224
225 return 0
226
227
228if __name__ == '__main__':
229 sys.exit(main(sys.argv))