blob: b239450fe1199e241459dceec2a9e978cb607a6f [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
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700122void ParseSignatureSizes(const string& signature_sizes_flag,
123 vector<int>* signature_sizes) {
124 signature_sizes->clear();
125 vector<string> split_strings;
126
127 base::SplitString(signature_sizes_flag, ':', &split_strings);
128 for (vector<string>::iterator i = split_strings.begin();
129 i < split_strings.end();
130 i++) {
Jay Srinivasanf4318702012-09-24 11:56:24 -0700131 int size = 0;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700132 bool parsing_successful = base::StringToInt(*i, &size);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700133 LOG_IF(FATAL, !parsing_successful)
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700134 << "Invalid signature size: " << *i;
135
136 LOG_IF(FATAL, size != (2048 / 8)) <<
137 "Only signature sizes of 256 bytes are supported.";
138
139 signature_sizes->push_back(size);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700140 }
141}
142
143
Don Garrett0dd39852013-04-03 16:55:42 -0700144bool ParseImageInfo(const string& channel,
145 const string& board,
146 const string& version,
147 const string& key,
148 const string& build_channel,
149 const string& build_version,
150 ImageInfo* image_info) {
151
152 // All of these arguments should be present or missing.
153 bool empty = channel.empty();
154
155 CHECK_EQ(channel.empty(), empty);
156 CHECK_EQ(board.empty(), empty);
157 CHECK_EQ(version.empty(), empty);
158 CHECK_EQ(key.empty(), empty);
159
160 if (empty)
161 return false;
162
163 image_info->set_channel(channel);
164 image_info->set_board(board);
165 image_info->set_version(version);
166 image_info->set_key(key);
167
168 image_info->set_build_channel(
169 build_channel.empty() ? channel : build_channel);
170
171 image_info->set_build_version(
172 build_version.empty() ? version : build_version);
173
174 return true;
175}
176
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700177void CalculatePayloadHashForSigning(const vector<int> &sizes,
178 const string& out_hash_file) {
Darin Petkovda8c1362011-01-13 14:04:24 -0800179 LOG(INFO) << "Calculating payload hash for signing.";
180 LOG_IF(FATAL, FLAGS_in_file.empty())
181 << "Must pass --in_file to calculate hash for signing.";
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700182 LOG_IF(FATAL, out_hash_file.empty())
Darin Petkovda8c1362011-01-13 14:04:24 -0800183 << "Must pass --out_hash_file to calculate hash for signing.";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700184
Darin Petkovda8c1362011-01-13 14:04:24 -0800185 vector<char> hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -0700186 bool result = PayloadSigner::HashPayloadForSigning(FLAGS_in_file, sizes,
187 &hash);
188 CHECK(result);
189
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700190 result = utils::WriteFile(out_hash_file.c_str(), hash.data(), hash.size());
Jay Srinivasanf4318702012-09-24 11:56:24 -0700191 CHECK(result);
Darin Petkovda8c1362011-01-13 14:04:24 -0800192 LOG(INFO) << "Done calculating payload hash for signing.";
193}
194
Jay Srinivasanf4318702012-09-24 11:56:24 -0700195
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700196void CalculateMetadataHashForSigning(const vector<int> &sizes,
197 const string& out_metadata_hash_file) {
Jay Srinivasanf4318702012-09-24 11:56:24 -0700198 LOG(INFO) << "Calculating metadata hash for signing.";
199 LOG_IF(FATAL, FLAGS_in_file.empty())
200 << "Must pass --in_file to calculate metadata hash for signing.";
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700201 LOG_IF(FATAL, out_metadata_hash_file.empty())
Jay Srinivasanf4318702012-09-24 11:56:24 -0700202 << "Must pass --out_metadata_hash_file to calculate metadata hash.";
Jay Srinivasanf4318702012-09-24 11:56:24 -0700203
204 vector<char> hash;
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700205 bool result = PayloadSigner::HashMetadataForSigning(FLAGS_in_file, sizes,
206 &hash);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700207 CHECK(result);
208
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700209 result = utils::WriteFile(out_metadata_hash_file.c_str(), hash.data(),
Jay Srinivasanf4318702012-09-24 11:56:24 -0700210 hash.size());
211 CHECK(result);
212
213 LOG(INFO) << "Done calculating metadata hash for signing.";
214}
215
Darin Petkovda8c1362011-01-13 14:04:24 -0800216void SignPayload() {
217 LOG(INFO) << "Signing payload.";
218 LOG_IF(FATAL, FLAGS_in_file.empty())
219 << "Must pass --in_file to sign payload.";
220 LOG_IF(FATAL, FLAGS_out_file.empty())
221 << "Must pass --out_file to sign payload.";
222 LOG_IF(FATAL, FLAGS_signature_file.empty())
223 << "Must pass --signature_file to sign payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700224 vector<vector<char> > signatures;
225 vector<string> signature_files;
226 base::SplitString(FLAGS_signature_file, ':', &signature_files);
227 for (vector<string>::iterator it = signature_files.begin(),
228 e = signature_files.end(); it != e; ++it) {
229 vector<char> signature;
230 CHECK(utils::ReadFile(*it, &signature));
231 signatures.push_back(signature);
232 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800233 uint64_t final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800234 CHECK(PayloadSigner::AddSignatureToPayload(
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800235 FLAGS_in_file, signatures, FLAGS_out_file, &final_metadata_size));
236 LOG(INFO) << "Done signing payload. Final metadata size = "
237 << final_metadata_size;
Darin Petkovda8c1362011-01-13 14:04:24 -0800238}
239
Darin Petkovadb3cef2011-01-13 16:16:08 -0800240void VerifySignedPayload() {
241 LOG(INFO) << "Verifying signed payload.";
242 LOG_IF(FATAL, FLAGS_in_file.empty())
243 << "Must pass --in_file to verify signed payload.";
244 LOG_IF(FATAL, FLAGS_public_key.empty())
245 << "Must pass --public_key to verify signed payload.";
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700246 CHECK(PayloadSigner::VerifySignedPayload(FLAGS_in_file, FLAGS_public_key,
247 FLAGS_public_key_version));
Darin Petkovadb3cef2011-01-13 16:16:08 -0800248 LOG(INFO) << "Done verifying signed payload.";
249}
250
Darin Petkovda8c1362011-01-13 14:04:24 -0800251void ApplyDelta() {
252 LOG(INFO) << "Applying delta.";
253 LOG_IF(FATAL, FLAGS_old_image.empty())
254 << "Must pass --old_image to apply delta.";
255 Prefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700256 InstallPlan install_plan;
Darin Petkovda8c1362011-01-13 14:04:24 -0800257 LOG(INFO) << "Setting up preferences under: " << FLAGS_prefs_dir;
258 LOG_IF(ERROR, !prefs.Init(FilePath(FLAGS_prefs_dir)))
259 << "Failed to initialize preferences.";
260 // Get original checksums
261 LOG(INFO) << "Calculating original checksums";
262 PartitionInfo kern_info, root_info;
263 CHECK(DeltaDiffGenerator::InitializePartitionInfo(true, // is_kernel
264 FLAGS_old_kernel,
265 &kern_info));
266 CHECK(DeltaDiffGenerator::InitializePartitionInfo(false, // is_kernel
267 FLAGS_old_image,
268 &root_info));
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700269 install_plan.kernel_hash.assign(kern_info.hash().begin(),
270 kern_info.hash().end());
271 install_plan.rootfs_hash.assign(root_info.hash().begin(),
272 root_info.hash().end());
Jay Srinivasanf0572052012-10-23 18:12:56 -0700273 DeltaPerformer performer(&prefs, NULL, &install_plan);
Darin Petkovda8c1362011-01-13 14:04:24 -0800274 CHECK_EQ(performer.Open(FLAGS_old_image.c_str(), 0, 0), 0);
275 CHECK(performer.OpenKernel(FLAGS_old_kernel.c_str()));
276 vector<char> buf(1024 * 1024);
277 int fd = open(FLAGS_in_file.c_str(), O_RDONLY, 0);
278 CHECK_GE(fd, 0);
279 ScopedFdCloser fd_closer(&fd);
280 for (off_t offset = 0;; offset += buf.size()) {
281 ssize_t bytes_read;
282 CHECK(utils::PReadAll(fd, &buf[0], buf.size(), offset, &bytes_read));
283 if (bytes_read == 0)
284 break;
285 CHECK_EQ(performer.Write(&buf[0], bytes_read), bytes_read);
286 }
287 CHECK_EQ(performer.Close(), 0);
288 DeltaPerformer::ResetUpdateProgress(&prefs, false);
289 LOG(INFO) << "Done applying delta.";
290}
291
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800292int Main(int argc, char** argv) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700293 google::ParseCommandLineFlags(&argc, &argv, true);
294 CommandLine::Init(argc, argv);
Darin Petkov9c0baf82010-10-07 13:44:48 -0700295 Terminator::Init();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000296 Subprocess::Init();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700297 logging::InitLogging("delta_generator.log",
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800298 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
299 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700300 logging::APPEND_TO_OLD_LOG_FILE,
301 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700302
303 vector<int> signature_sizes;
304 ParseSignatureSizes(FLAGS_signature_size, &signature_sizes);
305
306 if (!FLAGS_out_hash_file.empty() || !FLAGS_out_metadata_hash_file.empty()) {
Jay Srinivasanf4318702012-09-24 11:56:24 -0700307 if (!FLAGS_out_hash_file.empty()) {
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700308 CalculatePayloadHashForSigning(signature_sizes, FLAGS_out_hash_file);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700309 }
310 if (!FLAGS_out_metadata_hash_file.empty()) {
Don Garrettc2e9f5f2013-10-18 16:42:40 -0700311 CalculateMetadataHashForSigning(signature_sizes,
312 FLAGS_out_metadata_hash_file);
Jay Srinivasanf4318702012-09-24 11:56:24 -0700313 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800314 return 0;
315 }
316 if (!FLAGS_signature_file.empty()) {
317 SignPayload();
318 return 0;
319 }
Darin Petkovadb3cef2011-01-13 16:16:08 -0800320 if (!FLAGS_public_key.empty()) {
321 VerifySignedPayload();
322 return 0;
323 }
Darin Petkovda8c1362011-01-13 14:04:24 -0800324 if (!FLAGS_in_file.empty()) {
325 ApplyDelta();
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700326 return 0;
327 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700328 CHECK(!FLAGS_new_image.empty());
329 CHECK(!FLAGS_out_file.empty());
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700330 CHECK(!FLAGS_new_kernel.empty());
Don Garrett0dd39852013-04-03 16:55:42 -0700331
332 bool is_delta = !FLAGS_old_image.empty();
333
334 ImageInfo old_image_info;
335 ImageInfo new_image_info;
336
Don Garrett60fc59c2013-10-18 11:43:52 -0700337 // Ignore failures. These are optional arguments.
Don Garrett0dd39852013-04-03 16:55:42 -0700338 ParseImageInfo(FLAGS_new_channel,
339 FLAGS_new_board,
340 FLAGS_new_version,
341 FLAGS_new_key,
342 FLAGS_new_build_channel,
343 FLAGS_new_build_version,
344 &new_image_info);
345
Don Garrett60fc59c2013-10-18 11:43:52 -0700346 // Ignore failures. These are optional arguments.
347 ParseImageInfo(FLAGS_old_channel,
348 FLAGS_old_board,
349 FLAGS_old_version,
350 FLAGS_old_key,
351 FLAGS_old_build_channel,
352 FLAGS_old_build_version,
353 &old_image_info);
Don Garrett0dd39852013-04-03 16:55:42 -0700354
355 if (is_delta) {
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700356 LOG(INFO) << "Generating delta update";
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700357 CHECK(!FLAGS_old_dir.empty());
358 CHECK(!FLAGS_new_dir.empty());
359 if ((!IsDir(FLAGS_old_dir.c_str())) || (!IsDir(FLAGS_new_dir.c_str()))) {
360 LOG(FATAL) << "old_dir or new_dir not directory";
361 }
Don Garrett0dd39852013-04-03 16:55:42 -0700362 } else {
363 LOG(INFO) << "Generating full update";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000364 }
Don Garrett0dd39852013-04-03 16:55:42 -0700365
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800366 uint64_t metadata_size;
Don Garrett0dd39852013-04-03 16:55:42 -0700367 if (!DeltaDiffGenerator::GenerateDeltaUpdateFile(
368 FLAGS_old_dir,
369 FLAGS_old_image,
370 FLAGS_new_dir,
371 FLAGS_new_image,
372 FLAGS_old_kernel,
373 FLAGS_new_kernel,
374 FLAGS_out_file,
375 FLAGS_private_key,
376 FLAGS_chunk_size,
377 FLAGS_rootfs_partition_size,
378 is_delta ? &old_image_info : NULL,
379 &new_image_info,
380 &metadata_size)) {
Chris Sosac7c19cd2011-02-08 17:02:12 -0800381 return 1;
382 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000383 return 0;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800384}
385
386} // namespace {}
387
388} // namespace chromeos_update_engine
389
390int main(int argc, char** argv) {
391 return chromeos_update_engine::Main(argc, argv);
392}