blob: 7c8b54bfe186f3554bae5ab0ed25355d7064fed6 [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.");
Darin Petkov8e447e02013-04-16 16:23:50 +020063DEFINE_int32(chunk_size, -1, "Payload chunk size (-1 -- no limit/default)");
Chris Sosad5ae1562013-04-23 13:20:18 -070064DEFINE_int64(rootfs_partition_size,
65 chromeos_update_engine::kRootFSPartitionSize,
66 "RootFS partition size for the image once installed");
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070067
adlr@google.com3defe6a2009-12-04 20:57:17 +000068// This file contains a simple program that takes an old path, a new path,
69// and an output file as arguments and the path to an output file and
70// generates a delta that can be sent to Chrome OS clients.
71
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080072using std::set;
73using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070074using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080075
adlr@google.com3defe6a2009-12-04 20:57:17 +000076namespace chromeos_update_engine {
77
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080078namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080079
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080080bool IsDir(const char* path) {
81 struct stat stbuf;
82 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
83 return S_ISDIR(stbuf.st_mode);
84}
85
Jay Srinivasanf4318702012-09-24 11:56:24 -070086void ParseSignatureSizes(vector<int>* sizes) {
87 LOG_IF(FATAL, FLAGS_signature_size.empty())
88 << "Must pass --signature_size to calculate hash for signing.";
89 vector<string> strsizes;
90 base::SplitString(FLAGS_signature_size, ':', &strsizes);
91 for (vector<string>::iterator it = strsizes.begin(), e = strsizes.end();
92 it != e; ++it) {
93 int size = 0;
94 bool parsing_successful = base::StringToInt(*it, &size);
95 LOG_IF(FATAL, !parsing_successful)
96 << "Invalid signature size: " << *it;
97 sizes->push_back(size);
98 }
99}
100
101
Darin Petkovda8c1362011-01-13 14:04:24 -0800102void CalculatePayloadHashForSigning() {
103 LOG(INFO) << "Calculating payload hash for signing.";
104 LOG_IF(FATAL, FLAGS_in_file.empty())
105 << "Must pass --in_file to calculate hash for signing.";
106 LOG_IF(FATAL, FLAGS_out_hash_file.empty())
107 << "Must pass --out_hash_file to calculate hash for signing.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700108 vector<int> sizes;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700109 ParseSignatureSizes(&sizes);
110
Darin Petkovda8c1362011-01-13 14:04:24 -0800111 vector<char> hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700112 bool result = PayloadSigner::HashPayloadForSigning(FLAGS_in_file, sizes,
113 &hash);
114 CHECK(result);
115
116 result = utils::WriteFile(FLAGS_out_hash_file.c_str(), hash.data(),
117 hash.size());
118 CHECK(result);
Darin Petkovda8c1362011-01-13 14:04:24 -0800119 LOG(INFO) << "Done calculating payload hash for signing.";
120}
121
Jay Srinivasanf4318702012-09-24 11:56:24 -0700122
123void CalculateMetadataHashForSigning() {
124 LOG(INFO) << "Calculating metadata hash for signing.";
125 LOG_IF(FATAL, FLAGS_in_file.empty())
126 << "Must pass --in_file to calculate metadata hash for signing.";
127 LOG_IF(FATAL, FLAGS_out_metadata_hash_file.empty())
128 << "Must pass --out_metadata_hash_file to calculate metadata hash.";
129 vector<int> sizes;
130 ParseSignatureSizes(&sizes);
131
132 vector<char> hash;
133 bool result = PayloadSigner::HashMetadataForSigning(FLAGS_in_file, &hash);
134 CHECK(result);
135
136 result = utils::WriteFile(FLAGS_out_metadata_hash_file.c_str(), hash.data(),
137 hash.size());
138 CHECK(result);
139
140 LOG(INFO) << "Done calculating metadata hash for signing.";
141}
142
Darin Petkovda8c1362011-01-13 14:04:24 -0800143void SignPayload() {
144 LOG(INFO) << "Signing payload.";
145 LOG_IF(FATAL, FLAGS_in_file.empty())
146 << "Must pass --in_file to sign payload.";
147 LOG_IF(FATAL, FLAGS_out_file.empty())
148 << "Must pass --out_file to sign payload.";
149 LOG_IF(FATAL, FLAGS_signature_file.empty())
150 << "Must pass --signature_file to sign payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700151 vector<vector<char> > signatures;
152 vector<string> signature_files;
153 base::SplitString(FLAGS_signature_file, ':', &signature_files);
154 for (vector<string>::iterator it = signature_files.begin(),
155 e = signature_files.end(); it != e; ++it) {
156 vector<char> signature;
157 CHECK(utils::ReadFile(*it, &signature));
158 signatures.push_back(signature);
159 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800160 uint64_t final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800161 CHECK(PayloadSigner::AddSignatureToPayload(
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800162 FLAGS_in_file, signatures, FLAGS_out_file, &final_metadata_size));
163 LOG(INFO) << "Done signing payload. Final metadata size = "
164 << final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800165}
166
Darin Petkovadb3cef2011-01-13 16:16:08 -0800167void VerifySignedPayload() {
168 LOG(INFO) << "Verifying signed payload.";
169 LOG_IF(FATAL, FLAGS_in_file.empty())
170 << "Must pass --in_file to verify signed payload.";
171 LOG_IF(FATAL, FLAGS_public_key.empty())
172 << "Must pass --public_key to verify signed payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700173 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key,
174 FLAGS_public_key_version));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800175 LOG(INFO) << "Done verifying signed payload.";
176}
177
Darin Petkovda8c1362011-01-13 14:04:24 -0800178void ApplyDelta() {
179 LOG(INFO) << "Applying delta.";
180 LOG_IF(FATAL, FLAGS_old_image.empty())
181 << "Must pass --old_image to apply delta.";
182 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700183 InstallPlan install_plan;
Darin Petkovda8c1362011-01-13 14:04:24 -0800184 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
185 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
186 << "Failed to initialize preferences.";
187 // Get original checksums
188 LOG(INFO) << "Calculating original checksums";
189 PartitionInfo kern_info, root_info;
190 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
191 FLAGS_old_kernel,
192 &kern_info));
193 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
194 FLAGS_old_image,
195 &root_info));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700196 install_plan.kernel_hash.assign(kern_info.hash().begin(),
197 kern_info.hash().end());
198 install_plan.rootfs_hash.assign(root_info.hash().begin(),
199 root_info.hash().end());
Jay Srinivasanf0572052012-10-23 18:12:56 -0700200 DeltaPerformer performer(&prefs, NULL, &install_plan);
Darin Petkovda8c1362011-01-13 14:04:24 -0800201 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
202 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
203 vector<char> buf(1024 * 1024);
204 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
205 CHECK_GE(fd, 0);
206 ScopedFdCloser fd_closer(&fd);
207 for (off_t offset = 0;; offset += buf.size()) {
208 ssize_t bytes_read;
209 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
210 if (bytes_read == 0)
211 break;
212 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
213 }
214 CHECK_EQ(performer.Close(), 0);
215 DeltaPerformer::ResetUpdateProgress(&prefs, false);
216 LOG(INFO) << "Done applying delta.";
217}
218
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800219int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000220 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700221 google::ParseCommandLineFlags(&argc, &argv, true);
222 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700223 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000224 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700225 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800226 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
227 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700228 logging::APPEND_TO_OLD_LOG_FILE,
229 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700230 if (!FLAGS_signature_size.empty()) {
231 bool work_done = false;
232 if (!FLAGS_out_hash_file.empty()) {
233 CalculatePayloadHashForSigning();
234 work_done = true;
235 }
236 if (!FLAGS_out_metadata_hash_file.empty()) {
237 CalculateMetadataHashForSigning();
238 work_done = true;
239 }
240 if (!work_done) {
241 LOG(FATAL) << "Neither payload hash file nor metadata hash file supplied";
242 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800243 return 0;
244 }
245 if (!FLAGS_signature_file.empty()) {
246 SignPayload();
247 return 0;
248 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800249 if (!FLAGS_public_key.empty()) {
250 VerifySignedPayload();
251 return 0;
252 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800253 if (!FLAGS_in_file.empty()) {
254 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700255 return 0;
256 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700257 CHECK(!FLAGS_new_image.empty());
258 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700259 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700260 if (FLAGS_old_image.empty()) {
261 LOG(INFO) << "Generating full update";
262 } else {
263 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700264 CHECK(!FLAGS_old_dir.empty());
265 CHECK(!FLAGS_new_dir.empty());
266 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
267 LOG(FATAL) << "old_dir or new_dir not directory";
268 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000269 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800270 uint64_t metadata_size;
Chris Sosac7c19cd2011-02-08 17:02:12 -0800271 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
272 FLAGS_old_image,
273 FLAGS_new_dir,
274 FLAGS_new_image,
275 FLAGS_old_kernel,
276 FLAGS_new_kernel,
277 FLAGS_out_file,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800278 FLAGS_private_key,
Darin Petkov8e447e02013-04-16 16:23:50 +0200279 FLAGS_chunk_size,
Chris Sosad5ae1562013-04-23 13:20:18 -0700280 FLAGS_rootfs_partition_size,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800281 &metadata_size)) {
Chris Sosac7c19cd2011-02-08 17:02:12 -0800282 return 1;
283 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000284 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800285}
286
287} // namespace {}
288
289} // namespace chromeos_update_engine
290
291int main(int argc, char** argv) {
292 return chromeos_update_engine::Main(argc, argv);
293}