generate_delta_main.cc revision 4b9775a26cd5ff26db61b7762667d607458082f0
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/fake_boot_control.h"
33#include "update_engine/common/fake_hardware.h"
34#include "update_engine/common/prefs.h"
35#include "update_engine/common/terminator.h"
36#include "update_engine/common/utils.h"
37#include "update_engine/payload_consumer/delta_performer.h"
38#include "update_engine/payload_consumer/payload_constants.h"
39#include "update_engine/payload_generator/delta_diff_generator.h"
40#include "update_engine/payload_generator/delta_diff_utils.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
170void 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.
184bool ApplyPayload(const string& payload_file,
185                  // Simply reuses the payload config used for payload
186                  // generation.
187                  const PayloadGenerationConfig& config) {
188  LOG(INFO) << "Applying delta.";
189  FakeBootControl fake_boot_control;
190  FakeHardware fake_hardware;
191  MemoryPrefs prefs;
192  InstallPlan install_plan;
193  install_plan.source_slot =
194      config.is_delta ? 0 : BootControlInterface::kInvalidSlot;
195  install_plan.target_slot = 1;
196  install_plan.payload_type =
197      config.is_delta ? InstallPayloadType::kDelta : InstallPayloadType::kFull;
198
199  for (size_t i = 0; i < config.target.partitions.size(); i++) {
200    InstallPlan::Partition part;
201    part.name = config.target.partitions[i].name;
202    part.target_path = config.target.partitions[i].path;
203    fake_boot_control.SetPartitionDevice(
204        part.name, install_plan.target_slot, part.target_path);
205
206    if (config.is_delta) {
207      TEST_AND_RETURN_FALSE(config.target.partitions.size() ==
208                            config.source.partitions.size());
209      PartitionInfo part_info;
210      TEST_AND_RETURN_FALSE(diff_utils::InitializePartitionInfo(
211          config.source.partitions[i], &part_info));
212      part.source_hash.assign(part_info.hash().begin(), part_info.hash().end());
213      part.source_path = config.source.partitions[i].path;
214
215      fake_boot_control.SetPartitionDevice(
216          part.name, install_plan.source_slot, part.source_path);
217    }
218
219    install_plan.partitions.push_back(part);
220
221    LOG(INFO) << "Install partition:"
222              << " source: " << part.source_path
223              << " target: " << part.target_path;
224
225  }
226
227  DeltaPerformer performer(&prefs,
228                           &fake_boot_control,
229                           &fake_hardware,
230                           nullptr,
231                           &install_plan,
232                           true);  // is_interactive
233
234  brillo::Blob buf(1024 * 1024);
235  int fd = open(payload_file.c_str(), O_RDONLY, 0);
236  CHECK_GE(fd, 0);
237  ScopedFdCloser fd_closer(&fd);
238  for (off_t offset = 0;; offset += buf.size()) {
239    ssize_t bytes_read;
240    CHECK(utils::PReadAll(fd, buf.data(), buf.size(), offset, &bytes_read));
241    if (bytes_read == 0)
242      break;
243    TEST_AND_RETURN_FALSE(performer.Write(buf.data(), bytes_read));
244  }
245  CHECK_EQ(performer.Close(), 0);
246  DeltaPerformer::ResetUpdateProgress(&prefs, false);
247  LOG(INFO) << "Completed applying " << (config.is_delta ? "delta" : "full")
248            << " payload.";
249  return true;
250}
251
252int ExtractProperties(const string& payload_path, const string& props_file) {
253  brillo::KeyValueStore properties;
254  TEST_AND_RETURN_FALSE(
255      PayloadSigner::ExtractPayloadProperties(payload_path, &properties));
256  if (props_file == "-") {
257    printf("%s", properties.SaveToString().c_str());
258  } else {
259    properties.Save(base::FilePath(props_file));
260    LOG(INFO) << "Generated properties file at " << props_file;
261  }
262  return true;
263}
264
265int Main(int argc, char** argv) {
266  DEFINE_string(old_image, "", "Path to the old rootfs");
267  DEFINE_string(new_image, "", "Path to the new rootfs");
268  DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
269  DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
270  DEFINE_string(old_partitions, "",
271                "Path to the old partitions. To pass multiple partitions, use "
272                "a single argument with a colon between paths, e.g. "
273                "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
274                "be empty, but it has to match the order of partition_names.");
275  DEFINE_string(new_partitions, "",
276                "Path to the new partitions. To pass multiple partitions, use "
277                "a single argument with a colon between paths, e.g. "
278                "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
279                "to match the order of partition_names.");
280  DEFINE_string(old_mapfiles,
281                "",
282                "Path to the .map files associated with the partition files "
283                "in the old partition. The .map file is normally generated "
284                "when creating the image in Android builds. Only recommended "
285                "for unsupported filesystem. Pass multiple files separated by "
286                "a colon as with -old_partitions.");
287  DEFINE_string(new_mapfiles,
288                "",
289                "Path to the .map files associated with the partition files "
290                "in the new partition, similar to the -old_mapfiles flag.");
291  DEFINE_string(partition_names,
292                string(kLegacyPartitionNameRoot) + ":" +
293                kLegacyPartitionNameKernel,
294                "Names of the partitions. To pass multiple names, use a single "
295                "argument with a colon between names, e.g. "
296                "name:name2:name3:last_name . Name can not be empty, and it "
297                "has to match the order of partitions.");
298  DEFINE_string(in_file, "",
299                "Path to input delta payload file used to hash/sign payloads "
300                "and apply delta over old_image (for debugging)");
301  DEFINE_string(out_file, "", "Path to output delta payload file");
302  DEFINE_string(out_hash_file, "", "Path to output hash file");
303  DEFINE_string(out_metadata_hash_file, "",
304                "Path to output metadata hash file");
305  DEFINE_string(out_metadata_size_file, "",
306                "Path to output metadata size file");
307  DEFINE_string(private_key, "", "Path to private key in .pem format");
308  DEFINE_string(public_key, "", "Path to public key in .pem format");
309  DEFINE_int32(public_key_version, -1,
310               "DEPRECATED. Key-check version # of client");
311  DEFINE_string(signature_size, "",
312                "Raw signature size used for hash calculation. "
313                "You may pass in multiple sizes by colon separating them. E.g. "
314                "2048:2048:4096 will assume 3 signatures, the first two with "
315                "2048 size and the last 4096.");
316  DEFINE_string(signature_file, "",
317                "Raw signature file to sign payload with. To pass multiple "
318                "signatures, use a single argument with a colon between paths, "
319                "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
320                "signature will be assigned a client version, starting from "
321                "kSignatureOriginalVersion.");
322  DEFINE_string(metadata_signature_file, "",
323                "Raw signature file with the signature of the metadata hash. "
324                "To pass multiple signatures, use a single argument with a "
325                "colon between paths, "
326                "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
327  DEFINE_int32(chunk_size, 200 * 1024 * 1024,
328               "Payload chunk size (-1 for whole files)");
329  DEFINE_uint64(rootfs_partition_size,
330               chromeos_update_engine::kRootFSPartitionSize,
331               "RootFS partition size for the image once installed");
332  DEFINE_uint64(major_version, 1,
333               "The major version of the payload being generated.");
334  DEFINE_int32(minor_version, -1,
335               "The minor version of the payload being generated "
336               "(-1 means autodetect).");
337  DEFINE_string(properties_file, "",
338                "If passed, dumps the payload properties of the payload passed "
339                "in --in_file and exits.");
340
341  DEFINE_string(old_channel, "",
342                "The channel for the old image. 'dev-channel', 'npo-channel', "
343                "etc. Ignored, except during delta generation.");
344  DEFINE_string(old_board, "",
345                "The board for the old image. 'x86-mario', 'lumpy', "
346                "etc. Ignored, except during delta generation.");
347  DEFINE_string(old_version, "",
348                "The build version of the old image. 1.2.3, etc.");
349  DEFINE_string(old_key, "",
350                "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
351                " etc");
352  DEFINE_string(old_build_channel, "",
353                "The channel for the build of the old image. 'dev-channel', "
354                "etc, but will never contain special channels such as "
355                "'npo-channel'. Ignored, except during delta generation.");
356  DEFINE_string(old_build_version, "",
357                "The version of the build containing the old image.");
358
359  DEFINE_string(new_channel, "",
360                "The channel for the new image. 'dev-channel', 'npo-channel', "
361                "etc. Ignored, except during delta generation.");
362  DEFINE_string(new_board, "",
363                "The board for the new image. 'x86-mario', 'lumpy', "
364                "etc. Ignored, except during delta generation.");
365  DEFINE_string(new_version, "",
366                "The build version of the new image. 1.2.3, etc.");
367  DEFINE_string(new_key, "",
368                "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
369                " etc");
370  DEFINE_string(new_build_channel, "",
371                "The channel for the build of the new image. 'dev-channel', "
372                "etc, but will never contain special channels such as "
373                "'npo-channel'. Ignored, except during delta generation.");
374  DEFINE_string(new_build_version, "",
375                "The version of the build containing the new image.");
376  DEFINE_string(new_postinstall_config_file, "",
377                "A config file specifying postinstall related metadata. "
378                "Only allowed in major version 2 or newer.");
379
380  brillo::FlagHelper::Init(argc, argv,
381      "Generates a payload to provide to ChromeOS' update_engine.\n\n"
382      "This tool can create full payloads and also delta payloads if the src\n"
383      "image is provided. It also provides debugging options to apply, sign\n"
384      "and verify payloads.");
385  Terminator::Init();
386
387  logging::LoggingSettings log_settings;
388  log_settings.log_file     = "delta_generator.log";
389  log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
390  log_settings.lock_log     = logging::LOCK_LOG_FILE;
391  log_settings.delete_old   = logging::APPEND_TO_OLD_LOG_FILE;
392
393  logging::InitLogging(log_settings);
394
395  // Initialize the Xz compressor.
396  XzCompressInit();
397
398  vector<int> signature_sizes;
399  ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
400
401  if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
402    CHECK(FLAGS_out_metadata_size_file.empty());
403    CalculateHashForSigning(signature_sizes, FLAGS_out_hash_file,
404                            FLAGS_out_metadata_hash_file, FLAGS_in_file);
405    return 0;
406  }
407  if (!FLAGS_signature_file.empty()) {
408    SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file,
409                FLAGS_metadata_signature_file, FLAGS_out_metadata_size_file);
410    return 0;
411  }
412  if (!FLAGS_public_key.empty()) {
413    LOG_IF(WARNING, FLAGS_public_key_version != -1)
414        << "--public_key_version is deprecated and ignored.";
415    VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
416    return 0;
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    ApplyPayload(FLAGS_in_file, payload_config);
505    return 0;
506  }
507
508  if (!FLAGS_new_postinstall_config_file.empty()) {
509    LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion)
510        << "Postinstall config is only allowed in major version 2 or newer.";
511    brillo::KeyValueStore store;
512    CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
513    CHECK(payload_config.target.LoadPostInstallConfig(store));
514  }
515
516  // Use the default soft_chunk_size defined in the config.
517  payload_config.hard_chunk_size = FLAGS_chunk_size;
518  payload_config.block_size = kBlockSize;
519
520  // The partition size is never passed to the delta_generator, so we
521  // need to detect those from the provided files.
522  if (payload_config.is_delta) {
523    CHECK(payload_config.source.LoadImageSize());
524  }
525  CHECK(payload_config.target.LoadImageSize());
526
527  CHECK(!FLAGS_out_file.empty());
528
529  // Ignore failures. These are optional arguments.
530  ParseImageInfo(FLAGS_new_channel,
531                 FLAGS_new_board,
532                 FLAGS_new_version,
533                 FLAGS_new_key,
534                 FLAGS_new_build_channel,
535                 FLAGS_new_build_version,
536                 &payload_config.target.image_info);
537
538  // Ignore failures. These are optional arguments.
539  ParseImageInfo(FLAGS_old_channel,
540                 FLAGS_old_board,
541                 FLAGS_old_version,
542                 FLAGS_old_key,
543                 FLAGS_old_build_channel,
544                 FLAGS_old_build_version,
545                 &payload_config.source.image_info);
546
547  payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
548
549  if (payload_config.is_delta) {
550    // Avoid opening the filesystem interface for full payloads.
551    for (PartitionConfig& part : payload_config.target.partitions)
552      CHECK(part.OpenFilesystem());
553    for (PartitionConfig& part : payload_config.source.partitions)
554      CHECK(part.OpenFilesystem());
555  }
556
557  payload_config.version.major = FLAGS_major_version;
558  LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
559
560  if (FLAGS_minor_version == -1) {
561    // Autodetect minor_version by looking at the update_engine.conf in the old
562    // image.
563    if (payload_config.is_delta) {
564      payload_config.version.minor = kInPlaceMinorPayloadVersion;
565      brillo::KeyValueStore store;
566      uint32_t minor_version;
567      for (const PartitionConfig& part : payload_config.source.partitions) {
568        if (part.fs_interface && part.fs_interface->LoadSettings(&store) &&
569            utils::GetMinorVersion(store, &minor_version)) {
570          payload_config.version.minor = minor_version;
571          break;
572        }
573      }
574    } else {
575      payload_config.version.minor = kFullPayloadMinorVersion;
576    }
577    LOG(INFO) << "Auto-detected minor_version=" << payload_config.version.minor;
578  } else {
579    payload_config.version.minor = FLAGS_minor_version;
580    LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
581  }
582
583  LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full")
584            << " update";
585
586  // From this point, all the options have been parsed.
587  if (!payload_config.Validate()) {
588    LOG(ERROR) << "Invalid options passed. See errors above.";
589    return 1;
590  }
591
592  uint64_t metadata_size;
593  if (!GenerateUpdatePayloadFile(payload_config,
594                                 FLAGS_out_file,
595                                 FLAGS_private_key,
596                                 &metadata_size)) {
597    return 1;
598  }
599  if (!FLAGS_out_metadata_size_file.empty()) {
600    string metadata_size_string = std::to_string(metadata_size);
601    CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
602                           metadata_size_string.data(),
603                           metadata_size_string.size()));
604  }
605  return 0;
606}
607
608}  // namespace
609
610}  // namespace chromeos_update_engine
611
612int main(int argc, char** argv) {
613  return chromeos_update_engine::Main(argc, argv);
614}
615