blob: 6991813fd3ce048aadaafd14d6a463f53c3d10ad [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 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800156 CHECK(PayloadSigner::AddSignatureToPayload(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700157 FLAGS_in_file, signatures, FLAGS_out_file));
Darin Petkovda8c1362011-01-13 14:04:24 -0800158 LOG(INFO) << "Done signing payload.";
159}
160
Darin Petkovadb3cef2011-01-13 16:16:08 -0800161void VerifySignedPayload() {
162 LOG(INFO) << "Verifying signed payload.";
163 LOG_IF(FATAL, FLAGS_in_file.empty())
164 << "Must pass --in_file to verify signed payload.";
165 LOG_IF(FATAL, FLAGS_public_key.empty())
166 << "Must pass --public_key to verify signed payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700167 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key,
168 FLAGS_public_key_version));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800169 LOG(INFO) << "Done verifying signed payload.";
170}
171
Darin Petkovda8c1362011-01-13 14:04:24 -0800172void ApplyDelta() {
173 LOG(INFO) << "Applying delta.";
174 LOG_IF(FATAL, FLAGS_old_image.empty())
175 << "Must pass --old_image to apply delta.";
176 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700177 InstallPlan install_plan;
Darin Petkovda8c1362011-01-13 14:04:24 -0800178 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
179 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
180 << "Failed to initialize preferences.";
181 // Get original checksums
182 LOG(INFO) << "Calculating original checksums";
183 PartitionInfo kern_info, root_info;
184 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
185 FLAGS_old_kernel,
186 &kern_info));
187 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
188 FLAGS_old_image,
189 &root_info));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700190 install_plan.kernel_hash.assign(kern_info.hash().begin(),
191 kern_info.hash().end());
192 install_plan.rootfs_hash.assign(root_info.hash().begin(),
193 root_info.hash().end());
194 DeltaPerformer performer(&prefs, &install_plan);
Darin Petkovda8c1362011-01-13 14:04:24 -0800195 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
196 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
197 vector<char> buf(1024 * 1024);
198 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
199 CHECK_GE(fd, 0);
200 ScopedFdCloser fd_closer(&fd);
201 for (off_t offset = 0;; offset += buf.size()) {
202 ssize_t bytes_read;
203 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
204 if (bytes_read == 0)
205 break;
206 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
207 }
208 CHECK_EQ(performer.Close(), 0);
209 DeltaPerformer::ResetUpdateProgress(&prefs, false);
210 LOG(INFO) << "Done applying delta.";
211}
212
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800213int Main(int argc, char** argv) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214 g_thread_init(NULL);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700215 google::ParseCommandLineFlags(&argc, &argv, true);
216 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700217 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000218 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700219 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800220 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
221 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700222 logging::APPEND_TO_OLD_LOG_FILE,
223 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700224 if (!FLAGS_signature_size.empty()) {
225 bool work_done = false;
226 if (!FLAGS_out_hash_file.empty()) {
227 CalculatePayloadHashForSigning();
228 work_done = true;
229 }
230 if (!FLAGS_out_metadata_hash_file.empty()) {
231 CalculateMetadataHashForSigning();
232 work_done = true;
233 }
234 if (!work_done) {
235 LOG(FATAL) << "Neither payload hash file nor metadata hash file supplied";
236 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800237 return 0;
238 }
239 if (!FLAGS_signature_file.empty()) {
240 SignPayload();
241 return 0;
242 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800243 if (!FLAGS_public_key.empty()) {
244 VerifySignedPayload();
245 return 0;
246 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800247 if (!FLAGS_in_file.empty()) {
248 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700249 return 0;
250 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700251 CHECK(!FLAGS_new_image.empty());
252 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700253 CHECK(!FLAGS_new_kernel.empty());
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700254 if (FLAGS_old_image.empty()) {
255 LOG(INFO) << "Generating full update";
256 } else {
257 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700258 CHECK(!FLAGS_old_dir.empty());
259 CHECK(!FLAGS_new_dir.empty());
260 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
261 LOG(FATAL) << "old_dir or new_dir not directory";
262 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000263 }
Chris Sosac7c19cd2011-02-08 17:02:12 -0800264 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(FLAGS_old_dir,
265 FLAGS_old_image,
266 FLAGS_new_dir,
267 FLAGS_new_image,
268 FLAGS_old_kernel,
269 FLAGS_new_kernel,
270 FLAGS_out_file,
271 FLAGS_private_key)) {
272 return 1;
273 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000274 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800275}
276
277} // namespace {}
278
279} // namespace chromeos_update_engine
280
281int main(int argc, char** argv) {
282 return chromeos_update_engine::Main(argc, argv);
283}