xref: /aosp_15_r20/system/update_engine/payload_generator/generate_delta_main.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
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 <cstring>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include <android-base/strings.h>
23 #include <base/bind.h>
24 #include <base/files/file_path.h>
25 #include <base/files/file_util.h>
26 #include <base/logging.h>
27 #include <base/strings/string_number_conversions.h>
28 #include <base/strings/string_split.h>
29 #include <brillo/key_value_store.h>
30 #include <brillo/message_loops/base_message_loop.h>
31 #include <unistd.h>
32 #include <xz.h>
33 #include <gflags/gflags.h>
34 
35 #include "update_engine/common/download_action.h"
36 #include "update_engine/common/fake_boot_control.h"
37 #include "update_engine/common/fake_hardware.h"
38 #include "update_engine/common/file_fetcher.h"
39 #include "update_engine/common/prefs.h"
40 #include "update_engine/common/terminator.h"
41 #include "update_engine/common/utils.h"
42 #include "update_engine/payload_consumer/filesystem_verifier_action.h"
43 #include "update_engine/payload_consumer/payload_constants.h"
44 #include "update_engine/payload_generator/delta_diff_generator.h"
45 #include "update_engine/payload_generator/payload_generation_config.h"
46 #include "update_engine/payload_generator/payload_properties.h"
47 #include "update_engine/payload_generator/payload_signer.h"
48 #include "update_engine/payload_generator/xz.h"
49 #include "update_engine/update_metadata.pb.h"
50 
51 // This file contains a simple program that takes an old path, a new path,
52 // and an output file as arguments and the path to an output file and
53 // generates a delta that can be sent to Chrome OS clients.
54 
55 using std::map;
56 using std::string;
57 using std::vector;
58 
59 namespace chromeos_update_engine {
60 
61 namespace {
62 
63 constexpr char kPayloadPropertiesFormatKeyValue[] = "key-value";
64 constexpr char kPayloadPropertiesFormatJson[] = "json";
65 
ParseSignatureSizes(const string & signature_sizes_flag,vector<size_t> * signature_sizes)66 void ParseSignatureSizes(const string& signature_sizes_flag,
67                          vector<size_t>* signature_sizes) {
68   signature_sizes->clear();
69   vector<string> split_strings = base::SplitString(
70       signature_sizes_flag, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
71   for (const string& str : split_strings) {
72     size_t size = 0;
73     bool parsing_successful = base::StringToSizeT(str, &size);
74     LOG_IF(FATAL, !parsing_successful) << "Invalid signature size: " << str;
75 
76     signature_sizes->push_back(size);
77   }
78 }
79 
CalculateHashForSigning(const vector<size_t> & sizes,const string & out_hash_file,const string & out_metadata_hash_file,const string & in_file)80 void CalculateHashForSigning(const vector<size_t>& sizes,
81                              const string& out_hash_file,
82                              const string& out_metadata_hash_file,
83                              const string& in_file) {
84   LOG(INFO) << "Calculating hash for signing.";
85   LOG_IF(FATAL, in_file.empty())
86       << "Must pass --in_file to calculate hash for signing.";
87   LOG_IF(FATAL, out_hash_file.empty())
88       << "Must pass --out_hash_file to calculate hash for signing.";
89 
90   brillo::Blob payload_hash, metadata_hash;
91   CHECK(PayloadSigner::HashPayloadForSigning(
92       in_file, sizes, &payload_hash, &metadata_hash));
93   CHECK(utils::WriteFile(
94       out_hash_file.c_str(), payload_hash.data(), payload_hash.size()));
95   if (!out_metadata_hash_file.empty())
96     CHECK(utils::WriteFile(out_metadata_hash_file.c_str(),
97                            metadata_hash.data(),
98                            metadata_hash.size()));
99 
100   LOG(INFO) << "Done calculating hash for signing.";
101 }
102 
SignatureFileFlagToBlobs(const string & signature_file_flag,vector<brillo::Blob> * signatures)103 void SignatureFileFlagToBlobs(const string& signature_file_flag,
104                               vector<brillo::Blob>* signatures) {
105   vector<string> signature_files = base::SplitString(
106       signature_file_flag, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
107   for (const string& signature_file : signature_files) {
108     brillo::Blob signature;
109     CHECK(utils::ReadFile(signature_file, &signature));
110     signatures->push_back(signature);
111   }
112 }
113 
SignPayload(const string & in_file,const string & out_file,const vector<size_t> & signature_sizes,const string & payload_signature_file,const string & metadata_signature_file,const string & out_metadata_size_file)114 void SignPayload(const string& in_file,
115                  const string& out_file,
116                  const vector<size_t>& signature_sizes,
117                  const string& payload_signature_file,
118                  const string& metadata_signature_file,
119                  const string& out_metadata_size_file) {
120   LOG(INFO) << "Signing payload.";
121   LOG_IF(FATAL, in_file.empty()) << "Must pass --in_file to sign payload.";
122   LOG_IF(FATAL, out_file.empty()) << "Must pass --out_file to sign payload.";
123   LOG_IF(FATAL, payload_signature_file.empty())
124       << "Must pass --payload_signature_file to sign payload.";
125   vector<brillo::Blob> payload_signatures, metadata_signatures;
126   SignatureFileFlagToBlobs(payload_signature_file, &payload_signatures);
127   SignatureFileFlagToBlobs(metadata_signature_file, &metadata_signatures);
128   uint64_t final_metadata_size{};
129   CHECK(PayloadSigner::AddSignatureToPayload(in_file,
130                                              signature_sizes,
131                                              payload_signatures,
132                                              metadata_signatures,
133                                              out_file,
134                                              &final_metadata_size));
135   LOG(INFO) << "Done signing payload. Final metadata size = "
136             << final_metadata_size;
137   if (!out_metadata_size_file.empty()) {
138     string metadata_size_string = std::to_string(final_metadata_size);
139     CHECK(utils::WriteFile(out_metadata_size_file.c_str(),
140                            metadata_size_string.data(),
141                            metadata_size_string.size()));
142   }
143 }
144 
VerifySignedPayload(const string & in_file,const string & public_key)145 int VerifySignedPayload(const string& in_file, const string& public_key) {
146   LOG(INFO) << "Verifying signed payload.";
147   LOG_IF(FATAL, in_file.empty())
148       << "Must pass --in_file to verify signed payload.";
149   LOG_IF(FATAL, public_key.empty())
150       << "Must pass --public_key to verify signed payload.";
151   if (!PayloadSigner::VerifySignedPayload(in_file, public_key)) {
152     LOG(INFO) << "VerifySignedPayload failed";
153     return 1;
154   }
155 
156   LOG(INFO) << "Done verifying signed payload.";
157   return 0;
158 }
159 
160 class ApplyPayloadProcessorDelegate : public ActionProcessorDelegate {
161  public:
ProcessingDone(const ActionProcessor * processor,ErrorCode code)162   void ProcessingDone(const ActionProcessor* processor,
163                       ErrorCode code) override {
164     brillo::MessageLoop::current()->BreakLoop();
165     code_ = code;
166   }
ProcessingStopped(const ActionProcessor * processor)167   void ProcessingStopped(const ActionProcessor* processor) override {
168     brillo::MessageLoop::current()->BreakLoop();
169   }
170   ErrorCode code_{};
171 };
172 
173 // TODO(deymo): Move this function to a new file and make the delta_performer
174 // integration tests use this instead.
ApplyPayload(const string & payload_file,const PayloadGenerationConfig & config)175 bool ApplyPayload(const string& payload_file,
176                   // Simply reuses the payload config used for payload
177                   // generation.
178                   const PayloadGenerationConfig& config) {
179   LOG(INFO) << "Applying delta.";
180   FakeBootControl fake_boot_control;
181   FakeHardware fake_hardware;
182   MemoryPrefs prefs;
183   InstallPlan install_plan;
184   InstallPlan::Payload payload;
185   install_plan.source_slot =
186       config.is_delta ? 0 : BootControlInterface::kInvalidSlot;
187   install_plan.target_slot = 1;
188   // For partial updates, we always write kDelta to the payload. Make it
189   // consistent for host simulation.
190   payload.type = config.is_delta || config.is_partial_update
191                      ? InstallPayloadType::kDelta
192                      : InstallPayloadType::kFull;
193   payload.size = utils::FileSize(payload_file);
194   // TODO(senj): This hash is only correct for unsigned payload, need to support
195   // signed payload using PayloadSigner.
196   HashCalculator::RawHashOfFile(payload_file, payload.size, &payload.hash);
197   install_plan.payloads = {payload};
198   install_plan.download_url =
199       "file://" +
200       base::MakeAbsoluteFilePath(base::FilePath(payload_file)).value();
201 
202   for (size_t i = 0; i < config.target.partitions.size(); i++) {
203     const string& part_name = config.target.partitions[i].name;
204     const string& target_path = config.target.partitions[i].path;
205     fake_boot_control.SetPartitionDevice(
206         part_name, install_plan.target_slot, target_path);
207 
208     string source_path;
209     if (config.is_delta) {
210       TEST_AND_RETURN_FALSE(config.target.partitions.size() ==
211                             config.source.partitions.size());
212       source_path = config.source.partitions[i].path;
213       fake_boot_control.SetPartitionDevice(
214           part_name, install_plan.source_slot, source_path);
215     }
216 
217     LOG(INFO) << "Install partition:"
218               << " source: " << source_path << "\ttarget: " << target_path;
219   }
220 
221   xz_crc32_init();
222   brillo::BaseMessageLoop loop;
223   loop.SetAsCurrent();
224   auto install_plan_action = std::make_unique<InstallPlanAction>(install_plan);
225   auto download_action =
226       std::make_unique<DownloadAction>(&prefs,
227                                        &fake_boot_control,
228                                        &fake_hardware,
229                                        new FileFetcher(),
230                                        true /* interactive */);
231   auto filesystem_verifier_action = std::make_unique<FilesystemVerifierAction>(
232       fake_boot_control.GetDynamicPartitionControl());
233 
234   BondActions(install_plan_action.get(), download_action.get());
235   BondActions(download_action.get(), filesystem_verifier_action.get());
236   ActionProcessor processor;
237   ApplyPayloadProcessorDelegate delegate;
238   processor.set_delegate(&delegate);
239   processor.EnqueueAction(std::move(install_plan_action));
240   processor.EnqueueAction(std::move(download_action));
241   processor.EnqueueAction(std::move(filesystem_verifier_action));
242   loop.PostTask(FROM_HERE,
243                 base::Bind(&ActionProcessor::StartProcessing,
244                            base::Unretained(&processor)));
245   loop.Run();
246   CHECK_EQ(delegate.code_, ErrorCode::kSuccess);
247   LOG(INFO) << "Completed applying " << (config.is_delta ? "delta" : "full")
248             << " payload.";
249   return true;
250 }
251 
ExtractProperties(const string & payload_path,const string & props_file,const string & props_format)252 bool ExtractProperties(const string& payload_path,
253                        const string& props_file,
254                        const string& props_format) {
255   string properties;
256   PayloadProperties payload_props(payload_path);
257   if (props_format == kPayloadPropertiesFormatKeyValue) {
258     TEST_AND_RETURN_FALSE(payload_props.GetPropertiesAsKeyValue(&properties));
259   } else if (props_format == kPayloadPropertiesFormatJson) {
260     TEST_AND_RETURN_FALSE(payload_props.GetPropertiesAsJson(&properties));
261   } else {
262     LOG(FATAL) << "Invalid option " << props_format
263                << " for --properties_format flag.";
264   }
265   if (props_file == "-") {
266     printf("%s", properties.c_str());
267   } else {
268     utils::WriteFile(
269         props_file.c_str(), properties.c_str(), properties.length());
270     LOG(INFO) << "Generated properties file at " << props_file;
271   }
272   return true;
273 }
274 
275 template <typename Key, typename Val>
ToString(const map<Key,Val> & map)276 string ToString(const map<Key, Val>& map) {
277   vector<string> result;
278   result.reserve(map.size());
279   for (const auto& it : map) {
280     result.emplace_back(it.first + ": " + it.second);
281   }
282   return "{" + android::base::Join(result, ",") + "}";
283 }
284 
ParsePerPartitionTimestamps(const string & partition_timestamps,PayloadGenerationConfig * config)285 bool ParsePerPartitionTimestamps(const string& partition_timestamps,
286                                  PayloadGenerationConfig* config) {
287   base::StringPairs pairs;
288   CHECK(base::SplitStringIntoKeyValuePairs(
289       partition_timestamps, ':', ',', &pairs))
290       << "--partition_timestamps accepts commad "
291          "separated pairs. e.x. system:1234,vendor:5678";
292   map<string, string> partition_timestamps_map{
293       std::move_iterator(pairs.begin()), std::move_iterator(pairs.end())};
294   for (auto&& partition : config->target.partitions) {
295     auto&& it = partition_timestamps_map.find(partition.name);
296     if (it != partition_timestamps_map.end()) {
297       partition.version = std::move(it->second);
298       partition_timestamps_map.erase(it);
299     }
300   }
301   if (!partition_timestamps_map.empty()) {
302     LOG(ERROR) << "Unused timestamps: " << ToString(partition_timestamps_map);
303     return false;
304   }
305   return true;
306 }
307 
308 DEFINE_string(old_image, "", "Path to the old rootfs");
309 DEFINE_string(new_image, "", "Path to the new rootfs");
310 DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
311 DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
312 DEFINE_string(old_partitions,
313               "",
314               "Path to the old partitions. To pass multiple partitions, use "
315               "a single argument with a colon between paths, e.g. "
316               "/path/to/part:/path/to/part2::/path/to/last_part . Path can "
317               "be empty, but it has to match the order of partition_names.");
318 DEFINE_string(new_partitions,
319               "",
320               "Path to the new partitions. To pass multiple partitions, use "
321               "a single argument with a colon between paths, e.g. "
322               "/path/to/part:/path/to/part2:/path/to/last_part . Path has "
323               "to match the order of partition_names.");
324 DEFINE_string(old_mapfiles,
325               "",
326               "Path to the .map files associated with the partition files "
327               "in the old partition. The .map file is normally generated "
328               "when creating the image in Android builds. Only recommended "
329               "for unsupported filesystem. Pass multiple files separated by "
330               "a colon as with -old_partitions.");
331 DEFINE_string(new_mapfiles,
332               "",
333               "Path to the .map files associated with the partition files "
334               "in the new partition, similar to the -old_mapfiles flag.");
335 DEFINE_string(partition_names,
336               string(kPartitionNameRoot) + ":" + kPartitionNameKernel,
337               "Names of the partitions. To pass multiple names, use a single "
338               "argument with a colon between names, e.g. "
339               "name:name2:name3:last_name . Name can not be empty, and it "
340               "has to match the order of partitions.");
341 DEFINE_string(in_file,
342               "",
343               "Path to input delta payload file used to hash/sign payloads "
344               "and apply delta over old_image (for debugging)");
345 DEFINE_string(out_file, "", "Path to output delta payload file");
346 DEFINE_string(out_hash_file, "", "Path to output hash file");
347 DEFINE_string(out_metadata_hash_file, "", "Path to output metadata hash file");
348 DEFINE_string(out_metadata_size_file, "", "Path to output metadata size file");
349 DEFINE_string(private_key, "", "Path to private key in .pem format");
350 DEFINE_string(public_key, "", "Path to public key in .pem format");
351 DEFINE_int32(public_key_version,
352              -1,
353              "DEPRECATED. Key-check version # of client");
354 DEFINE_string(signature_size,
355               "",
356               "Raw signature size used for hash calculation. "
357               "You may pass in multiple sizes by colon separating them. E.g. "
358               "2048:2048:4096 will assume 3 signatures, the first two with "
359               "2048 size and the last 4096.");
360 DEFINE_string(payload_signature_file,
361               "",
362               "Raw signature file to sign payload with. To pass multiple "
363               "signatures, use a single argument with a colon between paths, "
364               "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
365               "signature will be assigned a client version, starting from "
366               "kSignatureOriginalVersion.");
367 DEFINE_string(metadata_signature_file,
368               "",
369               "Raw signature file with the signature of the metadata hash. "
370               "To pass multiple signatures, use a single argument with a "
371               "colon between paths, "
372               "e.g. /path/to/sig:/path/to/next:/path/to/last_sig .");
373 DEFINE_int32(chunk_size,
374              200 * 1024 * 1024,
375              "Payload chunk size (-1 for whole files)");
376 DEFINE_uint64(rootfs_partition_size,
377               chromeos_update_engine::kRootFSPartitionSize,
378               "RootFS partition size for the image once installed");
379 DEFINE_uint64(major_version,
380               2,
381               "The major version of the payload being generated.");
382 DEFINE_int32(minor_version,
383              -1,
384              "The minor version of the payload being generated "
385              "(-1 means autodetect).");
386 DEFINE_string(properties_file,
387               "",
388               "If passed, dumps the payload properties of the payload passed "
389               "in --in_file and exits. Look at --properties_format.");
390 DEFINE_string(properties_format,
391               kPayloadPropertiesFormatKeyValue,
392               "Defines the format of the --properties_file. The acceptable "
393               "values are: key-value (default) and json");
394 DEFINE_int64(max_timestamp,
395              0,
396              "The maximum timestamp of the OS allowed to apply this "
397              "payload.");
398 DEFINE_string(security_patch_level,
399               "",
400               "The security patch level of this OTA. Devices with a newer SPL "
401               "will not be allowed to apply this payload");
402 DEFINE_string(
403     partition_timestamps,
404     "",
405     "The per-partition maximum timestamps which the OS allowed to apply this "
406     "payload. Passed in comma separated pairs, e.x. system:1234,vendor:5678");
407 
408 DEFINE_string(new_postinstall_config_file,
409               "",
410               "A config file specifying postinstall related metadata. "
411               "Only allowed in major version 2 or newer.");
412 DEFINE_string(dynamic_partition_info_file,
413               "",
414               "An info file specifying dynamic partition metadata. "
415               "Only allowed in major version 2 or newer.");
416 DEFINE_bool(disable_fec_computation,
417             false,
418             "Disables the fec data computation on device.");
419 DEFINE_bool(disable_verity_computation,
420             false,
421             "Disables the verity data computation on device.");
422 DEFINE_string(out_maximum_signature_size_file,
423               "",
424               "Path to the output maximum signature size given a private key.");
425 DEFINE_bool(is_partial_update,
426             false,
427             "The payload only targets a subset of partitions on the device,"
428             "e.g. generic kernel image update.");
429 DEFINE_bool(
430     disable_vabc,
431     false,
432     "Whether to disable Virtual AB Compression when installing the OTA");
433 DEFINE_bool(enable_vabc_xor,
434             false,
435             "Whether to use Virtual AB Compression XOR feature");
436 DEFINE_string(apex_info_file,
437               "",
438               "Path to META/apex_info.pb found in target build");
439 DEFINE_string(compressor_types,
440               "bz2:brotli",
441               "Colon ':' separated list of compressors. Allowed valures are "
442               "bz2 and brotli.");
443 DEFINE_bool(enable_lz4diff,
444             false,
445             "Whether to enable LZ4diff feature when processing EROFS images.");
446 
447 DEFINE_bool(enable_puffdiff,
448             true,
449             "Whether to enable puffdiff feature. Enabling puffdiff will take "
450             "longer but generated OTA will be smaller.");
451 
452 DEFINE_bool(
453     enable_zucchini,
454     true,
455     "Whether to enable zucchini feature when processing executable files.");
456 
457 DEFINE_string(erofs_compression_param,
458               "",
459               "Compression parameter passed to mkfs.erofs's -z option. "
460               "Example: lz4 lz4hc,9");
461 
462 DEFINE_int64(max_threads,
463              0,
464              "The maximum number of threads allowed for generating "
465              "ota.");
466 
RoundDownPartitions(const ImageConfig & config)467 void RoundDownPartitions(const ImageConfig& config) {
468   for (const auto& part : config.partitions) {
469     if (part.path.empty()) {
470       continue;
471     }
472     const auto size = std::max<size_t>(utils::FileSize(part.path), kBlockSize);
473     if (size % kBlockSize != 0) {
474       const auto err =
475           truncate(part.path.c_str(), size / kBlockSize * kBlockSize);
476       CHECK_EQ(err, 0) << "Failed to truncate " << part.path << ", error "
477                        << strerror(errno);
478     }
479   }
480 }
481 
RoundUpPartitions(const ImageConfig & config)482 void RoundUpPartitions(const ImageConfig& config) {
483   for (const auto& part : config.partitions) {
484     if (part.path.empty()) {
485       continue;
486     }
487     const auto size = utils::FileSize(part.path);
488     if (size % kBlockSize != 0) {
489       const auto err = truncate(
490           part.path.c_str(), (size + kBlockSize - 1) / kBlockSize * kBlockSize);
491       CHECK_EQ(err, 0) << "Failed to truncate " << part.path << ", error "
492                        << strerror(errno);
493     }
494   }
495 }
496 
Main(int argc,char ** argv)497 int Main(int argc, char** argv) {
498   gflags::SetUsageMessage(
499       "Generates a payload to provide to ChromeOS' update_engine.\n\n"
500       "This tool can create full payloads and also delta payloads if the src\n"
501       "image is provided. It also provides debugging options to apply, sign\n"
502       "and verify payloads.");
503   gflags::ParseCommandLineFlags(&argc, &argv, true);
504   CHECK_EQ(argc, 1) << " Unused args: "
505                     << android::base::Join(
506                            std::vector<char*>(argv + 1, argv + argc), " ");
507 
508   Terminator::Init();
509 
510   logging::LoggingSettings log_settings;
511 #if BASE_VER < 780000
512   log_settings.log_file = "delta_generator.log";
513 #else
514   log_settings.log_file_path = "delta_generator.log";
515 #endif
516   log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
517   log_settings.lock_log = logging::LOCK_LOG_FILE;
518   log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
519 
520   logging::InitLogging(log_settings);
521 
522   // Initialize the Xz compressor.
523   XzCompressInit();
524 
525   if (!FLAGS_out_maximum_signature_size_file.empty()) {
526     LOG_IF(FATAL, FLAGS_private_key.empty())
527         << "Private key is not provided when calculating the maximum signature "
528            "size.";
529 
530     size_t maximum_signature_size{};
531     if (!PayloadSigner::GetMaximumSignatureSize(FLAGS_private_key,
532                                                 &maximum_signature_size)) {
533       LOG(ERROR) << "Failed to get the maximum signature size of private key: "
534                  << FLAGS_private_key;
535       return 1;
536     }
537     // Write the size string to output file.
538     string signature_size_string = std::to_string(maximum_signature_size);
539     if (!utils::WriteFile(FLAGS_out_maximum_signature_size_file.c_str(),
540                           signature_size_string.c_str(),
541                           signature_size_string.size())) {
542       PLOG(ERROR) << "Failed to write the maximum signature size to "
543                   << FLAGS_out_maximum_signature_size_file << ".";
544       return 1;
545     }
546     return 0;
547   }
548 
549   vector<size_t> signature_sizes;
550   if (!FLAGS_signature_size.empty()) {
551     ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
552   }
553 
554   if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
555     CHECK(FLAGS_out_metadata_size_file.empty());
556     CalculateHashForSigning(signature_sizes,
557                             FLAGS_out_hash_file,
558                             FLAGS_out_metadata_hash_file,
559                             FLAGS_in_file);
560     return 0;
561   }
562   if (!FLAGS_payload_signature_file.empty()) {
563     SignPayload(FLAGS_in_file,
564                 FLAGS_out_file,
565                 signature_sizes,
566                 FLAGS_payload_signature_file,
567                 FLAGS_metadata_signature_file,
568                 FLAGS_out_metadata_size_file);
569     return 0;
570   }
571   if (!FLAGS_public_key.empty()) {
572     LOG_IF(WARNING, FLAGS_public_key_version != -1)
573         << "--public_key_version is deprecated and ignored.";
574     return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
575   }
576   if (!FLAGS_properties_file.empty()) {
577     return ExtractProperties(
578                FLAGS_in_file, FLAGS_properties_file, FLAGS_properties_format)
579                ? 0
580                : 1;
581   }
582 
583   // A payload generation was requested. Convert the flags to a
584   // PayloadGenerationConfig.
585   PayloadGenerationConfig payload_config;
586   vector<string> partition_names, old_partitions, new_partitions;
587   vector<string> old_mapfiles, new_mapfiles;
588 
589   if (!FLAGS_old_mapfiles.empty()) {
590     old_mapfiles = base::SplitString(
591         FLAGS_old_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
592   }
593   if (!FLAGS_new_mapfiles.empty()) {
594     new_mapfiles = base::SplitString(
595         FLAGS_new_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
596   }
597 
598   partition_names = base::SplitString(
599       FLAGS_partition_names, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
600   CHECK(!partition_names.empty());
601   if (FLAGS_major_version < kMinSupportedMajorPayloadVersion ||
602       FLAGS_major_version > kMaxSupportedMajorPayloadVersion) {
603     LOG(FATAL) << "Unsupported major version " << FLAGS_major_version;
604     return 1;
605   }
606 
607   if (!FLAGS_apex_info_file.empty()) {
608     // apex_info_file should point to a regular file(or symlink to a regular
609     // file)
610     CHECK(utils::FileExists(FLAGS_apex_info_file.c_str()));
611     CHECK(utils::IsRegFile(FLAGS_apex_info_file.c_str()) ||
612           utils::IsSymlink(FLAGS_apex_info_file.c_str()));
613     payload_config.apex_info_file = FLAGS_apex_info_file;
614   }
615 
616   payload_config.enable_vabc_xor = FLAGS_enable_vabc_xor;
617   payload_config.enable_lz4diff = FLAGS_enable_lz4diff;
618   payload_config.enable_zucchini = FLAGS_enable_zucchini;
619   payload_config.enable_puffdiff = FLAGS_enable_puffdiff;
620 
621   payload_config.ParseCompressorTypes(FLAGS_compressor_types);
622 
623   if (!FLAGS_new_partitions.empty()) {
624     LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
625         << "--new_image and --new_kernel are deprecated, please use "
626         << "--new_partitions for all partitions.";
627     new_partitions = base::SplitString(
628         FLAGS_new_partitions, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
629     CHECK(partition_names.size() == new_partitions.size());
630 
631     payload_config.is_delta = !FLAGS_old_partitions.empty();
632     LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
633         << "--old_image and --old_kernel are deprecated, please use "
634         << "--old_partitions if you are using --new_partitions.";
635   } else {
636     new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
637     LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
638                  << "and --new_kernel flags.";
639 
640     payload_config.is_delta =
641         !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty();
642     LOG_IF(FATAL, !FLAGS_old_partitions.empty())
643         << "Please use --new_partitions if you are using --old_partitions.";
644   }
645   for (size_t i = 0; i < partition_names.size(); i++) {
646     LOG_IF(FATAL, partition_names[i].empty())
647         << "Partition name can't be empty, see --partition_names.";
648     payload_config.target.partitions.emplace_back(partition_names[i]);
649     payload_config.target.partitions.back().path = new_partitions[i];
650     payload_config.target.partitions.back().disable_fec_computation =
651         FLAGS_disable_fec_computation;
652     if (!FLAGS_erofs_compression_param.empty()) {
653       payload_config.target.partitions.back().erofs_compression_param =
654           PartitionConfig::ParseCompressionParam(FLAGS_erofs_compression_param);
655     }
656     if (i < new_mapfiles.size())
657       payload_config.target.partitions.back().mapfile_path = new_mapfiles[i];
658   }
659 
660   if (payload_config.is_delta) {
661     if (!FLAGS_old_partitions.empty()) {
662       old_partitions = base::SplitString(FLAGS_old_partitions,
663                                          ":",
664                                          base::TRIM_WHITESPACE,
665                                          base::SPLIT_WANT_ALL);
666       CHECK(old_partitions.size() == new_partitions.size());
667     } else {
668       old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
669       LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
670                    << "and --old_kernel flags.";
671     }
672     for (size_t i = 0; i < partition_names.size(); i++) {
673       payload_config.source.partitions.emplace_back(partition_names[i]);
674       payload_config.source.partitions.back().path = old_partitions[i];
675       if (i < old_mapfiles.size())
676         payload_config.source.partitions.back().mapfile_path = old_mapfiles[i];
677     }
678   }
679 
680   if (FLAGS_is_partial_update) {
681     payload_config.is_partial_update = true;
682   }
683 
684   if (!FLAGS_in_file.empty()) {
685     return ApplyPayload(FLAGS_in_file, payload_config) ? 0 : 1;
686   }
687 
688   if (!FLAGS_new_postinstall_config_file.empty()) {
689     brillo::KeyValueStore store;
690     CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
691     CHECK(payload_config.target.LoadPostInstallConfig(store));
692   }
693 
694   // Use the default soft_chunk_size defined in the config.
695   payload_config.hard_chunk_size = FLAGS_chunk_size;
696   payload_config.block_size = kBlockSize;
697 
698   // The partition size is never passed to the delta_generator, so we
699   // need to detect those from the provided files.
700   if (payload_config.is_delta) {
701     RoundDownPartitions(payload_config.source);
702     CHECK(payload_config.source.LoadImageSize());
703   }
704   RoundUpPartitions(payload_config.target);
705   CHECK(payload_config.target.LoadImageSize());
706 
707   if (!FLAGS_dynamic_partition_info_file.empty()) {
708     brillo::KeyValueStore store;
709     CHECK(store.Load(base::FilePath(FLAGS_dynamic_partition_info_file)));
710     CHECK(payload_config.target.LoadDynamicPartitionMetadata(store));
711     CHECK(payload_config.target.ValidateDynamicPartitionMetadata());
712     if (FLAGS_disable_vabc) {
713       LOG(INFO) << "Disabling VABC";
714       payload_config.target.dynamic_partition_metadata->set_vabc_enabled(false);
715       payload_config.target.dynamic_partition_metadata
716           ->set_vabc_compression_param("");
717     }
718   }
719 
720   CHECK(!FLAGS_out_file.empty());
721 
722   payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
723 
724   if (payload_config.is_delta) {
725     // Avoid opening the filesystem interface for full payloads.
726     for (PartitionConfig& part : payload_config.target.partitions)
727       CHECK(part.OpenFilesystem());
728     for (PartitionConfig& part : payload_config.source.partitions)
729       CHECK(part.OpenFilesystem());
730   }
731 
732   payload_config.version.major = FLAGS_major_version;
733   LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
734 
735   if (FLAGS_minor_version == -1) {
736     // Autodetect minor_version by looking at the update_engine.conf in the old
737     // image.
738     if (FLAGS_is_partial_update) {
739       payload_config.version.minor = kPartialUpdateMinorPayloadVersion;
740       LOG(INFO) << "Using minor_version=" << payload_config.version.minor
741                 << " for partial updates";
742     } else if (payload_config.is_delta) {
743       LOG(FATAL) << "Minor version is required for complete delta update!";
744       return 1;
745     } else {
746       payload_config.version.minor = kFullPayloadMinorVersion;
747       LOG(INFO) << "Using non-delta minor_version="
748                 << payload_config.version.minor;
749     }
750   } else {
751     payload_config.version.minor = FLAGS_minor_version;
752     LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
753   }
754 
755   if (payload_config.version.minor != kFullPayloadMinorVersion &&
756       (payload_config.version.minor < kMinSupportedMinorPayloadVersion ||
757        payload_config.version.minor > kMaxSupportedMinorPayloadVersion)) {
758     LOG(FATAL) << "Unsupported minor version " << payload_config.version.minor;
759     return 1;
760   }
761 
762   payload_config.max_timestamp = FLAGS_max_timestamp;
763 
764   payload_config.security_patch_level = FLAGS_security_patch_level;
765 
766   if (FLAGS_max_threads > 0) {
767     payload_config.max_threads = FLAGS_max_threads;
768   }
769 
770   if (!FLAGS_partition_timestamps.empty()) {
771     CHECK(ParsePerPartitionTimestamps(FLAGS_partition_timestamps,
772                                       &payload_config));
773   }
774 
775   if (payload_config.is_delta &&
776       payload_config.version.minor >= kVerityMinorPayloadVersion &&
777       !FLAGS_disable_verity_computation) {
778     CHECK(payload_config.target.LoadVerityConfig());
779     for (size_t i = 0; i < payload_config.target.partitions.size(); ++i) {
780       if (payload_config.source.partitions[i].fs_interface != nullptr) {
781         continue;
782       }
783       if (!payload_config.target.partitions[i].verity.IsEmpty()) {
784         LOG(INFO) << "Partition " << payload_config.target.partitions[i].name
785                   << " is installed in full OTA, disaling verity for this "
786                      "specific partition.";
787         payload_config.target.partitions[i].verity.Clear();
788       }
789     }
790   }
791 
792   LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full")
793             << " update";
794 
795   // From this point, all the options have been parsed.
796   if (!payload_config.Validate()) {
797     LOG(ERROR) << "Invalid options passed. See errors above.";
798     return 1;
799   }
800 
801   uint64_t metadata_size{};
802   if (!GenerateUpdatePayloadFile(
803           payload_config, FLAGS_out_file, FLAGS_private_key, &metadata_size)) {
804     return 1;
805   }
806   if (!FLAGS_out_metadata_size_file.empty()) {
807     string metadata_size_string = std::to_string(metadata_size);
808     CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
809                            metadata_size_string.data(),
810                            metadata_size_string.size()));
811   }
812   return 0;
813 }
814 
815 }  // namespace
816 
817 }  // namespace chromeos_update_engine
818 
main(int argc,char ** argv)819 int main(int argc, char** argv) {
820   return chromeos_update_engine::Main(argc, argv);
821 }
822