blob: 96b1032feca6e2c10e16e1cbd888756b0e34ddd9 [file] [log] [blame]
Gilad Arnoldb92167f2015-07-15 16:49:00 -07001#!/usr/bin/python2
Gilad Arnold553b0ec2013-01-26 01:00:39 -08002#
Amin Hassanif94b6432018-01-26 17:39:47 -08003# Copyright (C) 2013 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Gilad Arnold553b0ec2013-01-26 01:00:39 -080017
18"""Command-line tool for checking and applying Chrome OS update payloads."""
19
Gilad Arnoldb92167f2015-07-15 16:49:00 -070020from __future__ import print_function
21
Amin Hassani52b60392017-12-19 10:53:24 -080022# pylint: disable=import-error
23import argparse
Amin Hassanie20eb912018-02-21 12:39:42 -080024import filecmp
Gilad Arnold553b0ec2013-01-26 01:00:39 -080025import os
26import sys
Amin Hassanie20eb912018-02-21 12:39:42 -080027import tempfile
28
29from update_payload import error
Gilad Arnold553b0ec2013-01-26 01:00:39 -080030
Gilad Arnold553b0ec2013-01-26 01:00:39 -080031lib_dir = os.path.join(os.path.dirname(__file__), 'lib')
32if os.path.exists(lib_dir) and os.path.isdir(lib_dir):
33 sys.path.insert(1, lib_dir)
34import update_payload
35
36
37_TYPE_FULL = 'full'
38_TYPE_DELTA = 'delta'
39
40
Gilad Arnold4fbe4092013-04-17 10:00:55 -070041def ParseArguments(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -080042 """Parse and validate command-line arguments.
43
44 Args:
Gilad Arnold4fbe4092013-04-17 10:00:55 -070045 argv: command-line arguments to parse (excluding the program name)
Gilad Arnoldb92167f2015-07-15 16:49:00 -070046
Gilad Arnold553b0ec2013-01-26 01:00:39 -080047 Returns:
Amin Hassani52b60392017-12-19 10:53:24 -080048 Returns the arguments returned by the argument parser.
Gilad Arnold553b0ec2013-01-26 01:00:39 -080049 """
Amin Hassani52b60392017-12-19 10:53:24 -080050 parser = argparse.ArgumentParser(
51 description=('Applies a Chrome OS update PAYLOAD to src_kern and '
52 'src_root emitting dst_kern and dst_root, respectively. '
53 'src_kern and src_root are only needed for delta payloads. '
Gilad Arnold4fbe4092013-04-17 10:00:55 -070054 'When no partitions are provided, verifies the payload '
55 'integrity.'),
56 epilog=('Note: a payload may verify correctly but fail to apply, and '
57 'vice versa; this is by design and can be thought of as static '
58 'vs dynamic correctness. A payload that both verifies and '
59 'applies correctly should be safe for use by the Chrome OS '
60 'Update Engine. Use --check to verify a payload prior to '
Amin Hassani52b60392017-12-19 10:53:24 -080061 'applying it.'),
62 formatter_class=argparse.RawDescriptionHelpFormatter
63 )
Gilad Arnold553b0ec2013-01-26 01:00:39 -080064
Amin Hassani52b60392017-12-19 10:53:24 -080065 check_args = parser.add_argument_group('Checking payload integrity')
66 check_args.add_argument('-c', '--check', action='store_true', default=False,
67 help=('force payload integrity check (e.g. before '
68 'applying)'))
69 check_args.add_argument('-D', '--describe', action='store_true',
70 default=False,
71 help='Print a friendly description of the payload.')
72 check_args.add_argument('-r', '--report', metavar='FILE',
73 help="dump payload report (`-' for stdout)")
74 check_args.add_argument('-t', '--type', dest='assert_type',
75 help='assert the payload type',
76 choices=[_TYPE_FULL, _TYPE_DELTA])
77 check_args.add_argument('-z', '--block-size', metavar='NUM', default=0,
78 type=int,
79 help='assert a non-default (4096) payload block size')
80 check_args.add_argument('-u', '--allow-unhashed', action='store_true',
81 default=False, help='allow unhashed operations')
82 check_args.add_argument('-d', '--disabled_tests', default=(), metavar='',
83 help=('space separated list of tests to disable. '
84 'allowed options include: ' +
85 ', '.join(update_payload.CHECKS_TO_DISABLE)),
86 choices=update_payload.CHECKS_TO_DISABLE)
87 check_args.add_argument('-k', '--key', metavar='FILE',
88 help=('override standard key used for signature '
89 'validation'))
90 check_args.add_argument('-m', '--meta-sig', metavar='FILE',
91 help='verify metadata against its signature')
92 check_args.add_argument('-p', '--root-part-size', metavar='NUM',
93 default=0, type=int,
94 help='override rootfs partition size auto-inference')
95 check_args.add_argument('-P', '--kern-part-size', metavar='NUM',
96 default=0, type=int,
97 help='override kernel partition size auto-inference')
Gilad Arnold553b0ec2013-01-26 01:00:39 -080098
Amin Hassani52b60392017-12-19 10:53:24 -080099 apply_args = parser.add_argument_group('Applying payload')
100 # TODO(ahassani): Extent extract-bsdiff to puffdiff too.
101 apply_args.add_argument('-x', '--extract-bsdiff', action='store_true',
102 default=False,
103 help=('use temp input/output files with BSDIFF '
104 'operations (not in-place)'))
105 apply_args.add_argument('--bspatch-path', metavar='FILE',
106 help='use the specified bspatch binary')
107 apply_args.add_argument('--puffpatch-path', metavar='FILE',
108 help='use the specified puffpatch binary')
109 apply_args.add_argument('--dst_kern', metavar='FILE',
110 help='destination kernel partition file')
111 apply_args.add_argument('--dst_root', metavar='FILE',
112 help='destination root partition file')
113 apply_args.add_argument('--src_kern', metavar='FILE',
114 help='source kernel partition file')
115 apply_args.add_argument('--src_root', metavar='FILE',
116 help='source root partition file')
Amin Hassanie20eb912018-02-21 12:39:42 -0800117 apply_args.add_argument('--out_dst_kern', metavar='FILE',
118 help='created destination kernel partition file')
119 apply_args.add_argument('--out_dst_root', metavar='FILE',
120 help='created destination root partition file')
Gilad Arnold272a4992013-05-08 13:12:53 -0700121
Amin Hassani52b60392017-12-19 10:53:24 -0800122 parser.add_argument('payload', metavar='PAYLOAD', help='the payload file')
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800123
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700124 # Parse command-line arguments.
Amin Hassani52b60392017-12-19 10:53:24 -0800125 args = parser.parse_args(argv)
Gilad Arnoldeaed0d12013-04-30 15:38:22 -0700126
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700127 # There are several options that imply --check.
Amin Hassani52b60392017-12-19 10:53:24 -0800128 args.check = (args.check or args.report or args.assert_type or
129 args.block_size or args.allow_unhashed or
130 args.disabled_tests or args.meta_sig or args.key or
131 args.root_part_size or args.kern_part_size)
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700132
Amin Hassani52b60392017-12-19 10:53:24 -0800133 # Check the arguments, enforce payload type accordingly.
134 if (args.src_kern is None) != (args.src_root is None):
135 parser.error('--src_kern and --src_root should be given together')
136 if (args.dst_kern is None) != (args.dst_root is None):
137 parser.error('--dst_kern and --dst_root should be given together')
Amin Hassanie20eb912018-02-21 12:39:42 -0800138 if (args.out_dst_kern is None) != (args.out_dst_root is None):
139 parser.error('--out_dst_kern and --out_dst_root should be given together')
Amin Hassani52b60392017-12-19 10:53:24 -0800140
Amin Hassanie20eb912018-02-21 12:39:42 -0800141 if (args.dst_kern and args.dst_root) or \
142 (args.out_dst_kern and args.out_dst_root):
Amin Hassani52b60392017-12-19 10:53:24 -0800143 if args.src_kern and args.src_root:
144 if args.assert_type == _TYPE_FULL:
145 parser.error('%s payload does not accept source partition arguments'
146 % _TYPE_FULL)
147 else:
148 args.assert_type = _TYPE_DELTA
149 else:
150 if args.assert_type == _TYPE_DELTA:
151 parser.error('%s payload requires source partitions arguments'
152 % _TYPE_DELTA)
153 else:
154 args.assert_type = _TYPE_FULL
155 else:
Amin Hassania5489022018-01-26 11:23:26 -0800156 # Not applying payload.
Amin Hassani52b60392017-12-19 10:53:24 -0800157 if args.extract_bsdiff:
Gilad Arnold272a4992013-05-08 13:12:53 -0700158 parser.error('--extract-bsdiff can only be used when applying payloads')
Amin Hassani52b60392017-12-19 10:53:24 -0800159 if args.bspatch_path:
Gilad Arnold21a02502013-08-22 16:59:48 -0700160 parser.error('--bspatch-path can only be used when applying payloads')
Amin Hassani52b60392017-12-19 10:53:24 -0800161 if args.puffpatch_path:
Amin Hassani6be71682017-12-01 10:46:45 -0800162 parser.error('--puffpatch-path can only be used when applying payloads')
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700163
Don Garrett30027fd2013-05-01 16:56:16 -0700164 # By default, look for a metadata-signature file with a name based on the name
Gilad Arnold9b90c932013-05-22 17:12:56 -0700165 # of the payload we are checking. We only do it if check was triggered.
Amin Hassani52b60392017-12-19 10:53:24 -0800166 if args.check and not args.meta_sig:
167 default_meta_sig = args.payload + '.metadata-signature'
Don Garrett30027fd2013-05-01 16:56:16 -0700168 if os.path.isfile(default_meta_sig):
Amin Hassani52b60392017-12-19 10:53:24 -0800169 args.meta_sig = default_meta_sig
170 print('Using default metadata signature', args.meta_sig, file=sys.stderr)
Don Garrett30027fd2013-05-01 16:56:16 -0700171
Amin Hassani52b60392017-12-19 10:53:24 -0800172 return args
Gilad Arnold4fbe4092013-04-17 10:00:55 -0700173
174
175def main(argv):
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800176 # Parse and validate arguments.
Amin Hassani52b60392017-12-19 10:53:24 -0800177 args = ParseArguments(argv[1:])
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800178
Amin Hassani52b60392017-12-19 10:53:24 -0800179 with open(args.payload) as payload_file:
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800180 payload = update_payload.Payload(payload_file)
181 try:
182 # Initialize payload.
183 payload.Init()
184
Amin Hassani52b60392017-12-19 10:53:24 -0800185 if args.describe:
Don Garrett432d6012013-05-10 15:01:36 -0700186 payload.Describe()
187
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800188 # Perform payload integrity checks.
Amin Hassani52b60392017-12-19 10:53:24 -0800189 if args.check:
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800190 report_file = None
191 do_close_report_file = False
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700192 metadata_sig_file = None
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800193 try:
Amin Hassani52b60392017-12-19 10:53:24 -0800194 if args.report:
195 if args.report == '-':
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800196 report_file = sys.stdout
197 else:
Amin Hassani52b60392017-12-19 10:53:24 -0800198 report_file = open(args.report, 'w')
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800199 do_close_report_file = True
Gilad Arnold03959b72013-05-07 17:08:18 -0700200
Amin Hassani52b60392017-12-19 10:53:24 -0800201 metadata_sig_file = args.meta_sig and open(args.meta_sig)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800202 payload.Check(
Amin Hassani52b60392017-12-19 10:53:24 -0800203 pubkey_file_name=args.key,
Gilad Arnold4f8c17c2013-05-04 22:57:45 -0700204 metadata_sig_file=metadata_sig_file,
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800205 report_out_file=report_file,
Amin Hassani52b60392017-12-19 10:53:24 -0800206 assert_type=args.assert_type,
207 block_size=int(args.block_size),
208 rootfs_part_size=args.root_part_size,
209 kernel_part_size=args.kern_part_size,
210 allow_unhashed=args.allow_unhashed,
211 disabled_tests=args.disabled_tests)
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800212 finally:
Gilad Arnold7a7edfd2013-05-22 17:21:58 -0700213 if metadata_sig_file:
214 metadata_sig_file.close()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800215 if do_close_report_file:
216 report_file.close()
217
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800218 # Apply payload.
Amin Hassanie20eb912018-02-21 12:39:42 -0800219 if (args.dst_root and args.dst_kern) or \
220 (args.out_dst_root and args.out_dst_kern):
Amin Hassani52b60392017-12-19 10:53:24 -0800221 dargs = {'bsdiff_in_place': not args.extract_bsdiff}
222 if args.bspatch_path:
223 dargs['bspatch_path'] = args.bspatch_path
224 if args.puffpatch_path:
225 dargs['puffpatch_path'] = args.puffpatch_path
226 if args.assert_type == _TYPE_DELTA:
227 dargs['old_kernel_part'] = args.src_kern
228 dargs['old_rootfs_part'] = args.src_root
Gilad Arnold272a4992013-05-08 13:12:53 -0700229
Amin Hassanie20eb912018-02-21 12:39:42 -0800230 if args.out_dst_kern and args.out_dst_root:
231 out_dst_kern = open(args.out_dst_kern, 'w+')
232 out_dst_root = open(args.out_dst_root, 'w+')
233 else:
234 out_dst_kern = tempfile.NamedTemporaryFile()
235 out_dst_root = tempfile.NamedTemporaryFile()
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800236
Amin Hassanie20eb912018-02-21 12:39:42 -0800237 payload.Apply(out_dst_kern.name, out_dst_root.name, **dargs)
238
239 # If destination kernel and rootfs partitions are not given, then this
240 # just becomes an apply operation with no check.
241 if args.dst_kern and args.dst_root:
242 # Prior to comparing, add the unused space past the filesystem
243 # boundary in the new target partitions to become the same size as
244 # the given partitions. This will truncate to larger size.
245 out_dst_kern.truncate(os.path.getsize(args.dst_kern))
246 out_dst_root.truncate(os.path.getsize(args.dst_root))
247
248 # Compare resulting partitions with the ones from the target image.
249 if not filecmp.cmp(out_dst_kern.name, args.dst_kern):
250 raise error.PayloadError('Resulting kernel partition corrupted.')
251 if not filecmp.cmp(out_dst_root.name, args.dst_root):
252 raise error.PayloadError('Resulting rootfs partition corrupted.')
253
254 # Close the output files. If args.out_dst_* was not given, then these
255 # files are created as temp files and will be deleted upon close().
256 out_dst_kern.close()
257 out_dst_root.close()
258
259 except error.PayloadError, e:
Gilad Arnold553b0ec2013-01-26 01:00:39 -0800260 sys.stderr.write('Error: %s\n' % e)
261 return 1
262
263 return 0
264
265
266if __name__ == '__main__':
267 sys.exit(main(sys.argv))