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