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 <string> 18 #include <vector> 19 20 #include <base/files/file_path.h> 21 #include <base/files/file_util.h> 22 #include <base/logging.h> 23 #include <base/strings/string_number_conversions.h> 24 #include <base/strings/string_split.h> 25 #include <brillo/flag_helper.h> 26 #include <brillo/key_value_store.h> 27 #include <brillo/message_loops/base_message_loop.h> 28 #include <xz.h> 29 30 #include "update_engine/common/fake_boot_control.h" 31 #include "update_engine/common/fake_hardware.h" 32 #include "update_engine/common/file_fetcher.h" 33 #include "update_engine/common/prefs.h" 34 #include "update_engine/common/terminator.h" 35 #include "update_engine/common/utils.h" 36 #include "update_engine/payload_consumer/download_action.h" 37 #include "update_engine/payload_consumer/filesystem_verifier_action.h" 38 #include "update_engine/payload_consumer/payload_constants.h" 39 #include "update_engine/payload_generator/delta_diff_generator.h" 40 #include "update_engine/payload_generator/payload_generation_config.h" 41 #include "update_engine/payload_generator/payload_signer.h" 42 #include "update_engine/payload_generator/xz.h" 43 #include "update_engine/update_metadata.pb.h" 44 45 // This file contains a simple program that takes an old path, a new path, 46 // and an output file as arguments and the path to an output file and 47 // generates a delta that can be sent to Chrome OS clients. 48 49 using std::string; 50 using std::vector; 51 52 namespace chromeos_update_engine { 53 54 namespace { 55 56 void ParseSignatureSizes(const string& signature_sizes_flag, 57 vector<size_t>* signature_sizes) { 58 signature_sizes->clear(); 59 vector<string> split_strings = base::SplitString( 60 signature_sizes_flag, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 61 for (const string& str : split_strings) { 62 size_t size = 0; 63 bool parsing_successful = base::StringToSizeT(str, &size); 64 LOG_IF(FATAL, !parsing_successful) << "Invalid signature size: " << str; 65 66 signature_sizes->push_back(size); 67 } 68 } 69 70 bool ParseImageInfo(const string& channel, 71 const string& board, 72 const string& version, 73 const string& key, 74 const string& build_channel, 75 const string& build_version, 76 ImageInfo* image_info) { 77 // All of these arguments should be present or missing. 78 bool empty = channel.empty(); 79 80 CHECK_EQ(channel.empty(), empty); 81 CHECK_EQ(board.empty(), empty); 82 CHECK_EQ(version.empty(), empty); 83 CHECK_EQ(key.empty(), empty); 84 85 if (empty) 86 return false; 87 88 image_info->set_channel(channel); 89 image_info->set_board(board); 90 image_info->set_version(version); 91 image_info->set_key(key); 92 93 image_info->set_build_channel(build_channel.empty() ? channel 94 : build_channel); 95 96 image_info->set_build_version(build_version.empty() ? version 97 : build_version); 98 99 return true; 100 } 101 102 void CalculateHashForSigning(const vector<size_t>& sizes, 103 const string& out_hash_file, 104 const string& out_metadata_hash_file, 105 const string& in_file) { 106 LOG(INFO) << "Calculating hash for signing."; 107 LOG_IF(FATAL, in_file.empty()) 108 << "Must pass --in_file to calculate hash for signing."; 109 LOG_IF(FATAL, out_hash_file.empty()) 110 << "Must pass --out_hash_file to calculate hash for signing."; 111 112 brillo::Blob payload_hash, metadata_hash; 113 CHECK(PayloadSigner::HashPayloadForSigning( 114 in_file, sizes, &payload_hash, &metadata_hash)); 115 CHECK(utils::WriteFile( 116 out_hash_file.c_str(), payload_hash.data(), payload_hash.size())); 117 if (!out_metadata_hash_file.empty()) 118 CHECK(utils::WriteFile(out_metadata_hash_file.c_str(), 119 metadata_hash.data(), 120 metadata_hash.size())); 121 122 LOG(INFO) << "Done calculating hash for signing."; 123 } 124 125 void SignatureFileFlagToBlobs(const string& signature_file_flag, 126 vector<brillo::Blob>* signatures) { 127 vector<string> signature_files = base::SplitString( 128 signature_file_flag, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 129 for (const string& signature_file : signature_files) { 130 brillo::Blob signature; 131 CHECK(utils::ReadFile(signature_file, &signature)); 132 signatures->push_back(signature); 133 } 134 } 135 136 void SignPayload(const string& in_file, 137 const string& out_file, 138 const vector<size_t>& signature_sizes, 139 const string& payload_signature_file, 140 const string& metadata_signature_file, 141 const string& out_metadata_size_file) { 142 LOG(INFO) << "Signing payload."; 143 LOG_IF(FATAL, in_file.empty()) << "Must pass --in_file to sign payload."; 144 LOG_IF(FATAL, out_file.empty()) << "Must pass --out_file to sign payload."; 145 LOG_IF(FATAL, payload_signature_file.empty()) 146 << "Must pass --payload_signature_file to sign payload."; 147 vector<brillo::Blob> payload_signatures, metadata_signatures; 148 SignatureFileFlagToBlobs(payload_signature_file, &payload_signatures); 149 SignatureFileFlagToBlobs(metadata_signature_file, &metadata_signatures); 150 uint64_t final_metadata_size; 151 CHECK(PayloadSigner::AddSignatureToPayload(in_file, 152 signature_sizes, 153 payload_signatures, 154 metadata_signatures, 155 out_file, 156 &final_metadata_size)); 157 LOG(INFO) << "Done signing payload. Final metadata size = " 158 << final_metadata_size; 159 if (!out_metadata_size_file.empty()) { 160 string metadata_size_string = std::to_string(final_metadata_size); 161 CHECK(utils::WriteFile(out_metadata_size_file.c_str(), 162 metadata_size_string.data(), 163 metadata_size_string.size())); 164 } 165 } 166 167 int VerifySignedPayload(const string& in_file, const string& public_key) { 168 LOG(INFO) << "Verifying signed payload."; 169 LOG_IF(FATAL, in_file.empty()) 170 << "Must pass --in_file to verify signed payload."; 171 LOG_IF(FATAL, public_key.empty()) 172 << "Must pass --public_key to verify signed payload."; 173 if (!PayloadSigner::VerifySignedPayload(in_file, public_key)) { 174 LOG(INFO) << "VerifySignedPayload failed"; 175 return 1; 176 } 177 178 LOG(INFO) << "Done verifying signed payload."; 179 return 0; 180 } 181 182 class ApplyPayloadProcessorDelegate : public ActionProcessorDelegate { 183 public: 184 void ProcessingDone(const ActionProcessor* processor, 185 ErrorCode code) override { 186 brillo::MessageLoop::current()->BreakLoop(); 187 code_ = code; 188 } 189 void ProcessingStopped(const ActionProcessor* processor) override { 190 brillo::MessageLoop::current()->BreakLoop(); 191 } 192 ErrorCode code_; 193 }; 194 195 // TODO(deymo): Move this function to a new file and make the delta_performer 196 // integration tests use this instead. 197 bool ApplyPayload(const string& payload_file, 198 // Simply reuses the payload config used for payload 199 // generation. 200 const PayloadGenerationConfig& config) { 201 LOG(INFO) << "Applying delta."; 202 FakeBootControl fake_boot_control; 203 FakeHardware fake_hardware; 204 MemoryPrefs prefs; 205 InstallPlan install_plan; 206 InstallPlan::Payload payload; 207 install_plan.source_slot = 208 config.is_delta ? 0 : BootControlInterface::kInvalidSlot; 209 install_plan.target_slot = 1; 210 payload.type = 211 config.is_delta ? InstallPayloadType::kDelta : InstallPayloadType::kFull; 212 payload.size = utils::FileSize(payload_file); 213 // TODO(senj): This hash is only correct for unsigned payload, need to support 214 // signed payload using PayloadSigner. 215 HashCalculator::RawHashOfFile(payload_file, payload.size, &payload.hash); 216 install_plan.payloads = {payload}; 217 install_plan.download_url = 218 "file://" + 219 base::MakeAbsoluteFilePath(base::FilePath(payload_file)).value(); 220 221 for (size_t i = 0; i < config.target.partitions.size(); i++) { 222 const string& part_name = config.target.partitions[i].name; 223 const string& target_path = config.target.partitions[i].path; 224 fake_boot_control.SetPartitionDevice( 225 part_name, install_plan.target_slot, target_path); 226 227 string source_path; 228 if (config.is_delta) { 229 TEST_AND_RETURN_FALSE(config.target.partitions.size() == 230 config.source.partitions.size()); 231 source_path = config.source.partitions[i].path; 232 fake_boot_control.SetPartitionDevice( 233 part_name, install_plan.source_slot, source_path); 234 } 235 236 LOG(INFO) << "Install partition:" 237 << " source: " << source_path << "\ttarget: " << target_path; 238 } 239 240 xz_crc32_init(); 241 brillo::BaseMessageLoop loop; 242 loop.SetAsCurrent(); 243 auto install_plan_action = std::make_unique<InstallPlanAction>(install_plan); 244 auto download_action = 245 std::make_unique<DownloadAction>(&prefs, 246 &fake_boot_control, 247 &fake_hardware, 248 nullptr, 249 new FileFetcher(), 250 true /* interactive */); 251 auto filesystem_verifier_action = 252 std::make_unique<FilesystemVerifierAction>(); 253 254 BondActions(install_plan_action.get(), download_action.get()); 255 BondActions(download_action.get(), filesystem_verifier_action.get()); 256 ActionProcessor processor; 257 ApplyPayloadProcessorDelegate delegate; 258 processor.set_delegate(&delegate); 259 processor.EnqueueAction(std::move(install_plan_action)); 260 processor.EnqueueAction(std::move(download_action)); 261 processor.EnqueueAction(std::move(filesystem_verifier_action)); 262 processor.StartProcessing(); 263 loop.Run(); 264 CHECK_EQ(delegate.code_, ErrorCode::kSuccess); 265 LOG(INFO) << "Completed applying " << (config.is_delta ? "delta" : "full") 266 << " payload."; 267 return true; 268 } 269 270 int ExtractProperties(const string& payload_path, const string& props_file) { 271 brillo::KeyValueStore properties; 272 TEST_AND_RETURN_FALSE( 273 PayloadSigner::ExtractPayloadProperties(payload_path, &properties)); 274 if (props_file == "-") { 275 printf("%s", properties.SaveToString().c_str()); 276 } else { 277 properties.Save(base::FilePath(props_file)); 278 LOG(INFO) << "Generated properties file at " << props_file; 279 } 280 return true; 281 } 282 283 int Main(int argc, char** argv) { 284 DEFINE_string(old_image, "", "Path to the old rootfs"); 285 DEFINE_string(new_image, "", "Path to the new rootfs"); 286 DEFINE_string(old_kernel, "", "Path to the old kernel partition image"); 287 DEFINE_string(new_kernel, "", "Path to the new kernel partition image"); 288 DEFINE_string(old_partitions, 289 "", 290 "Path to the old partitions. To pass multiple partitions, use " 291 "a single argument with a colon between paths, e.g. " 292 "/path/to/part:/path/to/part2::/path/to/last_part . Path can " 293 "be empty, but it has to match the order of partition_names."); 294 DEFINE_string(new_partitions, 295 "", 296 "Path to the new partitions. To pass multiple partitions, use " 297 "a single argument with a colon between paths, e.g. " 298 "/path/to/part:/path/to/part2:/path/to/last_part . Path has " 299 "to match the order of partition_names."); 300 DEFINE_string(old_mapfiles, 301 "", 302 "Path to the .map files associated with the partition files " 303 "in the old partition. The .map file is normally generated " 304 "when creating the image in Android builds. Only recommended " 305 "for unsupported filesystem. Pass multiple files separated by " 306 "a colon as with -old_partitions."); 307 DEFINE_string(new_mapfiles, 308 "", 309 "Path to the .map files associated with the partition files " 310 "in the new partition, similar to the -old_mapfiles flag."); 311 DEFINE_string(partition_names, 312 string(kPartitionNameRoot) + ":" + kPartitionNameKernel, 313 "Names of the partitions. To pass multiple names, use a single " 314 "argument with a colon between names, e.g. " 315 "name:name2:name3:last_name . Name can not be empty, and it " 316 "has to match the order of partitions."); 317 DEFINE_string(in_file, 318 "", 319 "Path to input delta payload file used to hash/sign payloads " 320 "and apply delta over old_image (for debugging)"); 321 DEFINE_string(out_file, "", "Path to output delta payload file"); 322 DEFINE_string(out_hash_file, "", "Path to output hash file"); 323 DEFINE_string( 324 out_metadata_hash_file, "", "Path to output metadata hash file"); 325 DEFINE_string( 326 out_metadata_size_file, "", "Path to output metadata size file"); 327 DEFINE_string(private_key, "", "Path to private key in .pem format"); 328 DEFINE_string(public_key, "", "Path to public key in .pem format"); 329 DEFINE_int32( 330 public_key_version, -1, "DEPRECATED. Key-check version # of client"); 331 DEFINE_string(signature_size, 332 "", 333 "Raw signature size used for hash calculation. " 334 "You may pass in multiple sizes by colon separating them. E.g. " 335 "2048:2048:4096 will assume 3 signatures, the first two with " 336 "2048 size and the last 4096."); 337 DEFINE_string(payload_signature_file, 338 "", 339 "Raw signature file to sign payload with. To pass multiple " 340 "signatures, use a single argument with a colon between paths, " 341 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each " 342 "signature will be assigned a client version, starting from " 343 "kSignatureOriginalVersion."); 344 DEFINE_string(metadata_signature_file, 345 "", 346 "Raw signature file with the signature of the metadata hash. " 347 "To pass multiple signatures, use a single argument with a " 348 "colon between paths, " 349 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig ."); 350 DEFINE_int32( 351 chunk_size, 200 * 1024 * 1024, "Payload chunk size (-1 for whole files)"); 352 DEFINE_uint64(rootfs_partition_size, 353 chromeos_update_engine::kRootFSPartitionSize, 354 "RootFS partition size for the image once installed"); 355 DEFINE_uint64( 356 major_version, 2, "The major version of the payload being generated."); 357 DEFINE_int32(minor_version, 358 -1, 359 "The minor version of the payload being generated " 360 "(-1 means autodetect)."); 361 DEFINE_string(properties_file, 362 "", 363 "If passed, dumps the payload properties of the payload passed " 364 "in --in_file and exits."); 365 DEFINE_int64(max_timestamp, 366 0, 367 "The maximum timestamp of the OS allowed to apply this " 368 "payload."); 369 370 DEFINE_string(old_channel, 371 "", 372 "The channel for the old image. 'dev-channel', 'npo-channel', " 373 "etc. Ignored, except during delta generation."); 374 DEFINE_string(old_board, 375 "", 376 "The board for the old image. 'x86-mario', 'lumpy', " 377 "etc. Ignored, except during delta generation."); 378 DEFINE_string( 379 old_version, "", "The build version of the old image. 1.2.3, etc."); 380 DEFINE_string(old_key, 381 "", 382 "The key used to sign the old image. 'premp', 'mp', 'mp-v3'," 383 " etc"); 384 DEFINE_string(old_build_channel, 385 "", 386 "The channel for the build of the old image. 'dev-channel', " 387 "etc, but will never contain special channels such as " 388 "'npo-channel'. Ignored, except during delta generation."); 389 DEFINE_string(old_build_version, 390 "", 391 "The version of the build containing the old image."); 392 393 DEFINE_string(new_channel, 394 "", 395 "The channel for the new image. 'dev-channel', 'npo-channel', " 396 "etc. Ignored, except during delta generation."); 397 DEFINE_string(new_board, 398 "", 399 "The board for the new image. 'x86-mario', 'lumpy', " 400 "etc. Ignored, except during delta generation."); 401 DEFINE_string( 402 new_version, "", "The build version of the new image. 1.2.3, etc."); 403 DEFINE_string(new_key, 404 "", 405 "The key used to sign the new image. 'premp', 'mp', 'mp-v3'," 406 " etc"); 407 DEFINE_string(new_build_channel, 408 "", 409 "The channel for the build of the new image. 'dev-channel', " 410 "etc, but will never contain special channels such as " 411 "'npo-channel'. Ignored, except during delta generation."); 412 DEFINE_string(new_build_version, 413 "", 414 "The version of the build containing the new image."); 415 DEFINE_string(new_postinstall_config_file, 416 "", 417 "A config file specifying postinstall related metadata. " 418 "Only allowed in major version 2 or newer."); 419 DEFINE_string(dynamic_partition_info_file, 420 "", 421 "An info file specifying dynamic partition metadata. " 422 "Only allowed in major version 2 or newer."); 423 DEFINE_bool(disable_fec_computation, 424 false, 425 "Disables the fec data computation on device."); 426 DEFINE_string( 427 out_maximum_signature_size_file, 428 "", 429 "Path to the output maximum signature size given a private key."); 430 431 brillo::FlagHelper::Init( 432 argc, 433 argv, 434 "Generates a payload to provide to ChromeOS' update_engine.\n\n" 435 "This tool can create full payloads and also delta payloads if the src\n" 436 "image is provided. It also provides debugging options to apply, sign\n" 437 "and verify payloads."); 438 Terminator::Init(); 439 440 logging::LoggingSettings log_settings; 441 log_settings.log_file = "delta_generator.log"; 442 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 443 log_settings.lock_log = logging::LOCK_LOG_FILE; 444 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; 445 446 logging::InitLogging(log_settings); 447 448 // Initialize the Xz compressor. 449 XzCompressInit(); 450 451 if (!FLAGS_out_maximum_signature_size_file.empty()) { 452 LOG_IF(FATAL, FLAGS_private_key.empty()) 453 << "Private key is not provided when calculating the maximum signature " 454 "size."; 455 456 size_t maximum_signature_size; 457 if (!PayloadSigner::GetMaximumSignatureSize(FLAGS_private_key, 458 &maximum_signature_size)) { 459 LOG(ERROR) << "Failed to get the maximum signature size of private key: " 460 << FLAGS_private_key; 461 return 1; 462 } 463 // Write the size string to output file. 464 string signature_size_string = std::to_string(maximum_signature_size); 465 if (!utils::WriteFile(FLAGS_out_maximum_signature_size_file.c_str(), 466 signature_size_string.c_str(), 467 signature_size_string.size())) { 468 PLOG(ERROR) << "Failed to write the maximum signature size to " 469 << FLAGS_out_maximum_signature_size_file << "."; 470 return 1; 471 } 472 return 0; 473 } 474 475 vector<size_t> signature_sizes; 476 if (!FLAGS_signature_size.empty()) { 477 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes); 478 } 479 480 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) { 481 CHECK(FLAGS_out_metadata_size_file.empty()); 482 CalculateHashForSigning(signature_sizes, 483 FLAGS_out_hash_file, 484 FLAGS_out_metadata_hash_file, 485 FLAGS_in_file); 486 return 0; 487 } 488 if (!FLAGS_payload_signature_file.empty()) { 489 SignPayload(FLAGS_in_file, 490 FLAGS_out_file, 491 signature_sizes, 492 FLAGS_payload_signature_file, 493 FLAGS_metadata_signature_file, 494 FLAGS_out_metadata_size_file); 495 return 0; 496 } 497 if (!FLAGS_public_key.empty()) { 498 LOG_IF(WARNING, FLAGS_public_key_version != -1) 499 << "--public_key_version is deprecated and ignored."; 500 return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key); 501 } 502 if (!FLAGS_properties_file.empty()) { 503 return ExtractProperties(FLAGS_in_file, FLAGS_properties_file) ? 0 : 1; 504 } 505 506 // A payload generation was requested. Convert the flags to a 507 // PayloadGenerationConfig. 508 PayloadGenerationConfig payload_config; 509 vector<string> partition_names, old_partitions, new_partitions; 510 vector<string> old_mapfiles, new_mapfiles; 511 512 if (!FLAGS_old_mapfiles.empty()) { 513 old_mapfiles = base::SplitString( 514 FLAGS_old_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 515 } 516 if (!FLAGS_new_mapfiles.empty()) { 517 new_mapfiles = base::SplitString( 518 FLAGS_new_mapfiles, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 519 } 520 521 partition_names = base::SplitString( 522 FLAGS_partition_names, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 523 CHECK(!partition_names.empty()); 524 if (FLAGS_major_version == kChromeOSMajorPayloadVersion || 525 FLAGS_new_partitions.empty()) { 526 LOG_IF(FATAL, partition_names.size() != 2) 527 << "To support more than 2 partitions, please use the " 528 << "--new_partitions flag and major version 2."; 529 LOG_IF(FATAL, 530 partition_names[0] != kPartitionNameRoot || 531 partition_names[1] != kPartitionNameKernel) 532 << "To support non-default partition name, please use the " 533 << "--new_partitions flag and major version 2."; 534 } 535 536 if (!FLAGS_new_partitions.empty()) { 537 LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty()) 538 << "--new_image and --new_kernel are deprecated, please use " 539 << "--new_partitions for all partitions."; 540 new_partitions = base::SplitString( 541 FLAGS_new_partitions, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 542 CHECK(partition_names.size() == new_partitions.size()); 543 544 payload_config.is_delta = !FLAGS_old_partitions.empty(); 545 LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty()) 546 << "--old_image and --old_kernel are deprecated, please use " 547 << "--old_partitions if you are using --new_partitions."; 548 } else { 549 new_partitions = {FLAGS_new_image, FLAGS_new_kernel}; 550 LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image " 551 << "and --new_kernel flags."; 552 553 payload_config.is_delta = 554 !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty(); 555 LOG_IF(FATAL, !FLAGS_old_partitions.empty()) 556 << "Please use --new_partitions if you are using --old_partitions."; 557 } 558 for (size_t i = 0; i < partition_names.size(); i++) { 559 LOG_IF(FATAL, partition_names[i].empty()) 560 << "Partition name can't be empty, see --partition_names."; 561 payload_config.target.partitions.emplace_back(partition_names[i]); 562 payload_config.target.partitions.back().path = new_partitions[i]; 563 payload_config.target.partitions.back().disable_fec_computation = 564 FLAGS_disable_fec_computation; 565 if (i < new_mapfiles.size()) 566 payload_config.target.partitions.back().mapfile_path = new_mapfiles[i]; 567 } 568 569 if (payload_config.is_delta) { 570 if (!FLAGS_old_partitions.empty()) { 571 old_partitions = base::SplitString(FLAGS_old_partitions, 572 ":", 573 base::TRIM_WHITESPACE, 574 base::SPLIT_WANT_ALL); 575 CHECK(old_partitions.size() == new_partitions.size()); 576 } else { 577 old_partitions = {FLAGS_old_image, FLAGS_old_kernel}; 578 LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image " 579 << "and --old_kernel flags."; 580 } 581 for (size_t i = 0; i < partition_names.size(); i++) { 582 payload_config.source.partitions.emplace_back(partition_names[i]); 583 payload_config.source.partitions.back().path = old_partitions[i]; 584 if (i < old_mapfiles.size()) 585 payload_config.source.partitions.back().mapfile_path = old_mapfiles[i]; 586 } 587 } 588 589 if (!FLAGS_in_file.empty()) { 590 return ApplyPayload(FLAGS_in_file, payload_config) ? 0 : 1; 591 } 592 593 if (!FLAGS_new_postinstall_config_file.empty()) { 594 LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion) 595 << "Postinstall config is only allowed in major version 2 or newer."; 596 brillo::KeyValueStore store; 597 CHECK(store.Load(base::FilePath(FLAGS_new_postinstall_config_file))); 598 CHECK(payload_config.target.LoadPostInstallConfig(store)); 599 } 600 601 // Use the default soft_chunk_size defined in the config. 602 payload_config.hard_chunk_size = FLAGS_chunk_size; 603 payload_config.block_size = kBlockSize; 604 605 // The partition size is never passed to the delta_generator, so we 606 // need to detect those from the provided files. 607 if (payload_config.is_delta) { 608 CHECK(payload_config.source.LoadImageSize()); 609 } 610 CHECK(payload_config.target.LoadImageSize()); 611 612 if (!FLAGS_dynamic_partition_info_file.empty()) { 613 LOG_IF(FATAL, FLAGS_major_version == kChromeOSMajorPayloadVersion) 614 << "Dynamic partition info is only allowed in major version 2 or " 615 "newer."; 616 brillo::KeyValueStore store; 617 CHECK(store.Load(base::FilePath(FLAGS_dynamic_partition_info_file))); 618 CHECK(payload_config.target.LoadDynamicPartitionMetadata(store)); 619 CHECK(payload_config.target.ValidateDynamicPartitionMetadata()); 620 } 621 622 CHECK(!FLAGS_out_file.empty()); 623 624 // Ignore failures. These are optional arguments. 625 ParseImageInfo(FLAGS_new_channel, 626 FLAGS_new_board, 627 FLAGS_new_version, 628 FLAGS_new_key, 629 FLAGS_new_build_channel, 630 FLAGS_new_build_version, 631 &payload_config.target.image_info); 632 633 // Ignore failures. These are optional arguments. 634 ParseImageInfo(FLAGS_old_channel, 635 FLAGS_old_board, 636 FLAGS_old_version, 637 FLAGS_old_key, 638 FLAGS_old_build_channel, 639 FLAGS_old_build_version, 640 &payload_config.source.image_info); 641 642 payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size; 643 644 if (payload_config.is_delta) { 645 // Avoid opening the filesystem interface for full payloads. 646 for (PartitionConfig& part : payload_config.target.partitions) 647 CHECK(part.OpenFilesystem()); 648 for (PartitionConfig& part : payload_config.source.partitions) 649 CHECK(part.OpenFilesystem()); 650 } 651 652 payload_config.version.major = FLAGS_major_version; 653 LOG(INFO) << "Using provided major_version=" << FLAGS_major_version; 654 655 if (FLAGS_minor_version == -1) { 656 // Autodetect minor_version by looking at the update_engine.conf in the old 657 // image. 658 if (payload_config.is_delta) { 659 payload_config.version.minor = kInPlaceMinorPayloadVersion; 660 brillo::KeyValueStore store; 661 uint32_t minor_version; 662 for (const PartitionConfig& part : payload_config.source.partitions) { 663 if (part.fs_interface && part.fs_interface->LoadSettings(&store) && 664 utils::GetMinorVersion(store, &minor_version)) { 665 payload_config.version.minor = minor_version; 666 break; 667 } 668 } 669 } else { 670 payload_config.version.minor = kFullPayloadMinorVersion; 671 } 672 LOG(INFO) << "Auto-detected minor_version=" << payload_config.version.minor; 673 } else { 674 payload_config.version.minor = FLAGS_minor_version; 675 LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version; 676 } 677 678 payload_config.max_timestamp = FLAGS_max_timestamp; 679 680 if (payload_config.version.minor >= kVerityMinorPayloadVersion) 681 CHECK(payload_config.target.LoadVerityConfig()); 682 683 LOG(INFO) << "Generating " << (payload_config.is_delta ? "delta" : "full") 684 << " update"; 685 686 // From this point, all the options have been parsed. 687 if (!payload_config.Validate()) { 688 LOG(ERROR) << "Invalid options passed. See errors above."; 689 return 1; 690 } 691 692 uint64_t metadata_size; 693 if (!GenerateUpdatePayloadFile( 694 payload_config, FLAGS_out_file, FLAGS_private_key, &metadata_size)) { 695 return 1; 696 } 697 if (!FLAGS_out_metadata_size_file.empty()) { 698 string metadata_size_string = std::to_string(metadata_size); 699 CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(), 700 metadata_size_string.data(), 701 metadata_size_string.size())); 702 } 703 return 0; 704 } 705 706 } // namespace 707 708 } // namespace chromeos_update_engine 709 710 int main(int argc, char** argv) { 711 return chromeos_update_engine::Main(argc, argv); 712 } 713