Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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 <sys/types.h> |
| 6 | #include <sys/stat.h> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 7 | #include <errno.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include <unistd.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 10 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 11 | #include <set> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 12 | #include <string> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 13 | #include <vector> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 14 | |
| 15 | #include <base/command_line.h> |
| 16 | #include <base/logging.h> |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 17 | #include <gflags/gflags.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 18 | #include <glib.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 19 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 20 | #include "update_engine/delta_diff_generator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 21 | #include "update_engine/delta_performer.h" |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 22 | #include "update_engine/payload_signer.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 23 | #include "update_engine/prefs.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | #include "update_engine/subprocess.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 25 | #include "update_engine/terminator.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 26 | #include "update_engine/update_metadata.pb.h" |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 27 | #include "update_engine/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 29 | DEFINE_string(old_dir, "", |
| 30 | "Directory where the old rootfs is loop mounted read-only"); |
| 31 | DEFINE_string(new_dir, "", |
| 32 | "Directory where the new rootfs is loop mounted read-only"); |
| 33 | DEFINE_string(old_image, "", "Path to the old rootfs"); |
| 34 | DEFINE_string(new_image, "", "Path to the new rootfs"); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 35 | DEFINE_string(old_kernel, "", "Path to the old kernel partition image"); |
| 36 | DEFINE_string(new_kernel, "", "Path to the new kernel partition image"); |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 37 | DEFINE_string(in_file, "", |
| 38 | "Path to input delta payload file used to hash/sign payloads " |
| 39 | "and apply delta over old_image (for debugging)"); |
| 40 | DEFINE_string(out_file, "", "Path to output delta payload file"); |
| 41 | DEFINE_string(out_hash_file, "", "Path to output hash file"); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 42 | DEFINE_string(private_key, "", "Path to private key in .pem format"); |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 43 | DEFINE_string(public_key, "", "Path to public key in .pem format"); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 44 | DEFINE_string(prefs_dir, "/tmp/update_engine_prefs", |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 45 | "Preferences directory, used with apply_delta"); |
| 46 | DEFINE_int32(signature_size, 0, "Raw signature size used for hash calculation"); |
| 47 | DEFINE_string(signature_file, "", "Raw signature file to sign payload with"); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 48 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 49 | // This file contains a simple program that takes an old path, a new path, |
| 50 | // and an output file as arguments and the path to an output file and |
| 51 | // generates a delta that can be sent to Chrome OS clients. |
| 52 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 53 | using std::set; |
| 54 | using std::string; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 55 | using std::vector; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 56 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 57 | namespace chromeos_update_engine { |
| 58 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 59 | namespace { |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 60 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 61 | bool IsDir(const char* path) { |
| 62 | struct stat stbuf; |
| 63 | TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0); |
| 64 | return S_ISDIR(stbuf.st_mode); |
| 65 | } |
| 66 | |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 67 | void CalculatePayloadHashForSigning() { |
| 68 | LOG(INFO) << "Calculating payload hash for signing."; |
| 69 | LOG_IF(FATAL, FLAGS_in_file.empty()) |
| 70 | << "Must pass --in_file to calculate hash for signing."; |
| 71 | LOG_IF(FATAL, FLAGS_out_hash_file.empty()) |
| 72 | << "Must pass --out_hash_file to calculate hash for signing."; |
| 73 | LOG_IF(FATAL, FLAGS_signature_size <= 0) |
| 74 | << "Must pass --signature_size to calculate hash for signing."; |
| 75 | vector<char> hash; |
| 76 | CHECK(PayloadSigner::HashPayloadForSigning( |
| 77 | FLAGS_in_file, FLAGS_signature_size, &hash)); |
| 78 | CHECK(utils::WriteFile( |
| 79 | FLAGS_out_hash_file.c_str(), hash.data(), hash.size())); |
| 80 | LOG(INFO) << "Done calculating payload hash for signing."; |
| 81 | } |
| 82 | |
| 83 | void SignPayload() { |
| 84 | LOG(INFO) << "Signing payload."; |
| 85 | LOG_IF(FATAL, FLAGS_in_file.empty()) |
| 86 | << "Must pass --in_file to sign payload."; |
| 87 | LOG_IF(FATAL, FLAGS_out_file.empty()) |
| 88 | << "Must pass --out_file to sign payload."; |
| 89 | LOG_IF(FATAL, FLAGS_signature_file.empty()) |
| 90 | << "Must pass --signature_file to sign payload."; |
| 91 | vector<char> signature; |
| 92 | CHECK(utils::ReadFile(FLAGS_signature_file, &signature)); |
| 93 | CHECK(PayloadSigner::AddSignatureToPayload( |
| 94 | FLAGS_in_file, signature, FLAGS_out_file)); |
| 95 | LOG(INFO) << "Done signing payload."; |
| 96 | } |
| 97 | |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 98 | void VerifySignedPayload() { |
| 99 | LOG(INFO) << "Verifying signed payload."; |
| 100 | LOG_IF(FATAL, FLAGS_in_file.empty()) |
| 101 | << "Must pass --in_file to verify signed payload."; |
| 102 | LOG_IF(FATAL, FLAGS_public_key.empty()) |
| 103 | << "Must pass --public_key to verify signed payload."; |
| 104 | CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key)); |
| 105 | LOG(INFO) << "Done verifying signed payload."; |
| 106 | } |
| 107 | |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 108 | void ApplyDelta() { |
| 109 | LOG(INFO) << "Applying delta."; |
| 110 | LOG_IF(FATAL, FLAGS_old_image.empty()) |
| 111 | << "Must pass --old_image to apply delta."; |
| 112 | Prefs prefs; |
| 113 | LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir; |
| 114 | LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir))) |
| 115 | << "Failed to initialize preferences."; |
| 116 | // Get original checksums |
| 117 | LOG(INFO) << "Calculating original checksums"; |
| 118 | PartitionInfo kern_info, root_info; |
| 119 | CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel |
| 120 | FLAGS_old_kernel, |
| 121 | &kern_info)); |
| 122 | CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel |
| 123 | FLAGS_old_image, |
| 124 | &root_info)); |
| 125 | vector<char> kern_hash(kern_info.hash().begin(), |
| 126 | kern_info.hash().end()); |
| 127 | vector<char> root_hash(root_info.hash().begin(), |
| 128 | root_info.hash().end()); |
| 129 | DeltaPerformer performer(&prefs); |
| 130 | performer.set_current_kernel_hash(kern_hash); |
| 131 | performer.set_current_rootfs_hash(root_hash); |
| 132 | CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0); |
| 133 | CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str())); |
| 134 | vector<char> buf(1024 * 1024); |
| 135 | int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0); |
| 136 | CHECK_GE(fd, 0); |
| 137 | ScopedFdCloser fd_closer(&fd); |
| 138 | for (off_t offset = 0;; offset += buf.size()) { |
| 139 | ssize_t bytes_read; |
| 140 | CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read)); |
| 141 | if (bytes_read == 0) |
| 142 | break; |
| 143 | CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read); |
| 144 | } |
| 145 | CHECK_EQ(performer.Close(), 0); |
| 146 | DeltaPerformer::ResetUpdateProgress(&prefs, false); |
| 147 | LOG(INFO) << "Done applying delta."; |
| 148 | } |
| 149 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 150 | int Main(int argc, char** argv) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 151 | g_thread_init(NULL); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 152 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 153 | CommandLine::Init(argc, argv); |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 154 | Terminator::Init(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | Subprocess::Init(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 156 | logging::InitLogging("delta_generator.log", |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 157 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 158 | logging::DONT_LOCK_LOG_FILE, |
| 159 | logging::APPEND_TO_OLD_LOG_FILE); |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 160 | if (FLAGS_signature_size > 0 || !FLAGS_out_hash_file.empty()) { |
| 161 | CalculatePayloadHashForSigning(); |
| 162 | return 0; |
| 163 | } |
| 164 | if (!FLAGS_signature_file.empty()) { |
| 165 | SignPayload(); |
| 166 | return 0; |
| 167 | } |
Darin Petkov | adb3cef | 2011-01-13 16:16:08 -0800 | [diff] [blame] | 168 | if (!FLAGS_public_key.empty()) { |
| 169 | VerifySignedPayload(); |
| 170 | return 0; |
| 171 | } |
Darin Petkov | da8c136 | 2011-01-13 14:04:24 -0800 | [diff] [blame] | 172 | if (!FLAGS_in_file.empty()) { |
| 173 | ApplyDelta(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 176 | CHECK(!FLAGS_new_image.empty()); |
| 177 | CHECK(!FLAGS_out_file.empty()); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 178 | CHECK(!FLAGS_new_kernel.empty()); |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 179 | if (FLAGS_old_image.empty()) { |
| 180 | LOG(INFO) << "Generating full update"; |
| 181 | } else { |
| 182 | LOG(INFO) << "Generating delta update"; |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 183 | CHECK(!FLAGS_old_dir.empty()); |
| 184 | CHECK(!FLAGS_new_dir.empty()); |
| 185 | if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) { |
| 186 | LOG(FATAL) << "old_dir or new_dir not directory"; |
| 187 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | } |
Chris Sosa | c7c19cd | 2011-02-08 17:02:12 -0800 | [diff] [blame] | 189 | if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir, |
| 190 | FLAGS_old_image, |
| 191 | FLAGS_new_dir, |
| 192 | FLAGS_new_image, |
| 193 | FLAGS_old_kernel, |
| 194 | FLAGS_new_kernel, |
| 195 | FLAGS_out_file, |
| 196 | FLAGS_private_key)) { |
| 197 | return 1; |
| 198 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 199 | return 0; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | } // namespace {} |
| 203 | |
| 204 | } // namespace chromeos_update_engine |
| 205 | |
| 206 | int main(int argc, char** argv) { |
| 207 | return chromeos_update_engine::Main(argc, argv); |
| 208 | } |