generate_delta_main.cc revision d8ae872e3b5395580b3679cc92f1c5e52e409c16
1// 2// Copyright (C) 2010 The Android Open Source Project 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15// 16 17#include <errno.h> 18#include <fcntl.h> 19#include <sys/stat.h> 20#include <sys/types.h> 21#include <unistd.h> 22 23#include <set> 24#include <string> 25#include <vector> 26 27#include <base/logging.h> 28#include <base/strings/string_number_conversions.h> 29#include <base/strings/string_split.h> 30#include <chromeos/flag_helper.h> 31 32#include "update_engine/delta_performer.h" 33#include "update_engine/payload_constants.h" 34#include "update_engine/payload_generator/delta_diff_generator.h" 35#include "update_engine/payload_generator/delta_diff_utils.h" 36#include "update_engine/payload_generator/payload_generation_config.h" 37#include "update_engine/payload_generator/payload_signer.h" 38#include "update_engine/payload_verifier.h" 39#include "update_engine/prefs.h" 40#include "update_engine/terminator.h" 41#include "update_engine/update_metadata.pb.h" 42#include "update_engine/utils.h" 43 44// This file contains a simple program that takes an old path, a new path, 45// and an output file as arguments and the path to an output file and 46// generates a delta that can be sent to Chrome OS clients. 47 48using std::set; 49using std::string; 50using std::vector; 51 52namespace chromeos_update_engine { 53 54namespace { 55 56void ParseSignatureSizes(const string& signature_sizes_flag, 57 vector<int>* signature_sizes) { 58 signature_sizes->clear(); 59 vector<string> split_strings; 60 61 base::SplitString(signature_sizes_flag, ':', &split_strings); 62 for (const string& str : split_strings) { 63 int size = 0; 64 bool parsing_successful = base::StringToInt(str, &size); 65 LOG_IF(FATAL, !parsing_successful) 66 << "Invalid signature size: " << str; 67 68 LOG_IF(FATAL, size != (2048 / 8)) << 69 "Only signature sizes of 256 bytes are supported."; 70 71 signature_sizes->push_back(size); 72 } 73} 74 75bool ParseImageInfo(const string& channel, 76 const string& board, 77 const string& version, 78 const string& key, 79 const string& build_channel, 80 const string& build_version, 81 ImageInfo* image_info) { 82 // All of these arguments should be present or missing. 83 bool empty = channel.empty(); 84 85 CHECK_EQ(channel.empty(), empty); 86 CHECK_EQ(board.empty(), empty); 87 CHECK_EQ(version.empty(), empty); 88 CHECK_EQ(key.empty(), empty); 89 90 if (empty) 91 return false; 92 93 image_info->set_channel(channel); 94 image_info->set_board(board); 95 image_info->set_version(version); 96 image_info->set_key(key); 97 98 image_info->set_build_channel( 99 build_channel.empty() ? channel : build_channel); 100 101 image_info->set_build_version( 102 build_version.empty() ? version : build_version); 103 104 return true; 105} 106 107void CalculatePayloadHashForSigning(const vector<int> &sizes, 108 const string& out_hash_file, 109 const string& in_file) { 110 LOG(INFO) << "Calculating payload hash for signing."; 111 LOG_IF(FATAL, in_file.empty()) 112 << "Must pass --in_file to calculate hash for signing."; 113 LOG_IF(FATAL, out_hash_file.empty()) 114 << "Must pass --out_hash_file to calculate hash for signing."; 115 116 chromeos::Blob hash; 117 bool result = PayloadSigner::HashPayloadForSigning(in_file, sizes, 118 &hash); 119 CHECK(result); 120 121 result = utils::WriteFile(out_hash_file.c_str(), hash.data(), hash.size()); 122 CHECK(result); 123 LOG(INFO) << "Done calculating payload hash for signing."; 124} 125 126 127void CalculateMetadataHashForSigning(const vector<int> &sizes, 128 const string& out_metadata_hash_file, 129 const string& in_file) { 130 LOG(INFO) << "Calculating metadata hash for signing."; 131 LOG_IF(FATAL, in_file.empty()) 132 << "Must pass --in_file to calculate metadata hash for signing."; 133 LOG_IF(FATAL, out_metadata_hash_file.empty()) 134 << "Must pass --out_metadata_hash_file to calculate metadata hash."; 135 136 chromeos::Blob hash; 137 bool result = PayloadSigner::HashMetadataForSigning(in_file, sizes, 138 &hash); 139 CHECK(result); 140 141 result = utils::WriteFile(out_metadata_hash_file.c_str(), hash.data(), 142 hash.size()); 143 CHECK(result); 144 145 LOG(INFO) << "Done calculating metadata hash for signing."; 146} 147 148void SignPayload(const string& in_file, 149 const string& out_file, 150 const string& signature_file, 151 const string& out_metadata_size_file) { 152 LOG(INFO) << "Signing payload."; 153 LOG_IF(FATAL, in_file.empty()) 154 << "Must pass --in_file to sign payload."; 155 LOG_IF(FATAL, out_file.empty()) 156 << "Must pass --out_file to sign payload."; 157 LOG_IF(FATAL, signature_file.empty()) 158 << "Must pass --signature_file to sign payload."; 159 vector<chromeos::Blob> signatures; 160 vector<string> signature_files; 161 base::SplitString(signature_file, ':', &signature_files); 162 for (const string& signature_file : signature_files) { 163 chromeos::Blob signature; 164 CHECK(utils::ReadFile(signature_file, &signature)); 165 signatures.push_back(signature); 166 } 167 uint64_t final_metadata_size; 168 CHECK(PayloadSigner::AddSignatureToPayload( 169 in_file, signatures, out_file, &final_metadata_size)); 170 LOG(INFO) << "Done signing payload. Final metadata size = " 171 << final_metadata_size; 172 if (!out_metadata_size_file.empty()) { 173 string metadata_size_string = std::to_string(final_metadata_size); 174 CHECK(utils::WriteFile(out_metadata_size_file.c_str(), 175 metadata_size_string.data(), 176 metadata_size_string.size())); 177 } 178} 179 180void VerifySignedPayload(const string& in_file, 181 const string& public_key) { 182 LOG(INFO) << "Verifying signed payload."; 183 LOG_IF(FATAL, in_file.empty()) 184 << "Must pass --in_file to verify signed payload."; 185 LOG_IF(FATAL, public_key.empty()) 186 << "Must pass --public_key to verify signed payload."; 187 CHECK(PayloadVerifier::VerifySignedPayload(in_file, public_key)); 188 LOG(INFO) << "Done verifying signed payload."; 189} 190 191void ApplyDelta(const string& in_file, 192 const string& old_kernel, 193 const string& old_rootfs, 194 const string& prefs_dir) { 195 LOG(INFO) << "Applying delta."; 196 LOG_IF(FATAL, old_rootfs.empty()) 197 << "Must pass --old_image to apply delta."; 198 Prefs prefs; 199 InstallPlan install_plan; 200 LOG(INFO) << "Setting up preferences under: " << prefs_dir; 201 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir))) 202 << "Failed to initialize preferences."; 203 // Get original checksums 204 LOG(INFO) << "Calculating original checksums"; 205 PartitionInfo kern_info, root_info; 206 ImageConfig old_image; 207 old_image.partitions.emplace_back(kLegacyPartitionNameRoot); 208 old_image.partitions.back().path = old_rootfs; 209 old_image.partitions.emplace_back(kLegacyPartitionNameKernel); 210 old_image.partitions.back().path = old_kernel; 211 CHECK(old_image.LoadImageSize()); 212 CHECK(diff_utils::InitializePartitionInfo(old_image.partitions[0], 213 &root_info)); 214 CHECK(diff_utils::InitializePartitionInfo(old_image.partitions[1], 215 &kern_info)); 216 install_plan.kernel_hash.assign(kern_info.hash().begin(), 217 kern_info.hash().end()); 218 install_plan.rootfs_hash.assign(root_info.hash().begin(), 219 root_info.hash().end()); 220 DeltaPerformer performer(&prefs, nullptr, &install_plan); 221 CHECK_EQ(performer.Open(old_rootfs.c_str(), 0, 0), 0); 222 CHECK(performer.OpenKernel(old_kernel.c_str())); 223 chromeos::Blob buf(1024 * 1024); 224 int fd = open(in_file.c_str(), O_RDONLY, 0); 225 CHECK_GE(fd, 0); 226 ScopedFdCloser fd_closer(&fd); 227 for (off_t offset = 0;; offset += buf.size()) { 228 ssize_t bytes_read; 229 CHECK(utils::PReadAll(fd, buf.data(), buf.size(), offset, &bytes_read)); 230 if (bytes_read == 0) 231 break; 232 CHECK_EQ(performer.Write(buf.data(), bytes_read), bytes_read); 233 } 234 CHECK_EQ(performer.Close(), 0); 235 DeltaPerformer::ResetUpdateProgress(&prefs, false); 236 LOG(INFO) << "Done applying delta."; 237} 238 239int Main(int argc, char** argv) { 240 DEFINE_string(old_image, "", "Path to the old rootfs"); 241 DEFINE_string(new_image, "", "Path to the new rootfs"); 242 DEFINE_string(old_kernel, "", "Path to the old kernel partition image"); 243 DEFINE_string(new_kernel, "", "Path to the new kernel partition image"); 244 DEFINE_string(old_partitions, "", 245 "Path to the old partitions. To pass multiple partitions, use " 246 "a single argument with a colon between paths, e.g. " 247 "/path/to/part:/path/to/part2::/path/to/last_part . Path can " 248 "be empty, but it has to match the order of partition_names."); 249 DEFINE_string(new_partitions, "", 250 "Path to the new partitions. To pass multiple partitions, use " 251 "a single argument with a colon between paths, e.g. " 252 "/path/to/part:/path/to/part2:/path/to/last_part . Path has " 253 "to match the order of partition_names."); 254 DEFINE_string(partition_names, 255 string(kLegacyPartitionNameRoot) + ":" + 256 kLegacyPartitionNameKernel, 257 "Names of the partitions. To pass multiple names, use a single " 258 "argument with a colon between names, e.g. " 259 "name:name2:name3:last_name . Name can not be empty, and it " 260 "has to match the order of partitions."); 261 DEFINE_string(in_file, "", 262 "Path to input delta payload file used to hash/sign payloads " 263 "and apply delta over old_image (for debugging)"); 264 DEFINE_string(out_file, "", "Path to output delta payload file"); 265 DEFINE_string(out_hash_file, "", "Path to output hash file"); 266 DEFINE_string(out_metadata_hash_file, "", 267 "Path to output metadata hash file"); 268 DEFINE_string(out_metadata_size_file, "", 269 "Path to output metadata size file"); 270 DEFINE_string(private_key, "", "Path to private key in .pem format"); 271 DEFINE_string(public_key, "", "Path to public key in .pem format"); 272 DEFINE_int32(public_key_version, -1, 273 "DEPRECATED. Key-check version # of client"); 274 DEFINE_string(prefs_dir, "/tmp/update_engine_prefs", 275 "Preferences directory, used with apply_delta"); 276 DEFINE_string(signature_size, "", 277 "Raw signature size used for hash calculation. " 278 "You may pass in multiple sizes by colon separating them. E.g. " 279 "2048:2048:4096 will assume 3 signatures, the first two with " 280 "2048 size and the last 4096."); 281 DEFINE_string(signature_file, "", 282 "Raw signature file to sign payload with. To pass multiple " 283 "signatures, use a single argument with a colon between paths, " 284 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each " 285 "signature will be assigned a client version, starting from " 286 "kSignatureOriginalVersion."); 287 DEFINE_string(metadata_signature_file, "", 288 "Raw signature file with the signature of the metadata hash. " 289 "To pass multiple signatures, use a single argument with a " 290 "colon between paths, " 291 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig ."); 292 DEFINE_int32(chunk_size, 200 * 1024 * 1024, 293 "Payload chunk size (-1 for whole files)"); 294 DEFINE_uint64(rootfs_partition_size, 295 chromeos_update_engine::kRootFSPartitionSize, 296 "RootFS partition size for the image once installed"); 297 DEFINE_uint64(major_version, 1, 298 "The major version of the payload being generated."); 299 DEFINE_int32(minor_version, -1, 300 "The minor version of the payload being generated " 301 "(-1 means autodetect)."); 302 303 DEFINE_string(old_channel, "", 304 "The channel for the old image. 'dev-channel', 'npo-channel', " 305 "etc. Ignored, except during delta generation."); 306 DEFINE_string(old_board, "", 307 "The board for the old image. 'x86-mario', 'lumpy', " 308 "etc. Ignored, except during delta generation."); 309 DEFINE_string(old_version, "", 310 "The build version of the old image. 1.2.3, etc."); 311 DEFINE_string(old_key, "", 312 "The key used to sign the old image. 'premp', 'mp', 'mp-v3'," 313 " etc"); 314 DEFINE_string(old_build_channel, "", 315 "The channel for the build of the old image. 'dev-channel', " 316 "etc, but will never contain special channels such as " 317 "'npo-channel'. Ignored, except during delta generation."); 318 DEFINE_string(old_build_version, "", 319 "The version of the build containing the old image."); 320 321 DEFINE_string(new_channel, "", 322 "The channel for the new image. 'dev-channel', 'npo-channel', " 323 "etc. Ignored, except during delta generation."); 324 DEFINE_string(new_board, "", 325 "The board for the new image. 'x86-mario', 'lumpy', " 326 "etc. Ignored, except during delta generation."); 327 DEFINE_string(new_version, "", 328 "The build version of the new image. 1.2.3, etc."); 329 DEFINE_string(new_key, "", 330 "The key used to sign the new image. 'premp', 'mp', 'mp-v3'," 331 " etc"); 332 DEFINE_string(new_build_channel, "", 333 "The channel for the build of the new image. 'dev-channel', " 334 "etc, but will never contain special channels such as " 335 "'npo-channel'. Ignored, except during delta generation."); 336 DEFINE_string(new_build_version, "", 337 "The version of the build containing the new image."); 338 339 chromeos::FlagHelper::Init(argc, argv, 340 "Generates a payload to provide to ChromeOS' update_engine.\n\n" 341 "This tool can create full payloads and also delta payloads if the src\n" 342 "image is provided. It also provides debugging options to apply, sign\n" 343 "and verify payloads."); 344 Terminator::Init(); 345 346 logging::LoggingSettings log_settings; 347 log_settings.log_file = "delta_generator.log"; 348 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 349 log_settings.lock_log = logging::LOCK_LOG_FILE; 350 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; 351 352 logging::InitLogging(log_settings); 353 354 vector<int> signature_sizes; 355 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes); 356 357 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) { 358 CHECK(FLAGS_out_metadata_size_file.empty()); 359 if (!FLAGS_out_hash_file.empty()) { 360 CalculatePayloadHashForSigning(signature_sizes, FLAGS_out_hash_file, 361 FLAGS_in_file); 362 } 363 if (!FLAGS_out_metadata_hash_file.empty()) { 364 CalculateMetadataHashForSigning(signature_sizes, 365 FLAGS_out_metadata_hash_file, 366 FLAGS_in_file); 367 } 368 return 0; 369 } 370 if (!FLAGS_signature_file.empty()) { 371 SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file, 372 FLAGS_out_metadata_size_file); 373 return 0; 374 } 375 if (!FLAGS_public_key.empty()) { 376 LOG_IF(WARNING, FLAGS_public_key_version != -1) 377 << "--public_key_version is deprecated and ignored."; 378 VerifySignedPayload(FLAGS_in_file, FLAGS_public_key); 379 return 0; 380 } 381 if (!FLAGS_in_file.empty()) { 382 ApplyDelta(FLAGS_in_file, FLAGS_old_kernel, FLAGS_old_image, 383 FLAGS_prefs_dir); 384 return 0; 385 } 386 387 // A payload generation was requested. Convert the flags to a 388 // PayloadGenerationConfig. 389 PayloadGenerationConfig payload_config; 390 vector<string> partition_names, old_partitions, new_partitions; 391 392 base::SplitString(FLAGS_partition_names, ':', &partition_names); 393 CHECK(!partition_names.empty()); 394 if (FLAGS_major_version == kChromeOSMajorPayloadVersion || 395 FLAGS_new_partitions.empty()) { 396 LOG_IF(FATAL, partition_names.size() != 2) 397 << "To support more than 2 partitions, please use the " 398 << "--new_partitions flag and major version 2."; 399 LOG_IF(FATAL, partition_names[0] != kLegacyPartitionNameRoot || 400 partition_names[1] != kLegacyPartitionNameKernel) 401 << "To support non-default partition name, please use the " 402 << "--new_partitions flag and major version 2."; 403 } 404 405 if (!FLAGS_new_partitions.empty()) { 406 LOG_IF(FATAL, !FLAGS_new_image.empty() || !FLAGS_new_kernel.empty()) 407 << "--new_image and --new_kernel are deprecated, please use " 408 << "--new_partitions for all partitions."; 409 base::SplitString(FLAGS_new_partitions, ':', &new_partitions); 410 CHECK(partition_names.size() == new_partitions.size()); 411 412 payload_config.is_delta = !FLAGS_old_partitions.empty(); 413 LOG_IF(FATAL, !FLAGS_old_image.empty() || !FLAGS_old_kernel.empty()) 414 << "--old_image and --old_kernel are deprecated, please use " 415 << "--old_partitions if you are using --new_partitions."; 416 } else { 417 new_partitions = {FLAGS_new_image, FLAGS_new_kernel}; 418 LOG(WARNING) << "--new_partitions is empty, using deprecated --new_image " 419 << "and --new_kernel flags."; 420 421 payload_config.is_delta = !FLAGS_old_image.empty() || 422 !FLAGS_old_kernel.empty(); 423 LOG_IF(FATAL, !FLAGS_old_partitions.empty()) 424 << "Please use --new_partitions if you are using --old_partitions."; 425 } 426 for (size_t i = 0; i < partition_names.size(); i++) { 427 LOG_IF(FATAL, partition_names[i].empty()) 428 << "Partition name can't be empty, see --partition_names."; 429 payload_config.target.partitions.emplace_back(partition_names[i]); 430 payload_config.target.partitions.back().path = new_partitions[i]; 431 } 432 433 if (payload_config.is_delta) { 434 if (!FLAGS_old_partitions.empty()) { 435 base::SplitString(FLAGS_old_partitions, ':', &old_partitions); 436 CHECK(old_partitions.size() == new_partitions.size()); 437 } else { 438 old_partitions = {FLAGS_old_image, FLAGS_old_kernel}; 439 LOG(WARNING) << "--old_partitions is empty, using deprecated --old_image " 440 << "and --old_kernel flags."; 441 } 442 for (size_t i = 0; i < partition_names.size(); i++) { 443 payload_config.source.partitions.emplace_back(partition_names[i]); 444 payload_config.source.partitions.back().path = old_partitions[i]; 445 } 446 } 447 448 // Use the default soft_chunk_size defined in the config. 449 payload_config.hard_chunk_size = FLAGS_chunk_size; 450 payload_config.block_size = kBlockSize; 451 452 // The partition size is never passed to the delta_generator, so we 453 // need to detect those from the provided files. 454 if (payload_config.is_delta) { 455 CHECK(payload_config.source.LoadImageSize()); 456 } 457 CHECK(payload_config.target.LoadImageSize()); 458 459 CHECK(!FLAGS_out_file.empty()); 460 461 // Ignore failures. These are optional arguments. 462 ParseImageInfo(FLAGS_new_channel, 463 FLAGS_new_board, 464 FLAGS_new_version, 465 FLAGS_new_key, 466 FLAGS_new_build_channel, 467 FLAGS_new_build_version, 468 &payload_config.target.image_info); 469 470 // Ignore failures. These are optional arguments. 471 ParseImageInfo(FLAGS_old_channel, 472 FLAGS_old_board, 473 FLAGS_old_version, 474 FLAGS_old_key, 475 FLAGS_old_build_channel, 476 FLAGS_old_build_version, 477 &payload_config.source.image_info); 478 479 payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size; 480 481 if (payload_config.is_delta) { 482 // Avoid opening the filesystem interface for full payloads. 483 for (PartitionConfig& part : payload_config.target.partitions) 484 CHECK(part.OpenFilesystem()); 485 for (PartitionConfig& part : payload_config.source.partitions) 486 CHECK(part.OpenFilesystem()); 487 } 488 489 payload_config.major_version = FLAGS_major_version; 490 LOG(INFO) << "Using provided major_version=" << FLAGS_major_version; 491 492 if (FLAGS_minor_version == -1) { 493 // Autodetect minor_version by looking at the update_engine.conf in the old 494 // image. 495 if (payload_config.is_delta) { 496 payload_config.minor_version = kInPlaceMinorPayloadVersion; 497 chromeos::KeyValueStore store; 498 uint32_t minor_version; 499 for (const PartitionConfig& part : payload_config.source.partitions) { 500 if (part.fs_interface && part.fs_interface->LoadSettings(&store) && 501 utils::GetMinorVersion(store, &minor_version)) { 502 payload_config.minor_version = minor_version; 503 break; 504 } 505 } 506 } else { 507 payload_config.minor_version = kFullPayloadMinorVersion; 508 } 509 LOG(INFO) << "Auto-detected minor_version=" << payload_config.minor_version; 510 } else { 511 payload_config.minor_version = FLAGS_minor_version; 512 LOG(INFO) << "Using provided minor_version=" << FLAGS_minor_version; 513 } 514 515 if (payload_config.is_delta) { 516 LOG(INFO) << "Generating delta update"; 517 } else { 518 LOG(INFO) << "Generating full update"; 519 } 520 521 // From this point, all the options have been parsed. 522 if (!payload_config.Validate()) { 523 LOG(ERROR) << "Invalid options passed. See errors above."; 524 return 1; 525 } 526 527 uint64_t metadata_size; 528 if (!GenerateUpdatePayloadFile(payload_config, 529 FLAGS_out_file, 530 FLAGS_private_key, 531 &metadata_size)) { 532 return 1; 533 } 534 if (!FLAGS_out_metadata_size_file.empty()) { 535 string metadata_size_string = std::to_string(metadata_size); 536 CHECK(utils::WriteFile(FLAGS_out_metadata_size_file.c_str(), 537 metadata_size_string.data(), 538 metadata_size_string.size())); 539 } 540 return 0; 541} 542 543} // namespace 544 545} // namespace chromeos_update_engine 546 547int main(int argc, char** argv) { 548 return chromeos_update_engine::Main(argc, argv); 549} 550