Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 2 | # |
Amin Hassani | f94b643 | 2018-01-26 17:39:47 -0800 | [diff] [blame^] | 3 | # 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 Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 17 | |
| 18 | """Command-line tool for checking and applying Chrome OS update payloads.""" |
| 19 | |
Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 22 | # pylint: disable=import-error |
| 23 | import argparse |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 24 | import os |
| 25 | import sys |
| 26 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 27 | lib_dir = os.path.join(os.path.dirname(__file__), 'lib') |
| 28 | if os.path.exists(lib_dir) and os.path.isdir(lib_dir): |
| 29 | sys.path.insert(1, lib_dir) |
| 30 | import update_payload |
| 31 | |
| 32 | |
| 33 | _TYPE_FULL = 'full' |
| 34 | _TYPE_DELTA = 'delta' |
| 35 | |
| 36 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 37 | def ParseArguments(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 38 | """Parse and validate command-line arguments. |
| 39 | |
| 40 | Args: |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 41 | argv: command-line arguments to parse (excluding the program name) |
Gilad Arnold | b92167f | 2015-07-15 16:49:00 -0700 | [diff] [blame] | 42 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 43 | Returns: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 44 | Returns the arguments returned by the argument parser. |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 45 | """ |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 46 | parser = argparse.ArgumentParser( |
| 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. ' |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 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 ' |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 57 | 'applying it.'), |
| 58 | formatter_class=argparse.RawDescriptionHelpFormatter |
| 59 | ) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 60 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 61 | check_args = parser.add_argument_group('Checking payload integrity') |
| 62 | check_args.add_argument('-c', '--check', action='store_true', default=False, |
| 63 | help=('force payload integrity check (e.g. before ' |
| 64 | 'applying)')) |
| 65 | check_args.add_argument('-D', '--describe', action='store_true', |
| 66 | default=False, |
| 67 | help='Print a friendly description of the payload.') |
| 68 | check_args.add_argument('-r', '--report', metavar='FILE', |
| 69 | help="dump payload report (`-' for stdout)") |
| 70 | check_args.add_argument('-t', '--type', dest='assert_type', |
| 71 | help='assert the payload type', |
| 72 | choices=[_TYPE_FULL, _TYPE_DELTA]) |
| 73 | check_args.add_argument('-z', '--block-size', metavar='NUM', default=0, |
| 74 | type=int, |
| 75 | help='assert a non-default (4096) payload block size') |
| 76 | check_args.add_argument('-u', '--allow-unhashed', action='store_true', |
| 77 | default=False, help='allow unhashed operations') |
| 78 | check_args.add_argument('-d', '--disabled_tests', default=(), metavar='', |
| 79 | help=('space separated list of tests to disable. ' |
| 80 | 'allowed options include: ' + |
| 81 | ', '.join(update_payload.CHECKS_TO_DISABLE)), |
| 82 | choices=update_payload.CHECKS_TO_DISABLE) |
| 83 | check_args.add_argument('-k', '--key', metavar='FILE', |
| 84 | help=('override standard key used for signature ' |
| 85 | 'validation')) |
| 86 | check_args.add_argument('-m', '--meta-sig', metavar='FILE', |
| 87 | help='verify metadata against its signature') |
| 88 | check_args.add_argument('-p', '--root-part-size', metavar='NUM', |
| 89 | default=0, type=int, |
| 90 | help='override rootfs partition size auto-inference') |
| 91 | check_args.add_argument('-P', '--kern-part-size', metavar='NUM', |
| 92 | default=0, type=int, |
| 93 | help='override kernel partition size auto-inference') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 94 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 95 | apply_args = parser.add_argument_group('Applying payload') |
| 96 | # TODO(ahassani): Extent extract-bsdiff to puffdiff too. |
| 97 | apply_args.add_argument('-x', '--extract-bsdiff', action='store_true', |
| 98 | default=False, |
| 99 | help=('use temp input/output files with BSDIFF ' |
| 100 | 'operations (not in-place)')) |
| 101 | apply_args.add_argument('--bspatch-path', metavar='FILE', |
| 102 | help='use the specified bspatch binary') |
| 103 | apply_args.add_argument('--puffpatch-path', metavar='FILE', |
| 104 | help='use the specified puffpatch binary') |
| 105 | apply_args.add_argument('--dst_kern', metavar='FILE', |
| 106 | help='destination kernel partition file') |
| 107 | apply_args.add_argument('--dst_root', metavar='FILE', |
| 108 | help='destination root partition file') |
| 109 | apply_args.add_argument('--src_kern', metavar='FILE', |
| 110 | help='source kernel partition file') |
| 111 | apply_args.add_argument('--src_root', metavar='FILE', |
| 112 | help='source root partition file') |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 113 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 114 | parser.add_argument('payload', metavar='PAYLOAD', help='the payload file') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 115 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 116 | # Parse command-line arguments. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 117 | args = parser.parse_args(argv) |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 118 | |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 119 | # There are several options that imply --check. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 120 | args.check = (args.check or args.report or args.assert_type or |
| 121 | args.block_size or args.allow_unhashed or |
| 122 | args.disabled_tests or args.meta_sig or args.key or |
| 123 | args.root_part_size or args.kern_part_size) |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 124 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 125 | # Check the arguments, enforce payload type accordingly. |
| 126 | if (args.src_kern is None) != (args.src_root is None): |
| 127 | parser.error('--src_kern and --src_root should be given together') |
| 128 | if (args.dst_kern is None) != (args.dst_root is None): |
| 129 | parser.error('--dst_kern and --dst_root should be given together') |
| 130 | |
| 131 | if args.dst_kern and args.dst_root: |
| 132 | if args.src_kern and args.src_root: |
| 133 | if args.assert_type == _TYPE_FULL: |
| 134 | parser.error('%s payload does not accept source partition arguments' |
| 135 | % _TYPE_FULL) |
| 136 | else: |
| 137 | args.assert_type = _TYPE_DELTA |
| 138 | else: |
| 139 | if args.assert_type == _TYPE_DELTA: |
| 140 | parser.error('%s payload requires source partitions arguments' |
| 141 | % _TYPE_DELTA) |
| 142 | else: |
| 143 | args.assert_type = _TYPE_FULL |
| 144 | else: |
Amin Hassani | a548902 | 2018-01-26 11:23:26 -0800 | [diff] [blame] | 145 | # Not applying payload. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 146 | if args.extract_bsdiff: |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 147 | parser.error('--extract-bsdiff can only be used when applying payloads') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 148 | if args.bspatch_path: |
Gilad Arnold | 21a0250 | 2013-08-22 16:59:48 -0700 | [diff] [blame] | 149 | parser.error('--bspatch-path can only be used when applying payloads') |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 150 | if args.puffpatch_path: |
Amin Hassani | 6be7168 | 2017-12-01 10:46:45 -0800 | [diff] [blame] | 151 | parser.error('--puffpatch-path can only be used when applying payloads') |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 152 | |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 153 | # By default, look for a metadata-signature file with a name based on the name |
Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 154 | # of the payload we are checking. We only do it if check was triggered. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 155 | if args.check and not args.meta_sig: |
| 156 | default_meta_sig = args.payload + '.metadata-signature' |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 157 | if os.path.isfile(default_meta_sig): |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 158 | args.meta_sig = default_meta_sig |
| 159 | print('Using default metadata signature', args.meta_sig, file=sys.stderr) |
Don Garrett | 30027fd | 2013-05-01 16:56:16 -0700 | [diff] [blame] | 160 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 161 | return args |
Gilad Arnold | 4fbe409 | 2013-04-17 10:00:55 -0700 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def main(argv): |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 165 | # Parse and validate arguments. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 166 | args = ParseArguments(argv[1:]) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 167 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 168 | with open(args.payload) as payload_file: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 169 | payload = update_payload.Payload(payload_file) |
| 170 | try: |
| 171 | # Initialize payload. |
| 172 | payload.Init() |
| 173 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 174 | if args.describe: |
Don Garrett | 432d601 | 2013-05-10 15:01:36 -0700 | [diff] [blame] | 175 | payload.Describe() |
| 176 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 177 | # Perform payload integrity checks. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 178 | if args.check: |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 179 | report_file = None |
| 180 | do_close_report_file = False |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 181 | metadata_sig_file = None |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 182 | try: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 183 | if args.report: |
| 184 | if args.report == '-': |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 185 | report_file = sys.stdout |
| 186 | else: |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 187 | report_file = open(args.report, 'w') |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 188 | do_close_report_file = True |
Gilad Arnold | 03959b7 | 2013-05-07 17:08:18 -0700 | [diff] [blame] | 189 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 190 | metadata_sig_file = args.meta_sig and open(args.meta_sig) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 191 | payload.Check( |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 192 | pubkey_file_name=args.key, |
Gilad Arnold | 4f8c17c | 2013-05-04 22:57:45 -0700 | [diff] [blame] | 193 | metadata_sig_file=metadata_sig_file, |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 194 | report_out_file=report_file, |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 195 | assert_type=args.assert_type, |
| 196 | block_size=int(args.block_size), |
| 197 | rootfs_part_size=args.root_part_size, |
| 198 | kernel_part_size=args.kern_part_size, |
| 199 | allow_unhashed=args.allow_unhashed, |
| 200 | disabled_tests=args.disabled_tests) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 201 | finally: |
Gilad Arnold | 7a7edfd | 2013-05-22 17:21:58 -0700 | [diff] [blame] | 202 | if metadata_sig_file: |
| 203 | metadata_sig_file.close() |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 204 | if do_close_report_file: |
| 205 | report_file.close() |
| 206 | |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 207 | # Apply payload. |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 208 | if args.dst_root or args.dst_kern: |
| 209 | dargs = {'bsdiff_in_place': not args.extract_bsdiff} |
| 210 | if args.bspatch_path: |
| 211 | dargs['bspatch_path'] = args.bspatch_path |
| 212 | if args.puffpatch_path: |
| 213 | dargs['puffpatch_path'] = args.puffpatch_path |
| 214 | if args.assert_type == _TYPE_DELTA: |
| 215 | dargs['old_kernel_part'] = args.src_kern |
| 216 | dargs['old_rootfs_part'] = args.src_root |
Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 217 | |
Amin Hassani | 52b6039 | 2017-12-19 10:53:24 -0800 | [diff] [blame] | 218 | payload.Apply(args.dst_kern, args.dst_root, **dargs) |
Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 219 | |
| 220 | except update_payload.PayloadError, e: |
| 221 | sys.stderr.write('Error: %s\n' % e) |
| 222 | return 1 |
| 223 | |
| 224 | return 0 |
| 225 | |
| 226 | |
| 227 | if __name__ == '__main__': |
| 228 | sys.exit(main(sys.argv)) |