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