Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Script to generate a Chromium OS update for use by the update engine. |
| 7 | |
| 8 | If a source .bin is specified, the update is assumed to be a delta update. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 13 | from chromite.lib import commandline |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_logging as logging |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 15 | |
Amin Hassani | bdda5e4 | 2018-10-10 22:56:11 -0700 | [diff] [blame] | 16 | from chromite.lib.paygen import partition_lib |
Amin Hassani | 6bc73a1 | 2018-11-29 21:07:12 -0800 | [diff] [blame] | 17 | from chromite.lib.paygen import paygen_payload_lib |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | def ParseArguments(argv): |
| 21 | """Returns a namespace for the CLI arguments.""" |
| 22 | parser = commandline.ArgumentParser(description=__doc__) |
Amin Hassani | a809eda | 2019-03-07 15:28:01 -0800 | [diff] [blame] | 23 | parser.add_argument('--image', |
| 24 | help='The path (to local disk or Google Storage Bucket)' |
| 25 | ' of the target image to build the payload for.') |
| 26 | parser.add_argument('--src_image', |
| 27 | help='The path (to local disk or Google Storage Bucket)' |
| 28 | ' of the source image. If specified, this makes a delta' |
| 29 | ' update payload.') |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 30 | parser.add_argument('--output', type='path', help='Output file.') |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 31 | parser.add_argument('--private_key', type='path', |
| 32 | help='Path to private key in .pem format.') |
Amin Hassani | 6bc73a1 | 2018-11-29 21:07:12 -0800 | [diff] [blame] | 33 | parser.add_argument('--check', action='store_true', |
| 34 | help='If passed, verifies the integrity of the payload') |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 35 | parser.add_argument('--out_metadata_hash_file', type='path', |
| 36 | help='Path to output metadata hash file.') |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 37 | parser.add_argument('--extract', action='store_true', |
| 38 | help='If set, extract old/new kernel/rootfs to ' |
| 39 | '[old|new]_[kern|root].dat. Useful for debugging.') |
Amin Hassani | 6bc73a1 | 2018-11-29 21:07:12 -0800 | [diff] [blame] | 40 | parser.add_argument('--work_dir', type='path', |
| 41 | help='Path to a temporary directory in the chroot.') |
Amin Hassani | ff61884 | 2019-06-02 21:43:14 -0700 | [diff] [blame^] | 42 | parser.add_argument('--payload', type='path', |
| 43 | help='Path to the input payload file. Only used when ' |
| 44 | 'trying to generate the payload properties file using ' |
| 45 | 'the given payload. If --output is not passed, the ' |
| 46 | 'payload properties file path will be generated by ' |
| 47 | 'appending .json to the payload filename itself.') |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 48 | |
| 49 | # Specifying any of the following will cause it to not be cleaned up on exit. |
| 50 | parser.add_argument('--kern_path', type='path', |
| 51 | help='File path for extracting the kernel partition.') |
| 52 | parser.add_argument('--root_path', type='path', |
| 53 | help='File path for extracting the rootfs partition.') |
| 54 | parser.add_argument('--root_pretruncate_path', type='path', |
| 55 | help='File path for extracting the rootfs partition, ' |
| 56 | 'pre-truncation.') |
| 57 | parser.add_argument('--src_kern_path', type='path', |
| 58 | help='File path for extracting the source kernel ' |
| 59 | 'partition.') |
| 60 | parser.add_argument('--src_root_path', type='path', |
| 61 | help='File path for extracting the source root ' |
| 62 | 'partition.') |
| 63 | |
| 64 | opts = parser.parse_args(argv) |
| 65 | opts.Freeze() |
| 66 | |
Tudor Brindus | 3e03eba | 2018-07-18 11:27:13 -0700 | [diff] [blame] | 67 | return opts |
| 68 | |
| 69 | |
| 70 | def main(argv): |
| 71 | opts = ParseArguments(argv) |
| 72 | |
Amin Hassani | 6bc73a1 | 2018-11-29 21:07:12 -0800 | [diff] [blame] | 73 | if opts.kern_path: |
| 74 | partition_lib.ExtractKernel(opts.image, opts.kern_path) |
| 75 | if opts.root_path: |
| 76 | partition_lib.ExtractRoot(opts.image, opts.root_path) |
| 77 | if opts.root_pretruncate_path: |
| 78 | partition_lib.ExtractRoot(opts.image, opts.root_pretruncate_path, |
| 79 | truncate=False) |
| 80 | |
| 81 | if opts.src_image: |
| 82 | if opts.src_kern_path: |
| 83 | partition_lib.ExtractKernel(opts.src_image, opts.src_kern_path) |
| 84 | if opts.src_root_path: |
| 85 | partition_lib.ExtractRoot(opts.src_image, opts.src_root_path) |
| 86 | |
| 87 | if opts.extract: |
| 88 | # If we just wanted extraction, we did it, just return. |
| 89 | logging.info('Done extracting kernel/root.') |
| 90 | return |
| 91 | |
Amin Hassani | ff61884 | 2019-06-02 21:43:14 -0700 | [diff] [blame^] | 92 | if opts.payload: |
| 93 | # We only want the payload's metadata. Create it and exit. |
| 94 | logging.info('Generating payload properties file.') |
| 95 | paygen_payload_lib.GenerateUpdatePayloadPropertiesFile(opts.payload, |
| 96 | opts.output) |
| 97 | return |
| 98 | |
Amin Hassani | 6bc73a1 | 2018-11-29 21:07:12 -0800 | [diff] [blame] | 99 | return paygen_payload_lib.GenerateUpdatePayload( |
| 100 | opts.image, opts.output, src_image=opts.src_image, work_dir=opts.work_dir, |
| 101 | private_key=opts.private_key, check=opts.check, |
| 102 | out_metadata_hash_file=opts.out_metadata_hash_file) |