blob: e199a7c83359a5b5fcb63663e56967a6917e7a95 [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.
Gilad Arnold0990f512013-05-30 17:09:31 -070021# 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 Arnold382df5c2013-05-03 12:49:28 -070025_DEFAULT_ROOTFS_PART_SIZE = 2 * 1024 * 1024 * 1024
26_DEFAULT_KERNEL_PART_SIZE = 16 * 1024 * 1024
27
Gilad Arnold553b0ec2013-01-26 01:00:39 -080028_TYPE_FULL = 'full'
29_TYPE_DELTA = 'delta'
30
31
Gilad Arnold4fbe4092013-04-17 10:00:55 -070032def ParseArguments(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -080033 """Parse and validate command-line arguments.
34
35 Args:
Gilad Arnold4fbe4092013-04-17 10:00:55 -070036 argv: command-line arguments to parse (excluding the program name)
Gilad Arnold553b0ec2013-01-26 01:00:39 -080037 Returns:
Gilad Arnold4fbe4092013-04-17 10:00:55 -070038 A tuple (opts, payload, extra_args), where `opts' are the options
Gilad Arnold553b0ec2013-01-26 01:00:39 -080039 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 Arnold553b0ec2013-01-26 01:00:39 -080044 parser = optparse.OptionParser(
Gilad Arnold4fbe4092013-04-17 10:00:55 -070045 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 Arnold553b0ec2013-01-26 01:00:39 -080058
Gilad Arnold272a4992013-05-08 13:12:53 -070059 check_opts = optparse.OptionGroup(parser, 'Checking payload integrity')
Gilad Arnold553b0ec2013-01-26 01:00:39 -080060 check_opts.add_option('-c', '--check', action='store_true', default=False,
Gilad Arnold4fbe4092013-04-17 10:00:55 -070061 help=('force payload integrity check (e.g. before '
62 'applying)'))
Don Garrett432d6012013-05-10 15:01:36 -070063 check_opts.add_option('-D', '--describe', action='store_true', default=False,
64 help='Print a friendly description of the payload.')
Gilad Arnold553b0ec2013-01-26 01:00:39 -080065 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 Arnold4fbe4092013-04-17 10:00:55 -070068 help=("assert that payload is either `%s' or `%s'" %
69 (_TYPE_FULL, _TYPE_DELTA)))
Gilad Arnold553b0ec2013-01-26 01:00:39 -080070 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 Arnoldeaed0d12013-04-30 15:38:22 -070075 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 Arnold9b90c932013-05-22 17:12:56 -070080 check_opts.add_option('-k', '--key', metavar='FILE',
Gilad Arnold43116432013-05-07 17:12:50 -070081 help=('Override standard key used for signature '
82 'validation'))
Gilad Arnold553b0ec2013-01-26 01:00:39 -080083 check_opts.add_option('-m', '--meta-sig', metavar='FILE',
84 help='verify metadata against its signature')
Gilad Arnold382df5c2013-05-03 12:49:28 -070085 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 Arnold553b0ec2013-01-26 01:00:39 -080093 parser.add_option_group(check_opts)
94
Gilad Arnold272a4992013-05-08 13:12:53 -070095 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)'))
100 parser.add_option_group(trace_opts)
101
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800102 trace_opts = optparse.OptionGroup(parser, 'Block tracing')
103 trace_opts.add_option('-b', '--root-block', metavar='BLOCK', type='int',
104 help='trace the origin for a rootfs block')
105 trace_opts.add_option('-B', '--kern-block', metavar='BLOCK', type='int',
106 help='trace the origin for a kernel block')
107 trace_opts.add_option('-s', '--skip', metavar='NUM', default='0', type='int',
108 help='skip first NUM occurrences of traced block')
109 parser.add_option_group(trace_opts)
110
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700111 # Parse command-line arguments.
112 opts, args = parser.parse_args(argv)
113
114 # Validate a value given to --type, if any.
115 if opts.assert_type not in (None, _TYPE_FULL, _TYPE_DELTA):
116 parser.error('invalid argument to --type: %s' % opts.assert_type)
117
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700118 # Convert and validate --disabled_tests value list, if provided.
119 if opts.disabled_tests:
120 opts.disabled_tests = opts.disabled_tests.split(',')
121 for test in opts.disabled_tests:
122 if test not in update_payload.CHECKS_TO_DISABLE:
123 parser.error('invalid argument to --disabled_tests: %s' % test)
124
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700125 # Ensure consistent use of block tracing options.
126 do_block_trace = opts.root_block or opts.kern_block
127 if opts.skip and not do_block_trace:
128 parser.error('--skip must be used with either --root-block or --kern-block')
129
130 # There are several options that imply --check.
131 opts.check = (opts.check or opts.report or opts.assert_type or
Gilad Arnolde73dad92013-05-03 08:36:06 -0700132 opts.block_size or opts.allow_unhashed or
Gilad Arnold9b90c932013-05-22 17:12:56 -0700133 opts.disabled_tests or opts.meta_sig or opts.key or
Gilad Arnold382df5c2013-05-03 12:49:28 -0700134 opts.root_part_size != _DEFAULT_ROOTFS_PART_SIZE or
135 opts.kern_part_size != _DEFAULT_KERNEL_PART_SIZE)
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700136
137 # Check number of arguments, enforce payload type accordingly.
138 if len(args) == 3:
139 if opts.assert_type == _TYPE_DELTA:
140 parser.error('%s payload requires source partition arguments' %
141 _TYPE_DELTA)
142 opts.assert_type = _TYPE_FULL
143 elif len(args) == 5:
144 if opts.assert_type == _TYPE_FULL:
145 parser.error('%s payload does not accept source partition arguments' %
146 _TYPE_FULL)
147 opts.assert_type = _TYPE_DELTA
148 elif len(args) == 1:
149 # Not applying payload; if block tracing not requested either, do an
150 # integrity check.
151 if not do_block_trace:
152 opts.check = True
Gilad Arnold272a4992013-05-08 13:12:53 -0700153 if opts.extract_bsdiff:
154 parser.error('--extract-bsdiff can only be used when applying payloads')
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700155 else:
156 parser.error('unexpected number of arguments')
157
Don Garrett30027fd2013-05-01 16:56:16 -0700158 # By default, look for a metadata-signature file with a name based on the name
Gilad Arnold9b90c932013-05-22 17:12:56 -0700159 # of the payload we are checking. We only do it if check was triggered.
160 if opts.check and not opts.meta_sig:
Don Garrett30027fd2013-05-01 16:56:16 -0700161 default_meta_sig = args[0] + '.metadata-signature'
162 if os.path.isfile(default_meta_sig):
163 opts.meta_sig = default_meta_sig
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700164 print >> sys.stderr, 'Using default metadata signature', opts.meta_sig
Don Garrett30027fd2013-05-01 16:56:16 -0700165
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700166 return opts, args[0], args[1:]
167
168
169def main(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800170 # Parse and validate arguments.
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700171 options, payload_file_name, extra_args = ParseArguments(argv[1:])
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800172
173 with open(payload_file_name) as payload_file:
174 payload = update_payload.Payload(payload_file)
175 try:
176 # Initialize payload.
177 payload.Init()
178
Don Garrett432d6012013-05-10 15:01:36 -0700179 if options.describe:
180 payload.Describe()
181
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800182 # Perform payload integrity checks.
183 if options.check:
184 report_file = None
185 do_close_report_file = False
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700186 metadata_sig_file = None
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800187 try:
188 if options.report:
189 if options.report == '-':
190 report_file = sys.stdout
191 else:
192 report_file = open(options.report, 'w')
193 do_close_report_file = True
Gilad Arnold03959b72013-05-07 17:08:18 -0700194
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700195 metadata_sig_file = options.meta_sig and open(options.meta_sig)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800196 payload.Check(
197 pubkey_file_name=options.key,
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700198 metadata_sig_file=metadata_sig_file,
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800199 report_out_file=report_file,
200 assert_type=options.assert_type,
201 block_size=int(options.block_size),
Gilad Arnold382df5c2013-05-03 12:49:28 -0700202 rootfs_part_size=options.root_part_size,
203 kernel_part_size=options.kern_part_size,
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700204 allow_unhashed=options.allow_unhashed,
205 disabled_tests=options.disabled_tests)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800206 finally:
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700207 if metadata_sig_file:
208 metadata_sig_file.close()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800209 if do_close_report_file:
210 report_file.close()
211
212 # Trace blocks.
213 if options.root_block is not None:
214 payload.TraceBlock(options.root_block, options.skip, sys.stdout, False)
215 if options.kern_block is not None:
216 payload.TraceBlock(options.kern_block, options.skip, sys.stdout, True)
217
218 # Apply payload.
219 if extra_args:
Gilad Arnold272a4992013-05-08 13:12:53 -0700220 dargs = {'bsdiff_in_place': not options.extract_bsdiff}
221 if options.assert_type == _TYPE_DELTA:
222 dargs['old_kernel_part'] = extra_args[2]
223 dargs['old_rootfs_part'] = extra_args[3]
224
225 payload.Apply(extra_args[0], extra_args[1], **dargs)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800226
227 except update_payload.PayloadError, e:
228 sys.stderr.write('Error: %s\n' % e)
229 return 1
230
231 return 0
232
233
234if __name__ == '__main__':
235 sys.exit(main(sys.argv))