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