Home | History | Annotate | Download | only in payload_generator
      1 //
      2 // Copyright (C) 2010 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include <errno.h>
     18 #include <fcntl.h>
     19 #include <sys/stat.h>
     20 #include <sys/types.h>
     21 #include <unistd.h>
     22 #include <xz.h>
     23 
     24 #include <string>
     25 #include <vector>
     26 
     27 #include <base/logging.h>
     28 #include <base/strings/string_number_conversions.h>
     29 #include <base/strings/string_split.h>
     30 #include <brillo/flag_helper.h>
     31 #include <brillo/key_value_store.h>
     32 
     33 #include "update_engine/common/fake_boot_control.h"
     34 #include "update_engine/common/fake_hardware.h"
     35 #include "update_engine/common/prefs.h"
     36 #include "update_engine/common/terminator.h"
     37 #include "update_engine/common/utils.h"
     38 #include "update_engine/payload_consumer/delta_performer.h"
     39 #include "update_engine/payload_consumer/payload_constants.h"
     40 #include "update_engine/payload_generator/delta_diff_generator.h"
     41 #include "update_engine/payload_generator/payload_generation_config.h"
     42 #include "update_engine/payload_generator/payload_signer.h"
     43 #include "update_engine/payload_generator/xz.h"
     44 #include "update_engine/update_metadata.pb.h"
     45 
     46 // This file contains a simple program that takes an old path, a new path,
     47 // and an output file as arguments and the path to an output file and
     48 // generates a delta that can be sent to Chrome OS clients.
     49 
     50 using std::string;
     51 using std::vector;
     52 
     53 namespace chromeos_update_engine {
     54 
     55 namespace {
     56 
     57 void ParseSignatureSizes(const string& signature_sizes_flag,
     58                          vector<int>* signature_sizes) {
     59   signature_sizes->clear();
     60   vector<string> split_strings =
     61       base::SplitString(signature_sizes_flag, ":", base::TRIM_WHITESPACE,
     62                         base::SPLIT_WANT_ALL);
     63   for (const string& str : split_strings) {
     64     int size = 0;
     65     bool parsing_successful = base::StringToInt(str, &size);
     66     LOG_IF(FATAL, !parsing_successful)
     67         << "Invalid signature size: " << str;
     68 
     69     LOG_IF(FATAL, size != (2048 / 8)) <<
     70         "Only signature sizes of 256 bytes are supported.";
     71 
     72     signature_sizes->push_back(size);
     73   }
     74 }
     75 
     76 bool ParseImageInfo(const string& channel,
     77                     const string& board,
     78                     const string& version,
     79                     const string& key,
     80                     const string& build_channel,
     81                     const string& build_version,
     82                     ImageInfo* image_info) {
     83   // All of these arguments should be present or missing.
     84   bool empty = channel.empty();
     85 
     86   CHECK_EQ(channel.empty(), empty);
     87   CHECK_EQ(board.empty(), empty);
     88   CHECK_EQ(version.empty(), empty);
     89   CHECK_EQ(key.empty(), empty);
     90 
     91   if (empty)
     92     return false;
     93 
     94   image_info->set_channel(channel);
     95   image_info->set_board(board);
     96   image_info->set_version(version);
     97   image_info->set_key(key);
     98 
     99   image_info->set_build_channel(
    100       build_channel.empty() ? channel : build_channel);
    101 
    102   image_info->set_build_version(
    103       build_version.empty() ? version : build_version);
    104 
    105   return true;
    106 }
    107 
    108 void CalculateHashForSigning(const vector<int> &sizes,
    109                              const string& out_hash_file,
    110                              const string& out_metadata_hash_file,
    111                              const string& in_file) {
    112   LOG(INFO) << "Calculating hash for signing.";
    113   LOG_IF(FATAL, in_file.empty())
    114       << "Must pass --in_file to calculate hash for signing.";
    115   LOG_IF(FATAL, out_hash_file.empty())
    116       << "Must pass --out_hash_file to calculate hash for signing.";
    117 
    118   brillo::Blob payload_hash, metadata_hash;
    119   CHECK(PayloadSigner::HashPayloadForSigning(in_file, sizes, &payload_hash,
    120                                              &metadata_hash));
    121   CHECK(utils::WriteFile(out_hash_file.c_str(), payload_hash.data(),
    122                          payload_hash.size()));
    123   if (!out_metadata_hash_file.empty())
    124     CHECK(utils::WriteFile(out_metadata_hash_file.c_str(), metadata_hash.data(),
    125                            metadata_hash.size()));
    126 
    127   LOG(INFO) << "Done calculating hash for signing.";
    128 }
    129 
    130 void SignatureFileFlagToBlobs(const string& signature_file_flag,
    131                               vector<brillo::Blob>* signatures) {
    132   vector<string> signature_files =
    133       base::SplitString(signature_file_flag, ":", base::TRIM_WHITESPACE,
    134                         base::SPLIT_WANT_ALL);
    135   for (const string& signature_file : signature_files) {
    136     brillo::Blob signature;
    137     CHECK(utils::ReadFile(signature_file, &signature));
    138     signatures->push_back(signature);
    139   }
    140 }
    141 
    142 void SignPayload(const string& in_file,
    143                  const string& out_file,
    144                  const string& payload_signature_file,
    145                  const string& metadata_signature_file,
    146                  const string& out_metadata_size_file) {
    147   LOG(INFO) << "Signing payload.";
    148   LOG_IF(FATAL, in_file.empty())
    149       << "Must pass --in_file to sign payload.";
    150   LOG_IF(FATAL, out_file.empty())
    151       << "Must pass --out_file to sign payload.";
    152   LOG_IF(FATAL, payload_signature_file.empty())
    153       << "Must pass --signature_file to sign payload.";
    154   vector<brillo::Blob> signatures, metadata_signatures;
    155   SignatureFileFlagToBlobs(payload_signature_file, &signatures);
    156   SignatureFileFlagToBlobs(metadata_signature_file, &metadata_signatures);
    157   uint64_t final_metadata_size;
    158   CHECK(PayloadSigner::AddSignatureToPayload(in_file, signatures,
    159       metadata_signatures, out_file, &final_metadata_size));
    160   LOG(INFO) << "Done signing payload. Final metadata size = "
    161             << final_metadata_size;
    162   if (!out_metadata_size_file.empty()) {
    163     string metadata_size_string = std::to_string(final_metadata_size);
    164     CHECK(utils::WriteFile(out_metadata_size_file.c_str(),
    165                            metadata_size_string.data(),
    166                            metadata_size_string.size()));
    167   }
    168 }
    169 
    170 int VerifySignedPayload(const string& in_file, const string& public_key) {
    171   LOG(INFO) << "Verifying signed payload.";
    172   LOG_IF(FATAL, in_file.empty())
    173       << "Must pass --in_file to verify signed payload.";
    174   LOG_IF(FATAL, public_key.empty())
    175       << "Must pass --public_key to verify signed payload.";
    176   if (!PayloadSigner::VerifySignedPayload(in_file, public_key)) {
    177     LOG(INFO) << "VerifySignedPayload failed";
    178     return 1;
    179   }
    180 
    181   LOG(INFO) << "Done verifying signed payload.";
    182   return 0;
    183 }
    184 
    185 // TODO(deymo): This function is likely broken for deltas minor version 2 or
    186 // newer. Move this function to a new file and make the delta_performer
    187 // integration tests use this instead.
    188 bool ApplyPayload(const string& payload_file,
    189                   // Simply reuses the payload config used for payload
    190                   // generation.
    191                   const PayloadGenerationConfig& config) {
    192   LOG(INFO) << "Applying delta.";
    193   FakeBootControl fake_boot_control;
    194   FakeHardware fake_hardware;
    195   MemoryPrefs prefs;
    196   InstallPlan install_plan;
    197   InstallPlan::Payload payload;
    198   install_plan.source_slot =
    199       config.is_delta ? 0 : BootControlInterface::kInvalidSlot;
    200   install_plan.target_slot = 1;
    201   payload.type =
    202       config.is_delta ? InstallPayloadType::kDelta : InstallPayloadType::kFull;
    203 
    204   for (size_t i = 0; i < config.target.partitions.size(); i++) {
    205     const string& part_name = config.target.partitions[i].name;
    206     const string& target_path = config.target.partitions[i].path;
    207     fake_boot_control.SetPartitionDevice(
    208         part_name, install_plan.target_slot, target_path);
    209 
    210     string source_path;
    211     if (config.is_delta) {
    212       TEST_AND_RETURN_FALSE(config.target.partitions.size() ==
    213                             config.source.partitions.size());
    214       source_path = config.source.partitions[i].path;
    215       fake_boot_control.SetPartitionDevice(
    216           part_name, install_plan.source_slot, source_path);
    217     }
    218 
    219     LOG(INFO) << "Install partition:"
    220               << " source: " << source_path << " target: " << target_path;
    221   }
    222 
    223   DeltaPerformer performer(&prefs,
    224                            &fake_boot_control,
    225                            &fake_hardware,
    226                            nullptr,
    227                            &install_plan,
    228                            &payload,
    229                            true);  // is_interactive
    230 
    231   brillo::Blob buf(1024 * 1024);
    232   int fd = open(payload_file.c_str(), O_RDONLY, 0);
    233   CHECK_GE(fd, 0);
    234   ScopedFdCloser fd_closer(&fd);
    235   xz_crc32_init();
    236   for (off_t offset = 0;; offset += buf.size()) {
    237     ssize_t bytes_read;
    238     CHECK(utils::PReadAll(fd, buf.data(), buf.size(), offset, &bytes_read));
    239     if (bytes_read == 0)
    240       break;
    241     TEST_AND_RETURN_FALSE(performer.Write(buf.data(), bytes_read));
    242   }
    243   CHECK_EQ(performer.Close(), 0);
    244   DeltaPerformer::ResetUpdateProgress(&prefs, false);
    245   LOG(INFO) << "Completed applying " << (config.is_delta ? "delta" : "full")
    246             << " payload.";
    247   return true;
    248 }
    249 
    250 int ExtractProperties(const string& payload_path, const string& props_file) {
    251   brillo::KeyValueStore properties;
    252   TEST_AND_RETURN_FALSE(
    253       PayloadSigner::ExtractPayloadProperties(payload_path, &properties));
    254   if (props_file == "-") {
    255     printf("%s", properties.SaveToString().c_str());
    256   } else {
    257     properties.Save(base::FilePath(props_file));
    258     LOG(INFO) << "Generated properties file at " << props_file;
    259   }
    260   return true;
    261 }
    262 
    263 int Main(int argc, char** argv) {
    264   DEFINE_string(old_image, "", "Path to the old rootfs");
    265   DEFINE_string(new_image, "", "Path to the new rootfs");
    266   DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
    267   DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
    268   DEFINE_string(old_partitions, "",
    269                 "Path to the old partitions. To pass multiple partitions, use "
    270                 "a single argument with a colon between paths, e.g. "
    271                 "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
    272                 "be empty, but it has to match the order of partition_names.");
    273   DEFINE_string(new_partitions, "",
    274                 "Path to the new partitions. To pass multiple partitions, use "
    275                 "a single argument with a colon between paths, e.g. "
    276                 "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
    277                 "to match the order of partition_names.");
    278   DEFINE_string(old_mapfiles,
    279                 "",
    280                 "Path to the .map files associated with the partition files "
    281                 "in the old partition. The .map file is normally generated "
    282                 "when creating the image in Android builds. Only recommended "
    283                 "for unsupported filesystem. Pass multiple files separated by "
    284                 "a colon as with -old_partitions.");
    285   DEFINE_string(new_mapfiles,
    286                 "",
    287                 "Path to the .map files associated with the partition files "
    288                 "in the new partition, similar to the -old_mapfiles flag.");
    289   DEFINE_string(partition_names,
    290                 string(kLegacyPartitionNameRoot) + ":" +
    291                 kLegacyPartitionNameKernel,
    292                 "Names of the partitions. To pass multiple names, use a single "
    293                 "argument with a colon between names, e.g. "
    294                 "name:name2:name3:last_name . Name can not be empty, and it "
    295                 "has to match the order of partitions.");
    296   DEFINE_string(in_file, "",
    297                 "Path to input delta payload file used to hash/sign payloads "
    298                 "and apply delta over old_image (for debugging)");
    299   DEFINE_string(out_file, "", "Path to output delta payload file");
    300   DEFINE_string(out_hash_file, "", "Path to output hash file");
    301   DEFINE_string(out_metadata_hash_file, "",
    302                 "Path to output metadata hash file");
    303   DEFINE_string(out_metadata_size_file, "",
    304                 "Path to output metadata size file");
    305   DEFINE_string(private_key, "", "Path to private key in .pem format");
    306   DEFINE_string(public_key, "", "Path to public key in .pem format");
    307   DEFINE_int32(public_key_version, -1,
    308                "DEPRECATED. Key-check version # of client");
    309   DEFINE_string(signature_size, "",
    310                 "Raw signature size used for hash calculation. "
    311                 "You may pass in multiple sizes by colon separating them. E.g. "
    312                 "2048:2048:4096 will assume 3 signatures, the first two with "
    313                 "2048 size and the last 4096.");
    314   DEFINE_string(signature_file, "",
    315                 "Raw signature file to sign payload with. To pass multiple "
    316                 "signatures, use a single argument with a colon between paths, "
    317                 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
    318                 "signature will be assigned a client version, starting from "
    319                 "kSignatureOriginalVersion.");
    320   DEFINE_string(metadata_signature_file, "",
    321                 "Raw signature file with the signature of the metadata hash. "
    322                 "To pass multiple signatures, use a single argument with a "
    323                 "colon between paths, "
    324                 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
    325   DEFINE_int32(chunk_size, 200 * 1024 * 1024,
    326                "Payload chunk size (-1 for whole files)");
    327   DEFINE_uint64(rootfs_partition_size,
    328                chromeos_update_engine::kRootFSPartitionSize,
    329                "RootFS partition size for the image once installed");
    330   DEFINE_uint64(major_version, 1,
    331                "The major version of the payload being generated.");
    332   DEFINE_int32(minor_version, -1,
    333                "The minor version of the payload being generated "
    334                "(-1 means autodetect).");
    335   DEFINE_string(properties_file, "",
    336                 "If passed, dumps the payload properties of the payload passed "
    337                 "in --in_file and exits.");
    338   DEFINE_int64(max_timestamp,
    339                0,
    340                "The maximum timestamp of the OS allowed to apply this "
    341                "payload.");
    342 
    343   DEFINE_string(old_channel, "",
    344                 "The channel for the old image. 'dev-channel', 'npo-channel', "
    345                 "etc. Ignored, except during delta generation.");
    346   DEFINE_string(old_board, "",
    347                 "The board for the old image. 'x86-mario', 'lumpy', "
    348                 "etc. Ignored, except during delta generation.");
    349   DEFINE_string(old_version, "",
    350                 "The build version of the old image. 1.2.3, etc.");
    351   DEFINE_string(old_key, "",
    352                 "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
    353                 " etc");
    354   DEFINE_string(old_build_channel, "",
    355                 "The channel for the build of the old image. 'dev-channel', "
    356                 "etc, but will never contain special channels such as "
    357                 "'npo-channel'. Ignored, except during delta generation.");
    358   DEFINE_string(old_build_version, "",
    359                 "The version of the build containing the old image.");
    360 
    361   DEFINE_string(new_channel, "",
    362                 "The channel for the new image. 'dev-channel', 'npo-channel', "
    363                 "etc. Ignored, except during delta generation.");
    364   DEFINE_string(new_board, "",
    365                 "The board for the new image. 'x86-mario', 'lumpy', "
    366                 "etc. Ignored, except during delta generation.");
    367   DEFINE_string(new_version, "",
    368                 "The build version of the new image. 1.2.3, etc.");
    369   DEFINE_string(new_key, "",
    370                 "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
    371                 " etc");
    372   DEFINE_string(new_build_channel, "",
    373                 "The channel for the build of the new image. 'dev-channel', "
    374                 "etc, but will never contain special channels such as "
    375                 "'npo-channel'. Ignored, except during delta generation.");
    376   DEFINE_string(new_build_version, "",
    377                 "The version of the build containing the new image.");
    378   DEFINE_string(new_postinstall_config_file, "",
    379                 "A config file specifying postinstall related metadata. "
    380                 "Only allowed in major version 2 or newer.");
    381 
    382   brillo::FlagHelper::Init(argc, argv,
    383       "Generates a payload to provide to ChromeOS' update_engine.\n\n"
    384       "This tool can create full payloads and also delta payloads if the src\n"
    385       "image is provided. It also provides debugging options to apply, sign\n"
    386       "and verify payloads.");
    387   Terminator::Init();
    388 
    389   logging::LoggingSettings log_settings;
    390   log_settings.log_file     = "delta_generator.log";
    391   log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
    392   log_settings.lock_log     = logging::LOCK_LOG_FILE;
    393   log_settings.delete_old   = logging::APPEND_TO_OLD_LOG_FILE;
    394 
    395   logging::InitLogging(log_settings);
    396 
    397   // Initialize the Xz compressor.
    398   XzCompressInit();
    399 
    400   vector<int> signature_sizes;
    401   ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
    402 
    403   if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
    404     CHECK(FLAGS_out_metadata_size_file.empty());
    405     CalculateHashForSigning(signature_sizes, FLAGS_out_hash_file,
    406                             FLAGS_out_metadata_hash_file, FLAGS_in_file);
    407     return 0;
    408   }
    409   if (!FLAGS_signature_file.empty()) {
    410     SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file,
    411                 FLAGS_metadata_signature_file, FLAGS_out_metadata_size_file);
    412     return 0;
    413   }
    414   if (!FLAGS_public_key.empty()) {
    415     LOG_IF(WARNING, FLAGS_public_key_version != -1)
    416         << "--public_key_version is deprecated and ignored.";
    417     return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
    418   }
    419   if (!FLAGS_properties_file.empty()) {
    420     return ExtractProperties(FLAGS_in_file, FLAGS_properties_file) ? 0 : 1;
    421   }
    422 
    423   // A payload generation was requested. Convert the flags to a
    424   // PayloadGenerationConfig.
    425   PayloadGenerationConfig payload_config;
    426   vector<string> partition_names, old_partitions, new_partitions;
    427   vector<string> old_mapfiles, new_mapfiles;
    428 
    429   if (!FLAGS_old_mapfiles.empty()) {
    430     old_mapfiles = base::SplitString(
    431         FLAGS_old_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
    432   }
    433   if (!FLAGS_new_mapfiles.empty()) {
    434     new_mapfiles = base::SplitString(
    435         FLAGS_new_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
    436   }
    437 
    438   partition_names =
    439       base::SplitString(FLAGS_partition_names, ":", base::TRIM_WHITESPACE,
    440                         base::SPLIT_WANT_ALL);
    441   CHECK(!partition_names.empty());
    442   if (FLAGS_major_version == kChromeOSMajorPayloadVersion ||
    443       FLAGS_new_partitions.empty()) {
    444     LOG_IF(FATAL, partition_names.size() != 2)
    445         << "To support more than 2 partitions, please use the "
    446         << "--new_partitions flag and major version 2.";
    447     LOG_IF(FATAL, partition_names[0] != kLegacyPartitionNameRoot ||
    448                   partition_names[1] != kLegacyPartitionNameKernel)
    449         << "To support non-default partition name, please use the "
    450         << "--new_partitions flag and major version 2.";
    451   }
    452 
    453   if (!FLAGS_new_partitions.empty()) {
    454     LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
    455         << "--new_image and --new_kernel are deprecated, please use "
    456         << "--new_partitions for all partitions.";
    457     new_partitions =
    458         base::SplitString(FLAGS_new_partitions, ":", base::TRIM_WHITESPACE,
    459                           base::SPLIT_WANT_ALL);
    460     CHECK(partition_names.size() == new_partitions.size());
    461 
    462     payload_config.is_delta = !FLAGS_old_partitions.empty();
    463     LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
    464         << "--old_image and --old_kernel are deprecated, please use "
    465         << "--old_partitions if you are using --new_partitions.";
    466   } else {
    467     new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
    468     LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
    469                  << "and --new_kernel flags.";
    470 
    471     payload_config.is_delta = !FLAGS_old_image.empty() ||
    472                               !FLAGS_old_kernel.empty();
    473     LOG_IF(FATAL, !FLAGS_old_partitions.empty())
    474         << "Please use --new_partitions if you are using --old_partitions.";
    475   }
    476   for (size_t i = 0; i < partition_names.size(); i++) {
    477     LOG_IF(FATAL, partition_names[i].empty())
    478         << "Partition name can't be empty, see --partition_names.";
    479     payload_config.target.partitions.emplace_back(partition_names[i]);
    480     payload_config.target.partitions.back().path = new_partitions[i];
    481     if (i < new_mapfiles.size())
    482       payload_config.target.partitions.back().mapfile_path = new_mapfiles[i];
    483   }
    484 
    485   if (payload_config.is_delta) {
    486     if (!FLAGS_old_partitions.empty()) {
    487       old_partitions =
    488           base::SplitString(FLAGS_old_partitions, ":", base::TRIM_WHITESPACE,
    489                             base::SPLIT_WANT_ALL);
    490       CHECK(old_partitions.size() == new_partitions.size());
    491     } else {
    492       old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
    493       LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
    494                    << "and --old_kernel flags.";
    495     }
    496     for (size_t i = 0; i < partition_names.size(); i++) {
    497       payload_config.source.partitions.emplace_back(partition_names[i]);
    498       payload_config.source.partitions.back().path = old_partitions[i];
    499       if (i < old_mapfiles.size())
    500         payload_config.source.partitions.back().mapfile_path = old_mapfiles[i];
    501     }
    502   }
    503 
    504   if (!FLAGS_in_file.empty()) {
    505     return ApplyPayload(FLAGS_in_file, payload_config) ? 0 : 1;
    506   }
    507 
    508   if (!FLAGS_new_postinstall_config_file.empty()) {
    509     LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion)
    510         << "Postinstall config is only allowed in major version 2 or newer.";
    511     brillo::KeyValueStore store;
    512     CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
    513     CHECK(payload_config.target.LoadPostInstallConfig(store));
    514   }
    515 
    516   // Use the default soft_chunk_size defined in the config.
    517   payload_config.hard_chunk_size = FLAGS_chunk_size;
    518   payload_config.block_size = kBlockSize;
    519 
    520   // The partition size is never passed to the delta_generator, so we
    521   // need to detect those from the provided files.
    522   if (payload_config.is_delta) {
    523     CHECK(payload_config.source.LoadImageSize());
    524   }
    525   CHECK(payload_config.target.LoadImageSize());
    526 
    527   CHECK(!FLAGS_out_file.empty());
    528 
    529   // Ignore failures. These are optional arguments.
    530   ParseImageInfo(FLAGS_new_channel,
    531                  FLAGS_new_board,
    532                  FLAGS_new_version,
    533                  FLAGS_new_key,
    534                  FLAGS_new_build_channel,
    535                  FLAGS_new_build_version,
    536                  &payload_config.target.image_info);
    537 
    538   // Ignore failures. These are optional arguments.
    539   ParseImageInfo(FLAGS_old_channel,
    540                  FLAGS_old_board,
    541                  FLAGS_old_version,
    542                  FLAGS_old_key,
    543                  FLAGS_old_build_channel,
    544                  FLAGS_old_build_version,
    545                  &payload_config.source.image_info);
    546 
    547   payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
    548 
    549   if (payload_config.is_delta) {
    550     // Avoid opening the filesystem interface for full payloads.
    551     for (PartitionConfig& part : payload_config.target.partitions)
    552       CHECK(part.OpenFilesystem());
    553     for (PartitionConfig& part : payload_config.source.partitions)
    554       CHECK(part.OpenFilesystem());
    555   }
    556 
    557   payload_config.version.major = FLAGS_major_version;
    558   LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
    559 
    560   if (FLAGS_minor_version == -1) {
    561     // Autodetect minor_version by looking at the update_engine.conf in the old
    562     // image.
    563     if (payload_config.is_delta) {
    564       payload_config.version.minor = kInPlaceMinorPayloadVersion;
    565       brillo::KeyValueStore store;
    566       uint32_t minor_version;
    567       for (const PartitionConfig& part : payload_config.source.partitions) {
    568         if (part.fs_interface && part.fs_interface->LoadSettings(&store) &&
    569             utils::GetMinorVersion(store, &minor_version)) {
    570           payload_config.version.minor = minor_version;
    571           break;
    572         }
    573       }
    574     } else {
    575       payload_config.version.minor = kFullPayloadMinorVersion;
    576     }
    577     LOG(INFO) << "Auto-detected minor_version=" << payload_config.version.minor;
    578   } else {
    579     payload_config.version.minor = FLAGS_minor_version;
    580     LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
    581   }
    582 
    583   payload_config.max_timestamp = FLAGS_max_timestamp;
    584 
    585   LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full")
    586             << " update";
    587 
    588   // From this point, all the options have been parsed.
    589   if (!payload_config.Validate()) {
    590     LOG(ERROR) << "Invalid options passed. See errors above.";
    591     return 1;
    592   }
    593 
    594   uint64_t metadata_size;
    595   if (!GenerateUpdatePayloadFile(payload_config,
    596                                  FLAGS_out_file,
    597                                  FLAGS_private_key,
    598                                  &metadata_size)) {
    599     return 1;
    600   }
    601   if (!FLAGS_out_metadata_size_file.empty()) {
    602     string metadata_size_string = std::to_string(metadata_size);
    603     CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
    604                            metadata_size_string.data(),
    605                            metadata_size_string.size()));
    606   }
    607   return 0;
    608 }
    609 
    610 }  // namespace
    611 
    612 }  // namespace chromeos_update_engine
    613 
    614 int main(int argc, char** argv) {
    615   return chromeos_update_engine::Main(argc, argv);
    616 }
    617