generate_delta_main.cc revision 477aec2166a571cbe28081d806c3226e8b31b6e9
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/payload_generation_config.h" 24#include "update_engine/payload_generator/payload_signer.h" 25#include "update_engine/payload_verifier.h" 26#include "update_engine/prefs.h" 27#include "update_engine/subprocess.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_image, 177 const string& prefs_dir) { 178 LOG(INFO) << "Applying delta."; 179 LOG_IF(FATAL, old_image.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 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel 190 old_kernel, 191 &kern_info)); 192 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel 193 old_image, 194 &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_image.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_dir, "", 220 "Directory where the old rootfs is loop mounted read-only"); 221 DEFINE_string(new_dir, "", 222 "Directory where the new rootfs is loop mounted read-only"); 223 DEFINE_string(old_image, "", "Path to the old rootfs"); 224 DEFINE_string(new_image, "", "Path to the new rootfs"); 225 DEFINE_string(old_kernel, "", "Path to the old kernel partition image"); 226 DEFINE_string(new_kernel, "", "Path to the new kernel partition image"); 227 DEFINE_string(in_file, "", 228 "Path to input delta payload file used to hash/sign payloads " 229 "and apply delta over old_image (for debugging)"); 230 DEFINE_string(out_file, "", "Path to output delta payload file"); 231 DEFINE_string(out_hash_file, "", "Path to output hash file"); 232 DEFINE_string(out_metadata_hash_file, "", 233 "Path to output metadata hash file"); 234 DEFINE_string(private_key, "", "Path to private key in .pem format"); 235 DEFINE_string(public_key, "", "Path to public key in .pem format"); 236 DEFINE_int32(public_key_version, 237 chromeos_update_engine::kSignatureMessageCurrentVersion, 238 "Key-check version # of client"); 239 DEFINE_string(prefs_dir, "/tmp/update_engine_prefs", 240 "Preferences directory, used with apply_delta"); 241 DEFINE_string(signature_size, "", 242 "Raw signature size used for hash calculation. " 243 "You may pass in multiple sizes by colon separating them. E.g. " 244 "2048:2048:4096 will assume 3 signatures, the first two with " 245 "2048 size and the last 4096."); 246 DEFINE_string(signature_file, "", 247 "Raw signature file to sign payload with. To pass multiple " 248 "signatures, use a single argument with a colon between paths, " 249 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each " 250 "signature will be assigned a client version, starting from " 251 "kSignatureOriginalVersion."); 252 DEFINE_int32(chunk_size, -1, "Payload chunk size (-1 -- no limit/default)"); 253 DEFINE_uint64(rootfs_partition_size, 254 chromeos_update_engine::kRootFSPartitionSize, 255 "RootFS partition size for the image once installed"); 256 DEFINE_int32(minor_version, DeltaPerformer::kFullPayloadMinorVersion, 257 "The minor version of the payload being generated"); 258 259 DEFINE_string(old_channel, "", 260 "The channel for the old image. 'dev-channel', 'npo-channel', " 261 "etc. Ignored, except during delta generation."); 262 DEFINE_string(old_board, "", 263 "The board for the old image. 'x86-mario', 'lumpy', " 264 "etc. Ignored, except during delta generation."); 265 DEFINE_string(old_version, "", 266 "The build version of the old image. 1.2.3, etc."); 267 DEFINE_string(old_key, "", 268 "The key used to sign the old image. 'premp', 'mp', 'mp-v3'," 269 " etc"); 270 DEFINE_string(old_build_channel, "", 271 "The channel for the build of the old image. 'dev-channel', " 272 "etc, but will never contain special channels such as " 273 "'npo-channel'. Ignored, except during delta generation."); 274 DEFINE_string(old_build_version, "", 275 "The version of the build containing the old image."); 276 277 DEFINE_string(new_channel, "", 278 "The channel for the new image. 'dev-channel', 'npo-channel', " 279 "etc. Ignored, except during delta generation."); 280 DEFINE_string(new_board, "", 281 "The board for the new image. 'x86-mario', 'lumpy', " 282 "etc. Ignored, except during delta generation."); 283 DEFINE_string(new_version, "", 284 "The build version of the new image. 1.2.3, etc."); 285 DEFINE_string(new_key, "", 286 "The key used to sign the new image. 'premp', 'mp', 'mp-v3'," 287 " etc"); 288 DEFINE_string(new_build_channel, "", 289 "The channel for the build of the new image. 'dev-channel', " 290 "etc, but will never contain special channels such as " 291 "'npo-channel'. Ignored, except during delta generation."); 292 DEFINE_string(new_build_version, "", 293 "The version of the build containing the new image."); 294 295 chromeos::FlagHelper::Init(argc, argv, 296 "Generates a payload to provide to ChromeOS' update_engine.\n\n" 297 "This tool can create full payloads and also delta payloads if the src\n" 298 "image is provided. It also provides debugging options to apply, sign\n" 299 "and verify payloads."); 300 Terminator::Init(); 301 Subprocess::Init(); 302 303 logging::LoggingSettings log_settings; 304 log_settings.log_file = "delta_generator.log"; 305 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 306 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE; 307 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; 308 309 logging::InitLogging(log_settings); 310 311 vector<int> signature_sizes; 312 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes); 313 314 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) { 315 if (!FLAGS_out_hash_file.empty()) { 316 CalculatePayloadHashForSigning(signature_sizes, FLAGS_out_hash_file, 317 FLAGS_in_file); 318 } 319 if (!FLAGS_out_metadata_hash_file.empty()) { 320 CalculateMetadataHashForSigning(signature_sizes, 321 FLAGS_out_metadata_hash_file, 322 FLAGS_in_file); 323 } 324 return 0; 325 } 326 if (!FLAGS_signature_file.empty()) { 327 SignPayload(FLAGS_in_file, FLAGS_out_file, FLAGS_signature_file); 328 return 0; 329 } 330 if (!FLAGS_public_key.empty()) { 331 VerifySignedPayload(FLAGS_in_file, FLAGS_public_key, 332 FLAGS_public_key_version); 333 return 0; 334 } 335 if (!FLAGS_in_file.empty()) { 336 ApplyDelta(FLAGS_in_file, FLAGS_old_kernel, FLAGS_old_image, 337 FLAGS_prefs_dir); 338 return 0; 339 } 340 341 // A payload generation was requested. Convert the flags to a 342 // PayloadGenerationConfig. 343 PayloadGenerationConfig payload_config; 344 payload_config.source.rootfs_part = FLAGS_old_image; 345 payload_config.source.rootfs_mountpt = FLAGS_old_dir; 346 payload_config.source.kernel_part = FLAGS_old_kernel; 347 348 payload_config.target.rootfs_part = FLAGS_new_image; 349 payload_config.target.rootfs_mountpt = FLAGS_new_dir; 350 payload_config.target.kernel_part = FLAGS_new_kernel; 351 352 payload_config.chunk_size = FLAGS_chunk_size; 353 payload_config.block_size = kBlockSize; 354 355 // The kernel and rootfs size is never passed to the delta_generator, so we 356 // need to detect those from the provided files. 357 if (!FLAGS_old_image.empty()) { 358 CHECK(payload_config.source.LoadImageSize()); 359 } 360 if (!FLAGS_new_image.empty()) { 361 CHECK(payload_config.target.LoadImageSize()); 362 } 363 364 payload_config.is_delta = !FLAGS_old_image.empty(); 365 366 CHECK(!FLAGS_out_file.empty()); 367 368 // Ignore failures. These are optional arguments. 369 ParseImageInfo(FLAGS_new_channel, 370 FLAGS_new_board, 371 FLAGS_new_version, 372 FLAGS_new_key, 373 FLAGS_new_build_channel, 374 FLAGS_new_build_version, 375 &payload_config.target.image_info); 376 377 // Ignore failures. These are optional arguments. 378 ParseImageInfo(FLAGS_old_channel, 379 FLAGS_old_board, 380 FLAGS_old_version, 381 FLAGS_old_key, 382 FLAGS_old_build_channel, 383 FLAGS_old_build_version, 384 &payload_config.source.image_info); 385 386 payload_config.rootfs_partition_size = FLAGS_rootfs_partition_size; 387 payload_config.minor_version = FLAGS_minor_version; 388 // Look for the minor version in the old image if it was not given as an 389 // argument. 390 if (payload_config.is_delta && 391 !CommandLine::ForCurrentProcess()->HasSwitch("minor_version")) { 392 uint32_t minor_version; 393 base::FilePath image_path(FLAGS_old_dir); 394 base::FilePath conf_loc("etc/update_engine.conf"); 395 base::FilePath conf_path = image_path.Append(conf_loc); 396 if (utils::GetMinorVersion(conf_path, &minor_version)) { 397 payload_config.minor_version = minor_version; 398 } else { 399 payload_config.minor_version = kInPlaceMinorPayloadVersion; 400 } 401 } 402 403 // Load the rootfs size from verity's kernel command line if rootfs 404 // verification is enabled. 405 payload_config.source.LoadVerityRootfsSize(); 406 payload_config.target.LoadVerityRootfsSize(); 407 408 if (payload_config.is_delta) { 409 LOG(INFO) << "Generating delta update"; 410 } else { 411 LOG(INFO) << "Generating full update"; 412 } 413 414 // From this point, all the options have been parsed. 415 if (!payload_config.Validate()) { 416 LOG(ERROR) << "Invalid options passed. See errors above."; 417 return 1; 418 } 419 420 uint64_t metadata_size; 421 if (!GenerateUpdatePayloadFile(payload_config, 422 FLAGS_out_file, 423 FLAGS_private_key, 424 &metadata_size)) { 425 return 1; 426 } 427 428 return 0; 429} 430 431} // namespace 432 433} // namespace chromeos_update_engine 434 435int main(int argc, char** argv) { 436 return chromeos_update_engine::Main(argc, argv); 437} 438