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