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