generate_delta_main.cc revision 8e768e9359d01008b286db1f6c54a6c3972a56b9
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
50using std::string;
51using std::vector;
52
53namespace chromeos_update_engine {
54
55namespace {
56
57void 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
76bool 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
108void 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
130void 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
142void 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
170int 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.
188bool 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
230  brillo::Blob buf(1024 * 1024);
231  int fd = open(payload_file.c_str(), O_RDONLY, 0);
232  CHECK_GE(fd, 0);
233  ScopedFdCloser fd_closer(&fd);
234  xz_crc32_init();
235  for (off_t offset = 0;; offset += buf.size()) {
236    ssize_t bytes_read;
237    CHECK(utils::PReadAll(fd, buf.data(), buf.size(), offset, &bytes_read));
238    if (bytes_read == 0)
239      break;
240    TEST_AND_RETURN_FALSE(performer.Write(buf.data(), bytes_read));
241  }
242  CHECK_EQ(performer.Close(), 0);
243  DeltaPerformer::ResetUpdateProgress(&prefs, false);
244  LOG(INFO) << "Completed applying " << (config.is_delta ? "delta" : "full")
245            << " payload.";
246  return true;
247}
248
249int ExtractProperties(const string& payload_path, const string& props_file) {
250  brillo::KeyValueStore properties;
251  TEST_AND_RETURN_FALSE(
252      PayloadSigner::ExtractPayloadProperties(payload_path, &properties));
253  if (props_file == "-") {
254    printf("%s", properties.SaveToString().c_str());
255  } else {
256    properties.Save(base::FilePath(props_file));
257    LOG(INFO) << "Generated properties file at " << props_file;
258  }
259  return true;
260}
261
262int Main(int argc, char** argv) {
263  DEFINE_string(old_image, "", "Path to the old rootfs");
264  DEFINE_string(new_image, "", "Path to the new rootfs");
265  DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
266  DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
267  DEFINE_string(old_partitions, "",
268                "Path to the old partitions. To pass multiple partitions, use "
269                "a single argument with a colon between paths, e.g. "
270                "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
271                "be empty, but it has to match the order of partition_names.");
272  DEFINE_string(new_partitions, "",
273                "Path to the new partitions. To pass multiple partitions, use "
274                "a single argument with a colon between paths, e.g. "
275                "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
276                "to match the order of partition_names.");
277  DEFINE_string(old_mapfiles,
278                "",
279                "Path to the .map files associated with the partition files "
280                "in the old partition. The .map file is normally generated "
281                "when creating the image in Android builds. Only recommended "
282                "for unsupported filesystem. Pass multiple files separated by "
283                "a colon as with -old_partitions.");
284  DEFINE_string(new_mapfiles,
285                "",
286                "Path to the .map files associated with the partition files "
287                "in the new partition, similar to the -old_mapfiles flag.");
288  DEFINE_string(partition_names,
289                string(kLegacyPartitionNameRoot) + ":" +
290                kLegacyPartitionNameKernel,
291                "Names of the partitions. To pass multiple names, use a single "
292                "argument with a colon between names, e.g. "
293                "name:name2:name3:last_name . Name can not be empty, and it "
294                "has to match the order of partitions.");
295  DEFINE_string(in_file, "",
296                "Path to input delta payload file used to hash/sign payloads "
297                "and apply delta over old_image (for debugging)");
298  DEFINE_string(out_file, "", "Path to output delta payload file");
299  DEFINE_string(out_hash_file, "", "Path to output hash file");
300  DEFINE_string(out_metadata_hash_file, "",
301                "Path to output metadata hash file");
302  DEFINE_string(out_metadata_size_file, "",
303                "Path to output metadata size file");
304  DEFINE_string(private_key, "", "Path to private key in .pem format");
305  DEFINE_string(public_key, "", "Path to public key in .pem format");
306  DEFINE_int32(public_key_version, -1,
307               "DEPRECATED. Key-check version # of client");
308  DEFINE_string(signature_size, "",
309                "Raw signature size used for hash calculation. "
310                "You may pass in multiple sizes by colon separating them. E.g. "
311                "2048:2048:4096 will assume 3 signatures, the first two with "
312                "2048 size and the last 4096.");
313  DEFINE_string(signature_file, "",
314                "Raw signature file to sign payload with. To pass multiple "
315                "signatures, use a single argument with a colon between paths, "
316                "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
317                "signature will be assigned a client version, starting from "
318                "kSignatureOriginalVersion.");
319  DEFINE_string(metadata_signature_file, "",
320                "Raw signature file with the signature of the metadata hash. "
321                "To pass multiple signatures, use a single argument with a "
322                "colon between paths, "
323                "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
324  DEFINE_int32(chunk_size, 200 * 1024 * 1024,
325               "Payload chunk size (-1 for whole files)");
326  DEFINE_uint64(rootfs_partition_size,
327               chromeos_update_engine::kRootFSPartitionSize,
328               "RootFS partition size for the image once installed");
329  DEFINE_uint64(major_version, 1,
330               "The major version of the payload being generated.");
331  DEFINE_int32(minor_version, -1,
332               "The minor version of the payload being generated "
333               "(-1 means autodetect).");
334  DEFINE_string(properties_file, "",
335                "If passed, dumps the payload properties of the payload passed "
336                "in --in_file and exits.");
337  DEFINE_int64(max_timestamp,
338               0,
339               "The maximum timestamp of the OS allowed to apply this "
340               "payload.");
341
342  DEFINE_string(old_channel, "",
343                "The channel for the old image. 'dev-channel', 'npo-channel', "
344                "etc. Ignored, except during delta generation.");
345  DEFINE_string(old_board, "",
346                "The board for the old image. 'x86-mario', 'lumpy', "
347                "etc. Ignored, except during delta generation.");
348  DEFINE_string(old_version, "",
349                "The build version of the old image. 1.2.3, etc.");
350  DEFINE_string(old_key, "",
351                "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
352                " etc");
353  DEFINE_string(old_build_channel, "",
354                "The channel for the build of the old image. 'dev-channel', "
355                "etc, but will never contain special channels such as "
356                "'npo-channel'. Ignored, except during delta generation.");
357  DEFINE_string(old_build_version, "",
358                "The version of the build containing the old image.");
359
360  DEFINE_string(new_channel, "",
361                "The channel for the new image. 'dev-channel', 'npo-channel', "
362                "etc. Ignored, except during delta generation.");
363  DEFINE_string(new_board, "",
364                "The board for the new image. 'x86-mario', 'lumpy', "
365                "etc. Ignored, except during delta generation.");
366  DEFINE_string(new_version, "",
367                "The build version of the new image. 1.2.3, etc.");
368  DEFINE_string(new_key, "",
369                "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
370                " etc");
371  DEFINE_string(new_build_channel, "",
372                "The channel for the build of the new image. 'dev-channel', "
373                "etc, but will never contain special channels such as "
374                "'npo-channel'. Ignored, except during delta generation.");
375  DEFINE_string(new_build_version, "",
376                "The version of the build containing the new image.");
377  DEFINE_string(new_postinstall_config_file, "",
378                "A config file specifying postinstall related metadata. "
379                "Only allowed in major version 2 or newer.");
380
381  brillo::FlagHelper::Init(argc, argv,
382      "Generates a payload to provide to ChromeOS' update_engine.\n\n"
383      "This tool can create full payloads and also delta payloads if the src\n"
384      "image is provided. It also provides debugging options to apply, sign\n"
385      "and verify payloads.");
386  Terminator::Init();
387
388  logging::LoggingSettings log_settings;
389  log_settings.log_file     = "delta_generator.log";
390  log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
391  log_settings.lock_log     = logging::LOCK_LOG_FILE;
392  log_settings.delete_old   = logging::APPEND_TO_OLD_LOG_FILE;
393
394  logging::InitLogging(log_settings);
395
396  // Initialize the Xz compressor.
397  XzCompressInit();
398
399  vector<int> signature_sizes;
400  ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
401
402  if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
403    CHECK(FLAGS_out_metadata_size_file.empty());
404    CalculateHashForSigning(signature_sizes, FLAGS_out_hash_file,
405                            FLAGS_out_metadata_hash_file, FLAGS_in_file);
406    return 0;
407  }
408  if (!FLAGS_signature_file.empty()) {
409    SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file,
410                FLAGS_metadata_signature_file, FLAGS_out_metadata_size_file);
411    return 0;
412  }
413  if (!FLAGS_public_key.empty()) {
414    LOG_IF(WARNING, FLAGS_public_key_version != -1)
415        << "--public_key_version is deprecated and ignored.";
416    return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
417  }
418  if (!FLAGS_properties_file.empty()) {
419    return ExtractProperties(FLAGS_in_file, FLAGS_properties_file) ? 0 : 1;
420  }
421
422  // A payload generation was requested. Convert the flags to a
423  // PayloadGenerationConfig.
424  PayloadGenerationConfig payload_config;
425  vector<string> partition_names, old_partitions, new_partitions;
426  vector<string> old_mapfiles, new_mapfiles;
427
428  if (!FLAGS_old_mapfiles.empty()) {
429    old_mapfiles = base::SplitString(
430        FLAGS_old_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
431  }
432  if (!FLAGS_new_mapfiles.empty()) {
433    new_mapfiles = base::SplitString(
434        FLAGS_new_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
435  }
436
437  partition_names =
438      base::SplitString(FLAGS_partition_names, ":", base::TRIM_WHITESPACE,
439                        base::SPLIT_WANT_ALL);
440  CHECK(!partition_names.empty());
441  if (FLAGS_major_version == kChromeOSMajorPayloadVersion ||
442      FLAGS_new_partitions.empty()) {
443    LOG_IF(FATAL, partition_names.size() != 2)
444        << "To support more than 2 partitions, please use the "
445        << "--new_partitions flag and major version 2.";
446    LOG_IF(FATAL, partition_names[0] != kLegacyPartitionNameRoot ||
447                  partition_names[1] != kLegacyPartitionNameKernel)
448        << "To support non-default partition name, please use the "
449        << "--new_partitions flag and major version 2.";
450  }
451
452  if (!FLAGS_new_partitions.empty()) {
453    LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
454        << "--new_image and --new_kernel are deprecated, please use "
455        << "--new_partitions for all partitions.";
456    new_partitions =
457        base::SplitString(FLAGS_new_partitions, ":", base::TRIM_WHITESPACE,
458                          base::SPLIT_WANT_ALL);
459    CHECK(partition_names.size() == new_partitions.size());
460
461    payload_config.is_delta = !FLAGS_old_partitions.empty();
462    LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
463        << "--old_image and --old_kernel are deprecated, please use "
464        << "--old_partitions if you are using --new_partitions.";
465  } else {
466    new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
467    LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
468                 << "and --new_kernel flags.";
469
470    payload_config.is_delta = !FLAGS_old_image.empty() ||
471                              !FLAGS_old_kernel.empty();
472    LOG_IF(FATAL, !FLAGS_old_partitions.empty())
473        << "Please use --new_partitions if you are using --old_partitions.";
474  }
475  for (size_t i = 0; i < partition_names.size(); i++) {
476    LOG_IF(FATAL, partition_names[i].empty())
477        << "Partition name can't be empty, see --partition_names.";
478    payload_config.target.partitions.emplace_back(partition_names[i]);
479    payload_config.target.partitions.back().path = new_partitions[i];
480    if (i < new_mapfiles.size())
481      payload_config.target.partitions.back().mapfile_path = new_mapfiles[i];
482  }
483
484  if (payload_config.is_delta) {
485    if (!FLAGS_old_partitions.empty()) {
486      old_partitions =
487          base::SplitString(FLAGS_old_partitions, ":", base::TRIM_WHITESPACE,
488                            base::SPLIT_WANT_ALL);
489      CHECK(old_partitions.size() == new_partitions.size());
490    } else {
491      old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
492      LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
493                   << "and --old_kernel flags.";
494    }
495    for (size_t i = 0; i < partition_names.size(); i++) {
496      payload_config.source.partitions.emplace_back(partition_names[i]);
497      payload_config.source.partitions.back().path = old_partitions[i];
498      if (i < old_mapfiles.size())
499        payload_config.source.partitions.back().mapfile_path = old_mapfiles[i];
500    }
501  }
502
503  if (!FLAGS_in_file.empty()) {
504    return ApplyPayload(FLAGS_in_file, payload_config) ? 0 : 1;
505  }
506
507  if (!FLAGS_new_postinstall_config_file.empty()) {
508    LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion)
509        << "Postinstall config is only allowed in major version 2 or newer.";
510    brillo::KeyValueStore store;
511    CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
512    CHECK(payload_config.target.LoadPostInstallConfig(store));
513  }
514
515  // Use the default soft_chunk_size defined in the config.
516  payload_config.hard_chunk_size = FLAGS_chunk_size;
517  payload_config.block_size = kBlockSize;
518
519  // The partition size is never passed to the delta_generator, so we
520  // need to detect those from the provided files.
521  if (payload_config.is_delta) {
522    CHECK(payload_config.source.LoadImageSize());
523  }
524  CHECK(payload_config.target.LoadImageSize());
525
526  CHECK(!FLAGS_out_file.empty());
527
528  // Ignore failures. These are optional arguments.
529  ParseImageInfo(FLAGS_new_channel,
530                 FLAGS_new_board,
531                 FLAGS_new_version,
532                 FLAGS_new_key,
533                 FLAGS_new_build_channel,
534                 FLAGS_new_build_version,
535                 &payload_config.target.image_info);
536
537  // Ignore failures. These are optional arguments.
538  ParseImageInfo(FLAGS_old_channel,
539                 FLAGS_old_board,
540                 FLAGS_old_version,
541                 FLAGS_old_key,
542                 FLAGS_old_build_channel,
543                 FLAGS_old_build_version,
544                 &payload_config.source.image_info);
545
546  payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
547
548  if (payload_config.is_delta) {
549    // Avoid opening the filesystem interface for full payloads.
550    for (PartitionConfig& part : payload_config.target.partitions)
551      CHECK(part.OpenFilesystem());
552    for (PartitionConfig& part : payload_config.source.partitions)
553      CHECK(part.OpenFilesystem());
554  }
555
556  payload_config.version.major = FLAGS_major_version;
557  LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
558
559  if (FLAGS_minor_version == -1) {
560    // Autodetect minor_version by looking at the update_engine.conf in the old
561    // image.
562    if (payload_config.is_delta) {
563      payload_config.version.minor = kInPlaceMinorPayloadVersion;
564      brillo::KeyValueStore store;
565      uint32_t minor_version;
566      for (const PartitionConfig& part : payload_config.source.partitions) {
567        if (part.fs_interface && part.fs_interface->LoadSettings(&store) &&
568            utils::GetMinorVersion(store, &minor_version)) {
569          payload_config.version.minor = minor_version;
570          break;
571        }
572      }
573    } else {
574      payload_config.version.minor = kFullPayloadMinorVersion;
575    }
576    LOG(INFO) << "Auto-detected minor_version=" << payload_config.version.minor;
577  } else {
578    payload_config.version.minor = FLAGS_minor_version;
579    LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
580  }
581
582  payload_config.max_timestamp = FLAGS_max_timestamp;
583
584  LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full")
585            << " update";
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
613int main(int argc, char** argv) {
614  return chromeos_update_engine::Main(argc, argv);
615}
616