blob: aa2b9cb70be4a835a8fe3912c5c35b8dcc516e46 [file] [log] [blame]
Darin Petkov73058b42010-10-06 16:32:19 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// 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 Reyes4fe15d02009-12-10 19:01:36 -08007#include <errno.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07008#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <unistd.h>
Darin Petkov73058b42010-10-06 16:32:19 -070010
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080011#include <set>
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include <string>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070013#include <vector>
Darin Petkov73058b42010-10-06 16:32:19 -070014
15#include <base/command_line.h>
16#include <base/logging.h>
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070017#include <base/string_number_conversions.h>
18#include <base/string_split.h>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070019#include <gflags/gflags.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include <glib.h>
Darin Petkov73058b42010-10-06 16:32:19 -070021
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080022#include "update_engine/delta_diff_generator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070023#include "update_engine/delta_performer.h"
Darin Petkovda8c1362011-01-13 14:04:24 -080024#include "update_engine/payload_signer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070025#include "update_engine/prefs.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000026#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070027#include "update_engine/terminator.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000028#include "update_engine/update_metadata.pb.h"
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080029#include "update_engine/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000030
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070031DEFINE_string(old_dir, "",
32 "Directory where the old rootfs is loop mounted read-only");
33DEFINE_string(new_dir, "",
34 "Directory where the new rootfs is loop mounted read-only");
35DEFINE_string(old_image, "", "Path to the old rootfs");
36DEFINE_string(new_image, "", "Path to the new rootfs");
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070037DEFINE_string(old_kernel, "", "Path to the old kernel partition image");
38DEFINE_string(new_kernel, "", "Path to the new kernel partition image");
Darin Petkovda8c1362011-01-13 14:04:24 -080039DEFINE_string(in_file, "",
40 "Path to input delta payload file used to hash/sign payloads "
41 "and apply delta over old_image (for debugging)");
42DEFINE_string(out_file, "", "Path to output delta payload file");
43DEFINE_string(out_hash_file, "", "Path to output hash file");
Jay Srinivasanf4318702012-09-24 11:56:24 -070044DEFINE_string(out_metadata_hash_file, "", "Path to output metadata hash file");
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070045DEFINE_string(private_key, "", "Path to private key in .pem format");
Darin Petkovadb3cef2011-01-13 16:16:08 -080046DEFINE_string(public_key, "", "Path to public key in .pem format");
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070047DEFINE_int32(public_key_version,
48 chromeos_update_engine::kSignatureMessageCurrentVersion,
49 "Key-check version # of client");
Darin Petkov73058b42010-10-06 16:32:19 -070050DEFINE_string(prefs_dir, "/tmp/update_engine_prefs",
Darin Petkovda8c1362011-01-13 14:04:24 -080051 "Preferences directory, used with apply_delta");
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -070052DEFINE_string(signature_size, "",
53 "Raw signature size used for hash calculation. "
54 "You may pass in multiple sizes by colon separating them. E.g. "
55 "2048:2048:4096 will assume 3 signatures, the first two with "
56 "2048 size and the last 4096.");
57DEFINE_string(signature_file, "",
58 "Raw signature file to sign payload with. To pass multiple "
59 "signatures, use a single argument with a colon between paths, "
60 "e.g. /path/to/sig:/path/to/next:/path/to/last_sig . Each "
61 "signature will be assigned a client version, starting from "
62 "kSignatureOriginalVersion.");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070063
adlr@google.com3defe6a2009-12-04 20:57:17 +000064// This file contains a simple program that takes an old path, a new path,
65// and an output file as arguments and the path to an output file and
66// generates a delta that can be sent to Chrome OS clients.
67
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080068using std::set;
69using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070070using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080071
adlr@google.com3defe6a2009-12-04 20:57:17 +000072namespace chromeos_update_engine {
73
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080074namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080075
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080076bool IsDir(const char* path) {
77 struct stat stbuf;
78 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
79 return S_ISDIR(stbuf.st_mode);
80}
81
Jay Srinivasanf4318702012-09-24 11:56:24 -070082void ParseSignatureSizes(vector<int>* sizes) {
83 LOG_IF(FATAL, FLAGS_signature_size.empty())
84 << "Must pass --signature_size to calculate hash for signing.";
85 vector<string> strsizes;
86 base::SplitString(FLAGS_signature_size, ':', &strsizes);
87 for (vector<string>::iterator it = strsizes.begin(), e = strsizes.end();
88 it != e; ++it) {
89 int size = 0;
90 bool parsing_successful = base::StringToInt(*it, &size);
91 LOG_IF(FATAL, !parsing_successful)
92 << "Invalid signature size: " << *it;
93 sizes->push_back(size);
94 }
95}
96
97
Darin Petkovda8c1362011-01-13 14:04:24 -080098void CalculatePayloadHashForSigning() {
99 LOG(INFO) << "Calculating payload hash for signing.";
100 LOG_IF(FATAL, FLAGS_in_file.empty())
101 << "Must pass --in_file to calculate hash for signing.";
102 LOG_IF(FATAL, FLAGS_out_hash_file.empty())
103 << "Must pass --out_hash_file to calculate hash for signing.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700104 vector<int> sizes;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700105 ParseSignatureSizes(&sizes);
106
Darin Petkovda8c1362011-01-13 14:04:24 -0800107 vector<char> hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700108 bool result = PayloadSigner::HashPayloadForSigning(FLAGS_in_file, sizes,
109 &hash);
110 CHECK(result);
111
112 result = utils::WriteFile(FLAGS_out_hash_file.c_str(), hash.data(),
113 hash.size());
114 CHECK(result);
Darin Petkovda8c1362011-01-13 14:04:24 -0800115 LOG(INFO) << "Done calculating payload hash for signing.";
116}
117
Jay Srinivasanf4318702012-09-24 11:56:24 -0700118
119void CalculateMetadataHashForSigning() {
120 LOG(INFO) << "Calculating metadata hash for signing.";
121 LOG_IF(FATAL, FLAGS_in_file.empty())
122 << "Must pass --in_file to calculate metadata hash for signing.";
123 LOG_IF(FATAL, FLAGS_out_metadata_hash_file.empty())
124 << "Must pass --out_metadata_hash_file to calculate metadata hash.";
125 vector<int> sizes;
126 ParseSignatureSizes(&sizes);
127
128 vector<char> hash;
129 bool result = PayloadSigner::HashMetadataForSigning(FLAGS_in_file, &hash);
130 CHECK(result);
131
132 result = utils::WriteFile(FLAGS_out_metadata_hash_file.c_str(), hash.data(),
133 hash.size());
134 CHECK(result);
135
136 LOG(INFO) << "Done calculating metadata hash for signing.";
137}
138
Darin Petkovda8c1362011-01-13 14:04:24 -0800139void SignPayload() {
140 LOG(INFO) << "Signing payload.";
141 LOG_IF(FATAL, FLAGS_in_file.empty())
142 << "Must pass --in_file to sign payload.";
143 LOG_IF(FATAL, FLAGS_out_file.empty())
144 << "Must pass --out_file to sign payload.";
145 LOG_IF(FATAL, FLAGS_signature_file.empty())
146 << "Must pass --signature_file to sign payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700147 vector<vector<char> > signatures;
148 vector<string> signature_files;
149 base::SplitString(FLAGS_signature_file, ':', &signature_files);
150 for (vector<string>::iterator it = signature_files.begin(),
151 e = signature_files.end(); it != e; ++it) {
152 vector<char> signature;
153 CHECK(utils::ReadFile(*it, &signature));
154 signatures.push_back(signature);
155 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800156 uint64_t final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800157 CHECK(PayloadSigner::AddSignatureToPayload(
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800158 FLAGS_in_file, signatures, FLAGS_out_file, &final_metadata_size));
159 LOG(INFO) << "Done signing payload. Final metadata size = "
160 << final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800161}
162
Darin Petkovadb3cef2011-01-13 16:16:08 -0800163void VerifySignedPayload() {
164 LOG(INFO) << "Verifying signed payload.";
165 LOG_IF(FATAL, FLAGS_in_file.empty())
166 << "Must pass --in_file to verify signed payload.";
167 LOG_IF(FATAL, FLAGS_public_key.empty())
168 << "Must pass --public_key to verify signed payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700169 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key,
170 FLAGS_public_key_version));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800171 LOG(INFO) << "Done verifying signed payload.";
172}
173
Darin Petkovda8c1362011-01-13 14:04:24 -0800174void ApplyDelta() {
175 LOG(INFO) << "Applying delta.";
176 LOG_IF(FATAL, FLAGS_old_image.empty())
177 << "Must pass --old_image to apply delta.";
178 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700179 InstallPlan install_plan;
Darin Petkovda8c1362011-01-13 14:04:24 -0800180 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
181 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
182 << "Failed to initialize preferences.";
183 // Get original checksums
184 LOG(INFO) << "Calculating original checksums";
185 PartitionInfo kern_info, root_info;
186 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
187 FLAGS_old_kernel,
188 &kern_info));
189 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
190 FLAGS_old_image,
191 &root_info));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700192 install_plan.kernel_hash.assign(kern_info.hash().begin(),
193 kern_info.hash().end());
194 install_plan.rootfs_hash.assign(root_info.hash().begin(),
195 root_info.hash().end());
Jay Srinivasanf0572052012-10-23 18:12:56 -0700196 DeltaPerformer performer(&prefs, NULL, &install_plan);
Darin Petkovda8c1362011-01-13 14:04:24 -0800197 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
198 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
199 vector<char> buf(1024 * 1024);
200 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
201 CHECK_GE(fd, 0);
202 ScopedFdCloser fd_closer(&fd);
203 for (off_t offset = 0;; offset += buf.size()) {
204 ssize_t bytes_read;
205 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
206 if (bytes_read == 0)
207 break;
208 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
209 }
210 CHECK_EQ(performer.Close(), 0);
211 DeltaPerformer::ResetUpdateProgress(&prefs, false);
212 LOG(INFO) << "Done applying delta.";
213}
214
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800215int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000216 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700217 google::ParseCommandLineFlags(&argc, &argv, true);
218 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700219 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000220 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700221 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800222 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
223 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700224 logging::APPEND_TO_OLD_LOG_FILE,
225 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700226 if (!FLAGS_signature_size.empty()) {
227 bool work_done = false;
228 if (!FLAGS_out_hash_file.empty()) {
229 CalculatePayloadHashForSigning();
230 work_done = true;
231 }
232 if (!FLAGS_out_metadata_hash_file.empty()) {
233 CalculateMetadataHashForSigning();
234 work_done = true;
235 }
236 if (!work_done) {
237 LOG(FATAL) << "Neither payload hash file nor metadata hash file supplied";
238 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800239 return 0;
240 }
241 if (!FLAGS_signature_file.empty()) {
242 SignPayload();
243 return 0;
244 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800245 if (!FLAGS_public_key.empty()) {
246 VerifySignedPayload();
247 return 0;
248 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800249 if (!FLAGS_in_file.empty()) {
250 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700251 return 0;
252 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700253 CHECK(!FLAGS_new_image.empty());
254 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700255 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700256 if (FLAGS_old_image.empty()) {
257 LOG(INFO) << "Generating full update";
258 } else {
259 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700260 CHECK(!FLAGS_old_dir.empty());
261 CHECK(!FLAGS_new_dir.empty());
262 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
263 LOG(FATAL) << "old_dir or new_dir not directory";
264 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000265 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800266 uint64_t metadata_size;
Chris Sosac7c19cd2011-02-08 17:02:12 -0800267 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
268 FLAGS_old_image,
269 FLAGS_new_dir,
270 FLAGS_new_image,
271 FLAGS_old_kernel,
272 FLAGS_new_kernel,
273 FLAGS_out_file,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800274 FLAGS_private_key,
275 &metadata_size)) {
Chris Sosac7c19cd2011-02-08 17:02:12 -0800276 return 1;
277 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000278 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800279}
280
281} // namespace {}
282
283} // namespace chromeos_update_engine
284
285int main(int argc, char** argv) {
286 return chromeos_update_engine::Main(argc, argv);
287}