blob: ede8ab93345e958006fa8dbb1b008345844e31e9 [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
Don Garrett0dd39852013-04-03 16:55:42 -070068DEFINE_string(old_channel, "",
69 "The channel for the old image. 'dev-channel', 'npo-channel', "
70 "etc. Ignored, except during delta generation.");
71DEFINE_string(old_board, "",
72 "The board for the old image. 'x86-mario', 'lumpy', "
73 "etc. Ignored, except during delta generation.");
74DEFINE_string(old_version, "",
75 "The build version of the old image. 1.2.3, etc.");
76DEFINE_string(old_key, "",
77 "The key used to sign the old image. 'premp', 'mp', 'mp-v3',"
78 " etc");
79DEFINE_string(old_build_channel, "",
80 "The channel for the build of the old image. 'dev-channel', "
81 "etc, but will never contain special channels such as "
82 "'npo-channel'. Ignored, except during delta generation.");
83DEFINE_string(old_build_version, "",
84 "The version of the build containing the old image.");
85
86DEFINE_string(new_channel, "",
87 "The channel for the new image. 'dev-channel', 'npo-channel', "
88 "etc. Ignored, except during delta generation.");
89DEFINE_string(new_board, "",
90 "The board for the new image. 'x86-mario', 'lumpy', "
91 "etc. Ignored, except during delta generation.");
92DEFINE_string(new_version, "",
93 "The build version of the new image. 1.2.3, etc.");
94DEFINE_string(new_key, "",
95 "The key used to sign the new image. 'premp', 'mp', 'mp-v3',"
96 " etc");
97DEFINE_string(new_build_channel, "",
98 "The channel for the build of the new image. 'dev-channel', "
99 "etc, but will never contain special channels such as "
100 "'npo-channel'. Ignored, except during delta generation.");
101DEFINE_string(new_build_version, "",
102 "The version of the build containing the new image.");
103
adlr@google.com3defe6a2009-12-04 20:57:17 +0000104// This file contains a simple program that takes an old path, a new path,
105// and an output file as arguments and the path to an output file and
106// generates a delta that can be sent to Chrome OS clients.
107
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800108using std::set;
109using std::string;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700110using std::vector;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800111
adlr@google.com3defe6a2009-12-04 20:57:17 +0000112namespace chromeos_update_engine {
113
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800114namespace {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800115
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800116bool IsDir(const char* path) {
117 struct stat stbuf;
118 TEST_AND_RETURN_FALSE_ERRNO(lstat(path, &stbuf) == 0);
119 return S_ISDIR(stbuf.st_mode);
120}
121
Jay Srinivasanf4318702012-09-24 11:56:24 -0700122void ParseSignatureSizes(vector<int>* sizes) {
123 LOG_IF(FATAL, FLAGS_signature_size.empty())
124 << "Must pass --signature_size to calculate hash for signing.";
125 vector<string> strsizes;
126 base::SplitString(FLAGS_signature_size, ':', &strsizes);
127 for (vector<string>::iterator it = strsizes.begin(), e = strsizes.end();
128 it != e; ++it) {
129 int size = 0;
130 bool parsing_successful = base::StringToInt(*it, &size);
131 LOG_IF(FATAL, !parsing_successful)
132 << "Invalid signature size: " << *it;
133 sizes->push_back(size);
134 }
135}
136
137
Don Garrett0dd39852013-04-03 16:55:42 -0700138bool ParseImageInfo(const string& channel,
139 const string& board,
140 const string& version,
141 const string& key,
142 const string& build_channel,
143 const string& build_version,
144 ImageInfo* image_info) {
145
146 // All of these arguments should be present or missing.
147 bool empty = channel.empty();
148
149 CHECK_EQ(channel.empty(), empty);
150 CHECK_EQ(board.empty(), empty);
151 CHECK_EQ(version.empty(), empty);
152 CHECK_EQ(key.empty(), empty);
153
154 if (empty)
155 return false;
156
157 image_info->set_channel(channel);
158 image_info->set_board(board);
159 image_info->set_version(version);
160 image_info->set_key(key);
161
162 image_info->set_build_channel(
163 build_channel.empty() ? channel : build_channel);
164
165 image_info->set_build_version(
166 build_version.empty() ? version : build_version);
167
168 return true;
169}
170
171
Darin Petkovda8c1362011-01-13 14:04:24 -0800172void CalculatePayloadHashForSigning() {
173 LOG(INFO) << "Calculating payload hash for signing.";
174 LOG_IF(FATAL, FLAGS_in_file.empty())
175 << "Must pass --in_file to calculate hash for signing.";
176 LOG_IF(FATAL, FLAGS_out_hash_file.empty())
177 << "Must pass --out_hash_file to calculate hash for signing.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700178 vector<int> sizes;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700179 ParseSignatureSizes(&sizes);
180
Darin Petkovda8c1362011-01-13 14:04:24 -0800181 vector<char> hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700182 bool result = PayloadSigner::HashPayloadForSigning(FLAGS_in_file, sizes,
183 &hash);
184 CHECK(result);
185
186 result = utils::WriteFile(FLAGS_out_hash_file.c_str(), hash.data(),
187 hash.size());
188 CHECK(result);
Darin Petkovda8c1362011-01-13 14:04:24 -0800189 LOG(INFO) << "Done calculating payload hash for signing.";
190}
191
Jay Srinivasanf4318702012-09-24 11:56:24 -0700192
193void CalculateMetadataHashForSigning() {
194 LOG(INFO) << "Calculating metadata hash for signing.";
195 LOG_IF(FATAL, FLAGS_in_file.empty())
196 << "Must pass --in_file to calculate metadata hash for signing.";
197 LOG_IF(FATAL, FLAGS_out_metadata_hash_file.empty())
198 << "Must pass --out_metadata_hash_file to calculate metadata hash.";
199 vector<int> sizes;
200 ParseSignatureSizes(&sizes);
201
202 vector<char> hash;
203 bool result = PayloadSigner::HashMetadataForSigning(FLAGS_in_file, &hash);
204 CHECK(result);
205
206 result = utils::WriteFile(FLAGS_out_metadata_hash_file.c_str(), hash.data(),
207 hash.size());
208 CHECK(result);
209
210 LOG(INFO) << "Done calculating metadata hash for signing.";
211}
212
Darin Petkovda8c1362011-01-13 14:04:24 -0800213void SignPayload() {
214 LOG(INFO) << "Signing payload.";
215 LOG_IF(FATAL, FLAGS_in_file.empty())
216 << "Must pass --in_file to sign payload.";
217 LOG_IF(FATAL, FLAGS_out_file.empty())
218 << "Must pass --out_file to sign payload.";
219 LOG_IF(FATAL, FLAGS_signature_file.empty())
220 << "Must pass --signature_file to sign payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700221 vector<vector<char> > signatures;
222 vector<string> signature_files;
223 base::SplitString(FLAGS_signature_file, ':', &signature_files);
224 for (vector<string>::iterator it = signature_files.begin(),
225 e = signature_files.end(); it != e; ++it) {
226 vector<char> signature;
227 CHECK(utils::ReadFile(*it, &signature));
228 signatures.push_back(signature);
229 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800230 uint64_t final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800231 CHECK(PayloadSigner::AddSignatureToPayload(
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800232 FLAGS_in_file, signatures, FLAGS_out_file, &final_metadata_size));
233 LOG(INFO) << "Done signing payload. Final metadata size = "
234 << final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800235}
236
Darin Petkovadb3cef2011-01-13 16:16:08 -0800237void VerifySignedPayload() {
238 LOG(INFO) << "Verifying signed payload.";
239 LOG_IF(FATAL, FLAGS_in_file.empty())
240 << "Must pass --in_file to verify signed payload.";
241 LOG_IF(FATAL, FLAGS_public_key.empty())
242 << "Must pass --public_key to verify signed payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700243 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key,
244 FLAGS_public_key_version));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800245 LOG(INFO) << "Done verifying signed payload.";
246}
247
Darin Petkovda8c1362011-01-13 14:04:24 -0800248void ApplyDelta() {
249 LOG(INFO) << "Applying delta.";
250 LOG_IF(FATAL, FLAGS_old_image.empty())
251 << "Must pass --old_image to apply delta.";
252 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700253 InstallPlan install_plan;
Darin Petkovda8c1362011-01-13 14:04:24 -0800254 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
255 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
256 << "Failed to initialize preferences.";
257 // Get original checksums
258 LOG(INFO) << "Calculating original checksums";
259 PartitionInfo kern_info, root_info;
260 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
261 FLAGS_old_kernel,
262 &kern_info));
263 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
264 FLAGS_old_image,
265 &root_info));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700266 install_plan.kernel_hash.assign(kern_info.hash().begin(),
267 kern_info.hash().end());
268 install_plan.rootfs_hash.assign(root_info.hash().begin(),
269 root_info.hash().end());
Jay Srinivasanf0572052012-10-23 18:12:56 -0700270 DeltaPerformer performer(&prefs, NULL, &install_plan);
Darin Petkovda8c1362011-01-13 14:04:24 -0800271 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
272 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
273 vector<char> buf(1024 * 1024);
274 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
275 CHECK_GE(fd, 0);
276 ScopedFdCloser fd_closer(&fd);
277 for (off_t offset = 0;; offset += buf.size()) {
278 ssize_t bytes_read;
279 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
280 if (bytes_read == 0)
281 break;
282 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
283 }
284 CHECK_EQ(performer.Close(), 0);
285 DeltaPerformer::ResetUpdateProgress(&prefs, false);
286 LOG(INFO) << "Done applying delta.";
287}
288
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800289int Main(int argc, char** argv) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700290 google::ParseCommandLineFlags(&argc, &argv, true);
291 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700292 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000293 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700294 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800295 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
296 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700297 logging::APPEND_TO_OLD_LOG_FILE,
298 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700299 if (!FLAGS_signature_size.empty()) {
300 bool work_done = false;
301 if (!FLAGS_out_hash_file.empty()) {
302 CalculatePayloadHashForSigning();
303 work_done = true;
304 }
305 if (!FLAGS_out_metadata_hash_file.empty()) {
306 CalculateMetadataHashForSigning();
307 work_done = true;
308 }
309 if (!work_done) {
310 LOG(FATAL) << "Neither payload hash file nor metadata hash file supplied";
311 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800312 return 0;
313 }
314 if (!FLAGS_signature_file.empty()) {
315 SignPayload();
316 return 0;
317 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800318 if (!FLAGS_public_key.empty()) {
319 VerifySignedPayload();
320 return 0;
321 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800322 if (!FLAGS_in_file.empty()) {
323 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700324 return 0;
325 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700326 CHECK(!FLAGS_new_image.empty());
327 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700328 CHECK(!FLAGS_new_kernel.empty());
Don Garrett0dd39852013-04-03 16:55:42 -0700329
330 bool is_delta = !FLAGS_old_image.empty();
331
332 ImageInfo old_image_info;
333 ImageInfo new_image_info;
334
Don Garrett60fc59c2013-10-18 11:43:52 -0700335 // Ignore failures. These are optional arguments.
Don Garrett0dd39852013-04-03 16:55:42 -0700336 ParseImageInfo(FLAGS_new_channel,
337 FLAGS_new_board,
338 FLAGS_new_version,
339 FLAGS_new_key,
340 FLAGS_new_build_channel,
341 FLAGS_new_build_version,
342 &new_image_info);
343
Don Garrett60fc59c2013-10-18 11:43:52 -0700344 // Ignore failures. These are optional arguments.
345 ParseImageInfo(FLAGS_old_channel,
346 FLAGS_old_board,
347 FLAGS_old_version,
348 FLAGS_old_key,
349 FLAGS_old_build_channel,
350 FLAGS_old_build_version,
351 &old_image_info);
Don Garrett0dd39852013-04-03 16:55:42 -0700352
353 if (is_delta) {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700354 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700355 CHECK(!FLAGS_old_dir.empty());
356 CHECK(!FLAGS_new_dir.empty());
357 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
358 LOG(FATAL) << "old_dir or new_dir not directory";
359 }
Don Garrett0dd39852013-04-03 16:55:42 -0700360 } else {
361 LOG(INFO) << "Generating full update";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000362 }
Don Garrett0dd39852013-04-03 16:55:42 -0700363
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800364 uint64_t metadata_size;
Don Garrett0dd39852013-04-03 16:55:42 -0700365 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(
366 FLAGS_old_dir,
367 FLAGS_old_image,
368 FLAGS_new_dir,
369 FLAGS_new_image,
370 FLAGS_old_kernel,
371 FLAGS_new_kernel,
372 FLAGS_out_file,
373 FLAGS_private_key,
374 FLAGS_chunk_size,
375 FLAGS_rootfs_partition_size,
376 is_delta ? &old_image_info : NULL,
377 &new_image_info,
378 &metadata_size)) {
Chris Sosac7c19cd2011-02-08 17:02:12 -0800379 return 1;
380 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000381 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800382}
383
384} // namespace {}
385
386} // namespace chromeos_update_engine
387
388int main(int argc, char** argv) {
389 return chromeos_update_engine::Main(argc, argv);
390}