blob: 380809e78c8f6afab76b5e0fbfde9ea54c09f9fa [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)'))
Gilad Arnold21a02502013-08-22 16:59:48 -0700100 trace_opts.add_option('--bspatch-path', metavar='FILE',
101 help=('use the specified bspatch binary'))
Gilad Arnold272a4992013-05-08 13:12:53 -0700102 parser.add_option_group(trace_opts)
103
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800104 trace_opts = optparse.OptionGroup(parser, 'Block tracing')
105 trace_opts.add_option('-b', '--root-block', metavar='BLOCK', type='int',
106 help='trace the origin for a rootfs block')
107 trace_opts.add_option('-B', '--kern-block', metavar='BLOCK', type='int',
108 help='trace the origin for a kernel block')
109 trace_opts.add_option('-s', '--skip', metavar='NUM', default='0', type='int',
110 help='skip first NUM occurrences of traced block')
111 parser.add_option_group(trace_opts)
112
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700113 # Parse command-line arguments.
114 opts, args = parser.parse_args(argv)
115
116 # Validate a value given to --type, if any.
117 if opts.assert_type not in (None, _TYPE_FULL, _TYPE_DELTA):
118 parser.error('invalid argument to --type: %s' % opts.assert_type)
119
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700120 # Convert and validate --disabled_tests value list, if provided.
121 if opts.disabled_tests:
122 opts.disabled_tests = opts.disabled_tests.split(',')
123 for test in opts.disabled_tests:
124 if test not in update_payload.CHECKS_TO_DISABLE:
125 parser.error('invalid argument to --disabled_tests: %s' % test)
126
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700127 # Ensure consistent use of block tracing options.
128 do_block_trace = opts.root_block or opts.kern_block
129 if opts.skip and not do_block_trace:
130 parser.error('--skip must be used with either --root-block or --kern-block')
131
132 # There are several options that imply --check.
133 opts.check = (opts.check or opts.report or opts.assert_type or
Gilad Arnolde73dad92013-05-03 08:36:06 -0700134 opts.block_size or opts.allow_unhashed or
Gilad Arnold9b90c932013-05-22 17:12:56 -0700135 opts.disabled_tests or opts.meta_sig or opts.key or
Gilad Arnold382df5c2013-05-03 12:49:28 -0700136 opts.root_part_size != _DEFAULT_ROOTFS_PART_SIZE or
137 opts.kern_part_size != _DEFAULT_KERNEL_PART_SIZE)
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700138
139 # Check number of arguments, enforce payload type accordingly.
140 if len(args) == 3:
141 if opts.assert_type == _TYPE_DELTA:
142 parser.error('%s payload requires source partition arguments' %
143 _TYPE_DELTA)
144 opts.assert_type = _TYPE_FULL
145 elif len(args) == 5:
146 if opts.assert_type == _TYPE_FULL:
147 parser.error('%s payload does not accept source partition arguments' %
148 _TYPE_FULL)
149 opts.assert_type = _TYPE_DELTA
150 elif len(args) == 1:
151 # Not applying payload; if block tracing not requested either, do an
152 # integrity check.
153 if not do_block_trace:
154 opts.check = True
Gilad Arnold272a4992013-05-08 13:12:53 -0700155 if opts.extract_bsdiff:
156 parser.error('--extract-bsdiff can only be used when applying payloads')
Gilad Arnold21a02502013-08-22 16:59:48 -0700157 if opts.bspatch_path:
158 parser.error('--bspatch-path can only be used when applying payloads')
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700159 else:
160 parser.error('unexpected number of arguments')
161
Don Garrett30027fd2013-05-01 16:56:16 -0700162 # By default, look for a metadata-signature file with a name based on the name
Gilad Arnold9b90c932013-05-22 17:12:56 -0700163 # of the payload we are checking. We only do it if check was triggered.
164 if opts.check and not opts.meta_sig:
Don Garrett30027fd2013-05-01 16:56:16 -0700165 default_meta_sig = args[0] + '.metadata-signature'
166 if os.path.isfile(default_meta_sig):
167 opts.meta_sig = default_meta_sig
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700168 print >> sys.stderr, 'Using default metadata signature', opts.meta_sig
Don Garrett30027fd2013-05-01 16:56:16 -0700169
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700170 return opts, args[0], args[1:]
171
172
173def main(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800174 # Parse and validate arguments.
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700175 options, payload_file_name, extra_args = ParseArguments(argv[1:])
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800176
177 with open(payload_file_name) as payload_file:
178 payload = update_payload.Payload(payload_file)
179 try:
180 # Initialize payload.
181 payload.Init()
182
Don Garrett432d6012013-05-10 15:01:36 -0700183 if options.describe:
184 payload.Describe()
185
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800186 # Perform payload integrity checks.
187 if options.check:
188 report_file = None
189 do_close_report_file = False
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700190 metadata_sig_file = None
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800191 try:
192 if options.report:
193 if options.report == '-':
194 report_file = sys.stdout
195 else:
196 report_file = open(options.report, 'w')
197 do_close_report_file = True
Gilad Arnold03959b72013-05-07 17:08:18 -0700198
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700199 metadata_sig_file = options.meta_sig and open(options.meta_sig)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800200 payload.Check(
201 pubkey_file_name=options.key,
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700202 metadata_sig_file=metadata_sig_file,
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800203 report_out_file=report_file,
204 assert_type=options.assert_type,
205 block_size=int(options.block_size),
Gilad Arnold382df5c2013-05-03 12:49:28 -0700206 rootfs_part_size=options.root_part_size,
207 kernel_part_size=options.kern_part_size,
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700208 allow_unhashed=options.allow_unhashed,
209 disabled_tests=options.disabled_tests)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800210 finally:
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700211 if metadata_sig_file:
212 metadata_sig_file.close()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800213 if do_close_report_file:
214 report_file.close()
215
216 # Trace blocks.
217 if options.root_block is not None:
218 payload.TraceBlock(options.root_block, options.skip, sys.stdout, False)
219 if options.kern_block is not None:
220 payload.TraceBlock(options.kern_block, options.skip, sys.stdout, True)
221
222 # Apply payload.
223 if extra_args:
Gilad Arnold272a4992013-05-08 13:12:53 -0700224 dargs = {'bsdiff_in_place': not options.extract_bsdiff}
Gilad Arnold21a02502013-08-22 16:59:48 -0700225 if options.bspatch_path:
226 dargs['bspatch_path'] = options.bspatch_path
Gilad Arnold272a4992013-05-08 13:12:53 -0700227 if options.assert_type == _TYPE_DELTA:
228 dargs['old_kernel_part'] = extra_args[2]
229 dargs['old_rootfs_part'] = extra_args[3]
230
231 payload.Apply(extra_args[0], extra_args[1], **dargs)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800232
233 except update_payload.PayloadError, e:
234 sys.stderr.write('Error: %s\n' % e)
235 return 1
236
237 return 0
238
239
240if __name__ == '__main__':
241 sys.exit(main(sys.argv))