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(enable_puffdiff,
449 true,
450 "Whether to enable puffdiff feature. Enabling puffdiff will take "
451 "longer but generated OTA will be smaller.");
452
453 DEFINE_bool(
454 enable_zucchini,
455 true,
456 "Whether to enable zucchini feature when processing executable files.");
457
458 DEFINE_string(erofs_compression_param,
459 "",
460 "Compression parameter passed to mkfs.erofs's -z option. "
461 "Example: lz4 lz4hc,9");
462
463 DEFINE_int64(max_threads,
464 0,
465 "The maximum number of threads allowed for generating "
466 "ota.");
467
RoundDownPartitions(const ImageConfig & config)468 void RoundDownPartitions(const ImageConfig& config) {
469 for (const auto& part : config.partitions) {
470 if (part.path.empty()) {
471 continue;
472 }
473 const auto size = std::max<size_t>(utils::FileSize(part.path), kBlockSize);
474 if (size % kBlockSize != 0) {
475 const auto err =
476 truncate(part.path.c_str(), size / kBlockSize * kBlockSize);
477 CHECK_EQ(err, 0) << "Failed to truncate " << part.path << ", error "
478 << strerror(errno);
479 }
480 }
481 }
482
RoundUpPartitions(const ImageConfig & config)483 void RoundUpPartitions(const ImageConfig& config) {
484 for (const auto& part : config.partitions) {
485 if (part.path.empty()) {
486 continue;
487 }
488 const auto size = utils::FileSize(part.path);
489 if (size % kBlockSize != 0) {
490 const auto err = truncate(
491 part.path.c_str(), (size + kBlockSize - 1) / kBlockSize * kBlockSize);
492 CHECK_EQ(err, 0) << "Failed to truncate " << part.path << ", error "
493 << strerror(errno);
494 }
495 }
496 }
497
Main(int argc,char ** argv)498 int Main(int argc, char** argv) {
499 gflags::SetUsageMessage(
500 "Generates a payload to provide to ChromeOS' update_engine.\n\n"
501 "This tool can create full payloads and also delta payloads if the src\n"
502 "image is provided. It also provides debugging options to apply, sign\n"
503 "and verify payloads.");
504 gflags::ParseCommandLineFlags(&argc, &argv, true);
505 CHECK_EQ(argc, 1) << " Unused args: "
506 << android::base::Join(
507 std::vector<char*>(argv + 1, argv + argc), " ");
508
509 Terminator::Init();
510
511 logging::LoggingSettings log_settings;
512 #if BASE_VER < 780000
513 log_settings.log_file = "delta_generator.log";
514 #else
515 log_settings.log_file_path = "delta_generator.log";
516 #endif
517 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
518 log_settings.lock_log = logging::LOCK_LOG_FILE;
519 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
520
521 logging::InitLogging(log_settings);
522
523 // Initialize the Xz compressor.
524 XzCompressInit();
525
526 if (!FLAGS_out_maximum_signature_size_file.empty()) {
527 LOG_IF(FATAL, FLAGS_private_key.empty())
528 << "Private key is not provided when calculating the maximum signature "
529 "size.";
530
531 size_t maximum_signature_size{};
532 if (!PayloadSigner::GetMaximumSignatureSize(FLAGS_private_key,
533 &maximum_signature_size)) {
534 LOG(ERROR) << "Failed to get the maximum signature size of private key: "
535 << FLAGS_private_key;
536 return 1;
537 }
538 // Write the size string to output file.
539 string signature_size_string = std::to_string(maximum_signature_size);
540 if (!utils::WriteFile(FLAGS_out_maximum_signature_size_file.c_str(),
541 signature_size_string.c_str(),
542 signature_size_string.size())) {
543 PLOG(ERROR) << "Failed to write the maximum signature size to "
544 << FLAGS_out_maximum_signature_size_file << ".";
545 return 1;
546 }
547 return 0;
548 }
549
550 vector<size_t> signature_sizes;
551 if (!FLAGS_signature_size.empty()) {
552 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
553 }
554
555 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
556 CHECK(FLAGS_out_metadata_size_file.empty());
557 CalculateHashForSigning(signature_sizes,
558 FLAGS_out_hash_file,
559 FLAGS_out_metadata_hash_file,
560 FLAGS_in_file);
561 return 0;
562 }
563 if (!FLAGS_payload_signature_file.empty()) {
564 SignPayload(FLAGS_in_file,
565 FLAGS_out_file,
566 signature_sizes,
567 FLAGS_payload_signature_file,
568 FLAGS_metadata_signature_file,
569 FLAGS_out_metadata_size_file);
570 return 0;
571 }
572 if (!FLAGS_public_key.empty()) {
573 LOG_IF(WARNING, FLAGS_public_key_version != -1)
574 << "--public_key_version is deprecated and ignored.";
575 return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
576 }
577 if (!FLAGS_properties_file.empty()) {
578 return ExtractProperties(
579 FLAGS_in_file, FLAGS_properties_file, FLAGS_properties_format)
580 ? 0
581 : 1;
582 }
583
584 // A payload generation was requested. Convert the flags to a
585 // PayloadGenerationConfig.
586 PayloadGenerationConfig payload_config;
587 vector<string> partition_names, old_partitions, new_partitions;
588 vector<string> old_mapfiles, new_mapfiles;
589
590 if (!FLAGS_old_mapfiles.empty()) {
591 old_mapfiles = base::SplitString(
592 FLAGS_old_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
593 }
594 if (!FLAGS_new_mapfiles.empty()) {
595 new_mapfiles = base::SplitString(
596 FLAGS_new_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
597 }
598
599 partition_names = base::SplitString(
600 FLAGS_partition_names, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
601 CHECK(!partition_names.empty());
602 if (FLAGS_major_version < kMinSupportedMajorPayloadVersion ||
603 FLAGS_major_version > kMaxSupportedMajorPayloadVersion) {
604 LOG(FATAL) << "Unsupported major version " << FLAGS_major_version;
605 return 1;
606 }
607
608 if (!FLAGS_apex_info_file.empty()) {
609 // apex_info_file should point to a regular file(or symlink to a regular
610 // file)
611 CHECK(utils::FileExists(FLAGS_apex_info_file.c_str()));
612 CHECK(utils::IsRegFile(FLAGS_apex_info_file.c_str()) ||
613 utils::IsSymlink(FLAGS_apex_info_file.c_str()));
614 payload_config.apex_info_file = FLAGS_apex_info_file;
615 }
616
617 payload_config.enable_vabc_xor = FLAGS_enable_vabc_xor;
618 payload_config.enable_lz4diff = FLAGS_enable_lz4diff;
619 payload_config.enable_zucchini = FLAGS_enable_zucchini;
620 payload_config.enable_puffdiff = FLAGS_enable_puffdiff;
621
622 payload_config.ParseCompressorTypes(FLAGS_compressor_types);
623
624 if (!FLAGS_new_partitions.empty()) {
625 LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty())
626 << "--new_image and --new_kernel are deprecated, please use "
627 << "--new_partitions for all partitions.";
628 new_partitions = base::SplitString(
629 FLAGS_new_partitions, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
630 CHECK(partition_names.size() == new_partitions.size());
631
632 payload_config.is_delta = !FLAGS_old_partitions.empty();
633 LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty())
634 << "--old_image and --old_kernel are deprecated, please use "
635 << "--old_partitions if you are using --new_partitions.";
636 } else {
637 new_partitions = {FLAGS_new_image, FLAGS_new_kernel};
638 LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image "
639 << "and --new_kernel flags.";
640
641 payload_config.is_delta =
642 !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty();
643 LOG_IF(FATAL, !FLAGS_old_partitions.empty())
644 << "Please use --new_partitions if you are using --old_partitions.";
645 }
646 for (size_t i = 0; i < partition_names.size(); i++) {
647 LOG_IF(FATAL, partition_names[i].empty())
648 << "Partition name can't be empty, see --partition_names.";
649 payload_config.target.partitions.emplace_back(partition_names[i]);
650 payload_config.target.partitions.back().path = new_partitions[i];
651 payload_config.target.partitions.back().disable_fec_computation =
652 FLAGS_disable_fec_computation;
653 if (!FLAGS_erofs_compression_param.empty()) {
654 payload_config.target.partitions.back().erofs_compression_param =
655 PartitionConfig::ParseCompressionParam(FLAGS_erofs_compression_param);
656 }
657 if (i < new_mapfiles.size())
658 payload_config.target.partitions.back().mapfile_path = new_mapfiles[i];
659 }
660
661 if (payload_config.is_delta) {
662 if (!FLAGS_old_partitions.empty()) {
663 old_partitions = base::SplitString(FLAGS_old_partitions,
664 ":",
665 base::TRIM_WHITESPACE,
666 base::SPLIT_WANT_ALL);
667 CHECK(old_partitions.size() == new_partitions.size());
668 } else {
669 old_partitions = {FLAGS_old_image, FLAGS_old_kernel};
670 LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image "
671 << "and --old_kernel flags.";
672 }
673 for (size_t i = 0; i < partition_names.size(); i++) {
674 payload_config.source.partitions.emplace_back(partition_names[i]);
675 payload_config.source.partitions.back().path = old_partitions[i];
676 if (i < old_mapfiles.size())
677 payload_config.source.partitions.back().mapfile_path = old_mapfiles[i];
678 }
679 }
680
681 if (FLAGS_is_partial_update) {
682 payload_config.is_partial_update = true;
683 }
684
685 if (!FLAGS_in_file.empty()) {
686 return ApplyPayload(FLAGS_in_file, payload_config) ? 0 : 1;
687 }
688
689 if (!FLAGS_new_postinstall_config_file.empty()) {
690 brillo::KeyValueStore store;
691 CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file)));
692 CHECK(payload_config.target.LoadPostInstallConfig(store));
693 }
694
695 // Use the default soft_chunk_size defined in the config.
696 payload_config.hard_chunk_size = FLAGS_chunk_size;
697 payload_config.block_size = kBlockSize;
698
699 // The partition size is never passed to the delta_generator, so we
700 // need to detect those from the provided files.
701 if (payload_config.is_delta) {
702 RoundDownPartitions(payload_config.source);
703 CHECK(payload_config.source.LoadImageSize());
704 }
705 RoundUpPartitions(payload_config.target);
706 CHECK(payload_config.target.LoadImageSize());
707
708 if (!FLAGS_dynamic_partition_info_file.empty()) {
709 brillo::KeyValueStore store;
710 CHECK(store.Load(base::FilePath(FLAGS_dynamic_partition_info_file)));
711 CHECK(payload_config.target.LoadDynamicPartitionMetadata(store));
712 CHECK(payload_config.target.ValidateDynamicPartitionMetadata());
713 if (FLAGS_disable_vabc) {
714 LOG(INFO) << "Disabling VABC";
715 payload_config.target.dynamic_partition_metadata->set_vabc_enabled(false);
716 payload_config.target.dynamic_partition_metadata
717 ->set_vabc_compression_param("");
718 }
719 }
720
721 CHECK(!FLAGS_out_file.empty());
722
723 payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size;
724
725 if (payload_config.is_delta) {
726 // Avoid opening the filesystem interface for full payloads.
727 for (PartitionConfig& part : payload_config.target.partitions)
728 CHECK(part.OpenFilesystem());
729 for (PartitionConfig& part : payload_config.source.partitions)
730 CHECK(part.OpenFilesystem());
731 }
732
733 payload_config.version.major = FLAGS_major_version;
734 LOG(INFO) << "Using provided major_version=" << FLAGS_major_version;
735
736 if (FLAGS_minor_version == -1) {
737 // Autodetect minor_version by looking at the update_engine.conf in the old
738 // image.
739 if (FLAGS_is_partial_update) {
740 payload_config.version.minor = kPartialUpdateMinorPayloadVersion;
741 LOG(INFO) << "Using minor_version=" << payload_config.version.minor
742 << " for partial updates";
743 } else if (payload_config.is_delta) {
744 LOG(FATAL) << "Minor version is required for complete delta update!";
745 return 1;
746 } else {
747 payload_config.version.minor = kFullPayloadMinorVersion;
748 LOG(INFO) << "Using non-delta minor_version="
749 << payload_config.version.minor;
750 }
751 } else {
752 payload_config.version.minor = FLAGS_minor_version;
753 LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version;
754 }
755
756 if (payload_config.version.minor != kFullPayloadMinorVersion &&
757 (payload_config.version.minor < kMinSupportedMinorPayloadVersion ||
758 payload_config.version.minor > kMaxSupportedMinorPayloadVersion)) {
759 LOG(FATAL) << "Unsupported minor version " << payload_config.version.minor;
760 return 1;
761 }
762
763 payload_config.max_timestamp = FLAGS_max_timestamp;
764
765 payload_config.security_patch_level = FLAGS_security_patch_level;
766
767 payload_config.max_threads = FLAGS_max_threads;
768
769 if (!FLAGS_partition_timestamps.empty()) {
770 CHECK(ParsePerPartitionTimestamps(FLAGS_partition_timestamps,
771 &payload_config));
772 }
773
774 if (payload_config.is_delta &&
775 payload_config.version.minor >= kVerityMinorPayloadVersion &&
776 !FLAGS_disable_verity_computation) {
777 CHECK(payload_config.target.LoadVerityConfig());
778 for (size_t i = 0; i < payload_config.target.partitions.size(); ++i) {
779 if (payload_config.source.partitions[i].fs_interface != nullptr) {
780 continue;
781 }
782 if (!payload_config.target.partitions[i].verity.IsEmpty()) {
783 LOG(INFO) << "Partition " << payload_config.target.partitions[i].name
784 << " is installed in full OTA, disaling verity for this "
785 "specific partition.";
786 payload_config.target.partitions[i].verity.Clear();
787 }
788 }
789 }
790
791 LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full")
792 << " update";
793
794 // From this point, all the options have been parsed.
795 if (!payload_config.Validate()) {
796 LOG(ERROR) << "Invalid options passed. See errors above.";
797 return 1;
798 }
799
800 uint64_t metadata_size{};
801 if (!GenerateUpdatePayloadFile(
802 payload_config, FLAGS_out_file, FLAGS_private_key, &metadata_size)) {
803 return 1;
804 }
805 if (!FLAGS_out_metadata_size_file.empty()) {
806 string metadata_size_string = std::to_string(metadata_size);
807 CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(),
808 metadata_size_string.data(),
809 metadata_size_string.size()));
810 }
811 return 0;
812 }
813
814 } // namespace
815
816 } // namespace chromeos_update_engine
817
main(int argc,char ** argv)818 int main(int argc, char** argv) {
819 return chromeos_update_engine::Main(argc, argv);
820 }
821