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